* Issue 'cancel' messages for vCard when a user is deleted.
[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 MAILSYSINFO     "./network/mail.sysinfo"
21
22 /* Uncomment the DEBUG def to see noisy traces */
23 #define DEBUG 1
24
25
26 #include "sysdep.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <time.h>
36 #include <signal.h>
37 #include <errno.h>
38 #include <syslog.h>
39 #ifdef HAVE_GDBM_H
40 #include <gdbm.h>
41 #endif
42 #include "citadel.h"
43 #include "tools.h"
44
45 /* A list of users you wish to filter out of incoming traffic can be kept
46  * in ./network/filterlist -- messages from these users will be automatically
47  * moved to FILTERROOM.  Normally this will be the same as TWITROOM (the
48  * room problem user messages are moved to) but you can override this by
49  * specifying a different room name here.
50  */
51 #ifndef FILTERROOM
52 #define FILTERROOM TWITROOM
53 #endif
54
55 struct msglist {
56         struct msglist *next;
57         long m_num;
58         char m_rmname[ROOMNAMELEN];
59 };
60
61 struct rmlist {
62         struct rmlist *next;
63         char rm_name[ROOMNAMELEN];
64         long rm_lastsent;
65 };
66
67 struct filterlist {
68         struct filterlist *next;
69         char f_person[64];
70         char f_room[64];
71         char f_system[64];
72 };
73
74 struct syslist {
75         struct syslist *next;
76         char s_name[16];
77         char s_type[4];
78         char s_nexthop[128];
79         time_t s_lastcontact;
80         char s_humannode[64];
81         char s_phonenum[32];
82         char s_gdom[64];
83 };
84
85
86 void attach_to_server(int argc, char **argv);
87 void serv_read(char *buf, int bytes);
88 void serv_write(char *buf, int nbytes);
89 void get_config(void);
90
91 struct filterlist *filter = NULL;
92 struct syslist *slist = NULL;
93 struct msglist *purgelist = 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
821 /*
822  * process incoming files in ./network/spoolin
823  */
824 void inprocess(void)
825 {
826         FILE *fp, *message, *testfp, *ls, *duplist;
827         static struct minfo minfo;
828         struct recentmsg recentmsg;
829         char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
830         int a, b;
831         int FieldID;
832         struct syslist *stemp;
833         char *ptr = NULL;
834         char buf[256];
835         long msglen;
836         int bloklen;
837         int valid_msg;
838
839         /* temp file names */
840         sprintf(tname, tmpnam(NULL));
841         sprintf(iname, tmpnam(NULL));
842
843         load_filterlist();
844
845         /* Make sure we're in the right directory */
846         chdir(bbs_home_directory);
847
848         /* temporary file to contain a log of rejected dups */
849         duplist = tmpfile();
850
851         /* Let the shell do the dirty work. Get all data from spoolin */
852         do {
853                 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
854                 ls = popen(aaa, "r");
855                 if (ls == NULL) {
856                         syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
857                 }
858                 if (ls != NULL) {
859                         do {
860 SKIP:                           ptr = fgets(sfilename, sizeof sfilename, ls);
861                                 if (ptr != NULL) {
862 #ifdef DEBUG
863                                         syslog(LOG_DEBUG,
864                                                 "Trying %s", sfilename);
865 #endif
866                                         sfilename[strlen(sfilename) - 1] = 0;
867                                         if (!strcmp(sfilename, ".")) goto SKIP;
868                                         if (!strcmp(sfilename, "..")) goto SKIP;
869                                         if (!strcmp(sfilename, "CVS")) goto SKIP;
870                                         goto PROCESS_IT;
871                                 }
872                         } while (ptr != NULL);
873 PROCESS_IT:             pclose(ls);
874                 }
875                 if (ptr != NULL) {
876                         sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
877                         syslog(LOG_NOTICE, "processing <%s>", pfilename);
878
879                         fp = fopen(pfilename, "rb");
880                         if (fp == NULL) {
881                                 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
882                                 fp = fopen("/dev/null", "rb");
883                         }
884 NXMSG:  /* Seek to the beginning of the next message */
885                         do {
886                                 a = getc(fp);
887                         } while ((a != 255) && (a >= 0));
888                         if (a < 0)
889                                 goto ENDSTR;
890
891                         /* This crates the temporary file. */
892                         valid_msg = 1;
893                         message = fopen(tname, "wb");
894                         if (message == NULL) {
895                                 syslog(LOG_ERR, "error creating %s: %s",
896                                         tname, strerror(errno));
897                                 goto ENDSTR;
898                         }
899                         putc(255, message);     /* 0xFF (start-of-message) */
900                         a = getc(fp);
901                         putc(a, message);       /* type */
902                         a = getc(fp);
903                         putc(a, message);       /* mode */
904                         do {
905                                 FieldID = getc(fp); /* Header field ID */
906                                 if (isalpha(FieldID)) {
907                                         putc(FieldID, message);
908                                         do {
909                                                 a = getc(fp);
910                                                 if (a < 127) putc(a, message);
911                                         } while (a > 0);
912                                         if (a != 0) putc(0, message);
913                                 }
914                                 else {  /* Invalid field ID; flush it */
915                                         do {
916                                                 a = getc(fp);
917                                         } while (a > 0);
918                                         valid_msg = 0;
919                                 }
920                         } while ((FieldID != 'M') && (a >= 0));
921                         /* M is always last */
922                         if (FieldID != 'M') valid_msg = 0;
923
924                         msglen = ftell(message);
925                         fclose(message);
926
927                         if (!valid_msg) {
928                                 unlink(tname);
929                                 goto NXMSG;
930                         }
931
932                         /* process the individual mesage */
933                         msgfind(tname, &minfo);
934                         strncpy(recentmsg.RMnodename, minfo.N, 9);
935                         recentmsg.RMnodename[9] = 0;
936                         recentmsg.RMnum = minfo.I;
937                         syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
938                                minfo.I, minfo.A, minfo.O, minfo.N);
939                         if (strlen(minfo.R) > 0) {
940                                 syslog(LOG_NOTICE, "     to <%s>", minfo.R);
941                                 if (strlen(minfo.D) > 0) {
942                                         syslog(LOG_NOTICE, "     @ <%s>",
943                                                 minfo.D);
944                                 }
945                         }
946                         if (!strcasecmp(minfo.D, FQDN))
947                                 strcpy(minfo.D, NODENAME);
948
949 /* this routine updates our info on the system that sent the message */
950                         stemp = get_sys_ptr(minfo.N);
951                         if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
952                                 /* add non-neighbor system to map */
953                                 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
954                                        slist->s_name);
955                                 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
956                                 stemp->next = slist;
957                                 slist = stemp;
958                                 strcpy(slist->s_name, minfo.N);
959                                 strcpy(slist->s_type, "use");
960                                 strcpy(slist->s_nexthop, minfo.nexthop);
961                                 time(&slist->s_lastcontact);
962                         } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
963                                 /* add neighbor system to map */
964                                 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
965                                        slist->s_name);
966                                 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
967                                 testfp = fopen(aaa, "r");
968                                 if (testfp != NULL) {
969                                         fclose(testfp);
970                                         stemp = (struct syslist *)
971                                             malloc((long) sizeof(struct syslist));
972                                         stemp->next = slist;
973                                         slist = stemp;
974                                         strcpy(slist->s_name, minfo.N);
975                                         strcpy(slist->s_type, "bin");
976                                         strcpy(slist->s_nexthop, "Mail");
977                                         time(&slist->s_lastcontact);
978                                 }
979                         }
980                         /* now update last contact and long node name if we can */
981                         if (stemp != NULL) {
982                                 time(&stemp->s_lastcontact);
983                                 if (strlen(minfo.H) > 0)
984                                         strcpy(stemp->s_humannode, minfo.H);
985                                 if (strlen(minfo.B) > 0)
986                                         strcpy(stemp->s_phonenum, minfo.B);
987                                 if (strlen(minfo.G) > 0)
988                                         strcpy(stemp->s_gdom, minfo.G);
989                         }
990
991                         /* Check the use table; reject message if it's been here before */
992                         if (already_received(use_table, &minfo)) {
993                                 syslog(LOG_NOTICE, "rejected duplicate message");
994                                 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
995                                         minfo.I, minfo.A, minfo.O, minfo.N);
996                         }
997
998
999                         /* route the message if necessary */
1000                         else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
1001                                 a = get_sysinfo_type(minfo.D);
1002                                 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
1003                                 fflush(stdout);
1004                                 if (a == MES_INTERNET) {
1005                                         if (fork() == 0) {
1006                                                 syslog(LOG_NOTICE, "netmailer %s", tname);
1007                                                 fflush(stdout);
1008                                                 execlp("./netmailer", "netmailer",
1009                                                        tname, NULL);
1010                                                 syslog(LOG_ERR, "error running netmailer: %s",
1011                                                        strerror(errno));
1012                                                 exit(errno);
1013                                         } else
1014                                                 while (wait(&b) != (-1));
1015                                 } else if (a == MES_BINARY) {
1016                                         ship_to(tname, minfo.D);
1017                                 } else {
1018                                         /* message falls into the bit bucket? */
1019                                 }
1020                         }
1021
1022                         /* check to see if it's a file transfer */
1023                         else if (!strncasecmp(minfo.S, "FILE", 4)) {
1024                                 proc_file_transfer(tname);
1025                         }
1026
1027                         /* otherwise process it as a normal message */
1028                         else {
1029                                 if (!strcasecmp(minfo.R, "postmaster")) {
1030                                         strcpy(minfo.R, "");
1031                                         strcpy(minfo.C, "Aide");
1032                                 }
1033                                 if (strlen(minfo.R) > 0) {
1034                                         sprintf(buf, "GOTO _MAIL_");
1035                                 }
1036                                 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1037                                         sprintf(buf, "GOTO %s", FILTERROOM);
1038                                 } else {
1039                                         if (strlen(minfo.C) > 0) {
1040                                                 sprintf(buf, "GOTO %s", minfo.C);
1041                                         } else {
1042                                                 sprintf(buf, "GOTO %s", minfo.O);
1043                                         }
1044                                 }
1045                                 serv_puts(buf);
1046                                 serv_gets(buf);
1047                                 if (buf[0] != '2') {
1048                                         syslog(LOG_ERR, "%s", buf);
1049                                         sprintf(buf, "GOTO _BITBUCKET_");
1050                                         serv_puts(buf);
1051                                         serv_gets(buf);
1052                                 }
1053                                 /* Open the temporary file containing the message */
1054                                 message = fopen(tname, "rb");
1055                                 if (message == NULL) {
1056                                         syslog(LOG_ERR, "cannot open %s: %s",
1057                                                tname, strerror(errno));
1058                                         unlink(tname);
1059                                         goto NXMSG;
1060                                 }
1061                                 /* Transmit the message to the server */
1062                                 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1063                                 serv_puts(buf);
1064                                 serv_gets(buf);
1065                                 if (!strncmp(buf, "570", 3)) {
1066                                         /* no such user, do a bounce */
1067                                         bounce(&minfo);
1068                                 }
1069                                 if (buf[0] == '7') {
1070                                         /* Always use the server's idea of the message length,
1071                                          * even though they should both be identical */
1072                                         msglen = atol(&buf[4]);
1073                                         while (msglen > 0L) {
1074                                                 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1075                                                 if (fread(buf, bloklen, 1, message) < 1) {
1076                                                         syslog(LOG_ERR,
1077                                                                "error trying to read %d bytes: %s",
1078                                                                bloklen, strerror(errno));
1079                                                 }
1080                                                 serv_write(buf, bloklen);
1081                                                 msglen = msglen - (long) bloklen;
1082                                         }
1083                                         serv_puts("NOOP");
1084                                         serv_gets(buf);
1085                                 } else {
1086                                         syslog(LOG_ERR, "%s", buf);
1087                                 }
1088
1089                                 fclose(message);
1090                                 
1091                         }
1092
1093                         unlink(tname);
1094                         goto NXMSG;
1095
1096 ENDSTR:                 fclose(fp);
1097                         unlink(pfilename);
1098                 }
1099         } while (ptr != NULL);
1100         unlink(iname);
1101
1102
1103         /*
1104          * If dups were rejected, post a message saying so
1105          */
1106         if (ftell(duplist)!=0L) {
1107                 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1108                 if (fp != NULL) {
1109                         fprintf(fp, "%cA%c", 255, 1);
1110                         fprintf(fp, "T%ld%c", time(NULL), 0);
1111                         fprintf(fp, "ACitadel%c", 0);
1112                         fprintf(fp, "OAide%cM", 0);
1113                         fprintf(fp, "The following duplicate messages"
1114                                 " were rejected:\n \n");
1115                         rewind(duplist);
1116                         while (fgets(buf, sizeof(buf), duplist) != NULL) {
1117                                 buf[strlen(buf)-1] = 0;
1118                                 fprintf(fp, " %s\n", buf);
1119                         }
1120                         fprintf(fp, "%c", 0);
1121                         pclose(fp);
1122                 }
1123         }
1124
1125         fclose(duplist);
1126
1127 }
1128
1129
1130 /* Checks to see whether its ok to send */
1131 /* Returns 1 for ok, send message       */
1132 /* Returns 0 if message already there   */
1133 int checkpath(char *path, char *sys)
1134 {
1135         int a;
1136         char sys2[512];
1137         strcpy(sys2, sys);
1138         strcat(sys2, "!");
1139
1140 #ifdef DEBUG
1141         syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1142 #endif
1143         for (a = 0; a < strlen(path); ++a) {
1144                 if (!strncmp(&path[a], sys2, strlen(sys2)))
1145                         return (0);
1146         }
1147         return (1);
1148 }
1149
1150 /*
1151  * Implement split horizon algorithm (prevent infinite spooling loops
1152  * by refusing to send any node a message which already contains its
1153  * nodename in the path).
1154  */
1155 int ismsgok(FILE *mmfp, char *sysname)
1156 {
1157         int a;
1158         int ok = 0;             /* fail safe - no path, don't send it */
1159         char fbuf[256];
1160
1161         fseek(mmfp, 0L, 0);
1162         if (getc(mmfp) != 255)
1163                 return (0);
1164         getc(mmfp);
1165         getc(mmfp);
1166
1167         while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1168                 fpgetfield(mmfp, fbuf);
1169                 if (a == 'P') {
1170                         ok = checkpath(fbuf, sysname);
1171                 }
1172         }
1173 #ifdef DEBUG
1174         syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1175 #endif
1176         return (ok);
1177 }
1178
1179
1180
1181
1182 /*
1183  * Add a message to the list of messages to be deleted off the local server
1184  * at the end of this run.
1185  */
1186 void delete_locally(long msgid, char *roomname) {
1187         struct msglist *mptr;
1188
1189         mptr = (struct msglist *) malloc(sizeof(struct msglist));
1190         mptr->next = purgelist;
1191         mptr->m_num = msgid;
1192         strcpy(mptr->m_rmname, roomname);
1193         purgelist = mptr;
1194 }
1195
1196
1197
1198 /*
1199  * Delete all messages on the purge list from the local server.
1200  */
1201 void process_purgelist(void) {
1202         char curr_rm[ROOMNAMELEN];
1203         char buf[256];
1204         struct msglist *mptr;
1205
1206
1207         strcpy(curr_rm, "__nothing__");
1208         while (purgelist != NULL) {
1209                 if (strcasecmp(curr_rm, purgelist->m_rmname)) {
1210                         sprintf(buf, "GOTO %s", purgelist->m_rmname);
1211                         serv_puts(buf);
1212                         serv_gets(buf);
1213                         if (buf[0] == '2') extract(curr_rm, &buf[4], 0);
1214                 }
1215                 if (strcasecmp(curr_rm, purgelist->m_rmname)) {
1216                         sprintf(buf, "DELE %ld", purgelist->m_num);
1217                         serv_puts(buf);
1218                         serv_gets(buf);
1219                 }
1220                 mptr = purgelist->next;
1221                 free(purgelist);
1222                 purgelist = mptr;
1223         }
1224 }
1225
1226
1227
1228
1229 /* spool list of messages to a file */
1230 /* returns # of msgs spooled */
1231 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1232 {
1233         struct msglist *cmptr;
1234         FILE *mmfp;
1235         char fbuf[128];
1236         int a;
1237         int msgs_spooled = 0;
1238         long msg_len;
1239         int blok_len;
1240         static struct minfo minfo;
1241
1242         char buf[256];
1243         char curr_rm[256];
1244
1245         strcpy(curr_rm, "");
1246
1247         /* for each message in the list... */
1248         for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1249
1250                 /* make sure we're in the correct room... */
1251                 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1252                         sprintf(buf, "GOTO %s", cmptr->m_rmname);
1253                         serv_puts(buf);
1254                         serv_gets(buf);
1255                         if (buf[0] == '2') {
1256                                 strcpy(curr_rm, cmptr->m_rmname);
1257                         } else {
1258                                 syslog(LOG_ERR, "%s", buf);
1259                         }
1260                 }
1261                 /* download the message from the server... */
1262                 mmfp = tmpfile();
1263                 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1264                 serv_puts(buf);
1265                 serv_gets(buf);
1266                 if (buf[0] == '6') {    /* read the msg */
1267                         msg_len = atol(&buf[4]);
1268                         while (msg_len > 0L) {
1269                                 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1270                                 serv_read(buf, blok_len);
1271                                 fwrite(buf, blok_len, 1, mmfp);
1272                                 msg_len = msg_len - (long) blok_len;
1273                         }
1274                 } else {        /* or print the err */
1275                         syslog(LOG_ERR, "%s", buf);
1276                 }
1277
1278                 rewind(mmfp);
1279
1280                 if (ismsgok(mmfp, sysname)) {
1281                         ++msgs_spooled;
1282                         fflush(stdout);
1283                         fseek(mmfp, 0L, 0);
1284                         fread(fbuf, 3, 1, mmfp);
1285                         fwrite(fbuf, 3, 1, destfp);
1286                         while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1287                                 if (a != 'C')
1288                                         putc(a, destfp);
1289                                 fpgetfield(mmfp, fbuf);
1290                                 if (a == 'P')
1291                                         fprintf(destfp, "%s!", NODENAME);
1292                                 if (a != 'C')
1293                                         fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1294                                 if (a == 'S') if (!strcasecmp(fbuf, "CANCEL")) {
1295                                         delete_locally(cmptr->m_num, cmptr->m_rmname);
1296                                 }
1297                         }
1298                         if (a == 'M') {
1299                                 fprintf(destfp, "C%s%c",
1300                                         cmptr->m_rmname, 0);
1301                                 putc('M', destfp);
1302                                 do {
1303                                         a = getc(mmfp);
1304                                         putc(a, destfp);
1305                                 } while (a > 0);
1306                         }
1307
1308                 /* Get this message into the use table, so we can reject it
1309                  * if a misconfigured remote system sends it back to us.
1310                  */
1311                 fseek(mmfp, 0L, 0);
1312                 fpmsgfind(mmfp, &minfo);
1313                 already_received(use_table, &minfo);
1314
1315                 }
1316                 fclose(mmfp);
1317         }
1318
1319         return (msgs_spooled);
1320 }
1321
1322 void outprocess(char *sysname)
1323 {                               /* send new room messages to sysname */
1324         char sysflnm[64];
1325         char srmname[32];
1326         char shiptocmd[128];
1327         char lbuf[64];
1328         char tempflnm[64];
1329         char buf[256];
1330         struct msglist *cmlist = NULL;
1331         struct rmlist *crmlist = NULL;
1332         struct rmlist *rmptr, *rmptr2;
1333         struct msglist *cmptr, *cmptr2;
1334         FILE *sysflfp, *tempflfp;
1335         int outgoing_msgs;
1336         long thismsg;
1337
1338         sprintf(tempflnm, tmpnam(NULL));
1339         tempflfp = fopen(tempflnm, "w");
1340         if (tempflfp == NULL)
1341                 return;
1342
1343
1344 /*
1345  * Read system file for node in question and put together room list
1346  */
1347         sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1348         sysflfp = fopen(sysflnm, "r");
1349         if (sysflfp == NULL)
1350                 return;
1351         fgets(shiptocmd, 128, sysflfp);
1352         shiptocmd[strlen(shiptocmd) - 1] = 0;
1353         while (!feof(sysflfp)) {
1354                 if (fgets(srmname, 32, sysflfp) == NULL)
1355                         break;
1356                 srmname[strlen(srmname) - 1] = 0;
1357                 fgets(lbuf, 32, sysflfp);
1358                 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1359                 rmptr->next = NULL;
1360                 strcpy(rmptr->rm_name, srmname);
1361                 strip_trailing_whitespace(rmptr->rm_name);
1362                 rmptr->rm_lastsent = atol(lbuf);
1363                 if (crmlist == NULL)
1364                         crmlist = rmptr;
1365                 else if (!strcasecmp(rmptr->rm_name, "control")) {
1366                         /* control has to be first in room list */
1367                         rmptr->next = crmlist;
1368                         crmlist = rmptr;
1369                 } else {
1370                         rmptr2 = crmlist;
1371                         while (rmptr2->next != NULL)
1372                                 rmptr2 = rmptr2->next;
1373                         rmptr2->next = rmptr;
1374                 }
1375         }
1376         fclose(sysflfp);
1377
1378 /*
1379  * Assemble list of messages to be spooled
1380  */
1381         for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1382
1383                 sprintf(buf, "GOTO %s", rmptr->rm_name);
1384                 serv_puts(buf);
1385                 serv_gets(buf);
1386                 if (buf[0] != '2') {
1387                         syslog(LOG_ERR, "%s", buf);
1388                 } else {
1389                         sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1390                         serv_puts(buf);
1391                         serv_gets(buf);
1392                         if (buf[0] == '1')
1393                                 while (serv_gets(buf), strcmp(buf, "000")) {
1394                                         thismsg = atol(buf);
1395                                         if (thismsg > (rmptr->rm_lastsent)) {
1396                                                 rmptr->rm_lastsent = thismsg;
1397
1398                                                 cmptr = (struct msglist *)
1399                                                     malloc(sizeof(struct msglist));
1400                                                 cmptr->next = NULL;
1401                                                 cmptr->m_num = thismsg;
1402                                                 strcpy(cmptr->m_rmname, rmptr->rm_name);
1403
1404                                                 if (cmlist == NULL)
1405                                                         cmlist = cmptr;
1406                                                 else {
1407                                                         cmptr2 = cmlist;
1408                                                         while (cmptr2->next != NULL)
1409                                                                 cmptr2 = cmptr2->next;
1410                                                         cmptr2->next = cmptr;
1411                                                 }
1412                                         }
1413                         } else {        /* print error from "msgs all" */
1414                                 syslog(LOG_ERR, "%s", buf);
1415                         }
1416                 }
1417         }
1418
1419         outgoing_msgs = 0;
1420         cmptr2 = cmlist;        /* this loop counts the messages */
1421         while (cmptr2 != NULL) {
1422                 ++outgoing_msgs;
1423                 cmptr2 = cmptr2->next;
1424         }
1425         syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1426                outgoing_msgs, sysname);
1427
1428 /*
1429  * Spool out the messages, but only if there are any.
1430  */
1431         if (outgoing_msgs != 0)
1432                 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1433         syslog(LOG_NOTICE, "%d messages actually spooled",
1434                outgoing_msgs);
1435
1436 /*
1437  * Deallocate list of spooled messages.
1438  */
1439         while (cmlist != NULL) {
1440                 cmptr = cmlist->next;
1441                 free(cmlist);
1442                 cmlist = cmptr;
1443         }
1444
1445 /*
1446  * Rewrite system file and deallocate room list.
1447  */
1448         syslog(LOG_NOTICE, "Spooling...");
1449         sysflfp = fopen(sysflnm, "w");
1450         fprintf(sysflfp, "%s\n", shiptocmd);
1451         for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1452                 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1453         fclose(sysflfp);
1454         while (crmlist != NULL) {
1455                 rmptr = crmlist->next;
1456                 free(crmlist);
1457                 crmlist = rmptr;
1458         }
1459
1460 /* 
1461  * Close temporary file, ship it out, and return
1462  */
1463         fclose(tempflfp);
1464         if (outgoing_msgs != 0)
1465                 ship_to(tempflnm, sysname);
1466         unlink(tempflnm);
1467 }
1468
1469
1470 /*
1471  * Connect netproc to the Citadel server running on this computer.
1472  */
1473 void np_attach_to_server(void)
1474 {
1475         char buf[256];
1476         char portname[8];
1477         char *args[] =
1478         {"netproc", "localhost", NULL, NULL};
1479
1480         syslog(LOG_NOTICE, "Attaching to server...");
1481         sprintf(portname, "%d", config.c_port_number);
1482         args[2] = portname;
1483         attach_to_server(3, args);
1484         serv_gets(buf);
1485         syslog(LOG_NOTICE, "%s", &buf[4]);
1486         sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1487         serv_puts(buf);
1488         serv_gets(buf);
1489         syslog(LOG_NOTICE, "%s", &buf[4]);
1490         if (buf[0] != '2') {
1491                 cleanup(2);
1492         }
1493 }
1494
1495
1496
1497 /*
1498  * main
1499  */
1500 int main(int argc, char **argv)
1501 {
1502         char allst[32];
1503         FILE *allfp;
1504         int a;
1505         int import_only = 0;    /* if set to 1, don't export anything */
1506
1507         openlog("netproc", LOG_PID, LOG_USER);
1508         strcpy(bbs_home_directory, BBSDIR);
1509
1510         /*
1511          * Change directories if specified
1512          */
1513         for (a = 1; a < argc; ++a) {
1514                 if (!strncmp(argv[a], "-h", 2)) {
1515                         strcpy(bbs_home_directory, argv[a]);
1516                         strcpy(bbs_home_directory, &bbs_home_directory[2]);
1517                         home_specified = 1;
1518                 } else if (!strcmp(argv[a], "-i")) {
1519                         import_only = 1;
1520                 } else {
1521                         fprintf(stderr, "netproc: usage: ");
1522                         fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1523                         exit(1);
1524                 }
1525         }
1526
1527 #ifdef DEBUG
1528         syslog(LOG_DEBUG, "Calling get_config()");
1529 #endif
1530         get_config();
1531
1532 #ifdef DEBUG
1533         syslog(LOG_DEBUG, "Creating lock file");
1534 #endif
1535         if (set_lockfile() != 0) {
1536                 syslog(LOG_NOTICE, "lock file exists: already running");
1537                 cleanup(1);
1538         }
1539         signal(SIGINT, cleanup);
1540         signal(SIGQUIT, cleanup);
1541         signal(SIGHUP, cleanup);
1542         signal(SIGTERM, cleanup);
1543
1544         syslog(LOG_NOTICE, "started.  pid=%d", getpid());
1545         fflush(stdout);
1546         np_attach_to_server();
1547         fflush(stdout);
1548
1549         if (load_syslist() != 0)
1550                 syslog(LOG_ERR, "cannot load sysinfo");
1551         setup_special_nodes();
1552
1553         /* Open the use table */
1554         use_table = gdbm_open("./data/usetable.gdbm", 512,
1555                               GDBM_WRCREAT, 0600, 0);
1556         if (use_table == NULL) {
1557                 syslog(LOG_ERR, "could not open use table: %s",
1558                        strerror(errno));
1559         }
1560
1561         /* first collect incoming stuff */
1562         inprocess();
1563
1564         /* Now process outbound messages, but NOT if this is just a
1565          * quick import-only run (i.e. the -i command-line option
1566          * was specified)
1567          */
1568         if (import_only != 1) {
1569                 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1570                 if (allfp != NULL) {
1571                         while (fgets(allst, 32, allfp) != NULL) {
1572                                 allst[strlen(allst) - 1] = 0;
1573                                 outprocess(allst);
1574                         }
1575                         pclose(allfp);
1576                 }
1577                 /* import again in case anything new was generated */
1578                 inprocess();
1579         }
1580
1581         /* Update mail.sysinfo with new information we learned */
1582         rewrite_syslist();
1583
1584         /* Delete any messages which need to be purged locally */
1585         process_purgelist();
1586
1587         /* Close the use table */
1588         purge_use_table(use_table);
1589         gdbm_close(use_table);
1590
1591         syslog(LOG_NOTICE, "processing ended.");
1592         cleanup(0);
1593         return 0;
1594 }