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, "*")))
416 int get_sysinfo_type(char *name)
417 { /* determine routing from sysinfo file */
418 struct syslist *stemp;
419 GETSN:for (stemp = slist; stemp != NULL; stemp = stemp->next) {
420 if (!strcasecmp(stemp->s_name, name)) {
421 if (!strcasecmp(stemp->s_type, "use")) {
422 strcpy(name, stemp->s_nexthop);
425 if (!strcasecmp(stemp->s_type, "bin")) {
428 if (!strcasecmp(stemp->s_type, "uum")) {
429 return (MES_INTERNET);
433 syslog(LOG_ERR, "cannot find system '%s' in mail.sysinfo", name);
438 void fpgetfield(FILE * fp, char *string)
458 * Load all of the fields of a message, except the actual text, into a
459 * table in memory (so we know how to process the message).
461 void msgfind(char *msgfile, struct minfo *buffer)
463 int b, e, mtype, aflag;
469 fp = fopen(msgfile, "rb");
471 syslog(LOG_ERR, "can't open message file: %s", strerror(errno));
476 syslog(LOG_ERR, "incorrect message format");
495 while ((bbb[0] == ' ') && (strlen(bbb) > 1))
496 strcpy(bbb, &bbb[1]);
498 strcpy(buffer->A, bbb);
499 if (strlen(userid) == 0) {
501 for (e = 0; e < strlen(userid); ++e)
502 if (userid[e] == ' ')
507 strcpy(buffer->O, bbb);
509 strcpy(buffer->C, bbb);
511 strcpy(buffer->N, bbb);
513 strcpy(buffer->S, bbb);
515 /* extract the user id from the path */
516 for (e = 0; e < strlen(bbb); ++e)
518 strcpy(userid, &bbb[e + 1]);
520 /* now find the next hop */
521 for (e = 0; e < strlen(bbb); ++e)
524 strcpy(buffer->nexthop, bbb);
527 for (e = 0; e < strlen(bbb); ++e)
530 strcpy(buffer->R, bbb);
533 strcpy(buffer->D, bbb);
535 buffer->T = atol(bbb);
537 buffer->I = atol(bbb);
539 strcpy(buffer->H, bbb);
541 strcpy(buffer->B, bbb);
543 strcpy(buffer->G, bbb);
545 strcpy(buffer->E, bbb);
548 END:if (buffer->I == 0L)
549 buffer->I = buffer->T;
553 void ship_to(char *filenm, char *sysnm)
554 { /* send spool file filenm to system sysnm */
561 syslog(LOG_NOTICE, "shipping %s to %s", filenm, sysnm);
563 sprintf(sysflnm, "./network/systems/%s", sysnm);
564 sysflfd = fopen(sysflnm, "r");
566 syslog(LOG_ERR, "cannot open %s", sysflnm);
567 fgets(commbuf1, 99, sysflfd);
568 commbuf1[strlen(commbuf1) - 1] = 0;
570 sprintf(commbuf2, commbuf1, filenm);
575 * proc_file_transfer() - handle a simple file transfer packet
578 void proc_file_transfer(char *tname)
579 { /* name of temp file containing the whole message */
581 char dest_room[ROOMNAMELEN];
582 char subdir_name[256];
586 syslog(LOG_NOTICE, "processing network file transfer...");
588 tfp = fopen(tname, "rb");
590 syslog(LOG_ERR, "cannot open %s", tname);
597 fpgetfield(tfp, buf);
599 strcpy(dest_room, buf);
602 } while ((a != 'M') && (a >= 0));
605 syslog(LOG_ERR, "no message text for file transfer");
608 strcpy(subdir_name, "---xxx---");
609 sprintf(buf, "GOTO %s", dest_room);
613 extract(subdir_name, &buf[4], 2);
614 if (strlen(subdir_name) == 0)
615 strcpy(subdir_name, "--xxx--");
617 /* Change to the room's directory; if that fails, change to the
618 * bitbucket directory. Then run uudecode.
620 sprintf(buf, "(cd %s/files/%s || cd %s/files/%s ) ; exec %s",
621 bbs_home_directory, subdir_name,
622 bbs_home_directory, config.c_bucket_dir,
625 uud = (FILE *) popen(buf, "w");
627 syslog(LOG_ERR, "cannot open uudecode pipe");
631 fgets(buf, 128, tfp);
632 buf[strlen(buf) - 1] = 0;
633 for (a = 0; a < strlen(buf); ++a)
636 fprintf(uud, "%s\n", buf);
637 printf("netproc: %s\n", buf);
638 while (a = getc(tfp), a > 0)
646 /* send a bounce message */
647 void bounce(struct minfo *bminfo)
655 sprintf(bfilename, "./network/spoolin/bounce.%ld.%d", (long) getpid(),
657 bounce = fopen(bfilename, "wb");
660 fprintf(bounce, "%c%c%c", 0xFF, MES_NORMAL, 0);
661 fprintf(bounce, "Ppostmaster%c", 0);
662 fprintf(bounce, "T%ld%c", (long) now, 0);
663 fprintf(bounce, "APostmaster%c", 0);
664 fprintf(bounce, "OMail%c", 0);
665 fprintf(bounce, "N%s%c", config.c_nodename, 0);
666 fprintf(bounce, "H%s%c", config.c_humannode, 0);
668 if (strlen(bminfo->E) > 0) {
669 fprintf(bounce, "R%s%c", bminfo->E, 0);
671 fprintf(bounce, "R%s%c", bminfo->A, 0);
674 fprintf(bounce, "D%s%c", bminfo->N, 0);
675 fprintf(bounce, "M%s could not deliver your mail to:\n",
677 fprintf(bounce, " \n %s\n \n", bminfo->R);
678 fprintf(bounce, " because there is no such user on this system.\n");
679 fprintf(bounce, " (Unsent message does *not* follow. ");
680 fprintf(bounce, "Help to conserve bandwidth.)\n%c", 0);
688 * Generate a Message-ID string for the use table
690 void strmsgid(char *buf, struct minfo *msginfo) {
693 sprintf(buf, "%ld@%s", msginfo->I, msginfo->N);
694 for (i=0; i<strlen(buf); ++i) {
695 if (isspace(buf[i])) {
696 strcpy(&buf[i], &buf[i+1]);
698 buf[i] = tolower(buf[i]);
705 * Check the use table to see if a message has been here before.
706 * Returns 1 if the message is a duplicate; otherwise, it returns
707 * 0 and the message ID is added to the use table.
709 int already_received(GDBM_FILE ut, struct minfo *msginfo) {
715 /* We can't check for dups on a zero msgid, so just pass them through */
716 if ((msginfo->I)==0L) {
720 strmsgid(buf, msginfo);
724 mkey.dsize = strlen(buf);
726 /* Set return value to 1 if message exists */
727 if (gdbm_exists(ut, mkey)) {
731 /* Write a record into the use table for this message.
732 * Replace existing records; this keeps the timestamp fresh.
734 newrec.dptr = (char *)&now;
735 newrec.dsize = sizeof(now);
736 gdbm_store(ut, mkey, newrec, GDBM_REPLACE);
744 * Purge any old entries out of the use table.
746 * Yes, you're reading this correctly: it keeps traversing the table until
747 * it manages to do a complete pass without deleting any records. Read the
748 * gdbm man page to find out why.
751 void purge_use_table(GDBM_FILE ut) {
752 datum mkey, nextkey, therec;
753 int purged_anything = 0;
754 time_t rec_timestamp, now;
760 mkey = gdbm_firstkey(ut);
761 while (mkey.dptr != NULL) {
762 therec = gdbm_fetch(ut, mkey);
763 if (therec.dptr != NULL) {
764 memcpy(&rec_timestamp, therec.dptr,
768 if ((now - rec_timestamp) > USE_TIME) {
769 gdbm_delete(ut, mkey);
774 nextkey = gdbm_nextkey(ut, mkey);
778 } while (purged_anything != 0);
784 * process incoming files in ./network/spoolin
788 FILE *fp, *message, *testfp, *ls, *duplist;
789 static struct minfo minfo;
790 struct recentmsg recentmsg;
791 char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
794 struct syslist *stemp;
801 /* temp file names */
802 sprintf(tname, tmpnam(NULL));
803 sprintf(iname, tmpnam(NULL));
807 /* Make sure we're in the right directory */
808 chdir(bbs_home_directory);
810 /* Open the use table */
811 use_table = gdbm_open("./data/usetable.gdbm", 512,
812 GDBM_WRCREAT, 0600, 0);
813 if (use_table == NULL) {
814 syslog(LOG_ERR, "could not open use table: %s",
819 /* temporary file to contain a log of rejected dups */
822 /* Let the shell do the dirty work. Get all data from spoolin */
824 sprintf(aaa, "cd %s/network/spoolin; ls", bbs_home_directory);
825 ls = popen(aaa, "r");
827 syslog(LOG_ERR, "could not open dir cmd: %s", strerror(errno));
831 SKIP: ptr = fgets(sfilename, sizeof sfilename, ls);
833 sfilename[strlen(sfilename) - 1] = 0;
834 if (!strcmp(sfilename, ".")) goto SKIP;
835 if (!strcmp(sfilename, "..")) goto SKIP;
836 if (!strcmp(sfilename, "CVS")) goto SKIP;
839 } while (ptr != NULL);
840 PROCESS_IT: pclose(ls);
843 sprintf(pfilename, "%s/network/spoolin/%s", bbs_home_directory, sfilename);
844 syslog(LOG_NOTICE, "processing <%s>", pfilename);
846 fp = fopen(pfilename, "rb");
848 syslog(LOG_ERR, "cannot open %s: %s", pfilename, strerror(errno));
849 fp = fopen("/dev/null", "rb");
851 NXMSG: /* Seek to the beginning of the next message */
854 } while ((a != 255) && (a >= 0));
858 /* This crates the temporary file. */
859 message = fopen(tname, "wb");
860 if (message == NULL) {
861 syslog(LOG_ERR, "error creating %s: %s",
862 tname, strerror(errno));
865 putc(255, message); /* 0xFF (start-of-message) */
867 putc(a, message); /* type */
869 putc(a, message); /* mode */
871 FieldID = getc(fp); /* Header field ID */
872 putc(FieldID, message);
877 } while ((FieldID != 'M') && (a >= 0));
878 /* M is always last */
880 msglen = ftell(message);
883 /* process the individual mesage */
889 msgfind(tname, &minfo);
890 strncpy(recentmsg.RMnodename, minfo.N, 9);
891 recentmsg.RMnodename[9] = 0;
892 recentmsg.RMnum = minfo.I;
893 syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
894 minfo.I, minfo.A, minfo.O, minfo.N);
895 if (strlen(minfo.R) > 0) {
896 syslog(LOG_NOTICE, " to <%s>", minfo.R);
897 if (strlen(minfo.D) > 0) {
898 syslog(LOG_NOTICE, " @ <%s>",
902 if (!strcasecmp(minfo.D, FQDN))
903 strcpy(minfo.D, NODENAME);
905 /* this routine updates our info on the system that sent the message */
906 stemp = get_sys_ptr(minfo.N);
907 if ((stemp == NULL) && (get_sys_ptr(minfo.nexthop) != NULL)) {
908 /* add non-neighbor system to map */
909 syslog(LOG_NOTICE, "Adding non-neighbor system <%s> to map",
911 stemp = (struct syslist *) malloc((long) sizeof(struct syslist));
914 strcpy(slist->s_name, minfo.N);
915 strcpy(slist->s_type, "use");
916 strcpy(slist->s_nexthop, minfo.nexthop);
917 time(&slist->s_lastcontact);
918 } else if ((stemp == NULL) && (!strcasecmp(minfo.N, minfo.nexthop))) {
919 /* add neighbor system to map */
920 syslog(LOG_NOTICE, "Adding neighbor system <%s> to map",
922 sprintf(aaa, "%s/network/systems/%s", bbs_home_directory, minfo.N);
923 testfp = fopen(aaa, "r");
924 if (testfp != NULL) {
926 stemp = (struct syslist *)
927 malloc((long) sizeof(struct syslist));
930 strcpy(slist->s_name, minfo.N);
931 strcpy(slist->s_type, "bin");
932 strcpy(slist->s_nexthop, "Mail");
933 time(&slist->s_lastcontact);
936 /* now update last contact and long node name if we can */
938 time(&stemp->s_lastcontact);
939 if (strlen(minfo.H) > 0)
940 strcpy(stemp->s_humannode, minfo.H);
941 if (strlen(minfo.B) > 0)
942 strcpy(stemp->s_phonenum, minfo.B);
943 if (strlen(minfo.G) > 0)
944 strcpy(stemp->s_gdom, minfo.G);
947 /* Check the use table; reject message if it's been here before */
948 if (already_received(use_table, &minfo)) {
949 syslog(LOG_NOTICE, "rejected duplicate message");
950 fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
951 minfo.I, minfo.A, minfo.O, minfo.N);
955 /* route the message if necessary */
956 else if ((strcasecmp(minfo.D, NODENAME)) && (minfo.D[0] != 0)) {
957 a = get_sysinfo_type(minfo.D);
958 syslog(LOG_NOTICE, "routing message to system <%s>", minfo.D);
960 if (a == MES_INTERNET) {
962 syslog(LOG_NOTICE, "netmailer %s", tname);
964 execlp("./netmailer", "netmailer",
966 syslog(LOG_ERR, "error running netmailer: %s",
970 while (wait(&b) != (-1));
971 } else if (a == MES_BINARY) {
972 ship_to(tname, minfo.D);
974 /* message falls into the bit bucket? */
978 /* check to see if it's a file transfer */
979 else if (!strncasecmp(minfo.S, "FILE", 4)) {
980 proc_file_transfer(tname);
983 /* otherwise process it as a normal message */
986 if (!strcasecmp(minfo.R, "postmaster")) {
988 strcpy(minfo.C, "Aide");
990 if (strlen(minfo.R) > 0) {
991 sprintf(buf, "GOTO _MAIL_");
993 if (is_banned(minfo.A, minfo.C, minfo.N)) {
994 sprintf(buf, "GOTO %s", FILTERROOM);
996 if (strlen(minfo.C) > 0) {
997 sprintf(buf, "GOTO %s", minfo.C);
999 sprintf(buf, "GOTO %s", minfo.O);
1004 if (buf[0] != '2') {
1005 syslog(LOG_ERR, "%s", buf);
1006 sprintf(buf, "GOTO _BITBUCKET_");
1010 /* Open the temporary file containing the message */
1011 message = fopen(tname, "rb");
1012 if (message == NULL) {
1013 syslog(LOG_ERR, "cannot open %s: %s",
1014 tname, strerror(errno));
1018 /* Transmit the message to the server */
1019 sprintf(buf, "ENT3 1|%s|%ld", minfo.R, msglen);
1022 if (!strncmp(buf, "570", 3)) {
1023 /* no such user, do a bounce */
1026 if (buf[0] == '7') {
1027 /* Always use the server's idea of the message length,
1028 * even though they should both be identical */
1029 msglen = atol(&buf[4]);
1030 while (msglen > 0L) {
1031 bloklen = ((msglen >= 255L) ? 255 : ((int) msglen));
1032 if (fread(buf, bloklen, 1, message) < 1) {
1034 "error trying to read %d bytes: %s",
1035 bloklen, strerror(errno));
1037 serv_write(buf, bloklen);
1038 msglen = msglen - (long) bloklen;
1043 syslog(LOG_ERR, "%s", buf);
1055 } while (ptr != NULL);
1058 purge_use_table(use_table);
1059 gdbm_close(use_table);
1063 * If dups were rejected, post a message saying so
1065 if (ftell(duplist)!=0L) {
1066 fp = fopen("./network/spoolin/ctdl_rejects", "ab");
1068 fprintf(fp, "%cA%c", 255, 1);
1069 fprintf(fp, "T%ld%c", time(NULL), 0);
1070 fprintf(fp, "ACitadel%c", 0);
1071 fprintf(fp, "OAide%cM", 0);
1072 fprintf(fp, "The following duplicate messages"
1073 " were rejected:\n \n");
1075 while (fgets(buf, sizeof(buf), duplist) != NULL) {
1076 buf[strlen(buf)-1] = 0;
1077 fprintf(fp, " %s\n", buf);
1079 fprintf(fp, "%c", 0);
1089 /* Checks to see whether its ok to send */
1090 /* Returns 1 for ok, send message */
1091 /* Returns 0 if message already there */
1092 int checkpath(char *path, char *sys)
1100 syslog(LOG_NOTICE, "checkpath <%s> <%s> ... ", path, sys);
1102 for (a = 0; a < strlen(path); ++a) {
1103 if (!strncmp(&path[a], sys2, strlen(sys2)))
1110 * implement split horizon algorithm
1112 int ismsgok(long int mpos, FILE * mmfp, char *sysname)
1115 int ok = 0; /* fail safe - no path, don't send it */
1118 fseek(mmfp, mpos, 0);
1119 if (getc(mmfp) != 255)
1124 while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
1125 fpgetfield(mmfp, fbuf);
1127 ok = checkpath(fbuf, sysname);
1131 syslog(LOG_NOTICE, "%s", ((ok) ? "SEND" : "(no)"));
1138 /* spool list of messages to a file */
1139 /* returns # of msgs spooled */
1140 int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
1142 struct msglist *cmptr;
1146 int msgs_spooled = 0;
1153 strcpy(curr_rm, "");
1155 /* for each message in the list... */
1156 for (cmptr = cmlist; cmptr != NULL; cmptr = cmptr->next) {
1158 /* make sure we're in the correct room... */
1159 if (strcasecmp(curr_rm, cmptr->m_rmname)) {
1160 sprintf(buf, "GOTO %s", cmptr->m_rmname);
1163 if (buf[0] == '2') {
1164 strcpy(curr_rm, cmptr->m_rmname);
1166 syslog(LOG_ERR, "%s", buf);
1169 /* download the message from the server... */
1171 sprintf(buf, "MSG3 %ld", cmptr->m_num);
1174 if (buf[0] == '6') { /* read the msg */
1175 msg_len = atol(&buf[4]);
1176 while (msg_len > 0L) {
1177 blok_len = ((msg_len >= 256L) ? 256 : (int) msg_len);
1178 serv_read(buf, blok_len);
1179 fwrite(buf, blok_len, 1, mmfp);
1180 msg_len = msg_len - (long) blok_len;
1182 } else { /* or print the err */
1183 syslog(LOG_ERR, "%s", buf);
1188 if (ismsgok(0L, mmfp, sysname)) {
1192 fread(fbuf, 3, 1, mmfp);
1193 fwrite(fbuf, 3, 1, destfp);
1194 while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
1197 fpgetfield(mmfp, fbuf);
1199 fprintf(destfp, "%s!", NODENAME);
1201 fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
1204 fprintf(destfp, "C%s%c",
1205 cmptr->m_rmname, 0);
1216 return (msgs_spooled);
1219 void outprocess(char *sysname)
1220 { /* send new room messages to sysname */
1223 char shiptocmd[128];
1227 struct msglist *cmlist = NULL;
1228 struct rmlist *crmlist = NULL;
1229 struct rmlist *rmptr, *rmptr2;
1230 struct msglist *cmptr, *cmptr2;
1231 FILE *sysflfp, *tempflfp;
1235 sprintf(tempflnm, tmpnam(NULL));
1236 tempflfp = fopen(tempflnm, "w");
1237 if (tempflfp == NULL)
1242 * Read system file for node in question and put together room list
1244 sprintf(sysflnm, "%s/network/systems/%s", bbs_home_directory, sysname);
1245 sysflfp = fopen(sysflnm, "r");
1246 if (sysflfp == NULL)
1248 fgets(shiptocmd, 128, sysflfp);
1249 shiptocmd[strlen(shiptocmd) - 1] = 0;
1250 while (!feof(sysflfp)) {
1251 if (fgets(srmname, 32, sysflfp) == NULL)
1253 srmname[strlen(srmname) - 1] = 0;
1254 fgets(lbuf, 32, sysflfp);
1255 rmptr = (struct rmlist *) malloc(sizeof(struct rmlist));
1257 strcpy(rmptr->rm_name, srmname);
1258 strip_trailing_whitespace(rmptr->rm_name);
1259 rmptr->rm_lastsent = atol(lbuf);
1260 if (crmlist == NULL)
1262 else if (!strcasecmp(rmptr->rm_name, "control")) {
1263 /* control has to be first in room list */
1264 rmptr->next = crmlist;
1268 while (rmptr2->next != NULL)
1269 rmptr2 = rmptr2->next;
1270 rmptr2->next = rmptr;
1276 * Assemble list of messages to be spooled
1278 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next) {
1280 sprintf(buf, "GOTO %s", rmptr->rm_name);
1283 if (buf[0] != '2') {
1284 syslog(LOG_ERR, "%s", buf);
1286 sprintf(buf, "MSGS GT|%ld", rmptr->rm_lastsent);
1290 while (serv_gets(buf), strcmp(buf, "000")) {
1291 thismsg = atol(buf);
1292 if (thismsg > (rmptr->rm_lastsent)) {
1293 rmptr->rm_lastsent = thismsg;
1295 cmptr = (struct msglist *)
1296 malloc(sizeof(struct msglist));
1298 cmptr->m_num = thismsg;
1299 strcpy(cmptr->m_rmname, rmptr->rm_name);
1305 while (cmptr2->next != NULL)
1306 cmptr2 = cmptr2->next;
1307 cmptr2->next = cmptr;
1310 } else { /* print error from "msgs all" */
1311 syslog(LOG_ERR, "%s", buf);
1317 cmptr2 = cmlist; /* this loop counts the messages */
1318 while (cmptr2 != NULL) {
1320 cmptr2 = cmptr2->next;
1322 syslog(LOG_NOTICE, "%d messages to be spooled to %s",
1323 outgoing_msgs, sysname);
1326 * Spool out the messages, but only if there are any.
1328 if (outgoing_msgs != 0)
1329 outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
1330 syslog(LOG_NOTICE, "%d messages actually spooled",
1334 * Deallocate list of spooled messages.
1336 while (cmlist != NULL) {
1337 cmptr = cmlist->next;
1343 * Rewrite system file and deallocate room list.
1345 syslog(LOG_NOTICE, "Spooling...");
1346 sysflfp = fopen(sysflnm, "w");
1347 fprintf(sysflfp, "%s\n", shiptocmd);
1348 for (rmptr = crmlist; rmptr != NULL; rmptr = rmptr->next)
1349 fprintf(sysflfp, "%s\n%ld\n", rmptr->rm_name, rmptr->rm_lastsent);
1351 while (crmlist != NULL) {
1352 rmptr = crmlist->next;
1358 * Close temporary file, ship it out, and return
1361 if (outgoing_msgs != 0)
1362 ship_to(tempflnm, sysname);
1368 * Connect netproc to the Citadel server running on this computer.
1370 void np_attach_to_server(void)
1375 {"netproc", "localhost", NULL, NULL};
1377 syslog(LOG_NOTICE, "Attaching to server...");
1378 sprintf(portname, "%d", config.c_port_number);
1380 attach_to_server(3, args);
1382 syslog(LOG_NOTICE, "%s", &buf[4]);
1383 sprintf(buf, "IPGM %d", config.c_ipgm_secret);
1386 syslog(LOG_NOTICE, "%s", &buf[4]);
1387 if (buf[0] != '2') {
1397 int main(int argc, char **argv)
1402 int import_only = 0; /* if set to 1, don't export anything */
1404 openlog("netproc", LOG_PID, LOG_USER);
1405 strcpy(bbs_home_directory, BBSDIR);
1408 * Change directories if specified
1410 for (a = 1; a < argc; ++a) {
1411 if (!strncmp(argv[a], "-h", 2)) {
1412 strcpy(bbs_home_directory, argv[a]);
1413 strcpy(bbs_home_directory, &bbs_home_directory[2]);
1415 } else if (!strcmp(argv[a], "-i")) {
1418 fprintf(stderr, "netproc: usage: ");
1419 fprintf(stderr, "netproc [-hHomeDir] [-i]\n");
1426 if (set_lockfile() != 0) {
1427 syslog(LOG_NOTICE, "lock file exists: already running");
1430 signal(SIGINT, cleanup);
1431 signal(SIGQUIT, cleanup);
1432 signal(SIGHUP, cleanup);
1433 signal(SIGTERM, cleanup);
1435 syslog(LOG_NOTICE, "started. pid=%d", getpid());
1437 np_attach_to_server();
1440 if (load_syslist() != 0)
1441 syslog(LOG_ERR, "cannot load sysinfo");
1442 setup_special_nodes();
1444 inprocess(); /* first collect incoming stuff */
1446 if (import_only != 1) {
1447 allfp = (FILE *) popen("cd ./network/systems; ls", "r");
1448 if (allfp != NULL) {
1449 while (fgets(allst, 32, allfp) != NULL) {
1450 allst[strlen(allst) - 1] = 0;
1455 /* import again in case anything new was generated */
1459 syslog(LOG_NOTICE, "processing ended.");