]> code.citadel.org Git - citadel.git/blob - citadel/netproc.c
* Makefile.in, configure.in: link netproc with gdbm
[citadel.git] / citadel / netproc.c
1 /*
2  * Citadel/UX Intelligent Network Processor for IGnet/Open networks
3  * See copyright.txt for copyright information
4  * $Id$
5  */
6
7 /* How long it takes for an old node to drop off the network map */
8 #define EXPIRY_TIME     (2592000L)
9
10 /* How long we keep recently arrived messages in the use table */
11 #define USE_TIME        (604800L)
12
13 /* Where do we keep our lock file? */
14 #define LOCKFILE        "/var/lock/LCK.netproc"
15
16 /* Path to the 'uudecode' utility (needed for network file transfers) */
17 #define UUDECODE        "/usr/bin/uudecode"
18
19 /* Uncomment the DEBUG def to see noisy traces */
20 /* #define DEBUG 1 */
21
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <time.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <syslog.h>
36 #ifdef HAVE_GDBM_H
37 #include <gdbm.h>
38 #endif
39 #include "citadel.h"
40 #include "tools.h"
41
42 /* A list of users you wish to filter out of incoming traffic can be kept
43  * in ./network/filterlist -- messages from these users will be automatically
44  * moved to FILTERROOM.  Normally this will be the same as TWITROOM (the
45  * room problem user messages are moved to) but you can override this by
46  * specifying a different room name here.
47  */
48 #ifndef FILTERROOM
49 #define FILTERROOM TWITROOM
50 #endif
51
52 struct msglist {
53         struct msglist *next;
54         long m_num;
55         char m_rmname[ROOMNAMELEN];
56 };
57
58 struct rmlist {
59         struct rmlist *next;
60         char rm_name[ROOMNAMELEN];
61         long rm_lastsent;
62 };
63
64 struct filterlist {
65         struct filterlist *next;
66         char f_person[64];
67         char f_room[64];
68         char f_system[64];
69 };
70
71 struct syslist {
72         struct syslist *next;
73         char s_name[16];
74         char s_type[4];
75         char s_nexthop[128];
76         time_t s_lastcontact;
77         char s_humannode[64];
78         char s_phonenum[32];
79         char s_gdom[64];
80 };
81
82
83 void attach_to_server(int argc, char **argv);
84 void serv_read(char *buf, int bytes);
85 void serv_write(char *buf, int nbytes);
86 void get_config(void);
87
88 struct filterlist *filter = NULL;
89 struct syslist *slist = NULL;
90
91 struct config config;
92 extern char bbs_home_directory[];
93 extern int home_specified;
94
95
96 #ifndef HAVE_STRERROR
97 /*
98  * replacement strerror() for systems that don't have it
99  */
100 char *strerror(int e)
101 {
102         static char buf[32];
103
104         sprintf(buf, "errno = %d", e);
105         return (buf);
106 }
107 #endif
108
109
110 void strip_trailing_whitespace(char *buf)
111 {
112         while (isspace(buf[strlen(buf) - 1]))
113                 buf[strlen(buf) - 1] = 0;
114 }
115
116
117 /*
118  * we also load the network/mail.sysinfo table into memory, make changes
119  * as we learn more about the network from incoming messages, and write
120  * the table back to disk when we're done.
121  */
122 int load_syslist(void)
123 {
124         FILE *fp;
125         struct syslist *stemp;
126         char insys = 0;
127         char buf[128];
128
129         fp = fopen("network/mail.sysinfo", "r");
130         if (fp == NULL)
131                 return (1);
132
133         while (1) {
134                 if (fgets(buf, 128, fp) == NULL) {
135                         fclose(fp);
136                         return (0);
137                 }
138                 buf[strlen(buf) - 1] = 0;
139                 while (isspace(buf[0]))
140                         strcpy(buf, &buf[1]);
141                 if (buf[0] == '#')
142                         buf[0] = 0;
143                 if ((insys == 0) && (strlen(buf) != 0)) {
144                         insys = 1;
145                         stemp = (struct syslist *) malloc(sizeof(struct syslist));
146                         stemp->next = slist;
147                         slist = stemp;
148                         strcpy(slist->s_name, buf);
149                         strcpy(slist->s_type, "bin");
150                         strcpy(slist->s_nexthop, "Mail");
151                         slist->s_lastcontact = 0L;
152                         strcpy(slist->s_humannode, "");
153                         strcpy(slist->s_phonenum, "");
154                         strcpy(slist->s_gdom, "");
155                 } else if ((insys == 1) && (strlen(buf) == 0)) {
156                         insys = 0;
157                 } else if ((insys == 1) && (!strncasecmp(buf, "bin", 3))) {
158                         strcpy(slist->s_type, "bin");
159                         strcpy(slist->s_nexthop, &buf[4]);
160                 } else if ((insys == 1) && (!strncasecmp(buf, "use", 3))) {
161                         strcpy(slist->s_type, "use");
162                         strcpy(slist->s_nexthop, &buf[4]);
163                 } else if ((insys == 1) && (!strncasecmp(buf, "uum", 3))) {
164                         strcpy(slist->s_type, "uum");
165                         strcpy(slist->s_nexthop, &buf[4]);
166                 } else if ((insys == 1) && (!strncasecmp(buf, "lastcontact", 11))) {
167                         long foo;
168                         sscanf(&buf[12], "%ld", &foo);
169                         slist->s_lastcontact = foo;
170                 } else if ((insys == 1) && (!strncasecmp(buf, "humannode", 9))) {
171                         strcpy(slist->s_humannode, &buf[10]);
172                 } else if ((insys == 1) && (!strncasecmp(buf, "phonenum", 8))) {
173                         strcpy(slist->s_phonenum, &buf[9]);
174                 } else if ((insys == 1) && (!strncasecmp(buf, "gdom", 4))) {
175                         strcpy(slist->s_gdom, &buf[5]);
176                 }
177         }
178 }
179
180 /* now we have to set up two "special" nodes on the list: one
181  * for the local node, and one for an Internet gateway
182  */
183 void setup_special_nodes(void)
184 {
185         struct syslist *stemp, *slocal;
186
187         slocal = NULL;
188         for (stemp = slist; stemp != NULL; stemp = stemp->next) {
189                 if (!strcasecmp(stemp->s_name, config.c_nodename))
190                         slocal = stemp;
191         }
192         if (slocal == NULL) {
193                 slocal = (struct syslist *) malloc(sizeof(struct syslist));
194                 slocal->next = slist;
195                 slist = slocal;
196         }
197         strcpy(slocal->s_name, config.c_nodename);
198         strcpy(slocal->s_type, "bin");
199         strcpy(slocal->s_nexthop, "Mail");
200         time(&slocal->s_lastcontact);
201         strcpy(slocal->s_humannode, config.c_humannode);
202         strcpy(slocal->s_phonenum, config.c_phonenum);
203
204         slocal = NULL;
205         for (stemp = slist; stemp != NULL; stemp = stemp->next) {
206                 if (!strcasecmp(stemp->s_name, "internet"))
207                         slocal = stemp;
208         }
209         if (slocal == NULL) {
210                 slocal = (struct syslist *) malloc(sizeof(struct syslist));
211                 slocal->next = slist;
212                 slist = slocal;
213         }
214         strcpy(slocal->s_name, "internet");
215         strcpy(slocal->s_type, "uum");
216         strcpy(slocal->s_nexthop, "%s");
217         time(&slocal->s_lastcontact);
218         strcpy(slocal->s_humannode, "Internet Gateway");
219         strcpy(slocal->s_phonenum, "");
220         strcpy(slocal->s_gdom, "");
221
222 }
223
224 /*
225  * here's the routine to write the table back to disk.
226  */
227 void rewrite_syslist(void)
228 {
229         struct syslist *stemp;
230         FILE *newfp;
231         time_t now;
232
233         time(&now);
234         newfp = fopen("network/mail.sysinfo", "w");
235         for (stemp = slist; stemp != NULL; stemp = stemp->next) {
236                 if (!strcasecmp(stemp->s_name, config.c_nodename)) {
237                         time(&stemp->s_lastcontact);
238                         strcpy(stemp->s_type, "bin");
239                         strcpy(stemp->s_humannode, config.c_humannode);
240                         strcpy(stemp->s_phonenum, config.c_phonenum);
241                 }
242                 /* remove systems we haven't heard from in a while */
243                 if ((stemp->s_lastcontact == 0L)
244                     || (now - stemp->s_lastcontact < EXPIRY_TIME)) {
245                         fprintf(newfp, "%s\n%s %s\n",
246                          stemp->s_name, stemp->s_type, stemp->s_nexthop);
247                         if (strlen(stemp->s_phonenum) > 0)
248                                 fprintf(newfp, "phonenum %s\n", stemp->s_phonenum);
249                         if (strlen(stemp->s_gdom) > 0)
250                                 fprintf(newfp, "gdom %s\n", stemp->s_gdom);
251                         if (strlen(stemp->s_humannode) > 0)
252                                 fprintf(newfp, "humannode %s\n", stemp->s_humannode);
253                         if (stemp->s_lastcontact > 0L)
254                                 fprintf(newfp, "lastcontact %ld %s",
255                                         (long) stemp->s_lastcontact,
256                                         asctime(localtime(&stemp->s_lastcontact)));
257                         fprintf(newfp, "\n");
258                 }
259         }
260         fclose(newfp);
261         /* now free the list */
262         while (slist != NULL) {
263                 stemp = slist;
264                 slist = slist->next;
265                 free(stemp);
266         }
267 }
268
269
270 /* call this function with the node name of a system and it returns a pointer
271  * to its syslist structure.
272  */
273 struct syslist *get_sys_ptr(char *sysname)
274 {
275         static char sysnambuf[16];
276         static struct syslist *sysptrbuf = NULL;
277         struct syslist *stemp;
278
279         if ((!strcmp(sysname, sysnambuf))
280             && (sysptrbuf != NULL))
281                 return (sysptrbuf);
282
283         strcpy(sysnambuf, sysname);
284         for (stemp = slist; stemp != NULL; stemp = stemp->next) {
285                 if (!strcmp(sysname, stemp->s_name)) {
286                         sysptrbuf = stemp;
287                         return (stemp);
288                 }
289         }
290         sysptrbuf = NULL;
291         return (NULL);
292 }
293
294
295 /*
296  * make sure only one copy of netproc runs at a time, using lock files
297  */
298 int set_lockfile(void)
299 {
300         FILE *lfp;
301         int onppid;
302
303         if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
304                 fscanf(lfp, "%d", &onppid);
305                 fclose(lfp);
306                 if (!kill(onppid, 0) || errno == EPERM)
307                         return 1;
308         }
309         lfp = fopen(LOCKFILE, "w");
310         fprintf(lfp, "%ld\n", (long) getpid());
311         fclose(lfp);
312         return (0);
313 }
314
315 void remove_lockfile(void)
316 {
317         unlink(LOCKFILE);
318 }
319
320 /*
321  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
322  * cleanup() .  If for some reason netproc hangs waiting for the server
323  * to clean up, the alarm clock goes off and the program exits anyway.
324  * The cleanup() routine makes a check to ensure it's not reentering, in
325  * case the ipc module looped it somehow.
326  */
327 void nq_cleanup(int e)
328 {
329         remove_lockfile();
330         closelog();
331         exit(e);
332 }
333
334 void cleanup(int e)
335 {
336         static int nested = 0;
337
338         alarm(30);
339         signal(SIGALRM, nq_cleanup);
340         if (nested++ < 1)
341                 serv_puts("QUIT");
342         nq_cleanup(e);
343 }
344
345 /*
346  * This is implemented as a function rather than as a macro because the
347  * client-side IPC modules expect logoff() to be defined.  They call logoff()
348  * when a problem connecting or staying connected to the server occurs.
349  */
350 void logoff(int e)
351 {
352         cleanup(e);
353 }
354
355 /*
356  * If there is a kill file in place, this function will process it.
357  */
358 void load_filterlist(void)
359 {
360         FILE *fp;
361         struct filterlist *fbuf;
362         char sbuf[256];
363         int a, p;
364         fp = fopen("./network/filterlist", "r");
365         if (fp == NULL)
366                 return;
367         while (fgets(sbuf, 256, fp) != NULL) {
368                 if (sbuf[0] != '#') {
369                         sbuf[strlen(sbuf) - 1] = 0;
370                         fbuf = (struct filterlist *)
371                             malloc((long) sizeof(struct filterlist));
372                         fbuf->next = filter;
373                         filter = fbuf;
374                         strcpy(fbuf->f_person, "*");
375                         strcpy(fbuf->f_room, "*");
376                         strcpy(fbuf->f_system, "*");
377                         p = (-1);
378                         for (a = strlen(sbuf); a >= 0; --a)
379                                 if (sbuf[a] == ',')
380                                         p = a;
381                         if (p >= 0) {
382                                 sbuf[p] = 0;
383                                 strcpy(fbuf->f_person, sbuf);
384                                 strcpy(sbuf, &sbuf[p + 1]);
385                         }
386                         for (a = strlen(sbuf); a >= 0; --a)
387                                 if (sbuf[a] == ',')
388                                         p = a;
389                         if (p >= 0) {
390                                 sbuf[p] = 0;
391                                 strcpy(fbuf->f_room, sbuf);
392                                 strcpy(sbuf, &sbuf[p + 1]);
393                         }
394                         strcpy(fbuf->f_system, sbuf);
395                 }
396         }
397         fclose(fp);
398 }
399
400 /* returns 1 if user/message/room combination is in the kill file */
401 int is_banned(char *k_person, char *k_room, char *k_system)
402 {
403         struct filterlist *fptr;
404
405         for (fptr = filter; fptr != NULL; fptr = fptr->next)
406                 if (
407                            ((!strcasecmp(fptr->f_person, k_person)) || (!strcmp(fptr->f_person, "*")))
408                            &&
409                            ((!strcasecmp(fptr->f_room, k_room)) || (!strcmp(fptr->f_room, "*")))
410                            &&
411                            ((!strcasecmp(fptr->f_system, k_system)) || (!strcmp(fptr->f_system, "*")))
412                     )
413                         return (1);
414
415         return (0);
416 }
417
418 /*
419  * Determine routing from sysinfo file
420  */
421 int get_sysinfo_type(char *name) {
422         struct syslist *stemp;
423
424 GETSN:  for (stemp = slist; stemp != NULL; stemp = stemp->next) {
425                 if (!strcasecmp(stemp->s_name, name)) {
426                         if (!strcasecmp(stemp->s_type, "use")) {
427                                 strcpy(name, stemp->s_nexthop);
428                                 goto GETSN;
429                         }
430                         if (!strcasecmp(stemp->s_type, "bin")) {
431                                 return (MES_BINARY);
432                         }
433                         if (!strcasecmp(stemp->s_type, "uum")) {
434                                 return (MES_INTERNET);
435                         }
436                 }
437         }
438         syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
439         return (-1);
440 }
441
442
443 void fpgetfield(FILE * fp, char *string)
444 {
445         int a, b;
446
447         strcpy(string, "");
448         a = 0;
449         do {
450                 b = getc(fp);
451                 if (b < 1) {
452                         string[a] = 0;
453                         return;
454                 }
455                 string[a] = b;
456                 ++a;
457         } while (b != 0);
458 }
459
460
461
462 /*
463  * Load all of the fields of a message, except the actual text, into a
464  * table in memory (so we know how to process the message).
465  */
466 void msgfind(char *msgfile, struct minfo *buffer)
467 {
468         int b, e, mtype, aflag;
469         char bbb[1024];
470         char userid[1024];
471         FILE *fp;
472
473         strcpy(userid, "");
474         fp = fopen(msgfile, "rb");
475         if (fp == NULL) {
476                 syslog(LOG_ERR, "can't open message file: %s", strerror(errno));
477                 return;
478         }
479         e = getc(fp);
480         if (e != 255) {
481                 syslog(LOG_ERR, "incorrect message format");
482                 goto END;
483         }
484         mtype = getc(fp);
485         aflag = getc(fp);
486         buffer->I = 0L;
487         buffer->R[0] = 0;
488         buffer->E[0] = 0;
489         buffer->H[0] = 0;
490         buffer->S[0] = 0;
491         buffer->B[0] = 0;
492         buffer->G[0] = 0;
493
494 BONFGM: b = getc(fp);
495         if (b < 0)
496                 goto END;
497         if (b == 'M')
498                 goto END;
499         fpgetfield(fp, bbb);
500         while ((bbb[0] == ' ') && (strlen(bbb) > 1))
501                 strcpy(bbb, &bbb[1]);
502         if (b == 'A') {
503                 strcpy(buffer->A, bbb);
504                 if (strlen(userid) == 0) {
505                         strcpy(userid, bbb);
506                         for (e = 0; e < strlen(userid); ++e)
507                                 if (userid[e] == ' ')
508                                         userid[e] = '_';
509                 }
510         }
511         if (b == 'O')
512                 strcpy(buffer->O, bbb);
513         if (b == 'C')
514                 strcpy(buffer->C, bbb);
515         if (b == 'N')
516                 strcpy(buffer->N, bbb);
517         if (b == 'S')
518                 strcpy(buffer->S, bbb);
519         if (b == 'P') {
520                 /* extract the user id from the path */
521                 for (e = 0; e < strlen(bbb); ++e)
522                         if (bbb[e] == '!')
523                                 strcpy(userid, &bbb[e + 1]);
524
525                 /* now find the next hop */
526                 for (e = 0; e < strlen(bbb); ++e)
527                         if (bbb[e] == '!')
528                                 bbb[e] = 0;
529                 strcpy(buffer->nexthop, bbb);
530         }
531         if (b == 'R') {
532                 for (e = 0; e < strlen(bbb); ++e)
533                         if (bbb[e] == '_')
534                                 bbb[e] = ' ';
535                 strcpy(buffer->R, bbb);
536         }
537         if (b == 'D')
538                 strcpy(buffer->D, bbb);
539         if (b == 'T')
540                 buffer->T = atol(bbb);
541         if (b == 'I')
542                 buffer->I = atol(bbb);
543         if (b == 'H')
544                 strcpy(buffer->H, bbb);
545         if (b == 'B')
546                 strcpy(buffer->B, bbb);
547         if (b == 'G')
548                 strcpy(buffer->G, bbb);
549         if (b == 'E')
550                 strcpy(buffer->E, bbb);
551         goto BONFGM;
552
553 END:    fclose(fp);
554
555         /* NOTE: we used to use the following two lines of code to assign
556          * the timestamp as a message-ID if there was no message-ID already
557          * in the message.  We don't do this anymore because it screws up
558          * the loopzapper.
559          *
560         if (buffer->I == 0L)
561                 buffer->I = buffer->T;
562          */
563 }
564
565
566
567
568
569 void ship_to(char *filenm, char *sysnm)
570 {                               /* send spool file filenm to system sysnm */
571         char sysflnm[100];
572         char commbuf1[100];
573         char commbuf2[100];
574         FILE *sysflfd;
575
576 #ifdef DEBUG
577         syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
578 #endif
579         sprintf(sysflnm, "./network/systems/%s", sysnm);
580         sysflfd = fopen(sysflnm, "r");
581         if (sysflfd == NULL)
582                 syslog(LOG_ERR, "cannot open %s", sysflnm);
583         fgets(commbuf1, 99, sysflfd);
584         commbuf1[strlen(commbuf1) - 1] = 0;
585         fclose(sysflfd);
586         sprintf(commbuf2, commbuf1, filenm);
587         system(commbuf2);
588 }
589
590 /*
591  * proc_file_transfer()  -  handle a simple file transfer packet
592  *
593  */
594 void proc_file_transfer(char *tname)
595 {                               /* name of temp file containing the whole message */
596         char buf[256];
597         char dest_room[ROOMNAMELEN];
598         char subdir_name[256];
599         FILE *tfp, *uud;
600         int a;
601
602         syslog(LOG_NOTICE, "processing network file transfer...");
603
604         tfp = fopen(tname, "rb");
605         if (tfp == NULL)
606                 syslog(LOG_ERR, "cannot open %s", tname);
607         getc(tfp);
608         getc(tfp);
609         getc(tfp);
610         do {
611                 a = getc(tfp);
612                 if (a != 'M') {
613                         fpgetfield(tfp, buf);
614                         if (a == 'O') {
615                                 strcpy(dest_room, buf);
616                         }
617                 }
618         } while ((a != 'M') && (a >= 0));
619         if (a != 'M') {
620                 fclose(tfp);
621                 syslog(LOG_ERR, "no message text for file transfer");
622                 return;
623         }
624         strcpy(subdir_name, "---xxx---");
625         sprintf(buf, "GOTO %s", dest_room);
626         serv_puts(buf);
627         serv_gets(buf);
628         if (buf[0] == '2') {
629                 extract(subdir_name, &buf[4], 2);
630                 if (strlen(subdir_name) == 0)
631                         strcpy(subdir_name, "--xxx--");
632         }
633         /* Change to the room's directory; if that fails, change to the
634          * bitbucket directory.  Then run uudecode.
635          */
636         sprintf(buf, "(cd %s/files/%s || cd %s/files/%s ) ; exec %s",
637                 bbs_home_directory, subdir_name,
638                 bbs_home_directory, config.c_bucket_dir,
639                 UUDECODE);
640
641         uud = (FILE *) popen(buf, "w");
642         if (uud == NULL) {
643                 syslog(LOG_ERR, "cannot open uudecode pipe");
644                 fclose(tfp);
645                 return;
646         }
647         fgets(buf, 128, tfp);
648         buf[strlen(buf) - 1] = 0;
649         for (a = 0; a < strlen(buf); ++a)
650                 if (buf[a] == '/')
651                         buf[a] = '_';
652         fprintf(uud, "%s\n", buf);
653         printf("netproc: %s\n", buf);
654         while (a = getc(tfp), a > 0)
655                 putc(a, uud);
656         fclose(tfp);
657         pclose(uud);
658         return;
659 }
660
661
662 /* send a bounce message */
663 void bounce(struct minfo *bminfo)
664 {
665
666         FILE *bounce;
667         char bfilename[64];
668         static int bseq = 1;
669         time_t now;
670
671         sprintf(bfilename, "./network/spoolin/bounce.%ld.%d", (long) getpid(),
672                 bseq++);
673         bounce = fopen(bfilename, "wb");
674         time(&now);
675
676         fprintf(bounce, "%c%c%c", 0xFF, MES_NORMAL, 0);
677         fprintf(bounce, "Ppostmaster%c", 0);
678         fprintf(bounce, "T%ld%c", (long) now, 0);
679         fprintf(bounce, "APostmaster%c", 0);
680         fprintf(bounce, "OMail%c", 0);
681         fprintf(bounce, "N%s%c", config.c_nodename, 0);
682         fprintf(bounce, "H%s%c", config.c_humannode, 0);
683
684         if (strlen(bminfo->E) > 0) {
685                 fprintf(bounce, "R%s%c", bminfo->E, 0);
686         } else {
687                 fprintf(bounce, "R%s%c", bminfo->A, 0);
688         }
689
690         fprintf(bounce, "D%s%c", bminfo->N, 0);
691         fprintf(bounce, "M%s could not deliver your mail to:\n",
692                 config.c_humannode);
693         fprintf(bounce, " \n %s\n \n", bminfo->R);
694         fprintf(bounce, " because there is no such user on this system.\n");
695         fprintf(bounce, " (Unsent message does *not* follow.  ");
696         fprintf(bounce, "Help to conserve bandwidth.)\n%c", 0);
697         fclose(bounce);
698 }
699
700
701
702
703 /*
704  * Generate a Message-ID string for the use table
705  */
706 void strmsgid(char *buf, struct minfo *msginfo) {
707         int i;
708
709         sprintf(buf, "%ld@%s", msginfo->I, msginfo->N);
710         for (i=0; i<strlen(buf); ++i) {
711                 if (isspace(buf[i])) {
712                         strcpy(&buf[i], &buf[i+1]);
713                 }
714                 buf[i] = tolower(buf[i]);
715         }
716 }
717
718
719
720 /*
721  * Check the use table to see if a message has been here before.
722  * Returns 1 if the message is a duplicate; otherwise, it returns
723  * 0 and the message ID is added to the use table.
724  */
725 int already_received(GDBM_FILE ut, struct minfo *msginfo) {
726         char buf[256];
727         time_t now;
728         datum mkey, newrec;
729         int retval = 0;
730
731         /* We can't check for dups on a zero msgid, so just pass them through */
732         if ((msginfo->I)==0L) {
733                 return 0;
734         }
735
736         strmsgid(buf, msginfo);
737         now = time(NULL);
738
739         mkey.dptr = buf;
740         mkey.dsize = strlen(buf);
741
742         /* Set return value to 1 if message exists */
743         if (gdbm_exists(ut, mkey)) {
744                 retval = 1;
745         }
746
747         /* Write a record into the use table for this message.
748          * Replace existing records; this keeps the timestamp fresh.
749          */
750         newrec.dptr = (char *)&now;
751         newrec.dsize = sizeof(now);
752         gdbm_store(ut, mkey, newrec, GDBM_REPLACE);
753
754         return(retval);
755 }
756
757
758
759 /*
760  * Purge any old entries out of the use table.
761  * 
762  * Yes, you're reading this correctly: it keeps traversing the table until
763  * it manages to do a complete pass without deleting any records.  Read the
764  * gdbm man page to find out why.
765  *
766  */
767 void purge_use_table(GDBM_FILE ut) {
768         datum mkey, nextkey, therec;
769         int purged_anything = 0;
770         time_t rec_timestamp, now;
771
772         now = time(NULL);
773
774         do {
775                 purged_anything = 0;
776                 mkey = gdbm_firstkey(ut);
777                 while (mkey.dptr != NULL) {
778                         therec = gdbm_fetch(ut, mkey);
779                         if (therec.dptr != NULL) {
780                                 memcpy(&rec_timestamp, therec.dptr,
781                                         sizeof(time_t));
782                                 free(therec.dptr);
783
784                                 if ((now - rec_timestamp) > USE_TIME) {
785                                         gdbm_delete(ut, mkey);
786                                         purged_anything = 1;
787                                 }
788
789                         }
790                         nextkey = gdbm_nextkey(ut, mkey);
791                         free(mkey.dptr);
792                         mkey = nextkey;
793                 }
794         } while (purged_anything != 0);
795 }
796
797
798
799 /*
800  * process incoming files in ./network/spoolin
801  */
802 void inprocess(void)
803 {
804         FILE *fp, *message, *testfp, *ls, *duplist;
805         static struct minfo minfo;
806         struct recentmsg recentmsg;
807         char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
808         int a, b;
809         int FieldID;
810         struct syslist *stemp;
811         char *ptr = NULL;
812         char buf[256];
813         long msglen;
814         int bloklen;
815         GDBM_FILE use_table;
816
817         /* temp file names */
818         sprintf(tname, tmpnam(NULL));
819         sprintf(iname, tmpnam(NULL));
820
821         load_filterlist();
822
823         /* Make sure we're in the right directory */
824         chdir(bbs_home_directory);
825
826         /* Open the use table */
827         use_table = gdbm_open("./data/usetable.gdbm", 512,
828                               GDBM_WRCREAT, 0600, 0);
829         if (use_table == NULL) {
830                 syslog(LOG_ERR, "could not open use table: %s",
831                        strerror(errno));
832         }
833
834
835         /* temporary file to contain a log of rejected dups */
836         duplist = tmpfile();
837
838         /* Let the shell do the dirty work. Get all data from spoolin */
839         do {
840                 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
841                 ls = popen(aaa, "r");
842                 if (ls == NULL) {
843                         syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
844                 }
845                 if (ls != NULL) {
846                         do {
847 SKIP:                           ptr = fgets(sfilename, sizeof sfilename, ls);
848                                 if (ptr != NULL) {
849                                         sfilename[strlen(sfilename) - 1] = 0;
850                                         if (!strcmp(sfilename, ".")) goto SKIP;
851                                         if (!strcmp(sfilename, "..")) goto SKIP;
852                                         if (!strcmp(sfilename, "CVS")) goto SKIP;
853                                         goto PROCESS_IT;
854                                 }
855                         } while (ptr != NULL);
856 PROCESS_IT:             pclose(ls);
857                 }
858                 if (ptr != NULL) {
859                         sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
860                         syslog(LOG_NOTICE, "processing <%s>", pfilename);
861
862                         fp = fopen(pfilename, "rb");
863                         if (fp == NULL) {
864                                 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
865                                 fp = fopen("/dev/null", "rb");
866                         }
867 NXMSG:  /* Seek to the beginning of the next message */
868                         do {
869                                 a = getc(fp);
870                         } while ((a != 255) && (a >= 0));
871                         if (a < 0)
872                                 goto ENDSTR;
873
874                         /* This crates the temporary file. */
875                         message = fopen(tname, "wb");
876                         if (message == NULL) {
877                                 syslog(LOG_ERR, "error creating %s: %s",
878                                         tname, strerror(errno));
879                                 goto ENDSTR;
880                         }
881                         putc(255, message);     /* 0xFF (start-of-message) */
882                         a = getc(fp);
883                         putc(a, message);       /* type */
884                         a = getc(fp);
885                         putc(a, message);       /* mode */
886                         do {
887                                 FieldID = getc(fp);     /* Header field ID */
888                                 putc(FieldID, message);
889                                 do {
890                                         a = getc(fp);
891                                         putc(a, message);
892                                 } while (a > 0);
893                         } while ((FieldID != 'M') && (a >= 0));
894                         /* M is always last */
895
896                         msglen = ftell(message);
897                         fclose(message);
898
899                         /* process the individual mesage */
900                         minfo.D[0] = 0;
901                         minfo.C[0] = 0;
902                         minfo.B[0] = 0;
903                         minfo.G[0] = 0;
904                         minfo.R[0] = 0;
905                         msgfind(tname, &minfo);
906                         strncpy(recentmsg.RMnodename, minfo.N, 9);
907                         recentmsg.RMnodename[9] = 0;
908                         recentmsg.RMnum = minfo.I;
909                         syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
910                                minfo.I, minfo.A, minfo.O, minfo.N);
911                         if (strlen(minfo.R) > 0) {
912                                 syslog(LOG_NOTICE, "     to <%s>", minfo.R);
913                                 if (strlen(minfo.D) > 0) {
914                                         syslog(LOG_NOTICE, "     @ <%s>",
915                                                 minfo.D);
916                                 }
917                         }
918                         if (!strcasecmp(minfo.D, FQDN))
919                                 strcpy(minfo.D, NODENAME);
920
921 /* this routine updates our info on the system that sent the message */
922                         stemp = get_sys_ptr(minfo.N);
923                         if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
924                                 /* add non-neighbor system to map */
925                                 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
926                                        slist->s_name);
927                                 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
928                                 stemp->next = slist;
929                                 slist = stemp;
930                                 strcpy(slist->s_name, minfo.N);
931                                 strcpy(slist->s_type, "use");
932                                 strcpy(slist->s_nexthop, minfo.nexthop);
933                                 time(&slist->s_lastcontact);
934                         } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
935                                 /* add neighbor system to map */
936                                 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
937                                        slist->s_name);
938                                 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
939                                 testfp = fopen(aaa, "r");
940                                 if (testfp != NULL) {
941                                         fclose(testfp);
942                                         stemp = (struct syslist *)
943                                             malloc((long) sizeof(struct syslist));
944                                         stemp->next = slist;
945                                         slist = stemp;
946                                         strcpy(slist->s_name, minfo.N);
947                                         strcpy(slist->s_type, "bin");
948                                         strcpy(slist->s_nexthop, "Mail");
949                                         time(&slist->s_lastcontact);
950                                 }
951                         }
952                         /* now update last contact and long node name if we can */
953                         if (stemp != NULL) {
954                                 time(&stemp->s_lastcontact);
955                                 if (strlen(minfo.H) > 0)
956                                         strcpy(stemp->s_humannode, minfo.H);
957                                 if (strlen(minfo.B) > 0)
958                                         strcpy(stemp->s_phonenum, minfo.B);
959                                 if (strlen(minfo.G) > 0)
960                                         strcpy(stemp->s_gdom, minfo.G);
961                         }
962
963                         /* Check the use table; reject message if it's been here before */
964                         if (already_received(use_table, &minfo)) {
965                                 syslog(LOG_NOTICE, "rejected duplicate message");
966                                 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
967                                         minfo.I, minfo.A, minfo.O, minfo.N);
968                         }
969
970
971                         /* route the message if necessary */
972                         else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
973                                 a = get_sysinfo_type(minfo.D);
974                                 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
975                                 fflush(stdout);
976                                 if (a == MES_INTERNET) {
977                                         if (fork() == 0) {
978                                                 syslog(LOG_NOTICE, "netmailer %s", tname);
979                                                 fflush(stdout);
980                                                 execlp("./netmailer", "netmailer",
981                                                        tname, NULL);
982                                                 syslog(LOG_ERR, "error running netmailer: %s",
983                                                        strerror(errno));
984                                                 exit(errno);
985                                         } else
986                                                 while (wait(&b) != (-1));
987                                 } else if (a == MES_BINARY) {
988                                         ship_to(tname, minfo.D);
989                                 } else {
990                                         /* message falls into the bit bucket? */
991                                 }
992                         }
993
994                         /* check to see if it's a file transfer */
995                         else if (!strncasecmp(minfo.S, "FILE", 4)) {
996                                 proc_file_transfer(tname);
997                         }
998
999                         /* otherwise process it as a normal message */
1000                         else {
1001
1002                                 if (!strcasecmp(minfo.R, "postmaster")) {
1003                                         strcpy(minfo.R, "");
1004                                         strcpy(minfo.C, "Aide");
1005                                 }
1006                                 if (strlen(minfo.R) > 0) {
1007                                         sprintf(buf, "GOTO _MAIL_");
1008                                 }
1009                                 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1010                                         sprintf(buf, "GOTO %s", FILTERROOM);
1011                                 } else {
1012                                         if (strlen(minfo.C) > 0) {
1013                                                 sprintf(buf, "GOTO %s", minfo.C);
1014                                         } else {
1015                                                 sprintf(buf, "GOTO %s", minfo.O);
1016                                         }
1017                                 }
1018                                 serv_puts(buf);
1019                                 serv_gets(buf);
1020                                 if (buf[0] != '2') {
1021                                         syslog(LOG_ERR, "%s", buf);
1022                                         sprintf(buf, "GOTO _BITBUCKET_");
1023                                         serv_puts(buf);
1024                                         serv_gets(buf);
1025                                 }
1026                                 /* Open the temporary file containing the message */
1027                                 message = fopen(tname, "rb");
1028                                 if (message == NULL) {
1029                                         syslog(LOG_ERR, "cannot open %s: %s",
1030                                                tname, strerror(errno));
1031                                         unlink(tname);
1032                                         goto NXMSG;
1033                                 }
1034                                 /* Transmit the message to the server */
1035                                 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1036                                 serv_puts(buf);
1037                                 serv_gets(buf);
1038                                 if (!strncmp(buf, "570", 3)) {
1039                                         /* no such user, do a bounce */
1040                                         bounce(&minfo);
1041                                 }
1042                                 if (buf[0] == '7') {
1043                                         /* Always use the server's idea of the message length,
1044                                          * even though they should both be identical */
1045                                         msglen = atol(&buf[4]);
1046                                         while (msglen > 0L) {
1047                                                 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1048                                                 if (fread(buf, bloklen, 1, message) < 1) {
1049                                                         syslog(LOG_ERR,
1050                                                                "error trying to read %d bytes: %s",
1051                                                                bloklen, strerror(errno));
1052                                                 }
1053                                                 serv_write(buf, bloklen);
1054                                                 msglen = msglen - (long) bloklen;
1055                                         }
1056                                         serv_puts("NOOP");
1057                                         serv_gets(buf);
1058                                 } else {
1059                                         syslog(LOG_ERR, "%s", buf);
1060                                 }
1061
1062                                 fclose(message);
1063                         }
1064
1065                         unlink(tname);
1066                         goto NXMSG;
1067
1068 ENDSTR:                 fclose(fp);
1069                         unlink(pfilename);
1070                 }
1071         } while (ptr != NULL);
1072         unlink(iname);
1073
1074         purge_use_table(use_table);
1075         gdbm_close(use_table);
1076
1077
1078         /*
1079          * If dups were rejected, post a message saying so
1080          */
1081         if (ftell(duplist)!=0L) {
1082                 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1083                 if (fp != NULL) {
1084                         fprintf(fp, "%cA%c", 255, 1);
1085                         fprintf(fp, "T%ld%c", time(NULL), 0);
1086                         fprintf(fp, "ACitadel%c", 0);
1087                         fprintf(fp, "OAide%cM", 0);
1088                         fprintf(fp, "The following duplicate messages"
1089                                 " were rejected:\n \n");
1090                         rewind(duplist);
1091                         while (fgets(buf, sizeof(buf), duplist) != NULL) {
1092                                 buf[strlen(buf)-1] = 0;
1093                                 fprintf(fp, " %s\n", buf);
1094                         }
1095                         fprintf(fp, "%c", 0);
1096                         pclose(fp);
1097                 }
1098         }
1099
1100         fclose(duplist);
1101
1102 }
1103
1104
1105 /* Checks to see whether its ok to send */
1106 /* Returns 1 for ok, send message       */
1107 /* Returns 0 if message already there   */
1108 int checkpath(char *path, char *sys)
1109 {
1110         int a;
1111         char sys2[512];
1112         strcpy(sys2, sys);
1113         strcat(sys2, "!");
1114
1115 #ifdef DEBUG
1116         syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1117 #endif
1118         for (a = 0; a < strlen(path); ++a) {
1119                 if (!strncmp(&path[a], sys2, strlen(sys2)))
1120                         return (0);
1121         }
1122         return (1);
1123 }
1124
1125 /*
1126  * implement split horizon algorithm
1127  */
1128 int ismsgok(long int mpos, FILE * mmfp, char *sysname)
1129 {
1130         int a;
1131         int ok = 0;             /* fail safe - no path, don't send it */
1132         char fbuf[256];
1133
1134         fseek(mmfp, mpos, 0);
1135         if (getc(mmfp) != 255)
1136                 return (0);
1137         getc(mmfp);
1138         getc(mmfp);
1139
1140         while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1141                 fpgetfield(mmfp, fbuf);
1142                 if (a == 'P') {
1143                         ok = checkpath(fbuf, sysname);
1144                 }
1145         }
1146 #ifdef DEBUG
1147         syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1148 #endif
1149         return (ok);
1150 }
1151
1152
1153
1154 /* spool list of messages to a file */
1155 /* returns # of msgs spooled */
1156 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1157 {
1158         struct msglist *cmptr;
1159         FILE *mmfp;
1160         char fbuf[128];
1161         int a;
1162         int msgs_spooled = 0;
1163         long msg_len;
1164         int blok_len;
1165
1166         char buf[256];
1167         char curr_rm[256];
1168
1169         strcpy(curr_rm, "");
1170
1171         /* for each message in the list... */
1172         for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1173
1174                 /* make sure we're in the correct room... */
1175                 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1176                         sprintf(buf, "GOTO %s", cmptr->m_rmname);
1177                         serv_puts(buf);
1178                         serv_gets(buf);
1179                         if (buf[0] == '2') {
1180                                 strcpy(curr_rm, cmptr->m_rmname);
1181                         } else {
1182                                 syslog(LOG_ERR, "%s", buf);
1183                         }
1184                 }
1185                 /* download the message from the server... */
1186                 mmfp = tmpfile();
1187                 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1188                 serv_puts(buf);
1189                 serv_gets(buf);
1190                 if (buf[0] == '6') {    /* read the msg */
1191                         msg_len = atol(&buf[4]);
1192                         while (msg_len > 0L) {
1193                                 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1194                                 serv_read(buf, blok_len);
1195                                 fwrite(buf, blok_len, 1, mmfp);
1196                                 msg_len = msg_len - (long) blok_len;
1197                         }
1198                 } else {        /* or print the err */
1199                         syslog(LOG_ERR, "%s", buf);
1200                 }
1201
1202                 rewind(mmfp);
1203
1204                 if (ismsgok(0L, mmfp, sysname)) {
1205                         ++msgs_spooled;
1206                         fflush(stdout);
1207                         fseek(mmfp, 0L, 0);
1208                         fread(fbuf, 3, 1, mmfp);
1209                         fwrite(fbuf, 3, 1, destfp);
1210                         while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1211                                 if (a != 'C')
1212                                         putc(a, destfp);
1213                                 fpgetfield(mmfp, fbuf);
1214                                 if (a == 'P')
1215                                         fprintf(destfp, "%s!", NODENAME);
1216                                 if (a != 'C')
1217                                         fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1218                         }
1219                         if (a == 'M') {
1220                                 fprintf(destfp, "C%s%c",
1221                                         cmptr->m_rmname, 0);
1222                                 putc('M', destfp);
1223                                 do {
1224                                         a = getc(mmfp);
1225                                         putc(a, destfp);
1226                                 } while (a > 0);
1227                         }
1228                 }
1229                 fclose(mmfp);
1230         }
1231
1232         return (msgs_spooled);
1233 }
1234
1235 void outprocess(char *sysname)
1236 {                               /* send new room messages to sysname */
1237         char sysflnm[64];
1238         char srmname[32];
1239         char shiptocmd[128];
1240         char lbuf[64];
1241         char tempflnm[64];
1242         char buf[256];
1243         struct msglist *cmlist = NULL;
1244         struct rmlist *crmlist = NULL;
1245         struct rmlist *rmptr, *rmptr2;
1246         struct msglist *cmptr, *cmptr2;
1247         FILE *sysflfp, *tempflfp;
1248         int outgoing_msgs;
1249         long thismsg;
1250
1251         sprintf(tempflnm, tmpnam(NULL));
1252         tempflfp = fopen(tempflnm, "w");
1253         if (tempflfp == NULL)
1254                 return;
1255
1256
1257 /*
1258  * Read system file for node in question and put together room list
1259  */
1260         sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1261         sysflfp = fopen(sysflnm, "r");
1262         if (sysflfp == NULL)
1263                 return;
1264         fgets(shiptocmd, 128, sysflfp);
1265         shiptocmd[strlen(shiptocmd) - 1] = 0;
1266         while (!feof(sysflfp)) {
1267                 if (fgets(srmname, 32, sysflfp) == NULL)
1268                         break;
1269                 srmname[strlen(srmname) - 1] = 0;
1270                 fgets(lbuf, 32, sysflfp);
1271                 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1272                 rmptr->next = NULL;
1273                 strcpy(rmptr->rm_name, srmname);
1274                 strip_trailing_whitespace(rmptr->rm_name);
1275                 rmptr->rm_lastsent = atol(lbuf);
1276                 if (crmlist == NULL)
1277                         crmlist = rmptr;
1278                 else if (!strcasecmp(rmptr->rm_name, "control")) {
1279                         /* control has to be first in room list */
1280                         rmptr->next = crmlist;
1281                         crmlist = rmptr;
1282                 } else {
1283                         rmptr2 = crmlist;
1284                         while (rmptr2->next != NULL)
1285                                 rmptr2 = rmptr2->next;
1286                         rmptr2->next = rmptr;
1287                 }
1288         }
1289         fclose(sysflfp);
1290
1291 /*
1292  * Assemble list of messages to be spooled
1293  */
1294         for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1295
1296                 sprintf(buf, "GOTO %s", rmptr->rm_name);
1297                 serv_puts(buf);
1298                 serv_gets(buf);
1299                 if (buf[0] != '2') {
1300                         syslog(LOG_ERR, "%s", buf);
1301                 } else {
1302                         sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1303                         serv_puts(buf);
1304                         serv_gets(buf);
1305                         if (buf[0] == '1')
1306                                 while (serv_gets(buf), strcmp(buf, "000")) {
1307                                         thismsg = atol(buf);
1308                                         if (thismsg > (rmptr->rm_lastsent)) {
1309                                                 rmptr->rm_lastsent = thismsg;
1310
1311                                                 cmptr = (struct msglist *)
1312                                                     malloc(sizeof(struct msglist));
1313                                                 cmptr->next = NULL;
1314                                                 cmptr->m_num = thismsg;
1315                                                 strcpy(cmptr->m_rmname, rmptr->rm_name);
1316
1317                                                 if (cmlist == NULL)
1318                                                         cmlist = cmptr;
1319                                                 else {
1320                                                         cmptr2 = cmlist;
1321                                                         while (cmptr2->next != NULL)
1322                                                                 cmptr2 = cmptr2->next;
1323                                                         cmptr2->next = cmptr;
1324                                                 }
1325                                         }
1326                         } else {        /* print error from "msgs all" */
1327                                 syslog(LOG_ERR, "%s", buf);
1328                         }
1329                 }
1330         }
1331
1332         outgoing_msgs = 0;
1333         cmptr2 = cmlist;        /* this loop counts the messages */
1334         while (cmptr2 != NULL) {
1335                 ++outgoing_msgs;
1336                 cmptr2 = cmptr2->next;
1337         }
1338         syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1339                outgoing_msgs, sysname);
1340
1341 /*
1342  * Spool out the messages, but only if there are any.
1343  */
1344         if (outgoing_msgs != 0)
1345                 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1346         syslog(LOG_NOTICE, "%d messages actually spooled",
1347                outgoing_msgs);
1348
1349 /*
1350  * Deallocate list of spooled messages.
1351  */
1352         while (cmlist != NULL) {
1353                 cmptr = cmlist->next;
1354                 free(cmlist);
1355                 cmlist = cmptr;
1356         }
1357
1358 /*
1359  * Rewrite system file and deallocate room list.
1360  */
1361         syslog(LOG_NOTICE, "Spooling...");
1362         sysflfp = fopen(sysflnm, "w");
1363         fprintf(sysflfp, "%s\n", shiptocmd);
1364         for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1365                 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1366         fclose(sysflfp);
1367         while (crmlist != NULL) {
1368                 rmptr = crmlist->next;
1369                 free(crmlist);
1370                 crmlist = rmptr;
1371         }
1372
1373 /* 
1374  * Close temporary file, ship it out, and return
1375  */
1376         fclose(tempflfp);
1377         if (outgoing_msgs != 0)
1378                 ship_to(tempflnm, sysname);
1379         unlink(tempflnm);
1380 }
1381
1382
1383 /*
1384  * Connect netproc to the Citadel server running on this computer.
1385  */
1386 void np_attach_to_server(void)
1387 {
1388         char buf[256];
1389         char portname[8];
1390         char *args[] =
1391         {"netproc", "localhost", NULL, NULL};
1392
1393         syslog(LOG_NOTICE, "Attaching to server...");
1394         sprintf(portname, "%d", config.c_port_number);
1395         args[2] = portname;
1396         attach_to_server(3, args);
1397         serv_gets(buf);
1398         syslog(LOG_NOTICE, "%s", &buf[4]);
1399         sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1400         serv_puts(buf);
1401         serv_gets(buf);
1402         syslog(LOG_NOTICE, "%s", &buf[4]);
1403         if (buf[0] != '2') {
1404                 cleanup(2);
1405         }
1406 }
1407
1408
1409
1410 /*
1411  * main
1412  */
1413 int main(int argc, char **argv)
1414 {
1415         char allst[32];
1416         FILE *allfp;
1417         int a;
1418         int import_only = 0;    /* if set to 1, don't export anything */
1419
1420         openlog("netproc", LOG_PID, LOG_USER);
1421         strcpy(bbs_home_directory, BBSDIR);
1422
1423         /*
1424          * Change directories if specified
1425          */
1426         for (a = 1; a < argc; ++a) {
1427                 if (!strncmp(argv[a], "-h", 2)) {
1428                         strcpy(bbs_home_directory, argv[a]);
1429                         strcpy(bbs_home_directory, &bbs_home_directory[2]);
1430                         home_specified = 1;
1431                 } else if (!strcmp(argv[a], "-i")) {
1432                         import_only = 1;
1433                 } else {
1434                         fprintf(stderr, "netproc: usage: ");
1435                         fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1436                         exit(1);
1437                 }
1438         }
1439
1440         get_config();
1441
1442         if (set_lockfile() != 0) {
1443                 syslog(LOG_NOTICE, "lock file exists: already running");
1444                 cleanup(1);
1445         }
1446         signal(SIGINT, cleanup);
1447         signal(SIGQUIT, cleanup);
1448         signal(SIGHUP, cleanup);
1449         signal(SIGTERM, cleanup);
1450
1451         syslog(LOG_NOTICE, "started.  pid=%d", getpid());
1452         fflush(stdout);
1453         np_attach_to_server();
1454         fflush(stdout);
1455
1456         if (load_syslist() != 0)
1457                 syslog(LOG_ERR, "cannot load sysinfo");
1458         setup_special_nodes();
1459
1460         inprocess();            /* first collect incoming stuff */
1461
1462         if (import_only != 1) {
1463                 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1464                 if (allfp != NULL) {
1465                         while (fgets(allst, 32, allfp) != NULL) {
1466                                 allst[strlen(allst) - 1] = 0;
1467                                 outprocess(allst);
1468                         }
1469                         pclose(allfp);
1470                 }
1471                 /* import again in case anything new was generated */
1472                 inprocess();
1473         }
1474         rewrite_syslist();
1475         syslog(LOG_NOTICE, "processing ended.");
1476         cleanup(0);
1477         return 0;
1478 }