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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "mail.h"
/*
Send mail - High level sending routine
*/
void sendmail(argc, argv)
char **argv;
{
char **args;
char *tp, *zp;
char buf[2048],last1c;
FILE *input;
struct stat sbuf;
int aret;
int i, n;
int oldn = 1;
int ttyf = 0;
int pushrest = 0;
int hdrtyp = 0;
int ctf = FALSE;
int binflg = 0;
long count = 0L;
struct tm *bp;
struct hdrs *hptr;
static char pn[] = "sendmail";
reciplist list;
Dout(pn, 0, "entered\n");
new_reciplist(&list);
for (i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
if (argv[i][1] == '\0') {
errmsg(E_SYNTAX,"Hyphens MAY NOT be followed by spaces");
}
if (i > 1) {
errmsg(E_SYNTAX,"Options MUST PRECEDE persons");
}
done(0);
}
/*
Ensure no NULL names in list
*/
if (argv[i][0] == '\0' || argv[i][strlen(argv[i])-1] == '!') {
errmsg(E_SYNTAX,"Null names are not allowed");
done(0);
}
add_recip(&list, argv[i], FALSE); /* Don't check for duplication */
}
mktmp();
/*
Format time
*/
time(&iop);
bp = localtime(&iop);
tp = asctime(bp);
zp = tzname[bp->tm_isdst];
sprintf(datestring, "%.16s %.3s %.5s", tp, zp, tp+20);
trimnl (datestring);
/* asctime: Fri Sep 30 00:00:00 1986\n */
/* 0123456789012345678901234 */
/* RFCtime: Fri, 28 Jul 89 10:30 EDT */
sprintf(RFC822datestring, "%.3s, %.2s %.3s %.4s %.5s %.3s",
tp, tp+8, tp+4, tp+20, tp+11, zp);
/*
Write out the from line header for the letter
*/
if (fromflag && deliverflag && from_user[0] != '\0') {
(void) snprintf(buf, sizeof (buf), "%s%s %s\n",
header[H_FROM].tag, from_user, datestring);
} else {
(void) snprintf(buf, sizeof (buf), "%s%s %s\n",
header[H_FROM].tag, my_name, datestring);
}
if (!wtmpf(buf, strlen(buf))) {
done(0);
}
savehdrs(buf, H_FROM);
/*
Copy to list in mail entry?
*/
if (flgt == 1 && argc > 1) {
aret = argc;
args = argv;
while (--aret > 0) {
(void) snprintf(buf, sizeof (buf), "%s %s\n", header[H_TO].tag, *++args);
if (!wtmpf(buf, strlen(buf))) {
done(0);
}
savehdrs(buf, H_TO);
}
}
flgf = 1; /* reset when first read of message body succeeds */
/*
Read mail message, allowing for lines of infinite
length. This is tricky, have to watch for newlines.
*/
saveint = setsig(SIGINT, savdead);
last1c = ' '; /* anything other than newline */
ttyf = isatty (fileno(stdin));
pushrest = 0;
/*
* scan header & save relevant info.
*/
(void) strlcpy(fromU, my_name, sizeof (fromU));
fromS[0] = 0; /* set up for >From scan */
input = stdin;
if (fstat(fileno(input), &sbuf) < 0) {
if (errno == EOVERFLOW) {
perror("stdin");
exit(1);
}
}
while ((n = getline (line, sizeof line, stdin)) > 0) {
last1c = line[n-1];
if (pushrest) {
if (!wtmpf(line,n)) {
done(0);
}
pushrest = (last1c != '\n');
continue;
}
pushrest = (last1c != '\n');
if ((hdrtyp = isheader (line, &ctf)) == FALSE) {
break;
}
flgf = 0;
switch (hdrtyp) {
case H_RVERS:
/* Are we dealing with a delivery report? */
/* dflag = 9 ==> do not return on failure */
dflag = 9;
Dout(pn, 0, "dflag = 9\n");
break;
case H_FROM:
if (!wtmpf(">", 1)) {
done(0);
}
/* note dropthru */
hdrtyp = H_FROM1;
case H_FROM1:
if (substr(line, "forwarded by") > -1) {
break;
}
pickFrom (line);
if (Rpath[0] != '\0') {
strcat(Rpath, "!");
}
(void) strlcat(Rpath, fromS, sizeof (Rpath));
n = 0; /* don't copy remote from's into mesg. */
break;
case H_MIMEVERS:
case H_CLEN:
case H_CTYPE:
/* suppress it: only generated if needed */
n = 0; /* suppress */
break;
case H_TCOPY:
/* Write out placeholder for later */
(void) snprintf(buf, sizeof (buf), "%s \n", header[H_TCOPY].tag);
if (!wtmpf(buf, strlen(buf))) {
done(0);
}
n = 0; /* suppress */
break;
case H_MTYPE:
if (flgm) {
/* suppress if message-type argument */
n = 0;
}
break;
case H_CONT:
if (oldn == 0) {
/* suppress continuation line also */
n = 0;
}
break;
}
oldn = n; /* remember if this line was suppressed */
if (n && !wtmpf(line,n)) {
done(0);
}
if (!n) savehdrs(line, hdrtyp);
}
if (Rpath[0] != '\0') {
strcat(Rpath, "!");
}
(void) strlcat(Rpath, fromU, sizeof (Rpath));
/* push out message type if so requested */
if (flgm) { /* message-type */
snprintf(buf, sizeof(buf), "%s%s\n", header[H_MTYPE].tag, msgtype);
if (!wtmpf(buf, strlen(buf))) {
done(0);
}
}
memcpy (buf, line, n);
if (n == 0 || (ttyf && !strncmp (buf, ".\n", 2)) ) {
if (flgf) {
/* no input */
return;
} else {
/*
* no content: put mime-version, content-type
* and -length only if explicitly present.
* Write out 'place-holders' only. (see below....)
*/
if ((hptr = hdrlines[H_MIMEVERS].head) !=
(struct hdrs *)NULL) {
(void) snprintf(line, sizeof (line), "%s \n", header[H_MIMEVERS].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
}
if ((hptr = hdrlines[H_CTYPE].head) !=
(struct hdrs *)NULL) {
(void) snprintf(line, sizeof (line), "%s \n", header[H_CTYPE].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
}
if ((hptr = hdrlines[H_CLEN].head) !=
(struct hdrs *)NULL) {
(void) snprintf(line, sizeof (line), "%s \n", header[H_CLEN].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
}
goto wrapsend;
}
}
if (n == 1 && last1c == '\n') { /* blank line -- suppress */
n = getline (buf, sizeof buf, stdin);
if (n == 0 || (ttyf && !strncmp (buf, ".\n", 2)) ) {
/*
* no content: put mime-version, content-type
* and -length only if explicitly present.
* Write out 'place-holders' only. (see below....)
*/
if ((hptr = hdrlines[H_MIMEVERS].head) !=
(struct hdrs *)NULL) {
(void) snprintf(line, sizeof (line), "%s \n", header[H_MIMEVERS].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
}
if ((hptr = hdrlines[H_CTYPE].head) !=
(struct hdrs *)NULL) {
(void) snprintf(line, sizeof (line), "%s \n", header[H_CTYPE].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
}
if ((hptr = hdrlines[H_CLEN].head) !=
(struct hdrs *)NULL) {
(void) snprintf(line, sizeof (line), "%s \n", header[H_CLEN].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
}
goto wrapsend;
}
}
if (debug > 0) {
buf[n] = '\0';
Dout(pn, 0, "header scan complete, readahead %d = \"%s\"\n", n, buf);
}
/*
* Write out H_MIMEVERS, H_CTYPE & H_CLEN lines. These are used only as
* placeholders in the tmp file. When the 'real' message is sent,
* the proper values will be put out by copylet().
*/
(void) snprintf(line, sizeof (line), "%s \n", header[H_MIMEVERS].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
if (hdrlines[H_MIMEVERS].head == (struct hdrs *)NULL) {
savehdrs(line, H_MIMEVERS);
}
(void) snprintf(line, sizeof (line), "%s \n", header[H_CTYPE].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
if (hdrlines[H_CTYPE].head == (struct hdrs *)NULL) {
savehdrs(line,H_CTYPE);
}
(void) snprintf(line, sizeof (line), "%s \n", header[H_CLEN].tag);
if (!wtmpf(line, strlen(line))) {
done(0);
}
if (hdrlines[H_CLEN].head == (struct hdrs *)NULL) {
savehdrs(line,H_CLEN);
}
/* and a blank line */
if (!wtmpf("\n", 1)) {
done(0);
}
Dout(pn, 0, "header out completed\n");
pushrest = 0;
count = 0L;
/*
* Are we returning mail from a delivery failure of an old-style
* (SVR3.1 or SVR3.0) rmail? If so, we won't return THIS on failure
* [This line should occur as the FIRST non-blank non-header line]
*/
if (!strncmp("***** UNDELIVERABLE MAIL sent to",buf,32)) {
dflag = 9; /* 9 says do not return on failure */
Dout(pn, 0, "found old-style UNDELIVERABLE line. dflag = 9\n");
}
/* scan body of message */
while (n > 0) {
if (ttyf && !strcmp (buf, ".\n"))
break;
if (!binflg) {
binflg = !istext ((unsigned char *)buf, n);
}
if (!wtmpf(buf, n)) {
done(0);
}
count += n;
n = ttyf
? getline (buf, sizeof buf, stdin)
: fread (buf, 1, sizeof buf, stdin);
}
setsig(SIGINT, saveint);
wrapsend:
/*
* In order to use some of the subroutines that are used to
* read mail, the let array must be set up
*/
nlet = 1;
let[0].adr = 0;
let[1].adr = ftell(tmpf);
let[0].text = (binflg == 1 ? FALSE : TRUE);
Dout(pn, 0, "body copy complete, count %ld\n", count);
/*
* Modify value of H_MIMEVERS if necessary.
*/
if ((hptr = hdrlines[H_MIMEVERS].head) != (struct hdrs *)NULL) {
if (strlen(hptr->value) == 0) {
(void) strlcpy(hptr->value, "1.0",
sizeof (hptr->value));
}
}
/*
* Modify value of H_CTYPE if necessary.
*/
if ((hptr = hdrlines[H_CTYPE].head) != (struct hdrs *)NULL) {
if (strlen(hptr->value) == 0) {
(void) strlcpy(hptr->value, "text/plain",
sizeof (hptr->value));
}
}
/*
* Set 'place-holder' value of content length to true value
*/
if ((hptr = hdrlines[H_CLEN].head) != (struct hdrs *)NULL) {
(void) snprintf(hptr->value, sizeof (hptr->value),
"%ld", count);
}
if (fclose(tmpf) == EOF) {
tmperr();
done(0);
}
tmpf = doopen(lettmp,"r+",E_TMP);
/* Do not send mail on SIGINT */
if (dflag == 2) {
done(0);
}
sendlist(&list, 0, 0);
done(0);
}
|