1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4 
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 
9 /* This module provides TLS (aka SSL) support for Exim. The code for OpenSSL is
10 based on a patch that was originally contributed by Steve Haslam. It was
11 adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is
12 based on a patch contributed by Nikos Mavrogiannopoulos. Because these packages
13 are so very different, the functions for each are kept in separate files. The
14 relevant file is #included as required, after any any common functions.
15 
16 No cryptographic code is included in Exim. All this module does is to call
17 functions from the OpenSSL or GNU TLS libraries. */
18 
19 
20 #include "exim.h"
21 #include "transports/smtp.h"
22 
23 #if !defined(DISABLE_TLS) && !defined(USE_OPENSSL) && !defined(USE_GNUTLS)
24 # error One of USE_OPENSSL or USE_GNUTLS must be defined for a TLS build
25 #endif
26 
27 
28 #if defined(MACRO_PREDEF) && !defined(DISABLE_TLS)
29 # include "macro_predef.h"
30 # ifdef USE_GNUTLS
31 #  include "tls-gnu.c"
32 # else
33 #  include "tls-openssl.c"
34 # endif
35 #endif
36 
37 #ifndef MACRO_PREDEF
38 
39 static void tls_per_lib_daemon_init(void);
40 static void tls_per_lib_daemon_tick(void);
41 static unsigned  tls_server_creds_init(void);
42 static void tls_server_creds_invalidate(void);
43 static void tls_client_creds_init(transport_instance *, BOOL);
44 static void tls_client_creds_invalidate(transport_instance *);
45 static void tls_daemon_creds_reload(void);
46 static BOOL opt_set_and_noexpand(const uschar *);
47 static BOOL opt_unset_or_noexpand(const uschar *);
48 
49 
50 
51 /* This module is compiled only when it is specifically requested in the
52 build-time configuration. However, some compilers don't like compiling empty
53 modules, so keep them happy with a dummy when skipping the rest. Make it
54 reference itself to stop picky compilers complaining that it is unused, and put
55 in a dummy argument to stop even pickier compilers complaining about infinite
56 loops. */
57 
58 #ifdef DISABLE_TLS
dummy(int x)59 static void dummy(int x) { dummy(x-1); }
60 #else	/* most of the rest of the file */
61 
62 const exim_tlslib_state	null_tls_preload = {0};
63 
64 /* Static variables that are used for buffering data by both sets of
65 functions and the common functions below.
66 
67 We're moving away from this; GnuTLS is already using a state, which
68 can switch, so we can do TLS callouts during ACLs. */
69 
70 static const int ssl_xfer_buffer_size = 4096;
71 #ifdef USE_OPENSSL
72 static uschar *ssl_xfer_buffer = NULL;
73 static int ssl_xfer_buffer_lwm = 0;
74 static int ssl_xfer_buffer_hwm = 0;
75 static int ssl_xfer_eof = FALSE;
76 static BOOL ssl_xfer_error = FALSE;
77 #endif
78 
79 #ifdef EXIM_HAVE_KEVENT
80 # define KEV_SIZE 16	/* Eight file,dir pairs */
81 static struct kevent kev[KEV_SIZE];
82 static int kev_used = 0;
83 #endif
84 
85 static unsigned tls_creds_expire = 0;
86 
87 /*************************************************
88 *       Expand string; give error on failure     *
89 *************************************************/
90 
91 /* If expansion is forced to fail, set the result NULL and return TRUE.
92 Other failures return FALSE. For a server, an SMTP response is given.
93 
94 Arguments:
95   s         the string to expand; if NULL just return TRUE
96   name      name of string being expanded (for error)
97   result    where to put the result
98 
99 Returns:    TRUE if OK; result may still be NULL after forced failure
100 */
101 
102 static BOOL
expand_check(const uschar * s,const uschar * name,uschar ** result,uschar ** errstr)103 expand_check(const uschar *s, const uschar *name, uschar **result, uschar ** errstr)
104 {
105 if (!s)
106   *result = NULL;
107 else if (  !(*result = expand_string(US s)) /* need to clean up const more */
108 	&& !f.expand_string_forcedfail
109 	)
110   {
111   *errstr = US"Internal error";
112   log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
113     expand_string_message);
114   return FALSE;
115   }
116 return TRUE;
117 }
118 
119 
120 #if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
121 /* Add the directory for a filename to the inotify handle, creating that if
122 needed.  This is enough to see changes to files in that dir.
123 Return boolean success.
124 
125 The word "system" fails, which is on the safe side as we don't know what
126 directory it implies nor if the TLS library handles a watch for us.
127 
128 The string "system,cache" is recognised and explicitly accepted without
129 setting a watch.  This permits the system CA bundle to be cached even though
130 we have no way to tell when it gets modified by an update.
131 The call chain for OpenSSL uses a (undocumented) call into the library
132 to discover the actual file.  We don't know what GnuTLS uses.
133 
134 A full set of caching including the CAs takes 35ms output off of the
135 server tls_init() (GnuTLS, Fedora 32, 2018-class x86_64 laptop hardware).
136 */
137 static BOOL
tls_set_one_watch(const uschar * filename)138 tls_set_one_watch(const uschar * filename)
139 # ifdef EXIM_HAVE_INOTIFY
140 {
141 uschar * s;
142 
143 if (Ustrcmp(filename, "system,cache") == 0) return TRUE;
144 
145 if (!(s = Ustrrchr(filename, '/'))) return FALSE;
146 s = string_copyn(filename, s - filename);	/* mem released by tls_set_watch */
147 DEBUG(D_tls) debug_printf("watch dir '%s'\n", s);
148 
149 /*XXX unclear what effect symlinked files will have for inotify */
150 
151 if (inotify_add_watch(tls_watch_fd, CCS s,
152       IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE | IN_DELETE_SELF
153       | IN_MOVED_FROM | IN_MOVED_TO | IN_MOVE_SELF) >= 0)
154   return TRUE;
155 DEBUG(D_tls) debug_printf("notify_add_watch: %s\n", strerror(errno));
156 return FALSE;
157 }
158 # endif
159 # ifdef EXIM_HAVE_KEVENT
160 {
161 uschar * s, * t;
162 int fd1, fd2, i, j, cnt = 0;
163 struct stat sb;
164 #ifdef OpenBSD
165 struct kevent k_dummy;
166 struct timespec ts = {0};
167 #endif
168 
169 errno = 0;
170 if (Ustrcmp(filename, "system,cache") == 0) return TRUE;
171 
172 for (;;)
173   {
174   if (kev_used > KEV_SIZE-2) { s = US"out of kev space"; goto bad; }
175   if (!(s = Ustrrchr(filename, '/'))) return FALSE;
176   s = string_copyn(filename, s - filename);	/* mem released by tls_set_watch */
177 
178   /* The dir open will fail if there is a symlink on the path. Fine; it's too
179   much effort to handle all possible cases; just refuse the preload. */
180 
181   if ((fd2 = open(CCS s, O_RDONLY | O_NOFOLLOW)) < 0) { s = US"open dir"; goto bad; }
182 
183   if ((lstat(CCS filename, &sb)) < 0) { s = US"lstat"; goto bad; }
184   if (!S_ISLNK(sb.st_mode))
185     {
186     if ((fd1 = open(CCS filename, O_RDONLY | O_NOFOLLOW)) < 0)
187       { s = US"open file"; goto bad; }
188     DEBUG(D_tls) debug_printf("watch file '%s'\n", filename);
189     EV_SET(&kev[++kev_used],
190 	(uintptr_t)fd1,
191 	EVFILT_VNODE,
192 	EV_ADD | EV_ENABLE | EV_ONESHOT,
193 	NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND
194 	| NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE,
195 	0,
196 	NULL);
197     cnt++;
198     }
199   DEBUG(D_tls) debug_printf("watch dir  '%s'\n", s);
200   EV_SET(&kev[++kev_used],
201 	(uintptr_t)fd2,
202 	EVFILT_VNODE,
203 	EV_ADD | EV_ENABLE | EV_ONESHOT,
204 	NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND
205 	| NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE,
206 	0,
207 	NULL);
208   cnt++;
209 
210   if (!(S_ISLNK(sb.st_mode))) break;
211 
212   t = store_get(1024, FALSE);
213   Ustrncpy(t, s, 1022);
214   j = Ustrlen(s);
215   t[j++] = '/';
216   if ((i = readlink(CCS filename, (void *)(t+j), 1023-j)) < 0) { s = US"readlink"; goto bad; }
217   filename = t;
218   *(t += i+j) = '\0';
219   store_release_above(t+1);
220   }
221 
222 #ifdef OpenBSD
223 if (kevent(tls_watch_fd, &kev[kev_used-cnt], cnt, &k_dummy, 1, &ts) >= 0)
224   return TRUE;
225 #else
226 if (kevent(tls_watch_fd, &kev[kev_used-cnt], cnt, NULL, 0, NULL) >= 0)
227   return TRUE;
228 #endif
229 s = US"kevent";
230 
231 bad:
232 DEBUG(D_tls)
233   if (errno)
234     debug_printf("%s: %s: %s\n", __FUNCTION__, s, strerror(errno));
235   else
236     debug_printf("%s: %s\n", __FUNCTION__, s);
237 return FALSE;
238 }
239 # endif	/*EXIM_HAVE_KEVENT*/
240 
241 
242 /* Create an inotify facility if needed.
243 Then set watches on the dir containing the given file or (optionally)
244 list of files.  Return boolean success. */
245 
246 static BOOL
tls_set_watch(const uschar * filename,BOOL list)247 tls_set_watch(const uschar * filename, BOOL list)
248 {
249 rmark r;
250 BOOL rc = FALSE;
251 
252 if (!filename || !*filename) return TRUE;
253 if (Ustrncmp(filename, "system", 6) == 0) return TRUE;
254 
255 DEBUG(D_tls) debug_printf("tls_set_watch: '%s'\n", filename);
256 
257 if (  tls_watch_fd < 0
258 # ifdef EXIM_HAVE_INOTIFY
259    && (tls_watch_fd = inotify_init1(O_CLOEXEC)) < 0
260 # endif
261 # ifdef EXIM_HAVE_KEVENT
262    && (tls_watch_fd = kqueue()) < 0
263 # endif
264    )
265     {
266     DEBUG(D_tls) debug_printf("inotify_init: %s\n", strerror(errno));
267     return FALSE;
268     }
269 
270 r = store_mark();
271 
272 if (list)
273   {
274   int sep = 0;
275   for (uschar * s; s = string_nextinlist(&filename, &sep, NULL, 0); )
276     if (!(rc = tls_set_one_watch(s))) break;
277   }
278 else
279   rc = tls_set_one_watch(filename);
280 
281 store_reset(r);
282 if (!rc) DEBUG(D_tls) debug_printf("tls_set_watch() fail on '%s': %s\n", filename, strerror(errno));
283 return rc;
284 }
285 
286 
287 void
tls_watch_discard_event(int fd)288 tls_watch_discard_event(int fd)
289 {
290 #ifdef EXIM_HAVE_INOTIFY
291 (void) read(fd, big_buffer, big_buffer_size);
292 #endif
293 #ifdef EXIM_HAVE_KEVENT
294 struct kevent kev;
295 struct timespec t = {0};
296 (void) kevent(fd, NULL, 0, &kev, 1, &t);
297 #endif
298 }
299 #endif	/*EXIM_HAVE_INOTIFY*/
300 
301 
302 void
tls_client_creds_reload(BOOL watch)303 tls_client_creds_reload(BOOL watch)
304 {
305 for(transport_instance * t = transports; t; t = t->next)
306   if (Ustrcmp(t->driver_name, "smtp") == 0)
307     {
308     tls_client_creds_invalidate(t);
309     tls_client_creds_init(t, watch);
310     }
311 }
312 
313 
314 void
tls_watch_invalidate(void)315 tls_watch_invalidate(void)
316 {
317 if (tls_watch_fd < 0) return;
318 
319 #ifdef EXIM_HAVE_KEVENT
320 /* Close the files we had open for kevent */
321 for (int i = 0; i < kev_used; i++)
322   {
323   (void) close((int) kev[i].ident);
324   kev[i].ident = (uintptr_t)-1;
325   }
326 kev_used = 0;
327 #endif
328 
329 close(tls_watch_fd);
330 tls_watch_fd = -1;
331 }
332 
333 
334 static void
tls_daemon_creds_reload(void)335 tls_daemon_creds_reload(void)
336 {
337 unsigned lifetime;
338 
339 #ifdef EXIM_HAVE_KEVENT
340 tls_watch_invalidate();
341 #endif
342 
343 tls_server_creds_invalidate();
344 tls_creds_expire = (lifetime = tls_server_creds_init())
345   ? time(NULL) + lifetime : 0;
346 
347 tls_client_creds_reload(TRUE);
348 }
349 
350 
351 /* Utility predicates for use by the per-library code */
352 static BOOL
opt_set_and_noexpand(const uschar * opt)353 opt_set_and_noexpand(const uschar * opt)
354 { return opt && *opt && Ustrchr(opt, '$') == NULL; }
355 
356 static BOOL
opt_unset_or_noexpand(const uschar * opt)357 opt_unset_or_noexpand(const uschar * opt)
358 { return !opt || Ustrchr(opt, '$') == NULL; }
359 
360 
361 
362 /* Called every time round the daemon loop.
363 
364 If we reloaded fd-watcher, return the old watch fd
365 having modified the global for the new one. Otherwise
366 return -1.
367 */
368 
369 int
tls_daemon_tick(void)370 tls_daemon_tick(void)
371 {
372 int old_watch_fd = tls_watch_fd;
373 
374 tls_per_lib_daemon_tick();
375 #if defined(EXIM_HAVE_INOTIFY) || defined(EXIM_HAVE_KEVENT)
376 if (tls_creds_expire && time(NULL) >= tls_creds_expire)
377   {
378   /* The server cert is a selfsign, with limited lifetime.  Dump it and
379   generate a new one.  Reload the rest of the creds also as the machinery
380   is all there. */
381 
382   DEBUG(D_tls) debug_printf("selfsign cert rotate\n");
383   tls_creds_expire = 0;
384   tls_daemon_creds_reload();
385   return old_watch_fd;
386   }
387 else if (tls_watch_trigger_time && time(NULL) >= tls_watch_trigger_time + 5)
388   {
389   /* Called, after a delay for multiple file ops to get done, from
390   the daemon when any of the watches added (above) fire.
391   Dump the set of watches and arrange to reload cached creds (which
392   will set up new watches). */
393 
394   DEBUG(D_tls) debug_printf("watch triggered\n");
395   tls_watch_trigger_time = tls_creds_expire = 0;
396   tls_daemon_creds_reload();
397   return old_watch_fd;
398   }
399 #endif
400 return -1;
401 }
402 
403 /* Called once at daemon startup */
404 
405 void
tls_daemon_init(void)406 tls_daemon_init(void)
407 {
408 tls_per_lib_daemon_init();
409 }
410 
411 
412 /*************************************************
413 *        Timezone environment flipping           *
414 *************************************************/
415 
416 static uschar *
to_tz(uschar * tz)417 to_tz(uschar * tz)
418 {
419 uschar * old = US getenv("TZ");
420 (void) setenv("TZ", CCS tz, 1);
421 tzset();
422 return old;
423 }
424 
425 static void
restore_tz(uschar * tz)426 restore_tz(uschar * tz)
427 {
428 if (tz)
429   (void) setenv("TZ", CCS tz, 1);
430 else
431   (void) os_unsetenv(US"TZ");
432 tzset();
433 }
434 
435 /*************************************************
436 *        Many functions are package-specific     *
437 *************************************************/
438 
439 #ifdef USE_GNUTLS
440 # include "tls-gnu.c"
441 # include "tlscert-gnu.c"
442 # define ssl_xfer_buffer (state_server.xfer_buffer)
443 # define ssl_xfer_buffer_lwm (state_server.xfer_buffer_lwm)
444 # define ssl_xfer_buffer_hwm (state_server.xfer_buffer_hwm)
445 # define ssl_xfer_eof (state_server.xfer_eof)
446 # define ssl_xfer_error (state_server.xfer_error)
447 #endif
448 
449 #ifdef USE_OPENSSL
450 # include "tls-openssl.c"
451 # include "tlscert-openssl.c"
452 #endif
453 
454 
455 
456 /*************************************************
457 *           TLS version of ungetc                *
458 *************************************************/
459 
460 /* Puts a character back in the input buffer. Only ever
461 called once.
462 Only used by the server-side TLS.
463 
464 Arguments:
465   ch           the character
466 
467 Returns:       the character
468 */
469 
470 int
tls_ungetc(int ch)471 tls_ungetc(int ch)
472 {
473 if (ssl_xfer_buffer_lwm <= 0)
474   log_write(0, LOG_MAIN|LOG_PANIC_DIE, "buffer underflow in tls_ungetc");
475 
476 ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
477 return ch;
478 }
479 
480 
481 
482 /*************************************************
483 *           TLS version of feof                  *
484 *************************************************/
485 
486 /* Tests for a previous EOF
487 Only used by the server-side TLS.
488 
489 Arguments:     none
490 Returns:       non-zero if the eof flag is set
491 */
492 
493 int
tls_feof(void)494 tls_feof(void)
495 {
496 return (int)ssl_xfer_eof;
497 }
498 
499 
500 
501 /*************************************************
502 *              TLS version of ferror             *
503 *************************************************/
504 
505 /* Tests for a previous read error, and returns with errno
506 restored to what it was when the error was detected.
507 Only used by the server-side TLS.
508 
509 >>>>> Hmm. Errno not handled yet. Where do we get it from?  >>>>>
510 
511 Arguments:     none
512 Returns:       non-zero if the error flag is set
513 */
514 
515 int
tls_ferror(void)516 tls_ferror(void)
517 {
518 return (int)ssl_xfer_error;
519 }
520 
521 
522 /*************************************************
523 *           TLS version of smtp_buffered         *
524 *************************************************/
525 
526 /* Tests for unused chars in the TLS input buffer.
527 Only used by the server-side TLS.
528 
529 Arguments:     none
530 Returns:       TRUE/FALSE
531 */
532 
533 BOOL
tls_smtp_buffered(void)534 tls_smtp_buffered(void)
535 {
536 return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
537 }
538 
539 
540 #endif  /*DISABLE_TLS*/
541 
542 void
tls_modify_variables(tls_support * dest_tsp)543 tls_modify_variables(tls_support * dest_tsp)
544 {
545 modify_variable(US"tls_bits",                 &dest_tsp->bits);
546 modify_variable(US"tls_certificate_verified", &dest_tsp->certificate_verified);
547 modify_variable(US"tls_cipher",               &dest_tsp->cipher);
548 modify_variable(US"tls_peerdn",               &dest_tsp->peerdn);
549 #ifdef USE_OPENSSL
550 modify_variable(US"tls_sni",                  &dest_tsp->sni);
551 #endif
552 }
553 
554 
555 #ifndef DISABLE_TLS
556 /************************************************
557 *	TLS certificate name operations         *
558 ************************************************/
559 
560 /* Convert an rfc4514 DN to an exim comma-sep list.
561 Backslashed commas need to be replaced by doublecomma
562 for Exim's list quoting.  We modify the given string
563 inplace.
564 */
565 
566 static void
dn_to_list(uschar * dn)567 dn_to_list(uschar * dn)
568 {
569 for (uschar * cp = dn; *cp; cp++)
570   if (cp[0] == '\\' && cp[1] == ',')
571     *cp++ = ',';
572 }
573 
574 
575 /* Extract fields of a given type from an RFC4514-
576 format Distinguished Name.  Return an Exim list.
577 NOTE: We modify the supplied dn string during operation.
578 
579 Arguments:
580 	dn	Distinguished Name string
581 	mod	list containing optional output list-sep and
582 		field selector match, comma-separated
583 Return:
584 	allocated string with list of matching fields,
585 	field type stripped
586 */
587 
588 uschar *
tls_field_from_dn(uschar * dn,const uschar * mod)589 tls_field_from_dn(uschar * dn, const uschar * mod)
590 {
591 int insep = ',';
592 uschar outsep = '\n';
593 uschar * ele;
594 uschar * match = NULL;
595 int len;
596 gstring * list = NULL;
597 
598 while ((ele = string_nextinlist(&mod, &insep, NULL, 0)))
599   if (ele[0] != '>')
600     match = ele;	/* field tag to match */
601   else if (ele[1])
602     outsep = ele[1];	/* nondefault output separator */
603 
604 dn_to_list(dn);
605 insep = ',';
606 len = match ? Ustrlen(match) : -1;
607 while ((ele = string_nextinlist(CUSS &dn, &insep, NULL, 0)))
608   if (  !match
609      || Ustrncmp(ele, match, len) == 0 && ele[len] == '='
610      )
611     list = string_append_listele(list, outsep, ele+len+1);
612 return string_from_gstring(list);
613 }
614 
615 
616 /* Compare a domain name with a possibly-wildcarded name. Wildcards
617 are restricted to a single one, as the first element of patterns
618 having at least three dot-separated elements.  Case-independent.
619 Return TRUE for a match
620 */
621 static BOOL
is_name_match(const uschar * name,const uschar * pat)622 is_name_match(const uschar * name, const uschar * pat)
623 {
624 uschar * cp;
625 return *pat == '*'		/* possible wildcard match */
626   ?    *++pat == '.'		/* starts star, dot              */
627     && !Ustrchr(++pat, '*')	/* has no more stars             */
628     && Ustrchr(pat, '.')	/* and has another dot.          */
629     && (cp = Ustrchr(name, '.'))/* The name has at least one dot */
630     && strcmpic(++cp, pat) == 0 /* and we only compare after it. */
631   :    !Ustrchr(pat+1, '*')
632     && strcmpic(name, pat) == 0;
633 }
634 
635 /* Compare a list of names with the dnsname elements
636 of the Subject Alternate Name, if any, and the
637 Subject otherwise.
638 
639 Arguments:
640 	namelist names to compare
641 	cert	 certificate
642 
643 Returns:
644 	TRUE/FALSE
645 */
646 
647 BOOL
tls_is_name_for_cert(const uschar * namelist,void * cert)648 tls_is_name_for_cert(const uschar * namelist, void * cert)
649 {
650 uschar * altnames = tls_cert_subject_altname(cert, US"dns");
651 uschar * subjdn;
652 uschar * certname;
653 int cmp_sep = 0;
654 uschar * cmpname;
655 
656 if ((altnames = tls_cert_subject_altname(cert, US"dns")))
657   {
658   int alt_sep = '\n';
659   while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
660     {
661     const uschar * an = altnames;
662     while ((certname = string_nextinlist(&an, &alt_sep, NULL, 0)))
663       if (is_name_match(cmpname, certname))
664 	return TRUE;
665     }
666   }
667 
668 else if ((subjdn = tls_cert_subject(cert, NULL)))
669   {
670   int sn_sep = ',';
671 
672   dn_to_list(subjdn);
673   while ((cmpname = string_nextinlist(&namelist, &cmp_sep, NULL, 0)))
674     {
675     const uschar * sn = subjdn;
676     while ((certname = string_nextinlist(&sn, &sn_sep, NULL, 0)))
677       if (  *certname++ == 'C'
678 	 && *certname++ == 'N'
679 	 && *certname++ == '='
680 	 && is_name_match(cmpname, certname)
681 	 )
682 	return TRUE;
683     }
684   }
685 return FALSE;
686 }
687 
688 /* Environment cleanup: The GnuTLS library uses SSLKEYLOGFILE in the environment
689 and writes a file by that name.  Our OpenSSL code does the same, using keying
690 info from the library API.
691 The GnuTLS support only works if exim is run by root, not taking advantage of
692 the setuid bit.
693 You can use either the external environment (modulo the keep_environment config)
694 or the add_environment config option for SSLKEYLOGFILE; the latter takes
695 precedence.
696 
697 If the path is absolute, require it starts with the spooldir; otherwise delete
698 the env variable.  If relative, prefix the spooldir.
699 */
700 void
tls_clean_env(void)701 tls_clean_env(void)
702 {
703 uschar * path = US getenv("SSLKEYLOGFILE");
704 if (path)
705   if (!*path)
706     unsetenv("SSLKEYLOGFILE");
707   else if (*path != '/')
708     {
709     DEBUG(D_tls)
710       debug_printf("prepending spooldir to  env SSLKEYLOGFILE\n");
711     setenv("SSLKEYLOGFILE", CCS string_sprintf("%s/%s", spool_directory, path), 1);
712     }
713   else if (Ustrncmp(path, spool_directory, Ustrlen(spool_directory)) != 0)
714     {
715     DEBUG(D_tls)
716       debug_printf("removing env SSLKEYLOGFILE=%s: not under spooldir\n", path);
717     unsetenv("SSLKEYLOGFILE");
718     }
719 }
720 
721 /*************************************************
722 *       Drop privs for checking TLS config      *
723 *************************************************/
724 
725 /* We want to validate TLS options during readconf, but do not want to be
726 root when we call into the TLS library, in case of library linkage errors
727 which cause segfaults; before this check, those were always done as the Exim
728 runtime user and it makes sense to continue with that.
729 
730 Assumes:  tls_require_ciphers has been set, if it will be
731           exim_user has been set, if it will be
732           exim_group has been set, if it will be
733 
734 Returns:  bool for "okay"; false will cause caller to immediately exit.
735 */
736 
737 BOOL
tls_dropprivs_validate_require_cipher(BOOL nowarn)738 tls_dropprivs_validate_require_cipher(BOOL nowarn)
739 {
740 const uschar *errmsg;
741 pid_t pid;
742 int rc, status;
743 void (*oldsignal)(int);
744 
745 /* If TLS will never be used, no point checking ciphers */
746 
747 if (  !tls_advertise_hosts
748    || !*tls_advertise_hosts
749    || Ustrcmp(tls_advertise_hosts, ":") == 0
750    )
751   return TRUE;
752 else if (!nowarn && !tls_certificate)
753   log_write(0, LOG_MAIN,
754     "Warning: No server certificate defined; will use a selfsigned one.\n"
755     " Suggested action: either install a certificate or change tls_advertise_hosts option");
756 
757 oldsignal = signal(SIGCHLD, SIG_DFL);
758 
759 fflush(NULL);
760 if ((pid = exim_fork(US"cipher-validate")) < 0)
761   log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork failed for TLS check");
762 
763 if (pid == 0)
764   {
765   /* in some modes, will have dropped privilege already */
766   if (!geteuid())
767     exim_setugid(exim_uid, exim_gid, FALSE,
768         US"calling tls_validate_require_cipher");
769 
770   if ((errmsg = tls_validate_require_cipher()))
771     log_write(0, LOG_PANIC_DIE|LOG_CONFIG,
772         "tls_require_ciphers invalid: %s", errmsg);
773   fflush(NULL);
774   exim_underbar_exit(EXIT_SUCCESS);
775   }
776 
777 do {
778   rc = waitpid(pid, &status, 0);
779 } while (rc < 0 && errno == EINTR);
780 
781 DEBUG(D_tls)
782   debug_printf("tls_validate_require_cipher child %d ended: status=0x%x\n",
783       (int)pid, status);
784 
785 signal(SIGCHLD, oldsignal);
786 
787 return status == 0;
788 }
789 
790 
791 
792 
793 #endif	/*!DISABLE_TLS*/
794 #endif	/*!MACRO_PREDEF*/
795 
796 /* vi: aw ai sw=2
797 */
798 /* End of tls.c */
799