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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
22 * This is a fairly high-level type of critical section. It ensures that no
23 * two threads work on the netconfigs files at the same time. Since we do
24 * so many things inside these, here are the rules:
25 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
26 * 2. Do *not* perform any I/O with the client during these sections.
31 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
32 * requests that have not been confirmed will be deleted.
34 #define EXP 259200 /* three days */
46 #include <sys/types.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
61 # if HAVE_SYS_SYSCALL_H
62 # include <sys/syscall.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
78 #include "internet_addressing.h"
79 #include "serv_network.h"
80 #include "clientsocket.h"
82 #include "citadel_dirs.h"
90 #include "netconfig.h"
92 #include "ctdl_module.h"
96 void DeleteNodeConf(void *vNode)
98 NodeConf *Node = (NodeConf*) vNode;
99 FreeStrBuf(&Node->NodeName);
100 FreeStrBuf(&Node->Secret);
101 FreeStrBuf(&Node->Host);
102 FreeStrBuf(&Node->Port);
106 NodeConf *NewNode(StrBuf *SerializedNode)
108 const char *Pos = NULL;
111 /* we need at least 4 pipes and some other text so its invalid. */
112 if (StrLength(SerializedNode) < 8)
114 Node = (NodeConf *) malloc(sizeof(NodeConf));
118 Node->NodeName=NewStrBuf();
119 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
121 Node->Secret=NewStrBuf();
122 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
124 Node->Host=NewStrBuf();
125 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
127 Node->Port=NewStrBuf();
128 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
134 * Load or refresh the Citadel network (IGnet) configuration for this node.
136 HashList* load_ignetcfg(void)
145 Cfg = CtdlGetSysConfig(IGNETCFG);
146 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
152 Hash = NewHash(1, NULL);
153 Buf = NewStrBufPlain(Cfg, -1);
155 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
159 StrBufSipLine(LineBuf, Buf, &LinePos);
160 if (StrLength(LineBuf) != 0) {
161 Node = NewNode(LineBuf);
163 Put(Hash, SKEY(Node->NodeName), Node, DeleteNodeConf);
166 } while (LinePos != StrBufNOTNULL);
168 FreeStrBuf(&LineBuf);
172 void DeleteNetMap(void *vNetMap)
174 NetMap *TheNetMap = (NetMap*) vNetMap;
175 FreeStrBuf(&TheNetMap->NodeName);
176 FreeStrBuf(&TheNetMap->NextHop);
180 NetMap *NewNetMap(StrBuf *SerializedNetMap)
182 const char *Pos = NULL;
185 /* we need at least 3 pipes and some other text so its invalid. */
186 if (StrLength(SerializedNetMap) < 6)
188 NM = (NetMap *) malloc(sizeof(NetMap));
190 NM->NodeName=NewStrBuf();
191 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
193 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
195 NM->NextHop=NewStrBuf();
196 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
201 HashList* read_network_map(void)
210 Cfg = CtdlGetSysConfig(IGNETMAP);
211 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
217 Hash = NewHash(1, NULL);
218 Buf = NewStrBufPlain(Cfg, -1);
220 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
222 while (StrBufSipLine(Buf, LineBuf, &LinePos))
224 TheNetMap = NewNetMap(LineBuf);
225 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
226 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
230 FreeStrBuf(&LineBuf);
234 StrBuf *SerializeNetworkMap(HashList *Map)
239 StrBuf *Ret = NewStrBuf();
240 HashPos *Pos = GetNewHashPos(Map, 0);
242 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
244 NetMap *pMap = (NetMap*) vMap;
245 StrBufAppendBuf(Ret, pMap->NodeName, 0);
246 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
248 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
249 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
251 StrBufAppendBuf(Ret, pMap->NextHop, 0);
252 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
260 * Learn topology from path fields
262 void network_learn_topology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
269 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
270 (vptr != NULL))/* TODO: is the NodeName Uniq? */
273 extract_token(nexthop, path, 0, '!', sizeof nexthop);
274 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
275 pNM->lastcontact = time(NULL);
276 (*netmap_changed) ++;
281 /* If we got here then it's not in the map, so add it. */
282 nmptr = (NetMap *) malloc(sizeof (NetMap));
283 nmptr->NodeName = NewStrBufPlain(node, -1);
284 nmptr->lastcontact = time(NULL);
285 nmptr->NextHop = NewStrBuf ();
286 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
287 /* TODO: is the NodeName Uniq? */
288 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
289 (*netmap_changed) ++;
294 * Check the network map and determine whether the supplied node name is
295 * valid. If it is not a neighbor node, supply the name of a neighbor node
296 * which is the next hop. If it *is* a neighbor node, we also fill in the
299 int is_valid_node(const StrBuf **nexthop,
300 const StrBuf **secret,
303 HashList *the_netmap)
310 if (StrLength(node) == 0) {
315 * First try the neighbor nodes
317 if (GetCount(IgnetCfg) == 0) {
318 syslog(LOG_INFO, "IgnetCfg is empty!\n");
319 if (nexthop != NULL) {
325 /* try to find a neigbour with the name 'node' */
326 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
329 TheNode = (NodeConf*)vNodeConf;
331 *secret = TheNode->Secret;
332 return 0; /* yup, it's a direct neighbor */
336 * If we get to this point we have to see if we know the next hop
337 *//* TODO: is the NodeName Uniq? */
338 if ((GetCount(the_netmap) > 0) &&
339 (GetHash(the_netmap, SKEY(node), &vNetMap)))
341 TheNetMap = (NetMap*)vNetMap;
343 *nexthop = TheNetMap->NextHop;
348 * If we get to this point, the supplied node name is bogus.
350 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
355 void cmd_gnet(char *argbuf)
357 char filename[PATH_MAX];
361 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
362 /* users can edit the netconfigs for their own mailbox rooms */
364 else if (CtdlAccessCheck(ac_room_aide)) return;
366 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
367 cprintf("%d Network settings for room #%ld <%s>\n",
369 CC->room.QRnumber, CC->room.QRname);
371 fp = fopen(filename, "r");
373 while (fgets(buf, sizeof buf, fp) != NULL) {
374 buf[strlen(buf)-1] = 0;
375 cprintf("%s\n", buf);
384 void cmd_snet(char *argbuf) {
385 char tempfilename[PATH_MAX];
386 char filename[PATH_MAX];
395 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
396 /* users can edit the netconfigs for their own mailbox rooms */
398 else if (CtdlAccessCheck(ac_room_aide)) return;
400 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
401 memcpy(tempfilename, filename, len + 1);
403 memset(&StatBuf, 0, sizeof(struct stat));
404 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
405 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
407 sprintf(tempfilename + len, ".%d", CC->cs_pid);
409 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
411 if ((TmpFD > 0) && (errno == 0))
413 char *tmp = malloc(StatBuf.st_size * 2);
414 memset(tmp, ' ', StatBuf.st_size * 2);
415 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
417 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
420 cprintf("%d Unable to allocate the space required for %s: %s\n",
421 ERROR + INTERNAL_ERROR,
424 unlink(tempfilename);
427 lseek(TmpFD, SEEK_SET, 0);
430 cprintf("%d Unable to allocate the space required for %s: %s\n",
431 ERROR + INTERNAL_ERROR,
434 unlink(tempfilename);
439 cprintf("%d %s\n", SEND_LISTING, tempfilename);
442 while (rc = CtdlClientGetLine(Line),
445 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
447 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
448 write(TmpFD, ChrPtr(Line), StrLength(Line));
449 len += StrLength(Line);
452 ftruncate(TmpFD, len);
455 /* Now copy the temp file to its permanent location.
456 * (We copy instead of link because they may be on different filesystems)
458 begin_critical_section(S_NETCONFIGS);
459 rename(tempfilename, filename);
460 end_critical_section(S_NETCONFIGS);
464 * cmd_netp() - authenticate to the server as another Citadel node polling
465 * for network traffic
467 void cmd_netp(char *cmdbuf)
469 struct CitContext *CCC = CC;
470 HashList *working_ignetcfg;
478 const StrBuf *secret = NULL;
479 const StrBuf *nexthop = NULL;
480 char err_buf[SIZ] = "";
483 node = CCC->curr_user;
484 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
485 NodeStr = NewStrBufPlain(node, nodelen);
486 /* load the IGnet Configuration to check node validity */
487 working_ignetcfg = load_ignetcfg();
488 v = is_valid_node(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
490 snprintf(err_buf, sizeof err_buf,
491 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
492 node, CCC->cs_host, CCC->cs_addr
494 syslog(LOG_WARNING, "%s", err_buf);
495 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
497 strs[0] = CCC->cs_addr;
498 lens[0] = strlen(CCC->cs_addr);
500 strs[1] = "SRV_UNKNOWN";
501 lens[1] = sizeof("SRV_UNKNOWN" - 1);
506 2, strs, (long*) &lens);
508 DeleteHash(&working_ignetcfg);
509 FreeStrBuf(&NodeStr);
513 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
514 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
515 snprintf(err_buf, sizeof err_buf,
516 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
517 CCC->cs_host, CCC->cs_addr, node
519 syslog(LOG_WARNING, "%s", err_buf);
520 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
522 strs[0] = CCC->cs_addr;
523 lens[0] = strlen(CCC->cs_addr);
526 lens[1] = sizeof("SRV_PW" - 1);
531 2, strs, (long*) &lens);
533 DeleteHash(&working_ignetcfg);
534 FreeStrBuf(&NodeStr);
538 if (network_talking_to(node, nodelen, NTT_CHECK)) {
539 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
540 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
541 DeleteHash(&working_ignetcfg);
542 FreeStrBuf(&NodeStr);
545 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
546 network_talking_to(CCC->net_node, nodelen, NTT_ADD);
547 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
548 CCC->net_node, CCC->cs_host, CCC->cs_addr
550 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
551 DeleteHash(&working_ignetcfg);
552 FreeStrBuf(&NodeStr);
555 int netconfig_check_roomaccess(
558 const char* RemoteIdentifier)
564 if (RemoteIdentifier == NULL)
566 snprintf(errmsgbuf, n, "Need sender to permit access.");
567 return (ERROR + USERNAME_REQUIRED);
570 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
571 begin_critical_section(S_NETCONFIGS);
572 if (!read_spoolcontrol_file(&sc, filename))
574 end_critical_section(S_NETCONFIGS);
575 snprintf(errmsgbuf, n,
576 "This mailing list only accepts posts from subscribers.");
577 return (ERROR + NO_SUCH_USER);
579 end_critical_section(S_NETCONFIGS);
580 found = is_recipient (sc, RemoteIdentifier);
581 free_spoolcontrol_struct(&sc);
586 snprintf(errmsgbuf, n,
587 "This mailing list only accepts posts from subscribers.");
588 return (ERROR + NO_SUCH_USER);
594 CTDL_MODULE_INIT(netconfig)
598 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
599 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
600 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");