send.c revision 1.28 1 1.26 christos /* $NetBSD: send.c,v 1.28 2006/11/28 18:45:32 christos Exp $ */
2 1.6 christos
3 1.1 cgd /*
4 1.4 deraadt * Copyright (c) 1980, 1993
5 1.4 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.21 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.7 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.6 christos #if 0
35 1.6 christos static char sccsid[] = "@(#)send.c 8.1 (Berkeley) 6/6/93";
36 1.6 christos #else
37 1.26 christos __RCSID("$NetBSD: send.c,v 1.28 2006/11/28 18:45:32 christos Exp $");
38 1.6 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include "rcv.h"
42 1.4 deraadt #include "extern.h"
43 1.26 christos #ifdef MIME_SUPPORT
44 1.26 christos #include "mime.h"
45 1.26 christos #endif
46 1.26 christos
47 1.27 christos
48 1.28 christos /*
49 1.28 christos * Mail -- a mail program
50 1.28 christos *
51 1.28 christos * Mail to others.
52 1.28 christos */
53 1.28 christos
54 1.28 christos /*
55 1.28 christos * compare twwo name structures. Support for get_smopts.
56 1.28 christos */
57 1.27 christos static int
58 1.27 christos namecmp(struct name *np1, struct name *np2)
59 1.27 christos {
60 1.27 christos for (/*EMPTY*/; np1 && np2; np1 = np1->n_flink, np2 = np2->n_flink) {
61 1.27 christos if (strcmp(np1->n_name, np2->n_name) != 0)
62 1.27 christos return 1;
63 1.27 christos }
64 1.27 christos return np1 || np2;
65 1.27 christos }
66 1.27 christos
67 1.28 christos /*
68 1.28 christos * Get the sendmail options from the smopts list.
69 1.28 christos */
70 1.27 christos static struct name *
71 1.27 christos get_smopts(struct name *to)
72 1.27 christos {
73 1.27 christos struct smopts_s *sp;
74 1.27 christos struct name *smargs, *np;
75 1.27 christos smargs = NULL;
76 1.27 christos for (np = to; np; np = np->n_flink) {
77 1.27 christos if ((sp = findsmopts(np->n_name, 0)) == NULL)
78 1.27 christos continue;
79 1.27 christos if (smargs == NULL)
80 1.27 christos smargs = sp->s_smopts;
81 1.27 christos else if (namecmp(smargs, sp->s_smopts) != 0)
82 1.27 christos return NULL;
83 1.27 christos }
84 1.27 christos if (smargs &&
85 1.27 christos smargs->n_flink == NULL &&
86 1.27 christos (smargs->n_name == NULL || smargs->n_name[0] == '\0'))
87 1.27 christos return NULL;
88 1.27 christos return smargs;
89 1.27 christos }
90 1.1 cgd
91 1.1 cgd /*
92 1.28 christos * Output a reasonable looking status field.
93 1.1 cgd */
94 1.28 christos static void
95 1.28 christos statusput(struct message *mp, FILE *obuf, const char *prefix)
96 1.28 christos {
97 1.28 christos char statout[3];
98 1.28 christos char *cp = statout;
99 1.28 christos
100 1.28 christos if (mp->m_flag & MREAD)
101 1.28 christos *cp++ = 'R';
102 1.28 christos if ((mp->m_flag & MNEW) == 0)
103 1.28 christos *cp++ = 'O';
104 1.28 christos *cp = 0;
105 1.28 christos if (statout[0])
106 1.28 christos (void)fprintf(obuf, "%sStatus: %s\n",
107 1.28 christos prefix == NULL ? "" : prefix, statout);
108 1.28 christos }
109 1.1 cgd
110 1.1 cgd /*
111 1.1 cgd * Send message described by the passed pointer to the
112 1.1 cgd * passed output buffer. Return -1 on error.
113 1.1 cgd * Adjust the status: field if need be.
114 1.1 cgd * If doign is given, suppress ignored header fields.
115 1.1 cgd * prefix is a string to prepend to each output line.
116 1.1 cgd */
117 1.28 christos PUBLIC int
118 1.26 christos sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
119 1.26 christos const char *prefix, struct mime_info *mip)
120 1.1 cgd {
121 1.23 christos off_t len;
122 1.7 lukem FILE *ibuf;
123 1.1 cgd char line[LINESIZE];
124 1.28 christos int isheadflag, infld, ignoring, dostat, firstline;
125 1.7 lukem char *cp, *cp2;
126 1.28 christos int c; /* XXX - this variable is horribly abused */
127 1.23 christos size_t length;
128 1.28 christos size_t prefixlen;
129 1.28 christos
130 1.28 christos c = 0;
131 1.28 christos ignoring = 0;
132 1.28 christos prefixlen = 0;
133 1.1 cgd
134 1.1 cgd /*
135 1.1 cgd * Compute the prefix string, without trailing whitespace
136 1.1 cgd */
137 1.16 wiz if (prefix != NULL) {
138 1.22 christos const char *dp, *dp2 = NULL;
139 1.22 christos for (dp = prefix; *dp; dp++)
140 1.22 christos if (*dp != ' ' && *dp != '\t')
141 1.22 christos dp2 = dp;
142 1.22 christos prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
143 1.1 cgd }
144 1.1 cgd ibuf = setinput(mp);
145 1.15 wiz len = mp->m_size;
146 1.15 wiz isheadflag = 1;
147 1.1 cgd dostat = doign == 0 || !isign("status", doign);
148 1.1 cgd infld = 0;
149 1.1 cgd firstline = 1;
150 1.1 cgd /*
151 1.1 cgd * Process headers first
152 1.1 cgd */
153 1.26 christos #ifdef MIME_SUPPORT
154 1.26 christos if (mip)
155 1.26 christos obuf = mime_decode_header(mip);
156 1.26 christos #endif
157 1.15 wiz while (len > 0 && isheadflag) {
158 1.1 cgd if (fgets(line, LINESIZE, ibuf) == NULL)
159 1.1 cgd break;
160 1.15 wiz len -= length = strlen(line);
161 1.1 cgd if (firstline) {
162 1.1 cgd /*
163 1.1 cgd * First line is the From line, so no headers
164 1.1 cgd * there to worry about
165 1.1 cgd */
166 1.1 cgd firstline = 0;
167 1.1 cgd ignoring = doign == ignoreall;
168 1.26 christos #ifdef MIME_SUPPORT
169 1.26 christos /* XXX - ignore multipart boundary lines! */
170 1.26 christos if (line[0] == '-' && line[1] == '-')
171 1.26 christos ignoring = 1;
172 1.26 christos #endif
173 1.1 cgd } else if (line[0] == '\n') {
174 1.1 cgd /*
175 1.1 cgd * If line is blank, we've reached end of
176 1.1 cgd * headers, so force out status: field
177 1.1 cgd * and note that we are no longer in header
178 1.1 cgd * fields
179 1.1 cgd */
180 1.1 cgd if (dostat) {
181 1.1 cgd statusput(mp, obuf, prefix);
182 1.1 cgd dostat = 0;
183 1.1 cgd }
184 1.15 wiz isheadflag = 0;
185 1.1 cgd ignoring = doign == ignoreall;
186 1.1 cgd } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
187 1.1 cgd /*
188 1.1 cgd * If this line is a continuation (via space or tab)
189 1.1 cgd * of a previous header field, just echo it
190 1.1 cgd * (unless the field should be ignored).
191 1.1 cgd * In other words, nothing to do.
192 1.1 cgd */
193 1.1 cgd } else {
194 1.1 cgd /*
195 1.1 cgd * Pick up the header field if we have one.
196 1.1 cgd */
197 1.28 christos for (cp = line; (c = *cp++) && c != ':' && !isspace(c); /*EMPTY*/)
198 1.28 christos continue;
199 1.1 cgd cp2 = --cp;
200 1.10 christos while (isspace((unsigned char)*cp++))
201 1.28 christos continue;
202 1.1 cgd if (cp[-1] != ':') {
203 1.1 cgd /*
204 1.1 cgd * Not a header line, force out status:
205 1.1 cgd * This happens in uucp style mail where
206 1.1 cgd * there are no headers at all.
207 1.1 cgd */
208 1.1 cgd if (dostat) {
209 1.1 cgd statusput(mp, obuf, prefix);
210 1.1 cgd dostat = 0;
211 1.1 cgd }
212 1.1 cgd if (doign != ignoreall)
213 1.1 cgd /* add blank line */
214 1.18 wiz (void)putc('\n', obuf);
215 1.15 wiz isheadflag = 0;
216 1.1 cgd ignoring = 0;
217 1.1 cgd } else {
218 1.1 cgd /*
219 1.1 cgd * If it is an ignored field and
220 1.1 cgd * we care about such things, skip it.
221 1.1 cgd */
222 1.1 cgd *cp2 = 0; /* temporarily null terminate */
223 1.1 cgd if (doign && isign(line, doign))
224 1.1 cgd ignoring = 1;
225 1.1 cgd else if ((line[0] == 's' || line[0] == 'S') &&
226 1.1 cgd strcasecmp(line, "status") == 0) {
227 1.1 cgd /*
228 1.1 cgd * If the field is "status," go compute
229 1.1 cgd * and print the real Status: field
230 1.1 cgd */
231 1.1 cgd if (dostat) {
232 1.1 cgd statusput(mp, obuf, prefix);
233 1.1 cgd dostat = 0;
234 1.1 cgd }
235 1.1 cgd ignoring = 1;
236 1.1 cgd } else {
237 1.1 cgd ignoring = 0;
238 1.1 cgd *cp2 = c; /* restore */
239 1.1 cgd }
240 1.1 cgd infld = 1;
241 1.1 cgd }
242 1.1 cgd }
243 1.1 cgd if (!ignoring) {
244 1.1 cgd /*
245 1.1 cgd * Strip trailing whitespace from prefix
246 1.1 cgd * if line is blank.
247 1.1 cgd */
248 1.16 wiz if (prefix != NULL) {
249 1.1 cgd if (length > 1)
250 1.23 christos (void)fputs(prefix, obuf);
251 1.1 cgd else
252 1.18 wiz (void)fwrite(prefix, sizeof *prefix,
253 1.1 cgd prefixlen, obuf);
254 1.9 ross }
255 1.18 wiz (void)fwrite(line, sizeof *line, length, obuf);
256 1.1 cgd if (ferror(obuf))
257 1.1 cgd return -1;
258 1.1 cgd }
259 1.1 cgd }
260 1.1 cgd /*
261 1.1 cgd * Copy out message body
262 1.1 cgd */
263 1.26 christos #ifdef MIME_SUPPORT
264 1.26 christos if (mip) {
265 1.26 christos obuf = mime_decode_body(mip);
266 1.26 christos if (obuf == NULL) /* XXX - early out */
267 1.26 christos return 0;
268 1.26 christos }
269 1.26 christos #endif
270 1.28 christos c = 0; /* This is needed if len == 0. It's used differently above. */
271 1.1 cgd if (doign == ignoreall)
272 1.15 wiz len--; /* skip final blank line */
273 1.28 christos
274 1.16 wiz if (prefix != NULL)
275 1.15 wiz while (len > 0) {
276 1.1 cgd if (fgets(line, LINESIZE, ibuf) == NULL) {
277 1.1 cgd c = 0;
278 1.1 cgd break;
279 1.1 cgd }
280 1.15 wiz len -= c = strlen(line);
281 1.1 cgd /*
282 1.1 cgd * Strip trailing whitespace from prefix
283 1.1 cgd * if line is blank.
284 1.1 cgd */
285 1.1 cgd if (c > 1)
286 1.23 christos (void)fputs(prefix, obuf);
287 1.1 cgd else
288 1.18 wiz (void)fwrite(prefix, sizeof *prefix,
289 1.1 cgd prefixlen, obuf);
290 1.23 christos (void)fwrite(line, sizeof *line, (size_t)c, obuf);
291 1.1 cgd if (ferror(obuf))
292 1.1 cgd return -1;
293 1.1 cgd }
294 1.1 cgd else
295 1.15 wiz while (len > 0) {
296 1.23 christos c = (int)(len < LINESIZE ? len : LINESIZE);
297 1.28 christos if ((c = fread(line, sizeof *line, (size_t)c, ibuf)) == 0)
298 1.1 cgd break;
299 1.15 wiz len -= c;
300 1.28 christos if (fwrite(line, sizeof *line, (size_t)c, obuf) != (size_t)c)
301 1.1 cgd return -1;
302 1.1 cgd }
303 1.1 cgd if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
304 1.1 cgd /* no final blank line */
305 1.1 cgd if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
306 1.26 christos return -1;
307 1.1 cgd return 0;
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd /*
311 1.28 christos * Fix the header by glopping all of the expanded names from
312 1.28 christos * the distribution list into the appropriate fields.
313 1.28 christos */
314 1.28 christos static void
315 1.28 christos fixhead(struct header *hp, struct name *tolist)
316 1.28 christos {
317 1.28 christos struct name *np;
318 1.28 christos
319 1.28 christos hp->h_to = NULL;
320 1.28 christos hp->h_cc = NULL;
321 1.28 christos hp->h_bcc = NULL;
322 1.28 christos for (np = tolist; np != NULL; np = np->n_flink) {
323 1.28 christos if (np->n_type & GDEL)
324 1.28 christos continue; /* Don't copy deleted addresses to the header */
325 1.28 christos if ((np->n_type & GMASK) == GTO)
326 1.28 christos hp->h_to =
327 1.28 christos cat(hp->h_to, nalloc(np->n_name, np->n_type));
328 1.28 christos else if ((np->n_type & GMASK) == GCC)
329 1.28 christos hp->h_cc =
330 1.28 christos cat(hp->h_cc, nalloc(np->n_name, np->n_type));
331 1.28 christos else if ((np->n_type & GMASK) == GBCC)
332 1.28 christos hp->h_bcc =
333 1.28 christos cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
334 1.28 christos }
335 1.28 christos }
336 1.28 christos
337 1.28 christos /*
338 1.28 christos * Format the given header line to not exceed 72 characters.
339 1.1 cgd */
340 1.28 christos static void
341 1.28 christos fmt(const char *str, struct name *np, FILE *fo, int comma)
342 1.1 cgd {
343 1.28 christos int col, len;
344 1.1 cgd
345 1.28 christos comma = comma ? 1 : 0;
346 1.28 christos col = strlen(str);
347 1.28 christos if (col)
348 1.28 christos (void)fputs(str, fo);
349 1.28 christos for (; np != NULL; np = np->n_flink) {
350 1.28 christos if (np->n_flink == NULL)
351 1.28 christos comma = 0;
352 1.28 christos len = strlen(np->n_name);
353 1.28 christos col++; /* for the space */
354 1.28 christos if (col + len + comma > 72 && col > 4) {
355 1.28 christos (void)fputs("\n ", fo);
356 1.28 christos col = 4;
357 1.28 christos } else
358 1.28 christos (void)putc(' ', fo);
359 1.28 christos (void)fputs(np->n_name, fo);
360 1.28 christos if (comma)
361 1.28 christos (void)putc(',', fo);
362 1.28 christos col += len + comma;
363 1.28 christos }
364 1.28 christos (void)putc('\n', fo);
365 1.1 cgd }
366 1.1 cgd
367 1.1 cgd /*
368 1.28 christos * Dump the to, subject, cc header on the
369 1.28 christos * passed file buffer.
370 1.1 cgd */
371 1.28 christos PUBLIC int
372 1.28 christos puthead(struct header *hp, FILE *fo, int w)
373 1.28 christos {
374 1.28 christos int gotcha;
375 1.28 christos
376 1.28 christos gotcha = 0;
377 1.28 christos if (hp->h_to != NULL && w & GTO)
378 1.28 christos fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
379 1.28 christos if (hp->h_subject != NULL && w & GSUBJECT)
380 1.28 christos (void)fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
381 1.28 christos if (hp->h_smopts != NULL && w & GSMOPTS)
382 1.28 christos (void)fprintf(fo, "(sendmail options: %s)\n", detract(hp->h_smopts, GSMOPTS)), gotcha++;
383 1.28 christos if (hp->h_cc != NULL && w & GCC)
384 1.28 christos fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
385 1.28 christos if (hp->h_bcc != NULL && w & GBCC)
386 1.28 christos fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
387 1.28 christos if (hp->h_in_reply_to != NULL && w & GMISC)
388 1.28 christos (void)fprintf(fo, "In-Reply-To: %s\n", hp->h_in_reply_to), gotcha++;
389 1.28 christos if (hp->h_references != NULL && w & GMISC)
390 1.28 christos fmt("References:", hp->h_references, fo, w&GCOMMA), gotcha++;
391 1.26 christos #ifdef MIME_SUPPORT
392 1.28 christos if (w & GMIME && (hp->h_attach || value(ENAME_MIME_ENCODE_MSG)))
393 1.28 christos mime_putheader(fo, hp), gotcha++;
394 1.26 christos #endif
395 1.28 christos if (gotcha && w & GNL)
396 1.28 christos (void)putc('\n', fo);
397 1.28 christos return 0;
398 1.28 christos }
399 1.28 christos
400 1.28 christos /*
401 1.28 christos * Prepend a header in front of the collected stuff
402 1.28 christos * and return the new file.
403 1.28 christos */
404 1.28 christos static FILE *
405 1.28 christos infix(struct header *hp, FILE *fi)
406 1.1 cgd {
407 1.28 christos FILE *nfo, *nfi;
408 1.28 christos int c, fd;
409 1.28 christos char tempname[PATHSIZE];
410 1.1 cgd
411 1.28 christos (void)snprintf(tempname, sizeof(tempname),
412 1.28 christos "%s/mail.RsXXXXXXXXXX", tmpdir);
413 1.28 christos if ((fd = mkstemp(tempname)) == -1 ||
414 1.28 christos (nfo = Fdopen(fd, "w")) == NULL) {
415 1.28 christos if (fd != -1)
416 1.28 christos (void)close(fd);
417 1.28 christos warn("%s", tempname);
418 1.28 christos return fi;
419 1.28 christos }
420 1.28 christos if ((nfi = Fopen(tempname, "r")) == NULL) {
421 1.28 christos warn("%s", tempname);
422 1.28 christos (void)Fclose(nfo);
423 1.28 christos (void)rm(tempname);
424 1.28 christos return fi;
425 1.28 christos }
426 1.28 christos (void)rm(tempname);
427 1.26 christos #ifdef MIME_SUPPORT
428 1.28 christos (void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GMISC|GMIME|GNL|GCOMMA);
429 1.28 christos #else
430 1.28 christos (void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GMISC|GNL|GCOMMA);
431 1.26 christos #endif
432 1.28 christos
433 1.28 christos c = getc(fi);
434 1.28 christos while (c != EOF) {
435 1.28 christos (void)putc(c, nfo);
436 1.28 christos c = getc(fi);
437 1.28 christos }
438 1.28 christos if (ferror(fi)) {
439 1.28 christos warn("read");
440 1.28 christos rewind(fi);
441 1.28 christos return fi;
442 1.28 christos }
443 1.28 christos (void)fflush(nfo);
444 1.28 christos if (ferror(nfo)) {
445 1.28 christos warn("%s", tempname);
446 1.28 christos (void)Fclose(nfo);
447 1.28 christos (void)Fclose(nfi);
448 1.28 christos rewind(fi);
449 1.28 christos return fi;
450 1.28 christos }
451 1.28 christos (void)Fclose(nfo);
452 1.28 christos (void)Fclose(fi);
453 1.28 christos rewind(nfi);
454 1.28 christos return nfi;
455 1.1 cgd }
456 1.1 cgd
457 1.1 cgd /*
458 1.28 christos * Save the outgoing mail on the passed file.
459 1.1 cgd */
460 1.28 christos /*ARGSUSED*/
461 1.28 christos static int
462 1.28 christos savemail(const char name[], FILE *fi)
463 1.1 cgd {
464 1.28 christos FILE *fo;
465 1.28 christos char buf[LINESIZE];
466 1.28 christos int i;
467 1.28 christos time_t now;
468 1.28 christos mode_t m;
469 1.1 cgd
470 1.28 christos m = umask(077);
471 1.28 christos fo = Fopen(name, "a");
472 1.28 christos (void)umask(m);
473 1.28 christos if (fo == NULL) {
474 1.28 christos warn("%s", name);
475 1.28 christos return -1;
476 1.28 christos }
477 1.28 christos (void)time(&now);
478 1.28 christos (void)fprintf(fo, "From %s %s", myname, ctime(&now));
479 1.28 christos while ((i = fread(buf, 1, sizeof buf, fi)) > 0)
480 1.28 christos (void)fwrite(buf, 1, (size_t)i, fo);
481 1.28 christos (void)putc('\n', fo);
482 1.28 christos (void)fflush(fo);
483 1.28 christos if (ferror(fo))
484 1.28 christos warn("%s", name);
485 1.28 christos (void)Fclose(fo);
486 1.28 christos rewind(fi);
487 1.28 christos return 0;
488 1.1 cgd }
489 1.1 cgd
490 1.1 cgd /*
491 1.1 cgd * Mail a message on standard input to the people indicated
492 1.1 cgd * in the passed header. (Internal interface).
493 1.1 cgd */
494 1.28 christos PUBLIC void
495 1.14 wiz mail1(struct header *hp, int printheaders)
496 1.1 cgd {
497 1.22 christos const char *cp;
498 1.1 cgd int pid;
499 1.22 christos const char **namelist;
500 1.1 cgd struct name *to;
501 1.1 cgd FILE *mtf;
502 1.1 cgd
503 1.1 cgd /*
504 1.1 cgd * Collect user's mail from standard input.
505 1.1 cgd * Get the result as mtf.
506 1.1 cgd */
507 1.1 cgd if ((mtf = collect(hp, printheaders)) == NULL)
508 1.1 cgd return;
509 1.28 christos if (value(ENAME_INTERACTIVE) != NULL) {
510 1.28 christos if (value(ENAME_ASKCC) != NULL || value(ENAME_ASKBCC) != NULL) {
511 1.28 christos if (value(ENAME_ASKCC) != NULL)
512 1.23 christos (void)grabh(hp, GCC);
513 1.28 christos if (value(ENAME_ASKBCC) != NULL)
514 1.23 christos (void)grabh(hp, GBCC);
515 1.3 jtc } else {
516 1.23 christos (void)printf("EOT\n");
517 1.18 wiz (void)fflush(stdout);
518 1.1 cgd }
519 1.9 ross }
520 1.9 ross if (fsize(mtf) == 0) {
521 1.28 christos if (value(ENAME_DONTSENDEMPTY) != NULL)
522 1.12 christos goto out;
523 1.16 wiz if (hp->h_subject == NULL)
524 1.23 christos (void)printf("No message, no subject; hope that's ok\n");
525 1.1 cgd else
526 1.23 christos (void)printf("Null message body; hope that's ok\n");
527 1.9 ross }
528 1.1 cgd /*
529 1.1 cgd * Now, take the user names from the combined
530 1.1 cgd * to and cc lists and do all the alias
531 1.1 cgd * processing.
532 1.1 cgd */
533 1.1 cgd senderr = 0;
534 1.1 cgd to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
535 1.17 wiz if (to == NULL) {
536 1.23 christos (void)printf("No recipients specified\n");
537 1.1 cgd senderr++;
538 1.1 cgd }
539 1.26 christos #ifdef MIME_SUPPORT
540 1.26 christos /*
541 1.26 christos * If there are attachments, repackage the mail as a
542 1.26 christos * multi-part MIME message.
543 1.26 christos */
544 1.26 christos if (hp->h_attach || value(ENAME_MIME_ENCODE_MSG))
545 1.26 christos mtf = mime_encode(mtf, hp);
546 1.26 christos #endif
547 1.1 cgd /*
548 1.1 cgd * Look through the recipient list for names with /'s
549 1.1 cgd * in them which we write to as files directly.
550 1.1 cgd */
551 1.1 cgd to = outof(to, mtf, hp);
552 1.1 cgd if (senderr)
553 1.1 cgd savedeadletter(mtf);
554 1.1 cgd to = elide(to);
555 1.1 cgd if (count(to) == 0)
556 1.1 cgd goto out;
557 1.1 cgd fixhead(hp, to);
558 1.1 cgd if ((mtf = infix(hp, mtf)) == NULL) {
559 1.23 christos (void)fprintf(stderr, ". . . message lost, sorry.\n");
560 1.1 cgd return;
561 1.1 cgd }
562 1.27 christos if (hp->h_smopts == NULL) {
563 1.27 christos hp->h_smopts = get_smopts(to);
564 1.27 christos if (hp->h_smopts != NULL &&
565 1.27 christos hp->h_smopts->n_name[0] != '\0' &&
566 1.27 christos value(ENAME_SMOPTS_VERIFY) != NULL)
567 1.27 christos if (grabh(hp, GSMOPTS)) {
568 1.27 christos (void)printf("mail aborted!\n");
569 1.27 christos savedeadletter(mtf);
570 1.27 christos goto out;
571 1.27 christos }
572 1.27 christos }
573 1.1 cgd namelist = unpack(cat(hp->h_smopts, to));
574 1.1 cgd if (debug) {
575 1.22 christos const char **t;
576 1.1 cgd
577 1.23 christos (void)printf("Sendmail arguments:");
578 1.16 wiz for (t = namelist; *t != NULL; t++)
579 1.23 christos (void)printf(" \"%s\"", *t);
580 1.23 christos (void)printf("\n");
581 1.1 cgd goto out;
582 1.1 cgd }
583 1.27 christos if ((cp = value(ENAME_RECORD)) != NULL)
584 1.27 christos (void)savemail(expand(cp), mtf);
585 1.1 cgd /*
586 1.1 cgd * Fork, set up the temporary mail file as standard
587 1.1 cgd * input for "mail", and exec with the user list we generated
588 1.1 cgd * far above.
589 1.1 cgd */
590 1.1 cgd pid = fork();
591 1.1 cgd if (pid == -1) {
592 1.19 wiz warn("fork");
593 1.1 cgd savedeadletter(mtf);
594 1.1 cgd goto out;
595 1.1 cgd }
596 1.1 cgd if (pid == 0) {
597 1.6 christos sigset_t nset;
598 1.23 christos (void)sigemptyset(&nset);
599 1.23 christos (void)sigaddset(&nset, SIGHUP);
600 1.23 christos (void)sigaddset(&nset, SIGINT);
601 1.23 christos (void)sigaddset(&nset, SIGQUIT);
602 1.23 christos (void)sigaddset(&nset, SIGTSTP);
603 1.23 christos (void)sigaddset(&nset, SIGTTIN);
604 1.23 christos (void)sigaddset(&nset, SIGTTOU);
605 1.6 christos prepare_child(&nset, fileno(mtf), -1);
606 1.28 christos if ((cp = value(ENAME_SENDMAIL)) != NULL)
607 1.1 cgd cp = expand(cp);
608 1.1 cgd else
609 1.1 cgd cp = _PATH_SENDMAIL;
610 1.23 christos (void)execv(cp, (char *const *)__UNCONST(namelist));
611 1.19 wiz warn("%s", cp);
612 1.1 cgd _exit(1);
613 1.1 cgd }
614 1.28 christos if (value(ENAME_VERBOSE) != NULL)
615 1.18 wiz (void)wait_child(pid);
616 1.1 cgd else
617 1.1 cgd free_child(pid);
618 1.1 cgd out:
619 1.18 wiz (void)Fclose(mtf);
620 1.1 cgd }
621 1.1 cgd
622 1.1 cgd /*
623 1.28 christos * Interface between the argument list and the mail1 routine
624 1.28 christos * which does all the dirty work.
625 1.1 cgd */
626 1.28 christos PUBLIC int
627 1.28 christos mail(struct name *to, struct name *cc, struct name *bcc,
628 1.28 christos struct name *smopts, char *subject, struct attachment *attach)
629 1.1 cgd {
630 1.28 christos struct header head;
631 1.1 cgd
632 1.28 christos /* ensure that all header fields are initially NULL */
633 1.28 christos (void)memset(&head, 0, sizeof(head));
634 1.1 cgd
635 1.28 christos head.h_to = to;
636 1.28 christos head.h_subject = subject;
637 1.28 christos head.h_cc = cc;
638 1.28 christos head.h_bcc = bcc;
639 1.28 christos head.h_smopts = smopts;
640 1.26 christos #ifdef MIME_SUPPORT
641 1.28 christos head.h_attach = attach;
642 1.26 christos #endif
643 1.28 christos mail1(&head, 0);
644 1.28 christos return 0;
645 1.1 cgd }
646 1.1 cgd
647 1.1 cgd /*
648 1.28 christos * Send mail to a bunch of user names. The interface is through
649 1.28 christos * the mail1 routine below.
650 1.1 cgd */
651 1.28 christos PUBLIC int
652 1.28 christos sendmail(void *v)
653 1.1 cgd {
654 1.28 christos char *str = v;
655 1.28 christos struct header head;
656 1.1 cgd
657 1.28 christos /* ensure that all header fields are initially NULL */
658 1.28 christos (void)memset(&head, 0, sizeof(head));
659 1.1 cgd
660 1.28 christos head.h_to = extract(str, GTO);
661 1.1 cgd
662 1.28 christos mail1(&head, 0);
663 1.28 christos return 0;
664 1.1 cgd }
665