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 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCFG, char *filename)
137 const char *ErrStr = NULL;
139 const CfgLineType *pCfg;
142 OneRoomNetCfg *OneRNCFG;
144 fd = open(filename, O_NONBLOCK|O_RDONLY);
149 OneRNCFG = malloc(sizeof(OneRoomNetCfg));
150 memset(OneRNCFG, 0, sizeof(OneRoomNetCfg));
151 *pOneRNCFG = OneRNCFG;
154 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
155 if (StrLength(Line) == 0)
158 InStr = NewStrBufPlain(NULL, StrLength(Line));
159 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
161 pCfg = GetCfgTypeByStr(SKEY(InStr));
164 pCfg->Parser(pCfg, Line, Pos, OneRNCFG);
168 if (OneRNCFG->misc == NULL)
170 OneRNCFG->misc = NewStrBufDup(Line);
174 if(StrLength(OneRNCFG->misc) > 0)
175 StrBufAppendBufPlain(OneRNCFG->misc, HKEY("\n"), 0);
176 StrBufAppendBuf(OneRNCFG->misc, Line, 0);
187 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCFG, char *filename)
191 char tempfilename[PATH_MAX];
196 long reltid; /* if we don't have SYS_gettid, use "random" value */
201 len = strlen(filename);
202 memcpy(tempfilename, filename, len + 1);
204 #if defined(HAVE_SYONERNCFGALL_H) && defined (SYS_gettid)
205 reltid = syOneRNCFGall(SYS_gettid);
207 gettimeofday(&tv, NULL);
208 /* Promote to time_t; types differ on some OSes (like darwin) */
209 unixtime = tv.tv_sec;
211 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
213 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
215 if ((TmpFD < 0) || (errno != 0)) {
216 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
217 filename, strerror(errno));
218 unlink(tempfilename);
222 CfgIt = GetNewHashPos(CfgTypeHash, 1);
223 fchown(TmpFD, config.c_ctdluid, 0);
224 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
226 const CfgLineType *pCfg;
227 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
228 if (pCfg->IsSingleLine)
230 pCfg->Serializer(pCfg, OutBuffer, OneRNCFG, NULL);
234 RoomNetCfgLine *pName = OneRNCFG->NetConfigs[pCfg->C];
235 while (pName != NULL)
237 pCfg->Serializer(pCfg, OutBuffer, OneRNCFG, pName);
245 DeleteHashPos(&CfgIt);
248 if (OneRNCFG->misc != NULL) {
249 StrBufAppendBuf(OutBuffer, OneRNCFG->misc, 0);
252 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
253 if ((rc >=0 ) && (rc == StrLength(Cfg)))
256 rename(tempfilename, filename);
261 "unable to write %s; [%s]; not enough space on the disk?\n",
265 unlink(tempfilename);
268 FreeStrBuf(&OutBuffer);
275 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
279 OneRoomNetCfg *OneRNCFG;
281 OneRNCFG = (OneRoomNetCfg*)vOneRoomNetCfg;
282 CfgIt = GetNewHashPos(CfgTypeHash, 1);
283 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
285 const CfgLineType *pCfg;
286 RoomNetCfgLine *pNext, *pName;
288 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
289 pName= OneRNCFG->NetConfigs[pCfg->C];
290 while (pName != NULL)
293 pCfg->DeAllocator(pCfg, &pName);
297 DeleteHashPos(&CfgIt);
299 FreeStrBuf(&OneRNCFG->Sender);
300 FreeStrBuf(&OneRNCFG->RoomInfo);
301 FreeStrBuf(&OneRNCFG->misc);
304 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCFG)
306 vFreeRoomNetworkStruct(*pOneRNCFG);
310 const OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
313 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
314 return (OneRoomNetCfg*)pv;
318 void LoadAllNetConfigs(void)
322 struct dirent *filedir_entry;
326 OneRoomNetCfg *OneRNCFG;
331 RoomConfigs = NewHash(1, NULL);
332 filedir = opendir (ctdl_netcfg_dir);
333 if (filedir == NULL) {
334 return ; /// todo: panic!
337 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
343 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
344 (filedir_entry != NULL))
346 #ifdef _DIRENT_HAVE_D_NAMLEN
347 d_namelen = filedir_entry->d_namelen;
349 d_namelen = strlen(filedir_entry->d_name);
352 #ifdef _DIRENT_HAVE_D_TYPE
353 d_type = filedir_entry->d_type;
362 #define IFTODT(mode) (((mode) & 0170000) >> 12)
363 #define DTTOIF(dirtype) ((dirtype) << 12)
367 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
368 continue; /* Ignore backup files... */
370 if ((d_namelen == 1) &&
371 (filedir_entry->d_name[0] == '.'))
374 if ((d_namelen == 2) &&
375 (filedir_entry->d_name[0] == '.') &&
376 (filedir_entry->d_name[1] == '.'))
379 snprintf(path, PATH_MAX, "%s/%s",
380 ctdl_netcfg_dir, filedir_entry->d_name);
382 if (d_type == DT_UNKNOWN) {
384 if (lstat(path, &s) == 0) {
385 d_type = IFTODT(s.st_mode);
393 case DT_LNK: /* TODO: check whether its a file or a directory */
396 pch = filedir_entry->d_name;
407 RoomNumber = atol(filedir_entry->d_name);
408 ReadRoomNetConfigFile(&OneRNCFG, path);
410 if (OneRNCFG != NULL)
411 Put(RoomConfigs, LKEY(RoomNumber), OneRNCFG, vFreeRoomNetworkStruct);
413 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
427 /*-----------------------------------------------------------------------------*
428 * Per room network configs : exchange with client *
429 *-----------------------------------------------------------------------------*/
430 void cmd_gnet(char *argbuf)
432 char filename[PATH_MAX];
437 if (!IsEmptyStr(argbuf))
439 if (CtdlAccessCheck(ac_aide)) return;
440 if (strcmp(argbuf, FILE_MAILALIAS))
442 cprintf("%d No such file or directory\n",
443 ERROR + INTERNAL_ERROR);
446 safestrncpy(filename, file_mail_aliases, sizeof(filename));
447 cprintf("%d Settings for <%s>\n",
453 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
454 /* users can edit the netconfigs for their own mailbox rooms */
456 else if (CtdlAccessCheck(ac_room_aide)) return;
458 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
459 cprintf("%d Network settings for room #%ld <%s>\n",
461 CC->room.QRnumber, CC->room.QRname);
464 fp = fopen(filename, "r");
466 while (fgets(buf, sizeof buf, fp) != NULL) {
467 buf[strlen(buf)-1] = 0;
468 cprintf("%s\n", buf);
476 #define nForceAliases 5
477 const ConstStr ForceAliases[nForceAliases] = {
481 {HKEY("postmaster,")},
485 void cmd_snet(char *argbuf) {
486 char tempfilename[PATH_MAX];
487 char filename[PATH_MAX];
494 int MailAliasesFound[nForceAliases];
498 if (!IsEmptyStr(argbuf))
500 if (CtdlAccessCheck(ac_aide)) return;
501 if (strcmp(argbuf, FILE_MAILALIAS))
503 cprintf("%d No such file or directory\n",
504 ERROR + INTERNAL_ERROR);
507 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
508 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
509 memcpy(tempfilename, filename, len + 1);
514 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
515 /* users can edit the netconfigs for their own mailbox rooms */
517 else if (CtdlAccessCheck(ac_room_aide)) return;
519 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
520 memcpy(tempfilename, filename, len + 1);
522 memset(&StatBuf, 0, sizeof(struct stat));
523 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
524 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
526 sprintf(tempfilename + len, ".%d", CC->cs_pid);
528 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
530 if ((TmpFD > 0) && (errno == 0))
532 char *tmp = malloc(StatBuf.st_size * 2);
533 memset(tmp, ' ', StatBuf.st_size * 2);
534 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
536 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
539 cprintf("%d Unable to allocate the space required for %s: %s\n",
540 ERROR + INTERNAL_ERROR,
543 unlink(tempfilename);
546 lseek(TmpFD, SEEK_SET, 0);
549 cprintf("%d Unable to allocate the space required for %s: %s\n",
550 ERROR + INTERNAL_ERROR,
553 unlink(tempfilename);
558 cprintf("%d %s\n", SEND_LISTING, tempfilename);
561 while (rc = CtdlClientGetLine(Line),
564 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
570 for (i = 0; i < nForceAliases; i++)
572 if ((!MailAliasesFound[i]) &&
573 (strncmp(ForceAliases[i].Key,
575 ForceAliases[i].len) == 0)
578 MailAliasesFound[i] = 1;
584 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
585 write(TmpFD, ChrPtr(Line), StrLength(Line));
586 len += StrLength(Line);
589 ftruncate(TmpFD, len);
596 * Sanity check whether all aliases required by the RFCs were set
600 for (i = 0; i < nForceAliases; i++)
602 if (!MailAliasesFound[i])
607 cprintf("%d won't do this - you're missing an RFC required alias.\n",
608 ERROR + INTERNAL_ERROR);
609 unlink(tempfilename);
614 /* Now copy the temp file to its permanent location.
615 * (We copy instead of link because they may be on different filesystems)
617 begin_critical_section(S_NETCONFIGS);
618 rename(tempfilename, filename);
619 end_critical_section(S_NETCONFIGS);
623 /*-----------------------------------------------------------------------------*
624 * Per node network configs *
625 *-----------------------------------------------------------------------------*/
626 void DeleteCtdlNodeConf(void *vNode)
628 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
629 FreeStrBuf(&Node->NodeName);
630 FreeStrBuf(&Node->Secret);
631 FreeStrBuf(&Node->Host);
632 FreeStrBuf(&Node->Port);
636 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
638 const char *Pos = NULL;
641 /* we need at least 4 pipes and some other text so its invalid. */
642 if (StrLength(SerializedNode) < 8)
644 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
648 Node->NodeName=NewStrBuf();
649 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
651 Node->Secret=NewStrBuf();
652 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
654 Node->Host=NewStrBuf();
655 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
657 Node->Port=NewStrBuf();
658 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
664 * Load or refresh the Citadel network (IGnet) configuration for this node.
666 HashList* CtdlLoadIgNetCfg(void)
675 Cfg = CtdlGetSysConfig(IGNETCFG);
676 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
682 Hash = NewHash(1, NULL);
683 Buf = NewStrBufPlain(Cfg, -1);
685 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
689 StrBufSipLine(LineBuf, Buf, &LinePos);
690 if (StrLength(LineBuf) != 0) {
691 Node = NewNode(LineBuf);
693 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
696 } while (LinePos != StrBufNOTNULL);
698 FreeStrBuf(&LineBuf);
703 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
705 const RoomNetCfg RecipientCfgs[] = {
712 RoomNetCfgLine *nptr;
717 while (RecipientCfgs[i] != maxRoomNetCfg)
719 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
723 if ((StrLength(nptr->Value[0]) == len) &&
724 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
736 int CtdlNetconfigCheckRoomaccess(
739 const char* RemoteIdentifier)
741 OneRoomNetCfg *RNCfg;
745 if (RemoteIdentifier == NULL)
747 snprintf(errmsgbuf, n, "Need sender to permit access.");
748 return (ERROR + USERNAME_REQUIRED);
751 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
752 begin_critical_section(S_NETCONFIGS);
753 if (!ReadRoomNetConfigFile(&RNCfg, filename))
755 end_critical_section(S_NETCONFIGS);
756 snprintf(errmsgbuf, n,
757 "This mailing list only accepts posts from subscribers.");
758 return (ERROR + NO_SUCH_USER);
760 end_critical_section(S_NETCONFIGS);
761 found = is_recipient (RNCfg, RemoteIdentifier);
762 vFreeRoomNetworkStruct(&RNCfg);
767 snprintf(errmsgbuf, n,
768 "This mailing list only accepts posts from subscribers.");
769 return (ERROR + NO_SUCH_USER);
776 * cmd_netp() - authenticate to the server as another Citadel node polling
777 * for network traffic
779 void cmd_netp(char *cmdbuf)
781 struct CitContext *CCC = CC;
782 HashList *working_ignetcfg;
790 const StrBuf *secret = NULL;
791 const StrBuf *nexthop = NULL;
792 char err_buf[SIZ] = "";
795 node = CCC->curr_user;
796 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
797 NodeStr = NewStrBufPlain(node, nodelen);
798 /* load the IGnet Configuration to check node validity */
799 working_ignetcfg = CtdlLoadIgNetCfg();
800 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
802 snprintf(err_buf, sizeof err_buf,
803 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
804 node, CCC->cs_host, CCC->cs_addr
806 syslog(LOG_WARNING, "%s", err_buf);
807 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
809 strs[0] = CCC->cs_addr;
810 lens[0] = strlen(CCC->cs_addr);
812 strs[1] = "SRV_UNKNOWN";
813 lens[1] = sizeof("SRV_UNKNOWN" - 1);
818 2, strs, (long*) &lens);
820 DeleteHash(&working_ignetcfg);
821 FreeStrBuf(&NodeStr);
825 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
826 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
827 snprintf(err_buf, sizeof err_buf,
828 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
829 CCC->cs_host, CCC->cs_addr, node
831 syslog(LOG_WARNING, "%s", err_buf);
832 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
834 strs[0] = CCC->cs_addr;
835 lens[0] = strlen(CCC->cs_addr);
838 lens[1] = sizeof("SRV_PW" - 1);
843 2, strs, (long*) &lens);
845 DeleteHash(&working_ignetcfg);
846 FreeStrBuf(&NodeStr);
850 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
851 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
852 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
853 DeleteHash(&working_ignetcfg);
854 FreeStrBuf(&NodeStr);
857 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
858 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
859 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
860 CCC->net_node, CCC->cs_host, CCC->cs_addr
862 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
863 DeleteHash(&working_ignetcfg);
864 FreeStrBuf(&NodeStr);
868 /*-----------------------------------------------------------------------------*
869 * Network maps: evaluate other nodes *
870 *-----------------------------------------------------------------------------*/
872 void DeleteNetMap(void *vNetMap)
874 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
875 FreeStrBuf(&TheNetMap->NodeName);
876 FreeStrBuf(&TheNetMap->NextHop);
880 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
882 const char *Pos = NULL;
885 /* we need at least 3 pipes and some other text so its invalid. */
886 if (StrLength(SerializedNetMap) < 6)
888 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
890 NM->NodeName=NewStrBuf();
891 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
893 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
895 NM->NextHop=NewStrBuf();
896 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
901 HashList* CtdlReadNetworkMap(void)
908 CtdlNetMap *TheNetMap;
910 Cfg = CtdlGetSysConfig(IGNETMAP);
911 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
917 Hash = NewHash(1, NULL);
918 Buf = NewStrBufPlain(Cfg, -1);
920 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
922 while (StrBufSipLine(Buf, LineBuf, &LinePos))
924 TheNetMap = NewNetMap(LineBuf);
925 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
926 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
930 FreeStrBuf(&LineBuf);
934 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
939 StrBuf *Ret = NewStrBuf();
940 HashPos *Pos = GetNewHashPos(Map, 0);
942 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
944 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
945 StrBufAppendBuf(Ret, pMap->NodeName, 0);
946 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
948 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
949 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
951 StrBufAppendBuf(Ret, pMap->NextHop, 0);
952 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
960 * Learn topology from path fields
962 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
964 CtdlNetMap *pNM = NULL;
969 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
970 (vptr != NULL))/* TODO: is the NodeName Uniq? */
972 pNM = (CtdlNetMap*)vptr;
973 extract_token(nexthop, path, 0, '!', sizeof nexthop);
974 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
975 pNM->lastcontact = time(NULL);
976 (*netmap_changed) ++;
981 /* If we got here then it's not in the map, so add it. */
982 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
983 nmptr->NodeName = NewStrBufPlain(node, -1);
984 nmptr->lastcontact = time(NULL);
985 nmptr->NextHop = NewStrBuf ();
986 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
987 /* TODO: is the NodeName Uniq? */
988 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
989 (*netmap_changed) ++;
994 * Check the network map and determine whether the supplied node name is
995 * valid. If it is not a neighbor node, supply the name of a neighbor node
996 * which is the next hop. If it *is* a neighbor node, we also fill in the
999 int CtdlIsValidNode(const StrBuf **nexthop,
1000 const StrBuf **secret,
1003 HashList *the_netmap)
1007 CtdlNodeConf *TheNode;
1008 CtdlNetMap *TheNetMap;
1010 if (StrLength(node) == 0) {
1015 * First try the neighbor nodes
1017 if (GetCount(IgnetCfg) == 0) {
1018 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1019 if (nexthop != NULL) {
1025 /* try to find a neigbour with the name 'node' */
1026 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1027 (vNodeConf != NULL))
1029 TheNode = (CtdlNodeConf*)vNodeConf;
1031 *secret = TheNode->Secret;
1032 return 0; /* yup, it's a direct neighbor */
1036 * If we get to this point we have to see if we know the next hop
1037 *//* TODO: is the NodeName Uniq? */
1038 if ((GetCount(the_netmap) > 0) &&
1039 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1041 TheNetMap = (CtdlNetMap*)vNetMap;
1042 if (nexthop != NULL)
1043 *nexthop = TheNetMap->NextHop;
1048 * If we get to this point, the supplied node name is bogus.
1050 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1058 * Module entry point
1060 CTDL_MODULE_INIT(netconfig)
1064 LoadAllNetConfigs ();
1065 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1066 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1067 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");