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