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.
23 # if HAVE_SYS_SYSCALL_H
24 # include <sys/syscall.h>
28 #include <libcitadel.h>
30 #include "include/ctdl_module.h"
31 HashList *CfgTypeHash = NULL;
33 /*-----------------------------------------------------------------------------*
34 * Per room network configs *
35 *-----------------------------------------------------------------------------*/
36 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, CfgLineSerializer s, CfgLineDeAllocator d)
40 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
46 pCfg->IsSingleLine = uniq;
48 if (CfgTypeHash == NULL)
49 CfgTypeHash = NewHash(1, NULL);
50 Put(CfgTypeHash, Name, len, pCfg, NULL);
54 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
58 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
60 return (const CfgLineType *) pv;
68 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
75 RewindHashPos(CfgTypeHash, It, 1);
76 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
78 pCfg = (CfgLineType*) pv;
84 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
88 nptr = (RoomNetCfgLine *)
89 malloc(sizeof(RoomNetCfgLine));
90 nptr->next = OneRNCFG->NetConfigs[ThisOne->C];
91 nptr->Value = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
92 OneRNCFG->NetConfigs[ThisOne->C] = nptr;
95 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCFG, RoomNetCfgLine *data)
97 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
98 StrBufAppendBuf(OutputBuffer, data->Value, 0);
99 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
102 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
104 FreeStrBuf(&(*data)->Value);
108 int read_spoolcontrol_file(OneRoomNetCfg **pOneRNCFG, char *filename)
111 const char *ErrStr = NULL;
113 const CfgLineType *pCfg;
116 OneRoomNetCfg *OneRNCFG;
118 fd = open(filename, O_NONBLOCK|O_RDONLY);
123 OneRNCFG = malloc(sizeof(OneRoomNetCfg));
124 memset(OneRNCFG, 0, sizeof(OneRoomNetCfg));
125 *pOneRNCFG = OneRNCFG;
127 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
128 if (StrLength(Line) == 0)
131 InStr = NewStrBufPlain(NULL, StrLength(Line));
132 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
134 pCfg = GetCfgTypeByStr(SKEY(InStr));
137 pCfg->Parser(pCfg, Line, Pos, OneRNCFG);
141 if (OneRNCFG->misc == NULL)
143 OneRNCFG->misc = NewStrBufDup(Line);
147 if(StrLength(OneRNCFG->misc) > 0)
148 StrBufAppendBufPlain(OneRNCFG->misc, HKEY("\n"), 0);
149 StrBufAppendBuf(OneRNCFG->misc, Line, 0);
160 int save_spoolcontrol_file(OneRoomNetCfg *OneRNCFG, char *filename)
164 char tempfilename[PATH_MAX];
169 long reltid; /* if we don't have SYS_gettid, use "random" value */
174 len = strlen(filename);
175 memcpy(tempfilename, filename, len + 1);
177 #if defined(HAVE_SYONERNCFGALL_H) && defined (SYS_gettid)
178 reltid = syOneRNCFGall(SYS_gettid);
180 gettimeofday(&tv, NULL);
181 /* Promote to time_t; types differ on some OSes (like darwin) */
182 unixtime = tv.tv_sec;
184 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
186 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
188 if ((TmpFD < 0) || (errno != 0)) {
189 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
190 filename, strerror(errno));
191 unlink(tempfilename);
195 CfgIt = GetNewHashPos(CfgTypeHash, 1);
196 fchown(TmpFD, config.c_ctdluid, 0);
197 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
199 const CfgLineType *pCfg;
200 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
201 if (pCfg->IsSingleLine)
203 pCfg->Serializer(pCfg, OutBuffer, OneRNCFG, NULL);
207 RoomNetCfgLine *pName = OneRNCFG->NetConfigs[pCfg->C];
208 while (pName != NULL)
210 pCfg->Serializer(pCfg, OutBuffer, OneRNCFG, pName);
218 DeleteHashPos(&CfgIt);
221 if (OneRNCFG->misc != NULL) {
222 StrBufAppendBuf(OutBuffer, OneRNCFG->misc, 0);
225 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
226 if ((rc >=0 ) && (rc == StrLength(Cfg)))
229 rename(tempfilename, filename);
234 "unable to write %s; [%s]; not enough space on the disk?\n",
238 unlink(tempfilename);
241 FreeStrBuf(&OutBuffer);
249 void free_spoolcontrol_struct(OneRoomNetCfg **pOneRNCFG)
253 OneRoomNetCfg *OneRNCFG;
255 OneRNCFG = *pOneRNCFG;
256 CfgIt = GetNewHashPos(CfgTypeHash, 1);
257 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
259 const CfgLineType *pCfg;
260 RoomNetCfgLine *pNext, *pName;
262 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
263 pName= OneRNCFG->NetConfigs[pCfg->C];
264 while (pName != NULL)
267 pCfg->DeAllocator(pCfg, &pName);
271 DeleteHashPos(&CfgIt);
273 FreeStrBuf(&OneRNCFG->Sender);
274 FreeStrBuf(&OneRNCFG->RoomInfo);
275 FreeStrBuf(&OneRNCFG->misc);
281 /*-----------------------------------------------------------------------------*
282 * Per room network configs : exchange with client *
283 *-----------------------------------------------------------------------------*/
284 void cmd_gnet(char *argbuf)
286 char filename[PATH_MAX];
291 if (!IsEmptyStr(argbuf))
293 if (CtdlAccessCheck(ac_aide)) return;
294 if (strcmp(argbuf, FILE_MAILALIAS))
296 cprintf("%d No such file or directory\n",
297 ERROR + INTERNAL_ERROR);
300 safestrncpy(filename, file_mail_aliases, sizeof(filename));
301 cprintf("%d Settings for <%s>\n",
307 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
308 /* users can edit the netconfigs for their own mailbox rooms */
310 else if (CtdlAccessCheck(ac_room_aide)) return;
312 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
313 cprintf("%d Network settings for room #%ld <%s>\n",
315 CC->room.QRnumber, CC->room.QRname);
318 fp = fopen(filename, "r");
320 while (fgets(buf, sizeof buf, fp) != NULL) {
321 buf[strlen(buf)-1] = 0;
322 cprintf("%s\n", buf);
330 #define nForceAliases 5
331 const ConstStr ForceAliases[nForceAliases] = {
335 {HKEY("postmaster,")},
339 void cmd_snet(char *argbuf) {
340 char tempfilename[PATH_MAX];
341 char filename[PATH_MAX];
348 int MailAliasesFound[nForceAliases];
352 if (!IsEmptyStr(argbuf))
354 if (CtdlAccessCheck(ac_aide)) return;
355 if (strcmp(argbuf, FILE_MAILALIAS))
357 cprintf("%d No such file or directory\n",
358 ERROR + INTERNAL_ERROR);
361 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
362 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
363 memcpy(tempfilename, filename, len + 1);
368 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
369 /* users can edit the netconfigs for their own mailbox rooms */
371 else if (CtdlAccessCheck(ac_room_aide)) return;
373 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
374 memcpy(tempfilename, filename, len + 1);
376 memset(&StatBuf, 0, sizeof(struct stat));
377 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
378 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
380 sprintf(tempfilename + len, ".%d", CC->cs_pid);
382 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
384 if ((TmpFD > 0) && (errno == 0))
386 char *tmp = malloc(StatBuf.st_size * 2);
387 memset(tmp, ' ', StatBuf.st_size * 2);
388 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
390 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
393 cprintf("%d Unable to allocate the space required for %s: %s\n",
394 ERROR + INTERNAL_ERROR,
397 unlink(tempfilename);
400 lseek(TmpFD, SEEK_SET, 0);
403 cprintf("%d Unable to allocate the space required for %s: %s\n",
404 ERROR + INTERNAL_ERROR,
407 unlink(tempfilename);
412 cprintf("%d %s\n", SEND_LISTING, tempfilename);
415 while (rc = CtdlClientGetLine(Line),
418 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
424 for (i = 0; i < nForceAliases; i++)
426 if ((!MailAliasesFound[i]) &&
427 (strncmp(ForceAliases[i].Key,
429 ForceAliases[i].len) == 0)
432 MailAliasesFound[i] = 1;
438 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
439 write(TmpFD, ChrPtr(Line), StrLength(Line));
440 len += StrLength(Line);
443 ftruncate(TmpFD, len);
450 * Sanity check whether all aliases required by the RFCs were set
454 for (i = 0; i < nForceAliases; i++)
456 if (!MailAliasesFound[i])
461 cprintf("%d won't do this - you're missing an RFC required alias.\n",
462 ERROR + INTERNAL_ERROR);
463 unlink(tempfilename);
468 /* Now copy the temp file to its permanent location.
469 * (We copy instead of link because they may be on different filesystems)
471 begin_critical_section(S_NETCONFIGS);
472 rename(tempfilename, filename);
473 end_critical_section(S_NETCONFIGS);
477 /*-----------------------------------------------------------------------------*
478 * Per node network configs *
479 *-----------------------------------------------------------------------------*/
480 void DeleteCtdlNodeConf(void *vNode)
482 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
483 FreeStrBuf(&Node->NodeName);
484 FreeStrBuf(&Node->Secret);
485 FreeStrBuf(&Node->Host);
486 FreeStrBuf(&Node->Port);
490 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
492 const char *Pos = NULL;
495 /* we need at least 4 pipes and some other text so its invalid. */
496 if (StrLength(SerializedNode) < 8)
498 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
502 Node->NodeName=NewStrBuf();
503 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
505 Node->Secret=NewStrBuf();
506 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
508 Node->Host=NewStrBuf();
509 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
511 Node->Port=NewStrBuf();
512 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
518 * Load or refresh the Citadel network (IGnet) configuration for this node.
520 HashList* CtdlLoadIgNetCfg(void)
529 Cfg = CtdlGetSysConfig(IGNETCFG);
530 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
536 Hash = NewHash(1, NULL);
537 Buf = NewStrBufPlain(Cfg, -1);
539 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
543 StrBufSipLine(LineBuf, Buf, &LinePos);
544 if (StrLength(LineBuf) != 0) {
545 Node = NewNode(LineBuf);
547 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
550 } while (LinePos != StrBufNOTNULL);
552 FreeStrBuf(&LineBuf);
557 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
559 const RoomNetCfg RecipientCfgs[] = {
566 RoomNetCfgLine *nptr;
571 while (RecipientCfgs[i] != maxRoomNetCfg)
573 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
577 if ((StrLength(nptr->Value) == len) &&
578 (!strcmp(Name, ChrPtr(nptr->Value))))
590 int CtdlNetconfigCheckRoomaccess(
593 const char* RemoteIdentifier)
595 OneRoomNetCfg *RNCfg;
599 if (RemoteIdentifier == NULL)
601 snprintf(errmsgbuf, n, "Need sender to permit access.");
602 return (ERROR + USERNAME_REQUIRED);
605 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
606 begin_critical_section(S_NETCONFIGS);
607 if (!read_spoolcontrol_file(&RNCfg, filename))
609 end_critical_section(S_NETCONFIGS);
610 snprintf(errmsgbuf, n,
611 "This mailing list only accepts posts from subscribers.");
612 return (ERROR + NO_SUCH_USER);
614 end_critical_section(S_NETCONFIGS);
615 found = is_recipient (RNCfg, RemoteIdentifier);
616 free_spoolcontrol_struct(&RNCfg);
621 snprintf(errmsgbuf, n,
622 "This mailing list only accepts posts from subscribers.");
623 return (ERROR + NO_SUCH_USER);
630 * cmd_netp() - authenticate to the server as another Citadel node polling
631 * for network traffic
633 void cmd_netp(char *cmdbuf)
635 struct CitContext *CCC = CC;
636 HashList *working_ignetcfg;
644 const StrBuf *secret = NULL;
645 const StrBuf *nexthop = NULL;
646 char err_buf[SIZ] = "";
649 node = CCC->curr_user;
650 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
651 NodeStr = NewStrBufPlain(node, nodelen);
652 /* load the IGnet Configuration to check node validity */
653 working_ignetcfg = CtdlLoadIgNetCfg();
654 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
656 snprintf(err_buf, sizeof err_buf,
657 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
658 node, CCC->cs_host, CCC->cs_addr
660 syslog(LOG_WARNING, "%s", err_buf);
661 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
663 strs[0] = CCC->cs_addr;
664 lens[0] = strlen(CCC->cs_addr);
666 strs[1] = "SRV_UNKNOWN";
667 lens[1] = sizeof("SRV_UNKNOWN" - 1);
672 2, strs, (long*) &lens);
674 DeleteHash(&working_ignetcfg);
675 FreeStrBuf(&NodeStr);
679 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
680 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
681 snprintf(err_buf, sizeof err_buf,
682 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
683 CCC->cs_host, CCC->cs_addr, node
685 syslog(LOG_WARNING, "%s", err_buf);
686 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
688 strs[0] = CCC->cs_addr;
689 lens[0] = strlen(CCC->cs_addr);
692 lens[1] = sizeof("SRV_PW" - 1);
697 2, strs, (long*) &lens);
699 DeleteHash(&working_ignetcfg);
700 FreeStrBuf(&NodeStr);
704 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
705 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
706 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
707 DeleteHash(&working_ignetcfg);
708 FreeStrBuf(&NodeStr);
711 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
712 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
713 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
714 CCC->net_node, CCC->cs_host, CCC->cs_addr
716 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
717 DeleteHash(&working_ignetcfg);
718 FreeStrBuf(&NodeStr);
722 /*-----------------------------------------------------------------------------*
723 * Network maps: evaluate other nodes *
724 *-----------------------------------------------------------------------------*/
726 void DeleteNetMap(void *vNetMap)
728 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
729 FreeStrBuf(&TheNetMap->NodeName);
730 FreeStrBuf(&TheNetMap->NextHop);
734 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
736 const char *Pos = NULL;
739 /* we need at least 3 pipes and some other text so its invalid. */
740 if (StrLength(SerializedNetMap) < 6)
742 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
744 NM->NodeName=NewStrBuf();
745 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
747 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
749 NM->NextHop=NewStrBuf();
750 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
755 HashList* CtdlReadNetworkMap(void)
762 CtdlNetMap *TheNetMap;
764 Cfg = CtdlGetSysConfig(IGNETMAP);
765 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
771 Hash = NewHash(1, NULL);
772 Buf = NewStrBufPlain(Cfg, -1);
774 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
776 while (StrBufSipLine(Buf, LineBuf, &LinePos))
778 TheNetMap = NewNetMap(LineBuf);
779 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
780 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
784 FreeStrBuf(&LineBuf);
788 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
793 StrBuf *Ret = NewStrBuf();
794 HashPos *Pos = GetNewHashPos(Map, 0);
796 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
798 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
799 StrBufAppendBuf(Ret, pMap->NodeName, 0);
800 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
802 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
803 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
805 StrBufAppendBuf(Ret, pMap->NextHop, 0);
806 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
814 * Learn topology from path fields
816 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
818 CtdlNetMap *pNM = NULL;
823 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
824 (vptr != NULL))/* TODO: is the NodeName Uniq? */
826 pNM = (CtdlNetMap*)vptr;
827 extract_token(nexthop, path, 0, '!', sizeof nexthop);
828 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
829 pNM->lastcontact = time(NULL);
830 (*netmap_changed) ++;
835 /* If we got here then it's not in the map, so add it. */
836 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
837 nmptr->NodeName = NewStrBufPlain(node, -1);
838 nmptr->lastcontact = time(NULL);
839 nmptr->NextHop = NewStrBuf ();
840 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
841 /* TODO: is the NodeName Uniq? */
842 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
843 (*netmap_changed) ++;
848 * Check the network map and determine whether the supplied node name is
849 * valid. If it is not a neighbor node, supply the name of a neighbor node
850 * which is the next hop. If it *is* a neighbor node, we also fill in the
853 int CtdlIsValidNode(const StrBuf **nexthop,
854 const StrBuf **secret,
857 HashList *the_netmap)
861 CtdlNodeConf *TheNode;
862 CtdlNetMap *TheNetMap;
864 if (StrLength(node) == 0) {
869 * First try the neighbor nodes
871 if (GetCount(IgnetCfg) == 0) {
872 syslog(LOG_INFO, "IgnetCfg is empty!\n");
873 if (nexthop != NULL) {
879 /* try to find a neigbour with the name 'node' */
880 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
883 TheNode = (CtdlNodeConf*)vNodeConf;
885 *secret = TheNode->Secret;
886 return 0; /* yup, it's a direct neighbor */
890 * If we get to this point we have to see if we know the next hop
891 *//* TODO: is the NodeName Uniq? */
892 if ((GetCount(the_netmap) > 0) &&
893 (GetHash(the_netmap, SKEY(node), &vNetMap)))
895 TheNetMap = (CtdlNetMap*)vNetMap;
897 *nexthop = TheNetMap->NextHop;
902 * If we get to this point, the supplied node name is bogus.
904 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
912 CTDL_MODULE_INIT(netconfig)
917 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
918 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
919 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");