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