/[imapfilter]/imapfilter/socket.c
ViewVC logotype

Diff of /imapfilter/socket.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.19 by lefcha, Fri Jul 26 14:42:01 2002 UTC revision 1.20 by lefcha, Mon Jul 29 01:30:51 2002 UTC
# Line 24  int sockpri = -1;              /* Main socket used m Line 24  int sockpri = -1;              /* Main socket used m
24  int sockaux = -1;               /* Auxiliary socked used when another  int sockaux = -1;               /* Auxiliary socked used when another
25                                     connection is needed. */                                     connection is needed. */
26  #ifdef SSL_TLS  #ifdef SSL_TLS
27  static SSL *ssl;  static SSL *sslpri = NULL;
28    static SSL *sslaux = NULL;
29  #endif  #endif
30    
31    
# Line 36  int init_connection(int *sock, char *ser Line 37  int init_connection(int *sock, char *ser
37  {  {
38      struct sockaddr_in sa;      struct sockaddr_in sa;
39      struct hostent *he;      struct hostent *he;
40    #ifdef SSL_TLS
41  #ifndef SSL_TLS      SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux);
42    #else
43      if (protocol != SSL_DISABLED) {      if (protocol != SSL_DISABLED) {
44          error("imapfilter: SSL not supported by this build\n");          error("imapfilter: SSL not supported by this build\n");
45          return ERROR_SSL;          return ERROR_SSL;
# Line 77  int init_connection(int *sock, char *ser Line 79  int init_connection(int *sock, char *ser
79          if (!ssl_init(sock, protocol)) {          if (!ssl_init(sock, protocol)) {
80              if (sock == &sockpri)              if (sock == &sockpri)
81                  info("Connected to %s using %s.\n", serv,                  info("Connected to %s using %s.\n", serv,
82                       SSL_get_cipher(ssl));                       SSL_get_cipher(*ssl));
83              return 0;              return 0;
84          } else          } else
85              return ERROR_SSL;              return ERROR_SSL;
86      else      else
87          ssl = NULL;          *ssl = NULL;
88  #endif  #endif
89    
90      if (sock == &sockpri)      if (sock == &sockpri)
# Line 106  int ssl_init(int *sock, unsigned int pro Line 108  int ssl_init(int *sock, unsigned int pro
108      char *c;      char *c;
109      EVP_MD *evp;      EVP_MD *evp;
110      unsigned char digest[EVP_MAX_MD_SIZE];      unsigned char digest[EVP_MAX_MD_SIZE];
111        SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux);
112    
113      SSL_library_init();      SSL_library_init();
114    
# Line 124  int ssl_init(int *sock, unsigned int pro Line 127  int ssl_init(int *sock, unsigned int pro
127      if (!(ctx = SSL_CTX_new(method)))      if (!(ctx = SSL_CTX_new(method)))
128          return ERROR_SSL;          return ERROR_SSL;
129    
130      if (!(ssl = SSL_new(ctx)))      if (!(*ssl = SSL_new(ctx)))
131          return ERROR_SSL;          return ERROR_SSL;
132    
133      SSL_set_fd(ssl, *sock);      SSL_set_fd(*ssl, *sock);
134    
135      e = SSL_connect(ssl);      e = SSL_connect(*ssl);
136    
137      if (e < 0) {      if (e < 0) {
138          error("imapfilter: initiating SSL connection; %s",          error("imapfilter: initiating SSL connection; %s",
# Line 137  int ssl_init(int *sock, unsigned int pro Line 140  int ssl_init(int *sock, unsigned int pro
140          return ERROR_SSL;          return ERROR_SSL;
141      }      }
142      /* Get server's certificate. */      /* Get server's certificate. */
143      if (!(cert = SSL_get_peer_certificate(ssl)))      if (!(cert = SSL_get_peer_certificate(*ssl)))
144          return ERROR_SSL;          return ERROR_SSL;
145    
146      if (!(c = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0)))      if (!(c = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0)))
# Line 174  int ssl_init(int *sock, unsigned int pro Line 177  int ssl_init(int *sock, unsigned int pro
177  int close_connection(int *sock)  int close_connection(int *sock)
178  {  {
179  #ifdef SSL_TLS  #ifdef SSL_TLS
180      if (ssl) {      SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux);
181          SSL_shutdown(ssl);  
182          SSL_free(ssl);      if (*ssl) {
183            SSL_shutdown(*ssl);
184            SSL_free(*ssl);
185            *ssl = NULL;
186      }      }
187  #endif  #endif
188    
# Line 199  int socket_read(int *sock, char *buf) Line 205  int socket_read(int *sock, char *buf)
205      fd_set fds;      fd_set fds;
206      struct timeval tv;      struct timeval tv;
207      struct timeval *tvp = NULL;      struct timeval *tvp = NULL;
208    #ifdef SSL_TLS
209        SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux);
210    #endif
211    
212      r = 0;      r = 0;
213      s = 1;      s = 1;
# Line 217  int socket_read(int *sock, char *buf) Line 226  int socket_read(int *sock, char *buf)
226      FD_SET(*sock, &fds);      FD_SET(*sock, &fds);
227    
228  #ifdef SSL_TLS  #ifdef SSL_TLS
229      if (ssl) {      if (*ssl) {
230          if (SSL_pending(ssl)          if (SSL_pending(*ssl)
231              || ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0              || ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0
232                  && FD_ISSET(*sock, &fds)))                  && FD_ISSET(*sock, &fds)))
233              r = SSL_read(ssl, buf, RESPONSE_BUF - 1);              r = SSL_read(*ssl, buf, RESPONSE_BUF - 1);
234      } else      } else
235  #endif  #endif
236          if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0          if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0
# Line 238  int socket_read(int *sock, char *buf) Line 247  int socket_read(int *sock, char *buf)
247                "imapfilter: timeout period expired while waiting data\n");                "imapfilter: timeout period expired while waiting data\n");
248    
249  #ifdef SSL_TLS  #ifdef SSL_TLS
250      if (ssl) {      if (*ssl) {
251          if (r < 0)          if (r < 0)
252              fatal(ERROR_NETWORK, "imapfilter: reading data; %s",              fatal(ERROR_NETWORK, "imapfilter: reading data; %s",
253                    ERR_error_string(r, NULL));                    ERR_error_string(r, NULL));
# Line 259  int socket_write(int *sock, char *data) Line 268  int socket_write(int *sock, char *data)
268  {  {
269  #ifdef SSL_TLS  #ifdef SSL_TLS
270      int e;      int e;
271        SSL **ssl = (sock == &sockpri ? &sslpri : &sslaux);
272    
273      if (ssl) {      if (*ssl) {
274          e = SSL_write(ssl, data, strlen(data));          e = SSL_write(*ssl, data, strlen(data));
275          if (e <= 0)          if (e <= 0)
276              fatal(ERROR_NETWORK,              fatal(ERROR_NETWORK,
277                    "imapfilter: sending data; %s",                    "imapfilter: sending data; %s",

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.20

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26