2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2016 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.
19 #include <sys/types.h>
25 # if HAVE_SYS_SYSCALL_H
26 # include <sys/syscall.h>
31 #include <libcitadel.h>
33 #include "include/ctdl_module.h"
34 #include "serv_extensions.h"
37 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg);
38 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg);
40 HashList *CfgTypeHash = NULL;
41 HashList *RoomConfigs = NULL;
42 /*-----------------------------------------------------------------------------*
43 * Per room network configs *
44 *-----------------------------------------------------------------------------*/
45 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d)
49 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
52 pCfg->DeAllocator = d;
56 pCfg->IsSingleLine = uniq;
57 pCfg->nSegments = nSegments;
58 if (CfgTypeHash == NULL)
59 CfgTypeHash = NewHash(1, NULL);
60 Put(CfgTypeHash, Name, len, pCfg, NULL);
64 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
68 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
70 return (const CfgLineType *) pv;
78 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
85 RewindHashPos(CfgTypeHash, It, 1);
86 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
88 pCfg = (CfgLineType*) pv;
94 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCfg)
99 nptr = (RoomNetCfgLine *)
100 malloc(sizeof(RoomNetCfgLine));
101 nptr->next = OneRNCfg->NetConfigs[ThisOne->C];
102 nptr->Value = malloc(sizeof(StrBuf*) * ThisOne->nSegments);
104 memset(nptr->Value, 0, sizeof(StrBuf*) * ThisOne->nSegments);
105 if (ThisOne->nSegments == 1)
107 nptr->Value[0] = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
110 else for (i = 0; i < ThisOne->nSegments; i++)
113 nptr->Value[i] = NewStrBufPlain(NULL, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
114 StrBufExtract_NextToken(nptr->Value[i], Line, &LinePos, '|');
117 OneRNCfg->NetConfigs[ThisOne->C] = nptr;
120 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCfg, RoomNetCfgLine *data)
124 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
125 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
126 for (i = 0; i < ThisOne->nSegments; i++)
128 StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
129 if (i + 1 < ThisOne->nSegments)
130 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
132 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
135 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
142 for (i = 0; i < (*data)->nValues; i++)
144 FreeStrBuf(&(*data)->Value[i]);
146 free ((*data)->Value);
150 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
153 RoomNetCfgLine *NewData;
155 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
156 memset(NewData, 0, sizeof(RoomNetCfgLine));
157 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
158 memset(NewData->Value, 0, sizeof(StrBuf*) * data->nValues);
160 for (i = 0; i < data->nValues; i++)
162 NewData->Value[i] = NewStrBufDup(data->Value[i]);
164 NewData->nValues = data->nValues;
167 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
170 const char *ErrStr = NULL;
172 const CfgLineType *pCfg;
175 OneRoomNetCfg *OneRNCfg = NULL;
177 fd = open(filename, O_NONBLOCK|O_RDONLY);
182 fchown(fd, CTDLUID, (-1));
185 if (*pOneRNCfg != NULL)
187 OneRNCfg = *pOneRNCfg;
188 FreeRoomNetworkStructContent (OneRNCfg);
191 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
192 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
193 *pOneRNCfg = OneRNCfg;
197 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
198 if (StrLength(Line) == 0)
201 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
203 pCfg = GetCfgTypeByStr(SKEY(InStr));
206 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
210 if (OneRNCfg->misc == NULL)
212 OneRNCfg->misc = NewStrBufDup(Line);
216 if(StrLength(OneRNCfg->misc) > 0)
217 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
218 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
229 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
233 StrBuf *OutBuffer = NULL;
234 char tempfilename[PATH_MAX];
239 long reltid; /* if we don't have SYS_gettid, use "random" value */
243 len = strlen(filename);
244 memcpy(tempfilename, filename, len + 1);
246 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
247 reltid = syscall(SYS_gettid);
249 gettimeofday(&tv, NULL);
250 /* Promote to time_t; types differ on some OSes (like darwin) */
251 unixtime = tv.tv_sec;
253 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
255 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
257 if ((TmpFD < 0) || (errno != 0)) {
258 syslog(LOG_CRIT, "ERROR: cannot open %s: %s",
259 filename, strerror(errno));
260 unlink(tempfilename);
265 OutBuffer = NewStrBuf();
266 CfgIt = GetNewHashPos(CfgTypeHash, 1);
267 fchown(TmpFD, ctdluid, 0);
268 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
270 const CfgLineType *pCfg;
271 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
272 if (pCfg->IsSingleLine)
274 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
278 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
279 while (pName != NULL)
281 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
289 DeleteHashPos(&CfgIt);
292 if (OneRNCfg->misc != NULL) {
293 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
296 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
297 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
300 rename(tempfilename, filename);
305 "unable to write %s; [%s]; not enough space on the disk?",
310 unlink(tempfilename);
313 FreeStrBuf(&OutBuffer);
321 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
323 char filename[PATH_MAX];
325 if (OneRNCfg->changed)
327 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
328 SaveRoomNetConfigFile(OneRNCfg, filename);
329 OneRNCfg->changed = 0;
334 void SaveChangedConfigs(void)
336 CtdlForEachNetCfgRoom(SaveModifiedRooms, NULL, maxRoomNetCfg);
340 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
343 RoomNetCfgLine **pLine;
344 char filename[PATH_MAX];
346 if (OneRNCfg == NULL)
349 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
350 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
352 pLine = &OneRNCfg->NetConfigs[LineType];
354 while(*pLine != NULL) pLine = &((*pLine)->next);
357 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
358 SaveRoomNetConfigFile(OneRNCfg, filename);
359 OneRNCfg->changed = 0;
362 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
367 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
372 CfgIt = GetNewHashPos(CfgTypeHash, 1);
373 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
375 const CfgLineType *pCfg;
376 RoomNetCfgLine *pNext, *pName;
378 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
379 pName= OneRNCfg->NetConfigs[eCfg];
380 while (pName != NULL)
385 pCfg->DeAllocator(pCfg, &pName);
389 DeleteGenericCfgLine(NULL, &pName);
394 DeleteHashPos(&CfgIt);
396 FreeStrBuf(&OneRNCfg->Sender);
397 FreeStrBuf(&OneRNCfg->RoomInfo);
398 FreeStrBuf(&OneRNCfg->misc);
399 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
403 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
405 OneRoomNetCfg *OneRNCfg;
406 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
407 FreeRoomNetworkStructContent(OneRNCfg);
412 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
414 vFreeRoomNetworkStruct(*pOneRNCfg);
419 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
422 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
423 return (OneRoomNetCfg*)pv;
427 void LoadAllNetConfigs(void)
431 struct dirent *filedir_entry;
435 OneRoomNetCfg *OneRNCfg;
440 RoomConfigs = NewHash(1, NULL);
441 filedir = opendir (ctdl_netcfg_dir);
442 if (filedir == NULL) {
443 return ; /// todo: panic!
446 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
452 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
453 (filedir_entry != NULL))
455 #ifdef _DIRENT_HAVE_D_NAMLEN
456 d_namelen = filedir_entry->d_namlen;
458 d_namelen = strlen(filedir_entry->d_name);
461 #ifdef _DIRENT_HAVE_D_TYPE
462 d_type = filedir_entry->d_type;
471 #define IFTODT(mode) (((mode) & 0170000) >> 12)
472 #define DTTOIF(dirtype) ((dirtype) << 12)
476 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
477 continue; /* Ignore backup files... */
479 if ((d_namelen == 1) &&
480 (filedir_entry->d_name[0] == '.'))
483 if ((d_namelen == 2) &&
484 (filedir_entry->d_name[0] == '.') &&
485 (filedir_entry->d_name[1] == '.'))
488 snprintf(path, PATH_MAX, "%s/%s",
489 ctdl_netcfg_dir, filedir_entry->d_name);
491 if (d_type == DT_UNKNOWN) {
493 if (lstat(path, &s) == 0) {
494 d_type = IFTODT(s.st_mode);
502 case DT_LNK: /* TODO: check whether its a file or a directory */
505 pch = filedir_entry->d_name;
517 RoomNumber = atol(filedir_entry->d_name);
518 ReadRoomNetConfigFile(&OneRNCfg, path);
520 if (OneRNCfg != NULL)
521 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
535 /*-----------------------------------------------------------------------------*
536 * Per room network configs : exchange with client *
537 *-----------------------------------------------------------------------------*/
538 void cmd_gnet(char *argbuf)
540 char filename[PATH_MAX];
545 if (!IsEmptyStr(argbuf))
547 if (CtdlAccessCheck(ac_aide)) return;
548 if (strcmp(argbuf, FILE_MAILALIAS))
550 cprintf("%d No such file or directory\n",
551 ERROR + INTERNAL_ERROR);
554 safestrncpy(filename, file_mail_aliases, sizeof(filename));
555 cprintf("%d Settings for <%s>\n",
561 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
562 /* users can edit the netconfigs for their own mailbox rooms */
564 else if (CtdlAccessCheck(ac_room_aide)) return;
566 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
567 cprintf("%d Network settings for room #%ld <%s>\n",
569 CC->room.QRnumber, CC->room.QRname);
572 fp = fopen(filename, "r");
574 while (fgets(buf, sizeof buf, fp) != NULL) {
575 buf[strlen(buf)-1] = 0;
576 cprintf("%s\n", buf);
584 #define nForceAliases 5
585 const ConstStr ForceAliases[nForceAliases] = {
589 {HKEY("postmaster,")},
592 void cmd_snet(char *argbuf)
594 struct CitContext *CCC = CC;
595 char tempfilename[PATH_MAX];
596 char filename[PATH_MAX];
603 int MailAliasesFound[nForceAliases];
607 if (!IsEmptyStr(argbuf))
609 if (CtdlAccessCheck(ac_aide)) return;
610 if (strcmp(argbuf, FILE_MAILALIAS))
612 cprintf("%d No such file or directory\n",
613 ERROR + INTERNAL_ERROR);
616 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
617 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
618 memcpy(tempfilename, filename, len + 1);
623 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
624 /* users can edit the netconfigs for their own mailbox rooms */
626 else if (CtdlAccessCheck(ac_room_aide)) return;
628 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
629 memcpy(tempfilename, filename, len + 1);
631 memset(&StatBuf, 0, sizeof(struct stat));
632 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
633 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
635 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
637 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
639 if ((TmpFD > 0) && (errno == 0))
641 char *tmp = malloc(StatBuf.st_size * 2);
642 memset(tmp, ' ', StatBuf.st_size * 2);
643 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
645 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
648 cprintf("%d Unable to allocate the space required for %s: %s\n",
649 ERROR + INTERNAL_ERROR,
652 unlink(tempfilename);
655 lseek(TmpFD, SEEK_SET, 0);
658 cprintf("%d Unable to allocate the space required for %s: %s\n",
659 ERROR + INTERNAL_ERROR,
662 unlink(tempfilename);
667 cprintf("%d %s\n", SEND_LISTING, tempfilename);
670 while (rc = CtdlClientGetLine(Line),
673 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
679 for (i = 0; i < nForceAliases; i++)
681 if ((!MailAliasesFound[i]) &&
682 (strncmp(ForceAliases[i].Key,
684 ForceAliases[i].len) == 0)
687 MailAliasesFound[i] = 1;
693 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
694 write(TmpFD, ChrPtr(Line), StrLength(Line));
695 len += StrLength(Line);
698 ftruncate(TmpFD, len);
705 * Sanity check whether all aliases required by the RFCs were set
709 for (i = 0; i < nForceAliases; i++)
711 if (!MailAliasesFound[i])
716 cprintf("%d won't do this - you're missing an RFC required alias.\n",
717 ERROR + INTERNAL_ERROR);
718 unlink(tempfilename);
723 /* Now copy the temp file to its permanent location.
724 * (We copy instead of link because they may be on different filesystems)
726 begin_critical_section(S_NETCONFIGS);
727 rename(tempfilename, filename);
730 OneRoomNetCfg *RNCfg;
731 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
734 ReadRoomNetConfigFile(&RNCfg, filename);
738 ReadRoomNetConfigFile(&RNCfg, filename);
739 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
742 PerformRoomHooks(&CCC->room);
744 end_critical_section(S_NETCONFIGS);
748 /*-----------------------------------------------------------------------------*
749 * Per node network configs *
750 *-----------------------------------------------------------------------------*/
751 void DeleteCtdlNodeConf(void *vNode)
753 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
754 FreeStrBuf(&Node->NodeName);
755 FreeStrBuf(&Node->Secret);
756 FreeStrBuf(&Node->Host);
757 FreeStrBuf(&Node->Port);
761 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
763 const char *Pos = NULL;
766 /* we need at least 4 pipes and some other text so its invalid. */
767 if (StrLength(SerializedNode) < 8)
769 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
773 Node->NodeName=NewStrBuf();
774 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
776 Node->Secret=NewStrBuf();
777 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
779 Node->Host=NewStrBuf();
780 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
782 Node->Port=NewStrBuf();
783 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
789 * Load or refresh the Citadel network (IGnet) configuration for this node.
791 HashList* CtdlLoadIgNetCfg(void)
800 Cfg = CtdlGetSysConfig(IGNETCFG);
801 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
807 Hash = NewHash(1, NULL);
808 Buf = NewStrBufPlain(Cfg, -1);
810 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
814 StrBufSipLine(LineBuf, Buf, &LinePos);
815 if (StrLength(LineBuf) != 0) {
816 Node = NewNode(LineBuf);
818 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
821 } while (LinePos != StrBufNOTNULL);
823 FreeStrBuf(&LineBuf);
828 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
830 const RoomNetCfg RecipientCfgs[] = {
837 RoomNetCfgLine *nptr;
842 while (RecipientCfgs[i] != maxRoomNetCfg)
844 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
848 if ((StrLength(nptr->Value[0]) == len) &&
849 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
862 int CtdlNetconfigCheckRoomaccess(
865 const char* RemoteIdentifier)
867 OneRoomNetCfg *RNCfg;
870 if (RemoteIdentifier == NULL)
872 snprintf(errmsgbuf, n, "Need sender to permit access.");
873 return (ERROR + USERNAME_REQUIRED);
876 begin_critical_section(S_NETCONFIGS);
877 RNCfg = CtdlGetNetCfgForRoom (CC->room.QRnumber);
880 end_critical_section(S_NETCONFIGS);
881 snprintf(errmsgbuf, n,
882 "This mailing list only accepts posts from subscribers.");
883 return (ERROR + NO_SUCH_USER);
885 found = is_recipient (RNCfg, RemoteIdentifier);
886 end_critical_section(S_NETCONFIGS);
892 snprintf(errmsgbuf, n,
893 "This mailing list only accepts posts from subscribers.");
894 return (ERROR + NO_SUCH_USER);
901 * cmd_netp() - authenticate to the server as another Citadel node polling
902 * for network traffic
904 void cmd_netp(char *cmdbuf)
906 struct CitContext *CCC = CC;
907 HashList *working_ignetcfg;
915 const StrBuf *secret = NULL;
916 const StrBuf *nexthop = NULL;
917 char err_buf[SIZ] = "";
920 node = CCC->curr_user;
921 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
922 NodeStr = NewStrBufPlain(node, nodelen);
923 /* load the IGnet Configuration to check node validity */
924 working_ignetcfg = CtdlLoadIgNetCfg();
925 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
927 snprintf(err_buf, sizeof err_buf,
928 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
929 node, CCC->cs_host, CCC->cs_addr
931 syslog(LOG_WARNING, "%s", err_buf);
932 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
934 strs[0] = CCC->cs_addr;
935 lens[0] = strlen(CCC->cs_addr);
937 strs[1] = "SRV_UNKNOWN";
938 lens[1] = sizeof("SRV_UNKNOWN") - 1;
943 2, strs, (long*) &lens,
947 DeleteHash(&working_ignetcfg);
948 FreeStrBuf(&NodeStr);
952 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
953 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
954 snprintf(err_buf, sizeof err_buf,
955 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
956 CCC->cs_host, CCC->cs_addr, node
958 syslog(LOG_WARNING, "%s", err_buf);
959 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
961 strs[0] = CCC->cs_addr;
962 lens[0] = strlen(CCC->cs_addr);
965 lens[1] = sizeof("SRV_PW") - 1;
975 DeleteHash(&working_ignetcfg);
976 FreeStrBuf(&NodeStr);
980 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
981 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
982 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
983 DeleteHash(&working_ignetcfg);
984 FreeStrBuf(&NodeStr);
987 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
988 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
989 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]",
990 CCC->net_node, CCC->cs_host, CCC->cs_addr
992 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
993 DeleteHash(&working_ignetcfg);
994 FreeStrBuf(&NodeStr);
998 /*-----------------------------------------------------------------------------*
999 * Network maps: evaluate other nodes *
1000 *-----------------------------------------------------------------------------*/
1002 void DeleteNetMap(void *vNetMap)
1004 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
1005 FreeStrBuf(&TheNetMap->NodeName);
1006 FreeStrBuf(&TheNetMap->NextHop);
1010 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
1012 const char *Pos = NULL;
1015 /* we need at least 3 pipes and some other text so its invalid. */
1016 if (StrLength(SerializedNetMap) < 6)
1018 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
1020 NM->NodeName=NewStrBuf();
1021 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
1023 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
1025 NM->NextHop=NewStrBuf();
1026 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
1031 HashList* CtdlReadNetworkMap(void)
1033 const char *LinePos;
1038 CtdlNetMap *TheNetMap;
1040 Hash = NewHash(1, NULL);
1041 Cfg = CtdlGetSysConfig(IGNETMAP);
1042 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1048 Buf = NewStrBufPlain(Cfg, -1);
1050 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1052 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1054 TheNetMap = NewNetMap(LineBuf);
1055 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1056 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1060 FreeStrBuf(&LineBuf);
1064 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1069 StrBuf *Ret = NewStrBuf();
1070 HashPos *Pos = GetNewHashPos(Map, 0);
1072 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1074 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1075 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1076 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1078 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1079 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1081 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1082 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1084 DeleteHashPos(&Pos);
1090 * Learn topology from path fields
1092 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1094 CtdlNetMap *pNM = NULL;
1099 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1100 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1102 pNM = (CtdlNetMap*)vptr;
1103 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1104 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1105 pNM->lastcontact = time(NULL);
1106 (*netmap_changed) ++;
1111 /* If we got here then it's not in the map, so add it. */
1112 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1113 nmptr->NodeName = NewStrBufPlain(node, -1);
1114 nmptr->lastcontact = time(NULL);
1115 nmptr->NextHop = NewStrBuf ();
1116 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1117 /* TODO: is the NodeName Uniq? */
1118 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1119 (*netmap_changed) ++;
1124 * Check the network map and determine whether the supplied node name is
1125 * valid. If it is not a neighbor node, supply the name of a neighbor node
1126 * which is the next hop. If it *is* a neighbor node, we also fill in the
1129 int CtdlIsValidNode(const StrBuf **nexthop,
1130 const StrBuf **secret,
1133 HashList *the_netmap)
1137 CtdlNodeConf *TheNode;
1138 CtdlNetMap *TheNetMap;
1140 if (StrLength(node) == 0) {
1145 * First try the neighbor nodes
1147 if (GetCount(IgnetCfg) == 0) {
1148 syslog(LOG_INFO, "IgnetCfg is empty!");
1149 if (nexthop != NULL) {
1155 /* try to find a neigbour with the name 'node' */
1156 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1157 (vNodeConf != NULL))
1159 TheNode = (CtdlNodeConf*)vNodeConf;
1161 *secret = TheNode->Secret;
1162 return 0; /* yup, it's a direct neighbor */
1166 * If we get to this point we have to see if we know the next hop
1167 *//* TODO: is the NodeName Uniq? */
1168 if ((GetCount(the_netmap) > 0) &&
1169 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1171 TheNetMap = (CtdlNetMap*)vNetMap;
1172 if (nexthop != NULL)
1173 *nexthop = TheNetMap->NextHop;
1178 * If we get to this point, the supplied node name is bogus.
1180 syslog(LOG_ERR, "Invalid node name <%s>", ChrPtr(node));
1185 void destroy_network_cfgs(void)
1187 HashList *pCfgTypeHash = CfgTypeHash;
1188 HashList *pRoomConfigs;
1190 begin_critical_section(S_NETCONFIGS);
1191 pRoomConfigs = RoomConfigs;
1193 end_critical_section(S_NETCONFIGS);
1194 DeleteHash(&pRoomConfigs);
1197 DeleteHash(&pCfgTypeHash);
1204 * Create a config key for a room's netconfig entry
1206 void netcfg_keyname(char *keybuf, long roomnum)
1208 if (!keybuf) return;
1209 sprintf(keybuf, "c_netconfig_%010ld", roomnum);
1216 * Convert any legacy configuration files in the "netconfigs" directory
1218 void convert_legacy_netcfg_files(void)
1221 struct dirent *dit = NULL;
1223 char filename[PATH_MAX];
1229 dh = opendir(ctdl_netcfg_dir);
1232 syslog(LOG_INFO, "Legacy netconfig files exist - converting them!");
1234 while (dit = readdir(dh), dit != NULL) { // yes, we use the non-reentrant version; we're not in threaded mode yet
1235 roomnum = atol(dit->d_name);
1237 netcfg_keyname(keyname, roomnum);
1238 snprintf(filename, sizeof filename, "%s/%ld", ctdl_netcfg_dir, roomnum);
1239 fp = fopen(filename, "r");
1241 fseek(fp, 0L, SEEK_END);
1246 if (fread(v, len, 1, fp)) {
1247 char *enc = malloc(len * 2);
1250 enc_len = CtdlEncodeBase64(enc, v, len, 0);
1251 if ((enc_len > 1) && (enc[enc_len-2] == 13)) enc[enc_len-2] = 0;
1252 if ((enc_len > 0) && (enc[enc_len-1] == 10)) enc[enc_len-1] = 0;
1254 syslog(LOG_DEBUG, "Writing key '%s' (length=%d)", keyname, enc_len);
1255 CtdlSetConfigStr(keyname, enc);
1257 // unlink(filename); // FIXME uncomment this when ready
1268 // rmdir(ctdl_netcfg_dir); // FIXME uncomment this when ready
1274 * Module entry point
1276 CTDL_MODULE_INIT(netconfig)
1280 CtdlRegisterCleanupHook(destroy_network_cfgs);
1281 convert_legacy_netcfg_files();
1282 LoadAllNetConfigs();
1283 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1284 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1285 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");