2 * Citadel/UX Intelligent Network Processor for IGnet/Open networks
3 * See copyright.txt for copyright information
7 /* How long it takes for an old node to drop off the network map */
8 #define EXPIRY_TIME (2592000L)
10 /* How long we keep recently arrived messages in the use table */
11 #define USE_TIME (604800L)
13 /* Where do we keep our lock file? */
14 #define LOCKFILE "/var/lock/LCK.netproc"
16 /* Path to the 'uudecode' utility (needed for network file transfers) */
17 #define UUDECODE "/usr/bin/uudecode"
19 /* Uncomment the DEBUG def to see noisy traces */
26 #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
55 char m_rmname[ROOMNAMELEN];
60 char rm_name[ROOMNAMELEN];
65 struct filterlist *next;
83 void attach_to_server(int argc, char **argv);
84 void serv_read(char *buf, int bytes);
85 void serv_write(char *buf, int nbytes);
86 void get_config(void);
88 struct filterlist *filter = NULL;
89 struct syslist *slist = NULL;
92 extern char bbs_home_directory[];
93 extern int home_specified;
100 * replacement strerror() for systems that don't have it
102 char *strerror(int e)
106 sprintf(buf, "errno = %d", e);
112 void strip_trailing_whitespace(char *buf)
114 while (isspace(buf[strlen(buf) - 1]))
115 buf[strlen(buf) - 1] = 0;
120 * we also load the network/mail.sysinfo table into memory, make changes
121 * as we learn more about the network from incoming messages, and write
122 * the table back to disk when we're done.
124 int load_syslist(void)
127 struct syslist *stemp;
131 fp = fopen("network/mail.sysinfo", "r");
136 if (fgets(buf, 128, fp) == NULL) {
140 buf[strlen(buf) - 1] = 0;
141 while (isspace(buf[0]))
142 strcpy(buf, &buf[1]);
145 if ((insys == 0) && (strlen(buf) != 0)) {
147 stemp = (struct syslist *) malloc(sizeof(struct syslist));
150 strcpy(slist->s_name, buf);
151 strcpy(slist->s_type, "bin");
152 strcpy(slist->s_nexthop, "Mail");
153 slist->s_lastcontact = 0L;
154 strcpy(slist->s_humannode, "");
155 strcpy(slist->s_phonenum, "");
156 strcpy(slist->s_gdom, "");
157 } else if ((insys == 1) && (strlen(buf) == 0)) {
159 } else if ((insys == 1) && (!strncasecmp(buf, "bin", 3))) {
160 strcpy(slist->s_type, "bin");
161 strcpy(slist->s_nexthop, &buf[4]);
162 } else if ((insys == 1) && (!strncasecmp(buf, "use", 3))) {
163 strcpy(slist->s_type, "use");
164 strcpy(slist->s_nexthop, &buf[4]);
165 } else if ((insys == 1) && (!strncasecmp(buf, "uum", 3))) {
166 strcpy(slist->s_type, "uum");
167 strcpy(slist->s_nexthop, &buf[4]);
168 } else if ((insys == 1) && (!strncasecmp(buf, "lastcontact", 11))) {
170 sscanf(&buf[12], "%ld", &foo);
171 slist->s_lastcontact = foo;
172 } else if ((insys == 1) && (!strncasecmp(buf, "humannode", 9))) {
173 strcpy(slist->s_humannode, &buf[10]);
174 } else if ((insys == 1) && (!strncasecmp(buf, "phonenum", 8))) {
175 strcpy(slist->s_phonenum, &buf[9]);
176 } else if ((insys == 1) && (!strncasecmp(buf, "gdom", 4))) {
177 strcpy(slist->s_gdom, &buf[5]);
182 /* now we have to set up two "special" nodes on the list: one
183 * for the local node, and one for an Internet gateway
185 void setup_special_nodes(void)
187 struct syslist *stemp, *slocal;
190 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
191 if (!strcasecmp(stemp->s_name, config.c_nodename))
194 if (slocal == NULL) {
195 slocal = (struct syslist *) malloc(sizeof(struct syslist));
196 slocal->next = slist;
199 strcpy(slocal->s_name, config.c_nodename);
200 strcpy(slocal->s_type, "bin");
201 strcpy(slocal->s_nexthop, "Mail");
202 time(&slocal->s_lastcontact);
203 strcpy(slocal->s_humannode, config.c_humannode);
204 strcpy(slocal->s_phonenum, config.c_phonenum);
207 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
208 if (!strcasecmp(stemp->s_name, "internet"))
211 if (slocal == NULL) {
212 slocal = (struct syslist *) malloc(sizeof(struct syslist));
213 slocal->next = slist;
216 strcpy(slocal->s_name, "internet");
217 strcpy(slocal->s_type, "uum");
218 strcpy(slocal->s_nexthop, "%s");
219 time(&slocal->s_lastcontact);
220 strcpy(slocal->s_humannode, "Internet Gateway");
221 strcpy(slocal->s_phonenum, "");
222 strcpy(slocal->s_gdom, "");
227 * here's the routine to write the table back to disk.
229 void rewrite_syslist(void)
231 struct syslist *stemp;
236 newfp = fopen("network/mail.sysinfo", "w");
237 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
238 if (!strcasecmp(stemp->s_name, config.c_nodename)) {
239 time(&stemp->s_lastcontact);
240 strcpy(stemp->s_type, "bin");
241 strcpy(stemp->s_humannode, config.c_humannode);
242 strcpy(stemp->s_phonenum, config.c_phonenum);
244 /* remove systems we haven't heard from in a while */
245 if ((stemp->s_lastcontact == 0L)
246 || (now - stemp->s_lastcontact < EXPIRY_TIME)) {
247 fprintf(newfp, "%s\n%s %s\n",
248 stemp->s_name, stemp->s_type, stemp->s_nexthop);
249 if (strlen(stemp->s_phonenum) > 0)
250 fprintf(newfp, "phonenum %s\n", stemp->s_phonenum);
251 if (strlen(stemp->s_gdom) > 0)
252 fprintf(newfp, "gdom %s\n", stemp->s_gdom);
253 if (strlen(stemp->s_humannode) > 0)
254 fprintf(newfp, "humannode %s\n", stemp->s_humannode);
255 if (stemp->s_lastcontact > 0L)
256 fprintf(newfp, "lastcontact %ld %s",
257 (long) stemp->s_lastcontact,
258 asctime(localtime(&stemp->s_lastcontact)));
259 fprintf(newfp, "\n");
263 /* now free the list */
264 while (slist != NULL) {
272 /* call this function with the node name of a system and it returns a pointer
273 * to its syslist structure.
275 struct syslist *get_sys_ptr(char *sysname)
277 static char sysnambuf[16];
278 static struct syslist *sysptrbuf = NULL;
279 struct syslist *stemp;
281 if ((!strcmp(sysname, sysnambuf))
282 && (sysptrbuf != NULL))
285 strcpy(sysnambuf, sysname);
286 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
287 if (!strcmp(sysname, stemp->s_name)) {
298 * make sure only one copy of netproc runs at a time, using lock files
300 int set_lockfile(void)
305 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
306 fscanf(lfp, "%d", &onppid);
308 if (!kill(onppid, 0) || errno == EPERM)
311 lfp = fopen(LOCKFILE, "w");
313 syslog(LOG_NOTICE, "Cannot create %s: %s", LOCKFILE,
317 fprintf(lfp, "%ld\n", (long) getpid());
322 void remove_lockfile(void)
328 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
329 * cleanup() . If for some reason netproc hangs waiting for the server
330 * to clean up, the alarm clock goes off and the program exits anyway.
331 * The cleanup() routine makes a check to ensure it's not reentering, in
332 * case the ipc module looped it somehow.
334 void nq_cleanup(int e)
343 static int nested = 0;
346 signal(SIGALRM, nq_cleanup);
353 * This is implemented as a function rather than as a macro because the
354 * client-side IPC modules expect logoff() to be defined. They call logoff()
355 * when a problem connecting or staying connected to the server occurs.
363 * If there is a kill file in place, this function will process it.
365 void load_filterlist(void)
368 struct filterlist *fbuf;
371 fp = fopen("./network/filterlist", "r");
374 while (fgets(sbuf, 256, fp) != NULL) {
375 if (sbuf[0] != '#') {
376 sbuf[strlen(sbuf) - 1] = 0;
377 fbuf = (struct filterlist *)
378 malloc((long) sizeof(struct filterlist));
381 strcpy(fbuf->f_person, "*");
382 strcpy(fbuf->f_room, "*");
383 strcpy(fbuf->f_system, "*");
385 for (a = strlen(sbuf); a >= 0; --a)
390 strcpy(fbuf->f_person, sbuf);
391 strcpy(sbuf, &sbuf[p + 1]);
393 for (a = strlen(sbuf); a >= 0; --a)
398 strcpy(fbuf->f_room, sbuf);
399 strcpy(sbuf, &sbuf[p + 1]);
401 strcpy(fbuf->f_system, sbuf);
407 /* returns 1 if user/message/room combination is in the kill file */
408 int is_banned(char *k_person, char *k_room, char *k_system)
410 struct filterlist *fptr;
412 for (fptr = filter; fptr != NULL; fptr = fptr->next)
414 ((!strcasecmp(fptr->f_person, k_person)) || (!strcmp(fptr->f_person, "*")))
416 ((!strcasecmp(fptr->f_room, k_room)) || (!strcmp(fptr->f_room, "*")))
418 ((!strcasecmp(fptr->f_system, k_system)) || (!strcmp(fptr->f_system, "*")))
426 * Determine routing from sysinfo file
428 int get_sysinfo_type(char *name) {
429 struct syslist *stemp;
431 GETSN: for (stemp = slist; stemp != NULL; stemp = stemp->next) {
432 if (!strcasecmp(stemp->s_name, name)) {
433 if (!strcasecmp(stemp->s_type, "use")) {
434 strcpy(name, stemp->s_nexthop);
437 if (!strcasecmp(stemp->s_type, "bin")) {
440 if (!strcasecmp(stemp->s_type, "uum")) {
441 return (MES_INTERNET);
445 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
450 void fpgetfield(FILE * fp, char *string)
470 * Load all of the fields of a message, except the actual text, into a
471 * table in memory (so we know how to process the message).
473 void fpmsgfind(FILE *fp, struct minfo *buffer)
475 int b, e, mtype, aflag;
482 syslog(LOG_ERR, "Magic number check failed for this message");
495 BONFGM: b = getc(fp);
501 while ((bbb[0] == ' ') && (strlen(bbb) > 1))
502 strcpy(bbb, &bbb[1]);
504 strcpy(buffer->A, bbb);
505 if (strlen(userid) == 0) {
507 for (e = 0; e < strlen(userid); ++e)
508 if (userid[e] == ' ')
513 strcpy(buffer->O, bbb);
515 strcpy(buffer->C, bbb);
517 strcpy(buffer->N, bbb);
519 strcpy(buffer->S, bbb);
521 /* extract the user id from the path */
522 for (e = 0; e < strlen(bbb); ++e)
524 strcpy(userid, &bbb[e + 1]);
526 /* now find the next hop */
527 for (e = 0; e < strlen(bbb); ++e)
530 strcpy(buffer->nexthop, bbb);
533 for (e = 0; e < strlen(bbb); ++e)
536 strcpy(buffer->R, bbb);
539 strcpy(buffer->D, bbb);
541 buffer->T = atol(bbb);
543 buffer->I = atol(bbb);
545 strcpy(buffer->H, bbb);
547 strcpy(buffer->B, bbb);
549 strcpy(buffer->G, bbb);
551 strcpy(buffer->E, bbb);
556 /* NOTE: we used to use the following two lines of code to assign
557 * the timestamp as a message-ID if there was no message-ID already
558 * in the message. We don't do this anymore because it screws up
562 buffer->I = buffer->T;
568 * msgfind() is the same as fpmsgfind() except it accepts a filename
569 * instead of a file handle.
571 void msgfind(char *msgfile, struct minfo *buffer) {
574 fp = fopen(msgfile, "rb");
576 syslog(LOG_ERR, "can't open %s: %s", msgfile, strerror(errno));
580 fpmsgfind(fp, buffer);
588 void ship_to(char *filenm, char *sysnm)
589 { /* send spool file filenm to system sysnm */
596 syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
598 sprintf(sysflnm, "./network/systems/%s", sysnm);
599 sysflfd = fopen(sysflnm, "r");
601 syslog(LOG_ERR, "cannot open %s", sysflnm);
602 fgets(commbuf1, 99, sysflfd);
603 commbuf1[strlen(commbuf1) - 1] = 0;
605 sprintf(commbuf2, commbuf1, filenm);
610 * proc_file_transfer() - handle a simple file transfer packet
613 void proc_file_transfer(char *tname)
614 { /* name of temp file containing the whole message */
616 char dest_room[ROOMNAMELEN];
617 char subdir_name[256];
621 syslog(LOG_NOTICE, "processing network file transfer...");
623 tfp = fopen(tname, "rb");
625 syslog(LOG_ERR, "cannot open %s", tname);
632 fpgetfield(tfp, buf);
634 strcpy(dest_room, buf);
637 } while ((a != 'M') && (a >= 0));
640 syslog(LOG_ERR, "no message text for file transfer");
643 strcpy(subdir_name, "---xxx---");
644 sprintf(buf, "GOTO %s", dest_room);
648 extract(subdir_name, &buf[4], 2);
649 if (strlen(subdir_name) == 0)
650 strcpy(subdir_name, "--xxx--");
652 /* Change to the room's directory; if that fails, change to the
653 * bitbucket directory. Then run uudecode.
655 sprintf(buf, "(cd %s/files/%s || cd %s/files/%s ) ; exec %s",
656 bbs_home_directory, subdir_name,
657 bbs_home_directory, config.c_bucket_dir,
660 uud = (FILE *) popen(buf, "w");
662 syslog(LOG_ERR, "cannot open uudecode pipe");
666 fgets(buf, 128, tfp);
667 buf[strlen(buf) - 1] = 0;
668 for (a = 0; a < strlen(buf); ++a)
671 fprintf(uud, "%s\n", buf);
672 printf("netproc: %s\n", buf);
673 while (a = getc(tfp), a > 0)
681 /* send a bounce message */
682 void bounce(struct minfo *bminfo)
690 sprintf(bfilename, "./network/spoolin/bounce.%ld.%d", (long) getpid(),
692 bounce = fopen(bfilename, "wb");
695 fprintf(bounce, "%c%c%c", 0xFF, MES_NORMAL, 0);
696 fprintf(bounce, "Ppostmaster%c", 0);
697 fprintf(bounce, "T%ld%c", (long) now, 0);
698 fprintf(bounce, "APostmaster%c", 0);
699 fprintf(bounce, "OMail%c", 0);
700 fprintf(bounce, "N%s%c", config.c_nodename, 0);
701 fprintf(bounce, "H%s%c", config.c_humannode, 0);
703 if (strlen(bminfo->E) > 0) {
704 fprintf(bounce, "R%s%c", bminfo->E, 0);
706 fprintf(bounce, "R%s%c", bminfo->A, 0);
709 fprintf(bounce, "D%s%c", bminfo->N, 0);
710 fprintf(bounce, "M%s could not deliver your mail to:\n",
712 fprintf(bounce, " \n %s\n \n", bminfo->R);
713 fprintf(bounce, " because there is no such user on this system.\n");
714 fprintf(bounce, " (Unsent message does *not* follow. ");
715 fprintf(bounce, "Help to conserve bandwidth.)\n%c", 0);
723 * Generate a Message-ID string for the use table
725 void strmsgid(char *buf, struct minfo *msginfo) {
728 sprintf(buf, "%ld@%s", msginfo->I, msginfo->N);
729 for (i=0; i<strlen(buf); ++i) {
730 if (isspace(buf[i])) {
731 strcpy(&buf[i], &buf[i+1]);
733 buf[i] = tolower(buf[i]);
740 * Check the use table to see if a message has been here before.
741 * Returns 1 if the message is a duplicate; otherwise, it returns
742 * 0 and the message ID is added to the use table.
744 int already_received(GDBM_FILE ut, struct minfo *msginfo) {
750 /* We can't check for dups on a zero msgid, so just pass them through */
751 if ((msginfo->I)==0L) {
755 strmsgid(buf, msginfo);
759 mkey.dsize = strlen(buf);
761 /* Set return value to 1 if message exists */
762 if (gdbm_exists(ut, mkey)) {
766 /* Write a record into the use table for this message.
767 * Replace existing records; this keeps the timestamp fresh.
769 newrec.dptr = (char *)&now;
770 newrec.dsize = sizeof(now);
771 gdbm_store(ut, mkey, newrec, GDBM_REPLACE);
779 * Purge any old entries out of the use table.
781 * Yes, you're reading this correctly: it keeps traversing the table until
782 * it manages to do a complete pass without deleting any records. Read the
783 * gdbm man page to find out why.
786 void purge_use_table(GDBM_FILE ut) {
787 datum mkey, nextkey, therec;
788 int purged_anything = 0;
789 time_t rec_timestamp, now;
795 mkey = gdbm_firstkey(ut);
796 while (mkey.dptr != NULL) {
797 therec = gdbm_fetch(ut, mkey);
798 if (therec.dptr != NULL) {
799 memcpy(&rec_timestamp, therec.dptr,
803 if ((now - rec_timestamp) > USE_TIME) {
804 gdbm_delete(ut, mkey);
809 nextkey = gdbm_nextkey(ut, mkey);
813 } while (purged_anything != 0);
819 * process incoming files in ./network/spoolin
823 FILE *fp, *message, *testfp, *ls, *duplist;
824 static struct minfo minfo;
825 struct recentmsg recentmsg;
826 char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
829 struct syslist *stemp;
835 /* temp file names */
836 sprintf(tname, tmpnam(NULL));
837 sprintf(iname, tmpnam(NULL));
841 /* Make sure we're in the right directory */
842 chdir(bbs_home_directory);
845 /* temporary file to contain a log of rejected dups */
848 /* Let the shell do the dirty work. Get all data from spoolin */
850 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
851 ls = popen(aaa, "r");
853 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
857 SKIP: ptr = fgets(sfilename, sizeof sfilename, ls);
861 "Trying %s", sfilename);
863 sfilename[strlen(sfilename) - 1] = 0;
864 if (!strcmp(sfilename, ".")) goto SKIP;
865 if (!strcmp(sfilename, "..")) goto SKIP;
866 if (!strcmp(sfilename, "CVS")) goto SKIP;
869 } while (ptr != NULL);
870 PROCESS_IT: pclose(ls);
873 sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
874 syslog(LOG_NOTICE, "processing <%s>", pfilename);
876 fp = fopen(pfilename, "rb");
878 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
879 fp = fopen("/dev/null", "rb");
881 NXMSG: /* Seek to the beginning of the next message */
884 } while ((a != 255) && (a >= 0));
888 /* This crates the temporary file. */
889 message = fopen(tname, "wb");
890 if (message == NULL) {
891 syslog(LOG_ERR, "error creating %s: %s",
892 tname, strerror(errno));
895 putc(255, message); /* 0xFF (start-of-message) */
897 putc(a, message); /* type */
899 putc(a, message); /* mode */
901 FieldID = getc(fp); /* Header field ID */
902 putc(FieldID, message);
907 } while ((FieldID != 'M') && (a >= 0));
908 /* M is always last */
910 msglen = ftell(message);
913 /* process the individual mesage */
919 msgfind(tname, &minfo);
920 strncpy(recentmsg.RMnodename, minfo.N, 9);
921 recentmsg.RMnodename[9] = 0;
922 recentmsg.RMnum = minfo.I;
923 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
924 minfo.I, minfo.A, minfo.O, minfo.N);
925 if (strlen(minfo.R) > 0) {
926 syslog(LOG_NOTICE, " to <%s>", minfo.R);
927 if (strlen(minfo.D) > 0) {
928 syslog(LOG_NOTICE, " @ <%s>",
932 if (!strcasecmp(minfo.D, FQDN))
933 strcpy(minfo.D, NODENAME);
935 /* this routine updates our info on the system that sent the message */
936 stemp = get_sys_ptr(minfo.N);
937 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
938 /* add non-neighbor system to map */
939 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
941 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
944 strcpy(slist->s_name, minfo.N);
945 strcpy(slist->s_type, "use");
946 strcpy(slist->s_nexthop, minfo.nexthop);
947 time(&slist->s_lastcontact);
948 } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
949 /* add neighbor system to map */
950 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
952 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
953 testfp = fopen(aaa, "r");
954 if (testfp != NULL) {
956 stemp = (struct syslist *)
957 malloc((long) sizeof(struct syslist));
960 strcpy(slist->s_name, minfo.N);
961 strcpy(slist->s_type, "bin");
962 strcpy(slist->s_nexthop, "Mail");
963 time(&slist->s_lastcontact);
966 /* now update last contact and long node name if we can */
968 time(&stemp->s_lastcontact);
969 if (strlen(minfo.H) > 0)
970 strcpy(stemp->s_humannode, minfo.H);
971 if (strlen(minfo.B) > 0)
972 strcpy(stemp->s_phonenum, minfo.B);
973 if (strlen(minfo.G) > 0)
974 strcpy(stemp->s_gdom, minfo.G);
977 /* Check the use table; reject message if it's been here before */
978 if (already_received(use_table, &minfo)) {
979 syslog(LOG_NOTICE, "rejected duplicate message");
980 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
981 minfo.I, minfo.A, minfo.O, minfo.N);
985 /* route the message if necessary */
986 else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
987 a = get_sysinfo_type(minfo.D);
988 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
990 if (a == MES_INTERNET) {
992 syslog(LOG_NOTICE, "netmailer %s", tname);
994 execlp("./netmailer", "netmailer",
996 syslog(LOG_ERR, "error running netmailer: %s",
1000 while (wait(&b) != (-1));
1001 } else if (a == MES_BINARY) {
1002 ship_to(tname, minfo.D);
1004 /* message falls into the bit bucket? */
1008 /* check to see if it's a file transfer */
1009 else if (!strncasecmp(minfo.S, "FILE", 4)) {
1010 proc_file_transfer(tname);
1013 /* otherwise process it as a normal message */
1016 if (!strcasecmp(minfo.R, "postmaster")) {
1017 strcpy(minfo.R, "");
1018 strcpy(minfo.C, "Aide");
1020 if (strlen(minfo.R) > 0) {
1021 sprintf(buf, "GOTO _MAIL_");
1023 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1024 sprintf(buf, "GOTO %s", FILTERROOM);
1026 if (strlen(minfo.C) > 0) {
1027 sprintf(buf, "GOTO %s", minfo.C);
1029 sprintf(buf, "GOTO %s", minfo.O);
1034 if (buf[0] != '2') {
1035 syslog(LOG_ERR, "%s", buf);
1036 sprintf(buf, "GOTO _BITBUCKET_");
1040 /* Open the temporary file containing the message */
1041 message = fopen(tname, "rb");
1042 if (message == NULL) {
1043 syslog(LOG_ERR, "cannot open %s: %s",
1044 tname, strerror(errno));
1048 /* Transmit the message to the server */
1049 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1052 if (!strncmp(buf, "570", 3)) {
1053 /* no such user, do a bounce */
1056 if (buf[0] == '7') {
1057 /* Always use the server's idea of the message length,
1058 * even though they should both be identical */
1059 msglen = atol(&buf[4]);
1060 while (msglen > 0L) {
1061 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1062 if (fread(buf, bloklen, 1, message) < 1) {
1064 "error trying to read %d bytes: %s",
1065 bloklen, strerror(errno));
1067 serv_write(buf, bloklen);
1068 msglen = msglen - (long) bloklen;
1073 syslog(LOG_ERR, "%s", buf);
1085 } while (ptr != NULL);
1090 * If dups were rejected, post a message saying so
1092 if (ftell(duplist)!=0L) {
1093 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1095 fprintf(fp, "%cA%c", 255, 1);
1096 fprintf(fp, "T%ld%c", time(NULL), 0);
1097 fprintf(fp, "ACitadel%c", 0);
1098 fprintf(fp, "OAide%cM", 0);
1099 fprintf(fp, "The following duplicate messages"
1100 " were rejected:\n \n");
1102 while (fgets(buf, sizeof(buf), duplist) != NULL) {
1103 buf[strlen(buf)-1] = 0;
1104 fprintf(fp, " %s\n", buf);
1106 fprintf(fp, "%c", 0);
1116 /* Checks to see whether its ok to send */
1117 /* Returns 1 for ok, send message */
1118 /* Returns 0 if message already there */
1119 int checkpath(char *path, char *sys)
1127 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1129 for (a = 0; a < strlen(path); ++a) {
1130 if (!strncmp(&path[a], sys2, strlen(sys2)))
1137 * Implement split horizon algorithm (prevent infinite spooling loops
1138 * by refusing to send any node a message which already contains its
1139 * nodename in the path).
1141 int ismsgok(FILE *mmfp, char *sysname)
1144 int ok = 0; /* fail safe - no path, don't send it */
1148 if (getc(mmfp) != 255)
1153 while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1154 fpgetfield(mmfp, fbuf);
1156 ok = checkpath(fbuf, sysname);
1160 syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1167 /* spool list of messages to a file */
1168 /* returns # of msgs spooled */
1169 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1171 struct msglist *cmptr;
1175 int msgs_spooled = 0;
1178 static struct minfo minfo;
1183 strcpy(curr_rm, "");
1185 /* for each message in the list... */
1186 for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1188 /* make sure we're in the correct room... */
1189 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1190 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1193 if (buf[0] == '2') {
1194 strcpy(curr_rm, cmptr->m_rmname);
1196 syslog(LOG_ERR, "%s", buf);
1199 /* download the message from the server... */
1201 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1204 if (buf[0] == '6') { /* read the msg */
1205 msg_len = atol(&buf[4]);
1206 while (msg_len > 0L) {
1207 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1208 serv_read(buf, blok_len);
1209 fwrite(buf, blok_len, 1, mmfp);
1210 msg_len = msg_len - (long) blok_len;
1212 } else { /* or print the err */
1213 syslog(LOG_ERR, "%s", buf);
1218 if (ismsgok(mmfp, sysname)) {
1222 fread(fbuf, 3, 1, mmfp);
1223 fwrite(fbuf, 3, 1, destfp);
1224 while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1227 fpgetfield(mmfp, fbuf);
1229 fprintf(destfp, "%s!", NODENAME);
1231 fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1234 fprintf(destfp, "C%s%c",
1235 cmptr->m_rmname, 0);
1243 /* Get this message into the use table, so we can reject it
1244 * if a misconfigured remote system sends it back to us.
1247 fpmsgfind(mmfp, &minfo);
1248 already_received(use_table, &minfo);
1254 return (msgs_spooled);
1257 void outprocess(char *sysname)
1258 { /* send new room messages to sysname */
1261 char shiptocmd[128];
1265 struct msglist *cmlist = NULL;
1266 struct rmlist *crmlist = NULL;
1267 struct rmlist *rmptr, *rmptr2;
1268 struct msglist *cmptr, *cmptr2;
1269 FILE *sysflfp, *tempflfp;
1273 sprintf(tempflnm, tmpnam(NULL));
1274 tempflfp = fopen(tempflnm, "w");
1275 if (tempflfp == NULL)
1280 * Read system file for node in question and put together room list
1282 sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1283 sysflfp = fopen(sysflnm, "r");
1284 if (sysflfp == NULL)
1286 fgets(shiptocmd, 128, sysflfp);
1287 shiptocmd[strlen(shiptocmd) - 1] = 0;
1288 while (!feof(sysflfp)) {
1289 if (fgets(srmname, 32, sysflfp) == NULL)
1291 srmname[strlen(srmname) - 1] = 0;
1292 fgets(lbuf, 32, sysflfp);
1293 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1295 strcpy(rmptr->rm_name, srmname);
1296 strip_trailing_whitespace(rmptr->rm_name);
1297 rmptr->rm_lastsent = atol(lbuf);
1298 if (crmlist == NULL)
1300 else if (!strcasecmp(rmptr->rm_name, "control")) {
1301 /* control has to be first in room list */
1302 rmptr->next = crmlist;
1306 while (rmptr2->next != NULL)
1307 rmptr2 = rmptr2->next;
1308 rmptr2->next = rmptr;
1314 * Assemble list of messages to be spooled
1316 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1318 sprintf(buf, "GOTO %s", rmptr->rm_name);
1321 if (buf[0] != '2') {
1322 syslog(LOG_ERR, "%s", buf);
1324 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1328 while (serv_gets(buf), strcmp(buf, "000")) {
1329 thismsg = atol(buf);
1330 if (thismsg > (rmptr->rm_lastsent)) {
1331 rmptr->rm_lastsent = thismsg;
1333 cmptr = (struct msglist *)
1334 malloc(sizeof(struct msglist));
1336 cmptr->m_num = thismsg;
1337 strcpy(cmptr->m_rmname, rmptr->rm_name);
1343 while (cmptr2->next != NULL)
1344 cmptr2 = cmptr2->next;
1345 cmptr2->next = cmptr;
1348 } else { /* print error from "msgs all" */
1349 syslog(LOG_ERR, "%s", buf);
1355 cmptr2 = cmlist; /* this loop counts the messages */
1356 while (cmptr2 != NULL) {
1358 cmptr2 = cmptr2->next;
1360 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1361 outgoing_msgs, sysname);
1364 * Spool out the messages, but only if there are any.
1366 if (outgoing_msgs != 0)
1367 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1368 syslog(LOG_NOTICE, "%d messages actually spooled",
1372 * Deallocate list of spooled messages.
1374 while (cmlist != NULL) {
1375 cmptr = cmlist->next;
1381 * Rewrite system file and deallocate room list.
1383 syslog(LOG_NOTICE, "Spooling...");
1384 sysflfp = fopen(sysflnm, "w");
1385 fprintf(sysflfp, "%s\n", shiptocmd);
1386 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1387 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1389 while (crmlist != NULL) {
1390 rmptr = crmlist->next;
1396 * Close temporary file, ship it out, and return
1399 if (outgoing_msgs != 0)
1400 ship_to(tempflnm, sysname);
1406 * Connect netproc to the Citadel server running on this computer.
1408 void np_attach_to_server(void)
1413 {"netproc", "localhost", NULL, NULL};
1415 syslog(LOG_NOTICE, "Attaching to server...");
1416 sprintf(portname, "%d", config.c_port_number);
1418 attach_to_server(3, args);
1420 syslog(LOG_NOTICE, "%s", &buf[4]);
1421 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1424 syslog(LOG_NOTICE, "%s", &buf[4]);
1425 if (buf[0] != '2') {
1435 int main(int argc, char **argv)
1440 int import_only = 0; /* if set to 1, don't export anything */
1442 openlog("netproc", LOG_PID, LOG_USER);
1443 strcpy(bbs_home_directory, BBSDIR);
1446 * Change directories if specified
1448 for (a = 1; a < argc; ++a) {
1449 if (!strncmp(argv[a], "-h", 2)) {
1450 strcpy(bbs_home_directory, argv[a]);
1451 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1453 } else if (!strcmp(argv[a], "-i")) {
1456 fprintf(stderr, "netproc: usage: ");
1457 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1463 syslog(LOG_DEBUG, "Calling get_config()");
1468 syslog(LOG_DEBUG, "Creating lock file");
1470 if (set_lockfile() != 0) {
1471 syslog(LOG_NOTICE, "lock file exists: already running");
1474 signal(SIGINT, cleanup);
1475 signal(SIGQUIT, cleanup);
1476 signal(SIGHUP, cleanup);
1477 signal(SIGTERM, cleanup);
1479 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1481 np_attach_to_server();
1484 if (load_syslist() != 0)
1485 syslog(LOG_ERR, "cannot load sysinfo");
1486 setup_special_nodes();
1488 /* Open the use table */
1489 use_table = gdbm_open("./data/usetable.gdbm", 512,
1490 GDBM_WRCREAT, 0600, 0);
1491 if (use_table == NULL) {
1492 syslog(LOG_ERR, "could not open use table: %s",
1496 /* first collect incoming stuff */
1499 /* Now process outbound messages, but NOT if this is just a
1500 * quick import-only run (i.e. the -i command-line option
1503 if (import_only != 1) {
1504 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1505 if (allfp != NULL) {
1506 while (fgets(allst, 32, allfp) != NULL) {
1507 allst[strlen(allst) - 1] = 0;
1512 /* import again in case anything new was generated */
1516 /* Update mail.sysinfo with new information we learned */
1519 /* Close the use table */
1520 purge_use_table(use_table);
1521 gdbm_close(use_table);
1523 syslog(LOG_NOTICE, "processing ended.");