Debian Codegraph @

nnrpd/post.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.32 $
2 **
3 ** Check article, send it to the local server.
4 */
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include "configdata.h"
8 #include "clibrary.h"
9 #include "nnrpd.h"
10 #if defined(DO_NEED_TIME)
11 #include <time.h>
12 #endif /* defined(DO_NEED_TIME) */
13 #include <sys/time.h>
14
15 #include "post.h"
16
17 #define FLUSH_ERROR(F) (fflush((F)) == EOF || ferror((F)))
18 #define HEADER_DELTA 20
19
20 STATIC char *tmpPtr ;
21 STATIC char Error[SMBUF];
22 STATIC char NGSEPS[] = NG_SEPARATOR;
23 char **OtherHeaders;
24 int OtherCount;
25 STATIC int OtherSize;
26 STATIC BOOL WasMailed;
27 STATIC STRING BadDistribs[] = {
28 BAD_DISTRIBS
29 };
30
31 HEADER Table[] = {
32 /* Name Canset Type */
33 { "Path", TRUE, HTstd },
34 #define _path 0
35 { "From", TRUE, HTreq },
36 #define _from 1
37 { "Newsgroups", TRUE, HTreq },
38 #define _newsgroups 2
39 { "Subject", TRUE, HTreq },
40 #define _subject 3
41 { "Control", TRUE, HTstd },
42 #define _control 4
43 { "Supersedes", TRUE, HTstd },
44 #define _supersedes 5
45 { "Followup-To", TRUE, HTstd },
46 #define _followupto 6
47 { "Date", TRUE, HTstd },
48 #define _date 7
49 { "Organization", TRUE, HTstd },
50 #define _organization 8
51 { "Lines", TRUE, HTstd },
52 #define _lines 9
53 { "Sender", TRUE, HTstd },
54 #define _sender 10
55 { "Approved", TRUE, HTstd },
56 #define _approved 11
57 { "Distribution", TRUE, HTstd },
58 #define _distribution 12
59 { "Expires", TRUE, HTstd },
60 #define _expires 13
61 { "Message-ID", TRUE, HTstd },
62 #define _messageid 14
63 { "References", TRUE, HTstd },
64 #define _references 15
65 { "Reply-To", TRUE, HTstd },
66 #define _replyto 16
67 { "NNTP-Posting-Host", FALSE, HTstd },
68 #define _nntpposthost 17
69 { "Mime-Version", TRUE, HTstd },
70 #define _mimeversion 18
71 { "Content-Type", TRUE, HTstd },
72 #define _contenttype 19
73 { "Content-Transfer-Encoding", TRUE, HTstd },
74 #define _contenttransferencoding 20
75 { "X-Trace", FALSE, HTstd },
76 #define _xtrace 21
77 { "NNTP-Posting-Date", FALSE, HTstd },
78 { "Xref", FALSE, HTstd },
79 { "Summary", TRUE, HTstd },
80 { "Keywords", TRUE, HTstd },
81 { "Date-Received", FALSE, HTobs },
82 { "Received", FALSE, HTobs },
83 { "Posted", FALSE, HTobs },
84 { "Posting-Version", FALSE, HTobs },
85 { "Relay-Version", FALSE, HTobs },
86 };
87
88 HEADER *EndOfTable = ENDOF(Table);
89
90
91
92 /* Join() and MaxLength() are taken from innd.c */
93 /*
94 ** Turn any \r or \n in text into spaces. Used to splice back multi-line
95 ** headers into a single line.
96 */
97 STATIC char *
98 Join(text)
99 register char *text;
100 {
101 register char *p;
102
103 for (p = text; *p; p++)
104 if (*p == '\n' || *p == '\r')
105 *p = ' ';
106 return text;
107 }
108
109 /*
110 ** Return a short name that won't overrun our bufer or syslog's buffer.
111 ** q should either be p, or point into p where the "interesting" part is.
112 */
113 char *
114 MaxLength(p, q)
115 char *p;
116 char *q;
117 {
118 static char buff[80];
119 register int i;
120
121 /* Already short enough? */
122 i = strlen(p);
123 if (i < sizeof buff - 1)
124 return Join(p);
125
126 /* Simple case of just want the begining? */
127 if (q - p < sizeof buff - 4) {
128 (void)strncpy(buff, p, sizeof buff - 4);
129 (void)strcpy(&buff[sizeof buff - 4], "...");
130 }
131 /* Is getting last 10 characters good enough? */
132 else if ((p + i) - q < 10) {
133 (void)strncpy(buff, p, sizeof buff - 14);
134 (void)strcpy(&buff[sizeof buff - 14], "...");
135 (void)strcpy(&buff[sizeof buff - 11], &p[i - 10]);
136 }
137 else {
138 /* Not in last 10 bytes, so use double elipses. */
139 (void)strncpy(buff, p, sizeof buff - 17);
140 (void)strcpy(&buff[sizeof buff - 17], "...");
141 (void)strncpy(&buff[sizeof buff - 14], &q[-5], 10);
142 (void)strcpy(&buff[sizeof buff - 4], "...");
143 }
144 return Join(buff);
145 }
146 /*
147 ** Trim trailing spaces, return pointer to first non-space char.
148 */
149 STATIC char *
150 TrimSpaces(p)
151 register char *p;
152 {
153 register char *start;
154
155 for (start = p; ISWHITE(*start); start++)
156 continue;
157 for (p = start + strlen(start); p > start && CTYPE(isspace, p[-1]); )
158 *--p = '\0';
159 return start;
160 }
161
162
163 /*
164 ** Mark the end of the header starting at p, and return a pointer
165 ** to the start of the next one or NULL. Handles continuations.
166 */
167 STATIC char *
168 NextHeader(p)
169 register char *p;
170 {
171 for ( ; (p = strchr(p, '\n')) != NULL; p++) {
172 if (ISWHITE(p[1]))
173 continue;
174 *p = '\0';
175 return p + 1;
176 }
177 return NULL;
178 }
179
180
181 /*
182 ** Strip any headers off the article and dump them into the table.
183 ** On error, return NULL and fill in Error.
184 */
185 STATIC char *
186 StripOffHeaders(article)
187 char *article;
188 {
189 register char *p;
190 register char *q;
191 register HEADER *hp;
192 register char c;
193
194 /* Scan through buffer, a header at a time. */
195 for (p = article; ; ) {
196
197 /* See if it's a known header. */
198 c = CTYPE(islower, *p) ? toupper(*p) : *p;
199 for (hp = Table; hp < ENDOF(Table); hp++)
200 if (c == hp->Name[0]
201 && p[hp->Size] == ':'
202 && caseEQn(p, hp->Name, hp->Size)) {
203 if (hp->Type == HTobs) {
204 (void)sprintf(Error, "Obsolete \"%s\" header", hp->Name);
205 return NULL;
206 }
207 if (hp->Value) {
208 (void)sprintf(Error, "Duplicate \"%s\" header", hp->Name);
209 return NULL;
210 }
211 for (q = &p[hp->Size + 1]; ISWHITE(*q); q++)
212 continue;
213 hp->Value = q;
214 break;
215 }
216
217 /* No; add it to the set of other headers. */
218 if (hp == ENDOF(Table)) {
219 if (OtherCount >= OtherSize - 1) {
220 OtherSize += HEADER_DELTA;
221 RENEW(OtherHeaders, char*, OtherSize);
222 }
223 OtherHeaders[OtherCount++] = p;
224 }
225
226 /* Get start of next header; if it's a blank line, we hit the end. */
227 if ((p = NextHeader(p)) == NULL) {
228 (void)strcpy(Error, "Article has no body -- just headers");
229 return NULL;
230 }
231 if (*p == '\n')
232 break;
233 }
234
235 return p + 1;
236 }
237
238
239
240 /*
241 ** Check the control message, and see if it's legit. Return pointer to
242 ** error message if not.
243 */
244 STATIC STRING
245 CheckControl(ctrl)
246 char *ctrl;
247 {
248 register char *p;
249 register char *q;
250 char save;
251
252 /* Snip off the first word. */
253 for (p = ctrl; ISWHITE(*p); p++)
254 continue;
255 for (ctrl = p; *p && !ISWHITE(*p); p++)
256 continue;
257 if (p == ctrl)
258 return "Empty control message";
259 save = *p;
260 *p = '\0';
261
262 if (EQ(ctrl, "cancel")) {
263 for (q = p + 1; ISWHITE(*q); q++)
264 continue;
265 if (*q == '\0')
266 return "Message-ID missing in cancel";
267 }
268 else if (EQ(ctrl, "sendsys") || EQ(ctrl, "senduuname")
269 || EQ(ctrl, "version") || EQ(ctrl, "checkgroups")
270 || EQ(ctrl, "ihave") || EQ(ctrl, "sendme")
271 || EQ(ctrl, "newgroup") || EQ(ctrl, "rmgroup"))
272 /* SUPPRESS 530 *//* Empty body for statement */
273 ;
274 else {
275 (void)sprintf(Error, "\"%s\" is not a valid control message",
276 MaxLength(ctrl,ctrl));
277 return Error;
278 }
279 *p = save;
280 return NULL;
281 }
282
283
284 /*
285 ** Check the Distribution header, and exit on error.
286 */
287 STATIC STRING
288 CheckDistribution(p)
289 register char *p;
290 {
291 static char SEPS[] = " \t,";
292 register STRING *dp;
293
294 if ((p = strtok(p, SEPS)) == NULL)
295 return "Can't parse Distribution line.";
296 do {
297 for (dp = BadDistribs; *dp; dp++)
298 if (wildmat(p, *dp)) {
299 (void)sprintf(Error, "Illegal distribution \"%s\"", MaxLength(p,p));
300 return Error;
301 }
302 } while ((p = strtok((char *)NULL, SEPS)) != NULL);
303 return NULL;
304 }
305
306
307 /*
308 ** Process all the headers. FYI, they're done in RFC-order.
309 ** Return NULL if okay, or an error message.
310 */
311 STATIC STRING
312 ProcessHeaders(linecount, idbuff)
313 int linecount;
314 char *idbuff;
315 {
316 static char MONTHS[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
317 static char datebuff[40];
318 static char orgbuff[SMBUF];
319 static char linebuff[40];
320 static char sendbuff[SMBUF];
321 static char mimeversion[SMBUF];
322 static char mimetype[SMBUF];
323 static char mimeencoding[SMBUF];
324 register HEADER *hp;
325 register char *p;
326 time_t t;
327 struct tm *gmt;
328 TIMEINFO Now;
329 STRING error;
330
331 /* Do some preliminary fix-ups. */
332 for (hp = Table; hp < ENDOF(Table); hp++) {
333 if (!hp->CanSet && hp->Value) {
334 (void)sprintf(Error, "Can't set system \"%s\" header", hp->Name);
335 return Error;
336 }
337 if (hp->Value) {
338 hp->Value = TrimSpaces(hp->Value);
339 if (*hp->Value == '\0')
340 hp->Value = NULL;
341 }
342 }
343
344 #if defined(DO_NNRP_AUTH_SENDER)
345 /* If authorized and we didn't get a sender, add the header based on
346 * our info. If not authorized, zap the Sender so we don't put out
347 * unauthenticated data. */
348 if (PERMauthorized && HDR(_sender) == NULL) {
349 (void)sprintf(sendbuff, "%s@%s",
350 PERMuser ? PERMuser : "UNKNOWN", ClientHost);
351 HDR(_sender) = sendbuff;
352 }
353 else if (!PERMauthorized)
354 HDR(_sender) = NULL;
355 #endif /* defined(DO_NNRP_AUTH_SENDER) */
356
357 /* Set Date. */
358 if (GetTimeInfo(&Now) < 0) {
359 (void)sprintf(Error, "Can't get the time, %s", strerror(errno));
360 return Error;
361 }
362
363 if ((gmt = gmtime(&Now.time)) == NULL)
364 return "Can't get the time";
365 (void)sprintf(datebuff, "%d %3.3s %d %02d:%02d:%02d GMT",
366 gmt->tm_mday, &MONTHS[3 * gmt->tm_mon], 1900 + gmt->tm_year,
367 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
368 if (HDR(_date) == NULL) {
369 HDR(_date) = datebuff;
370 }
371 else {
372 if ((t = parsedate(HDR(_date), &Now)) == -1)
373 return "Can't parse \"Date\" header";
374 if (t > Now.time + DATE_FUZZ)
375 return "Article posted in the future";
376 }
377
378 /* Newsgroups are checked later. */
379
380 if (HDR(_control)) {
381 if ((error = CheckControl(HDR(_control))) != NULL)
382 return error;
383 }
384 else {
385 p = HDR(_subject);
386 if (p == NULL)
387 return "Required \"Subject\" header is missing";
388 if (EQn(p, "cmsg ", 5)) {
389 HDR(_control) = p + 5;
390 if ((error = CheckControl(HDR(_control))) != NULL)
391 return error;
392 }
393 #if 0
394 if (EQn(p, "Re: ", 4) && HDR(_references) == NULL)
395 return "Article starts with \"Re: \" but has no references";
396 #endif /* 0 */
397 }
398
399 /* Set Message-ID */
400 if (HDR(_messageid) == NULL) {
401 HDR(_messageid) = COPY(idbuff); /* XXX leak? valgrind says no */
402 }
403
404 /* Set Path */
405 if (HDR(_path) == NULL) {
406 /* Note that innd will put host name here for us. */
407 HDR(_path) = PATHMASTER;
408 } else {
409 #if 0
410 /* Here's where to do Path changes for new Posts. */
411 char *newpath;
412 if ((newpath = strrchr(HDR(_path), '!')) != NULL) {
413 newpath++;
414 if (*newpath == '\0') {
415 HDR(_path) = NEWSMASTER;
416 } else {
417 HDR(_path) = newpath;
418 }
419 }
420 #endif
421 }
422
423
424 /* Reply-To; left alone. */
425 /* Sender; set above. */
426
427 /* Check Expires. */
428 if (HDR(_expires) && parsedate(HDR(_expires), &Now) == -1)
429 return "Can't parse \"Expires\" header";
430
431 /* References; left alone. */
432 /* Control; checked above. */
433
434 /* Distribution. */
435 if ((p = HDR(_distribution)) != NULL) {
436 p = COPY(p);
437 error = CheckDistribution(p);
438 DISPOSE(p);
439 if (error != NULL)
440 return error;
441 }
442
443 /* Set Organization */
444 if (HDR(_organization) == NULL
445 && (p = GetConfigValue(_CONF_ORGANIZATION)) != NULL) {
446 (void)strcpy(orgbuff, p);
447 HDR(_organization) = orgbuff;
448 }
449
450 /* Keywords; left alone. */
451 /* Summary; left alone. */
452 /* Approved; left alone. */
453
454 /* MIME headers. */
455 if (HDR(_mimeversion) == NULL
456 && (p = GetConfigValue(_CONF_MIMEVERSION)) != NULL) {
457 (void)strcpy(mimeversion, p);
458 HDR(_mimeversion) = mimeversion;
459
460 /* Set Content-Type */
461 if (HDR(_contenttype) == NULL) {
462 if ((p = GetConfigValue(_CONF_CONTENTTYPE)) == NULL)
463 return "Can't get \"Content-Type\" header";
464 (void)strcpy(mimetype, p);
465 HDR(_contenttype) = mimetype;
466 }
467
468 /* Set Content-Transfer-Encoding */
469 if (HDR(_contenttransferencoding) == NULL) {
470 if ((p = GetConfigValue(_CONF_ENCODING)) == NULL)
471 return "Can't get \"Content-Transfer-Encoding\" header";
472 (void)strcpy(mimeencoding, p);
473 HDR(_contenttransferencoding) = mimeencoding;
474 }
475 }
476
477 /* Set Lines */
478 (void)sprintf(linebuff, "%d", linecount);
479 HDR(_lines) = linebuff;
480
481 /* Supersedes; left alone. */
482
483 /* NNTP-Posting host; set. */
484 HDR(_nntpposthost) = ClientHost;
485
486 {
487 static char xtracebuff[SMBUF];
488
489 /* X-Trace; set. */
490 if ((p = GetFQDN()) == NULL )
491 p = "unknown";
492 sprintf(xtracebuff, "%s %ld %ld %s (%s)",
493 p, (long) time((time_t *)NULL), (long) getpid(), ClientIp, datebuff);
494 HDR(_xtrace) = xtracebuff;
495 }
496
497 /* Now make sure everything is there. */
498 for (hp = Table; hp < ENDOF(Table); hp++)
499 if (hp->Type == HTreq && hp->Value == NULL) {
500 (void)sprintf(Error, "Required \"%s\" header is missing",
501 hp->Name);
502 return Error;
503 }
504
505 return NULL;
506 }
507
508
509 #if defined(DO_CHECK_INCLUDED_TEXT)
510 /*
511 ** See if the user has more included text than new text. Simple-minded, but
512 ** reasonably effective for catching neophyte's mistakes. A line starting
513 ** with > is included text. Decrement the count on lines starting with <
514 ** so that we don't reject diff(1) output.
515 */
516 STATIC STRING
517 CheckIncludedText(p, lines)
518 register char *p;
519 register int lines;
520 {
521 register int i;
522
523 for (i = 0; ; p++) {
524 switch (*p) {
525 case '>':
526 i++;
527 break;
528 case '|':
529 i++;
530 break;
531 case ':':
532 i++;
533 break;
534 case '<':
535 i--;
536 break;
537 }
538 if ((p = strchr(p + 1, '\n')) == NULL)
539 break;
540 }
541 if ((i * 2 > lines) && (lines > 40))
542 return "Article not posted -- more included text than new text";
543 return NULL;
544 }
545 #endif /* defined(DO_CHECK_INCLUDED_TEXT) */
546
547
548
549 /*
550 ** Try to mail an article to the moderator of the group.
551 */
552 STATIC STRING
553 MailArticle(group, article)
554 char *group;
555 char *article;
556 {
557 static char CANTSEND[] = "Can't send text to mailer";
558 register FILE *F;
559 register HEADER *hp;
560 register int i;
561 char *address;
562 char buff[SMBUF];
563
564 /* Try to get the address first. */
565 if ((address = GetModeratorAddress(NULL, NULL, group)) == NULL) {
566 (void)sprintf(Error, "No mailing address for \"%s\" -- %s",
567 group, "ask your news administrator to fix this");
568 return Error;
569 }
570
571 /* Now build up the command (ignore format/argument mismatch errors,
572 * in case %s isn't in _PATH_SENDMAIL) and send the headers. */
573 (void)sprintf(buff, _PATH_SENDMAIL, address);
574 if ((F = popen(buff, "w")) == NULL)
575 return "Can't start mailer";
576 (void)fprintf(F, "To: %s\n", address);
577 if (FLUSH_ERROR(F)) {
578 (void)pclose(F);
579 return CANTSEND;
580 }
581
582 /* Write the headers, a blank line, then the article. */
583 for (hp = Table; hp < ENDOF(Table); hp++)
584 if (hp->Value) {
585 (void)fprintf(F, "%s: %s\n", hp->Name, hp->Value);
586 if (FLUSH_ERROR(F)) {
587 (void)pclose(F);
588 return CANTSEND;
589 }
590 }
591 for (i = 0; i < OtherCount; i++) {
592 (void)fprintf(F, "%s\n", OtherHeaders[i]);
593 if (FLUSH_ERROR(F)) {
594 (void)pclose(F);
595 return CANTSEND;
596 }
597 }
598 (void)fprintf(F, "\n");
599 i = strlen(article);
600 if (fwrite((POINTER)article, (SIZE_T)1, (SIZE_T)i, F) != i)
601 return "Can't send article";
602 if (FLUSH_ERROR(F)) {
603 (void)pclose(F);
604 return CANTSEND;
605 }
606 i = pclose(F);
607 if (i) {
608 (void)sprintf(Error, "Mailer exited with status %d -- %s",
609 i, "Article might not have been mailed");
610 return Error;
611 }
612 WasMailed = TRUE;
613 return NULL;
614 }
615
616
617 /*
618 ** Check the newsgroups and make sure they're all valid, that none are
619 ** moderated, etc.
620 */
621 STATIC STRING
622 ValidNewsgroups(hdr, article)
623 char *hdr;
624 char *article;
625 {
626 static char distbuff[SMBUF];
627 register char *groups;
628 register char *p;
629 register GROUPENTRY *gp;
630 register BOOL approved;
631 struct _DDHANDLE *h;
632 char *grplist[2];
633 BOOL IsNewgroup;
634 BOOL FoundOne;
635
636 p = HDR(_control);
637 IsNewgroup = p && EQn(p, "newgroup", 8);
638 groups = COPY(hdr);
639 if ((p = strtok(groups, NGSEPS)) == NULL)
640 return "Can't parse newsgroups line";
641
642 /* Don't mail article if just checking Followup-To line. */
643 approved = HDR(_approved) != NULL || article == NULL;
644
645 Error[0] = '\0';
646 FoundOne = FALSE;
647 h = DDstart((FILE *)NULL, (FILE *)NULL);
648 do {
649 #if defined(DO_MERGE_TO_GROUPS)
650 if (p[0] == 't' && p[1] == 'o' && p[2] == '.')
651 p = "to";
652 #endif /* defined(DO_MERGE_TO_GROUPS) */
653 if ((gp = GRPfind(p)) == NULL)
654 continue;
655 FoundOne = TRUE;
656 DDcheck(h, p);
657 if (PERMspecified) {
658 grplist[0] = p;
659 grplist[1] = NULL;
660 if (!PERMmatch(PERMdefault, PERMlist, grplist)) {
661 sprintf(Error,"%s: %s\r\n", NNTP_NOSUCHGROUP,p);
662 DISPOSE(groups);
663 tmpPtr = DDend(h);
664 DISPOSE(tmpPtr);
665 return Error;
666 }
667 }
668 switch (gp->Flag) {
669 case NF_FLAG_OK:
670 break;
671 case NF_FLAG_MODERATED:
672 if (!approved) {
673 DISPOSE(groups);
674 tmpPtr = DDend(h);
675 DISPOSE(tmpPtr);
676 return MailArticle(gp->Name, article);
677 }
678 break;
679 case NF_FLAG_IGNORE:
680 case NF_FLAG_NOLOCAL:
681 (void)sprintf(Error, "Postings to \"%s\" are not allowed here.",
682 gp->Name);
683 break;
684 case NF_FLAG_EXCLUDED:
685 /* Do NOT return an error. */
686 break;
687 case NF_FLAG_ALIAS:
688 (void)sprintf(Error,
689 "The newsgroup \"%s\" has been renamed to \"%s\".\n",
690 p, gp->Alias);
691 break;
692 }
693 } while ((p = strtok((char *)NULL, NGSEPS)) != NULL);
694 DISPOSE(groups);
695
696 if (!FoundOne && !IsNewgroup)
697 (void)sprintf(Error, "No valid newsgroups in \"%s\"", MaxLength(hdr,hdr));
698 if (Error[0]) {
699 tmpPtr = DDend(h);
700 DISPOSE(tmpPtr) ;
701 return Error;
702 }
703
704 p = DDend(h);
705 if (HDR(_distribution) == NULL && *p) {
706 (void)strcpy(distbuff, p);
707 HDR(_distribution) = distbuff;
708 }
709 DISPOSE(p);
710 return NULL;
711 }
712
713
714 /*
715 ** Send a quit message to the server, eat its reply.
716 */
717 STATIC void
718 SendQuit(FromServer, ToServer)
719 FILE *FromServer;
720 FILE *ToServer;
721 {
722 char buff[NNTP_STRLEN];
723
724 (void)fprintf(ToServer, "quit\r\n");
725 (void)fflush(ToServer);
726 (void)fclose(ToServer);
727 (void)fgets(buff, sizeof buff, FromServer);
728 (void)fclose(FromServer);
729 }
730
731
732 /*
733 ** Offer the article to the server, return its reply.
734 */
735 STATIC int
736 OfferArticle(buff, buffsize, FromServer, ToServer)
737 char *buff;
738 int buffsize;
739 FILE *FromServer;
740 FILE *ToServer;
741 {
742 static char CANTSEND[] = "Can't send %s to server, %s";
743
744 (void)fprintf(ToServer, "ihave %s\r\n", HDR(_messageid));
745 if (FLUSH_ERROR(ToServer)
746 || fgets(buff, buffsize, FromServer) == NULL) {
747 (void)sprintf(buff, CANTSEND, "IHAVE", strerror(errno));
748 return -1;
749 }
750 return atoi(buff);
751 }
752
753
754 /*
755 ** Spool article to temp file.
756 */
757 STATIC STRING
758 Spoolit(article, Error)
759 char *article;
760 char *Error;
761 {
762 static char CANTSPOOL[NNTP_STRLEN+2];
763 register HEADER *hp;
764 register FILE *F = NULL;
765 register int i;
766 char temp[BUFSIZ];
767 char path[BUFSIZ];
768 struct stat Sb;
769
770 /* Initialize the returned error message */
771 sprintf(CANTSPOOL, "%s and can't write text to local spool file", Error);
772
773 /* Try to write it to the spool dir. */
774 TempName(_PATH_SPOOLNEWS, temp);
775 /* rnews -U ignores files starting with . */
776 strrchr(temp, '/')[1] = '.';
777 if ((F = fopen(temp, "w")) == NULL) {
778 syslog(L_FATAL, "cant open %s %m", temp);
779 return CANTSPOOL;
780 }
781 fchmod(fileno(F), BATCHFILE_MODE);
782
783 /* Write the headers and a blank line. */
784 for (hp = Table; hp < ENDOF(Table); hp++)
785 if (hp->Value) {
786 (void)fprintf(F, "%s: %s\n", hp->Name, hp->Value);
787 if (FLUSH_ERROR(F)) {
788 (void)fclose(F);
789 return CANTSPOOL;
790 }
791 }
792 for (i = 0; i < OtherCount; i++) {
793 (void)fprintf(F, "%s\n", OtherHeaders[i]);
794 if (FLUSH_ERROR(F)) {
795 (void)fclose(F);
796 return CANTSPOOL;
797 }
798 }
799 (void)fprintf(F, "\n");
800
801 /* Write the article body */
802 i = strlen(article);
803 if (fwrite((POINTER)article, (SIZE_T)1, (SIZE_T)i, F) != i) {
804 fclose(F);
805 return CANTSPOOL;
806 }
807
808 /* Flush and catch any errors */
809 if (fclose(F))
810 return CANTSPOOL;
811
812 TempName(_PATH_SPOOLNEWS, path);
813 if (rename(temp, path) < 0) {
814 syslog(L_FATAL, "cant rename %s %s %m", temp, path);
815 return CANTSPOOL;
816 }
817
818 /* Article has been spooled */
819 return NULL;
820 }
821
822 STRING
823 ARTpost(article, idbuff)
824 char *article;
825 char *idbuff;
826 {
827 static char CANTSEND[] = "Can't send %s to server, %s";
828 register int i;
829 register char *p;
830 register char *next;
831 register HEADER *hp;
832 FILE *ToServer;
833 FILE *FromServer;
834 char buff[NNTP_STRLEN + 2], frombuf[SMBUF];
835 STRING error;
836
837 /* Set up the other headers list. */
838 if (OtherHeaders == NULL) {
839 OtherSize = HEADER_DELTA;
840 OtherHeaders = NEW(char*, OtherSize);
841 }
842
843 /* Basic processing. */
844 OtherCount = 0;
845 WasMailed = FALSE;
846 for (hp = Table; hp < ENDOF(Table); hp++) {
847 hp->Size = strlen(hp->Name);
848 hp->Value = NULL;
849 }
850 if ((article = StripOffHeaders(article)) == NULL)
851 return Error;
852 for (i = 0, p = article; p; i++, p = next + 1)
853 if ((next = strchr(p, '\n')) == NULL)
854 break;
855 #if defined(DO_CHECK_INCLUDED_TEXT)
856 if ((error = CheckIncludedText(article, i)) != NULL)
857 return error;
858 #endif /* defined(DO_CHECK_INCLUDED_TEXT) */
859 if ((error = ProcessHeaders(i, idbuff)) != NULL)
860 return error;
861 if (i == 0 && HDR(_control) == NULL)
862 return "Article is empty";
863
864 if (idbuff != NULL)
865 strcpy (idbuff,"(mailed to moderator)") ;
866 if ((error = ValidNewsgroups(HDR(_newsgroups), article)) != NULL
867 || WasMailed)
868 return error;
869 if (idbuff != NULL)
870 idbuff [0] = '\0' ;
871
872 strncpy(frombuf, HDR(_from), sizeof(frombuf) - 1);
873 frombuf[sizeof(frombuf) - 1] = '\0';
874 HeaderCleanFrom(frombuf);
875 p = strchr(frombuf, '@');
876 if (p) {
877 memmove(frombuf, p+1, strlen(p+1) + 1);
878 p = strrchr(frombuf, '.');
879 if (!p)
880 return "From: address not in Internet syntax";
881 }
882 else
883 return "From: address not in Internet syntax";
884 if ((p = HDR(_followupto)) != NULL
885 && !EQ(p, "poster")
886 && (error = ValidNewsgroups(p, (char *)NULL)) != NULL)
887 return error;
888 #if LOCAL_MAX_ARTSIZE > 0
889 if (strlen(article) > LOCAL_MAX_ARTSIZE) {
890 (void)sprintf(Error,
891 "Article is bigger then local limit of %ld bytes\n",
892 LOCAL_MAX_ARTSIZE);
893 return Error;
894 }
895 #endif /* LOCAL_MAX_ARTSIZE > 0 */
896
897 #if defined(DO_PERL)
898 /* Calls the Perl subroutine for headers management */
899 if ((p = HandleHeaders(article)) != NULL) {
900 if (strncmp(p, "DROP", 4) == 0) {
901 syslog(L_NOTICE, "%s post %s", ClientHost, p);
902 return NULL;
903 }
904 return p;
905 }
906 #endif /* defined(DO_PERL) */
907
908 /* Open a local connection to the server. */
909 if (RemoteMaster)
910 i = NNTPconnect(RemoteMaster, &FromServer, &ToServer, buff);
911 else {
912 #if defined(DO_HAVE_UNIX_DOMAIN)
913 i = NNTPlocalopen(&FromServer, &ToServer, buff);
914 #else
915 i = NNTPremoteopen(&FromServer, &ToServer, buff);
916 #endif /* defined(DO_HAVE_UNIX_DOMAIN) */
917 }
918
919 /* If we cannot open the connection, initialize the error message and
920 * attempt to recover from this by spooling it locally */
921 if (i < 0) {
922 if (buff[0])
923 (void)strcpy(Error, buff);
924 else
925 (void)sprintf(Error, CANTSEND, "connect request", strerror(errno));
926 return Spoolit(article,Error);
927 }
928 if (Tracing)
929 syslog(L_TRACE, "%s post_connect %s",
930 ClientHost, RemoteMaster ? RemoteMaster : "localhost");
931
932 /* The code below has too many (void) casts for my tastes. At least
933 * they are all inside cases that are most likely never going to
934 * happen -- for example, if the server crashes. */
935
936 /* Offer article to server. */
937 i = OfferArticle(buff, sizeof buff, FromServer, ToServer);
938 if (i == NNTP_AUTH_NEEDED_VAL) {
939 /* Send authorization. */
940 if (NNTPsendpassword(RemoteMaster, FromServer, ToServer) < 0) {
941 (void)sprintf(Error, "Can't authorize with %s",
942 RemoteMaster ? RemoteMaster : "innd");
943 return Spoolit(article,Error);
944 }
945 i = OfferArticle(buff, sizeof buff, FromServer, ToServer);
946 }
947 if (i != NNTP_SENDIT_VAL) {
948 (void)strcpy(Error, buff);
949 SendQuit(FromServer, ToServer);
950 return (i != NNTP_HAVEIT_VAL ? Spoolit(article, Error) : Error) ;
951 }
952 if (Tracing)
953 syslog(L_TRACE, "%s post starting", ClientHost);
954
955 /* Write the headers and a blank line. */
956 for (hp = Table; hp < ENDOF(Table); hp++)
957 if (hp->Value)
958 (void)fprintf(ToServer, "%s: %s\r\n", hp->Name, hp->Value);
959 for (i = 0; i < OtherCount; i++)
960 (void)fprintf(ToServer, "%s\r\n", OtherHeaders[i]);
961 (void)fprintf(ToServer, "\r\n");
962 if (FLUSH_ERROR(ToServer)) {
963 (void)sprintf(Error, CANTSEND, "headers", strerror(errno));
964 (void)fclose(FromServer);
965 (void)fclose(ToServer);
966 return Spoolit(article, Error);
967 }
968
969 /* Send the article, get the server's reply. */
970 if (NNTPsendarticle(article, ToServer, TRUE) < 0
971 || fgets(buff, sizeof buff, FromServer) == NULL) {
972 (void)sprintf(Error, CANTSEND, "article", strerror(errno));
973 (void)fclose(FromServer);
974 (void)fclose(ToServer);
975 return Spoolit(article, Error);
976 }
977
978 /* Did the server want the article? */
979 if ((i = atoi(buff)) != NNTP_TOOKIT_VAL) {
980 (void)strcpy(Error, buff);
981 SendQuit(FromServer, ToServer);
982 return (i != NNTP_REJECTIT_VAL ? Spoolit(article, Error) : Error) ;
983 }
984
985 /* Send a quit and close down */
986 SendQuit(FromServer, ToServer);
987 if (idbuff) {
988 (void)strncpy(idbuff, HDR(_messageid), SMBUF - 1);
989 idbuff[SMBUF - 1] = '\0';
990 }
991 return NULL;
992 }