File: main.c

package info (click to toggle)
smtpfeed 1.02-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 824 kB
  • ctags: 765
  • sloc: ansic: 13,150; sh: 230; perl: 133; makefile: 97
file content (502 lines) | stat: -rw-r--r-- 13,269 bytes parent folder | download
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
/*
 * Copyright (c) 1997,1998 Motonori Nakamura <motonori@econ.kyoto-u.ac.jp>
 * Copyright (c) 1997,1998 WIDE Project
 * Copyright (c) 1997,1998 Kyoto University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by WIDE Project and
 *      its contributors.
 * 4. Neither the name of the Project, 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 BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char *copyright1 = "@(#)Copyright (c) 1997,1998 Motonori Nakamura";
static char *copyright2 = "@(#)Copyright (c) 1997,1998 WIDE Project.";
static char *copyright3 = "@(#)Copyright (c) 1997,1998 Kyoto University.";
static char *copyright4 = "@(#)All rights reserved.";
static char *_id_ = "$Id: main.c,v 1.55 1999/04/15 05:57:10 motonori Exp $";
#endif

# include "common.h"
# define MAIN	1
# include "extern.h"

void
usage(progname)
char *progname;
{
	printf("Usage: %s [options]\n", progname);
	printf("Options are:\n");
	printf("	-1		no piggybacking; par recipient SMTP\n");
	printf("	-a alias	my alias names to check MX list\n");
	printf("	-A		try T_ANY query first to get MX\n");
	printf("	-b size		TCP send buffer size\n");
	printf("	-c name		client side official name\n");
	printf("	-d debugopt	set debug options\n");
	printf("\t   opts:	A: all		a: address	c: conn.\n");
	printf("\t\t	e: event	h: hash		l: LMTP\n");
	printf("\t\t	m: map		n: DNS		s: SMTP\n");
	printf("\t\t	t: transaction	u: res. usage\n");
	printf("\t\t	D: no delivery	Q: DNS query only\n");
	printf("	-E		get queue-id info. from Received:\n");
	printf("	-F		insert recipient info. on Received:\n");
	printf("	-h		show this message\n");
	printf("	-I queue-id	equiv. to -i but no purge on RSET\n");
	printf("	-i queue-id	queue-id from sendmail for logging\n");
	printf("	-l facility	set logging facility\n");
	printf("	-M map-file	path of map file\n");
	printf("	-m fallbackmx	set fallback MX host\n");
#ifdef INET6
	printf("	-N domain	4:IPv4 only, 6:IPv6 only, B:Both\n");
#endif
	printf("	-n num		available number of sockets\n");
	printf("	-o source-addr	source address for SMTP connection\n");
	printf("	-p port		SMTP source port number\n");
	printf("	-R num		max recipients per transaction\n");
	printf("	-r num		min recipients to work\n");
	printf("	-s size		max message size\n");
	printf("	-t cate=val	set timeouts (val: NUM[hms]; default is s)\n");
	printf("\t   cate(gory):	total, connect, greet, helo, mail,\n");
	printf("\t		rcpt, data, body, term, cache, rset, quit\n");
	printf("	-u		reuse SMTP connection for other MX\n");
	printf("	-V		insert version info. on Received:\n");
	printf("	-y\t	refer A RR if this host is the first MX\n");
	printf("	-z		surpress SMTP pipelining\n");
	printf("Version: %s\n", version);
}

static void
sig_quit()
{
	exit(EX_TEMPFAIL);
}

void
main(argc, argv, envp)
int argc;
char *argv[];
char *envp[];
{
	struct servent *sp;
	extern int optind;
	extern int opterr;
	extern char *optarg;
	char *progname, *p;
	int opt;
	struct rlimit limit;

	if (chdir("/var/tmp") < 0)
		if (chdir("/usr/tmp") < 0)
			chdir("/tmp");

	signal(SIGALRM, SIG_IGN);
        signal(SIGHUP,  SIG_IGN);
	signal(SIGQUIT, sig_quit);
        signal(SIGINT,  sig_quit);
        signal(SIGTERM,  sig_quit);
	if ((progname = strrchr(argv[0], '/')) != NULL)
		progname++;
	else
		progname = argv[0];
	myname[0] = '\0';
	myaliases = NULL;

	bzero(&sti, sizeof(sti));
	if ((p = getenv("SM_START_TIME")) != NULL)
	{
		sti.time_sm_start = (time_t)atoi(p);
	}

	bzero(&cnf, sizeof(cnf));
	cnf.sd_max = MAX_SOCK;
	cnf.log_facility = LOG_DEFAULT;
	cnf.rcpts_trans = 100;
	cnf.src_mod_ptn = "<%s+%s=%s@%s>";

        if((sp = getservbyname("smtp", "tcp")) != NULL)
                cnf.dst_port = ntohs(sp->s_port);
        else
                cnf.dst_port = 25;

	initsetproctitle(argc, argv, envp);

	opterr = 0;
#ifdef INET6
#define	OPTS	"1Aa:b:c:d:EFhI:i:l:M:m:N:n:o:p:R:r:S:s:t:uVyz"
#else
#define	OPTS	"1Aa:b:c:d:EFhI:i:l:M:m:n:o:p:R:r:S:s:t:uVyz"
#endif
	while ((opt = getopt(argc, argv, OPTS)) != -1)
	{
		switch (opt)
		{
		    case '1':	/* 1: no piggybacking */
			cnf.rcpts_trans = 1;
			break;
		    case 'A':	/* A: try T_ANY first */
			cnf.tryANYfirst = 1;
			break;
		    case 'a':	/* a: alias names */
			if (addmyalias(optarg) < 0)
			{
				/* XXX */
			}
			break;
		    case 'b':	/* b: send buffer size */
			cnf.sendbufsize = atoi(optarg);
			break;
		    case 'c':	/* c: client side official name */
			strncpy(myname, optarg, sizeof(myname));
			break;
		    case 'd':	/* d: debug option */
			p = optarg;
			while (*p != '\0')
			{
				switch (*p)
				{
				    case 'A':
					cnf.debug |= DEBUG_ALL;
					break;
				    case 'a':
					cnf.debug |= DEBUG_ADDRESS;
					break;
				    case 'c':
					cnf.debug |= DEBUG_CONNECT;
					break;
				    case 'e':
					cnf.debug |= DEBUG_EVENT;
					break;
				    case 'h':
					cnf.debug |= DEBUG_HASH;
					break;
				    case 'l':
					cnf.debug |= DEBUG_LMTP;
					break;
				    case 'm':
					cnf.debug |= DEBUG_MAP;
					break;
				    case 'n':
					cnf.debug |= DEBUG_DNS;
					break;
				    case 's':
					cnf.debug |= DEBUG_SMTP;
					break;
				    case 't':
					cnf.debug |= DEBUG_TRANS;
					break;
				    case 'u':
					cnf.debug |= DEBUG_RESUSE;
					break;
				    case 'D':
					cnf.debug |= DEBUG_NODELIVERY;
					break;
				    case 'Q':
					cnf.debug |= DEBUG_QUERYONLY;
					break;
				}
				p++;
			}
			break;
		    case 'E':	/* E: get queue-id info. from Received: */
			cnf.parsequeueid = 1;
			break;
#if 0
		    case 'f':	/* f: socket number to be freed at a time */
			break;
#endif
		    case 'F':	/* F: show recipient information */
			cnf.showrecipient++;
			break;
		    case 'I':	/* I: queue-id just for logging (no purge) */
			if (env.queueid != NULL)
				free(env.queueid);
			env.queueid = newstr(optarg);
			env.keepqueueid = 1;
			break;
		    case 'i':	/* i: queue-id just for logging */
			if (env.queueid != NULL)
				free(env.queueid);
			env.queueid = newstr(optarg);
			break;
		    case 'l':	/* l: logging facility */
			if (strcasecmp(optarg, "none") == 0)
				cnf.log_facility = LOG_NONE;
			else if (strcasecmp(optarg, "stderr") == 0)
				cnf.log_facility = LOG_STDERR;
#ifdef LOG_DAEMON
			else if (strcasecmp(optarg, "daemon") == 0)
				cnf.log_facility = LOG_DAEMON;
#endif
#ifdef LOG_MAIL
			else if (strcasecmp(optarg, "mail") == 0)
				cnf.log_facility = LOG_MAIL;
#endif
#ifdef LOG_USER
			else if (strcasecmp(optarg, "user") == 0)
				cnf.log_facility = LOG_USER;
#endif
#ifdef LOG_LOCAL0
			else if (strcasecmp(optarg, "local0") == 0)
				cnf.log_facility = LOG_LOCAL0;
#endif
#ifdef LOG_LOCAL1
			else if (strcasecmp(optarg, "local1") == 0)
				cnf.log_facility = LOG_LOCAL1;
#endif
#ifdef LOG_LOCAL2
			else if (strcasecmp(optarg, "local2") == 0)
				cnf.log_facility = LOG_LOCAL2;
#endif
#ifdef LOG_LOCAL3
			else if (strcasecmp(optarg, "local3") == 0)
				cnf.log_facility = LOG_LOCAL3;
#endif
#ifdef LOG_LOCAL4
			else if (strcasecmp(optarg, "local4") == 0)
				cnf.log_facility = LOG_LOCAL4;
#endif
#ifdef LOG_LOCAL5
			else if (strcasecmp(optarg, "local5") == 0)
				cnf.log_facility = LOG_LOCAL5;
#endif
#ifdef LOG_LOCAL6
			else if (strcasecmp(optarg, "local6") == 0)
				cnf.log_facility = LOG_LOCAL6;
#endif
#ifdef LOG_LOCAL7
			else if (strcasecmp(optarg, "local7") == 0)
				cnf.log_facility = LOG_LOCAL7;
#endif
			break;
		    case 'M':	/* M: path of map faile */
			if (cnf.map != NULL)
				free(cnf.map);
			cnf.map = newstr(optarg);
			break;
		    case 'm':	/* m: fallback MX */
			if (cnf.fallbackmx != NULL)
				free(cnf.fallbackmx);
			cnf.fallbackmx = newstr(optarg);
			break;
#ifdef INET6
		    case 'N':	/* N: internet domain to be used */
			switch (*optarg)
			{
			  case '4':
				cnf.inetdom |= SMTP_V4;
				break;
			  case '6':
				cnf.inetdom |= SMTP_V6;
				break;
			  case 'b':
			  case 'B':
				cnf.inetdom |= SMTP_V4|SMTP_V6;
				break;
			}
			break;
#endif
		    case 'n':	/* n: available socket number */
			cnf.sd_max = atoi(optarg);
			if (cnf.sd_max < 1)
				cnf.sd_max = 1;
			break;
		    case 'o':	/* o: source IP address */
			if (cnf.sourceIP != NULL)
				free(cnf.sourceIP);
			cnf.sourceIP = newstr(optarg);
			break;
		    case 'p':	/* p: source port number */
				/* rresvport() used if negative */
			cnf.src_port = atoi(optarg);
			break;
		    /* q: quick start */
		    case 'R':	/* R: recipients par transaction */
			cnf.rcpts_trans = atoi(optarg);
			break;
		    case 'r':	/* r: number of recipients lower limit */
			cnf.rcpt_min = atoi(optarg);
			break;
		    case 'S':	/* S: sender envelope address modification */
			if (*optarg == '/')
			{
				if (cnf.src_mod_path != NULL)
					free(cnf.src_mod_path);
				cnf.src_mod_path = newstr(optarg);
			}
			else
			{
#if 0
				if (cnf.src_mod_ptn != NULL)
					free(cnf.src_mod_ptn);
#endif
				cnf.src_mod_ptn = newstr(optarg);
			}
			break;
		    case 's':	/* s: max message size */
			cnf.data_max = atoi(optarg);
			break;
		    case 't':	/* t: timeouts */
			set_timeout(optarg);
			break;
		    case 'u':	/* u: reuse SMTP connection */
			cnf.smtp_reuse = 1;
			break;
		    case 'V':	/* V: show version info. on Received: */
			cnf.showversion = 1;
			break;
		    case 'y':	/* y: refer A RR if this host is first MX */
			cnf.useAfirstMX = 1;
			break;
		    case 'z':	/* z: surpress SMTP pipelining */
			cnf.nopipelining = 1;
			break;
		    case 'h':
		    default:
			usage(progname);
			exit(EX_TEMPFAIL);
			break;
		}
	}
	argc -= optind;
	argv += optind;

#if 0
	if (argc > 0)
	{
		usage(progname);
		exit(EX_TEMPFAIL);
	}
#endif

#ifdef INET6
	/* set default */
	if (cnf.inetdom == 0)
		cnf.inetdom |= SMTP_V4|SMTP_V6;
#endif

	init_log();

	if (cnf.sd_max > MAX_SOCK)
	{
		log(LOG_INFO, "cnf.sd_max should not be greater than %d, recompile smtpfeed with greater FD_SETSIZE", MAX_SOCK);
		cnf.sd_max = MAX_SOCK;
	}
#ifdef RLIMIT_NOFILE
	if (!getrlimit(RLIMIT_NOFILE, &limit))
	{
		if (limit.rlim_max - 1 < cnf.sd_max)
		{
			log(LOG_INFO, "cnf.sd_max should be %d",
				limit.rlim_max - 1);
			cnf.sd_max = limit.rlim_max - 1;
		}
		limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
		if (setrlimit(RLIMIT_NOFILE, &limit) == -1)
		{
			log(LOG_NOTICE, "can't set max sd to %d",
				limit.rlim_cur);
		}
	}
#endif

#ifdef INET6
        log(LOG_DEBUG, "%s (with IPv6) started (with %d descriptors)",
		version, cnf.sd_max);
#else
        log(LOG_DEBUG, "%s started (with %d descriptors)", version, cnf.sd_max);
#endif

	res_init();
	if (cnf.map != NULL && host_map_load() < 0)
	{
		exit(EX_TEMPFAIL);
	}
	resource_usage("initialize");

	if (cnf.t_timeout == 0)
		cnf.t_timeout = 55 MINUTES;
	host_list = NULL;
	domain_list = NULL;

	_res.options |= RES_DEFNAMES | RES_DNSRCH;

	if (*myname == '\0')
		gethostname(myname, sizeof(myname));
	if (strchr(myname, '.') == NULL)
	{
		/* not a FQDN */
		struct hostent *hp;

		hp = gethostbyname(myname);
		if (hp != NULL)
		{
			if (strchr(hp->h_name, '.') != NULL)
				strncpy(myname, hp->h_name, sizeof(myname));
			else
			{
				while (*hp->h_aliases != NULL)
				{
					if (strchr(*hp->h_aliases, '.') != NULL)
					{
						strncpy(myname, *hp->h_aliases,
							sizeof(myname));
						break;
					}
					hp->h_aliases++;
				}
			}
		}
		if (strchr(myname, '.') == NULL)
			log(LOG_INFO, "hostname (%s) is not a FQDN", myname);
	}
	if (addmyalias(myname) < 0)
	{
		/* XXX */
	}
	if (cnf.fallbackmx != NULL && isamyalias(cnf.fallbackmx))
	{
		log(LOG_INFO, "ignoring fallbackmx (%s)", cnf.fallbackmx);
		free(cnf.fallbackmx);
		cnf.fallbackmx = NULL;
	}

	/* stdin/stdout will be used */
#if 0
	close (2);
	close (3);
	close (4);
#endif

	_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);

	lmtp();

	resource_usage("all done");

	exit(EX_OK);
}