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