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