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"
32 HashList *CfgTypeHash = NULL;
33 HashList *RoomConfigs = NULL;
34 /*-----------------------------------------------------------------------------*
35 * Per room network configs *
36 *-----------------------------------------------------------------------------*/
37 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d)
41 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
44 pCfg->DeAllocator = d;
48 pCfg->IsSingleLine = uniq;
49 pCfg->nSegments = nSegments;
50 if (CfgTypeHash == NULL)
51 CfgTypeHash = NewHash(1, NULL);
52 Put(CfgTypeHash, Name, len, pCfg, NULL);
56 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
60 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
62 return (const CfgLineType *) pv;
70 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
77 RewindHashPos(CfgTypeHash, It, 1);
78 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
80 pCfg = (CfgLineType*) pv;
86 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCfg)
91 nptr = (RoomNetCfgLine *)
92 malloc(sizeof(RoomNetCfgLine));
93 nptr->next = OneRNCfg->NetConfigs[ThisOne->C];
94 nptr->Value = malloc(sizeof(StrBuf*) * ThisOne->nSegments);
96 memset(nptr->Value, 0, sizeof(StrBuf*) * ThisOne->nSegments);
97 if (ThisOne->nSegments == 1)
99 nptr->Value[0] = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
102 else for (i = 0; i < ThisOne->nSegments; i++)
105 nptr->Value[i] = NewStrBufPlain(NULL, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
106 StrBufExtract_NextToken(nptr->Value[i], Line, &LinePos, '|');
109 OneRNCfg->NetConfigs[ThisOne->C] = nptr;
112 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCfg, RoomNetCfgLine *data)
116 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
117 for (i = 0; i < ThisOne->nSegments; i++)
119 StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
120 if (i + 1 < ThisOne->nSegments)
121 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
123 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
126 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
130 for (i = 0; i < ThisOne->nSegments; i++)
132 FreeStrBuf(&(*data)->Value[i]);
134 free ((*data)->Value);
138 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
140 RoomNetCfgLine *NewData;
142 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
144 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
146 for (i = 0; i < data->nValues; i++)
148 NewData->Value[i] = NewStrBufDup(data->Value[i]);
152 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
155 const char *ErrStr = NULL;
157 const CfgLineType *pCfg;
160 OneRoomNetCfg *OneRNCfg;
162 fd = open(filename, O_NONBLOCK|O_RDONLY);
167 if (*pOneRNCfg != NULL)
168 OneRNCfg = *pOneRNCfg;
170 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
171 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
172 *pOneRNCfg = OneRNCfg;
175 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
176 if (StrLength(Line) == 0)
179 InStr = NewStrBufPlain(NULL, StrLength(Line));
180 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
182 pCfg = GetCfgTypeByStr(SKEY(InStr));
185 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
189 if (OneRNCfg->misc == NULL)
191 OneRNCfg->misc = NewStrBufDup(Line);
195 if(StrLength(OneRNCfg->misc) > 0)
196 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
197 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
208 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
212 StrBuf *OutBuffer = NULL;
213 char tempfilename[PATH_MAX];
218 long reltid; /* if we don't have SYS_gettid, use "random" value */
222 len = strlen(filename);
223 memcpy(tempfilename, filename, len + 1);
225 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
226 reltid = syscall(SYS_gettid);
228 gettimeofday(&tv, NULL);
229 /* Promote to time_t; types differ on some OSes (like darwin) */
230 unixtime = tv.tv_sec;
232 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
234 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
236 if ((TmpFD < 0) || (errno != 0)) {
237 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
238 filename, strerror(errno));
239 unlink(tempfilename);
244 OutBuffer = NewStrBuf();
245 CfgIt = GetNewHashPos(CfgTypeHash, 1);
246 fchown(TmpFD, config.c_ctdluid, 0);
247 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
249 const CfgLineType *pCfg;
250 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
251 if (pCfg->IsSingleLine)
253 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
257 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
258 while (pName != NULL)
260 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
268 DeleteHashPos(&CfgIt);
271 if (OneRNCfg->misc != NULL) {
272 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
275 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
276 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
279 rename(tempfilename, filename);
284 "unable to write %s; [%s]; not enough space on the disk?\n",
288 unlink(tempfilename);
291 FreeStrBuf(&OutBuffer);
298 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
300 char filename[PATH_MAX];
302 if (OneRNCfg->changed)
304 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
305 SaveRoomNetConfigFile(OneRNCfg, filename);
306 OneRNCfg->changed = 0;
309 void SaveChangedConfigs(void)
311 CtdlForEachNetCfgRoom(SaveModifiedRooms,
315 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
320 CfgIt = GetNewHashPos(CfgTypeHash, 1);
321 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
323 const CfgLineType *pCfg;
324 RoomNetCfgLine *pNext, *pName;
326 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
327 pName= OneRNCfg->NetConfigs[pCfg->C];
328 while (pName != NULL)
331 pCfg->DeAllocator(pCfg, &pName);
335 DeleteHashPos(&CfgIt);
337 FreeStrBuf(&OneRNCfg->Sender);
338 FreeStrBuf(&OneRNCfg->RoomInfo);
339 FreeStrBuf(&OneRNCfg->misc);
341 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
343 OneRoomNetCfg *OneRNCfg;
344 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
345 FreeRoomNetworkStructContent(OneRNCfg);
348 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
350 vFreeRoomNetworkStruct(*pOneRNCfg);
354 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
357 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
358 return (OneRoomNetCfg*)pv;
362 void LoadAllNetConfigs(void)
366 struct dirent *filedir_entry;
370 OneRoomNetCfg *OneRNCfg;
375 RoomConfigs = NewHash(1, NULL);
376 filedir = opendir (ctdl_netcfg_dir);
377 if (filedir == NULL) {
378 return ; /// todo: panic!
381 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
387 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
388 (filedir_entry != NULL))
390 #ifdef _DIRENT_HAVE_D_NAMLEN
391 d_namelen = filedir_entry->d_namelen;
393 d_namelen = strlen(filedir_entry->d_name);
396 #ifdef _DIRENT_HAVE_D_TYPE
397 d_type = filedir_entry->d_type;
406 #define IFTODT(mode) (((mode) & 0170000) >> 12)
407 #define DTTOIF(dirtype) ((dirtype) << 12)
411 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
412 continue; /* Ignore backup files... */
414 if ((d_namelen == 1) &&
415 (filedir_entry->d_name[0] == '.'))
418 if ((d_namelen == 2) &&
419 (filedir_entry->d_name[0] == '.') &&
420 (filedir_entry->d_name[1] == '.'))
423 snprintf(path, PATH_MAX, "%s/%s",
424 ctdl_netcfg_dir, filedir_entry->d_name);
426 if (d_type == DT_UNKNOWN) {
428 if (lstat(path, &s) == 0) {
429 d_type = IFTODT(s.st_mode);
437 case DT_LNK: /* TODO: check whether its a file or a directory */
440 pch = filedir_entry->d_name;
452 RoomNumber = atol(filedir_entry->d_name);
453 ReadRoomNetConfigFile(&OneRNCfg, path);
455 if (OneRNCfg != NULL)
456 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
457 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
471 /*-----------------------------------------------------------------------------*
472 * Per room network configs : exchange with client *
473 *-----------------------------------------------------------------------------*/
474 void cmd_gnet(char *argbuf)
476 char filename[PATH_MAX];
481 if (!IsEmptyStr(argbuf))
483 if (CtdlAccessCheck(ac_aide)) return;
484 if (strcmp(argbuf, FILE_MAILALIAS))
486 cprintf("%d No such file or directory\n",
487 ERROR + INTERNAL_ERROR);
490 safestrncpy(filename, file_mail_aliases, sizeof(filename));
491 cprintf("%d Settings for <%s>\n",
497 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
498 /* users can edit the netconfigs for their own mailbox rooms */
500 else if (CtdlAccessCheck(ac_room_aide)) return;
502 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
503 cprintf("%d Network settings for room #%ld <%s>\n",
505 CC->room.QRnumber, CC->room.QRname);
508 fp = fopen(filename, "r");
510 while (fgets(buf, sizeof buf, fp) != NULL) {
511 buf[strlen(buf)-1] = 0;
512 cprintf("%s\n", buf);
520 #define nForceAliases 5
521 const ConstStr ForceAliases[nForceAliases] = {
525 {HKEY("postmaster,")},
529 void cmd_snet(char *argbuf)
531 struct CitContext *CCC = CC;
532 char tempfilename[PATH_MAX];
533 char filename[PATH_MAX];
540 int MailAliasesFound[nForceAliases];
544 if (!IsEmptyStr(argbuf))
546 if (CtdlAccessCheck(ac_aide)) return;
547 if (strcmp(argbuf, FILE_MAILALIAS))
549 cprintf("%d No such file or directory\n",
550 ERROR + INTERNAL_ERROR);
553 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
554 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
555 memcpy(tempfilename, filename, len + 1);
560 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
561 /* users can edit the netconfigs for their own mailbox rooms */
563 else if (CtdlAccessCheck(ac_room_aide)) return;
565 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
566 memcpy(tempfilename, filename, len + 1);
568 memset(&StatBuf, 0, sizeof(struct stat));
569 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
570 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
572 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
574 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
576 if ((TmpFD > 0) && (errno == 0))
578 char *tmp = malloc(StatBuf.st_size * 2);
579 memset(tmp, ' ', StatBuf.st_size * 2);
580 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
582 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
585 cprintf("%d Unable to allocate the space required for %s: %s\n",
586 ERROR + INTERNAL_ERROR,
589 unlink(tempfilename);
592 lseek(TmpFD, SEEK_SET, 0);
595 cprintf("%d Unable to allocate the space required for %s: %s\n",
596 ERROR + INTERNAL_ERROR,
599 unlink(tempfilename);
604 cprintf("%d %s\n", SEND_LISTING, tempfilename);
607 while (rc = CtdlClientGetLine(Line),
610 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
616 for (i = 0; i < nForceAliases; i++)
618 if ((!MailAliasesFound[i]) &&
619 (strncmp(ForceAliases[i].Key,
621 ForceAliases[i].len) == 0)
624 MailAliasesFound[i] = 1;
630 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
631 write(TmpFD, ChrPtr(Line), StrLength(Line));
632 len += StrLength(Line);
635 ftruncate(TmpFD, len);
642 * Sanity check whether all aliases required by the RFCs were set
646 for (i = 0; i < nForceAliases; i++)
648 if (!MailAliasesFound[i])
653 cprintf("%d won't do this - you're missing an RFC required alias.\n",
654 ERROR + INTERNAL_ERROR);
655 unlink(tempfilename);
660 /* Now copy the temp file to its permanent location.
661 * (We copy instead of link because they may be on different filesystems)
663 begin_critical_section(S_NETCONFIGS);
664 rename(tempfilename, filename);
667 OneRoomNetCfg *RNCfg;
668 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
671 FreeRoomNetworkStructContent(RNCfg);
672 ReadRoomNetConfigFile(&RNCfg, filename);
676 ReadRoomNetConfigFile(&RNCfg, filename);
677 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
680 end_critical_section(S_NETCONFIGS);
684 /*-----------------------------------------------------------------------------*
685 * Per node network configs *
686 *-----------------------------------------------------------------------------*/
687 void DeleteCtdlNodeConf(void *vNode)
689 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
690 FreeStrBuf(&Node->NodeName);
691 FreeStrBuf(&Node->Secret);
692 FreeStrBuf(&Node->Host);
693 FreeStrBuf(&Node->Port);
697 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
699 const char *Pos = NULL;
702 /* we need at least 4 pipes and some other text so its invalid. */
703 if (StrLength(SerializedNode) < 8)
705 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
709 Node->NodeName=NewStrBuf();
710 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
712 Node->Secret=NewStrBuf();
713 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
715 Node->Host=NewStrBuf();
716 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
718 Node->Port=NewStrBuf();
719 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
725 * Load or refresh the Citadel network (IGnet) configuration for this node.
727 HashList* CtdlLoadIgNetCfg(void)
736 Cfg = CtdlGetSysConfig(IGNETCFG);
737 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
743 Hash = NewHash(1, NULL);
744 Buf = NewStrBufPlain(Cfg, -1);
746 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
750 StrBufSipLine(LineBuf, Buf, &LinePos);
751 if (StrLength(LineBuf) != 0) {
752 Node = NewNode(LineBuf);
754 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
757 } while (LinePos != StrBufNOTNULL);
759 FreeStrBuf(&LineBuf);
764 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
766 const RoomNetCfg RecipientCfgs[] = {
773 RoomNetCfgLine *nptr;
778 while (RecipientCfgs[i] != maxRoomNetCfg)
780 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
784 if ((StrLength(nptr->Value[0]) == len) &&
785 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
797 int CtdlNetconfigCheckRoomaccess(
800 const char* RemoteIdentifier)
802 OneRoomNetCfg *RNCfg;
806 if (RemoteIdentifier == NULL)
808 snprintf(errmsgbuf, n, "Need sender to permit access.");
809 return (ERROR + USERNAME_REQUIRED);
812 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
813 begin_critical_section(S_NETCONFIGS);
814 if (!ReadRoomNetConfigFile(&RNCfg, filename))
816 end_critical_section(S_NETCONFIGS);
817 snprintf(errmsgbuf, n,
818 "This mailing list only accepts posts from subscribers.");
819 return (ERROR + NO_SUCH_USER);
821 end_critical_section(S_NETCONFIGS);
822 found = is_recipient (RNCfg, RemoteIdentifier);
823 vFreeRoomNetworkStruct(&RNCfg);
828 snprintf(errmsgbuf, n,
829 "This mailing list only accepts posts from subscribers.");
830 return (ERROR + NO_SUCH_USER);
837 * cmd_netp() - authenticate to the server as another Citadel node polling
838 * for network traffic
840 void cmd_netp(char *cmdbuf)
842 struct CitContext *CCC = CC;
843 HashList *working_ignetcfg;
851 const StrBuf *secret = NULL;
852 const StrBuf *nexthop = NULL;
853 char err_buf[SIZ] = "";
856 node = CCC->curr_user;
857 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
858 NodeStr = NewStrBufPlain(node, nodelen);
859 /* load the IGnet Configuration to check node validity */
860 working_ignetcfg = CtdlLoadIgNetCfg();
861 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
863 snprintf(err_buf, sizeof err_buf,
864 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
865 node, CCC->cs_host, CCC->cs_addr
867 syslog(LOG_WARNING, "%s", err_buf);
868 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
870 strs[0] = CCC->cs_addr;
871 lens[0] = strlen(CCC->cs_addr);
873 strs[1] = "SRV_UNKNOWN";
874 lens[1] = sizeof("SRV_UNKNOWN" - 1);
879 2, strs, (long*) &lens);
881 DeleteHash(&working_ignetcfg);
882 FreeStrBuf(&NodeStr);
886 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
887 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
888 snprintf(err_buf, sizeof err_buf,
889 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
890 CCC->cs_host, CCC->cs_addr, node
892 syslog(LOG_WARNING, "%s", err_buf);
893 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
895 strs[0] = CCC->cs_addr;
896 lens[0] = strlen(CCC->cs_addr);
899 lens[1] = sizeof("SRV_PW" - 1);
904 2, strs, (long*) &lens);
906 DeleteHash(&working_ignetcfg);
907 FreeStrBuf(&NodeStr);
911 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
912 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
913 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
914 DeleteHash(&working_ignetcfg);
915 FreeStrBuf(&NodeStr);
918 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
919 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
920 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
921 CCC->net_node, CCC->cs_host, CCC->cs_addr
923 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
924 DeleteHash(&working_ignetcfg);
925 FreeStrBuf(&NodeStr);
929 /*-----------------------------------------------------------------------------*
930 * Network maps: evaluate other nodes *
931 *-----------------------------------------------------------------------------*/
933 void DeleteNetMap(void *vNetMap)
935 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
936 FreeStrBuf(&TheNetMap->NodeName);
937 FreeStrBuf(&TheNetMap->NextHop);
941 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
943 const char *Pos = NULL;
946 /* we need at least 3 pipes and some other text so its invalid. */
947 if (StrLength(SerializedNetMap) < 6)
949 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
951 NM->NodeName=NewStrBuf();
952 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
954 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
956 NM->NextHop=NewStrBuf();
957 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
962 HashList* CtdlReadNetworkMap(void)
969 CtdlNetMap *TheNetMap;
971 Hash = NewHash(1, NULL);
972 Cfg = CtdlGetSysConfig(IGNETMAP);
973 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
979 Buf = NewStrBufPlain(Cfg, -1);
981 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
983 while (StrBufSipLine(Buf, LineBuf, &LinePos))
985 TheNetMap = NewNetMap(LineBuf);
986 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
987 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
991 FreeStrBuf(&LineBuf);
995 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1000 StrBuf *Ret = NewStrBuf();
1001 HashPos *Pos = GetNewHashPos(Map, 0);
1003 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1005 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1006 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1007 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1009 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1010 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1012 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1013 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1015 DeleteHashPos(&Pos);
1021 * Learn topology from path fields
1023 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1025 CtdlNetMap *pNM = NULL;
1030 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1031 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1033 pNM = (CtdlNetMap*)vptr;
1034 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1035 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1036 pNM->lastcontact = time(NULL);
1037 (*netmap_changed) ++;
1042 /* If we got here then it's not in the map, so add it. */
1043 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1044 nmptr->NodeName = NewStrBufPlain(node, -1);
1045 nmptr->lastcontact = time(NULL);
1046 nmptr->NextHop = NewStrBuf ();
1047 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1048 /* TODO: is the NodeName Uniq? */
1049 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1050 (*netmap_changed) ++;
1055 * Check the network map and determine whether the supplied node name is
1056 * valid. If it is not a neighbor node, supply the name of a neighbor node
1057 * which is the next hop. If it *is* a neighbor node, we also fill in the
1060 int CtdlIsValidNode(const StrBuf **nexthop,
1061 const StrBuf **secret,
1064 HashList *the_netmap)
1068 CtdlNodeConf *TheNode;
1069 CtdlNetMap *TheNetMap;
1071 if (StrLength(node) == 0) {
1076 * First try the neighbor nodes
1078 if (GetCount(IgnetCfg) == 0) {
1079 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1080 if (nexthop != NULL) {
1086 /* try to find a neigbour with the name 'node' */
1087 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1088 (vNodeConf != NULL))
1090 TheNode = (CtdlNodeConf*)vNodeConf;
1092 *secret = TheNode->Secret;
1093 return 0; /* yup, it's a direct neighbor */
1097 * If we get to this point we have to see if we know the next hop
1098 *//* TODO: is the NodeName Uniq? */
1099 if ((GetCount(the_netmap) > 0) &&
1100 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1102 TheNetMap = (CtdlNetMap*)vNetMap;
1103 if (nexthop != NULL)
1104 *nexthop = TheNetMap->NextHop;
1109 * If we get to this point, the supplied node name is bogus.
1111 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1116 void destroy_network_cfgs(void)
1118 HashList *pCfgTypeHash = CfgTypeHash;
1119 HashList *pRoomConfigs = RoomConfigs;
1124 DeleteHash(&pCfgTypeHash);
1125 DeleteHash(&pRoomConfigs);
1129 * Module entry point
1131 CTDL_MODULE_INIT(netconfig)
1135 CtdlRegisterCleanupHook(destroy_network_cfgs);
1136 LoadAllNetConfigs ();
1137 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1138 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1139 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");