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 /* Files used by the networker */
20 #define MAILSYSINFO "./network/mail.sysinfo"
22 /* Uncomment the DEBUG def to see noisy traces */
29 #include <sys/types.h>
45 /* A list of users you wish to filter out of incoming traffic can be kept
46 * in ./network/filterlist -- messages from these users will be automatically
47 * moved to FILTERROOM. Normally this will be the same as TWITROOM (the
48 * room problem user messages are moved to) but you can override this by
49 * specifying a different room name here.
52 #define FILTERROOM TWITROOM
58 char m_rmname[ROOMNAMELEN];
63 char rm_name[ROOMNAMELEN];
68 struct filterlist *next;
86 void attach_to_server(int argc, char **argv);
87 void serv_read(char *buf, int bytes);
88 void serv_write(char *buf, int nbytes);
89 void get_config(void);
91 struct filterlist *filter = NULL;
92 struct syslist *slist = NULL;
95 extern char bbs_home_directory[];
96 extern int home_specified;
101 #ifndef HAVE_STRERROR
103 * replacement strerror() for systems that don't have it
105 char *strerror(int e)
109 sprintf(buf, "errno = %d", e);
115 void strip_trailing_whitespace(char *buf)
117 while (isspace(buf[strlen(buf) - 1]))
118 buf[strlen(buf) - 1] = 0;
123 * we also load the mail.sysinfo table into memory, make changes
124 * as we learn more about the network from incoming messages, and write
125 * the table back to disk when we're done.
127 int load_syslist(void)
130 struct syslist *stemp;
134 fp = fopen(MAILSYSINFO, "r");
139 if (fgets(buf, 128, fp) == NULL) {
143 buf[strlen(buf) - 1] = 0;
144 while (isspace(buf[0]))
145 strcpy(buf, &buf[1]);
148 if ((insys == 0) && (strlen(buf) != 0)) {
150 stemp = (struct syslist *) malloc(sizeof(struct syslist));
153 strcpy(slist->s_name, buf);
154 strcpy(slist->s_type, "bin");
155 strcpy(slist->s_nexthop, "Mail");
156 slist->s_lastcontact = 0L;
157 strcpy(slist->s_humannode, "");
158 strcpy(slist->s_phonenum, "");
159 strcpy(slist->s_gdom, "");
160 } else if ((insys == 1) && (strlen(buf) == 0)) {
162 } else if ((insys == 1) && (!strncasecmp(buf, "bin", 3))) {
163 strcpy(slist->s_type, "bin");
164 strcpy(slist->s_nexthop, &buf[4]);
165 } else if ((insys == 1) && (!strncasecmp(buf, "use", 3))) {
166 strcpy(slist->s_type, "use");
167 strcpy(slist->s_nexthop, &buf[4]);
168 } else if ((insys == 1) && (!strncasecmp(buf, "uum", 3))) {
169 strcpy(slist->s_type, "uum");
170 strcpy(slist->s_nexthop, &buf[4]);
171 } else if ((insys == 1) && (!strncasecmp(buf, "lastcontact", 11))) {
173 sscanf(&buf[12], "%ld", &foo);
174 slist->s_lastcontact = foo;
175 } else if ((insys == 1) && (!strncasecmp(buf, "humannode", 9))) {
176 strcpy(slist->s_humannode, &buf[10]);
177 } else if ((insys == 1) && (!strncasecmp(buf, "phonenum", 8))) {
178 strcpy(slist->s_phonenum, &buf[9]);
179 } else if ((insys == 1) && (!strncasecmp(buf, "gdom", 4))) {
180 strcpy(slist->s_gdom, &buf[5]);
185 /* now we have to set up two "special" nodes on the list: one
186 * for the local node, and one for an Internet gateway
188 void setup_special_nodes(void)
190 struct syslist *stemp, *slocal;
193 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
194 if (!strcasecmp(stemp->s_name, config.c_nodename))
197 if (slocal == NULL) {
198 slocal = (struct syslist *) malloc(sizeof(struct syslist));
199 slocal->next = slist;
202 strcpy(slocal->s_name, config.c_nodename);
203 strcpy(slocal->s_type, "bin");
204 strcpy(slocal->s_nexthop, "Mail");
205 time(&slocal->s_lastcontact);
206 strcpy(slocal->s_humannode, config.c_humannode);
207 strcpy(slocal->s_phonenum, config.c_phonenum);
210 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
211 if (!strcasecmp(stemp->s_name, "internet"))
214 if (slocal == NULL) {
215 slocal = (struct syslist *) malloc(sizeof(struct syslist));
216 slocal->next = slist;
219 strcpy(slocal->s_name, "internet");
220 strcpy(slocal->s_type, "uum");
221 strcpy(slocal->s_nexthop, "%s");
222 time(&slocal->s_lastcontact);
223 strcpy(slocal->s_humannode, "Internet Gateway");
224 strcpy(slocal->s_phonenum, "");
225 strcpy(slocal->s_gdom, "");
230 * here's the routine to write the table back to disk.
232 void rewrite_syslist(void)
234 struct syslist *stemp;
239 newfp = fopen(MAILSYSINFO, "w");
240 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
241 if (!strcasecmp(stemp->s_name, config.c_nodename)) {
242 time(&stemp->s_lastcontact);
243 strcpy(stemp->s_type, "bin");
244 strcpy(stemp->s_humannode, config.c_humannode);
245 strcpy(stemp->s_phonenum, config.c_phonenum);
247 /* remove systems we haven't heard from in a while */
248 if ((stemp->s_lastcontact == 0L)
249 || (now - stemp->s_lastcontact < EXPIRY_TIME)) {
250 fprintf(newfp, "%s\n%s %s\n",
251 stemp->s_name, stemp->s_type, stemp->s_nexthop);
252 if (strlen(stemp->s_phonenum) > 0)
253 fprintf(newfp, "phonenum %s\n", stemp->s_phonenum);
254 if (strlen(stemp->s_gdom) > 0)
255 fprintf(newfp, "gdom %s\n", stemp->s_gdom);
256 if (strlen(stemp->s_humannode) > 0)
257 fprintf(newfp, "humannode %s\n", stemp->s_humannode);
258 if (stemp->s_lastcontact > 0L)
259 fprintf(newfp, "lastcontact %ld %s",
260 (long) stemp->s_lastcontact,
261 asctime(localtime(&stemp->s_lastcontact)));
262 fprintf(newfp, "\n");
266 /* now free the list */
267 while (slist != NULL) {
275 /* call this function with the node name of a system and it returns a pointer
276 * to its syslist structure.
278 struct syslist *get_sys_ptr(char *sysname)
280 static char sysnambuf[16];
281 static struct syslist *sysptrbuf = NULL;
282 struct syslist *stemp;
284 if ((!strcmp(sysname, sysnambuf))
285 && (sysptrbuf != NULL))
288 strcpy(sysnambuf, sysname);
289 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
290 if (!strcmp(sysname, stemp->s_name)) {
301 * make sure only one copy of netproc runs at a time, using lock files
303 int set_lockfile(void)
308 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
309 fscanf(lfp, "%d", &onppid);
311 if (!kill(onppid, 0) || errno == EPERM)
314 lfp = fopen(LOCKFILE, "w");
316 syslog(LOG_NOTICE, "Cannot create %s: %s", LOCKFILE,
320 fprintf(lfp, "%ld\n", (long) getpid());
325 void remove_lockfile(void)
331 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
332 * cleanup() . If for some reason netproc hangs waiting for the server
333 * to clean up, the alarm clock goes off and the program exits anyway.
334 * The cleanup() routine makes a check to ensure it's not reentering, in
335 * case the ipc module looped it somehow.
337 void nq_cleanup(int e)
346 static int nested = 0;
349 signal(SIGALRM, nq_cleanup);
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)
371 struct filterlist *fbuf;
374 fp = fopen("./network/filterlist", "r");
377 while (fgets(sbuf, 256, fp) != NULL) {
378 if (sbuf[0] != '#') {
379 sbuf[strlen(sbuf) - 1] = 0;
380 fbuf = (struct filterlist *)
381 malloc((long) sizeof(struct filterlist));
384 strcpy(fbuf->f_person, "*");
385 strcpy(fbuf->f_room, "*");
386 strcpy(fbuf->f_system, "*");
388 for (a = strlen(sbuf); a >= 0; --a)
393 strcpy(fbuf->f_person, sbuf);
394 strcpy(sbuf, &sbuf[p + 1]);
396 for (a = strlen(sbuf); a >= 0; --a)
401 strcpy(fbuf->f_room, sbuf);
402 strcpy(sbuf, &sbuf[p + 1]);
404 strcpy(fbuf->f_system, sbuf);
410 /* returns 1 if user/message/room combination is in the kill file */
411 int is_banned(char *k_person, char *k_room, char *k_system)
413 struct filterlist *fptr;
415 for (fptr = filter; fptr != NULL; fptr = fptr->next)
417 ((!strcasecmp(fptr->f_person, k_person)) || (!strcmp(fptr->f_person, "*")))
419 ((!strcasecmp(fptr->f_room, k_room)) || (!strcmp(fptr->f_room, "*")))
421 ((!strcasecmp(fptr->f_system, k_system)) || (!strcmp(fptr->f_system, "*")))
429 * Determine routing from sysinfo file
431 int get_sysinfo_type(char *name) {
432 struct syslist *stemp;
434 GETSN: for (stemp = slist; stemp != NULL; stemp = stemp->next) {
435 if (!strcasecmp(stemp->s_name, name)) {
436 if (!strcasecmp(stemp->s_type, "use")) {
437 strcpy(name, stemp->s_nexthop);
440 if (!strcasecmp(stemp->s_type, "bin")) {
443 if (!strcasecmp(stemp->s_type, "uum")) {
444 return (MES_INTERNET);
448 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
453 void fpgetfield(FILE * fp, char *string)
473 * Load all of the fields of a message, except the actual text, into a
474 * table in memory (so we know how to process the message).
476 void fpmsgfind(FILE *fp, struct minfo *buffer)
478 int b, e, mtype, aflag;
485 syslog(LOG_ERR, "Magic number check failed for this message");
489 memset(buffer, 0, sizeof(struct minfo));
493 BONFGM: b = getc(fp);
499 while ((bbb[0] == ' ') && (strlen(bbb) > 1))
500 strcpy(bbb, &bbb[1]);
502 strcpy(buffer->A, bbb);
503 if (strlen(userid) == 0) {
505 for (e = 0; e < strlen(userid); ++e)
506 if (userid[e] == ' ')
511 strcpy(buffer->O, bbb);
513 strcpy(buffer->C, bbb);
515 strcpy(buffer->N, bbb);
517 strcpy(buffer->S, bbb);
519 /* extract the user id from the path */
520 for (e = 0; e < strlen(bbb); ++e)
522 strcpy(userid, &bbb[e + 1]);
524 /* now find the next hop */
525 for (e = 0; e < strlen(bbb); ++e)
528 strcpy(buffer->nexthop, bbb);
531 for (e = 0; e < strlen(bbb); ++e)
534 strcpy(buffer->R, bbb);
537 strcpy(buffer->D, bbb);
539 buffer->T = atol(bbb);
541 buffer->I = atol(bbb);
543 strcpy(buffer->H, bbb);
545 strcpy(buffer->B, bbb);
547 strcpy(buffer->G, bbb);
549 strcpy(buffer->E, bbb);
551 strcpy(buffer->Z, 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);
821 * process incoming files in ./network/spoolin
825 FILE *fp, *message, *testfp, *ls, *duplist;
826 static struct minfo minfo;
827 struct recentmsg recentmsg;
828 char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
831 struct syslist *stemp;
838 /* temp file names */
839 sprintf(tname, tmpnam(NULL));
840 sprintf(iname, tmpnam(NULL));
844 /* Make sure we're in the right directory */
845 chdir(bbs_home_directory);
847 /* temporary file to contain a log of rejected dups */
850 /* Let the shell do the dirty work. Get all data from spoolin */
852 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
853 ls = popen(aaa, "r");
855 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
859 SKIP: ptr = fgets(sfilename, sizeof sfilename, ls);
863 "Trying %s", sfilename);
865 sfilename[strlen(sfilename) - 1] = 0;
866 if (!strcmp(sfilename, ".")) goto SKIP;
867 if (!strcmp(sfilename, "..")) goto SKIP;
868 if (!strcmp(sfilename, "CVS")) goto SKIP;
871 } while (ptr != NULL);
872 PROCESS_IT: pclose(ls);
875 sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
876 syslog(LOG_NOTICE, "processing <%s>", pfilename);
878 fp = fopen(pfilename, "rb");
880 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
881 fp = fopen("/dev/null", "rb");
883 NXMSG: /* Seek to the beginning of the next message */
886 } while ((a != 255) && (a >= 0));
890 /* This crates the temporary file. */
892 message = fopen(tname, "wb");
893 if (message == NULL) {
894 syslog(LOG_ERR, "error creating %s: %s",
895 tname, strerror(errno));
898 putc(255, message); /* 0xFF (start-of-message) */
900 putc(a, message); /* type */
902 putc(a, message); /* mode */
904 FieldID = getc(fp); /* Header field ID */
905 if (isalpha(FieldID)) {
906 putc(FieldID, message);
909 if (a < 127) putc(a, message);
911 if (a != 0) putc(0, message);
913 else { /* Invalid field ID; flush it */
919 } while ((FieldID != 'M') && (a >= 0));
920 /* M is always last */
921 if (FieldID != 'M') valid_msg = 0;
923 msglen = ftell(message);
931 /* process the individual mesage */
932 msgfind(tname, &minfo);
933 strncpy(recentmsg.RMnodename, minfo.N, 9);
934 recentmsg.RMnodename[9] = 0;
935 recentmsg.RMnum = minfo.I;
936 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
937 minfo.I, minfo.A, minfo.O, minfo.N);
938 if (strlen(minfo.R) > 0) {
939 syslog(LOG_NOTICE, " to <%s>", minfo.R);
940 if (strlen(minfo.D) > 0) {
941 syslog(LOG_NOTICE, " @ <%s>",
945 if (!strcasecmp(minfo.D, FQDN))
946 strcpy(minfo.D, NODENAME);
948 /* this routine updates our info on the system that sent the message */
949 stemp = get_sys_ptr(minfo.N);
950 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
951 /* add non-neighbor system to map */
952 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
954 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
957 strcpy(slist->s_name, minfo.N);
958 strcpy(slist->s_type, "use");
959 strcpy(slist->s_nexthop, minfo.nexthop);
960 time(&slist->s_lastcontact);
961 } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
962 /* add neighbor system to map */
963 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
965 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
966 testfp = fopen(aaa, "r");
967 if (testfp != NULL) {
969 stemp = (struct syslist *)
970 malloc((long) sizeof(struct syslist));
973 strcpy(slist->s_name, minfo.N);
974 strcpy(slist->s_type, "bin");
975 strcpy(slist->s_nexthop, "Mail");
976 time(&slist->s_lastcontact);
979 /* now update last contact and long node name if we can */
981 time(&stemp->s_lastcontact);
982 if (strlen(minfo.H) > 0)
983 strcpy(stemp->s_humannode, minfo.H);
984 if (strlen(minfo.B) > 0)
985 strcpy(stemp->s_phonenum, minfo.B);
986 if (strlen(minfo.G) > 0)
987 strcpy(stemp->s_gdom, minfo.G);
990 /* Check the use table; reject message if it's been here before */
991 if (already_received(use_table, &minfo)) {
992 syslog(LOG_NOTICE, "rejected duplicate message");
993 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
994 minfo.I, minfo.A, minfo.O, minfo.N);
998 /* route the message if necessary */
999 else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
1000 a = get_sysinfo_type(minfo.D);
1001 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
1003 if (a == MES_INTERNET) {
1005 syslog(LOG_NOTICE, "netmailer %s", tname);
1007 execlp("./netmailer", "netmailer",
1009 syslog(LOG_ERR, "error running netmailer: %s",
1013 while (wait(&b) != (-1));
1014 } else if (a == MES_BINARY) {
1015 ship_to(tname, minfo.D);
1017 /* message falls into the bit bucket? */
1021 /* check to see if it's a file transfer */
1022 else if (!strncasecmp(minfo.S, "FILE", 4)) {
1023 proc_file_transfer(tname);
1026 /* otherwise process it as a normal message */
1028 if (!strcasecmp(minfo.R, "postmaster")) {
1029 strcpy(minfo.R, "");
1030 strcpy(minfo.C, "Aide");
1032 if (strlen(minfo.R) > 0) {
1033 sprintf(buf, "GOTO _MAIL_");
1035 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1036 sprintf(buf, "GOTO %s", FILTERROOM);
1038 if (strlen(minfo.C) > 0) {
1039 sprintf(buf, "GOTO %s", minfo.C);
1041 sprintf(buf, "GOTO %s", minfo.O);
1046 if (buf[0] != '2') {
1047 syslog(LOG_ERR, "%s", buf);
1048 sprintf(buf, "GOTO _BITBUCKET_");
1052 /* Open the temporary file containing the message */
1053 message = fopen(tname, "rb");
1054 if (message == NULL) {
1055 syslog(LOG_ERR, "cannot open %s: %s",
1056 tname, strerror(errno));
1060 /* Transmit the message to the server */
1061 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1064 if (!strncmp(buf, "570", 3)) {
1065 /* no such user, do a bounce */
1068 if (buf[0] == '7') {
1069 /* Always use the server's idea of the message length,
1070 * even though they should both be identical */
1071 msglen = atol(&buf[4]);
1072 while (msglen > 0L) {
1073 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1074 if (fread(buf, bloklen, 1, message) < 1) {
1076 "error trying to read %d bytes: %s",
1077 bloklen, strerror(errno));
1079 serv_write(buf, bloklen);
1080 msglen = msglen - (long) bloklen;
1085 syslog(LOG_ERR, "%s", buf);
1098 } while (ptr != NULL);
1103 * If dups were rejected, post a message saying so
1105 if (ftell(duplist)!=0L) {
1106 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1108 fprintf(fp, "%cA%c", 255, 1);
1109 fprintf(fp, "T%ld%c", time(NULL), 0);
1110 fprintf(fp, "ACitadel%c", 0);
1111 fprintf(fp, "OAide%cM", 0);
1112 fprintf(fp, "The following duplicate messages"
1113 " were rejected:\n \n");
1115 while (fgets(buf, sizeof(buf), duplist) != NULL) {
1116 buf[strlen(buf)-1] = 0;
1117 fprintf(fp, " %s\n", buf);
1119 fprintf(fp, "%c", 0);
1129 /* Checks to see whether its ok to send */
1130 /* Returns 1 for ok, send message */
1131 /* Returns 0 if message already there */
1132 int checkpath(char *path, char *sys)
1140 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1142 for (a = 0; a < strlen(path); ++a) {
1143 if (!strncmp(&path[a], sys2, strlen(sys2)))
1150 * Implement split horizon algorithm (prevent infinite spooling loops
1151 * by refusing to send any node a message which already contains its
1152 * nodename in the path).
1154 int ismsgok(FILE *mmfp, char *sysname)
1157 int ok = 0; /* fail safe - no path, don't send it */
1161 if (getc(mmfp) != 255)
1166 while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1167 fpgetfield(mmfp, fbuf);
1169 ok = checkpath(fbuf, sysname);
1173 syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1180 /* spool list of messages to a file */
1181 /* returns # of msgs spooled */
1182 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1184 struct msglist *cmptr;
1188 int msgs_spooled = 0;
1191 static struct minfo minfo;
1196 strcpy(curr_rm, "");
1198 /* for each message in the list... */
1199 for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1201 /* make sure we're in the correct room... */
1202 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1203 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1206 if (buf[0] == '2') {
1207 strcpy(curr_rm, cmptr->m_rmname);
1209 syslog(LOG_ERR, "%s", buf);
1212 /* download the message from the server... */
1214 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1217 if (buf[0] == '6') { /* read the msg */
1218 msg_len = atol(&buf[4]);
1219 while (msg_len > 0L) {
1220 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1221 serv_read(buf, blok_len);
1222 fwrite(buf, blok_len, 1, mmfp);
1223 msg_len = msg_len - (long) blok_len;
1225 } else { /* or print the err */
1226 syslog(LOG_ERR, "%s", buf);
1231 if (ismsgok(mmfp, sysname)) {
1235 fread(fbuf, 3, 1, mmfp);
1236 fwrite(fbuf, 3, 1, destfp);
1237 while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1240 fpgetfield(mmfp, fbuf);
1242 fprintf(destfp, "%s!", NODENAME);
1244 fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1247 fprintf(destfp, "C%s%c",
1248 cmptr->m_rmname, 0);
1256 /* Get this message into the use table, so we can reject it
1257 * if a misconfigured remote system sends it back to us.
1260 fpmsgfind(mmfp, &minfo);
1261 already_received(use_table, &minfo);
1267 return (msgs_spooled);
1270 void outprocess(char *sysname)
1271 { /* send new room messages to sysname */
1274 char shiptocmd[128];
1278 struct msglist *cmlist = NULL;
1279 struct rmlist *crmlist = NULL;
1280 struct rmlist *rmptr, *rmptr2;
1281 struct msglist *cmptr, *cmptr2;
1282 FILE *sysflfp, *tempflfp;
1286 sprintf(tempflnm, tmpnam(NULL));
1287 tempflfp = fopen(tempflnm, "w");
1288 if (tempflfp == NULL)
1293 * Read system file for node in question and put together room list
1295 sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1296 sysflfp = fopen(sysflnm, "r");
1297 if (sysflfp == NULL)
1299 fgets(shiptocmd, 128, sysflfp);
1300 shiptocmd[strlen(shiptocmd) - 1] = 0;
1301 while (!feof(sysflfp)) {
1302 if (fgets(srmname, 32, sysflfp) == NULL)
1304 srmname[strlen(srmname) - 1] = 0;
1305 fgets(lbuf, 32, sysflfp);
1306 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1308 strcpy(rmptr->rm_name, srmname);
1309 strip_trailing_whitespace(rmptr->rm_name);
1310 rmptr->rm_lastsent = atol(lbuf);
1311 if (crmlist == NULL)
1313 else if (!strcasecmp(rmptr->rm_name, "control")) {
1314 /* control has to be first in room list */
1315 rmptr->next = crmlist;
1319 while (rmptr2->next != NULL)
1320 rmptr2 = rmptr2->next;
1321 rmptr2->next = rmptr;
1327 * Assemble list of messages to be spooled
1329 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1331 sprintf(buf, "GOTO %s", rmptr->rm_name);
1334 if (buf[0] != '2') {
1335 syslog(LOG_ERR, "%s", buf);
1337 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1341 while (serv_gets(buf), strcmp(buf, "000")) {
1342 thismsg = atol(buf);
1343 if (thismsg > (rmptr->rm_lastsent)) {
1344 rmptr->rm_lastsent = thismsg;
1346 cmptr = (struct msglist *)
1347 malloc(sizeof(struct msglist));
1349 cmptr->m_num = thismsg;
1350 strcpy(cmptr->m_rmname, rmptr->rm_name);
1356 while (cmptr2->next != NULL)
1357 cmptr2 = cmptr2->next;
1358 cmptr2->next = cmptr;
1361 } else { /* print error from "msgs all" */
1362 syslog(LOG_ERR, "%s", buf);
1368 cmptr2 = cmlist; /* this loop counts the messages */
1369 while (cmptr2 != NULL) {
1371 cmptr2 = cmptr2->next;
1373 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1374 outgoing_msgs, sysname);
1377 * Spool out the messages, but only if there are any.
1379 if (outgoing_msgs != 0)
1380 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1381 syslog(LOG_NOTICE, "%d messages actually spooled",
1385 * Deallocate list of spooled messages.
1387 while (cmlist != NULL) {
1388 cmptr = cmlist->next;
1394 * Rewrite system file and deallocate room list.
1396 syslog(LOG_NOTICE, "Spooling...");
1397 sysflfp = fopen(sysflnm, "w");
1398 fprintf(sysflfp, "%s\n", shiptocmd);
1399 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1400 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1402 while (crmlist != NULL) {
1403 rmptr = crmlist->next;
1409 * Close temporary file, ship it out, and return
1412 if (outgoing_msgs != 0)
1413 ship_to(tempflnm, sysname);
1419 * Connect netproc to the Citadel server running on this computer.
1421 void np_attach_to_server(void)
1426 {"netproc", "localhost", NULL, NULL};
1428 syslog(LOG_NOTICE, "Attaching to server...");
1429 sprintf(portname, "%d", config.c_port_number);
1431 attach_to_server(3, args);
1433 syslog(LOG_NOTICE, "%s", &buf[4]);
1434 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1437 syslog(LOG_NOTICE, "%s", &buf[4]);
1438 if (buf[0] != '2') {
1448 int main(int argc, char **argv)
1453 int import_only = 0; /* if set to 1, don't export anything */
1455 openlog("netproc", LOG_PID, LOG_USER);
1456 strcpy(bbs_home_directory, BBSDIR);
1459 * Change directories if specified
1461 for (a = 1; a < argc; ++a) {
1462 if (!strncmp(argv[a], "-h", 2)) {
1463 strcpy(bbs_home_directory, argv[a]);
1464 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1466 } else if (!strcmp(argv[a], "-i")) {
1469 fprintf(stderr, "netproc: usage: ");
1470 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1476 syslog(LOG_DEBUG, "Calling get_config()");
1481 syslog(LOG_DEBUG, "Creating lock file");
1483 if (set_lockfile() != 0) {
1484 syslog(LOG_NOTICE, "lock file exists: already running");
1487 signal(SIGINT, cleanup);
1488 signal(SIGQUIT, cleanup);
1489 signal(SIGHUP, cleanup);
1490 signal(SIGTERM, cleanup);
1492 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1494 np_attach_to_server();
1497 if (load_syslist() != 0)
1498 syslog(LOG_ERR, "cannot load sysinfo");
1499 setup_special_nodes();
1501 /* Open the use table */
1502 use_table = gdbm_open("./data/usetable.gdbm", 512,
1503 GDBM_WRCREAT, 0600, 0);
1504 if (use_table == NULL) {
1505 syslog(LOG_ERR, "could not open use table: %s",
1509 /* first collect incoming stuff */
1512 /* Now process outbound messages, but NOT if this is just a
1513 * quick import-only run (i.e. the -i command-line option
1516 if (import_only != 1) {
1517 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1518 if (allfp != NULL) {
1519 while (fgets(allst, 32, allfp) != NULL) {
1520 allst[strlen(allst) - 1] = 0;
1525 /* import again in case anything new was generated */
1529 /* Update mail.sysinfo with new information we learned */
1532 /* Close the use table */
1533 purge_use_table(use_table);
1534 gdbm_close(use_table);
1536 syslog(LOG_NOTICE, "processing ended.");