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