-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
1744 lines (1482 loc) · 41 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* Since this program contains concepts and possibly pieces of code
* from the sendmail sources, the above copyright notice is included.
*/
/*
* Written by Eric Wassenaar, Nikhef-H, <e07@nikhef.nl>
*
* Maintained by Greg A. Woods, Planix, Inc.; <woods@planix.com>
*
* https://github.com/robohack/vrfy
*
* The latest source release of this program is also available via anonymous FTP
* from machine 'ftp.weird.com' in the directory '/pub/Planix' as 'vrfy.tar.Z'
*
* ftp://ftp.weird.com/pub/Planix/vrfy.tar.Z
* http://www.weird.com/ftp/pub/Planix/vrfy.tar.Z
*
* You are kindly requested to report bugs and make suggestions
* for improvements to the author at the given email address,
* and to not re-distribute your own modifications to others.
*/
#ifndef lint
static char Version[] = "@(#)vrfy.c e07@nikhef.nl (Eric Wassenaar) 980820";
#endif
#include "vrfy.h"
/*
* Overview
*
* vrfy is a tool to verify electronic mail addresses.
* It recognizes elementary syntax errors, but can do a lot more,
* up to complex tasks such as recursively expand mailing lists
* and detect mail forwarding loops.
*
* In its simplest form, vrfy accepts an electronic mail address
* "user@domain" for which it will figure out the mx hosts for
* "domain", set up an SMTP session with the primary mx host,
* and issue the SMTP VRFY command with the given mail address.
* The reply from the remote host to the VRFY command is printed.
*
* If no mx hosts exist, it will try to contact "domain" itself.
* In case there is no "domain", the address is supposed to
* represent a local recipient which is verified at "localhost".
*
* By default only the primary mx host is contacted, hoping that
* "user" is local to that machine so that some extra information
* may be retrieved, such as full name, forwarding, aliasing, or
* mailing list expansion.
* With an option one may choose to also query the other mx hosts.
*
* For pseudo domains like "uucp" or "bitnet" one can compile in
* explicit servers to be contacted. They default to "localhost".
* Not many servers will tell what they are actually going to do
* with such addresses.
*
* Instead of an electronic mail address one can specify the name
* of a file containing address lists, e.g. mailing list recipient
* files or .forward files. Verification of all recipients in the
* file is then attempted.
*
* If an explicit additional host name is specified on the command
* line, verification is carried out at that host, and the input
* addresses are passed to the host without further parsing.
*
* Various levels of verbose output can be selected. Very verbose
* mode prints all SMTP transactions with the remote host in detail.
* When even more verbose, an additional SMTP VERB command will be
* issued, resulting in the display of all actions taken by the
* remote host when verifying the address. This can be fun.
*
* In the special ping mode, the mail exchangers for the specified
* electronic mail domain will be contacted to check whether they
* do respond to SMTP requests. No address verification is done.
*
* vrfy has built in the basic address parsing rules of sendmail,
* so it can determine the "domain" part in complicated addresses
* "comment \"comment\" comment" <user@domain (comment \comment)>
*
* Elementary syntax errors are caught locally. If the domain part
* could not be parsed beyond doubt, the address is passed on to
* "localhost" hoping to get detailed 'official' error messages.
* My sendmail.cf has an enormous amount of syntax checking rules.
* With an option one can reject invalid addresses internally.
*
* Another option lets you recursively verify the received replies
* to the original verified address. This is handy for mailing list
* expansions, and also to detect possible mail forwarding loops.
* This works only by the grace of sendmail and other MTAs sending
* formal address specifications in the VRFY replies.
*
* Recursion stops automatically if a local recipient address is
* received, or if a mail loop is detected. If the received reply
* is the same as the address that was asked for (modulo comments)
* the request is retried at its domain itself, unless this was the
* machine we just queried, or it is not an internet domain host.
*
* The default recursion level is set to the MAXHOP value (17) as
* used by sendmail, but this can be overruled (smaller or larger).
*/
/*
* Limitations
*
* Many non-sendmail SMTP servers have a lousy VRFY handling.
* Sometimes this command is not implemented at all. Other servers
* are only willing to VRFY local recipient names without a domain
* part (PMDF, VM, MVS).
* Furthermore, the sendmail V8 server can be configured to refuse
* the VRFY or EXPN command for privacy reasons.
*
* For those hosts there is an option to use the RCPT instead of
* the VRFY command. This does not give the same information, but
* it is better than nothing. Usually the HELO and MAIL commands
* are required as well. Recursive mode is not possible.
*
* Some hosts refuse to VRFY the bracketed <comment <user@domain>>
* but accept the same address without the outermost brackets.
*
* Usually hosts return addresses with an abundant amount of nested
* brackets if you present a bracketed address with comments.
* Note that sendmail returns "q_paddr" within a new set of brackets.
* It would be more illustrative if "q_user" were returned instead.
*
* An option will strip all comments from addresses to be verified,
* to avoid accumulating brackets during recursive verification.
* This is now the default when using the default recursion mode.
*
* Some hosts return an error message, but with the 250 status code.
* As long as there is no '@' in the message, it can do no harm.
*
* Some mailing lists have CNAME addresses, but we can now handle
* these, and do not get into infinite recursion.
*
* Some hosts return an unqualified address for local recipients.
* This is acceptable if it consists only of the pure "local part"
* but sometimes it is of the form <user@host> which is difficult
* to trace further.
*
* MX records may direct mail to a central mail host. Tracing down
* may yield an address at a host which is not reachable from the
* outside world.
*/
/*
* Compilation options
*
* This program usually compiles without special compilation options,
* but for some platforms you may have to define special settings.
* See the Makefile and the header file port.h for details.
*/
/*
* Usage: vrfy [options] address [host]
*
* This section is still to be supplied.
* For now, refer to the manual page.
*/
static char Usage[] =
"\
Usage: vrfy [options] [-v] address [host]\n\
File: vrfy [options] [-v] -f [file] [host]\n\
Ping: vrfy [options] [-v] -p domain\n\
Etrn: vrfy [options] [-v] -T domain [name]\n\
Options: [-a] [-d] [-l] [-s] [-c secs] [-t secs]\n\
Special: [-L level] [-R] [-S sender] [-n] [-e] [-h] [-H]\
";
char **optargv = NULL; /* argument list including default options */
int optargc = 0; /* number of arguments in new argument list */
char *FromAddr = NULL; /* -S explicit envelope sender address */
char *HostSpec = NULL; /* explicit host to be queried */
char *AddrSpec = NULL; /* address being processed */
char *FileName = NULL; /* name of file being processed */
int LineNumber = 0; /* line number into file */
int ExitStat = EX_SUCCESS; /* overall result status */
bool_t SuprErrs = FALSE; /* suppress parsing errors, if set */
int debug = 0; /* -d debugging level */
int verbose = 0; /* -v verbosity level */
int recursive = 0; /* -L recursive mode maximum level */
bool_t stripit = FALSE; /* -s strip comments, if set */
bool_t vrfyall = FALSE; /* -a query all mx hosts found, if set */
bool_t localerr = FALSE; /* -l handle errors locally, if set */
bool_t etrnmode = FALSE; /* -T etrn mx hosts, if set */
bool_t pingmode = FALSE; /* -p ping mx hosts, if set */
bool_t filemode = FALSE; /* -f verify file or stdin, if set */
bool_t helomode = FALSE; /* -h issue HELO command, if set */
bool_t ehlomode = FALSE; /* -H issue EHLO/HELO command, if set */
bool_t onexmode = FALSE; /* -o issue ONEX command, if set */
bool_t expnmode = FALSE; /* -e use EXPN instead of VRFY, if set */
bool_t rcptmode = FALSE; /* -r use RCPT instead of VRFY, if set */
bool_t datamode = FALSE; /* -M add DATA after MAIL/RCPT, if set */
char *ReplyList[MAXREPLY]; /* saved address expansions */
int ReplyCount = 0; /* number of valid replies */
char *AddrChain[MAXLOOP]; /* address chain in recursive mode */
char *localhost = LOCALHOST; /* nearest sendmail daemon */
char *uucprelay = UUCPRELAY; /* uucp relay host */
char *bitnetrelay = BITNETRELAY; /* bitnet relay host */
char *singlerelay = SINGLERELAY; /* unqualified host relay */
extern char *MxHosts[MAXMXHOSTS]; /* names of mx hosts found */
extern int ConnTimeout; /* -c timeout in secs for connect() */
extern int ReadTimeout; /* -t timeout in secs for sfgets() */
extern char *MyHostName; /* my own fully qualified host name */
extern char *version; /* program version number */
#if defined(apollo)
int h_errno = 0;
#endif
/*
** MAIN -- Start of program vrfy
** -----------------------------
**
** Exits:
** Various possibilities from sysexits.h
*/
int
main(argc, argv)
int argc;
char *argv[];
{
register char *option;
assert(sizeof(u_int) >= 4); /* probably paranoid */
#ifdef obsolete
assert(sizeof(u_short) == 2); /* perhaps less paranoid */
assert(sizeof(ipaddr_t) == 4); /* but this is critical */
#endif
/*
* Synchronize stdout and stderr in case output is redirected.
*/
linebufmode(stdout);
/*
* Initialize resolver. Set new defaults.
* The old defaults are (RES_RECURSE | RES_DEFNAMES | RES_DNSRCH)
*/
(void) res_init();
_res.options |= RES_DEFNAMES; /* qualify single names */
_res.options &= ~RES_DNSRCH; /* dotted names are qualified */
/*
* Overrule compiled-in defaults.
*/
option = getenv("VRFY_LOCALHOST");
if (option != NULL)
localhost = maxstr(newstr(option), MAXHOST, FALSE);
option = getenv("VRFY_UUCPRELAY");
if (option != NULL)
uucprelay = maxstr(newstr(option), MAXHOST, FALSE);
option = getenv("VRFY_BITNETRELAY");
if (option != NULL)
bitnetrelay = maxstr(newstr(option), MAXHOST, FALSE);
option = getenv("VRFY_SINGLERELAY");
if (option != NULL)
singlerelay = maxstr(newstr(option), MAXHOST, FALSE);
/*
* Interpolate default options and parameters.
*/
if (argc < 1 || argv[0] == NULL)
fatal(Usage);
option = getenv("VRFY_DEFAULTS");
if (option != NULL)
{
set_defaults(option, argc, argv);
argc = optargc; argv = optargv;
}
/*
* Fetch command line options.
*/
while (argc > 1 && argv[1] != NULL && argv[1][0] == '-')
{
for (option = &argv[1][1]; *option; option++)
{
switch (*option)
{
case 'd': /* increment debugging level */
debug++;
break;
case 'v': /* increment verbosity level */
verbose++;
break;
case 'a': /* query all mx hosts */
vrfyall = TRUE;
break;
case 'l': /* handle errors locally */
localerr = TRUE;
break;
case 's': /* strip comments from address */
stripit = TRUE;
break;
case 'H': /* issue EHLO/HELO command */
ehlomode = TRUE;
/*FALLTHROUGH*/
case 'h': /* issue HELO command */
helomode = TRUE;
break;
case 'o': /* issue ONEX command */
onexmode = TRUE;
break;
case 'e': /* use EXPN instead of VRFY */
expnmode = TRUE;
break;
case 'S': /* explicit envelope sender address */
if (argv[2] == NULL || argv[2][0] == '-')
fatal("Missing sender address");
FromAddr = setsender(argv[2]);
if (FromAddr == NULL)
fatal("Invalid sender address");
argc--, argv++;
/*FALLTHROUGH*/
case 'n': /* use alternative protocol suite */
ehlomode = TRUE;
helomode = TRUE;
/*FALLTHROUGH*/
case 'r': /* use MAIL/RCPT instead of VRFY */
rcptmode = TRUE;
recursive = 0;
break;
case 'M': /* add DATA after MAIL/RCPT */
datamode = TRUE;
break;
case 'f': /* verify file */
filemode = TRUE;
if (etrnmode)
fatal("-f conflicts with -T");
if (pingmode)
fatal("-f conflicts with -p");
break;
case 'p': /* ping mx hosts */
pingmode = TRUE;
if (etrnmode)
fatal("-p conflicts with -T");
if (filemode)
fatal("-p conflicts with -f");
break;
case 'T': /* etrn mx hosts */
etrnmode = TRUE;
if (pingmode)
fatal("-T conflicts with -p");
if (filemode)
fatal("-T conflicts with -f");
break;
case 'c': /* set connect timeout */
ConnTimeout = getval(argv[2], "timeout value", 1, 0);
--argc; argv++;
break;
case 't': /* set read timeout */
ReadTimeout = getval(argv[2], "timeout value", 1, 0);
--argc; argv++;
break;
case 'L' : /* set recursion level */
recursive = getval(argv[2], "recursion level", 1, MAXLOOP);
rcptmode = FALSE;
--argc; argv++;
break;
case 'R' : /* set default recursion level */
if (recursive == 0)
recursive = MAXHOP;
stripit = TRUE;
rcptmode = FALSE;
break;
case 'V' :
printf("Version %s\n", version);
exit(EX_SUCCESS);
default:
fatal(Usage);
}
}
--argc; argv++;
}
/*
* Scan remaining command line arguments.
*/
/* must have at least one argument */
if (!filemode && (argc < 2 || argv[1] == NULL))
fatal(Usage);
/* set optional explicit host to be queried */
if (argc > 2 && argv[2] != NULL)
HostSpec = maxstr(argv[2], MAXHOST, TRUE);
/* rest is undefined */
if (argc > 3)
fatal(Usage);
/*
* Miscellaneous initialization.
*/
/* reset control variables that may have been used */
AddrSpec = NULL;
SuprErrs = FALSE;
/* get own host name before turning on debugging */
if (helomode || etrnmode || pingmode)
setmyhostname();
/* set proper parameter for etrn mode */
if (etrnmode)
option = (HostSpec != NULL) ? HostSpec : MyHostName;
/*
* Set proper resolver options.
*/
/* set nameserver debugging on, if requested */
if (debug == 2)
_res.options |= RES_DEBUG;
/*
* All set. Execute the required function.
*/
if (etrnmode) /* etrn the given domain */
etrn(argv[1], option);
else if (pingmode) /* ping the given domain */
ping(argv[1]);
else if (filemode) /* verify the given file */
file(argv[1]);
else /* verify the address list */
list(argv[1]);
return(ExitStat);
/*NOTREACHED*/
}
/*
** SET_DEFAULTS -- Interpolate default options and parameters in argv
** ------------------------------------------------------------------
**
** The VRFY_DEFAULTS env variable gives customized options.
**
** Returns:
** None.
**
** Outputs:
** Creates ``optargv'' vector with ``optargc'' arguments.
*/
void
set_defaults(option, argc, argv)
char *option; /* option string */
int argc; /* original command line arg count */
char *argv[]; /* original command line arguments */
{
register char *p, *q;
register int i;
/*
* Allocate new argument vector.
*/
optargv = newlist(NULL, 2, char *);
optargv[0] = argv[0];
optargc = 1;
/*
* Construct argument list from option string.
*/
for (q = newstr(option), p = q; *p != '\0'; p = q)
{
while (is_space(*p))
p++;
if (*p == '\0')
break;
for (q = p; *q != '\0' && !is_space(*q); q++)
continue;
if (*q != '\0')
*q++ = '\0';
optargv = newlist(optargv, optargc+2, char *);
optargv[optargc] = p;
optargc++;
}
/*
* Append command line arguments.
*/
for (i = 1; i < argc && argv[i] != NULL; i++)
{
optargv = newlist(optargv, optargc+2, char *);
optargv[optargc] = argv[i];
optargc++;
}
/* and terminate */
optargv[optargc] = NULL;
}
/*
** GETVAL -- Decode parameter value and perform range check
** --------------------------------------------------------
**
** Returns:
** Parameter value if successfully decoded.
** Aborts in case of syntax or range errors.
*/
int
getval(optstring, optname, minvalue, maxvalue)
char *optstring; /* parameter from command line */
char *optname; /* descriptive name of option */
int minvalue; /* minimum value for option */
int maxvalue; /* maximum value for option */
{
register int optvalue;
if (optstring == NULL || optstring[0] == '-')
fatal("Missing %s", optname);
optvalue = atoi(optstring);
if (optvalue == 0 && optstring[0] != '0')
fatal("Invalid %s %s", optname, optstring);
if (optvalue < minvalue)
fatal("Minimum %s %s", optname, itoa(minvalue));
if (maxvalue > 0 && optvalue > maxvalue)
fatal("Maximum %s %s", optname, itoa(maxvalue));
return(optvalue);
}
/*
** FATAL -- Abort program when illegal option encountered
** ------------------------------------------------------
**
** Returns:
** Aborts after issuing error message.
*/
void
#ifdef __STDC__
fatal(const char *fmt, ...)
#else
/*VARARGS1*/
fatal(fmt, va_alist)
const char *fmt; /* format of message */
va_dcl /* arguments for printf */
#endif
{
va_list ap;
VA_START(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[strlen(fmt) - 1] != '\n')
(void) fputc('\n', stderr);
exit(EX_USAGE);
}
/*
** ERROR -- Issue error message to error output
** --------------------------------------------
**
** Returns:
** None.
*/
void
#ifdef __STDC__
error(const char *fmt, ...)
#else
/*VARARGS1*/
error(fmt, va_alist)
const char *fmt; /* format of message */
va_dcl /* arguments for printf */
#endif
{
va_list ap;
VA_START(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[strlen(fmt) - 1] != '\n')
(void) fputc('\n', stderr);
}
/*
** USRERR -- Issue error message during address parsing
** ----------------------------------------------------
**
** Returns:
** None.
**
** Called from various parsing routines when an error is found.
** Printing of the message is suppressed if necessary.
*/
void
#ifdef __STDC__
usrerr(const char *fmt, ...)
#else
/*VARARGS1*/
usrerr(fmt, va_alias)
const char *fmt; /* format of message */
va_dcl /* arguments for printf */
#endif
{
va_list ap;
/* suppress message if requested */
if (SuprErrs)
return;
/* issue message with fatal error status */
(void) fputs("554 ", stderr);
VA_START(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[strlen(fmt) - 1] != '\n')
(void) fputc('\n', stderr);
}
/*
** MESSAGE -- Issue a status message in special format
** ---------------------------------------------------
**
** Returns:
** None.
**
** The status message should begin with 3-digit status code.
*/
void
#ifdef __STDC__
message(const char *msg, ...)
#else
/*VARARGS1*/
message(msg, va_alist)
const char *msg; /* status message */
va_dcl /* arguments for printf */
#endif
{
va_list ap;
const char *fmt;
VA_START(ap, msg);
/* do not print informational messages if not verbose */
if ((msg[0] == '0' || msg[0] == '1') && !verbose)
return;
fmt = &msg[4]; /* format of the actual message */
/* prepend with filename and line number if appropriate */
if (FileName != NULL)
printf("%s: line %d: ", FileName, LineNumber);
/* print the address being processed */
if (AddrSpec != NULL && *AddrSpec != '\0')
printf("%s ... ", printable(AddrSpec));
/* print the message itself */
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
/* add a newline if needed */
if (fmt[strlen(fmt) - 1] != '\n')
(void) fputc('\n', stderr);
}
/*
** RESPONSE -- Process reply message from smtp vrfy request
** --------------------------------------------------------
**
** Returns:
** None.
**
** Side effects:
** Valid replies may be saved for later recursion.
**
** Called from smtpreply() for each reply line received in the
** vrfy wait phase. Status code 2xx indicates a message with a
** valid address expansion. More than one such line may arrive.
**
** Strictly speaking only 250 and 251 are valid for VRFY, EXPN,
** and RCPT. Code 252 is added per RFC 1123 to reject VRFY.
**
** Note that we must have an AddrSpec in order to do recursion.
*/
void
response(msg)
char *msg; /* status message from reply */
{
char *address = &msg[4]; /* address expansion in reply */
/* print the text of the reply */
printf("%s\n", address);
/* skip if this is not a standard reply line */
if (!is_digit(msg[0]) || (msg[3] != ' ' && msg[3] != '-'))
return;
/* skip if this is not a valid address expansion */
if (msg[0] != '2')
return;
/* only allow 250 and 251 -- explicitly skip 252 */
if (msg[1] != '5' || (msg[2] != '0' && msg[2] != '1'))
return;
/* save the reply for later recursion processing */
if (recursive && AddrSpec != NULL && ReplyCount < MAXREPLY)
{
ReplyList[ReplyCount] = newstr(address);
ReplyCount++;
}
}
/*
** SHOW -- Print a detailed result status of a transaction
** -------------------------------------------------------
**
** Returns:
** None.
**
** Side effects:
** Updates the overall result status.
**
** In recursive mode, all received replies are verified in turn.
** Note that we must have an AddrSpec in order to do recursion.
*/
#define tempfail(x) (x == EX_TEMPFAIL || x == EX_OSERR || x == EX_IOERR)
void
show(status, host)
int status; /* result status of operation */
char *host; /* remote host that was queried */
{
/* save result status, keeping previous failures */
if (status != EX_SUCCESS && !tempfail(ExitStat))
ExitStat = status;
/* this must be an internal programming error */
if (status == EX_SUCCESS && host == NULL)
status = EX_SOFTWARE;
/* display the appropriate error message */
if (status != EX_SUCCESS)
giveresponse(status);
/* special message in etrn mode */
else if (etrnmode)
printf("%s responded\n", host);
/* special message in ping mode */
else if (pingmode)
printf("%s is alive\n", host);
/* recursively verify the received replies */
else if (recursive && AddrSpec != NULL && ReplyCount > 0)
loop(AddrSpec, host);
}
/*
** LOOP -- Recursively verify received address expansions
** ------------------------------------------------------
**
** Returns:
** None.
**
** Called from show() to reprocess the saved valid replies
** after the smtp session is terminated.
*/
int recursion_level = 0; /* current recursion level */
void
loop(address, host)
char *address; /* address we have just verified */
char *host; /* remote host that was queried */
{
char *replylist[MAXREPLY]; /* local copy of replies */
int replycount = ReplyCount; /* number of replies received */
char *oldhost = host; /* host queried for old address */
char oldaddr[MAXSPEC+1]; /* parsed original address */
char newaddr[MAXSPEC+1]; /* parsed reply to address */
char hostbuf[MAXHOST+1]; /* local copy of domain part */
char *domain; /* domain part of address */
char *SaveFile;
int SaveLine;
register int i;
/*
* Save state.
*/
SaveFile = FileName;
SaveLine = LineNumber;
/*
* Save local copy of replies.
*/
for (i = 0; i < replycount; i++)
replylist[i] = ReplyList[i];
/*
* Parse original address and save local copy. Do not report errors.
* Loop over the replies if not yet at the maximum recursion level.
*/
AddrSpec = NULL;
SuprErrs = TRUE;
domain = parsespec(address, oldaddr, (char *)NULL);
if (domain != NULL && recursive && recursion_level < recursive)
{
/* put the current address on the chain */
AddrChain[recursion_level] = oldaddr;
FileName = oldaddr;
LineNumber = 0;
for (i = 0; i < replycount; i++)
{
LineNumber++;
address = replylist[i];
/* always process address parsing errors */
AddrSpec = address;
SuprErrs = FALSE;
/* make sure it is a single address */
address = parselist(address);
if (address == NULL)
continue;
/* skip if address cannot be parsed */
domain = parsespec(address, newaddr, hostbuf);
if (domain == NULL)
continue;
/* skip if this is a local recipient address */
if (sameword(domain, "localhost"))
continue;
/* retry if this is not the same address */
if (!sameword(newaddr, oldaddr))
host = NULL;
/* skip if this host was just queried */
else if (sameword(domain, oldhost))
continue;
/* skip if this is not an internet host */
else if (!internet(domain))
continue;
/* give explicit host a try */
else
host = hostbuf;
/* skip if forwarding loop is detected */
if (host == NULL && invalidloop(newaddr))
continue;
/* reset for message */
AddrSpec = NULL;
/* display address */
message("250 %s", address);
/* recursively verify the reply */
recursion_level++;
vrfy(address, host);
recursion_level--;
}
}
/*
* Replies were allocated dynamically.
*/
for (i = 0; i < replycount; i++)
xfree(replylist[i]);
/*
* Restore state.
*/
FileName = SaveFile;
LineNumber = SaveLine;
}
/*
** FILE -- Process a file containing address lists
** -----------------------------------------------
**
** Returns:
** None.
**
** Side effects:
** Shows the status for each transaction done.
*/
void
file(filename)
char *filename; /* name of file to be verified */
{
char *addrlist; /* address list to be verified */
int status; /* result status */
FILE *fp;
char buf[BUFSIZ];
char *SaveFile;
int SaveLine;
register char *p;
/*
* Save state across recursive calls.
*/
SaveFile = FileName;
SaveLine = LineNumber;
/*
* Allow the use of vrfy -f as a filter.
*/
if (filename == NULL || *filename == '\0')
filename = "stdin";
if (sameword(filename, "stdin"))
fp = stdin;
else
fp = fopen(filename, "r");
/*
* Terminate if the file could not be opened.
*/