summaryrefslogtreecommitdiff
path: root/main.c
blob: 87d16ad91d50ae2a458bfc2e42a70372004ccf92 (plain)
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
/**
 * \file main.c
 * Main program.
 */


#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#include "main.h"
#include "message.h"
#include "smtp.h"
#include "local.h"
#include "rcfile.h"


static void drop_sgids( void )
{
	gid_t real_gid, effective_gid;

	real_gid = getgid();
	effective_gid = getegid();

	if( setregid(real_gid,real_gid) != 0 )
	{
		fprintf (stderr, "Could not drop setgid: %s\n", strerror(errno));
		exit(EX_DROPPERM);
	}
}


/** Modes of operation. */
typedef enum {
	ENQUEUE,		/**< delivery mode */
	NEWALIAS,		/**< initialize alias database */
	MAILQ,			/**< list mail queue */
	FLUSHQ			/**< flush the mail queue */
} opmode_t;


int verbose = 0;

FILE *log_fp = NULL;

static void message_send(message_t *message)
{
	int local, remote;
	identity_t *identity;

	/* Lookup the identity already here */
	identity = identity_lookup(message->reverse_path); 
	assert(identity);
	
	if( identity->qualifydomain )
	{
		local = 0;
		remote = 1;
	}
	else
	{
		local = !list_empty(&message->local_recipients);
		remote = !list_empty(&message->remote_recipients);
	}
	
	if(remote && !local)
		smtp_send(message,identity);
	else if(!remote && local)
	{
		local_init(message);
		local_flush(message);
	}
	else
	{
		local_init(message);
		smtp_send(message,identity);
		local_flush(message);
	}
	
	local_cleanup();
}

int main (int argc, char **argv)
{
	int c;
	message_t *message;
	int parse_headers = 0;
	opmode_t mode;
	char *rcfile = NULL;
	
	message = message_new();

	/* Set the default mode of operation. */
	if (!strcmp(argv[0], "mailq")) {
		mode = MAILQ;
	} else if (!strcmp(argv[0], "newaliases")) {
		mode = NEWALIAS;
	} else {
		mode = ENQUEUE;
	}

	while ((c = getopt (argc, argv, "A:B:b:C:cd:e:F:f:Gh:IiL:M:mN:nO:o:p:q:R:r:sTtV:vX:")) != EOF)
		switch (c)
		{
			case 'A':
				/* Use alternate sendmail/submit.cf */
				break;

			case 'B':
				/* Body type */
				if (!strcmp (optarg, "7BIT"))
					message->body = E8bitmime_7BIT;
				else if (!strcmp (optarg, "8BITMIME"))
					message->body = E8bitmime_8BITMIME;
				else
				{
					fprintf (stderr, "Unsupported body type %s\n", optarg);
					exit (EX_USAGE);
				}
				break;

			case 'C':
				/* Select configuration file */
				rcfile = optarg;
				break;

			case 'F':
				/* Set full name */
				break;

			case 'G':
				/* Relay (gateway) submission */
				break;

			case 'I':
				/* Initialize alias database */
				mode = NEWALIAS;
				break;

			case 'L':
				/* Program label */
				break;

			case 'M':
				/* Define macro */
				break;

			case 'N':
				/* Delivery status notifications */
				if (!strcmp (optarg, "never"))
					message->notify = Notify_NEVER;
				else
				{
					if (strstr (optarg, "failure"))
						message->notify |= Notify_FAILURE;
					if (strstr (optarg, "delay"))
						message->notify |= Notify_DELAY;
					if (strstr (optarg, "success"))
						message->notify |= Notify_SUCCESS;
				}
				break;

			case 'R':
				/* What to return */
				if (!strcmp (optarg, "full"))
					message->ret |= Ret_FULL;
				if (!strcmp (optarg, "hdrs"))
					message->ret |= Ret_HDRS;
				break;

			case 'T':
				/* Set timeout interval */
				break;

			case 'X':
				/* Traffic log file */
				if (log_fp)
					fclose(log_fp);
				log_fp = fopen(optarg, "a");
				break;

			case 'V':
				/* Set original envelope id */
				message_set_envid(message, optarg);

			case 'b':
				/* Operations mode */
				c = (optarg == NULL) ? ' ' : *optarg;
				switch (c)
				{
					case 'm':
						/* Deliver mail in the usual way */
						mode = ENQUEUE;
						break;

					case 'i':
						/* Initialize the alias database */
						mode = NEWALIAS;
						break;

					case 'p':
						/* Print a listing of the queue(s) */
						mode = MAILQ;
						break;
						
					case 'a':
						/* Go into ARPANET mode */
					case 'd':
						/* Run as a daemon */
					case 'D':
						/* Run as a daemon in foreground */
					case 'h':
						/* Print the persistent host status database */
					case 'H':
						/* Purge expired entries from the persistent host
						 * status database */
					case 'P':
						/* Print number of entries in the queue(s) */
					case 's':
						/* Use the SMTP protocol as described in RFC821
						 * on standard input and output */
					case 't':
						/* Run in address test mode */
					case 'v':
						/* Verify names only */
						fprintf (stderr, "Unsupported operation mode %c\n", c);
						exit (EX_USAGE);
						break;

					default:
						fprintf (stderr, "Invalid operation mode %c\n", c);
						exit (EX_USAGE);
						break;
				 }
				 break;

			case 'c':
				/* Connect to non-local mailers */
				break;

			case 'd':
				/* Debugging */
				break;

			case 'e':
				/* Error message disposition */
				break;

			case 'f':
				/* From address */
			case 'r':
				/* Obsolete -f flag */
				message_set_reverse_path(message, optarg);
				break;

			case 'h':
				/* Hop count */
				break;

			case 'i':
				/* Don't let dot stop me */
				break;

			case 'm':
				/* Send to me too */
				break;

			case 'n':
				/* don't alias */
				break;

			case 'o':
				/* Set option */
				break;

			case 'p':
				/* Set protocol */
				break;

			case 'q':
				/* Run queue files at intervals */
				mode = FLUSHQ;
				if (optarg[0] == '!')
				{
					/* Negate the meaning of pattern match */
					optarg++;
				}

				switch (optarg[0])
				{
					case 'G':
						/* Limit by queue group name */
						break;

					case 'I':
						/* Limit by ID */
						break;

					case 'R':
						/* Limit by recipient */
						break;

					case 'S':
						/* Limit by sender */
						break;

					case 'f':
						/* Foreground queue run */
						break;

					case 'p':
						/* Persistent queue */
						if (optarg[1] == '\0')
							break;
						++optarg;

					default:
						break;
				}
				break;

			case 's':
				/* Save From lines in headers */
				break;

			case 't':
				/* Read recipients from message */
				if(!message_parse_headers(message))
				{
					fprintf(stderr, "No recipients found\n");
					exit(EX_DATAERR);
				}
				parse_headers = 1;
				break;

			case 'v':
				/* Verbose */
				verbose = 1;
				break;

			default:
				fprintf (stderr, "Invalid option %c\n", c);
				exit (EX_USAGE);
			}

	switch (mode)
	{
		case ENQUEUE:
			break;
		
		case MAILQ:
			printf ("Mail queue is empty\n");
		case NEWALIAS:
		case FLUSHQ:
			goto done;
	}

	/* At least one more argument is needed. */
	if (optind > argc - 1 && !parse_headers)
	{
		fprintf(stderr, "Recipient names must be specified\n");
		exit (EX_USAGE);
	}

	/* Parse the rc file.
	 *
	 * Do this before message_add_recipient; to enable the force_mda recipient
	 */
	rcfile_parse(rcfile);

	/* Add remaining program arguments as message recipients. */
	while (optind < argc)
		message_add_recipient(message, argv[optind++]);

	identities_init();

	drop_sgids();

	message_send(message);
	
	identities_cleanup();

done:
	if(log_fp)
		fclose(log_fp);
	
	message_free(message);

	exit(EX_OK);
}