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);
311 void LoadAllNetConfigs(void)
315 struct dirent *filedir_entry;
319 OneRoomNetCfg *OneRNCFG;
324 RoomConfigs = NewHash(1, NULL);
325 filedir = opendir (ctdl_netcfg_dir);
326 if (filedir == NULL) {
327 return ; /// todo: panic!
330 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
336 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
337 (filedir_entry != NULL))
339 #ifdef _DIRENT_HAVE_D_NAMLEN
340 d_namelen = filedir_entry->d_namelen;
342 d_namelen = strlen(filedir_entry->d_name);
345 #ifdef _DIRENT_HAVE_D_TYPE
346 d_type = filedir_entry->d_type;
355 #define IFTODT(mode) (((mode) & 0170000) >> 12)
356 #define DTTOIF(dirtype) ((dirtype) << 12)
360 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
361 continue; /* Ignore backup files... */
363 if ((d_namelen == 1) &&
364 (filedir_entry->d_name[0] == '.'))
367 if ((d_namelen == 2) &&
368 (filedir_entry->d_name[0] == '.') &&
369 (filedir_entry->d_name[1] == '.'))
372 snprintf(path, PATH_MAX, "%s/%s",
373 ctdl_netcfg_dir, filedir_entry->d_name);
375 if (d_type == DT_UNKNOWN) {
377 if (lstat(path, &s) == 0) {
378 d_type = IFTODT(s.st_mode);
386 case DT_LNK: /* TODO: check whether its a file or a directory */
389 pch = filedir_entry->d_name;
400 RoomNumber = atol(filedir_entry->d_name);
401 ReadRoomNetConfigFile(&OneRNCFG, path);
403 if (OneRNCFG != NULL)
404 Put(RoomConfigs, LKEY(RoomNumber), OneRNCFG, vFreeRoomNetworkStruct);
406 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
420 /*-----------------------------------------------------------------------------*
421 * Per room network configs : exchange with client *
422 *-----------------------------------------------------------------------------*/
423 void cmd_gnet(char *argbuf)
425 char filename[PATH_MAX];
430 if (!IsEmptyStr(argbuf))
432 if (CtdlAccessCheck(ac_aide)) return;
433 if (strcmp(argbuf, FILE_MAILALIAS))
435 cprintf("%d No such file or directory\n",
436 ERROR + INTERNAL_ERROR);
439 safestrncpy(filename, file_mail_aliases, sizeof(filename));
440 cprintf("%d Settings for <%s>\n",
446 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
447 /* users can edit the netconfigs for their own mailbox rooms */
449 else if (CtdlAccessCheck(ac_room_aide)) return;
451 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
452 cprintf("%d Network settings for room #%ld <%s>\n",
454 CC->room.QRnumber, CC->room.QRname);
457 fp = fopen(filename, "r");
459 while (fgets(buf, sizeof buf, fp) != NULL) {
460 buf[strlen(buf)-1] = 0;
461 cprintf("%s\n", buf);
469 #define nForceAliases 5
470 const ConstStr ForceAliases[nForceAliases] = {
474 {HKEY("postmaster,")},
478 void cmd_snet(char *argbuf) {
479 char tempfilename[PATH_MAX];
480 char filename[PATH_MAX];
487 int MailAliasesFound[nForceAliases];
491 if (!IsEmptyStr(argbuf))
493 if (CtdlAccessCheck(ac_aide)) return;
494 if (strcmp(argbuf, FILE_MAILALIAS))
496 cprintf("%d No such file or directory\n",
497 ERROR + INTERNAL_ERROR);
500 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
501 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
502 memcpy(tempfilename, filename, len + 1);
507 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
508 /* users can edit the netconfigs for their own mailbox rooms */
510 else if (CtdlAccessCheck(ac_room_aide)) return;
512 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
513 memcpy(tempfilename, filename, len + 1);
515 memset(&StatBuf, 0, sizeof(struct stat));
516 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
517 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
519 sprintf(tempfilename + len, ".%d", CC->cs_pid);
521 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
523 if ((TmpFD > 0) && (errno == 0))
525 char *tmp = malloc(StatBuf.st_size * 2);
526 memset(tmp, ' ', StatBuf.st_size * 2);
527 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
529 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
532 cprintf("%d Unable to allocate the space required for %s: %s\n",
533 ERROR + INTERNAL_ERROR,
536 unlink(tempfilename);
539 lseek(TmpFD, SEEK_SET, 0);
542 cprintf("%d Unable to allocate the space required for %s: %s\n",
543 ERROR + INTERNAL_ERROR,
546 unlink(tempfilename);
551 cprintf("%d %s\n", SEND_LISTING, tempfilename);
554 while (rc = CtdlClientGetLine(Line),
557 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
563 for (i = 0; i < nForceAliases; i++)
565 if ((!MailAliasesFound[i]) &&
566 (strncmp(ForceAliases[i].Key,
568 ForceAliases[i].len) == 0)
571 MailAliasesFound[i] = 1;
577 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
578 write(TmpFD, ChrPtr(Line), StrLength(Line));
579 len += StrLength(Line);
582 ftruncate(TmpFD, len);
589 * Sanity check whether all aliases required by the RFCs were set
593 for (i = 0; i < nForceAliases; i++)
595 if (!MailAliasesFound[i])
600 cprintf("%d won't do this - you're missing an RFC required alias.\n",
601 ERROR + INTERNAL_ERROR);
602 unlink(tempfilename);
607 /* Now copy the temp file to its permanent location.
608 * (We copy instead of link because they may be on different filesystems)
610 begin_critical_section(S_NETCONFIGS);
611 rename(tempfilename, filename);
612 end_critical_section(S_NETCONFIGS);
616 /*-----------------------------------------------------------------------------*
617 * Per node network configs *
618 *-----------------------------------------------------------------------------*/
619 void DeleteCtdlNodeConf(void *vNode)
621 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
622 FreeStrBuf(&Node->NodeName);
623 FreeStrBuf(&Node->Secret);
624 FreeStrBuf(&Node->Host);
625 FreeStrBuf(&Node->Port);
629 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
631 const char *Pos = NULL;
634 /* we need at least 4 pipes and some other text so its invalid. */
635 if (StrLength(SerializedNode) < 8)
637 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
641 Node->NodeName=NewStrBuf();
642 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
644 Node->Secret=NewStrBuf();
645 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
647 Node->Host=NewStrBuf();
648 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
650 Node->Port=NewStrBuf();
651 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
657 * Load or refresh the Citadel network (IGnet) configuration for this node.
659 HashList* CtdlLoadIgNetCfg(void)
668 Cfg = CtdlGetSysConfig(IGNETCFG);
669 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
675 Hash = NewHash(1, NULL);
676 Buf = NewStrBufPlain(Cfg, -1);
678 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
682 StrBufSipLine(LineBuf, Buf, &LinePos);
683 if (StrLength(LineBuf) != 0) {
684 Node = NewNode(LineBuf);
686 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
689 } while (LinePos != StrBufNOTNULL);
691 FreeStrBuf(&LineBuf);
696 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
698 const RoomNetCfg RecipientCfgs[] = {
705 RoomNetCfgLine *nptr;
710 while (RecipientCfgs[i] != maxRoomNetCfg)
712 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
716 if ((StrLength(nptr->Value[0]) == len) &&
717 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
729 int CtdlNetconfigCheckRoomaccess(
732 const char* RemoteIdentifier)
734 OneRoomNetCfg *RNCfg;
738 if (RemoteIdentifier == NULL)
740 snprintf(errmsgbuf, n, "Need sender to permit access.");
741 return (ERROR + USERNAME_REQUIRED);
744 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
745 begin_critical_section(S_NETCONFIGS);
746 if (!ReadRoomNetConfigFile(&RNCfg, filename))
748 end_critical_section(S_NETCONFIGS);
749 snprintf(errmsgbuf, n,
750 "This mailing list only accepts posts from subscribers.");
751 return (ERROR + NO_SUCH_USER);
753 end_critical_section(S_NETCONFIGS);
754 found = is_recipient (RNCfg, RemoteIdentifier);
755 vFreeRoomNetworkStruct(&RNCfg);
760 snprintf(errmsgbuf, n,
761 "This mailing list only accepts posts from subscribers.");
762 return (ERROR + NO_SUCH_USER);
769 * cmd_netp() - authenticate to the server as another Citadel node polling
770 * for network traffic
772 void cmd_netp(char *cmdbuf)
774 struct CitContext *CCC = CC;
775 HashList *working_ignetcfg;
783 const StrBuf *secret = NULL;
784 const StrBuf *nexthop = NULL;
785 char err_buf[SIZ] = "";
788 node = CCC->curr_user;
789 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
790 NodeStr = NewStrBufPlain(node, nodelen);
791 /* load the IGnet Configuration to check node validity */
792 working_ignetcfg = CtdlLoadIgNetCfg();
793 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
795 snprintf(err_buf, sizeof err_buf,
796 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
797 node, CCC->cs_host, CCC->cs_addr
799 syslog(LOG_WARNING, "%s", err_buf);
800 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
802 strs[0] = CCC->cs_addr;
803 lens[0] = strlen(CCC->cs_addr);
805 strs[1] = "SRV_UNKNOWN";
806 lens[1] = sizeof("SRV_UNKNOWN" - 1);
811 2, strs, (long*) &lens);
813 DeleteHash(&working_ignetcfg);
814 FreeStrBuf(&NodeStr);
818 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
819 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
820 snprintf(err_buf, sizeof err_buf,
821 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
822 CCC->cs_host, CCC->cs_addr, node
824 syslog(LOG_WARNING, "%s", err_buf);
825 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
827 strs[0] = CCC->cs_addr;
828 lens[0] = strlen(CCC->cs_addr);
831 lens[1] = sizeof("SRV_PW" - 1);
836 2, strs, (long*) &lens);
838 DeleteHash(&working_ignetcfg);
839 FreeStrBuf(&NodeStr);
843 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
844 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
845 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
846 DeleteHash(&working_ignetcfg);
847 FreeStrBuf(&NodeStr);
850 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
851 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
852 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
853 CCC->net_node, CCC->cs_host, CCC->cs_addr
855 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
856 DeleteHash(&working_ignetcfg);
857 FreeStrBuf(&NodeStr);
861 /*-----------------------------------------------------------------------------*
862 * Network maps: evaluate other nodes *
863 *-----------------------------------------------------------------------------*/
865 void DeleteNetMap(void *vNetMap)
867 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
868 FreeStrBuf(&TheNetMap->NodeName);
869 FreeStrBuf(&TheNetMap->NextHop);
873 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
875 const char *Pos = NULL;
878 /* we need at least 3 pipes and some other text so its invalid. */
879 if (StrLength(SerializedNetMap) < 6)
881 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
883 NM->NodeName=NewStrBuf();
884 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
886 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
888 NM->NextHop=NewStrBuf();
889 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
894 HashList* CtdlReadNetworkMap(void)
901 CtdlNetMap *TheNetMap;
903 Cfg = CtdlGetSysConfig(IGNETMAP);
904 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
910 Hash = NewHash(1, NULL);
911 Buf = NewStrBufPlain(Cfg, -1);
913 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
915 while (StrBufSipLine(Buf, LineBuf, &LinePos))
917 TheNetMap = NewNetMap(LineBuf);
918 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
919 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
923 FreeStrBuf(&LineBuf);
927 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
932 StrBuf *Ret = NewStrBuf();
933 HashPos *Pos = GetNewHashPos(Map, 0);
935 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
937 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
938 StrBufAppendBuf(Ret, pMap->NodeName, 0);
939 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
941 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
942 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
944 StrBufAppendBuf(Ret, pMap->NextHop, 0);
945 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
953 * Learn topology from path fields
955 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
957 CtdlNetMap *pNM = NULL;
962 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
963 (vptr != NULL))/* TODO: is the NodeName Uniq? */
965 pNM = (CtdlNetMap*)vptr;
966 extract_token(nexthop, path, 0, '!', sizeof nexthop);
967 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
968 pNM->lastcontact = time(NULL);
969 (*netmap_changed) ++;
974 /* If we got here then it's not in the map, so add it. */
975 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
976 nmptr->NodeName = NewStrBufPlain(node, -1);
977 nmptr->lastcontact = time(NULL);
978 nmptr->NextHop = NewStrBuf ();
979 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
980 /* TODO: is the NodeName Uniq? */
981 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
982 (*netmap_changed) ++;
987 * Check the network map and determine whether the supplied node name is
988 * valid. If it is not a neighbor node, supply the name of a neighbor node
989 * which is the next hop. If it *is* a neighbor node, we also fill in the
992 int CtdlIsValidNode(const StrBuf **nexthop,
993 const StrBuf **secret,
996 HashList *the_netmap)
1000 CtdlNodeConf *TheNode;
1001 CtdlNetMap *TheNetMap;
1003 if (StrLength(node) == 0) {
1008 * First try the neighbor nodes
1010 if (GetCount(IgnetCfg) == 0) {
1011 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1012 if (nexthop != NULL) {
1018 /* try to find a neigbour with the name 'node' */
1019 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1020 (vNodeConf != NULL))
1022 TheNode = (CtdlNodeConf*)vNodeConf;
1024 *secret = TheNode->Secret;
1025 return 0; /* yup, it's a direct neighbor */
1029 * If we get to this point we have to see if we know the next hop
1030 *//* TODO: is the NodeName Uniq? */
1031 if ((GetCount(the_netmap) > 0) &&
1032 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1034 TheNetMap = (CtdlNetMap*)vNetMap;
1035 if (nexthop != NULL)
1036 *nexthop = TheNetMap->NextHop;
1041 * If we get to this point, the supplied node name is bogus.
1043 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1051 * Module entry point
1053 CTDL_MODULE_INIT(netconfig)
1057 LoadAllNetConfigs ();
1058 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1059 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1060 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");