No more cleanup hooks. The OS can reclaim memory better than we can. We want to...
[citadel.git] / citadel / modules / network / serv_network.c
1 /*
2  * This module handles network mail and mailing list processing.
3  *
4  * Copyright (c) 2000-2021 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 /* comes from lookup3.c from libcitadel... */
81 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
82
83 typedef struct __roomlists {
84         RoomProcList *rplist;
85 } roomlists;
86
87
88 /*
89  * When we do network processing, it's accomplished in two passes; one to
90  * gather a list of rooms and one to actually do them.  It's ok that rplist
91  * is global; we have a mutex that keeps it safe.
92  */
93 struct RoomProcList *rplist = NULL;
94
95 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
96 {
97         int i;
98         struct RoomProcList *ptr;
99
100         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
101         if (ptr == NULL) {
102                 return NULL;
103         }
104
105         ptr->namelen = strlen(qrbuf->QRname);
106         if (ptr->namelen > ROOMNAMELEN) {
107                 ptr->namelen = ROOMNAMELEN - 1;
108         }
109
110         memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
111         ptr->name[ptr->namelen] = '\0';
112         ptr->QRNum = qrbuf->QRnumber;
113
114         for (i = 0; i < ptr->namelen; i++)
115         {
116                 ptr->lcname[i] = tolower(ptr->name[i]);
117         }
118
119         ptr->lcname[ptr->namelen] = '\0';
120         ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
121         return ptr;
122 }
123
124 /*
125  * Batch up and send all outbound traffic from the current room
126  */
127 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
128 {
129         struct RoomProcList *ptr;
130         roomlists *RP = (roomlists*) data;
131
132         if (!HaveSpoolConfig(OneRNCfg)) {
133                 return;
134         }
135
136         ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
137
138         if (ptr != NULL)
139         {
140                 ptr->next = RP->rplist;
141                 RP->rplist = ptr;
142         }
143 }
144
145 /*
146  * Batch up and send all outbound traffic from the current room
147  */
148 int network_room_handler(struct ctdlroom *qrbuf)
149 {
150         struct RoomProcList *ptr;
151         OneRoomNetCfg *RNCfg;
152
153         if (qrbuf->QRdefaultview == VIEW_QUEUE) {
154                 return 1;
155         }
156
157         RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
158         if (RNCfg == NULL) {
159                 return 1;
160         }
161
162         if (!HaveSpoolConfig(RNCfg)) {
163                 FreeRoomNetworkStruct(&RNCfg);
164                 return 1;
165         }
166
167         ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
168         if (ptr == NULL) {
169                 FreeRoomNetworkStruct(&RNCfg);
170                 return 1;
171         }
172
173         begin_critical_section(S_RPLIST);
174         ptr->next = rplist;
175         rplist = ptr;
176         end_critical_section(S_RPLIST);
177         FreeRoomNetworkStruct(&RNCfg);
178         return 1;
179 }
180
181 void destroy_network_queue_room(RoomProcList *rplist)
182 {
183         struct RoomProcList *cur, *p;
184
185         cur = rplist;
186         while (cur != NULL)
187         {
188                 p = cur->next;
189                 free (cur);
190                 cur = p;                
191         }
192 }
193
194
195 /*
196  * network_do_queue()
197  * 
198  * Run through the rooms doing various types of network stuff.
199  */
200 void network_do_queue(void)
201 {
202         static time_t last_run = 0L;
203         int full_processing = 1;
204         HashList *working_ignetcfg;
205         HashList *the_netmap = NULL;
206         int netmap_changed = 0;
207         roomlists RL;
208         SpoolControl *sc = NULL;
209         SpoolControl *pSC;
210
211         /*
212          * Run the full set of processing tasks no more frequently
213          * than once every n seconds
214          */
215         if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
216         {
217                 full_processing = 0;
218                 syslog(LOG_DEBUG, "network: full processing in %ld seconds.", CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run));
219         }
220
221         begin_critical_section(S_RPLIST);
222         RL.rplist = rplist;
223         rplist = NULL;
224         end_critical_section(S_RPLIST);
225
226         // TODO hm, check whether we have a config at all here?
227         /* Load the IGnet Configuration into memory */
228         working_ignetcfg = CtdlLoadIgNetCfg();
229
230         /*
231          * Load the network map and filter list into memory.
232          */
233         if (!server_shutting_down) {
234                 the_netmap = CtdlReadNetworkMap();
235         }
236
237         /* 
238          * Go ahead and run the queue
239          */
240         if (full_processing && !server_shutting_down) {
241                 syslog(LOG_DEBUG, "network: loading outbound queue");
242                 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL);
243         }
244
245         if ((RL.rplist != NULL) && (!server_shutting_down)) {
246                 RoomProcList *ptr, *cmp;
247                 ptr = RL.rplist;
248                 syslog(LOG_DEBUG, "network: running outbound queue");
249                 while (ptr != NULL && !server_shutting_down) {
250                         
251                         cmp = ptr->next;
252                         /* filter duplicates from the list... */
253                         while (cmp != NULL) {
254                                 if ((cmp->namelen > 0) &&
255                                     (cmp->key == ptr->key) &&
256                                     (cmp->namelen == ptr->namelen) &&
257                                     (strcmp(cmp->lcname, ptr->lcname) == 0))
258                                 {
259                                         cmp->namelen = 0;
260                                 }
261                                 cmp = cmp->next;
262                         }
263
264                         if (ptr->namelen > 0) {
265                                 InspectQueuedRoom(&sc, ptr, working_ignetcfg, the_netmap);
266                         }
267                         ptr = ptr->next;
268                 }
269         }
270
271
272         pSC = sc;
273         while (pSC != NULL)
274         {
275                 network_spoolout_room(pSC);
276                 pSC = pSC->next;
277         }
278
279         pSC = sc;
280         while (pSC != NULL)
281         {
282                 sc = pSC->next;
283                 free_spoolcontrol_struct(&pSC);
284                 pSC = sc;
285         }
286
287         /* Save the network map back to disk */
288         if (netmap_changed) {
289                 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
290                 char *pMapStr = SmashStrBuf(&MapStr);
291                 CtdlPutSysConfig(IGNETMAP, pMapStr);
292                 free(pMapStr);
293         }
294
295         /* shut down. */
296
297         DeleteHash(&the_netmap);
298
299         DeleteHash(&working_ignetcfg);
300
301         syslog(LOG_DEBUG, "network: queue run completed");
302
303         if (full_processing) {
304                 last_run = time(NULL);
305         }
306         destroy_network_queue_room(RL.rplist);
307 }
308
309
310 /*
311  * Module entry point
312  */
313 CTDL_MODULE_INIT(network)
314 {
315         if (!threading)
316         {
317                 CtdlRegisterRoomHook(network_room_handler);
318                 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);
319         }
320         return "network";
321 }