diff --git a/src/config.h b/src/config.h index fd535138..cf7a684d 100644 --- a/src/config.h +++ b/src/config.h @@ -13,7 +13,7 @@ #define VERSION "0.1.23-master-branch" -#define BUILD 783 +#define BUILD 784 #define HOSTID "mailarchiver" diff --git a/src/imap.c b/src/imap.c index 7d3b172b..d3590593 100644 --- a/src/imap.c +++ b/src/imap.c @@ -191,28 +191,13 @@ int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sda } -int connect_to_imap_server(int sd, int *seq, char *server, char *username, char *password, int port, struct __data *data, int use_ssl){ +int connect_to_imap_server(int sd, int *seq, char *username, char *password, int port, struct __data *data, int use_ssl){ int n; char tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], buf[MAXBUFSIZE]; - unsigned long host=0; - struct sockaddr_in remote_addr; X509* server_cert; char *str; - host = resolve_host(server); - - remote_addr.sin_family = AF_INET; - remote_addr.sin_port = htons(port); - remote_addr.sin_addr.s_addr = host; - bzero(&(remote_addr.sin_zero),8); - - if(connect(sd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1){ - printf("connect()\n"); - return ERR; - } - - if(use_ssl == 1){ SSL_library_init(); diff --git a/src/misc.c b/src/misc.c index 7c9b8bad..14512a42 100644 --- a/src/misc.c +++ b/src/misc.c @@ -533,22 +533,6 @@ void strtolower(char *s){ } -unsigned long resolve_host(char *host){ - struct hostent *h; - struct in_addr addr; - - if(!host) return 0; - - if((addr.s_addr = inet_addr(host)) == -1){ - if((h = gethostbyname(host)) == NULL){ - return 0; - } - else return *(unsigned long*)h->h_addr; - } - else return addr.s_addr; -} - - #ifndef _GNU_SOURCE char *strcasestr(const char *s, const char *find){ char c, sc; diff --git a/src/misc.h b/src/misc.h index bb1769cd..fe93ed22 100644 --- a/src/misc.h +++ b/src/misc.h @@ -39,8 +39,6 @@ void init_session_data(struct session_data *sdata, struct __config *cfg); int read_from_stdin(struct session_data *sdata); void strtolower(char *s); -unsigned long resolve_host(char *host); - #ifndef _GNU_SOURCE char *strcasestr(const char *s, const char *find); #endif diff --git a/src/pilerimport.c b/src/pilerimport.c index 35295a90..f751e735 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -11,6 +11,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -31,10 +34,10 @@ int quiet=0; int remove_after_successful_import = 0; -int connect_to_imap_server(int sd, int *seq, char *server, char *username, char *password, int port, struct __data *data, int use_ssl); +int connect_to_imap_server(int sd, int *seq, char *username, char *password, int port, struct __data *data, int use_ssl); int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, struct __data *data); int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sdata, struct __data *data, int use_ssl, struct __config *cfg); -int connect_to_pop3_server(int sd, char *server, char *username, char *password, int port, struct __data *data, int use_ssl); +int connect_to_pop3_server(int sd, char *username, char *password, int port, struct __data *data, int use_ssl); int process_pop3_emails(int sd, struct session_data *sdata, struct __data *data, int use_ssl, struct __config *cfg); void close_connection(int sd, struct __data *data, int use_ssl); @@ -254,19 +257,39 @@ int import_from_imap_server(char *server, char *username, char *password, int po char *p, puf[SMALLBUFSIZE]; char muf[SMALLBUFSIZE]; char folders[MAXBUFSIZE]; + char port_string[6]; + struct addrinfo hints, *res; + snprintf(port_string, sizeof(port_string)-1, "%d", port); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if((rc = getaddrinfo(server, port_string, &hints, &res)) != 0){ + printf("getaddrinfo for '%s': %s\n", server, gai_strerror(rc)); + return ERR; + } if(port == 993) use_ssl = 1; - if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ + if((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){ printf("cannot create socket\n"); - return ERR; + ret = ERR; + goto ENDE_IMAP; } - if(connect_to_imap_server(sd, &seq, server, username, password, port, data, use_ssl) == ERR){ + if(connect(sd, res->ai_addr, res->ai_addrlen) == -1){ + printf("connect()\n"); + ret = ERR; + goto ENDE_IMAP; + } + + if(connect_to_imap_server(sd, &seq, username, password, port, data, use_ssl) == ERR){ close(sd); - return ERR; + ret = ERR; + goto ENDE_IMAP; } @@ -294,38 +317,64 @@ int import_from_imap_server(char *server, char *username, char *password, int po if(quiet == 0) printf("processing folder: %s... ", puf); - rc = process_imap_folder(sd, &seq, puf, sdata, data, use_ssl, cfg); - if(rc == ERR) ret = ERR; + if(process_imap_folder(sd, &seq, puf, sdata, data, use_ssl, cfg) == ERR) ret = ERR; } while(p); close_connection(sd, data, use_ssl); +ENDE_IMAP: + freeaddrinfo(res); + return ret; } int import_from_pop3_server(char *server, char *username, char *password, int port, struct session_data *sdata, struct __data *data, struct __config *cfg){ - int rc=ERR, ret=OK, sd, use_ssl=0; + int rc, ret=OK, sd, use_ssl=0; + char port_string[6]; + struct addrinfo hints, *res; + + snprintf(port_string, sizeof(port_string)-1, "%d", port); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if((rc = getaddrinfo(server, port_string, &hints, &res)) != 0){ + printf("getaddrinfo for '%s': %s\n", server, gai_strerror(rc)); + return ERR; + } if(port == 995) use_ssl = 1; - if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ + if((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){ printf("cannot create socket\n"); - return ERR; + ret = ERR; + goto ENDE_POP3; } - if(connect_to_pop3_server(sd, server, username, password, port, data, use_ssl) == ERR){ + if(connect(sd, res->ai_addr, res->ai_addrlen) == -1){ + printf("connect()\n"); + ret = ERR; + goto ENDE_POP3; + } + + + if(connect_to_pop3_server(sd, username, password, port, data, use_ssl) == ERR){ close(sd); - return ERR; + ret = ERR; + goto ENDE_POP3; } - rc = process_pop3_emails(sd, sdata, data, use_ssl, cfg); - if(rc == ERR) ret = ERR; + if(process_pop3_emails(sd, sdata, data, use_ssl, cfg) == ERR) ret = ERR; close_connection(sd, data, use_ssl); +ENDE_POP3: + freeaddrinfo(res); + return ret; } diff --git a/src/pop3.c b/src/pop3.c index 0e92310e..fc51f5a3 100644 --- a/src/pop3.c +++ b/src/pop3.c @@ -33,26 +33,12 @@ int is_last_complete_pop3_packet(char *s, int len){ } -int connect_to_pop3_server(int sd, char *server, char *username, char *password, int port, struct __data *data, int use_ssl){ +int connect_to_pop3_server(int sd, char *username, char *password, int port, struct __data *data, int use_ssl){ int n; char buf[MAXBUFSIZE]; - unsigned long host=0; - struct sockaddr_in remote_addr; X509* server_cert; char *str; - host = resolve_host(server); - - remote_addr.sin_family = AF_INET; - remote_addr.sin_port = htons(port); - remote_addr.sin_addr.s_addr = host; - bzero(&(remote_addr.sin_zero),8); - - if(connect(sd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1){ - printf("connect()\n"); - return ERR; - } - if(use_ssl == 1){