]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
531518d8dd54efe8466a410534b28806f93938f5
[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 "sbr/fmt_new.h"
10 #include "distsbr.h"
11 #include "sbr/m_gmprot.h"
12 #include "sbr/m_getfld.h"
13 #include "sbr/getarguments.h"
14 #include "sbr/smatch.h"
15 #include "sbr/cpydata.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
18 #include "sbr/pidstatus.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/arglist.h"
22 #include "sbr/error.h"
23 #include "h/fmt_scan.h"
24 #include "h/tws.h"
25 #include "h/mts.h"
26 #include "h/done.h"
27 #include "h/utils.h"
28 #include "sbr/m_mktemp.h"
29
30 #define RCVDIST_SWITCHES \
31 X("form formfile", 4, FORMSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(RCVDIST);
37 #undef X
38
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(RCVDIST, switches);
41 #undef X
42
43 #ifndef CYRUS_SASL
44 # define SASLminc(a) (a)
45 #else /* CYRUS_SASL */
46 # define SASLminc(a) 0
47 #endif /* CYRUS_SASL */
48
49 #ifndef OAUTH_SUPPORT
50 # define OAUTHminc(a) (a)
51 #else /* OAUTH_SUPPORT */
52 # define OAUTHminc(a) 0
53 #endif /* OAUTH_SUPPORT */
54
55 /* These are just the post(1) switches that take an argument. */
56 #define POST_SWITCHES \
57 X("alias aliasfile", 0, ALIASW) \
58 X("filter filterfile", 0, FILTSW) \
59 X("library directory", -7, LIBSW) /* interface from send, whom */ \
60 X("width columns", 0, WIDTHSW) \
61 X("idanno number", -6, ANNOSW) /* interface from send */ \
62 X("client host", -6, CLIESW) \
63 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
64 X("partno", -6, PARTSW) \
65 X("saslmech", SASLminc(5), SASLMECHSW) \
66 X("user", SASLminc(-4), USERSW) \
67 X("port server submission port name/number", 4, PORTSW) \
68 X("fileproc", -4, FILEPROCSW) \
69 X("mhlproc", -3, MHLPROCSW) \
70 X("sendmail program", 0, MTSSM) \
71 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
72 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
73 X("messageid localname|random", 2, MESSAGEIDSW) \
74 X("authservice auth-service-name", OAUTHminc(-11), AUTHSERVICESW) \
75 X("oauthcredfile credential-file", OAUTHminc(-7), OAUTHCREDFILESW) \
76 X("oauthclientid client-id", OAUTHminc(-12), OAUTHCLIDSW) \
77 X("oauthclientsecret client-secret", OAUTHminc(-12), OAUTHCLSECSW) \
78 X("oauthauthendpoint authentication-endpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \
79 X("oauthredirect redirect-uri", OAUTHminc(-6), OAUTHREDIRSW) \
80 X("oauthtokenendpoint token-endpoint", OAUTHminc(-6), OAUTHTOKENDSW) \
81 X("oauthscope scope", OAUTHminc(-6), OAUTHSCOPESW) \
82
83 #define X(sw, minchars, id) id,
84 DEFINE_SWITCH_ENUM(POST);
85 #undef X
86
87 #define X(sw, minchars, id) { sw, minchars, id },
88 DEFINE_SWITCH_ARRAY(POST, post_switches);
89 #undef X
90
91 static char backup[BUFSIZ] = "";
92 static char drft[BUFSIZ] = "";
93 static char tmpfil[BUFSIZ] = "";
94
95 /*
96 * prototypes
97 */
98 static void rcvdistout (FILE *, char *, char *);
99 static void unlink_done (int) NORETURN;
100
101
102 int
103 main (int argc, char **argv)
104 {
105 pid_t child_id;
106 int vecp;
107 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ], *program;
108 char **argp, **arguments, **vec;
109 FILE *fp;
110 char *tfile = NULL;
111
112 if (nmh_init(argv[0], true, false)) { return 1; }
113
114 set_done(unlink_done);
115
116 /*
117 * Configure this now, since any unknown switches to rcvdist get
118 * sent to postproc
119 */
120
121 vec = argsplit(postproc, &program, &vecp);
122
123 mts_init ();
124 arguments = getarguments (invo_name, argc, argv, 1);
125 argp = arguments;
126
127 while ((cp = *argp++)) {
128 if (*cp == '-') {
129 switch (smatch (++cp, switches)) {
130 case AMBIGSW:
131 ambigsw (cp, switches);
132 done (1);
133 case UNKWNSW:
134 vec[vecp++] = --cp;
135 if (smatch(cp + 1, post_switches) != UNKWNSW) {
136 /* It's a post switch that does take an argument. */
137 if (!(cp = *argp) || *cp == '-') {
138 die("missing argument to %s", argp[-1]);
139 }
140 vec[vecp++] = *argp++;
141 }
142 continue;
143
144 case HELPSW:
145 snprintf (buf, sizeof(buf),
146 "%s [switches] [switches for postproc] address ...",
147 invo_name);
148 print_help (buf, switches, 1);
149 done (0);
150 case VERSIONSW:
151 print_version(invo_name);
152 done (0);
153
154 case FORMSW:
155 if (!(form = *argp++) || *form == '-')
156 die("missing argument to %s", argp[-2]);
157 continue;
158 }
159 }
160 addrs = addrs ? add (cp, add (", ", addrs)) : mh_xstrdup(cp);
161 }
162
163 if (addrs == NULL)
164 die("usage: %s [switches] [switches for postproc] address ...",
165 invo_name);
166
167 umask (~m_gmprot ());
168
169 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
170 die("unable to create temporary file in %s", get_temp_dir());
171 }
172 strncpy (tmpfil, tfile, sizeof(tmpfil));
173
174 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
175 fseek (fp, 0L, SEEK_SET);
176
177 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
178 die("unable to create temporary file in %s", get_temp_dir());
179 }
180 strncpy (drft, tfile, sizeof(tmpfil));
181
182 rcvdistout (fp, form, addrs);
183 fclose (fp);
184
185 if (distout (drft, tmpfil, backup) == NOTOK)
186 done (1);
187
188 vec[vecp++] = "-dist";
189 if ((cp = context_find ("mhlproc"))) {
190 vec[vecp++] = "-mhlproc";
191 vec[vecp++] = cp;
192 }
193 vec[vecp++] = drft;
194 vec[vecp] = NULL;
195
196 child_id = fork();
197 switch (child_id) {
198 case NOTOK:
199 adios("fork", "failed:");
200
201 case OK:
202 execvp (program, vec);
203 fprintf (stderr, "unable to exec ");
204 perror (postproc);
205 _exit (1);
206
207 default:
208 done (pidXwait(child_id, postproc));
209 }
210
211 return 0; /* dead code to satisfy the compiler */
212 }
213
214 /* very similar to routine in replsbr.c */
215
216 static int outputlinelen = OUTPUTLINELEN;
217
218 static struct format *fmt;
219
220 static int dat[5];
221
222 static char *addrcomps[] = {
223 "from",
224 "sender",
225 "reply-to",
226 "to",
227 "cc",
228 "bcc",
229 "resent-from",
230 "resent-sender",
231 "resent-reply-to",
232 "resent-to",
233 "resent-cc",
234 "resent-bcc",
235 NULL
236 };
237
238
239 static void
240 rcvdistout (FILE *inb, char *form, char *addrs)
241 {
242 int char_read = 0, format_len, i, state;
243 char **ap;
244 char *cp, name[NAMESZ], tmpbuf[NMH_BUFSIZ];
245 charstring_t scanl;
246 struct comp *cptr;
247 FILE *out;
248 m_getfld_state_t gstate;
249
250 if (!(out = fopen (drft, "w")))
251 adios (drft, "unable to create");
252
253 /* get new format string */
254 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
255 format_len = strlen (cp);
256 fmt_compile (cp, &fmt, 1);
257
258 for (ap = addrcomps; *ap; ap++) {
259 cptr = fmt_findcomp (*ap);
260 if (cptr)
261 cptr->c_type |= CT_ADDR;
262 }
263
264 cptr = fmt_findcomp ("addresses");
265 if (cptr)
266 cptr->c_text = addrs;
267
268 gstate = m_getfld_state_init(inb);
269 for (;;) {
270 int msg_count = sizeof tmpbuf;
271 switch (state = m_getfld2(&gstate, name, tmpbuf, &msg_count)) {
272 case FLD:
273 case FLDPLUS:
274 i = fmt_addcomptext(name, tmpbuf);
275 if (i != -1) {
276 char_read += msg_count;
277 while (state == FLDPLUS) {
278 msg_count = sizeof tmpbuf;
279 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
280 fmt_appendcomp(i, name, tmpbuf);
281 char_read += msg_count;
282 }
283 }
284
285 while (state == FLDPLUS) {
286 msg_count = sizeof tmpbuf;
287 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
288 }
289 break;
290
291 case LENERR:
292 case FMTERR:
293 case BODY:
294 case FILEEOF:
295 goto finished;
296
297 default:
298 die("m_getfld2() returned %d", state);
299 }
300 }
301 finished: ;
302 m_getfld_state_destroy (&gstate);
303
304 i = format_len + char_read + 256;
305 scanl = charstring_create (i + 2);
306 dat[0] = dat[1] = dat[2] = dat[4] = 0;
307 dat[3] = outputlinelen;
308 fmt_scan (fmt, scanl, i, dat, NULL);
309 fputs (charstring_buffer (scanl), out);
310
311 if (ferror (out))
312 adios (drft, "error writing");
313 fclose (out);
314
315 charstring_free (scanl);
316 fmt_free(fmt, 1);
317 }
318
319
320 static void NORETURN
321 unlink_done (int status)
322 {
323 if (backup[0])
324 (void) m_unlink (backup);
325 if (drft[0])
326 (void) m_unlink (drft);
327 if (tmpfil[0])
328 (void) m_unlink (tmpfil);
329
330 exit(status ? 1 : 0);
331 }