Removing code duplicates from imap.c and pop3.c

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2018-11-03 14:31:02 +01:00
parent 400f9f2ca4
commit ab488283ec
4 changed files with 45 additions and 73 deletions

View File

@ -91,47 +91,12 @@ END:
int connect_to_imap_server(struct data *data){ int connect_to_imap_server(struct data *data){
int n;
char buf[MAXBUFSIZE]; char buf[MAXBUFSIZE];
X509* server_cert;
char *str;
data->import->cap_uidplus = 0; data->import->cap_uidplus = 0;
if(data->net->use_ssl == 1){ if(data->net->use_ssl == 1){
SSL_library_init(); init_ssl_to_server(data);
SSL_load_error_strings();
#if OPENSSL_VERSION_NUMBER < 0x10100000L
data->net->ctx = SSL_CTX_new(TLSv1_client_method());
#else
data->net->ctx = SSL_CTX_new(TLS_client_method());
#endif
CHK_NULL(data->net->ctx, "internal SSL error");
data->net->ssl = SSL_new(data->net->ctx);
CHK_NULL(data->net->ssl, "internal ssl error");
SSL_set_fd(data->net->ssl, data->net->socket);
n = SSL_connect(data->net->ssl);
CHK_SSL(n, "internal ssl error");
printf("Cipher: %s\n", SSL_get_cipher(data->net->ssl));
server_cert = SSL_get_peer_certificate(data->net->ssl);
CHK_NULL(server_cert, "server cert error");
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);
} }

View File

@ -730,6 +730,47 @@ int read_one_line(char *s, int c, char *buf, int buflen, int *rc){
} }
int init_ssl_to_server(struct data *data){
int n;
X509* server_cert;
char *str;
SSL_library_init();
SSL_load_error_strings();
#if OPENSSL_VERSION_NUMBER < 0x10100000L
data->net->ctx = SSL_CTX_new(TLSv1_client_method());
#else
data->net->ctx = SSL_CTX_new(TLS_client_method());
#endif
CHK_NULL(data->net->ctx, "internal SSL error");
data->net->ssl = SSL_new(data->net->ctx);
CHK_NULL(data->net->ssl, "internal ssl error");
SSL_set_fd(data->net->ssl, data->net->socket);
n = SSL_connect(data->net->ssl);
CHK_SSL(n, "internal ssl error");
printf("Cipher: %s\n", SSL_get_cipher(data->net->ssl));
server_cert = SSL_get_peer_certificate(data->net->ssl);
CHK_NULL(server_cert, "server cert error");
str = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0);
CHK_NULL(str, "error in server cert");
OPENSSL_free(str);
str = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0);
CHK_NULL(str, "error in server cert");
OPENSSL_free(str);
X509_free(server_cert);
return OK;
}
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
char *strcasestr(const char *s, const char *find){ char *strcasestr(const char *s, const char *find){
char c, sc; char c, sc;

View File

@ -51,6 +51,8 @@ int can_i_write_directory(char *dir);
void move_email(struct smtp_session *session); void move_email(struct smtp_session *session);
int read_one_line(char *s, int c, char *buf, int buflen, int *rc); int read_one_line(char *s, int c, char *buf, int buflen, int *rc);
int init_ssl_to_server(struct data *data);
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
char *strcasestr(const char *s, const char *find); char *strcasestr(const char *s, const char *find);
#endif #endif

View File

@ -34,48 +34,12 @@ int is_last_complete_pop3_packet(char *s, int len){
int connect_to_pop3_server(struct data *data){ int connect_to_pop3_server(struct data *data){
int n;
char buf[MAXBUFSIZE]; char buf[MAXBUFSIZE];
X509* server_cert;
char *str;
if(data->net->use_ssl == 1){ if(data->net->use_ssl == 1){
init_ssl_to_server(data);
SSL_library_init();
SSL_load_error_strings();
#if OPENSSL_VERSION_NUMBER < 0x10100000L
data->net->ctx = SSL_CTX_new(TLSv1_client_method());
#else
data->net->ctx = SSL_CTX_new(TLS_client_method());
#endif
CHK_NULL(data->net->ctx, "internal SSL error");
data->net->ssl = SSL_new(data->net->ctx);
CHK_NULL(data->net->ssl, "internal ssl error");
SSL_set_fd(data->net->ssl, data->net->socket);
n = SSL_connect(data->net->ssl);
CHK_SSL(n, "internal ssl error");
printf("Cipher: %s\n", SSL_get_cipher(data->net->ssl));
server_cert = SSL_get_peer_certificate(data->net->ssl);
CHK_NULL(server_cert, "server cert error");
str = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0);
CHK_NULL(str, "error in server cert");
OPENSSL_free(str);
str = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0);
CHK_NULL(str, "error in server cert");
OPENSSL_free(str);
X509_free(server_cert);
} }
recvtimeoutssl(data->net, buf, sizeof(buf)); recvtimeoutssl(data->net, buf, sizeof(buf));