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>
29 #include <libcitadel.h>
31 #include "include/ctdl_module.h"
34 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg);
36 HashList *CfgTypeHash = NULL;
37 HashList *RoomConfigs = NULL;
38 /*-----------------------------------------------------------------------------*
39 * Per room network configs *
40 *-----------------------------------------------------------------------------*/
41 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d)
45 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
48 pCfg->DeAllocator = d;
52 pCfg->IsSingleLine = uniq;
53 pCfg->nSegments = nSegments;
54 if (CfgTypeHash == NULL)
55 CfgTypeHash = NewHash(1, NULL);
56 Put(CfgTypeHash, Name, len, pCfg, NULL);
60 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
64 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
66 return (const CfgLineType *) pv;
74 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
81 RewindHashPos(CfgTypeHash, It, 1);
82 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
84 pCfg = (CfgLineType*) pv;
90 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCfg)
95 nptr = (RoomNetCfgLine *)
96 malloc(sizeof(RoomNetCfgLine));
97 nptr->next = OneRNCfg->NetConfigs[ThisOne->C];
98 nptr->Value = malloc(sizeof(StrBuf*) * ThisOne->nSegments);
100 memset(nptr->Value, 0, sizeof(StrBuf*) * ThisOne->nSegments);
101 if (ThisOne->nSegments == 1)
103 nptr->Value[0] = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
106 else for (i = 0; i < ThisOne->nSegments; i++)
109 nptr->Value[i] = NewStrBufPlain(NULL, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
110 StrBufExtract_NextToken(nptr->Value[i], Line, &LinePos, '|');
113 OneRNCfg->NetConfigs[ThisOne->C] = nptr;
116 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCfg, RoomNetCfgLine *data)
120 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
121 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
122 for (i = 0; i < ThisOne->nSegments; i++)
124 StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
125 if (i + 1 < ThisOne->nSegments)
126 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
128 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
131 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
138 for (i = 0; i < (*data)->nValues; i++)
140 FreeStrBuf(&(*data)->Value[i]);
142 free ((*data)->Value);
146 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
148 RoomNetCfgLine *NewData;
150 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
152 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
154 for (i = 0; i < data->nValues; i++)
156 NewData->Value[i] = NewStrBufDup(data->Value[i]);
160 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
163 const char *ErrStr = NULL;
165 const CfgLineType *pCfg;
168 OneRoomNetCfg *OneRNCfg;
170 fd = open(filename, O_NONBLOCK|O_RDONLY);
175 if (*pOneRNCfg != NULL)
176 OneRNCfg = *pOneRNCfg;
178 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
179 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
180 *pOneRNCfg = OneRNCfg;
184 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
185 if (StrLength(Line) == 0)
188 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
190 pCfg = GetCfgTypeByStr(SKEY(InStr));
193 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
197 if (OneRNCfg->misc == NULL)
199 OneRNCfg->misc = NewStrBufDup(Line);
203 if(StrLength(OneRNCfg->misc) > 0)
204 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
205 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
216 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
220 StrBuf *OutBuffer = NULL;
221 char tempfilename[PATH_MAX];
226 long reltid; /* if we don't have SYS_gettid, use "random" value */
230 len = strlen(filename);
231 memcpy(tempfilename, filename, len + 1);
233 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
234 reltid = syscall(SYS_gettid);
236 gettimeofday(&tv, NULL);
237 /* Promote to time_t; types differ on some OSes (like darwin) */
238 unixtime = tv.tv_sec;
240 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
242 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
244 if ((TmpFD < 0) || (errno != 0)) {
245 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
246 filename, strerror(errno));
247 unlink(tempfilename);
252 OutBuffer = NewStrBuf();
253 CfgIt = GetNewHashPos(CfgTypeHash, 1);
254 fchown(TmpFD, config.c_ctdluid, 0);
255 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
257 const CfgLineType *pCfg;
258 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
259 if (pCfg->IsSingleLine)
261 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
265 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
266 while (pName != NULL)
268 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
276 DeleteHashPos(&CfgIt);
279 if (OneRNCfg->misc != NULL) {
280 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
283 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
284 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
287 rename(tempfilename, filename);
292 "unable to write %s; [%s]; not enough space on the disk?\n",
296 unlink(tempfilename);
299 FreeStrBuf(&OutBuffer);
306 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
308 char filename[PATH_MAX];
310 if (OneRNCfg->changed)
312 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
313 SaveRoomNetConfigFile(OneRNCfg, filename);
314 OneRNCfg->changed = 0;
317 void SaveChangedConfigs(void)
319 CtdlForEachNetCfgRoom(SaveModifiedRooms,
325 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
328 RoomNetCfgLine **pLine;
329 char filename[PATH_MAX];
331 if (OneRNCfg == NULL)
334 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
335 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
337 pLine = &OneRNCfg->NetConfigs[LineType];
339 while(*pLine != NULL) pLine = &((*pLine)->next);
342 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
343 SaveRoomNetConfigFile(OneRNCfg, filename);
344 OneRNCfg->changed = 0;
347 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
351 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
356 CfgIt = GetNewHashPos(CfgTypeHash, 1);
357 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
359 const CfgLineType *pCfg;
360 RoomNetCfgLine *pNext, *pName;
362 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
363 pName= OneRNCfg->NetConfigs[eCfg];
364 while (pName != NULL)
369 pCfg->DeAllocator(pCfg, &pName);
373 DeleteGenericCfgLine(NULL, &pName);
378 DeleteHashPos(&CfgIt);
380 FreeStrBuf(&OneRNCfg->Sender);
381 FreeStrBuf(&OneRNCfg->RoomInfo);
382 FreeStrBuf(&OneRNCfg->misc);
384 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
386 OneRoomNetCfg *OneRNCfg;
387 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
388 FreeRoomNetworkStructContent(OneRNCfg);
391 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
393 vFreeRoomNetworkStruct(*pOneRNCfg);
397 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
400 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
401 return (OneRoomNetCfg*)pv;
405 void LoadAllNetConfigs(void)
409 struct dirent *filedir_entry;
413 OneRoomNetCfg *OneRNCfg;
418 RoomConfigs = NewHash(1, NULL);
419 filedir = opendir (ctdl_netcfg_dir);
420 if (filedir == NULL) {
421 return ; /// todo: panic!
424 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
430 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
431 (filedir_entry != NULL))
433 #ifdef _DIRENT_HAVE_D_NAMLEN
434 d_namelen = filedir_entry->d_namelen;
436 d_namelen = strlen(filedir_entry->d_name);
439 #ifdef _DIRENT_HAVE_D_TYPE
440 d_type = filedir_entry->d_type;
449 #define IFTODT(mode) (((mode) & 0170000) >> 12)
450 #define DTTOIF(dirtype) ((dirtype) << 12)
454 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
455 continue; /* Ignore backup files... */
457 if ((d_namelen == 1) &&
458 (filedir_entry->d_name[0] == '.'))
461 if ((d_namelen == 2) &&
462 (filedir_entry->d_name[0] == '.') &&
463 (filedir_entry->d_name[1] == '.'))
466 snprintf(path, PATH_MAX, "%s/%s",
467 ctdl_netcfg_dir, filedir_entry->d_name);
469 if (d_type == DT_UNKNOWN) {
471 if (lstat(path, &s) == 0) {
472 d_type = IFTODT(s.st_mode);
480 case DT_LNK: /* TODO: check whether its a file or a directory */
483 pch = filedir_entry->d_name;
495 RoomNumber = atol(filedir_entry->d_name);
496 ReadRoomNetConfigFile(&OneRNCfg, path);
498 if (OneRNCfg != NULL)
499 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
500 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
514 /*-----------------------------------------------------------------------------*
515 * Per room network configs : exchange with client *
516 *-----------------------------------------------------------------------------*/
517 void cmd_gnet(char *argbuf)
519 char filename[PATH_MAX];
524 if (!IsEmptyStr(argbuf))
526 if (CtdlAccessCheck(ac_aide)) return;
527 if (strcmp(argbuf, FILE_MAILALIAS))
529 cprintf("%d No such file or directory\n",
530 ERROR + INTERNAL_ERROR);
533 safestrncpy(filename, file_mail_aliases, sizeof(filename));
534 cprintf("%d Settings for <%s>\n",
540 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
541 /* users can edit the netconfigs for their own mailbox rooms */
543 else if (CtdlAccessCheck(ac_room_aide)) return;
545 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
546 cprintf("%d Network settings for room #%ld <%s>\n",
548 CC->room.QRnumber, CC->room.QRname);
551 fp = fopen(filename, "r");
553 while (fgets(buf, sizeof buf, fp) != NULL) {
554 buf[strlen(buf)-1] = 0;
555 cprintf("%s\n", buf);
563 #define nForceAliases 5
564 const ConstStr ForceAliases[nForceAliases] = {
568 {HKEY("postmaster,")},
572 void cmd_snet(char *argbuf)
574 struct CitContext *CCC = CC;
575 char tempfilename[PATH_MAX];
576 char filename[PATH_MAX];
583 int MailAliasesFound[nForceAliases];
587 if (!IsEmptyStr(argbuf))
589 if (CtdlAccessCheck(ac_aide)) return;
590 if (strcmp(argbuf, FILE_MAILALIAS))
592 cprintf("%d No such file or directory\n",
593 ERROR + INTERNAL_ERROR);
596 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
597 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
598 memcpy(tempfilename, filename, len + 1);
603 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
604 /* users can edit the netconfigs for their own mailbox rooms */
606 else if (CtdlAccessCheck(ac_room_aide)) return;
608 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
609 memcpy(tempfilename, filename, len + 1);
611 memset(&StatBuf, 0, sizeof(struct stat));
612 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
613 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
615 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
617 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
619 if ((TmpFD > 0) && (errno == 0))
621 char *tmp = malloc(StatBuf.st_size * 2);
622 memset(tmp, ' ', StatBuf.st_size * 2);
623 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
625 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
628 cprintf("%d Unable to allocate the space required for %s: %s\n",
629 ERROR + INTERNAL_ERROR,
632 unlink(tempfilename);
635 lseek(TmpFD, SEEK_SET, 0);
638 cprintf("%d Unable to allocate the space required for %s: %s\n",
639 ERROR + INTERNAL_ERROR,
642 unlink(tempfilename);
647 cprintf("%d %s\n", SEND_LISTING, tempfilename);
650 while (rc = CtdlClientGetLine(Line),
653 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
659 for (i = 0; i < nForceAliases; i++)
661 if ((!MailAliasesFound[i]) &&
662 (strncmp(ForceAliases[i].Key,
664 ForceAliases[i].len) == 0)
667 MailAliasesFound[i] = 1;
673 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
674 write(TmpFD, ChrPtr(Line), StrLength(Line));
675 len += StrLength(Line);
678 ftruncate(TmpFD, len);
685 * Sanity check whether all aliases required by the RFCs were set
689 for (i = 0; i < nForceAliases; i++)
691 if (!MailAliasesFound[i])
696 cprintf("%d won't do this - you're missing an RFC required alias.\n",
697 ERROR + INTERNAL_ERROR);
698 unlink(tempfilename);
703 /* Now copy the temp file to its permanent location.
704 * (We copy instead of link because they may be on different filesystems)
706 begin_critical_section(S_NETCONFIGS);
707 rename(tempfilename, filename);
710 OneRoomNetCfg *RNCfg;
711 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
714 FreeRoomNetworkStructContent(RNCfg);
715 ReadRoomNetConfigFile(&RNCfg, filename);
719 ReadRoomNetConfigFile(&RNCfg, filename);
720 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
723 end_critical_section(S_NETCONFIGS);
727 /*-----------------------------------------------------------------------------*
728 * Per node network configs *
729 *-----------------------------------------------------------------------------*/
730 void DeleteCtdlNodeConf(void *vNode)
732 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
733 FreeStrBuf(&Node->NodeName);
734 FreeStrBuf(&Node->Secret);
735 FreeStrBuf(&Node->Host);
736 FreeStrBuf(&Node->Port);
740 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
742 const char *Pos = NULL;
745 /* we need at least 4 pipes and some other text so its invalid. */
746 if (StrLength(SerializedNode) < 8)
748 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
752 Node->NodeName=NewStrBuf();
753 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
755 Node->Secret=NewStrBuf();
756 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
758 Node->Host=NewStrBuf();
759 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
761 Node->Port=NewStrBuf();
762 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
768 * Load or refresh the Citadel network (IGnet) configuration for this node.
770 HashList* CtdlLoadIgNetCfg(void)
779 Cfg = CtdlGetSysConfig(IGNETCFG);
780 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
786 Hash = NewHash(1, NULL);
787 Buf = NewStrBufPlain(Cfg, -1);
789 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
793 StrBufSipLine(LineBuf, Buf, &LinePos);
794 if (StrLength(LineBuf) != 0) {
795 Node = NewNode(LineBuf);
797 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
800 } while (LinePos != StrBufNOTNULL);
802 FreeStrBuf(&LineBuf);
807 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
809 const RoomNetCfg RecipientCfgs[] = {
816 RoomNetCfgLine *nptr;
821 while (RecipientCfgs[i] != maxRoomNetCfg)
823 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
827 if ((StrLength(nptr->Value[0]) == len) &&
828 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
840 int CtdlNetconfigCheckRoomaccess(
843 const char* RemoteIdentifier)
845 OneRoomNetCfg *RNCfg;
849 if (RemoteIdentifier == NULL)
851 snprintf(errmsgbuf, n, "Need sender to permit access.");
852 return (ERROR + USERNAME_REQUIRED);
855 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
856 begin_critical_section(S_NETCONFIGS);
857 if (!ReadRoomNetConfigFile(&RNCfg, filename))
859 end_critical_section(S_NETCONFIGS);
860 snprintf(errmsgbuf, n,
861 "This mailing list only accepts posts from subscribers.");
862 return (ERROR + NO_SUCH_USER);
864 end_critical_section(S_NETCONFIGS);
865 found = is_recipient (RNCfg, RemoteIdentifier);
866 vFreeRoomNetworkStruct(&RNCfg);
871 snprintf(errmsgbuf, n,
872 "This mailing list only accepts posts from subscribers.");
873 return (ERROR + NO_SUCH_USER);
880 * cmd_netp() - authenticate to the server as another Citadel node polling
881 * for network traffic
883 void cmd_netp(char *cmdbuf)
885 struct CitContext *CCC = CC;
886 HashList *working_ignetcfg;
894 const StrBuf *secret = NULL;
895 const StrBuf *nexthop = NULL;
896 char err_buf[SIZ] = "";
899 node = CCC->curr_user;
900 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
901 NodeStr = NewStrBufPlain(node, nodelen);
902 /* load the IGnet Configuration to check node validity */
903 working_ignetcfg = CtdlLoadIgNetCfg();
904 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
906 snprintf(err_buf, sizeof err_buf,
907 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
908 node, CCC->cs_host, CCC->cs_addr
910 syslog(LOG_WARNING, "%s", err_buf);
911 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
913 strs[0] = CCC->cs_addr;
914 lens[0] = strlen(CCC->cs_addr);
916 strs[1] = "SRV_UNKNOWN";
917 lens[1] = sizeof("SRV_UNKNOWN" - 1);
922 2, strs, (long*) &lens);
924 DeleteHash(&working_ignetcfg);
925 FreeStrBuf(&NodeStr);
929 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
930 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
931 snprintf(err_buf, sizeof err_buf,
932 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
933 CCC->cs_host, CCC->cs_addr, node
935 syslog(LOG_WARNING, "%s", err_buf);
936 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
938 strs[0] = CCC->cs_addr;
939 lens[0] = strlen(CCC->cs_addr);
942 lens[1] = sizeof("SRV_PW" - 1);
947 2, strs, (long*) &lens);
949 DeleteHash(&working_ignetcfg);
950 FreeStrBuf(&NodeStr);
954 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
955 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
956 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
957 DeleteHash(&working_ignetcfg);
958 FreeStrBuf(&NodeStr);
961 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
962 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
963 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
964 CCC->net_node, CCC->cs_host, CCC->cs_addr
966 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
967 DeleteHash(&working_ignetcfg);
968 FreeStrBuf(&NodeStr);
972 /*-----------------------------------------------------------------------------*
973 * Network maps: evaluate other nodes *
974 *-----------------------------------------------------------------------------*/
976 void DeleteNetMap(void *vNetMap)
978 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
979 FreeStrBuf(&TheNetMap->NodeName);
980 FreeStrBuf(&TheNetMap->NextHop);
984 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
986 const char *Pos = NULL;
989 /* we need at least 3 pipes and some other text so its invalid. */
990 if (StrLength(SerializedNetMap) < 6)
992 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
994 NM->NodeName=NewStrBuf();
995 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
997 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
999 NM->NextHop=NewStrBuf();
1000 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
1005 HashList* CtdlReadNetworkMap(void)
1007 const char *LinePos;
1012 CtdlNetMap *TheNetMap;
1014 Hash = NewHash(1, NULL);
1015 Cfg = CtdlGetSysConfig(IGNETMAP);
1016 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1022 Buf = NewStrBufPlain(Cfg, -1);
1024 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1026 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1028 TheNetMap = NewNetMap(LineBuf);
1029 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1030 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1034 FreeStrBuf(&LineBuf);
1038 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1043 StrBuf *Ret = NewStrBuf();
1044 HashPos *Pos = GetNewHashPos(Map, 0);
1046 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1048 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1049 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1050 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1052 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1053 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1055 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1056 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1058 DeleteHashPos(&Pos);
1064 * Learn topology from path fields
1066 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1068 CtdlNetMap *pNM = NULL;
1073 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1074 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1076 pNM = (CtdlNetMap*)vptr;
1077 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1078 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1079 pNM->lastcontact = time(NULL);
1080 (*netmap_changed) ++;
1085 /* If we got here then it's not in the map, so add it. */
1086 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1087 nmptr->NodeName = NewStrBufPlain(node, -1);
1088 nmptr->lastcontact = time(NULL);
1089 nmptr->NextHop = NewStrBuf ();
1090 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1091 /* TODO: is the NodeName Uniq? */
1092 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1093 (*netmap_changed) ++;
1098 * Check the network map and determine whether the supplied node name is
1099 * valid. If it is not a neighbor node, supply the name of a neighbor node
1100 * which is the next hop. If it *is* a neighbor node, we also fill in the
1103 int CtdlIsValidNode(const StrBuf **nexthop,
1104 const StrBuf **secret,
1107 HashList *the_netmap)
1111 CtdlNodeConf *TheNode;
1112 CtdlNetMap *TheNetMap;
1114 if (StrLength(node) == 0) {
1119 * First try the neighbor nodes
1121 if (GetCount(IgnetCfg) == 0) {
1122 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1123 if (nexthop != NULL) {
1129 /* try to find a neigbour with the name 'node' */
1130 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1131 (vNodeConf != NULL))
1133 TheNode = (CtdlNodeConf*)vNodeConf;
1135 *secret = TheNode->Secret;
1136 return 0; /* yup, it's a direct neighbor */
1140 * If we get to this point we have to see if we know the next hop
1141 *//* TODO: is the NodeName Uniq? */
1142 if ((GetCount(the_netmap) > 0) &&
1143 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1145 TheNetMap = (CtdlNetMap*)vNetMap;
1146 if (nexthop != NULL)
1147 *nexthop = TheNetMap->NextHop;
1152 * If we get to this point, the supplied node name is bogus.
1154 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1159 void destroy_network_cfgs(void)
1161 HashList *pCfgTypeHash = CfgTypeHash;
1162 HashList *pRoomConfigs = RoomConfigs;
1167 DeleteHash(&pCfgTypeHash);
1168 DeleteHash(&pRoomConfigs);
1172 * Module entry point
1174 CTDL_MODULE_INIT(netconfig)
1178 CtdlRegisterCleanupHook(destroy_network_cfgs);
1179 LoadAllNetConfigs ();
1180 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1181 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1182 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");