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 #include "serv_extensions.h"
35 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg);
36 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg);
38 HashList *CfgTypeHash = NULL;
39 HashList *RoomConfigs = NULL;
40 /*-----------------------------------------------------------------------------*
41 * Per room network configs *
42 *-----------------------------------------------------------------------------*/
43 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d)
47 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
50 pCfg->DeAllocator = d;
54 pCfg->IsSingleLine = uniq;
55 pCfg->nSegments = nSegments;
56 if (CfgTypeHash == NULL)
57 CfgTypeHash = NewHash(1, NULL);
58 Put(CfgTypeHash, Name, len, pCfg, NULL);
62 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
66 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
68 return (const CfgLineType *) pv;
76 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
83 RewindHashPos(CfgTypeHash, It, 1);
84 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
86 pCfg = (CfgLineType*) pv;
92 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCfg)
97 nptr = (RoomNetCfgLine *)
98 malloc(sizeof(RoomNetCfgLine));
99 nptr->next = OneRNCfg->NetConfigs[ThisOne->C];
100 nptr->Value = malloc(sizeof(StrBuf*) * ThisOne->nSegments);
102 memset(nptr->Value, 0, sizeof(StrBuf*) * ThisOne->nSegments);
103 if (ThisOne->nSegments == 1)
105 nptr->Value[0] = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
108 else for (i = 0; i < ThisOne->nSegments; i++)
111 nptr->Value[i] = NewStrBufPlain(NULL, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
112 StrBufExtract_NextToken(nptr->Value[i], Line, &LinePos, '|');
115 OneRNCfg->NetConfigs[ThisOne->C] = nptr;
118 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCfg, RoomNetCfgLine *data)
122 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
123 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
124 for (i = 0; i < ThisOne->nSegments; i++)
126 StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
127 if (i + 1 < ThisOne->nSegments)
128 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
130 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
133 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
140 for (i = 0; i < (*data)->nValues; i++)
142 FreeStrBuf(&(*data)->Value[i]);
144 free ((*data)->Value);
148 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
151 RoomNetCfgLine *NewData;
153 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
154 memset(NewData, 0, sizeof(RoomNetCfgLine));
155 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
156 memset(NewData->Value, 0, sizeof(StrBuf*) * data->nValues);
158 for (i = 0; i < data->nValues; i++)
160 NewData->Value[i] = NewStrBufDup(data->Value[i]);
162 NewData->nValues = data->nValues;
165 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
168 const char *ErrStr = NULL;
170 const CfgLineType *pCfg;
173 OneRoomNetCfg *OneRNCfg = NULL;
175 fd = open(filename, O_NONBLOCK|O_RDONLY);
180 fchown(fd, CTDLUID, (-1));
183 if (*pOneRNCfg != NULL)
185 OneRNCfg = *pOneRNCfg;
186 FreeRoomNetworkStructContent (OneRNCfg);
189 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
190 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
191 *pOneRNCfg = OneRNCfg;
195 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
196 if (StrLength(Line) == 0)
199 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
201 pCfg = GetCfgTypeByStr(SKEY(InStr));
204 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
208 if (OneRNCfg->misc == NULL)
210 OneRNCfg->misc = NewStrBufDup(Line);
214 if(StrLength(OneRNCfg->misc) > 0)
215 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
216 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
227 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
231 StrBuf *OutBuffer = NULL;
232 char tempfilename[PATH_MAX];
237 long reltid; /* if we don't have SYS_gettid, use "random" value */
241 len = strlen(filename);
242 memcpy(tempfilename, filename, len + 1);
244 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
245 reltid = syscall(SYS_gettid);
247 gettimeofday(&tv, NULL);
248 /* Promote to time_t; types differ on some OSes (like darwin) */
249 unixtime = tv.tv_sec;
251 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
253 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
255 if ((TmpFD < 0) || (errno != 0)) {
256 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
257 filename, strerror(errno));
258 unlink(tempfilename);
263 OutBuffer = NewStrBuf();
264 CfgIt = GetNewHashPos(CfgTypeHash, 1);
265 fchown(TmpFD, ctdluid, 0);
266 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
268 const CfgLineType *pCfg;
269 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
270 if (pCfg->IsSingleLine)
272 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
276 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
277 while (pName != NULL)
279 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
287 DeleteHashPos(&CfgIt);
290 if (OneRNCfg->misc != NULL) {
291 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
294 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
295 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
298 rename(tempfilename, filename);
303 "unable to write %s; [%s]; not enough space on the disk?\n",
307 unlink(tempfilename);
310 FreeStrBuf(&OutBuffer);
317 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
319 char filename[PATH_MAX];
321 if (OneRNCfg->changed)
323 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
324 SaveRoomNetConfigFile(OneRNCfg, filename);
325 OneRNCfg->changed = 0;
328 void SaveChangedConfigs(void)
330 CtdlForEachNetCfgRoom(SaveModifiedRooms,
336 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
339 RoomNetCfgLine **pLine;
340 char filename[PATH_MAX];
342 if (OneRNCfg == NULL)
345 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
346 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
348 pLine = &OneRNCfg->NetConfigs[LineType];
350 while(*pLine != NULL) pLine = &((*pLine)->next);
353 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
354 SaveRoomNetConfigFile(OneRNCfg, filename);
355 OneRNCfg->changed = 0;
358 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
362 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
367 CfgIt = GetNewHashPos(CfgTypeHash, 1);
368 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
370 const CfgLineType *pCfg;
371 RoomNetCfgLine *pNext, *pName;
373 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
374 pName= OneRNCfg->NetConfigs[eCfg];
375 while (pName != NULL)
380 pCfg->DeAllocator(pCfg, &pName);
384 DeleteGenericCfgLine(NULL, &pName);
389 DeleteHashPos(&CfgIt);
391 FreeStrBuf(&OneRNCfg->Sender);
392 FreeStrBuf(&OneRNCfg->RoomInfo);
393 FreeStrBuf(&OneRNCfg->misc);
394 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
396 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
398 OneRoomNetCfg *OneRNCfg;
399 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
400 FreeRoomNetworkStructContent(OneRNCfg);
403 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
405 vFreeRoomNetworkStruct(*pOneRNCfg);
409 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
412 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
413 return (OneRoomNetCfg*)pv;
417 void LoadAllNetConfigs(void)
421 struct dirent *filedir_entry;
425 OneRoomNetCfg *OneRNCfg;
430 RoomConfigs = NewHash(1, NULL);
431 filedir = opendir (ctdl_netcfg_dir);
432 if (filedir == NULL) {
433 return ; /// todo: panic!
436 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
442 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
443 (filedir_entry != NULL))
445 #ifdef _DIRENT_HAVE_D_NAMLEN
446 d_namelen = filedir_entry->d_namlen;
448 d_namelen = strlen(filedir_entry->d_name);
451 #ifdef _DIRENT_HAVE_D_TYPE
452 d_type = filedir_entry->d_type;
461 #define IFTODT(mode) (((mode) & 0170000) >> 12)
462 #define DTTOIF(dirtype) ((dirtype) << 12)
466 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
467 continue; /* Ignore backup files... */
469 if ((d_namelen == 1) &&
470 (filedir_entry->d_name[0] == '.'))
473 if ((d_namelen == 2) &&
474 (filedir_entry->d_name[0] == '.') &&
475 (filedir_entry->d_name[1] == '.'))
478 snprintf(path, PATH_MAX, "%s/%s",
479 ctdl_netcfg_dir, filedir_entry->d_name);
481 if (d_type == DT_UNKNOWN) {
483 if (lstat(path, &s) == 0) {
484 d_type = IFTODT(s.st_mode);
492 case DT_LNK: /* TODO: check whether its a file or a directory */
495 pch = filedir_entry->d_name;
507 RoomNumber = atol(filedir_entry->d_name);
508 ReadRoomNetConfigFile(&OneRNCfg, path);
510 if (OneRNCfg != NULL)
511 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
512 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
526 /*-----------------------------------------------------------------------------*
527 * Per room network configs : exchange with client *
528 *-----------------------------------------------------------------------------*/
529 void cmd_gnet(char *argbuf)
531 char filename[PATH_MAX];
536 if (!IsEmptyStr(argbuf))
538 if (CtdlAccessCheck(ac_aide)) return;
539 if (strcmp(argbuf, FILE_MAILALIAS))
541 cprintf("%d No such file or directory\n",
542 ERROR + INTERNAL_ERROR);
545 safestrncpy(filename, file_mail_aliases, sizeof(filename));
546 cprintf("%d Settings for <%s>\n",
552 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
553 /* users can edit the netconfigs for their own mailbox rooms */
555 else if (CtdlAccessCheck(ac_room_aide)) return;
557 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
558 cprintf("%d Network settings for room #%ld <%s>\n",
560 CC->room.QRnumber, CC->room.QRname);
563 fp = fopen(filename, "r");
565 while (fgets(buf, sizeof buf, fp) != NULL) {
566 buf[strlen(buf)-1] = 0;
567 cprintf("%s\n", buf);
575 #define nForceAliases 5
576 const ConstStr ForceAliases[nForceAliases] = {
580 {HKEY("postmaster,")},
583 void cmd_snet(char *argbuf)
585 struct CitContext *CCC = CC;
586 char tempfilename[PATH_MAX];
587 char filename[PATH_MAX];
594 int MailAliasesFound[nForceAliases];
598 if (!IsEmptyStr(argbuf))
600 if (CtdlAccessCheck(ac_aide)) return;
601 if (strcmp(argbuf, FILE_MAILALIAS))
603 cprintf("%d No such file or directory\n",
604 ERROR + INTERNAL_ERROR);
607 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
608 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
609 memcpy(tempfilename, filename, len + 1);
614 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
615 /* users can edit the netconfigs for their own mailbox rooms */
617 else if (CtdlAccessCheck(ac_room_aide)) return;
619 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
620 memcpy(tempfilename, filename, len + 1);
622 memset(&StatBuf, 0, sizeof(struct stat));
623 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
624 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
626 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
628 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
630 if ((TmpFD > 0) && (errno == 0))
632 char *tmp = malloc(StatBuf.st_size * 2);
633 memset(tmp, ' ', StatBuf.st_size * 2);
634 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
636 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
639 cprintf("%d Unable to allocate the space required for %s: %s\n",
640 ERROR + INTERNAL_ERROR,
643 unlink(tempfilename);
646 lseek(TmpFD, SEEK_SET, 0);
649 cprintf("%d Unable to allocate the space required for %s: %s\n",
650 ERROR + INTERNAL_ERROR,
653 unlink(tempfilename);
658 cprintf("%d %s\n", SEND_LISTING, tempfilename);
661 while (rc = CtdlClientGetLine(Line),
664 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
670 for (i = 0; i < nForceAliases; i++)
672 if ((!MailAliasesFound[i]) &&
673 (strncmp(ForceAliases[i].Key,
675 ForceAliases[i].len) == 0)
678 MailAliasesFound[i] = 1;
684 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
685 write(TmpFD, ChrPtr(Line), StrLength(Line));
686 len += StrLength(Line);
689 ftruncate(TmpFD, len);
696 * Sanity check whether all aliases required by the RFCs were set
700 for (i = 0; i < nForceAliases; i++)
702 if (!MailAliasesFound[i])
707 cprintf("%d won't do this - you're missing an RFC required alias.\n",
708 ERROR + INTERNAL_ERROR);
709 unlink(tempfilename);
714 /* Now copy the temp file to its permanent location.
715 * (We copy instead of link because they may be on different filesystems)
717 begin_critical_section(S_NETCONFIGS);
718 rename(tempfilename, filename);
721 OneRoomNetCfg *RNCfg;
722 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
725 ReadRoomNetConfigFile(&RNCfg, filename);
729 ReadRoomNetConfigFile(&RNCfg, filename);
730 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
733 PerformRoomHooks(&CCC->room);
735 end_critical_section(S_NETCONFIGS);
739 /*-----------------------------------------------------------------------------*
740 * Per node network configs *
741 *-----------------------------------------------------------------------------*/
742 void DeleteCtdlNodeConf(void *vNode)
744 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
745 FreeStrBuf(&Node->NodeName);
746 FreeStrBuf(&Node->Secret);
747 FreeStrBuf(&Node->Host);
748 FreeStrBuf(&Node->Port);
752 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
754 const char *Pos = NULL;
757 /* we need at least 4 pipes and some other text so its invalid. */
758 if (StrLength(SerializedNode) < 8)
760 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
764 Node->NodeName=NewStrBuf();
765 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
767 Node->Secret=NewStrBuf();
768 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
770 Node->Host=NewStrBuf();
771 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
773 Node->Port=NewStrBuf();
774 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
780 * Load or refresh the Citadel network (IGnet) configuration for this node.
782 HashList* CtdlLoadIgNetCfg(void)
791 Cfg = CtdlGetSysConfig(IGNETCFG);
792 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
798 Hash = NewHash(1, NULL);
799 Buf = NewStrBufPlain(Cfg, -1);
801 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
805 StrBufSipLine(LineBuf, Buf, &LinePos);
806 if (StrLength(LineBuf) != 0) {
807 Node = NewNode(LineBuf);
809 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
812 } while (LinePos != StrBufNOTNULL);
814 FreeStrBuf(&LineBuf);
819 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
821 const RoomNetCfg RecipientCfgs[] = {
828 RoomNetCfgLine *nptr;
833 while (RecipientCfgs[i] != maxRoomNetCfg)
835 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
839 if ((StrLength(nptr->Value[0]) == len) &&
840 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
853 int CtdlNetconfigCheckRoomaccess(
856 const char* RemoteIdentifier)
858 OneRoomNetCfg *RNCfg;
861 if (RemoteIdentifier == NULL)
863 snprintf(errmsgbuf, n, "Need sender to permit access.");
864 return (ERROR + USERNAME_REQUIRED);
867 begin_critical_section(S_NETCONFIGS);
868 RNCfg = CtdlGetNetCfgForRoom (CC->room.QRnumber);
871 end_critical_section(S_NETCONFIGS);
872 snprintf(errmsgbuf, n,
873 "This mailing list only accepts posts from subscribers.");
874 return (ERROR + NO_SUCH_USER);
876 found = is_recipient (RNCfg, RemoteIdentifier);
877 end_critical_section(S_NETCONFIGS);
883 snprintf(errmsgbuf, n,
884 "This mailing list only accepts posts from subscribers.");
885 return (ERROR + NO_SUCH_USER);
892 * cmd_netp() - authenticate to the server as another Citadel node polling
893 * for network traffic
895 void cmd_netp(char *cmdbuf)
897 struct CitContext *CCC = CC;
898 HashList *working_ignetcfg;
906 const StrBuf *secret = NULL;
907 const StrBuf *nexthop = NULL;
908 char err_buf[SIZ] = "";
911 node = CCC->curr_user;
912 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
913 NodeStr = NewStrBufPlain(node, nodelen);
914 /* load the IGnet Configuration to check node validity */
915 working_ignetcfg = CtdlLoadIgNetCfg();
916 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
918 snprintf(err_buf, sizeof err_buf,
919 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
920 node, CCC->cs_host, CCC->cs_addr
922 syslog(LOG_WARNING, "%s", err_buf);
923 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
925 strs[0] = CCC->cs_addr;
926 lens[0] = strlen(CCC->cs_addr);
928 strs[1] = "SRV_UNKNOWN";
929 lens[1] = sizeof("SRV_UNKNOWN") - 1;
934 2, strs, (long*) &lens,
938 DeleteHash(&working_ignetcfg);
939 FreeStrBuf(&NodeStr);
943 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
944 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
945 snprintf(err_buf, sizeof err_buf,
946 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
947 CCC->cs_host, CCC->cs_addr, node
949 syslog(LOG_WARNING, "%s", err_buf);
950 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
952 strs[0] = CCC->cs_addr;
953 lens[0] = strlen(CCC->cs_addr);
956 lens[1] = sizeof("SRV_PW") - 1;
966 DeleteHash(&working_ignetcfg);
967 FreeStrBuf(&NodeStr);
971 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
972 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
973 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
974 DeleteHash(&working_ignetcfg);
975 FreeStrBuf(&NodeStr);
978 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
979 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
980 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
981 CCC->net_node, CCC->cs_host, CCC->cs_addr
983 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
984 DeleteHash(&working_ignetcfg);
985 FreeStrBuf(&NodeStr);
989 /*-----------------------------------------------------------------------------*
990 * Network maps: evaluate other nodes *
991 *-----------------------------------------------------------------------------*/
993 void DeleteNetMap(void *vNetMap)
995 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
996 FreeStrBuf(&TheNetMap->NodeName);
997 FreeStrBuf(&TheNetMap->NextHop);
1001 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
1003 const char *Pos = NULL;
1006 /* we need at least 3 pipes and some other text so its invalid. */
1007 if (StrLength(SerializedNetMap) < 6)
1009 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
1011 NM->NodeName=NewStrBuf();
1012 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
1014 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
1016 NM->NextHop=NewStrBuf();
1017 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
1022 HashList* CtdlReadNetworkMap(void)
1024 const char *LinePos;
1029 CtdlNetMap *TheNetMap;
1031 Hash = NewHash(1, NULL);
1032 Cfg = CtdlGetSysConfig(IGNETMAP);
1033 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1039 Buf = NewStrBufPlain(Cfg, -1);
1041 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1043 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1045 TheNetMap = NewNetMap(LineBuf);
1046 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1047 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1051 FreeStrBuf(&LineBuf);
1055 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1060 StrBuf *Ret = NewStrBuf();
1061 HashPos *Pos = GetNewHashPos(Map, 0);
1063 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1065 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1066 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1067 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1069 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1070 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1072 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1073 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1075 DeleteHashPos(&Pos);
1081 * Learn topology from path fields
1083 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1085 CtdlNetMap *pNM = NULL;
1090 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1091 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1093 pNM = (CtdlNetMap*)vptr;
1094 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1095 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1096 pNM->lastcontact = time(NULL);
1097 (*netmap_changed) ++;
1102 /* If we got here then it's not in the map, so add it. */
1103 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1104 nmptr->NodeName = NewStrBufPlain(node, -1);
1105 nmptr->lastcontact = time(NULL);
1106 nmptr->NextHop = NewStrBuf ();
1107 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1108 /* TODO: is the NodeName Uniq? */
1109 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1110 (*netmap_changed) ++;
1115 * Check the network map and determine whether the supplied node name is
1116 * valid. If it is not a neighbor node, supply the name of a neighbor node
1117 * which is the next hop. If it *is* a neighbor node, we also fill in the
1120 int CtdlIsValidNode(const StrBuf **nexthop,
1121 const StrBuf **secret,
1124 HashList *the_netmap)
1128 CtdlNodeConf *TheNode;
1129 CtdlNetMap *TheNetMap;
1131 if (StrLength(node) == 0) {
1136 * First try the neighbor nodes
1138 if (GetCount(IgnetCfg) == 0) {
1139 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1140 if (nexthop != NULL) {
1146 /* try to find a neigbour with the name 'node' */
1147 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1148 (vNodeConf != NULL))
1150 TheNode = (CtdlNodeConf*)vNodeConf;
1152 *secret = TheNode->Secret;
1153 return 0; /* yup, it's a direct neighbor */
1157 * If we get to this point we have to see if we know the next hop
1158 *//* TODO: is the NodeName Uniq? */
1159 if ((GetCount(the_netmap) > 0) &&
1160 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1162 TheNetMap = (CtdlNetMap*)vNetMap;
1163 if (nexthop != NULL)
1164 *nexthop = TheNetMap->NextHop;
1169 * If we get to this point, the supplied node name is bogus.
1171 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1176 void destroy_network_cfgs(void)
1178 HashList *pCfgTypeHash = CfgTypeHash;
1179 HashList *pRoomConfigs;
1181 begin_critical_section(S_NETCONFIGS);
1182 pRoomConfigs = RoomConfigs;
1184 end_critical_section(S_NETCONFIGS);
1185 DeleteHash(&pRoomConfigs);
1188 DeleteHash(&pCfgTypeHash);
1192 * Module entry point
1194 CTDL_MODULE_INIT(netconfig)
1198 CtdlRegisterCleanupHook(destroy_network_cfgs);
1199 LoadAllNetConfigs ();
1200 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1201 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1202 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");