File: main.C

package info (click to toggle)
maildrop 0.54a-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 872 kB
  • ctags: 883
  • sloc: cpp: 8,469; ansic: 658; sh: 327; makefile: 82
file content (536 lines) | stat: -rw-r--r-- 10,418 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
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
/* DSTART                                                                    */
/*                                                                           */
/*           maildrop - mail delivery agent with filtering abilities         */
/*                                                                           */
/*  Copyright 1998, Double Precision Inc.                                    */
/*                                                                           */
/*  This program is distributed under the terms of the GNU General Public    */
/*  License. See COPYING for additional information.                         */
/* DEND                                                                      */
#include	"lexer.h"
#include	"recipe.h"
#include	"varlist.h"
#include	"funcs.h"
#include	"tempfile.h"
#include	"message.h"
#include	"messageinfo.h"
#include	"config.h"
#include	"exittrap.h"
#include	"maildrop.h"
#include	"autoconf.h"
#include	"setgroupid.h"
#include	<sys/types.h>
#if HAVE_SYS_STAT_H
#include	<sys/stat.h>
#endif
#include	<sysexits.h>
#include	<string.h>
#include	<stdlib.h>
#include	<pwd.h>

static const char rcsid[]="$Id: main.C 1.5 1998/08/31 03:54:50 mrsam Exp $";

static Message m1, m2;
extern char **environ;
static int errexit=EX_TEMPFAIL;
static const char *defaults_vars[]={"LOCKEXT","LOCKSLEEP","LOCKTIMEOUT",
					"LOCKREFRESH", "PATH", "SENDMAIL"};
static const char *defaults_vals[]={LOCKEXT_DEF,LOCKSLEEP_DEF,LOCKTIMEOUT_DEF,
					LOCKREFRESH_DEF, DEFAULT_PATH,
					SENDMAIL_DEF};

Maildrop maildrop;

Maildrop::Maildrop()
{
	verbose_level=0;
	isdelivery=0;
	sigfpe=0;
	includelevel=0;
	embedded_mode=0;
	msgptr= &m1;
	savemsgptr= &m2;
}

static void help()
{
	mout << "Usage: maildrop [options] [-d user] [arg] [arg] ...\n";
	mout << "       maildrop [options] [filterfile [arg] [arg] ...\n";
}

static void bad()
{
	throw "Bad command line arguments, -h for help.\n";
}

static void nouser()
{
	errexit=EX_NOUSER;
	throw "Invalid user for -d option.\n";
}

static void copyright()
{
static const char msg[]="maildrop 0.51c Copyright 1998 Double Precision, Inc."

#if CRLF_TERM
	"\r\n"
#else
	"\n"
#endif
	"This program is distributed under the terms of the GNU General Public"
#if CRLF_TERM
	"\r\n"
#else
	"\n"
#endif
        "License. See COPYING for additional information."
#if CRLF_TERM
	"\r\n"
#else
	"\n"
#endif
 
		;

	mout << msg;
	mout.flush();
}

static int run(int argc, char **argv)
{
int	argn;
const	char *deliverymode=0;
const	char *embedded_filter=0;
Buffer	recipe;
uid_t	orig_uid;
Buffer	extra_headers;

	umask( 0007 );
	for (argn=1; argn < argc; )
	{
		if (argv[argn][0] != '-')	break;
		if (strcmp(argv[argn], "--") == 0)	{ ++argn; break; }

	char	optc=argv[argn][1];
	const char *optarg=argv[argn]+2;

		++argn;
		switch (optc)	{
		case 'd':
			if (!*optarg && argn < argc)	optarg=argv[argn++];
			deliverymode=optarg;
			break;
		case 'V':
			if (!*optarg && argn < argc)	optarg=argv[argn++];
			maildrop.verbose_level=atoi(optarg);
			break;
		case 'v':
			copyright();
			return (0);
		case 'm':
			maildrop.embedded_mode=1;
			break;
		case 'M':
			if (!*optarg && argn < argc)	optarg=argv[argn++];
			maildrop.embedded_mode=1;
			if (!deliverymode)	deliverymode="";
			if (!*optarg)
			{
				help();
				return (EX_TEMPFAIL);
			}
			embedded_filter=optarg;
			if (*(const char*)embedded_filter == SLASH_CHAR
				|| strchr(embedded_filter, '.'))
				throw "Invalid argument for -M.\n";
			break;
		case 'A':
			if (!*optarg && argn < argc)	optarg=argv[argn++];
			if (*optarg)
			{
				extra_headers += optarg;
				extra_headers += '\n';
			}
			break;
		case 'h':
			help();
			return (EX_TEMPFAIL);
		default:
			bad();
		}
	}

	orig_uid=getuid();
	if (!deliverymode && argn < argc)
		recipe=argv[argn++];
	else
	{
	struct	passwd	*pw;

		if (!deliverymode)	deliverymode="";

		if (*deliverymode)
		{
			pw=getpwnam(deliverymode);
			if (!pw)
				nouser();
#if	RESET_GID
			setgroupid(pw->pw_gid);
#endif
			setuid(pw->pw_uid);
			if (getuid() != pw->pw_uid)
				nouser();	// Security violation.
		}
		maildrop.isdelivery=1;
#if	RESTRICT_TRUSTED
		if ( getuid() != orig_uid)
		{
		static char trusted_users[]=TRUSTED_USERS;
		static char buf[ sizeof(trusted_users) ];
		char	*p;

			strcpy(buf, trusted_users);
			for (p=buf; (p=strtok(p, " ")) != 0; p=0)
			{
			struct	passwd *q=getpwnam(p);

				if (q && q->pw_uid == orig_uid)
					break;
			}

			if (!p)	nouser();	// Security violation
		}
#endif
	}

#if	RESET_GID
	setgroupid(getgid());
#endif
	setuid(getuid());	// Drop any setuid privileges.

int	i;
Buffer	name;
Buffer	value;

	if (!maildrop.isdelivery)
	{
		for (i=0; environ[i]; i++)
		{
			name=environ[i];

		char	*p=strchr(environ[i], '=');

			value= p ? (name.Length(p - environ[i]), p+1):"";
			SetVar(name, value);
		}
	}

	i=1;
	while (argn < argc)
	{
		name="";
		name.append( (unsigned long)i);
		value=argv[argn++];
		SetVar(name, value);
		++i;
	}

	for (i=0; i<(int)(sizeof(defaults_vars)/sizeof(defaults_vars[0])); i++)
	{
		name=defaults_vars[i];
		value=defaults_vals[i];
		SetVar(name, value);
	}

struct	passwd	*p=getpwuid(getuid());

	if (!p)	nouser();
	if (deliverymode)
	{
	struct	stat	buf;

		if (VerboseLevel() > 1)
			merr << "maildrop: Changing to " << p->pw_dir << "\n";

		if (chdir(p->pw_dir) < 0)
			throw "Unable to change to home directory.\n";
		recipe=".mailfilter";

		if ( stat(".", &buf) < 0 ||
			!S_ISDIR(buf.st_mode) ||
			buf.st_mode & S_IWOTH ||
			buf.st_uid != getuid())
			throw "Invalid directory permissions.\n";

		// Quietly terminate if the sticky bit is set on the homedir

		if ( buf.st_mode & S_ISVTX)
			return (EX_TEMPFAIL);

		if (embedded_filter)
		{
			i=stat(".mailfilters", &buf);

			if ( (i < 0 && errno != ENOENT) || (i >= 0 && (
				!S_ISDIR(buf.st_mode) ||
				(buf.st_mode & (S_IRWXO|S_IRWXG)) ||
				buf.st_uid != getuid())
							)
				)
				throw "Invalid directory permissions.\n";
			recipe = embedded_filter;
		}
	}
	name="HOME";
	value=p->pw_dir;
	maildrop.tempdir=p->pw_dir;
	maildrop.tempdir += "/" TEMPDIR;
	maildrop.tempdir += '\0';
	mkdir( (const char *)maildrop.tempdir, 0700 );
	SetVar(name, value);
	name="LOGNAME";
	value=p->pw_name;
	SetVar(name, value);
	name="SHELL";
	if (p->pw_shell && *p->pw_shell)
		value=p->pw_shell;
	else
		value="/bin/sh";
	SetVar(name, value);
	name="DEFAULT";
	value=GetDefaultMailbox(p->pw_name);
	SetVar(name, value);

Buffer	msg;

	maildrop.global_timer.Set(GLOBAL_TIMEOUT);
	maildrop.msgptr->Init(0);	// Read message from standard input.
	maildrop.msginfo.info( *maildrop.msgptr );
	maildrop.msgptr->ExtraHeaders(extra_headers);
	maildrop.msgptr->setmsgsize();

// If invoking user is root, trust the From line, else set it to invoking
// user.

	if (orig_uid > 0 || maildrop.msginfo.fromname.Length() == 0)
	{
		p=getpwuid(orig_uid);
		maildrop.msginfo.fromname=p ? p->pw_name:"root";
	}

	if (VerboseLevel() > 1)
	{
		msg.reset();
		msg.append("Message start at ");
		msg.append((unsigned long)maildrop.msginfo.msgoffset);
		msg.append(" bytes, envelope sender=");
		if (maildrop.msginfo.fromname.Length() > 0)
			msg += maildrop.msginfo.fromname;
		msg.append("\n");
		msg += '\0';
		merr.write(msg);
	}

int	fd;

	//
	//

	if (!embedded_filter && deliverymode)
	{
	Recipe r;
	Lexer in;

		fd=in.Open("/etc/maildroprc");
		if (fd < 0)
		{
			if (errno != ENOENT)
				throw "Error on open of /etc/maildroprc.\n";
		}
		else
		{
			if (r.ParseRecipe(in) < 0)
				return (EX_TEMPFAIL);
			r.ExecuteRecipe();
		}
	}

Recipe	r;
Lexer	in;

#ifdef	DEFAULTEXT
int	firstdefault=1;
#endif

	name="MAILFILTER";
	value=recipe;
	SetVar(name, value);

	for (;;)
	{
		if (embedded_filter)
		{
			msg=".mailfilters/";
			msg += recipe;
			if (VerboseLevel() > 1)
				merr << "maildrop: Attempting " << msg << "\n";
			msg += '\0';
			fd=in.Open((const char *)msg);
		}
		else
		{
			msg=recipe;
			if (VerboseLevel() > 1)
				merr << "maildrop: Attempting " << msg << "\n";
			msg += '\0';
			fd=in.Open((const char *)msg);
			break;
		}
#ifndef	DEFAULTEXT
		break;
#else
		if (fd >= 0)	break;
		if (errno != ENOENT)	break;

		if (firstdefault)
		{
			recipe += DEFAULTEXT;
			firstdefault=0;
			continue;
		}

		// Pop DEFAULTEXT bytes from end of recipe name

		for (fd=sizeof(DEFAULTEXT)-1; fd; --fd)
			recipe.pop();

		while (recipe.Length())
		{
			if (recipe.pop() == '-')
			{
				recipe += DEFAULTEXT;
				break;
			}
		}
		if (recipe.Length() == 0)
		{
			msg=".mailfilters/";
			msg += DEFAULTEXT+1;
			if (VerboseLevel() > 1)
				merr << "maildrop: Attempting " << msg << "\n";
			msg += '\0';
			fd=in.Open((const char *)msg);
			break;
		}
#endif
	}

	if (fd < 0)
	{
		//
		//  If we are operating in delivery mode, it's ok if
		//  .mailfilter does not exist.
		//
		if (!deliverymode || errno != ENOENT)
			throw "Unable to open filter file.\n";
	}
	else
	{
	struct	stat	stat_buf;

		if (fstat( fd, &stat_buf) != 0)
			throw "stat() failed.\n";

		if (!S_ISREG(stat_buf.st_mode) ||
			(stat_buf.st_mode & (S_IRWXO | S_IRWXG)) ||
			stat_buf.st_uid != getuid())
			throw "Invalid permissions on filter file.\n";

		if (r.ParseRecipe(in) < 0)
			return (EX_TEMPFAIL);

//		if (maildrop.msgptr->MessageSize() > 0)
			r.ExecuteRecipe();
	}
//
// If a message is succesfully delivered, an exception is thrown.
// If we're here, we should deliver to the default mailbox.
//

	if (!maildrop.embedded_mode)
	{
		name="DEFAULT";

	const char *v=GetVarStr(name);

		if (!v)	throw "DEFAULT mailbox not defined.\n";

		value=v;
		value += '\0';
		if (delivery((const char *)value) < 0)
			return (EX_TEMPFAIL);
	}

	value="EXITCODE";
	return ( GetVar(value)->Int("0") );
}

int main(int argc, char **argv)
{
	_exit(Maildrop::trap(run, argc, argv));
}

const char *GetDefaultMailbox(const char *username)
{
static Buffer buf;
int	isfile=0;

	buf="";

const	char *p=DEFAULT_DEF;

	if (*p != SLASH_CHAR)	// Relative to home directory
	{
		buf="HOME";
		buf=GetVarStr(buf);
		buf.push(SLASH_CHAR);
		isfile=1;
	}

	while (*p)
	{
		if (*p != '=')
		{
			buf.push(*p);
			++p;
		}

	const char *q=username;

		while (*p == '=')
		{
			buf.push (*q ? *q:'.');
			if (*q)	q++;
			p++;
		}
	}

	if (!isfile)
	{
		buf.push(SLASH_CHAR);
		buf += username;
	}
	buf += '\0';
	return (buf);
}

const char *TempName()
{
Buffer	t;

	t=(const char *)maildrop.tempdir;
	t += "/tmp.";
	t += '\0';
	return (TempName((const char *)t, 0));
}