Debian Codegraph @

innd/rc.c

raw ◀ ▶
Source reconstructed at patch state 0 of 7 — not the installed (fully patched) source.
7 Debian patches touch this file
1 /* $Revision: 1.42 $
2 **
3 ** Routines for the remote connect channel. Create an Internet stream socket
4 ** that processes connect to. If the incoming site is not one of our feeds,
5 ** then we pass the connection off to the standard nntp daemon.
6 */
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include "configdata.h"
10 #include "clibrary.h"
11 #include "innd.h"
12 #include <netdb.h>
13
14
15 #if !defined(NETSWAP)
16 #if !defined(htons)
17 extern unsigned short htons();
18 #endif /* !defined(htons) */
19 #if !defined(htonl)
20 #if 0
21 extern unsigned long htonl(); /* nobody should really need this anymore */
22 #endif
23 #endif /* !defined(htonl) */
24 #endif /* !defined(NETSWAP) */
25
26 #define COPYADDR(dest, src) \
27 (void)memcpy((POINTER)dest, (POINTER)src, (SIZE_T)sizeof (INADDR))
28
29 #if defined(DO_TCPIDENT_PATH)
30 #include <ident.h>
31 #endif /* defined(DO_TCPIDENT_PATH) */
32
33 int RCfix_options(int fd, struct sockaddr_in *remote);
34
35 /*
36 ** A remote host has an address and a password.
37 */
38 typedef struct _REMOTEHOST {
39 char *Name;
40 INADDR Address;
41 char *Password;
42 BOOL Streaming ;
43 BOOL PathAddr ;
44 char *RequirePathHostForPath ;
45 BOOL PathIdent ;
46 char **PathNoIdentIdents;
47 char **Patterns;
48 } REMOTEHOST;
49
50 typedef struct _REMOTETABLE {
51 INADDR Address;
52 time_t Expires;
53 } REMOTETABLE;
54
55 STATIC INADDR *RCmaster;
56 STATIC int RCnmaster;
57 STATIC char *RCslaveflag;
58 STATIC char RCnnrpd[] = _PATH_NNRPD;
59 STATIC char RCnnrqd[] = _PATH_NNQRD;
60 STATIC char RCnntpd[] = _PATH_NNTPD;
61 STATIC CHANNEL *RCchan;
62 STATIC REMOTEHOST *RCpeerlist;
63 STATIC int RCnpeerlist;
64 STATIC REMOTEHOST *RCnolimitlist;
65 STATIC int RCnnolimitlist;
66
67 /*
68 ** Stuff needed for limiting incoming connects.
69 */
70 STATIC char RCterm[] = "\r\n";
71 STATIC REMOTETABLE remotetable[REMOTETABLESIZE];
72 STATIC int remotecount;
73 STATIC int remotefirst;
74
75
76 /*
77 ** See if the site properly entered the password.
78 */
79 BOOL
80 RCauthorized(cp, pass)
81 register CHANNEL *cp;
82 char *pass;
83 {
84 register REMOTEHOST *rp;
85 register int i;
86
87 for (rp = RCpeerlist, i = RCnpeerlist; --i >= 0; rp++)
88 /* SUPPRESS 112 *//* Retrieving long where char is stored */
89 if (cp->Address.s_addr == rp->Address.s_addr) {
90 if (rp->Password[0] == '\0' || EQ(pass, rp->Password))
91 return TRUE;
92 syslog(L_ERROR, "%s bad_auth", inet_ntoa(cp->Address));
93 return FALSE;
94 }
95
96 if (!AnyIncoming)
97 /* Not found in our table; this can't happen. */
98 syslog(L_ERROR, "%s not_found", inet_ntoa(cp->Address));
99
100 /* Anonymous hosts should not authenticate. */
101 return FALSE;
102 }
103
104
105 /*
106 ** See if a host is in the "nolimit" file.
107 */
108 BOOL
109 RCnolimit(cp)
110 register CHANNEL *cp;
111 {
112 register REMOTEHOST *rp;
113 register int i;
114
115 for (rp = RCnolimitlist, i = RCnnolimitlist; --i >= 0; rp++)
116 /* SUPPRESS 112 *//* Retrieving long where char is stored */
117 if (cp->Address.s_addr == rp->Address.s_addr)
118 return TRUE;
119 return FALSE;
120 }
121
122
123 /*
124 ** Is this an address of the master?
125 */
126 BOOL
127 RCismaster(addr)
128 INADDR addr;
129 {
130 register INADDR *ip;
131 register int i;
132
133 if (AmSlave)
134 for (i = RCnmaster, ip = RCmaster; --i >= 0; ip++)
135 /* SUPPRESS 112 *//* Retrieving long where char is stored */
136 if (addr.s_addr == ip->s_addr)
137 return TRUE;
138 return FALSE;
139 }
140
141
142 /*
143 ** Called when input is ready to read. Shouldn't happen.
144 */
145 /* ARGSUSED0 */
146 STATIC FUNCTYPE
147 RCrejectreader(cp)
148 CHANNEL *cp;
149 {
150 syslog(L_ERROR, "%s internal RCrejectreader", LogName);
151 }
152
153
154 /*
155 ** Write-done function for rejects.
156 */
157 STATIC FUNCTYPE
158 RCrejectwritedone(cp)
159 register CHANNEL *cp;
160 {
161 switch (cp->State) {
162 default:
163 syslog(L_ERROR, "%s internal RCrejectwritedone state %d",
164 CHANname(cp), cp->State);
165 break;
166 case CSwritegoodbye:
167 CHANclose(cp, CHANname(cp));
168 break;
169 }
170 }
171
172
173 /*
174 ** Hand off a descriptor to NNRPD.
175 */
176 void
177 RChandoff(fd, h)
178 int fd;
179 HANDOFF h;
180 {
181 STRING argv[6];
182 char buff[SMBUF];
183 int i;
184
185 #if defined(SOL_SOCKET) && defined(SO_KEEPALIVE)
186 /* Set KEEPALIVE to catch broken socket connections. */
187 i = 1;
188 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof i) < 0)
189 syslog(L_ERROR, "fd %d cant setsockopt(KEEPALIVE) %m", fd);
190 #endif /* defined(SOL_SOCKET) && defined(SO_KEEPALIVE) */
191
192 if (SetNonBlocking(fd, FALSE) < 0)
193 syslog(L_ERROR, "%s cant nonblock %d in RChandoff %m", LogName, fd);
194 switch (h) {
195 default:
196 syslog(L_ERROR, "%s internal RChandoff %d type %d", LogName, fd, h);
197 /* FALLTHROUGH */
198 case HOnnrpd: argv[0] = RCnnrpd; break;
199 case HOnnrqd: argv[0] = RCnnrqd; break;
200 case HOnntpd: argv[0] = RCnntpd; break;
201 }
202 argv[1] = "-s ";
203 i = 2;
204 if (NNRPReason) {
205 (void)sprintf(buff, "-r%s", NNRPReason);
206 argv[i++] = buff;
207 }
208 if (NNRPTracing)
209 argv[i++] = "-t";
210 if (RCslaveflag)
211 argv[i++] = RCslaveflag;
212 argv[i] = NULL;
213
214 /* Call NNRP; don't send back a QUIT message if Spawn fails since
215 * that's a major error we want to find out about quickly. */
216 (void)Spawn(fd, fd, fd, argv);
217 }
218
219
220 /*
221 ** Sets the Path logging on a new NNTP channel, according to the
222 ** parameters given (which usually came from the hosts.nntp).
223 */
224 STATIC FUNCTYPE
225 RCsetpathlogging(cp,PathAddr,RequirePathHost,PathIdent,PathNoIdentIdents)
226 CHANNEL *cp;
227 BOOL PathAddr;
228 char *RequirePathHost;
229 BOOL PathIdent;
230 char **PathNoIdentIdents;
231 {
232 char *ident;
233 char *protectwalk;
234 char *protectout;
235 char **noidentswalk;
236 int c;
237 int protectedlen;
238
239 #if defined(DO_IPADDR_PATH)
240 #if defined(DO_TCPIDENT_PATH)
241 if (!PathIdent) {
242 cp->PathAddr= PathAddr;
243 cp->RequirePathHostForPath = RequirePathHost ? COPY(RequirePathHost) : NULL;
244 cp->IdentForPath = NULL;
245 return;
246 }
247 ident = ident_id(cp->fd,10);
248 if (!ident) {
249 cp->PathAddr = TRUE;
250 cp->RequirePathHostForPath = NULL;
251 cp->IdentForPath = NEW(char,2);
252 (void)strcpy(cp->IdentForPath,"?");
253 return;
254 }
255 for (noidentswalk = PathNoIdentIdents;
256 noidentswalk && *noidentswalk;
257 noidentswalk++) {
258 if (!strcmp(*noidentswalk,ident)) {
259 DISPOSE(ident);
260 cp->PathAddr = PathAddr;
261 cp->RequirePathHostForPath = RequirePathHost ? COPY(RequirePathHost) : NULL;
262 cp->IdentForPath = NULL;
263 return;
264 }
265 }
266 for (protectwalk = ident, protectedlen = 0;
267 c = *protectwalk;
268 protectwalk++)
269 protectedlen += c == '\\' ? 2 : (c > 32 && c <= 127) ? 1 : 4;
270 cp->PathAddr = TRUE;
271 cp->RequirePathHostForPath = NULL;
272 cp->IdentForPath = NEW(char,protectedlen+1);
273 for (protectwalk = ident, protectout = cp->IdentForPath;
274 c = *protectwalk;
275 protectwalk++) {
276 if (c == '\\') {
277 *protectout++ = '\\';
278 *protectout++ = '\\';
279 } else if (c > 32 && c <= 127) {
280 *protectout++ = c;
281 } else {
282 sprintf(protectout,"\\x%02x",c & 0x0ff);
283 protectout += 4;
284 }
285 }
286 *protectout = '\0';
287 return;
288 #else
289 cp->PathAddr = PathAddr;
290 cp->RequirePathHostForPath = RequirePathHost ? COPY(RequirePathHost) : NULL;
291 cp->IdentForPath = NULL;
292 #endif /* defined(DO_TCPIDENT_PATH) */
293 #else
294 cp->PathAddr = FALSE;
295 cp->RequirePathHostForPath = NULL;
296 cp->IdentForPath = NULL;
297 #endif /* defined(DO_IPADDR_PATH) */
298 }
299
300 /*
301 ** Read function. Accept the connection and either create an NNTP channel
302 ** or spawn an nnrpd to handle it.
303 */
304 STATIC FUNCTYPE
305 RCreader(cp)
306 CHANNEL *cp;
307 {
308 int fd;
309 struct sockaddr_in remote;
310 int size;
311 register int i;
312 register REMOTEHOST *rp;
313 CHANNEL *new;
314 char *name;
315 long reject_val;
316 char *reject_message;
317 int count;
318 int found;
319 time_t now;
320 CHANNEL tempchan;
321
322 if (cp != RCchan) {
323 syslog(L_ERROR, "%s internal RCreader wrong channel %p not %p",
324 LogName, cp, RCchan);
325 return;
326 }
327
328 /* Get the connection. */
329 size = sizeof remote;
330 if ((fd = accept(cp->fd, (struct sockaddr *)&remote, &size)) < 0) {
331 syslog(L_ERROR, "%s cant accept RCreader %m", LogName);
332 return;
333 }
334
335 /*
336 ** Clear any IP_OPTIONS, including source routing, on the socket
337 */
338 if (RCfix_options(fd, &remote) != 0) {
339 /* shouldn't happen, but we're bit paranoid at this point */
340 if (close(fd) < 0)
341 syslog(L_ERROR, "%s cant close %d %m", LogName, fd);
342 return;
343 }
344
345 /*
346 ** If RemoteTimer is not zero, then check the limits on incoming
347 ** connections on a total and per host basis.
348 **
349 ** The incoming connection table is fixed at 128 entries to make
350 ** calculating the index easy (i + 1) & 7, and to be pretty sure
351 ** that you won't run out of space. The table is used as a ring
352 ** with new entries being added to the end (wrapping around) and
353 ** expired entries being deleted from the front (again wrapping
354 ** around). It is doubtful that you will ever use even half of
355 ** the table.
356 **
357 ** There are three parameters controlling the use of the table not
358 ** counting the starting index and count.
359 **
360 ** L = per host incoming connects per X seconds allowed
361 ** T = total incoming connects per X seconds allowed
362 ** X = number of seconds to remember a successful connect
363 **
364 ** First, one pass is made over the live entries deleting any that
365 ** are over X seconds old. If the entry hasn't expired, compare
366 ** the incoming connection's host address with the entry's host
367 ** address. If equal, increment the ``found'' counter.
368 **
369 ** Second, if the number of entries now in the table is equal to
370 ** the ``T'' parameter, reject the connection with the ``504''
371 ** error code.
372 **
373 ** 504 Server overloaded, try later
374 **
375 ** Third, if the number of entries now in the table which match
376 ** the incoming connection's host address is equal to the ``L''
377 ** parameter, reject the connection with the ``505'' error code.
378 **
379 ** 505 Connection rejected, you're making too many connects per minute
380 **
381 ** Finally, if neither rejection happened, add the entry to the
382 ** table, and continue on as a normal connect.
383 */
384 tempchan.Address.s_addr = remote.sin_addr.s_addr;
385 reject_message = NULL;
386 if (RemoteTimer != 0) {
387 now = time(NULL);
388 i = remotefirst;
389 count = remotecount;
390 found = 0;
391 while (count--) {
392 if (remotetable[i].Expires < now) {
393 remotecount--;
394 remotefirst = (remotefirst + 1) & (REMOTETABLESIZE - 1);
395 i = (i + 1) & (REMOTETABLESIZE - 1);
396 continue;
397 }
398 if (remotetable[i].Address.s_addr == remote.sin_addr.s_addr)
399 found++;
400 i = (i + 1) & (REMOTETABLESIZE - 1);
401 }
402 if (remotecount == RemoteTotal) {
403 reject_val = NNTP_SERVER_TOOBUSY_VAL;
404 reject_message = NNTP_SERVER_TOOBUSY;
405 }
406 else if (found >= RemoteLimit && !RCnolimit(&tempchan)) {
407 reject_val = NNTP_TOO_MANY_CONNECTS_VAL;
408 reject_message = NNTP_TOO_MANY_CONNECTS;
409 }
410 else {
411 i = (remotefirst + remotecount) & (REMOTETABLESIZE - 1);
412 remotetable[i].Address = remote.sin_addr;
413 remotetable[i].Expires = now + RemoteTimer;
414 remotecount++;
415 }
416 }
417
418 /*
419 ** Create a reject channel to reject the connection. This is done
420 ** to avoid a call to fork.
421 */
422 if (reject_message) {
423 new = CHANcreate(fd, CTreject, CSwritegoodbye, RCrejectreader,
424 RCrejectwritedone);
425 new->Address.s_addr = remote.sin_addr.s_addr;
426 new->Rejected = reject_val;
427 RCHANremove(new);
428 WCHANset(new, reject_message, (int)strlen(reject_message));
429 WCHANappend(new, RCterm, STRLEN(RCterm));
430 WCHANadd(new);
431 return;
432 }
433
434 /* See if it's one of our servers. */
435 for (name = NULL, rp = RCpeerlist, i = RCnpeerlist; --i >= 0; rp++)
436 /* SUPPRESS 112 *//* Retrieving long where char is stored */
437 if (rp->Address.s_addr == remote.sin_addr.s_addr) {
438 name = rp->Name;
439 break;
440 }
441
442 /* If not a server, and not allowing anyone, hand him off. */
443 if (i >= 0) {
444 new = NCcreate(fd, rp->Password[0] != '\0', FALSE);
445 new->Streaming = rp->Streaming ;
446 RCsetpathlogging(new,
447 rp->PathAddr, rp->RequirePathHostForPath,
448 rp->PathIdent, rp->PathNoIdentIdents);
449 } else if (AnyIncoming) {
450 new = NCcreate(fd, FALSE, FALSE);
451 new->Streaming = FALSE ;
452 RCsetpathlogging(new, TRUE, NULL, TRUE, NULL);
453 } else {
454 RChandoff(fd, HOnntpd);
455 if (close(fd) < 0)
456 syslog(L_ERROR, "%s cant close %d %m", LogName, fd);
457 return;
458 }
459
460 /* SUPPRESS 112 *//* Retrieving long where char is stored */
461 new->Address.s_addr = remote.sin_addr.s_addr;
462 syslog(L_NOTICE, "%s connected %d%s%s streaming %s",
463 name ? name : inet_ntoa(new->Address), new->fd,
464 new->IdentForPath ? " ident " : "",
465 new->IdentForPath ? new->IdentForPath : "",
466 (!StreamingOff || new->Streaming) ? "allowed" : "not allowed");
467 }
468
469
470 /*
471 ** Write-done function. Shouldn't happen.
472 */
473 STATIC FUNCTYPE
474 RCwritedone()
475 {
476 syslog(L_ERROR, "%s internal RCwritedone", LogName);
477 }
478
479
480 /*
481 ** Read in the file listing the hosts we take news from, and fill in the
482 ** global list of their Internet addresses. On modern systems a host can
483 ** have multiple addresses, so we take care to add all of them to the list.
484 ** We can distinguish between the two because h_addr is a #define for the
485 ** first element of the address list in modern systems, while it's a field
486 ** name in old ones.
487 */
488 STATIC void
489 RCreadfile(list, count, filename)
490 REMOTEHOST **list;
491 int *count;
492 char *filename;
493 {
494 static char NOPASS[] = "";
495 char buff[SMBUF];
496 register FILE *F;
497 register char *p;
498 struct hostent *hp;
499 register int i;
500 register REMOTEHOST *rp;
501 register int j;
502 int k ;
503 BOOL usedefaultrequirepathhost;
504 int exceptionscount;
505 char *attrib;
506 char *exceptionswalk;
507 char **exceptionsdispose;
508 char *delimiter;
509 char *pass;
510 char *pats;
511 int errors;
512
513 /* Free anything that might have been there. */
514 if (*list) {
515 for (rp = *list, i = *count; --i >= 0; rp++) {
516 DISPOSE(rp->Name);
517 DISPOSE(rp->Password);
518 if (rp->RequirePathHostForPath)
519 DISPOSE(rp->RequirePathHostForPath);
520 if (rp->PathNoIdentIdents) {
521 for (exceptionsdispose= rp->PathNoIdentIdents;
522 *exceptionsdispose;
523 exceptionsdispose++)
524 DISPOSE(*exceptionsdispose);
525 DISPOSE(rp->PathNoIdentIdents);
526 }
527 if (rp->Patterns) {
528 DISPOSE(rp->Patterns[0]);
529 DISPOSE(rp->Patterns);
530 }
531 }
532 DISPOSE(*list);
533 *list = NULL;
534 *count = 0;
535 }
536
537 /* Open the server file, count the lines. */
538 if ((F = fopen(filename, "r")) == NULL) {
539 syslog(L_FATAL, "%s cant read %s %m", LogName, filename);
540 exit(1);
541 }
542 for (i = 1; fgets(buff, sizeof buff, F) != NULL; )
543 if (buff[0] != COMMENT_CHAR && buff[0] != '\n')
544 i++;
545 *count = i;
546 rp = *list = NEW(REMOTEHOST, *count);
547 memset(rp, 0, *count * sizeof(REMOTEHOST));
548 #if !defined(DO_HAVE_UNIX_DOMAIN)
549 rp->Address.s_addr = inet_addr(LOOPBACK_HOST);
550 rp->Name = COPY("localhost");
551 rp->Password = COPY(NOPASS);
552 rp->Patterns = NULL;
553 rp->PathAddr = FALSE;
554 rp->PathIdent = FALSE;
555 rp->PathNoIdentIdents = NULL;
556 rp++;
557 #endif /* !defined(DO_HAVE_UNIX_DOMAIN) */
558
559 /* Now read the file to add all the hosts. */
560 (void)fseek(F, (OFFSET_T)0, SEEK_SET);
561 for (errors = 0; fgets(buff, sizeof buff, F) != NULL; ) {
562 /* Ignore blank and comment lines. */
563 if ((p = strchr(buff, '\n')) != NULL)
564 *p = '\0';
565 if ((p = strchr(buff, COMMENT_CHAR)) != NULL)
566 *p = '\0';
567 if (buff[0] == '\0')
568 continue;
569 if ((pass = strchr(buff, ':')) != NULL) {
570 *pass++ = '\0';
571 if ((pats = strchr(pass, ':')) != NULL)
572 *pats++ = '\0';
573 else
574 pats = NULL;
575 }
576 else {
577 pass = NOPASS;
578 pats = NULL;
579 }
580
581 rp->Streaming = FALSE;
582 rp->PathAddr = FALSE;
583 rp->PathIdent = FALSE;
584 rp->PathNoIdentIdents = NULL;
585 rp->RequirePathHostForPath = NULL;
586 usedefaultrequirepathhost = TRUE;
587
588 /* Check for `attributes', ie things after the hostname separated
589 by a /. This includes the following checks ... */
590
591 while ((attrib = strrchr(buff,'/')) != NULL) {
592 if (!strcmp(attrib,"/s")) {
593 /* Check if the host name ends with '/s' which means that streaming is
594 specifically permitted (as opposed to defaulted). The default
595 for the global StreamingOff is FALSE, meaning any host can use
596 streaming commands. If any entry is hosts.nntp has a suffix of
597 '/s' then StreamingOff is set to TRUE, and then only those
598 hosts.nntp entries with '/s' can use streaming commands. */
599
600 rp->Streaming = TRUE ;
601 StreamingOff = TRUE ;
602 #if defined(DO_IPADDR_PATH)
603 } else if (!strcmp(attrib,"/t")) {
604 /* Check if the host name ends with '/t' which means
605 that this entry's articles should never have the
606 calling IP address of the feeding host logged in the
607 Path: line of accepted articles. */
608
609 rp->PathAddr = FALSE ;
610 usedefaultrequirepathhost = FALSE;
611 } else if (!strcmp(attrib,"/a")) {
612 /* Check if the host name ends with '/a' which means that this
613 entry's articles should have the calling IP address of the feeding
614 host logged in the Path: line of all accepted articles. */
615
616 rp->PathAddr = TRUE ;
617 usedefaultrequirepathhost = FALSE;
618 if (rp->RequirePathHostForPath != NULL) {
619 DISPOSE(rp->RequirePathHostForPath);
620 rp->RequirePathHostForPath = NULL;
621 }
622 } else if (!strncmp(attrib,"/a",2)) {
623 /* Check if the host name ends with `/a<hostname>' which
624 means that this entry's articles should have the
625 calling IP address logged if it the most recent Path:
626 entry does not match the hostname supplied after `/a'. */
627
628 if (rp->RequirePathHostForPath != NULL)
629 DISPOSE(rp->RequirePathHostForPath);
630 usedefaultrequirepathhost = FALSE;
631 rp->RequirePathHostForPath = COPY(attrib+2);
632 #if defined(DO_TCPIDENT_PATH)
633 } else if (!strncmp(attrib,"/i",2)) {
634 /* Check if the host name ends with
635 '/i[<username>,<username>,...]' which means that the
636 calling IP address and RFC1413 (TCP Remote Username
637 Service, aka 'ident') value should be logged, unless
638 the ident value is one of those listed. '/i' implies '/a'. */
639
640 rp->PathIdent = TRUE ;
641 if (attrib[2] == '\0') {
642 rp->PathNoIdentIdents = NULL;
643 } else {
644 exceptionswalk = attrib+2;
645 exceptionscount = 0;
646 for (;;) {
647 exceptionscount++;
648 exceptionswalk = strchr(exceptionswalk,',');
649 if (!exceptionswalk) break;
650 exceptionswalk++;
651 }
652 rp->PathNoIdentIdents = NEW(char*,exceptionscount++);
653 exceptionscount = 0;
654 exceptionswalk = attrib+2;
655 for (;;) {
656 delimiter = strchr(exceptionswalk,',');
657 k = delimiter ? delimiter-exceptionswalk
658 : strlen(exceptionswalk);
659 rp->PathNoIdentIdents[exceptionscount] = NEW(char,k+1);
660 memcpy(rp->PathNoIdentIdents[exceptionscount],exceptionswalk,k);
661 rp->PathNoIdentIdents[exceptionscount][k] = '\0';
662 exceptionscount++;
663 if (!delimiter) break;
664 exceptionswalk = delimiter+1;
665 }
666 rp->PathNoIdentIdents[exceptionscount] = NULL;
667 }
668 #endif /* defined(DO_TCPIDENT_PATH) */
669 #endif /* defined(DO_IPADDR_PATH) */
670 } else {
671 syslog(L_NOTICE,"unknown attribute (ignored): %s",attrib);
672 }
673 *attrib = '\0';
674 }
675
676 #if defined(DO_IPADDR_PATH)
677 if (usedefaultrequirepathhost) {
678 rp->RequirePathHostForPath = COPY(buff);
679 }
680
681 #endif /* defined(DO_IPADDR_PATH) */
682
683 /* Was host specified as as dotted quad? */
684 if ((rp->Address.s_addr = inet_addr(buff)) != (unsigned int) -1) {
685 syslog(LOG_NOTICE, "think it's a dotquad: %s",buff);
686 rp->Name = COPY(buff);
687 rp->Password = COPY(pass);
688 rp->Patterns = (pats && *pats) ? CommaSplit(COPY(pats)) : NULL;
689 rp++;
690 continue;
691 }
692
693 /* Host specified as a text name? */
694 if ((hp = gethostbyname(buff)) == NULL) {
695 syslog(L_ERROR, "%s cant gethostbyname %s %m", LogName, buff);
696 errors++;
697 continue;
698 }
699
700 #if defined(h_addr)
701 /* Count the addresses and see if we have to grow the list. */
702 for (i = 0; hp->h_addr_list[i]; i++)
703 continue;
704 if (i == 0) {
705 syslog(L_ERROR, "%s no_address %s %m", LogName, buff);
706 errors++;
707 continue;
708 }
709 if (i == 1) {
710 /* Just one, no need to grow. */
711 COPYADDR(&rp->Address, hp->h_addr_list[0]);
712 rp->Name = COPY(hp->h_name);
713 rp->Password = COPY(pass);
714 rp->Patterns = (pats && *pats) ? CommaSplit(COPY(pats)) : NULL;
715 rp++;
716 continue;
717 }
718
719 /* Note the relative position, grow the array, and restore it. */
720 j = rp - *list;
721 *count += i - 1;
722 RENEW(*list, REMOTEHOST, *count);
723 rp = *list + j;
724
725 /* Add all the hosts. */
726 for (i = 0; hp->h_addr_list[i]; i++) {
727 COPYADDR(&rp->Address, hp->h_addr_list[i]);
728 rp->Name = COPY(hp->h_name);
729 rp->Password = COPY(pass);
730 rp->Patterns = (pats && *pats) ? CommaSplit(COPY(pats)) : NULL;
731 rp->Streaming = (*list + j)->Streaming ;
732 rp->RequirePathHostForPath = COPY(hp->h_name);
733 rp->PathAddr = (*list + j)->PathAddr;
734 rp->PathIdent = (*list + j)->PathIdent;
735 rp->PathNoIdentIdents = NULL;
736 rp++;
737 }
738 #else
739 /* Old-style, single address, just add it. */
740 COPYADDR(&rp->Address, hp->h_addr);
741 rp->Name = COPY(hp->h_name);
742 rp->Password = COPY(pass);
743 rp->Patterns = (pats && *pats) ? CommaSplit(COPY(pats)) : NULL;
744 rp++;
745 #endif /* defined(h_addr) */
746 }
747 *count = rp - *list;
748
749 if (fclose(F) == EOF)
750 syslog(L_ERROR, "%s cant fclose %s %m", LogName, filename);
751
752 if (errors)
753 syslog(L_ERROR, "%s bad_hosts %d in %s", LogName, errors, filename);
754 }
755
756
757 void
758 RCreadlist()
759 {
760 static char INNDHOSTS[] = _PATH_INNDHOSTS;
761 char name[sizeof _PATH_INNDHOSTS + sizeof ".nolimit"];
762 struct stat Sb;
763
764 StreamingOff = FALSE ;
765 RCreadfile(&RCpeerlist, &RCnpeerlist, INNDHOSTS);
766 FileGlue(name, INNDHOSTS, '.', "nolimit");
767 if (stat(name, &Sb) >= 0)
768 RCreadfile(&RCnolimitlist, &RCnnolimitlist, name);
769 }
770
771
772
773 /*
774 ** Find the name of a remote host we've connected to.
775 */
776 char *
777 RChostname(cp)
778 register CHANNEL *cp;
779 {
780 static char buff[SMBUF];
781 register REMOTEHOST *rp;
782 register int i;
783
784 for (rp = RCpeerlist, i = RCnpeerlist; --i >= 0; rp++)
785 /* SUPPRESS 112 *//* Retrieving long where char is stored */
786 if (cp->Address.s_addr == rp->Address.s_addr)
787 return rp->Name;
788 (void)strcpy(buff, inet_ntoa(cp->Address));
789 return buff;
790 }
791
792
793 /*
794 ** Is the remote site allowed to post to this group?
795 */
796 int
797 RCcanpost(cp, group)
798 register CHANNEL *cp;
799 register char *group;
800 {
801 register REMOTEHOST *rp;
802 register char match;
803 register char subvalue;
804 register char **argv;
805 register char *pat;
806 register int i;
807
808 for (rp = RCpeerlist, i = RCnpeerlist; --i >= 0; rp++) {
809 /* SUPPRESS 112 *//* Retrieving long where char is stored */
810 if (cp->Address.s_addr != rp->Address.s_addr)
811 continue;
812 if (rp->Patterns == NULL)
813 break;
814 for (match = 0, argv = rp->Patterns; (pat = *argv++) != NULL; ) {
815 subvalue = (*pat != SUB_NEGATE) && (*pat != SUB_POISON) ?
816 0 : *pat;
817 if (subvalue)
818 pat++;
819 if ((match != subvalue) && wildmat(group, pat)) {
820 if (subvalue == SUB_POISON)
821 return -1;
822 match = subvalue;
823 }
824 }
825 return !match;
826 }
827 return 1;
828 }
829
830
831 /*
832 ** Create the channel.
833 */
834 void
835 RCsetup(i, master)
836 register int i;
837 char *master;
838 {
839 struct sockaddr_in server;
840 struct hostent *hp;
841 INADDR a;
842 char buff[SMBUF];
843 #if defined(SO_REUSEADDR)
844 int on;
845 #endif /* defined(SO_REUSEADDR) */
846
847 if (i < 0) {
848 /* Create a socket and name it. */
849 if ((i = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
850 syslog(L_FATAL, "%s cant socket RCreader %m", LogName);
851 exit(1);
852 }
853 #if defined(SO_REUSEADDR)
854 on = 1;
855 if (setsockopt(i, SOL_SOCKET, SO_REUSEADDR,
856 (caddr_t)&on, sizeof on) < 0)
857 syslog(L_ERROR, "%s cant setsockopt RCreader %m", LogName);
858 #endif /* defined(SO_REUSEADDR) */
859 (void)memset((POINTER)&server, 0, sizeof server);
860 server.sin_port = htons(atoi(GetConfigValue(_CONF_PORT)));
861 server.sin_family = AF_INET;
862 server.sin_addr.s_addr = inet_addr(GetConfigValue(_CONF_BINDADDRESS));
863 if (bind(i, (struct sockaddr *)&server, sizeof server) < 0) {
864 syslog(L_FATAL, "%s cant bind RCreader %m", LogName);
865 exit(1);
866 }
867 }
868
869 /* Set it up to wait for connections. */
870 if (listen(i, MAXLISTEN) < 0) {
871 syslog(L_FATAL, "%s cant listen RCreader %m", LogName);
872 exit(1);
873 }
874 RCchan = CHANcreate(i, CTremconn, CSwaiting, RCreader, RCwritedone);
875 syslog(L_NOTICE, "%s rcsetup %s", LogName, CHANname(RCchan));
876 RCHANadd(RCchan);
877
878 /* Get the list of hosts we handle. */
879 RCreadlist();
880
881 /* If we have a master, get all his addresses. */
882 AmSlave = master != NULL;
883 if (AmSlave) {
884 /* Dotted quad? */
885 if ((a.s_addr = inet_addr(master)) != (unsigned int) -1) {
886 RCnmaster = 1;
887 RCmaster = NEW(INADDR, 1);
888 COPYADDR(&RCmaster[0], &a);
889 }
890 else {
891 /* Must be a text name. */
892 if ((hp = gethostbyname(master)) == NULL) {
893 syslog(L_FATAL, "%s cant gethostbyname %s %m", LogName, master);
894 exit(1);
895 }
896 #if defined(h_addr)
897 /* Count the addresses. */
898 for (i = 0; hp->h_addr_list[i]; i++)
899 continue;
900 if (i == 0) {
901 syslog(L_FATAL, "%s no_address %s %m", LogName, master);
902 exit(1);
903 }
904 RCnmaster = i;
905 RCmaster = NEW(INADDR, RCnmaster);
906 for (i = 0; hp->h_addr_list[i]; i++)
907 COPYADDR(&RCmaster[i], hp->h_addr_list[i]);
908 #else
909 RCnmaster = 1;
910 RCmaster = NEW(INADDR, 1)
911 COPYADDR(&RCmaster[0], &a);
912 #endif /* defined(h_addr) */
913 }
914
915 /* Set flag for nnrp. */
916 (void)sprintf(buff, "-S%s", master);
917 RCslaveflag = COPY(buff);
918 }
919 }
920
921
922 /*
923 ** Cleanly shut down the channel.
924 */
925 void
926 RCclose()
927 {
928 register REMOTEHOST *rp;
929 register int i;
930
931 CHANclose(RCchan, CHANname(RCchan));
932 RCchan = NULL;
933 if (RCpeerlist) {
934 for (rp = RCpeerlist, i = RCnpeerlist; --i >= 0; rp++) {
935 DISPOSE(rp->Name);
936 DISPOSE(rp->Password);
937 if (rp->Patterns)
938 DISPOSE(rp->Patterns);
939 }
940 DISPOSE(RCpeerlist);
941 RCpeerlist = NULL;
942 RCnpeerlist = 0;
943 }
944
945 if (RCmaster) {
946 DISPOSE(RCmaster);
947 RCmaster = NULL;
948 RCnmaster = 0;
949 }
950 }
951
952 /*
953 * Routine to disable IP-level socket options. This code was taken from 4.4BSD
954 * rlogind source, but all mistakes in it are my fault.
955 *
956 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
957 *
958 * 21-Jan-1997 smd
959 * Code copied again, and modified for INN, all new mistakes are mine.
960 *
961 */
962
963 /* fix_options - get rid of IP-level socket options */
964 #ifndef IP_OPTIONS
965 #define IP_OPTIONS 1
966 #endif
967
968 int
969 RCfix_options(fd, remote)
970 int fd;
971 struct sockaddr_in *remote;
972 {
973 #if IP_OPTIONS
974 unsigned char optbuf[BUFSIZ / 3], *cp;
975 char lbuf[BUFSIZ], *lp;
976 int optsize = sizeof(optbuf), ipproto;
977 struct protoent *ip;
978
979 if ((ip = getprotobyname("ip")) != 0)
980 ipproto = ip->p_proto;
981 else
982 ipproto = IPPROTO_IP;
983
984 if (getsockopt(fd, ipproto, IP_OPTIONS, (char *) optbuf, &optsize) == 0
985 && optsize != 0) {
986 lp = lbuf;
987 for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
988 sprintf(lp, " %2.2x", *cp);
989 syslog(LOG_NOTICE,
990 "connect from %s with IP options (ignored):%s",
991 inet_ntoa(remote->sin_addr), lbuf);
992 if (setsockopt(fd, ipproto, IP_OPTIONS, (char *) 0, optsize) != 0) {
993 syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
994 return -1;
995 }
996 }
997 #endif
998 return 0;
999 }