]> code.citadel.org Git - citadel.git/blob - citadel/serv_network.c
* Began the migration of netproc into part of the serv_network.c module instead
[citadel.git] / citadel / serv_network.c
1 /*
2  * $Id$ 
3  *
4  * This module will eventually replace netproc and some of its utilities.  In
5  * the meantime, it serves as a mailing list manager.
6  *
7  * Copyright (C) 2000-2001 by Art Cancro and others.
8  * This code is released under the terms of the GNU General Public License.
9  *
10  */
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "dynloader.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.h"
48 #include "tools.h"
49 #include "internet_addressing.h"
50 #include "serv_network.h"
51
52
53 /*
54  * When we do network processing, it's accomplished in two passes; one to
55  * gather a list of rooms and one to actually do them.  It's ok that rplist
56  * is global; this process *only* runs as part of the housekeeping loop and
57  * therefore only one will run at a time.
58  */
59 struct RoomProcList {
60         struct RoomProcList *next;
61         char name[ROOMNAMELEN];
62 };
63
64 struct RoomProcList *rplist = NULL;
65
66
67
68
69 void cmd_gnet(char *argbuf) {
70         char filename[SIZ];
71         char buf[SIZ];
72         FILE *fp;
73
74         if (CtdlAccessCheck(ac_room_aide)) return;
75         assoc_file_name(filename, &CC->quickroom, "netconfigs");
76         cprintf("%d Network settings for room #%ld <%s>\n",
77                 LISTING_FOLLOWS,
78                 CC->quickroom.QRnumber, CC->quickroom.QRname);
79
80         fp = fopen(filename, "r");
81         if (fp != NULL) {
82                 while (fgets(buf, sizeof buf, fp) != NULL) {
83                         buf[strlen(buf)-1] = 0;
84                         cprintf("%s\n", buf);
85                 }
86                 fclose(fp);
87         }
88
89         cprintf("000\n");
90 }
91
92
93 void cmd_snet(char *argbuf) {
94         char tempfilename[SIZ];
95         char filename[SIZ];
96         char buf[SIZ];
97         FILE *fp;
98
99         if (CtdlAccessCheck(ac_room_aide)) return;
100         safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
101         assoc_file_name(filename, &CC->quickroom, "netconfigs");
102
103         fp = fopen(tempfilename, "w");
104         if (fp == NULL) {
105                 cprintf("%d Cannot open %s: %s\n",
106                         ERROR+INTERNAL_ERROR,
107                         tempfilename,
108                         strerror(errno));
109         }
110
111         cprintf("%d %s\n", SEND_LISTING, tempfilename);
112         while (client_gets(buf), strcmp(buf, "000")) {
113                 fprintf(fp, "%s\n", buf);
114         }
115         fclose(fp);
116
117         /* Now copy the temp file to its permanent location
118          * (We use /bin/mv instead of link() because they may be on
119          * different filesystems)
120          */
121         unlink(filename);
122         snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
123         system(buf);
124 }
125
126
127
128 /*
129  * Spools out one message from the list.
130  */
131 void network_spool_msg(long msgnum, void *userdata) {
132         struct SpoolControl *sc;
133         struct namelist *nptr;
134         int err;
135         char *instr = NULL;
136         char *newpath = NULL;
137         size_t instr_len = SIZ;
138         struct CtdlMessage *msg;
139         struct CtdlMessage *imsg;
140         struct ser_ret sermsg;
141         FILE *fp;
142         char filename[SIZ];
143
144         sc = (struct SpoolControl *)userdata;
145
146         /*
147          * Process mailing list recipients
148          */
149         if (sc->listrecps != NULL) {
150         
151                 /* First, copy it to the spoolout room */
152                 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
153                 if (err != 0) return;
154
155                 /* 
156                  * Figure out how big a buffer we need to allocate
157                  */
158                 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
159                         instr_len = instr_len + strlen(nptr->name);
160                 }
161         
162                 /*
163                  * allocate...
164                  */
165                 lprintf(9, "Generating delivery instructions\n");
166                 instr = mallok(instr_len);
167                 if (instr == NULL) {
168                         lprintf(1, "Cannot allocate %d bytes for instr...\n",
169                                 instr_len);
170                         abort();
171                 }
172                 sprintf(instr,
173                         "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
174                         "bounceto|postmaster@%s\n" ,
175                         SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
176         
177                 /* Generate delivery instructions for each recipient */
178                 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
179                         sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
180                                 nptr->name);
181                 }
182         
183                 /*
184                  * Generate a message from the instructions
185                  */
186                 imsg = mallok(sizeof(struct CtdlMessage));
187                 memset(imsg, 0, sizeof(struct CtdlMessage));
188                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
189                 imsg->cm_anon_type = MES_NORMAL;
190                 imsg->cm_format_type = FMT_RFC822;
191                 imsg->cm_fields['A'] = strdoop("Citadel");
192                 imsg->cm_fields['M'] = instr;
193         
194                 /* Save delivery instructions in spoolout room */
195                 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
196                 CtdlFreeMessage(imsg);
197         }
198         
199         /*
200          * Process IGnet push shares
201          */
202         if (sc->ignet_push_shares != NULL) {
203         
204                 msg = CtdlFetchMessage(msgnum);
205                 if (msg != NULL) {
206
207                         /* Prepend our node name to the Path field whenever
208                          * sending a message to another IGnet node
209                          */
210                         if (msg->cm_fields['P'] == NULL) {
211                                 msg->cm_fields['P'] = strdoop("username");
212                         }
213                         newpath = mallok(strlen(msg->cm_fields['P']) + 
214                                         strlen(config.c_nodename) + 2);
215                         sprintf(newpath, "%s!%s", config.c_nodename,
216                                         msg->cm_fields['P']);
217                         phree(msg->cm_fields['P']);
218                         msg->cm_fields['P'] = newpath;
219
220                         /*
221                          * Force the message to appear in the correct room
222                          * on the far end by setting the C field correctly
223                          */
224                         if (msg->cm_fields['C'] != NULL) {
225                                 phree(msg->cm_fields['C']);
226                         }
227                         msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
228
229                         /* 
230                          * Now serialize it for transmission
231                          */
232                         serialize_message(&sermsg, msg);
233                         CtdlFreeMessage(msg);
234
235                         /* Now send it to every node */
236                         for (nptr = sc->ignet_push_shares; nptr != NULL;
237                             nptr = nptr->next) {
238
239                                 /* FIXME check for valid node name */
240                                 /* FIXME check for split horizon */
241
242                                 /* Send the message */
243                                 sprintf(filename, "./network/spoolout/%s",
244                                         nptr->name);
245                                 fp = fopen(filename, "ab");
246                                 if (fp != NULL) {
247                                         fwrite(sermsg.ser, sermsg.len, 1, fp);
248                                         fclose(fp);
249                                 }
250                         }
251
252
253                 }
254
255         }
256
257         /* update lastsent */
258         sc->lastsent = msgnum;
259 }
260         
261
262
263
264 /*
265  * Batch up and send all outbound traffic from the current room
266  */
267 void network_spoolout_room(char *room_to_spool) {
268         char filename[SIZ];
269         char buf[SIZ];
270         char instr[SIZ];
271         FILE *fp;
272         struct SpoolControl sc;
273         /* struct namelist *digestrecps = NULL; */
274         struct namelist *nptr;
275
276         lprintf(7, "Spooling <%s>\n", room_to_spool);
277         if (getroom(&CC->quickroom, room_to_spool) != 0) {
278                 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
279                 return;
280         }
281
282         memset(&sc, 0, sizeof(struct SpoolControl));
283         assoc_file_name(filename, &CC->quickroom, "netconfigs");
284
285         fp = fopen(filename, "r");
286         if (fp == NULL) {
287                 lprintf(7, "Outbound batch processing skipped for <%s>\n",
288                         CC->quickroom.QRname);
289                 return;
290         }
291
292         lprintf(5, "Outbound batch processing started for <%s>\n",
293                 CC->quickroom.QRname);
294
295         while (fgets(buf, sizeof buf, fp) != NULL) {
296                 buf[strlen(buf)-1] = 0;
297
298                 extract(instr, buf, 0);
299                 if (!strcasecmp(instr, "lastsent")) {
300                         sc.lastsent = extract_long(buf, 1);
301                 }
302                 else if (!strcasecmp(instr, "listrecp")) {
303                         nptr = (struct namelist *)
304                                 mallok(sizeof(struct namelist));
305                         nptr->next = sc.listrecps;
306                         extract(nptr->name, buf, 1);
307                         sc.listrecps = nptr;
308                 }
309                 else if (!strcasecmp(instr, "ignet_push_share")) {
310                         nptr = (struct namelist *)
311                                 mallok(sizeof(struct namelist));
312                         nptr->next = sc.ignet_push_shares;
313                         extract(nptr->name, buf, 1);
314                         sc.ignet_push_shares = nptr;
315                 }
316
317
318         }
319         fclose(fp);
320
321
322         /* Do something useful */
323         CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
324                 network_spool_msg, &sc);
325
326
327         /* Now rewrite the config file */
328         fp = fopen(filename, "w");
329         if (fp == NULL) {
330                 lprintf(1, "ERROR: cannot open %s: %s\n",
331                         filename, strerror(errno));
332         }
333         else {
334                 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
335
336                 /* Write out the listrecps while freeing from memory at the
337                  * same time.  Am I clever or what?  :)
338                  */
339                 while (sc.listrecps != NULL) {
340                         fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
341                         nptr = sc.listrecps->next;
342                         phree(sc.listrecps);
343                         sc.listrecps = nptr;
344                 }
345                 while (sc.ignet_push_shares != NULL) {
346                         fprintf(fp, "ignet_push_share|%s\n",
347                                 sc.ignet_push_shares->name);
348                         nptr = sc.ignet_push_shares->next;
349                         phree(sc.ignet_push_shares);
350                         sc.ignet_push_shares = nptr;
351                 }
352
353                 fclose(fp);
354         }
355
356         lprintf(5, "Outbound batch processing finished for <%s>\n",
357                 CC->quickroom.QRname);
358 }
359
360
361 /*
362  * Batch up and send all outbound traffic from the current room
363  */
364 void network_queue_room(struct quickroom *qrbuf, void *data) {
365         struct RoomProcList *ptr;
366
367         ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
368         if (ptr == NULL) return;
369
370         safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
371         ptr->next = rplist;
372         rplist = ptr;
373 }
374         
375
376 /*
377  * network_do_queue()
378  * 
379  * Run through the rooms doing various types of network stuff.
380  */
381 void network_do_queue(void) {
382         static int doing_queue = 0;
383         static time_t last_run = 0L;
384         struct RoomProcList *ptr;
385
386 #define NETWORK_QUEUE_FREQUENCY 3600    /* one hour ... FIXME put in config */
387         /*
388          * Run no more frequently than once every n seconds
389          */
390         if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
391
392         /*
393          * This is a simple concurrency check to make sure only one queue run
394          * is done at a time.  We could do this with a mutex, but since we
395          * don't really require extremely fine granularity here, we'll do it
396          * with a static variable instead.
397          */
398         if (doing_queue) return;
399         doing_queue = 1;
400         last_run = time(NULL);
401
402         /* 
403          * Go ahead and run the queue
404          */
405         lprintf(7, "network: loading outbound queue\n");
406         ForEachRoom(network_queue_room, NULL);
407
408         lprintf(7, "network: running outbound queue\n");
409         while (rplist != NULL) {
410                 network_spoolout_room(rplist->name);
411                 ptr = rplist;
412                 rplist = rplist->next;
413                 phree(ptr);
414         }
415
416         lprintf(7, "network: queue run completed\n");
417         doing_queue = 0;
418 }
419
420
421 /*
422  * Module entry point
423  */
424 char *Dynamic_Module_Init(void)
425 {
426         CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
427         CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
428         CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
429         return "$Id$";
430 }