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 struct syslist *slist = NULL;
108 struct config config;
109 extern char bbs_home_directory[];
110 extern int home_specified;
113 #ifndef HAVE_STRERROR
115 * replacement strerror() for systems that don't have it
117 char *strerror(int e)
121 sprintf(buf,"errno = %d",e);
127 void strip_trailing_whitespace(char *buf)
129 while(isspace(buf[strlen(buf)-1]))
130 buf[strlen(buf)-1]=0;
135 * we also load the network/mail.sysinfo table into memory, make changes
136 * as we learn more about the network from incoming messages, and write
137 * the table back to disk when we're done.
139 int load_syslist(void) {
141 struct syslist *stemp;
145 fp=fopen("network/mail.sysinfo","rb");
146 if (fp==NULL) return(1);
149 if (fgets(buf,128,fp)==NULL) {
153 buf[strlen(buf)-1] = 0;
154 while (isspace(buf[0])) strcpy(buf,&buf[1]);
155 if (buf[0]=='#') buf[0]=0;
156 if ( (insys==0) && (strlen(buf)!=0) ) {
158 stemp =(struct syslist *)malloc(sizeof(struct syslist));
161 strcpy(slist->s_name,buf);
162 strcpy(slist->s_type,"bin");
163 strcpy(slist->s_nexthop,"Mail");
164 slist->s_lastcontact = 0L;
165 strcpy(slist->s_humannode,"");
166 strcpy(slist->s_phonenum,"");
167 strcpy(slist->s_gdom,"");
169 else if ( (insys==1) && (strlen(buf)==0) ) {
172 else if ( (insys==1) && (!strncmp(buf,"bin",3)) ) {
173 strcpy(slist->s_type,"bin");
174 strcpy(slist->s_nexthop,&buf[4]);
176 else if ( (insys==1) && (!strncmp(buf,"use",3)) ) {
177 strcpy(slist->s_type,"use");
178 strcpy(slist->s_nexthop,&buf[4]);
180 else if ( (insys==1) && (!strncmp(buf,"uum",3)) ) {
181 strcpy(slist->s_type,"uum");
182 strcpy(slist->s_nexthop,&buf[4]);
184 else if ( (insys==1) && (!strncmp(buf,"lastcontact",11)) ) {
185 sscanf(&buf[12],"%ld",&slist->s_lastcontact);
187 else if ( (insys==1) && (!strncmp(buf,"humannode",9)) ) {
188 strcpy(slist->s_humannode,&buf[10]);
190 else if ( (insys==1) && (!strncmp(buf,"phonenum",8)) ) {
191 strcpy(slist->s_phonenum,&buf[9]);
193 else if ( (insys==1) && (!strncmp(buf,"gdom",4)) ) {
194 strcpy(slist->s_gdom,&buf[5]);
199 /* now we have to set up two "special" nodes on the list: one
200 * for the local node, and one for an Internet gateway
202 void setup_special_nodes(void) {
203 struct syslist *stemp,*slocal;
206 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
207 if (!strcasecmp(stemp->s_name,config.c_nodename)) slocal=stemp;
210 slocal =(struct syslist *)malloc(sizeof(struct syslist));
211 slocal->next = slist;
214 strcpy(slocal->s_name,config.c_nodename);
215 strcpy(slocal->s_type,"bin");
216 strcpy(slocal->s_nexthop,"Mail");
217 time(&slocal->s_lastcontact);
218 strcpy(slocal->s_humannode,config.c_humannode);
219 strcpy(slocal->s_phonenum,config.c_phonenum);
222 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
223 if (!strcasecmp(stemp->s_name,"internet")) slocal=stemp;
226 slocal =(struct syslist *)malloc(sizeof(struct syslist));
227 slocal->next = slist;
230 strcpy(slocal->s_name,"internet");
231 strcpy(slocal->s_type,"uum");
232 strcpy(slocal->s_nexthop,"%s");
233 time(&slocal->s_lastcontact);
234 strcpy(slocal->s_humannode,"Internet Gateway");
235 strcpy(slocal->s_phonenum,"");
236 strcpy(slocal->s_gdom,"");
241 * here's the routine to write the table back to disk.
243 void rewrite_syslist(void) {
244 struct syslist *stemp;
249 newfp=fopen("network/mail.sysinfo","w");
250 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
251 if (!strcasecmp(stemp->s_name,config.c_nodename)) {
252 time(&stemp->s_lastcontact);
253 strcpy(stemp->s_type,"bin");
254 strcpy(stemp->s_humannode,config.c_humannode);
255 strcpy(stemp->s_phonenum,config.c_phonenum);
257 /* remove systems we haven't heard from in a while */
258 if ( (stemp->s_lastcontact == 0L)
259 || (now - stemp->s_lastcontact < EXPIRY_TIME) ) {
260 fprintf(newfp,"%s\n%s %s\n",
261 stemp->s_name,stemp->s_type,stemp->s_nexthop);
262 if (strlen(stemp->s_phonenum) > 0)
263 fprintf(newfp,"phonenum %s\n",stemp->s_phonenum);
264 if (strlen(stemp->s_gdom) > 0)
265 fprintf(newfp,"gdom %s\n",stemp->s_gdom);
266 if (strlen(stemp->s_humannode) > 0)
267 fprintf(newfp,"humannode %s\n",stemp->s_humannode);
268 if (stemp->s_lastcontact > 0L)
269 fprintf(newfp,"lastcontact %ld %s",
270 stemp->s_lastcontact,
271 asctime(localtime(&stemp->s_lastcontact)));
276 /* now free the list */
277 while (slist!=NULL) {
285 /* call this function with the node name of a system and it returns a pointer
286 * to its syslist structure.
288 struct syslist *get_sys_ptr(char *sysname)
290 static char sysnambuf[16];
291 static struct syslist *sysptrbuf = NULL;
292 struct syslist *stemp;
294 if ( (!strcmp(sysname,sysnambuf))
295 && (sysptrbuf!=NULL) ) return(sysptrbuf);
297 strcpy(sysnambuf,sysname);
298 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
299 if (!strcmp(sysname,stemp->s_name)) {
310 * make sure only one copy of netproc runs at a time, using lock files
312 int set_lockfile(void) {
316 if ((lfp = fopen(LOCKFILE,"r")) != NULL) {
317 fscanf(lfp,"%d",&onppid);
319 if (!kill(onppid, 0) || errno == EPERM) return 1;
322 lfp=fopen(LOCKFILE,"w");
323 fprintf(lfp,"%d\n",getpid());
328 void remove_lockfile(void) {
333 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
334 * cleanup() . If for some reason netproc hangs waiting for the server
335 * to clean up, the alarm clock goes off and the program exits anyway.
336 * The cleanup() routine makes a check to ensure it's not reentering, in
337 * case the ipc module looped it somehow.
339 void nq_cleanup(int e)
347 static int nested = 0;
350 signal(SIGALRM,nq_cleanup);
351 if (nested++ < 1) serv_puts("QUIT");
356 * This is implemented as a function rather than as a macro because the
357 * client-side IPC modules expect logoff() to be defined. They call logoff()
358 * when a problem connecting or staying connected to the server occurs.
366 * If there is a kill file in place, this function will process it.
368 void load_filterlist(void) {
370 struct filterlist *fbuf;
373 fp=fopen("./network/filterlist","r");
374 if (fp==NULL) return;
375 while (fgets(sbuf,256,fp)!=NULL) {
377 sbuf[strlen(sbuf)-1]=0;
378 fbuf=(struct filterlist *)
379 malloc((long)sizeof(struct filterlist));
382 strcpy(fbuf->f_person,"*");
383 strcpy(fbuf->f_room,"*");
384 strcpy(fbuf->f_system,"*");
386 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
389 strcpy(fbuf->f_person,sbuf);
390 strcpy(sbuf,&sbuf[p+1]);
392 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
395 strcpy(fbuf->f_room,sbuf);
396 strcpy(sbuf,&sbuf[p+1]);
398 strcpy(fbuf->f_system,sbuf);
404 /* returns 1 if user/message/room combination is in the kill file */
405 int is_banned(char *k_person, char *k_room, char *k_system)
407 struct filterlist *fptr;
409 for (fptr=filter; fptr!=NULL; fptr=fptr->next) if (
410 ((!strcasecmp(fptr->f_person,k_person))||(!strcmp(fptr->f_person,"*")))
412 ((!strcasecmp(fptr->f_room,k_room))||(!strcmp(fptr->f_room,"*")))
414 ((!strcasecmp(fptr->f_system,k_system))||(!strcmp(fptr->f_system,"*")))
420 int get_sysinfo_type(char *name) /* determine routing from sysinfo file */
422 struct syslist *stemp;
423 GETSN: for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
424 if (!strcasecmp(stemp->s_name,name)) {
425 if (!strcasecmp(stemp->s_type,"use")) {
426 strcpy(name,stemp->s_nexthop);
429 if (!strcasecmp(stemp->s_type,"bin")) {
432 if (!strcasecmp(stemp->s_type,"uum")) {
437 printf("netproc: cannot find system '%s' in mail.sysinfo\n",name);
442 void fpgetfield(FILE *fp, char *string)
462 * Load all of the fields of a message, except the actual text, into a
463 * table in memory (so we know how to process the message).
465 void msgfind(char *msgfile, struct minfo *buffer)
473 fp=fopen(msgfile,"rb");
475 fprintf(stderr,"Can't open message file: %s\n",strerror(errno));
480 fprintf(stdout,"Incorrect message format\n");
483 mtype=getc(fp); aflag=getc(fp);
492 BONFGM: b=getc(fp); if (b<0) goto END;
493 if (b=='M') goto END;
495 while ((bbb[0]==' ')&&(strlen(bbb)>1)) strcpy(bbb,&bbb[1]);
497 strcpy(buffer->A,bbb);
498 if (strlen(userid)==0) {
500 for (e=0; e<strlen(userid); ++e)
501 if (userid[e]==' ') userid[e]='_';
504 if (b=='O') strcpy(buffer->O,bbb);
505 if (b=='C') strcpy(buffer->C,bbb);
506 if (b=='N') strcpy(buffer->N,bbb);
507 if (b=='S') strcpy(buffer->S,bbb);
509 /* extract the user id from the path */
510 for (e=0; e<strlen(bbb); ++e)
511 if (bbb[e]=='!') strcpy(userid,&bbb[e+1]);
513 /* now find the next hop */
514 for (e=0; e<strlen(bbb); ++e) if (bbb[e]=='!') bbb[e]=0;
515 strcpy(buffer->nexthop,bbb);
518 for (e=0; e<strlen(bbb); ++e) if (bbb[e]=='_') bbb[e]=' ';
519 strcpy(buffer->R,bbb);
521 if (b=='D') strcpy(buffer->D,bbb);
522 if (b=='T') buffer->T=atol(bbb);
523 if (b=='I') buffer->I=atol(bbb);
524 if (b=='H') strcpy(buffer->H,bbb);
525 if (b=='B') strcpy(buffer->B,bbb);
526 if (b=='G') strcpy(buffer->G,bbb);
527 if (b=='E') strcpy(buffer->E,bbb);
530 END: if (buffer->I==0L) buffer->I=buffer->T;
534 void ship_to(char *filenm, char *sysnm) /* send spool file filenm to system sysnm */
543 fprintf(stdout,"netproc: shipping %s to %s\n",filenm,sysnm);
545 sprintf(sysflnm,"./network/systems/%s",sysnm);
546 sysflfd=fopen(sysflnm,"r");
547 if (sysflfd==NULL) fprintf(stdout,"netproc: cannot open %s\n",sysflnm);
548 fgets(commbuf1,99,sysflfd);
549 commbuf1[strlen(commbuf1)-1] = 0;
551 sprintf(commbuf2,commbuf1,filenm);
556 * proc_file_transfer() - handle a simple file transfer packet
558 * FIX This shouldn't be like this. What it needs to do is begin an upload
559 * and transmit the file to the server.
561 void proc_file_transfer(char *tname)
562 { /* name of temp file containing the whole message */
568 printf("netproc: processing network file transfer...\n");
570 tfp=fopen(tname,"rb");
571 if (tfp==NULL) printf("netproc: cannot open %s\n",tname);
572 getc(tfp); getc(tfp); getc(tfp);
578 strcpy(dest_room, buf);
581 } while ((a!='M')&&(a>=0));
584 printf("netproc: no message text for file transfer\n");
588 sprintf(buf,"cd %s/files/%s; exec %s",bbs_home_directory,config.c_bucket_dir,UUDECODE);
589 uud=(FILE *)popen(buf,"w");
591 printf("netproc: cannot open uudecode pipe\n");
597 buf[strlen(buf)-1] = 0;
598 for (a=0; a<strlen(buf); ++a) if (buf[a]=='/') buf[a]='_';
599 fprintf(uud,"%s\n",buf);
600 printf("netproc: %s\n",buf);
601 while(a=getc(tfp), a>0) putc(a,uud);
608 /* send a bounce message */
609 void bounce(struct minfo *bminfo)
617 sprintf(bfilename,"./network/spoolin/bounce.%d.%d",getpid(),bseq++);
618 bounce = fopen(bfilename,"wb");
621 fprintf(bounce,"%c%c%c",0xFF,MES_NORMAL,0);
622 fprintf(bounce,"Ppostmaster%c",0);
623 fprintf(bounce,"T%ld%c",now,0);
624 fprintf(bounce,"APostmaster%c",0);
625 fprintf(bounce,"OMail%c",0);
626 fprintf(bounce,"N%s%c",config.c_nodename,0);
627 fprintf(bounce,"H%s%c",config.c_humannode,0);
629 if (strlen(bminfo->E) > 0) {
630 fprintf(bounce,"R%s%c",bminfo->E,0);
633 fprintf(bounce,"R%s%c",bminfo->A,0);
636 fprintf(bounce,"D%s%c",bminfo->N,0);
637 fprintf(bounce,"M%s could not deliver your mail to:\n",
639 fprintf(bounce," \n %s\n \n",bminfo->R);
640 fprintf(bounce," because there is no such user on this system.\n");
641 fprintf(bounce," (Unsent message does *not* follow. ");
642 fprintf(bounce,"Help to conserve bandwidth.)\n%c",0);
649 * process incoming files in ./network/spoolin
651 void inprocess(void) {
652 FILE *fp,*message,*testfp,*ls;
653 static struct minfo minfo;
654 struct recentmsg recentmsg;
655 char tname[128],aaa[1024],iname[256],sfilename[256],pfilename[256];
658 struct syslist *stemp;
665 sprintf(tname,"/tmp/net.t%d",getpid()); /* temp file name */
666 sprintf(iname,"/tmp/net.i%d",getpid()); /* temp file name */
670 chdir(bbs_home_directory);
672 /* Let the shell do the dirty work. Get all data from spoolin */
674 sprintf(aaa,"cd %s/network/spoolin; ls",bbs_home_directory);
677 fprintf(stderr,"netproc: could not open dir cmd: %s\n",
682 ptr=fgets(sfilename,256,ls);
683 if (ptr!=NULL) sfilename[strlen(sfilename)-1] = 0;
684 } while( (ptr!=NULL)&&((!strcmp(sfilename,"."))
685 ||(!strcmp(sfilename,".."))));
686 if (ptr!=NULL) printf("netproc: processing %s\n",sfilename);
691 sprintf(pfilename,"%s/network/spoolin/%s",bbs_home_directory,sfilename);
692 fprintf(stderr,"netproc: processing <%s>\n", pfilename);
695 fp = fopen(pfilename, "rb");
697 fprintf(stderr, "netproc: cannot open <%s>: %s\n",
698 pfilename, strerror(errno));
700 fp = fopen("/dev/null" ,"rb");
703 NXMSG: /* Seek to the beginning of the next message */
706 } while((a!=255)&&(a>=0));
707 if (a<0) goto ENDSTR;
709 message = fopen(tname,"wb"); /* This crates the temporary file. */
710 if (message == NULL) {
711 fprintf(stderr, "Error creating %s: %s\n",
712 tname, strerror(errno));
715 putc(255,message); /* 0xFF (start-of-message) */
716 a = getc(fp); putc(a, message); /* type */
717 a = getc(fp); putc(a, message); /* mode */
719 FieldID = getc(fp); /* Header field ID */
720 putc(FieldID, message);
725 } while ((FieldID != 'M') && (a >= 0)); /* M is always last */
727 msglen = ftell(message);
730 /* process the individual mesage */
736 msgfind(tname,&minfo);
737 strncpy(recentmsg.RMnodename,minfo.N,9);
738 recentmsg.RMnodename[9]=0;
739 recentmsg.RMnum=minfo.I;
740 printf("netproc: #%ld fm <%s> in <%s> @ <%s>\n",
741 minfo.I,minfo.A,minfo.O,minfo.N);
742 if (strlen(minfo.R)>0) {
743 printf(" to <%s>",minfo.R);
744 if (strlen(minfo.D)>0) {
745 printf(" @ <%s>",minfo.D);
750 if (!strcasecmp(minfo.D,FQDN)) strcpy(minfo.D,NODENAME);
752 /* this routine updates our info on the system that sent the message */
753 stemp = get_sys_ptr(minfo.N);
754 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
755 /* add non-neighbor system to map */
756 printf("Adding non-neighbor system <%s> to map\n", slist->s_name);
757 stemp = (struct syslist *)malloc((long)sizeof(struct syslist));
760 strcpy(slist->s_name,minfo.N);
761 strcpy(slist->s_type,"use");
762 strcpy(slist->s_nexthop,minfo.nexthop);
763 time(&slist->s_lastcontact);
765 else if ((stemp == NULL) && (!strcasecmp(minfo.N,minfo.nexthop))) {
766 /* add neighbor system to map */
767 printf("Adding neighbor system <%s> to map\n", slist->s_name);
768 sprintf(aaa,"%s/network/systems/%s",bbs_home_directory,minfo.N);
769 testfp=fopen(aaa,"r");
772 stemp = (struct syslist *)
773 malloc((long)sizeof(struct syslist));
776 strcpy(slist->s_name,minfo.N);
777 strcpy(slist->s_type,"bin");
778 strcpy(slist->s_nexthop,"Mail");
779 time(&slist->s_lastcontact);
782 /* now update last contact and long node name if we can */
784 time(&stemp->s_lastcontact);
785 if (strlen(minfo.H) > 0) strcpy(stemp->s_humannode,minfo.H);
786 if (strlen(minfo.B) > 0) strcpy(stemp->s_phonenum,minfo.B);
787 if (strlen(minfo.G) > 0) strcpy(stemp->s_gdom,minfo.G);
790 /* route the message if necessary */
791 if ((strcasecmp(minfo.D,NODENAME))&&(minfo.D[0]!=0)) {
792 a = get_sysinfo_type(minfo.D);
793 printf("netproc: routing message to system <%s>\n",minfo.D);
797 printf("netproc: netmailer %s\n", tname);
799 execlp("./netmailer","netmailer",
801 printf("netproc: error running netmailer: %s\n",
806 else while (wait(&b)!=(-1));
808 else if (a==M_BINARY) {
809 ship_to(tname,minfo.D);
812 /* message falls into the bit bucket? */
816 /* check to see if it's a file transfer */
817 else if (!strncasecmp(minfo.S,"FILE",4)) {
818 proc_file_transfer(tname);
821 /* otherwise process it as a normal message */
824 if (!strcasecmp(minfo.R, "postmaster")) {
826 strcpy(minfo.C, "Aide");
829 if (strlen(minfo.R) > 0) {
830 sprintf(buf,"GOTO _MAIL_");
832 if (is_banned(minfo.A,minfo.C,minfo.N)) {
833 sprintf(buf,"GOTO %s", FILTERROOM);
836 if (strlen(minfo.C) > 0) {
837 sprintf(buf,"GOTO %s", minfo.C);
840 sprintf(buf,"GOTO %s", minfo.O);
846 puts(buf); fflush(stdout);
847 sprintf(buf,"GOTO _BITBUCKET_");
852 /* Open the temporary file containing the message */
853 message = fopen(tname, "rb");
854 if (message == NULL) {
855 fprintf(stderr, "netproc: cannot open %s: %s\n",
856 tname, strerror(errno));
861 /* Transmit the message to the server */
862 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
863 printf("< %s\n", buf);
866 printf("> %s\n", buf);
867 if (!strncmp(buf, "570", 3)) {
868 /* no such user, do a bounce */
872 /* Always use the server's idea of the message length,
873 * even though they should both be identical */
874 msglen = atol(&buf[4]);
875 while (msglen > 0L) {
876 bloklen = ((msglen >= 255L) ? 255 : ((int)msglen));
877 if (fread(buf, bloklen, 1, message) < 1) {
878 fprintf(stderr, "netproc: error trying to read %d bytes: %s\n",
879 bloklen, strerror(errno));
882 serv_write(buf, bloklen);
883 msglen = msglen - (long)bloklen;
907 int checkpath(char *path, char *sys) /* Checks to see whether its ok to send */
908 /* Returns 1 for ok, send message */
909 { /* Returns 0 if message already there */
916 printf("netproc: checkpath <%s> <%s> ... ",path,sys);
918 for (a=0; a<strlen(path); ++a) {
919 if (!strncmp(&path[a],sys2,strlen(sys2))) return(0);
925 * implement split horizon algorithm
927 int ismsgok(long int mpos, FILE *mmfp, char *sysname)
930 int ok = 0; /* fail safe - no path, don't send it */
934 if (getc(mmfp)!=255) return(0);
935 getc(mmfp); getc(mmfp);
937 while (a=getc(mmfp),((a!='M')&&(a!=0))) {
938 fpgetfield(mmfp,fbuf);
940 ok = checkpath(fbuf,sysname);
944 printf("%s\n", ((ok)?"SEND":"(no)") );
949 int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list of messages to a file */
950 /* returns # of msgs spooled */
954 struct msglist *cmptr;
959 int msgs_spooled = 0;
967 sprintf(mmtemp, "/tmp/net.m%d", getpid());
969 /* for each message in the list... */
970 for (cmptr=cmlist; cmptr!=NULL; cmptr=cmptr->next) {
972 /* make sure we're in the correct room... */
973 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
974 sprintf(buf, "GOTO %s", cmptr->m_rmname);
978 strcpy(curr_rm, cmptr->m_rmname);
981 fprintf(stderr,"%s\n", buf);
985 /* download the message from the server... */
986 mmfp = fopen(mmtemp, "wb");
987 sprintf(buf, "MSG3 %ld", cmptr->m_num);
990 if (buf[0]=='6') { /* read the msg */
991 msg_len = atol(&buf[4]);
992 while (msg_len > 0L) {
993 blok_len = ((msg_len >= 256L) ? 256 : (int)msg_len);
994 serv_read(buf, blok_len);
995 fwrite(buf, blok_len, 1, mmfp);
996 msg_len = msg_len - (long)blok_len;
999 else { /* or print the err */
1000 fprintf(stderr, "%s\n", buf);
1004 mmfp = fopen(mmtemp,"rb");
1006 if (ismsgok(0L,mmfp,sysname)) {
1010 fread(fbuf,3,1,mmfp);
1011 fwrite(fbuf,3,1,destfp);
1012 while (a=getc(mmfp),((a!=0)&&(a!='M'))) {
1013 if (a!='C') putc(a,destfp);
1014 fpgetfield(mmfp,fbuf);
1015 if (a=='P') fprintf(destfp,"%s!",NODENAME);
1017 fwrite(fbuf,strlen(fbuf)+1,1,destfp);
1020 fprintf(destfp, "C%s%c",
1021 cmptr->m_rmname, 0);
1033 return(msgs_spooled);
1036 void outprocess(char *sysname) /* send new room messages to sysname */
1040 char shiptocmd[128];
1044 struct msglist *cmlist = NULL;
1045 struct rmlist *crmlist = NULL;
1046 struct rmlist *rmptr,*rmptr2;
1047 struct msglist *cmptr,*cmptr2;
1048 FILE *sysflfp,*tempflfp;
1052 sprintf(tempflnm,"/tmp/%s.%d",NODENAME,getpid());
1053 tempflfp=fopen(tempflnm,"w");
1054 if (tempflfp==NULL) return;
1058 * Read system file for node in question and put together room list
1060 sprintf(sysflnm,"%s/network/systems/%s",bbs_home_directory,sysname);
1061 sysflfp=fopen(sysflnm,"r");
1062 if (sysflfp==NULL) return;
1063 fgets(shiptocmd,128,sysflfp); shiptocmd[strlen(shiptocmd)-1]=0;
1064 while(!feof(sysflfp)) {
1065 if (fgets(srmname,32,sysflfp)==NULL) break;
1066 srmname[strlen(srmname)-1]=0;
1067 fgets(lbuf,32,sysflfp);
1068 rmptr=(struct rmlist *)malloc(sizeof(struct rmlist));
1070 strcpy(rmptr->rm_name,srmname);
1071 strip_trailing_whitespace(rmptr->rm_name);
1072 rmptr->rm_lastsent = atol(lbuf);
1073 if (crmlist==NULL) crmlist=rmptr;
1074 else if (!strcasecmp(rmptr->rm_name,"control")) {
1075 /* control has to be first in room list */
1076 rmptr->next = crmlist;
1081 while (rmptr2->next != NULL) rmptr2=rmptr2->next;
1088 * Assemble list of messages to be spooled
1090 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next) {
1092 sprintf(buf,"GOTO %s",rmptr->rm_name);
1096 fprintf(stderr, "%s\n", buf);
1099 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1102 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf,"000")) {
1103 thismsg = atol(buf);
1104 if ( thismsg > (rmptr->rm_lastsent) ) {
1105 rmptr->rm_lastsent = thismsg;
1107 cmptr=(struct msglist *)
1108 malloc(sizeof(struct msglist));
1110 cmptr->m_num = thismsg;
1111 strcpy(cmptr->m_rmname, rmptr->rm_name);
1113 if (cmlist == NULL) cmlist = cmptr;
1116 while (cmptr2->next != NULL)
1117 cmptr2 = cmptr2->next;
1118 cmptr2->next = cmptr;
1122 else { /* print error from "msgs all" */
1123 fprintf(stderr, "%s\n", buf);
1128 outgoing_msgs=0; cmptr2=cmlist; /* this loop counts the messages */
1129 while (cmptr2!=NULL) {
1131 cmptr2 = cmptr2->next;
1133 printf("netproc: %d messages to be spooled to %s\n",
1134 outgoing_msgs,sysname);
1138 * Spool out the messages, but only if there are any.
1141 if (outgoing_msgs!=0) outgoing_msgs=spool_out(cmlist,tempflfp,sysname);
1142 printf("netproc: %d messages actually spooled\n",
1147 * Deallocate list of spooled messages.
1149 while(cmlist!=NULL) {
1156 * Rewrite system file and deallocate room list.
1158 printf("Spooling...\n");
1160 sysflfp=fopen(sysflnm,"w");
1161 fprintf(sysflfp,"%s\n",shiptocmd);
1162 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next)
1163 fprintf(sysflfp,"%s\n%ld\n",rmptr->rm_name,rmptr->rm_lastsent);
1165 while(crmlist!=NULL) {
1166 rmptr=crmlist->next;
1172 * Close temporary file, ship it out, and return
1175 if (outgoing_msgs!=0) ship_to(tempflnm,sysname);
1181 * Connect netproc to the Citadel server running on this computer.
1183 void np_attach_to_server(void) {
1186 char *args[] = { "netproc", "localhost", NULL, NULL } ;
1188 printf("Attaching to server...\n");
1189 sprintf(portname, "%d", config.c_port_number);
1191 attach_to_server(3, args);
1193 printf("%s\n",&buf[4]);
1194 sprintf(buf,"IPGM %d", config.c_ipgm_secret);
1197 printf("%s\n",&buf[4]);
1208 int main(int argc, char **argv)
1215 strcpy(bbs_home_directory, BBSDIR);
1218 * Change directories if specified
1220 if (argv > 0) for (a=1; a<argc; ++a) {
1221 if (!strncmp(argv[a], "-h", 2)) {
1222 strcpy(bbs_home_directory, argv[a]);
1223 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1227 fprintf(stderr, "netproc: usage: netproc [-hHomeDir]\n");
1234 /* write all messages to the log from this point onward */
1235 freopen(NPLOGFILE,NPLOGMODE,stdout);
1236 freopen(NPLOGFILE,NPLOGMODE,stderr);
1238 if (set_lockfile()!=0) {
1239 fprintf(stderr,"netproc: lock file exists: already running\n");
1243 signal(SIGINT,cleanup);
1244 signal(SIGQUIT,cleanup);
1245 signal(SIGHUP,cleanup);
1246 signal(SIGTERM,cleanup);
1248 printf("netproc: started. pid=%d\n",getpid());
1250 np_attach_to_server();
1253 if (load_syslist()!=0) fprintf(stdout,"netproc: cannot load sysinfo\n");
1254 setup_special_nodes();
1256 inprocess(); /* first collect incoming stuff */
1258 allfp=(FILE *)popen("cd ./network/systems; ls","r");
1260 while (fgets(allst,32,allfp)!=NULL) {
1261 allst[strlen(allst)-1] = 0;
1267 inprocess(); /* incoming again in case anything new was generated */
1269 printf("netproc: processing ended.\n");