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