/
ezmlm-store.c
385 lines (340 loc) · 11.4 KB
/
ezmlm-store.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "stralloc.h"
#include "subfd.h"
#include "strerr.h"
#include "error.h"
#include "qmail.h"
#include "env.h"
#include "sender.h"
#include "lock.h"
#include "sig.h"
#include "open.h"
#include "getln.h"
#include "str.h"
#include "fmt.h"
#include "readwrite.h"
#include "wait.h"
#include "exit.h"
#include "substdio.h"
#include "getconf.h"
#include "datetime.h"
#include "now.h"
#include "cookie.h"
#include "getconfopt.h"
#include "messages.h"
#include "byte.h"
#include "case.h"
#include "quote.h"
#include "hdr.h"
#include "die.h"
#include "idx.h"
#include "copy.h"
#include "subdb.h"
#include "mime.h"
#include "wrap.h"
#include "config.h"
#include "auto_version.h"
int flagmime = MOD_MIME; /* default is message as attachment */
int flagmodpostonly = -1; /* default anyone can post */
/* =1 for only moderators can */
int flagself = 0; /* `modpost` mods approve own posts */
/* but mod/ is used for moderators */
/* of other posts. Def=no=0 */
int flagconfirm = -1; /* if true, sender must approve its own posts */
int flagbody = 1; /* body of message enclosed with mod request */
/* 0 => headers only */
const char FATAL[] = "ezmlm-store: fatal: ";
const char USAGE[] =
"ezmlm-store: usage: ezmlm-store [-cCmMpPrRsSvV] dir";
static stralloc sendopt = {0};
static struct option options[] = {
OPT_FLAG(flagbody,'b',1,0),
OPT_FLAG(flagbody,'B',0,0),
OPT_FLAG(flagmime,'m',1,0),
OPT_FLAG(flagmime,'M',0,0),
OPT_FLAG(flagmodpostonly,'p',0,0), /* anyone can post (still mod'd) */
OPT_FLAG(flagmodpostonly,'P',1,"modpostonly"), /* only moderators can post */
OPT_FLAG(flagself,'s',1,0), /* modpost and DIR/mod diff fxns */
OPT_FLAG(flagself,'S',0,0), /* same fxn */
OPT_FLAG(flagconfirm,'y',1,"confirmpost"), /* force post confirmation */
OPT_FLAG(flagconfirm,'Y',0,0), /* disable post confirmation */
OPT_COPY_FLAG(sendopt,'c'),
OPT_COPY_FLAG(sendopt,'C'),
OPT_COPY_FLAG(sendopt,'r'),
OPT_COPY_FLAG(sendopt,'R'),
OPT_END
};
stralloc fnmsg = {0};
void die_msg(void) { strerr_die2sys(111,FATAL,MSG1(ERR_WRITE,fnmsg.s)); }
int fdmsg;
int fdmod;
int pid;
int match;
char strnum[FMT_ULONG];
char hash[COOKIE];
char boundary[COOKIE];
datetime_sec when;
struct stat st;
stralloc fnbase = {0};
stralloc line = {0};
stralloc mydtline = {0};
stralloc returnpath = {0};
stralloc accept = {0};
stralloc action = {0};
stralloc reject = {0};
stralloc quoted = {0};
stralloc moderators = {0};
struct qmail qq;
int subto(const char *s,unsigned int l)
{
qmail_put(&qq,"T",1);
qmail_put(&qq,s,l);
qmail_put(&qq,"",1);
return (int) l;
}
substdio ssin;
char inbuf[1024];
substdio ssmsg;
char msgbuf[1024];
substdio sstext;
char textbuf[512];
substdio sssub;
char subbuf[512];
void makeacthash(stralloc *act)
/* act is expected to be -reject-ddddd.ttttt or -accept-ddddd.ttttt and
* has to be 0-terminated. */
/* The routine will add .hash@outhost to act. act will NOT be 0-terminated */
{
int d;
d = 2 + str_chr(act->s + 1,'-');
cookie(hash,key.s,key.len,act->s + d,"","a");
*(act->s + act->len - 1) = '.'; /* we put a '.' Bad, but works */
if (!stralloc_catb(act,hash,COOKIE)) die_nomem();
if (!stralloc_cats(act,"@")) die_nomem();
if (!stralloc_cat(act,&outhost)) die_nomem();
}
void main(int argc,char **argv)
{
const char *dir;
int fdlock;
const char *sender;
int match;
int flaginheader;
int flagmodpost;
int flagremote;
int ismod;
stralloc mod = {0};
const char *err;
unsigned int i;
(void) umask(022);
sig_pipeignore();
sender = get_sender();
if (sender) {
if (!*sender || str_equal(sender,"#@[]"))
strerr_die2x(100,FATAL,MSG(ERR_BOUNCE));
}
if (!stralloc_copys(&sendopt,"-")) die_nomem();
getconfopt(argc,argv,options,1,&dir);
initsub(0);
flagmodpost = getconf_line(&moderators,"modpost",0);
flagremote = getconf_isset("remote");
if (!flagmodpost && !flagconfirm) /* not msg-mod. Exec ezmlm-send */
wrap_execbin("/ezmlm-send", &sendopt, dir);
if (!moderators.len) {
if (!stralloc_copys(&moderators,"mod")) die_nomem();
}
if (!stralloc_0(&moderators)) die_nomem();
if (sender) {
ismod = issub(moderators.s,sender,&mod);
closesub();
/* sender = moderator? */
} else
ismod = 0;
if (!ismod && flagmodpostonly)
strerr_die2x(100,FATAL,MSG(ERR_NO_POST));
fdlock = lockfile("mod/lock");
if (!stralloc_copys(&mydtline, flagconfirm
? "Delivered-To: confirm to "
: "Delivered-To: moderator for ")) die_nomem();
if (!stralloc_catb(&mydtline,outlocal.s,outlocal.len)) die_nomem();
if (!stralloc_append(&mydtline,"@")) die_nomem();
if (!stralloc_catb(&mydtline,outhost.s,outhost.len)) die_nomem();
if (!stralloc_cats(&mydtline,"\n")) die_nomem();
if (!stralloc_copys(&returnpath,"Return-Path: <")) die_nomem();
if (sender) {
if (!stralloc_cats(&returnpath,sender)) die_nomem();
for (i = 14; i < returnpath.len;++i)
if (returnpath.s[i] == '\n' || !returnpath.s[i] )
returnpath.s[i] = '_';
/* NUL and '\n' are bad, but we don't quote since this is */
/* only for ezmlm-moderate, NOT for SMTP */
}
if (!stralloc_cats(&returnpath,">\n")) die_nomem();
pid = getpid(); /* unique file name */
for (i = 0;;++i) /* got lock - nobody else can add files */
{
when = now(); /* when is also used later for date! */
if (!stralloc_copys(&fnmsg, flagconfirm?"mod/unconfirmed/":"mod/pending/")) die_nomem();
if (!stralloc_copyb(&fnbase,strnum,fmt_ulong(strnum,when))) die_nomem();
if (!stralloc_append(&fnbase,".")) die_nomem();
if (!stralloc_catb(&fnbase,strnum,fmt_ulong(strnum,pid))) die_nomem();
if (!stralloc_cat(&fnmsg,&fnbase)) die_nomem();
if (!stralloc_0(&fnmsg)) die_nomem();
if (stat(fnmsg.s,&st) == -1) if (errno == error_noent) break;
/* really should never get to this point */
if (i == 2)
strerr_die2x(111,FATAL,MSG(ERR_UNIQUE));
sleep(2);
}
if (!stralloc_copys(&action,"-")) die_nomem();
if (!stralloc_cats(&action,flagconfirm?ACTION_DISCARD:ACTION_REJECT)) die_nomem();
if (!stralloc_cat(&action,&fnbase)) die_nomem();
if (!stralloc_0(&action)) die_nomem();
makeacthash(&action);
if (!quote("ed,&outlocal)) die_nomem();
if (!stralloc_copy(&reject,"ed)) die_nomem();
if (!stralloc_cat(&reject,&action)) die_nomem();
if (!stralloc_0(&reject)) die_nomem();
if (!stralloc_copys(&action,"-")) die_nomem();
if (!stralloc_cats(&action,flagconfirm?ACTION_CONFIRM:ACTION_ACCEPT)) die_nomem();
if (!stralloc_cat(&action,&fnbase)) die_nomem();
if (!stralloc_0(&action)) die_nomem();
makeacthash(&action);
if (!stralloc_copy(&accept,"ed)) die_nomem();
if (!stralloc_cat(&accept,&action)) die_nomem();
if (!stralloc_0(&accept)) die_nomem();
set_cptarget(accept.s); /* for copy () */
set_cpconfirm(reject.s,quoted.len);
fdmsg = open_trunc(fnmsg.s);
if (fdmsg == -1)
strerr_die2sys(111,FATAL,MSG1(ERR_WRITE,fnmsg.s));
substdio_fdbuf(&ssmsg,write,fdmsg,msgbuf,sizeof(msgbuf));
if (qmail_open(&qq) == -1) /* Open mailer */
strerr_die2sys(111,FATAL,MSG(ERR_QMAIL_QUEUE));
hdr_add2s("Mailing-List: ",MSG(TXT_MAILING_LIST));
if (listid.len > 0)
hdr_add2("List-ID: ",listid.s,listid.len);
hdr_datemsgid(when);
if (flagconfirm)
hdr_from("-owner");
else
hdr_add2s("From: ",reject.s);
hdr_add2s("Reply-To: ",accept.s);
if (!flagconfirm && !ismod && flagremote) { /* if remote admin add -allow- address */
qmail_puts(&qq,"Cc: "); /* for ezmlm-gate users */
strnum[fmt_ulong(strnum,(unsigned long) when)] = 0;
cookie(hash,key.s,key.len-FLD_ALLOW,strnum,sender,"t");
if (!stralloc_copy(&line,&outlocal)) die_nomem();
if (!stralloc_cats(&line,"-allow-tc.")) die_nomem();
if (!stralloc_cats(&line,strnum)) die_nomem();
if (!stralloc_append(&line,".")) die_nomem();
if (!stralloc_catb(&line,hash,COOKIE)) die_nomem();
if (!stralloc_append(&line,"-")) die_nomem();
i = str_rchr(sender,'@');
if (!stralloc_catb(&line,sender,i)) die_nomem();
if (sender[i]) {
if (!stralloc_append(&line,"=")) die_nomem();
if (!stralloc_cats(&line,sender + i + 1)) die_nomem();
}
qmail_put(&qq,line.s,line.len);
qmail_puts(&qq,"@");
qmail_put(&qq,outhost.s,outhost.len);
qmail_puts(&qq,"\n");
}
qmail_puts(&qq,"To: <");
if (flagconfirm) {
if (sender)
qmail_puts(&qq, sender);
} else {
if (!quote("ed,&outlocal))
die_nomem();
qmail_put(&qq,quoted.s,quoted.len);
qmail_puts(&qq,"-moderators@");
qmail_put(&qq,outhost.s,outhost.len);
}
qmail_puts(&qq,">\n");
/* FIXME: Drop the custom subject hack and use hdr_listsubject1 */
hdr_subject(flagconfirm ? MSG(SUB_CONFIRM_POST) : MSG(SUB_MODERATE));
if (flagmime) {
hdr_mime(CTYPE_MULTIPART);
hdr_boundary(0);
hdr_ctype(CTYPE_TEXT);
hdr_transferenc();
} else
qmail_puts(&qq,"\n\n");
copy(&qq,flagconfirm?"text/post-confirm":"text/mod-request",flagcd);
if (flagcd == 'B') {
encodeB("",0,&line,2);
qmail_put(&qq,line.s,line.len);
}
if (substdio_put(&ssmsg,returnpath.s,returnpath.len) == -1) die_msg();
if (substdio_put(&ssmsg,mydtline.s,mydtline.len) == -1) die_msg();
substdio_fdbuf(&ssin,read,0,inbuf,sizeof(inbuf));
if (flagmime) {
hdr_boundary(0);
hdr_ctype(CTYPE_MESSAGE);
qmail_puts(&qq, "\n");
}
qmail_put(&qq,returnpath.s,returnpath.len);
qmail_put(&qq,mydtline.s,mydtline.len);
flaginheader = 1;
for (;;) {
if (getln(&ssin,&line,&match,'\n') == -1)
strerr_die2sys(111,FATAL,MSG(ERR_READ_INPUT));
if (!match) break;
if (line.len == 1) flaginheader = 0;
if (flaginheader) {
if ((line.len == mydtline.len) &&
!byte_diff(line.s,line.len,mydtline.s)) {
close(fdmsg); /* be nice - clean up */
unlink(fnmsg.s);
strerr_die2x(100,FATAL,MSG(ERR_LOOPING));
}
if (case_startb(line.s,line.len,"mailing-list:")) {
close(fdmsg); /* be nice - clean up */
unlink(fnmsg.s);
strerr_die2x(100,FATAL,MSG(ERR_MAILING_LIST));
}
}
if (flagbody || flaginheader) /* skip body if !flagbody */
qmail_put(&qq,line.s,line.len);
if (substdio_put(&ssmsg,line.s,line.len) == -1) die_msg();
}
if (flagmime)
hdr_boundary(1);
/* close archive before qmail. Loss of qmail will result in re-run, and */
/* worst case this results in a duplicate msg sitting orphaned until it's */
/* cleaned out. */
if (substdio_flush(&ssmsg) == -1) die_msg();
if (fsync(fdmsg) == -1) die_msg();
if (fchmod(fdmsg,MODE_MOD_MSG | 0700) == -1) die_msg();
if (close(fdmsg) == -1) die_msg(); /* NFS stupidity */
close(fdlock);
if (flagconfirm) {
qmail_from(&qq,reject.s); /* envelope sender */
} else {
if (!stralloc_copy(&line,&outlocal)) die_nomem();
if (!stralloc_cats(&line,"-return-@")) die_nomem();
if (!stralloc_cat(&line,&outhost)) die_nomem();
if (!stralloc_0(&line)) die_nomem();
qmail_from(&qq,line.s); /* envelope sender */
}
if (flagconfirm) /* to sender */
qmail_to(&qq,sender);
else if (ismod) /* to moderator only */
qmail_to(&qq,mod.s);
else {
if (flagself) { /* to all moderators */
if (!stralloc_copys(&moderators,"mod")) die_nomem();
if (!stralloc_0(&moderators)) die_nomem();
}
putsubs(moderators.s,0,52,subto);
}
if (*(err = qmail_close(&qq)) == '\0') {
strnum[fmt_ulong(strnum,qmail_qp(&qq))] = 0;
strerr_die2x(0,"ezmlm-store: info: qp ",strnum);
} else
strerr_die4x(111,FATAL,MSG(ERR_TMP_QMAIL_QUEUE),": ",err+1);
}