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));
47 pCfg->IsSingleLine = uniq;
48 pCfg->nSegments = nSegments;
49 if (CfgTypeHash == NULL)
50 CfgTypeHash = NewHash(1, NULL);
51 Put(CfgTypeHash, Name, len, pCfg, NULL);
55 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
59 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
61 return (const CfgLineType *) pv;
69 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
76 RewindHashPos(CfgTypeHash, It, 1);
77 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
79 pCfg = (CfgLineType*) pv;
85 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
90 nptr = (RoomNetCfgLine *)
91 malloc(sizeof(RoomNetCfgLine));
92 nptr->next = OneRNCFG->NetConfigs[ThisOne->C];
93 nptr->Value = malloc(sizeof(StrBuf*) * ThisOne->nSegments);
94 memset(nptr->Value, 0, sizeof(StrBuf*) * ThisOne->nSegments);
95 if (ThisOne->nSegments == 1)
97 nptr->Value[0] = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
99 else for (i = 0; i < ThisOne->nSegments; i++)
101 nptr->Value[i] = NewStrBufPlain(NULL, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
102 StrBufExtract_NextToken(nptr->Value[i], Line, &LinePos, '|');
105 OneRNCFG->NetConfigs[ThisOne->C] = nptr;
108 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCFG, RoomNetCfgLine *data)
112 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
113 for (i = 0; i < ThisOne->nSegments; i++)
115 StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
116 if (i + 1 < ThisOne->nSegments)
117 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
119 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
122 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
126 for (i = 0; i < ThisOne->nSegments; i++)
128 FreeStrBuf(&(*data)->Value[i]);
130 free ((*data)->Value);
134 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
136 RoomNetCfgLine *NewData;
138 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
140 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
142 for (i = 0; i < data->nValues; i++)
144 NewData->Value[i] = NewStrBufDup(data->Value[i]);
148 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCFG, char *filename)
151 const char *ErrStr = NULL;
153 const CfgLineType *pCfg;
156 OneRoomNetCfg *OneRNCFG;
158 fd = open(filename, O_NONBLOCK|O_RDONLY);
163 OneRNCFG = malloc(sizeof(OneRoomNetCfg));
164 memset(OneRNCFG, 0, sizeof(OneRoomNetCfg));
165 *pOneRNCFG = OneRNCFG;
168 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
169 if (StrLength(Line) == 0)
172 InStr = NewStrBufPlain(NULL, StrLength(Line));
173 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
175 pCfg = GetCfgTypeByStr(SKEY(InStr));
178 pCfg->Parser(pCfg, Line, Pos, OneRNCFG);
182 if (OneRNCFG->misc == NULL)
184 OneRNCFG->misc = NewStrBufDup(Line);
188 if(StrLength(OneRNCFG->misc) > 0)
189 StrBufAppendBufPlain(OneRNCFG->misc, HKEY("\n"), 0);
190 StrBufAppendBuf(OneRNCFG->misc, Line, 0);
201 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCFG, char *filename)
205 char tempfilename[PATH_MAX];
210 long reltid; /* if we don't have SYS_gettid, use "random" value */
215 len = strlen(filename);
216 memcpy(tempfilename, filename, len + 1);
218 #if defined(HAVE_SYONERNCFGALL_H) && defined (SYS_gettid)
219 reltid = syOneRNCFGall(SYS_gettid);
221 gettimeofday(&tv, NULL);
222 /* Promote to time_t; types differ on some OSes (like darwin) */
223 unixtime = tv.tv_sec;
225 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
227 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
229 if ((TmpFD < 0) || (errno != 0)) {
230 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
231 filename, strerror(errno));
232 unlink(tempfilename);
236 CfgIt = GetNewHashPos(CfgTypeHash, 1);
237 fchown(TmpFD, config.c_ctdluid, 0);
238 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
240 const CfgLineType *pCfg;
241 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
242 if (pCfg->IsSingleLine)
244 pCfg->Serializer(pCfg, OutBuffer, OneRNCFG, NULL);
248 RoomNetCfgLine *pName = OneRNCFG->NetConfigs[pCfg->C];
249 while (pName != NULL)
251 pCfg->Serializer(pCfg, OutBuffer, OneRNCFG, pName);
259 DeleteHashPos(&CfgIt);
262 if (OneRNCFG->misc != NULL) {
263 StrBufAppendBuf(OutBuffer, OneRNCFG->misc, 0);
266 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
267 if ((rc >=0 ) && (rc == StrLength(Cfg)))
270 rename(tempfilename, filename);
275 "unable to write %s; [%s]; not enough space on the disk?\n",
279 unlink(tempfilename);
282 FreeStrBuf(&OutBuffer);
289 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
293 OneRoomNetCfg *OneRNCFG;
295 OneRNCFG = (OneRoomNetCfg*)vOneRoomNetCfg;
296 CfgIt = GetNewHashPos(CfgTypeHash, 1);
297 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
299 const CfgLineType *pCfg;
300 RoomNetCfgLine *pNext, *pName;
302 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
303 pName= OneRNCFG->NetConfigs[pCfg->C];
304 while (pName != NULL)
307 pCfg->DeAllocator(pCfg, &pName);
311 DeleteHashPos(&CfgIt);
313 FreeStrBuf(&OneRNCFG->Sender);
314 FreeStrBuf(&OneRNCFG->RoomInfo);
315 FreeStrBuf(&OneRNCFG->misc);
318 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCFG)
320 vFreeRoomNetworkStruct(*pOneRNCFG);
324 const OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
327 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
328 return (OneRoomNetCfg*)pv;
332 void LoadAllNetConfigs(void)
336 struct dirent *filedir_entry;
340 OneRoomNetCfg *OneRNCFG;
345 RoomConfigs = NewHash(1, NULL);
346 filedir = opendir (ctdl_netcfg_dir);
347 if (filedir == NULL) {
348 return ; /// todo: panic!
351 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
357 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
358 (filedir_entry != NULL))
360 #ifdef _DIRENT_HAVE_D_NAMLEN
361 d_namelen = filedir_entry->d_namelen;
363 d_namelen = strlen(filedir_entry->d_name);
366 #ifdef _DIRENT_HAVE_D_TYPE
367 d_type = filedir_entry->d_type;
376 #define IFTODT(mode) (((mode) & 0170000) >> 12)
377 #define DTTOIF(dirtype) ((dirtype) << 12)
381 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
382 continue; /* Ignore backup files... */
384 if ((d_namelen == 1) &&
385 (filedir_entry->d_name[0] == '.'))
388 if ((d_namelen == 2) &&
389 (filedir_entry->d_name[0] == '.') &&
390 (filedir_entry->d_name[1] == '.'))
393 snprintf(path, PATH_MAX, "%s/%s",
394 ctdl_netcfg_dir, filedir_entry->d_name);
396 if (d_type == DT_UNKNOWN) {
398 if (lstat(path, &s) == 0) {
399 d_type = IFTODT(s.st_mode);
407 case DT_LNK: /* TODO: check whether its a file or a directory */
410 pch = filedir_entry->d_name;
421 RoomNumber = atol(filedir_entry->d_name);
422 ReadRoomNetConfigFile(&OneRNCFG, path);
424 if (OneRNCFG != NULL)
425 Put(RoomConfigs, LKEY(RoomNumber), OneRNCFG, vFreeRoomNetworkStruct);
427 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
441 /*-----------------------------------------------------------------------------*
442 * Per room network configs : exchange with client *
443 *-----------------------------------------------------------------------------*/
444 void cmd_gnet(char *argbuf)
446 char filename[PATH_MAX];
451 if (!IsEmptyStr(argbuf))
453 if (CtdlAccessCheck(ac_aide)) return;
454 if (strcmp(argbuf, FILE_MAILALIAS))
456 cprintf("%d No such file or directory\n",
457 ERROR + INTERNAL_ERROR);
460 safestrncpy(filename, file_mail_aliases, sizeof(filename));
461 cprintf("%d Settings for <%s>\n",
467 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
468 /* users can edit the netconfigs for their own mailbox rooms */
470 else if (CtdlAccessCheck(ac_room_aide)) return;
472 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
473 cprintf("%d Network settings for room #%ld <%s>\n",
475 CC->room.QRnumber, CC->room.QRname);
478 fp = fopen(filename, "r");
480 while (fgets(buf, sizeof buf, fp) != NULL) {
481 buf[strlen(buf)-1] = 0;
482 cprintf("%s\n", buf);
490 #define nForceAliases 5
491 const ConstStr ForceAliases[nForceAliases] = {
495 {HKEY("postmaster,")},
499 void cmd_snet(char *argbuf) {
500 char tempfilename[PATH_MAX];
501 char filename[PATH_MAX];
508 int MailAliasesFound[nForceAliases];
512 if (!IsEmptyStr(argbuf))
514 if (CtdlAccessCheck(ac_aide)) return;
515 if (strcmp(argbuf, FILE_MAILALIAS))
517 cprintf("%d No such file or directory\n",
518 ERROR + INTERNAL_ERROR);
521 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
522 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
523 memcpy(tempfilename, filename, len + 1);
528 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
529 /* users can edit the netconfigs for their own mailbox rooms */
531 else if (CtdlAccessCheck(ac_room_aide)) return;
533 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
534 memcpy(tempfilename, filename, len + 1);
536 memset(&StatBuf, 0, sizeof(struct stat));
537 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
538 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
540 sprintf(tempfilename + len, ".%d", CC->cs_pid);
542 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
544 if ((TmpFD > 0) && (errno == 0))
546 char *tmp = malloc(StatBuf.st_size * 2);
547 memset(tmp, ' ', StatBuf.st_size * 2);
548 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
550 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
553 cprintf("%d Unable to allocate the space required for %s: %s\n",
554 ERROR + INTERNAL_ERROR,
557 unlink(tempfilename);
560 lseek(TmpFD, SEEK_SET, 0);
563 cprintf("%d Unable to allocate the space required for %s: %s\n",
564 ERROR + INTERNAL_ERROR,
567 unlink(tempfilename);
572 cprintf("%d %s\n", SEND_LISTING, tempfilename);
575 while (rc = CtdlClientGetLine(Line),
578 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
584 for (i = 0; i < nForceAliases; i++)
586 if ((!MailAliasesFound[i]) &&
587 (strncmp(ForceAliases[i].Key,
589 ForceAliases[i].len) == 0)
592 MailAliasesFound[i] = 1;
598 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
599 write(TmpFD, ChrPtr(Line), StrLength(Line));
600 len += StrLength(Line);
603 ftruncate(TmpFD, len);
610 * Sanity check whether all aliases required by the RFCs were set
614 for (i = 0; i < nForceAliases; i++)
616 if (!MailAliasesFound[i])
621 cprintf("%d won't do this - you're missing an RFC required alias.\n",
622 ERROR + INTERNAL_ERROR);
623 unlink(tempfilename);
628 /* Now copy the temp file to its permanent location.
629 * (We copy instead of link because they may be on different filesystems)
631 begin_critical_section(S_NETCONFIGS);
632 rename(tempfilename, filename);
633 end_critical_section(S_NETCONFIGS);
637 /*-----------------------------------------------------------------------------*
638 * Per node network configs *
639 *-----------------------------------------------------------------------------*/
640 void DeleteCtdlNodeConf(void *vNode)
642 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
643 FreeStrBuf(&Node->NodeName);
644 FreeStrBuf(&Node->Secret);
645 FreeStrBuf(&Node->Host);
646 FreeStrBuf(&Node->Port);
650 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
652 const char *Pos = NULL;
655 /* we need at least 4 pipes and some other text so its invalid. */
656 if (StrLength(SerializedNode) < 8)
658 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
662 Node->NodeName=NewStrBuf();
663 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
665 Node->Secret=NewStrBuf();
666 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
668 Node->Host=NewStrBuf();
669 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
671 Node->Port=NewStrBuf();
672 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
678 * Load or refresh the Citadel network (IGnet) configuration for this node.
680 HashList* CtdlLoadIgNetCfg(void)
689 Cfg = CtdlGetSysConfig(IGNETCFG);
690 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
696 Hash = NewHash(1, NULL);
697 Buf = NewStrBufPlain(Cfg, -1);
699 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
703 StrBufSipLine(LineBuf, Buf, &LinePos);
704 if (StrLength(LineBuf) != 0) {
705 Node = NewNode(LineBuf);
707 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
710 } while (LinePos != StrBufNOTNULL);
712 FreeStrBuf(&LineBuf);
717 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
719 const RoomNetCfg RecipientCfgs[] = {
726 RoomNetCfgLine *nptr;
731 while (RecipientCfgs[i] != maxRoomNetCfg)
733 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
737 if ((StrLength(nptr->Value[0]) == len) &&
738 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
750 int CtdlNetconfigCheckRoomaccess(
753 const char* RemoteIdentifier)
755 OneRoomNetCfg *RNCfg;
759 if (RemoteIdentifier == NULL)
761 snprintf(errmsgbuf, n, "Need sender to permit access.");
762 return (ERROR + USERNAME_REQUIRED);
765 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
766 begin_critical_section(S_NETCONFIGS);
767 if (!ReadRoomNetConfigFile(&RNCfg, filename))
769 end_critical_section(S_NETCONFIGS);
770 snprintf(errmsgbuf, n,
771 "This mailing list only accepts posts from subscribers.");
772 return (ERROR + NO_SUCH_USER);
774 end_critical_section(S_NETCONFIGS);
775 found = is_recipient (RNCfg, RemoteIdentifier);
776 vFreeRoomNetworkStruct(&RNCfg);
781 snprintf(errmsgbuf, n,
782 "This mailing list only accepts posts from subscribers.");
783 return (ERROR + NO_SUCH_USER);
790 * cmd_netp() - authenticate to the server as another Citadel node polling
791 * for network traffic
793 void cmd_netp(char *cmdbuf)
795 struct CitContext *CCC = CC;
796 HashList *working_ignetcfg;
804 const StrBuf *secret = NULL;
805 const StrBuf *nexthop = NULL;
806 char err_buf[SIZ] = "";
809 node = CCC->curr_user;
810 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
811 NodeStr = NewStrBufPlain(node, nodelen);
812 /* load the IGnet Configuration to check node validity */
813 working_ignetcfg = CtdlLoadIgNetCfg();
814 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
816 snprintf(err_buf, sizeof err_buf,
817 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
818 node, CCC->cs_host, CCC->cs_addr
820 syslog(LOG_WARNING, "%s", err_buf);
821 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
823 strs[0] = CCC->cs_addr;
824 lens[0] = strlen(CCC->cs_addr);
826 strs[1] = "SRV_UNKNOWN";
827 lens[1] = sizeof("SRV_UNKNOWN" - 1);
832 2, strs, (long*) &lens);
834 DeleteHash(&working_ignetcfg);
835 FreeStrBuf(&NodeStr);
839 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
840 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
841 snprintf(err_buf, sizeof err_buf,
842 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
843 CCC->cs_host, CCC->cs_addr, node
845 syslog(LOG_WARNING, "%s", err_buf);
846 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
848 strs[0] = CCC->cs_addr;
849 lens[0] = strlen(CCC->cs_addr);
852 lens[1] = sizeof("SRV_PW" - 1);
857 2, strs, (long*) &lens);
859 DeleteHash(&working_ignetcfg);
860 FreeStrBuf(&NodeStr);
864 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
865 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
866 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
867 DeleteHash(&working_ignetcfg);
868 FreeStrBuf(&NodeStr);
871 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
872 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
873 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
874 CCC->net_node, CCC->cs_host, CCC->cs_addr
876 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
877 DeleteHash(&working_ignetcfg);
878 FreeStrBuf(&NodeStr);
882 /*-----------------------------------------------------------------------------*
883 * Network maps: evaluate other nodes *
884 *-----------------------------------------------------------------------------*/
886 void DeleteNetMap(void *vNetMap)
888 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
889 FreeStrBuf(&TheNetMap->NodeName);
890 FreeStrBuf(&TheNetMap->NextHop);
894 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
896 const char *Pos = NULL;
899 /* we need at least 3 pipes and some other text so its invalid. */
900 if (StrLength(SerializedNetMap) < 6)
902 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
904 NM->NodeName=NewStrBuf();
905 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
907 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
909 NM->NextHop=NewStrBuf();
910 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
915 HashList* CtdlReadNetworkMap(void)
922 CtdlNetMap *TheNetMap;
924 Cfg = CtdlGetSysConfig(IGNETMAP);
925 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
931 Hash = NewHash(1, NULL);
932 Buf = NewStrBufPlain(Cfg, -1);
934 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
936 while (StrBufSipLine(Buf, LineBuf, &LinePos))
938 TheNetMap = NewNetMap(LineBuf);
939 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
940 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
944 FreeStrBuf(&LineBuf);
948 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
953 StrBuf *Ret = NewStrBuf();
954 HashPos *Pos = GetNewHashPos(Map, 0);
956 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
958 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
959 StrBufAppendBuf(Ret, pMap->NodeName, 0);
960 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
962 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
963 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
965 StrBufAppendBuf(Ret, pMap->NextHop, 0);
966 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
974 * Learn topology from path fields
976 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
978 CtdlNetMap *pNM = NULL;
983 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
984 (vptr != NULL))/* TODO: is the NodeName Uniq? */
986 pNM = (CtdlNetMap*)vptr;
987 extract_token(nexthop, path, 0, '!', sizeof nexthop);
988 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
989 pNM->lastcontact = time(NULL);
990 (*netmap_changed) ++;
995 /* If we got here then it's not in the map, so add it. */
996 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
997 nmptr->NodeName = NewStrBufPlain(node, -1);
998 nmptr->lastcontact = time(NULL);
999 nmptr->NextHop = NewStrBuf ();
1000 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1001 /* TODO: is the NodeName Uniq? */
1002 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1003 (*netmap_changed) ++;
1008 * Check the network map and determine whether the supplied node name is
1009 * valid. If it is not a neighbor node, supply the name of a neighbor node
1010 * which is the next hop. If it *is* a neighbor node, we also fill in the
1013 int CtdlIsValidNode(const StrBuf **nexthop,
1014 const StrBuf **secret,
1017 HashList *the_netmap)
1021 CtdlNodeConf *TheNode;
1022 CtdlNetMap *TheNetMap;
1024 if (StrLength(node) == 0) {
1029 * First try the neighbor nodes
1031 if (GetCount(IgnetCfg) == 0) {
1032 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1033 if (nexthop != NULL) {
1039 /* try to find a neigbour with the name 'node' */
1040 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1041 (vNodeConf != NULL))
1043 TheNode = (CtdlNodeConf*)vNodeConf;
1045 *secret = TheNode->Secret;
1046 return 0; /* yup, it's a direct neighbor */
1050 * If we get to this point we have to see if we know the next hop
1051 *//* TODO: is the NodeName Uniq? */
1052 if ((GetCount(the_netmap) > 0) &&
1053 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1055 TheNetMap = (CtdlNetMap*)vNetMap;
1056 if (nexthop != NULL)
1057 *nexthop = TheNetMap->NextHop;
1062 * If we get to this point, the supplied node name is bogus.
1064 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1072 * Module entry point
1074 CTDL_MODULE_INIT(netconfig)
1078 LoadAllNetConfigs ();
1079 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1080 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1081 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");