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>
40 /* A list of users you wish to filter out of incoming traffic can be kept
41 * in ./network/filterlist -- messages from these users will be automatically
42 * moved to FILTERROOM. Normally this will be the same as TWITROOM (the
43 * room problem user messages are moved to) but you can override this by
44 * specifying a different room name here.
47 #define FILTERROOM TWITROOM
53 char m_rmname[ROOMNAMELEN];
58 char rm_name[ROOMNAMELEN];
63 struct filterlist *next;
81 void attach_to_server(int argc, char **argv);
82 void serv_read(char *buf, int bytes);
83 void serv_write(char *buf, int nbytes);
84 void get_config(void);
86 struct filterlist *filter = NULL;
87 struct syslist *slist = NULL;
90 extern char bbs_home_directory[];
91 extern int home_specified;
96 * replacement strerror() for systems that don't have it
102 sprintf(buf, "errno = %d", e);
108 void strip_trailing_whitespace(char *buf)
110 while (isspace(buf[strlen(buf) - 1]))
111 buf[strlen(buf) - 1] = 0;
116 * we also load the network/mail.sysinfo table into memory, make changes
117 * as we learn more about the network from incoming messages, and write
118 * the table back to disk when we're done.
120 int load_syslist(void)
123 struct syslist *stemp;
127 fp = fopen("network/mail.sysinfo", "r");
132 if (fgets(buf, 128, fp) == NULL) {
136 buf[strlen(buf) - 1] = 0;
137 while (isspace(buf[0]))
138 strcpy(buf, &buf[1]);
141 if ((insys == 0) && (strlen(buf) != 0)) {
143 stemp = (struct syslist *) malloc(sizeof(struct syslist));
146 strcpy(slist->s_name, buf);
147 strcpy(slist->s_type, "bin");
148 strcpy(slist->s_nexthop, "Mail");
149 slist->s_lastcontact = 0L;
150 strcpy(slist->s_humannode, "");
151 strcpy(slist->s_phonenum, "");
152 strcpy(slist->s_gdom, "");
153 } else if ((insys == 1) && (strlen(buf) == 0)) {
155 } else if ((insys == 1) && (!strncasecmp(buf, "bin", 3))) {
156 strcpy(slist->s_type, "bin");
157 strcpy(slist->s_nexthop, &buf[4]);
158 } else if ((insys == 1) && (!strncasecmp(buf, "use", 3))) {
159 strcpy(slist->s_type, "use");
160 strcpy(slist->s_nexthop, &buf[4]);
161 } else if ((insys == 1) && (!strncasecmp(buf, "uum", 3))) {
162 strcpy(slist->s_type, "uum");
163 strcpy(slist->s_nexthop, &buf[4]);
164 } else if ((insys == 1) && (!strncasecmp(buf, "lastcontact", 11))) {
166 sscanf(&buf[12], "%ld", &foo);
167 slist->s_lastcontact = foo;
168 } else if ((insys == 1) && (!strncasecmp(buf, "humannode", 9))) {
169 strcpy(slist->s_humannode, &buf[10]);
170 } else if ((insys == 1) && (!strncasecmp(buf, "phonenum", 8))) {
171 strcpy(slist->s_phonenum, &buf[9]);
172 } else if ((insys == 1) && (!strncasecmp(buf, "gdom", 4))) {
173 strcpy(slist->s_gdom, &buf[5]);
178 /* now we have to set up two "special" nodes on the list: one
179 * for the local node, and one for an Internet gateway
181 void setup_special_nodes(void)
183 struct syslist *stemp, *slocal;
186 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
187 if (!strcasecmp(stemp->s_name, config.c_nodename))
190 if (slocal == NULL) {
191 slocal = (struct syslist *) malloc(sizeof(struct syslist));
192 slocal->next = slist;
195 strcpy(slocal->s_name, config.c_nodename);
196 strcpy(slocal->s_type, "bin");
197 strcpy(slocal->s_nexthop, "Mail");
198 time(&slocal->s_lastcontact);
199 strcpy(slocal->s_humannode, config.c_humannode);
200 strcpy(slocal->s_phonenum, config.c_phonenum);
203 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
204 if (!strcasecmp(stemp->s_name, "internet"))
207 if (slocal == NULL) {
208 slocal = (struct syslist *) malloc(sizeof(struct syslist));
209 slocal->next = slist;
212 strcpy(slocal->s_name, "internet");
213 strcpy(slocal->s_type, "uum");
214 strcpy(slocal->s_nexthop, "%s");
215 time(&slocal->s_lastcontact);
216 strcpy(slocal->s_humannode, "Internet Gateway");
217 strcpy(slocal->s_phonenum, "");
218 strcpy(slocal->s_gdom, "");
223 * here's the routine to write the table back to disk.
225 void rewrite_syslist(void)
227 struct syslist *stemp;
232 newfp = fopen("network/mail.sysinfo", "w");
233 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
234 if (!strcasecmp(stemp->s_name, config.c_nodename)) {
235 time(&stemp->s_lastcontact);
236 strcpy(stemp->s_type, "bin");
237 strcpy(stemp->s_humannode, config.c_humannode);
238 strcpy(stemp->s_phonenum, config.c_phonenum);
240 /* remove systems we haven't heard from in a while */
241 if ((stemp->s_lastcontact == 0L)
242 || (now - stemp->s_lastcontact < EXPIRY_TIME)) {
243 fprintf(newfp, "%s\n%s %s\n",
244 stemp->s_name, stemp->s_type, stemp->s_nexthop);
245 if (strlen(stemp->s_phonenum) > 0)
246 fprintf(newfp, "phonenum %s\n", stemp->s_phonenum);
247 if (strlen(stemp->s_gdom) > 0)
248 fprintf(newfp, "gdom %s\n", stemp->s_gdom);
249 if (strlen(stemp->s_humannode) > 0)
250 fprintf(newfp, "humannode %s\n", stemp->s_humannode);
251 if (stemp->s_lastcontact > 0L)
252 fprintf(newfp, "lastcontact %ld %s",
253 (long) stemp->s_lastcontact,
254 asctime(localtime(&stemp->s_lastcontact)));
255 fprintf(newfp, "\n");
259 /* now free the list */
260 while (slist != NULL) {
268 /* call this function with the node name of a system and it returns a pointer
269 * to its syslist structure.
271 struct syslist *get_sys_ptr(char *sysname)
273 static char sysnambuf[16];
274 static struct syslist *sysptrbuf = NULL;
275 struct syslist *stemp;
277 if ((!strcmp(sysname, sysnambuf))
278 && (sysptrbuf != NULL))
281 strcpy(sysnambuf, sysname);
282 for (stemp = slist; stemp != NULL; stemp = stemp->next) {
283 if (!strcmp(sysname, stemp->s_name)) {
294 * make sure only one copy of netproc runs at a time, using lock files
296 int set_lockfile(void)
301 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
302 fscanf(lfp, "%d", &onppid);
304 if (!kill(onppid, 0) || errno == EPERM)
307 lfp = fopen(LOCKFILE, "w");
308 fprintf(lfp, "%ld\n", (long) getpid());
313 void remove_lockfile(void)
319 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
320 * cleanup() . If for some reason netproc hangs waiting for the server
321 * to clean up, the alarm clock goes off and the program exits anyway.
322 * The cleanup() routine makes a check to ensure it's not reentering, in
323 * case the ipc module looped it somehow.
325 void nq_cleanup(int e)
334 static int nested = 0;
337 signal(SIGALRM, nq_cleanup);
344 * This is implemented as a function rather than as a macro because the
345 * client-side IPC modules expect logoff() to be defined. They call logoff()
346 * when a problem connecting or staying connected to the server occurs.
354 * If there is a kill file in place, this function will process it.
356 void load_filterlist(void)
359 struct filterlist *fbuf;
362 fp = fopen("./network/filterlist", "r");
365 while (fgets(sbuf, 256, fp) != NULL) {
366 if (sbuf[0] != '#') {
367 sbuf[strlen(sbuf) - 1] = 0;
368 fbuf = (struct filterlist *)
369 malloc((long) sizeof(struct filterlist));
372 strcpy(fbuf->f_person, "*");
373 strcpy(fbuf->f_room, "*");
374 strcpy(fbuf->f_system, "*");
376 for (a = strlen(sbuf); a >= 0; --a)
381 strcpy(fbuf->f_person, sbuf);
382 strcpy(sbuf, &sbuf[p + 1]);
384 for (a = strlen(sbuf); a >= 0; --a)
389 strcpy(fbuf->f_room, sbuf);
390 strcpy(sbuf, &sbuf[p + 1]);
392 strcpy(fbuf->f_system, sbuf);
398 /* returns 1 if user/message/room combination is in the kill file */
399 int is_banned(char *k_person, char *k_room, char *k_system)
401 struct filterlist *fptr;
403 for (fptr = filter; fptr != NULL; fptr = fptr->next)
405 ((!strcasecmp(fptr->f_person, k_person)) || (!strcmp(fptr->f_person, "*")))
407 ((!strcasecmp(fptr->f_room, k_room)) || (!strcmp(fptr->f_room, "*")))
409 ((!strcasecmp(fptr->f_system, k_system)) || (!strcmp(fptr->f_system, "*")))
417 * Determine routing from sysinfo file
419 int get_sysinfo_type(char *name) {
420 struct syslist *stemp;
422 GETSN: for (stemp = slist; stemp != NULL; stemp = stemp->next) {
423 if (!strcasecmp(stemp->s_name, name)) {
424 if (!strcasecmp(stemp->s_type, "use")) {
425 strcpy(name, stemp->s_nexthop);
428 if (!strcasecmp(stemp->s_type, "bin")) {
431 if (!strcasecmp(stemp->s_type, "uum")) {
432 return (MES_INTERNET);
436 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
441 void fpgetfield(FILE * fp, char *string)
461 * Load all of the fields of a message, except the actual text, into a
462 * table in memory (so we know how to process the message).
464 void msgfind(char *msgfile, struct minfo *buffer)
466 int b, e, mtype, aflag;
472 fp = fopen(msgfile, "rb");
474 syslog(LOG_ERR, "can't open message file: %s", strerror(errno));
479 syslog(LOG_ERR, "incorrect message format");
492 BONFGM: b = getc(fp);
498 while ((bbb[0] == ' ') && (strlen(bbb) > 1))
499 strcpy(bbb, &bbb[1]);
501 strcpy(buffer->A, bbb);
502 if (strlen(userid) == 0) {
504 for (e = 0; e < strlen(userid); ++e)
505 if (userid[e] == ' ')
510 strcpy(buffer->O, bbb);
512 strcpy(buffer->C, bbb);
514 strcpy(buffer->N, bbb);
516 strcpy(buffer->S, bbb);
518 /* extract the user id from the path */
519 for (e = 0; e < strlen(bbb); ++e)
521 strcpy(userid, &bbb[e + 1]);
523 /* now find the next hop */
524 for (e = 0; e < strlen(bbb); ++e)
527 strcpy(buffer->nexthop, bbb);
530 for (e = 0; e < strlen(bbb); ++e)
533 strcpy(buffer->R, bbb);
536 strcpy(buffer->D, bbb);
538 buffer->T = atol(bbb);
540 buffer->I = atol(bbb);
542 strcpy(buffer->H, bbb);
544 strcpy(buffer->B, bbb);
546 strcpy(buffer->G, bbb);
548 strcpy(buffer->E, bbb);
553 /* NOTE: we used to use the following two lines of code to assign
554 * the timestamp as a message-ID if there was no message-ID already
555 * in the message. We don't do this anymore because it screws up
559 buffer->I = buffer->T;
567 void ship_to(char *filenm, char *sysnm)
568 { /* send spool file filenm to system sysnm */
575 syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
577 sprintf(sysflnm, "./network/systems/%s", sysnm);
578 sysflfd = fopen(sysflnm, "r");
580 syslog(LOG_ERR, "cannot open %s", sysflnm);
581 fgets(commbuf1, 99, sysflfd);
582 commbuf1[strlen(commbuf1) - 1] = 0;
584 sprintf(commbuf2, commbuf1, filenm);
589 * proc_file_transfer() - handle a simple file transfer packet
592 void proc_file_transfer(char *tname)
593 { /* name of temp file containing the whole message */
595 char dest_room[ROOMNAMELEN];
596 char subdir_name[256];
600 syslog(LOG_NOTICE, "processing network file transfer...");
602 tfp = fopen(tname, "rb");
604 syslog(LOG_ERR, "cannot open %s", tname);
611 fpgetfield(tfp, buf);
613 strcpy(dest_room, buf);
616 } while ((a != 'M') && (a >= 0));
619 syslog(LOG_ERR, "no message text for file transfer");
622 strcpy(subdir_name, "---xxx---");
623 sprintf(buf, "GOTO %s", dest_room);
627 extract(subdir_name, &buf[4], 2);
628 if (strlen(subdir_name) == 0)
629 strcpy(subdir_name, "--xxx--");
631 /* Change to the room's directory; if that fails, change to the
632 * bitbucket directory. Then run uudecode.
634 sprintf(buf, "(cd %s/files/%s || cd %s/files/%s ) ; exec %s",
635 bbs_home_directory, subdir_name,
636 bbs_home_directory, config.c_bucket_dir,
639 uud = (FILE *) popen(buf, "w");
641 syslog(LOG_ERR, "cannot open uudecode pipe");
645 fgets(buf, 128, tfp);
646 buf[strlen(buf) - 1] = 0;
647 for (a = 0; a < strlen(buf); ++a)
650 fprintf(uud, "%s\n", buf);
651 printf("netproc: %s\n", buf);
652 while (a = getc(tfp), a > 0)
660 /* send a bounce message */
661 void bounce(struct minfo *bminfo)
669 sprintf(bfilename, "./network/spoolin/bounce.%ld.%d", (long) getpid(),
671 bounce = fopen(bfilename, "wb");
674 fprintf(bounce, "%c%c%c", 0xFF, MES_NORMAL, 0);
675 fprintf(bounce, "Ppostmaster%c", 0);
676 fprintf(bounce, "T%ld%c", (long) now, 0);
677 fprintf(bounce, "APostmaster%c", 0);
678 fprintf(bounce, "OMail%c", 0);
679 fprintf(bounce, "N%s%c", config.c_nodename, 0);
680 fprintf(bounce, "H%s%c", config.c_humannode, 0);
682 if (strlen(bminfo->E) > 0) {
683 fprintf(bounce, "R%s%c", bminfo->E, 0);
685 fprintf(bounce, "R%s%c", bminfo->A, 0);
688 fprintf(bounce, "D%s%c", bminfo->N, 0);
689 fprintf(bounce, "M%s could not deliver your mail to:\n",
691 fprintf(bounce, " \n %s\n \n", bminfo->R);
692 fprintf(bounce, " because there is no such user on this system.\n");
693 fprintf(bounce, " (Unsent message does *not* follow. ");
694 fprintf(bounce, "Help to conserve bandwidth.)\n%c", 0);
702 * Generate a Message-ID string for the use table
704 void strmsgid(char *buf, struct minfo *msginfo) {
707 sprintf(buf, "%ld@%s", msginfo->I, msginfo->N);
708 for (i=0; i<strlen(buf); ++i) {
709 if (isspace(buf[i])) {
710 strcpy(&buf[i], &buf[i+1]);
712 buf[i] = tolower(buf[i]);
719 * Check the use table to see if a message has been here before.
720 * Returns 1 if the message is a duplicate; otherwise, it returns
721 * 0 and the message ID is added to the use table.
723 int already_received(GDBM_FILE ut, struct minfo *msginfo) {
729 /* We can't check for dups on a zero msgid, so just pass them through */
730 if ((msginfo->I)==0L) {
734 strmsgid(buf, msginfo);
738 mkey.dsize = strlen(buf);
740 /* Set return value to 1 if message exists */
741 if (gdbm_exists(ut, mkey)) {
745 /* Write a record into the use table for this message.
746 * Replace existing records; this keeps the timestamp fresh.
748 newrec.dptr = (char *)&now;
749 newrec.dsize = sizeof(now);
750 gdbm_store(ut, mkey, newrec, GDBM_REPLACE);
758 * Purge any old entries out of the use table.
760 * Yes, you're reading this correctly: it keeps traversing the table until
761 * it manages to do a complete pass without deleting any records. Read the
762 * gdbm man page to find out why.
765 void purge_use_table(GDBM_FILE ut) {
766 datum mkey, nextkey, therec;
767 int purged_anything = 0;
768 time_t rec_timestamp, now;
774 mkey = gdbm_firstkey(ut);
775 while (mkey.dptr != NULL) {
776 therec = gdbm_fetch(ut, mkey);
777 if (therec.dptr != NULL) {
778 memcpy(&rec_timestamp, therec.dptr,
782 if ((now - rec_timestamp) > USE_TIME) {
783 gdbm_delete(ut, mkey);
788 nextkey = gdbm_nextkey(ut, mkey);
792 } while (purged_anything != 0);
798 * process incoming files in ./network/spoolin
802 FILE *fp, *message, *testfp, *ls, *duplist;
803 static struct minfo minfo;
804 struct recentmsg recentmsg;
805 char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
808 struct syslist *stemp;
815 /* temp file names */
816 sprintf(tname, tmpnam(NULL));
817 sprintf(iname, tmpnam(NULL));
821 /* Make sure we're in the right directory */
822 chdir(bbs_home_directory);
824 /* Open the use table */
825 use_table = gdbm_open("./data/usetable.gdbm", 512,
826 GDBM_WRCREAT, 0600, 0);
827 if (use_table == NULL) {
828 syslog(LOG_ERR, "could not open use table: %s",
833 /* temporary file to contain a log of rejected dups */
836 /* Let the shell do the dirty work. Get all data from spoolin */
838 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
839 ls = popen(aaa, "r");
841 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
845 SKIP: ptr = fgets(sfilename, sizeof sfilename, ls);
847 sfilename[strlen(sfilename) - 1] = 0;
848 if (!strcmp(sfilename, ".")) goto SKIP;
849 if (!strcmp(sfilename, "..")) goto SKIP;
850 if (!strcmp(sfilename, "CVS")) goto SKIP;
853 } while (ptr != NULL);
854 PROCESS_IT: pclose(ls);
857 sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
858 syslog(LOG_NOTICE, "processing <%s>", pfilename);
860 fp = fopen(pfilename, "rb");
862 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
863 fp = fopen("/dev/null", "rb");
865 NXMSG: /* Seek to the beginning of the next message */
868 } while ((a != 255) && (a >= 0));
872 /* This crates the temporary file. */
873 message = fopen(tname, "wb");
874 if (message == NULL) {
875 syslog(LOG_ERR, "error creating %s: %s",
876 tname, strerror(errno));
879 putc(255, message); /* 0xFF (start-of-message) */
881 putc(a, message); /* type */
883 putc(a, message); /* mode */
885 FieldID = getc(fp); /* Header field ID */
886 putc(FieldID, message);
891 } while ((FieldID != 'M') && (a >= 0));
892 /* M is always last */
894 msglen = ftell(message);
897 /* process the individual mesage */
903 msgfind(tname, &minfo);
904 strncpy(recentmsg.RMnodename, minfo.N, 9);
905 recentmsg.RMnodename[9] = 0;
906 recentmsg.RMnum = minfo.I;
907 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
908 minfo.I, minfo.A, minfo.O, minfo.N);
909 if (strlen(minfo.R) > 0) {
910 syslog(LOG_NOTICE, " to <%s>", minfo.R);
911 if (strlen(minfo.D) > 0) {
912 syslog(LOG_NOTICE, " @ <%s>",
916 if (!strcasecmp(minfo.D, FQDN))
917 strcpy(minfo.D, NODENAME);
919 /* this routine updates our info on the system that sent the message */
920 stemp = get_sys_ptr(minfo.N);
921 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
922 /* add non-neighbor system to map */
923 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
925 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
928 strcpy(slist->s_name, minfo.N);
929 strcpy(slist->s_type, "use");
930 strcpy(slist->s_nexthop, minfo.nexthop);
931 time(&slist->s_lastcontact);
932 } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
933 /* add neighbor system to map */
934 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
936 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
937 testfp = fopen(aaa, "r");
938 if (testfp != NULL) {
940 stemp = (struct syslist *)
941 malloc((long) sizeof(struct syslist));
944 strcpy(slist->s_name, minfo.N);
945 strcpy(slist->s_type, "bin");
946 strcpy(slist->s_nexthop, "Mail");
947 time(&slist->s_lastcontact);
950 /* now update last contact and long node name if we can */
952 time(&stemp->s_lastcontact);
953 if (strlen(minfo.H) > 0)
954 strcpy(stemp->s_humannode, minfo.H);
955 if (strlen(minfo.B) > 0)
956 strcpy(stemp->s_phonenum, minfo.B);
957 if (strlen(minfo.G) > 0)
958 strcpy(stemp->s_gdom, minfo.G);
961 /* Check the use table; reject message if it's been here before */
962 if (already_received(use_table, &minfo)) {
963 syslog(LOG_NOTICE, "rejected duplicate message");
964 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
965 minfo.I, minfo.A, minfo.O, minfo.N);
969 /* route the message if necessary */
970 else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
971 a = get_sysinfo_type(minfo.D);
972 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
974 if (a == MES_INTERNET) {
976 syslog(LOG_NOTICE, "netmailer %s", tname);
978 execlp("./netmailer", "netmailer",
980 syslog(LOG_ERR, "error running netmailer: %s",
984 while (wait(&b) != (-1));
985 } else if (a == MES_BINARY) {
986 ship_to(tname, minfo.D);
988 /* message falls into the bit bucket? */
992 /* check to see if it's a file transfer */
993 else if (!strncasecmp(minfo.S, "FILE", 4)) {
994 proc_file_transfer(tname);
997 /* otherwise process it as a normal message */
1000 if (!strcasecmp(minfo.R, "postmaster")) {
1001 strcpy(minfo.R, "");
1002 strcpy(minfo.C, "Aide");
1004 if (strlen(minfo.R) > 0) {
1005 sprintf(buf, "GOTO _MAIL_");
1007 if (is_banned(minfo.A, minfo.C, minfo.N)) {
1008 sprintf(buf, "GOTO %s", FILTERROOM);
1010 if (strlen(minfo.C) > 0) {
1011 sprintf(buf, "GOTO %s", minfo.C);
1013 sprintf(buf, "GOTO %s", minfo.O);
1018 if (buf[0] != '2') {
1019 syslog(LOG_ERR, "%s", buf);
1020 sprintf(buf, "GOTO _BITBUCKET_");
1024 /* Open the temporary file containing the message */
1025 message = fopen(tname, "rb");
1026 if (message == NULL) {
1027 syslog(LOG_ERR, "cannot open %s: %s",
1028 tname, strerror(errno));
1032 /* Transmit the message to the server */
1033 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1036 if (!strncmp(buf, "570", 3)) {
1037 /* no such user, do a bounce */
1040 if (buf[0] == '7') {
1041 /* Always use the server's idea of the message length,
1042 * even though they should both be identical */
1043 msglen = atol(&buf[4]);
1044 while (msglen > 0L) {
1045 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1046 if (fread(buf, bloklen, 1, message) < 1) {
1048 "error trying to read %d bytes: %s",
1049 bloklen, strerror(errno));
1051 serv_write(buf, bloklen);
1052 msglen = msglen - (long) bloklen;
1057 syslog(LOG_ERR, "%s", buf);
1069 } while (ptr != NULL);
1072 purge_use_table(use_table);
1073 gdbm_close(use_table);
1077 * If dups were rejected, post a message saying so
1079 if (ftell(duplist)!=0L) {
1080 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1082 fprintf(fp, "%cA%c", 255, 1);
1083 fprintf(fp, "T%ld%c", time(NULL), 0);
1084 fprintf(fp, "ACitadel%c", 0);
1085 fprintf(fp, "OAide%cM", 0);
1086 fprintf(fp, "The following duplicate messages"
1087 " were rejected:\n \n");
1089 while (fgets(buf, sizeof(buf), duplist) != NULL) {
1090 buf[strlen(buf)-1] = 0;
1091 fprintf(fp, " %s\n", buf);
1093 fprintf(fp, "%c", 0);
1103 /* Checks to see whether its ok to send */
1104 /* Returns 1 for ok, send message */
1105 /* Returns 0 if message already there */
1106 int checkpath(char *path, char *sys)
1114 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1116 for (a = 0; a < strlen(path); ++a) {
1117 if (!strncmp(&path[a], sys2, strlen(sys2)))
1124 * implement split horizon algorithm
1126 int ismsgok(long int mpos, FILE * mmfp, char *sysname)
1129 int ok = 0; /* fail safe - no path, don't send it */
1132 fseek(mmfp, mpos, 0);
1133 if (getc(mmfp) != 255)
1138 while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1139 fpgetfield(mmfp, fbuf);
1141 ok = checkpath(fbuf, sysname);
1145 syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1152 /* spool list of messages to a file */
1153 /* returns # of msgs spooled */
1154 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1156 struct msglist *cmptr;
1160 int msgs_spooled = 0;
1167 strcpy(curr_rm, "");
1169 /* for each message in the list... */
1170 for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1172 /* make sure we're in the correct room... */
1173 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1174 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1177 if (buf[0] == '2') {
1178 strcpy(curr_rm, cmptr->m_rmname);
1180 syslog(LOG_ERR, "%s", buf);
1183 /* download the message from the server... */
1185 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1188 if (buf[0] == '6') { /* read the msg */
1189 msg_len = atol(&buf[4]);
1190 while (msg_len > 0L) {
1191 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1192 serv_read(buf, blok_len);
1193 fwrite(buf, blok_len, 1, mmfp);
1194 msg_len = msg_len - (long) blok_len;
1196 } else { /* or print the err */
1197 syslog(LOG_ERR, "%s", buf);
1202 if (ismsgok(0L, mmfp, sysname)) {
1206 fread(fbuf, 3, 1, mmfp);
1207 fwrite(fbuf, 3, 1, destfp);
1208 while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1211 fpgetfield(mmfp, fbuf);
1213 fprintf(destfp, "%s!", NODENAME);
1215 fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1218 fprintf(destfp, "C%s%c",
1219 cmptr->m_rmname, 0);
1230 return (msgs_spooled);
1233 void outprocess(char *sysname)
1234 { /* send new room messages to sysname */
1237 char shiptocmd[128];
1241 struct msglist *cmlist = NULL;
1242 struct rmlist *crmlist = NULL;
1243 struct rmlist *rmptr, *rmptr2;
1244 struct msglist *cmptr, *cmptr2;
1245 FILE *sysflfp, *tempflfp;
1249 sprintf(tempflnm, tmpnam(NULL));
1250 tempflfp = fopen(tempflnm, "w");
1251 if (tempflfp == NULL)
1256 * Read system file for node in question and put together room list
1258 sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1259 sysflfp = fopen(sysflnm, "r");
1260 if (sysflfp == NULL)
1262 fgets(shiptocmd, 128, sysflfp);
1263 shiptocmd[strlen(shiptocmd) - 1] = 0;
1264 while (!feof(sysflfp)) {
1265 if (fgets(srmname, 32, sysflfp) == NULL)
1267 srmname[strlen(srmname) - 1] = 0;
1268 fgets(lbuf, 32, sysflfp);
1269 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1271 strcpy(rmptr->rm_name, srmname);
1272 strip_trailing_whitespace(rmptr->rm_name);
1273 rmptr->rm_lastsent = atol(lbuf);
1274 if (crmlist == NULL)
1276 else if (!strcasecmp(rmptr->rm_name, "control")) {
1277 /* control has to be first in room list */
1278 rmptr->next = crmlist;
1282 while (rmptr2->next != NULL)
1283 rmptr2 = rmptr2->next;
1284 rmptr2->next = rmptr;
1290 * Assemble list of messages to be spooled
1292 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1294 sprintf(buf, "GOTO %s", rmptr->rm_name);
1297 if (buf[0] != '2') {
1298 syslog(LOG_ERR, "%s", buf);
1300 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1304 while (serv_gets(buf), strcmp(buf, "000")) {
1305 thismsg = atol(buf);
1306 if (thismsg > (rmptr->rm_lastsent)) {
1307 rmptr->rm_lastsent = thismsg;
1309 cmptr = (struct msglist *)
1310 malloc(sizeof(struct msglist));
1312 cmptr->m_num = thismsg;
1313 strcpy(cmptr->m_rmname, rmptr->rm_name);
1319 while (cmptr2->next != NULL)
1320 cmptr2 = cmptr2->next;
1321 cmptr2->next = cmptr;
1324 } else { /* print error from "msgs all" */
1325 syslog(LOG_ERR, "%s", buf);
1331 cmptr2 = cmlist; /* this loop counts the messages */
1332 while (cmptr2 != NULL) {
1334 cmptr2 = cmptr2->next;
1336 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1337 outgoing_msgs, sysname);
1340 * Spool out the messages, but only if there are any.
1342 if (outgoing_msgs != 0)
1343 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1344 syslog(LOG_NOTICE, "%d messages actually spooled",
1348 * Deallocate list of spooled messages.
1350 while (cmlist != NULL) {
1351 cmptr = cmlist->next;
1357 * Rewrite system file and deallocate room list.
1359 syslog(LOG_NOTICE, "Spooling...");
1360 sysflfp = fopen(sysflnm, "w");
1361 fprintf(sysflfp, "%s\n", shiptocmd);
1362 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1363 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1365 while (crmlist != NULL) {
1366 rmptr = crmlist->next;
1372 * Close temporary file, ship it out, and return
1375 if (outgoing_msgs != 0)
1376 ship_to(tempflnm, sysname);
1382 * Connect netproc to the Citadel server running on this computer.
1384 void np_attach_to_server(void)
1389 {"netproc", "localhost", NULL, NULL};
1391 syslog(LOG_NOTICE, "Attaching to server...");
1392 sprintf(portname, "%d", config.c_port_number);
1394 attach_to_server(3, args);
1396 syslog(LOG_NOTICE, "%s", &buf[4]);
1397 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1400 syslog(LOG_NOTICE, "%s", &buf[4]);
1401 if (buf[0] != '2') {
1411 int main(int argc, char **argv)
1416 int import_only = 0; /* if set to 1, don't export anything */
1418 openlog("netproc", LOG_PID, LOG_USER);
1419 strcpy(bbs_home_directory, BBSDIR);
1422 * Change directories if specified
1424 for (a = 1; a < argc; ++a) {
1425 if (!strncmp(argv[a], "-h", 2)) {
1426 strcpy(bbs_home_directory, argv[a]);
1427 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1429 } else if (!strcmp(argv[a], "-i")) {
1432 fprintf(stderr, "netproc: usage: ");
1433 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1440 if (set_lockfile() != 0) {
1441 syslog(LOG_NOTICE, "lock file exists: already running");
1444 signal(SIGINT, cleanup);
1445 signal(SIGQUIT, cleanup);
1446 signal(SIGHUP, cleanup);
1447 signal(SIGTERM, cleanup);
1449 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1451 np_attach_to_server();
1454 if (load_syslist() != 0)
1455 syslog(LOG_ERR, "cannot load sysinfo");
1456 setup_special_nodes();
1458 inprocess(); /* first collect incoming stuff */
1460 if (import_only != 1) {
1461 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1462 if (allfp != NULL) {
1463 while (fgets(allst, 32, allfp) != NULL) {
1464 allst[strlen(allst) - 1] = 0;
1469 /* import again in case anything new was generated */
1473 syslog(LOG_NOTICE, "processing ended.");