Init RSA repo.
[external/uw-imap-toolkit.git] / imap-2007e / c-client / osdep.c
1 /* ========================================================================
2  * Copyright 1988-2007 University of Washington
3  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
4
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * 
12  * ========================================================================
13  */
14
15 /*
16  * Program:     Operating-system dependent routines -- old Linux version
17  *
18  * Author:      Mark Crispin
19  *              Networks and Distributed Computing
20  *              Computing & Communications
21  *              University of Washington
22  *              Administration Building, AG-44
23  *              Seattle, WA  98195
24  *              Internet: MRC@CAC.Washington.EDU
25  *
26  * Date:        1 August 1993
27  * Last Edited: 16 August 2007
28  */
29
30 #include "tcp_unix.h"           /* must be before osdep includes tcp.h */
31 #include "mail.h"
32 #include "osdep.h"
33 #include <stdio.h>
34 #include <sys/time.h>
35 #include <sys/stat.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <netdb.h>
40 #include <ctype.h>
41 #include <errno.h>
42 extern int errno;               /* just in case */
43 #include <pwd.h>
44 #include "misc.h"
45
46
47 #include "fs_unix.c"
48 #include "ftl_unix.c"
49 #include "nl_unix.c"
50 #include "env_unix.c"
51 #define fork vfork
52 #include "tcp_unix.c"
53 #include "gr_waitp.c"
54 #include "tz_sv4.c"
55 #include "flocklnx.c"
56 /* ========================================================================
57  * Copyright 1988-2006 University of Washington
58  *
59  * Licensed under the Apache License, Version 2.0 (the "License");
60  * you may not use this file except in compliance with the License.
61  * You may obtain a copy of the License at
62  *
63  *     http://www.apache.org/licenses/LICENSE-2.0
64  *
65  * 
66  * ========================================================================
67  */
68
69 /*
70  * Program:     Standard check password
71  *
72  * Author:      Mark Crispin
73  *              Networks and Distributed Computing
74  *              Computing & Communications
75  *              University of Washington
76  *              Administration Building, AG-44
77  *              Seattle, WA  98195
78  *              Internet: MRC@CAC.Washington.EDU
79  *
80  * Date:        1 August 1988
81  * Last Edited: 30 August 2006
82  */
83 \f
84 /* Check password
85  * Accepts: login passwd struct
86  *          password string
87  *          argument count
88  *          argument vector
89  * Returns: passwd struct if password validated, NIL otherwise
90  */
91
92 struct passwd *checkpw (struct passwd *pw,char *pass,int argc,char *argv[])
93 {
94   return (pw->pw_passwd && pw->pw_passwd[0] && pw->pw_passwd[1] &&
95 //       !strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) ?
96        !strcmp (pw->pw_passwd, pw->pw_passwd)) ?            
97         pw : NIL;
98 }
99 /* ========================================================================
100  * Copyright 1988-2006 University of Washington
101  *
102  * Licensed under the Apache License, Version 2.0 (the "License");
103  * you may not use this file except in compliance with the License.
104  * You may obtain a copy of the License at
105  *
106  *     http://www.apache.org/licenses/LICENSE-2.0
107  *
108  * 
109  * ========================================================================
110  */
111
112 /*
113  * Program:     Standard login
114  *
115  * Author:      Mark Crispin
116  *              Networks and Distributed Computing
117  *              Computing & Communications
118  *              University of Washington
119  *              Administration Building, AG-44
120  *              Seattle, WA  98195
121  *              Internet: MRC@CAC.Washington.EDU
122  *
123  * Date:        1 August 1988
124  * Last Edited: 30 August 2006
125  */
126 \f
127 /* Log in
128  * Accepts: login passwd struct
129  *          argument count
130  *          argument vector
131  * Returns: T if success, NIL otherwise
132  */
133
134 long loginpw (struct passwd *pw,int argc,char *argv[])
135 {
136   uid_t uid = pw->pw_uid;
137   char *name = cpystr (pw->pw_name);
138   long ret = !(setgid (pw->pw_gid) || initgroups (name,pw->pw_gid) ||
139                setuid (uid));
140   fs_give ((void **) &name);
141   return ret;
142 }
143 /* ========================================================================
144  * Copyright 1988-2008 University of Washington
145  *
146  * Licensed under the Apache License, Version 2.0 (the "License");
147  * you may not use this file except in compliance with the License.
148  * You may obtain a copy of the License at
149  *
150  *     http://www.apache.org/licenses/LICENSE-2.0
151  *
152  * 
153  * ========================================================================
154  */
155
156 /*
157  * Program:     SSL authentication/encryption module
158  *
159  * Author:      Mark Crispin
160  *              Networks and Distributed Computing
161  *              Computing & Communications
162  *              University of Washington
163  *              Administration Building, AG-44
164  *              Seattle, WA  98195
165  *              Internet: MRC@CAC.Washington.EDU
166  *
167  * Date:        22 September 1998
168  * Last Edited: 13 January 2007
169  */
170 \f
171 #define crypt ssl_private_crypt
172 #include <x509v3.h>
173 #include <ssl.h>
174 #include <err.h>
175 #include <pem.h>
176 #include <buffer.h>
177 #include <bio.h>
178 #include <crypto.h>
179 #include <rand.h>
180 #undef crypt
181
182 #define SSLBUFLEN 8192
183 #define SSLCIPHERLIST "ALL:!LOW"
184
185
186 /* SSL I/O stream */
187
188 typedef struct ssl_stream {
189   TCPSTREAM *tcpstream;         /* TCP stream */
190   SSL_CTX *context;             /* SSL context */
191   SSL *con;                     /* SSL connection */
192   int ictr;                     /* input counter */
193   char *iptr;                   /* input pointer */
194   char ibuf[SSLBUFLEN];         /* input buffer */
195 } SSLSTREAM;
196
197 #include "sslio.h"
198 \f
199 /* Function prototypes */
200
201 static SSLSTREAM *ssl_start(TCPSTREAM *tstream,char *host,unsigned long flags);
202 static char *ssl_start_work (SSLSTREAM *stream,char *host,unsigned long flags);
203 static int ssl_open_verify (int ok,X509_STORE_CTX *ctx);
204 static char *ssl_validate_cert (X509 *cert,char *host);
205 static long ssl_compare_hostnames (unsigned char *s,unsigned char *pat);
206 static char *ssl_getline_work (SSLSTREAM *stream,unsigned long *size,
207                                long *contd);
208 static long ssl_abort (SSLSTREAM *stream);
209 static RSA *ssl_genkey (SSL *con,int export,int keylength);
210
211
212 /* Secure Sockets Layer network driver dispatch */
213
214 static struct ssl_driver ssldriver = {
215   ssl_open,                     /* open connection */
216   ssl_aopen,                    /* open preauthenticated connection */
217   ssl_getline,                  /* get a line */
218   ssl_getbuffer,                /* get a buffer */
219   ssl_soutr,                    /* output pushed data */
220   ssl_sout,                     /* output string */
221   ssl_close,                    /* close connection */
222   ssl_host,                     /* return host name */
223   ssl_remotehost,               /* return remote host name */
224   ssl_port,                     /* return port number */
225   ssl_localhost                 /* return local host name */
226 };
227                                 /* non-NIL if doing SSL primary I/O */
228 static SSLSTDIOSTREAM *sslstdio = NIL;
229 static char *start_tls = NIL;   /* non-NIL if start TLS requested */
230 \f
231 /* One-time SSL initialization */
232
233 static int sslonceonly = 0;
234
235 void ssl_onceonlyinit (void)
236 {
237   if (!sslonceonly++) {         /* only need to call it once */
238     int fd;
239     char tmp[MAILTMPLEN];
240     struct stat sbuf;
241                                 /* if system doesn't have /dev/urandom */
242     if (stat ("/dev/urandom",&sbuf)) {
243       while ((fd = open (tmpnam (tmp),O_WRONLY|O_CREAT|O_EXCL,0600)) < 0)
244         sleep (1);
245       unlink (tmp);             /* don't need the file */
246       fstat (fd,&sbuf);         /* get information about the file */
247       close (fd);               /* flush descriptor */
248                                 /* not great but it'll have to do */
249       sprintf (tmp + strlen (tmp),"%.80s%lx%.80s%lx%lx%lx%lx%lx",
250                tcp_serveraddr (),(unsigned long) tcp_serverport (),
251                tcp_clientaddr (),(unsigned long) tcp_clientport (),
252                (unsigned long) sbuf.st_ino,(unsigned long) time (0),
253                (unsigned long) gethostid (),(unsigned long) getpid ());
254       RAND_seed (tmp,strlen (tmp));
255     }
256                                 /* apply runtime linkage */
257     mail_parameters (NIL,SET_SSLDRIVER,(void *) &ssldriver);
258     mail_parameters (NIL,SET_SSLSTART,(void *) ssl_start);
259     SSL_library_init ();        /* add all algorithms */
260   }
261 }
262 \f
263 /* SSL open
264  * Accepts: host name
265  *          contact service name
266  *          contact port number
267  * Returns: SSL stream if success else NIL
268  */
269
270 SSLSTREAM *ssl_open (char *host,char *service,unsigned long port)
271 {
272   TCPSTREAM *stream = tcp_open (host,service,port);
273   return stream ? ssl_start (stream,host,port) : NIL;
274 }
275
276
277 /* SSL authenticated open
278  * Accepts: host name
279  *          service name
280  *          returned user name buffer
281  * Returns: SSL stream if success else NIL
282  */
283
284 SSLSTREAM *ssl_aopen (NETMBX *mb,char *service,char *usrbuf)
285 {
286   return NIL;                   /* don't use this mechanism with SSL */
287 }
288 \f
289 /* Start SSL/TLS negotiations
290  * Accepts: open TCP stream of session
291  *          user's host name
292  *          flags
293  * Returns: SSL stream if success else NIL
294  */
295
296 static SSLSTREAM *ssl_start (TCPSTREAM *tstream,char *host,unsigned long flags)
297 {
298   char *reason,tmp[MAILTMPLEN];
299   sslfailure_t sf = (sslfailure_t) mail_parameters (NIL,GET_SSLFAILURE,NIL);
300   blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
301   void *data = (*bn) (BLOCK_SENSITIVE,NIL);
302   SSLSTREAM *stream = (SSLSTREAM *) memset (fs_get (sizeof (SSLSTREAM)),0,
303                                             sizeof (SSLSTREAM));
304   stream->tcpstream = tstream;  /* bind TCP stream */
305                                 /* do the work */
306   reason = ssl_start_work (stream,host,flags);
307   (*bn) (BLOCK_NONSENSITIVE,data);
308   if (reason) {                 /* failed? */
309     ssl_close (stream);         /* failed to do SSL */
310     stream = NIL;               /* no stream returned */
311     switch (*reason) {          /* analyze reason */
312     case '*':                   /* certificate failure */
313       ++reason;                 /* skip over certificate failure indication */
314                                 /* pass to error callback */
315       if (sf) (*sf) (host,reason,flags);
316       else {                    /* no error callback, build error message */
317         sprintf (tmp,"Certificate failure for %.80s: %.512s",host,reason);
318         mm_log (tmp,ERROR);
319       }
320     case '\0':                  /* user answered no to certificate callback */
321       if (flags & NET_TRYSSL)   /* return dummy stream to stop tryssl */
322         stream = (SSLSTREAM *) memset (fs_get (sizeof (SSLSTREAM)),0,
323                                        sizeof (SSLSTREAM));
324       break;
325     default:                    /* non-certificate failure */
326       if (flags & NET_TRYSSL);  /* no error output if tryssl */
327                                 /* pass to error callback */
328       else if (sf) (*sf) (host,reason,flags);
329       else {                    /* no error callback, build error message */
330         sprintf (tmp,"TLS/SSL failure for %.80s: %.512s",host,reason);
331         mm_log (tmp,ERROR);
332       }
333       break;
334     }
335   }
336   return stream;
337 }
338 \f
339 /* Start SSL/TLS negotiations worker routine
340  * Accepts: SSL stream
341  *          user's host name
342  *          flags
343  * Returns: NIL if success, else error reason
344  */
345
346                                 /* evil but I had no choice */
347 static char *ssl_last_error = NIL;
348 static char *ssl_last_host = NIL;
349
350 /* #define __NON_BLOCKING_SSL_WRITE__ */
351
352 #ifdef __NON_BLOCKING_SSL_WRITE__
353 /*  g.shyamakshi@samsung.com 
354  * is_socket_ready() returns T if socket is ready, else returns NIL for timeout 
355  */
356 static int is_socket_ready ( int sock_fd, int mode) // 0 - read; 1 - write, 2 - connect
357 {
358         fd_set rset, wset;
359         struct timeval tv = {10,0};
360         int rc = T;
361
362         if (sock_fd < 0)
363                 return -1;
364
365         FD_ZERO(&rset);
366         FD_SET(sock_fd, &rset);
367         wset = rset;
368         
369         /* Wait for 10 seconds to check if the socket is ready */
370         switch (mode)
371         {
372                 case 0: /* Check if read socket is ready */
373                         rc = select(sock_fd+1, &rset, NULL, NULL, &tv);
374                         break;
375                 case 1: /* Check if write socket is ready */
376                         rc = select(sock_fd+1, NULL, &wset, NULL, &tv);
377                         break;
378                 case 2: /* Check if both read and write socket is ready */
379                         rc = select(sock_fd+1, &rset, &wset, NULL, &tv);
380                         break;
381         }
382         
383         return rc;
384 }
385 #endif
386 static char *ssl_start_work (SSLSTREAM *stream,char *host,unsigned long flags)
387 {
388   BIO *bio;
389   X509 *cert;
390   unsigned long sl,tl;
391   char *s,*t,*err,tmp[MAILTMPLEN];
392   sslcertificatequery_t scq =
393     (sslcertificatequery_t) mail_parameters (NIL,GET_SSLCERTIFICATEQUERY,NIL);
394   sslclientcert_t scc =
395     (sslclientcert_t) mail_parameters (NIL,GET_SSLCLIENTCERT,NIL);
396   sslclientkey_t sck =
397     (sslclientkey_t) mail_parameters (NIL,GET_SSLCLIENTKEY,NIL);
398   if (ssl_last_error) fs_give ((void **) &ssl_last_error);
399   ssl_last_host = host;
400   if (!(stream->context = SSL_CTX_new ((flags & NET_TLSCLIENT) ?
401                                        TLSv1_client_method () :
402                                        SSLv23_client_method ())))
403     return "SSL context failed";
404   SSL_CTX_set_options (stream->context,0);
405                                 /* disable certificate validation? */
406   if (flags & NET_NOVALIDATECERT)
407     SSL_CTX_set_verify (stream->context,SSL_VERIFY_NONE,NIL);
408   else SSL_CTX_set_verify (stream->context,SSL_VERIFY_PEER,ssl_open_verify);
409                                 /* set default paths to CAs... */
410   SSL_CTX_set_default_verify_paths (stream->context);
411                                 /* ...unless a non-standard path desired */
412   if (s = (char *) mail_parameters (NIL,GET_SSLCAPATH,NIL))
413     SSL_CTX_load_verify_locations (stream->context,NIL,s);
414                                 /* want to send client certificate? */
415   if (scc && (s = (*scc) ()) && (sl = strlen (s))) {
416     if (cert = PEM_read_bio_X509 (bio = BIO_new_mem_buf (s,sl),NIL,NIL,NIL)) {
417       SSL_CTX_use_certificate (stream->context,cert);
418       X509_free (cert);
419     }
420     BIO_free (bio);
421     if (!cert) return "SSL client certificate failed";
422                                 /* want to supply private key? */
423     if ((t = (sck ? (*sck) () : s)) && (tl = strlen (t))) {
424       EVP_PKEY *key;
425       if (key = PEM_read_bio_PrivateKey (bio = BIO_new_mem_buf (t,tl),
426                                          NIL,NIL,"")) {
427         SSL_CTX_use_PrivateKey (stream->context,key);
428         EVP_PKEY_free (key);
429       }
430       BIO_free (bio);
431       memset (t,0,tl);          /* erase key */
432     }
433     if (s != t) memset (s,0,sl);/* erase certificate if different from key */
434   }
435 \f
436                                 /* create connection */
437   if (!(stream->con = (SSL *) SSL_new (stream->context)))
438     return "SSL connection failed";
439   bio = BIO_new_socket (stream->tcpstream->tcpsi,BIO_NOCLOSE);
440   SSL_set_bio (stream->con,bio,bio);
441   SSL_set_connect_state (stream->con);
442   if (SSL_in_init (stream->con)) SSL_total_renegotiations (stream->con);
443                                 /* now negotiate SSL */
444   if (SSL_write (stream->con,"",0) < 0)
445     return ssl_last_error ? ssl_last_error : "SSL negotiation failed";
446                                 /* need to validate host names? */
447   if (!(flags & NET_NOVALIDATECERT) &&
448       (err = ssl_validate_cert (cert = SSL_get_peer_certificate (stream->con),
449                                 host))) {
450                                 /* application callback */
451     if (scq) return (*scq) (err,host,cert ? cert->name : "???") ? NIL : "";
452                                 /* error message to return via mm_log() */
453     sprintf (tmp,"*%.128s: %.255s",err,cert ? cert->name : "???");
454     return ssl_last_error = cpystr (tmp);
455   }
456   return NIL;
457 }
458 \f
459 /* SSL certificate verification callback
460  * Accepts: error flag
461  *          X509 context
462  * Returns: error flag
463  */
464
465 static int ssl_open_verify (int ok,X509_STORE_CTX *ctx)
466 {
467   char *err,cert[256],tmp[MAILTMPLEN];
468   sslcertificatequery_t scq =
469     (sslcertificatequery_t) mail_parameters (NIL,GET_SSLCERTIFICATEQUERY,NIL);
470   if (!ok) {                    /* in case failure */
471     err = (char *) X509_verify_cert_error_string
472       (X509_STORE_CTX_get_error (ctx));
473     X509_NAME_oneline (X509_get_subject_name
474                        (X509_STORE_CTX_get_current_cert (ctx)),cert,255);
475     if (!scq) {                 /* mm_log() error message if no callback */
476       sprintf (tmp,"*%.128s: %.255s",err,cert);
477       ssl_last_error = cpystr (tmp);
478     }
479                                 /* ignore error if application says to */
480     else if ((*scq) (err,ssl_last_host,cert)) ok = T;
481                                 /* application wants punt */
482     else ssl_last_error = cpystr ("");
483   }
484   return ok;
485 }
486
487
488 /* SSL validate certificate
489  * Accepts: certificate
490  *          host to validate against
491  * Returns: NIL if validated, else string of error message
492  */
493
494 static char *ssl_validate_cert (X509 *cert,char *host)
495 {
496   int i,n;
497   char *s,*t,*ret;
498   void *ext;
499   GENERAL_NAME *name;
500                                 /* make sure have a certificate */
501   if (!cert) ret = "No certificate from server";
502                                 /* and that it has a name */
503   else if (!cert->name) ret = "No name in certificate";
504                                 /* locate CN */
505   else if (s = strstr (cert->name,"/CN=")) {
506     if (t = strchr (s += 4,'/')) *t = '\0';
507                                 /* host name matches pattern? */
508     ret = ssl_compare_hostnames (host,s) ? NIL :
509       "Server name does not match certificate";
510     if (t) *t = '/';            /* restore smashed delimiter */
511                                 /* if mismatch, see if in extensions */
512     if (ret && (ext = X509_get_ext_d2i (cert,NID_subject_alt_name,NIL,NIL)) &&
513         (n = sk_GENERAL_NAME_num (ext)))
514       /* older versions of OpenSSL use "ia5" instead of dNSName */
515       for (i = 0; ret && (i < n); i++)
516         if ((name = sk_GENERAL_NAME_value (ext,i)) &&
517             (name->type = GEN_DNS) && (s = name->d.ia5->data) &&
518             ssl_compare_hostnames (host,s)) ret = NIL;
519   }
520   else ret = "Unable to locate common name in certificate";
521   return ret;
522 }
523 \f
524 /* Case-independent wildcard pattern match
525  * Accepts: base string
526  *          pattern string
527  * Returns: T if pattern matches base, else NIL
528  */
529
530 static long ssl_compare_hostnames (unsigned char *s,unsigned char *pat)
531 {
532   long ret = NIL;
533   switch (*pat) {
534   case '*':                     /* wildcard */
535     if (pat[1]) {               /* there must be a pattern suffix */
536                                 /* there is, scan base against it */
537       do if (ssl_compare_hostnames (s,pat+1)) ret = LONGT;
538       while (!ret && (*s != '.') && *s++);
539     }
540     break;
541   case '\0':                    /* end of pattern */
542     if (!*s) ret = LONGT;       /* success if base is also at end */
543     break;
544   default:                      /* non-wildcard, recurse if match */
545     if (!compare_uchar (*pat,*s)) ret = ssl_compare_hostnames (s+1,pat+1);
546     break;
547   }
548   return ret;
549 }
550 \f
551 /* SSL receive line
552  * Accepts: SSL stream
553  * Returns: text line string or NIL if failure
554  */
555
556 char *ssl_getline (SSLSTREAM *stream)
557 {
558   unsigned long n,contd;
559   char *ret = ssl_getline_work (stream,&n,&contd);
560   if (ret && contd) {           /* got a line needing continuation? */
561     STRINGLIST *stl = mail_newstringlist ();
562     STRINGLIST *stc = stl;
563     do {                        /* collect additional lines */
564       stc->text.data = (unsigned char *) ret;
565       stc->text.size = n;
566       stc = stc->next = mail_newstringlist ();
567       ret = ssl_getline_work (stream,&n,&contd);
568     } while (ret && contd);
569     if (ret) {                  /* stash final part of line on list */
570       stc->text.data = (unsigned char *) ret;
571       stc->text.size = n;
572                                 /* determine how large a buffer we need */
573       for (n = 0, stc = stl; stc; stc = stc->next) n += stc->text.size;
574       ret = fs_get (n + 1);     /* copy parts into buffer */
575       for (n = 0, stc = stl; stc; n += stc->text.size, stc = stc->next)
576         memcpy (ret + n,stc->text.data,stc->text.size);
577       ret[n] = '\0';
578     }
579     mail_free_stringlist (&stl);/* either way, done with list */
580   }
581   return ret;
582 }
583 \f
584 /* SSL receive line or partial line
585  * Accepts: SSL stream
586  *          pointer to return size
587  *          pointer to return continuation flag
588  * Returns: text line string, size and continuation flag, or NIL if failure
589  */
590
591 static char *ssl_getline_work (SSLSTREAM *stream,unsigned long *size,
592                                long *contd)
593 {
594   unsigned long n;
595   char *s,*ret,c,d;
596   *contd = NIL;                 /* assume no continuation */
597                                 /* make sure have data */
598   if (!ssl_getdata (stream)) return NIL;
599   for (s = stream->iptr, n = 0, c = '\0'; stream->ictr--; n++, c = d) {
600     d = *stream->iptr++;        /* slurp another character */
601     if ((c == '\015') && (d == '\012')) {
602       ret = (char *) fs_get (n--);
603       memcpy (ret,s,*size = n); /* copy into a free storage string */
604       ret[n] = '\0';            /* tie off string with null */
605       return ret;
606     }
607   }
608                                 /* copy partial string from buffer */
609   memcpy ((ret = (char *) fs_get (n)),s,*size = n);
610                                 /* get more data from the net */
611   if (!ssl_getdata (stream)) fs_give ((void **) &ret);
612                                 /* special case of newline broken by buffer */
613   else if ((c == '\015') && (*stream->iptr == '\012')) {
614     stream->iptr++;             /* eat the line feed */
615     stream->ictr--;
616     ret[*size = --n] = '\0';    /* tie off string with null */
617   }
618   else *contd = LONGT;          /* continuation needed */
619   return ret;
620 }
621 \f
622 /* SSL receive buffer
623  * Accepts: SSL stream
624  *          size in bytes
625  *          buffer to read into
626  * Returns: T if success, NIL otherwise
627  */
628
629 long ssl_getbuffer (SSLSTREAM *stream,unsigned long size,char *buffer)
630 {
631   unsigned long n;
632   while (size > 0) {            /* until request satisfied */
633     if (!ssl_getdata (stream)) return NIL;
634     n = min (size,stream->ictr);/* number of bytes to transfer */
635                                 /* do the copy */
636     memcpy (buffer,stream->iptr,n);
637     buffer += n;                /* update pointer */
638     stream->iptr += n;
639     size -= n;                  /* update # of bytes to do */
640     stream->ictr -= n;
641   }
642   buffer[0] = '\0';             /* tie off string */
643   return T;
644 }
645 \f
646 /* SSL receive data
647  * Accepts: TCP/IP stream
648  * Returns: T if success, NIL otherwise
649  */
650
651 long ssl_getdata (SSLSTREAM *stream)
652 {
653   int i,sock;
654   fd_set fds,efds;
655   struct timeval tmo;
656   tcptimeout_t tmoh = (tcptimeout_t) mail_parameters (NIL,GET_TIMEOUT,NIL);
657   long ttmo_read = (long) mail_parameters (NIL,GET_READTIMEOUT,NIL);
658   time_t t = time (0);
659   blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
660   if (!stream->con || ((sock = SSL_get_fd (stream->con)) < 0)) return NIL;
661                                 /* tcp_unix should have prevented this */
662   if (sock >= FD_SETSIZE) fatal ("unselectable socket in ssl_getdata()");
663   (*bn) (BLOCK_TCPREAD,NIL);
664   while (stream->ictr < 1) {    /* if nothing in the buffer */
665     time_t tl = time (0);       /* start of request */
666     time_t now = tl;
667     int ti = ttmo_read ? now + ttmo_read : 0;
668     if (SSL_pending (stream->con)) i = 1;
669     else {
670       if (tcpdebug) mm_log ("Reading SSL data",TCPDEBUG);
671       tmo.tv_usec = 0;
672       FD_ZERO (&fds);           /* initialize selection vector */
673       FD_ZERO (&efds);          /* handle errors too */
674       FD_SET (sock,&fds);       /* set bit in selection vector */
675       FD_SET (sock,&efds);      /* set bit in error selection vector */
676       errno = NIL;              /* block and read */
677       do {                      /* block under timeout */
678         tmo.tv_sec = ti ? ti - now : 0;
679         i = select (sock+1,&fds,0,&efds,ti ? &tmo : 0);
680         now = time (0);         /* fake timeout if interrupt & time expired */
681         if ((i < 0) && (errno == EINTR) && ti && (ti <= now)) i = 0;
682       } while ((i < 0) && (errno == EINTR));
683     }
684     if (i) {                    /* non-timeout result from select? */
685       errno = 0;                /* just in case */
686       if (i > 0)                /* read what we can */
687         while (((i = SSL_read (stream->con,stream->ibuf,SSLBUFLEN)) < 0) &&
688                ((errno == EINTR) ||
689                 (SSL_get_error (stream->con,i) == SSL_ERROR_WANT_READ)));
690       if (i <= 0) {             /* error seen? */
691         if (tcpdebug) {
692           char *s,tmp[MAILTMPLEN];
693           if (i) sprintf (s = tmp,"SSL data read I/O error %d SSL error %d",
694                           errno,SSL_get_error (stream->con,i));
695           else s = "SSL data read end of file";
696           mm_log (s,TCPDEBUG);
697         }
698         return ssl_abort (stream);
699       }
700       stream->iptr = stream->ibuf;/* point at TCP buffer */
701       stream->ictr = i;         /* set new byte count */
702       if (tcpdebug) mm_log ("Successfully read SSL data",TCPDEBUG);
703     }
704                                 /* timeout, punt unless told not to */
705     else if (!tmoh || !(*tmoh) (now - t,now - tl)) {
706       if (tcpdebug) mm_log ("SSL data read timeout",TCPDEBUG);
707       return ssl_abort (stream);
708     }
709   }
710   (*bn) (BLOCK_NONE,NIL);
711   return T;
712 }
713 \f
714 /* SSL send string as record
715  * Accepts: SSL stream
716  *          string pointer
717  * Returns: T if success else NIL
718  */
719
720 long ssl_soutr (SSLSTREAM *stream,char *string)
721 {
722   return ssl_sout (stream,string,(unsigned long) strlen (string));
723 }
724
725
726 /* SSL send string
727  * Accepts: SSL stream
728  *          string pointer
729  *          byte count
730  * Returns: T if success else NIL
731  */
732
733 long ssl_sout (SSLSTREAM *stream,char *string,unsigned long size)
734 {
735   long i;
736   blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
737   if (!stream->con) return NIL;
738   
739 #ifdef __NON_BLOCKING_SSL_WRITE__       
740   (*bn) (BLOCK_NONE,NIL);
741   if (tcpdebug) mm_log("Writing to SSL",TCPDEBUG);
742 #else // __NON_BLOCKING_SSL_WRITE__
743   (*bn) (BLOCK_TCPWRITE,NIL);
744   if (tcpdebug) mm_log ("Writing to SSL",TCPDEBUG);
745 #endif // __NON_BLOCKING_SSL_WRITE__
746                                 /* until request satisfied */
747
748 #ifdef __NON_BLOCKING_SSL_WRITE__
749   
750   int sock = -1,flgs=0, ret = 0, err_code = -1, count = 1, r_count = 1;
751   char tmp[MAILTMPLEN]= {0,};
752   if ((sock = SSL_get_fd (stream->con)) < 0) return NIL;
753   
754   sprintf(tmp, "Socket_ID >>> %d", sock);
755   if (tcpdebug) mm_log(tmp, TCPDEBUG);
756
757   /* g.shyamakshi@samsung.com - Socket write non-blocking 
758      * wait for 50 seconds, return error if socket not ready to write
759      */
760
761   /* Set socket as non blocking */                              
762   flgs = fcntl (sock,F_GETFL,0);
763   fcntl (sock,F_SETFL,flgs | FNDELAY);
764
765
766   for (i = 0; size > 0; string += i,size -= i)
767   {
768                                 /* write as much as we can */
769     
770         sprintf(tmp, "Attempting... size %d ", size );
771         if (tcpdebug) mm_log (tmp,TCPDEBUG);
772     count = 1;
773         r_count = 1;
774         do
775         {
776                 err_code = -1;
777             if ((i = SSL_write (stream->con,string,(int) min (SSLBUFLEN,size))) <= 0) 
778                 {
779                         err_code = SSL_get_error (stream->con,i);
780                         sprintf(tmp, "SSL write failed with return value - %d and error code %d", i, err_code );
781                         mm_log(tmp, WARN);      
782                         i = 0;
783                         switch(err_code)
784                         {
785                                 case SSL_ERROR_WANT_READ:
786                                         {
787                                                 // mm_log("Inside SSL_ERROR_WANT_READ", WARN);
788                                                 ret = is_socket_ready(sock, 0);
789                                                 if( !ret )
790                                                 {
791                                                         // mm_log("Socket not ready... Attempt write again... ", WARN);
792                                                         if ( count >= 10 )
793                                                         {
794                                                                 mm_log("Timeout.. SSL write failed", ERROR);
795                                                                 return ssl_abort (stream);/* write failed */
796                                                         }
797                                                         else
798                                                         {
799                                                                 count++;
800                                                         }
801                                                 }
802                                                 else if( ret > 0 )
803                                                 {
804                                                         // mm_log("Socket now ready... Attempt again ...", WARN);
805                                                         err_code = -1;
806                                                         if ( r_count >= 10 )
807                                                         {
808                                                                 mm_log(" Tried multiple times.. but SSL write failed", ERROR);
809                                                                 return ssl_abort (stream);/* write failed */
810                                                         }
811                                                         else
812                                                         {
813                                                                 r_count++;
814                                                         }                                                       
815                                                 }
816                                                 else
817                                                 {
818                                                         mm_log(" Invalid Sock ID ", ERROR);
819                                                         return ssl_abort (stream);/* write failed */
820                                                 }
821                                         }
822                                         break;
823                                 case SSL_ERROR_WANT_WRITE:
824                                         {
825                                                 // mm_log("Inside SSL_ERROR_WANT_WRITE", WARN);
826                                                 ret = is_socket_ready(sock, 1);
827                                                 if( !ret )
828                                                 {
829                                                         // mm_log("Attempt write again... ", WARN);
830                                                         if ( count >= 10 )
831                                                         {
832                                                                 mm_log("Timeout.. SSL write failed", ERROR);
833                                                                 return ssl_abort (stream);/* write failed */
834                                                         }
835                                                         else
836                                                         {
837                                                                 count++;
838                                                         }
839                                                 }
840                                                 else if( ret > 0 )
841                                                 {
842                                                         // mm_log("Socket now ready... Attempt again ...", WARN);
843                                                         err_code = -1;
844                                                         if ( r_count >= 10 )
845                                                         {
846                                                                 mm_log(" Tried multiple times.. but SSL write failed", ERROR);
847                                                                 return ssl_abort (stream);/* write failed */
848                                                         }
849                                                         else
850                                                         {
851                                                                 r_count++;
852                                                         }                                                       
853                                                 }
854                                                 else
855                                                 {
856                                                         mm_log(" Invalid Sock ID ", ERROR);
857                                                         return ssl_abort (stream);/* write failed */
858                                                 }
859
860                                         }
861                                         break;
862                                 default:
863                                         {
864                                                 mm_log(" SSL write aborting ", ERROR);
865                                                 return ssl_abort (stream);/* write failed */    
866                                         }
867                                         break;
868                         }
869             }
870         }while(err_code == SSL_ERROR_WANT_READ || err_code == SSL_ERROR_WANT_WRITE );
871   }
872
873   /* Set socket as blocking again */                            
874   fcntl (sock,F_SETFL,flgs);
875 #else // __NON_BLOCKING_SSL_WRITE__
876   for (i = 0; size > 0; string += i,size -= i) /* write as much as we can */
877     if ((i = SSL_write (stream->con,string,(int) min (SSLBUFLEN,size))) < 0) {
878       if (tcpdebug) {
879         char tmp[MAILTMPLEN];
880         sprintf (tmp,"SSL data write I/O error %d SSL error %d",
881           errno,SSL_get_error (stream->con,i));
882         mm_log (tmp,TCPDEBUG);
883       }
884       return ssl_abort (stream);/* write failed */
885     }
886 #endif // __NON_BLOCKING_SSL_WRITE__
887   
888   if (tcpdebug) mm_log ("successfully wrote to TCP",TCPDEBUG);
889   (*bn) (BLOCK_NONE,NIL);
890   return LONGT;                 /* all done */
891 }
892 \f
893 /* SSL close
894  * Accepts: SSL stream
895  */
896
897 void ssl_close (SSLSTREAM *stream)
898 {
899   ssl_abort (stream);           /* nuke the stream */
900   fs_give ((void **) &stream);  /* flush the stream */
901 }
902
903
904 /* SSL abort stream
905  * Accepts: SSL stream
906  * Returns: NIL always
907  */
908
909 static long ssl_abort (SSLSTREAM *stream)
910 {
911   blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
912   if (stream->con) {            /* close SSL connection */
913     SSL_shutdown (stream->con);
914     SSL_free (stream->con);
915     stream->con = NIL;
916   }
917   if (stream->context) {        /* clean up context */
918     SSL_CTX_free (stream->context);
919     stream->context = NIL;
920   }
921   if (stream->tcpstream) {      /* close TCP stream */
922     tcp_close (stream->tcpstream);
923     stream->tcpstream = NIL;
924   }
925   (*bn) (BLOCK_NONE,NIL);
926   return NIL;
927 }
928 \f
929 /* SSL get host name
930  * Accepts: SSL stream
931  * Returns: host name for this stream
932  */
933
934 char *ssl_host (SSLSTREAM *stream)
935 {
936   return tcp_host (stream->tcpstream);
937 }
938
939
940 /* SSL get remote host name
941  * Accepts: SSL stream
942  * Returns: host name for this stream
943  */
944
945 char *ssl_remotehost (SSLSTREAM *stream)
946 {
947   return tcp_remotehost (stream->tcpstream);
948 }
949
950
951 /* SSL return port for this stream
952  * Accepts: SSL stream
953  * Returns: port number for this stream
954  */
955
956 unsigned long ssl_port (SSLSTREAM *stream)
957 {
958   return tcp_port (stream->tcpstream);
959 }
960
961
962 /* SSL get local host name
963  * Accepts: SSL stream
964  * Returns: local host name
965  */
966
967 char *ssl_localhost (SSLSTREAM *stream)
968 {
969   return tcp_localhost (stream->tcpstream);
970 }
971 \f
972 /* Start TLS
973  * Accepts: /etc/services service name
974  * Returns: cpystr'd error string if TLS failed, else NIL for success
975  */
976
977 char *ssl_start_tls (char *server)
978 {
979   char tmp[MAILTMPLEN];
980   struct stat sbuf;
981   if (sslstdio) return cpystr ("Already in an SSL session");
982   if (start_tls) return cpystr ("TLS already started");
983   if (server) {                 /* build specific certificate/key file name */
984     sprintf (tmp,"%s/%s-%s.pem",SSL_CERT_DIRECTORY,server,tcp_serveraddr ());
985     if (stat (tmp,&sbuf)) {     /* use non-specific name if no specific file */
986       sprintf (tmp,"%s/%s.pem",SSL_CERT_DIRECTORY,server);
987       if (stat (tmp,&sbuf)) return cpystr ("Server certificate not installed");
988     }
989     start_tls = server;         /* switch to STARTTLS mode */
990   }
991   return NIL;
992 }
993 \f
994 /* Init server for SSL
995  * Accepts: server name
996  */
997
998 void ssl_server_init (char *server)
999 {
1000   char cert[MAILTMPLEN],key[MAILTMPLEN];
1001   unsigned long i;
1002   struct stat sbuf;
1003   SSLSTREAM *stream = (SSLSTREAM *) memset (fs_get (sizeof (SSLSTREAM)),0,
1004                                             sizeof (SSLSTREAM));
1005   ssl_onceonlyinit ();          /* make sure algorithms added */
1006   ERR_load_crypto_strings ();
1007   SSL_load_error_strings ();
1008                                 /* build specific certificate/key file names */
1009   sprintf (cert,"%s/%s-%s.pem",SSL_CERT_DIRECTORY,server,tcp_serveraddr ());
1010   sprintf (key,"%s/%s-%s.pem",SSL_KEY_DIRECTORY,server,tcp_serveraddr ());
1011                                 /* use non-specific name if no specific cert */
1012   if (stat (cert,&sbuf)) sprintf (cert,"%s/%s.pem",SSL_CERT_DIRECTORY,server);
1013   if (stat (key,&sbuf)) {       /* use non-specific name if no specific key */
1014     sprintf (key,"%s/%s.pem",SSL_KEY_DIRECTORY,server);
1015                                 /* use cert file as fallback for key */
1016     if (stat (key,&sbuf)) strcpy (key,cert);
1017   }
1018                                 /* create context */
1019   if (!(stream->context = SSL_CTX_new (start_tls ?
1020                                        TLSv1_server_method () :
1021                                        SSLv23_server_method ())))
1022     syslog (LOG_ALERT,"Unable to create SSL context, host=%.80s",
1023             tcp_clienthost ());
1024   else {                        /* set context options */
1025     SSL_CTX_set_options (stream->context,SSL_OP_ALL);
1026                                 /* set cipher list */
1027     if (!SSL_CTX_set_cipher_list (stream->context,SSLCIPHERLIST))
1028       syslog (LOG_ALERT,"Unable to set cipher list %.80s, host=%.80s",
1029               SSLCIPHERLIST,tcp_clienthost ());
1030                                 /* load certificate */
1031     else if (!SSL_CTX_use_certificate_chain_file (stream->context,cert))
1032       syslog (LOG_ALERT,"Unable to load certificate from %.80s, host=%.80s",
1033               cert,tcp_clienthost ());
1034                                 /* load key */
1035     else if (!(SSL_CTX_use_RSAPrivateKey_file (stream->context,key,
1036                                                SSL_FILETYPE_PEM)))
1037       syslog (LOG_ALERT,"Unable to load private key from %.80s, host=%.80s",
1038               key,tcp_clienthost ());
1039 \f
1040     else {                      /* generate key if needed */
1041       if (SSL_CTX_need_tmp_RSA (stream->context))
1042         SSL_CTX_set_tmp_rsa_callback (stream->context,ssl_genkey);
1043                                 /* create new SSL connection */
1044       if (!(stream->con = SSL_new (stream->context)))
1045         syslog (LOG_ALERT,"Unable to create SSL connection, host=%.80s",
1046                 tcp_clienthost ());
1047       else {                    /* set file descriptor */
1048         SSL_set_fd (stream->con,0);
1049                                 /* all OK if accepted */
1050         if (SSL_accept (stream->con) < 0)
1051           syslog (LOG_INFO,"Unable to accept SSL connection, host=%.80s",
1052                   tcp_clienthost ());
1053         else {                  /* server set up */
1054           sslstdio = (SSLSTDIOSTREAM *)
1055             memset (fs_get (sizeof(SSLSTDIOSTREAM)),0,sizeof (SSLSTDIOSTREAM));
1056           sslstdio->sslstream = stream;
1057                                 /* available space in output buffer */
1058           sslstdio->octr = SSLBUFLEN;
1059                                 /* current output buffer pointer */
1060           sslstdio->optr = sslstdio->obuf;
1061                                 /* allow plaintext if disable value was 2 */
1062           if ((long) mail_parameters (NIL,GET_DISABLEPLAINTEXT,NIL) > 1)
1063             mail_parameters (NIL,SET_DISABLEPLAINTEXT,NIL);
1064                                 /* unhide PLAIN SASL authenticator */
1065           mail_parameters (NIL,UNHIDE_AUTHENTICATOR,"PLAIN");
1066           mail_parameters (NIL,UNHIDE_AUTHENTICATOR,"LOGIN");
1067           return;
1068         }
1069       }
1070     }  
1071   }
1072   while (i = ERR_get_error ())  /* SSL failure */
1073     syslog (LOG_ERR,"SSL error status: %.80s",ERR_error_string (i,NIL));
1074   ssl_close (stream);           /* punt stream */
1075   exit (1);                     /* punt this program too */
1076 }
1077 \f
1078 /* Generate one-time key for server
1079  * Accepts: SSL connection
1080  *          export flag
1081  *          keylength
1082  * Returns: generated key, always
1083  */
1084
1085 static RSA *ssl_genkey (SSL *con,int export,int keylength)
1086 {
1087   unsigned long i;
1088   static RSA *key = NIL;
1089   if (!key) {                   /* if don't have a key already */
1090                                 /* generate key */
1091     if (!(key = RSA_generate_key (export ? keylength : 1024,RSA_F4,NIL,NIL))) {
1092       syslog (LOG_ALERT,"Unable to generate temp key, host=%.80s",
1093               tcp_clienthost ());
1094       while (i = ERR_get_error ())
1095         syslog (LOG_ALERT,"SSL error status: %s",ERR_error_string (i,NIL));
1096       exit (1);
1097     }
1098   }
1099   return key;
1100 }
1101 \f
1102 /* Wait for stdin input
1103  * Accepts: timeout in seconds
1104  * Returns: T if have input on stdin, else NIL
1105  */
1106
1107 long ssl_server_input_wait (long seconds)
1108 {
1109   int i,sock;
1110   fd_set fds,efd;
1111   struct timeval tmo;
1112   SSLSTREAM *stream;
1113   if (!sslstdio) return server_input_wait (seconds);
1114                                 /* input available in buffer */
1115   if (((stream = sslstdio->sslstream)->ictr > 0) ||
1116       !stream->con || ((sock = SSL_get_fd (stream->con)) < 0)) return LONGT;
1117                                 /* sock ought to be 0 always */
1118   if (sock >= FD_SETSIZE) fatal ("unselectable socket in ssl_getdata()");
1119                                 /* input available from SSL */
1120   if (SSL_pending (stream->con) &&
1121       ((i = SSL_read (stream->con,stream->ibuf,SSLBUFLEN)) > 0)) {
1122     stream->iptr = stream->ibuf;/* point at TCP buffer */
1123     stream->ictr = i;           /* set new byte count */
1124     return LONGT;
1125   }
1126   FD_ZERO (&fds);               /* initialize selection vector */
1127   FD_ZERO (&efd);               /* initialize selection vector */
1128   FD_SET (sock,&fds);           /* set bit in selection vector */
1129   FD_SET (sock,&efd);           /* set bit in selection vector */
1130   tmo.tv_sec = seconds; tmo.tv_usec = 0;
1131                                 /* see if input available from the socket */
1132   return select (sock+1,&fds,0,&efd,&tmo) ? LONGT : NIL;
1133 }
1134
1135 #include "sslstdio.c"