a06459ed263ec5feff23f4cf2bc3ea470ca37cc5
[citadel.git] / citadel / modules / network / serv_network.c
1 /*
2  * This module handles network mail and mailing list processing.
3  *
4  * Copyright (c) 2000-2018 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * ** NOTE **   A word on the S_NETCONFIGS semaphore:
15  * This is a fairly high-level type of critical section.  It ensures that no
16  * two threads work on the netconfigs files at the same time.  Since we do
17  * so many things inside these, here are the rules:
18  *  1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
19  *  2. Do *not* perform any I/O with the client during these sections.
20  */
21
22 /*
23  * Duration of time (in seconds) after which pending list subscribe/unsubscribe
24  * requests that have not been confirmed will be deleted.
25  */
26 #define EXP     259200  /* three days */
27
28 #include "sysdep.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <signal.h>
35 #include <pwd.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <dirent.h>
40 #if TIME_WITH_SYS_TIME
41 # include <sys/time.h>
42 # include <time.h>
43 #else
44 # if HAVE_SYS_TIME_H
45 #  include <sys/time.h>
46 # else
47 #  include <time.h>
48 # endif
49 #endif
50 #ifdef HAVE_SYSCALL_H
51 # include <syscall.h>
52 #else 
53 # if HAVE_SYS_SYSCALL_H
54 #  include <sys/syscall.h>
55 # endif
56 #endif
57
58 #include <sys/wait.h>
59 #include <string.h>
60 #include <limits.h>
61 #include <libcitadel.h>
62 #include "citadel.h"
63 #include "server.h"
64 #include "citserver.h"
65 #include "support.h"
66 #include "config.h"
67 #include "user_ops.h"
68 #include "database.h"
69 #include "msgbase.h"
70 #include "internet_addressing.h"
71 #include "serv_network.h"
72 #include "clientsocket.h"
73 #include "citadel_dirs.h"
74 #include "threads.h"
75 #include "context.h"
76 #include "ctdl_module.h"
77 #include "netspool.h"
78 #include "netmail.h"
79
80 struct CitContext networker_spool_CC;
81
82 /* comes from lookup3.c from libcitadel... */
83 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
84
85 typedef struct __roomlists {
86         RoomProcList *rplist;
87 } roomlists;
88
89
90 /*
91  * When we do network processing, it's accomplished in two passes; one to
92  * gather a list of rooms and one to actually do them.  It's ok that rplist
93  * is global; we have a mutex that keeps it safe.
94  */
95 struct RoomProcList *rplist = NULL;
96
97 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
98 {
99         int i;
100         struct RoomProcList *ptr;
101
102         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
103         if (ptr == NULL) {
104                 return NULL;
105         }
106
107         ptr->namelen = strlen(qrbuf->QRname);
108         if (ptr->namelen > ROOMNAMELEN) {
109                 ptr->namelen = ROOMNAMELEN - 1;
110         }
111
112         memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
113         ptr->name[ptr->namelen] = '\0';
114         ptr->QRNum = qrbuf->QRnumber;
115
116         for (i = 0; i < ptr->namelen; i++)
117         {
118                 ptr->lcname[i] = tolower(ptr->name[i]);
119         }
120
121         ptr->lcname[ptr->namelen] = '\0';
122         ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
123         return ptr;
124 }
125
126 /*
127  * Batch up and send all outbound traffic from the current room
128  */
129 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
130 {
131         struct RoomProcList *ptr;
132         roomlists *RP = (roomlists*) data;
133
134         if (!HaveSpoolConfig(OneRNCfg)) {
135                 return;
136         }
137
138         ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
139
140         if (ptr != NULL)
141         {
142                 ptr->next = RP->rplist;
143                 RP->rplist = ptr;
144         }
145 }
146
147 /*
148  * Batch up and send all outbound traffic from the current room
149  */
150 int network_room_handler(struct ctdlroom *qrbuf)
151 {
152         struct RoomProcList *ptr;
153         OneRoomNetCfg *RNCfg;
154
155         if (qrbuf->QRdefaultview == VIEW_QUEUE) {
156                 return 1;
157         }
158
159         RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
160         if (RNCfg == NULL) {
161                 return 1;
162         }
163
164         if (!HaveSpoolConfig(RNCfg)) {
165                 FreeRoomNetworkStruct(&RNCfg);
166                 return 1;
167         }
168
169         ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
170         if (ptr == NULL) {
171                 FreeRoomNetworkStruct(&RNCfg);
172                 return 1;
173         }
174
175         begin_critical_section(S_RPLIST);
176         ptr->next = rplist;
177         rplist = ptr;
178         end_critical_section(S_RPLIST);
179         FreeRoomNetworkStruct(&RNCfg);
180         return 1;
181 }
182
183 void destroy_network_queue_room(RoomProcList *rplist)
184 {
185         struct RoomProcList *cur, *p;
186
187         cur = rplist;
188         while (cur != NULL)
189         {
190                 p = cur->next;
191                 free (cur);
192                 cur = p;                
193         }
194 }
195
196 void destroy_network_queue_room_locked (void)
197 {
198         begin_critical_section(S_RPLIST);
199         destroy_network_queue_room(rplist);
200         end_critical_section(S_RPLIST);
201 }
202
203
204 /*
205  * network_do_queue()
206  * 
207  * Run through the rooms doing various types of network stuff.
208  */
209 void network_do_queue(void)
210 {
211         static time_t last_run = 0L;
212         int full_processing = 1;
213         HashList *working_ignetcfg;
214         HashList *the_netmap = NULL;
215         int netmap_changed = 0;
216         roomlists RL;
217         SpoolControl *sc = NULL;
218         SpoolControl *pSC;
219
220         /*
221          * Run the full set of processing tasks no more frequently
222          * than once every n seconds
223          */
224         if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
225         {
226                 full_processing = 0;
227                 syslog(LOG_DEBUG, "network: full processing in %ld seconds.", CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run));
228         }
229
230         become_session(&networker_spool_CC);
231         begin_critical_section(S_RPLIST);
232         RL.rplist = rplist;
233         rplist = NULL;
234         end_critical_section(S_RPLIST);
235
236         // TODO hm, check whether we have a config at all here?
237         /* Load the IGnet Configuration into memory */
238         working_ignetcfg = CtdlLoadIgNetCfg();
239
240         /*
241          * Load the network map and filter list into memory.
242          */
243         if (!server_shutting_down) {
244                 the_netmap = CtdlReadNetworkMap();
245         }
246
247         /* 
248          * Go ahead and run the queue
249          */
250         if (full_processing && !server_shutting_down) {
251                 syslog(LOG_DEBUG, "network: loading outbound queue");
252                 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL);
253         }
254
255         if ((RL.rplist != NULL) && (!server_shutting_down)) {
256                 RoomProcList *ptr, *cmp;
257                 ptr = RL.rplist;
258                 syslog(LOG_DEBUG, "network: running outbound queue");
259                 while (ptr != NULL && !server_shutting_down) {
260                         
261                         cmp = ptr->next;
262                         /* filter duplicates from the list... */
263                         while (cmp != NULL) {
264                                 if ((cmp->namelen > 0) &&
265                                     (cmp->key == ptr->key) &&
266                                     (cmp->namelen == ptr->namelen) &&
267                                     (strcmp(cmp->lcname, ptr->lcname) == 0))
268                                 {
269                                         cmp->namelen = 0;
270                                 }
271                                 cmp = cmp->next;
272                         }
273
274                         if (ptr->namelen > 0) {
275                                 InspectQueuedRoom(&sc, ptr, working_ignetcfg, the_netmap);
276                         }
277                         ptr = ptr->next;
278                 }
279         }
280
281
282         pSC = sc;
283         while (pSC != NULL)
284         {
285                 network_spoolout_room(pSC);
286                 pSC = pSC->next;
287         }
288
289         pSC = sc;
290         while (pSC != NULL)
291         {
292                 sc = pSC->next;
293                 free_spoolcontrol_struct(&pSC);
294                 pSC = sc;
295         }
296
297         /* Save the network map back to disk */
298         if (netmap_changed) {
299                 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
300                 char *pMapStr = SmashStrBuf(&MapStr);
301                 CtdlPutSysConfig(IGNETMAP, pMapStr);
302                 free(pMapStr);
303         }
304
305         /* shut down. */
306
307         DeleteHash(&the_netmap);
308
309         DeleteHash(&working_ignetcfg);
310
311         syslog(LOG_DEBUG, "network: queue run completed");
312
313         if (full_processing) {
314                 last_run = time(NULL);
315         }
316         destroy_network_queue_room(RL.rplist);
317 }
318
319
320 /*
321  * Module entry point
322  */
323
324
325 CTDL_MODULE_INIT(network)
326 {
327         if (!threading)
328         {
329                 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
330                 CtdlRegisterRoomHook(network_room_handler);
331                 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
332                 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);
333         }
334         return "network";
335 }