-
Notifications
You must be signed in to change notification settings - Fork 1
/
archive.c
325 lines (266 loc) · 6.22 KB
/
archive.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
/* $Id$
*
* Archiving program for distribute
*
* Shigeya Suzuki, Dec 1993
* Copyright(c)1993-1999 Shigeya Suzuki
*/
#include <config.h>
#include <stdio.h>
#ifndef STDC_HEADERS
# ifndef HAVE_STRCHR
# define strchr index
# endif
# ifndef HAVE_STRRCHR
# define strrchr rindex
# endif
#endif
#include <ctype.h>
#include <string.h>
#include <sysexits.h>
#include <sys/types.h>
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#endif
#include <sys/param.h>
#ifdef USE_SYSLOG
# ifdef HAVE_SYSLOG_H
# include <syslog.h>
# endif
#endif
#ifdef DEBUGLOG
FILE *debuglog;
#endif
#include "patchlevel.h" /* version identifier */
#include "config.h"
#include "memory.h"
#include "history.h"
#include "pathutil.h"
#include "header.h"
#include "logging.h"
char *progname;
char *Xsequence = NULL; /* X-Sequence value */
char *XMl_Name = NULL;
char *XMl_Count = NULL;
char *Xdistribute; /* X-Distribute value */
char *subject;
char *listname = NULL;
char *archive_path = DEF_ARCHIVE_PATH;
char *archivedir = NULL;
char *mltag;
int mlseq;
char *headv[MAXHEADERLINE]; /* Header vector */
int headc; /* Number of headers */
char filename[512];
char *index_name = DEF_INDEX_NAME;
int majordomo = 0;
int zaprecv = 0;
int useindex = 0;
/* Forward Declaration
*/
void usage __P((void));
/* Option parser
*/
void
parse_options(argc, argv)
int argc;
char **argv;
{
extern char *optarg;
int c;
while ((c = getopt(argc, argv, "M:N:C:Z:Y:jDR?")) != EOF) {
switch(c) {
case 'M': /* generic mailinglist with reply-to */
case 'N': /* generic mailinglist without reply-to */
listname = optarg;
break;
case 'Y':
archive_path = optarg;
useindex++;
break;
case 'C':
archivedir = optarg;
useindex++;
break;
case 'j': /* majodomo */
majordomo++;
break;
case 'D': /* print error message to stderr for debugging */
logging_setprinterror(1);
break;
case 'R': /* zap Received: lines */
zaprecv++;
break;
case 'Z':
index_name = optarg;
break;
default:
usage();
logandexit(EX_USAGE, "unknown option: %c", c);
break;
case '?':
usage();
exit(0);
break; /*NOTREACHED*/
}
}
}
void
parse_header(file)
FILE *file;
{
/* Read all of the headers and make a header vector
*/
headc = head_parse(MAXHEADERLINE, headv, file);
/* Try to find "X-Distribute". If not exists, log and exit.
* Then, find "X-Sequence", "X-Ml-Name:", "X-Ml-Count:".
* If none exists, log and exit.
*/
#if 0
if ((Xdistribute = head_find(headc, headv, "X-Distribute:")) == NULL) {
logandexit(EX_NOINPUT, "Does not contain X-Distribute tag");
}
#endif
Xsequence = head_find(headc, headv, "X-Sequence: ");
XMl_Name = head_find(headc, headv, "X-Ml-Name: ");
XMl_Count = head_find(headc, headv, "X-Ml-Count: ");
if (Xsequence == NULL && (XMl_Name == NULL || XMl_Count == NULL)) {
logandexit(EX_NOINPUT, "Does not contain any Sequence tag (fatal)");
}
if ((subject = head_find(headc, headv, "Subject:")) == NULL) {
subject = "(No Subject)";
}
else {
subject += sizeof("Subject:");
}
/* Delete all the Received: lines, if the user said to do so.
*/
if (zaprecv) {
while (head_delete(headc, headv, "Received:") != NULL)
;
}
}
char *
parse_sequence(sequence, ml_n, ml_c, mlseq)
char *sequence;
char *ml_n, *ml_c;
int *mlseq;
{
char *tag;
char *endp;
char *p;
*mlseq = 0; /* default error value */
if (sequence != NULL) {
if (strncasecmp(sequence,"X-Sequence: ",sizeof("X-Sequence: ")-1) != 0) {
return NULL;
}
tag = strsave(sequence + sizeof("X-Sequence: ")-1);
endp = strrchr(tag, '\0');
for (p=endp; p-- > tag;) {
if (!isdigit(*p))
break;
}
if (*p != ' ') { /* sequence separator must be space */
free(tag);
return NULL;
}
*p++ = '\0';
*mlseq = atoi(p);
return tag;
}
else { /* try MSC Style */
if (strncasecmp(ml_n, "X-Ml-Name: ", sizeof("X-Ml-Name: ")-1) != 0) {
return NULL;
}
if (strncasecmp(ml_c, "X-Ml-Count: ", sizeof("X-Ml-Count: ")-1) != 0) {
return NULL;
}
tag = strsave(ml_n + sizeof("X-Ml-Name: ")-1);
p = ml_c + sizeof("X-Ml-Count: ")-1;
while (*p && *p == '0') /* skip leading zer0 */
p++;
*mlseq = atoi(p);
return tag;
}
}
/* doarchive -- archive the mail
*/
void
doarchive()
{
char buf[BUFSIZ]; /* string buffer */
char tmpname[16];
char *dir;
char target[16];
int sum;
int fd;
int i;
FILE *out = stdout;
struct history *h = NULL;
mltag = parse_sequence(Xsequence, XMl_Name, XMl_Count, &mlseq);
if (listname != NULL)
dir = makearchivepath(archive_path, archivedir, listname);
else
dir = makearchivepath(archive_path, archivedir, mltag);
if (chdir(dir) == -1) {
logandexit(EX_NOPERM, "Cannot change directory to %s", dir);
}
if (useindex) {
openhistory(index_name, "r");
h = findhistory(mlseq);
closehistory();
if (h == NULL)
logandexit(EX_UNAVAILABLE, "Can't find history entry for article %d", mlseq);
}
strcpy(tmpname, "msgXXXXXX");
#ifdef HAVE_MKSTEMP
mkstemp(tmpname);
#else
mktemp(tmpname);
#endif
if ((fd = creat(tmpname, 0640)) == -1) {
logandexit(EX_CANTCREAT, "Cannot make file: %s\n", tmpname);
}
out = fdopen(fd, "w");
for (i=0; i<headc; i++) {
if (headv[i] != NULL) {
fputs(headv[i], out);
putc('\n', out);
}
}
putc('\n', out);
sum = 0;
while (fgets(buf, sizeof buf, stdin) != NULL) {
fputs_sum(buf, out, &sum);
}
fclose(out);
if (useindex && h && h->h_bodysum != sum) {
unlink(tmpname);
logandexit(EX_DATAERR, "body checksum mismatch: incoming: %d", sum);
}
sprintf(target, "%d", mlseq);
rename(tmpname, target);
sprintf(filename, "%s/%s", dir, target);
}
/* Finally, the Main Entry
*/
int
main(argc, argv)
int argc;
char ** argv;
{
/* chdir("/tmp"); */
progname = argv[0];
init_log("archive");
parse_options(argc, argv);
parse_header(stdin);
doarchive();
loginfo("\"%s\" archived as %s", subject, filename);
return 0; /*exit(0);*/
}
void
usage()
{
fprintf(stderr, "usage: %s ", progname);
fprintf(stderr, "{-M list | -N list}\n(and more...)");
}