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
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.
12 #define NPLOGFILE "./netproc.log"
15 /* How long it takes for an old node to drop off the network map */
16 #define EXPIRY_TIME (2592000L)
18 /* Where do we keep our lock file? */
19 #define LOCKFILE "/var/lock/LCK.netproc"
21 /* Path to the 'uudecode' utility (needed for network file transfers) */
22 #define UUDECODE "/usr/bin/uudecode"
24 /* Uncomment the DEBUG def to see noisy traces */
31 #include <sys/types.h>
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.
49 #define FILTERROOM TWITROOM
65 struct filterlist *next;
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);
105 struct filterlist *filter = NULL;
106 char roomnames[MAXROOMS][20];
107 char roomdirs[MAXROOMS][15];
108 struct syslist *slist = NULL;
110 struct config config;
111 extern char bbs_home_directory[];
112 extern int home_specified;
117 * replacement strerror() for systems that don't have it
119 char *strerror(int e)
123 sprintf(buf,"errno = %d",e);
129 void strip_trailing_whitespace(char *buf)
131 while(isspace(buf[strlen(buf)-1]))
132 buf[strlen(buf)-1]=0;
137 * for performance optimization, netproc loads the list of room names (and
138 * their corresponding directory names, if applicable) into a table in memory.
140 int load_roomnames(void) {
142 struct quickroom qbuf;
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)
150 strcpy(roomnames[i],qbuf.QRname);
151 if (qbuf.QRflags & QR_DIRECTORY)
152 strcpy(roomdirs[i],qbuf.QRdirname);
154 strcpy(roomdirs[i],config.c_bucket_dir);
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.
165 int load_syslist(void) {
167 struct syslist *stemp;
171 fp=fopen("network/mail.sysinfo","rb");
172 if (fp==NULL) return(1);
175 if (fgets(buf,128,fp)==NULL) {
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) ) {
184 stemp =(struct syslist *)malloc(sizeof(struct syslist));
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,"");
195 else if ( (insys==1) && (strlen(buf)==0) ) {
198 else if ( (insys==1) && (!strncmp(buf,"bin",3)) ) {
199 strcpy(slist->s_type,"bin");
200 strcpy(slist->s_nexthop,&buf[4]);
202 else if ( (insys==1) && (!strncmp(buf,"use",3)) ) {
203 strcpy(slist->s_type,"use");
204 strcpy(slist->s_nexthop,&buf[4]);
206 else if ( (insys==1) && (!strncmp(buf,"uum",3)) ) {
207 strcpy(slist->s_type,"uum");
208 strcpy(slist->s_nexthop,&buf[4]);
210 else if ( (insys==1) && (!strncmp(buf,"lastcontact",11)) ) {
211 sscanf(&buf[12],"%ld",&slist->s_lastcontact);
213 else if ( (insys==1) && (!strncmp(buf,"humannode",9)) ) {
214 strcpy(slist->s_humannode,&buf[10]);
216 else if ( (insys==1) && (!strncmp(buf,"phonenum",8)) ) {
217 strcpy(slist->s_phonenum,&buf[9]);
219 else if ( (insys==1) && (!strncmp(buf,"gdom",4)) ) {
220 strcpy(slist->s_gdom,&buf[5]);
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
228 void setup_special_nodes(void) {
229 struct syslist *stemp,*slocal;
232 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
233 if (!strcasecmp(stemp->s_name,config.c_nodename)) slocal=stemp;
236 slocal =(struct syslist *)malloc(sizeof(struct syslist));
237 slocal->next = slist;
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);
248 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
249 if (!strcasecmp(stemp->s_name,"internet")) slocal=stemp;
252 slocal =(struct syslist *)malloc(sizeof(struct syslist));
253 slocal->next = slist;
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,"");
267 * here's the routine to write the table back to disk.
269 void rewrite_syslist(void) {
270 struct syslist *stemp;
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);
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)));
302 /* now free the list */
303 while (slist!=NULL) {
311 /* call this function with the node name of a system and it returns a pointer
312 * to its syslist structure.
314 struct syslist *get_sys_ptr(char *sysname)
316 static char sysnambuf[16];
317 static struct syslist *sysptrbuf = NULL;
318 struct syslist *stemp;
320 if ( (!strcmp(sysname,sysnambuf))
321 && (sysptrbuf!=NULL) ) return(sysptrbuf);
323 strcpy(sysnambuf,sysname);
324 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
325 if (!strcmp(sysname,stemp->s_name)) {
336 * make sure only one copy of netproc runs at a time, using lock files
338 int set_lockfile(void) {
344 if (access(LOCKFILE,0)==0) {
347 * if the /proc filesystem is available, we can further check to
348 * make sure that the process that wrote the lock file is actually
349 * running, and didn't simply terminate and not clean up after itself
352 lfp = fopen(LOCKFILE,"r");
353 fscanf(lfp,"%d",&onppid);
355 sprintf(buf,"/proc/%d/cmdline",onppid);
356 if (access(buf,0)==0) ok = 0;
362 if (ok == 0) return(1);
363 lfp=fopen(LOCKFILE,"w");
364 fprintf(lfp,"%d\n",getpid());
369 void remove_lockfile(void) {
374 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
375 * cleanup() . If for some reason netproc hangs waiting for the server
376 * to clean up, the alarm clock goes off and the program exits anyway.
377 * The cleanup() routine makes a check to ensure it's not reentering, in
378 * case the ipc module looped it somehow.
380 void nq_cleanup(int e)
388 static int nested = 0;
391 signal(SIGALRM,nq_cleanup);
392 if (nested++ < 1) serv_puts("QUIT");
397 * This is implemented as a function rather than as a macro because the
398 * client-side IPC modules expect logoff() to be defined. They call logoff()
399 * when a problem connecting or staying connected to the server occurs.
407 * If there is a kill file in place, this function will process it.
409 void load_filterlist(void) {
411 struct filterlist *fbuf;
414 fp=fopen("./network/filterlist","r");
415 if (fp==NULL) return;
416 while (fgets(sbuf,256,fp)!=NULL) {
418 sbuf[strlen(sbuf)-1]=0;
419 fbuf=(struct filterlist *)
420 malloc((long)sizeof(struct filterlist));
423 strcpy(fbuf->f_person,"*");
424 strcpy(fbuf->f_room,"*");
425 strcpy(fbuf->f_system,"*");
427 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
430 strcpy(fbuf->f_person,sbuf);
431 strcpy(sbuf,&sbuf[p+1]);
433 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
436 strcpy(fbuf->f_room,sbuf);
437 strcpy(sbuf,&sbuf[p+1]);
439 strcpy(fbuf->f_system,sbuf);
445 /* returns 1 if user/message/room combination is in the kill file */
446 int is_banned(char *k_person, char *k_room, char *k_system)
448 struct filterlist *fptr;
450 for (fptr=filter; fptr!=NULL; fptr=fptr->next) if (
451 ((!strcasecmp(fptr->f_person,k_person))||(!strcmp(fptr->f_person,"*")))
453 ((!strcasecmp(fptr->f_room,k_room))||(!strcmp(fptr->f_room,"*")))
455 ((!strcasecmp(fptr->f_system,k_system))||(!strcmp(fptr->f_system,"*")))
461 int get_sysinfo_type(char *name) /* determine routing from sysinfo file */
463 struct syslist *stemp;
464 GETSN: for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
465 if (!strcasecmp(stemp->s_name,name)) {
466 if (!strcasecmp(stemp->s_type,"use")) {
467 strcpy(name,stemp->s_nexthop);
470 if (!strcasecmp(stemp->s_type,"bin")) {
473 if (!strcasecmp(stemp->s_type,"uum")) {
478 printf("netproc: cannot find system '%s' in mail.sysinfo\n",name);
483 void fpgetfield(FILE *fp, char *string)
503 * Load all of the fields of a message, except the actual text, into a
504 * table in memory (so we know how to process the message).
506 void msgfind(char *msgfile, struct minfo *buffer)
514 fp=fopen(msgfile,"rb");
516 fprintf(stderr,"Can't open message file: %s\n",strerror(errno));
521 fprintf(stdout,"Incorrect message format\n");
524 mtype=getc(fp); aflag=getc(fp);
533 BONFGM: b=getc(fp); if (b<0) goto END;
534 if (b=='M') goto END;
536 while ((bbb[0]==' ')&&(strlen(bbb)>1)) strcpy(bbb,&bbb[1]);
538 strcpy(buffer->A,bbb);
539 if (strlen(userid)==0) {
541 for (e=0; e<strlen(userid); ++e)
542 if (userid[e]==' ') userid[e]='_';
545 if (b=='O') strcpy(buffer->O,bbb);
546 if (b=='C') strcpy(buffer->C,bbb);
547 if (b=='N') strcpy(buffer->N,bbb);
548 if (b=='S') strcpy(buffer->S,bbb);
550 /* extract the user id from the path */
551 for (e=0; e<strlen(bbb); ++e)
552 if (bbb[e]=='!') strcpy(userid,&bbb[e+1]);
554 /* now find the next hop */
555 for (e=0; e<strlen(bbb); ++e) if (bbb[e]=='!') bbb[e]=0;
556 strcpy(buffer->nexthop,bbb);
559 for (e=0; e<strlen(bbb); ++e) if (bbb[e]=='_') bbb[e]=' ';
560 strcpy(buffer->R,bbb);
562 if (b=='D') strcpy(buffer->D,bbb);
563 if (b=='T') buffer->T=atol(bbb);
564 if (b=='I') buffer->I=atol(bbb);
565 if (b=='H') strcpy(buffer->H,bbb);
566 if (b=='B') strcpy(buffer->B,bbb);
567 if (b=='G') strcpy(buffer->G,bbb);
568 if (b=='E') strcpy(buffer->E,bbb);
571 END: if (buffer->I==0L) buffer->I=buffer->T;
575 void ship_to(char *filenm, char *sysnm) /* send spool file filenm to system sysnm */
584 fprintf(stdout,"netproc: shipping %s to %s\n",filenm,sysnm);
586 sprintf(sysflnm,"./network/systems/%s",sysnm);
587 sysflfd=fopen(sysflnm,"r");
588 if (sysflfd==NULL) fprintf(stdout,"netproc: cannot open %s\n",sysflnm);
589 fgets(commbuf1,99,sysflfd);
590 commbuf1[strlen(commbuf1)-1] = 0;
592 sprintf(commbuf2,commbuf1,filenm);
597 * proc_file_transfer() - handle a simple file transfer packet
599 void proc_file_transfer(char *tname)
600 { /* name of temp file containing the whole message */
606 printf("netproc: processing network file transfer...\n");
607 strcpy(dest_dir,config.c_bucket_dir);
609 tfp=fopen(tname,"rb");
610 if (tfp==NULL) printf("netproc: cannot open %s\n",tname);
611 getc(tfp); getc(tfp); getc(tfp);
616 if (a=='O') for (b=0; b<MAXROOMS; ++b) {
617 if (!strcasecmp(buf,roomnames[b]))
618 strcpy(dest_dir,roomdirs[b]);
621 } while ((a!='M')&&(a>=0));
624 printf("netproc: no message text for file transfer\n");
628 sprintf(buf,"cd %s/files/%s; exec %s",bbs_home_directory,dest_dir,UUDECODE);
629 uud=(FILE *)popen(buf,"w");
631 printf("netproc: cannot open uudecode pipe\n");
637 buf[strlen(buf)-1] = 0;
638 for (a=0; a<strlen(buf); ++a) if (buf[a]=='/') buf[a]='_';
639 fprintf(uud,"%s\n",buf);
640 printf("netproc: %s\n",buf);
641 while(a=getc(tfp), a>0) putc(a,uud);
648 /* send a bounce message */
649 void bounce(struct minfo *bminfo)
657 sprintf(bfilename,"./network/spoolin/bounce.%d.%d",getpid(),bseq++);
658 bounce = fopen(bfilename,"wb");
661 fprintf(bounce,"%c%c%c",0xFF,MES_NORMAL,0);
662 fprintf(bounce,"Ppostmaster%c",0);
663 fprintf(bounce,"T%ld%c",now,0);
664 fprintf(bounce,"APostmaster%c",0);
665 fprintf(bounce,"OMail%c",0);
666 fprintf(bounce,"N%s%c",config.c_nodename,0);
667 fprintf(bounce,"H%s%c",config.c_humannode,0);
669 if (strlen(bminfo->E) > 0) {
670 fprintf(bounce,"R%s%c",bminfo->E,0);
673 fprintf(bounce,"R%s%c",bminfo->A,0);
676 fprintf(bounce,"D%s%c",bminfo->N,0);
677 fprintf(bounce,"M%s could not deliver your mail to:\n",
679 fprintf(bounce," \n %s\n \n",bminfo->R);
680 fprintf(bounce," because there is no such user on this system.\n");
681 fprintf(bounce," (Unsent message does *not* follow. ");
682 fprintf(bounce,"Help to conserve bandwidth.)\n%c",0);
689 * process incoming files in ./network/spoolin
691 void inprocess(void) {
692 FILE *fp,*message,*testfp,*ls;
693 static struct minfo minfo;
694 struct recentmsg recentmsg;
695 char tname[128],aaa[1024],iname[256],sfilename[256],pfilename[256];
698 struct syslist *stemp;
705 sprintf(tname,"/tmp/net.t%d",getpid()); /* temp file name */
706 sprintf(iname,"/tmp/net.i%d",getpid()); /* temp file name */
710 chdir(bbs_home_directory);
712 /* Let the shell do the dirty work. Get all data from spoolin */
714 sprintf(aaa,"cd %s/network/spoolin; ls",bbs_home_directory);
717 fprintf(stderr,"netproc: could not open dir cmd: %s\n",
722 ptr=fgets(sfilename,256,ls);
723 if (ptr!=NULL) sfilename[strlen(sfilename)-1] = 0;
724 } while( (ptr!=NULL)&&((!strcmp(sfilename,"."))
725 ||(!strcmp(sfilename,".."))));
726 if (ptr!=NULL) printf("netproc: processing %s\n",sfilename);
731 sprintf(pfilename,"%s/network/spoolin/%s",bbs_home_directory,sfilename);
732 fprintf(stderr,"netproc: processing <%s>\n", pfilename);
735 fp = fopen(pfilename, "rb");
737 fprintf(stderr, "netproc: cannot open <%s>: %s\n",
738 pfilename, strerror(errno));
740 fp = fopen("/dev/null" ,"rb");
743 NXMSG: /* Seek to the beginning of the next message */
746 } while((a!=255)&&(a>=0));
747 if (a<0) goto ENDSTR;
749 message = fopen(tname,"wb"); /* This crates the temporary file. */
750 if (message == NULL) {
751 fprintf(stderr, "Error creating %s: %s\n",
752 tname, strerror(errno));
755 putc(255,message); /* 0xFF (start-of-message) */
756 a = getc(fp); putc(a, message); /* type */
757 a = getc(fp); putc(a, message); /* mode */
759 FieldID = getc(fp); /* Header field ID */
760 putc(FieldID, message);
765 } while ((FieldID != 'M') && (a >= 0)); /* M is always last */
767 msglen = ftell(message);
770 /* process the individual mesage */
776 msgfind(tname,&minfo);
777 strncpy(recentmsg.RMnodename,minfo.N,9);
778 recentmsg.RMnodename[9]=0;
779 recentmsg.RMnum=minfo.I;
780 printf("netproc: #%ld fm <%s> in <%s> @ <%s>\n",
781 minfo.I,minfo.A,minfo.O,minfo.N);
782 if (strlen(minfo.R)>0) {
783 printf(" to <%s>",minfo.R);
784 if (strlen(minfo.D)>0) {
785 printf(" @ <%s>",minfo.D);
790 if (!strcasecmp(minfo.D,FQDN)) strcpy(minfo.D,NODENAME);
792 /* this routine updates our info on the system that sent the message */
793 stemp = get_sys_ptr(minfo.N);
794 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
795 /* add non-neighbor system to map */
796 printf("Adding non-neighbor system <%s> to map\n", slist->s_name);
797 stemp = (struct syslist *)malloc((long)sizeof(struct syslist));
800 strcpy(slist->s_name,minfo.N);
801 strcpy(slist->s_type,"use");
802 strcpy(slist->s_nexthop,minfo.nexthop);
803 time(&slist->s_lastcontact);
805 else if ((stemp == NULL) && (!strcasecmp(minfo.N,minfo.nexthop))) {
806 /* add neighbor system to map */
807 printf("Adding neighbor system <%s> to map\n", slist->s_name);
808 sprintf(aaa,"%s/network/systems/%s",bbs_home_directory,minfo.N);
809 testfp=fopen(aaa,"r");
812 stemp = (struct syslist *)
813 malloc((long)sizeof(struct syslist));
816 strcpy(slist->s_name,minfo.N);
817 strcpy(slist->s_type,"bin");
818 strcpy(slist->s_nexthop,"Mail");
819 time(&slist->s_lastcontact);
822 /* now update last contact and long node name if we can */
824 time(&stemp->s_lastcontact);
825 if (strlen(minfo.H) > 0) strcpy(stemp->s_humannode,minfo.H);
826 if (strlen(minfo.B) > 0) strcpy(stemp->s_phonenum,minfo.B);
827 if (strlen(minfo.G) > 0) strcpy(stemp->s_gdom,minfo.G);
830 /* route the message if necessary */
831 if ((strcasecmp(minfo.D,NODENAME))&&(minfo.D[0]!=0)) {
832 a = get_sysinfo_type(minfo.D);
833 printf("netproc: routing message to system <%s>\n",minfo.D);
837 printf("netproc: netmailer %s\n", tname);
839 execlp("./netmailer","netmailer",
841 printf("netproc: error running netmailer: %s\n",
846 else while (wait(&b)!=(-1));
848 else if (a==M_BINARY) {
849 ship_to(tname,minfo.D);
852 /* message falls into the bit bucket? */
856 /* check to see if it's a file transfer */
857 else if (!strncasecmp(minfo.S,"FILE",4)) {
858 proc_file_transfer(tname);
861 /* otherwise process it as a normal message */
864 if (!strcasecmp(minfo.R, "postmaster")) {
866 strcpy(minfo.C, "Aide");
869 if (strlen(minfo.R) > 0) {
870 sprintf(buf,"GOTO _MAIL_");
872 if (is_banned(minfo.A,minfo.C,minfo.N)) {
873 sprintf(buf,"GOTO %s", FILTERROOM);
876 if (strlen(minfo.C) > 0) {
877 sprintf(buf,"GOTO %s", minfo.C);
880 sprintf(buf,"GOTO %s", minfo.O);
886 puts(buf); fflush(stdout);
887 sprintf(buf,"GOTO _BITBUCKET_");
892 /* Open the temporary file containing the message */
893 message = fopen(tname, "rb");
894 if (message == NULL) {
895 fprintf(stderr, "netproc: cannot open %s: %s\n",
896 tname, strerror(errno));
901 /* Transmit the message to the server */
902 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
903 printf("< %s\n", buf);
906 printf("> %s\n", buf);
907 if (!strncmp(buf, "570", 3)) {
908 /* no such user, do a bounce */
912 /* Always use the server's idea of the message length,
913 * even though they should both be identical */
914 msglen = atol(&buf[4]);
915 while (msglen > 0L) {
916 bloklen = ((msglen >= 255L) ? 255 : ((int)msglen));
917 if (fread(buf, bloklen, 1, message) < 1) {
918 fprintf(stderr, "netproc: error trying to read %d bytes: %s\n",
919 bloklen, strerror(errno));
922 serv_write(buf, bloklen);
923 msglen = msglen - (long)bloklen;
947 int checkpath(char *path, char *sys) /* Checks to see whether its ok to send */
948 /* Returns 1 for ok, send message */
949 { /* Returns 0 if message already there */
956 printf("netproc: checkpath <%s> <%s> ... ",path,sys);
958 for (a=0; a<strlen(path); ++a) {
959 if (!strncmp(&path[a],sys2,strlen(sys2))) return(0);
965 * implement split horizon algorithm
967 int ismsgok(long int mpos, FILE *mmfp, char *sysname)
970 int ok = 0; /* fail safe - no path, don't send it */
974 if (getc(mmfp)!=255) return(0);
975 getc(mmfp); getc(mmfp);
977 while (a=getc(mmfp),((a!='M')&&(a!=0))) {
978 fpgetfield(mmfp,fbuf);
980 ok = checkpath(fbuf,sysname);
984 printf("%s\n", ((ok)?"SEND":"(no)") );
989 int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list of messages to a file */
990 /* returns # of msgs spooled */
994 struct msglist *cmptr;
999 int msgs_spooled = 0;
1006 strcpy(curr_rm, "");
1007 sprintf(mmtemp, "/tmp/net.m%d", getpid());
1009 /* for each message in the list... */
1010 for (cmptr=cmlist; cmptr!=NULL; cmptr=cmptr->next) {
1012 /* make sure we're in the correct room... */
1013 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1014 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1017 if (buf[0] == '2') {
1018 strcpy(curr_rm, cmptr->m_rmname);
1021 fprintf(stderr,"%s\n", buf);
1025 /* download the message from the server... */
1026 mmfp = fopen(mmtemp, "wb");
1027 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1030 if (buf[0]=='6') { /* read the msg */
1031 msg_len = atol(&buf[4]);
1032 while (msg_len > 0L) {
1033 blok_len = ((msg_len >= 256L) ? 256 : (int)msg_len);
1034 serv_read(buf, blok_len);
1035 fwrite(buf, blok_len, 1, mmfp);
1036 msg_len = msg_len - (long)blok_len;
1039 else { /* or print the err */
1040 fprintf(stderr, "%s\n", buf);
1044 mmfp = fopen(mmtemp,"rb");
1046 if (ismsgok(0L,mmfp,sysname)) {
1050 fread(fbuf,3,1,mmfp);
1051 fwrite(fbuf,3,1,destfp);
1052 while (a=getc(mmfp),((a!=0)&&(a!='M'))) {
1053 if (a!='C') putc(a,destfp);
1054 fpgetfield(mmfp,fbuf);
1055 if (a=='P') fprintf(destfp,"%s!",NODENAME);
1057 fwrite(fbuf,strlen(fbuf)+1,1,destfp);
1060 fprintf(destfp, "C%s%c",
1061 cmptr->m_rmname, 0);
1073 return(msgs_spooled);
1076 void outprocess(char *sysname) /* send new room messages to sysname */
1080 char shiptocmd[128];
1084 struct msglist *cmlist = NULL;
1085 struct rmlist *crmlist = NULL;
1086 struct rmlist *rmptr,*rmptr2;
1087 struct msglist *cmptr,*cmptr2;
1088 FILE *sysflfp,*tempflfp;
1092 sprintf(tempflnm,"/tmp/%s.%d",NODENAME,getpid());
1093 tempflfp=fopen(tempflnm,"w");
1094 if (tempflfp==NULL) return;
1098 * Read system file for node in question and put together room list
1100 sprintf(sysflnm,"%s/network/systems/%s",bbs_home_directory,sysname);
1101 sysflfp=fopen(sysflnm,"r");
1102 if (sysflfp==NULL) return;
1103 fgets(shiptocmd,128,sysflfp); shiptocmd[strlen(shiptocmd)-1]=0;
1104 while(!feof(sysflfp)) {
1105 if (fgets(srmname,32,sysflfp)==NULL) break;
1106 srmname[strlen(srmname)-1]=0;
1107 fgets(lbuf,32,sysflfp);
1108 rmptr=(struct rmlist *)malloc(sizeof(struct rmlist));
1110 strcpy(rmptr->rm_name,srmname);
1111 strip_trailing_whitespace(rmptr->rm_name);
1112 rmptr->rm_lastsent = atol(lbuf);
1113 if (crmlist==NULL) crmlist=rmptr;
1114 else if (!strcasecmp(rmptr->rm_name,"control")) {
1115 /* control has to be first in room list */
1116 rmptr->next = crmlist;
1121 while (rmptr2->next != NULL) rmptr2=rmptr2->next;
1128 * Assemble list of messages to be spooled
1130 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next) {
1132 sprintf(buf,"GOTO %s",rmptr->rm_name);
1136 fprintf(stderr, "%s\n", buf);
1139 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1142 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf,"000")) {
1143 thismsg = atol(buf);
1144 if ( thismsg > (rmptr->rm_lastsent) ) {
1145 rmptr->rm_lastsent = thismsg;
1147 cmptr=(struct msglist *)
1148 malloc(sizeof(struct msglist));
1150 cmptr->m_num = thismsg;
1151 strcpy(cmptr->m_rmname, rmptr->rm_name);
1153 if (cmlist == NULL) cmlist = cmptr;
1156 while (cmptr2->next != NULL)
1157 cmptr2 = cmptr2->next;
1158 cmptr2->next = cmptr;
1162 else { /* print error from "msgs all" */
1163 fprintf(stderr, "%s\n", buf);
1168 outgoing_msgs=0; cmptr2=cmlist; /* this loop counts the messages */
1169 while (cmptr2!=NULL) {
1171 cmptr2 = cmptr2->next;
1173 printf("netproc: %d messages to be spooled to %s\n",
1174 outgoing_msgs,sysname);
1178 * Spool out the messages, but only if there are any.
1181 if (outgoing_msgs!=0) outgoing_msgs=spool_out(cmlist,tempflfp,sysname);
1182 printf("netproc: %d messages actually spooled\n",
1187 * Deallocate list of spooled messages.
1189 while(cmlist!=NULL) {
1196 * Rewrite system file and deallocate room list.
1198 printf("Spooling...\n");
1200 sysflfp=fopen(sysflnm,"w");
1201 fprintf(sysflfp,"%s\n",shiptocmd);
1202 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next)
1203 fprintf(sysflfp,"%s\n%ld\n",rmptr->rm_name,rmptr->rm_lastsent);
1205 while(crmlist!=NULL) {
1206 rmptr=crmlist->next;
1212 * Close temporary file, ship it out, and return
1215 if (outgoing_msgs!=0) ship_to(tempflnm,sysname);
1221 * Connect netproc to the Citadel server running on this computer.
1223 void np_attach_to_server(void) {
1226 char *args[] = { "netproc", "localhost", NULL, NULL } ;
1228 printf("Attaching to server...\n");
1229 sprintf(portname, "%d", config.c_port_number);
1231 attach_to_server(3, args);
1233 printf("%s\n",&buf[4]);
1234 sprintf(buf,"IPGM %d", config.c_ipgm_secret);
1237 printf("%s\n",&buf[4]);
1248 void main(int argc, char **argv)
1255 strcpy(bbs_home_directory, BBSDIR);
1258 * Change directories if specified
1260 if (argv > 0) for (a=1; a<argc; ++a) {
1261 if (!strncmp(argv[a], "-h", 2)) {
1262 strcpy(bbs_home_directory, argv[a]);
1263 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1267 fprintf(stderr, "netproc: usage: netproc [-hHomeDir]\n");
1274 /* write all messages to the log from this point onward */
1275 freopen(NPLOGFILE,NPLOGMODE,stdout);
1276 freopen(NPLOGFILE,NPLOGMODE,stderr);
1278 if (set_lockfile()!=0) {
1279 fprintf(stderr,"netproc: lock file exists: already running\n");
1283 signal(SIGINT,cleanup);
1284 signal(SIGQUIT,cleanup);
1285 signal(SIGHUP,cleanup);
1286 signal(SIGTERM,cleanup);
1288 printf("netproc: started. pid=%d\n",getpid());
1290 np_attach_to_server();
1293 if (load_roomnames()!=0) fprintf(stdout,"netproc: cannot load rooms\n");
1294 if (load_syslist()!=0) fprintf(stdout,"netproc: cannot load sysinfo\n");
1295 setup_special_nodes();
1297 inprocess(); /* first collect incoming stuff */
1299 allfp=(FILE *)popen("cd ./network/systems; ls","r");
1301 while (fgets(allst,32,allfp)!=NULL) {
1302 allst[strlen(allst)-1] = 0;
1308 inprocess(); /* incoming again in case anything new was generated */
1310 printf("netproc: processing ended.\n");