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;
837 /* temp file names */
838 sprintf(tname, tmpnam(NULL));
839 sprintf(iname, tmpnam(NULL));
843 /* Make sure we're in the right directory */
844 chdir(bbs_home_directory);
846 /* temporary file to contain a log of rejected dups */
849 /* Let the shell do the dirty work. Get all data from spoolin */
851 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
852 ls = popen(aaa, "r");
854 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
858 SKIP: ptr = fgets(sfilename, sizeof sfilename, ls);
862 "Trying %s", sfilename);
864 sfilename[strlen(sfilename) - 1] = 0;
865 if (!strcmp(sfilename, ".")) goto SKIP;
866 if (!strcmp(sfilename, "..")) goto SKIP;
867 if (!strcmp(sfilename, "CVS")) goto SKIP;
870 } while (ptr != NULL);
871 PROCESS_IT: pclose(ls);
874 sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
875 syslog(LOG_NOTICE, "processing <%s>", pfilename);
877 fp = fopen(pfilename, "rb");
879 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
880 fp = fopen("/dev/null", "rb");
882 NXMSG: /* Seek to the beginning of the next message */
885 } while ((a != 255) && (a >= 0));
889 /* This crates the temporary file. */
890 message = fopen(tname, "wb");
891 if (message == NULL) {
892 syslog(LOG_ERR, "error creating %s: %s",
893 tname, strerror(errno));
896 putc(255, message); /* 0xFF (start-of-message) */
898 putc(a, message); /* type */
900 putc(a, message); /* mode */
902 FieldID = getc(fp); /* Header field ID */
903 putc(FieldID, message);
908 } while ((FieldID != 'M') && (a >= 0));
909 /* M is always last */
911 msglen = ftell(message);
914 /* process the individual mesage */
915 msgfind(tname, &minfo);
916 strncpy(recentmsg.RMnodename, minfo.N, 9);
917 recentmsg.RMnodename[9] = 0;
918 recentmsg.RMnum = minfo.I;
919 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
920 minfo.I, minfo.A, minfo.O, minfo.N);
921 if (strlen(minfo.R) > 0) {
922 syslog(LOG_NOTICE, " to <%s>", minfo.R);
923 if (strlen(minfo.D) > 0) {
924 syslog(LOG_NOTICE, " @ <%s>",
928 if (!strcasecmp(minfo.D, FQDN))
929 strcpy(minfo.D, NODENAME);
931 /* this routine updates our info on the system that sent the message */
932 stemp = get_sys_ptr(minfo.N);
933 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
934 /* add non-neighbor system to map */
935 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
937 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
940 strcpy(slist->s_name, minfo.N);
941 strcpy(slist->s_type, "use");
942 strcpy(slist->s_nexthop, minfo.nexthop);
943 time(&slist->s_lastcontact);
944 } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
945 /* add neighbor system to map */
946 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
948 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
949 testfp = fopen(aaa, "r");
950 if (testfp != NULL) {
952 stemp = (struct syslist *)
953 malloc((long) sizeof(struct syslist));
956 strcpy(slist->s_name, minfo.N);
957 strcpy(slist->s_type, "bin");
958 strcpy(slist->s_nexthop, "Mail");
959 time(&slist->s_lastcontact);
962 /* now update last contact and long node name if we can */
964 time(&stemp->s_lastcontact);
965 if (strlen(minfo.H) > 0)
966 strcpy(stemp->s_humannode, minfo.H);
967 if (strlen(minfo.B) > 0)
968 strcpy(stemp->s_phonenum, minfo.B);
969 if (strlen(minfo.G) > 0)
970 strcpy(stemp->s_gdom, minfo.G);
973 /* Check the use table; reject message if it's been here before */
974 if (already_received(use_table, &minfo)) {
975 syslog(LOG_NOTICE, "rejected duplicate message");
976 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
977 minfo.I, minfo.A, minfo.O, minfo.N);
981 /* route the message if necessary */
982 else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
983 a = get_sysinfo_type(minfo.D);
984 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
986 if (a == MES_INTERNET) {
988 syslog(LOG_NOTICE, "netmailer %s", tname);
990 execlp("./netmailer", "netmailer",
992 syslog(LOG_ERR, "error running netmailer: %s",
996 while (wait(&b) != (-1));
997 } else if (a == MES_BINARY) {
998 ship_to(tname, minfo.D);
1000 /* message falls into the bit bucket? */
1004 /* check to see if it's a file transfer */
1005 else if (!strncasecmp(minfo.S, "FILE", 4)) {
1006 proc_file_transfer(tname);
1009 /* otherwise process it as a normal message */
1011 if (!strcasecmp(minfo.R, "postmaster")) {
1012 strcpy(minfo.R, "");
1013 strcpy(minfo.C, "Aide");
1015 if (strlen(minfo.R) > 0) {
1016 sprintf(buf, "GOTO _MAIL_");
1018 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1019 sprintf(buf, "GOTO %s", FILTERROOM);
1021 if (strlen(minfo.C) > 0) {
1022 sprintf(buf, "GOTO %s", minfo.C);
1024 sprintf(buf, "GOTO %s", minfo.O);
1029 if (buf[0] != '2') {
1030 syslog(LOG_ERR, "%s", buf);
1031 sprintf(buf, "GOTO _BITBUCKET_");
1035 /* Open the temporary file containing the message */
1036 message = fopen(tname, "rb");
1037 if (message == NULL) {
1038 syslog(LOG_ERR, "cannot open %s: %s",
1039 tname, strerror(errno));
1043 /* Transmit the message to the server */
1044 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1047 if (!strncmp(buf, "570", 3)) {
1048 /* no such user, do a bounce */
1051 if (buf[0] == '7') {
1052 /* Always use the server's idea of the message length,
1053 * even though they should both be identical */
1054 msglen = atol(&buf[4]);
1055 while (msglen > 0L) {
1056 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1057 if (fread(buf, bloklen, 1, message) < 1) {
1059 "error trying to read %d bytes: %s",
1060 bloklen, strerror(errno));
1062 serv_write(buf, bloklen);
1063 msglen = msglen - (long) bloklen;
1068 syslog(LOG_ERR, "%s", buf);
1081 } while (ptr != NULL);
1086 * If dups were rejected, post a message saying so
1088 if (ftell(duplist)!=0L) {
1089 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1091 fprintf(fp, "%cA%c", 255, 1);
1092 fprintf(fp, "T%ld%c", time(NULL), 0);
1093 fprintf(fp, "ACitadel%c", 0);
1094 fprintf(fp, "OAide%cM", 0);
1095 fprintf(fp, "The following duplicate messages"
1096 " were rejected:\n \n");
1098 while (fgets(buf, sizeof(buf), duplist) != NULL) {
1099 buf[strlen(buf)-1] = 0;
1100 fprintf(fp, " %s\n", buf);
1102 fprintf(fp, "%c", 0);
1112 /* Checks to see whether its ok to send */
1113 /* Returns 1 for ok, send message */
1114 /* Returns 0 if message already there */
1115 int checkpath(char *path, char *sys)
1123 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1125 for (a = 0; a < strlen(path); ++a) {
1126 if (!strncmp(&path[a], sys2, strlen(sys2)))
1133 * Implement split horizon algorithm (prevent infinite spooling loops
1134 * by refusing to send any node a message which already contains its
1135 * nodename in the path).
1137 int ismsgok(FILE *mmfp, char *sysname)
1140 int ok = 0; /* fail safe - no path, don't send it */
1144 if (getc(mmfp) != 255)
1149 while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1150 fpgetfield(mmfp, fbuf);
1152 ok = checkpath(fbuf, sysname);
1156 syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1163 /* spool list of messages to a file */
1164 /* returns # of msgs spooled */
1165 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1167 struct msglist *cmptr;
1171 int msgs_spooled = 0;
1174 static struct minfo minfo;
1179 strcpy(curr_rm, "");
1181 /* for each message in the list... */
1182 for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1184 /* make sure we're in the correct room... */
1185 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1186 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1189 if (buf[0] == '2') {
1190 strcpy(curr_rm, cmptr->m_rmname);
1192 syslog(LOG_ERR, "%s", buf);
1195 /* download the message from the server... */
1197 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1200 if (buf[0] == '6') { /* read the msg */
1201 msg_len = atol(&buf[4]);
1202 while (msg_len > 0L) {
1203 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1204 serv_read(buf, blok_len);
1205 fwrite(buf, blok_len, 1, mmfp);
1206 msg_len = msg_len - (long) blok_len;
1208 } else { /* or print the err */
1209 syslog(LOG_ERR, "%s", buf);
1214 if (ismsgok(mmfp, sysname)) {
1218 fread(fbuf, 3, 1, mmfp);
1219 fwrite(fbuf, 3, 1, destfp);
1220 while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1223 fpgetfield(mmfp, fbuf);
1225 fprintf(destfp, "%s!", NODENAME);
1227 fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1230 fprintf(destfp, "C%s%c",
1231 cmptr->m_rmname, 0);
1239 /* Get this message into the use table, so we can reject it
1240 * if a misconfigured remote system sends it back to us.
1243 fpmsgfind(mmfp, &minfo);
1244 already_received(use_table, &minfo);
1250 return (msgs_spooled);
1253 void outprocess(char *sysname)
1254 { /* send new room messages to sysname */
1257 char shiptocmd[128];
1261 struct msglist *cmlist = NULL;
1262 struct rmlist *crmlist = NULL;
1263 struct rmlist *rmptr, *rmptr2;
1264 struct msglist *cmptr, *cmptr2;
1265 FILE *sysflfp, *tempflfp;
1269 sprintf(tempflnm, tmpnam(NULL));
1270 tempflfp = fopen(tempflnm, "w");
1271 if (tempflfp == NULL)
1276 * Read system file for node in question and put together room list
1278 sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1279 sysflfp = fopen(sysflnm, "r");
1280 if (sysflfp == NULL)
1282 fgets(shiptocmd, 128, sysflfp);
1283 shiptocmd[strlen(shiptocmd) - 1] = 0;
1284 while (!feof(sysflfp)) {
1285 if (fgets(srmname, 32, sysflfp) == NULL)
1287 srmname[strlen(srmname) - 1] = 0;
1288 fgets(lbuf, 32, sysflfp);
1289 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1291 strcpy(rmptr->rm_name, srmname);
1292 strip_trailing_whitespace(rmptr->rm_name);
1293 rmptr->rm_lastsent = atol(lbuf);
1294 if (crmlist == NULL)
1296 else if (!strcasecmp(rmptr->rm_name, "control")) {
1297 /* control has to be first in room list */
1298 rmptr->next = crmlist;
1302 while (rmptr2->next != NULL)
1303 rmptr2 = rmptr2->next;
1304 rmptr2->next = rmptr;
1310 * Assemble list of messages to be spooled
1312 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1314 sprintf(buf, "GOTO %s", rmptr->rm_name);
1317 if (buf[0] != '2') {
1318 syslog(LOG_ERR, "%s", buf);
1320 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1324 while (serv_gets(buf), strcmp(buf, "000")) {
1325 thismsg = atol(buf);
1326 if (thismsg > (rmptr->rm_lastsent)) {
1327 rmptr->rm_lastsent = thismsg;
1329 cmptr = (struct msglist *)
1330 malloc(sizeof(struct msglist));
1332 cmptr->m_num = thismsg;
1333 strcpy(cmptr->m_rmname, rmptr->rm_name);
1339 while (cmptr2->next != NULL)
1340 cmptr2 = cmptr2->next;
1341 cmptr2->next = cmptr;
1344 } else { /* print error from "msgs all" */
1345 syslog(LOG_ERR, "%s", buf);
1351 cmptr2 = cmlist; /* this loop counts the messages */
1352 while (cmptr2 != NULL) {
1354 cmptr2 = cmptr2->next;
1356 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1357 outgoing_msgs, sysname);
1360 * Spool out the messages, but only if there are any.
1362 if (outgoing_msgs != 0)
1363 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1364 syslog(LOG_NOTICE, "%d messages actually spooled",
1368 * Deallocate list of spooled messages.
1370 while (cmlist != NULL) {
1371 cmptr = cmlist->next;
1377 * Rewrite system file and deallocate room list.
1379 syslog(LOG_NOTICE, "Spooling...");
1380 sysflfp = fopen(sysflnm, "w");
1381 fprintf(sysflfp, "%s\n", shiptocmd);
1382 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1383 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1385 while (crmlist != NULL) {
1386 rmptr = crmlist->next;
1392 * Close temporary file, ship it out, and return
1395 if (outgoing_msgs != 0)
1396 ship_to(tempflnm, sysname);
1402 * Connect netproc to the Citadel server running on this computer.
1404 void np_attach_to_server(void)
1409 {"netproc", "localhost", NULL, NULL};
1411 syslog(LOG_NOTICE, "Attaching to server...");
1412 sprintf(portname, "%d", config.c_port_number);
1414 attach_to_server(3, args);
1416 syslog(LOG_NOTICE, "%s", &buf[4]);
1417 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1420 syslog(LOG_NOTICE, "%s", &buf[4]);
1421 if (buf[0] != '2') {
1431 int main(int argc, char **argv)
1436 int import_only = 0; /* if set to 1, don't export anything */
1438 openlog("netproc", LOG_PID, LOG_USER);
1439 strcpy(bbs_home_directory, BBSDIR);
1442 * Change directories if specified
1444 for (a = 1; a < argc; ++a) {
1445 if (!strncmp(argv[a], "-h", 2)) {
1446 strcpy(bbs_home_directory, argv[a]);
1447 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1449 } else if (!strcmp(argv[a], "-i")) {
1452 fprintf(stderr, "netproc: usage: ");
1453 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1459 syslog(LOG_DEBUG, "Calling get_config()");
1464 syslog(LOG_DEBUG, "Creating lock file");
1466 if (set_lockfile() != 0) {
1467 syslog(LOG_NOTICE, "lock file exists: already running");
1470 signal(SIGINT, cleanup);
1471 signal(SIGQUIT, cleanup);
1472 signal(SIGHUP, cleanup);
1473 signal(SIGTERM, cleanup);
1475 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1477 np_attach_to_server();
1480 if (load_syslist() != 0)
1481 syslog(LOG_ERR, "cannot load sysinfo");
1482 setup_special_nodes();
1484 /* Open the use table */
1485 use_table = gdbm_open("./data/usetable.gdbm", 512,
1486 GDBM_WRCREAT, 0600, 0);
1487 if (use_table == NULL) {
1488 syslog(LOG_ERR, "could not open use table: %s",
1492 /* first collect incoming stuff */
1495 /* Now process outbound messages, but NOT if this is just a
1496 * quick import-only run (i.e. the -i command-line option
1499 if (import_only != 1) {
1500 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1501 if (allfp != NULL) {
1502 while (fgets(allst, 32, allfp) != NULL) {
1503 allst[strlen(allst) - 1] = 0;
1508 /* import again in case anything new was generated */
1512 /* Update mail.sysinfo with new information we learned */
1515 /* Close the use table */
1516 purge_use_table(use_table);
1517 gdbm_close(use_table);
1519 syslog(LOG_NOTICE, "processing ended.");