source: trunk/src/sh_mail.c@ 436

Last change on this file since 436 was 383, checked in by katerina, 13 years ago

Fix for ticket #281 (warnings from clang static analyzer).

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