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;
93 struct msglist *purgelist = NULL;
96 extern char bbs_home_directory[];
97 extern int home_specified;
102 #ifndef HAVE_STRERROR
104 * replacement strerror() for systems that don't have it
106 char *strerror(int e)
110 sprintf(buf, "errno = %d", e);
116 void strip_trailing_whitespace(char *buf)
118 while (isspace(buf[strlen(buf) - 1]))
119 buf[strlen(buf) - 1] = 0;
124 * we also load the mail.sysinfo table into memory, make changes
125 * as we learn more about the network from incoming messages, and write
126 * the table back to disk when we're done.
128 int load_syslist(void)
131 struct syslist *stemp;
135 fp = fopen(MAILSYSINFO, "r");
140 if (fgets(buf, 128, fp) == NULL) {
144 buf[strlen(buf) - 1] = 0;
145 while (isspace(buf[0]))
146 strcpy(buf, &buf[1]);
149 if ((insys == 0) && (strlen(buf) != 0)) {
151 stemp = (struct syslist *) malloc(sizeof(struct syslist));
154 strcpy(slist->s_name, buf);
155 strcpy(slist->s_type, "bin");
156 strcpy(slist->s_nexthop, "Mail");
157 slist->s_lastcontact = 0L;
158 strcpy(slist->s_humannode, "");
159 strcpy(slist->s_phonenum, "");
160 strcpy(slist->s_gdom, "");
161 } else if ((insys == 1) && (strlen(buf) == 0)) {
163 } else if ((insys == 1) && (!strncasecmp(buf, "bin", 3))) {
164 strcpy(slist->s_type, "bin");
165 strcpy(slist->s_nexthop, &buf[4]);
166 } else if ((insys == 1) && (!strncasecmp(buf, "use", 3))) {
167 strcpy(slist->s_type, "use");
168 strcpy(slist->s_nexthop, &buf[4]);
169 } else if ((insys == 1) && (!strncasecmp(buf, "uum", 3))) {
170 strcpy(slist->s_type, "uum");
171 strcpy(slist->s_nexthop, &buf[4]);
172 } else if ((insys == 1) && (!strncasecmp(buf, "lastcontact", 11))) {
174 sscanf(&buf[12], "%ld", &foo);
175 slist->s_lastcontact = foo;
176 } else if ((insys == 1) && (!strncasecmp(buf, "humannode", 9))) {
177 strcpy(slist->s_humannode, &buf[10]);
178 } else if ((insys == 1) && (!strncasecmp(buf, "phonenum", 8))) {
179 strcpy(slist->s_phonenum, &buf[9]);
180 } else if ((insys == 1) && (!strncasecmp(buf, "gdom", 4))) {
181 strcpy(slist->s_gdom, &buf[5]);
186 /* now we have to set up two "special" nodes on the list: one
187 * for the local node, and one for an Internet gateway
189 void setup_special_nodes(void)
191 struct syslist *stemp, *slocal;
194 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
195 if (!strcasecmp(stemp->s_name, config.c_nodename))
198 if (slocal == NULL) {
199 slocal = (struct syslist *) malloc(sizeof(struct syslist));
200 slocal->next = slist;
203 strcpy(slocal->s_name, config.c_nodename);
204 strcpy(slocal->s_type, "bin");
205 strcpy(slocal->s_nexthop, "Mail");
206 time(&slocal->s_lastcontact);
207 strcpy(slocal->s_humannode, config.c_humannode);
208 strcpy(slocal->s_phonenum, config.c_phonenum);
211 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
212 if (!strcasecmp(stemp->s_name, "internet"))
215 if (slocal == NULL) {
216 slocal = (struct syslist *) malloc(sizeof(struct syslist));
217 slocal->next = slist;
220 strcpy(slocal->s_name, "internet");
221 strcpy(slocal->s_type, "uum");
222 strcpy(slocal->s_nexthop, "%s");
223 time(&slocal->s_lastcontact);
224 strcpy(slocal->s_humannode, "Internet Gateway");
225 strcpy(slocal->s_phonenum, "");
226 strcpy(slocal->s_gdom, "");
231 * here's the routine to write the table back to disk.
233 void rewrite_syslist(void)
235 struct syslist *stemp;
240 newfp = fopen(MAILSYSINFO, "w");
241 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
242 if (!strcasecmp(stemp->s_name, config.c_nodename)) {
243 time(&stemp->s_lastcontact);
244 strcpy(stemp->s_type, "bin");
245 strcpy(stemp->s_humannode, config.c_humannode);
246 strcpy(stemp->s_phonenum, config.c_phonenum);
248 /* remove systems we haven't heard from in a while */
249 if ((stemp->s_lastcontact == 0L)
250 || (now - stemp->s_lastcontact < EXPIRY_TIME)) {
251 fprintf(newfp, "%s\n%s %s\n",
252 stemp->s_name, stemp->s_type, stemp->s_nexthop);
253 if (strlen(stemp->s_phonenum) > 0)
254 fprintf(newfp, "phonenum %s\n", stemp->s_phonenum);
255 if (strlen(stemp->s_gdom) > 0)
256 fprintf(newfp, "gdom %s\n", stemp->s_gdom);
257 if (strlen(stemp->s_humannode) > 0)
258 fprintf(newfp, "humannode %s\n", stemp->s_humannode);
259 if (stemp->s_lastcontact > 0L)
260 fprintf(newfp, "lastcontact %ld %s",
261 (long) stemp->s_lastcontact,
262 asctime(localtime(&stemp->s_lastcontact)));
263 fprintf(newfp, "\n");
267 /* now free the list */
268 while (slist != NULL) {
276 /* call this function with the node name of a system and it returns a pointer
277 * to its syslist structure.
279 struct syslist *get_sys_ptr(char *sysname)
281 static char sysnambuf[16];
282 static struct syslist *sysptrbuf = NULL;
283 struct syslist *stemp;
285 if ((!strcmp(sysname, sysnambuf))
286 && (sysptrbuf != NULL))
289 strcpy(sysnambuf, sysname);
290 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
291 if (!strcmp(sysname, stemp->s_name)) {
302 * make sure only one copy of netproc runs at a time, using lock files
304 int set_lockfile(void)
309 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
310 fscanf(lfp, "%d", &onppid);
312 if (!kill(onppid, 0) || errno == EPERM)
315 lfp = fopen(LOCKFILE, "w");
317 syslog(LOG_NOTICE, "Cannot create %s: %s", LOCKFILE,
321 fprintf(lfp, "%ld\n", (long) getpid());
326 void remove_lockfile(void)
332 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
333 * cleanup() . If for some reason netproc hangs waiting for the server
334 * to clean up, the alarm clock goes off and the program exits anyway.
335 * The cleanup() routine makes a check to ensure it's not reentering, in
336 * case the ipc module looped it somehow.
338 void nq_cleanup(int e)
347 static int nested = 0;
350 signal(SIGALRM, nq_cleanup);
357 * This is implemented as a function rather than as a macro because the
358 * client-side IPC modules expect logoff() to be defined. They call logoff()
359 * when a problem connecting or staying connected to the server occurs.
367 * If there is a kill file in place, this function will process it.
369 void load_filterlist(void)
372 struct filterlist *fbuf;
375 fp = fopen("./network/filterlist", "r");
378 while (fgets(sbuf, 256, fp) != NULL) {
379 if (sbuf[0] != '#') {
380 sbuf[strlen(sbuf) - 1] = 0;
381 fbuf = (struct filterlist *)
382 malloc((long) sizeof(struct filterlist));
385 strcpy(fbuf->f_person, "*");
386 strcpy(fbuf->f_room, "*");
387 strcpy(fbuf->f_system, "*");
389 for (a = strlen(sbuf); a >= 0; --a)
394 strcpy(fbuf->f_person, sbuf);
395 strcpy(sbuf, &sbuf[p + 1]);
397 for (a = strlen(sbuf); a >= 0; --a)
402 strcpy(fbuf->f_room, sbuf);
403 strcpy(sbuf, &sbuf[p + 1]);
405 strcpy(fbuf->f_system, sbuf);
411 /* returns 1 if user/message/room combination is in the kill file */
412 int is_banned(char *k_person, char *k_room, char *k_system)
414 struct filterlist *fptr;
416 for (fptr = filter; fptr != NULL; fptr = fptr->next)
418 ((!strcasecmp(fptr->f_person, k_person)) || (!strcmp(fptr->f_person, "*")))
420 ((!strcasecmp(fptr->f_room, k_room)) || (!strcmp(fptr->f_room, "*")))
422 ((!strcasecmp(fptr->f_system, k_system)) || (!strcmp(fptr->f_system, "*")))
430 * Determine routing from sysinfo file
432 int get_sysinfo_type(char *name) {
433 struct syslist *stemp;
435 GETSN: for (stemp = slist; stemp != NULL; stemp = stemp->next) {
436 if (!strcasecmp(stemp->s_name, name)) {
437 if (!strcasecmp(stemp->s_type, "use")) {
438 strcpy(name, stemp->s_nexthop);
441 if (!strcasecmp(stemp->s_type, "bin")) {
444 if (!strcasecmp(stemp->s_type, "uum")) {
445 return (MES_INTERNET);
449 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
454 void fpgetfield(FILE * fp, char *string)
474 * Load all of the fields of a message, except the actual text, into a
475 * table in memory (so we know how to process the message).
477 void fpmsgfind(FILE *fp, struct minfo *buffer)
479 int b, e, mtype, aflag;
486 syslog(LOG_ERR, "Magic number check failed for this message");
490 memset(buffer, 0, sizeof(struct minfo));
494 BONFGM: b = getc(fp);
500 while ((bbb[0] == ' ') && (strlen(bbb) > 1))
501 strcpy(bbb, &bbb[1]);
503 strcpy(buffer->A, bbb);
504 if (strlen(userid) == 0) {
506 for (e = 0; e < strlen(userid); ++e)
507 if (userid[e] == ' ')
512 strcpy(buffer->O, bbb);
514 strcpy(buffer->C, bbb);
516 strcpy(buffer->N, bbb);
518 strcpy(buffer->S, bbb);
520 /* extract the user id from the path */
521 for (e = 0; e < strlen(bbb); ++e)
523 strcpy(userid, &bbb[e + 1]);
525 /* now find the next hop */
526 for (e = 0; e < strlen(bbb); ++e)
529 strcpy(buffer->nexthop, bbb);
532 for (e = 0; e < strlen(bbb); ++e)
535 strcpy(buffer->R, bbb);
538 strcpy(buffer->D, bbb);
540 buffer->T = atol(bbb);
542 buffer->I = atol(bbb);
544 strcpy(buffer->H, bbb);
546 strcpy(buffer->B, bbb);
548 strcpy(buffer->G, bbb);
550 strcpy(buffer->E, bbb);
552 strcpy(buffer->Z, bbb);
557 /* NOTE: we used to use the following two lines of code to assign
558 * the timestamp as a message-ID if there was no message-ID already
559 * in the message. We don't do this anymore because it screws up
563 buffer->I = buffer->T;
569 * msgfind() is the same as fpmsgfind() except it accepts a filename
570 * instead of a file handle.
572 void msgfind(char *msgfile, struct minfo *buffer) {
575 fp = fopen(msgfile, "rb");
577 syslog(LOG_ERR, "can't open %s: %s", msgfile, strerror(errno));
581 fpmsgfind(fp, buffer);
589 void ship_to(char *filenm, char *sysnm)
590 { /* send spool file filenm to system sysnm */
597 syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
599 sprintf(sysflnm, "./network/systems/%s", sysnm);
600 sysflfd = fopen(sysflnm, "r");
602 syslog(LOG_ERR, "cannot open %s", sysflnm);
603 fgets(commbuf1, 99, sysflfd);
604 commbuf1[strlen(commbuf1) - 1] = 0;
606 sprintf(commbuf2, commbuf1, filenm);
611 * proc_file_transfer() - handle a simple file transfer packet
614 void proc_file_transfer(char *tname)
615 { /* name of temp file containing the whole message */
617 char dest_room[ROOMNAMELEN];
618 char subdir_name[256];
622 syslog(LOG_NOTICE, "processing network file transfer...");
624 tfp = fopen(tname, "rb");
626 syslog(LOG_ERR, "cannot open %s", tname);
633 fpgetfield(tfp, buf);
635 strcpy(dest_room, buf);
638 } while ((a != 'M') && (a >= 0));
641 syslog(LOG_ERR, "no message text for file transfer");
644 strcpy(subdir_name, "---xxx---");
645 sprintf(buf, "GOTO %s", dest_room);
649 extract(subdir_name, &buf[4], 2);
650 if (strlen(subdir_name) == 0)
651 strcpy(subdir_name, "--xxx--");
653 /* Change to the room's directory; if that fails, change to the
654 * bitbucket directory. Then run uudecode.
656 sprintf(buf, "(cd %s/files/%s || cd %s/files/%s ) ; exec %s",
657 bbs_home_directory, subdir_name,
658 bbs_home_directory, config.c_bucket_dir,
661 uud = (FILE *) popen(buf, "w");
663 syslog(LOG_ERR, "cannot open uudecode pipe");
667 fgets(buf, 128, tfp);
668 buf[strlen(buf) - 1] = 0;
669 for (a = 0; a < strlen(buf); ++a)
672 fprintf(uud, "%s\n", buf);
673 printf("netproc: %s\n", buf);
674 while (a = getc(tfp), a > 0)
682 /* send a bounce message */
683 void bounce(struct minfo *bminfo)
691 sprintf(bfilename, "./network/spoolin/bounce.%ld.%d", (long) getpid(),
693 bounce = fopen(bfilename, "wb");
696 fprintf(bounce, "%c%c%c", 0xFF, MES_NORMAL, 0);
697 fprintf(bounce, "Ppostmaster%c", 0);
698 fprintf(bounce, "T%ld%c", (long) now, 0);
699 fprintf(bounce, "APostmaster%c", 0);
700 fprintf(bounce, "OMail%c", 0);
701 fprintf(bounce, "N%s%c", config.c_nodename, 0);
702 fprintf(bounce, "H%s%c", config.c_humannode, 0);
704 if (strlen(bminfo->E) > 0) {
705 fprintf(bounce, "R%s%c", bminfo->E, 0);
707 fprintf(bounce, "R%s%c", bminfo->A, 0);
710 fprintf(bounce, "D%s%c", bminfo->N, 0);
711 fprintf(bounce, "M%s could not deliver your mail to:\n",
713 fprintf(bounce, " \n %s\n \n", bminfo->R);
714 fprintf(bounce, " because there is no such user on this system.\n");
715 fprintf(bounce, " (Unsent message does *not* follow. ");
716 fprintf(bounce, "Help to conserve bandwidth.)\n%c", 0);
724 * Generate a Message-ID string for the use table
726 void strmsgid(char *buf, struct minfo *msginfo) {
729 sprintf(buf, "%ld@%s", msginfo->I, msginfo->N);
730 for (i=0; i<strlen(buf); ++i) {
731 if (isspace(buf[i])) {
732 strcpy(&buf[i], &buf[i+1]);
734 buf[i] = tolower(buf[i]);
741 * Check the use table to see if a message has been here before.
742 * Returns 1 if the message is a duplicate; otherwise, it returns
743 * 0 and the message ID is added to the use table.
745 int already_received(GDBM_FILE ut, struct minfo *msginfo) {
751 /* We can't check for dups on a zero msgid, so just pass them through */
752 if ((msginfo->I)==0L) {
756 strmsgid(buf, msginfo);
760 mkey.dsize = strlen(buf);
762 /* Set return value to 1 if message exists */
763 if (gdbm_exists(ut, mkey)) {
767 /* Write a record into the use table for this message.
768 * Replace existing records; this keeps the timestamp fresh.
770 newrec.dptr = (char *)&now;
771 newrec.dsize = sizeof(now);
772 gdbm_store(ut, mkey, newrec, GDBM_REPLACE);
780 * Purge any old entries out of the use table.
782 * Yes, you're reading this correctly: it keeps traversing the table until
783 * it manages to do a complete pass without deleting any records. Read the
784 * gdbm man page to find out why.
787 void purge_use_table(GDBM_FILE ut) {
788 datum mkey, nextkey, therec;
789 int purged_anything = 0;
790 time_t rec_timestamp, now;
796 mkey = gdbm_firstkey(ut);
797 while (mkey.dptr != NULL) {
798 therec = gdbm_fetch(ut, mkey);
799 if (therec.dptr != NULL) {
800 memcpy(&rec_timestamp, therec.dptr,
804 if ((now - rec_timestamp) > USE_TIME) {
805 gdbm_delete(ut, mkey);
810 nextkey = gdbm_nextkey(ut, mkey);
814 } while (purged_anything != 0);
822 * process incoming files in ./network/spoolin
826 FILE *fp, *message, *testfp, *ls, *duplist;
827 static struct minfo minfo;
828 struct recentmsg recentmsg;
829 char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
832 struct syslist *stemp;
839 /* temp file names */
840 sprintf(tname, tmpnam(NULL));
841 sprintf(iname, tmpnam(NULL));
845 /* Make sure we're in the right directory */
846 chdir(bbs_home_directory);
848 /* temporary file to contain a log of rejected dups */
851 /* Let the shell do the dirty work. Get all data from spoolin */
853 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
854 ls = popen(aaa, "r");
856 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
860 SKIP: ptr = fgets(sfilename, sizeof sfilename, ls);
864 "Trying %s", sfilename);
866 sfilename[strlen(sfilename) - 1] = 0;
867 if (!strcmp(sfilename, ".")) goto SKIP;
868 if (!strcmp(sfilename, "..")) goto SKIP;
869 if (!strcmp(sfilename, "CVS")) goto SKIP;
872 } while (ptr != NULL);
873 PROCESS_IT: pclose(ls);
876 sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
877 syslog(LOG_NOTICE, "processing <%s>", pfilename);
879 fp = fopen(pfilename, "rb");
881 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
882 fp = fopen("/dev/null", "rb");
884 NXMSG: /* Seek to the beginning of the next message */
887 } while ((a != 255) && (a >= 0));
891 /* This crates the temporary file. */
893 message = fopen(tname, "wb");
894 if (message == NULL) {
895 syslog(LOG_ERR, "error creating %s: %s",
896 tname, strerror(errno));
899 putc(255, message); /* 0xFF (start-of-message) */
901 putc(a, message); /* type */
903 putc(a, message); /* mode */
905 FieldID = getc(fp); /* Header field ID */
906 if (isalpha(FieldID)) {
907 putc(FieldID, message);
910 if (a < 127) putc(a, message);
912 if (a != 0) putc(0, message);
914 else { /* Invalid field ID; flush it */
920 } while ((FieldID != 'M') && (a >= 0));
921 /* M is always last */
922 if (FieldID != 'M') valid_msg = 0;
924 msglen = ftell(message);
932 /* process the individual mesage */
933 msgfind(tname, &minfo);
934 strncpy(recentmsg.RMnodename, minfo.N, 9);
935 recentmsg.RMnodename[9] = 0;
936 recentmsg.RMnum = minfo.I;
937 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
938 minfo.I, minfo.A, minfo.O, minfo.N);
939 if (strlen(minfo.R) > 0) {
940 syslog(LOG_NOTICE, " to <%s>", minfo.R);
941 if (strlen(minfo.D) > 0) {
942 syslog(LOG_NOTICE, " @ <%s>",
946 if (!strcasecmp(minfo.D, FQDN))
947 strcpy(minfo.D, NODENAME);
949 /* this routine updates our info on the system that sent the message */
950 stemp = get_sys_ptr(minfo.N);
951 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
952 /* add non-neighbor system to map */
953 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
955 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
958 strcpy(slist->s_name, minfo.N);
959 strcpy(slist->s_type, "use");
960 strcpy(slist->s_nexthop, minfo.nexthop);
961 time(&slist->s_lastcontact);
962 } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
963 /* add neighbor system to map */
964 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
966 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
967 testfp = fopen(aaa, "r");
968 if (testfp != NULL) {
970 stemp = (struct syslist *)
971 malloc((long) sizeof(struct syslist));
974 strcpy(slist->s_name, minfo.N);
975 strcpy(slist->s_type, "bin");
976 strcpy(slist->s_nexthop, "Mail");
977 time(&slist->s_lastcontact);
980 /* now update last contact and long node name if we can */
982 time(&stemp->s_lastcontact);
983 if (strlen(minfo.H) > 0)
984 strcpy(stemp->s_humannode, minfo.H);
985 if (strlen(minfo.B) > 0)
986 strcpy(stemp->s_phonenum, minfo.B);
987 if (strlen(minfo.G) > 0)
988 strcpy(stemp->s_gdom, minfo.G);
991 /* Check the use table; reject message if it's been here before */
992 if (already_received(use_table, &minfo)) {
993 syslog(LOG_NOTICE, "rejected duplicate message");
994 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
995 minfo.I, minfo.A, minfo.O, minfo.N);
999 /* route the message if necessary */
1000 else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
1001 a = get_sysinfo_type(minfo.D);
1002 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
1004 if (a == MES_INTERNET) {
1006 syslog(LOG_NOTICE, "netmailer %s", tname);
1008 execlp("./netmailer", "netmailer",
1010 syslog(LOG_ERR, "error running netmailer: %s",
1014 while (wait(&b) != (-1));
1015 } else if (a == MES_BINARY) {
1016 ship_to(tname, minfo.D);
1018 /* message falls into the bit bucket? */
1022 /* check to see if it's a file transfer */
1023 else if (!strncasecmp(minfo.S, "FILE", 4)) {
1024 proc_file_transfer(tname);
1027 /* otherwise process it as a normal message */
1029 if (!strcasecmp(minfo.R, "postmaster")) {
1030 strcpy(minfo.R, "");
1031 strcpy(minfo.C, "Aide");
1033 if (strlen(minfo.R) > 0) {
1034 sprintf(buf, "GOTO _MAIL_");
1036 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1037 sprintf(buf, "GOTO %s", FILTERROOM);
1039 if (strlen(minfo.C) > 0) {
1040 sprintf(buf, "GOTO %s", minfo.C);
1042 sprintf(buf, "GOTO %s", minfo.O);
1047 if (buf[0] != '2') {
1048 syslog(LOG_ERR, "%s", buf);
1049 sprintf(buf, "GOTO _BITBUCKET_");
1053 /* Open the temporary file containing the message */
1054 message = fopen(tname, "rb");
1055 if (message == NULL) {
1056 syslog(LOG_ERR, "cannot open %s: %s",
1057 tname, strerror(errno));
1061 /* Transmit the message to the server */
1062 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1065 if (!strncmp(buf, "570", 3)) {
1066 /* no such user, do a bounce */
1069 if (buf[0] == '7') {
1070 /* Always use the server's idea of the message length,
1071 * even though they should both be identical */
1072 msglen = atol(&buf[4]);
1073 while (msglen > 0L) {
1074 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1075 if (fread(buf, bloklen, 1, message) < 1) {
1077 "error trying to read %d bytes: %s",
1078 bloklen, strerror(errno));
1080 serv_write(buf, bloklen);
1081 msglen = msglen - (long) bloklen;
1086 syslog(LOG_ERR, "%s", buf);
1099 } while (ptr != NULL);
1104 * If dups were rejected, post a message saying so
1106 if (ftell(duplist)!=0L) {
1107 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1109 fprintf(fp, "%cA%c", 255, 1);
1110 fprintf(fp, "T%ld%c", time(NULL), 0);
1111 fprintf(fp, "ACitadel%c", 0);
1112 fprintf(fp, "OAide%cM", 0);
1113 fprintf(fp, "The following duplicate messages"
1114 " were rejected:\n \n");
1116 while (fgets(buf, sizeof(buf), duplist) != NULL) {
1117 buf[strlen(buf)-1] = 0;
1118 fprintf(fp, " %s\n", buf);
1120 fprintf(fp, "%c", 0);
1130 /* Checks to see whether its ok to send */
1131 /* Returns 1 for ok, send message */
1132 /* Returns 0 if message already there */
1133 int checkpath(char *path, char *sys)
1141 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1143 for (a = 0; a < strlen(path); ++a) {
1144 if (!strncmp(&path[a], sys2, strlen(sys2)))
1151 * Implement split horizon algorithm (prevent infinite spooling loops
1152 * by refusing to send any node a message which already contains its
1153 * nodename in the path).
1155 int ismsgok(FILE *mmfp, char *sysname)
1158 int ok = 0; /* fail safe - no path, don't send it */
1162 if (getc(mmfp) != 255)
1167 while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1168 fpgetfield(mmfp, fbuf);
1170 ok = checkpath(fbuf, sysname);
1174 syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1183 * Add a message to the list of messages to be deleted off the local server
1184 * at the end of this run.
1186 void delete_locally(long msgid, char *roomname) {
1187 struct msglist *mptr;
1189 mptr = (struct msglist *) malloc(sizeof(struct msglist));
1190 mptr->next = purgelist;
1191 mptr->m_num = msgid;
1192 strcpy(mptr->m_rmname, roomname);
1199 * Delete all messages on the purge list from the local server.
1201 void process_purgelist(void) {
1202 char curr_rm[ROOMNAMELEN];
1204 struct msglist *mptr;
1207 strcpy(curr_rm, "__nothing__");
1208 while (purgelist != NULL) {
1209 if (strcasecmp(curr_rm, purgelist->m_rmname)) {
1210 sprintf(buf, "GOTO %s", purgelist->m_rmname);
1213 if (buf[0] == '2') extract(curr_rm, &buf[4], 0);
1215 if (strcasecmp(curr_rm, purgelist->m_rmname)) {
1216 sprintf(buf, "DELE %ld", purgelist->m_num);
1220 mptr = purgelist->next;
1229 /* spool list of messages to a file */
1230 /* returns # of msgs spooled */
1231 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1233 struct msglist *cmptr;
1237 int msgs_spooled = 0;
1240 static struct minfo minfo;
1245 strcpy(curr_rm, "");
1247 /* for each message in the list... */
1248 for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1250 /* make sure we're in the correct room... */
1251 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1252 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1255 if (buf[0] == '2') {
1256 strcpy(curr_rm, cmptr->m_rmname);
1258 syslog(LOG_ERR, "%s", buf);
1261 /* download the message from the server... */
1263 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1266 if (buf[0] == '6') { /* read the msg */
1267 msg_len = atol(&buf[4]);
1268 while (msg_len > 0L) {
1269 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1270 serv_read(buf, blok_len);
1271 fwrite(buf, blok_len, 1, mmfp);
1272 msg_len = msg_len - (long) blok_len;
1274 } else { /* or print the err */
1275 syslog(LOG_ERR, "%s", buf);
1280 if (ismsgok(mmfp, sysname)) {
1284 fread(fbuf, 3, 1, mmfp);
1285 fwrite(fbuf, 3, 1, destfp);
1286 while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1289 fpgetfield(mmfp, fbuf);
1291 fprintf(destfp, "%s!", NODENAME);
1293 fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1294 if (a == 'S') if (!strcasecmp(fbuf, "CANCEL")) {
1295 delete_locally(cmptr->m_num, cmptr->m_rmname);
1299 fprintf(destfp, "C%s%c",
1300 cmptr->m_rmname, 0);
1308 /* Get this message into the use table, so we can reject it
1309 * if a misconfigured remote system sends it back to us.
1312 fpmsgfind(mmfp, &minfo);
1313 already_received(use_table, &minfo);
1319 return (msgs_spooled);
1322 void outprocess(char *sysname)
1323 { /* send new room messages to sysname */
1326 char shiptocmd[128];
1330 struct msglist *cmlist = NULL;
1331 struct rmlist *crmlist = NULL;
1332 struct rmlist *rmptr, *rmptr2;
1333 struct msglist *cmptr, *cmptr2;
1334 FILE *sysflfp, *tempflfp;
1338 sprintf(tempflnm, tmpnam(NULL));
1339 tempflfp = fopen(tempflnm, "w");
1340 if (tempflfp == NULL)
1345 * Read system file for node in question and put together room list
1347 sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1348 sysflfp = fopen(sysflnm, "r");
1349 if (sysflfp == NULL)
1351 fgets(shiptocmd, 128, sysflfp);
1352 shiptocmd[strlen(shiptocmd) - 1] = 0;
1353 while (!feof(sysflfp)) {
1354 if (fgets(srmname, 32, sysflfp) == NULL)
1356 srmname[strlen(srmname) - 1] = 0;
1357 fgets(lbuf, 32, sysflfp);
1358 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1360 strcpy(rmptr->rm_name, srmname);
1361 strip_trailing_whitespace(rmptr->rm_name);
1362 rmptr->rm_lastsent = atol(lbuf);
1363 if (crmlist == NULL)
1365 else if (!strcasecmp(rmptr->rm_name, "control")) {
1366 /* control has to be first in room list */
1367 rmptr->next = crmlist;
1371 while (rmptr2->next != NULL)
1372 rmptr2 = rmptr2->next;
1373 rmptr2->next = rmptr;
1379 * Assemble list of messages to be spooled
1381 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1383 sprintf(buf, "GOTO %s", rmptr->rm_name);
1386 if (buf[0] != '2') {
1387 syslog(LOG_ERR, "%s", buf);
1389 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1393 while (serv_gets(buf), strcmp(buf, "000")) {
1394 thismsg = atol(buf);
1395 if (thismsg > (rmptr->rm_lastsent)) {
1396 rmptr->rm_lastsent = thismsg;
1398 cmptr = (struct msglist *)
1399 malloc(sizeof(struct msglist));
1401 cmptr->m_num = thismsg;
1402 strcpy(cmptr->m_rmname, rmptr->rm_name);
1408 while (cmptr2->next != NULL)
1409 cmptr2 = cmptr2->next;
1410 cmptr2->next = cmptr;
1413 } else { /* print error from "msgs all" */
1414 syslog(LOG_ERR, "%s", buf);
1420 cmptr2 = cmlist; /* this loop counts the messages */
1421 while (cmptr2 != NULL) {
1423 cmptr2 = cmptr2->next;
1425 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1426 outgoing_msgs, sysname);
1429 * Spool out the messages, but only if there are any.
1431 if (outgoing_msgs != 0)
1432 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1433 syslog(LOG_NOTICE, "%d messages actually spooled",
1437 * Deallocate list of spooled messages.
1439 while (cmlist != NULL) {
1440 cmptr = cmlist->next;
1446 * Rewrite system file and deallocate room list.
1448 syslog(LOG_NOTICE, "Spooling...");
1449 sysflfp = fopen(sysflnm, "w");
1450 fprintf(sysflfp, "%s\n", shiptocmd);
1451 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1452 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1454 while (crmlist != NULL) {
1455 rmptr = crmlist->next;
1461 * Close temporary file, ship it out, and return
1464 if (outgoing_msgs != 0)
1465 ship_to(tempflnm, sysname);
1471 * Connect netproc to the Citadel server running on this computer.
1473 void np_attach_to_server(void)
1478 {"netproc", "localhost", NULL, NULL};
1480 syslog(LOG_NOTICE, "Attaching to server...");
1481 sprintf(portname, "%d", config.c_port_number);
1483 attach_to_server(3, args);
1485 syslog(LOG_NOTICE, "%s", &buf[4]);
1486 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1489 syslog(LOG_NOTICE, "%s", &buf[4]);
1490 if (buf[0] != '2') {
1500 int main(int argc, char **argv)
1505 int import_only = 0; /* if set to 1, don't export anything */
1507 openlog("netproc", LOG_PID, LOG_USER);
1508 strcpy(bbs_home_directory, BBSDIR);
1511 * Change directories if specified
1513 for (a = 1; a < argc; ++a) {
1514 if (!strncmp(argv[a], "-h", 2)) {
1515 strcpy(bbs_home_directory, argv[a]);
1516 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1518 } else if (!strcmp(argv[a], "-i")) {
1521 fprintf(stderr, "netproc: usage: ");
1522 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1528 syslog(LOG_DEBUG, "Calling get_config()");
1533 syslog(LOG_DEBUG, "Creating lock file");
1535 if (set_lockfile() != 0) {
1536 syslog(LOG_NOTICE, "lock file exists: already running");
1539 signal(SIGINT, cleanup);
1540 signal(SIGQUIT, cleanup);
1541 signal(SIGHUP, cleanup);
1542 signal(SIGTERM, cleanup);
1544 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1546 np_attach_to_server();
1549 if (load_syslist() != 0)
1550 syslog(LOG_ERR, "cannot load sysinfo");
1551 setup_special_nodes();
1553 /* Open the use table */
1554 use_table = gdbm_open("./data/usetable.gdbm", 512,
1555 GDBM_WRCREAT, 0600, 0);
1556 if (use_table == NULL) {
1557 syslog(LOG_ERR, "could not open use table: %s",
1561 /* first collect incoming stuff */
1564 /* Now process outbound messages, but NOT if this is just a
1565 * quick import-only run (i.e. the -i command-line option
1568 if (import_only != 1) {
1569 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1570 if (allfp != NULL) {
1571 while (fgets(allst, 32, allfp) != NULL) {
1572 allst[strlen(allst) - 1] = 0;
1577 /* import again in case anything new was generated */
1581 /* Update mail.sysinfo with new information we learned */
1584 /* Delete any messages which need to be purged locally */
1585 process_purgelist();
1587 /* Close the use table */
1588 purge_use_table(use_table);
1589 gdbm_close(use_table);
1591 syslog(LOG_NOTICE, "processing ended.");