Removed all of the SYS_ user contexts, they aren't needed anymore
[citadel.git] / citadel / modules / network / serv_network.c
1 /*
2  * This module handles network mail and mailing list processing.
3  *
4  * Copyright (c) 2000-2019 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 void destroy_network_queue_room_locked (void)
195 {
196         begin_critical_section(S_RPLIST);
197         destroy_network_queue_room(rplist);
198         end_critical_section(S_RPLIST);
199 }
200
201
202 /*
203  * network_do_queue()
204  * 
205  * Run through the rooms doing various types of network stuff.
206  */
207 void network_do_queue(void)
208 {
209         static time_t last_run = 0L;
210         int full_processing = 1;
211         HashList *working_ignetcfg;
212         HashList *the_netmap = NULL;
213         int netmap_changed = 0;
214         roomlists RL;
215         SpoolControl *sc = NULL;
216         SpoolControl *pSC;
217
218         /*
219          * Run the full set of processing tasks no more frequently
220          * than once every n seconds
221          */
222         if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
223         {
224                 full_processing = 0;
225                 syslog(LOG_DEBUG, "network: full processing in %ld seconds.", CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run));
226         }
227
228         begin_critical_section(S_RPLIST);
229         RL.rplist = rplist;
230         rplist = NULL;
231         end_critical_section(S_RPLIST);
232
233         // TODO hm, check whether we have a config at all here?
234         /* Load the IGnet Configuration into memory */
235         working_ignetcfg = CtdlLoadIgNetCfg();
236
237         /*
238          * Load the network map and filter list into memory.
239          */
240         if (!server_shutting_down) {
241                 the_netmap = CtdlReadNetworkMap();
242         }
243
244         /* 
245          * Go ahead and run the queue
246          */
247         if (full_processing && !server_shutting_down) {
248                 syslog(LOG_DEBUG, "network: loading outbound queue");
249                 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL);
250         }
251
252         if ((RL.rplist != NULL) && (!server_shutting_down)) {
253                 RoomProcList *ptr, *cmp;
254                 ptr = RL.rplist;
255                 syslog(LOG_DEBUG, "network: running outbound queue");
256                 while (ptr != NULL && !server_shutting_down) {
257                         
258                         cmp = ptr->next;
259                         /* filter duplicates from the list... */
260                         while (cmp != NULL) {
261                                 if ((cmp->namelen > 0) &&
262                                     (cmp->key == ptr->key) &&
263                                     (cmp->namelen == ptr->namelen) &&
264                                     (strcmp(cmp->lcname, ptr->lcname) == 0))
265                                 {
266                                         cmp->namelen = 0;
267                                 }
268                                 cmp = cmp->next;
269                         }
270
271                         if (ptr->namelen > 0) {
272                                 InspectQueuedRoom(&sc, ptr, working_ignetcfg, the_netmap);
273                         }
274                         ptr = ptr->next;
275                 }
276         }
277
278
279         pSC = sc;
280         while (pSC != NULL)
281         {
282                 network_spoolout_room(pSC);
283                 pSC = pSC->next;
284         }
285
286         pSC = sc;
287         while (pSC != NULL)
288         {
289                 sc = pSC->next;
290                 free_spoolcontrol_struct(&pSC);
291                 pSC = sc;
292         }
293
294         /* Save the network map back to disk */
295         if (netmap_changed) {
296                 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
297                 char *pMapStr = SmashStrBuf(&MapStr);
298                 CtdlPutSysConfig(IGNETMAP, pMapStr);
299                 free(pMapStr);
300         }
301
302         /* shut down. */
303
304         DeleteHash(&the_netmap);
305
306         DeleteHash(&working_ignetcfg);
307
308         syslog(LOG_DEBUG, "network: queue run completed");
309
310         if (full_processing) {
311                 last_run = time(NULL);
312         }
313         destroy_network_queue_room(RL.rplist);
314 }
315
316
317 /*
318  * Module entry point
319  */
320 CTDL_MODULE_INIT(network)
321 {
322         if (!threading)
323         {
324                 CtdlRegisterRoomHook(network_room_handler);
325                 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
326                 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);
327         }
328         return "network";
329 }