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"
34 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg);
35 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg);
37 HashList *CfgTypeHash = NULL;
38 HashList *RoomConfigs = NULL;
39 /*-----------------------------------------------------------------------------*
40 * Per room network configs *
41 *-----------------------------------------------------------------------------*/
42 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d)
46 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
49 pCfg->DeAllocator = d;
53 pCfg->IsSingleLine = uniq;
54 pCfg->nSegments = nSegments;
55 if (CfgTypeHash == NULL)
56 CfgTypeHash = NewHash(1, NULL);
57 Put(CfgTypeHash, Name, len, pCfg, NULL);
61 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
65 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
67 return (const CfgLineType *) pv;
75 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
82 RewindHashPos(CfgTypeHash, It, 1);
83 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
85 pCfg = (CfgLineType*) pv;
91 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCfg)
96 nptr = (RoomNetCfgLine *)
97 malloc(sizeof(RoomNetCfgLine));
98 nptr->next = OneRNCfg->NetConfigs[ThisOne->C];
99 nptr->Value = malloc(sizeof(StrBuf*) * ThisOne->nSegments);
101 memset(nptr->Value, 0, sizeof(StrBuf*) * ThisOne->nSegments);
102 if (ThisOne->nSegments == 1)
104 nptr->Value[0] = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
107 else for (i = 0; i < ThisOne->nSegments; i++)
110 nptr->Value[i] = NewStrBufPlain(NULL, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
111 StrBufExtract_NextToken(nptr->Value[i], Line, &LinePos, '|');
114 OneRNCfg->NetConfigs[ThisOne->C] = nptr;
117 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCfg, RoomNetCfgLine *data)
121 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
122 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
123 for (i = 0; i < ThisOne->nSegments; i++)
125 StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
126 if (i + 1 < ThisOne->nSegments)
127 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
129 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
132 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
139 for (i = 0; i < (*data)->nValues; i++)
141 FreeStrBuf(&(*data)->Value[i]);
143 free ((*data)->Value);
147 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
150 RoomNetCfgLine *NewData;
152 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
153 memset(NewData, 0, sizeof(RoomNetCfgLine));
154 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
155 memset(NewData->Value, 0, sizeof(StrBuf*) * data->nValues);
157 for (i = 0; i < data->nValues; i++)
159 NewData->Value[i] = NewStrBufDup(data->Value[i]);
161 NewData->nValues = data->nValues;
164 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
167 const char *ErrStr = NULL;
169 const CfgLineType *pCfg;
172 OneRoomNetCfg *OneRNCfg = NULL;
174 fd = open(filename, O_NONBLOCK|O_RDONLY);
179 if (*pOneRNCfg != NULL)
181 OneRNCfg = *pOneRNCfg;
182 FreeRoomNetworkStructContent (OneRNCfg);
185 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
186 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
187 *pOneRNCfg = OneRNCfg;
191 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
192 if (StrLength(Line) == 0)
195 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
197 pCfg = GetCfgTypeByStr(SKEY(InStr));
200 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
204 if (OneRNCfg->misc == NULL)
206 OneRNCfg->misc = NewStrBufDup(Line);
210 if(StrLength(OneRNCfg->misc) > 0)
211 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
212 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
223 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
227 StrBuf *OutBuffer = NULL;
228 char tempfilename[PATH_MAX];
233 long reltid; /* if we don't have SYS_gettid, use "random" value */
237 len = strlen(filename);
238 memcpy(tempfilename, filename, len + 1);
240 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
241 reltid = syscall(SYS_gettid);
243 gettimeofday(&tv, NULL);
244 /* Promote to time_t; types differ on some OSes (like darwin) */
245 unixtime = tv.tv_sec;
247 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
249 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
251 if ((TmpFD < 0) || (errno != 0)) {
252 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
253 filename, strerror(errno));
254 unlink(tempfilename);
259 OutBuffer = NewStrBuf();
260 CfgIt = GetNewHashPos(CfgTypeHash, 1);
261 fchown(TmpFD, config.c_ctdluid, 0);
262 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
264 const CfgLineType *pCfg;
265 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
266 if (pCfg->IsSingleLine)
268 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
272 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
273 while (pName != NULL)
275 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
283 DeleteHashPos(&CfgIt);
286 if (OneRNCfg->misc != NULL) {
287 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
290 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
291 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
294 rename(tempfilename, filename);
299 "unable to write %s; [%s]; not enough space on the disk?\n",
303 unlink(tempfilename);
306 FreeStrBuf(&OutBuffer);
313 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
315 char filename[PATH_MAX];
317 if (OneRNCfg->changed)
319 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
320 SaveRoomNetConfigFile(OneRNCfg, filename);
321 OneRNCfg->changed = 0;
324 void SaveChangedConfigs(void)
326 CtdlForEachNetCfgRoom(SaveModifiedRooms,
332 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
335 RoomNetCfgLine **pLine;
336 char filename[PATH_MAX];
338 if (OneRNCfg == NULL)
341 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
342 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
344 pLine = &OneRNCfg->NetConfigs[LineType];
346 while(*pLine != NULL) pLine = &((*pLine)->next);
349 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
350 SaveRoomNetConfigFile(OneRNCfg, filename);
351 OneRNCfg->changed = 0;
354 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
358 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
363 CfgIt = GetNewHashPos(CfgTypeHash, 1);
364 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
366 const CfgLineType *pCfg;
367 RoomNetCfgLine *pNext, *pName;
369 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
370 pName= OneRNCfg->NetConfigs[eCfg];
371 while (pName != NULL)
376 pCfg->DeAllocator(pCfg, &pName);
380 DeleteGenericCfgLine(NULL, &pName);
385 DeleteHashPos(&CfgIt);
387 FreeStrBuf(&OneRNCfg->Sender);
388 FreeStrBuf(&OneRNCfg->RoomInfo);
389 FreeStrBuf(&OneRNCfg->misc);
390 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
392 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
394 OneRoomNetCfg *OneRNCfg;
395 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
396 FreeRoomNetworkStructContent(OneRNCfg);
399 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
401 vFreeRoomNetworkStruct(*pOneRNCfg);
405 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
408 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
409 return (OneRoomNetCfg*)pv;
413 void LoadAllNetConfigs(void)
417 struct dirent *filedir_entry;
421 OneRoomNetCfg *OneRNCfg;
426 RoomConfigs = NewHash(1, NULL);
427 filedir = opendir (ctdl_netcfg_dir);
428 if (filedir == NULL) {
429 return ; /// todo: panic!
432 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
438 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
439 (filedir_entry != NULL))
441 #ifdef _DIRENT_HAVE_D_NAMLEN
442 d_namelen = filedir_entry->d_namelen;
444 d_namelen = strlen(filedir_entry->d_name);
447 #ifdef _DIRENT_HAVE_D_TYPE
448 d_type = filedir_entry->d_type;
457 #define IFTODT(mode) (((mode) & 0170000) >> 12)
458 #define DTTOIF(dirtype) ((dirtype) << 12)
462 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
463 continue; /* Ignore backup files... */
465 if ((d_namelen == 1) &&
466 (filedir_entry->d_name[0] == '.'))
469 if ((d_namelen == 2) &&
470 (filedir_entry->d_name[0] == '.') &&
471 (filedir_entry->d_name[1] == '.'))
474 snprintf(path, PATH_MAX, "%s/%s",
475 ctdl_netcfg_dir, filedir_entry->d_name);
477 if (d_type == DT_UNKNOWN) {
479 if (lstat(path, &s) == 0) {
480 d_type = IFTODT(s.st_mode);
488 case DT_LNK: /* TODO: check whether its a file or a directory */
491 pch = filedir_entry->d_name;
503 RoomNumber = atol(filedir_entry->d_name);
504 ReadRoomNetConfigFile(&OneRNCfg, path);
506 if (OneRNCfg != NULL)
507 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
508 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
522 /*-----------------------------------------------------------------------------*
523 * Per room network configs : exchange with client *
524 *-----------------------------------------------------------------------------*/
525 void cmd_gnet(char *argbuf)
527 char filename[PATH_MAX];
532 if (!IsEmptyStr(argbuf))
534 if (CtdlAccessCheck(ac_aide)) return;
535 if (strcmp(argbuf, FILE_MAILALIAS))
537 cprintf("%d No such file or directory\n",
538 ERROR + INTERNAL_ERROR);
541 safestrncpy(filename, file_mail_aliases, sizeof(filename));
542 cprintf("%d Settings for <%s>\n",
548 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
549 /* users can edit the netconfigs for their own mailbox rooms */
551 else if (CtdlAccessCheck(ac_room_aide)) return;
553 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
554 cprintf("%d Network settings for room #%ld <%s>\n",
556 CC->room.QRnumber, CC->room.QRname);
559 fp = fopen(filename, "r");
561 while (fgets(buf, sizeof buf, fp) != NULL) {
562 buf[strlen(buf)-1] = 0;
563 cprintf("%s\n", buf);
571 #define nForceAliases 5
572 const ConstStr ForceAliases[nForceAliases] = {
576 {HKEY("postmaster,")},
579 void cmd_snet(char *argbuf)
581 struct CitContext *CCC = CC;
582 char tempfilename[PATH_MAX];
583 char filename[PATH_MAX];
590 int MailAliasesFound[nForceAliases];
594 if (!IsEmptyStr(argbuf))
596 if (CtdlAccessCheck(ac_aide)) return;
597 if (strcmp(argbuf, FILE_MAILALIAS))
599 cprintf("%d No such file or directory\n",
600 ERROR + INTERNAL_ERROR);
603 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
604 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
605 memcpy(tempfilename, filename, len + 1);
610 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
611 /* users can edit the netconfigs for their own mailbox rooms */
613 else if (CtdlAccessCheck(ac_room_aide)) return;
615 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
616 memcpy(tempfilename, filename, len + 1);
618 memset(&StatBuf, 0, sizeof(struct stat));
619 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
620 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
622 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
624 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
626 if ((TmpFD > 0) && (errno == 0))
628 char *tmp = malloc(StatBuf.st_size * 2);
629 memset(tmp, ' ', StatBuf.st_size * 2);
630 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
632 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
635 cprintf("%d Unable to allocate the space required for %s: %s\n",
636 ERROR + INTERNAL_ERROR,
639 unlink(tempfilename);
642 lseek(TmpFD, SEEK_SET, 0);
645 cprintf("%d Unable to allocate the space required for %s: %s\n",
646 ERROR + INTERNAL_ERROR,
649 unlink(tempfilename);
654 cprintf("%d %s\n", SEND_LISTING, tempfilename);
657 while (rc = CtdlClientGetLine(Line),
660 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
666 for (i = 0; i < nForceAliases; i++)
668 if ((!MailAliasesFound[i]) &&
669 (strncmp(ForceAliases[i].Key,
671 ForceAliases[i].len) == 0)
674 MailAliasesFound[i] = 1;
680 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
681 write(TmpFD, ChrPtr(Line), StrLength(Line));
682 len += StrLength(Line);
685 ftruncate(TmpFD, len);
692 * Sanity check whether all aliases required by the RFCs were set
696 for (i = 0; i < nForceAliases; i++)
698 if (!MailAliasesFound[i])
703 cprintf("%d won't do this - you're missing an RFC required alias.\n",
704 ERROR + INTERNAL_ERROR);
705 unlink(tempfilename);
710 /* Now copy the temp file to its permanent location.
711 * (We copy instead of link because they may be on different filesystems)
713 begin_critical_section(S_NETCONFIGS);
714 rename(tempfilename, filename);
717 OneRoomNetCfg *RNCfg;
718 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
721 ReadRoomNetConfigFile(&RNCfg, filename);
725 ReadRoomNetConfigFile(&RNCfg, filename);
726 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
729 PerformRoomHooks(&CCC->room);
731 end_critical_section(S_NETCONFIGS);
735 /*-----------------------------------------------------------------------------*
736 * Per node network configs *
737 *-----------------------------------------------------------------------------*/
738 void DeleteCtdlNodeConf(void *vNode)
740 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
741 FreeStrBuf(&Node->NodeName);
742 FreeStrBuf(&Node->Secret);
743 FreeStrBuf(&Node->Host);
744 FreeStrBuf(&Node->Port);
748 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
750 const char *Pos = NULL;
753 /* we need at least 4 pipes and some other text so its invalid. */
754 if (StrLength(SerializedNode) < 8)
756 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
760 Node->NodeName=NewStrBuf();
761 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
763 Node->Secret=NewStrBuf();
764 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
766 Node->Host=NewStrBuf();
767 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
769 Node->Port=NewStrBuf();
770 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
776 * Load or refresh the Citadel network (IGnet) configuration for this node.
778 HashList* CtdlLoadIgNetCfg(void)
787 Cfg = CtdlGetSysConfig(IGNETCFG);
788 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
794 Hash = NewHash(1, NULL);
795 Buf = NewStrBufPlain(Cfg, -1);
797 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
801 StrBufSipLine(LineBuf, Buf, &LinePos);
802 if (StrLength(LineBuf) != 0) {
803 Node = NewNode(LineBuf);
805 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
808 } while (LinePos != StrBufNOTNULL);
810 FreeStrBuf(&LineBuf);
815 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
817 const RoomNetCfg RecipientCfgs[] = {
824 RoomNetCfgLine *nptr;
829 while (RecipientCfgs[i] != maxRoomNetCfg)
831 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
835 if ((StrLength(nptr->Value[0]) == len) &&
836 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
849 int CtdlNetconfigCheckRoomaccess(
852 const char* RemoteIdentifier)
854 OneRoomNetCfg *RNCfg;
857 if (RemoteIdentifier == NULL)
859 snprintf(errmsgbuf, n, "Need sender to permit access.");
860 return (ERROR + USERNAME_REQUIRED);
863 begin_critical_section(S_NETCONFIGS);
864 RNCfg = CtdlGetNetCfgForRoom (CC->room.QRnumber);
867 end_critical_section(S_NETCONFIGS);
868 snprintf(errmsgbuf, n,
869 "This mailing list only accepts posts from subscribers.");
870 return (ERROR + NO_SUCH_USER);
872 found = is_recipient (RNCfg, RemoteIdentifier);
873 end_critical_section(S_NETCONFIGS);
879 snprintf(errmsgbuf, n,
880 "This mailing list only accepts posts from subscribers.");
881 return (ERROR + NO_SUCH_USER);
888 * cmd_netp() - authenticate to the server as another Citadel node polling
889 * for network traffic
891 void cmd_netp(char *cmdbuf)
893 struct CitContext *CCC = CC;
894 HashList *working_ignetcfg;
902 const StrBuf *secret = NULL;
903 const StrBuf *nexthop = NULL;
904 char err_buf[SIZ] = "";
907 node = CCC->curr_user;
908 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
909 NodeStr = NewStrBufPlain(node, nodelen);
910 /* load the IGnet Configuration to check node validity */
911 working_ignetcfg = CtdlLoadIgNetCfg();
912 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
914 snprintf(err_buf, sizeof err_buf,
915 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
916 node, CCC->cs_host, CCC->cs_addr
918 syslog(LOG_WARNING, "%s", err_buf);
919 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
921 strs[0] = CCC->cs_addr;
922 lens[0] = strlen(CCC->cs_addr);
924 strs[1] = "SRV_UNKNOWN";
925 lens[1] = sizeof("SRV_UNKNOWN" - 1);
930 2, strs, (long*) &lens,
934 DeleteHash(&working_ignetcfg);
935 FreeStrBuf(&NodeStr);
939 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
940 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
941 snprintf(err_buf, sizeof err_buf,
942 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
943 CCC->cs_host, CCC->cs_addr, node
945 syslog(LOG_WARNING, "%s", err_buf);
946 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
948 strs[0] = CCC->cs_addr;
949 lens[0] = strlen(CCC->cs_addr);
952 lens[1] = sizeof("SRV_PW" - 1);
962 DeleteHash(&working_ignetcfg);
963 FreeStrBuf(&NodeStr);
967 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
968 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
969 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
970 DeleteHash(&working_ignetcfg);
971 FreeStrBuf(&NodeStr);
974 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
975 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
976 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
977 CCC->net_node, CCC->cs_host, CCC->cs_addr
979 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
980 DeleteHash(&working_ignetcfg);
981 FreeStrBuf(&NodeStr);
985 /*-----------------------------------------------------------------------------*
986 * Network maps: evaluate other nodes *
987 *-----------------------------------------------------------------------------*/
989 void DeleteNetMap(void *vNetMap)
991 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
992 FreeStrBuf(&TheNetMap->NodeName);
993 FreeStrBuf(&TheNetMap->NextHop);
997 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
999 const char *Pos = NULL;
1002 /* we need at least 3 pipes and some other text so its invalid. */
1003 if (StrLength(SerializedNetMap) < 6)
1005 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
1007 NM->NodeName=NewStrBuf();
1008 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
1010 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
1012 NM->NextHop=NewStrBuf();
1013 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
1018 HashList* CtdlReadNetworkMap(void)
1020 const char *LinePos;
1025 CtdlNetMap *TheNetMap;
1027 Hash = NewHash(1, NULL);
1028 Cfg = CtdlGetSysConfig(IGNETMAP);
1029 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1035 Buf = NewStrBufPlain(Cfg, -1);
1037 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1039 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1041 TheNetMap = NewNetMap(LineBuf);
1042 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1043 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1047 FreeStrBuf(&LineBuf);
1051 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1056 StrBuf *Ret = NewStrBuf();
1057 HashPos *Pos = GetNewHashPos(Map, 0);
1059 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1061 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1062 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1063 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1065 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1066 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1068 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1069 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1071 DeleteHashPos(&Pos);
1077 * Learn topology from path fields
1079 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1081 CtdlNetMap *pNM = NULL;
1086 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1087 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1089 pNM = (CtdlNetMap*)vptr;
1090 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1091 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1092 pNM->lastcontact = time(NULL);
1093 (*netmap_changed) ++;
1098 /* If we got here then it's not in the map, so add it. */
1099 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1100 nmptr->NodeName = NewStrBufPlain(node, -1);
1101 nmptr->lastcontact = time(NULL);
1102 nmptr->NextHop = NewStrBuf ();
1103 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1104 /* TODO: is the NodeName Uniq? */
1105 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1106 (*netmap_changed) ++;
1111 * Check the network map and determine whether the supplied node name is
1112 * valid. If it is not a neighbor node, supply the name of a neighbor node
1113 * which is the next hop. If it *is* a neighbor node, we also fill in the
1116 int CtdlIsValidNode(const StrBuf **nexthop,
1117 const StrBuf **secret,
1120 HashList *the_netmap)
1124 CtdlNodeConf *TheNode;
1125 CtdlNetMap *TheNetMap;
1127 if (StrLength(node) == 0) {
1132 * First try the neighbor nodes
1134 if (GetCount(IgnetCfg) == 0) {
1135 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1136 if (nexthop != NULL) {
1142 /* try to find a neigbour with the name 'node' */
1143 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1144 (vNodeConf != NULL))
1146 TheNode = (CtdlNodeConf*)vNodeConf;
1148 *secret = TheNode->Secret;
1149 return 0; /* yup, it's a direct neighbor */
1153 * If we get to this point we have to see if we know the next hop
1154 *//* TODO: is the NodeName Uniq? */
1155 if ((GetCount(the_netmap) > 0) &&
1156 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1158 TheNetMap = (CtdlNetMap*)vNetMap;
1159 if (nexthop != NULL)
1160 *nexthop = TheNetMap->NextHop;
1165 * If we get to this point, the supplied node name is bogus.
1167 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1172 void destroy_network_cfgs(void)
1174 HashList *pCfgTypeHash = CfgTypeHash;
1175 HashList *pRoomConfigs;
1177 begin_critical_section(S_NETCONFIGS);
1178 pRoomConfigs = RoomConfigs;
1180 end_critical_section(S_NETCONFIGS);
1181 DeleteHash(&pRoomConfigs);
1184 DeleteHash(&pCfgTypeHash);
1188 * Module entry point
1190 CTDL_MODULE_INIT(netconfig)
1194 CtdlRegisterCleanupHook(destroy_network_cfgs);
1195 LoadAllNetConfigs ();
1196 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1197 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1198 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");