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];
351 if (!IsEmptyStr(argbuf))
353 if (CtdlAccessCheck(ac_aide)) return;
354 if (strcmp(argbuf, FILE_MAILALIAS))
356 cprintf("%d No such file or directory\n",
357 ERROR + INTERNAL_ERROR);
360 safestrncpy(filename, file_mail_aliases, sizeof(filename));
361 cprintf("%d Settings for <%s>\n",
367 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
368 /* users can edit the netconfigs for their own mailbox rooms */
370 else if (CtdlAccessCheck(ac_room_aide)) return;
372 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
373 cprintf("%d Network settings for room #%ld <%s>\n",
375 CC->room.QRnumber, CC->room.QRname);
378 fp = fopen(filename, "r");
380 while (fgets(buf, sizeof buf, fp) != NULL) {
381 buf[strlen(buf)-1] = 0;
382 cprintf("%s\n", buf);
390 #define nForceAliases 5
391 const ConstStr ForceAliases[nForceAliases] = {
395 {HKEY("postmaster,")},
399 void cmd_snet(char *argbuf) {
400 char tempfilename[PATH_MAX];
401 char filename[PATH_MAX];
408 int MailAliasesFound[nForceAliases];
412 if (!IsEmptyStr(argbuf))
414 if (CtdlAccessCheck(ac_aide)) return;
415 if (strcmp(argbuf, FILE_MAILALIAS))
417 cprintf("%d No such file or directory\n",
418 ERROR + INTERNAL_ERROR);
421 safestrncpy(filename, file_mail_aliases, sizeof(filename));
422 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
427 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
428 /* users can edit the netconfigs for their own mailbox rooms */
430 else if (CtdlAccessCheck(ac_room_aide)) return;
432 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
433 memcpy(tempfilename, filename, len + 1);
435 memset(&StatBuf, 0, sizeof(struct stat));
436 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
437 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
439 sprintf(tempfilename + len, ".%d", CC->cs_pid);
441 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
443 if ((TmpFD > 0) && (errno == 0))
445 char *tmp = malloc(StatBuf.st_size * 2);
446 memset(tmp, ' ', StatBuf.st_size * 2);
447 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
449 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
452 cprintf("%d Unable to allocate the space required for %s: %s\n",
453 ERROR + INTERNAL_ERROR,
456 unlink(tempfilename);
459 lseek(TmpFD, SEEK_SET, 0);
462 cprintf("%d Unable to allocate the space required for %s: %s\n",
463 ERROR + INTERNAL_ERROR,
466 unlink(tempfilename);
471 cprintf("%d %s\n", SEND_LISTING, tempfilename);
474 while (rc = CtdlClientGetLine(Line),
477 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
483 for (i = 0; i < nForceAliases; i++)
485 if ((!MailAliasesFound[i]) &&
486 (strncmp(ForceAliases[i].Key,
488 ForceAliases[i].len) == 0)
491 MailAliasesFound[i] = 1;
497 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
498 write(TmpFD, ChrPtr(Line), StrLength(Line));
499 len += StrLength(Line);
502 ftruncate(TmpFD, len);
509 * Sanity check whether all aliases required by the RFCs were set
513 for (i = 0; i < nForceAliases; i++)
515 if (!MailAliasesFound[i])
520 cprintf("%d won't do this - you're missing an RFC required alias.\n",
521 ERROR + INTERNAL_ERROR);
522 unlink(tempfilename);
527 /* Now copy the temp file to its permanent location.
528 * (We copy instead of link because they may be on different filesystems)
530 begin_critical_section(S_NETCONFIGS);
531 rename(tempfilename, filename);
532 end_critical_section(S_NETCONFIGS);
536 * cmd_netp() - authenticate to the server as another Citadel node polling
537 * for network traffic
539 void cmd_netp(char *cmdbuf)
541 struct CitContext *CCC = CC;
542 HashList *working_ignetcfg;
550 const StrBuf *secret = NULL;
551 const StrBuf *nexthop = NULL;
552 char err_buf[SIZ] = "";
555 node = CCC->curr_user;
556 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
557 NodeStr = NewStrBufPlain(node, nodelen);
558 /* load the IGnet Configuration to check node validity */
559 working_ignetcfg = load_ignetcfg();
560 v = is_valid_node(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
562 snprintf(err_buf, sizeof err_buf,
563 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
564 node, CCC->cs_host, CCC->cs_addr
566 syslog(LOG_WARNING, "%s", err_buf);
567 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
569 strs[0] = CCC->cs_addr;
570 lens[0] = strlen(CCC->cs_addr);
572 strs[1] = "SRV_UNKNOWN";
573 lens[1] = sizeof("SRV_UNKNOWN" - 1);
578 2, strs, (long*) &lens);
580 DeleteHash(&working_ignetcfg);
581 FreeStrBuf(&NodeStr);
585 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
586 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
587 snprintf(err_buf, sizeof err_buf,
588 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
589 CCC->cs_host, CCC->cs_addr, node
591 syslog(LOG_WARNING, "%s", err_buf);
592 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
594 strs[0] = CCC->cs_addr;
595 lens[0] = strlen(CCC->cs_addr);
598 lens[1] = sizeof("SRV_PW" - 1);
603 2, strs, (long*) &lens);
605 DeleteHash(&working_ignetcfg);
606 FreeStrBuf(&NodeStr);
610 if (network_talking_to(node, nodelen, NTT_CHECK)) {
611 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
612 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
613 DeleteHash(&working_ignetcfg);
614 FreeStrBuf(&NodeStr);
617 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
618 network_talking_to(CCC->net_node, nodelen, NTT_ADD);
619 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
620 CCC->net_node, CCC->cs_host, CCC->cs_addr
622 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
623 DeleteHash(&working_ignetcfg);
624 FreeStrBuf(&NodeStr);
627 int netconfig_check_roomaccess(
630 const char* RemoteIdentifier)
636 if (RemoteIdentifier == NULL)
638 snprintf(errmsgbuf, n, "Need sender to permit access.");
639 return (ERROR + USERNAME_REQUIRED);
642 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
643 begin_critical_section(S_NETCONFIGS);
644 if (!read_spoolcontrol_file(&sc, filename))
646 end_critical_section(S_NETCONFIGS);
647 snprintf(errmsgbuf, n,
648 "This mailing list only accepts posts from subscribers.");
649 return (ERROR + NO_SUCH_USER);
651 end_critical_section(S_NETCONFIGS);
652 found = is_recipient (sc, RemoteIdentifier);
653 free_spoolcontrol_struct(&sc);
658 snprintf(errmsgbuf, n,
659 "This mailing list only accepts posts from subscribers.");
660 return (ERROR + NO_SUCH_USER);
666 CTDL_MODULE_INIT(netconfig)
670 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
671 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
672 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");