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 = NULL;
170 fd = open(filename, O_NONBLOCK|O_RDONLY);
175 if (*pOneRNCfg != NULL)
177 OneRNCfg = *pOneRNCfg;
178 FreeRoomNetworkStructContent (OneRNCfg);
181 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
182 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
183 *pOneRNCfg = OneRNCfg;
187 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
188 if (StrLength(Line) == 0)
191 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
193 pCfg = GetCfgTypeByStr(SKEY(InStr));
196 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
200 if (OneRNCfg->misc == NULL)
202 OneRNCfg->misc = NewStrBufDup(Line);
206 if(StrLength(OneRNCfg->misc) > 0)
207 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
208 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
219 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
223 StrBuf *OutBuffer = NULL;
224 char tempfilename[PATH_MAX];
229 long reltid; /* if we don't have SYS_gettid, use "random" value */
233 len = strlen(filename);
234 memcpy(tempfilename, filename, len + 1);
236 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
237 reltid = syscall(SYS_gettid);
239 gettimeofday(&tv, NULL);
240 /* Promote to time_t; types differ on some OSes (like darwin) */
241 unixtime = tv.tv_sec;
243 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
245 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
247 if ((TmpFD < 0) || (errno != 0)) {
248 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
249 filename, strerror(errno));
250 unlink(tempfilename);
255 OutBuffer = NewStrBuf();
256 CfgIt = GetNewHashPos(CfgTypeHash, 1);
257 fchown(TmpFD, config.c_ctdluid, 0);
258 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
260 const CfgLineType *pCfg;
261 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
262 if (pCfg->IsSingleLine)
264 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
268 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
269 while (pName != NULL)
271 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
279 DeleteHashPos(&CfgIt);
282 if (OneRNCfg->misc != NULL) {
283 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
286 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
287 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
290 rename(tempfilename, filename);
295 "unable to write %s; [%s]; not enough space on the disk?\n",
299 unlink(tempfilename);
302 FreeStrBuf(&OutBuffer);
309 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
311 char filename[PATH_MAX];
313 if (OneRNCfg->changed)
315 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
316 SaveRoomNetConfigFile(OneRNCfg, filename);
317 OneRNCfg->changed = 0;
320 void SaveChangedConfigs(void)
322 CtdlForEachNetCfgRoom(SaveModifiedRooms,
328 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
331 RoomNetCfgLine **pLine;
332 char filename[PATH_MAX];
334 if (OneRNCfg == NULL)
337 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
338 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
340 pLine = &OneRNCfg->NetConfigs[LineType];
342 while(*pLine != NULL) pLine = &((*pLine)->next);
345 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
346 SaveRoomNetConfigFile(OneRNCfg, filename);
347 OneRNCfg->changed = 0;
350 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
354 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
359 CfgIt = GetNewHashPos(CfgTypeHash, 1);
360 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
362 const CfgLineType *pCfg;
363 RoomNetCfgLine *pNext, *pName;
365 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
366 pName= OneRNCfg->NetConfigs[eCfg];
367 while (pName != NULL)
372 pCfg->DeAllocator(pCfg, &pName);
376 DeleteGenericCfgLine(NULL, &pName);
381 DeleteHashPos(&CfgIt);
383 FreeStrBuf(&OneRNCfg->Sender);
384 FreeStrBuf(&OneRNCfg->RoomInfo);
385 FreeStrBuf(&OneRNCfg->misc);
387 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
389 OneRoomNetCfg *OneRNCfg;
390 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
391 FreeRoomNetworkStructContent(OneRNCfg);
394 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
396 vFreeRoomNetworkStruct(*pOneRNCfg);
400 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
403 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
404 return (OneRoomNetCfg*)pv;
408 void LoadAllNetConfigs(void)
412 struct dirent *filedir_entry;
416 OneRoomNetCfg *OneRNCfg;
421 RoomConfigs = NewHash(1, NULL);
422 filedir = opendir (ctdl_netcfg_dir);
423 if (filedir == NULL) {
424 return ; /// todo: panic!
427 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
433 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
434 (filedir_entry != NULL))
436 #ifdef _DIRENT_HAVE_D_NAMLEN
437 d_namelen = filedir_entry->d_namelen;
439 d_namelen = strlen(filedir_entry->d_name);
442 #ifdef _DIRENT_HAVE_D_TYPE
443 d_type = filedir_entry->d_type;
452 #define IFTODT(mode) (((mode) & 0170000) >> 12)
453 #define DTTOIF(dirtype) ((dirtype) << 12)
457 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
458 continue; /* Ignore backup files... */
460 if ((d_namelen == 1) &&
461 (filedir_entry->d_name[0] == '.'))
464 if ((d_namelen == 2) &&
465 (filedir_entry->d_name[0] == '.') &&
466 (filedir_entry->d_name[1] == '.'))
469 snprintf(path, PATH_MAX, "%s/%s",
470 ctdl_netcfg_dir, filedir_entry->d_name);
472 if (d_type == DT_UNKNOWN) {
474 if (lstat(path, &s) == 0) {
475 d_type = IFTODT(s.st_mode);
483 case DT_LNK: /* TODO: check whether its a file or a directory */
486 pch = filedir_entry->d_name;
498 RoomNumber = atol(filedir_entry->d_name);
499 ReadRoomNetConfigFile(&OneRNCfg, path);
501 if (OneRNCfg != NULL)
502 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
503 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
517 /*-----------------------------------------------------------------------------*
518 * Per room network configs : exchange with client *
519 *-----------------------------------------------------------------------------*/
520 void cmd_gnet(char *argbuf)
522 char filename[PATH_MAX];
527 if (!IsEmptyStr(argbuf))
529 if (CtdlAccessCheck(ac_aide)) return;
530 if (strcmp(argbuf, FILE_MAILALIAS))
532 cprintf("%d No such file or directory\n",
533 ERROR + INTERNAL_ERROR);
536 safestrncpy(filename, file_mail_aliases, sizeof(filename));
537 cprintf("%d Settings for <%s>\n",
543 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
544 /* users can edit the netconfigs for their own mailbox rooms */
546 else if (CtdlAccessCheck(ac_room_aide)) return;
548 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
549 cprintf("%d Network settings for room #%ld <%s>\n",
551 CC->room.QRnumber, CC->room.QRname);
554 fp = fopen(filename, "r");
556 while (fgets(buf, sizeof buf, fp) != NULL) {
557 buf[strlen(buf)-1] = 0;
558 cprintf("%s\n", buf);
566 #define nForceAliases 5
567 const ConstStr ForceAliases[nForceAliases] = {
571 {HKEY("postmaster,")},
575 void cmd_snet(char *argbuf)
577 struct CitContext *CCC = CC;
578 char tempfilename[PATH_MAX];
579 char filename[PATH_MAX];
586 int MailAliasesFound[nForceAliases];
590 if (!IsEmptyStr(argbuf))
592 if (CtdlAccessCheck(ac_aide)) return;
593 if (strcmp(argbuf, FILE_MAILALIAS))
595 cprintf("%d No such file or directory\n",
596 ERROR + INTERNAL_ERROR);
599 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
600 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
601 memcpy(tempfilename, filename, len + 1);
606 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
607 /* users can edit the netconfigs for their own mailbox rooms */
609 else if (CtdlAccessCheck(ac_room_aide)) return;
611 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
612 memcpy(tempfilename, filename, len + 1);
614 memset(&StatBuf, 0, sizeof(struct stat));
615 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
616 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
618 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
620 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
622 if ((TmpFD > 0) && (errno == 0))
624 char *tmp = malloc(StatBuf.st_size * 2);
625 memset(tmp, ' ', StatBuf.st_size * 2);
626 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
628 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
631 cprintf("%d Unable to allocate the space required for %s: %s\n",
632 ERROR + INTERNAL_ERROR,
635 unlink(tempfilename);
638 lseek(TmpFD, SEEK_SET, 0);
641 cprintf("%d Unable to allocate the space required for %s: %s\n",
642 ERROR + INTERNAL_ERROR,
645 unlink(tempfilename);
650 cprintf("%d %s\n", SEND_LISTING, tempfilename);
653 while (rc = CtdlClientGetLine(Line),
656 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
662 for (i = 0; i < nForceAliases; i++)
664 if ((!MailAliasesFound[i]) &&
665 (strncmp(ForceAliases[i].Key,
667 ForceAliases[i].len) == 0)
670 MailAliasesFound[i] = 1;
676 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
677 write(TmpFD, ChrPtr(Line), StrLength(Line));
678 len += StrLength(Line);
681 ftruncate(TmpFD, len);
688 * Sanity check whether all aliases required by the RFCs were set
692 for (i = 0; i < nForceAliases; i++)
694 if (!MailAliasesFound[i])
699 cprintf("%d won't do this - you're missing an RFC required alias.\n",
700 ERROR + INTERNAL_ERROR);
701 unlink(tempfilename);
706 /* Now copy the temp file to its permanent location.
707 * (We copy instead of link because they may be on different filesystems)
709 begin_critical_section(S_NETCONFIGS);
710 rename(tempfilename, filename);
713 OneRoomNetCfg *RNCfg;
714 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
717 FreeRoomNetworkStructContent(RNCfg);
718 ReadRoomNetConfigFile(&RNCfg, filename);
722 ReadRoomNetConfigFile(&RNCfg, filename);
723 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
726 end_critical_section(S_NETCONFIGS);
730 /*-----------------------------------------------------------------------------*
731 * Per node network configs *
732 *-----------------------------------------------------------------------------*/
733 void DeleteCtdlNodeConf(void *vNode)
735 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
736 FreeStrBuf(&Node->NodeName);
737 FreeStrBuf(&Node->Secret);
738 FreeStrBuf(&Node->Host);
739 FreeStrBuf(&Node->Port);
743 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
745 const char *Pos = NULL;
748 /* we need at least 4 pipes and some other text so its invalid. */
749 if (StrLength(SerializedNode) < 8)
751 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
755 Node->NodeName=NewStrBuf();
756 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
758 Node->Secret=NewStrBuf();
759 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
761 Node->Host=NewStrBuf();
762 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
764 Node->Port=NewStrBuf();
765 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
771 * Load or refresh the Citadel network (IGnet) configuration for this node.
773 HashList* CtdlLoadIgNetCfg(void)
782 Cfg = CtdlGetSysConfig(IGNETCFG);
783 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
789 Hash = NewHash(1, NULL);
790 Buf = NewStrBufPlain(Cfg, -1);
792 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
796 StrBufSipLine(LineBuf, Buf, &LinePos);
797 if (StrLength(LineBuf) != 0) {
798 Node = NewNode(LineBuf);
800 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
803 } while (LinePos != StrBufNOTNULL);
805 FreeStrBuf(&LineBuf);
810 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
812 const RoomNetCfg RecipientCfgs[] = {
819 RoomNetCfgLine *nptr;
824 while (RecipientCfgs[i] != maxRoomNetCfg)
826 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
830 if ((StrLength(nptr->Value[0]) == len) &&
831 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
843 int CtdlNetconfigCheckRoomaccess(
846 const char* RemoteIdentifier)
848 OneRoomNetCfg *RNCfg;
851 if (RemoteIdentifier == NULL)
853 snprintf(errmsgbuf, n, "Need sender to permit access.");
854 return (ERROR + USERNAME_REQUIRED);
857 begin_critical_section(S_NETCONFIGS);
858 RNCfg = CtdlGetNetCfgForRoom (CC->room.QRnumber);
861 end_critical_section(S_NETCONFIGS);
862 snprintf(errmsgbuf, n,
863 "This mailing list only accepts posts from subscribers.");
864 return (ERROR + NO_SUCH_USER);
866 found = is_recipient (RNCfg, RemoteIdentifier);
867 end_critical_section(S_NETCONFIGS);
873 snprintf(errmsgbuf, n,
874 "This mailing list only accepts posts from subscribers.");
875 return (ERROR + NO_SUCH_USER);
882 * cmd_netp() - authenticate to the server as another Citadel node polling
883 * for network traffic
885 void cmd_netp(char *cmdbuf)
887 struct CitContext *CCC = CC;
888 HashList *working_ignetcfg;
896 const StrBuf *secret = NULL;
897 const StrBuf *nexthop = NULL;
898 char err_buf[SIZ] = "";
901 node = CCC->curr_user;
902 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
903 NodeStr = NewStrBufPlain(node, nodelen);
904 /* load the IGnet Configuration to check node validity */
905 working_ignetcfg = CtdlLoadIgNetCfg();
906 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
908 snprintf(err_buf, sizeof err_buf,
909 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
910 node, CCC->cs_host, CCC->cs_addr
912 syslog(LOG_WARNING, "%s", err_buf);
913 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
915 strs[0] = CCC->cs_addr;
916 lens[0] = strlen(CCC->cs_addr);
918 strs[1] = "SRV_UNKNOWN";
919 lens[1] = sizeof("SRV_UNKNOWN" - 1);
924 2, strs, (long*) &lens);
926 DeleteHash(&working_ignetcfg);
927 FreeStrBuf(&NodeStr);
931 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
932 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
933 snprintf(err_buf, sizeof err_buf,
934 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
935 CCC->cs_host, CCC->cs_addr, node
937 syslog(LOG_WARNING, "%s", err_buf);
938 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
940 strs[0] = CCC->cs_addr;
941 lens[0] = strlen(CCC->cs_addr);
944 lens[1] = sizeof("SRV_PW" - 1);
949 2, strs, (long*) &lens);
951 DeleteHash(&working_ignetcfg);
952 FreeStrBuf(&NodeStr);
956 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
957 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
958 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
959 DeleteHash(&working_ignetcfg);
960 FreeStrBuf(&NodeStr);
963 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
964 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
965 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
966 CCC->net_node, CCC->cs_host, CCC->cs_addr
968 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
969 DeleteHash(&working_ignetcfg);
970 FreeStrBuf(&NodeStr);
974 /*-----------------------------------------------------------------------------*
975 * Network maps: evaluate other nodes *
976 *-----------------------------------------------------------------------------*/
978 void DeleteNetMap(void *vNetMap)
980 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
981 FreeStrBuf(&TheNetMap->NodeName);
982 FreeStrBuf(&TheNetMap->NextHop);
986 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
988 const char *Pos = NULL;
991 /* we need at least 3 pipes and some other text so its invalid. */
992 if (StrLength(SerializedNetMap) < 6)
994 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
996 NM->NodeName=NewStrBuf();
997 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
999 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
1001 NM->NextHop=NewStrBuf();
1002 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
1007 HashList* CtdlReadNetworkMap(void)
1009 const char *LinePos;
1014 CtdlNetMap *TheNetMap;
1016 Hash = NewHash(1, NULL);
1017 Cfg = CtdlGetSysConfig(IGNETMAP);
1018 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1024 Buf = NewStrBufPlain(Cfg, -1);
1026 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1028 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1030 TheNetMap = NewNetMap(LineBuf);
1031 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1032 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1036 FreeStrBuf(&LineBuf);
1040 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1045 StrBuf *Ret = NewStrBuf();
1046 HashPos *Pos = GetNewHashPos(Map, 0);
1048 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1050 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1051 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1052 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1054 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1055 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1057 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1058 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1060 DeleteHashPos(&Pos);
1066 * Learn topology from path fields
1068 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1070 CtdlNetMap *pNM = NULL;
1075 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1076 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1078 pNM = (CtdlNetMap*)vptr;
1079 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1080 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1081 pNM->lastcontact = time(NULL);
1082 (*netmap_changed) ++;
1087 /* If we got here then it's not in the map, so add it. */
1088 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1089 nmptr->NodeName = NewStrBufPlain(node, -1);
1090 nmptr->lastcontact = time(NULL);
1091 nmptr->NextHop = NewStrBuf ();
1092 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1093 /* TODO: is the NodeName Uniq? */
1094 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1095 (*netmap_changed) ++;
1100 * Check the network map and determine whether the supplied node name is
1101 * valid. If it is not a neighbor node, supply the name of a neighbor node
1102 * which is the next hop. If it *is* a neighbor node, we also fill in the
1105 int CtdlIsValidNode(const StrBuf **nexthop,
1106 const StrBuf **secret,
1109 HashList *the_netmap)
1113 CtdlNodeConf *TheNode;
1114 CtdlNetMap *TheNetMap;
1116 if (StrLength(node) == 0) {
1121 * First try the neighbor nodes
1123 if (GetCount(IgnetCfg) == 0) {
1124 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1125 if (nexthop != NULL) {
1131 /* try to find a neigbour with the name 'node' */
1132 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1133 (vNodeConf != NULL))
1135 TheNode = (CtdlNodeConf*)vNodeConf;
1137 *secret = TheNode->Secret;
1138 return 0; /* yup, it's a direct neighbor */
1142 * If we get to this point we have to see if we know the next hop
1143 *//* TODO: is the NodeName Uniq? */
1144 if ((GetCount(the_netmap) > 0) &&
1145 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1147 TheNetMap = (CtdlNetMap*)vNetMap;
1148 if (nexthop != NULL)
1149 *nexthop = TheNetMap->NextHop;
1154 * If we get to this point, the supplied node name is bogus.
1156 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1161 void destroy_network_cfgs(void)
1163 HashList *pCfgTypeHash = CfgTypeHash;
1164 HashList *pRoomConfigs = RoomConfigs;
1169 DeleteHash(&pCfgTypeHash);
1170 DeleteHash(&pRoomConfigs);
1174 * Module entry point
1176 CTDL_MODULE_INIT(netconfig)
1180 CtdlRegisterCleanupHook(destroy_network_cfgs);
1181 LoadAllNetConfigs ();
1182 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1183 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1184 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");