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 /* How long it takes for an old node to drop off the network map */
9 #define EXPIRY_TIME (2592000L)
11 /* Where do we keep our lock file? */
12 #define LOCKFILE "/var/lock/LCK.netproc"
14 /* Path to the 'uudecode' utility (needed for network file transfers) */
15 #define UUDECODE "/usr/bin/uudecode"
17 /* Uncomment the DEBUG def to see noisy traces */
24 #include <sys/types.h>
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.
44 #define FILTERROOM TWITROOM
50 char m_rmname[ROOMNAMELEN];
55 char rm_name[ROOMNAMELEN];
60 struct filterlist *next;
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);
83 struct filterlist *filter = NULL;
84 struct syslist *slist = NULL;
87 extern char bbs_home_directory[];
88 extern int home_specified;
93 * replacement strerror() for systems that don't have it
99 sprintf(buf,"errno = %d",e);
105 void strip_trailing_whitespace(char *buf)
107 while(isspace(buf[strlen(buf)-1]))
108 buf[strlen(buf)-1]=0;
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.
117 int load_syslist(void) {
119 struct syslist *stemp;
123 fp=fopen("network/mail.sysinfo","rb");
124 if (fp==NULL) return(1);
127 if (fgets(buf,128,fp)==NULL) {
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) ) {
136 stemp =(struct syslist *)malloc(sizeof(struct syslist));
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,"");
147 else if ( (insys==1) && (strlen(buf)==0) ) {
150 else if ( (insys==1) && (!strncmp(buf,"bin",3)) ) {
151 strcpy(slist->s_type,"bin");
152 strcpy(slist->s_nexthop,&buf[4]);
154 else if ( (insys==1) && (!strncmp(buf,"use",3)) ) {
155 strcpy(slist->s_type,"use");
156 strcpy(slist->s_nexthop,&buf[4]);
158 else if ( (insys==1) && (!strncmp(buf,"uum",3)) ) {
159 strcpy(slist->s_type,"uum");
160 strcpy(slist->s_nexthop,&buf[4]);
162 else if ( (insys==1) && (!strncmp(buf,"lastcontact",11)) ) {
163 sscanf(&buf[12],"%ld",&slist->s_lastcontact);
165 else if ( (insys==1) && (!strncmp(buf,"humannode",9)) ) {
166 strcpy(slist->s_humannode,&buf[10]);
168 else if ( (insys==1) && (!strncmp(buf,"phonenum",8)) ) {
169 strcpy(slist->s_phonenum,&buf[9]);
171 else if ( (insys==1) && (!strncmp(buf,"gdom",4)) ) {
172 strcpy(slist->s_gdom,&buf[5]);
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
180 void setup_special_nodes(void) {
181 struct syslist *stemp,*slocal;
184 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
185 if (!strcasecmp(stemp->s_name,config.c_nodename)) slocal=stemp;
188 slocal =(struct syslist *)malloc(sizeof(struct syslist));
189 slocal->next = slist;
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);
200 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
201 if (!strcasecmp(stemp->s_name,"internet")) slocal=stemp;
204 slocal =(struct syslist *)malloc(sizeof(struct syslist));
205 slocal->next = slist;
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,"");
219 * here's the routine to write the table back to disk.
221 void rewrite_syslist(void) {
222 struct syslist *stemp;
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);
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)));
254 /* now free the list */
255 while (slist!=NULL) {
263 /* call this function with the node name of a system and it returns a pointer
264 * to its syslist structure.
266 struct syslist *get_sys_ptr(char *sysname)
268 static char sysnambuf[16];
269 static struct syslist *sysptrbuf = NULL;
270 struct syslist *stemp;
272 if ( (!strcmp(sysname,sysnambuf))
273 && (sysptrbuf!=NULL) ) return(sysptrbuf);
275 strcpy(sysnambuf,sysname);
276 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
277 if (!strcmp(sysname,stemp->s_name)) {
288 * make sure only one copy of netproc runs at a time, using lock files
290 int set_lockfile(void) {
294 if ((lfp = fopen(LOCKFILE,"r")) != NULL) {
295 fscanf(lfp,"%d",&onppid);
297 if (!kill(onppid, 0) || errno == EPERM) return 1;
300 lfp=fopen(LOCKFILE,"w");
301 fprintf(lfp,"%d\n",getpid());
306 void remove_lockfile(void) {
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.
317 void nq_cleanup(int e)
326 static int nested = 0;
329 signal(SIGALRM,nq_cleanup);
330 if (nested++ < 1) serv_puts("QUIT");
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.
345 * If there is a kill file in place, this function will process it.
347 void load_filterlist(void) {
349 struct filterlist *fbuf;
352 fp=fopen("./network/filterlist","r");
353 if (fp==NULL) return;
354 while (fgets(sbuf,256,fp)!=NULL) {
356 sbuf[strlen(sbuf)-1]=0;
357 fbuf=(struct filterlist *)
358 malloc((long)sizeof(struct filterlist));
361 strcpy(fbuf->f_person,"*");
362 strcpy(fbuf->f_room,"*");
363 strcpy(fbuf->f_system,"*");
365 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
368 strcpy(fbuf->f_person,sbuf);
369 strcpy(sbuf,&sbuf[p+1]);
371 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
374 strcpy(fbuf->f_room,sbuf);
375 strcpy(sbuf,&sbuf[p+1]);
377 strcpy(fbuf->f_system,sbuf);
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)
386 struct filterlist *fptr;
388 for (fptr=filter; fptr!=NULL; fptr=fptr->next) if (
389 ((!strcasecmp(fptr->f_person,k_person))||(!strcmp(fptr->f_person,"*")))
391 ((!strcasecmp(fptr->f_room,k_room))||(!strcmp(fptr->f_room,"*")))
393 ((!strcasecmp(fptr->f_system,k_system))||(!strcmp(fptr->f_system,"*")))
399 int get_sysinfo_type(char *name) /* determine routing from sysinfo file */
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);
408 if (!strcasecmp(stemp->s_type,"bin")) {
411 if (!strcasecmp(stemp->s_type,"uum")) {
416 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
421 void fpgetfield(FILE *fp, char *string)
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).
444 void msgfind(char *msgfile, struct minfo *buffer)
452 fp=fopen(msgfile,"rb");
454 syslog(LOG_ERR, "can't open message file: %s",strerror(errno));
459 syslog(LOG_ERR, "incorrect message format");
462 mtype=getc(fp); aflag=getc(fp);
471 BONFGM: b=getc(fp); if (b<0) goto END;
472 if (b=='M') goto END;
474 while ((bbb[0]==' ')&&(strlen(bbb)>1)) strcpy(bbb,&bbb[1]);
476 strcpy(buffer->A,bbb);
477 if (strlen(userid)==0) {
479 for (e=0; e<strlen(userid); ++e)
480 if (userid[e]==' ') userid[e]='_';
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);
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]);
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);
497 for (e=0; e<strlen(bbb); ++e) if (bbb[e]=='_') bbb[e]=' ';
498 strcpy(buffer->R,bbb);
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);
509 END: if (buffer->I==0L) buffer->I=buffer->T;
513 void ship_to(char *filenm, char *sysnm) /* send spool file filenm to system sysnm */
522 syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
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;
530 sprintf(commbuf2,commbuf1,filenm);
535 * proc_file_transfer() - handle a simple file transfer packet
538 void proc_file_transfer(char *tname)
539 { /* name of temp file containing the whole message */
541 char dest_room[ROOMNAMELEN];
542 char subdir_name[256];
546 syslog(LOG_NOTICE, "processing network file transfer...");
548 tfp=fopen(tname,"rb");
549 if (tfp==NULL) syslog(LOG_ERR, "cannot open %s", tname);
550 getc(tfp); getc(tfp); getc(tfp);
556 strcpy(dest_room, buf);
559 } while ((a!='M')&&(a>=0));
562 syslog(LOG_ERR, "no message text for file transfer");
566 strcpy(subdir_name, "---xxx---");
567 sprintf(buf, "GOTO %s", dest_room);
571 extract(subdir_name, &buf[4], 2);
572 if (strlen(subdir_name) == 0) strcpy(subdir_name, "--xxx--");
575 /* Change to the room's directory; if that fails, change to the
576 * bitbucket directory. Then run uudecode.
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,
583 uud=(FILE *)popen(buf,"w");
585 syslog(LOG_ERR, "cannot open uudecode pipe");
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);
602 /* send a bounce message */
603 void bounce(struct minfo *bminfo)
611 sprintf(bfilename,"./network/spoolin/bounce.%d.%d",getpid(),bseq++);
612 bounce = fopen(bfilename,"wb");
615 fprintf(bounce,"%c%c%c",0xFF,MES_NORMAL,0);
616 fprintf(bounce,"Ppostmaster%c",0);
617 fprintf(bounce,"T%ld%c",now,0);
618 fprintf(bounce,"APostmaster%c",0);
619 fprintf(bounce,"OMail%c",0);
620 fprintf(bounce,"N%s%c",config.c_nodename,0);
621 fprintf(bounce,"H%s%c",config.c_humannode,0);
623 if (strlen(bminfo->E) > 0) {
624 fprintf(bounce,"R%s%c",bminfo->E,0);
627 fprintf(bounce,"R%s%c",bminfo->A,0);
630 fprintf(bounce,"D%s%c",bminfo->N,0);
631 fprintf(bounce,"M%s could not deliver your mail to:\n",
633 fprintf(bounce," \n %s\n \n",bminfo->R);
634 fprintf(bounce," because there is no such user on this system.\n");
635 fprintf(bounce," (Unsent message does *not* follow. ");
636 fprintf(bounce,"Help to conserve bandwidth.)\n%c",0);
643 * process incoming files in ./network/spoolin
645 void inprocess(void) {
646 FILE *fp,*message,*testfp,*ls;
647 static struct minfo minfo;
648 struct recentmsg recentmsg;
649 char tname[128],aaa[1024],iname[256],sfilename[256],pfilename[256];
652 struct syslist *stemp;
659 sprintf(tname,"/tmp/net.t%d",getpid()); /* temp file name */
660 sprintf(iname,"/tmp/net.i%d",getpid()); /* temp file name */
664 chdir(bbs_home_directory);
666 /* Let the shell do the dirty work. Get all data from spoolin */
668 sprintf(aaa,"cd %s/network/spoolin; ls",bbs_home_directory);
671 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
675 ptr=fgets(sfilename,256,ls);
676 if (ptr!=NULL) sfilename[strlen(sfilename)-1] = 0;
677 } while( (ptr!=NULL)&&((!strcmp(sfilename,"."))
678 ||(!strcmp(sfilename,".."))));
679 if (ptr!=NULL) syslog(LOG_NOTICE, "processing %s", sfilename);
684 sprintf(pfilename,"%s/network/spoolin/%s",bbs_home_directory,sfilename);
685 syslog(LOG_NOTICE, "processing <%s>", pfilename);
687 fp = fopen(pfilename, "rb");
689 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
690 fp = fopen("/dev/null" ,"rb");
693 NXMSG: /* Seek to the beginning of the next message */
696 } while((a!=255)&&(a>=0));
697 if (a<0) goto ENDSTR;
699 message = fopen(tname,"wb"); /* This crates the temporary file. */
700 if (message == NULL) {
701 syslog(LOG_ERR, "error creating %s: %s", tname,strerror(errno));
704 putc(255,message); /* 0xFF (start-of-message) */
705 a = getc(fp); putc(a, message); /* type */
706 a = getc(fp); putc(a, message); /* mode */
708 FieldID = getc(fp); /* Header field ID */
709 putc(FieldID, message);
714 } while ((FieldID != 'M') && (a >= 0)); /* M is always last */
716 msglen = ftell(message);
719 /* process the individual mesage */
725 msgfind(tname,&minfo);
726 strncpy(recentmsg.RMnodename,minfo.N,9);
727 recentmsg.RMnodename[9]=0;
728 recentmsg.RMnum=minfo.I;
729 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
730 minfo.I,minfo.A,minfo.O,minfo.N);
731 if (strlen(minfo.R)>0) {
732 syslog(LOG_NOTICE, " to <%s>",minfo.R);
733 if (strlen(minfo.D)>0) {
734 syslog(LOG_NOTICE, " @ <%s>",minfo.D);
737 if (!strcasecmp(minfo.D,FQDN)) strcpy(minfo.D,NODENAME);
739 /* this routine updates our info on the system that sent the message */
740 stemp = get_sys_ptr(minfo.N);
741 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
742 /* add non-neighbor system to map */
743 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
745 stemp = (struct syslist *)malloc((long)sizeof(struct syslist));
748 strcpy(slist->s_name,minfo.N);
749 strcpy(slist->s_type,"use");
750 strcpy(slist->s_nexthop,minfo.nexthop);
751 time(&slist->s_lastcontact);
753 else if ((stemp == NULL) && (!strcasecmp(minfo.N,minfo.nexthop))) {
754 /* add neighbor system to map */
755 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
757 sprintf(aaa,"%s/network/systems/%s",bbs_home_directory,minfo.N);
758 testfp=fopen(aaa,"r");
761 stemp = (struct syslist *)
762 malloc((long)sizeof(struct syslist));
765 strcpy(slist->s_name,minfo.N);
766 strcpy(slist->s_type,"bin");
767 strcpy(slist->s_nexthop,"Mail");
768 time(&slist->s_lastcontact);
771 /* now update last contact and long node name if we can */
773 time(&stemp->s_lastcontact);
774 if (strlen(minfo.H) > 0) strcpy(stemp->s_humannode,minfo.H);
775 if (strlen(minfo.B) > 0) strcpy(stemp->s_phonenum,minfo.B);
776 if (strlen(minfo.G) > 0) strcpy(stemp->s_gdom,minfo.G);
779 /* route the message if necessary */
780 if ((strcasecmp(minfo.D,NODENAME))&&(minfo.D[0]!=0)) {
781 a = get_sysinfo_type(minfo.D);
782 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
786 syslog(LOG_NOTICE, "netmailer %s", tname);
788 execlp("./netmailer","netmailer",
790 syslog(LOG_ERR, "error running netmailer: %s",
794 else while (wait(&b)!=(-1));
796 else if (a==M_BINARY) {
797 ship_to(tname,minfo.D);
800 /* message falls into the bit bucket? */
804 /* check to see if it's a file transfer */
805 else if (!strncasecmp(minfo.S,"FILE",4)) {
806 proc_file_transfer(tname);
809 /* otherwise process it as a normal message */
812 if (!strcasecmp(minfo.R, "postmaster")) {
814 strcpy(minfo.C, "Aide");
817 if (strlen(minfo.R) > 0) {
818 sprintf(buf,"GOTO _MAIL_");
820 if (is_banned(minfo.A,minfo.C,minfo.N)) {
821 sprintf(buf,"GOTO %s", FILTERROOM);
824 if (strlen(minfo.C) > 0) {
825 sprintf(buf,"GOTO %s", minfo.C);
828 sprintf(buf,"GOTO %s", minfo.O);
834 syslog(LOG_ERR, "%s", buf);
835 sprintf(buf,"GOTO _BITBUCKET_");
840 /* Open the temporary file containing the message */
841 message = fopen(tname, "rb");
842 if (message == NULL) {
843 syslog(LOG_ERR, "cannot open %s: %s",
844 tname, strerror(errno));
849 /* Transmit the message to the server */
850 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
853 if (!strncmp(buf, "570", 3)) {
854 /* no such user, do a bounce */
858 /* Always use the server's idea of the message length,
859 * even though they should both be identical */
860 msglen = atol(&buf[4]);
861 while (msglen > 0L) {
862 bloklen = ((msglen >= 255L) ? 255 : ((int)msglen));
863 if (fread(buf, bloklen, 1, message) < 1) {
865 "error trying to read %d bytes: %s",
866 bloklen, strerror(errno));
868 serv_write(buf, bloklen);
869 msglen = msglen - (long)bloklen;
875 syslog(LOG_ERR, "%s", buf);
892 int checkpath(char *path, char *sys) /* Checks to see whether its ok to send */
893 /* Returns 1 for ok, send message */
894 { /* Returns 0 if message already there */
901 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
903 for (a=0; a<strlen(path); ++a) {
904 if (!strncmp(&path[a],sys2,strlen(sys2))) return(0);
910 * implement split horizon algorithm
912 int ismsgok(long int mpos, FILE *mmfp, char *sysname)
915 int ok = 0; /* fail safe - no path, don't send it */
919 if (getc(mmfp)!=255) return(0);
920 getc(mmfp); getc(mmfp);
922 while (a=getc(mmfp),((a!='M')&&(a!=0))) {
923 fpgetfield(mmfp,fbuf);
925 ok = checkpath(fbuf,sysname);
929 syslog(LOG_NOTICE, "%s", ((ok)?"SEND":"(no)") );
934 int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list of messages to a file */
935 /* returns # of msgs spooled */
939 struct msglist *cmptr;
944 int msgs_spooled = 0;
952 sprintf(mmtemp, "/tmp/net.m%d", getpid());
954 /* for each message in the list... */
955 for (cmptr=cmlist; cmptr!=NULL; cmptr=cmptr->next) {
957 /* make sure we're in the correct room... */
958 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
959 sprintf(buf, "GOTO %s", cmptr->m_rmname);
963 strcpy(curr_rm, cmptr->m_rmname);
966 syslog(LOG_ERR, "%s", buf);
970 /* download the message from the server... */
971 mmfp = fopen(mmtemp, "wb");
972 sprintf(buf, "MSG3 %ld", cmptr->m_num);
975 if (buf[0]=='6') { /* read the msg */
976 msg_len = atol(&buf[4]);
977 while (msg_len > 0L) {
978 blok_len = ((msg_len >= 256L) ? 256 : (int)msg_len);
979 serv_read(buf, blok_len);
980 fwrite(buf, blok_len, 1, mmfp);
981 msg_len = msg_len - (long)blok_len;
984 else { /* or print the err */
985 syslog(LOG_ERR, "%s", buf);
989 mmfp = fopen(mmtemp,"rb");
991 if (ismsgok(0L,mmfp,sysname)) {
995 fread(fbuf,3,1,mmfp);
996 fwrite(fbuf,3,1,destfp);
997 while (a=getc(mmfp),((a!=0)&&(a!='M'))) {
998 if (a!='C') putc(a,destfp);
999 fpgetfield(mmfp,fbuf);
1000 if (a=='P') fprintf(destfp,"%s!",NODENAME);
1002 fwrite(fbuf,strlen(fbuf)+1,1,destfp);
1005 fprintf(destfp, "C%s%c",
1006 cmptr->m_rmname, 0);
1018 return(msgs_spooled);
1021 void outprocess(char *sysname) /* send new room messages to sysname */
1025 char shiptocmd[128];
1029 struct msglist *cmlist = NULL;
1030 struct rmlist *crmlist = NULL;
1031 struct rmlist *rmptr,*rmptr2;
1032 struct msglist *cmptr,*cmptr2;
1033 FILE *sysflfp,*tempflfp;
1037 sprintf(tempflnm,"/tmp/%s.%d",NODENAME,getpid());
1038 tempflfp=fopen(tempflnm,"w");
1039 if (tempflfp==NULL) return;
1043 * Read system file for node in question and put together room list
1045 sprintf(sysflnm,"%s/network/systems/%s",bbs_home_directory,sysname);
1046 sysflfp=fopen(sysflnm,"r");
1047 if (sysflfp==NULL) return;
1048 fgets(shiptocmd,128,sysflfp); shiptocmd[strlen(shiptocmd)-1]=0;
1049 while(!feof(sysflfp)) {
1050 if (fgets(srmname,32,sysflfp)==NULL) break;
1051 srmname[strlen(srmname)-1]=0;
1052 fgets(lbuf,32,sysflfp);
1053 rmptr=(struct rmlist *)malloc(sizeof(struct rmlist));
1055 strcpy(rmptr->rm_name,srmname);
1056 strip_trailing_whitespace(rmptr->rm_name);
1057 rmptr->rm_lastsent = atol(lbuf);
1058 if (crmlist==NULL) crmlist=rmptr;
1059 else if (!strcasecmp(rmptr->rm_name,"control")) {
1060 /* control has to be first in room list */
1061 rmptr->next = crmlist;
1066 while (rmptr2->next != NULL) rmptr2=rmptr2->next;
1073 * Assemble list of messages to be spooled
1075 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next) {
1077 sprintf(buf,"GOTO %s",rmptr->rm_name);
1081 syslog(LOG_ERR, "%s", buf);
1084 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1087 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf,"000")) {
1088 thismsg = atol(buf);
1089 if ( thismsg > (rmptr->rm_lastsent) ) {
1090 rmptr->rm_lastsent = thismsg;
1092 cmptr=(struct msglist *)
1093 malloc(sizeof(struct msglist));
1095 cmptr->m_num = thismsg;
1096 strcpy(cmptr->m_rmname, rmptr->rm_name);
1098 if (cmlist == NULL) cmlist = cmptr;
1101 while (cmptr2->next != NULL)
1102 cmptr2 = cmptr2->next;
1103 cmptr2->next = cmptr;
1107 else { /* print error from "msgs all" */
1108 syslog(LOG_ERR, "%s", buf);
1113 outgoing_msgs=0; cmptr2=cmlist; /* this loop counts the messages */
1114 while (cmptr2!=NULL) {
1116 cmptr2 = cmptr2->next;
1118 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1119 outgoing_msgs,sysname);
1122 * Spool out the messages, but only if there are any.
1124 if (outgoing_msgs!=0) outgoing_msgs=spool_out(cmlist,tempflfp,sysname);
1125 syslog(LOG_NOTICE, "%d messages actually spooled",
1129 * Deallocate list of spooled messages.
1131 while(cmlist!=NULL) {
1138 * Rewrite system file and deallocate room list.
1140 syslog(LOG_NOTICE, "Spooling...");
1141 sysflfp=fopen(sysflnm,"w");
1142 fprintf(sysflfp,"%s\n",shiptocmd);
1143 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next)
1144 fprintf(sysflfp,"%s\n%ld\n",rmptr->rm_name,rmptr->rm_lastsent);
1146 while(crmlist!=NULL) {
1147 rmptr=crmlist->next;
1153 * Close temporary file, ship it out, and return
1156 if (outgoing_msgs!=0) ship_to(tempflnm,sysname);
1162 * Connect netproc to the Citadel server running on this computer.
1164 void np_attach_to_server(void) {
1167 char *args[] = { "netproc", "localhost", NULL, NULL } ;
1169 syslog(LOG_NOTICE, "Attaching to server...");
1170 sprintf(portname, "%d", config.c_port_number);
1172 attach_to_server(3, args);
1174 syslog(LOG_NOTICE, "%s", &buf[4]);
1175 sprintf(buf,"IPGM %d", config.c_ipgm_secret);
1178 syslog(LOG_NOTICE, "%s", &buf[4]);
1189 int main(int argc, char **argv)
1194 int import_only = 0; /* if set to 1, don't export anything */
1196 openlog("netproc", LOG_PID, LOG_USER);
1197 strcpy(bbs_home_directory, BBSDIR);
1200 * Change directories if specified
1202 for (a=1; a<argc; ++a) {
1203 if (!strncmp(argv[a], "-h", 2)) {
1204 strcpy(bbs_home_directory, argv[a]);
1205 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1208 else if (!strcmp(argv[a], "-i")) {
1212 fprintf(stderr, "netproc: usage: ");
1213 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1220 if (set_lockfile()!=0) {
1221 syslog(LOG_NOTICE, "lock file exists: already running");
1225 signal(SIGINT,cleanup);
1226 signal(SIGQUIT,cleanup);
1227 signal(SIGHUP,cleanup);
1228 signal(SIGTERM,cleanup);
1230 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1232 np_attach_to_server();
1235 if (load_syslist()!=0) syslog(LOG_ERR, "cannot load sysinfo");
1236 setup_special_nodes();
1238 inprocess(); /* first collect incoming stuff */
1240 if (import_only != 1) {
1241 allfp=(FILE *)popen("cd ./network/systems; ls","r");
1243 while (fgets(allst,32,allfp)!=NULL) {
1244 allst[strlen(allst)-1] = 0;
1249 /* import again in case anything new was generated */
1254 syslog(LOG_NOTICE, "processing ended.");