scraping the bits away
[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
99 /*
100  * Send the *entire* contents of the current room to one specific network node,
101  * ignoring anything we know about which messages have already undergone
102  * network processing.  This can be used to bring a new node into sync.
103  */
104 int network_sync_to(char *target_node, long len)
105 {
106         struct CitContext *CCC = CC;
107         OneRoomNetCfg OneRNCFG;
108         OneRoomNetCfg *pRNCFG;
109         const RoomNetCfgLine *pCfgLine;
110         SpoolControl sc;
111         int num_spooled = 0;
112
113         /* Load the netconfig for this room */
114         pRNCFG = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
115         if (pRNCFG == NULL) {                                   // no netconfig at all?
116                 return -1;
117         }
118         if (pRNCFG->NetConfigs[ignet_push_share] == NULL)       // no ignet push shares?
119         {
120                 FreeRoomNetworkStruct(&pRNCFG);
121                 return -1;
122         }
123
124         /* Search for an ignet_oush_share configuration bearing the target node's name */
125         for (pCfgLine = pRNCFG->NetConfigs[ignet_push_share]; pCfgLine != NULL; pCfgLine = pCfgLine->next)
126         {
127                 if (!strcmp(ChrPtr(pCfgLine->Value[0]), target_node))
128                         break;
129         }
130
131         /* If we aren't sharing with that node, bail out */
132         if (pCfgLine == NULL)
133         {
134                 FreeRoomNetworkStruct(&pRNCFG);
135                 return -1;
136         }
137
138         /* If we got here, we're good to go ... make up a dummy spoolconfig and roll with it */
139
140         begin_critical_section(S_NETCONFIGS);
141         memset(&sc, 0, sizeof(SpoolControl));
142         memset(&OneRNCFG, 0, sizeof(OneRoomNetCfg));
143         sc.RNCfg = &OneRNCFG;
144         sc.RNCfg->NetConfigs[ignet_push_share] = DuplicateOneGenericCfgLine(pCfgLine);
145         sc.Users[ignet_push_share] = NewStrBufPlain(NULL, (StrLength(pCfgLine->Value[0]) + StrLength(pCfgLine->Value[1]) + 10) );
146         StrBufAppendBuf(sc.Users[ignet_push_share], pCfgLine->Value[0], 0);
147         StrBufAppendBufPlain(sc.Users[ignet_push_share], HKEY(","), 0);
148         StrBufAppendBuf(sc.Users[ignet_push_share], pCfgLine->Value[1], 0);
149         CalcListID(&sc);
150         end_critical_section(S_NETCONFIGS);
151
152         sc.working_ignetcfg = CtdlLoadIgNetCfg();
153         sc.the_netmap = CtdlReadNetworkMap();
154
155         /* Send ALL messages */
156         num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, network_spool_msg, &sc);
157
158         /* Concise cleanup because we know there's only one node in the sc */
159         DeleteGenericCfgLine(NULL, &sc.RNCfg->NetConfigs[ignet_push_share]);
160
161         DeleteHash(&sc.working_ignetcfg);
162         DeleteHash(&sc.the_netmap);
163         free_spoolcontrol_struct_members(&sc);
164
165         syslog(LOG_NOTICE, "network: synchronized %d messages to <%s>", num_spooled, target_node);
166         return(num_spooled);
167 }
168
169
170 /*
171  * Implements the NSYN command
172  */
173 void cmd_nsyn(char *argbuf) {
174         int num_spooled;
175         long len;
176         char target_node[256];
177
178         if (CtdlAccessCheck(ac_aide)) return;
179
180         len = extract_token(target_node, argbuf, 0, '|', sizeof target_node);
181         num_spooled = network_sync_to(target_node, len);
182         if (num_spooled >= 0) {
183                 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
184         }
185         else {
186                 cprintf("%d No such room/node share exists.\n", ERROR + ROOM_NOT_FOUND);
187         }
188 }
189
190 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
191 {
192         int i;
193         struct RoomProcList *ptr;
194
195         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
196         if (ptr == NULL) {
197                 return NULL;
198         }
199
200         ptr->namelen = strlen(qrbuf->QRname);
201         if (ptr->namelen > ROOMNAMELEN) {
202                 ptr->namelen = ROOMNAMELEN - 1;
203         }
204
205         memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
206         ptr->name[ptr->namelen] = '\0';
207         ptr->QRNum = qrbuf->QRnumber;
208
209         for (i = 0; i < ptr->namelen; i++)
210         {
211                 ptr->lcname[i] = tolower(ptr->name[i]);
212         }
213
214         ptr->lcname[ptr->namelen] = '\0';
215         ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
216         return ptr;
217 }
218
219 /*
220  * Batch up and send all outbound traffic from the current room
221  */
222 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
223 {
224         struct RoomProcList *ptr;
225         roomlists *RP = (roomlists*) data;
226
227         if (!HaveSpoolConfig(OneRNCfg)) {
228                 return;
229         }
230
231         ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
232
233         if (ptr != NULL)
234         {
235                 ptr->next = RP->rplist;
236                 RP->rplist = ptr;
237         }
238 }
239
240 /*
241  * Batch up and send all outbound traffic from the current room
242  */
243 int network_room_handler(struct ctdlroom *qrbuf)
244 {
245         struct RoomProcList *ptr;
246         OneRoomNetCfg *RNCfg;
247
248         if (qrbuf->QRdefaultview == VIEW_QUEUE) {
249                 return 1;
250         }
251
252         RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
253         if (RNCfg == NULL) {
254                 return 1;
255         }
256
257         if (!HaveSpoolConfig(RNCfg)) {
258                 FreeRoomNetworkStruct(&RNCfg);
259                 return 1;
260         }
261
262         ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
263         if (ptr == NULL) {
264                 FreeRoomNetworkStruct(&RNCfg);
265                 return 1;
266         }
267
268         begin_critical_section(S_RPLIST);
269         ptr->next = rplist;
270         rplist = ptr;
271         end_critical_section(S_RPLIST);
272         FreeRoomNetworkStruct(&RNCfg);
273         return 1;
274 }
275
276 void destroy_network_queue_room(RoomProcList *rplist)
277 {
278         struct RoomProcList *cur, *p;
279
280         cur = rplist;
281         while (cur != NULL)
282         {
283                 p = cur->next;
284                 free (cur);
285                 cur = p;                
286         }
287 }
288
289 void destroy_network_queue_room_locked (void)
290 {
291         begin_critical_section(S_RPLIST);
292         destroy_network_queue_room(rplist);
293         end_critical_section(S_RPLIST);
294 }
295
296
297 /*
298  * network_do_queue()
299  * 
300  * Run through the rooms doing various types of network stuff.
301  */
302 void network_do_queue(void)
303 {
304         static time_t last_run = 0L;
305         int full_processing = 1;
306         HashList *working_ignetcfg;
307         HashList *the_netmap = NULL;
308         int netmap_changed = 0;
309         roomlists RL;
310         SpoolControl *sc = NULL;
311         SpoolControl *pSC;
312
313         /*
314          * Run the full set of processing tasks no more frequently
315          * than once every n seconds
316          */
317         if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
318         {
319                 full_processing = 0;
320                 syslog(LOG_DEBUG, "network: full processing in %ld seconds.", CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run));
321         }
322
323         become_session(&networker_spool_CC);
324         begin_critical_section(S_RPLIST);
325         RL.rplist = rplist;
326         rplist = NULL;
327         end_critical_section(S_RPLIST);
328
329         // TODO hm, check whether we have a config at all here?
330         /* Load the IGnet Configuration into memory */
331         working_ignetcfg = CtdlLoadIgNetCfg();
332
333         /*
334          * Load the network map and filter list into memory.
335          */
336         if (!server_shutting_down) {
337                 the_netmap = CtdlReadNetworkMap();
338         }
339
340         /* 
341          * Go ahead and run the queue
342          */
343         if (full_processing && !server_shutting_down) {
344                 syslog(LOG_DEBUG, "network: loading outbound queue");
345                 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL);
346         }
347
348         if ((RL.rplist != NULL) && (!server_shutting_down)) {
349                 RoomProcList *ptr, *cmp;
350                 ptr = RL.rplist;
351                 syslog(LOG_DEBUG, "network: running outbound queue");
352                 while (ptr != NULL && !server_shutting_down) {
353                         
354                         cmp = ptr->next;
355                         /* filter duplicates from the list... */
356                         while (cmp != NULL) {
357                                 if ((cmp->namelen > 0) &&
358                                     (cmp->key == ptr->key) &&
359                                     (cmp->namelen == ptr->namelen) &&
360                                     (strcmp(cmp->lcname, ptr->lcname) == 0))
361                                 {
362                                         cmp->namelen = 0;
363                                 }
364                                 cmp = cmp->next;
365                         }
366
367                         if (ptr->namelen > 0) {
368                                 InspectQueuedRoom(&sc, ptr, working_ignetcfg, the_netmap);
369                         }
370                         ptr = ptr->next;
371                 }
372         }
373
374
375         pSC = sc;
376         while (pSC != NULL)
377         {
378                 network_spoolout_room(pSC);
379                 pSC = pSC->next;
380         }
381
382         pSC = sc;
383         while (pSC != NULL)
384         {
385                 sc = pSC->next;
386                 free_spoolcontrol_struct(&pSC);
387                 pSC = sc;
388         }
389
390         /* Save the network map back to disk */
391         if (netmap_changed) {
392                 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
393                 char *pMapStr = SmashStrBuf(&MapStr);
394                 CtdlPutSysConfig(IGNETMAP, pMapStr);
395                 free(pMapStr);
396         }
397
398         /* combine single message files into one spool entry per remote node. */
399         network_consolidate_spoolout(working_ignetcfg, the_netmap);
400
401         /* shut down. */
402
403         DeleteHash(&the_netmap);
404
405         DeleteHash(&working_ignetcfg);
406
407         syslog(LOG_DEBUG, "network: queue run completed");
408
409         if (full_processing) {
410                 last_run = time(NULL);
411         }
412         destroy_network_queue_room(RL.rplist);
413 }
414
415
416 void network_logout_hook(void)
417 {
418         CitContext *CCC = MyContext();
419
420         /*
421          * If we were talking to a network node, we're not anymore...
422          */
423         if (!IsEmptyStr(CCC->net_node)) {
424                 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
425                 CCC->net_node[0] = '\0';
426         }
427 }
428
429
430 void network_cleanup_function(void)
431 {
432         struct CitContext *CCC = CC;
433
434         if (!IsEmptyStr(CCC->net_node)) {
435                 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
436                 CCC->net_node[0] = '\0';
437         }
438 }
439
440
441 /*
442  * Module entry point
443  */
444
445
446 CTDL_MODULE_INIT(network)
447 {
448         if (!threading)
449         {
450                 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
451                 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
452                 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
453                 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
454                 CtdlRegisterRoomHook(network_room_handler);
455                 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
456                 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);
457         }
458         return "network";
459 }