piler/src/imap.c

474 lines
12 KiB
C
Raw Normal View History

/*
* imap.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <openssl/ssl.h>
2012-10-27 14:40:25 +02:00
#include <openssl/err.h>
#include <fcntl.h>
#include <ctype.h>
#include <syslog.h>
#include <unistd.h>
#include <limits.h>
#include <piler.h>
2013-09-05 22:38:03 +02:00
void update_import_job_stat(struct session_data *sdata, struct __data *data);
2014-07-08 15:16:23 +02:00
int get_message_length_from_imap_answer(char *s){
char *p, *q;
int len=0;
2014-07-08 15:16:23 +02:00
p = strstr(s, "\r");
if(!p){
printf("invalid reply: %s", s);
return len;
}
*p = '\0';
if(*(p-1) == '}') *(p-1) = '\0';
q = strchr(s, '{');
if(q){
q++;
len = atoi(q);
}
*(p-1) = '}';
*p = '\r';
return len;
}
2013-09-25 22:08:38 +02:00
int read_response(int sd, char *buf, int buflen, int *seq, struct __data *data, int use_ssl){
int i=0, n, len=0, rc=0;
2014-12-15 22:20:53 +01:00
char puf[MAXBUFSIZE], tagok[SMALLBUFSIZE], tagno[SMALLBUFSIZE], tagbad[SMALLBUFSIZE];
2013-09-25 22:08:38 +02:00
2013-09-25 22:43:37 +02:00
snprintf(tagok, sizeof(tagok)-1, "A%d OK", *seq);
2013-09-25 22:08:38 +02:00
snprintf(tagno, sizeof(tagno)-1, "A%d NO", *seq);
2014-12-15 22:20:53 +01:00
snprintf(tagbad, sizeof(tagbad)-1, "A%d BAD", *seq);
2013-07-27 22:31:55 +02:00
memset(buf, 0, buflen);
while(!strstr(buf, tagok)){
n = recvtimeoutssl(sd, puf, sizeof(puf), 10, use_ssl, data->ssl);
2013-09-25 22:08:38 +02:00
2013-07-27 22:31:55 +02:00
if(n + len < buflen) strncat(buf, puf, n);
2013-09-25 22:08:38 +02:00
else goto END;
2014-12-15 22:20:53 +01:00
/*
* possible error message from the imap server:
*
* * BYE Temporary problem, please try again later\r\n
*/
if(i == 0 && (strstr(puf, tagno) || strstr(puf, tagbad) || strstr(puf, "* BYE ")) ) goto END;
2013-09-25 22:08:38 +02:00
2013-07-27 22:31:55 +02:00
len += n;
2013-09-25 22:08:38 +02:00
i++;
2013-07-27 22:31:55 +02:00
}
2013-09-25 22:08:38 +02:00
rc = 1;
END:
(*seq)++;
return rc;
2013-07-27 22:31:55 +02:00
}
2013-08-23 13:02:51 +02:00
int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sdata, struct __data *data, int use_ssl, int dryrun, struct __config *cfg){
2015-04-09 21:26:55 +02:00
int rc=ERR, i, n, messages=0, len, readlen, fd, nreads, readpos, finished, msglen, msg_written_len, tagoklen, tagbadlen, result;
char *p, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], tagbad[SMALLBUFSIZE], buf[MAXBUFSIZE], puf[MAXBUFSIZE], filename[SMALLBUFSIZE];
2013-07-27 22:31:55 +02:00
/* imap cmd: SELECT */
2015-04-09 21:26:55 +02:00
snprintf(buf, sizeof(buf)-1, "A%d SELECT %s\r\n", *seq, folder);
2013-04-09 14:50:27 +02:00
n = write1(sd, buf, strlen(buf), use_ssl, data->ssl);
2013-09-25 22:08:38 +02:00
if(read_response(sd, buf, sizeof(buf), seq, data, use_ssl) == 0){
trimBuffer(buf);
2015-05-22 11:36:36 +02:00
printf("select cmd error: %s\n", buf);
return rc;
}
p = strstr(buf, " EXISTS");
if(p){
*p = '\0';
2013-01-10 22:30:30 +01:00
p = strrchr(buf, ' ');
if(p){
while(!isdigit(*p)){ p++; }
messages = atoi(p);
}
}
printf("found %d messages\n", messages);
2013-09-04 23:42:07 +02:00
if(messages <= 0) return OK;
data->import->total_messages += messages;
2014-10-15 09:36:09 +02:00
for(i=data->import->start_position; i<=messages; i++){
2013-09-04 23:42:07 +02:00
data->import->processed_messages++;
2014-10-15 22:51:50 +02:00
if(data->quiet == 0){ printf("processed: %7d [%3d%%]\r", data->import->processed_messages, 100*i/messages); fflush(stdout); }
snprintf(tag, sizeof(tag)-1, "A%d", *seq);
2014-07-08 15:16:23 +02:00
snprintf(tagok, sizeof(tagok)-1, "A%d OK", (*seq)++);
snprintf(tagbad, sizeof(tagbad)-1, "%s BAD", tag);
tagoklen = strlen(tagok);
tagbadlen = strlen(tagbad);
snprintf(buf, sizeof(buf)-1, "%s FETCH %d (BODY.PEEK[])\r\n", tag, i);
2014-02-09 20:51:18 +01:00
snprintf(filename, sizeof(filename)-1, "%d-imap-%d.txt", getpid(), data->import->processed_messages);
unlink(filename);
fd = open(filename, O_CREAT|O_EXCL|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
if(fd == -1){
printf("cannot open: %s\n", filename);
return rc;
}
2013-04-09 14:50:27 +02:00
n = write1(sd, buf, strlen(buf), use_ssl, data->ssl);
readlen = 0;
nreads = 0;
2014-07-08 15:16:23 +02:00
readpos = 0;
finished = 0;
msglen = 0;
msg_written_len = 0;
while((n = recvtimeoutssl(sd, &buf[readpos], sizeof(buf)-readpos, 60, use_ssl, data->ssl)) > 0){
readlen += n;
2014-07-08 15:16:23 +02:00
if(strchr(buf, '\n')){
readpos = 0;
p = &buf[0];
do {
nreads++;
memset(puf, 0, sizeof(puf));
2014-07-22 16:00:03 +02:00
p = split(p, '\n', puf, sizeof(puf)-1, &result);
2014-07-08 15:16:23 +02:00
len = strlen(puf);
2014-07-22 16:00:03 +02:00
if(result == 1){
// process a complete line
2014-07-16 12:39:54 +02:00
2014-07-22 16:00:03 +02:00
if(nreads == 1){
2014-07-16 12:39:54 +02:00
2014-07-22 16:00:03 +02:00
if(strcasestr(puf, " FETCH ")){
msglen = get_message_length_from_imap_answer(puf);
if(msglen == 0){
finished = 1;
break;
}
continue;
2014-07-16 12:39:54 +02:00
}
2014-07-22 16:00:03 +02:00
if(strcasestr(puf, " BYE")){
printf("imap server sent BYE response: '%s'\n", puf);
close(fd);
unlink(filename);
return ERR;
}
2014-07-16 12:39:54 +02:00
}
2014-07-22 16:00:03 +02:00
if(len > 0 && msg_written_len < msglen){
2014-07-08 15:16:23 +02:00
write(fd, puf, len);
write(fd, "\n", 1);
msg_written_len += len + 1;
}
2014-07-22 16:00:03 +02:00
if(strncmp(puf, tagok, tagoklen) == 0){
finished = 1;
break;
}
if(strncmp(puf, tagbad, tagbadlen) == 0){
printf("ERROR happened reading the message!\n");
finished = 1;
break;
}
2014-07-08 15:16:23 +02:00
}
2014-07-22 16:00:03 +02:00
else {
// prepend the last incomplete line back to 'buf'
2014-07-08 15:16:23 +02:00
2014-07-22 16:00:03 +02:00
snprintf(buf, sizeof(buf)-2, "%s", puf);
readpos = len;
2014-07-08 15:16:23 +02:00
break;
}
} while(p);
2014-07-22 16:00:03 +02:00
}
2012-10-27 14:40:25 +02:00
else {
2014-07-08 15:16:23 +02:00
readpos += n;
2012-10-27 14:40:25 +02:00
}
2012-08-09 00:08:49 +02:00
2014-07-08 15:16:23 +02:00
if(finished == 1) break;
}
2012-08-09 00:08:49 +02:00
close(fd);
if(dryrun == 0 && msglen > 10){
rc = import_message(filename, sdata, data, cfg);
2013-09-05 22:38:03 +02:00
if(data->import->processed_messages % 100 == 0){
time(&(data->import->updated));
update_import_job_stat(sdata, data);
}
2013-09-05 22:38:03 +02:00
}
else rc = OK;
2013-09-05 22:38:03 +02:00
if(rc == ERR) printf("error importing '%s'\n", filename);
else {
if(data->import->remove_after_import == 1){
snprintf(buf, sizeof(buf)-1, "A%d STORE %d +FLAGS.SILENT (\\Deleted)\r\n", *seq, i);
n = write1(sd, buf, strlen(buf), use_ssl, data->ssl);
read_response(sd, buf, sizeof(buf), seq, data, use_ssl);
}
2014-10-15 23:22:36 +02:00
if(data->import->download_only == 0) unlink(filename);
}
}
if(data->import->remove_after_import == 1){
snprintf(buf, sizeof(buf)-1, "A%d EXPUNGE\r\n", *seq);
n = write1(sd, buf, strlen(buf), use_ssl, data->ssl);
read_response(sd, buf, sizeof(buf), seq, data, use_ssl);
}
printf("\n");
return OK;
}
int connect_to_imap_server(int sd, int *seq, char *username, char *password, int port, struct __data *data, int use_ssl){
int n;
2013-09-25 22:08:38 +02:00
char buf[MAXBUFSIZE];
2012-10-27 14:40:25 +02:00
X509* server_cert;
char *str;
2012-10-27 14:40:25 +02:00
if(use_ssl == 1){
2012-10-28 20:36:46 +01:00
SSL_library_init();
2012-10-27 14:40:25 +02:00
SSL_load_error_strings();
2014-10-15 23:22:36 +02:00
data->ctx = SSL_CTX_new(TLSv1_client_method());
2012-10-27 14:40:25 +02:00
CHK_NULL(data->ctx, "internal SSL error");
data->ssl = SSL_new(data->ctx);
CHK_NULL(data->ssl, "internal ssl error");
SSL_set_fd(data->ssl, sd);
n = SSL_connect(data->ssl);
CHK_SSL(n, "internal ssl error");
2014-10-15 23:22:36 +02:00
printf("Cipher: %s\n", SSL_get_cipher(data->ssl));
2012-10-27 14:40:25 +02:00
server_cert = SSL_get_peer_certificate(data->ssl);
CHK_NULL(server_cert, "server cert error");
//if(verbose){
str = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0);
CHK_NULL(str, "error in server cert");
printf("server cert:\n\t subject: %s\n", str);
OPENSSL_free(str);
str = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0);
CHK_NULL(str, "error in server cert");
printf("\t issuer: %s\n\n", str);
OPENSSL_free(str);
//}
X509_free(server_cert);
}
n = recvtimeoutssl(sd, buf, sizeof(buf), 10, use_ssl, data->ssl);
2013-07-27 22:31:55 +02:00
/* imap cmd: CAPABILITY */
2013-09-25 22:08:38 +02:00
/*snprintf(buf, sizeof(buf)-1, "A%d CAPABILITY\r\n", *seq);
2013-04-09 14:50:27 +02:00
write1(sd, buf, strlen(buf), use_ssl, data->ssl);
2013-09-25 22:08:38 +02:00
read_response(sd, buf, sizeof(buf), seq, data, use_ssl);*/
2013-07-27 22:31:55 +02:00
2013-07-27 22:31:55 +02:00
/* imap cmd: LOGIN */
2013-09-25 22:08:38 +02:00
snprintf(buf, sizeof(buf)-1, "A%d LOGIN %s \"%s\"\r\n", *seq, username, password);
2012-10-27 14:40:25 +02:00
2013-04-09 14:50:27 +02:00
write1(sd, buf, strlen(buf), use_ssl, data->ssl);
2013-09-25 22:08:38 +02:00
if(read_response(sd, buf, sizeof(buf), seq, data, use_ssl) == 0){
printf("login failed, server reponse: %s\n", buf);
return ERR;
}
return OK;
}
2013-09-25 22:08:38 +02:00
void send_imap_close(int sd, int *seq, struct __data *data, int use_ssl){
char puf[SMALLBUFSIZE];
snprintf(puf, sizeof(puf)-1, "A%d CLOSE\r\n", *seq);
write1(sd, puf, strlen(puf), use_ssl, data->ssl);
}
2012-10-27 14:40:25 +02:00
void close_connection(int sd, struct __data *data, int use_ssl){
close(sd);
2013-09-25 22:08:38 +02:00
2012-10-27 14:40:25 +02:00
if(use_ssl == 1){
SSL_shutdown(data->ssl);
SSL_free(data->ssl);
SSL_CTX_free(data->ctx);
2012-10-28 20:36:46 +01:00
ERR_free_strings();
2012-10-27 14:40:25 +02:00
}
}
2013-09-04 22:42:06 +02:00
int list_folders(int sd, int *seq, int use_ssl, struct __data *data){
2014-02-09 20:51:18 +01:00
char *p, *q, *r, *buf, *ruf, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], puf[MAXBUFSIZE];
2014-07-22 16:00:03 +02:00
int len=MAXBUFSIZE+3, pos=0, n, rc=ERR, fldrlen=0, result;
2013-01-10 22:30:30 +01:00
2013-09-12 09:40:20 +02:00
printf("List of IMAP folders:\n");
2013-09-04 22:42:06 +02:00
buf = malloc(len);
if(!buf) return rc;
memset(buf, 0, len);
snprintf(tag, sizeof(tag)-1, "A%d", *seq); snprintf(tagok, sizeof(tagok)-1, "A%d OK", (*seq)++);
2013-01-10 22:30:30 +01:00
//snprintf(puf, sizeof(puf)-1, "%s LIST \"\" %%\r\n", tag);
snprintf(puf, sizeof(puf)-1, "%s LIST \"\" \"*\"\r\n", tag);
2013-04-09 14:50:27 +02:00
write1(sd, puf, strlen(puf), use_ssl, data->ssl);
2013-01-10 22:30:30 +01:00
while(1){
n = recvtimeoutssl(sd, puf, sizeof(puf), 10, use_ssl, data->ssl);
2014-10-15 09:36:09 +02:00
if(n < 0) return ERR;
2013-09-04 22:42:06 +02:00
if(pos + n >= len){
q = realloc(buf, len+MAXBUFSIZE+1);
if(!q){
printf("realloc failure: %d bytes\n", pos+MAXBUFSIZE+1);
goto ENDE_FOLDERS;
}
buf = q;
memset(buf+pos, 0, MAXBUFSIZE+1);
len += MAXBUFSIZE+1;
2013-01-10 22:30:30 +01:00
}
2013-09-04 22:42:06 +02:00
memcpy(buf + pos, puf, n);
pos += n;
2013-01-10 22:30:30 +01:00
if(strstr(buf, tagok)) break;
}
2013-09-04 22:42:06 +02:00
2015-04-09 21:26:55 +02:00
2013-09-04 22:42:06 +02:00
p = buf;
do {
memset(puf, 0, sizeof(puf));
2014-07-22 16:00:03 +02:00
p = split(p, '\n', puf, sizeof(puf)-1, &result);
trimBuffer(puf);
2014-02-09 20:51:18 +01:00
if(strncmp(puf, "* LIST ", 7) == 0 || fldrlen){
2013-01-10 22:30:30 +01:00
2014-02-09 20:51:18 +01:00
if (fldrlen)
q = puf;
else
q = strstr(puf, ") \"");
if(q){
2014-02-09 20:51:18 +01:00
if (!fldrlen) {
q += 3;
while(*q != '"') q++;
q++;
if(*q == ' ') q++;
}
2013-09-12 09:40:20 +02:00
2014-02-09 20:51:18 +01:00
if(!fldrlen && *q == '{' && q[strlen(q)-1] == '}') {
q++;
fldrlen = strtol(q, NULL, 10);
} else {
2015-04-09 21:26:55 +02:00
//if(*q == '"') q++;
2014-10-15 09:36:09 +02:00
2015-04-09 21:26:55 +02:00
//if(q[strlen(q)-1] == ' ') q[strlen(q)-1] = '\0';
//if(q[strlen(q)-1] == '"') q[strlen(q)-1] = '\0';
2014-02-09 20:51:18 +01:00
if(fldrlen) {
ruf = malloc(strlen(q) * 2 + 1);
memset(ruf, 0, strlen(q) * 2 + 1);
memcpy(ruf, q, strlen(q));
r = ruf;
while(*r != '\0') {
if(*r == '\\') {
memmove(r + 1, r, strlen(r));
r++;
}
r++;
}
addnode(data->imapfolders, ruf);
printf("=> '%s'\n", ruf);
free(ruf);
fldrlen = 0;
} else {
addnode(data->imapfolders, q);
printf("=> '%s'\n", q);
}
}
2013-09-12 09:40:20 +02:00
}
}
else {
if(strncmp(puf, tagok, strlen(tagok)) == 0) {}
}
} while(p);
2013-09-04 22:42:06 +02:00
rc = OK;
ENDE_FOLDERS:
free(buf);
return rc;
}