File: copymsg.c

package info (click to toggle)
deliver 2.1.14-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 488 kB
  • ctags: 914
  • sloc: ansic: 6,050; yacc: 405; makefile: 131; sh: 34
file content (474 lines) | stat: -rw-r--r-- 9,546 bytes parent folder | download | duplicates (4)
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
/* $Id: copymsg.c,v 1.7 1998/01/11 02:16:59 chip Exp $
 *
 * Take the message from standard input and write it to two temp files,
 * one for the header (including the empty line) and one for the body.
 *
 * $Log: copymsg.c,v $
 * Revision 1.7  1998/01/11 02:16:59  chip
 * Security (buffer overflow) fix from Martin Schulze.
 *
 * Revision 1.6  1991/11/12 20:43:30  chip
 * Ignore return value of fflush().
 *
 * Revision 1.5  1991/10/23  20:02:46  chip
 * Use tdup().
 *
 * Revision 1.4  1991/08/27  15:39:15  chip
 * Add support for SYSV_FROM.
 *
 * Revision 1.3  1991/06/20  12:43:19  chip
 * Check return value of fclose().
 *
 * Revision 1.2  1991/05/23  17:23:19  chip
 * Follow RFC822 definition of header syntax.
 * Guard isxxx() macros against negative values.
 *
 * Revision 1.1  1991/05/13  18:36:55  chip
 * Initial revision
 *
 */

#include "deliver.h"
#include <time.h>

/*----------------------------------------------------------------------
 * Copy the message on the standard input to two temp files:
 * one for the header and one for the body.
 */

int
copy_message()
{
    char buf[BUFSIZ];
    FILE *dfp[T_MAX];
    CONTEXT *sender_ct;
    char *p, *from_line, *fsender, *fdate, *fremote;
    int t, b, c, nl;
    int ret = 0;

    /*
     * Create temporary files to hold the header and message body.
     */

    for (t = T_HDR; t <= T_BODY; ++t)
    {
	int fd;

	tfile[t] = tempfile();
	if ((tfd[t] = tcreate(tfile[t])) == -1)
	    return -1;

	if ((fd = tdup(tfd[t], ttype[t])) == -1)
	    return -1;
	(void) lseek(fd, 0L, 0);
	if ((dfp[t] = fdopen(fd, "r+")) == NULL)
	{
	    error("can't fdopen %s fd", ttype[t]);
	    (void) close(fd);
	    return -1;
	}
    }

    /* Debugging message for later examination of temp files. */

    if (verbose)
    {
	message("header=%s, body=%s\n",
		tfile[T_HDR], tfile[T_BODY]);
    }

    /*
     * If there is a From_ line, find the sender name therein.
     */

    from_line = NULL;
    fsender = fdate = fremote = NULL;

    b = (fgets(buf, GETSIZE(buf), stdin) ? TRUE : FALSE);

    if (b && skipfrom(buf) && (p = strchr(buf, '\n')) != NULL)
    {
	b = FALSE;

	/* Make a mungable copy of the From_ line */

	from_line = copystr(buf);
	if ((p = strchr(from_line, '\n')) != NULL)
	    *p = 0;

	/* Find sender */

	p = from_line + FROMSIZE;
	while (isspace(*p & 0xFF))
	    ++p;
	fsender = p;
	while (*p && !isspace(*p & 0xFF))
	    ++p;
	if (*p)
	    *p++ = 0;

	/* Date received should be around here somewhere */

	fdate = p;

	/* Find 'remote from' phrase (if any) */

	for (; (p = strchr(p, 'r')) != NULL; ++p)
	{
	    if (strncmp(p, "remote from", 11) == 0)
	    {
		*p = 0;
		p += 11;
		while (isspace(*p & 0xFF))
		    ++p;
		if (*p)
		    fremote = p;
		break;
	    }
	}

	/*
	 * Advance to first non-space in date.
	 * Strip trailing spaces from date.
	 * If there is no date, clear the date pointer.
	 */

	while (isspace(*fdate & 0xFF))
	    ++fdate;
	p = fdate + strlen(fdate);
	while (p > fdate && isspace(*(p - 1) & 0xFF))
	    *--p = 0;
	if (*fdate == 0)
	    fdate = NULL;

	/*
	 * If sender is missing, or if date is invalid,
	 * we consider the entire From_ line invalid.
	 */

	if (*fsender == 0
	    || (fdate != NULL && unctime(fdate) == -1))
	{
	    static char invuucp[] = "Invalid-UUCP-From: ";

	    /* Ignore everything we found. */

	    fsender = fdate = fremote = NULL;

	    /* Print invalid From_ line in a harmless way. */

	    (void) strcpy(from_line, buf);
	    (void) strcpy(buf, invuucp);
	    (void) strncat(buf, from_line, BUFSIZ - sizeof invuucp);
	    buf[BUFSIZ - 1] = '\0';
	    b = TRUE;
	}
    }

    /*
     * Write a From_ line to the header file.
     */

    /* if caller specified original sender, use it */
    if (orig_sender)
	;			/* fine */

    /* else if we found a From_ line, use it */
    else if (fsender)
    {
	if (fremote)
	{
	    orig_sender = zalloc(strlen(fremote) + sizeof("!")
				 + strlen(fsender));
	    (void) sprintf(orig_sender, "%s!%s", fremote, fsender);
	}
	else
	    orig_sender = copystr(fsender);
    }

    /* else use the local sender */
    else
	orig_sender = local_sender;

    /* debugging message */

    if (verbose && strcmp(orig_sender, local_sender) != 0)
	message("original sender is \"%s\"\n", orig_sender);

    /*
     * Finally!  Write the From_ line.
     */

    (void) fputs("From ", dfp[T_HDR]);
    (void) fputs(orig_sender, dfp[T_HDR]);
    (void) fputc(' ', dfp[T_HDR]);

    if (fdate)
    {
	(void) fputs(fdate, dfp[T_HDR]);
	(void) fputc('\n', dfp[T_HDR]);
    }
    else
    {
	struct tm *ftm;
	time_t now;

	(void) time(&now);
	ftm = localtime(&now);
	p = asctime(ftm);
#ifdef SYSV_FROM
	(void) fprintf(dfp[T_HDR], "%.16s %.3s %.5s", p, tmzone(ftm), p + 20);
#else
	(void) fputs(p, dfp[T_HDR]);
#endif
    }

    /*
     * Free the From_ line if we allocated a copy of it.
     */

    if (from_line)
	free(from_line);

    /*
     * If a local sender is not a mailer, and if the original and
     * local senders differ, record the attempt at forgery.
     */

    if (!mailer_user
	&& ((sender_ct = name_context(orig_sender)) == NULL
	    || sender_ct->ct_uid != real_uid))
    {
	(void) fputs("X-Delivered: at request of ", dfp[T_HDR]);
	(void) fputs(local_sender, dfp[T_HDR]);
	(void) fputs(" on ", dfp[T_HDR]);
	(void) fputs(hostname, dfp[T_HDR]);
	(void) fputc('\n', dfp[T_HDR]);
    }

    /*
     * Copy the rest of the header (if any).
     */

    for (; !feof(stdin) && !ferror(stdin); b = FALSE)
    {
	if (!b)
	{
	    if (fgets(buf, GETSIZE(buf), stdin))
		b = TRUE;
	    else
		break;
	}

	/* Empty line means "end of header" */

	if (buf[0] == '\n')
	{
	    b = FALSE;		/* Don't put this line in the body. */
	    break;
	}

	/*
	 * A line too long to fit in buf[] can't be a header line.
	 * At least, that's my opinion... :-)
	 */

	if (!strchr(buf, '\n'))
	    break;

	/*
	 * If line begins with whitespace, it's a continuation.
	 * Else if line begins with From_ or '>', prepend '>'.
	 * Else if line doesn't look like a header, this must
	 * be the beginning of the body.
	 */

	if (isspace(buf[0] & 0xFF))
	    ;			/* continuation */
	else if (skipfrom(buf) || (buf[0] == '>'))
	    (void) fputc('>', dfp[T_HDR]);
	else
	{
	    /* look for the colon on a header label */

	    p = buf;
	    while (*p
		   && !isspace(*p & 0xFF)
		   && !iscntrl(*p & 0xFF)
		   && *p != ':')
		++p;
	    if ((p == buf) || (*p != ':'))
		break;		/* Not a header line! */
	}

	/* Write the line to the header file. */

	(void) fputs(buf, dfp[T_HDR]);
    }

    /*
     * End the header file with a blank line.
     * This enables us to simply concatenate it with the body file
     * to produce a valid message.
     */

    (void) fputc('\n', dfp[T_HDR]);

    /*
     * Copy the body (if any).
     * Ensure that it ends with a blank line.
     */

    if (b)
	(void) fputs(buf, dfp[T_BODY]);

    nl = 1;
    if (!feof(stdin) && !ferror(stdin))
    {
	while ((c = getchar()) != EOF)
	{
	    (void) fputc(c, dfp[T_BODY]);
	    if (c == '\n')
		++nl;
	    else
		nl = 0;
	}
    }
    for (; nl < 2; ++nl)
	(void) fputc('\n', dfp[T_BODY]);

    /*
     * If we encountered any trouble writing to the temp files,
     * let's not keep it secret.
     */

    for (t = T_HDR; t <= T_BODY; ++t)
    {
	(void) fflush(dfp[t]);
	if (ferror(dfp[t]))
	{
	    error("error writing to %s file %s",
		  ttype[t], tfile[t]);
	    ret = -1;
	}
	if (fclose(dfp[t]))
	{
	    error("error closing %s file %s",
		  ttype[t], tfile[t]);
	    ret = -1;
	}
    }

    /* Return error/success. */

    return ret;
}

/*----------------------------------------------------------------------
 * Don't bother copying message.
 * Put the original names in the environment.
 */

int
dont_copy()
{
    int r, t;

    for (r = T_HDR, t = T_HDRCOPY; r <= T_BODY; ++r, ++t)
    {
	if (tenv[t] && tfile[r])
	    alloc_env(tenv[t], tfile[r]);
    }

    if (verbose)
    {
	message("dont_copy: header is %s, body is %s\n",
		tfile[T_HDR], tfile[T_BODY]);
    }

    return 0;
}

/*----------------------------------------------------------------------
 * Create another copy of each temp file, for security reasons.
 * Also, put the names of the copies in the environment.
 */

int
copy_again()
{
    int r, t;

    for (r = T_HDR, t = T_HDRCOPY; r <= T_BODY; ++r, ++t)
    {
	/* If the file is open, close it. */

	if (tfd[t] != -1)
	{
	    (void) close(tfd[t]);
	    tfd[t] = -1;
	}

	/*
	 * If the file exists, remove it but keep its name.
	 * Otherwise, make a new name and put that name in
	 * the environment.
	 */

	if (tfile[t])
	    (void) unlink(tfile[t]);
	else
	{
	    tfile[t] = tempfile();
	    if (tenv[t])
		alloc_env(tenv[t], tfile[t]);
	}

	/*
	 * Create the file and copy the contents of the
	 * original file to it.
	 */

	if ((tfd[t] = tcreate(tfile[t])) == -1)
	    return -1;

	(void) lseek(tfd[r], 0L, 0);
	if (copyfd(tfd[r], tfd[t]) < 0)
	    return -1;
    }

    if (verbose)
    {
	message("copy_again: header to %s, body to %s\n",
		tfile[T_HDRCOPY], tfile[T_BODYCOPY]);
    }

    return 0;
}

/*----------------------------------------------------------------------
 * Copy a file via file descriptors.
 */

int
copyfd(src_fd, dest_fd)
int src_fd;
int dest_fd;
{
    char buf[BUFSIZ];
    int rd, wr;

    while ((rd = read(src_fd, buf, sizeof(buf))) > 0)
    {
	if ((wr = write(dest_fd, buf, (unsigned) rd)) != rd)
	{
	    if (wr == -1)
		syserr("can't write in copyfd");
	    else
		error("write error -- disk full?");
	    return -1;
	}
    }

    return 0;
}