source: trunk/src/sh_mail.c@ 131

Last change on this file since 131 was 131, checked in by rainer, 17 years ago

Use thread-safe libc functions.

File size: 48.8 KB
RevLine 
[1]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
[20]95int sh_mail_sigverify (const char * s)
[1]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 */
[34]175 (void) sh_unix_getline (fd, key, (int)sizeof(key));
[1]176 key[KEY_LEN] = '\0';
177
[34]178 (void) sh_unix_getline (fd, number, (int)sizeof(number));
[1]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
[22]273int sh_filter_filteradd (const char * argstring,
274 sh_filter_type * filter, int ftype)
[1]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
[22]375int sh_filter_filter (const char * message, sh_filter_type * filter)
[1]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 */
[22]435int sh_mail_add_or (const char * str)
[1]436{
437 return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_OR));
438}
439
440/*
441 * -- add keywords to the AND filter
442 */
[22]443int sh_mail_add_and (const char * str)
[1]444{
445 return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_AND));
446}
447
448/*
449 * -- add keywords to the NOT filter
450 */
[22]451int sh_mail_add_not (const char * str)
[1]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
[22]476int sh_mail_setaddress (const char * address)
[1]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
[22]526int sh_mail_setaddress_int (const char * address)
[1]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
[22]536int sh_mail_setNum (const char * str)
[1]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
[22]552int sh_mail_setFlag (const char * str)
[1]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
[22]562int set_mail_subject (const char * str)
[1]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 */
[131]656static int sh_mail_end_conn (FILE * connfile, int fd);
657static FILE * sh_mail_start_conn (int aFlag, int * fd);
[1]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{
[34]744 size_t newsize;
745 char * outbuf = inbuf;
[1]746
747 SL_ENTER(_("sh_mail_realloc"));
748
[34]749 if (sl_ok_adds((*insize), 1))
750 {
751 newsize = (*insize) + 1;
[1]752
[34]753 if (sl_ok_adds(newsize, increase))
754 {
755 newsize += increase;
[1]756
[34]757 outbuf = SH_ALLOC(newsize);
758 MLOCK(outbuf, newsize);
759 (void) sl_strlcpy(outbuf, inbuf, newsize);
[1]760
[34]761 memset (inbuf, 0, (*insize));
762 MUNLOCK(inbuf, (*insize));
763 SH_FREE(inbuf);
764
765 *insize = newsize;
766 }
767 }
768
[1]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
[131]800 int ma_socket = -1;
801
[1]802 SH_FIFO * fifo_temp = NULL;
803
804 char * theSig;
805 char * theMsg = NULL;
806
807 /* #define SH_MAILBUF (256) */
808#define SH_MAILBUF (8*4096)
809
810 size_t msgbufsize = SH_MAILBUF;
811 size_t combufsize = SH_MAILBUF;
812
813 SL_ENTER(_("sh_mail_msg"));
814
815 if (ma_block == 1)
816 SL_RETURN( (0), _("sh_mail_msg"));
817
818 /* Return if we cannot mail.
819 */
820 if (failedMail == SL_TRUE)
821 SL_RETURN((-1), _("sh_mail_msg"));
822
823 if (failedMail == SL_FALSE && address_list[0] == NULL)
824 {
825 TPT((0, FIL__, __LINE__,
826 _("msg=<Mail error: no recipient address.>\n")));
827 failedMail = SL_TRUE;
828 SL_RETURN((-1), _("sh_mail_msg"));
829 }
830
831 if ( (success_time > 0) && (fail_time > 0) &&
832 (time(NULL) - success_time) > 3600*SH_MAX_FAIL)
833 {
834 ma_block = 1;
835 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
836 _("mail"), address_list[0]);
837 ma_block = 0;
838 sh_mail_emptystack();
839 sh.mailNum.alarm_last = 0;
840 failedMail = SL_TRUE;
841 SL_RETURN((-1), _("sh_mail_msg"));
842 }
843
844 /* Try at most each hour.
845 */
846 if ((fail_time > 0) && (time(NULL) - fail_time) < 3/*600*/)
847 {
848 if (failcount > 3)
849 {
850 /* -- Save for later. --
851 */
852 sh_mail_pushstack (message);
853 ++failcount;
854
855 SL_RETURN((-1), _("sh_mail_msg"));
856 }
857 else
858 {
859 (void) retry_msleep(2, 0);
860 ++failcount;
861 }
862 }
863
864 /* -- Reset time of last failure. --
865 */
866 fail_time = 0;
867
868 /* -- Polling, empty queue. --
869 */
870 if (message == NULL && sh.mailNum.alarm_last == 0)
871 SL_RETURN((-1), _("sh_mail_msg"));
872
873 /* -- Filtered. --
874 */
875 if (message != NULL && 0 != sh_filter_filter(message, &mail_filter))
876 SL_RETURN((-1), (_("sh_mail_msg")));
877
878
879
880 /* --------- Build complete message. ------------------------ */
881
882 theMsg = split_string(message);
883
884 /* ---------- Header ---------------------------------------- */
885
886 if (mail_subject == NULL)
887 {
888 (void) sl_strlcpy(mheader, _("Subject: "), sizeof(mheader)-5);
889 (void) sl_strlcat(mheader, sh_unix_time (0), sizeof(mheader)-5);
890 (void) sl_strlcat(mheader, " ", sizeof(mheader)-5);
891 (void) sl_strlcat(mheader, sh.host.name, sizeof(mheader)-5);
892 }
893 else
894 {
895
896 if (message == NULL)
897 {
898 theMsg = pop_list(fifo_mail);
899 message = theMsg;
900 if (message)
901 --sh.mailNum.alarm_last;
902 }
903
904 if (message)
905 {
906 sh_mail_get_subject(message, mheader, sizeof(mheader)-5);
907 }
908 else
909 {
910 (void) sl_strlcpy(mheader, _("Subject: "), sizeof(mheader)-5);
911 (void) sl_strlcat(mheader, sh_unix_time (0), sizeof(mheader)-5);
912 (void) sl_strlcat(mheader, " ", sizeof(mheader)-5);
913 (void) sl_strlcat(mheader, sh.host.name, sizeof(mheader)-5);
914 }
915 }
916
917 /* RFC 821: Header is terminated by an empty line
918 */
919 (void) sl_strlcat(mheader, "\015\012\015\012", sizeof(mheader));
920
921 /* ---------- Message --------------------------------------- */
922
923 (void) sl_strlcpy(subject, sh_unix_time (0), sizeof(subject));
924 (void) sl_strlcat(subject, " ", sizeof(subject));
925 (void) sl_strlcat(subject, sh.host.name, sizeof(subject));
926 (void) sl_strlcat(subject, "\r\n", sizeof(subject));
927
928
929 mailMsg = (char *) SH_ALLOC (msgbufsize);
930 bufcompress = (char *) SH_ALLOC (combufsize);
931
932 MLOCK(mailMsg , msgbufsize);
933 MLOCK(bufcompress , combufsize);
934
935 (void) sl_strlcpy(mailMsg, mheader, msgbufsize);
936 bufcompress[0] = '\0';
937
938 (void) sl_strlcat(mailMsg, _("-----BEGIN MESSAGE-----\r\n"), msgbufsize);
939 (void) sl_strlcat(mailMsg, subject, msgbufsize);
940 (void) sh_util_compress (bufcompress, subject,
941 (combufsize - KEY_LEN - 1));
942 if (message != NULL)
943 {
944 if ((sl_strlen(theMsg) + sl_strlen(mailMsg) + 1) >
945 (msgbufsize-(4*KEY_LEN)))
946 {
947 mailMsg = sh_mail_realloc(mailMsg,
948 &msgbufsize, sl_strlen(theMsg)+2);
949 bufcompress = sh_mail_realloc(bufcompress,
950 &combufsize, sl_strlen(theMsg));
951 }
952 (void) sl_strlcat(mailMsg, theMsg, msgbufsize-(4*KEY_LEN));
953 (void) sl_strlcat(mailMsg, "\r\n", msgbufsize-(4*KEY_LEN));
954
955 (void) sh_util_compress (bufcompress, theMsg, combufsize-KEY_LEN-1);
956 }
957
958 if (sh.mailNum.alarm_last > 0)
959 {
960 fifo_temp = SH_ALLOC (sizeof(SH_FIFO));
961 fifo_init (fifo_temp);
962
963 while ( NULL != (popMsg = pop_list(fifo_mail)) )
964 {
965 (void) push_list (fifo_temp, popMsg);
966
967 if ((sl_strlen(popMsg) + sl_strlen(mailMsg) + 1) >
968 (msgbufsize-(4*KEY_LEN)))
969 {
970 mailMsg = sh_mail_realloc(mailMsg,
971 &msgbufsize,
972 sl_strlen(popMsg)+2);
973 bufcompress = sh_mail_realloc(bufcompress,
974 &combufsize,
975 sl_strlen(popMsg));
976 }
977
978 (void) sl_strlcat(mailMsg, popMsg, msgbufsize-(4*KEY_LEN));
979 (void) sl_strlcat(mailMsg, "\r\n", msgbufsize-(4*KEY_LEN));
980 (void) sh_util_compress(bufcompress, popMsg, combufsize-KEY_LEN-1);
981 SH_FREE(popMsg);
982 --sh.mailNum.alarm_last;
983 ++num_popped;
984 }
985 }
986
987 /* ------ signature block ------------------------------------ */
988
989 (void) sl_strlcat(mailMsg, _("-----BEGIN SIGNATURE-----\r\n"), msgbufsize);
990
991 /* Generate new signature key.
992 */
993 if (isfirst == 1)
994 {
995 BREAKEXIT(sh_util_keyinit);
996 (void) sh_util_keyinit (skey->mailkey_old, KEY_LEN+1);
997 }
998
999 /* iterate the key
1000 */
1001 (void) sl_strlcpy(skey->mailkey_new,
1002 sh_tiger_hash (skey->mailkey_old, TIGER_DATA, KEY_LEN),
1003 KEY_LEN+1);
1004
1005 if (isfirst == 0)
1006 {
1007 /* Sign the message with the signature key.
1008 */
1009 theSig = sh_util_siggen (skey->mailkey_new,
1010 bufcompress, sl_strlen(bufcompress));
1011 (void) sl_strlcat (mailMsg,
1012 theSig,
1013 msgbufsize);
1014 }
1015 else
1016 {
1017 id_audit = time (NULL);
1018
1019 /* reveal first signature key
1020 */
[22]1021 /* flawfinder: ignore */
1022 (void) sl_strlcpy(skey->crypt, skey->mailkey_new, KEY_LEN+1);
[1]1023
1024 BREAKEXIT(sh_util_encode);
[22]1025 /* flawfinder: ignore */
[1]1026 sh_util_encode(skey->crypt, bufcompress, 0, 'A');
1027
[22]1028 /* flawfinder: ignore */
[1]1029 (void) sl_strlcat (mailMsg, skey->crypt, msgbufsize);
[22]1030 /* flawfinder: ignore */
[1]1031 memset (skey->crypt, 0, KEY_LEN);
1032 isfirst = 0;
1033 }
1034 (void) sl_strlcat (mailMsg, "\r\n", msgbufsize);
1035
1036 /* X(n) -> X(n-1)
1037 */
1038 (void) sl_strlcpy (skey->mailkey_old, skey->mailkey_new, KEY_LEN+1);
1039
[22]1040 sl_snprintf(subject, sizeof(subject), _("%06d %010ld::%s\r\n"),
1041 mailcount, (long) id_audit, sh.host.name);
[1]1042
1043 (void) sl_strlcat (mailMsg, subject, msgbufsize);
1044 ++mailcount;
1045
1046 (void) sl_strlcat (mailMsg, _("-----END MESSAGE-----"), msgbufsize);
1047
1048
1049
1050 /* ---------- Connect ---------------------------------------- */
1051
1052
1053
1054 /* -- Catch (ignore) 'broken pipe'.
1055 */
1056 new_act.sa_handler = SIG_IGN;
1057 sigemptyset( &new_act.sa_mask ); /* set an empty mask */
1058 new_act.sa_flags = 0; /* init sa_flags */
1059
1060 (void) sigaction (SIGPIPE, &new_act, &old_act);
1061
1062 i = 0;
1063 errcount = 0;
1064
1065 if (all_in_one == S_FALSE)
1066 {
1067 while (address_list[i] != NULL && i < address_num)
1068 {
[131]1069 connfile = sh_mail_start_conn (i, &ma_socket);
[1]1070
1071 if (NULL != connfile)
1072 {
1073 wrlen = fwrite (mailMsg, 1, sl_strlen(mailMsg),
1074 connfile);
1075 wrlen -= sl_strlen(mailMsg);
1076 if (wrlen == 0)
[131]1077 status = sh_mail_end_conn (connfile, ma_socket);
[1]1078 else
1079 status = -1;
1080 }
1081 if (NULL == connfile || status != 0)
1082 {
1083 ma_block = 1;
1084 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
1085 _("mail"), address_list[i]);
1086 ma_block = 0;
1087 ++errcount;
1088 ++sh.statistics.mail_failed;
1089 }
1090 else
1091 {
1092 ++sh.statistics.mail_success;
1093 }
1094
1095 if (connfile != NULL)
1096 {
1097 (void) fclose (connfile);
1098 connfile = NULL;
1099 }
1100 ++i;
1101 }
1102 }
1103 else
1104 {
[131]1105 connfile = sh_mail_start_conn ( -9 , &ma_socket);
[1]1106
1107 if (NULL != connfile)
1108 {
1109 wrlen = fwrite (mailMsg, 1, sl_strlen(mailMsg), connfile);
1110 wrlen -= sl_strlen(mailMsg);
1111 if (wrlen == 0)
[131]1112 status = sh_mail_end_conn (connfile, ma_socket);
[1]1113 else
1114 status = -1;
1115 }
1116 if (NULL == connfile || status != 0)
1117 {
1118 ma_block = 1;
1119 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
1120 _("mail"), address_list[0]);
1121 ma_block = 0;
1122 errcount = address_num;
1123 ++sh.statistics.mail_failed;
1124 }
1125 else
1126 {
1127 ++sh.statistics.mail_success;
1128 }
1129
1130 if (connfile != NULL)
1131 {
1132 (void) fclose (connfile);
1133 connfile = NULL;
1134 }
1135 }
1136
1137
1138 memset (bufcompress, 0, combufsize);
1139 MUNLOCK(bufcompress , combufsize);
1140 SH_FREE(bufcompress);
1141
1142 memset (mailMsg, 0, msgbufsize);
1143 MUNLOCK(mailMsg , msgbufsize);
1144 SH_FREE(mailMsg);
1145
1146 /* --- Stay responsible for delivery in case of failure --- */
1147
1148 if (errcount == address_num && fifo_temp != NULL)
1149 {
1150 while ( (NULL != (popMsg = pop_list(fifo_temp))) )
1151 {
1152 if (push_list (fifo_mail, popMsg) >= 0)
1153 ++sh.mailNum.alarm_last;
1154 SH_FREE(popMsg);
1155 }
1156 if (message != NULL)
1157 {
1158 if (fifo_mail == NULL)
1159 {
1160 fifo_mail = SH_ALLOC(sizeof(SH_FIFO));
1161 fifo_init(fifo_mail);
1162 }
1163 retval = push_list (fifo_mail, theMsg);
1164 if (retval >= 0)
1165 ++sh.mailNum.alarm_last;
1166 if (retval == SH_FIFO_MAX)
1167 retval = -2;
1168 else
1169 retval = -1;
1170 }
1171 }
1172 else if (fifo_temp != NULL)
1173 {
1174 while ( (NULL != (popMsg = pop_list(fifo_temp))) )
1175 {
1176 SH_FREE(popMsg);
1177 }
1178 }
1179 if (fifo_temp != NULL)
1180 SH_FREE(fifo_temp);
1181
1182 /*
1183 if (connfile != NULL)
1184 fclose (connfile);
1185 */
1186
1187 if (theMsg != NULL)
1188 SH_FREE(theMsg);
1189
1190 /* --- Reset signal. ---
1191 */
1192 (void) sigaction (SIGPIPE, &old_act, NULL);
1193
1194 if (errcount == address_num)
1195 {
1196 fail_time = time(NULL);
1197 SL_RETURN((retval), _("sh_mail_msg"));
1198 }
1199 success_time = time(NULL);
1200 failcount = 0;
1201
1202 SL_RETURN((0), _("sh_mail_msg"));
1203}
1204
1205
1206/*
1207 *
1208 * SMTP CODE BELOW
1209 *
1210 *
1211 */
1212
1213#include <ctype.h>
1214#ifdef HOST_IS_HPUX
1215#define _XOPEN_SOURCE_EXTENDED
1216#endif
1217#include <netdb.h>
1218#include <sys/types.h>
1219#include <sys/socket.h>
1220#include <netinet/in.h>
1221#ifndef S_SPLINT_S
1222#include <arpa/inet.h>
1223#else
1224#define AF_INET 2
1225#endif
1226
1227#define SH_NEED_GETHOSTBYXXX
1228#include "sh_static.h"
1229
1230/* missing on HP-UX 10.20 */
1231#ifndef IPPORT_SMTP
1232#define IPPORT_SMTP 25
1233#endif
1234
1235static int sh_mail_wait(int code, FILE * m_socket);
1236
1237static char * relay_host = NULL;
1238
[22]1239int sh_mail_set_relay (const char * str_s)
[1]1240{
1241 SL_ENTER(_("sh_mail_set_relay"));
1242
1243 if (str_s == NULL)
1244 SL_RETURN( -1, _("sh_mail_set_relay"));
1245
1246 if (relay_host != NULL)
[34]1247 {
1248 SH_FREE (relay_host);
1249 relay_host = NULL;
1250 }
[1]1251
1252 if (0 == sl_strncmp(str_s, _("NULL"), 4))
1253 {
1254 SL_RETURN( 0, _("sh_mail_set_relay"));
1255 }
1256
[34]1257 relay_host = sh_util_strdup(str_s);
1258
[1]1259 SL_RETURN( 0, _("sh_mail_set_relay"));
1260}
1261
1262static char * mail_sender = NULL;
1263
[22]1264int sh_mail_set_sender (const char *str)
[1]1265{
1266 if (mail_sender != NULL)
1267 {
1268 SH_FREE (mail_sender);
1269 mail_sender = NULL;
1270 }
1271 if (str != NULL)
1272 {
1273 mail_sender = sh_util_strdup (str);
1274 }
1275 if (mail_sender == NULL)
1276 {
1277 return -1;
1278 }
1279 return 0;
1280}
1281
1282
1283/*************************
1284 *
1285 * start connection
1286 * for details on SMTP, see RFC 821
1287 */
1288
1289static time_t time_wait = 300;
1290
[131]1291static FILE * sh_mail_start_conn (int aFlag, int * ma_socket)
[1]1292{
1293 char * address;
1294
1295 int ecount;
1296
1297 char this_address[256];
1298 char ma_machine[256];
1299 char ma_user[256];
1300 char error_msg[256];
1301 char error_call[SH_MINIBUF];
1302 int error_num = 0;
1303 register int i, j, k;
1304 FILE * connFile = NULL;
1305 struct tm * my_tm;
[131]1306#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
1307 struct tm time_tm;
1308#endif
[1]1309 time_t my_time;
1310 char my_tbuf[128];
1311
1312 int fd;
1313
1314 dnsrep * answers;
1315 mx * result;
1316
1317 SL_ENTER(_("sh_mail_start_conn"));
1318
[131]1319 *ma_socket = -1;
1320 time_wait = 300;
[1]1321
1322 if (aFlag >= 0)
1323 address = address_list[aFlag];
1324 else
1325 address = address_list[0];
1326
1327 TPT(( 0, FIL__, __LINE__, _("msg=<aFlag %d address %s>\n"),
1328 aFlag, address));
1329
1330 /* ------- split adress ------------------ */
1331
1332 if (0 == strcmp(address, _("NULL")))
1333 {
1334 SL_RETURN( NULL, _("sh_mail_start_conn"));
1335 }
1336
1337 if (strchr (address, '@') == NULL) {
1338 (void) sl_strlcpy(ma_user, address, 256);
1339 (void) sl_strlcpy(ma_machine, _("localhost"), 256);
1340 } else {
1341 i = 0;
1342 while (i < 255 && address[i] != '@') {
1343 ma_user[i] = address[i];
1344 ++i;
1345 }
1346
1347 /* adress[i] = '@'
1348 */
1349 ma_user[i] = '\0';
1350 j = i + 1; k = i; i = 0;
1351 while (i < 255 && address[i+j] != '\0') {
1352 ma_machine[i] = address[i+j];
1353 ++i;
1354 }
1355 ma_machine[i] = '\0';
1356 if (address[k] != '@' || address[k+i+1] != '\0')
1357 {
1358 SL_RETURN( NULL, _("sh_mail_start_conn"));
1359 }
1360 }
1361
1362
1363 if (relay_host != NULL)
1364 {
1365 (void) sl_strlcpy (ma_machine, relay_host, sizeof(ma_machine));
1366 TPT((0, FIL__, __LINE__, _("msg=<user %s machine %s>\n"),
1367 ma_user, ma_machine));
1368 fd = connect_port (ma_machine, IPPORT_SMTP,
1369 error_call, &error_num, error_msg, 256);
1370 }
1371 else
1372 {
1373 answers = return_mx (ma_machine);
1374 if (answers)
1375 {
1376 result = answers->reply;
1377 fd = -1;
1378 for (i = 0; i < answers->count; ++i)
1379 {
1380 (void) sl_strlcpy(ma_machine, result[i].address,
1381 sizeof(ma_machine));
1382 TPT((0, FIL__, __LINE__,
1383 _("msg=<user %s mx %s pref %d>\n"),
1384 ma_user, ma_machine, result[i].pref));
1385 fd = connect_port (ma_machine, IPPORT_SMTP,
1386 error_call, &error_num, error_msg, 256);
1387 if (fd >= 0)
1388 break;
1389 }
1390 (void) free_mx(answers);
1391 }
1392 else
1393 {
1394 (void) sl_strlcpy(error_call, _("return_mx"), SH_MINIBUF);
1395 (void) sl_strlcpy(error_msg, _("The specified host is unknown: "),
1396 256);
1397 (void) sl_strlcat(error_msg, ma_machine, 256);
1398 fd = -1;
1399 }
1400 }
1401
1402
1403 if (fd < 0)
1404 {
1405 sh_error_handle ((-1), FIL__, __LINE__, error_num,
1406 MSG_E_NET, error_msg, error_call,
1407 _("email"), ma_machine);
1408 SL_RETURN( NULL, _("sh_mail_start_conn"));
1409 }
1410
1411 /* associate a FILE structure with it
1412 */
1413 connFile = fdopen (fd, "r+");
1414 if (connFile == NULL)
1415 {
1416 TPT(( 0, FIL__, __LINE__, _("msg=<fdopen() failed>\n")));
1417 (void) close(fd);
1418 SL_RETURN( NULL, _("sh_mail_start_conn"));
1419 }
1420
1421
1422 /* say HELO to the other socket
1423 */
[131]1424 if (0 == sh_mail_wait (220, fd))
[1]1425 {
1426 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1427 _("Timeout on SMTP session init"),
1428 _("sh_mail_start_conn"),
1429 _("mail"), sh.host.name);
1430 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
1431 (void) fclose(connFile);
1432 SL_RETURN( NULL, _("sh_mail_start_conn"));
1433 }
1434
1435 (void) fflush(connFile);
1436
1437 if (0 != is_numeric(sh.host.name))
1438 {
1439 TPT(( 0, FIL__, __LINE__, _("msg=<HELO [%s]>%c%c"),
1440 sh.host.name, 13, 10));
1441 }
1442 else
1443 {
1444 TPT(( 0, FIL__, __LINE__, _("msg=<HELO %s>%c%c"),
1445 sh.host.name, 13, 10));
1446 }
1447 if (0 != is_numeric(sh.host.name))
1448 fprintf(connFile, _("HELO [%s]%c%c"), sh.host.name, 13, 10);
1449 else
1450 fprintf(connFile, _("HELO %s%c%c"), sh.host.name, 13, 10);
1451
1452 (void) fflush(connFile);
1453
[131]1454 if (0 == sh_mail_wait(250, fd))
[1]1455 {
1456 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1457 _("HELO failed"), _("sh_mail_start_conn"),
1458 _("mail"), sh.host.name);
1459
1460 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1461 (void) fclose(connFile);
1462 SL_RETURN( NULL, _("sh_mail_start_conn"));
1463 }
1464
1465 /* tell them who we are
1466 */
1467 (void) sl_strlcpy (this_address,
1468 mail_sender ? mail_sender : DEFAULT_SENDER, 256);
1469 if (NULL == strchr(this_address, '@'))
1470 {
1471 (void) sl_strlcat (this_address, "@", 256);
1472 if (0 != is_numeric(sh.host.name))
1473 (void) sl_strlcat (this_address, _("example.com"), 256);
1474 else
1475 (void) sl_strlcat (this_address, sh.host.name, 256);
1476 }
1477
1478 TPT(( 0, FIL__, __LINE__, _("msg=<MAIL FROM:<%s>>%c%c"),
1479 this_address, 13, 10));
1480
1481 (void) fflush(connFile);
1482 /*@-usedef@*/
1483 fprintf(connFile, _("MAIL FROM:<%s>%c%c"), this_address, 13, 10);
1484 /*@+usedef@*/
1485 (void) fflush(connFile);
1486
[131]1487 if (0 == sh_mail_wait(250, fd))
[1]1488 {
1489 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1490 _("MAIL FROM failed"), _("sh_mail_start_conn"),
1491 _("mail"), this_address);
1492 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1493 (void) fclose(connFile);
1494 SL_RETURN( NULL, _("sh_mail_start_conn"));
1495 }
1496
1497 /* tell them who to send mail to
1498 */
1499 if (aFlag >= 0)
1500 {
1501 TPT(( 0, FIL__, __LINE__, _("msg=<RCPT TO:<%s>>%c%c"),
1502 address, 13, 10));
1503
1504 (void) fflush(connFile);
1505 fprintf(connFile, _("RCPT TO:<%s>%c%c"), address, 13, 10);
1506 (void) fflush(connFile);
1507
[131]1508 if (0 == sh_mail_wait(250, fd))
[1]1509 {
1510 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1511 _("RCPT TO failed"), _("sh_mail_start_conn"),
1512 _("mail"), address);
1513 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1514 (void) fclose(connFile);
1515 SL_RETURN( NULL, _("sh_mail_start_conn"));
1516 }
1517 }
1518 else
1519 {
1520 ecount = 0;
1521 for (i = 0; i < address_num; ++i)
1522 {
1523 if (address_list[i] == NULL) /* paranoia */
1524 break;
1525 TPT(( 0, FIL__, __LINE__, _("msg=<RCPT TO:<%s>>%c%c"),
1526 address_list[i], 13, 10));
1527
1528 (void) fflush(connFile);
1529 fprintf(connFile, _("RCPT TO:<%s>%c%c"), address_list[i], 13, 10);
1530 (void) fflush(connFile);
1531
[131]1532 if (0 == sh_mail_wait(250, fd))
[1]1533 {
1534 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1535 _("RCPT TO failed"), _("sh_mail_start_conn"),
1536 _("mail"), address_list[i]);
1537
1538 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1539 ++ecount;
1540 }
1541 }
1542 if (ecount == address_num)
1543 {
1544 (void) fclose(connFile);
1545 SL_RETURN( NULL, _("sh_mail_start_conn"));
1546 }
1547 }
1548
1549 /* Send the message
1550 */
1551 TPT(( 0, FIL__, __LINE__, _("msg=<DATA>%c%c"), 13, 10));
1552
1553 (void) fflush(connFile);
1554 fprintf(connFile, _("DATA%c%c"), 13, 10);
1555 (void) fflush(connFile);
1556
[131]1557 if (0 == sh_mail_wait(354, fd))
[1]1558 {
1559 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1560 _("DATA failed"), _("sh_mail_start_conn"),
1561 _("mail"), address);
1562 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
1563 (void) fclose(connFile);
1564 SL_RETURN( NULL, _("sh_mail_start_conn"));
1565 }
1566
1567
1568 my_time = time(NULL);
[131]1569#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
1570 my_tm = localtime_r(&my_time, &time_tm);
1571#else
[1]1572 my_tm = localtime(&my_time);
[131]1573#endif
[1]1574 (void) strftime(my_tbuf, 127, _("%a, %d %b %Y %H:%M:%S %Z"), my_tm);
1575
1576 TPT(( 0, FIL__, __LINE__, _("msg=<From: <%s>%c%cTo: <%s>%c%cDate: %s>%c%c"),
1577 this_address, 13, 10, address, 13, 10, my_tbuf, 13, 10));
1578
1579 (void) fflush(connFile);
1580 fprintf(connFile,
1581 _("From: <%s>%c%c"\
1582 "To: <%s>%c%c"\
1583 "Date: %s%c%c"),
1584 this_address, 13, 10,
1585 address, 13, 10,
1586 my_tbuf, 13, 10);
1587
[131]1588 *ma_socket = fd;
[1]1589 SL_RETURN( connFile, _("sh_mail_start_conn"));
1590}
1591
1592/*************************
1593 *
1594 * end connection
1595 *
1596 */
1597
[131]1598static int sh_mail_end_conn (FILE * connFile, int fd)
[1]1599{
1600 SL_ENTER(_("sh_mail_end_conn"));
1601
1602 time_wait = 300;
1603
1604 (void) fflush(connFile);
1605 fprintf(connFile, _("%c%c.%c%c"), 13, 10, 13, 10);
1606 (void) fflush(connFile);
1607
1608 TPT(( 0, FIL__, __LINE__, _("msg=<message end written>\n")));
1609
[131]1610 if (0 != sh_mail_wait(250, fd))
[1]1611 {
1612 (void) fflush(connFile);
1613 fprintf(connFile, _("QUIT%c%c"), 13, 10);
1614 (void) fflush(connFile);
1615 TPT(( 0, FIL__, __LINE__, _("msg=<exit>\n")));
1616
1617 SL_RETURN (0, _("sh_mail_end_conn"));
1618 }
1619
1620 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1621 _("QUIT failed"), _("sh_mail_end_conn"),
1622 _("mail"), _("SMTP server"));
1623
1624 TPT(( 0, FIL__, __LINE__, _("msg=<abnormal exit>\n")));
1625
1626 SL_RETURN ((-1), _("sh_mail_end_conn"));
1627}
1628
1629
1630
1631/****************************
1632 *
1633 * Handle server replies
1634 *
1635 *
1636 */
1637
[131]1638static int sh_mail_wait(int code, int ma_socket)
[1]1639{
[131]1640 int rcode, g;
[1]1641
[131]1642 char c;
[1]1643
1644 char errmsg[128];
1645
1646 enum {
1647 WAIT_CODE_START,
1648 WAIT_CODE,
1649 WAIT_NL,
1650 WAIT_NL_CONT
1651 } state;
1652
1653 time_t waited_time;
1654
1655 SL_ENTER(_("mail_wait"));
1656
1657 waited_time = time(NULL);
1658
1659 /* timeout after 5 minutes
1660 */
1661
1662 rcode = 0;
1663 state = WAIT_CODE_START;
1664
[131]1665 while (sl_read_timeout_fd (ma_socket, &c, 1, time_wait, SL_FALSE) > 0) {
[1]1666
[131]1667 g = (int) c;
1668
[1]1669 if ( (g=fgetc(ma_socket)) == EOF)
1670 {
1671 TPT((0, FIL__, __LINE__, _("msg=<mail_wait: EOF>\n")));
1672 SL_RETURN( 0, _("mail_wait"));
1673 }
1674
1675 switch(state) {
1676
1677 /* wait for start of a numerical code
1678 */
1679 case WAIT_CODE_START:
1680 if (0 != isspace(g))
1681 break; /* Skip white space */
1682 if (0 == isdigit(g))
1683 return 0; /* No leading number */
1684 rcode = g-(int)'0'; /* convert to number */
1685 state = WAIT_CODE;
1686 break;
1687
1688 /* wait for completion of numerical code
1689 */
1690 case WAIT_CODE:
1691 if (0 != isdigit(g)) {
1692 rcode = rcode * 10 + (g-(int)'0'); /* next digit */
1693 break;
1694 }
1695 /*@+charintliteral@*/
1696 state = ((g == '-') ? WAIT_NL_CONT : WAIT_NL);
1697 /*@-charintliteral@*/
1698 break;
1699
1700 /* wait for newline, then return with status code
1701 */
1702 case WAIT_NL:
1703 /*@+charintliteral@*/
1704 if (g != '\n')
1705 break;
1706 /*@-charintliteral@*/
1707
1708 TPT((0, FIL__, __LINE__,
1709 _("msg=<mail_wait: OK got %d (%d) need %d (%d)>\n"),
1710 rcode, (int)(rcode/100), code, (int)(code/100) ));
1711 g = ((int)(rcode/100) == (int)(code/100)) ? 1 : 0;
1712 if (g != 1)
1713 {
[22]1714 sl_snprintf(errmsg, sizeof(errmsg),
1715 _("Bad response (%d), expected %d"), rcode, code);
1716
[1]1717 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET,
1718 errmsg, _("sh_mail_wait"),
1719 _("mail"), _("SMTP server"));
1720 }
1721 waited_time = time(NULL) - waited_time;
1722 time_wait -= waited_time;
1723 TPT((0, FIL__, __LINE__,
1724 _("msg=<mail_wait: time_wait reduced to %d sec>\n"),
1725 (int) time_wait));
1726 SL_RETURN( (g), _("mail_wait")) ;
1727
1728 /* wait for continuation line
1729 */
1730 /*@fallthrough@*//* no, but splint doesn't understand */
1731 case WAIT_NL_CONT:
1732 /*@+charintliteral@*/
1733 if (g == '\n')
1734 state = WAIT_CODE_START; /* There is a continuation line */
1735 /*@-charintliteral@*/
1736 break;
1737
1738 default:
1739
1740 TPT((0, FIL__, __LINE__, _("msg=<mail_wait: bad>\n")));
1741 SL_RETURN( 0, _("mail_wait"));
1742
1743 }
1744 }
1745
1746 TPT((0, FIL__, __LINE__, _("msg=<mail_wait: failed>\n")));
1747
1748 /* Failed, EOF or error on socket */
1749 SL_RETURN( 0, _("mail_wait"));
1750}
1751
1752/* -- function to insert "\r\n" after each 998 chars --
1753 */
1754
1755#define SPLIT_AT 998
1756
1757static char * split_string(char * str)
1758{
1759 size_t size;
1760 size_t blocks;
1761 int count = 0;
1762
1763 char * p, * p0;
1764 char * q;
1765
1766 if (!str)
1767 return NULL;
1768
[34]1769 size = strlen(str) + 1;
[1]1770 blocks = 1 + (size / SPLIT_AT);
1771
[34]1772 if (sl_ok_muls(2, blocks) && sl_ok_adds(size, (2*blocks)))
1773 {
1774 size = size + (2*blocks);
1775 }
1776 else
1777 {
1778 /* integer overflow, do not split */
1779 p = sh_util_strdup(str);
1780 return p;
1781 }
1782
[1]1783 p = SH_ALLOC(size);
1784 memset(p, 0, size);
[34]1785
[1]1786 p0 = p;
1787
1788 q = str;
1789 while (*q != '\0') {
1790 *p = *q;
1791 ++p;
1792 ++q;
1793 ++count;
1794 if (0 == (count % SPLIT_AT)) {
1795 count = 0;
1796 *p = '\r';
1797 ++p;
1798 *p = '\n';
1799 ++p;
1800 }
1801 }
1802 /* fprintf(stderr, "used = %d\n", strlen(p0)); */
1803
1804 return p0;
1805}
1806
1807
1808
1809/*****************************************************************
1810 *
1811 * MX Resolver Routines
1812 *
1813 *****************************************************************/
1814
1815#if defined(HAVE_ARPA_NAMESER_H)
1816
1817#include <netinet/in.h>
1818#ifdef __APPLE__
1819#define BIND_8_COMPAT 1
1820#endif
1821#ifndef S_SPLINT_S
1822#include <arpa/nameser.h>
1823#include <resolv.h>
1824#endif
1825#include <netdb.h>
1826#include <sys/socket.h>
1827#ifndef S_SPLINT_S
1828#include <arpa/inet.h>
1829#endif
1830
1831#include "sh_tools.h"
1832
1833#ifndef HFIXEDSZ
1834#define HFIXEDSZ 12
1835#endif
1836#ifndef QFIXEDSZ
1837#define QFIXEDSZ 4
1838#endif
1839
1840/*@unused@*//* used in get_mx() which is not parsed by splint */
1841static unsigned int get_short (unsigned char * loc)
1842{
1843 unsigned int retval = 0;
1844 if (loc)
1845 {
1846 /* byte order: MSB first
1847 */
1848 /*@+charint@*/
1849 retval = (((unsigned char) * loc) * 256) | ((unsigned char) * (loc + 1));
1850 /*@-charint@*/
1851 }
1852 return (retval);
1853}
1854
1855/* parser errors with splint */
1856#ifndef S_SPLINT_S
1857static dnsrep * get_mx (char *hostname)
1858{
1859 int ret, length, status;
1860 mx * result;
[22]1861 size_t len;
[1]1862
1863 typedef union
1864 {
1865 HEADER head;
1866 unsigned char buffer[4096];
1867 } querybuf;
1868
1869 querybuf reply;
1870 char expanded[1024];
1871 unsigned char * comp_dn, * eom;
1872 HEADER * header;
[34]1873 int type, rdlength, pref;
1874 unsigned int count, index;
[1]1875 dnsrep * retval;
1876
1877 SL_ENTER(_("get_mx"));
1878
1879 if (0 != res_init ())
1880 SL_RETURN (NULL, _("get_mx"));
1881
1882 errno = 0;
1883 length = res_query (hostname, C_IN, T_MX,
1884 (unsigned char *) &reply, 4095);
1885 if (length < 1)
1886 {
1887 /* error handling
1888 */
1889 if (length == -1)
1890 {
1891 if (errno == ECONNREFUSED)
1892 status = ECONNREFUSED;
1893 else
1894 status = h_errno;
1895
1896#ifdef FIL__
1897 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
1898 (errno == ECONNREFUSED) ?
1899 sh_error_message (status) :
1900 sh_tools_errmessage(status),
1901 _("res_query"));
1902#else
1903 if (errno == ECONNREFUSED)
1904 fprintf(stderr, " ERROR: %s: \n", strerror(errno));
1905 else
1906 fprintf(stderr, "HERROR: %s\n", hstrerror(h_errno));
1907#endif
1908 }
1909 SL_RETURN (NULL, _("get_mx"));
1910 }
1911
1912 ret = 0;
1913 header = (HEADER *) &reply;
1914
1915 /* start of data section
1916 */
1917 comp_dn = (unsigned char *) &reply + HFIXEDSZ;
1918
1919 /* end-of-message
1920 */
1921 eom = (unsigned char *) &reply + length;
1922
1923 /* HEADER NAME -- must be skipped or decompressed
1924 * TYPE -- type of data we got back, 16 bit integer
1925 * CLASS -- class we got back, also a 16 bit integer
1926 * TTL -- 32 bit time-to-live. just skip this
1927 * RDLENGTH -- length of the data to follow
1928 * RDATA -- the data:
1929 * PREF -- 16 bit preference
1930 * MX -- name of mail exchanger, must be decompressed
1931 */
1932
1933 /* Skip the query data.
1934 * QDCOUNT is the number of entries (unsigned 16 bit int).
1935 */
1936 count = ntohs (header->qdcount);
1937 for (index = 0; index < count; ++index)
1938 {
1939 ret = dn_skipname (comp_dn, eom);
1940 comp_dn += ret + QFIXEDSZ;
1941 if (ret < 1 || comp_dn >= eom)
1942 SL_RETURN (NULL, _("get_mx"));
1943 }
1944 count = ntohs (header->ancount);
1945 if (count < 1)
1946 SL_RETURN (NULL, _("get_mx"));
1947
1948 retval = SH_ALLOC (sizeof (dnsrep));
1949 if (!retval)
1950 SL_RETURN (NULL, _("get_mx"));
1951 retval->count = count;
1952
1953 /* allocate space for the results */
1954
[34]1955 if (!sl_ok_muls(count, sizeof (mx)))
1956 {
1957 SH_FREE (retval);
1958 SL_RETURN (NULL, _("get_mx"));
1959 }
1960
[1]1961 result = SH_ALLOC (count * sizeof (mx));
[34]1962
[1]1963 if (!result)
1964 {
1965 SH_FREE (retval);
1966 SL_RETURN (NULL, _("get_mx"));
1967 }
1968 retval->reply = result;
1969
1970 do
1971 {
1972 /* HEADER NAME
1973 */
1974 ret = dn_expand ((unsigned char *) &reply, eom, comp_dn,
1975 (char *) expanded, 1023);
1976 comp_dn += ret;
1977 if (ret < 1 || comp_dn >= eom)
1978 {
1979 SH_FREE (result);
1980 SH_FREE (retval);
1981 SL_RETURN (NULL, _("get_mx"));
1982 }
1983
1984 /* TYPE
1985 */
1986 type = get_short (comp_dn);
1987 comp_dn += 2;
1988 if (type != T_MX || comp_dn >= eom)
1989 {
1990 SH_FREE (result);
1991 SH_FREE (retval);
1992 SL_RETURN (NULL, _("get_mx"));
1993 }
1994
1995 /* CLASS (re-use 'type' var)
1996 */
1997 type = get_short (comp_dn);
1998 comp_dn += 2;
1999 if (comp_dn >= eom)
2000 {
2001 SH_FREE (result);
2002 SH_FREE (retval);
2003 SL_RETURN (NULL, _("get_mx"));
2004 }
2005
2006 /* TTL
2007 */
2008 comp_dn += 4;
2009 if (comp_dn >= eom)
2010 {
2011 SH_FREE (result);
2012 SH_FREE (retval);
2013 SL_RETURN (NULL, _("get_mx"));
2014 }
2015
2016 /* RDLENGTH
2017 */
2018 rdlength = get_short (comp_dn);
2019 comp_dn += 2;
2020 if (rdlength < 1 || comp_dn >= eom)
2021 {
2022 SH_FREE (result);
2023 SH_FREE (retval);
2024 SL_RETURN (NULL, _("get_mx"));
2025 }
2026
2027 /* RDATA
2028 */
2029 pref = get_short (comp_dn);
2030 comp_dn += 2;
2031 if (comp_dn >= eom)
2032 {
2033 SH_FREE (result);
2034 SH_FREE (retval);
2035 SL_RETURN (NULL, _("get_mx"));
2036 }
2037
2038 ret = dn_expand ((unsigned char *) &reply, eom, comp_dn,
2039 (char *) expanded, 1023);
2040 comp_dn += ret;
2041 if (ret < 1)
2042 {
2043 SH_FREE (result);
2044 SH_FREE (retval);
2045 SL_RETURN (NULL, _("get_mx"));
2046 }
2047 count--;
2048
2049 /* fill in the struct
2050 */
2051 result[count].pref = pref;
[22]2052 len = strlen (expanded) + 1;
2053 result[count].address = SH_ALLOC (len);
2054 sl_strlcpy (result[count].address, expanded, len);
[1]2055 }
2056 while (ret > 0 && comp_dn < eom && count);
2057
2058 SL_RETURN (retval, _("get_mx"));
2059}
2060/* ifndef S_SPLINT_S */
2061#endif
2062
2063/* #if defined(HAVE_ARPA_NAMESER_H) */
2064#endif
2065
2066
2067static int comp_mx_pref (const void * a, const void * b)
2068{
2069 const mx * ax = (const mx *) a;
2070 const mx * bx = (const mx *) b;
2071
2072 if (ax->pref > bx->pref)
2073 return 1;
2074 else if (ax->pref < bx->pref)
2075 return -1;
2076 else
2077 return 0;
2078}
2079
2080/*
2081 * return_mx returns a list of valid mail exchangers for domain
2082 */
2083static dnsrep * return_mx (char *domain)
2084{
2085 struct hostent *host = NULL;
2086 dnsrep * answers = NULL;
2087 mx * result;
2088 dnsrep * retval;
[22]2089 char errmsg[128];
2090 size_t len;
[1]2091
2092 SL_ENTER(_("return_mx"));
2093
2094#if defined(HAVE_ARPA_NAMESER_H)
2095 if (domain != NULL)
2096 answers = /*@-unrecog@*/get_mx (domain)/*@+unrecog@*/;
2097#endif
2098
2099 if (answers != NULL && answers->count > 0)
2100 {
2101 qsort(answers->reply, (size_t) answers->count, sizeof(mx),
2102 comp_mx_pref);
2103 SL_RETURN (answers, _("return_mx"));
2104 }
2105 else
2106 {
2107 if (domain != NULL)
2108 {
2109#if defined(HAVE_ARPA_NAMESER_H)
2110#ifdef FIL__
2111 (void) sl_strlcpy (errmsg, _("No MX record for domain "), 127);
2112 (void) sl_strlcat (errmsg, domain, 127);
2113 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2114 errmsg,
2115 _("get_mx"));
2116#else
[22]2117 /* flawfinder: ignore *//* test code only */
[1]2118 strcpy (errmsg, /* known to fit */
2119 _("No MX record for domain "));
2120 strncat (errmsg, domain, 100);
2121 errmsg[122] = '\0';
2122 fprintf(stderr, "Warning: %s\n", errmsg);
2123#endif
2124#endif
2125 }
2126 if (domain != NULL)
2127 host = /*@-unrecog@*/sh_gethostbyname (domain)/*@+unrecog@*/;
2128 if (!host)
2129 {
2130#ifdef FIL__
2131 (void) sl_strlcpy (errmsg, _("Unknown host "), 127);
2132 (void) sl_strlcat (errmsg, domain, 127);
2133 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
2134 errmsg,
2135 _("return_mx"));
2136#endif
2137 SL_RETURN (NULL, _("return_mx"));
2138 }
2139 result = SH_ALLOC (sizeof (mx));
2140 retval = SH_ALLOC (sizeof (dnsrep));
2141 retval->reply = result;
2142 retval->count = 1;
2143 result->pref = 0;
2144 /*@-type@*/
[22]2145 len = strlen (host->h_name) + 1;
2146 result->address = SH_ALLOC (len);
2147 sl_strlcpy (result->address, host->h_name, len);
[1]2148 /*@+type@*/
2149 SL_RETURN (retval, _("return_mx"));
2150 }
2151}
2152
2153static int free_mx (dnsrep * answers)
2154{
2155 mx * result;
2156 int i;
2157
2158 SL_ENTER(_("free_mx"));
2159 if (!answers)
2160 SL_RETURN (0, _("return_mx"));
2161
2162 result = answers->reply;
2163 for (i = 0; i < answers->count; ++i)
2164 {
2165 SH_FREE (result[i].address);
2166 }
2167 SH_FREE(result);
2168 SH_FREE(answers);
2169 SL_RETURN (0, _("return_mx"));
2170}
2171
2172#ifdef TEST_ONLY
2173int main(int argc, char * argv[])
2174{
2175 int i;
2176 dnsrep * answers;
2177 mx * result;
2178
2179 if (argc < 2)
2180 {
2181 fprintf(stderr, "Usage: dns <hostname>\n");
2182 return -1;
2183 }
2184 answers = return_mx(argv[1]);
2185
2186 if (!answers)
2187 {
2188 fprintf(stderr, "No answer\n");
2189 return -1;
2190 }
2191
2192 if (answers->count > 0)
2193 {
2194 result = answers->reply;
2195 for (i = 0; i < answers->count; ++i)
2196 {
2197 fprintf(stderr, "Record %3d: [%3d] %s\n", i,
2198 result[i].pref, result[i].address);
2199 }
2200 }
2201 else
2202 {
2203 fprintf(stderr, "No answer\n");
2204 free_mx(answers);
2205 return -1;
2206 }
2207 free_mx(answers);
2208 return (0);
2209}
2210#endif
2211
2212
2213
2214/* if defined(SH_WITH_MAIL) */
2215#endif
2216
2217
2218
Note: See TracBrowser for help on using the repository browser.