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;
98 * replacement strerror() for systems that don't have it
100 char *strerror(int e)
104 sprintf(buf, "errno = %d", e);
110 void strip_trailing_whitespace(char *buf)
112 while (isspace(buf[strlen(buf) - 1]))
113 buf[strlen(buf) - 1] = 0;
118 * we also load the network/mail.sysinfo table into memory, make changes
119 * as we learn more about the network from incoming messages, and write
120 * the table back to disk when we're done.
122 int load_syslist(void)
125 struct syslist *stemp;
129 fp = fopen("network/mail.sysinfo", "r");
134 if (fgets(buf, 128, fp) == NULL) {
138 buf[strlen(buf) - 1] = 0;
139 while (isspace(buf[0]))
140 strcpy(buf, &buf[1]);
143 if ((insys == 0) && (strlen(buf) != 0)) {
145 stemp = (struct syslist *) malloc(sizeof(struct syslist));
148 strcpy(slist->s_name, buf);
149 strcpy(slist->s_type, "bin");
150 strcpy(slist->s_nexthop, "Mail");
151 slist->s_lastcontact = 0L;
152 strcpy(slist->s_humannode, "");
153 strcpy(slist->s_phonenum, "");
154 strcpy(slist->s_gdom, "");
155 } else if ((insys == 1) && (strlen(buf) == 0)) {
157 } else if ((insys == 1) && (!strncasecmp(buf, "bin", 3))) {
158 strcpy(slist->s_type, "bin");
159 strcpy(slist->s_nexthop, &buf[4]);
160 } else if ((insys == 1) && (!strncasecmp(buf, "use", 3))) {
161 strcpy(slist->s_type, "use");
162 strcpy(slist->s_nexthop, &buf[4]);
163 } else if ((insys == 1) && (!strncasecmp(buf, "uum", 3))) {
164 strcpy(slist->s_type, "uum");
165 strcpy(slist->s_nexthop, &buf[4]);
166 } else if ((insys == 1) && (!strncasecmp(buf, "lastcontact", 11))) {
168 sscanf(&buf[12], "%ld", &foo);
169 slist->s_lastcontact = foo;
170 } else if ((insys == 1) && (!strncasecmp(buf, "humannode", 9))) {
171 strcpy(slist->s_humannode, &buf[10]);
172 } else if ((insys == 1) && (!strncasecmp(buf, "phonenum", 8))) {
173 strcpy(slist->s_phonenum, &buf[9]);
174 } else if ((insys == 1) && (!strncasecmp(buf, "gdom", 4))) {
175 strcpy(slist->s_gdom, &buf[5]);
180 /* now we have to set up two "special" nodes on the list: one
181 * for the local node, and one for an Internet gateway
183 void setup_special_nodes(void)
185 struct syslist *stemp, *slocal;
188 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
189 if (!strcasecmp(stemp->s_name, config.c_nodename))
192 if (slocal == NULL) {
193 slocal = (struct syslist *) malloc(sizeof(struct syslist));
194 slocal->next = slist;
197 strcpy(slocal->s_name, config.c_nodename);
198 strcpy(slocal->s_type, "bin");
199 strcpy(slocal->s_nexthop, "Mail");
200 time(&slocal->s_lastcontact);
201 strcpy(slocal->s_humannode, config.c_humannode);
202 strcpy(slocal->s_phonenum, config.c_phonenum);
205 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
206 if (!strcasecmp(stemp->s_name, "internet"))
209 if (slocal == NULL) {
210 slocal = (struct syslist *) malloc(sizeof(struct syslist));
211 slocal->next = slist;
214 strcpy(slocal->s_name, "internet");
215 strcpy(slocal->s_type, "uum");
216 strcpy(slocal->s_nexthop, "%s");
217 time(&slocal->s_lastcontact);
218 strcpy(slocal->s_humannode, "Internet Gateway");
219 strcpy(slocal->s_phonenum, "");
220 strcpy(slocal->s_gdom, "");
225 * here's the routine to write the table back to disk.
227 void rewrite_syslist(void)
229 struct syslist *stemp;
234 newfp = fopen("network/mail.sysinfo", "w");
235 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
236 if (!strcasecmp(stemp->s_name, config.c_nodename)) {
237 time(&stemp->s_lastcontact);
238 strcpy(stemp->s_type, "bin");
239 strcpy(stemp->s_humannode, config.c_humannode);
240 strcpy(stemp->s_phonenum, config.c_phonenum);
242 /* remove systems we haven't heard from in a while */
243 if ((stemp->s_lastcontact == 0L)
244 || (now - stemp->s_lastcontact < EXPIRY_TIME)) {
245 fprintf(newfp, "%s\n%s %s\n",
246 stemp->s_name, stemp->s_type, stemp->s_nexthop);
247 if (strlen(stemp->s_phonenum) > 0)
248 fprintf(newfp, "phonenum %s\n", stemp->s_phonenum);
249 if (strlen(stemp->s_gdom) > 0)
250 fprintf(newfp, "gdom %s\n", stemp->s_gdom);
251 if (strlen(stemp->s_humannode) > 0)
252 fprintf(newfp, "humannode %s\n", stemp->s_humannode);
253 if (stemp->s_lastcontact > 0L)
254 fprintf(newfp, "lastcontact %ld %s",
255 (long) stemp->s_lastcontact,
256 asctime(localtime(&stemp->s_lastcontact)));
257 fprintf(newfp, "\n");
261 /* now free the list */
262 while (slist != NULL) {
270 /* call this function with the node name of a system and it returns a pointer
271 * to its syslist structure.
273 struct syslist *get_sys_ptr(char *sysname)
275 static char sysnambuf[16];
276 static struct syslist *sysptrbuf = NULL;
277 struct syslist *stemp;
279 if ((!strcmp(sysname, sysnambuf))
280 && (sysptrbuf != NULL))
283 strcpy(sysnambuf, sysname);
284 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
285 if (!strcmp(sysname, stemp->s_name)) {
296 * make sure only one copy of netproc runs at a time, using lock files
298 int set_lockfile(void)
303 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
304 fscanf(lfp, "%d", &onppid);
306 if (!kill(onppid, 0) || errno == EPERM)
309 lfp = fopen(LOCKFILE, "w");
310 fprintf(lfp, "%ld\n", (long) getpid());
315 void remove_lockfile(void)
321 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
322 * cleanup() . If for some reason netproc hangs waiting for the server
323 * to clean up, the alarm clock goes off and the program exits anyway.
324 * The cleanup() routine makes a check to ensure it's not reentering, in
325 * case the ipc module looped it somehow.
327 void nq_cleanup(int e)
336 static int nested = 0;
339 signal(SIGALRM, nq_cleanup);
346 * This is implemented as a function rather than as a macro because the
347 * client-side IPC modules expect logoff() to be defined. They call logoff()
348 * when a problem connecting or staying connected to the server occurs.
356 * If there is a kill file in place, this function will process it.
358 void load_filterlist(void)
361 struct filterlist *fbuf;
364 fp = fopen("./network/filterlist", "r");
367 while (fgets(sbuf, 256, fp) != NULL) {
368 if (sbuf[0] != '#') {
369 sbuf[strlen(sbuf) - 1] = 0;
370 fbuf = (struct filterlist *)
371 malloc((long) sizeof(struct filterlist));
374 strcpy(fbuf->f_person, "*");
375 strcpy(fbuf->f_room, "*");
376 strcpy(fbuf->f_system, "*");
378 for (a = strlen(sbuf); a >= 0; --a)
383 strcpy(fbuf->f_person, sbuf);
384 strcpy(sbuf, &sbuf[p + 1]);
386 for (a = strlen(sbuf); a >= 0; --a)
391 strcpy(fbuf->f_room, sbuf);
392 strcpy(sbuf, &sbuf[p + 1]);
394 strcpy(fbuf->f_system, sbuf);
400 /* returns 1 if user/message/room combination is in the kill file */
401 int is_banned(char *k_person, char *k_room, char *k_system)
403 struct filterlist *fptr;
405 for (fptr = filter; fptr != NULL; fptr = fptr->next)
407 ((!strcasecmp(fptr->f_person, k_person)) || (!strcmp(fptr->f_person, "*")))
409 ((!strcasecmp(fptr->f_room, k_room)) || (!strcmp(fptr->f_room, "*")))
411 ((!strcasecmp(fptr->f_system, k_system)) || (!strcmp(fptr->f_system, "*")))
419 * Determine routing from sysinfo file
421 int get_sysinfo_type(char *name) {
422 struct syslist *stemp;
424 GETSN: for (stemp = slist; stemp != NULL; stemp = stemp->next) {
425 if (!strcasecmp(stemp->s_name, name)) {
426 if (!strcasecmp(stemp->s_type, "use")) {
427 strcpy(name, stemp->s_nexthop);
430 if (!strcasecmp(stemp->s_type, "bin")) {
433 if (!strcasecmp(stemp->s_type, "uum")) {
434 return (MES_INTERNET);
438 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
443 void fpgetfield(FILE * fp, char *string)
463 * Load all of the fields of a message, except the actual text, into a
464 * table in memory (so we know how to process the message).
466 void msgfind(char *msgfile, struct minfo *buffer)
468 int b, e, mtype, aflag;
474 fp = fopen(msgfile, "rb");
476 syslog(LOG_ERR, "can't open message file: %s", strerror(errno));
481 syslog(LOG_ERR, "incorrect message format");
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);
555 /* NOTE: we used to use the following two lines of code to assign
556 * the timestamp as a message-ID if there was no message-ID already
557 * in the message. We don't do this anymore because it screws up
561 buffer->I = buffer->T;
569 void ship_to(char *filenm, char *sysnm)
570 { /* send spool file filenm to system sysnm */
577 syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
579 sprintf(sysflnm, "./network/systems/%s", sysnm);
580 sysflfd = fopen(sysflnm, "r");
582 syslog(LOG_ERR, "cannot open %s", sysflnm);
583 fgets(commbuf1, 99, sysflfd);
584 commbuf1[strlen(commbuf1) - 1] = 0;
586 sprintf(commbuf2, commbuf1, filenm);
591 * proc_file_transfer() - handle a simple file transfer packet
594 void proc_file_transfer(char *tname)
595 { /* name of temp file containing the whole message */
597 char dest_room[ROOMNAMELEN];
598 char subdir_name[256];
602 syslog(LOG_NOTICE, "processing network file transfer...");
604 tfp = fopen(tname, "rb");
606 syslog(LOG_ERR, "cannot open %s", tname);
613 fpgetfield(tfp, buf);
615 strcpy(dest_room, buf);
618 } while ((a != 'M') && (a >= 0));
621 syslog(LOG_ERR, "no message text for file transfer");
624 strcpy(subdir_name, "---xxx---");
625 sprintf(buf, "GOTO %s", dest_room);
629 extract(subdir_name, &buf[4], 2);
630 if (strlen(subdir_name) == 0)
631 strcpy(subdir_name, "--xxx--");
633 /* Change to the room's directory; if that fails, change to the
634 * bitbucket directory. Then run uudecode.
636 sprintf(buf, "(cd %s/files/%s || cd %s/files/%s ) ; exec %s",
637 bbs_home_directory, subdir_name,
638 bbs_home_directory, config.c_bucket_dir,
641 uud = (FILE *) popen(buf, "w");
643 syslog(LOG_ERR, "cannot open uudecode pipe");
647 fgets(buf, 128, tfp);
648 buf[strlen(buf) - 1] = 0;
649 for (a = 0; a < strlen(buf); ++a)
652 fprintf(uud, "%s\n", buf);
653 printf("netproc: %s\n", buf);
654 while (a = getc(tfp), a > 0)
662 /* send a bounce message */
663 void bounce(struct minfo *bminfo)
671 sprintf(bfilename, "./network/spoolin/bounce.%ld.%d", (long) getpid(),
673 bounce = fopen(bfilename, "wb");
676 fprintf(bounce, "%c%c%c", 0xFF, MES_NORMAL, 0);
677 fprintf(bounce, "Ppostmaster%c", 0);
678 fprintf(bounce, "T%ld%c", (long) now, 0);
679 fprintf(bounce, "APostmaster%c", 0);
680 fprintf(bounce, "OMail%c", 0);
681 fprintf(bounce, "N%s%c", config.c_nodename, 0);
682 fprintf(bounce, "H%s%c", config.c_humannode, 0);
684 if (strlen(bminfo->E) > 0) {
685 fprintf(bounce, "R%s%c", bminfo->E, 0);
687 fprintf(bounce, "R%s%c", bminfo->A, 0);
690 fprintf(bounce, "D%s%c", bminfo->N, 0);
691 fprintf(bounce, "M%s could not deliver your mail to:\n",
693 fprintf(bounce, " \n %s\n \n", bminfo->R);
694 fprintf(bounce, " because there is no such user on this system.\n");
695 fprintf(bounce, " (Unsent message does *not* follow. ");
696 fprintf(bounce, "Help to conserve bandwidth.)\n%c", 0);
704 * Generate a Message-ID string for the use table
706 void strmsgid(char *buf, struct minfo *msginfo) {
709 sprintf(buf, "%ld@%s", msginfo->I, msginfo->N);
710 for (i=0; i<strlen(buf); ++i) {
711 if (isspace(buf[i])) {
712 strcpy(&buf[i], &buf[i+1]);
714 buf[i] = tolower(buf[i]);
721 * Check the use table to see if a message has been here before.
722 * Returns 1 if the message is a duplicate; otherwise, it returns
723 * 0 and the message ID is added to the use table.
725 int already_received(GDBM_FILE ut, struct minfo *msginfo) {
731 /* We can't check for dups on a zero msgid, so just pass them through */
732 if ((msginfo->I)==0L) {
736 strmsgid(buf, msginfo);
740 mkey.dsize = strlen(buf);
742 /* Set return value to 1 if message exists */
743 if (gdbm_exists(ut, mkey)) {
747 /* Write a record into the use table for this message.
748 * Replace existing records; this keeps the timestamp fresh.
750 newrec.dptr = (char *)&now;
751 newrec.dsize = sizeof(now);
752 gdbm_store(ut, mkey, newrec, GDBM_REPLACE);
760 * Purge any old entries out of the use table.
762 * Yes, you're reading this correctly: it keeps traversing the table until
763 * it manages to do a complete pass without deleting any records. Read the
764 * gdbm man page to find out why.
767 void purge_use_table(GDBM_FILE ut) {
768 datum mkey, nextkey, therec;
769 int purged_anything = 0;
770 time_t rec_timestamp, now;
776 mkey = gdbm_firstkey(ut);
777 while (mkey.dptr != NULL) {
778 therec = gdbm_fetch(ut, mkey);
779 if (therec.dptr != NULL) {
780 memcpy(&rec_timestamp, therec.dptr,
784 if ((now - rec_timestamp) > USE_TIME) {
785 gdbm_delete(ut, mkey);
790 nextkey = gdbm_nextkey(ut, mkey);
794 } while (purged_anything != 0);
800 * process incoming files in ./network/spoolin
804 FILE *fp, *message, *testfp, *ls, *duplist;
805 static struct minfo minfo;
806 struct recentmsg recentmsg;
807 char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
810 struct syslist *stemp;
817 /* temp file names */
818 sprintf(tname, tmpnam(NULL));
819 sprintf(iname, tmpnam(NULL));
823 /* Make sure we're in the right directory */
824 chdir(bbs_home_directory);
826 /* Open the use table */
827 use_table = gdbm_open("./data/usetable.gdbm", 512,
828 GDBM_WRCREAT, 0600, 0);
829 if (use_table == NULL) {
830 syslog(LOG_ERR, "could not open use table: %s",
835 /* temporary file to contain a log of rejected dups */
838 /* Let the shell do the dirty work. Get all data from spoolin */
840 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
841 ls = popen(aaa, "r");
843 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
847 SKIP: ptr = fgets(sfilename, sizeof sfilename, ls);
849 sfilename[strlen(sfilename) - 1] = 0;
850 if (!strcmp(sfilename, ".")) goto SKIP;
851 if (!strcmp(sfilename, "..")) goto SKIP;
852 if (!strcmp(sfilename, "CVS")) goto SKIP;
855 } while (ptr != NULL);
856 PROCESS_IT: pclose(ls);
859 sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
860 syslog(LOG_NOTICE, "processing <%s>", pfilename);
862 fp = fopen(pfilename, "rb");
864 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
865 fp = fopen("/dev/null", "rb");
867 NXMSG: /* Seek to the beginning of the next message */
870 } while ((a != 255) && (a >= 0));
874 /* This crates the temporary file. */
875 message = fopen(tname, "wb");
876 if (message == NULL) {
877 syslog(LOG_ERR, "error creating %s: %s",
878 tname, strerror(errno));
881 putc(255, message); /* 0xFF (start-of-message) */
883 putc(a, message); /* type */
885 putc(a, message); /* mode */
887 FieldID = getc(fp); /* Header field ID */
888 putc(FieldID, message);
893 } while ((FieldID != 'M') && (a >= 0));
894 /* M is always last */
896 msglen = ftell(message);
899 /* process the individual mesage */
905 msgfind(tname, &minfo);
906 strncpy(recentmsg.RMnodename, minfo.N, 9);
907 recentmsg.RMnodename[9] = 0;
908 recentmsg.RMnum = minfo.I;
909 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
910 minfo.I, minfo.A, minfo.O, minfo.N);
911 if (strlen(minfo.R) > 0) {
912 syslog(LOG_NOTICE, " to <%s>", minfo.R);
913 if (strlen(minfo.D) > 0) {
914 syslog(LOG_NOTICE, " @ <%s>",
918 if (!strcasecmp(minfo.D, FQDN))
919 strcpy(minfo.D, NODENAME);
921 /* this routine updates our info on the system that sent the message */
922 stemp = get_sys_ptr(minfo.N);
923 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
924 /* add non-neighbor system to map */
925 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
927 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
930 strcpy(slist->s_name, minfo.N);
931 strcpy(slist->s_type, "use");
932 strcpy(slist->s_nexthop, minfo.nexthop);
933 time(&slist->s_lastcontact);
934 } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
935 /* add neighbor system to map */
936 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
938 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
939 testfp = fopen(aaa, "r");
940 if (testfp != NULL) {
942 stemp = (struct syslist *)
943 malloc((long) sizeof(struct syslist));
946 strcpy(slist->s_name, minfo.N);
947 strcpy(slist->s_type, "bin");
948 strcpy(slist->s_nexthop, "Mail");
949 time(&slist->s_lastcontact);
952 /* now update last contact and long node name if we can */
954 time(&stemp->s_lastcontact);
955 if (strlen(minfo.H) > 0)
956 strcpy(stemp->s_humannode, minfo.H);
957 if (strlen(minfo.B) > 0)
958 strcpy(stemp->s_phonenum, minfo.B);
959 if (strlen(minfo.G) > 0)
960 strcpy(stemp->s_gdom, minfo.G);
963 /* Check the use table; reject message if it's been here before */
964 if (already_received(use_table, &minfo)) {
965 syslog(LOG_NOTICE, "rejected duplicate message");
966 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
967 minfo.I, minfo.A, minfo.O, minfo.N);
971 /* route the message if necessary */
972 else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
973 a = get_sysinfo_type(minfo.D);
974 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
976 if (a == MES_INTERNET) {
978 syslog(LOG_NOTICE, "netmailer %s", tname);
980 execlp("./netmailer", "netmailer",
982 syslog(LOG_ERR, "error running netmailer: %s",
986 while (wait(&b) != (-1));
987 } else if (a == MES_BINARY) {
988 ship_to(tname, minfo.D);
990 /* message falls into the bit bucket? */
994 /* check to see if it's a file transfer */
995 else if (!strncasecmp(minfo.S, "FILE", 4)) {
996 proc_file_transfer(tname);
999 /* otherwise process it as a normal message */
1002 if (!strcasecmp(minfo.R, "postmaster")) {
1003 strcpy(minfo.R, "");
1004 strcpy(minfo.C, "Aide");
1006 if (strlen(minfo.R) > 0) {
1007 sprintf(buf, "GOTO _MAIL_");
1009 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1010 sprintf(buf, "GOTO %s", FILTERROOM);
1012 if (strlen(minfo.C) > 0) {
1013 sprintf(buf, "GOTO %s", minfo.C);
1015 sprintf(buf, "GOTO %s", minfo.O);
1020 if (buf[0] != '2') {
1021 syslog(LOG_ERR, "%s", buf);
1022 sprintf(buf, "GOTO _BITBUCKET_");
1026 /* Open the temporary file containing the message */
1027 message = fopen(tname, "rb");
1028 if (message == NULL) {
1029 syslog(LOG_ERR, "cannot open %s: %s",
1030 tname, strerror(errno));
1034 /* Transmit the message to the server */
1035 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1038 if (!strncmp(buf, "570", 3)) {
1039 /* no such user, do a bounce */
1042 if (buf[0] == '7') {
1043 /* Always use the server's idea of the message length,
1044 * even though they should both be identical */
1045 msglen = atol(&buf[4]);
1046 while (msglen > 0L) {
1047 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1048 if (fread(buf, bloklen, 1, message) < 1) {
1050 "error trying to read %d bytes: %s",
1051 bloklen, strerror(errno));
1053 serv_write(buf, bloklen);
1054 msglen = msglen - (long) bloklen;
1059 syslog(LOG_ERR, "%s", buf);
1071 } while (ptr != NULL);
1074 purge_use_table(use_table);
1075 gdbm_close(use_table);
1079 * If dups were rejected, post a message saying so
1081 if (ftell(duplist)!=0L) {
1082 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1084 fprintf(fp, "%cA%c", 255, 1);
1085 fprintf(fp, "T%ld%c", time(NULL), 0);
1086 fprintf(fp, "ACitadel%c", 0);
1087 fprintf(fp, "OAide%cM", 0);
1088 fprintf(fp, "The following duplicate messages"
1089 " were rejected:\n \n");
1091 while (fgets(buf, sizeof(buf), duplist) != NULL) {
1092 buf[strlen(buf)-1] = 0;
1093 fprintf(fp, " %s\n", buf);
1095 fprintf(fp, "%c", 0);
1105 /* Checks to see whether its ok to send */
1106 /* Returns 1 for ok, send message */
1107 /* Returns 0 if message already there */
1108 int checkpath(char *path, char *sys)
1116 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1118 for (a = 0; a < strlen(path); ++a) {
1119 if (!strncmp(&path[a], sys2, strlen(sys2)))
1126 * implement split horizon algorithm
1128 int ismsgok(long int mpos, FILE * mmfp, char *sysname)
1131 int ok = 0; /* fail safe - no path, don't send it */
1134 fseek(mmfp, mpos, 0);
1135 if (getc(mmfp) != 255)
1140 while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1141 fpgetfield(mmfp, fbuf);
1143 ok = checkpath(fbuf, sysname);
1147 syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1154 /* spool list of messages to a file */
1155 /* returns # of msgs spooled */
1156 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1158 struct msglist *cmptr;
1162 int msgs_spooled = 0;
1169 strcpy(curr_rm, "");
1171 /* for each message in the list... */
1172 for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1174 /* make sure we're in the correct room... */
1175 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1176 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1179 if (buf[0] == '2') {
1180 strcpy(curr_rm, cmptr->m_rmname);
1182 syslog(LOG_ERR, "%s", buf);
1185 /* download the message from the server... */
1187 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1190 if (buf[0] == '6') { /* read the msg */
1191 msg_len = atol(&buf[4]);
1192 while (msg_len > 0L) {
1193 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1194 serv_read(buf, blok_len);
1195 fwrite(buf, blok_len, 1, mmfp);
1196 msg_len = msg_len - (long) blok_len;
1198 } else { /* or print the err */
1199 syslog(LOG_ERR, "%s", buf);
1204 if (ismsgok(0L, mmfp, sysname)) {
1208 fread(fbuf, 3, 1, mmfp);
1209 fwrite(fbuf, 3, 1, destfp);
1210 while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1213 fpgetfield(mmfp, fbuf);
1215 fprintf(destfp, "%s!", NODENAME);
1217 fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1220 fprintf(destfp, "C%s%c",
1221 cmptr->m_rmname, 0);
1232 return (msgs_spooled);
1235 void outprocess(char *sysname)
1236 { /* send new room messages to sysname */
1239 char shiptocmd[128];
1243 struct msglist *cmlist = NULL;
1244 struct rmlist *crmlist = NULL;
1245 struct rmlist *rmptr, *rmptr2;
1246 struct msglist *cmptr, *cmptr2;
1247 FILE *sysflfp, *tempflfp;
1251 sprintf(tempflnm, tmpnam(NULL));
1252 tempflfp = fopen(tempflnm, "w");
1253 if (tempflfp == NULL)
1258 * Read system file for node in question and put together room list
1260 sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1261 sysflfp = fopen(sysflnm, "r");
1262 if (sysflfp == NULL)
1264 fgets(shiptocmd, 128, sysflfp);
1265 shiptocmd[strlen(shiptocmd) - 1] = 0;
1266 while (!feof(sysflfp)) {
1267 if (fgets(srmname, 32, sysflfp) == NULL)
1269 srmname[strlen(srmname) - 1] = 0;
1270 fgets(lbuf, 32, sysflfp);
1271 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1273 strcpy(rmptr->rm_name, srmname);
1274 strip_trailing_whitespace(rmptr->rm_name);
1275 rmptr->rm_lastsent = atol(lbuf);
1276 if (crmlist == NULL)
1278 else if (!strcasecmp(rmptr->rm_name, "control")) {
1279 /* control has to be first in room list */
1280 rmptr->next = crmlist;
1284 while (rmptr2->next != NULL)
1285 rmptr2 = rmptr2->next;
1286 rmptr2->next = rmptr;
1292 * Assemble list of messages to be spooled
1294 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1296 sprintf(buf, "GOTO %s", rmptr->rm_name);
1299 if (buf[0] != '2') {
1300 syslog(LOG_ERR, "%s", buf);
1302 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1306 while (serv_gets(buf), strcmp(buf, "000")) {
1307 thismsg = atol(buf);
1308 if (thismsg > (rmptr->rm_lastsent)) {
1309 rmptr->rm_lastsent = thismsg;
1311 cmptr = (struct msglist *)
1312 malloc(sizeof(struct msglist));
1314 cmptr->m_num = thismsg;
1315 strcpy(cmptr->m_rmname, rmptr->rm_name);
1321 while (cmptr2->next != NULL)
1322 cmptr2 = cmptr2->next;
1323 cmptr2->next = cmptr;
1326 } else { /* print error from "msgs all" */
1327 syslog(LOG_ERR, "%s", buf);
1333 cmptr2 = cmlist; /* this loop counts the messages */
1334 while (cmptr2 != NULL) {
1336 cmptr2 = cmptr2->next;
1338 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1339 outgoing_msgs, sysname);
1342 * Spool out the messages, but only if there are any.
1344 if (outgoing_msgs != 0)
1345 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1346 syslog(LOG_NOTICE, "%d messages actually spooled",
1350 * Deallocate list of spooled messages.
1352 while (cmlist != NULL) {
1353 cmptr = cmlist->next;
1359 * Rewrite system file and deallocate room list.
1361 syslog(LOG_NOTICE, "Spooling...");
1362 sysflfp = fopen(sysflnm, "w");
1363 fprintf(sysflfp, "%s\n", shiptocmd);
1364 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1365 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1367 while (crmlist != NULL) {
1368 rmptr = crmlist->next;
1374 * Close temporary file, ship it out, and return
1377 if (outgoing_msgs != 0)
1378 ship_to(tempflnm, sysname);
1384 * Connect netproc to the Citadel server running on this computer.
1386 void np_attach_to_server(void)
1391 {"netproc", "localhost", NULL, NULL};
1393 syslog(LOG_NOTICE, "Attaching to server...");
1394 sprintf(portname, "%d", config.c_port_number);
1396 attach_to_server(3, args);
1398 syslog(LOG_NOTICE, "%s", &buf[4]);
1399 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1402 syslog(LOG_NOTICE, "%s", &buf[4]);
1403 if (buf[0] != '2') {
1413 int main(int argc, char **argv)
1418 int import_only = 0; /* if set to 1, don't export anything */
1420 openlog("netproc", LOG_PID, LOG_USER);
1421 strcpy(bbs_home_directory, BBSDIR);
1424 * Change directories if specified
1426 for (a = 1; a < argc; ++a) {
1427 if (!strncmp(argv[a], "-h", 2)) {
1428 strcpy(bbs_home_directory, argv[a]);
1429 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1431 } else if (!strcmp(argv[a], "-i")) {
1434 fprintf(stderr, "netproc: usage: ");
1435 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1442 if (set_lockfile() != 0) {
1443 syslog(LOG_NOTICE, "lock file exists: already running");
1446 signal(SIGINT, cleanup);
1447 signal(SIGQUIT, cleanup);
1448 signal(SIGHUP, cleanup);
1449 signal(SIGTERM, cleanup);
1451 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1453 np_attach_to_server();
1456 if (load_syslist() != 0)
1457 syslog(LOG_ERR, "cannot load sysinfo");
1458 setup_special_nodes();
1460 inprocess(); /* first collect incoming stuff */
1462 if (import_only != 1) {
1463 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1464 if (allfp != NULL) {
1465 while (fgets(allst, 32, allfp) != NULL) {
1466 allst[strlen(allst) - 1] = 0;
1471 /* import again in case anything new was generated */
1475 syslog(LOG_NOTICE, "processing ended.");