]> code.citadel.org Git - citadel.git/blob - citadel/serv_network.c
6aa5d9481d4454096b551f4ddcf120fb0ba76c48
[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         size_t instr_len = SIZ;
137         struct CtdlMessage *imsg;
138
139         sc = (struct SpoolControl *)userdata;
140
141         /* If no recipients, bail out now.
142          * (May need to tweak this when we add other types of targets)
143          */
144         if (sc->listrecps == NULL) return;
145         
146         /* First, copy it to the spoolout room */
147         err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
148         if (err != 0) return;
149
150         /* 
151          * Figure out how big a buffer we need to allocate
152          */
153         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
154                 instr_len = instr_len + strlen(nptr->name);
155         }
156
157         /*
158          * allocate...
159          */
160         lprintf(9, "Generating delivery instructions\n");
161         instr = mallok(instr_len);
162         if (instr == NULL) {
163                 lprintf(1, "Cannot allocate %d bytes for instr...\n",
164                         instr_len);
165                 abort();
166         }
167         sprintf(instr,
168                 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
169                 "bounceto|postmaster@%s\n" ,
170                 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
171
172         /* Generate delivery instructions for each recipient */
173         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
174                 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
175                         nptr->name);
176         }
177
178         /*
179          * Generate a message from the instructions
180          */
181         imsg = mallok(sizeof(struct CtdlMessage));
182         memset(imsg, 0, sizeof(struct CtdlMessage));
183         imsg->cm_magic = CTDLMESSAGE_MAGIC;
184         imsg->cm_anon_type = MES_NORMAL;
185         imsg->cm_format_type = FMT_RFC822;
186         imsg->cm_fields['A'] = strdoop("Citadel");
187         imsg->cm_fields['M'] = instr;
188
189         /* Save delivery instructions in spoolout room */
190         CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
191         CtdlFreeMessage(imsg);
192
193         /* update lastsent */
194         sc->lastsent = msgnum;
195 }
196
197
198
199
200 /*
201  * Batch up and send all outbound traffic from the current room
202  */
203 void network_spoolout_room(char *room_to_spool) {
204         char filename[SIZ];
205         char buf[SIZ];
206         char instr[SIZ];
207         FILE *fp;
208         struct SpoolControl sc;
209         /* struct namelist *digestrecps = NULL; */
210         struct namelist *nptr;
211
212         lprintf(7, "Spooling <%s>\n", room_to_spool);
213         if (getroom(&CC->quickroom, room_to_spool) != 0) {
214                 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
215                 return;
216         }
217
218         memset(&sc, 0, sizeof(struct SpoolControl));
219         assoc_file_name(filename, &CC->quickroom, "netconfigs");
220
221         fp = fopen(filename, "r");
222         if (fp == NULL) {
223                 lprintf(7, "Outbound batch processing skipped for <%s>\n",
224                         CC->quickroom.QRname);
225                 return;
226         }
227
228         lprintf(5, "Outbound batch processing started for <%s>\n",
229                 CC->quickroom.QRname);
230
231         while (fgets(buf, sizeof buf, fp) != NULL) {
232                 buf[strlen(buf)-1] = 0;
233
234                 extract(instr, buf, 0);
235                 if (!strcasecmp(instr, "lastsent")) {
236                         sc.lastsent = extract_long(buf, 1);
237                 }
238                 else if (!strcasecmp(instr, "listrecp")) {
239                         nptr = (struct namelist *)
240                                 mallok(sizeof(struct namelist));
241                         nptr->next = sc.listrecps;
242                         extract(nptr->name, buf, 1);
243                         sc.listrecps = nptr;
244                 }
245
246
247         }
248         fclose(fp);
249
250
251         /* Do something useful */
252         CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
253                 network_spool_msg, &sc);
254
255
256         /* Now rewrite the config file */
257         fp = fopen(filename, "w");
258         if (fp == NULL) {
259                 lprintf(1, "ERROR: cannot open %s: %s\n",
260                         filename, strerror(errno));
261         }
262         else {
263                 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
264
265                 /* Write out the listrecps while freeing from memory at the
266                  * same time.  Am I clever or what?  :)
267                  */
268                 while (sc.listrecps != NULL) {
269                         fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
270                         nptr = sc.listrecps->next;
271                         phree(sc.listrecps);
272                         sc.listrecps = nptr;
273                 }
274
275                 fclose(fp);
276         }
277
278         lprintf(5, "Outbound batch processing finished for <%s>\n",
279                 CC->quickroom.QRname);
280 }
281
282
283 /*
284  * Batch up and send all outbound traffic from the current room
285  */
286 void network_queue_room(struct quickroom *qrbuf, void *data) {
287         struct RoomProcList *ptr;
288
289         ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
290         if (ptr == NULL) return;
291
292         safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
293         ptr->next = rplist;
294         rplist = ptr;
295 }
296         
297
298 /*
299  * network_do_queue()
300  * 
301  * Run through the rooms doing various types of network stuff.
302  */
303 void network_do_queue(void) {
304         static int doing_queue = 0;
305         static time_t last_run = 0L;
306         struct RoomProcList *ptr;
307
308 #define NETWORK_QUEUE_FREQUENCY 3600    /* one hour ... FIXME put in config */
309         /*
310          * Run no more frequently than once every n seconds
311          */
312         if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
313
314         /*
315          * This is a simple concurrency check to make sure only one queue run
316          * is done at a time.  We could do this with a mutex, but since we
317          * don't really require extremely fine granularity here, we'll do it
318          * with a static variable instead.
319          */
320         if (doing_queue) return;
321         doing_queue = 1;
322         last_run = time(NULL);
323
324         /* 
325          * Go ahead and run the queue
326          */
327         lprintf(7, "network: loading outbound queue\n");
328         ForEachRoom(network_queue_room, NULL);
329
330         lprintf(7, "network: running outbound queue\n");
331         while (rplist != NULL) {
332                 network_spoolout_room(rplist->name);
333                 ptr = rplist;
334                 rplist = rplist->next;
335                 phree(ptr);
336         }
337
338         lprintf(7, "network: queue run completed\n");
339         doing_queue = 0;
340 }
341
342
343 char *Dynamic_Module_Init(void)
344 {
345         CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
346         CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
347         CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
348         return "$Id$";
349 }