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