]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
decode_rfc2047(): Stop unused-variable warning if HAVE_ICONV false.
[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_with_args);
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 const int argno = smatch(cp, post_switches_with_args);
135 vec[vecp++] = --cp;
136 if (argno != AMBIGSW && argno != UNKWNSW) {
137 /* It's a post switch that does take an argument. */
138 if (!(cp = *argp) || *cp == '-') {
139 die("missing argument to %s", argp[-1]);
140 }
141 vec[vecp++] = cp;
142 ++argp;
143 }
144 continue;
145 }
146 case HELPSW:
147 snprintf (buf, sizeof(buf),
148 "%s [switches] [switches for postproc] address ...",
149 invo_name);
150 print_help (buf, switches, 1);
151 done (0);
152 case VERSIONSW:
153 print_version(invo_name);
154 done (0);
155
156 case FORMSW:
157 if (!(form = *argp++) || *form == '-')
158 die("missing argument to %s", argp[-2]);
159 continue;
160 }
161 }
162 addrs = addrs ? add (cp, add (", ", addrs)) : mh_xstrdup(cp);
163 }
164
165 if (addrs == NULL)
166 die("usage: %s [switches] [switches for postproc] address ...",
167 invo_name);
168
169 umask (~m_gmprot ());
170
171 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
172 die("unable to create temporary file in %s", get_temp_dir());
173 }
174 strncpy (tmpfil, tfile, sizeof(tmpfil));
175
176 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
177 fseek (fp, 0L, SEEK_SET);
178
179 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
180 die("unable to create temporary file in %s", get_temp_dir());
181 }
182 strncpy (drft, tfile, sizeof(tmpfil));
183
184 rcvdistout (fp, form, addrs);
185 fclose (fp);
186
187 if (distout (drft, tmpfil, backup) == NOTOK)
188 done (1);
189
190 vec[vecp++] = "-dist";
191 if ((cp = context_find ("mhlproc"))) {
192 vec[vecp++] = "-mhlproc";
193 vec[vecp++] = cp;
194 }
195 vec[vecp++] = drft;
196 vec[vecp] = NULL;
197
198 child_id = fork();
199 switch (child_id) {
200 case NOTOK:
201 adios("fork", "failed:");
202
203 case OK:
204 execvp (program, vec);
205 fprintf (stderr, "unable to exec ");
206 perror (postproc);
207 _exit (1);
208
209 default:
210 done (pidXwait(child_id, postproc));
211 }
212
213 return 0; /* dead code to satisfy the compiler */
214 }
215
216 /* very similar to routine in replsbr.c */
217
218 static int outputlinelen = OUTPUTLINELEN;
219
220 static struct format *fmt;
221
222 static int dat[5];
223
224 static char *addrcomps[] = {
225 "from",
226 "sender",
227 "reply-to",
228 "to",
229 "cc",
230 "bcc",
231 "resent-from",
232 "resent-sender",
233 "resent-reply-to",
234 "resent-to",
235 "resent-cc",
236 "resent-bcc",
237 NULL
238 };
239
240
241 static void
242 rcvdistout (FILE *inb, char *form, char *addrs)
243 {
244 int char_read = 0, format_len, i, state;
245 char **ap;
246 char *cp, name[NAMESZ], tmpbuf[NMH_BUFSIZ];
247 charstring_t scanl;
248 struct comp *cptr;
249 FILE *out;
250 m_getfld_state_t gstate;
251
252 if (!(out = fopen (drft, "w")))
253 adios (drft, "unable to create");
254
255 /* get new format string */
256 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
257 format_len = strlen (cp);
258 fmt_compile (cp, &fmt, 1);
259
260 for (ap = addrcomps; *ap; ap++) {
261 cptr = fmt_findcomp (*ap);
262 if (cptr)
263 cptr->c_type |= CT_ADDR;
264 }
265
266 cptr = fmt_findcomp ("addresses");
267 if (cptr)
268 cptr->c_text = addrs;
269
270 gstate = m_getfld_state_init(inb);
271 for (;;) {
272 int msg_count = sizeof tmpbuf;
273 switch (state = m_getfld2(&gstate, name, tmpbuf, &msg_count)) {
274 case FLD:
275 case FLDPLUS:
276 i = fmt_addcomptext(name, tmpbuf);
277 if (i != -1) {
278 char_read += msg_count;
279 while (state == FLDPLUS) {
280 msg_count = sizeof tmpbuf;
281 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
282 fmt_appendcomp(i, name, tmpbuf);
283 char_read += msg_count;
284 }
285 }
286
287 while (state == FLDPLUS) {
288 msg_count = sizeof tmpbuf;
289 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
290 }
291 break;
292
293 case LENERR:
294 case FMTERR:
295 case BODY:
296 case FILEEOF:
297 goto finished;
298
299 default:
300 die("m_getfld2() returned %d", state);
301 }
302 }
303 finished: ;
304 m_getfld_state_destroy (&gstate);
305
306 i = format_len + char_read + 256;
307 scanl = charstring_create (i + 2);
308 dat[0] = dat[1] = dat[2] = dat[4] = 0;
309 dat[3] = outputlinelen;
310 fmt_scan (fmt, scanl, i, dat, NULL);
311 fputs (charstring_buffer (scanl), out);
312
313 if (ferror (out))
314 adios (drft, "error writing");
315 fclose (out);
316
317 charstring_free (scanl);
318 fmt_free(fmt, 1);
319 }
320
321
322 static void NORETURN
323 unlink_done (int status)
324 {
325 if (backup[0])
326 (void) m_unlink (backup);
327 if (drft[0])
328 (void) m_unlink (drft);
329 if (tmpfil[0])
330 (void) m_unlink (tmpfil);
331
332 exit(status ? 1 : 0);
333 }