summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sendmail/util/mailcompat.c
blob: ce685337f05254852a7c4b093646cba5d93e6cc7 (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
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 *	Copyright (c) 1983, 1984, 1986, 1986, 1987, 1988, 1989 AT&T
 *	  All Rights Reserved
 */

/*
 *  Vacation
 *  Copyright (c) 1983  Eric P. Allman
 *  Berkeley, California
 *
 *  Copyright (c) 1983 Regents of the University of California.
 *  All rights reserved.  The Berkeley software License Agreement
 *  specifies the terms and conditions for redistribution.
 */

#include <pwd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <sysexits.h>
#include <string.h>
#include <ctype.h>
#include <sm/bitops.h>
#include "conf.h"

/*
 *  MAILCOMPAT -- Deliver mail to a user's mailbox with the "From's" stuffed.
 */

typedef int bool;

#define	FALSE	0
#define	TRUE	1

bool	Debug = FALSE;
char	*myname;		/* person who is to have their mail filtered */
char	*homedir;		/* home directory of said person */
char	*AliasList[MAXLINE];	/* list of aliases to allow */
char    *fromp;
char    *fromuser;
int	AliasCount = 0;

static char *newstr();

int	ask(char *);
int	sendmessage(char *);
void	AutoInstall(void);
void	usrerr(const char *, ...);

int
main(argc, argv)
	int argc;
	char **argv;
{
	register char *p;
	struct passwd *pw;
	extern char *getfrom();

	/* process arguments */
	while (--argc > 0 && (p = *++argv) != NULL && *p == '-')
	{
		switch (*++p)
		{
		    case 'd':	/* debug */
			Debug = TRUE;
			break;
		    default:
			usrerr("Unknown flag -%s", p);
			exit(EX_USAGE);
		}
	}

	/* verify recipient argument */
	if (argc != 1)
	{
		if (argc == 0)
			AutoInstall();
		else
			usrerr("Usage: mailcompat username (or) mailcompat -r");
		exit(EX_USAGE);
	}

	myname = p;
	/* find user's home directory */
	pw = getpwnam(myname);
	if (pw == NULL)
	{
		usrerr("user: %s look up failed, name services outage ?", myname);
		exit(EX_TEMPFAIL);
	}
	homedir = newstr(pw->pw_dir);

	/* read message from standard input (just from line) */
	fromuser = getfrom(&fromp);
	return (sendmessage(fromuser));
}

/*
**  sendmessage -- read message from standard input do the from stuffing
**             and forward to /bin/mail, Being sure to delete any
**             content-length headers (/bin/mail recalculates them).
**
**
**	Parameters:
**		none.
**
**
**	Side Effects:
**		Reads first line from standard input.
*/

#define	L_HEADER	"Content-Length:"
#define	LL_HEADER	15

int
sendmessage(from)
char *from;
{
	static char line[MAXLINE];
	static char command[MAXLINE];
	bool in_body = FALSE;
	FILE *mail_fp;
	static char user_name[L_cuserid];

	if (from == NULL)
		from = cuserid(user_name);

	snprintf(command, sizeof (command), "/bin/mail -f %s -d %s", from,
	    myname);
	mail_fp = popen(command, "w");

	/* read the  line */
	while (fgets(line, sizeof line, stdin) != NULL)
	{
		if (line[0] == (char)'\n')  /* end of mail headers */
			in_body = TRUE;
		if (in_body && (strncmp(line, "From ", 5) == 0))
			fprintf(mail_fp, ">");
		if (in_body || (strncasecmp(line, L_HEADER, LL_HEADER) != 0))
			fputs(line, mail_fp);
	}
	return (pclose(mail_fp));
}

char *
getfrom(shortp)
char **shortp;
{
	static char line[MAXLINE];
	register char *p, *start, *at, *bang;
	char saveat;

	/* read the from line */
	if (fgets(line, sizeof line, stdin) == NULL ||
	    strncmp(line, "From ", 5) != 0)
	{
		usrerr("No initial From line");
		exit(EX_USAGE);
	}

	/* find the end of the sender address and terminate it */
	start = &line[5];
	p = strchr(start, ' ');
	if (p == NULL)
	{
		usrerr("Funny From line '%s'", line);
		exit(EX_USAGE);
	}
	*p = '\0';

	/*
	 * Strip all but the rightmost UUCP host
	 * to prevent loops due to forwarding.
	 * Start searching leftward from the leftmost '@'.
	 *	a!b!c!d yields a short name of c!d
	 *	a!b!c!d@e yields a short name of c!d@e
	 *	e@a!b!c yields the same short name
	 */
#ifdef VDEBUG
printf("start='%s'\n", start);
#endif /* VDEBUG */
	*shortp = start;			/* assume whole addr */
	if ((at = strchr(start, '@')) == NULL)	/* leftmost '@' */
		at = p;				/* if none, use end of addr */
	saveat = *at;
	*at = '\0';
	if ((bang = strrchr(start, '!')) != NULL) {	/* rightmost '!' */
		char *bang2;
		*bang = '\0';
		if ((bang2 = strrchr(start, '!')) != NULL) /* 2nd rightmost '!' */
			*shortp = bang2 + 1;		/* move past ! */
		*bang = '!';
	}
	*at = saveat;
#ifdef VDEBUG
printf("place='%s'\n", *shortp);
#endif /* VDEBUG */

	/* return the sender address */
	return newstr(start);
}

/*
**  USRERR -- print user error
**
**	Parameters:
**		f -- format.
**
**	Returns:
**		none.
**
**	Side Effects:
**		none.
*/

void
usrerr(const char *f, ...)
{
	va_list alist;

	va_start(alist, f);
	(void) fprintf(stderr, "mailcompat: ");
	(void) vfprintf(stderr, f, alist);
	(void) fprintf(stderr, "\n");
	va_end(alist);
}

/*
**  NEWSTR -- copy a string
**
**	Parameters:
**		s -- the string to copy.
**
**	Returns:
**		A copy of the string.
**
**	Side Effects:
**		none.
*/

char *
newstr(s)
	char *s;
{
	char *p;
	size_t psize = strlen(s) + 1;

	p = malloc(psize);
	if (p == NULL)
	{
		usrerr("newstr: cannot alloc memory");
		exit(EX_OSERR);
	}
	strlcpy(p, s, psize);
	return (p);
}

/*
 * When invoked with no arguments, we fall into an automatic installation
 * mode, stepping the user through a default installation.
 */
void
AutoInstall()
{
	char forward[MAXLINE];
	char line[MAXLINE];
	static char user_name[L_cuserid];
	FILE *f;

	myname = cuserid(user_name);
	homedir = getenv("HOME");
	if (homedir == NULL) {
		usrerr("Home directory unknown");
		exit(EX_CONFIG);
	}

	printf("This program can be used to store your mail in a format\n");
	printf("that you can read with SunOS 4.X based mail readers\n");
	(void) strlcpy(forward, homedir, sizeof (forward));
	(void) strlcat(forward, "/.forward", sizeof (forward));
	f = fopen(forward, "r");
	if (f) {
		printf("You have a .forward file in your home directory");
		printf("  containing:\n");
		while (fgets(line, MAXLINE, f))
			printf("    %s", line);
		fclose(f);
		if (!ask("Would you like to remove it and disable the mailcompat feature"))
			exit(0);
		if (unlink(forward))
			perror("Error removing .forward file:");
		else
			printf("Back to normal reception of mail.\n");
		exit(0);
	}

	printf("To enable the mailcompat feature a \".forward\" ");
	printf("file is created.\n");
	if (!ask("Would you like to enable the mailcompat feature")) {
		printf("OK, mailcompat feature NOT enabled.\n");
		exit(0);
	}
	f = fopen(forward, "w");
	if (f == NULL) {
		perror("Error opening .forward file");
		exit(EX_USAGE);
	}
	fprintf(f, "\"|/usr/bin/mailcompat %s\"\n", myname);
	fclose(f);
	printf("Mailcompat feature ENABLED.");
	printf("Run mailcompat with no arguments to remove it\n");
}


/*
 * Ask the user a question until we get a reasonable answer
 */
int
ask(prompt)
	char *prompt;
{
	char line[MAXLINE];

	for (;;) {
		printf("%s? ", prompt);
		fflush(stdout);
		fgets(line, sizeof (line), stdin);
		if (line[0] == 'y' || line[0] == 'Y')
			return (TRUE);
		if (line[0] == 'n' || line[0] == 'N')
			return (FALSE);
		printf("Please reply \"yes\" or \"no\" (\'y\' or \'n\')\n");
	}
	/* NOTREACHED */
}