]> code.citadel.org Git - citadel.git/blob - citadel/serv_network.c
* flush networker caching list on exit
[citadel.git] / citadel / serv_network.c
1 /*
2  * $Id$ 
3  *
4  * This module handles shared rooms, inter-Citadel mail, and outbound
5  * mailing list processing.
6  *
7  * Copyright (C) 2000-2005 by Art Cancro and others.
8  * This code is released under the terms of the GNU General Public License.
9  *
10  * ** NOTE **   A word on the S_NETCONFIGS semaphore:
11  * This is a fairly high-level type of critical section.  It ensures that no
12  * two threads work on the netconfigs files at the same time.  Since we do
13  * so many things inside these, here are the rules:
14  *  1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
15  *  2. Do *not* perform any I/O with the client during these sections.
16  *
17  */
18
19 /*
20  * Duration of time (in seconds) after which pending list subscribe/unsubscribe
21  * requests that have not been confirmed will be deleted.
22  */
23 #define EXP     259200  /* three days */
24
25 #include "sysdep.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <dirent.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # if HAVE_SYS_TIME_H
42 #  include <sys/time.h>
43 # else
44 #  include <time.h>
45 # endif
46 #endif
47
48 #include <sys/wait.h>
49 #include <string.h>
50 #include <limits.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "sysdep_decls.h"
54 #include "citserver.h"
55 #include "support.h"
56 #include "config.h"
57 #include "serv_extensions.h"
58 #include "room_ops.h"
59 #include "user_ops.h"
60 #include "policy.h"
61 #include "database.h"
62 #include "msgbase.h"
63 #include "tools.h"
64 #include "internet_addressing.h"
65 #include "serv_network.h"
66 #include "clientsocket.h"
67 #include "file_ops.h"
68 #include "citadel_dirs.h"
69
70 #ifndef HAVE_SNPRINTF
71 #include "snprintf.h"
72 #endif
73
74 /* Nonzero while we are doing network processing */
75 static int doing_queue = 0;
76
77 /*
78  * When we do network processing, it's accomplished in two passes; one to
79  * gather a list of rooms and one to actually do them.  It's ok that rplist
80  * is global; we have a mutex that keeps it safe.
81  */
82 struct RoomProcList *rplist = NULL;
83
84 /*
85  * We build a map of network nodes during processing.
86  */
87 struct NetMap *the_netmap = NULL;
88 int netmap_changed = 0;
89 char *working_ignetcfg = NULL;
90
91 /*
92  * Load or refresh the Citadel network (IGnet) configuration for this node.
93  */
94 void load_working_ignetcfg(void) {
95         char *cfg;
96         char *oldcfg;
97
98         cfg = CtdlGetSysConfig(IGNETCFG);
99         if (cfg == NULL) {
100                 cfg = strdup("");
101         }
102
103         oldcfg = working_ignetcfg;
104         working_ignetcfg = cfg;
105         if (oldcfg != NULL) {
106                 free(oldcfg);
107         }
108 }
109
110
111
112
113
114 /*
115  * Keep track of what messages to reject
116  */
117 struct FilterList *load_filter_list(void) {
118         char *serialized_list = NULL;
119         int i;
120         char buf[SIZ];
121         struct FilterList *newlist = NULL;
122         struct FilterList *nptr;
123
124         serialized_list = CtdlGetSysConfig(FILTERLIST);
125         if (serialized_list == NULL) return(NULL); /* if null, no entries */
126
127         /* Use the string tokenizer to grab one line at a time */
128         for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
129                 extract_token(buf, serialized_list, i, '\n', sizeof buf);
130                 nptr = (struct FilterList *) malloc(sizeof(struct FilterList));
131                 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
132                 striplt(nptr->fl_user);
133                 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
134                 striplt(nptr->fl_room);
135                 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
136                 striplt(nptr->fl_node);
137
138                 /* Cowardly refuse to add an any/any/any entry that would
139                  * end up filtering every single message.
140                  */
141                 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
142                    + strlen(nptr->fl_node) == 0) {
143                         free(nptr);
144                 }
145                 else {
146                         nptr->next = newlist;
147                         newlist = nptr;
148                 }
149         }
150
151         free(serialized_list);
152         return newlist;
153 }
154
155
156 void free_filter_list(struct FilterList *fl) {
157         if (fl == NULL) return;
158         free_filter_list(fl->next);
159         free(fl);
160 }
161
162
163
164 /*
165  * Check the use table.  This is a list of messages which have recently
166  * arrived on the system.  It is maintained and queried to prevent the same
167  * message from being entered into the database multiple times if it happens
168  * to arrive multiple times by accident.
169  */
170 int network_usetable(struct CtdlMessage *msg) {
171
172         char msgid[SIZ];
173         struct cdbdata *cdbut;
174         struct UseTable ut;
175
176         /* Bail out if we can't generate a message ID */
177         if (msg == NULL) {
178                 return(0);
179         }
180         if (msg->cm_fields['I'] == NULL) {
181                 return(0);
182         }
183         if (strlen(msg->cm_fields['I']) == 0) {
184                 return(0);
185         }
186
187         /* Generate the message ID */
188         strcpy(msgid, msg->cm_fields['I']);
189         if (haschar(msgid, '@') == 0) {
190                 strcat(msgid, "@");
191                 if (msg->cm_fields['N'] != NULL) {
192                         strcat(msgid, msg->cm_fields['N']);
193                 }
194                 else {
195                         return(0);
196                 }
197         }
198
199         cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
200         if (cdbut != NULL) {
201                 cdb_free(cdbut);
202                 return(1);
203         }
204
205         /* If we got to this point, it's unique: add it. */
206         strcpy(ut.ut_msgid, msgid);
207         ut.ut_timestamp = time(NULL);
208         cdb_store(CDB_USETABLE, msgid, strlen(msgid),
209                 &ut, sizeof(struct UseTable) );
210         return(0);
211 }
212
213
214 /* 
215  * Read the network map from its configuration file into memory.
216  */
217 void read_network_map(void) {
218         char *serialized_map = NULL;
219         int i;
220         char buf[SIZ];
221         struct NetMap *nmptr;
222
223         serialized_map = CtdlGetSysConfig(IGNETMAP);
224         if (serialized_map == NULL) return;     /* if null, no entries */
225
226         /* Use the string tokenizer to grab one line at a time */
227         for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
228                 extract_token(buf, serialized_map, i, '\n', sizeof buf);
229                 nmptr = (struct NetMap *) malloc(sizeof(struct NetMap));
230                 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
231                 nmptr->lastcontact = extract_long(buf, 1);
232                 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
233                 nmptr->next = the_netmap;
234                 the_netmap = nmptr;
235         }
236
237         free(serialized_map);
238         netmap_changed = 0;
239 }
240
241
242 /*
243  * Write the network map from memory back to the configuration file.
244  */
245 void write_network_map(void) {
246         char *serialized_map = NULL;
247         struct NetMap *nmptr;
248
249
250         if (netmap_changed) {
251                 serialized_map = strdup("");
252         
253                 if (the_netmap != NULL) {
254                         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
255                                 serialized_map = realloc(serialized_map,
256                                                         (strlen(serialized_map)+SIZ) );
257                                 if (strlen(nmptr->nodename) > 0) {
258                                         snprintf(&serialized_map[strlen(serialized_map)],
259                                                 SIZ,
260                                                 "%s|%ld|%s\n",
261                                                 nmptr->nodename,
262                                                 (long)nmptr->lastcontact,
263                                                 nmptr->nexthop);
264                                 }
265                         }
266                 }
267
268                 CtdlPutSysConfig(IGNETMAP, serialized_map);
269                 free(serialized_map);
270         }
271
272         /* Now free the list */
273         while (the_netmap != NULL) {
274                 nmptr = the_netmap->next;
275                 free(the_netmap);
276                 the_netmap = nmptr;
277         }
278         netmap_changed = 0;
279 }
280
281
282
283 /* 
284  * Check the network map and determine whether the supplied node name is
285  * valid.  If it is not a neighbor node, supply the name of a neighbor node
286  * which is the next hop.  If it *is* a neighbor node, we also fill in the
287  * shared secret.
288  */
289 int is_valid_node(char *nexthop, char *secret, char *node) {
290         int i;
291         char linebuf[SIZ];
292         char buf[SIZ];
293         int retval;
294         struct NetMap *nmptr;
295
296         if (node == NULL) {
297                 return(-1);
298         }
299
300         /*
301          * First try the neighbor nodes
302          */
303         if (working_ignetcfg == NULL) {
304                 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
305                 if (nexthop != NULL) {
306                         strcpy(nexthop, "");
307                 }
308                 return(-1);
309         }
310
311         retval = (-1);
312         if (nexthop != NULL) {
313                 strcpy(nexthop, "");
314         }
315
316         /* Use the string tokenizer to grab one line at a time */
317         for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
318                 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
319                 extract_token(buf, linebuf, 0, '|', sizeof buf);
320                 if (!strcasecmp(buf, node)) {
321                         if (nexthop != NULL) {
322                                 strcpy(nexthop, "");
323                         }
324                         if (secret != NULL) {
325                                 extract_token(secret, linebuf, 1, '|', 256);
326                         }
327                         retval = 0;
328                 }
329         }
330
331         if (retval == 0) {
332                 return(retval);         /* yup, it's a direct neighbor */
333         }
334
335         /*      
336          * If we get to this point we have to see if we know the next hop
337          */
338         if (the_netmap != NULL) {
339                 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
340                         if (!strcasecmp(nmptr->nodename, node)) {
341                                 if (nexthop != NULL) {
342                                         strcpy(nexthop, nmptr->nexthop);
343                                 }
344                                 return(0);
345                         }
346                 }
347         }
348
349         /*
350          * If we get to this point, the supplied node name is bogus.
351          */
352         lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
353         return(-1);
354 }
355
356
357
358
359
360 void cmd_gnet(char *argbuf) {
361         char filename[SIZ];
362         char buf[SIZ];
363         FILE *fp;
364
365         if (CtdlAccessCheck(ac_room_aide)) return;
366         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
367         cprintf("%d Network settings for room #%ld <%s>\n",
368                 LISTING_FOLLOWS,
369                 CC->room.QRnumber, CC->room.QRname);
370
371         fp = fopen(filename, "r");
372         if (fp != NULL) {
373                 while (fgets(buf, sizeof buf, fp) != NULL) {
374                         buf[strlen(buf)-1] = 0;
375                         cprintf("%s\n", buf);
376                 }
377                 fclose(fp);
378         }
379
380         cprintf("000\n");
381 }
382
383
384 void cmd_snet(char *argbuf) {
385         char tempfilename[SIZ];
386         char filename[SIZ];
387         char buf[SIZ];
388         FILE *fp;
389
390         unbuffer_output();
391
392         if (CtdlAccessCheck(ac_room_aide)) return;
393         CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
394         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
395
396         fp = fopen(tempfilename, "w");
397         if (fp == NULL) {
398                 cprintf("%d Cannot open %s: %s\n",
399                         ERROR + INTERNAL_ERROR,
400                         tempfilename,
401                         strerror(errno));
402         }
403
404         cprintf("%d %s\n", SEND_LISTING, tempfilename);
405         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
406                 fprintf(fp, "%s\n", buf);
407         }
408         fclose(fp);
409
410         /* Now copy the temp file to its permanent location
411          * (We use /bin/mv instead of link() because they may be on
412          * different filesystems)
413          */
414         unlink(filename);
415         snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
416         begin_critical_section(S_NETCONFIGS);
417         system(buf);
418         end_critical_section(S_NETCONFIGS);
419 }
420
421
422 /*
423  * Deliver digest messages
424  */
425 void network_deliver_digest(struct SpoolControl *sc) {
426         char buf[SIZ];
427         int i;
428         struct CtdlMessage *msg = NULL;
429         long msglen;
430         long msgnum;
431         char *instr = NULL;
432         size_t instr_len = SIZ;
433         struct CtdlMessage *imsg = NULL;
434         struct namelist *nptr;
435
436         if (sc->num_msgs_spooled < 1) {
437                 fclose(sc->digestfp);
438                 sc->digestfp = NULL;
439                 return;
440         }
441
442         msg = malloc(sizeof(struct CtdlMessage));
443         memset(msg, 0, sizeof(struct CtdlMessage));
444         msg->cm_magic = CTDLMESSAGE_MAGIC;
445         msg->cm_format_type = FMT_RFC822;
446         msg->cm_anon_type = MES_NORMAL;
447
448         sprintf(buf, "%ld", time(NULL));
449         msg->cm_fields['T'] = strdup(buf);
450         msg->cm_fields['A'] = strdup(CC->room.QRname);
451         snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
452         msg->cm_fields['U'] = strdup(buf);
453         sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
454         for (i=0; i<strlen(buf); ++i) {
455                 if (isspace(buf[i])) buf[i]='_';
456                 buf[i] = tolower(buf[i]);
457         }
458         msg->cm_fields['F'] = strdup(buf);
459         msg->cm_fields['R'] = strdup(buf);
460
461         /*
462          * Go fetch the contents of the digest
463          */
464         fseek(sc->digestfp, 0L, SEEK_END);
465         msglen = ftell(sc->digestfp);
466
467         msg->cm_fields['M'] = malloc(msglen + 1);
468         fseek(sc->digestfp, 0L, SEEK_SET);
469         fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
470         msg->cm_fields['M'][msglen] = 0;
471
472         fclose(sc->digestfp);
473         sc->digestfp = NULL;
474
475         msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
476         CtdlFreeMessage(msg);
477
478         /* Now generate the delivery instructions */
479
480         /* 
481          * Figure out how big a buffer we need to allocate
482          */
483         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
484                 instr_len = instr_len + strlen(nptr->name) + 2;
485         }
486         
487         /*
488          * allocate...
489          */
490         lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
491         instr = malloc(instr_len);
492         if (instr == NULL) {
493                 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
494                         (long)instr_len);
495                 abort();
496         }
497         snprintf(instr, instr_len,
498                 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
499                 "bounceto|postmaster@%s\n" ,
500                 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
501
502         /* Generate delivery instructions for each recipient */
503         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
504                 size_t tmp = strlen(instr);
505                 snprintf(&instr[tmp], instr_len - tmp,
506                          "remote|%s|0||\n", nptr->name);
507         }
508
509         /*
510          * Generate a message from the instructions
511          */
512         imsg = malloc(sizeof(struct CtdlMessage));
513         memset(imsg, 0, sizeof(struct CtdlMessage));
514         imsg->cm_magic = CTDLMESSAGE_MAGIC;
515         imsg->cm_anon_type = MES_NORMAL;
516         imsg->cm_format_type = FMT_RFC822;
517         imsg->cm_fields['A'] = strdup("Citadel");
518         imsg->cm_fields['M'] = instr;
519
520         /* Save delivery instructions in spoolout room */
521         CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
522         CtdlFreeMessage(imsg);
523 }
524
525
526 /*
527  * Deliver list messages to everyone on the list ... efficiently
528  */
529 void network_deliver_list(struct CtdlMessage *msg, struct SpoolControl *sc) {
530         long msgnum;
531         char *instr = NULL;
532         size_t instr_len = SIZ;
533         struct CtdlMessage *imsg = NULL;
534         struct namelist *nptr;
535
536         /* Don't do this if there were no recipients! */
537         if (sc->listrecps == NULL) return;
538
539         /* Save the message to disk... */
540         msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
541
542         /* Now generate the delivery instructions */
543
544         /* 
545          * Figure out how big a buffer we need to allocate
546          */
547         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
548                 instr_len = instr_len + strlen(nptr->name) + 2;
549         }
550         
551         /*
552          * allocate...
553          */
554         lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
555         instr = malloc(instr_len);
556         if (instr == NULL) {
557                 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
558                         (long)instr_len);
559                 abort();
560         }
561         snprintf(instr, instr_len,
562                 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
563                 "bounceto|postmaster@%s\n" ,
564                 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
565
566         /* Generate delivery instructions for each recipient */
567         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
568                 size_t tmp = strlen(instr);
569                 snprintf(&instr[tmp], instr_len - tmp,
570                          "remote|%s|0||\n", nptr->name);
571         }
572
573         /*
574          * Generate a message from the instructions
575          */
576         imsg = malloc(sizeof(struct CtdlMessage));
577         memset(imsg, 0, sizeof(struct CtdlMessage));
578         imsg->cm_magic = CTDLMESSAGE_MAGIC;
579         imsg->cm_anon_type = MES_NORMAL;
580         imsg->cm_format_type = FMT_RFC822;
581         imsg->cm_fields['A'] = strdup("Citadel");
582         imsg->cm_fields['M'] = instr;
583
584         /* Save delivery instructions in spoolout room */
585         CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
586         CtdlFreeMessage(imsg);
587 }
588
589
590
591
592 /*
593  * Spools out one message from the list.
594  */
595 void network_spool_msg(long msgnum, void *userdata) {
596         struct SpoolControl *sc;
597         int i;
598         char *newpath = NULL;
599         size_t instr_len = SIZ;
600         struct CtdlMessage *msg = NULL;
601         struct namelist *nptr;
602         struct maplist *mptr;
603         struct ser_ret sermsg;
604         FILE *fp;
605         char filename[SIZ];
606         char buf[SIZ];
607         int bang = 0;
608         int send = 1;
609         int delete_after_send = 0;      /* Set to 1 to delete after spooling */
610         int ok_to_participate = 0;
611         struct recptypes *valid;
612
613         sc = (struct SpoolControl *)userdata;
614
615         /*
616          * Process mailing list recipients
617          */
618         instr_len = SIZ;
619         if (sc->listrecps != NULL) {
620                 /* Fetch the message.  We're going to need to modify it
621                  * in order to insert the [list name] in it, etc.
622                  */
623                 msg = CtdlFetchMessage(msgnum, 1);
624                 if (msg != NULL) {
625
626                         /* Prepend "[List name]" to the subject */
627                         if (msg->cm_fields['U'] == NULL) {
628                                 msg->cm_fields['U'] = strdup("(no subject)");
629                         }
630                         snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
631                         free(msg->cm_fields['U']);
632                         msg->cm_fields['U'] = strdup(buf);
633
634                         /* Set the recipient of the list message to the
635                          * email address of the room itself.
636                          * FIXME ... I want to be able to pick any address
637                          */
638                         if (msg->cm_fields['R'] != NULL) {
639                                 free(msg->cm_fields['R']);
640                         }
641                         msg->cm_fields['R'] = malloc(256);
642                         snprintf(msg->cm_fields['R'], 256,
643                                 "room_%s@%s", CC->room.QRname,
644                                 config.c_fqdn);
645                         for (i=0; i<strlen(msg->cm_fields['R']); ++i) {
646                                 if (isspace(msg->cm_fields['R'][i])) {
647                                         msg->cm_fields['R'][i] = '_';
648                                 }
649                         }
650
651                         /* Handle delivery */
652                         network_deliver_list(msg, sc);
653                         CtdlFreeMessage(msg);
654                 }
655         }
656
657         /*
658          * Process digest recipients
659          */
660         if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
661                 msg = CtdlFetchMessage(msgnum, 1);
662                 if (msg != NULL) {
663                         fprintf(sc->digestfp,   " -----------------------------------"
664                                                 "------------------------------------"
665                                                 "-------\n");
666                         fprintf(sc->digestfp, "From: ");
667                         if (msg->cm_fields['A'] != NULL) {
668                                 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
669                         }
670                         if (msg->cm_fields['F'] != NULL) {
671                                 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
672                         }
673                         else if (msg->cm_fields['N'] != NULL) {
674                                 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
675                         }
676                         fprintf(sc->digestfp, "\n");
677                         if (msg->cm_fields['U'] != NULL) {
678                                 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
679                         }
680
681                         CC->redirect_buffer = malloc(SIZ);
682                         CC->redirect_len = 0;
683                         CC->redirect_alloc = SIZ;
684
685                         safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
686                         CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
687
688                         striplt(CC->redirect_buffer);
689                         fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
690
691                         free(CC->redirect_buffer);
692                         CC->redirect_buffer = NULL;
693                         CC->redirect_len = 0;
694                         CC->redirect_alloc = 0;
695
696                         sc->num_msgs_spooled += 1;
697                         free(msg);
698                 }
699         }
700
701         /*
702          * Process client-side list participations for this room
703          */
704         instr_len = SIZ;
705         if (sc->participates != NULL) {
706                 msg = CtdlFetchMessage(msgnum, 1);
707                 if (msg != NULL) {
708
709                         /* Only send messages which originated on our own Citadel
710                          * network, otherwise we'll end up sending the remote
711                          * mailing list's messages back to it, which is rude...
712                          */
713                         ok_to_participate = 0;
714                         if (msg->cm_fields['N'] != NULL) {
715                                 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
716                                         ok_to_participate = 1;
717                                 }
718                                 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
719                                         ok_to_participate = 1;
720                                 }
721                         }
722                         if (ok_to_participate) {
723                                 if (msg->cm_fields['F'] != NULL) {
724                                         free(msg->cm_fields['F']);
725                                 }
726                                 msg->cm_fields['F'] = malloc(SIZ);
727                                 /* Replace the Internet email address of the actual
728                                 * author with the email address of the room itself,
729                                 * so the remote listserv doesn't reject us.
730                                 * FIXME ... I want to be able to pick any address
731                                 */
732                                 snprintf(msg->cm_fields['F'], SIZ,
733                                         "room_%s@%s", CC->room.QRname,
734                                         config.c_fqdn);
735                                 for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
736                                         if (isspace(msg->cm_fields['F'][i])) {
737                                                 msg->cm_fields['F'][i] = '_';
738                                         }
739                                 }
740
741                                 /* 
742                                  * Figure out how big a buffer we need to allocate
743                                  */
744                                 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
745
746                                         if (msg->cm_fields['R'] == NULL) {
747                                                 free(msg->cm_fields['R']);
748                                         }
749                                         msg->cm_fields['R'] = strdup(nptr->name);
750         
751                                         valid = validate_recipients(nptr->name);
752                                         CtdlSubmitMsg(msg, valid, "");
753                                         free(valid);
754                                 }
755                         
756                         }
757                         CtdlFreeMessage(msg);
758                 }
759         }
760         
761         /*
762          * Process IGnet push shares
763          */
764         msg = CtdlFetchMessage(msgnum, 1);
765         if (msg != NULL) {
766                 size_t newpath_len;
767
768                 /* Prepend our node name to the Path field whenever
769                  * sending a message to another IGnet node
770                  */
771                 if (msg->cm_fields['P'] == NULL) {
772                         msg->cm_fields['P'] = strdup("username");
773                 }
774                 newpath_len = strlen(msg->cm_fields['P']) +
775                          strlen(config.c_nodename) + 2;
776                 newpath = malloc(newpath_len);
777                 snprintf(newpath, newpath_len, "%s!%s",
778                          config.c_nodename, msg->cm_fields['P']);
779                 free(msg->cm_fields['P']);
780                 msg->cm_fields['P'] = newpath;
781
782                 /*
783                  * Determine if this message is set to be deleted
784                  * after sending out on the network
785                  */
786                 if (msg->cm_fields['S'] != NULL) {
787                         if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
788                                 delete_after_send = 1;
789                         }
790                 }
791
792                 /* Now send it to every node */
793                 if (sc->ignet_push_shares != NULL)
794                   for (mptr = sc->ignet_push_shares; mptr != NULL;
795                     mptr = mptr->next) {
796
797                         send = 1;
798
799                         /* Check for valid node name */
800                         if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
801                                 lprintf(CTDL_ERR, "Invalid node <%s>\n",
802                                         mptr->remote_nodename);
803                                 send = 0;
804                         }
805
806                         /* Check for split horizon */
807                         lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
808                         bang = num_tokens(msg->cm_fields['P'], '!');
809                         if (bang > 1) for (i=0; i<(bang-1); ++i) {
810                                 extract_token(buf, msg->cm_fields['P'],
811                                         i, '!', sizeof buf);
812                                 if (!strcasecmp(buf, mptr->remote_nodename)) {
813                                         send = 0;
814                                 }
815                         }
816
817                         /* Send the message */
818                         if (send == 1) {
819
820                                 /*
821                                  * Force the message to appear in the correct room
822                                  * on the far end by setting the C field correctly
823                                  */
824                                 if (msg->cm_fields['C'] != NULL) {
825                                         free(msg->cm_fields['C']);
826                                 }
827                                 if (strlen(mptr->remote_roomname) > 0) {
828                                         msg->cm_fields['C'] = strdup(mptr->remote_roomname);
829                                 }
830                                 else {
831                                         msg->cm_fields['C'] = strdup(CC->room.QRname);
832                                 }
833
834                                 /* serialize it for transmission */
835                                 serialize_message(&sermsg, msg);
836                                 if (sermsg.len > 0) {
837
838                                         /* write it to the spool file */
839                                         snprintf(filename, sizeof filename,"%s/%s",
840                                                         ctdl_netout_dir,
841                                                         mptr->remote_nodename);
842                                         lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
843                                         fp = fopen(filename, "ab");
844                                         if (fp != NULL) {
845                                                 fwrite(sermsg.ser,
846                                                         sermsg.len, 1, fp);
847                                                 fclose(fp);
848                                         }
849                                         else {
850                                                 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
851                                         }
852         
853                                         /* free the serialized version */
854                                         free(sermsg.ser);
855                                 }
856
857                         }
858                 }
859                 CtdlFreeMessage(msg);
860         }
861
862         /* update lastsent */
863         sc->lastsent = msgnum;
864
865         /* Delete this message if delete-after-send is set */
866         if (delete_after_send) {
867                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
868         }
869
870 }
871         
872
873 /*
874  * Batch up and send all outbound traffic from the current room
875  */
876 void network_spoolout_room(char *room_to_spool) {
877         char filename[SIZ];
878         char buf[SIZ];
879         char instr[SIZ];
880         char nodename[256];
881         char roomname[ROOMNAMELEN];
882         char nexthop[256];
883         FILE *fp;
884         struct SpoolControl sc;
885         struct namelist *nptr = NULL;
886         struct maplist *mptr = NULL;
887         size_t miscsize = 0;
888         size_t linesize = 0;
889         int skipthisline = 0;
890         int i;
891
892         /*
893          * If the room doesn't exist, don't try to perform its networking tasks.
894          * Normally this should never happen, but once in a while maybe a room gets
895          * queued for networking and then deleted before it can happen.
896          */
897         if (getroom(&CC->room, room_to_spool) != 0) {
898                 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
899                 return;
900         }
901
902         memset(&sc, 0, sizeof(struct SpoolControl));
903         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
904
905         begin_critical_section(S_NETCONFIGS);
906
907         /* Only do net processing for rooms that have netconfigs */
908         fp = fopen(filename, "r");
909         if (fp == NULL) {
910                 end_critical_section(S_NETCONFIGS);
911                 return;
912         }
913
914         lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
915
916         while (fgets(buf, sizeof buf, fp) != NULL) {
917                 buf[strlen(buf)-1] = 0;
918
919                 extract_token(instr, buf, 0, '|', sizeof instr);
920                 if (!strcasecmp(instr, "lastsent")) {
921                         sc.lastsent = extract_long(buf, 1);
922                 }
923                 else if (!strcasecmp(instr, "listrecp")) {
924                         nptr = (struct namelist *)
925                                 malloc(sizeof(struct namelist));
926                         nptr->next = sc.listrecps;
927                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
928                         sc.listrecps = nptr;
929                 }
930                 else if (!strcasecmp(instr, "participate")) {
931                         nptr = (struct namelist *)
932                                 malloc(sizeof(struct namelist));
933                         nptr->next = sc.participates;
934                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
935                         sc.participates = nptr;
936                 }
937                 else if (!strcasecmp(instr, "digestrecp")) {
938                         nptr = (struct namelist *)
939                                 malloc(sizeof(struct namelist));
940                         nptr->next = sc.digestrecps;
941                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
942                         sc.digestrecps = nptr;
943                 }
944                 else if (!strcasecmp(instr, "ignet_push_share")) {
945                         /* by checking each node's validity, we automatically
946                          * purge nodes which do not exist from room network
947                          * configurations at this time.
948                          */
949                         extract_token(nodename, buf, 1, '|', sizeof nodename);
950                         extract_token(roomname, buf, 2, '|', sizeof roomname);
951                         strcpy(nexthop, "xxx");
952                         if (is_valid_node(nexthop, NULL, nodename) == 0) {
953                                 if (strlen(nexthop) == 0) {
954                                         mptr = (struct maplist *)
955                                                 malloc(sizeof(struct maplist));
956                                         mptr->next = sc.ignet_push_shares;
957                                         strcpy(mptr->remote_nodename, nodename);
958                                         strcpy(mptr->remote_roomname, roomname);
959                                         sc.ignet_push_shares = mptr;
960                                 }
961                         }
962                 }
963                 else {
964                         /* Preserve 'other' lines ... *unless* they happen to
965                          * be subscribe/unsubscribe pendings with expired
966                          * timestamps.
967                          */
968                         skipthisline = 0;
969                         if (!strncasecmp(buf, "subpending|", 11)) {
970                                 if (time(NULL) - extract_long(buf, 4) > EXP) {
971                                         skipthisline = 1;
972                                 }
973                         }
974                         if (!strncasecmp(buf, "unsubpending|", 13)) {
975                                 if (time(NULL) - extract_long(buf, 3) > EXP) {
976                                         skipthisline = 1;
977                                 }
978                         }
979
980                         if (skipthisline == 0) {
981                                 linesize = strlen(buf);
982                                 sc.misc = realloc(sc.misc,
983                                         (miscsize + linesize + 2) );
984                                 sprintf(&sc.misc[miscsize], "%s\n", buf);
985                                 miscsize = miscsize + linesize + 1;
986                         }
987                 }
988
989
990         }
991         fclose(fp);
992
993         /* If there are digest recipients, we have to build a digest */
994         if (sc.digestrecps != NULL) {
995                 sc.digestfp = tmpfile();
996                 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
997         }
998
999         /* Do something useful */
1000         CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL, NULL,
1001                 network_spool_msg, &sc);
1002
1003         /* If we wrote a digest, deliver it and then close it */
1004         snprintf(buf, sizeof buf, "room_%s@%s",
1005                 CC->room.QRname, config.c_fqdn);
1006         for (i=0; i<strlen(buf); ++i) {
1007                 buf[i] = tolower(buf[i]);
1008                 if (isspace(buf[i])) buf[i] = '_';
1009         }
1010         if (sc.digestfp != NULL) {
1011                 fprintf(sc.digestfp,    " -----------------------------------"
1012                                         "------------------------------------"
1013                                         "-------\n"
1014                                         "You are subscribed to the '%s' "
1015                                         "list.\n"
1016                                         "To post to the list: %s\n",
1017                                         CC->room.QRname, buf
1018                 );
1019                 network_deliver_digest(&sc);    /* deliver and close */
1020         }
1021
1022         /* Now rewrite the config file */
1023         fp = fopen(filename, "w");
1024         if (fp == NULL) {
1025                 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
1026                         filename, strerror(errno));
1027         }
1028         else {
1029                 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
1030
1031                 /* Write out the listrecps while freeing from memory at the
1032                  * same time.  Am I clever or what?  :)
1033                  */
1034                 while (sc.listrecps != NULL) {
1035                         fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
1036                         nptr = sc.listrecps->next;
1037                         free(sc.listrecps);
1038                         sc.listrecps = nptr;
1039                 }
1040                 /* Do the same for digestrecps */
1041                 while (sc.digestrecps != NULL) {
1042                         fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
1043                         nptr = sc.digestrecps->next;
1044                         free(sc.digestrecps);
1045                         sc.digestrecps = nptr;
1046                 }
1047                 /* Do the same for participates */
1048                 while (sc.participates != NULL) {
1049                         fprintf(fp, "participate|%s\n", sc.participates->name);
1050                         nptr = sc.participates->next;
1051                         free(sc.participates);
1052                         sc.participates = nptr;
1053                 }
1054                 while (sc.ignet_push_shares != NULL) {
1055                         /* by checking each node's validity, we automatically
1056                          * purge nodes which do not exist from room network
1057                          * configurations at this time.
1058                          */
1059                         if (is_valid_node(NULL, NULL, sc.ignet_push_shares->remote_nodename) == 0) {
1060                         }
1061                         fprintf(fp, "ignet_push_share|%s",
1062                                 sc.ignet_push_shares->remote_nodename);
1063                         if (strlen(sc.ignet_push_shares->remote_roomname) > 0) {
1064                                 fprintf(fp, "|%s", sc.ignet_push_shares->remote_roomname);
1065                         }
1066                         fprintf(fp, "\n");
1067                         mptr = sc.ignet_push_shares->next;
1068                         free(sc.ignet_push_shares);
1069                         sc.ignet_push_shares = mptr;
1070                 }
1071                 if (sc.misc != NULL) {
1072                         fwrite(sc.misc, strlen(sc.misc), 1, fp);
1073                 }
1074                 free(sc.misc);
1075
1076                 fclose(fp);
1077         }
1078         end_critical_section(S_NETCONFIGS);
1079 }
1080
1081
1082
1083 /*
1084  * Send the *entire* contents of the current room to one specific network node,
1085  * ignoring anything we know about which messages have already undergone
1086  * network processing.  This can be used to bring a new node into sync.
1087  */
1088 int network_sync_to(char *target_node) {
1089         struct SpoolControl sc;
1090         int num_spooled = 0;
1091         int found_node = 0;
1092         char buf[256];
1093         char sc_type[256];
1094         char sc_node[256];
1095         char sc_room[256];
1096         char filename[256];
1097         FILE *fp;
1098
1099         /* Grab the configuration line we're looking for */
1100         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1101         begin_critical_section(S_NETCONFIGS);
1102         fp = fopen(filename, "r");
1103         if (fp == NULL) {
1104                 end_critical_section(S_NETCONFIGS);
1105                 return(-1);
1106         }
1107         while (fgets(buf, sizeof buf, fp) != NULL) {
1108                 buf[strlen(buf)-1] = 0;
1109                 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1110                 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1111                 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1112                 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1113                    && (!strcasecmp(sc_node, target_node)) ) {
1114                         found_node = 1;
1115                         
1116                         /* Concise syntax because we don't need a full linked-list */
1117                         memset(&sc, 0, sizeof(struct SpoolControl));
1118                         sc.ignet_push_shares = (struct maplist *)
1119                                 malloc(sizeof(struct maplist));
1120                         sc.ignet_push_shares->next = NULL;
1121                         safestrncpy(sc.ignet_push_shares->remote_nodename,
1122                                 sc_node,
1123                                 sizeof sc.ignet_push_shares->remote_nodename);
1124                         safestrncpy(sc.ignet_push_shares->remote_roomname,
1125                                 sc_room,
1126                                 sizeof sc.ignet_push_shares->remote_roomname);
1127                 }
1128         }
1129         fclose(fp);
1130         end_critical_section(S_NETCONFIGS);
1131
1132         if (!found_node) return(-1);
1133
1134         /* Send ALL messages */
1135         num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
1136                 network_spool_msg, &sc);
1137
1138         /* Concise cleanup because we know there's only one node in the sc */
1139         free(sc.ignet_push_shares);
1140
1141         lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1142                 num_spooled, target_node);
1143         return(num_spooled);
1144 }
1145
1146
1147 /*
1148  * Implements the NSYN command
1149  */
1150 void cmd_nsyn(char *argbuf) {
1151         int num_spooled;
1152         char target_node[256];
1153
1154         if (CtdlAccessCheck(ac_aide)) return;
1155
1156         extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1157         num_spooled = network_sync_to(target_node);
1158         if (num_spooled >= 0) {
1159                 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1160         }
1161         else {
1162                 cprintf("%d No such room/node share exists.\n",
1163                         ERROR + ROOM_NOT_FOUND);
1164         }
1165 }
1166
1167
1168
1169 /*
1170  * Batch up and send all outbound traffic from the current room
1171  */
1172 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1173         struct RoomProcList *ptr;
1174
1175         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1176         if (ptr == NULL) return;
1177
1178         safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1179         begin_critical_section(S_RPLIST);
1180         ptr->next = rplist;
1181         rplist = ptr;
1182         end_critical_section(S_RPLIST);
1183 }
1184
1185 void destroy_network_queue_room(void)
1186 {
1187         struct RoomProcList *cur, *p;
1188
1189         cur = rplist;
1190         begin_critical_section(S_RPLIST);
1191         while (cur != NULL)
1192         {
1193                 p = cur->next;
1194                 free (cur);
1195                 cur = p;                
1196         }
1197         rplist = NULL;
1198         end_critical_section(S_RPLIST);
1199 }
1200
1201
1202 /*
1203  * Learn topology from path fields
1204  */
1205 void network_learn_topology(char *node, char *path) {
1206         char nexthop[256];
1207         struct NetMap *nmptr;
1208
1209         strcpy(nexthop, "");
1210
1211         if (num_tokens(path, '!') < 3) return;
1212         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1213                 if (!strcasecmp(nmptr->nodename, node)) {
1214                         extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1215                         nmptr->lastcontact = time(NULL);
1216                         ++netmap_changed;
1217                         return;
1218                 }
1219         }
1220
1221         /* If we got here then it's not in the map, so add it. */
1222         nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1223         strcpy(nmptr->nodename, node);
1224         nmptr->lastcontact = time(NULL);
1225         extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1226         nmptr->next = the_netmap;
1227         the_netmap = nmptr;
1228         ++netmap_changed;
1229 }
1230
1231
1232
1233
1234 /*
1235  * Bounce a message back to the sender
1236  */
1237 void network_bounce(struct CtdlMessage *msg, char *reason) {
1238         char *oldpath = NULL;
1239         char buf[SIZ];
1240         char bouncesource[SIZ];
1241         char recipient[SIZ];
1242         struct recptypes *valid = NULL;
1243         char force_room[ROOMNAMELEN];
1244         static int serialnum = 0;
1245         size_t size;
1246
1247         lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1248
1249         if (msg == NULL) return;
1250
1251         snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1252
1253         /* 
1254          * Give it a fresh message ID
1255          */
1256         if (msg->cm_fields['I'] != NULL) {
1257                 free(msg->cm_fields['I']);
1258         }
1259         snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1260                 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1261         msg->cm_fields['I'] = strdup(buf);
1262
1263         /*
1264          * FIXME ... right now we're just sending a bounce; we really want to
1265          * include the text of the bounced message.
1266          */
1267         if (msg->cm_fields['M'] != NULL) {
1268                 free(msg->cm_fields['M']);
1269         }
1270         msg->cm_fields['M'] = strdup(reason);
1271         msg->cm_format_type = 0;
1272
1273         /*
1274          * Turn the message around
1275          */
1276         if (msg->cm_fields['R'] == NULL) {
1277                 free(msg->cm_fields['R']);
1278         }
1279
1280         if (msg->cm_fields['D'] == NULL) {
1281                 free(msg->cm_fields['D']);
1282         }
1283
1284         snprintf(recipient, sizeof recipient, "%s@%s",
1285                 msg->cm_fields['A'], msg->cm_fields['N']);
1286
1287         if (msg->cm_fields['A'] == NULL) {
1288                 free(msg->cm_fields['A']);
1289         }
1290
1291         if (msg->cm_fields['N'] == NULL) {
1292                 free(msg->cm_fields['N']);
1293         }
1294
1295         if (msg->cm_fields['U'] == NULL) {
1296                 free(msg->cm_fields['U']);
1297         }
1298
1299         msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1300         msg->cm_fields['N'] = strdup(config.c_nodename);
1301         msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1302
1303         /* prepend our node to the path */
1304         if (msg->cm_fields['P'] != NULL) {
1305                 oldpath = msg->cm_fields['P'];
1306                 msg->cm_fields['P'] = NULL;
1307         }
1308         else {
1309                 oldpath = strdup("unknown_user");
1310         }
1311         size = strlen(oldpath) + SIZ;
1312         msg->cm_fields['P'] = malloc(size);
1313         snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1314         free(oldpath);
1315
1316         /* Now submit the message */
1317         valid = validate_recipients(recipient);
1318         if (valid != NULL) if (valid->num_error != 0) {
1319                 free(valid);
1320                 valid = NULL;
1321         }
1322         if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1323                 strcpy(force_room, config.c_aideroom);
1324         }
1325         else {
1326                 strcpy(force_room, "");
1327         }
1328         if ( (valid == NULL) && (strlen(force_room) == 0) ) {
1329                 strcpy(force_room, config.c_aideroom);
1330         }
1331         CtdlSubmitMsg(msg, valid, force_room);
1332
1333         /* Clean up */
1334         if (valid != NULL) free(valid);
1335         CtdlFreeMessage(msg);
1336         lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1337 }
1338
1339
1340
1341
1342 /*
1343  * Process a buffer containing a single message from a single file
1344  * from the inbound queue 
1345  */
1346 void network_process_buffer(char *buffer, long size) {
1347         struct CtdlMessage *msg = NULL;
1348         long pos;
1349         int field;
1350         struct recptypes *recp = NULL;
1351         char target_room[ROOMNAMELEN];
1352         struct ser_ret sermsg;
1353         char *oldpath = NULL;
1354         char filename[SIZ];
1355         FILE *fp;
1356         char nexthop[SIZ];
1357         unsigned char firstbyte;
1358         unsigned char lastbyte;
1359
1360         /* Validate just a little bit.  First byte should be FF and
1361          * last byte should be 00.
1362          */
1363         memcpy(&firstbyte, &buffer[0], 1);
1364         memcpy(&lastbyte, &buffer[size-1], 1);
1365         if ( (firstbyte != 255) || (lastbyte != 0) ) {
1366                 lprintf(CTDL_ERR, "Corrupt message!  Ignoring.\n");
1367                 return;
1368         }
1369
1370         /* Set default target room to trash */
1371         strcpy(target_room, TWITROOM);
1372
1373         /* Load the message into memory */
1374         msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1375         memset(msg, 0, sizeof(struct CtdlMessage));
1376         msg->cm_magic = CTDLMESSAGE_MAGIC;
1377         msg->cm_anon_type = buffer[1];
1378         msg->cm_format_type = buffer[2];
1379
1380         for (pos = 3; pos < size; ++pos) {
1381                 field = buffer[pos];
1382                 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1383                 pos = pos + strlen(&buffer[(int)pos]);
1384         }
1385
1386         /* Check for message routing */
1387         if (msg->cm_fields['D'] != NULL) {
1388                 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1389
1390                         /* route the message */
1391                         strcpy(nexthop, "");
1392                         if (is_valid_node(nexthop, NULL,
1393                            msg->cm_fields['D']) == 0) {
1394
1395                                 /* prepend our node to the path */
1396                                 if (msg->cm_fields['P'] != NULL) {
1397                                         oldpath = msg->cm_fields['P'];
1398                                         msg->cm_fields['P'] = NULL;
1399                                 }
1400                                 else {
1401                                         oldpath = strdup("unknown_user");
1402                                 }
1403                                 size = strlen(oldpath) + SIZ;
1404                                 msg->cm_fields['P'] = malloc(size);
1405                                 snprintf(msg->cm_fields['P'], size, "%s!%s",
1406                                         config.c_nodename, oldpath);
1407                                 free(oldpath);
1408
1409                                 /* serialize the message */
1410                                 serialize_message(&sermsg, msg);
1411
1412                                 /* now send it */
1413                                 if (strlen(nexthop) == 0) {
1414                                         strcpy(nexthop, msg->cm_fields['D']);
1415                                 }
1416                                 snprintf(filename, 
1417                                                  sizeof filename,
1418                                                  "%s/%s",
1419                                                  ctdl_netout_dir,
1420                                                  nexthop);
1421                                 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1422                                 fp = fopen(filename, "ab");
1423                                 if (fp != NULL) {
1424                                         fwrite(sermsg.ser,
1425                                                 sermsg.len, 1, fp);
1426                                         fclose(fp);
1427                                 }
1428                                 else {
1429                                         lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1430                                 }
1431                                 free(sermsg.ser);
1432                                 CtdlFreeMessage(msg);
1433                                 return;
1434                         }
1435                         
1436                         else {  /* invalid destination node name */
1437
1438                                 network_bounce(msg,
1439 "A message you sent could not be delivered due to an invalid destination node"
1440 " name.  Please check the address and try sending the message again.\n");
1441                                 msg = NULL;
1442                                 return;
1443
1444                         }
1445                 }
1446         }
1447
1448         /*
1449          * Check to see if we already have a copy of this message, and
1450          * abort its processing if so.  (We used to post a warning to Aide>
1451          * every time this happened, but the network is now so densely
1452          * connected that it's inevitable.)
1453          */
1454         if (network_usetable(msg) != 0) {
1455                 return;
1456         }
1457
1458         /* Learn network topology from the path */
1459         if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1460                 network_learn_topology(msg->cm_fields['N'], 
1461                                         msg->cm_fields['P']);
1462         }
1463
1464         /* Is the sending node giving us a very persuasive suggestion about
1465          * which room this message should be saved in?  If so, go with that.
1466          */
1467         if (msg->cm_fields['C'] != NULL) {
1468                 safestrncpy(target_room,
1469                         msg->cm_fields['C'],
1470                         sizeof target_room);
1471         }
1472
1473         /* Otherwise, does it have a recipient?  If so, validate it... */
1474         else if (msg->cm_fields['R'] != NULL) {
1475                 recp = validate_recipients(msg->cm_fields['R']);
1476                 if (recp != NULL) if (recp->num_error != 0) {
1477                         network_bounce(msg,
1478 "A message you sent could not be delivered due to an invalid address.\n"
1479 "Please check the address and try sending the message again.\n");
1480                         msg = NULL;
1481                         free(recp);
1482                         return;
1483                 }
1484                 strcpy(target_room, "");        /* no target room if mail */
1485         }
1486
1487         /* Our last shot at finding a home for this message is to see if
1488          * it has the O field (Originating room) set.
1489          */
1490         else if (msg->cm_fields['O'] != NULL) {
1491                 safestrncpy(target_room,
1492                         msg->cm_fields['O'],
1493                         sizeof target_room);
1494         }
1495
1496         /* Strip out fields that are only relevant during transit */
1497         if (msg->cm_fields['D'] != NULL) {
1498                 free(msg->cm_fields['D']);
1499                 msg->cm_fields['D'] = NULL;
1500         }
1501         if (msg->cm_fields['C'] != NULL) {
1502                 free(msg->cm_fields['C']);
1503                 msg->cm_fields['C'] = NULL;
1504         }
1505
1506         /* save the message into a room */
1507         if (PerformNetprocHooks(msg, target_room) == 0) {
1508                 msg->cm_flags = CM_SKIP_HOOKS;
1509                 CtdlSubmitMsg(msg, recp, target_room);
1510         }
1511         CtdlFreeMessage(msg);
1512         free(recp);
1513 }
1514
1515
1516 /*
1517  * Process a single message from a single file from the inbound queue 
1518  */
1519 void network_process_message(FILE *fp, long msgstart, long msgend) {
1520         long hold_pos;
1521         long size;
1522         char *buffer;
1523
1524         hold_pos = ftell(fp);
1525         size = msgend - msgstart + 1;
1526         buffer = malloc(size);
1527         if (buffer != NULL) {
1528                 fseek(fp, msgstart, SEEK_SET);
1529                 fread(buffer, size, 1, fp);
1530                 network_process_buffer(buffer, size);
1531                 free(buffer);
1532         }
1533
1534         fseek(fp, hold_pos, SEEK_SET);
1535 }
1536
1537
1538 /*
1539  * Process a single file from the inbound queue 
1540  */
1541 void network_process_file(char *filename) {
1542         FILE *fp;
1543         long msgstart = (-1L);
1544         long msgend = (-1L);
1545         long msgcur = 0L;
1546         int ch;
1547
1548
1549         fp = fopen(filename, "rb");
1550         if (fp == NULL) {
1551                 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1552                         filename, strerror(errno));
1553                 return;
1554         }
1555
1556         lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1557
1558         /* Look for messages in the data stream and break them out */
1559         while (ch = getc(fp), ch >= 0) {
1560         
1561                 if (ch == 255) {
1562                         if (msgstart >= 0L) {
1563                                 msgend = msgcur - 1;
1564                                 network_process_message(fp, msgstart, msgend);
1565                         }
1566                         msgstart = msgcur;
1567                 }
1568
1569                 ++msgcur;
1570         }
1571
1572         msgend = msgcur - 1;
1573         if (msgstart >= 0L) {
1574                 network_process_message(fp, msgstart, msgend);
1575         }
1576
1577         fclose(fp);
1578         unlink(filename);
1579 }
1580
1581
1582 /*
1583  * Process anything in the inbound queue
1584  */
1585 void network_do_spoolin(void) {
1586         DIR *dp;
1587         struct dirent *d;
1588         struct stat statbuf;
1589         char filename[256];
1590         static time_t last_spoolin_mtime = 0L;
1591
1592         /*
1593          * Check the spoolin directory's modification time.  If it hasn't
1594          * been touched, we don't need to scan it.
1595          */
1596         if (stat(ctdl_netin_dir, &statbuf)) return;
1597         if (statbuf.st_mtime == last_spoolin_mtime) {
1598                 lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
1599                 return;
1600         }
1601         last_spoolin_mtime = statbuf.st_mtime;
1602         lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1603
1604         /*
1605          * Ok, there's something interesting in there, so scan it.
1606          */
1607         dp = opendir(ctdl_netin_dir);
1608         if (dp == NULL) return;
1609
1610         while (d = readdir(dp), d != NULL) {
1611                 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1612                         snprintf(filename, 
1613                                          sizeof filename,
1614                                          "%s/%s",
1615                                          ctdl_netin_dir,
1616                                          d->d_name);
1617                         network_process_file(filename);
1618                 }
1619         }
1620
1621         closedir(dp);
1622 }
1623
1624 /*
1625  * Delete any files in the outbound queue that were intended
1626  * to be sent to nodes which no longer exist.
1627  */
1628 void network_purge_spoolout(void) {
1629         DIR *dp;
1630         struct dirent *d;
1631         char filename[256];
1632         char nexthop[256];
1633         int i;
1634
1635         dp = opendir(ctdl_netout_dir);
1636         if (dp == NULL) return;
1637
1638         while (d = readdir(dp), d != NULL) {
1639                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1640                         continue;
1641                 snprintf(filename, 
1642                                  sizeof filename,
1643                                  "%s/%s",
1644                                  ctdl_netout_dir,
1645                                  d->d_name);
1646
1647                 strcpy(nexthop, "");
1648                 i = is_valid_node(nexthop, NULL, d->d_name);
1649         
1650                 if ( (i != 0) || (strlen(nexthop) > 0) ) {
1651                         unlink(filename);
1652                 }
1653         }
1654
1655
1656         closedir(dp);
1657 }
1658
1659
1660 /*
1661  * receive network spool from the remote system
1662  */
1663 void receive_spool(int sock, char *remote_nodename) {
1664         long download_len;
1665         long bytes_received;
1666         char buf[SIZ];
1667         static char pbuf[IGNET_PACKET_SIZE];
1668         char tempfilename[PATH_MAX];
1669         long plen;
1670         FILE *fp;
1671
1672         CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1673         if (sock_puts(sock, "NDOP") < 0) return;
1674         if (sock_gets(sock, buf) < 0) return;
1675         lprintf(CTDL_DEBUG, "<%s\n", buf);
1676         if (buf[0] != '2') {
1677                 return;
1678         }
1679         download_len = extract_long(&buf[4], 0);
1680
1681         bytes_received = 0L;
1682         fp = fopen(tempfilename, "w");
1683         if (fp == NULL) {
1684                 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1685                         strerror(errno));
1686                 return;
1687         }
1688
1689         while (bytes_received < download_len) {
1690                 snprintf(buf, sizeof buf, "READ %ld|%ld",
1691                         bytes_received,
1692                      ((download_len - bytes_received > IGNET_PACKET_SIZE)
1693                  ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1694                 if (sock_puts(sock, buf) < 0) {
1695                         fclose(fp);
1696                         unlink(tempfilename);
1697                         return;
1698                 }
1699                 if (sock_gets(sock, buf) < 0) {
1700                         fclose(fp);
1701                         unlink(tempfilename);
1702                         return;
1703                 }
1704                 if (buf[0] == '6') {
1705                         plen = extract_long(&buf[4], 0);
1706                         if (sock_read(sock, pbuf, plen) < 0) {
1707                                 fclose(fp);
1708                                 unlink(tempfilename);
1709                                 return;
1710                         }
1711                         fwrite((char *) pbuf, plen, 1, fp);
1712                         bytes_received = bytes_received + plen;
1713                 }
1714         }
1715
1716         fclose(fp);
1717         if (sock_puts(sock, "CLOS") < 0) {
1718                 unlink(tempfilename);
1719                 return;
1720         }
1721         if (sock_gets(sock, buf) < 0) {
1722                 unlink(tempfilename);
1723                 return;
1724         }
1725         if (download_len > 0)
1726                 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>",
1727                                 download_len, remote_nodename);
1728         lprintf(CTDL_DEBUG, "%s", buf);
1729         /* TODO: make move inline. forking is verry expensive. */
1730         snprintf(buf, 
1731                          sizeof buf, 
1732                          "mv %s %s/%s.%ld",
1733                          tempfilename, 
1734                          ctdl_netin_dir,
1735                          remote_nodename, 
1736                          (long) getpid());
1737         system(buf);
1738 }
1739
1740
1741
1742 /*
1743  * transmit network spool to the remote system
1744  */
1745 void transmit_spool(int sock, char *remote_nodename)
1746 {
1747         char buf[SIZ];
1748         char pbuf[4096];
1749         long plen;
1750         long bytes_to_write, thisblock, bytes_written;
1751         int fd;
1752         char sfname[128];
1753
1754         if (sock_puts(sock, "NUOP") < 0) return;
1755         if (sock_gets(sock, buf) < 0) return;
1756         lprintf(CTDL_DEBUG, "<%s\n", buf);
1757         if (buf[0] != '2') {
1758                 return;
1759         }
1760
1761         snprintf(sfname, sizeof sfname, 
1762                          "%s/%s",
1763                          ctdl_netout_dir,
1764                          remote_nodename);
1765         fd = open(sfname, O_RDONLY);
1766         if (fd < 0) {
1767                 if (errno != ENOENT) {
1768                         lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1769                                 strerror(errno));
1770                 }
1771                 return;
1772         }
1773         bytes_written = 0;
1774         while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1775                 bytes_to_write = plen;
1776                 while (bytes_to_write > 0L) {
1777                         snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1778                         if (sock_puts(sock, buf) < 0) {
1779                                 close(fd);
1780                                 return;
1781                         }
1782                         if (sock_gets(sock, buf) < 0) {
1783                                 close(fd);
1784                                 return;
1785                         }
1786                         thisblock = atol(&buf[4]);
1787                         if (buf[0] == '7') {
1788                                 if (sock_write(sock, pbuf,
1789                                    (int) thisblock) < 0) {
1790                                         close(fd);
1791                                         return;
1792                                 }
1793                                 bytes_to_write -= thisblock;
1794                                 bytes_written += thisblock;
1795                         } else {
1796                                 goto ABORTUPL;
1797                         }
1798                 }
1799         }
1800
1801 ABORTUPL:
1802         close(fd);
1803         if (sock_puts(sock, "UCLS 1") < 0) return;
1804         if (sock_gets(sock, buf) < 0) return;
1805         lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1806                         bytes_written, remote_nodename);
1807         lprintf(CTDL_DEBUG, "<%s\n", buf);
1808         if (buf[0] == '2') {
1809                 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1810                 unlink(sfname);
1811         }
1812 }
1813
1814
1815
1816 /*
1817  * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1818  */
1819 void network_poll_node(char *node, char *secret, char *host, char *port) {
1820         int sock;
1821         char buf[SIZ];
1822
1823         if (network_talking_to(node, NTT_CHECK)) return;
1824         network_talking_to(node, NTT_ADD);
1825         lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1826
1827         sock = sock_connect(host, port, "tcp");
1828         if (sock < 0) {
1829                 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1830                 network_talking_to(node, NTT_REMOVE);
1831                 return;
1832         }
1833         
1834         lprintf(CTDL_DEBUG, "Connected!\n");
1835
1836         /* Read the server greeting */
1837         if (sock_gets(sock, buf) < 0) goto bail;
1838         lprintf(CTDL_DEBUG, ">%s\n", buf);
1839
1840         /* Identify ourselves */
1841         snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1842         lprintf(CTDL_DEBUG, "<%s\n", buf);
1843         if (sock_puts(sock, buf) <0) goto bail;
1844         if (sock_gets(sock, buf) < 0) goto bail;
1845         lprintf(CTDL_DEBUG, ">%s\n", buf);
1846         if (buf[0] != '2') goto bail;
1847
1848         /* At this point we are authenticated. */
1849         receive_spool(sock, node);
1850         transmit_spool(sock, node);
1851
1852         sock_puts(sock, "QUIT");
1853 bail:   sock_close(sock);
1854         network_talking_to(node, NTT_REMOVE);
1855 }
1856
1857
1858
1859 /*
1860  * Poll other Citadel nodes and transfer inbound/outbound network data.
1861  * Set "full" to nonzero to force a poll of every node, or to zero to poll
1862  * only nodes to which we have data to send.
1863  */
1864 void network_poll_other_citadel_nodes(int full_poll) {
1865         int i;
1866         char linebuf[256];
1867         char node[SIZ];
1868         char host[256];
1869         char port[256];
1870         char secret[256];
1871         int poll = 0;
1872         char spoolfile[256];
1873
1874         if (working_ignetcfg == NULL) {
1875                 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1876                 return;
1877         }
1878
1879         /* Use the string tokenizer to grab one line at a time */
1880         for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1881                 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1882                 extract_token(node, linebuf, 0, '|', sizeof node);
1883                 extract_token(secret, linebuf, 1, '|', sizeof secret);
1884                 extract_token(host, linebuf, 2, '|', sizeof host);
1885                 extract_token(port, linebuf, 3, '|', sizeof port);
1886                 if ( (strlen(node) > 0) && (strlen(secret) > 0) 
1887                    && (strlen(host) > 0) && strlen(port) > 0) {
1888                         poll = full_poll;
1889                         if (poll == 0) {
1890                                 snprintf(spoolfile, 
1891                                                  sizeof spoolfile,
1892                                                  "%s/%s",
1893                                                  ctdl_netout_dir, 
1894                                                  node);
1895                                 if (access(spoolfile, R_OK) == 0) {
1896                                         poll = 1;
1897                                 }
1898                         }
1899                         if (poll) {
1900                                 network_poll_node(node, secret, host, port);
1901                         }
1902                 }
1903         }
1904
1905 }
1906
1907
1908
1909
1910 /*
1911  * It's ok if these directories already exist.  Just fail silently.
1912  */
1913 void create_spool_dirs(void) {
1914         mkdir(ctdl_spool_dir, 0700);
1915         chown(ctdl_spool_dir, CTDLUID, (-1));
1916         mkdir(ctdl_netin_dir, 0700);
1917         chown(ctdl_netin_dir, CTDLUID, (-1));
1918         mkdir(ctdl_netout_dir, 0700);
1919         chown(ctdl_netout_dir, CTDLUID, (-1));
1920 }
1921
1922
1923
1924
1925
1926 /*
1927  * network_do_queue()
1928  * 
1929  * Run through the rooms doing various types of network stuff.
1930  */
1931 void network_do_queue(void) {
1932         static time_t last_run = 0L;
1933         struct RoomProcList *ptr;
1934         int full_processing = 1;
1935
1936         /*
1937          * Run the full set of processing tasks no more frequently
1938          * than once every n seconds
1939          */
1940         if ( (time(NULL) - last_run) < config.c_net_freq ) {
1941                 full_processing = 0;
1942         }
1943
1944         /*
1945          * This is a simple concurrency check to make sure only one queue run
1946          * is done at a time.  We could do this with a mutex, but since we
1947          * don't really require extremely fine granularity here, we'll do it
1948          * with a static variable instead.
1949          */
1950         if (doing_queue) return;
1951         doing_queue = 1;
1952
1953         /* Load the IGnet Configuration into memory */
1954         load_working_ignetcfg();
1955
1956         /*
1957          * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
1958          * then we poll everyone.  Otherwise we only poll nodes we have stuff
1959          * to send to.
1960          */
1961         network_poll_other_citadel_nodes(full_processing);
1962
1963         /*
1964          * Load the network map and filter list into memory.
1965          */
1966         read_network_map();
1967         filterlist = load_filter_list();
1968
1969         /* 
1970          * Go ahead and run the queue
1971          */
1972         if (full_processing) {
1973                 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1974                 ForEachRoom(network_queue_room, NULL);
1975         }
1976
1977         if (rplist != NULL) {
1978                 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1979                 while (rplist != NULL) {
1980                         char spoolroomname[ROOMNAMELEN];
1981                         safestrncpy(spoolroomname, rplist->name, sizeof spoolroomname);
1982                         begin_critical_section(S_RPLIST);
1983
1984                         /* pop this record off the list */
1985                         ptr = rplist;
1986                         rplist = rplist->next;
1987                         free(ptr);
1988
1989                         /* invalidate any duplicate entries to prevent double processing */
1990                         for (ptr=rplist; ptr!=NULL; ptr=ptr->next) {
1991                                 if (!strcasecmp(ptr->name, spoolroomname)) {
1992                                         ptr->name[0] = 0;
1993                                 }
1994                         }
1995
1996                         end_critical_section(S_RPLIST);
1997                         if (spoolroomname[0] != 0) {
1998                                 network_spoolout_room(spoolroomname);
1999                         }
2000                 }
2001         }
2002
2003         /* If there is anything in the inbound queue, process it */
2004         network_do_spoolin();
2005
2006         /* Save the network map back to disk */
2007         write_network_map();
2008
2009         /* Free the filter list in memory */
2010         free_filter_list(filterlist);
2011         filterlist = NULL;
2012
2013         network_purge_spoolout();
2014
2015         lprintf(CTDL_DEBUG, "network: queue run completed\n");
2016
2017         if (full_processing) {
2018                 last_run = time(NULL);
2019         }
2020
2021         doing_queue = 0;
2022 }
2023
2024
2025 /*
2026  * cmd_netp() - authenticate to the server as another Citadel node polling
2027  *            for network traffic
2028  */
2029 void cmd_netp(char *cmdbuf)
2030 {
2031         char node[256];
2032         char pass[256];
2033         int v;
2034
2035         char secret[256];
2036         char nexthop[256];
2037
2038         /* Authenticate */
2039         extract_token(node, cmdbuf, 0, '|', sizeof node);
2040         extract_token(pass, cmdbuf, 1, '|', sizeof pass);
2041
2042         if (doing_queue) {
2043                 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
2044                 cprintf("%d spooling - try again in a few minutes\n",
2045                         ERROR + RESOURCE_BUSY);
2046                 return;
2047         }
2048
2049         /* load the IGnet Configuration to check node validity */
2050         load_working_ignetcfg();
2051         v = is_valid_node(nexthop, secret, node);
2052
2053         if (v != 0) {
2054                 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
2055                 cprintf("%d authentication failed\n",
2056                         ERROR + PASSWORD_REQUIRED);
2057                 return;
2058         }
2059
2060         if (strcasecmp(pass, secret)) {
2061                 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
2062                 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
2063                 return;
2064         }
2065
2066         if (network_talking_to(node, NTT_CHECK)) {
2067                 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
2068                 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
2069                 return;
2070         }
2071
2072         safestrncpy(CC->net_node, node, sizeof CC->net_node);
2073         network_talking_to(node, NTT_ADD);
2074         lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
2075         cprintf("%d authenticated as network node '%s'\n", CIT_OK,
2076                 CC->net_node);
2077 }
2078
2079 /*
2080  * Module entry point
2081  */
2082 char *serv_network_init(void)
2083 {
2084         create_spool_dirs();
2085         CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
2086         CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
2087         CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
2088         CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
2089         CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
2090         return "$Id$";
2091 }