Debian Codegraph @

innd/newsfeeds.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* $Revision: 1.38 $
2 **
3 ** Routines for the in-core data structures for the newsfeeds file.
4 */
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include "configdata.h"
8 #include "clibrary.h"
9 #include "innd.h"
10
11
12 STATIC SITE SITEnull;
13 STATIC char SITEfeedspath[] = _PATH_NEWSFEEDS;
14
15
16 /*
17 ** Return a copy of an array of strings.
18 */
19 STATIC char **
20 SITEcopystrings(av)
21 char **av;
22 {
23 register char **new;
24 register char **pp;
25 char **save;
26
27 for (pp = av; *pp; pp++)
28 continue;
29 for (new = save = NEW(char*, pp - av + 1), pp = av; *pp; pp++)
30 *new++ = COPY(*pp);
31 *new = NULL;
32 return save;
33 }
34
35
36 /*
37 ** Read the newsfeeds file, return a string array of entries.
38 */
39 char **
40 SITEreadfile(ReadOnly)
41 BOOL ReadOnly;
42 {
43 static char **old_strings;
44 static time_t old_mtime;
45 static ino_t old_ino;
46 static OFFSET_T old_size;
47 register char *p;
48 register char *to;
49 register char *site;
50 register int i;
51 struct stat Sb;
52 char *data;
53
54 if (old_strings != NULL) {
55 /* If the file hasn't changed, return a copy of the old data. */
56 if (stat(SITEfeedspath, &Sb) >= 0
57 && Sb.st_ino == old_ino
58 && Sb.st_size == old_size
59 && Sb.st_mtime == old_mtime)
60 return ReadOnly ? old_strings : SITEcopystrings(old_strings);
61
62 /* Data's bad, toss it. */
63 for (i = 0; old_strings[i] != NULL; i++)
64 DISPOSE(old_strings[i]);
65 DISPOSE(old_strings);
66 }
67
68 /* Read in the file, note its statistics. */
69 if ((data = ReadInFile(SITEfeedspath, &Sb)) == NULL) {
70 syslog(L_FATAL, "%s cant read %s %m", LogName, SITEfeedspath);
71 exit(1);
72 }
73 old_mtime = Sb.st_mtime;
74 old_ino = Sb.st_ino;
75 old_size = Sb.st_size;
76
77 /* Get a gross count of the number of sites. */
78 for (p = data, i = 0; (p = strchr(p, '\n')) != NULL; p++, i++)
79 continue;
80
81 /* Scan the file, parse all multi-line entries. */
82 for (old_strings = NEW(char*, i + 1), i = 0, to = p = data; *p; ) {
83 for (site = to; *p; ) {
84 if (*p == '\n') {
85 p++;
86 *to = '\0';
87 break;
88 }
89 if (*p == '\\' && p[1] == '\n')
90 while (*++p && CTYPE(isspace, *p))
91 continue;
92 else
93 *to++ = *p++;
94 }
95 *to++ = '\0';
96 if (*site == COMMENT_CHAR || *site == '\0')
97 continue ;
98 if (strspn(site," \t") == strlen (site))
99 continue;
100 old_strings[i++] = COPY(site);
101 }
102 old_strings[i] = NULL;
103
104 DISPOSE(data);
105 return ReadOnly ? old_strings : SITEcopystrings(old_strings);
106 }
107
108
109 /*
110 ** Modify "subbed" according to the patterns in "patlist."
111 */
112 STATIC void
113 SITEsetlist(patlist, subbed, poison, poisonEntry)
114 char **patlist;
115 char *subbed;
116 char *poison;
117 BOOL *poisonEntry;
118 {
119 register char *pat;
120 register char *p;
121 register char *u;
122 register char subvalue;
123 register char poisonvalue;
124 register NEWSGROUP *ngp;
125 register int i;
126
127 while ((pat = *patlist++) != NULL) {
128 subvalue = *pat != SUB_NEGATE && *pat != SUB_POISON;
129 poisonvalue = *pat == SUB_POISON;
130 if (!subvalue)
131 pat++;
132 if (!*pat)
133 continue;
134
135 /* See if pattern is a simple newsgroup name. If so, set the
136 * right subbed element for that one group (if found); if not,
137 * pattern-match against all the groups. */
138 for (p = pat; *p; p++)
139 if (*p == '?' || *p == '*' || *p == '[')
140 break;
141 if (*p == '\0') {
142 /* Simple string; look it up, set it. */
143 if ((ngp = NGfind(pat)) != NULL) {
144 subbed[ngp - Groups] = subvalue;
145 poison[ngp - Groups] = poisonvalue;
146 }
147 }
148 else
149 for (p = subbed, u = poison, ngp = Groups, i = nGroups;
150 --i >= 0; ngp++, p++, u++)
151 if (wildmat(ngp->Name, pat)) {
152 *p = subvalue;
153 *u = poisonvalue;
154 }
155 }
156 }
157
158
159 /*
160 ** Parse an individual site entry. Subbed is used to build the subscription
161 ** list. Since this routine is called once for each site, the caller
162 ** allocates subbed once, and frees it after the last site has been parsed.
163 ** If subbed is NULL, we don't update the SITE array, since we're just
164 ** doing syntax checking.
165 */
166 STRING
167 SITEparseone(Entry, sp, subbed, poison)
168 char *Entry;
169 SITE *sp;
170 char *subbed;
171 char *poison;
172 {
173 static char BATCH[] = _PATH_BATCHDIR;
174 register int i;
175 register int j;
176 register NEWSGROUP *ngp;
177 register char *p;
178 register char *u;
179 char *f2;
180 char *f3;
181 char *f4;
182 char **save;
183 char **argv;
184 BOOL JustModerated;
185 BOOL JustUnmoderated;
186 int isp;
187 SITE *nsp;
188 BUFFER b;
189
190 b = sp->Buffer;
191 *sp = SITEnull;
192 sp->Buffer = b;
193 sp->Master = NOSITE;
194 sp->Funnel = NOSITE;
195 sp->PoisonEntry = FALSE;
196 sp->Process = -1;
197 sp->Next = sp->Prev = NOSITE;
198 sp->Entry = Entry;
199 sp->FileFlags[0] = FEED_NAME;
200 sp->FileFlags[1] = '\0';
201
202 /* Nip off the first field, the site name. */
203 if ((f2 = strchr(Entry, NF_FIELD_SEP)) == NULL)
204 return "missing field 2";
205 *f2++ = '\0';
206 sp->Name = Entry;
207 if ((p = strchr(sp->Name, NF_SUBFIELD_SEP)) != NULL) {
208 /* Exclusions within the site field. */
209 *p++ = '\0';
210 if (*p)
211 sp->Exclusions = CommaSplit(p);
212 }
213 sp->NameLength = strlen(sp->Name);
214
215 /* Parse the second field, the subscriptions. */
216 if ((f3 = strchr(f2, NF_FIELD_SEP)) == NULL)
217 return "missing field 3";
218 *f3++ = '\0';
219 if ((p = strchr(f2, NF_SUBFIELD_SEP)) != NULL) {
220 /* Distributions within the subscription field. */
221 *p++ = '\0';
222 if (*p)
223 sp->Distributions = CommaSplit(p);
224 }
225 if (f2)
226 sp->Patterns = CommaSplit(f2);
227
228 if (subbed) {
229 /* Read the subscription patterns and set the bits. */
230 (void)memset((POINTER)subbed, SUB_DEFAULT, (SIZE_T)nGroups);
231 (void)memset((POINTER)poison, SUB_DEFAULT, (SIZE_T)nGroups);
232 if (ME.Patterns)
233 SITEsetlist(ME.Patterns, subbed, poison, &ME.PoisonEntry);
234 SITEsetlist(sp->Patterns, subbed, poison, &sp->PoisonEntry);
235 }
236
237 /* Get the third field, the flags. */
238 if ((f4 = strchr(f3, NF_FIELD_SEP)) == NULL)
239 return "missing field 4";
240 *f4++ = '\0';
241 JustModerated = FALSE;
242 JustUnmoderated = FALSE;
243 sp->Type = FTfile;
244 for (save = argv = CommaSplit(f3); (p = *argv++) != NULL; )
245 switch (*p) {
246 default:
247 return "unknown field 3 flag";
248 case '\0':
249 break;
250 case '<':
251 if (*++p && CTYPE(isdigit, *p))
252 sp->MaxSize = atol(p);
253 break;
254 case '>':
255 if (*++p && CTYPE(isdigit, *p))
256 sp->MinSize = atol(p);
257 break;
258 case 'A':
259 while (*++p)
260 switch (*p) {
261 default:
262 return "unknown A param in field 3";
263 case 'd': sp->DistRequired = TRUE; break;
264 case 'p': sp->IgnorePath = TRUE; break;
265 }
266 break;
267 case 'B':
268 if (*++p && CTYPE(isdigit, *p)) {
269 sp->StartWriting = atoi(p);
270 if ((p = strchr(p, NF_SUBFIELD_SEP)) != NULL
271 && *++p
272 && CTYPE(isdigit, *p))
273 sp->StopWriting = atoi(p);
274 }
275 break;
276 case 'F':
277 if (*++p == '\0')
278 return "missing file name for F param in field 3";
279 else if (*p == '/')
280 sp->SpoolName = COPY(p);
281 else {
282 sp->SpoolName = NEW(char, STRLEN(BATCH) + 1 + strlen(p) + 1);
283 FileGlue(sp->SpoolName, BATCH, '/', p);
284 }
285 break;
286 case 'G':
287 if (*++p && CTYPE(isdigit, *p))
288 sp->Groupcount = atoi(p);
289 else
290 sp->Groupcount = 1;
291 break;
292 case 'H':
293 if (*++p && CTYPE(isdigit, *p))
294 sp->Hops = atoi(p);
295 else
296 sp->Hops = 1;
297 break;
298 case 'I':
299 if (*++p && CTYPE(isdigit, *p))
300 sp->Flushpoint = atol(p);
301 break;
302 case 'N':
303 while (*++p)
304 switch (*p) {
305 default:
306 return "unknown N param in field 3";
307 case 'm': JustModerated = TRUE; break;
308 case 'u': JustUnmoderated = TRUE; break;
309 }
310 break;
311 case 'S':
312 if (*++p && CTYPE(isdigit, *p))
313 sp->StartSpooling = atol(p);
314 break;
315 case 'T':
316 switch (*++p) {
317 default:
318 return "unknown T param in field 3";
319 case 'c': sp->Type = FTchannel; break;
320 case 'l': sp->Type = FTlogonly; break;
321 case 'f': sp->Type = FTfile; break;
322 case 'm': sp->Type = FTfunnel; break;
323 case 'p': sp->Type = FTprogram; break;
324 case 'x': sp->Type = FTexploder; break;
325 }
326 break;
327 case 'W':
328 for (i = 0; *++p && i < FEED_MAXFLAGS; ) {
329 switch (*p) {
330 default:
331 return "unknown W param in field 3";
332 case FEED_FNLNAMES: /* Funnel feed names */
333 sp->FNLwantsnames = TRUE;
334 break;
335 case FEED_HEADERS: /* Article headers */
336 NeedHeaders = TRUE;
337 break;
338 case FEED_OVERVIEW:
339 NeedOverview = TRUE; /* Overview data */
340 break;
341 case FEED_BYTESIZE: /* Size in bytes */
•342 case FEED_PATH: /* Pathline header */
343 case FEED_FULLNAME: /* Full filename */
344 case FEED_HDR_DISTRIB: /* Distribution header */
345 case FEED_HDR_NEWSGROUP: /* Newsgroup header */
346 case FEED_MESSAGEID: /* Message-ID */
347 case FEED_NAME: /* Filename */
348 case FEED_NEWSGROUP: /* Newsgroup */
349 case FEED_REPLIC: /* Replication data */
350 case FEED_SITE: /* Site that gave it */
351 case FEED_TIMERECEIVED: /* When received */
352 case FEED_TIMEPOSTED: /* When posted */
353 break;
354 }
355 sp->FileFlags[i++] = *p;
356 }
357 if (*p)
358 return "too many W param values";
359 sp->FileFlags[i] = '\0';
360 break;
361 }
362 DISPOSE(save);
363 if (sp->Flushpoint && sp->Type != FTfile)
364 return "I param with non-file feed";
365 if (sp->Flushpoint == 0 && sp->Type == FTfile)
366 sp->Flushpoint = SITE_BUFFER_SIZE;
367
368 if (subbed) {
369 /* Modify the subscription list based on the flags. */
370 if (JustModerated)
371 for (p = subbed, ngp = Groups, i = nGroups; --i >= 0; ngp++, p++)
372 if (ngp->Rest[0] != NF_FLAG_MODERATED)
373 *p = FALSE;
374 if (JustUnmoderated)
375 for (p = subbed, ngp = Groups, i = nGroups; --i >= 0; ngp++, p++)
376 if (ngp->Rest[0] == NF_FLAG_MODERATED)
377 *p = FALSE;
378 }
379
380 /* Get the fourth field, the param. */
381 if (*f4 == '\0' && sp != &ME) {
382 if (sp->Type != FTfile && sp->Type != FTlogonly)
383 return "empty field 4";
384 sp->Param = NEW(char, STRLEN(BATCH) + 1 + sp->NameLength + 1);
385 FileGlue(sp->Param, BATCH, '/', sp->Name);
386 }
387 else if (sp->Type == FTfile && *f4 != '/') {
388 sp->Param = NEW(char, STRLEN(BATCH) + 1 + strlen(f4) + 1);
389 FileGlue(sp->Param, BATCH, '/', f4);
390 }
391 else
392 sp->Param = COPY(f4);
393
394 if (sp->SpoolName == NULL) {
395 sp->SpoolName = NEW(char, STRLEN(BATCH) + 1 + strlen(sp->Name) + 1);
396 FileGlue(sp->SpoolName, BATCH, '/', sp->Name);
397 }
398
399 /* Make sure there is only one %s, and only one *. */
400 if (sp->Type == FTprogram) {
401 f3 = NULL;
402 for (f2 = sp->Param; *f2; f2 = p + 1) {
403 p = strchr(f2, '%');
404 if (p == NULL)
405 break;
406 if (p[1] == '%') {
407 p++;
408 continue;
409 }
410 if (f3 != NULL)
411 return "bad (extra) sprintf format for field 4";
412 f3 = p;
413 while (*++p && *p != '*' && !CTYPE(isalpha, *p))
414 continue;
415 if (*p != 's')
416 return "bad sprintf format for field 4";
417 }
418 if (sp->FNLwantsnames
419 && ((p = strchr(sp->Param, '*')) == NULL
420 || strchr(++p, '*') != NULL))
421 return "multiple or no *'s in field 4";
422 }
423
424 /* Now tell the groups this site gets that they should feed this site. */
425 if (sp != &ME && subbed) {
426 isp = sp - Sites;
427 for (p = subbed, u = poison, ngp = Groups, i = nGroups;
428 --i >= 0; ngp++) {
429 if (*p++) {
430 for (j = 0; j < ngp->nSites; j++)
431 if (ngp->Sites[j] == NOSITE) {
432 ngp->Sites[j] = isp;
433 break;
434 }
435 if (j == ngp->nSites)
436 ngp->Sites[ngp->nSites++] = isp;
437 }
438 if (*u++) {
439 for (j = 0; j < ngp->nPoison; j++)
440 if (ngp->Poison[j] == NOSITE) {
441 ngp->Poison[j] = isp;
442 break;
443 }
444 if (j == ngp->nPoison)
445 ngp->Poison[ngp->nPoison++] = isp;
446 }
447 }
448 }
449
450 /* If this is a duplicate name, find the master. */
451 nsp = SITEfind(sp->Name);
452 if (nsp == sp)
453 nsp = SITEfindnext(sp->Name, nsp);
454 if (nsp != NULL) {
455 if (nsp->Master != NOSITE)
456 nsp = &Sites[nsp->Master];
457 if (nsp != sp) {
458 sp->Master = nsp - Sites;
459 nsp->IsMaster = TRUE;
460 }
461 }
462
463 return NULL;
464 }
465
466
467 /*
468 ** Patch up the funnel references.
469 */
470 BOOL
471 SITEfunnelpatch()
472 {
473 register int i;
474 register int length;
475 register SITE *sp;
476 SITE *funnel;
477 BOOL result;
478
479 /* Get worst-case length of all sitenames. */
480 for (length = 0, i = nSites, sp = Sites; --i >= 0; sp++)
481 if (sp->Name != NULL)
482 length += 1 + strlen(sp->Name);
483
484 /* Loop over all funnel feeds. */
485 for (result = TRUE, i = nSites, sp = Sites; --i >= 0; sp++) {
486 if (sp->Name == NULL || sp->Type != FTfunnel)
487 continue;
488
489 /* Find the entry they feed in to, give that entry a buffer. */
490 if (sp->Param == NULL) {
491 syslog(L_FATAL, "%s funnel NULL", sp->Name);
492 SITEfree(sp);
493 result = FALSE;
494 continue;
495 }
496 if ((funnel = SITEfind(sp->Param)) == NULL) {
497 syslog(L_FATAL, "%s funnel_bad", sp->Name);
498 SITEfree(sp);
499 result = FALSE;
500 continue;
501 }
502 if (funnel->Type == FTfunnel) {
503 syslog(L_FATAL, "%s funnels to funnel %s", sp->Name, funnel->Name);
504 SITEfree(sp);
505 result = FALSE;
506 continue;
507 }
508 if (funnel->FNLnames.Data == NULL) {
509 funnel->FNLnames.Size = length;
510 funnel->FNLnames.Data = NEW(char, length);
511 }
512 else if (funnel->FNLnames.Size != length) {
513 funnel->FNLnames.Size = length;
514 RENEW(funnel->FNLnames.Data, char, length);
515 }
516 sp->Funnel = funnel - Sites;
517 }
518
519 return result;
520 }
521
522
523 /*
524 ** Read the entries in the newsfeeds file, and parse them one at a time.
525 */
526 void
527 SITEparsefile(StartSite)
528 BOOL StartSite;
529 {
530 register int i;
531 register char *p;
532 char **strings;
533 SITE *sp;
534 char *subbed;
535 char *poison;
536 STRING error;
537 int errors;
538
539 /* Free old sites info. */
540 if (Sites) {
541 for (i = nSites, sp = Sites; --i >= 0; sp++) {
542 SITEflush(sp, FALSE);
543 SITEfree(sp);
544 }
545 DISPOSE(Sites);
546 SITEfree(&ME);
547 }
548
549 /* Count the number of sites. */
550 for (strings = SITEreadfile(FALSE), nSites = 0; strings[nSites]; nSites++)
551 continue;
552 Sites = NEW(SITE, nSites);
553 for (sp = Sites, i = 0; i < nSites; i++, sp++) {
554 sp->Name = NULL;
555 sp->Buffer.Data = NULL;
556 sp->Buffer.Size = sp->Buffer.Used = sp->Buffer.Left = 0 ;
557 }
558
559 /* Set up scratch subscription list. */
560 subbed = NEW(char, nGroups);
561 poison = NEW(char, nGroups);
562
563 ME.Prev = 0; /* Used as a flag to ensure exactly one ME entry */
564 for (sp = Sites, errors = 0, i = 0; i < nSites; i++) {
565 p = strings[i];
566 if (p[0] == 'M' && p[1] == 'E' &&
567 ((p[2] == NF_FIELD_SEP) || (p[2] == NF_SUBFIELD_SEP))) {
568 if (ME.Prev == NOSITE) {
569 syslog(L_FATAL, "bad_newsfeeds. Must have exactly one ME entry");
570 errors++;
571 } else
572 if ((error = SITEparseone(p, &ME, subbed, poison)) != NULL) {
573 syslog(L_FATAL, "%s bad_newsfeeds %s", MaxLength(p, p), error);
574 errors++;
575 }
576 continue;
577 }
578 if ((error = SITEparseone(p, sp, subbed, poison)) != NULL) {
579 syslog(L_FATAL, "%s bad_newsfeeds %s", MaxLength(p, p), error);
580 errors++;
581 continue;
582 }
583 if (StartSite && !SITEsetup(sp)) {
584 syslog(L_FATAL, "%s cant setup %m", sp->Name);
585 errors++;
586 continue;
587 }
588 sp->Working = TRUE;
589 sp++;
590 }
591 if (ME.Prev != NOSITE) {
592 syslog(L_FATAL, "bad_newsfeeds. Must have exactly one ME entry");
593 errors++;
594 }
595
596 if (errors) {
597 syslog(L_FATAL, "%s syntax_error %s", LogName, SITEfeedspath);
598 JustCleanup();
599 exit(1);
600 }
601
602 /* Free our scratch array, set up the funnel links. */
603 nSites = sp - Sites;
604 DISPOSE(subbed);
605 DISPOSE(poison);
606 DISPOSE(strings);
607 if (!SITEfunnelpatch()) {
608 JustCleanup();
609 exit(1);
610 }
611 }