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)
135 for (i = 0; i < ThisOne->nSegments; i++)
137 FreeStrBuf(&(*data)->Value[i]);
139 free ((*data)->Value);
143 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
145 RoomNetCfgLine *NewData;
147 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
149 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
151 for (i = 0; i < data->nValues; i++)
153 NewData->Value[i] = NewStrBufDup(data->Value[i]);
157 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
160 const char *ErrStr = NULL;
162 const CfgLineType *pCfg;
165 OneRoomNetCfg *OneRNCfg;
167 fd = open(filename, O_NONBLOCK|O_RDONLY);
172 if (*pOneRNCfg != NULL)
173 OneRNCfg = *pOneRNCfg;
175 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
176 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
177 *pOneRNCfg = OneRNCfg;
180 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
181 if (StrLength(Line) == 0)
184 InStr = NewStrBufPlain(NULL, StrLength(Line));
185 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
187 pCfg = GetCfgTypeByStr(SKEY(InStr));
190 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
194 if (OneRNCfg->misc == NULL)
196 OneRNCfg->misc = NewStrBufDup(Line);
200 if(StrLength(OneRNCfg->misc) > 0)
201 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
202 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
213 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
217 StrBuf *OutBuffer = NULL;
218 char tempfilename[PATH_MAX];
223 long reltid; /* if we don't have SYS_gettid, use "random" value */
227 len = strlen(filename);
228 memcpy(tempfilename, filename, len + 1);
230 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
231 reltid = syscall(SYS_gettid);
233 gettimeofday(&tv, NULL);
234 /* Promote to time_t; types differ on some OSes (like darwin) */
235 unixtime = tv.tv_sec;
237 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
239 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
241 if ((TmpFD < 0) || (errno != 0)) {
242 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
243 filename, strerror(errno));
244 unlink(tempfilename);
249 OutBuffer = NewStrBuf();
250 CfgIt = GetNewHashPos(CfgTypeHash, 1);
251 fchown(TmpFD, config.c_ctdluid, 0);
252 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
254 const CfgLineType *pCfg;
255 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
256 if (pCfg->IsSingleLine)
258 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
262 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
263 while (pName != NULL)
265 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
273 DeleteHashPos(&CfgIt);
276 if (OneRNCfg->misc != NULL) {
277 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
280 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
281 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
284 rename(tempfilename, filename);
289 "unable to write %s; [%s]; not enough space on the disk?\n",
293 unlink(tempfilename);
296 FreeStrBuf(&OutBuffer);
303 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
305 char filename[PATH_MAX];
307 if (OneRNCfg->changed)
309 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
310 SaveRoomNetConfigFile(OneRNCfg, filename);
311 OneRNCfg->changed = 0;
314 void SaveChangedConfigs(void)
316 CtdlForEachNetCfgRoom(SaveModifiedRooms,
322 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
325 RoomNetCfgLine **pLine;
326 char filename[PATH_MAX];
328 if (OneRNCfg == NULL)
331 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
332 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
334 pLine = &OneRNCfg->NetConfigs[LineType];
336 while(*pLine != NULL) pLine = &((*pLine)->next);
339 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
340 SaveRoomNetConfigFile(OneRNCfg, filename);
341 OneRNCfg->changed = 0;
344 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
348 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
353 CfgIt = GetNewHashPos(CfgTypeHash, 1);
354 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
356 const CfgLineType *pCfg;
357 RoomNetCfgLine *pNext, *pName;
359 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
360 pName= OneRNCfg->NetConfigs[pCfg->C];
361 while (pName != NULL)
364 pCfg->DeAllocator(pCfg, &pName);
368 DeleteHashPos(&CfgIt);
370 FreeStrBuf(&OneRNCfg->Sender);
371 FreeStrBuf(&OneRNCfg->RoomInfo);
372 FreeStrBuf(&OneRNCfg->misc);
374 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
376 OneRoomNetCfg *OneRNCfg;
377 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
378 FreeRoomNetworkStructContent(OneRNCfg);
381 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
383 vFreeRoomNetworkStruct(*pOneRNCfg);
387 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
390 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
391 return (OneRoomNetCfg*)pv;
395 void LoadAllNetConfigs(void)
399 struct dirent *filedir_entry;
403 OneRoomNetCfg *OneRNCfg;
408 RoomConfigs = NewHash(1, NULL);
409 filedir = opendir (ctdl_netcfg_dir);
410 if (filedir == NULL) {
411 return ; /// todo: panic!
414 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
420 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
421 (filedir_entry != NULL))
423 #ifdef _DIRENT_HAVE_D_NAMLEN
424 d_namelen = filedir_entry->d_namelen;
426 d_namelen = strlen(filedir_entry->d_name);
429 #ifdef _DIRENT_HAVE_D_TYPE
430 d_type = filedir_entry->d_type;
439 #define IFTODT(mode) (((mode) & 0170000) >> 12)
440 #define DTTOIF(dirtype) ((dirtype) << 12)
444 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
445 continue; /* Ignore backup files... */
447 if ((d_namelen == 1) &&
448 (filedir_entry->d_name[0] == '.'))
451 if ((d_namelen == 2) &&
452 (filedir_entry->d_name[0] == '.') &&
453 (filedir_entry->d_name[1] == '.'))
456 snprintf(path, PATH_MAX, "%s/%s",
457 ctdl_netcfg_dir, filedir_entry->d_name);
459 if (d_type == DT_UNKNOWN) {
461 if (lstat(path, &s) == 0) {
462 d_type = IFTODT(s.st_mode);
470 case DT_LNK: /* TODO: check whether its a file or a directory */
473 pch = filedir_entry->d_name;
485 RoomNumber = atol(filedir_entry->d_name);
486 ReadRoomNetConfigFile(&OneRNCfg, path);
488 if (OneRNCfg != NULL)
489 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
490 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
504 /*-----------------------------------------------------------------------------*
505 * Per room network configs : exchange with client *
506 *-----------------------------------------------------------------------------*/
507 void cmd_gnet(char *argbuf)
509 char filename[PATH_MAX];
514 if (!IsEmptyStr(argbuf))
516 if (CtdlAccessCheck(ac_aide)) return;
517 if (strcmp(argbuf, FILE_MAILALIAS))
519 cprintf("%d No such file or directory\n",
520 ERROR + INTERNAL_ERROR);
523 safestrncpy(filename, file_mail_aliases, sizeof(filename));
524 cprintf("%d Settings for <%s>\n",
530 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
531 /* users can edit the netconfigs for their own mailbox rooms */
533 else if (CtdlAccessCheck(ac_room_aide)) return;
535 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
536 cprintf("%d Network settings for room #%ld <%s>\n",
538 CC->room.QRnumber, CC->room.QRname);
541 fp = fopen(filename, "r");
543 while (fgets(buf, sizeof buf, fp) != NULL) {
544 buf[strlen(buf)-1] = 0;
545 cprintf("%s\n", buf);
553 #define nForceAliases 5
554 const ConstStr ForceAliases[nForceAliases] = {
558 {HKEY("postmaster,")},
562 void cmd_snet(char *argbuf)
564 struct CitContext *CCC = CC;
565 char tempfilename[PATH_MAX];
566 char filename[PATH_MAX];
573 int MailAliasesFound[nForceAliases];
577 if (!IsEmptyStr(argbuf))
579 if (CtdlAccessCheck(ac_aide)) return;
580 if (strcmp(argbuf, FILE_MAILALIAS))
582 cprintf("%d No such file or directory\n",
583 ERROR + INTERNAL_ERROR);
586 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
587 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
588 memcpy(tempfilename, filename, len + 1);
593 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
594 /* users can edit the netconfigs for their own mailbox rooms */
596 else if (CtdlAccessCheck(ac_room_aide)) return;
598 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
599 memcpy(tempfilename, filename, len + 1);
601 memset(&StatBuf, 0, sizeof(struct stat));
602 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
603 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
605 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
607 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
609 if ((TmpFD > 0) && (errno == 0))
611 char *tmp = malloc(StatBuf.st_size * 2);
612 memset(tmp, ' ', StatBuf.st_size * 2);
613 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
615 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
618 cprintf("%d Unable to allocate the space required for %s: %s\n",
619 ERROR + INTERNAL_ERROR,
622 unlink(tempfilename);
625 lseek(TmpFD, SEEK_SET, 0);
628 cprintf("%d Unable to allocate the space required for %s: %s\n",
629 ERROR + INTERNAL_ERROR,
632 unlink(tempfilename);
637 cprintf("%d %s\n", SEND_LISTING, tempfilename);
640 while (rc = CtdlClientGetLine(Line),
643 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
649 for (i = 0; i < nForceAliases; i++)
651 if ((!MailAliasesFound[i]) &&
652 (strncmp(ForceAliases[i].Key,
654 ForceAliases[i].len) == 0)
657 MailAliasesFound[i] = 1;
663 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
664 write(TmpFD, ChrPtr(Line), StrLength(Line));
665 len += StrLength(Line);
668 ftruncate(TmpFD, len);
675 * Sanity check whether all aliases required by the RFCs were set
679 for (i = 0; i < nForceAliases; i++)
681 if (!MailAliasesFound[i])
686 cprintf("%d won't do this - you're missing an RFC required alias.\n",
687 ERROR + INTERNAL_ERROR);
688 unlink(tempfilename);
693 /* Now copy the temp file to its permanent location.
694 * (We copy instead of link because they may be on different filesystems)
696 begin_critical_section(S_NETCONFIGS);
697 rename(tempfilename, filename);
700 OneRoomNetCfg *RNCfg;
701 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
704 FreeRoomNetworkStructContent(RNCfg);
705 ReadRoomNetConfigFile(&RNCfg, filename);
709 ReadRoomNetConfigFile(&RNCfg, filename);
710 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
713 end_critical_section(S_NETCONFIGS);
717 /*-----------------------------------------------------------------------------*
718 * Per node network configs *
719 *-----------------------------------------------------------------------------*/
720 void DeleteCtdlNodeConf(void *vNode)
722 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
723 FreeStrBuf(&Node->NodeName);
724 FreeStrBuf(&Node->Secret);
725 FreeStrBuf(&Node->Host);
726 FreeStrBuf(&Node->Port);
730 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
732 const char *Pos = NULL;
735 /* we need at least 4 pipes and some other text so its invalid. */
736 if (StrLength(SerializedNode) < 8)
738 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
742 Node->NodeName=NewStrBuf();
743 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
745 Node->Secret=NewStrBuf();
746 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
748 Node->Host=NewStrBuf();
749 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
751 Node->Port=NewStrBuf();
752 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
758 * Load or refresh the Citadel network (IGnet) configuration for this node.
760 HashList* CtdlLoadIgNetCfg(void)
769 Cfg = CtdlGetSysConfig(IGNETCFG);
770 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
776 Hash = NewHash(1, NULL);
777 Buf = NewStrBufPlain(Cfg, -1);
779 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
783 StrBufSipLine(LineBuf, Buf, &LinePos);
784 if (StrLength(LineBuf) != 0) {
785 Node = NewNode(LineBuf);
787 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
790 } while (LinePos != StrBufNOTNULL);
792 FreeStrBuf(&LineBuf);
797 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
799 const RoomNetCfg RecipientCfgs[] = {
806 RoomNetCfgLine *nptr;
811 while (RecipientCfgs[i] != maxRoomNetCfg)
813 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
817 if ((StrLength(nptr->Value[0]) == len) &&
818 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
830 int CtdlNetconfigCheckRoomaccess(
833 const char* RemoteIdentifier)
835 OneRoomNetCfg *RNCfg;
839 if (RemoteIdentifier == NULL)
841 snprintf(errmsgbuf, n, "Need sender to permit access.");
842 return (ERROR + USERNAME_REQUIRED);
845 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
846 begin_critical_section(S_NETCONFIGS);
847 if (!ReadRoomNetConfigFile(&RNCfg, filename))
849 end_critical_section(S_NETCONFIGS);
850 snprintf(errmsgbuf, n,
851 "This mailing list only accepts posts from subscribers.");
852 return (ERROR + NO_SUCH_USER);
854 end_critical_section(S_NETCONFIGS);
855 found = is_recipient (RNCfg, RemoteIdentifier);
856 vFreeRoomNetworkStruct(&RNCfg);
861 snprintf(errmsgbuf, n,
862 "This mailing list only accepts posts from subscribers.");
863 return (ERROR + NO_SUCH_USER);
870 * cmd_netp() - authenticate to the server as another Citadel node polling
871 * for network traffic
873 void cmd_netp(char *cmdbuf)
875 struct CitContext *CCC = CC;
876 HashList *working_ignetcfg;
884 const StrBuf *secret = NULL;
885 const StrBuf *nexthop = NULL;
886 char err_buf[SIZ] = "";
889 node = CCC->curr_user;
890 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
891 NodeStr = NewStrBufPlain(node, nodelen);
892 /* load the IGnet Configuration to check node validity */
893 working_ignetcfg = CtdlLoadIgNetCfg();
894 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
896 snprintf(err_buf, sizeof err_buf,
897 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
898 node, CCC->cs_host, CCC->cs_addr
900 syslog(LOG_WARNING, "%s", err_buf);
901 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
903 strs[0] = CCC->cs_addr;
904 lens[0] = strlen(CCC->cs_addr);
906 strs[1] = "SRV_UNKNOWN";
907 lens[1] = sizeof("SRV_UNKNOWN" - 1);
912 2, strs, (long*) &lens);
914 DeleteHash(&working_ignetcfg);
915 FreeStrBuf(&NodeStr);
919 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
920 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
921 snprintf(err_buf, sizeof err_buf,
922 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
923 CCC->cs_host, CCC->cs_addr, node
925 syslog(LOG_WARNING, "%s", err_buf);
926 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
928 strs[0] = CCC->cs_addr;
929 lens[0] = strlen(CCC->cs_addr);
932 lens[1] = sizeof("SRV_PW" - 1);
937 2, strs, (long*) &lens);
939 DeleteHash(&working_ignetcfg);
940 FreeStrBuf(&NodeStr);
944 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
945 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
946 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
947 DeleteHash(&working_ignetcfg);
948 FreeStrBuf(&NodeStr);
951 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
952 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
953 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
954 CCC->net_node, CCC->cs_host, CCC->cs_addr
956 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
957 DeleteHash(&working_ignetcfg);
958 FreeStrBuf(&NodeStr);
962 /*-----------------------------------------------------------------------------*
963 * Network maps: evaluate other nodes *
964 *-----------------------------------------------------------------------------*/
966 void DeleteNetMap(void *vNetMap)
968 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
969 FreeStrBuf(&TheNetMap->NodeName);
970 FreeStrBuf(&TheNetMap->NextHop);
974 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
976 const char *Pos = NULL;
979 /* we need at least 3 pipes and some other text so its invalid. */
980 if (StrLength(SerializedNetMap) < 6)
982 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
984 NM->NodeName=NewStrBuf();
985 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
987 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
989 NM->NextHop=NewStrBuf();
990 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
995 HashList* CtdlReadNetworkMap(void)
1002 CtdlNetMap *TheNetMap;
1004 Hash = NewHash(1, NULL);
1005 Cfg = CtdlGetSysConfig(IGNETMAP);
1006 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1012 Buf = NewStrBufPlain(Cfg, -1);
1014 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1016 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1018 TheNetMap = NewNetMap(LineBuf);
1019 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1020 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1024 FreeStrBuf(&LineBuf);
1028 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1033 StrBuf *Ret = NewStrBuf();
1034 HashPos *Pos = GetNewHashPos(Map, 0);
1036 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1038 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1039 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1040 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1042 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1043 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1045 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1046 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1048 DeleteHashPos(&Pos);
1054 * Learn topology from path fields
1056 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1058 CtdlNetMap *pNM = NULL;
1063 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1064 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1066 pNM = (CtdlNetMap*)vptr;
1067 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1068 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1069 pNM->lastcontact = time(NULL);
1070 (*netmap_changed) ++;
1075 /* If we got here then it's not in the map, so add it. */
1076 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1077 nmptr->NodeName = NewStrBufPlain(node, -1);
1078 nmptr->lastcontact = time(NULL);
1079 nmptr->NextHop = NewStrBuf ();
1080 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1081 /* TODO: is the NodeName Uniq? */
1082 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1083 (*netmap_changed) ++;
1088 * Check the network map and determine whether the supplied node name is
1089 * valid. If it is not a neighbor node, supply the name of a neighbor node
1090 * which is the next hop. If it *is* a neighbor node, we also fill in the
1093 int CtdlIsValidNode(const StrBuf **nexthop,
1094 const StrBuf **secret,
1097 HashList *the_netmap)
1101 CtdlNodeConf *TheNode;
1102 CtdlNetMap *TheNetMap;
1104 if (StrLength(node) == 0) {
1109 * First try the neighbor nodes
1111 if (GetCount(IgnetCfg) == 0) {
1112 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1113 if (nexthop != NULL) {
1119 /* try to find a neigbour with the name 'node' */
1120 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1121 (vNodeConf != NULL))
1123 TheNode = (CtdlNodeConf*)vNodeConf;
1125 *secret = TheNode->Secret;
1126 return 0; /* yup, it's a direct neighbor */
1130 * If we get to this point we have to see if we know the next hop
1131 *//* TODO: is the NodeName Uniq? */
1132 if ((GetCount(the_netmap) > 0) &&
1133 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1135 TheNetMap = (CtdlNetMap*)vNetMap;
1136 if (nexthop != NULL)
1137 *nexthop = TheNetMap->NextHop;
1142 * If we get to this point, the supplied node name is bogus.
1144 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1149 void destroy_network_cfgs(void)
1151 HashList *pCfgTypeHash = CfgTypeHash;
1152 HashList *pRoomConfigs = RoomConfigs;
1157 DeleteHash(&pCfgTypeHash);
1158 DeleteHash(&pRoomConfigs);
1162 * Module entry point
1164 CTDL_MODULE_INIT(netconfig)
1168 CtdlRegisterCleanupHook(destroy_network_cfgs);
1169 LoadAllNetConfigs ();
1170 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1171 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1172 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");