Debian Codegraph @

innd/nc.c

raw ◀ ▶
Source reconstructed at patch state 0 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 /* $Revision: 1.51+precommit-cache$
2 **
3 ** Routines for the NNTP channel. Other channels get the descriptors which
4 ** we turn into NNTP channels, and over which we speak NNTP.
5 */
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include "configdata.h"
9 #include "clibrary.h"
10 #include "innd.h"
11 #include "dbz.h"
12
13
14 #define BAD_COMMAND_COUNT 10
15 #define WIP_CHECK (1 * 60)
16 #define SAVE_AMT 10
17 #define ART_EOF(c, s) \
18 ((c) >= 5 && (s)[-5] == '\r' && (s)[-4] == '\n' && (s)[-3] == '.' \
19 && (s)[-2] == '\r' && (s)[-1] == '\n')
20
21
22 /*
23 ** An entry in the dispatch table. The name, and implementing function,
24 ** of every command we support.
25 */
26 typedef struct _NCDISPATCH {
27 STRING Name;
28 FUNCPTR Function;
29 int Size;
30 } NCDISPATCH;
31
32
33 /*
34 ** Information about the work in progress on all our open channels.
35 */
36 typedef struct _WIP {
37 char *MessageID;
38 long Size;
39 time_t Timestamp;
40 BUFFER Replic;
41 BOOL Wanted;
42 } WIP;
43
44
45 #if 0
46 static FUNCTYPE NCarticle();
47 #endif /* 0 */
48 static FUNCTYPE NCauthinfo();
49 static FUNCTYPE NChead();
50 static FUNCTYPE NChelp();
51 static FUNCTYPE NCihave();
52 static FUNCTYPE NClist();
53 static FUNCTYPE NCmode();
54 static FUNCTYPE NCquit();
55 static FUNCTYPE NCstat();
56 static FUNCTYPE NCxpath();
57 static FUNCTYPE NCxreplic();
58 static FUNCTYPE NC_unimp();
59 /* new modules for streaming */
60 static FUNCTYPE NCxbatch();
61 static FUNCTYPE NCcheck();
62 static FUNCTYPE NCtakethis();
63 static FUNCTYPE NCwritedone();
64
65 STATIC int NCcount; /* Number of open connections */
66 STATIC int NCwipsize; /* Size of NCwip array */
67 STATIC char *NCfreelist[5]; /* Free string list */
68 STATIC WIP *NCwip; /* Work-in-progress */
69 STATIC WIP NCnullwip;
70 STATIC NCDISPATCH NCcommands[] = {
71 #if 0
72 { "article", NCarticle },
73 #else
74 { "article", NC_unimp },
75 #endif /* 0 */
76 { "authinfo", NCauthinfo },
77 { "help", NChelp },
78 { "ihave", NCihave },
79 { "check", NCcheck },
80 { "takethis", NCtakethis },
81 { "list", NClist },
82 { "mode", NCmode },
83 { "xmode", NCmode },
84 { "quit", NCquit },
85 { "head", NChead },
86 { "stat", NCstat },
87 { "body", NC_unimp },
88 { "group", NC_unimp },
89 { "last", NC_unimp },
90 { "newgroups", NC_unimp },
91 { "newnews", NC_unimp },
92 { "next", NC_unimp },
93 { "post", NC_unimp },
94 { "slave", NC_unimp },
95 { "xbatch", NCxbatch },
96 { "xhdr", NC_unimp },
97 { "xpath", NCxpath },
98 { "xreplic", NCxreplic }
99 };
100 STATIC char *NCquietlist[] = {
101 INND_QUIET_BADLIST
102 };
103 STATIC char NCterm[] = "\r\n";
104 STATIC char NCdotterm[] = ".\r\n";
105 STATIC char NCdot[] = "." ;
106 STATIC char NCbadcommand[] = NNTP_BAD_COMMAND;
107 STATIC STRING NCgreeting;
108
109
110 /*
111 * precommit cache
112 */
113 #define NCPRECOMCACHESIZE 4096L
114 #define NCPRECOMCACHETIMEOUT 30
115
116 /* uncomment line below to:
117 * - zap cache entry after art acceptance or rejection
118 */
119 /* #define NCPRECOM_PEDANTIC */
120
121 /* uncomment line below to:
122 * - have CHECK return DEFER instead of REFUSE
123 * for cache hits
124 */
125 /* #define NCPRECOM_DEFER */
126
127 typedef struct {
128 long MessageIDHash;
129 time_t Timestamp;
130 } _NCprecomcache;
131
132 STATIC _NCprecomcache NCprecomcache[NCPRECOMCACHESIZE];
133 unsigned long NCprecomhits;
134 unsigned long NCprecomlookups;
135 STATIC long NCprecompos, NCprecomval;
136 STATIC int NCprecominit = 0;
137
138
139 STATIC void
140 NCprecomclear ()
141 {
142 memset((void *)&NCprecomcache, 0, sizeof(NCprecomcache));
143 NCprecomhits = NCprecomlookups = 0;
144 }
145
146
147
148
149 /*
150 ** Clear the work-in-progress entry and wake up anyone who might
151 ** have been waiting for us.
152 */
153 STATIC void
154 NCclearwip(wp)
155 register WIP *wp;
156 {
157 char *p;
158
159 if ((p = wp->MessageID) != NULL)
160 *p = '\0';
161 wp->Size = 0;
162 if (wp->Wanted) {
163 wp->Wanted = FALSE;
164 SCHANwakeup((POINTER)wp);
165 }
166 }
167
168
169 /*
170 ** Write an NNTP reply message.
171 **
172 ** Tries to do the actual write immediately if it will not block
173 ** and if there is not already other buffered output. Then, if the
174 ** write is successful, calls NCwritedone (which does whatever is
175 ** necessary to accommodate state changes). Else, NCwritedone will
176 ** be called from the main select loop later.
177 **
178 ** If the reply that we are writing now is associated with a
179 ** state change, then cp->State must be set to its new value
180 ** *before* NCwritereply is called.
181 */
182 STATIC void
183 NCwritereply(cp, text)
184 CHANNEL *cp;
185 char *text;
186 {
187 register BUFFER *bp;
188 register int i;
189
190 /* XXX could do RCHANremove(cp) here, as the old NCwritetext() used to
191 * do, but that would be wrong if the channel is sreaming (because it
192 * would zap the channell's input buffer). There's no harm in
193 * never calling RCHANremove here. */
194
195 bp = &cp->Out;
196 i = bp->Left;
197 WCHANappend(cp, text, (int)strlen(text)); /* text in buffer */
198 WCHANappend(cp, NCterm, STRLEN(NCterm)); /* add CR NL to text */
199 if (i == 0) { /* if only data then try to write directly */
200 i = write(cp->fd, &bp->Data[bp->Used], bp->Left);
201 if (Tracing || cp->Tracing)
202 syslog(L_TRACE, "%s NCwritereply %d=write(%d, \"%.15s\", %ld)",
203 CHANname(cp), i, cp->fd, &bp->Data[bp->Used], bp->Left);
204 if (i > 0) bp->Used += i;
205 if (bp->Used == bp->Left) {
206 /* all the data was written */
207 bp->Used = bp->Left = 0;
208 NCwritedone(cp);
209 }
210 else i = 0;
211 } else i = 0;
212 if (i <= 0) { /* write failed, queue it for later */
213 WCHANadd(cp);
214 }
215 if (Tracing || cp->Tracing)
216 syslog(L_TRACE, "%s > %s", CHANname(cp), text);
217 }
218
219 /*
220 ** Tell the NNTP channel to go away.
221 */
222 STATIC void
223 NCwriteshutdown(cp, text)
224 CHANNEL *cp;
225 char *text;
226 {
227 cp->State = CSwritegoodbye;
228 RCHANremove(cp); /* we're not going to read anything more */
229 #if 0
230 /* XXX why would we want to zap whatever was already
231 * in the output buffer? */
232 WCHANset(cp, NNTP_GOODBYE, STRLEN(NNTP_GOODBYE));
233 #else
234 WCHANappend(cp, NNTP_GOODBYE, STRLEN(NNTP_GOODBYE));
235 #endif
236 WCHANappend(cp, " ", 1);
237 WCHANappend(cp, text, (int)strlen(text));
238 WCHANappend(cp, NCterm, STRLEN(NCterm));
239 WCHANadd(cp);
240 }
241
242
243 /*
244 ** If a Message-ID is bad, write a reject message and return TRUE.
245 */
246 STATIC BOOL
247 NCbadid(cp, p)
248 register CHANNEL *cp;
249 register char *p;
250 {
251 if (ARTidok(p))
252 return FALSE;
253
254 NCwritereply(cp, NNTP_HAVEIT_BADID);
255 syslog(L_NOTICE, "%s bad_messageid %s", CHANname(cp), MaxLength(p, p));
256 return TRUE;
257 }
258
259
260 /*
261 ** We have an entire article collected; try to post it. If we're
262 ** not running, drop the article or just pause and reschedule.
263 */
264 STATIC void
265 NCpostit(cp)
266 register CHANNEL *cp;
267 {
268 STRING response;
269 WIP *wp;
270
271 /* Note that some use break, some use return here. */
272 switch (Mode) {
273 default:
274 syslog(L_ERROR, "%s internal NCpostit mode %d", CHANname(cp), Mode);
275 return;
276 case OMpaused:
277 SCHANadd(cp, (time_t)(Now.time + PAUSE_RETRY_TIME), (POINTER)&Mode,
278 NCpostit, (POINTER)NULL);
279 return;
280 case OMrunning:
281 wp = &NCwip[cp->fd];
282 response = ARTpost(cp, AmSlave ? &wp->Replic : NULL, wp->MessageID);
283 if (atoi(response) == NNTP_TOOKIT_VAL) {
284 cp->Received++;
285 if (cp->Sendid.Size > 3) { /* We be streaming */
286 char buff[4];
287 (void)sprintf(buff, "%d", NNTP_OK_RECID_VAL);
288 cp->Sendid.Data[0] = buff[0];
289 cp->Sendid.Data[1] = buff[1];
290 cp->Sendid.Data[2] = buff[2];
291 response = cp->Sendid.Data;
292 }
293 } else {
294 cp->Rejected++;
295 if (cp->Sendid.Size) response = cp->Sendid.Data;
296 }
297 cp->Reported++;
298 if (cp->Reported >= NNTP_ACTIVITY_SYNC) {
299 syslog(L_NOTICE,
300 "%s checkpoint seconds %ld accepted %ld refused %ld rejected %ld",
301 CHANname(cp), (long)(Now.time - cp->Started),
302 cp->Received, cp->Refused, cp->Rejected);
303 cp->Reported = 0;
304 }
305 cp->State = CSgetcmd;
306 NCwritereply(cp, response);
307 #ifdef NCPRECOM_PEDANTIC
308 NCprecomval = dbzhash(wp->MessageID, strlen(wp->MessageID));
309 NCprecompos = NCprecomval % NCPRECOMCACHESIZE;
310 NCprecomcache[NCprecompos].MessageIDHash = 0L;
311 #endif
312 break;
313
314 case OMthrottled:
315 NCwriteshutdown(cp, ModeReason);
316 cp->Rejected++;
317 break;
318 }
319
320 /* Clear the work-in-progress entry. */
321 NCclearwip(&NCwip[cp->fd]);
322 }
323
324
325 /*
326 ** Write-done function. Close down or set state for what we expect to
327 ** read next.
328 */
329 STATIC FUNCTYPE
330 NCwritedone(cp)
331 register CHANNEL *cp;
332 {
333 switch (cp->State) {
334 default:
335 syslog(L_ERROR, "%s internal NCwritedone state %d",
336 CHANname(cp), cp->State);
337 break;
338
339 case CSwritegoodbye:
340 if (NCcount > 0)
341 NCcount--;
342 CHANclose(cp, CHANname(cp));
343 break;
344
345 case CSgetcmd:
346 case CSgetauth:
347 case CSgetarticle:
348 case CSgetrep:
349 case CSgetxbatch:
350 RCHANadd(cp);
351 break;
352 }
353 }
354
355
356
357 #if 0
358 /*
359 ** The "article" command.
360 */
361 STATIC FUNCTYPE
362 NCarticle(cp)
363 register CHANNEL *cp;
364 {
365 register char *p;
366 register char *q;
367 char *art;
368
369 /* Snip off the Message-ID. */
370 for (p = cp->In.Data + STRLEN("article"); ISWHITE(*p); p++)
371 continue;
372 if (NCbadid(cp, p))
373 return;
374
375 /* Get the article filenames, and the article header+body. */
376 if ((art = ARTreadarticle(HISfilesfor(p))) == NULL) {
377 NCwritereply(cp, NNTP_DONTHAVEIT);
378 return;
379 }
380
381 /* Write it. */
382 NCwritereply(cp, NNTP_ARTICLE_FOLLOWS);
383 for (p = art; ((q = strchr(p, '\n')) != NULL); p = q + 1) {
384 if (*p == '.')
385 WCHANappend(cp, ".", 1);
386 WCHANappend(cp, p, q - p);
387 WCHANappend(cp, NCterm, STRLEN(NCterm));
388 }
389
390 /* Write the terminator. */
391 NCwritereply(cp, NCdot); /* Was WCHANappend. fixed 1997/10/31 [email protected] */
392 }
393 #endif /* 0 */
394
395
396 /*
397 ** The "head" command.
398 */
399 STATIC FUNCTYPE
400 NChead(cp)
401 CHANNEL *cp;
402 {
403 register char *p;
404 register char *q;
405 char *head;
406
407 /* Snip off the Message-ID. */
408 for (p = cp->In.Data + STRLEN("head"); ISWHITE(*p); p++)
409 continue;
410 if (NCbadid(cp, p))
411 return;
412
413 /* Get the article filenames, and the header. */
414 if ((head = ARTreadheader(HISfilesfor(p))) == NULL) {
415 NCwritereply(cp, NNTP_DONTHAVEIT);
416 return;
417 }
418
419 /* Write it. */
420 WCHANappend(cp, NNTP_HEAD_FOLLOWS, STRLEN(NNTP_HEAD_FOLLOWS));
421 WCHANappend(cp, NCterm, STRLEN(NCterm));
422 for (p = head; ((q = strchr(p, '\n')) != NULL); p = q + 1) {
423 if (*p == '.')
424 WCHANappend(cp, ".", 1);
425 WCHANappend(cp, p, q - p);
426 WCHANappend(cp, NCterm, STRLEN(NCterm));
427 }
428
429 /* Write the terminator. */
430 NCwritereply(cp, NCdot); /* Was WCHANappend. fixed 1997/10/31 [email protected] */
431 }
432
433
434 /*
435 ** The "stat" command.
436 */
437 STATIC FUNCTYPE
438 NCstat(cp)
439 CHANNEL *cp;
440 {
441 register char *p;
442 /* mibsoft 97-Aug-12 If DBZMAXKEY is changed to be much different than SMBUF,
443 then better bump up SMBUF!
444 */
445 char buff[SMBUF+16];
446
447 /* Snip off the Message-ID. */
448 for (p = cp->In.Data + STRLEN("stat"); ISWHITE(*p); p++)
449 continue;
450 if (NCbadid(cp, p))
451 return;
452
453 /* Get the article filenames; read the header (to make sure not
454 * the article is still here). */
455 if (ARTreadheader(HISfilesfor(p)) == NULL) {
456 NCwritereply(cp, NNTP_DONTHAVEIT);
457 return;
458 }
459
460 /* Write the message. */
461 /* We know there is enough room as long as DBZMAXKEY < SMBUF+16-7 */
462 (void)sprintf(buff, "%d 0 %s", NNTP_NOTHING_FOLLOWS_VAL, p);
463 NCwritereply(cp, buff);
464 }
465
466
467 /*
468 ** The "authinfo" command. Actually, we come in here whenever the
469 ** channel is in CSgetauth state and we just got a command.
470 */
471 STATIC FUNCTYPE
472 NCauthinfo(cp)
473 register CHANNEL *cp;
474 {
475 static char AUTHINFO[] = "authinfo ";
476 static char PASS[] = "pass ";
477 static char USER[] = "user ";
478 register char *p;
479
480 p = cp->In.Data;
481
482 /* Allow the poor sucker to quit. */
483 if (caseEQ(p, "quit")) {
484 NCquit(cp);
485 return;
486 }
487
488 /* Otherwise, make sure we're only getting "authinfo" commands. */
489 if (!caseEQn(p, AUTHINFO, STRLEN(AUTHINFO))) {
490 NCwritereply(cp, NNTP_AUTH_NEEDED);
491 return;
492 }
493 for (p += STRLEN(AUTHINFO); ISWHITE(*p); p++)
494 continue;
495
496 /* Ignore "authinfo user" commands, since we only care about the
497 * password. */
498 if (caseEQn(p, USER, STRLEN(USER))) {
499 NCwritereply(cp, NNTP_AUTH_NEXT);
500 return;
501 }
502
503 /* Now make sure we're getting only "authinfo pass" commands. */
504 if (!caseEQn(p, PASS, STRLEN(PASS))) {
505 NCwritereply(cp, NNTP_AUTH_NEEDED);
506 return;
507 }
508 for (p += STRLEN(PASS); ISWHITE(*p); p++)
509 continue;
510
511 /* Got the password -- is it okay? */
512 if (!RCauthorized(cp, p)) {
513 cp->State = CSwritegoodbye;
514 NCwritereply(cp, NNTP_AUTH_BAD);
515 }
516 else {
517 cp->State = CSgetcmd;
518 NCwritereply(cp, NNTP_AUTH_OK);
519 }
520 }
521
522
523 /*
524 ** Is someone already sending us this article?
525 */
526 STATIC BOOL
527 NCinprogress(cp, id, who)
528 CHANNEL *cp;
529 register char *id;
530 WIP **who;
531 {
532 register WIP *wp;
533 register char *p;
534 register int i;
535
536 for (i = NCwipsize, wp = NCwip; --i >= 0; wp++)
537 if ((p = wp->MessageID) != NULL && *p == *id && EQ(p, id)
538 && Now.time - wp->Timestamp < WIP_CHECK) {
539 *who = wp;
540 return TRUE;
541 }
542 wp = &NCwip[cp->fd];
543 if (wp->MessageID == NULL) {
544 for (i = SIZEOF(NCfreelist); --i >= 0; )
545 if (NCfreelist[i] != NULL) {
546 wp->MessageID = NCfreelist[i];
547 NCfreelist[i] = NULL;
548 break;
549 }
550 if (i < 0)
551 wp->MessageID = NEW(char, DBZMAXKEY + 3);
552 }
553 (void)strcpy(wp->MessageID, id);
554 return FALSE;
555 }
556
557
558 /*
559 ** The "help" command.
560 */
561 STATIC FUNCTYPE
562 NChelp(cp)
563 register CHANNEL *cp;
564 {
565 static char LINE1[] = "For more information, contact \"";
566 static char LINE2[] = "\" at this machine.";
567 register NCDISPATCH *dp;
568
569 WCHANappend(cp, NNTP_HELP_FOLLOWS,STRLEN(NNTP_HELP_FOLLOWS));
570 WCHANappend(cp, NCterm,STRLEN(NCterm));
571 for (dp = NCcommands; dp < ENDOF(NCcommands); dp++)
572 if (dp->Function != NC_unimp) {
573 if (!StreamingOff || cp->Streaming ||
574 (dp->Function != NCcheck && dp->Function != NCtakethis)) {
575 WCHANappend(cp, "\t", 1);
576 WCHANappend(cp, dp->Name, dp->Size);
577 WCHANappend(cp, NCterm, STRLEN(NCterm));
578 }
579 }
580 WCHANappend(cp, LINE1, STRLEN(LINE1));
581 WCHANappend(cp, NEWSMASTER, STRLEN(NEWSMASTER));
582 WCHANappend(cp, LINE2, STRLEN(LINE2));
583 WCHANappend(cp, NCterm, STRLEN(NCterm));
584 NCwritereply(cp, NCdot) ;
585 }
586
587
588 #if !defined(NNTP_RESENDIT_LATER)
589 /*
590 ** We woke up because we got offered an article that was already in
591 ** progress somewhere else. If the other channel finished, then we
592 ** don't want the article, otherwise let's accept it.
593 */
594 STATIC FUNCTYPE
595 NCwaitfor(cp)
596 register CHANNEL *cp;
597 {
598 WIP *who;
599
600 if (HIShavearticle(cp->Argument)) {
601 cp->Refused++;
602 NCwritereply(cp, NNTP_HAVEIT);
603 DISPOSE(cp->Argument);
604 cp->Argument = NULL;
605 }
606 else if (NCinprogress(cp, cp->Argument, &who)) {
607 who->Wanted = TRUE;
608 SCHANadd(cp, (time_t)(Now.time + WIP_CHECK / 2 + 1), (POINTER)who,
609 NCwaitfor, (POINTER)cp->Argument);
610 }
611 else {
612 cp->State = CSgetarticle;
613 NCwritereply(cp, NNTP_SENDIT);
614 DISPOSE(cp->Argument);
615 cp->Argument = NULL;
616 }
617 }
618 #endif /* !defined(NNTP_RESENDIT_LATER) */
619
620
621 /*
622 ** The "ihave" command. Check the Message-ID, and see if we want the
623 ** article or not. Set the state appropriately.
624 */
625 STATIC FUNCTYPE
626 NCihave(cp)
627 CHANNEL *cp;
628 {
629 register char *p;
630 WIP *who;
631
632 if (AmSlave) {
633 NCwritereply(cp, NCbadcommand);
634 return;
635 }
636
637 /* Snip off the Message-ID. */
638 for (p = cp->In.Data + STRLEN("ihave"); ISWHITE(*p); p++)
639 continue;
640 if (NCbadid(cp, p))
641 return;
642
643 /* compute msg id hash for pre-commit cache check */
644 NCprecomval = dbzhash(p, strlen(p));
645 NCprecompos = NCprecomval % NCPRECOMCACHESIZE;
646 NCprecomlookups++;
647
648 /* check pre-commit cache for msg id, with timeout */
649 if ((NCprecomcache[NCprecompos].MessageIDHash == NCprecomval) &&
650 ((NCprecomcache[NCprecompos].Timestamp + NCPRECOMCACHETIMEOUT) > Now.time)) {
651 NCprecomhits++;
652 cp->Refused++;
653 NCwritereply(cp, NNTP_HAVEIT);
654 }
655
656 else if (HIShavearticle(p)) {
657 cp->Refused++;
658 NCwritereply(cp, NNTP_HAVEIT);
659 }
660 else if (NCinprogress(cp, p, &who)) {
661 #if defined(NNTP_RESENDIT_LATER)
662 NCwritereply(cp, NNTP_RESENDIT_LATER);
663 #else
664 /* Somebody else is sending it to us; wait until they're done. */
665 who->Wanted = TRUE;
666 SCHANadd(cp, (time_t)(Now.time + WIP_CHECK + 1), (POINTER)who,
667 NCwaitfor, (POINTER)COPY(p));
668 /* Clear input buffer. */
669 cp->In.Used = 0;
670 #endif /* defined(NNTP_RESENDIT_LATER) */
671 }
672 else {
673 /* pre-commit msg id so that noone else offers us this for 30 seconds */
674 NCprecomcache[NCprecompos].MessageIDHash = NCprecomval;
675 NCprecomcache[NCprecompos].Timestamp = Now.time;
676 cp->State = CSgetarticle;
677 NCwritereply(cp, NNTP_SENDIT);
678 }
679 }
680
681 /* The "xbatch" command. We (ab)use WIP->Size to memorize the batch size.
682 ** Set the state appropriately.
683 */
684
685 STATIC FUNCTYPE
686 NCxbatch(cp)
687 CHANNEL *cp;
688 {
689 register char *p;
690 WIP *wp;
691
692 if (AmSlave) {
693 NCwritereply(cp, NCbadcommand);
694 return;
695 }
696
697 /* Snip off the batch size */
698 for (p = cp->In.Data + STRLEN("xbatch"); ISWHITE(*p); p++)
699 continue;
700
701 wp = &NCwip[cp->fd];
702
703 /* safety checks, unnecessary once we are running stable */
704 if (!wp)
705 syslog(L_FATAL, "NCxbatch(): oops, wp == NULL");
706 if (wp->Size) {
707 syslog(L_FATAL, "NCxbatch(): oops, wp->Size already set to %ld",
708 wp->Size);
709 }
710
711 wp->Size = atoi(p);
712 if (Tracing || cp->Tracing)
713 syslog(L_TRACE, "%s will read batch of size %ld",
714 CHANname(cp), wp->Size);
715
716 if (wp->Size <= 0) {
717 syslog(L_NOTICE, "%s got bad xbatch size %ld",
718 CHANname(cp), wp->Size);
719 NCwritereply(cp, NNTP_XBATCH_BADSIZE);
720 return;
721 }
722
723 #if 1
724 /* we prefer not to touch the buffer, NCreader() does enough magic
725 * with it
726 */
727 #else
728 /* We already know how much data will arrive, so we can allocate
729 * sufficient buffer space for the batch in advance. We allocate
730 * LOW_WATER + 1 more than needed, so that CHANreadtext() will never
731 * reallocate it.
732 */
733 if (cp->In.Size < wp->Size + LOW_WATER + 1) {
734 DISPOSE(cp->In.Data);
735 cp->In.Left = cp->In.Size = wp->Size + LOW_WATER + 1;
736 cp->In.Used = 0;
737 cp->In.Data = NEW(char, cp->In.Size);
738 }
739 #endif
740 cp->State = CSgetxbatch;
741 NCwritereply(cp, NNTP_CONT_XBATCH);
742 }
743
744 /*
745 ** The "list" command. Send the active file.
746 */
747 STATIC FUNCTYPE
748 NClist(cp)
749 register CHANNEL *cp;
750 {
751 register char *p;
752 register char *q;
753 char *trash;
754 char *end;
755
756 for (p = cp->In.Data + STRLEN("list"); ISWHITE(*p); p++)
757 continue;
758 if (caseEQ(p, "newsgroups")) {
759 trash = p = ReadInFile(_PATH_NEWSGROUPS, (struct stat *)NULL);
760 end = p + strlen(p);
761 }
762 else if (caseEQ(p, "active.times")) {
763 trash = p = ReadInFile(_PATH_ACTIVETIMES, (struct stat *)NULL);
764 end = p + strlen(p);
765 }
766 else if (*p == '\0' || (caseEQ(p, "active"))) {
767 p = ICDreadactive(&end);
768 trash = NULL;
769 }
770 else {
771 NCwritereply(cp, NCbadcommand);
772 return;
773 }
774
775 /* Loop over all lines, sending the text and \r\n. */
776 WCHANappend(cp, NNTP_LIST_FOLLOWS,STRLEN(NNTP_LIST_FOLLOWS));
777 WCHANappend(cp, NCterm, STRLEN(NCterm)) ;
778 for (; p < end && (q = strchr(p, '\n')) != NULL; p = q + 1) {
779 WCHANappend(cp, p, q - p);
780 WCHANappend(cp, NCterm, STRLEN(NCterm));
781 }
782 NCwritereply(cp, NCdot);
783 if (trash)
784 DISPOSE(trash);
785 }
786
787
788 /*
789 ** The "mode" command. Hand off the channel.
790 */
791 STATIC FUNCTYPE
792 NCmode(cp)
793 register CHANNEL *cp;
794 {
795 register char *p;
796 HANDOFF h;
797
798 /* Skip the first word, get the argument. */
799 p = cp->In.Data;
800 if (*p == 'x') p++;
801 p += STRLEN("mode");
802 for (; ISWHITE(*p); p++)
803 continue;
804
805 if (caseEQ(p, "reader"))
806 h = HOnnrpd;
807 else if (caseEQ(p, "query"))
808 h = HOnnrqd;
809 else if (caseEQ(p, "stream") &&
810 (!StreamingOff || (StreamingOff && cp->Streaming))) {
811 char buff[16];
812 (void)sprintf(buff, "%d StreamOK.", NNTP_OK_STREAM_VAL);
813 NCwritereply(cp, buff);
814 syslog(L_NOTICE, "%s NCmode \"mode stream\" received",
815 CHANname(cp));
816 return;
817 } else {
818 NCwritereply(cp, NCbadcommand);
819 return;
820 }
821 RChandoff(cp->fd, h);
822 if (NCcount > 0)
823 NCcount--;
824 CHANclose(cp, CHANname(cp));
825 }
826
827
828 /*
829 ** The "quit" command. Acknowledge, and set the state to closing down.
830 */
831 STATIC FUNCTYPE
832 NCquit(cp)
833 CHANNEL *cp;
834 {
835 register WIP *wp;
836 register int i;
837
838 wp = &NCwip[cp->fd];
839 for (i = SIZEOF(NCfreelist); --i >= 0; )
840 if (NCfreelist[i] == NULL) {
841 NCfreelist[i] = wp->MessageID;
842 wp->MessageID = NULL;
843 break;
844 }
845 #if 0
846 if (i < 0) {
847 DISPOSE(wp->MessageID);
848 wp->MessageID = NULL;
849 }
850 #endif /* 0 */
851 cp->State = CSwritegoodbye;
852 NCwritereply(cp, NNTP_GOODBYE_ACK);
853 }
854
855
856 /*
857 ** The "xpath" command. Return the paths for an article is.
858 */
859 STATIC FUNCTYPE
860 NCxpath(cp)
861 CHANNEL *cp;
862 {
863 static BUFFER Reply;
864 register char *p;
865 register int i;
866
867 /* Nip off the Message-ID. */
868 for (p = cp->In.Data + STRLEN("xpath"); ISWHITE(*p); p++)
869 continue;
870 if (NCbadid(cp, p))
871 return;
872
873 if ((p = HISfilesfor(p)) == NULL) {
874 NCwritereply(cp, NNTP_DONTHAVEIT);
875 return;
876 }
877 i = 3 + 1 + strlen(p);
878 if (Reply.Data == NULL) {
879 Reply.Size = i;
880 Reply.Data = NEW(char, i + 1);
881 }
882 else if (Reply.Size < i) {
883 Reply.Size = i;
884 RENEW(Reply.Data, char, i + 1);
885 }
886 (void)sprintf(Reply.Data, "%d %s", NNTP_NOTHING_FOLLOWS_VAL, p);
887 NCwritereply(cp, Reply.Data);
888 }
889
890
891 /*
892 ** The "xreplic" command. Take an article and the places to file it.
893 */
894 STATIC FUNCTYPE
895 NCxreplic(cp)
896 CHANNEL *cp;
897 {
898 register char *p;
899 register BUFFER *bp;
900 register int i;
901
902 if (!RCismaster(cp->Address)) {
903 NCwritereply(cp, NCbadcommand);
904 return;
905 }
906
907 /* Stash the filename arguments. */
908 for (p = cp->In.Data + STRLEN("xreplic"); ISWHITE(*p); p++)
909 continue;
910 i = cp->In.Used - (p - cp->In.Data) + 1;
911 bp = &NCwip[cp->fd].Replic;
912 if (bp->Data == NULL) {
913 bp->Size = i;
914 bp->Data = NEW(char, i);
915 }
916 BUFFset(bp, p, i);
917 bp->Used = bp->Left;
918
919 /* Tell master to send it to us. */
920 cp->State = CSgetrep;
921 NCwritereply(cp, NNTP_SENDIT);
922 }
923
924
925 /*
926 ** The catch-all for inimplemented commands.
927 */
928 STATIC FUNCTYPE
929 NC_unimp(cp)
930 CHANNEL *cp;
931 {
932 register char *p;
933 char buff[SMBUF];
934
935 /* Nip off the first word. */
936 for (p = cp->In.Data; *p && !ISWHITE(*p); p++)
937 continue;
938 *p = '\0';
939 (void)sprintf(buff, "%d \"%s\" not implemented; try \"help\".",
940 NNTP_BAD_COMMAND_VAL, MaxLength(cp->In.Data, cp->In.Data));
941 NCwritereply(cp, buff);
942 }
943
944
945
946 /*
947 ** Remove the \r\n and leading dot escape that the NNTP protocol adds.
948 */
949 STATIC void
950 NCclean(bp)
951 register BUFFER *bp;
952 {
953 register char *end;
954 register char *p;
955 register char *dest;
956
957 for (p = bp->Data, dest = p, end = p + bp->Used; p < end; ) {
958 if (p[0] == '\r' && p[1] == '\n') {
959 p += 2;
960 *dest++ = '\n';
961 if (p[0] == '.' && p[1] == '.') {
962 p += 2;
963 *dest++ = '.';
964 }
965 }
966 else
967 *dest++ = *p++;
968 }
969 *dest = '\0';
970 bp->Used = dest - bp->Data;
971 }
972
973
974 /*
975 ** Check whatever data is available on the channel. If we got the
976 ** full amount (i.e., the command or the whole article) process it.
977 */
978 STATIC FUNCTYPE
979 NCproc(cp)
980 register CHANNEL *cp;
981 {
982 register char *p;
983 register NCDISPATCH *dp;
984 register BUFFER *bp;
985 register WIP *wp;
986 STRING q;
987 char buff[SMBUF];
988 char *av[2];
989 int i;
990
991 if (Tracing || cp->Tracing)
992 syslog(L_TRACE, "%s NCproc Used=%ld",
993 CHANname(cp), cp->In.Used);
994
995 bp = &cp->In;
996 if (bp->Used == 0) return;
997
998 p = &bp->Data[bp->Used];
999 wp = &NCwip[cp->fd];
1000 for ( ; ; ) {
1001 cp->Rest = 0;
1002 cp->SaveUsed = bp->Used;
1003 if (Tracing || cp->Tracing)
1004 if (bp->Used > 15)
1005 syslog(L_TRACE, "%s NCproc state=%d next \"%.15s\"",
1006 CHANname(cp), cp->State, bp->Data);
1007 switch (cp->State) {
1008 default:
1009 syslog(L_ERROR, "%s internal NCproc state %d",
1010 CHANname(cp), cp->State);
1011 break;
1012
1013 case CSgetcmd:
1014 case CSgetauth:
1015 /* Did we get the whole command, terminated with "\r\n"? */
1016 for (i = 0; (i < bp->Used) && (bp->Data[i] != '\n'); i++) ;
1017 if (i < bp->Used) cp->Rest = bp->Used = ++i;
1018 else {
1019 cp->Rest = 0;
1020 break; /* come back later for rest of line */
1021 }
1022 if (cp->Rest < 2) break;
1023 p = &bp->Data[cp->Rest];
1024 if (p[-2] != '\r' || p[-1] != '\n') { /* probably in an article */
1025 int j;
1026
1027 syslog(L_NOTICE, "%s bad_command %s",
1028 CHANname(cp), MaxLength(bp->Data, bp->Data));
1029 if (++(cp->BadCommands) >= BAD_COMMAND_COUNT) {
1030 cp->State = CSwritegoodbye;
1031 NCwritereply(cp, NCbadcommand);
1032 cp->Rest = cp->SaveUsed;
1033 break;
1034 } else
1035 NCwritereply(cp, NCbadcommand);
1036 for (j = i + 1; j < cp->SaveUsed; j++)
1037 if (bp->Data[j] == '\n') {
1038 if (bp->Data[j - 1] == '\r') break;
1039 else cp->Rest = bp->Used = j + 1;
1040 }
1041 break;
1042 }
1043 p[-2] = '\0';
1044 bp->Used -= 2;
1045
1046 /* Ignore blank lines. */
1047 if (bp->Data[0] == '\0')
1048 break;
1049 if (Tracing || cp->Tracing)
1050 syslog(L_TRACE, "%s < %s", CHANname(cp), bp->Data);
1051
1052 /* We got something -- stop sleeping (in case we were). */
1053 SCHANremove(cp);
1054 if (cp->Argument != NULL) {
1055 DISPOSE(cp->Argument);
1056 cp->Argument = NULL;
1057 }
1058
1059 if (cp->State == CSgetauth) {
1060 if (caseEQn(bp->Data, "mode", 4) ||
1061 caseEQn(bp->Data, "xmode", 5))
1062 NCmode(cp);
1063 else
1064 NCauthinfo(cp);
1065 break;
1066 }
1067
1068 /* Loop through the command table. */
1069 for (p = bp->Data, dp = NCcommands; dp < ENDOF(NCcommands); dp++)
1070 if (caseEQn(p, dp->Name, dp->Size)) {
1071 /* ignore the streaming commands if necessary. */
1072 if (!StreamingOff || cp->Streaming ||
1073 (dp->Function != NCcheck && dp->Function != NCtakethis)) {
1074 (*dp->Function)(cp);
1075 cp->BadCommands = 0;
1076 break;
1077 }
1078 }
1079
1080 if (dp == ENDOF(NCcommands)) {
1081 if (++(cp->BadCommands) >= BAD_COMMAND_COUNT) {
1082 cp->State = CSwritegoodbye;
1083 NCwritereply(cp, NCbadcommand);
1084 cp->Rest = cp->SaveUsed;
1085 } else
1086 NCwritereply(cp, NCbadcommand);
1087
1088 /* Channel could have been freed by above NCwritereply if
1089 we're writing-goodbye */
1090 if (cp->Type == CTfree) {
1091 return;
1092 }
1093
1094 for (i = 0; (p = NCquietlist[i]) != NULL; i++)
1095 if (caseEQ(p, bp->Data))
1096 break;
1097 if (p == NULL)
1098 syslog(L_NOTICE, "%s bad_command %s",
1099 CHANname(cp), MaxLength(bp->Data, bp->Data));
1100 }
1101 break;
1102
1103 case CSgetarticle:
1104 case CSgetrep:
1105 /* Check for the null article. */
1106 if ((bp->Used >= 3) && (bp->Data[0] == '.')
1107 && (bp->Data[1] == '\r') && (bp->Data[2] == '\n')) {
1108 cp->Rest = 3; /* null article (canceled?) */
1109 cp->Rejected++;
1110 #if 0
1111 syslog(L_NOTICE, "%s empty article", CHANname(cp)) ;
1112 #endif
1113 cp->State = CSgetcmd;
1114 if (cp->Sendid.Size > 3) { /* We be streaming */
1115 char buff[4];
1116 (void)sprintf(buff, "%d", NNTP_ERR_FAILID_VAL);
1117 cp->Sendid.Data[0] = buff[0];
1118 cp->Sendid.Data[1] = buff[1];
1119 cp->Sendid.Data[2] = buff[2];
1120 NCwritereply(cp, cp->Sendid.Data);
1121 }
1122 else NCwritereply(cp, NNTP_REJECTIT_EMPTY);
1123 bp->Used = 0;
1124
1125 /* Clear the work-in-progress entry. */
1126 NCclearwip(wp);
1127 break;
1128 }
1129 /* Reading an article; look for "\r\n.\r\n" terminator. */
1130 if (cp->Lastch > 5) i = cp->Lastch; /* only look at new data */
1131 else i = 5;
1132 for ( ; i <= bp->Used; i++) {
1133 if ((bp->Data[i - 5] == '\r')
1134 && (bp->Data[i - 4] == '\n')
1135 && (bp->Data[i - 3] == '.')
1136 && (bp->Data[i - 2] == '\r')
1137 && (bp->Data[i - 1] == '\n')) {
1138 cp->Rest = bp->Used = i;
1139 p = &bp->Data[i];
1140 break;
1141 }
1142 }
1143 cp->Lastch = i;
1144 if (i > bp->Used) { /* did not find terminator */
1145 /* Check for big articles. */
1146 if (LargestArticle > SAVE_AMT && bp->Used > LargestArticle) {
1147 /* Make some room, saving only the last few bytes. */
1148 for (p = bp->Data, i = 0; i < SAVE_AMT; p++, i++)
1149 p[0] = p[bp->Used - SAVE_AMT];
1150 wp->Size += bp->Used - SAVE_AMT;
1151 bp->Used = cp->Lastch = SAVE_AMT;
1152 cp->State = CSeatarticle;
1153 }
1154 cp->Rest = 0;
1155 break;
1156 }
1157 if (Mode == OMpaused) { /* defer processing while paused */
1158 cp->Rest = 0;
1159 bp->Used = cp->SaveUsed;
1160 SCHANadd(cp, (time_t)(Now.time + PAUSE_RETRY_TIME),
1161 (POINTER)&Mode, NCproc, (POINTER)NULL);
1162 return;
1163 }
1164
1165 /* Strip article terminator and post the article. */
1166 p[-3] = '\0';
1167 bp->Used -= 2;
1168 SCHANremove(cp);
1169 if (cp->Argument != NULL) {
1170 DISPOSE(cp->Argument);
1171 cp->Argument = NULL;
1172 }
1173 NCclean(bp);
1174 NCpostit(cp);
1175 cp->State = CSgetcmd;
1176 break;
1177
1178 case CSeatarticle:
1179 /* Eat the article and then complain that it was too large */
1180 /* Reading an article; look for "\r\n.\r\n" terminator. */
1181 if (cp->Lastch > 5) i = cp->Lastch; /* only look at new data */
1182 else i = 5;
1183 for ( ; i <= bp->Used; i++) {
1184 if ((bp->Data[i - 5] == '\r')
1185 && (bp->Data[i - 4] == '\n')
1186 && (bp->Data[i - 3] == '.')
1187 && (bp->Data[i - 2] == '\r')
1188 && (bp->Data[i - 1] == '\n')) {
1189 cp->Rest = bp->Used = i;
1190 p = &bp->Data[i];
1191 break;
1192 }
1193 }
1194 if (i <= bp->Used) { /* did find terminator */
1195 /* Reached the end of the article. */
1196 SCHANremove(cp);
1197 if (cp->Argument != NULL) {
1198 DISPOSE(cp->Argument);
1199 cp->Argument = NULL;
1200 }
1201 p = wp->MessageID;
1202 i = wp->Size + bp->Used;
1203 syslog(L_ERROR, "%s internal rejecting huge article %s (%d > %ld)",
1204 CHANname(cp), p ? p : "(null)", i, LargestArticle);
1205 (void)sprintf(buff, "%d Article exceeds local limit of %ld bytes",
1206 NNTP_REJECTIT_VAL, LargestArticle);
1207 cp->State = CSgetcmd;
1208 if (cp->Sendid.Size) NCwritereply(cp, cp->Sendid.Data);
1209 else NCwritereply(cp, buff);
1210 cp->Rejected++;
1211
1212 /* Write a local cancel entry so nobody else gives it to us. */
1213 if (p) {
1214 av[0] = p;
1215 av[1] = NULL;
1216 if ((q = CCcancel(av)) != NULL)
1217 syslog(L_ERROR, "%s cant cancel %s %s", LogName, av[0], q);
1218 }
1219
1220 /* Clear the work-in-progress entry. */
1221 NCclearwip(wp);
1222
1223 /*
1224 * only free and allocate the buffer back to
1225 * START_BUFF_SIZE if there's nothing in the buffer we
1226 * need to save (i.e., following commands.
1227 * if there is, then we're probably in streaming mode,
1228 * so probably not much point in trying to keep the
1229 * buffers minimal anyway...
1230 */
1231 if (bp->Used == cp->SaveUsed) {
1232 /* Reset input buffer to the default size; don't let realloc
1233 * be lazy. */
1234 DISPOSE(bp->Data);
1235 bp->Size = START_BUFF_SIZE;
1236 bp->Used = 0;
1237 bp->Data = NEW(char, bp->Size);
1238 cp->SaveUsed = cp->Rest = cp->Lastch = 0;
1239 }
1240 }
1241 else if (bp->Used > 8 * 1024) {
1242 /* Make some room; save the last few bytes of the article */
1243 for (p = bp->Data, i = 0; i < SAVE_AMT; p++, i++)
1244 p[0] = p[bp->Used - SAVE_AMT + 0];
1245 wp->Size += bp->Used - SAVE_AMT;
1246 bp->Used = cp->Lastch = SAVE_AMT;
1247 cp->Rest = 0;
1248 }
1249 break;
1250 case CSgetxbatch:
1251 /* if the batch is complete, write it out into the in.coming
1252 * directory with an unique timestamp, and start rnews on it.
1253 */
1254 if (Tracing || cp->Tracing)
1255 syslog(L_TRACE, "%s CSgetxbatch: now %ld of %ld bytes",
1256 CHANname(cp), bp->Used, wp->Size);
1257
1258 if (bp->Used < wp->Size) {
1259 cp->Rest = 0;
1260 break; /* give us more data */
1261 }
1262 cp->Rest = wp->Size;
1263
1264 /* now do something with the batch */
1265 {
1266 char buff[SMBUF], buff2[SMBUF];
1267 int fd, oerrno, failed;
1268 long now;
1269
1270 now = time(NULL);
1271 failed = 0;
1272 /* time+channel file descriptor should make an unique file name */
1273 sprintf(buff, "%s/%ld%d.tmp", _PATH_XBATCHES, now, cp->fd);
1274 fd = open(buff, O_WRONLY|O_CREAT|O_EXCL, ARTFILE_MODE);
1275 if (fd < 0) {
1276 oerrno = errno;
1277 failed = 1;
1278 syslog(L_ERROR, "%s cannot open outfile %s for xbatch: %m",
1279 CHANname(cp), buff);
1280 sprintf(buff, "%s cant create file: %s",
1281 NNTP_RESENDIT_XBATCHERR, strerror(oerrno));
1282 NCwritereply(cp, buff);
1283 } else {
1284 if (write(fd, cp->In.Data, wp->Size) != wp->Size) {
1285 oerrno = errno;
1286 syslog(L_ERROR, "%s cant write batch to file %s: %m",
1287 CHANname(cp), buff);
1288 sprintf(buff, "%s cant write batch to file: %s",
1289 NNTP_RESENDIT_XBATCHERR, strerror(oerrno));
1290 NCwritereply(cp, buff);
1291 failed = 1;
1292 }
1293 }
1294 if (fd >= 0 && close(fd) != 0) {
1295 oerrno = errno;
1296 syslog(L_ERROR, "%s error closing batch file %s: %m",
1297 CHANname(cp), failed ? "" : buff);
1298 sprintf(buff, "%s error closing batch file: %s",
1299 NNTP_RESENDIT_XBATCHERR, strerror(oerrno));
1300 NCwritereply(cp, buff);
1301 failed = 1;
1302 }
1303 sprintf(buff2, "%s/%ld%d.x", _PATH_XBATCHES, now, cp->fd);
1304 if (rename(buff, buff2)) {
1305 oerrno = errno;
1306 syslog(L_ERROR, "%s cant rename %s to %s: %m",
1307 CHANname(cp), failed ? "" : buff, buff2);
1308 sprintf(buff, "%s cant rename batch to %s: %s",
1309 NNTP_RESENDIT_XBATCHERR, buff2, strerror(oerrno));
1310 NCwritereply(cp, buff);
1311 failed = 1;
1312 }
1313 cp->Reported++;
1314 if (!failed) {
1315 NCwritereply(cp, NNTP_OK_XBATCHED);
1316 cp->Received++;
1317 } else
1318 cp->Rejected++;
1319 }
1320 syslog(L_NOTICE, "%s accepted batch size %ld", CHANname(cp), wp->Size);
1321
1322 /* Clear the work-in-progress entry. */
1323 NCclearwip(wp);
1324
1325 #if 1
1326 /* leave the input buffer as it is, there is a fair chance
1327 * that we will get another xbatch on this channel.
1328 * The buffer will finally be disposed at connection shutdown.
1329 */
1330 #else
1331 /* Reset input buffer to the default size; don't let realloc
1332 * be lazy. */
1333 DISPOSE(bp->Data);
1334 bp->Size = START_BUFF_SIZE;
1335 bp->Used = 0;
1336 bp->Data = NEW(char, bp->Size);
1337 #endif
1338 break;
1339 }
1340 if (Tracing || cp->Tracing)
1341 syslog(L_TRACE, "%s NCproc Rest=%d Used=%ld SaveUsed=%d",
1342 CHANname(cp), cp->Rest, bp->Used, cp->SaveUsed);
1343
1344 if (cp->Rest > 0) {
1345 if (cp->Rest < cp->SaveUsed) { /* more commands in buffer */
1346 bp->Used = cp->SaveUsed = cp->SaveUsed - cp->Rest;
1347 /* It would be nice to avoid this copy but that
1348 ** would require changes to the bp structure and
1349 ** the way it is used.
1350 */
1351 (void)memcpy((POINTER)bp->Data, (POINTER)&bp->Data[cp->Rest], (SIZE_T)bp->Used);
1352 cp->Rest = cp->Lastch = 0;
1353 } else {
1354 bp->Used = cp->Lastch = 0;
1355 break;
1356 }
1357 } else break;
1358 }
1359 }
1360
1361
1362 /*
1363 ** Read whatever data is available on the channel. If we got the
1364 ** full amount (i.e., the command or the whole article) process it.
1365 */
1366 STATIC FUNCTYPE
1367 NCreader(cp)
1368 register CHANNEL *cp;
1369 {
1370 register WIP *wp;
1371 int i;
1372
1373 if (Tracing || cp->Tracing)
1374 syslog(L_TRACE, "%s NCreader Used=%ld",
1375 CHANname(cp), cp->In.Used);
1376
1377 /* Read any data that's there; ignore errors (retry next time it's our
1378 * turn) and if we got nothing, then it's EOF so mark it closed. */
1379 if ((i = CHANreadtext(cp)) <= 0) {
1380 #ifdef POLL_BUG
1381 /* return of -2 indicates EAGAIN, for SUNOS5.4 poll() bug workaround */
1382 if (i == -2) {
1383 return;
1384 }
1385 #endif
1386 if (i == 0 || cp->BadReads++ >= BAD_IO_COUNT) {
1387 if (NCcount > 0)
1388 NCcount--;
1389 CHANclose(cp, CHANname(cp));
1390 }
1391 return;
1392 }
1393
1394 /* Update timestamp. */
1395 wp = &NCwip[cp->fd];
1396 wp->Timestamp = Now.time;
1397
1398 NCproc(cp); /* check and process data */
1399 }
1400
1401
1402
1403 /*
1404 ** Set up the NNTP channel state.
1405 */
1406 void
1407 NCsetup(i)
1408 register int i;
1409 {
1410 register WIP *wp;
1411 register NCDISPATCH *dp;
1412 char *p;
1413 char buff[SMBUF];
1414
1415 /* Set the greeting message. */
1416 if ((p = GetConfigValue(_CONF_PATHHOST)) == NULL)
1417 /* Worked in main, now it fails? Curious. */
1418 p = Path.Data;
1419 (void)sprintf(buff, "%d %s InterNetNews server %s ready",
1420 NNTP_POSTOK_VAL, p, Version);
1421 NCgreeting = COPY(buff);
1422
1423 /* Set up the work-in-progress structure. */
1424 for (wp = NCwip = NEW(WIP, i), NCwipsize = i; --i >= 0; wp++)
1425 *wp = NCnullwip;
1426
1427 /* Get the length of every command. */
1428 for (dp = NCcommands; dp < ENDOF(NCcommands); dp++)
1429 dp->Size = strlen(dp->Name);
1430 }
1431
1432
1433 /*
1434 ** Tear down our state.
1435 */
1436 void
1437 NCclose()
1438 {
1439 register WIP *wp;
1440 register int i;
1441 register CHANNEL *cp;
1442 int j;
1443
1444 /* Close all incoming channels. */
1445 for (j = 0; (cp = CHANiter(&j, CTnntp)) != NULL; ) {
1446 if (NCcount > 0)
1447 NCcount--;
1448 CHANclose(cp, CHANname(cp));
1449 }
1450
1451 /* Free the WIP list. */
1452 for (wp = NCwip, i = NCwipsize; --i >= 0; wp++) {
1453 if (wp->MessageID)
1454 DISPOSE(wp->MessageID);
1455 if (wp->Replic.Data)
1456 DISPOSE(wp->Replic.Data);
1457 }
1458 DISPOSE(NCwip);
1459 for (i = SIZEOF(NCfreelist); --i >= 0; )
1460 if (NCfreelist[i] != NULL)
1461 DISPOSE(NCfreelist[i]);
1462 }
1463
1464
1465 /*
1466 ** Create an NNTP channel and print the greeting message.
1467 */
1468 CHANNEL *
1469 NCcreate(fd, MustAuthorize, IsLocal)
1470 int fd;
1471 BOOL MustAuthorize;
1472 BOOL IsLocal;
1473 {
1474 register CHANNEL *cp;
1475 int i;
1476
1477 /* one-time initialization of pre-commit cache */
1478 if (!NCprecominit) {
1479 NCprecomclear();
1480 NCprecominit = 1;
1481 syslog(L_NOTICE, "Pre-commit cache initialized: %ld entries, %ld bytes",
1482 NCPRECOMCACHESIZE, (long) sizeof(NCprecomcache));
1483 }
1484
1485 /* Create the channel. */
1486 cp = CHANcreate(fd, CTnntp, MustAuthorize ? CSgetauth : CSgetcmd,
1487 NCreader, NCwritedone);
1488
1489 /* check for wip overflow */
1490 if(fd > NCwipsize) {
1491 NCwriteshutdown(cp, "too many file descriptors");
1492 return cp;
1493 }
1494
1495 NCclearwip(&NCwip[cp->fd]);
1496 #if defined(SOL_SOCKET) && defined(SO_SNDBUF) && defined(SO_RCVBUF)
1497 #if defined (DO_SET_SOCKOPT)
1498 i = 24 * 1024;
1499 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&i, sizeof i) < 0)
1500 syslog(L_ERROR, "%s cant setsockopt(SNDBUF) %m", CHANname(cp));
1501 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&i, sizeof i) < 0)
1502 syslog(L_ERROR, "%s cant setsockopt(RCVBUF) %m", CHANname(cp));
1503 #endif
1504 #endif /* defined(SOL_SOCKET) && defined(SO_SNDBUF) && defined(SO_RCVBUF) */
1505
1506 #if defined(SOL_SOCKET) && defined(SO_KEEPALIVE)
1507 #if defined (DO_SET_SOCKOPT)
1508 /* Set KEEPALIVE to catch broken socket connections. */
1509 i = 1;
1510 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof i) < 0)
1511 syslog(L_ERROR, "%s cant setsockopt(KEEPALIVE) %m", CHANname(cp));
1512 #endif
1513 #endif /* defined(SOL_SOCKET) && defined(SO_KEEPALIVE) */
1514
1515 /* Now check our operating mode. */
1516 NCcount++;
1517 if (Mode == OMthrottled) {
1518 NCwriteshutdown(cp, ModeReason);
1519 return cp;
1520 }
1521 if (RejectReason) {
1522 NCwriteshutdown(cp, RejectReason);
1523 return cp;
1524 }
1525
1526 /* See if we have too many channels. */
1527 if (!IsLocal && MaxIncoming && NCcount >= MaxIncoming && !RCnolimit(cp)) {
1528 /* Recount, just in case we got out of sync. */
1529 for (NCcount = 0, i = 0; CHANiter(&i, CTnntp) != NULL; )
1530 NCcount++;
1531 if (NCcount >= MaxIncoming) {
1532 NCwriteshutdown(cp, "Too many connections");
1533 return cp;
1534 }
1535 }
1536 cp->BadReads = 0;
1537 cp->BadCommands = 0;
1538 NCwritereply(cp, NCgreeting);
1539 return cp;
1540 }
1541
1542
1543
1544 /* These modules support the streaming option to tranfer articles
1545 ** faster.
1546 */
1547
1548 /*
1549 ** The "check" command. Check the Message-ID, and see if we want the
1550 ** article or not. Stay in command state.
1551 */
1552 STATIC FUNCTYPE
1553 NCcheck(cp)
1554 CHANNEL *cp;
1555 {
1556 register char *p;
1557 int msglen;
1558 WIP *who;
1559
1560 #if defined(DO_PERL)
1561 char *perlrc;
1562 #endif /* DO_PERL */
1563
1564
1565
1566 if (AmSlave) {
1567 NCwritereply(cp, NCbadcommand);
1568 return;
1569 }
1570
1571 /* Snip off the Message-ID. */
1572 for (p = cp->In.Data; !ISWHITE(*p); p++)
1573 continue;
1574 for ( ; ISWHITE(*p); p++)
1575 continue;
1576 msglen = strlen(p) + 5; /* 3 digits + space + id + null */
1577 if (cp->Sendid.Size < msglen) {
1578 if (cp->Sendid.Size > 0) DISPOSE(cp->Sendid.Data);
1579 if (msglen > MAXHEADERSIZE) cp->Sendid.Size = msglen;
1580 else cp->Sendid.Size = MAXHEADERSIZE;
1581 cp->Sendid.Data = NEW(char, cp->Sendid.Size);
1582 }
1583 if (!ARTidok(p)) {
1584 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
1585 NCwritereply(cp, cp->Sendid.Data);
1586 syslog(L_NOTICE, "%s bad_messageid %s", CHANname(cp), MaxLength(p, p));
1587 return;
1588 }
1589
1590 #if defined(DO_PERL)
1591 /* invoke a perl message filter on the message id */
1592 else if ((perlrc = HandleMessageID(p)) != NULL) {
1593 cp->Refused++;
1594 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
1595 NCwritereply(cp, cp->Sendid.Data);
1596 }
1597 #endif
1598
1599 /* compute msg id hash for pre-commit cache check */
1600 NCprecomval = dbzhash(p, msglen - 5);
1601 NCprecompos = NCprecomval % NCPRECOMCACHESIZE;
1602 NCprecomlookups++;
1603
1604 /* check pre-commit cache for msg id, with timeout */
1605 if ((NCprecomcache[NCprecompos].MessageIDHash == NCprecomval) &&
1606 ((NCprecomcache[NCprecompos].Timestamp + NCPRECOMCACHETIMEOUT) > Now.time)) {
1607 NCprecomhits++;
1608
1609 #ifdef NCPRECOM_DEFER
1610 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_RESENDID_VAL, p);
1611 #else
1612 cp->Refused++;
1613 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
1614 #endif
1615 NCwritereply(cp, cp->Sendid.Data);
1616 }
1617
1618 /* check history for msg id existence */
1619 else if (HIShavearticle(p)) {
1620 cp->Refused++;
1621 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_GOTID_VAL, p);
1622 NCwritereply(cp, cp->Sendid.Data);
1623
1624 /* check to see if article's already in progress on some other channel */
1625 } else if (NCinprogress(cp, p, &who)) {
1626 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_RESENDID_VAL, p);
1627 NCwritereply(cp, cp->Sendid.Data);
1628
1629 /* otherwise, we don't have the message, tell remote to send it to us */
1630 } else {
1631 /* pre-commit msg id so that noone else offers us this for 30 seconds */
1632 NCprecomcache[NCprecompos].MessageIDHash = NCprecomval;
1633 NCprecomcache[NCprecompos].Timestamp = Now.time;
1634
1635 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_OK_SENDID_VAL, p);
1636 NCwritereply(cp, cp->Sendid.Data);
1637 }
1638 /* stay in command mode */
1639 }
1640
1641 /*
1642 ** The "takethis" command. Article follows.
1643 ** Remember <id> for later ack.
1644 */
1645 STATIC FUNCTYPE
1646 NCtakethis(cp)
1647 CHANNEL *cp;
1648 {
1649 register char *p;
1650 int msglen;
1651 register WIP *wp;
1652 register int i;
1653
1654 /* Snip off the Message-ID. */
1655 for (p = cp->In.Data + STRLEN("takethis"); ISWHITE(*p); p++)
1656 continue;
1657 for ( ; ISWHITE(*p); p++)
1658 continue;
1659
1660 msglen = strlen(p) + 5; /* 3 digits + space + id + null */
1661
1662 if (!ARTidok(p)) {
1663 syslog(L_NOTICE, "%s bad_messageid %s", CHANname(cp), MaxLength(p, p));
1664 }
1665
1666 /* pre-commit msg id so that noone else offers us this for 30 seconds */
1667 else {
1668 NCprecomval = dbzhash(p, msglen - 5);
1669 NCprecompos = NCprecomval % NCPRECOMCACHESIZE;
1670 NCprecomcache[NCprecompos].MessageIDHash = NCprecomval;
1671 NCprecomcache[NCprecompos].Timestamp = Now.time;
1672 }
1673
1674 if (cp->Sendid.Size < msglen) {
1675 if (cp->Sendid.Size > 0) DISPOSE(cp->Sendid.Data);
1676 if (msglen > MAXHEADERSIZE) cp->Sendid.Size = msglen;
1677 else cp->Sendid.Size = MAXHEADERSIZE;
1678 cp->Sendid.Data = NEW(char, cp->Sendid.Size);
1679 }
1680 /* save ID for later NACK or ACK */
1681 (void)sprintf(cp->Sendid.Data, "%d %s", NNTP_ERR_FAILID_VAL, p);
1682
1683 cp->State = CSgetarticle;
1684 /* set wp->MessageID for benefit of later code in NCreader
1685 * (especially while in CSeatarticle state)
1686 */
1687 wp = &NCwip[cp->fd];
1688 if (wp->MessageID == NULL) {
1689 for (i = SIZEOF(NCfreelist); --i >= 0; )
1690 if (NCfreelist[i] != NULL) {
1691 wp->MessageID = NCfreelist[i];
1692 NCfreelist[i] = NULL;
1693 break;
1694 }
1695 if (i < 0)
1696 wp->MessageID = NEW(char, DBZMAXKEY + 3);
1697 }
1698 (void)strcpy(wp->MessageID, p);
1699 }