source: trunk/src/sh_mail.c@ 88

Last change on this file since 88 was 34, checked in by rainer, 19 years ago

Code cleanup and minor fixes

File size: 50.1 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <pwd.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <errno.h>
31#include <signal.h>
32#include <setjmp.h>
33
34#if defined(SH_WITH_MAIL)
35
36#if TIME_WITH_SYS_TIME
37#include <sys/time.h>
38#include <time.h>
39#else
40#if HAVE_SYS_TIME_H
41#include <sys/time.h>
42#else
43#include <time.h>
44#endif
45#endif
46
47
48#ifdef HAVE_MEMORY_H
49#include <memory.h>
50#endif
51
52#include "samhain.h"
53#include "sh_error.h"
54#include "sh_unix.h"
55#include "sh_tiger.h"
56#include "sh_mail.h"
57#include "sh_utils.h"
58#include "sh_fifo.h"
59#include "sh_tools.h"
60
61#undef FIL__
62#define FIL__ _("sh_mail.c")
63#undef GOOD
64#undef BAD
65
66static int failedMail = SL_FALSE;
67
68/* MX Resolver Struct
69 */
70typedef struct mx_ {
71 int pref;
72 char * address;
73} mx;
74
75typedef struct dnsrep_ {
76 int count;
77 mx * reply;
78} dnsrep;
79
80static int free_mx (dnsrep * answers);
81static dnsrep * return_mx (char *domain);
82
83/*********************************************
84 * utility function for verifying mails
85 *********************************************/
86
87typedef struct mail_trail_struct {
88 char trail_id[2*SH_MINIBUF];
89 char trail_key[KEY_LEN+1];
90 struct mail_trail_struct * next;
91} mail_trail_type;
92
93static mail_trail_type * mail_trail = NULL;
94
95int sh_mail_sigverify (const char * s)
96{
97 SL_TICKET fd;
98 long i;
99 char * buf;
100 char * bufc;
101 char key[81];
102 char number[2*SH_MINIBUF];
103 char audit_id[2 * SH_MINIBUF];
104 long numsig;
105 char key2[KEY_LEN+1];
106
107 char * theSig;
108
109 mail_trail_type * mail_trail_ptr = NULL;
110
111 sh_error_logoff();
112
113 ASSERT((s != NULL && sl_strlen(s) < PATH_MAX),
114 _("(s != NULL && sl_strlen(s) < PATH_MAX)"));
115
116 if (s == NULL || sl_strlen(s) >= PATH_MAX)
117 _exit (EXIT_FAILURE);
118
119 /* open the file, then check it
120 */
121 if (0 != sl_is_suid())
122 {
123 fprintf(stderr, _("Cannot open file %s in suid mode\n"), s);
124 _exit (EXIT_FAILURE);
125 }
126 if ( SL_ISERROR(fd = sl_open_read (s, SL_NOPRIV)))
127 {
128 fprintf(stderr, _("Could not open file %s\n"), s);
129 _exit (EXIT_FAILURE);
130 }
131
132 buf = SH_ALLOC( (size_t)(SH_BUFSIZE+1));
133 bufc = SH_ALLOC( (size_t)(SH_MAXBUF+1));
134
135 while (1 == 1)
136 {
137 buf[0] = '\0';
138 bufc[0] = '\0';
139
140 /* find start of next message
141 */
142 while (0 != sl_strncmp(buf, _("-----BEGIN MESSAGE-----"),
143 sizeof("-----BEGIN MESSAGE-----")-1))
144 {
145 (void) sh_unix_getline (fd, buf, SH_BUFSIZE);
146 if (buf[0] == '\0')
147 {
148 /* End of mailbox reached, exit.
149 */
150 (void) fflush(stdout);
151 _exit (EXIT_SUCCESS);
152
153 /* Fix for AIX cc complaint.
154 */
155 /*@notreached@*/
156 return 0;
157 }
158 }
159
160 /* Read message, compress into bufc.
161 */
162 while (1 == 1)
163 {
164 (void) sh_unix_getline (fd, buf, SH_BUFSIZE);
165 if (0 == sl_strncmp(buf, _("-----BEGIN SIGNATURE-----"),
166 sizeof("-----BEGIN SIGNATURE-----")-1))
167 break;
168 if (buf[0] == '\0')
169 _exit (EXIT_FAILURE);
170 (void) sh_util_compress(bufc, buf, SH_MAXBUF-KEY_LEN);
171 }
172
173 /* get signature and number
174 */
175 (void) sh_unix_getline (fd, key, (int)sizeof(key));
176 key[KEY_LEN] = '\0';
177
178 (void) sh_unix_getline (fd, number, (int)sizeof(number));
179 number[(2*SH_MINIBUF) - 2] = '\0';
180 numsig = atol (number);
181 (void) sl_strlcpy (audit_id, &number[7], 2*SH_MINIBUF);
182
183 fprintf(stderr, _("Message %06ld Trail %s\n"),
184 numsig, /*@-usedef@*/ audit_id /*@+usedef@*/);
185
186 mail_trail_ptr = mail_trail;
187 while (mail_trail_ptr)
188 {
189 if (0 == sl_strcmp(mail_trail_ptr->trail_id, audit_id))
190 break;
191 mail_trail_ptr = mail_trail_ptr->next;
192 }
193
194 if (!mail_trail_ptr)
195 {
196 if (numsig > 0)
197 {
198 fprintf (stderr, _("ERROR (no key -- cannot check)\n"));
199 continue;
200 }
201 else
202 {
203 mail_trail_ptr = SH_ALLOC (sizeof(mail_trail_type));
204 mail_trail_ptr->next = mail_trail;
205 mail_trail = mail_trail_ptr;
206 (void) sl_strlcpy (mail_trail_ptr->trail_id,
207 audit_id, 2*SH_MINIBUF);
208 }
209 }
210 else if (numsig == 0)
211 {
212 fprintf (stderr, _("ERROR (repeated audit trail)\n"));
213 continue;
214 }
215
216
217 if (numsig == 0)
218 {
219 sh_util_encode(key, bufc, 1, 'A');
220 (void) sl_strlcpy (mail_trail_ptr->trail_key, key, KEY_LEN+1);
221 fprintf (stderr, _("(unchecked)\n"));
222 }
223 else
224 {
225 /* iterate key
226 */
227 (void) sl_strlcpy(key2, mail_trail_ptr->trail_key, KEY_LEN+1);
228 for (i = 0; i < numsig; ++i)
229 {
230 (void) sl_strlcpy (key2,
231 sh_tiger_hash (key2, TIGER_DATA, KEY_LEN),
232 KEY_LEN+1);
233 }
234
235
236 theSig = sh_util_siggen (key2, bufc, sl_strlen(bufc));
237 if (sl_strncmp (key,
238 theSig,
239 KEY_LEN) != 0)
240 {
241 fprintf (stderr, _("(FAILED)\n"));
242 }
243 else
244 {
245 fprintf (stderr, _("(passed)\n"));
246 }
247
248 }
249
250 } /* end scan mailbox */
251
252 /*@notreached@*/
253}
254
255#define SH_FILT_NUM 32
256#define SH_FILT_OR 0
257#define SH_FILT_AND 1
258#define SH_FILT_NOT 2
259#define SH_FILT_INIT { 0, { NULL }, 0, { NULL }, 0, { NULL }}
260
261typedef struct _sh_filter_type
262{
263 int for_c;
264 char * for_v[SH_FILT_NUM];
265 int fand_c;
266 char * fand_v[SH_FILT_NUM];
267 int fnot_c;
268 char * fnot_v[SH_FILT_NUM];
269
270} sh_filter_type;
271
272static
273int sh_filter_filteradd (const char * argstring,
274 sh_filter_type * filter, int ftype)
275{
276 int i = 0;
277 int flag = 0;
278 size_t s;
279
280 char * dup;
281 char * p;
282 char * end;
283 int * ntok;
284 char ** stok;
285
286 SL_ENTER(_("sh_filter_filteradd"));
287
288 if (NULL == argstring)
289 {
290 SL_RETURN((-1), _("sh_filter_filteradd"));
291 }
292
293 if (ftype == SH_FILT_OR) {
294 ntok = &(filter->for_c);
295 stok = filter->for_v;
296 }
297 else if (ftype == SH_FILT_AND) {
298 ntok = &(filter->fand_c);
299 stok = filter->fand_v;
300 }
301 else if (ftype == SH_FILT_NOT) {
302 ntok = &(filter->fnot_c);
303 stok = filter->fnot_v;
304 }
305 else {
306 SL_RETURN((-1), _("sh_filter_filteradd"));
307 }
308
309 *ntok = 0;
310
311 dup = sh_util_strdup(argstring);
312 p = dup;
313
314 do
315 {
316 while (*p == ',' || *p == ' ' || *p == '\t')
317 ++p;
318 if (*p == '\0')
319 break;
320
321 end = p; ++end;
322 if (*end == '\0')
323 break;
324
325 if (*p == '\'')
326 {
327 ++p; end = p; ++end;
328 if (*p == '\0' || *end == '\0')
329 break;
330 while (*end != '\0' && *end != '\'')
331 ++end;
332 }
333 else if (*p == '"')
334 {
335 ++p; end = p; ++end;
336 if (*p == '\0' || *end == '\0')
337 break;
338 while (*end != '\0' && *end != '"')
339 ++end;
340 }
341 else
342 {
343 while (*end != '\0' && *end != ',' && *end != ' ' && *end != '\t')
344 ++end;
345 }
346 if (*end == '\0')
347 flag = 1;
348 else
349 *end = '\0';
350
351 s = strlen(p) + 1;
352 if (stok[i] != NULL)
353 SH_FREE(stok[i]);
354 stok[i] = SH_ALLOC(s);
355 (void) sl_strlcpy(stok[i], p, s);
356
357 p = end; ++p;
358
359 ++i;
360 if (i == SH_FILT_NUM)
361 break;
362 }
363 while (p != NULL && *p != '\0' && flag == 0);
364
365 *ntok = i;
366 SH_FREE(dup);
367
368 SL_RETURN (0, _("sh_filter_filteradd"));
369}
370
371/*
372 * -- check filters
373 */
374static
375int sh_filter_filter (const char * message, sh_filter_type * filter)
376{
377 int i;
378 int j = 0;
379
380 SL_ENTER(_("sh_mail_filter"));
381
382 /* Presence of any of these keywords prevents execution.
383 */
384 if (filter->fnot_c > 0)
385 {
386 for (i = 0; i < filter->fnot_c; ++i)
387 {
388 if (NULL != sl_strstr(message, filter->fnot_v[i]))
389 {
390 SL_RETURN ((-1), _("sh_filter_filter"));
391 }
392 }
393 }
394
395 /* Presence of all of these keywords is required for execution.
396 */
397 if (filter->fand_c > 0)
398 {
399 j = 0;
400
401 for (i = 0; i < filter->fand_c; ++i)
402 if (NULL != sl_strstr(message, filter->fand_v[i]))
403 ++j;
404
405 if (j != filter->fand_c)
406 {
407 SL_RETURN ((-1), _("sh_filter_filter"));
408 }
409 }
410
411 /* Presence of at least one of these keywords is required for execution.
412 */
413 if (filter->for_c > 0)
414 {
415 for (i = 0; i < filter->for_c; ++i)
416 {
417 if (NULL != sl_strstr(message, filter->for_v[i]))
418 {
419 goto isok;
420 }
421 }
422 SL_RETURN ((-1), _("sh_filter_filter"));
423 }
424
425 isok:
426 SL_RETURN ((0), _("sh_filter_filter"));
427}
428
429
430static sh_filter_type mail_filter = SH_FILT_INIT;
431
432/*
433 * -- add keywords to the OR filter
434 */
435int sh_mail_add_or (const char * str)
436{
437 return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_OR));
438}
439
440/*
441 * -- add keywords to the AND filter
442 */
443int sh_mail_add_and (const char * str)
444{
445 return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_AND));
446}
447
448/*
449 * -- add keywords to the NOT filter
450 */
451int sh_mail_add_not (const char * str)
452{
453 return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_NOT));
454}
455
456
457static char * address_list[8] = {
458 NULL, NULL, NULL, NULL,
459 NULL, NULL, NULL, NULL
460};
461
462static int address_num = 0;
463static int address_num_compiled = 0;
464static int setaddress_compiled = S_FALSE;
465
466void reset_count_dev_mail(void)
467{
468 /* if not, then we still have the compiled-in address (if any), so we
469 * don' touch them
470 */
471 if (address_num_compiled == -99)
472 address_num = 0;
473 return;
474}
475
476int sh_mail_setaddress (const char * address)
477{
478 char * p;
479
480 SL_ENTER(_("sh_mail_setaddress"));
481
482 if (0 == strcmp(address, _("NULL")))
483 SL_RETURN ( (0), _("sh_mail_setaddress"));
484
485 if (address != NULL && address_num < (2 * SH_PATHBUF / 64 ))
486 {
487 if (address_num < (SH_PATHBUF / 64 ))
488 p = &sh.srvmail.name[address_num*64];
489 else
490 p = &sh.srvmail.alt[address_num*64];
491
492 (void) sl_strlcpy (p, address, 64);
493
494 if ((p == NULL) || ( sl_strlen(address) != sl_strlen(p)))
495 {
496 memset(p, (int)'\0', 64);
497 SL_RETURN ( (-1), _("sh_mail_setaddress"));
498 }
499 address_list[address_num] = p;
500#if 0
501 if (!sl_is_suid())
502 {
503 TPT(( 0, FIL__, __LINE__, _("msg=<address_list[%d] = %s>\n"),
504 address_num, address_list[address_num]));
505 }
506#endif
507 if (setaddress_compiled == S_TRUE)
508 {
509 ++address_num;
510 ++address_num_compiled;
511 }
512 else
513 {
514 if (address_num == address_num_compiled)
515 {
516 address_num = 0;
517 address_num_compiled = -99;
518 }
519 ++address_num;
520 }
521 SL_RETURN ( (0), _("sh_mail_setaddress"));
522 }
523 SL_RETURN ( (-1), _("sh_mail_setaddress"));
524}
525
526int sh_mail_setaddress_int (const char * address)
527{
528 int i;
529 SL_ENTER(_("sh_mail_setaddress_int"));
530 setaddress_compiled = S_TRUE;
531 i = sh_mail_setaddress(address);
532 setaddress_compiled = S_FALSE;
533 SL_RETURN(i, _("sh_mail_setaddress_int"));
534}
535
536int sh_mail_setNum (const char * str)
537{
538 int i = atoi (str);
539
540 SL_ENTER(_("sh_mail_setNum"));
541
542 if (i >= 0 && i < SH_FIFO_MAX)
543 sh.mailNum.alarm_interval = (time_t) i;
544 else
545 SL_RETURN ((-1), _("sh_mail_setNum"));
546 SL_RETURN( (0), _("sh_mail_setNum"));
547}
548
549
550static int all_in_one = S_FALSE;
551
552int sh_mail_setFlag (const char * str)
553{
554 int i;
555 SL_ENTER(_("sh_mail_setFlag"));
556 i = sh_util_flagval(str, &all_in_one);
557 SL_RETURN(i, _("sh_mail_setFlag"));
558}
559
560static char * mail_subject = NULL;
561
562int set_mail_subject (const char * str)
563{
564 SL_ENTER(_("set_mail_subject"));
565 if (!str)
566 SL_RETURN( (-1), _("set_mail_subject"));
567
568 if (mail_subject != NULL)
569 SH_FREE(mail_subject);
570
571 if (0 == sl_strncmp(str, _("NULL"), 4))
572 {
573 mail_subject = NULL;
574 SL_RETURN( 0, _("set_mail_subject"));
575 }
576
577 mail_subject = sh_util_strdup(str);
578 SL_RETURN( (0), _("set_mail_subject"));
579}
580
581
582static SH_FIFO * fifo_mail = NULL;
583
584static
585void sh_mail_emptystack (void)
586{
587 char * msg;
588 size_t len;
589
590 SL_ENTER(_("sh_mail_emptystack"));
591
592 if (fifo_mail == NULL)
593 SL_RET0(_("sh_mail_emptystack"));
594
595 while (NULL != (msg = pop_list(fifo_mail)))
596 {
597 len = sl_strlen(msg);
598 memset(msg, 0, len);
599 SH_FREE(msg);
600 }
601
602 SL_RET0(_("sh_mail_emptystack"));
603}
604
605/* insert "\r\n" after each 998 char
606 */
607static char * split_string(char * str);
608
609int sh_mail_pushstack (/*@null@*/char * msg)
610{
611 char * p;
612 int retval = 0;
613 int status;
614
615 SL_ENTER(_("sh_mail_pushstack"));
616
617 if (msg == NULL || failedMail == SL_TRUE || sh.srvmail.name[0] == '\0')
618 SL_RETURN((0), (_("sh_mail_pushstack")));
619
620 if (0 != sh_filter_filter(msg, &mail_filter))
621 SL_RETURN((0), (_("sh_mail_pushstack")));
622
623#if 0
624 if (msg != NULL && sl_strlen(msg) > 998) /* RFC 2822 */
625 msg[998] = '\0';
626#endif
627
628 p = split_string(msg);
629
630 if (fifo_mail == NULL)
631 {
632 fifo_mail = SH_ALLOC(sizeof(SH_FIFO));
633 fifo_init(fifo_mail);
634 }
635
636 status = push_list (fifo_mail, p);
637 if (status >= 0)
638 ++sh.mailNum.alarm_last;
639
640 SH_FREE(p);
641
642 if (sh.mailNum.alarm_last >= sh.mailNum.alarm_interval)
643 {
644 BREAKEXIT(sh_mail_msg);
645 retval = sh_mail_msg (NULL);
646 }
647
648 if (status == SH_FIFO_MAX)
649 retval = -2;
650 SL_RETURN(retval, (_("sh_mail_pushstack")));
651}
652
653
654/* The mailer.
655 */
656static int sh_mail_end_conn (FILE * connfile);
657static FILE * sh_mail_start_conn (int aFlag);
658
659static
660void sh_mail_get_subject(char * message,
661 char * mheader, size_t len)
662{
663 st_format rep_serv_tab[] = {
664 { 'T', S_FMT_TIME, 0, 0, NULL},
665 { 'H', S_FMT_STRING, 0, 0, NULL},
666 { 'M', S_FMT_STRING, 0, 0, NULL},
667 { 'S', S_FMT_STRING, 0, 0, NULL},
668 {'\0', S_FMT_ULONG, 0, 0, NULL},
669 };
670
671 char * p;
672 char * mptr;
673 char sev[8];
674
675 SL_ENTER(_("sh_mail_get_subject"));
676
677 (void) sl_strlcpy(mheader, _("Subject: "), len);
678 if (NULL == strchr(mail_subject, '%'))
679 {
680 (void) sl_strlcat(mheader, mail_subject, len);
681 SL_RET0(_("sh_mail_get_subject"));
682 }
683
684
685 rep_serv_tab[0].data_ulong = (unsigned long) time(NULL);
686 rep_serv_tab[1].data_str = sh.host.name;
687
688 /* fast forward to the important part
689 */
690 mptr = sl_strstr(message, _("msg="));
691 if (mptr)
692 {
693 mptr += 4;
694 rep_serv_tab[2].data_str = mptr;
695 }
696 else
697 rep_serv_tab[2].data_str = message;
698
699 mptr = sl_strstr(message, _("sev="));
700 if (mptr)
701 {
702 mptr += 5;
703 sev[0] = *mptr; ++mptr;
704 sev[1] = *mptr; ++mptr;
705 sev[2] = *mptr; ++mptr;
706 sev[3] = *mptr; ++mptr;
707 sev[4] = '\0';
708 }
709 else
710 {
711 mptr = message;
712 sev[0] = *mptr; ++mptr;
713 sev[1] = *mptr; ++mptr;
714 sev[2] = *mptr; ++mptr;
715 sev[3] = *mptr; ++mptr;
716 if (*mptr == ' ') {
717 sev[4] = '\0';
718 } else {
719 sev[4] = *mptr; ++mptr;
720 if (*mptr == ' ') {
721 sev[5] = '\0';
722 } else {
723 sev[5] = *mptr;
724 sev[6] = '\0';
725 }
726 }
727 }
728 rep_serv_tab[3].data_str = sev;
729
730
731 p = sh_util_formatted(mail_subject, rep_serv_tab);
732 (void) sl_strlcat(mheader, p, len);
733 SH_FREE(p);
734 SL_RET0(_("sh_mail_get_subject"));
735}
736
737
738#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
739#include <sys/mman.h>
740#endif
741
742static char * sh_mail_realloc (char * inbuf, size_t * insize, size_t increase)
743{
744 size_t newsize;
745 char * outbuf = inbuf;
746
747 SL_ENTER(_("sh_mail_realloc"));
748
749 if (sl_ok_adds((*insize), 1))
750 {
751 newsize = (*insize) + 1;
752
753 if (sl_ok_adds(newsize, increase))
754 {
755 newsize += increase;
756
757 outbuf = SH_ALLOC(newsize);
758 MLOCK(outbuf, newsize);
759 (void) sl_strlcpy(outbuf, inbuf, newsize);
760
761 memset (inbuf, 0, (*insize));
762 MUNLOCK(inbuf, (*insize));
763 SH_FREE(inbuf);
764
765 *insize = newsize;
766 }
767 }
768
769 SL_RETURN( (outbuf), _("sh_mail_realloc"));
770}
771
772int sh_mail_msg (/*@null@*/char * message)
773{
774 char subject[32+32+SH_MINIBUF+2+3+SH_PATHBUF];
775 char mheader[32+32+SH_MINIBUF+2+3];
776
777 char * mailMsg;
778 char * popMsg;
779 int status = 0, errcount;
780 size_t wrlen;
781 int i;
782 int num_popped = 0;
783 int retval = -1;
784
785 char * bufcompress;
786 static int failcount = 0;
787 static int isfirst = 1;
788 static int mailcount = 0;
789 FILE * connfile = NULL;
790
791 struct sigaction old_act;
792 struct sigaction new_act;
793
794 static time_t id_audit = 0;
795 static time_t fail_time = 0;
796 static time_t success_time = 0;
797
798 static int ma_block = 0;
799
800 SH_FIFO * fifo_temp = NULL;
801
802 char * theSig;
803 char * theMsg = NULL;
804
805 /* #define SH_MAILBUF (256) */
806#define SH_MAILBUF (8*4096)
807
808 size_t msgbufsize = SH_MAILBUF;
809 size_t combufsize = SH_MAILBUF;
810
811 SL_ENTER(_("sh_mail_msg"));
812
813 if (ma_block == 1)
814 SL_RETURN( (0), _("sh_mail_msg"));
815
816 /* Return if we cannot mail.
817 */
818 if (failedMail == SL_TRUE)
819 SL_RETURN((-1), _("sh_mail_msg"));
820
821 if (failedMail == SL_FALSE && address_list[0] == NULL)
822 {
823 TPT((0, FIL__, __LINE__,
824 _("msg=<Mail error: no recipient address.>\n")));
825 failedMail = SL_TRUE;
826 SL_RETURN((-1), _("sh_mail_msg"));
827 }
828
829 if ( (success_time > 0) && (fail_time > 0) &&
830 (time(NULL) - success_time) > 3600*SH_MAX_FAIL)
831 {
832 ma_block = 1;
833 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
834 _("mail"), address_list[0]);
835 ma_block = 0;
836 sh_mail_emptystack();
837 sh.mailNum.alarm_last = 0;
838 failedMail = SL_TRUE;
839 SL_RETURN((-1), _("sh_mail_msg"));
840 }
841
842 /* Try at most each hour.
843 */
844 if ((fail_time > 0) && (time(NULL) - fail_time) < 3/*600*/)
845 {
846 if (failcount > 3)
847 {
848 /* -- Save for later. --
849 */
850 sh_mail_pushstack (message);
851 ++failcount;
852
853 SL_RETURN((-1), _("sh_mail_msg"));
854 }
855 else
856 {
857 (void) retry_msleep(2, 0);
858 ++failcount;
859 }
860 }
861
862 /* -- Reset time of last failure. --
863 */
864 fail_time = 0;
865
866 /* -- Polling, empty queue. --
867 */
868 if (message == NULL && sh.mailNum.alarm_last == 0)
869 SL_RETURN((-1), _("sh_mail_msg"));
870
871 /* -- Filtered. --
872 */
873 if (message != NULL && 0 != sh_filter_filter(message, &mail_filter))
874 SL_RETURN((-1), (_("sh_mail_msg")));
875
876
877
878 /* --------- Build complete message. ------------------------ */
879
880 theMsg = split_string(message);
881
882 /* ---------- Header ---------------------------------------- */
883
884 if (mail_subject == NULL)
885 {
886 (void) sl_strlcpy(mheader, _("Subject: "), sizeof(mheader)-5);
887 (void) sl_strlcat(mheader, sh_unix_time (0), sizeof(mheader)-5);
888 (void) sl_strlcat(mheader, " ", sizeof(mheader)-5);
889 (void) sl_strlcat(mheader, sh.host.name, sizeof(mheader)-5);
890 }
891 else
892 {
893
894 if (message == NULL)
895 {
896 theMsg = pop_list(fifo_mail);
897 message = theMsg;
898 if (message)
899 --sh.mailNum.alarm_last;
900 }
901
902 if (message)
903 {
904 sh_mail_get_subject(message, mheader, sizeof(mheader)-5);
905 }
906 else
907 {
908 (void) sl_strlcpy(mheader, _("Subject: "), sizeof(mheader)-5);
909 (void) sl_strlcat(mheader, sh_unix_time (0), sizeof(mheader)-5);
910 (void) sl_strlcat(mheader, " ", sizeof(mheader)-5);
911 (void) sl_strlcat(mheader, sh.host.name, sizeof(mheader)-5);
912 }
913 }
914
915 /* RFC 821: Header is terminated by an empty line
916 */
917 (void) sl_strlcat(mheader, "\015\012\015\012", sizeof(mheader));
918
919 /* ---------- Message --------------------------------------- */
920
921 (void) sl_strlcpy(subject, sh_unix_time (0), sizeof(subject));
922 (void) sl_strlcat(subject, " ", sizeof(subject));
923 (void) sl_strlcat(subject, sh.host.name, sizeof(subject));
924 (void) sl_strlcat(subject, "\r\n", sizeof(subject));
925
926
927 mailMsg = (char *) SH_ALLOC (msgbufsize);
928 bufcompress = (char *) SH_ALLOC (combufsize);
929
930 MLOCK(mailMsg , msgbufsize);
931 MLOCK(bufcompress , combufsize);
932
933 (void) sl_strlcpy(mailMsg, mheader, msgbufsize);
934 bufcompress[0] = '\0';
935
936 (void) sl_strlcat(mailMsg, _("-----BEGIN MESSAGE-----\r\n"), msgbufsize);
937 (void) sl_strlcat(mailMsg, subject, msgbufsize);
938 (void) sh_util_compress (bufcompress, subject,
939 (combufsize - KEY_LEN - 1));
940 if (message != NULL)
941 {
942 if ((sl_strlen(theMsg) + sl_strlen(mailMsg) + 1) >
943 (msgbufsize-(4*KEY_LEN)))
944 {
945 mailMsg = sh_mail_realloc(mailMsg,
946 &msgbufsize, sl_strlen(theMsg)+2);
947 bufcompress = sh_mail_realloc(bufcompress,
948 &combufsize, sl_strlen(theMsg));
949 }
950 (void) sl_strlcat(mailMsg, theMsg, msgbufsize-(4*KEY_LEN));
951 (void) sl_strlcat(mailMsg, "\r\n", msgbufsize-(4*KEY_LEN));
952
953 (void) sh_util_compress (bufcompress, theMsg, combufsize-KEY_LEN-1);
954 }
955
956 if (sh.mailNum.alarm_last > 0)
957 {
958 fifo_temp = SH_ALLOC (sizeof(SH_FIFO));
959 fifo_init (fifo_temp);
960
961 while ( NULL != (popMsg = pop_list(fifo_mail)) )
962 {
963 (void) push_list (fifo_temp, popMsg);
964
965 if ((sl_strlen(popMsg) + sl_strlen(mailMsg) + 1) >
966 (msgbufsize-(4*KEY_LEN)))
967 {
968 mailMsg = sh_mail_realloc(mailMsg,
969 &msgbufsize,
970 sl_strlen(popMsg)+2);
971 bufcompress = sh_mail_realloc(bufcompress,
972 &combufsize,
973 sl_strlen(popMsg));
974 }
975
976 (void) sl_strlcat(mailMsg, popMsg, msgbufsize-(4*KEY_LEN));
977 (void) sl_strlcat(mailMsg, "\r\n", msgbufsize-(4*KEY_LEN));
978 (void) sh_util_compress(bufcompress, popMsg, combufsize-KEY_LEN-1);
979 SH_FREE(popMsg);
980 --sh.mailNum.alarm_last;
981 ++num_popped;
982 }
983 }
984
985 /* ------ signature block ------------------------------------ */
986
987 (void) sl_strlcat(mailMsg, _("-----BEGIN SIGNATURE-----\r\n"), msgbufsize);
988
989 /* Generate new signature key.
990 */
991 if (isfirst == 1)
992 {
993 BREAKEXIT(sh_util_keyinit);
994 (void) sh_util_keyinit (skey->mailkey_old, KEY_LEN+1);
995 }
996
997 /* iterate the key
998 */
999 (void) sl_strlcpy(skey->mailkey_new,
1000 sh_tiger_hash (skey->mailkey_old, TIGER_DATA, KEY_LEN),
1001 KEY_LEN+1);
1002
1003 if (isfirst == 0)
1004 {
1005 /* Sign the message with the signature key.
1006 */
1007 theSig = sh_util_siggen (skey->mailkey_new,
1008 bufcompress, sl_strlen(bufcompress));
1009 (void) sl_strlcat (mailMsg,
1010 theSig,
1011 msgbufsize);
1012 }
1013 else
1014 {
1015 id_audit = time (NULL);
1016
1017 /* reveal first signature key
1018 */
1019 /* flawfinder: ignore */
1020 (void) sl_strlcpy(skey->crypt, skey->mailkey_new, KEY_LEN+1);
1021
1022 BREAKEXIT(sh_util_encode);
1023 /* flawfinder: ignore */
1024 sh_util_encode(skey->crypt, bufcompress, 0, 'A');
1025
1026 /* flawfinder: ignore */
1027 (void) sl_strlcat (mailMsg, skey->crypt, msgbufsize);
1028 /* flawfinder: ignore */
1029 memset (skey->crypt, 0, KEY_LEN);
1030 isfirst = 0;
1031 }
1032 (void) sl_strlcat (mailMsg, "\r\n", msgbufsize);
1033
1034 /* X(n) -> X(n-1)
1035 */
1036 (void) sl_strlcpy (skey->mailkey_old, skey->mailkey_new, KEY_LEN+1);
1037
1038 sl_snprintf(subject, sizeof(subject), _("%06d %010ld::%s\r\n"),
1039 mailcount, (long) id_audit, sh.host.name);
1040
1041 (void) sl_strlcat (mailMsg, subject, msgbufsize);
1042 ++mailcount;
1043
1044 (void) sl_strlcat (mailMsg, _("-----END MESSAGE-----"), msgbufsize);
1045
1046
1047
1048 /* ---------- Connect ---------------------------------------- */
1049
1050
1051
1052 /* -- Catch (ignore) 'broken pipe'.
1053 */
1054 new_act.sa_handler = SIG_IGN;
1055 sigemptyset( &new_act.sa_mask ); /* set an empty mask */
1056 new_act.sa_flags = 0; /* init sa_flags */
1057
1058 (void) sigaction (SIGPIPE, &new_act, &old_act);
1059
1060 i = 0;
1061 errcount = 0;
1062
1063 if (all_in_one == S_FALSE)
1064 {
1065 while (address_list[i] != NULL && i < address_num)
1066 {
1067 connfile = sh_mail_start_conn (i);
1068
1069 if (NULL != connfile)
1070 {
1071 wrlen = fwrite (mailMsg, 1, sl_strlen(mailMsg),
1072 connfile);
1073 wrlen -= sl_strlen(mailMsg);
1074 if (wrlen == 0)
1075 status = sh_mail_end_conn (connfile);
1076 else
1077 status = -1;
1078 }
1079 if (NULL == connfile || status != 0)
1080 {
1081 ma_block = 1;
1082 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
1083 _("mail"), address_list[i]);
1084 ma_block = 0;
1085 ++errcount;
1086 ++sh.statistics.mail_failed;
1087 }
1088 else
1089 {
1090 ++sh.statistics.mail_success;
1091 }
1092
1093 if (connfile != NULL)
1094 {
1095 (void) fclose (connfile);
1096 connfile = NULL;
1097 }
1098 ++i;
1099 }
1100 }
1101 else
1102 {
1103 connfile = sh_mail_start_conn ( -9 );
1104
1105 if (NULL != connfile)
1106 {
1107 wrlen = fwrite (mailMsg, 1, sl_strlen(mailMsg), connfile);
1108 wrlen -= sl_strlen(mailMsg);
1109 if (wrlen == 0)
1110 status = sh_mail_end_conn (connfile);
1111 else
1112 status = -1;
1113 }
1114 if (NULL == connfile || status != 0)
1115 {
1116 ma_block = 1;
1117 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
1118 _("mail"), address_list[0]);
1119 ma_block = 0;
1120 errcount = address_num;
1121 ++sh.statistics.mail_failed;
1122 }
1123 else
1124 {
1125 ++sh.statistics.mail_success;
1126 }
1127
1128 if (connfile != NULL)
1129 {
1130 (void) fclose (connfile);
1131 connfile = NULL;
1132 }
1133 }
1134
1135
1136 memset (bufcompress, 0, combufsize);
1137 MUNLOCK(bufcompress , combufsize);
1138 SH_FREE(bufcompress);
1139
1140 memset (mailMsg, 0, msgbufsize);
1141 MUNLOCK(mailMsg , msgbufsize);
1142 SH_FREE(mailMsg);
1143
1144 /* --- Stay responsible for delivery in case of failure --- */
1145
1146 if (errcount == address_num && fifo_temp != NULL)
1147 {
1148 while ( (NULL != (popMsg = pop_list(fifo_temp))) )
1149 {
1150 if (push_list (fifo_mail, popMsg) >= 0)
1151 ++sh.mailNum.alarm_last;
1152 SH_FREE(popMsg);
1153 }
1154 if (message != NULL)
1155 {
1156 if (fifo_mail == NULL)
1157 {
1158 fifo_mail = SH_ALLOC(sizeof(SH_FIFO));
1159 fifo_init(fifo_mail);
1160 }
1161 retval = push_list (fifo_mail, theMsg);
1162 if (retval >= 0)
1163 ++sh.mailNum.alarm_last;
1164 if (retval == SH_FIFO_MAX)
1165 retval = -2;
1166 else
1167 retval = -1;
1168 }
1169 }
1170 else if (fifo_temp != NULL)
1171 {
1172 while ( (NULL != (popMsg = pop_list(fifo_temp))) )
1173 {
1174 SH_FREE(popMsg);
1175 }
1176 }
1177 if (fifo_temp != NULL)
1178 SH_FREE(fifo_temp);
1179
1180 /*
1181 if (connfile != NULL)
1182 fclose (connfile);
1183 */
1184
1185 if (theMsg != NULL)
1186 SH_FREE(theMsg);
1187
1188 /* --- Reset signal. ---
1189 */
1190 (void) sigaction (SIGPIPE, &old_act, NULL);
1191
1192 if (errcount == address_num)
1193 {
1194 fail_time = time(NULL);
1195 SL_RETURN((retval), _("sh_mail_msg"));
1196 }
1197 success_time = time(NULL);
1198 failcount = 0;
1199
1200 SL_RETURN((0), _("sh_mail_msg"));
1201}
1202
1203
1204/*
1205 *
1206 * SMTP CODE BELOW
1207 *
1208 *
1209 */
1210
1211#include <ctype.h>
1212#ifdef HOST_IS_HPUX
1213#define _XOPEN_SOURCE_EXTENDED
1214#endif
1215#include <netdb.h>
1216#include <sys/types.h>
1217#include <sys/socket.h>
1218#include <netinet/in.h>
1219#ifndef S_SPLINT_S
1220#include <arpa/inet.h>
1221#else
1222#define AF_INET 2
1223#endif
1224
1225#define SH_NEED_GETHOSTBYXXX
1226#include "sh_static.h"
1227
1228/* missing on HP-UX 10.20 */
1229#ifndef IPPORT_SMTP
1230#define IPPORT_SMTP 25
1231#endif
1232
1233static int sh_mail_wait(int code, FILE * m_socket);
1234
1235static char * relay_host = NULL;
1236
1237int sh_mail_set_relay (const char * str_s)
1238{
1239 SL_ENTER(_("sh_mail_set_relay"));
1240
1241 if (str_s == NULL)
1242 SL_RETURN( -1, _("sh_mail_set_relay"));
1243
1244 if (relay_host != NULL)
1245 {
1246 SH_FREE (relay_host);
1247 relay_host = NULL;
1248 }
1249
1250 if (0 == sl_strncmp(str_s, _("NULL"), 4))
1251 {
1252 SL_RETURN( 0, _("sh_mail_set_relay"));
1253 }
1254
1255 relay_host = sh_util_strdup(str_s);
1256
1257 SL_RETURN( 0, _("sh_mail_set_relay"));
1258}
1259
1260static char * mail_sender = NULL;
1261
1262int sh_mail_set_sender (const char *str)
1263{
1264 if (mail_sender != NULL)
1265 {
1266 SH_FREE (mail_sender);
1267 mail_sender = NULL;
1268 }
1269 if (str != NULL)
1270 {
1271 mail_sender = sh_util_strdup (str);
1272 }
1273 if (mail_sender == NULL)
1274 {
1275 return -1;
1276 }
1277 return 0;
1278}
1279
1280
1281/*************************
1282 *
1283 * start connection
1284 * for details on SMTP, see RFC 821
1285 */
1286
1287static time_t time_wait = 300;
1288
1289static FILE * sh_mail_start_conn (int aFlag)
1290{
1291 char * address;
1292
1293 int ecount;
1294
1295 char this_address[256];
1296 char ma_machine[256];
1297 char ma_user[256];
1298 char error_msg[256];
1299 char error_call[SH_MINIBUF];
1300 int error_num = 0;
1301 register int i, j, k;
1302 FILE * connFile = NULL;
1303 struct tm * my_tm;
1304 time_t my_time;
1305 char my_tbuf[128];
1306
1307 int fd;
1308
1309 dnsrep * answers;
1310 mx * result;
1311
1312 SL_ENTER(_("sh_mail_start_conn"));
1313
1314 time_wait = 300;
1315
1316 if (aFlag >= 0)
1317 address = address_list[aFlag];
1318 else
1319 address = address_list[0];
1320
1321 TPT(( 0, FIL__, __LINE__, _("msg=<aFlag %d address %s>\n"),
1322 aFlag, address));
1323
1324 /* ------- split adress ------------------ */
1325
1326 if (0 == strcmp(address, _("NULL")))
1327 {
1328 SL_RETURN( NULL, _("sh_mail_start_conn"));
1329 }
1330
1331 if (strchr (address, '@') == NULL) {
1332 (void) sl_strlcpy(ma_user, address, 256);
1333 (void) sl_strlcpy(ma_machine, _("localhost"), 256);
1334 } else {
1335 i = 0;
1336 while (i < 255 && address[i] != '@') {
1337 ma_user[i] = address[i];
1338 ++i;
1339 }
1340
1341 /* adress[i] = '@'
1342 */
1343 ma_user[i] = '\0';
1344 j = i + 1; k = i; i = 0;
1345 while (i < 255 && address[i+j] != '\0') {
1346 ma_machine[i] = address[i+j];
1347 ++i;
1348 }
1349 ma_machine[i] = '\0';
1350 if (address[k] != '@' || address[k+i+1] != '\0')
1351 {
1352 SL_RETURN( NULL, _("sh_mail_start_conn"));
1353 }
1354 }
1355
1356
1357 if (relay_host != NULL)
1358 {
1359 (void) sl_strlcpy (ma_machine, relay_host, sizeof(ma_machine));
1360 TPT((0, FIL__, __LINE__, _("msg=<user %s machine %s>\n"),
1361 ma_user, ma_machine));
1362 fd = connect_port (ma_machine, IPPORT_SMTP,
1363 error_call, &error_num, error_msg, 256);
1364 }
1365 else
1366 {
1367 answers = return_mx (ma_machine);
1368 if (answers)
1369 {
1370 result = answers->reply;
1371 fd = -1;
1372 for (i = 0; i < answers->count; ++i)
1373 {
1374 (void) sl_strlcpy(ma_machine, result[i].address,
1375 sizeof(ma_machine));
1376 TPT((0, FIL__, __LINE__,
1377 _("msg=<user %s mx %s pref %d>\n"),
1378 ma_user, ma_machine, result[i].pref));
1379 fd = connect_port (ma_machine, IPPORT_SMTP,
1380 error_call, &error_num, error_msg, 256);
1381 if (fd >= 0)
1382 break;
1383 }
1384 (void) free_mx(answers);
1385 }
1386 else
1387 {
1388 (void) sl_strlcpy(error_call, _("return_mx"), SH_MINIBUF);
1389 (void) sl_strlcpy(error_msg, _("The specified host is unknown: "),
1390 256);
1391 (void) sl_strlcat(error_msg, ma_machine, 256);
1392 fd = -1;
1393 }
1394 }
1395
1396
1397 if (fd < 0)
1398 {
1399 sh_error_handle ((-1), FIL__, __LINE__, error_num,
1400 MSG_E_NET, error_msg, error_call,
1401 _("email"), ma_machine);
1402 SL_RETURN( NULL, _("sh_mail_start_conn"));
1403 }
1404
1405 /* associate a FILE structure with it
1406 */
1407 connFile = fdopen (fd, "r+");
1408 if (connFile == NULL)
1409 {
1410 TPT(( 0, FIL__, __LINE__, _("msg=<fdopen() failed>\n")));
1411 (void) close(fd);
1412 SL_RETURN( NULL, _("sh_mail_start_conn"));
1413 }
1414
1415
1416 /* say HELO to the other socket
1417 */
1418 if (0 == sh_mail_wait (220, connFile))
1419 {
1420 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1421 _("Timeout on SMTP session init"),
1422 _("sh_mail_start_conn"),
1423 _("mail"), sh.host.name);
1424 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
1425 (void) fclose(connFile);
1426 SL_RETURN( NULL, _("sh_mail_start_conn"));
1427 }
1428
1429 (void) fflush(connFile);
1430
1431 if (0 != is_numeric(sh.host.name))
1432 {
1433 TPT(( 0, FIL__, __LINE__, _("msg=<HELO [%s]>%c%c"),
1434 sh.host.name, 13, 10));
1435 }
1436 else
1437 {
1438 TPT(( 0, FIL__, __LINE__, _("msg=<HELO %s>%c%c"),
1439 sh.host.name, 13, 10));
1440 }
1441 if (0 != is_numeric(sh.host.name))
1442 fprintf(connFile, _("HELO [%s]%c%c"), sh.host.name, 13, 10);
1443 else
1444 fprintf(connFile, _("HELO %s%c%c"), sh.host.name, 13, 10);
1445
1446 (void) fflush(connFile);
1447
1448 if (0 == sh_mail_wait(250, connFile))
1449 {
1450 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1451 _("HELO failed"), _("sh_mail_start_conn"),
1452 _("mail"), sh.host.name);
1453
1454 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1455 (void) fclose(connFile);
1456 SL_RETURN( NULL, _("sh_mail_start_conn"));
1457 }
1458
1459 /* tell them who we are
1460 */
1461 (void) sl_strlcpy (this_address,
1462 mail_sender ? mail_sender : DEFAULT_SENDER, 256);
1463 if (NULL == strchr(this_address, '@'))
1464 {
1465 (void) sl_strlcat (this_address, "@", 256);
1466 if (0 != is_numeric(sh.host.name))
1467 (void) sl_strlcat (this_address, _("example.com"), 256);
1468 else
1469 (void) sl_strlcat (this_address, sh.host.name, 256);
1470 }
1471
1472 TPT(( 0, FIL__, __LINE__, _("msg=<MAIL FROM:<%s>>%c%c"),
1473 this_address, 13, 10));
1474
1475 (void) fflush(connFile);
1476 /*@-usedef@*/
1477 fprintf(connFile, _("MAIL FROM:<%s>%c%c"), this_address, 13, 10);
1478 /*@+usedef@*/
1479 (void) fflush(connFile);
1480
1481 if (0 == sh_mail_wait(250, connFile))
1482 {
1483 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1484 _("MAIL FROM failed"), _("sh_mail_start_conn"),
1485 _("mail"), this_address);
1486 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1487 (void) fclose(connFile);
1488 SL_RETURN( NULL, _("sh_mail_start_conn"));
1489 }
1490
1491 /* tell them who to send mail to
1492 */
1493 if (aFlag >= 0)
1494 {
1495 TPT(( 0, FIL__, __LINE__, _("msg=<RCPT TO:<%s>>%c%c"),
1496 address, 13, 10));
1497
1498 (void) fflush(connFile);
1499 fprintf(connFile, _("RCPT TO:<%s>%c%c"), address, 13, 10);
1500 (void) fflush(connFile);
1501
1502 if (0 == sh_mail_wait(250, connFile))
1503 {
1504 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1505 _("RCPT TO failed"), _("sh_mail_start_conn"),
1506 _("mail"), address);
1507 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1508 (void) fclose(connFile);
1509 SL_RETURN( NULL, _("sh_mail_start_conn"));
1510 }
1511 }
1512 else
1513 {
1514 ecount = 0;
1515 for (i = 0; i < address_num; ++i)
1516 {
1517 if (address_list[i] == NULL) /* paranoia */
1518 break;
1519 TPT(( 0, FIL__, __LINE__, _("msg=<RCPT TO:<%s>>%c%c"),
1520 address_list[i], 13, 10));
1521
1522 (void) fflush(connFile);
1523 fprintf(connFile, _("RCPT TO:<%s>%c%c"), address_list[i], 13, 10);
1524 (void) fflush(connFile);
1525
1526 if (0 == sh_mail_wait(250, connFile))
1527 {
1528 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1529 _("RCPT TO failed"), _("sh_mail_start_conn"),
1530 _("mail"), address_list[i]);
1531
1532 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1533 ++ecount;
1534 }
1535 }
1536 if (ecount == address_num)
1537 {
1538 (void) fclose(connFile);
1539 SL_RETURN( NULL, _("sh_mail_start_conn"));
1540 }
1541 }
1542
1543 /* Send the message
1544 */
1545 TPT(( 0, FIL__, __LINE__, _("msg=<DATA>%c%c"), 13, 10));
1546
1547 (void) fflush(connFile);
1548 fprintf(connFile, _("DATA%c%c"), 13, 10);
1549 (void) fflush(connFile);
1550
1551 if (0 == sh_mail_wait(354, connFile))
1552 {
1553 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1554 _("DATA failed"), _("sh_mail_start_conn"),
1555 _("mail"), address);
1556 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1557 (void) fclose(connFile);
1558 SL_RETURN( NULL, _("sh_mail_start_conn"));
1559 }
1560
1561
1562 my_time = time(NULL);
1563 my_tm = localtime(&my_time);
1564 (void) strftime(my_tbuf, 127, _("%a, %d %b %Y %H:%M:%S %Z"), my_tm);
1565
1566 TPT(( 0, FIL__, __LINE__, _("msg=<From: <%s>%c%cTo: <%s>%c%cDate: %s>%c%c"),
1567 this_address, 13, 10, address, 13, 10, my_tbuf, 13, 10));
1568
1569 (void) fflush(connFile);
1570 fprintf(connFile,
1571 _("From: <%s>%c%c"\
1572 "To: <%s>%c%c"\
1573 "Date: %s%c%c"),
1574 this_address, 13, 10,
1575 address, 13, 10,
1576 my_tbuf, 13, 10);
1577
1578 SL_RETURN( connFile, _("sh_mail_start_conn"));
1579}
1580
1581/*************************
1582 *
1583 * end connection
1584 *
1585 */
1586
1587static int sh_mail_end_conn (FILE * connFile)
1588{
1589 SL_ENTER(_("sh_mail_end_conn"));
1590
1591 time_wait = 300;
1592
1593 (void) fflush(connFile);
1594 fprintf(connFile, _("%c%c.%c%c"), 13, 10, 13, 10);
1595 (void) fflush(connFile);
1596
1597 TPT(( 0, FIL__, __LINE__, _("msg=<message end written>\n")));
1598
1599 if (0 != sh_mail_wait(250, connFile))
1600 {
1601 (void) fflush(connFile);
1602 fprintf(connFile, _("QUIT%c%c"), 13, 10);
1603 (void) fflush(connFile);
1604 TPT(( 0, FIL__, __LINE__, _("msg=<exit>\n")));
1605
1606 SL_RETURN (0, _("sh_mail_end_conn"));
1607 }
1608
1609 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1610 _("QUIT failed"), _("sh_mail_end_conn"),
1611 _("mail"), _("SMTP server"));
1612
1613 TPT(( 0, FIL__, __LINE__, _("msg=<abnormal exit>\n")));
1614
1615 SL_RETURN ((-1), _("sh_mail_end_conn"));
1616}
1617
1618
1619
1620/****************************
1621 *
1622 * Handle server replies
1623 *
1624 *
1625 */
1626
1627static jmp_buf wait_timeout;
1628
1629static void sh_mail_alarmhandle (int mysignal)
1630{
1631 /*@-noeffect@*/
1632 (void) mysignal; /* avoid compiler warning */
1633 /*@+noeffect@*/
1634
1635 longjmp(wait_timeout, 1);
1636}
1637
1638static int sh_mail_wait(int code, FILE * ma_socket)
1639{
1640 int rcode, g;
1641
1642 char errmsg[128];
1643
1644 enum {
1645 WAIT_CODE_START,
1646 WAIT_CODE,
1647 WAIT_NL,
1648 WAIT_NL_CONT
1649 } state;
1650
1651 time_t waited_time;
1652
1653 struct sigaction old_act;
1654 volatile unsigned int old_alarm = 0;
1655
1656 struct sigaction new_act;
1657 sigset_t unblock;
1658
1659 (void) sigemptyset(&unblock);
1660 (void) sigaddset (&unblock, SIGALRM);
1661
1662 new_act.sa_handler = sh_mail_alarmhandle;
1663 (void) sigemptyset( &new_act.sa_mask ); /* set an empty mask */
1664 new_act.sa_flags = 0; /* init sa_flags */
1665
1666 SL_ENTER(_("mail_wait"));
1667
1668 /* alarm was triggered
1669 */
1670 if (setjmp(wait_timeout) != 0)
1671 {
1672 (void) alarm(0);
1673 (void) sigaction (SIGALRM, &old_act, NULL);
1674 (void) alarm(old_alarm);
1675 (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
1676 TPT((0, FIL__, __LINE__, _("msg=<mail_wait: timeout>\n")));
1677 SL_RETURN( 0, _("mail_wait"));
1678 }
1679
1680 waited_time = time(NULL);
1681
1682 /* timeout after 5 minutes
1683 */
1684 old_alarm = alarm(0);
1685 (void) sigaction (SIGALRM, &new_act, &old_act);
1686 (void) alarm((unsigned int) time_wait);
1687
1688 rcode = 0;
1689 state = WAIT_CODE_START;
1690
1691 while (0 == feof(ma_socket) && 0 == ferror(ma_socket)) {
1692
1693 if ( (g=fgetc(ma_socket)) == EOF)
1694 {
1695 (void) alarm(0);
1696 (void) sigaction (SIGALRM, &old_act, NULL);
1697 (void) alarm(old_alarm);
1698 (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
1699 TPT((0, FIL__, __LINE__, _("msg=<mail_wait: EOF>\n")));
1700 SL_RETURN( 0, _("mail_wait"));
1701 }
1702
1703 switch(state) {
1704
1705 /* wait for start of a numerical code
1706 */
1707 case WAIT_CODE_START:
1708 if (0 != isspace(g))
1709 break; /* Skip white space */
1710 if (0 == isdigit(g))
1711 return 0; /* No leading number */
1712 rcode = g-(int)'0'; /* convert to number */
1713 state = WAIT_CODE;
1714 break;
1715
1716 /* wait for completion of numerical code
1717 */
1718 case WAIT_CODE:
1719 if (0 != isdigit(g)) {
1720 rcode = rcode * 10 + (g-(int)'0'); /* next digit */
1721 break;
1722 }
1723 /*@+charintliteral@*/
1724 state = ((g == '-') ? WAIT_NL_CONT : WAIT_NL);
1725 /*@-charintliteral@*/
1726 break;
1727
1728 /* wait for newline, then return with status code
1729 */
1730 case WAIT_NL:
1731 /*@+charintliteral@*/
1732 if (g != '\n')
1733 break;
1734 /*@-charintliteral@*/
1735 (void) alarm(0);
1736 (void) sigaction (SIGALRM, &old_act, NULL);
1737 (void) alarm(old_alarm);
1738 (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
1739
1740 TPT((0, FIL__, __LINE__,
1741 _("msg=<mail_wait: OK got %d (%d) need %d (%d)>\n"),
1742 rcode, (int)(rcode/100), code, (int)(code/100) ));
1743 g = ((int)(rcode/100) == (int)(code/100)) ? 1 : 0;
1744 if (g != 1)
1745 {
1746 sl_snprintf(errmsg, sizeof(errmsg),
1747 _("Bad response (%d), expected %d"), rcode, code);
1748
1749 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1750 errmsg, _("sh_mail_wait"),
1751 _("mail"), _("SMTP server"));
1752 }
1753 waited_time = time(NULL) - waited_time;
1754 time_wait -= waited_time;
1755 TPT((0, FIL__, __LINE__,
1756 _("msg=<mail_wait: time_wait reduced to %d sec>\n"),
1757 (int) time_wait));
1758 SL_RETURN( (g), _("mail_wait")) ;
1759
1760 /* wait for continuation line
1761 */
1762 /*@fallthrough@*//* no, but splint doesn't understand */
1763 case WAIT_NL_CONT:
1764 /*@+charintliteral@*/
1765 if (g == '\n')
1766 state = WAIT_CODE_START; /* There is a continuation line */
1767 /*@-charintliteral@*/
1768 break;
1769
1770 default:
1771 (void) alarm(0);
1772 (void) sigaction (SIGALRM, &old_act, NULL);
1773 (void) alarm(old_alarm);
1774 (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
1775
1776 TPT((0, FIL__, __LINE__, _("msg=<mail_wait: bad>\n")));
1777 SL_RETURN( 0, _("mail_wait"));
1778
1779 }
1780 }
1781
1782 (void) alarm(0); /* Disable alarm */
1783 (void) sigaction (SIGALRM, &old_act, NULL);
1784 (void) alarm(old_alarm);
1785 (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
1786
1787 TPT((0, FIL__, __LINE__, _("msg=<mail_wait: failed>\n")));
1788
1789 /* Failed, EOF or error on socket */
1790 SL_RETURN( 0, _("mail_wait"));
1791}
1792
1793/* -- function to insert "\r\n" after each 998 chars --
1794 */
1795
1796#define SPLIT_AT 998
1797
1798static char * split_string(char * str)
1799{
1800 size_t size;
1801 size_t blocks;
1802 int count = 0;
1803
1804 char * p, * p0;
1805 char * q;
1806
1807 if (!str)
1808 return NULL;
1809
1810 size = strlen(str) + 1;
1811 blocks = 1 + (size / SPLIT_AT);
1812
1813 if (sl_ok_muls(2, blocks) && sl_ok_adds(size, (2*blocks)))
1814 {
1815 size = size + (2*blocks);
1816 }
1817 else
1818 {
1819 /* integer overflow, do not split */
1820 p = sh_util_strdup(str);
1821 return p;
1822 }
1823
1824 p = SH_ALLOC(size);
1825 memset(p, 0, size);
1826
1827 p0 = p;
1828
1829 q = str;
1830 while (*q != '\0') {
1831 *p = *q;
1832 ++p;
1833 ++q;
1834 ++count;
1835 if (0 == (count % SPLIT_AT)) {
1836 count = 0;
1837 *p = '\r';
1838 ++p;
1839 *p = '\n';
1840 ++p;
1841 }
1842 }
1843 /* fprintf(stderr, "used = %d\n", strlen(p0)); */
1844
1845 return p0;
1846}
1847
1848
1849
1850/*****************************************************************
1851 *
1852 * MX Resolver Routines
1853 *
1854 *****************************************************************/
1855
1856#if defined(HAVE_ARPA_NAMESER_H)
1857
1858#include <netinet/in.h>
1859#ifdef __APPLE__
1860#define BIND_8_COMPAT 1
1861#endif
1862#ifndef S_SPLINT_S
1863#include <arpa/nameser.h>
1864#include <resolv.h>
1865#endif
1866#include <netdb.h>
1867#include <sys/socket.h>
1868#ifndef S_SPLINT_S
1869#include <arpa/inet.h>
1870#endif
1871
1872#include "sh_tools.h"
1873
1874#ifndef HFIXEDSZ
1875#define HFIXEDSZ 12
1876#endif
1877#ifndef QFIXEDSZ
1878#define QFIXEDSZ 4
1879#endif
1880
1881/*@unused@*//* used in get_mx() which is not parsed by splint */
1882static unsigned int get_short (unsigned char * loc)
1883{
1884 unsigned int retval = 0;
1885 if (loc)
1886 {
1887 /* byte order: MSB first
1888 */
1889 /*@+charint@*/
1890 retval = (((unsigned char) * loc) * 256) | ((unsigned char) * (loc + 1));
1891 /*@-charint@*/
1892 }
1893 return (retval);
1894}
1895
1896/* parser errors with splint */
1897#ifndef S_SPLINT_S
1898static dnsrep * get_mx (char *hostname)
1899{
1900 int ret, length, status;
1901 mx * result;
1902 size_t len;
1903
1904 typedef union
1905 {
1906 HEADER head;
1907 unsigned char buffer[4096];
1908 } querybuf;
1909
1910 querybuf reply;
1911 char expanded[1024];
1912 unsigned char * comp_dn, * eom;
1913 HEADER * header;
1914 int type, rdlength, pref;
1915 unsigned int count, index;
1916 dnsrep * retval;
1917
1918 SL_ENTER(_("get_mx"));
1919
1920 if (0 != res_init ())
1921 SL_RETURN (NULL, _("get_mx"));
1922
1923 errno = 0;
1924 length = res_query (hostname, C_IN, T_MX,
1925 (unsigned char *) &reply, 4095);
1926 if (length < 1)
1927 {
1928 /* error handling
1929 */
1930 if (length == -1)
1931 {
1932 if (errno == ECONNREFUSED)
1933 status = ECONNREFUSED;
1934 else
1935 status = h_errno;
1936
1937#ifdef FIL__
1938 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
1939 (errno == ECONNREFUSED) ?
1940 sh_error_message (status) :
1941 sh_tools_errmessage(status),
1942 _("res_query"));
1943#else
1944 if (errno == ECONNREFUSED)
1945 fprintf(stderr, " ERROR: %s: \n", strerror(errno));
1946 else
1947 fprintf(stderr, "HERROR: %s\n", hstrerror(h_errno));
1948#endif
1949 }
1950 SL_RETURN (NULL, _("get_mx"));
1951 }
1952
1953 ret = 0;
1954 header = (HEADER *) &reply;
1955
1956 /* start of data section
1957 */
1958 comp_dn = (unsigned char *) &reply + HFIXEDSZ;
1959
1960 /* end-of-message
1961 */
1962 eom = (unsigned char *) &reply + length;
1963
1964 /* HEADER NAME -- must be skipped or decompressed
1965 * TYPE -- type of data we got back, 16 bit integer
1966 * CLASS -- class we got back, also a 16 bit integer
1967 * TTL -- 32 bit time-to-live. just skip this
1968 * RDLENGTH -- length of the data to follow
1969 * RDATA -- the data:
1970 * PREF -- 16 bit preference
1971 * MX -- name of mail exchanger, must be decompressed
1972 */
1973
1974 /* Skip the query data.
1975 * QDCOUNT is the number of entries (unsigned 16 bit int).
1976 */
1977 count = ntohs (header->qdcount);
1978 for (index = 0; index < count; ++index)
1979 {
1980 ret = dn_skipname (comp_dn, eom);
1981 comp_dn += ret + QFIXEDSZ;
1982 if (ret < 1 || comp_dn >= eom)
1983 SL_RETURN (NULL, _("get_mx"));
1984 }
1985 count = ntohs (header->ancount);
1986 if (count < 1)
1987 SL_RETURN (NULL, _("get_mx"));
1988
1989 retval = SH_ALLOC (sizeof (dnsrep));
1990 if (!retval)
1991 SL_RETURN (NULL, _("get_mx"));
1992 retval->count = count;
1993
1994 /* allocate space for the results */
1995
1996 if (!sl_ok_muls(count, sizeof (mx)))
1997 {
1998 SH_FREE (retval);
1999 SL_RETURN (NULL, _("get_mx"));
2000 }
2001
2002 result = SH_ALLOC (count * sizeof (mx));
2003
2004 if (!result)
2005 {
2006 SH_FREE (retval);
2007 SL_RETURN (NULL, _("get_mx"));
2008 }
2009 retval->reply = result;
2010
2011 do
2012 {
2013 /* HEADER NAME
2014 */
2015 ret = dn_expand ((unsigned char *) &reply, eom, comp_dn,
2016 (char *) expanded, 1023);
2017 comp_dn += ret;
2018 if (ret < 1 || comp_dn >= eom)
2019 {
2020 SH_FREE (result);
2021 SH_FREE (retval);
2022 SL_RETURN (NULL, _("get_mx"));
2023 }
2024
2025 /* TYPE
2026 */
2027 type = get_short (comp_dn);
2028 comp_dn += 2;
2029 if (type != T_MX || comp_dn >= eom)
2030 {
2031 SH_FREE (result);
2032 SH_FREE (retval);
2033 SL_RETURN (NULL, _("get_mx"));
2034 }
2035
2036 /* CLASS (re-use 'type' var)
2037 */
2038 type = get_short (comp_dn);
2039 comp_dn += 2;
2040 if (comp_dn >= eom)
2041 {
2042 SH_FREE (result);
2043 SH_FREE (retval);
2044 SL_RETURN (NULL, _("get_mx"));
2045 }
2046
2047 /* TTL
2048 */
2049 comp_dn += 4;
2050 if (comp_dn >= eom)
2051 {
2052 SH_FREE (result);
2053 SH_FREE (retval);
2054 SL_RETURN (NULL, _("get_mx"));
2055 }
2056
2057 /* RDLENGTH
2058 */
2059 rdlength = get_short (comp_dn);
2060 comp_dn += 2;
2061 if (rdlength < 1 || comp_dn >= eom)
2062 {
2063 SH_FREE (result);
2064 SH_FREE (retval);
2065 SL_RETURN (NULL, _("get_mx"));
2066 }
2067
2068 /* RDATA
2069 */
2070 pref = get_short (comp_dn);
2071 comp_dn += 2;
2072 if (comp_dn >= eom)
2073 {
2074 SH_FREE (result);
2075 SH_FREE (retval);
2076 SL_RETURN (NULL, _("get_mx"));
2077 }
2078
2079 ret = dn_expand ((unsigned char *) &reply, eom, comp_dn,
2080 (char *) expanded, 1023);
2081 comp_dn += ret;
2082 if (ret < 1)
2083 {
2084 SH_FREE (result);
2085 SH_FREE (retval);
2086 SL_RETURN (NULL, _("get_mx"));
2087 }
2088 count--;
2089
2090 /* fill in the struct
2091 */
2092 result[count].pref = pref;
2093 len = strlen (expanded) + 1;
2094 result[count].address = SH_ALLOC (len);
2095 sl_strlcpy (result[count].address, expanded, len);
2096 }
2097 while (ret > 0 && comp_dn < eom && count);
2098
2099 SL_RETURN (retval, _("get_mx"));
2100}
2101/* ifndef S_SPLINT_S */
2102#endif
2103
2104/* #if defined(HAVE_ARPA_NAMESER_H) */
2105#endif
2106
2107
2108static int comp_mx_pref (const void * a, const void * b)
2109{
2110 const mx * ax = (const mx *) a;
2111 const mx * bx = (const mx *) b;
2112
2113 if (ax->pref > bx->pref)
2114 return 1;
2115 else if (ax->pref < bx->pref)
2116 return -1;
2117 else
2118 return 0;
2119}
2120
2121/*
2122 * return_mx returns a list of valid mail exchangers for domain
2123 */
2124static dnsrep * return_mx (char *domain)
2125{
2126 struct hostent *host = NULL;
2127 dnsrep * answers = NULL;
2128 mx * result;
2129 dnsrep * retval;
2130 char errmsg[128];
2131 size_t len;
2132
2133 SL_ENTER(_("return_mx"));
2134
2135#if defined(HAVE_ARPA_NAMESER_H)
2136 if (domain != NULL)
2137 answers = /*@-unrecog@*/get_mx (domain)/*@+unrecog@*/;
2138#endif
2139
2140 if (answers != NULL && answers->count > 0)
2141 {
2142 qsort(answers->reply, (size_t) answers->count, sizeof(mx),
2143 comp_mx_pref);
2144 SL_RETURN (answers, _("return_mx"));
2145 }
2146 else
2147 {
2148 if (domain != NULL)
2149 {
2150#if defined(HAVE_ARPA_NAMESER_H)
2151#ifdef FIL__
2152 (void) sl_strlcpy (errmsg, _("No MX record for domain "), 127);
2153 (void) sl_strlcat (errmsg, domain, 127);
2154 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2155 errmsg,
2156 _("get_mx"));
2157#else
2158 /* flawfinder: ignore *//* test code only */
2159 strcpy (errmsg, /* known to fit */
2160 _("No MX record for domain "));
2161 strncat (errmsg, domain, 100);
2162 errmsg[122] = '\0';
2163 fprintf(stderr, "Warning: %s\n", errmsg);
2164#endif
2165#endif
2166 }
2167 if (domain != NULL)
2168 host = /*@-unrecog@*/sh_gethostbyname (domain)/*@+unrecog@*/;
2169 if (!host)
2170 {
2171#ifdef FIL__
2172 (void) sl_strlcpy (errmsg, _("Unknown host "), 127);
2173 (void) sl_strlcat (errmsg, domain, 127);
2174 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
2175 errmsg,
2176 _("return_mx"));
2177#endif
2178 SL_RETURN (NULL, _("return_mx"));
2179 }
2180 result = SH_ALLOC (sizeof (mx));
2181 retval = SH_ALLOC (sizeof (dnsrep));
2182 retval->reply = result;
2183 retval->count = 1;
2184 result->pref = 0;
2185 /*@-type@*/
2186 len = strlen (host->h_name) + 1;
2187 result->address = SH_ALLOC (len);
2188 sl_strlcpy (result->address, host->h_name, len);
2189 /*@+type@*/
2190 SL_RETURN (retval, _("return_mx"));
2191 }
2192}
2193
2194static int free_mx (dnsrep * answers)
2195{
2196 mx * result;
2197 int i;
2198
2199 SL_ENTER(_("free_mx"));
2200 if (!answers)
2201 SL_RETURN (0, _("return_mx"));
2202
2203 result = answers->reply;
2204 for (i = 0; i < answers->count; ++i)
2205 {
2206 SH_FREE (result[i].address);
2207 }
2208 SH_FREE(result);
2209 SH_FREE(answers);
2210 SL_RETURN (0, _("return_mx"));
2211}
2212
2213#ifdef TEST_ONLY
2214int main(int argc, char * argv[])
2215{
2216 int i;
2217 dnsrep * answers;
2218 mx * result;
2219
2220 if (argc < 2)
2221 {
2222 fprintf(stderr, "Usage: dns <hostname>\n");
2223 return -1;
2224 }
2225 answers = return_mx(argv[1]);
2226
2227 if (!answers)
2228 {
2229 fprintf(stderr, "No answer\n");
2230 return -1;
2231 }
2232
2233 if (answers->count > 0)
2234 {
2235 result = answers->reply;
2236 for (i = 0; i < answers->count; ++i)
2237 {
2238 fprintf(stderr, "Record %3d: [%3d] %s\n", i,
2239 result[i].pref, result[i].address);
2240 }
2241 }
2242 else
2243 {
2244 fprintf(stderr, "No answer\n");
2245 free_mx(answers);
2246 return -1;
2247 }
2248 free_mx(answers);
2249 return (0);
2250}
2251#endif
2252
2253
2254
2255/* if defined(SH_WITH_MAIL) */
2256#endif
2257
2258
2259
Note: See TracBrowser for help on using the repository browser.