]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / rcvdist.c
1 /* rcvdist.c -- asynchronously redistribute messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/fmt_scan.h>
10 #include <h/rcvmail.h>
11 #include <h/tws.h>
12 #include <h/mts.h>
13 #include <h/utils.h>
14 #include "sbr/m_mktemp.h"
15
16 #define RCVDIST_SWITCHES \
17 X("form formfile", 4, FORMSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
20
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(RCVDIST);
23 #undef X
24
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(RCVDIST, switches);
27 #undef X
28
29 static char backup[BUFSIZ] = "";
30 static char drft[BUFSIZ] = "";
31 static char tmpfil[BUFSIZ] = "";
32
33 /*
34 * prototypes
35 */
36 static void rcvdistout (FILE *, char *, char *);
37 static void unlink_done (int) NORETURN;
38
39
40 int
41 main (int argc, char **argv)
42 {
43 pid_t child_id;
44 int vecp;
45 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ], *program;
46 char **argp, **arguments, **vec;
47 FILE *fp;
48 char *tfile = NULL;
49
50 if (nmh_init(argv[0], 2)) { return 1; }
51
52 done=unlink_done;
53
54 /*
55 * Configure this now, since any unknown switches to rcvdist get
56 * sent to postproc
57 */
58
59 vec = argsplit(postproc, &program, &vecp);
60
61 mts_init ();
62 arguments = getarguments (invo_name, argc, argv, 1);
63 argp = arguments;
64
65 while ((cp = *argp++)) {
66 if (*cp == '-') {
67 switch (smatch (++cp, switches)) {
68 case AMBIGSW:
69 ambigsw (cp, switches);
70 done (1);
71 case UNKWNSW:
72 vec[vecp++] = --cp;
73 continue;
74
75 case HELPSW:
76 snprintf (buf, sizeof(buf),
77 "%s [switches] [switches for postproc] address ...",
78 invo_name);
79 print_help (buf, switches, 1);
80 done (0);
81 case VERSIONSW:
82 print_version(invo_name);
83 done (0);
84
85 case FORMSW:
86 if (!(form = *argp++) || *form == '-')
87 adios (NULL, "missing argument to %s", argp[-2]);
88 continue;
89 }
90 }
91 addrs = addrs ? add (cp, add (", ", addrs)) : mh_xstrdup(cp);
92 }
93
94 if (addrs == NULL)
95 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
96 invo_name);
97
98 umask (~m_gmprot ());
99
100 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
101 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
102 }
103 strncpy (tmpfil, tfile, sizeof(tmpfil));
104
105 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
106 fseek (fp, 0L, SEEK_SET);
107
108 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
109 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
110 }
111 strncpy (drft, tfile, sizeof(tmpfil));
112
113 rcvdistout (fp, form, addrs);
114 fclose (fp);
115
116 if (distout (drft, tmpfil, backup) == NOTOK)
117 done (1);
118
119 vec[vecp++] = "-dist";
120 if ((cp = context_find ("mhlproc"))) {
121 vec[vecp++] = "-mhlproc";
122 vec[vecp++] = cp;
123 }
124 vec[vecp++] = drft;
125 vec[vecp] = NULL;
126
127 child_id = fork();
128 switch (child_id) {
129 case NOTOK:
130 inform("unable to fork, continuing...");
131 /* FALLTHRU */
132 case OK:
133 execvp (program, vec);
134 fprintf (stderr, "unable to exec ");
135 perror (postproc);
136 _exit (1);
137
138 default:
139 done (pidXwait(child_id, postproc));
140 }
141
142 return 0; /* dead code to satisfy the compiler */
143 }
144
145 /* very similar to routine in replsbr.c */
146
147 static int outputlinelen = OUTPUTLINELEN;
148
149 static struct format *fmt;
150
151 static int dat[5];
152
153 static char *addrcomps[] = {
154 "from",
155 "sender",
156 "reply-to",
157 "to",
158 "cc",
159 "bcc",
160 "resent-from",
161 "resent-sender",
162 "resent-reply-to",
163 "resent-to",
164 "resent-cc",
165 "resent-bcc",
166 NULL
167 };
168
169
170 static void
171 rcvdistout (FILE *inb, char *form, char *addrs)
172 {
173 int char_read = 0, format_len, i, state;
174 char **ap;
175 char *cp, name[NAMESZ], tmpbuf[NMH_BUFSIZ];
176 charstring_t scanl;
177 struct comp *cptr;
178 FILE *out;
179 m_getfld_state_t gstate;
180
181 if (!(out = fopen (drft, "w")))
182 adios (drft, "unable to create");
183
184 /* get new format string */
185 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
186 format_len = strlen (cp);
187 fmt_compile (cp, &fmt, 1);
188
189 for (ap = addrcomps; *ap; ap++) {
190 cptr = fmt_findcomp (*ap);
191 if (cptr)
192 cptr->c_type |= CT_ADDR;
193 }
194
195 cptr = fmt_findcomp ("addresses");
196 if (cptr)
197 cptr->c_text = addrs;
198
199 gstate = m_getfld_state_init(inb);
200 for (;;) {
201 int msg_count = sizeof tmpbuf;
202 switch (state = m_getfld2(&gstate, name, tmpbuf, &msg_count)) {
203 case FLD:
204 case FLDPLUS:
205 i = fmt_addcomptext(name, tmpbuf);
206 if (i != -1) {
207 char_read += msg_count;
208 while (state == FLDPLUS) {
209 msg_count = sizeof tmpbuf;
210 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
211 fmt_appendcomp(i, name, tmpbuf);
212 char_read += msg_count;
213 }
214 }
215
216 while (state == FLDPLUS) {
217 msg_count = sizeof tmpbuf;
218 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
219 }
220 break;
221
222 case LENERR:
223 case FMTERR:
224 case BODY:
225 case FILEEOF:
226 goto finished;
227
228 default:
229 adios (NULL, "m_getfld2() returned %d", state);
230 }
231 }
232 finished: ;
233 m_getfld_state_destroy (&gstate);
234
235 i = format_len + char_read + 256;
236 scanl = charstring_create (i + 2);
237 dat[0] = dat[1] = dat[2] = dat[4] = 0;
238 dat[3] = outputlinelen;
239 fmt_scan (fmt, scanl, i, dat, NULL);
240 fputs (charstring_buffer (scanl), out);
241
242 if (ferror (out))
243 adios (drft, "error writing");
244 fclose (out);
245
246 charstring_free (scanl);
247 fmt_free(fmt, 1);
248 }
249
250
251 static void NORETURN
252 unlink_done (int status)
253 {
254 if (backup[0])
255 (void) m_unlink (backup);
256 if (drft[0])
257 (void) m_unlink (drft);
258 if (tmpfil[0])
259 (void) m_unlink (tmpfil);
260
261 exit (status ? RCV_MBX : RCV_MOK);
262 }