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", "r");
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)) ) {
164 sscanf(&buf[12],"%ld",&foo);
165 slist->s_lastcontact = foo;
167 else if ( (insys==1) && (!strncmp(buf,"humannode",9)) ) {
168 strcpy(slist->s_humannode,&buf[10]);
170 else if ( (insys==1) && (!strncmp(buf,"phonenum",8)) ) {
171 strcpy(slist->s_phonenum,&buf[9]);
173 else if ( (insys==1) && (!strncmp(buf,"gdom",4)) ) {
174 strcpy(slist->s_gdom,&buf[5]);
179 /* now we have to set up two "special" nodes on the list: one
180 * for the local node, and one for an Internet gateway
182 void setup_special_nodes(void) {
183 struct syslist *stemp,*slocal;
186 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
187 if (!strcasecmp(stemp->s_name,config.c_nodename)) slocal=stemp;
190 slocal =(struct syslist *)malloc(sizeof(struct syslist));
191 slocal->next = slist;
194 strcpy(slocal->s_name,config.c_nodename);
195 strcpy(slocal->s_type,"bin");
196 strcpy(slocal->s_nexthop,"Mail");
197 time(&slocal->s_lastcontact);
198 strcpy(slocal->s_humannode,config.c_humannode);
199 strcpy(slocal->s_phonenum,config.c_phonenum);
202 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
203 if (!strcasecmp(stemp->s_name,"internet")) slocal=stemp;
206 slocal =(struct syslist *)malloc(sizeof(struct syslist));
207 slocal->next = slist;
210 strcpy(slocal->s_name,"internet");
211 strcpy(slocal->s_type,"uum");
212 strcpy(slocal->s_nexthop,"%s");
213 time(&slocal->s_lastcontact);
214 strcpy(slocal->s_humannode,"Internet Gateway");
215 strcpy(slocal->s_phonenum,"");
216 strcpy(slocal->s_gdom,"");
221 * here's the routine to write the table back to disk.
223 void rewrite_syslist(void) {
224 struct syslist *stemp;
229 newfp=fopen("network/mail.sysinfo","w");
230 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
231 if (!strcasecmp(stemp->s_name,config.c_nodename)) {
232 time(&stemp->s_lastcontact);
233 strcpy(stemp->s_type,"bin");
234 strcpy(stemp->s_humannode,config.c_humannode);
235 strcpy(stemp->s_phonenum,config.c_phonenum);
237 /* remove systems we haven't heard from in a while */
238 if ( (stemp->s_lastcontact == 0L)
239 || (now - stemp->s_lastcontact < EXPIRY_TIME) ) {
240 fprintf(newfp,"%s\n%s %s\n",
241 stemp->s_name,stemp->s_type,stemp->s_nexthop);
242 if (strlen(stemp->s_phonenum) > 0)
243 fprintf(newfp,"phonenum %s\n",stemp->s_phonenum);
244 if (strlen(stemp->s_gdom) > 0)
245 fprintf(newfp,"gdom %s\n",stemp->s_gdom);
246 if (strlen(stemp->s_humannode) > 0)
247 fprintf(newfp,"humannode %s\n",stemp->s_humannode);
248 if (stemp->s_lastcontact > 0L)
249 fprintf(newfp,"lastcontact %ld %s",
250 (long)stemp->s_lastcontact,
251 asctime(localtime(&stemp->s_lastcontact)));
256 /* now free the list */
257 while (slist!=NULL) {
265 /* call this function with the node name of a system and it returns a pointer
266 * to its syslist structure.
268 struct syslist *get_sys_ptr(char *sysname)
270 static char sysnambuf[16];
271 static struct syslist *sysptrbuf = NULL;
272 struct syslist *stemp;
274 if ( (!strcmp(sysname,sysnambuf))
275 && (sysptrbuf!=NULL) ) return(sysptrbuf);
277 strcpy(sysnambuf,sysname);
278 for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
279 if (!strcmp(sysname,stemp->s_name)) {
290 * make sure only one copy of netproc runs at a time, using lock files
292 int set_lockfile(void) {
296 if ((lfp = fopen(LOCKFILE,"r")) != NULL) {
297 fscanf(lfp,"%d",&onppid);
299 if (!kill(onppid, 0) || errno == EPERM) return 1;
302 lfp=fopen(LOCKFILE,"w");
303 fprintf(lfp,"%ld\n",(long)getpid());
308 void remove_lockfile(void) {
313 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
314 * cleanup() . If for some reason netproc hangs waiting for the server
315 * to clean up, the alarm clock goes off and the program exits anyway.
316 * The cleanup() routine makes a check to ensure it's not reentering, in
317 * case the ipc module looped it somehow.
319 void nq_cleanup(int e)
328 static int nested = 0;
331 signal(SIGALRM,nq_cleanup);
332 if (nested++ < 1) serv_puts("QUIT");
337 * This is implemented as a function rather than as a macro because the
338 * client-side IPC modules expect logoff() to be defined. They call logoff()
339 * when a problem connecting or staying connected to the server occurs.
347 * If there is a kill file in place, this function will process it.
349 void load_filterlist(void) {
351 struct filterlist *fbuf;
354 fp=fopen("./network/filterlist","r");
355 if (fp==NULL) return;
356 while (fgets(sbuf,256,fp)!=NULL) {
358 sbuf[strlen(sbuf)-1]=0;
359 fbuf=(struct filterlist *)
360 malloc((long)sizeof(struct filterlist));
363 strcpy(fbuf->f_person,"*");
364 strcpy(fbuf->f_room,"*");
365 strcpy(fbuf->f_system,"*");
367 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
370 strcpy(fbuf->f_person,sbuf);
371 strcpy(sbuf,&sbuf[p+1]);
373 for (a=strlen(sbuf); a>=0; --a) if (sbuf[a]==',') p=a;
376 strcpy(fbuf->f_room,sbuf);
377 strcpy(sbuf,&sbuf[p+1]);
379 strcpy(fbuf->f_system,sbuf);
385 /* returns 1 if user/message/room combination is in the kill file */
386 int is_banned(char *k_person, char *k_room, char *k_system)
388 struct filterlist *fptr;
390 for (fptr=filter; fptr!=NULL; fptr=fptr->next) if (
391 ((!strcasecmp(fptr->f_person,k_person))||(!strcmp(fptr->f_person,"*")))
393 ((!strcasecmp(fptr->f_room,k_room))||(!strcmp(fptr->f_room,"*")))
395 ((!strcasecmp(fptr->f_system,k_system))||(!strcmp(fptr->f_system,"*")))
401 int get_sysinfo_type(char *name) /* determine routing from sysinfo file */
403 struct syslist *stemp;
404 GETSN: for (stemp=slist; stemp!=NULL; stemp=stemp->next) {
405 if (!strcasecmp(stemp->s_name,name)) {
406 if (!strcasecmp(stemp->s_type,"use")) {
407 strcpy(name,stemp->s_nexthop);
410 if (!strcasecmp(stemp->s_type,"bin")) {
413 if (!strcasecmp(stemp->s_type,"uum")) {
414 return(MES_INTERNET);
418 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
423 void fpgetfield(FILE *fp, char *string)
443 * Load all of the fields of a message, except the actual text, into a
444 * table in memory (so we know how to process the message).
446 void msgfind(char *msgfile, struct minfo *buffer)
454 fp=fopen(msgfile,"rb");
456 syslog(LOG_ERR, "can't open message file: %s",strerror(errno));
461 syslog(LOG_ERR, "incorrect message format");
464 mtype=getc(fp); aflag=getc(fp);
473 BONFGM: b=getc(fp); if (b<0) goto END;
474 if (b=='M') goto END;
476 while ((bbb[0]==' ')&&(strlen(bbb)>1)) strcpy(bbb,&bbb[1]);
478 strcpy(buffer->A,bbb);
479 if (strlen(userid)==0) {
481 for (e=0; e<strlen(userid); ++e)
482 if (userid[e]==' ') userid[e]='_';
485 if (b=='O') strcpy(buffer->O,bbb);
486 if (b=='C') strcpy(buffer->C,bbb);
487 if (b=='N') strcpy(buffer->N,bbb);
488 if (b=='S') strcpy(buffer->S,bbb);
490 /* extract the user id from the path */
491 for (e=0; e<strlen(bbb); ++e)
492 if (bbb[e]=='!') strcpy(userid,&bbb[e+1]);
494 /* now find the next hop */
495 for (e=0; e<strlen(bbb); ++e) if (bbb[e]=='!') bbb[e]=0;
496 strcpy(buffer->nexthop,bbb);
499 for (e=0; e<strlen(bbb); ++e) if (bbb[e]=='_') bbb[e]=' ';
500 strcpy(buffer->R,bbb);
502 if (b=='D') strcpy(buffer->D,bbb);
503 if (b=='T') buffer->T=atol(bbb);
504 if (b=='I') buffer->I=atol(bbb);
505 if (b=='H') strcpy(buffer->H,bbb);
506 if (b=='B') strcpy(buffer->B,bbb);
507 if (b=='G') strcpy(buffer->G,bbb);
508 if (b=='E') strcpy(buffer->E,bbb);
511 END: if (buffer->I==0L) buffer->I=buffer->T;
515 void ship_to(char *filenm, char *sysnm) /* send spool file filenm to system sysnm */
524 syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
526 sprintf(sysflnm,"./network/systems/%s",sysnm);
527 sysflfd=fopen(sysflnm,"r");
528 if (sysflfd==NULL) syslog(LOG_ERR, "cannot open %s", sysflnm);
529 fgets(commbuf1,99,sysflfd);
530 commbuf1[strlen(commbuf1)-1] = 0;
532 sprintf(commbuf2,commbuf1,filenm);
537 * proc_file_transfer() - handle a simple file transfer packet
540 void proc_file_transfer(char *tname)
541 { /* name of temp file containing the whole message */
543 char dest_room[ROOMNAMELEN];
544 char subdir_name[256];
548 syslog(LOG_NOTICE, "processing network file transfer...");
550 tfp=fopen(tname,"rb");
551 if (tfp==NULL) syslog(LOG_ERR, "cannot open %s", tname);
552 getc(tfp); getc(tfp); getc(tfp);
558 strcpy(dest_room, buf);
561 } while ((a!='M')&&(a>=0));
564 syslog(LOG_ERR, "no message text for file transfer");
568 strcpy(subdir_name, "---xxx---");
569 sprintf(buf, "GOTO %s", dest_room);
573 extract(subdir_name, &buf[4], 2);
574 if (strlen(subdir_name) == 0) strcpy(subdir_name, "--xxx--");
577 /* Change to the room's directory; if that fails, change to the
578 * bitbucket directory. Then run uudecode.
580 sprintf(buf,"(cd %s/files/%s || cd %s/files/%s ) ; exec %s",
581 bbs_home_directory, subdir_name,
582 bbs_home_directory, config.c_bucket_dir,
585 uud=(FILE *)popen(buf,"w");
587 syslog(LOG_ERR, "cannot open uudecode pipe");
593 buf[strlen(buf)-1] = 0;
594 for (a=0; a<strlen(buf); ++a) if (buf[a]=='/') buf[a]='_';
595 fprintf(uud,"%s\n",buf);
596 printf("netproc: %s\n",buf);
597 while(a=getc(tfp), a>0) putc(a,uud);
604 /* send a bounce message */
605 void bounce(struct minfo *bminfo)
613 sprintf(bfilename,"./network/spoolin/bounce.%ld.%d",(long)getpid(),
615 bounce = fopen(bfilename,"wb");
618 fprintf(bounce,"%c%c%c",0xFF,MES_NORMAL,0);
619 fprintf(bounce,"Ppostmaster%c",0);
620 fprintf(bounce,"T%ld%c",(long)now,0);
621 fprintf(bounce,"APostmaster%c",0);
622 fprintf(bounce,"OMail%c",0);
623 fprintf(bounce,"N%s%c",config.c_nodename,0);
624 fprintf(bounce,"H%s%c",config.c_humannode,0);
626 if (strlen(bminfo->E) > 0) {
627 fprintf(bounce,"R%s%c",bminfo->E,0);
630 fprintf(bounce,"R%s%c",bminfo->A,0);
633 fprintf(bounce,"D%s%c",bminfo->N,0);
634 fprintf(bounce,"M%s could not deliver your mail to:\n",
636 fprintf(bounce," \n %s\n \n",bminfo->R);
637 fprintf(bounce," because there is no such user on this system.\n");
638 fprintf(bounce," (Unsent message does *not* follow. ");
639 fprintf(bounce,"Help to conserve bandwidth.)\n%c",0);
646 * process incoming files in ./network/spoolin
648 void inprocess(void) {
649 FILE *fp,*message,*testfp,*ls;
650 static struct minfo minfo;
651 struct recentmsg recentmsg;
652 char tname[128],aaa[1024],iname[256],sfilename[256],pfilename[256];
655 struct syslist *stemp;
661 /* temp file names */
662 sprintf(tname, tmpnam(NULL));
663 sprintf(iname, tmpnam(NULL));
667 chdir(bbs_home_directory);
669 /* Let the shell do the dirty work. Get all data from spoolin */
671 sprintf(aaa,"cd %s/network/spoolin; ls",bbs_home_directory);
674 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
678 SKIP: ptr=fgets(sfilename, sizeof sfilename, ls);
680 sfilename[strlen(sfilename)-1] = 0;
681 if (!strcmp(sfilename, ".")) goto SKIP;
682 if (!strcmp(sfilename, "..")) goto SKIP;
683 if (!strcmp(sfilename, "CVS")) goto SKIP;
687 PROCESS_IT: pclose(ls);
691 sprintf(pfilename,"%s/network/spoolin/%s",bbs_home_directory,sfilename);
692 syslog(LOG_NOTICE, "processing <%s>", pfilename);
694 fp = fopen(pfilename, "rb");
696 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
697 fp = fopen("/dev/null" ,"rb");
700 NXMSG: /* Seek to the beginning of the next message */
703 } while((a!=255)&&(a>=0));
704 if (a<0) goto ENDSTR;
706 message = fopen(tname, "wb"); /* This crates the temporary file. */
707 if (message == NULL) {
708 syslog(LOG_ERR, "error creating %s: %s", tname,strerror(errno));
711 putc(255,message); /* 0xFF (start-of-message) */
712 a = getc(fp); putc(a, message); /* type */
713 a = getc(fp); putc(a, message); /* mode */
715 FieldID = getc(fp); /* Header field ID */
716 putc(FieldID, message);
721 } while ((FieldID != 'M') && (a >= 0)); /* M is always last */
723 msglen = ftell(message);
726 /* process the individual mesage */
732 msgfind(tname,&minfo);
733 strncpy(recentmsg.RMnodename,minfo.N,9);
734 recentmsg.RMnodename[9]=0;
735 recentmsg.RMnum=minfo.I;
736 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
737 minfo.I,minfo.A,minfo.O,minfo.N);
738 if (strlen(minfo.R)>0) {
739 syslog(LOG_NOTICE, " to <%s>",minfo.R);
740 if (strlen(minfo.D)>0) {
741 syslog(LOG_NOTICE, " @ <%s>",minfo.D);
744 if (!strcasecmp(minfo.D,FQDN)) strcpy(minfo.D,NODENAME);
746 /* this routine updates our info on the system that sent the message */
747 stemp = get_sys_ptr(minfo.N);
748 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
749 /* add non-neighbor system to map */
750 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
752 stemp = (struct syslist *)malloc((long)sizeof(struct syslist));
755 strcpy(slist->s_name,minfo.N);
756 strcpy(slist->s_type,"use");
757 strcpy(slist->s_nexthop,minfo.nexthop);
758 time(&slist->s_lastcontact);
760 else if ((stemp == NULL) && (!strcasecmp(minfo.N,minfo.nexthop))) {
761 /* add neighbor system to map */
762 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
764 sprintf(aaa,"%s/network/systems/%s",bbs_home_directory,minfo.N);
765 testfp=fopen(aaa,"r");
768 stemp = (struct syslist *)
769 malloc((long)sizeof(struct syslist));
772 strcpy(slist->s_name,minfo.N);
773 strcpy(slist->s_type,"bin");
774 strcpy(slist->s_nexthop,"Mail");
775 time(&slist->s_lastcontact);
778 /* now update last contact and long node name if we can */
780 time(&stemp->s_lastcontact);
781 if (strlen(minfo.H) > 0) strcpy(stemp->s_humannode,minfo.H);
782 if (strlen(minfo.B) > 0) strcpy(stemp->s_phonenum,minfo.B);
783 if (strlen(minfo.G) > 0) strcpy(stemp->s_gdom,minfo.G);
786 /* route the message if necessary */
787 if ((strcasecmp(minfo.D,NODENAME))&&(minfo.D[0]!=0)) {
788 a = get_sysinfo_type(minfo.D);
789 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
791 if (a==MES_INTERNET) {
793 syslog(LOG_NOTICE, "netmailer %s", tname);
795 execlp("./netmailer","netmailer",
797 syslog(LOG_ERR, "error running netmailer: %s",
801 else while (wait(&b)!=(-1));
803 else if (a==MES_BINARY) {
804 ship_to(tname,minfo.D);
807 /* message falls into the bit bucket? */
811 /* check to see if it's a file transfer */
812 else if (!strncasecmp(minfo.S,"FILE",4)) {
813 proc_file_transfer(tname);
816 /* otherwise process it as a normal message */
819 if (!strcasecmp(minfo.R, "postmaster")) {
821 strcpy(minfo.C, "Aide");
824 if (strlen(minfo.R) > 0) {
825 sprintf(buf,"GOTO _MAIL_");
827 if (is_banned(minfo.A,minfo.C,minfo.N)) {
828 sprintf(buf,"GOTO %s", FILTERROOM);
831 if (strlen(minfo.C) > 0) {
832 sprintf(buf,"GOTO %s", minfo.C);
835 sprintf(buf,"GOTO %s", minfo.O);
841 syslog(LOG_ERR, "%s", buf);
842 sprintf(buf,"GOTO _BITBUCKET_");
847 /* Open the temporary file containing the message */
848 message = fopen(tname, "rb");
849 if (message == NULL) {
850 syslog(LOG_ERR, "cannot open %s: %s",
851 tname, strerror(errno));
856 /* Transmit the message to the server */
857 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
860 if (!strncmp(buf, "570", 3)) {
861 /* no such user, do a bounce */
865 /* Always use the server's idea of the message length,
866 * even though they should both be identical */
867 msglen = atol(&buf[4]);
868 while (msglen > 0L) {
869 bloklen = ((msglen >= 255L) ? 255 : ((int)msglen));
870 if (fread(buf, bloklen, 1, message) < 1) {
872 "error trying to read %d bytes: %s",
873 bloklen, strerror(errno));
875 serv_write(buf, bloklen);
876 msglen = msglen - (long)bloklen;
882 syslog(LOG_ERR, "%s", buf);
899 int checkpath(char *path, char *sys) /* Checks to see whether its ok to send */
900 /* Returns 1 for ok, send message */
901 { /* Returns 0 if message already there */
908 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
910 for (a=0; a<strlen(path); ++a) {
911 if (!strncmp(&path[a],sys2,strlen(sys2))) return(0);
917 * implement split horizon algorithm
919 int ismsgok(long int mpos, FILE *mmfp, char *sysname)
922 int ok = 0; /* fail safe - no path, don't send it */
926 if (getc(mmfp)!=255) return(0);
927 getc(mmfp); getc(mmfp);
929 while (a=getc(mmfp),((a!='M')&&(a!=0))) {
930 fpgetfield(mmfp,fbuf);
932 ok = checkpath(fbuf,sysname);
936 syslog(LOG_NOTICE, "%s", ((ok)?"SEND":"(no)") );
943 /* spool list of messages to a file */
944 /* returns # of msgs spooled */
945 int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname)
947 struct msglist *cmptr;
951 int msgs_spooled = 0;
960 /* for each message in the list... */
961 for (cmptr=cmlist; cmptr!=NULL; cmptr=cmptr->next) {
963 /* make sure we're in the correct room... */
964 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
965 sprintf(buf, "GOTO %s", cmptr->m_rmname);
969 strcpy(curr_rm, cmptr->m_rmname);
972 syslog(LOG_ERR, "%s", buf);
976 /* download the message from the server... */
978 sprintf(buf, "MSG3 %ld", cmptr->m_num);
981 if (buf[0]=='6') { /* read the msg */
982 msg_len = atol(&buf[4]);
983 while (msg_len > 0L) {
984 blok_len = ((msg_len >= 256L) ? 256 : (int)msg_len);
985 serv_read(buf, blok_len);
986 fwrite(buf, blok_len, 1, mmfp);
987 msg_len = msg_len - (long)blok_len;
990 else { /* or print the err */
991 syslog(LOG_ERR, "%s", buf);
996 if (ismsgok(0L,mmfp,sysname)) {
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);
1007 fwrite(fbuf,strlen(fbuf)+1,1,destfp);
1010 fprintf(destfp, "C%s%c",
1011 cmptr->m_rmname, 0);
1022 return(msgs_spooled);
1025 void outprocess(char *sysname) /* send new room messages to sysname */
1029 char shiptocmd[128];
1033 struct msglist *cmlist = NULL;
1034 struct rmlist *crmlist = NULL;
1035 struct rmlist *rmptr,*rmptr2;
1036 struct msglist *cmptr,*cmptr2;
1037 FILE *sysflfp,*tempflfp;
1041 sprintf(tempflnm, tmpnam(NULL));
1042 tempflfp = fopen(tempflnm,"w");
1043 if (tempflfp == NULL) return;
1047 * Read system file for node in question and put together room list
1049 sprintf(sysflnm,"%s/network/systems/%s",bbs_home_directory,sysname);
1050 sysflfp=fopen(sysflnm,"r");
1051 if (sysflfp==NULL) return;
1052 fgets(shiptocmd,128,sysflfp); shiptocmd[strlen(shiptocmd)-1]=0;
1053 while(!feof(sysflfp)) {
1054 if (fgets(srmname,32,sysflfp)==NULL) break;
1055 srmname[strlen(srmname)-1]=0;
1056 fgets(lbuf,32,sysflfp);
1057 rmptr=(struct rmlist *)malloc(sizeof(struct rmlist));
1059 strcpy(rmptr->rm_name,srmname);
1060 strip_trailing_whitespace(rmptr->rm_name);
1061 rmptr->rm_lastsent = atol(lbuf);
1062 if (crmlist==NULL) crmlist=rmptr;
1063 else if (!strcasecmp(rmptr->rm_name,"control")) {
1064 /* control has to be first in room list */
1065 rmptr->next = crmlist;
1070 while (rmptr2->next != NULL) rmptr2=rmptr2->next;
1077 * Assemble list of messages to be spooled
1079 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next) {
1081 sprintf(buf,"GOTO %s",rmptr->rm_name);
1085 syslog(LOG_ERR, "%s", buf);
1088 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1091 if (buf[0]=='1') while (serv_gets(buf), strcmp(buf,"000")) {
1092 thismsg = atol(buf);
1093 if ( thismsg > (rmptr->rm_lastsent) ) {
1094 rmptr->rm_lastsent = thismsg;
1096 cmptr=(struct msglist *)
1097 malloc(sizeof(struct msglist));
1099 cmptr->m_num = thismsg;
1100 strcpy(cmptr->m_rmname, rmptr->rm_name);
1102 if (cmlist == NULL) cmlist = cmptr;
1105 while (cmptr2->next != NULL)
1106 cmptr2 = cmptr2->next;
1107 cmptr2->next = cmptr;
1111 else { /* print error from "msgs all" */
1112 syslog(LOG_ERR, "%s", buf);
1117 outgoing_msgs=0; cmptr2=cmlist; /* this loop counts the messages */
1118 while (cmptr2!=NULL) {
1120 cmptr2 = cmptr2->next;
1122 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1123 outgoing_msgs,sysname);
1126 * Spool out the messages, but only if there are any.
1128 if (outgoing_msgs!=0) outgoing_msgs=spool_out(cmlist,tempflfp,sysname);
1129 syslog(LOG_NOTICE, "%d messages actually spooled",
1133 * Deallocate list of spooled messages.
1135 while(cmlist!=NULL) {
1142 * Rewrite system file and deallocate room list.
1144 syslog(LOG_NOTICE, "Spooling...");
1145 sysflfp=fopen(sysflnm,"w");
1146 fprintf(sysflfp,"%s\n",shiptocmd);
1147 for (rmptr=crmlist; rmptr!=NULL; rmptr=rmptr->next)
1148 fprintf(sysflfp,"%s\n%ld\n",rmptr->rm_name,rmptr->rm_lastsent);
1150 while(crmlist!=NULL) {
1151 rmptr=crmlist->next;
1157 * Close temporary file, ship it out, and return
1160 if (outgoing_msgs!=0) ship_to(tempflnm,sysname);
1166 * Connect netproc to the Citadel server running on this computer.
1168 void np_attach_to_server(void) {
1171 char *args[] = { "netproc", "localhost", NULL, NULL } ;
1173 syslog(LOG_NOTICE, "Attaching to server...");
1174 sprintf(portname, "%d", config.c_port_number);
1176 attach_to_server(3, args);
1178 syslog(LOG_NOTICE, "%s", &buf[4]);
1179 sprintf(buf,"IPGM %d", config.c_ipgm_secret);
1182 syslog(LOG_NOTICE, "%s", &buf[4]);
1193 int main(int argc, char **argv)
1198 int import_only = 0; /* if set to 1, don't export anything */
1200 openlog("netproc", LOG_PID, LOG_USER);
1201 strcpy(bbs_home_directory, BBSDIR);
1204 * Change directories if specified
1206 for (a=1; a<argc; ++a) {
1207 if (!strncmp(argv[a], "-h", 2)) {
1208 strcpy(bbs_home_directory, argv[a]);
1209 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1212 else if (!strcmp(argv[a], "-i")) {
1216 fprintf(stderr, "netproc: usage: ");
1217 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1224 if (set_lockfile()!=0) {
1225 syslog(LOG_NOTICE, "lock file exists: already running");
1229 signal(SIGINT,cleanup);
1230 signal(SIGQUIT,cleanup);
1231 signal(SIGHUP,cleanup);
1232 signal(SIGTERM,cleanup);
1234 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1236 np_attach_to_server();
1239 if (load_syslist()!=0) syslog(LOG_ERR, "cannot load sysinfo");
1240 setup_special_nodes();
1242 inprocess(); /* first collect incoming stuff */
1244 if (import_only != 1) {
1245 allfp=(FILE *)popen("cd ./network/systems; ls","r");
1247 while (fgets(allst,32,allfp)!=NULL) {
1248 allst[strlen(allst)-1] = 0;
1253 /* import again in case anything new was generated */
1258 syslog(LOG_NOTICE, "processing ended.");