2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2012 by the citadel.org team
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.
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.
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.
25 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
26 * requests that have not been confirmed will be deleted.
28 #define EXP 259200 /* three days */
40 #include <sys/types.h>
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
47 # include <sys/time.h>
55 # if HAVE_SYS_SYSCALL_H
56 # include <sys/syscall.h>
63 #include <libcitadel.h>
66 #include "citserver.h"
72 #include "internet_addressing.h"
73 #include "serv_network.h"
74 #include "clientsocket.h"
76 #include "citadel_dirs.h"
79 #include "netconfig.h"
81 #include "ctdl_module.h"
85 void DeleteNodeConf(void *vNode)
87 NodeConf *Node = (NodeConf*) vNode;
88 FreeStrBuf(&Node->NodeName);
89 FreeStrBuf(&Node->Secret);
90 FreeStrBuf(&Node->Host);
91 FreeStrBuf(&Node->Port);
95 NodeConf *NewNode(StrBuf *SerializedNode)
97 const char *Pos = NULL;
100 /* we need at least 4 pipes and some other text so its invalid. */
101 if (StrLength(SerializedNode) < 8)
103 Node = (NodeConf *) malloc(sizeof(NodeConf));
107 Node->NodeName=NewStrBuf();
108 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
110 Node->Secret=NewStrBuf();
111 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
113 Node->Host=NewStrBuf();
114 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
116 Node->Port=NewStrBuf();
117 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
123 * Load or refresh the Citadel network (IGnet) configuration for this node.
125 HashList* load_ignetcfg(void)
134 Cfg = CtdlGetSysConfig(IGNETCFG);
135 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
141 Hash = NewHash(1, NULL);
142 Buf = NewStrBufPlain(Cfg, -1);
144 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
148 StrBufSipLine(LineBuf, Buf, &LinePos);
149 if (StrLength(LineBuf) != 0) {
150 Node = NewNode(LineBuf);
152 Put(Hash, SKEY(Node->NodeName), Node, DeleteNodeConf);
155 } while (LinePos != StrBufNOTNULL);
157 FreeStrBuf(&LineBuf);
161 void DeleteNetMap(void *vNetMap)
163 NetMap *TheNetMap = (NetMap*) vNetMap;
164 FreeStrBuf(&TheNetMap->NodeName);
165 FreeStrBuf(&TheNetMap->NextHop);
169 NetMap *NewNetMap(StrBuf *SerializedNetMap)
171 const char *Pos = NULL;
174 /* we need at least 3 pipes and some other text so its invalid. */
175 if (StrLength(SerializedNetMap) < 6)
177 NM = (NetMap *) malloc(sizeof(NetMap));
179 NM->NodeName=NewStrBuf();
180 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
182 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
184 NM->NextHop=NewStrBuf();
185 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
190 HashList* read_network_map(void)
199 Cfg = CtdlGetSysConfig(IGNETMAP);
200 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
206 Hash = NewHash(1, NULL);
207 Buf = NewStrBufPlain(Cfg, -1);
209 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
211 while (StrBufSipLine(Buf, LineBuf, &LinePos))
213 TheNetMap = NewNetMap(LineBuf);
214 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
215 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
219 FreeStrBuf(&LineBuf);
223 StrBuf *SerializeNetworkMap(HashList *Map)
228 StrBuf *Ret = NewStrBuf();
229 HashPos *Pos = GetNewHashPos(Map, 0);
231 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
233 NetMap *pMap = (NetMap*) vMap;
234 StrBufAppendBuf(Ret, pMap->NodeName, 0);
235 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
237 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
238 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
240 StrBufAppendBuf(Ret, pMap->NextHop, 0);
241 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
249 * Learn topology from path fields
251 void network_learn_topology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
258 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
259 (vptr != NULL))/* TODO: is the NodeName Uniq? */
262 extract_token(nexthop, path, 0, '!', sizeof nexthop);
263 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
264 pNM->lastcontact = time(NULL);
265 (*netmap_changed) ++;
270 /* If we got here then it's not in the map, so add it. */
271 nmptr = (NetMap *) malloc(sizeof (NetMap));
272 nmptr->NodeName = NewStrBufPlain(node, -1);
273 nmptr->lastcontact = time(NULL);
274 nmptr->NextHop = NewStrBuf ();
275 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
276 /* TODO: is the NodeName Uniq? */
277 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
278 (*netmap_changed) ++;
283 * Check the network map and determine whether the supplied node name is
284 * valid. If it is not a neighbor node, supply the name of a neighbor node
285 * which is the next hop. If it *is* a neighbor node, we also fill in the
288 int is_valid_node(const StrBuf **nexthop,
289 const StrBuf **secret,
292 HashList *the_netmap)
299 if (StrLength(node) == 0) {
304 * First try the neighbor nodes
306 if (GetCount(IgnetCfg) == 0) {
307 syslog(LOG_INFO, "IgnetCfg is empty!\n");
308 if (nexthop != NULL) {
314 /* try to find a neigbour with the name 'node' */
315 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
318 TheNode = (NodeConf*)vNodeConf;
320 *secret = TheNode->Secret;
321 return 0; /* yup, it's a direct neighbor */
325 * If we get to this point we have to see if we know the next hop
326 *//* TODO: is the NodeName Uniq? */
327 if ((GetCount(the_netmap) > 0) &&
328 (GetHash(the_netmap, SKEY(node), &vNetMap)))
330 TheNetMap = (NetMap*)vNetMap;
332 *nexthop = TheNetMap->NextHop;
337 * If we get to this point, the supplied node name is bogus.
339 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
344 void cmd_gnet(char *argbuf)
346 char filename[PATH_MAX];
350 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
351 /* users can edit the netconfigs for their own mailbox rooms */
353 else if (CtdlAccessCheck(ac_room_aide)) return;
355 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
356 cprintf("%d Network settings for room #%ld <%s>\n",
358 CC->room.QRnumber, CC->room.QRname);
360 fp = fopen(filename, "r");
362 while (fgets(buf, sizeof buf, fp) != NULL) {
363 buf[strlen(buf)-1] = 0;
364 cprintf("%s\n", buf);
373 void cmd_snet(char *argbuf) {
374 char tempfilename[PATH_MAX];
375 char filename[PATH_MAX];
384 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
385 /* users can edit the netconfigs for their own mailbox rooms */
387 else if (CtdlAccessCheck(ac_room_aide)) return;
389 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
390 memcpy(tempfilename, filename, len + 1);
392 memset(&StatBuf, 0, sizeof(struct stat));
393 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
394 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
396 sprintf(tempfilename + len, ".%d", CC->cs_pid);
398 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
400 if ((TmpFD > 0) && (errno == 0))
402 char *tmp = malloc(StatBuf.st_size * 2);
403 memset(tmp, ' ', StatBuf.st_size * 2);
404 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
406 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
409 cprintf("%d Unable to allocate the space required for %s: %s\n",
410 ERROR + INTERNAL_ERROR,
413 unlink(tempfilename);
416 lseek(TmpFD, SEEK_SET, 0);
419 cprintf("%d Unable to allocate the space required for %s: %s\n",
420 ERROR + INTERNAL_ERROR,
423 unlink(tempfilename);
428 cprintf("%d %s\n", SEND_LISTING, tempfilename);
431 while (rc = CtdlClientGetLine(Line),
434 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
436 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
437 write(TmpFD, ChrPtr(Line), StrLength(Line));
438 len += StrLength(Line);
441 ftruncate(TmpFD, len);
444 /* Now copy the temp file to its permanent location.
445 * (We copy instead of link because they may be on different filesystems)
447 begin_critical_section(S_NETCONFIGS);
448 rename(tempfilename, filename);
449 end_critical_section(S_NETCONFIGS);
453 * cmd_netp() - authenticate to the server as another Citadel node polling
454 * for network traffic
456 void cmd_netp(char *cmdbuf)
458 struct CitContext *CCC = CC;
459 HashList *working_ignetcfg;
467 const StrBuf *secret = NULL;
468 const StrBuf *nexthop = NULL;
469 char err_buf[SIZ] = "";
472 node = CCC->curr_user;
473 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
474 NodeStr = NewStrBufPlain(node, nodelen);
475 /* load the IGnet Configuration to check node validity */
476 working_ignetcfg = load_ignetcfg();
477 v = is_valid_node(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
479 snprintf(err_buf, sizeof err_buf,
480 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
481 node, CCC->cs_host, CCC->cs_addr
483 syslog(LOG_WARNING, "%s", err_buf);
484 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
486 strs[0] = CCC->cs_addr;
487 lens[0] = strlen(CCC->cs_addr);
489 strs[1] = "SRV_UNKNOWN";
490 lens[1] = sizeof("SRV_UNKNOWN" - 1);
495 2, strs, (long*) &lens);
497 DeleteHash(&working_ignetcfg);
498 FreeStrBuf(&NodeStr);
502 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
503 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
504 snprintf(err_buf, sizeof err_buf,
505 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
506 CCC->cs_host, CCC->cs_addr, node
508 syslog(LOG_WARNING, "%s", err_buf);
509 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
511 strs[0] = CCC->cs_addr;
512 lens[0] = strlen(CCC->cs_addr);
515 lens[1] = sizeof("SRV_PW" - 1);
520 2, strs, (long*) &lens);
522 DeleteHash(&working_ignetcfg);
523 FreeStrBuf(&NodeStr);
527 if (network_talking_to(node, nodelen, NTT_CHECK)) {
528 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
529 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
530 DeleteHash(&working_ignetcfg);
531 FreeStrBuf(&NodeStr);
534 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
535 network_talking_to(CCC->net_node, nodelen, NTT_ADD);
536 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
537 CCC->net_node, CCC->cs_host, CCC->cs_addr
539 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
540 DeleteHash(&working_ignetcfg);
541 FreeStrBuf(&NodeStr);
544 int netconfig_check_roomaccess(
547 const char* RemoteIdentifier)
553 if (RemoteIdentifier == NULL)
555 snprintf(errmsgbuf, n, "Need sender to permit access.");
556 return (ERROR + USERNAME_REQUIRED);
559 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
560 begin_critical_section(S_NETCONFIGS);
561 if (!read_spoolcontrol_file(&sc, filename))
563 end_critical_section(S_NETCONFIGS);
564 snprintf(errmsgbuf, n,
565 "This mailing list only accepts posts from subscribers.");
566 return (ERROR + NO_SUCH_USER);
568 end_critical_section(S_NETCONFIGS);
569 found = is_recipient (sc, RemoteIdentifier);
570 free_spoolcontrol_struct(&sc);
575 snprintf(errmsgbuf, n,
576 "This mailing list only accepts posts from subscribers.");
577 return (ERROR + NO_SUCH_USER);
583 CTDL_MODULE_INIT(netconfig)
587 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
588 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
589 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");