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 fchown(fd, CTDLUID, (-1));
182 if (*pOneRNCfg != NULL)
184 OneRNCfg = *pOneRNCfg;
185 FreeRoomNetworkStructContent (OneRNCfg);
188 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
189 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
190 *pOneRNCfg = OneRNCfg;
194 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
195 if (StrLength(Line) == 0)
198 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
200 pCfg = GetCfgTypeByStr(SKEY(InStr));
203 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
207 if (OneRNCfg->misc == NULL)
209 OneRNCfg->misc = NewStrBufDup(Line);
213 if(StrLength(OneRNCfg->misc) > 0)
214 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
215 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
226 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
230 StrBuf *OutBuffer = NULL;
231 char tempfilename[PATH_MAX];
236 long reltid; /* if we don't have SYS_gettid, use "random" value */
240 len = strlen(filename);
241 memcpy(tempfilename, filename, len + 1);
243 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
244 reltid = syscall(SYS_gettid);
246 gettimeofday(&tv, NULL);
247 /* Promote to time_t; types differ on some OSes (like darwin) */
248 unixtime = tv.tv_sec;
250 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
252 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
254 if ((TmpFD < 0) || (errno != 0)) {
255 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
256 filename, strerror(errno));
257 unlink(tempfilename);
262 OutBuffer = NewStrBuf();
263 CfgIt = GetNewHashPos(CfgTypeHash, 1);
264 fchown(TmpFD, config.c_ctdluid, 0);
265 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
267 const CfgLineType *pCfg;
268 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
269 if (pCfg->IsSingleLine)
271 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
275 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
276 while (pName != NULL)
278 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
286 DeleteHashPos(&CfgIt);
289 if (OneRNCfg->misc != NULL) {
290 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
293 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
294 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
297 rename(tempfilename, filename);
302 "unable to write %s; [%s]; not enough space on the disk?\n",
306 unlink(tempfilename);
309 FreeStrBuf(&OutBuffer);
316 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
318 char filename[PATH_MAX];
320 if (OneRNCfg->changed)
322 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
323 SaveRoomNetConfigFile(OneRNCfg, filename);
324 OneRNCfg->changed = 0;
327 void SaveChangedConfigs(void)
329 CtdlForEachNetCfgRoom(SaveModifiedRooms,
335 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
338 RoomNetCfgLine **pLine;
339 char filename[PATH_MAX];
341 if (OneRNCfg == NULL)
344 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
345 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
347 pLine = &OneRNCfg->NetConfigs[LineType];
349 while(*pLine != NULL) pLine = &((*pLine)->next);
352 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
353 SaveRoomNetConfigFile(OneRNCfg, filename);
354 OneRNCfg->changed = 0;
357 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
361 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
366 CfgIt = GetNewHashPos(CfgTypeHash, 1);
367 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
369 const CfgLineType *pCfg;
370 RoomNetCfgLine *pNext, *pName;
372 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
373 pName= OneRNCfg->NetConfigs[eCfg];
374 while (pName != NULL)
379 pCfg->DeAllocator(pCfg, &pName);
383 DeleteGenericCfgLine(NULL, &pName);
388 DeleteHashPos(&CfgIt);
390 FreeStrBuf(&OneRNCfg->Sender);
391 FreeStrBuf(&OneRNCfg->RoomInfo);
392 FreeStrBuf(&OneRNCfg->misc);
393 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
395 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
397 OneRoomNetCfg *OneRNCfg;
398 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
399 FreeRoomNetworkStructContent(OneRNCfg);
402 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
404 vFreeRoomNetworkStruct(*pOneRNCfg);
408 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
411 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
412 return (OneRoomNetCfg*)pv;
416 void LoadAllNetConfigs(void)
420 struct dirent *filedir_entry;
424 OneRoomNetCfg *OneRNCfg;
429 RoomConfigs = NewHash(1, NULL);
430 filedir = opendir (ctdl_netcfg_dir);
431 if (filedir == NULL) {
432 return ; /// todo: panic!
435 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
441 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
442 (filedir_entry != NULL))
444 #ifdef _DIRENT_HAVE_D_NAMLEN
445 d_namelen = filedir_entry->d_namlen;
447 d_namelen = strlen(filedir_entry->d_name);
450 #ifdef _DIRENT_HAVE_D_TYPE
451 d_type = filedir_entry->d_type;
460 #define IFTODT(mode) (((mode) & 0170000) >> 12)
461 #define DTTOIF(dirtype) ((dirtype) << 12)
465 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
466 continue; /* Ignore backup files... */
468 if ((d_namelen == 1) &&
469 (filedir_entry->d_name[0] == '.'))
472 if ((d_namelen == 2) &&
473 (filedir_entry->d_name[0] == '.') &&
474 (filedir_entry->d_name[1] == '.'))
477 snprintf(path, PATH_MAX, "%s/%s",
478 ctdl_netcfg_dir, filedir_entry->d_name);
480 if (d_type == DT_UNKNOWN) {
482 if (lstat(path, &s) == 0) {
483 d_type = IFTODT(s.st_mode);
491 case DT_LNK: /* TODO: check whether its a file or a directory */
494 pch = filedir_entry->d_name;
506 RoomNumber = atol(filedir_entry->d_name);
507 ReadRoomNetConfigFile(&OneRNCfg, path);
509 if (OneRNCfg != NULL)
510 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
511 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
525 /*-----------------------------------------------------------------------------*
526 * Per room network configs : exchange with client *
527 *-----------------------------------------------------------------------------*/
528 void cmd_gnet(char *argbuf)
530 char filename[PATH_MAX];
535 if (!IsEmptyStr(argbuf))
537 if (CtdlAccessCheck(ac_aide)) return;
538 if (strcmp(argbuf, FILE_MAILALIAS))
540 cprintf("%d No such file or directory\n",
541 ERROR + INTERNAL_ERROR);
544 safestrncpy(filename, file_mail_aliases, sizeof(filename));
545 cprintf("%d Settings for <%s>\n",
551 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
552 /* users can edit the netconfigs for their own mailbox rooms */
554 else if (CtdlAccessCheck(ac_room_aide)) return;
556 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
557 cprintf("%d Network settings for room #%ld <%s>\n",
559 CC->room.QRnumber, CC->room.QRname);
562 fp = fopen(filename, "r");
564 while (fgets(buf, sizeof buf, fp) != NULL) {
565 buf[strlen(buf)-1] = 0;
566 cprintf("%s\n", buf);
574 #define nForceAliases 5
575 const ConstStr ForceAliases[nForceAliases] = {
579 {HKEY("postmaster,")},
582 void cmd_snet(char *argbuf)
584 struct CitContext *CCC = CC;
585 char tempfilename[PATH_MAX];
586 char filename[PATH_MAX];
593 int MailAliasesFound[nForceAliases];
597 if (!IsEmptyStr(argbuf))
599 if (CtdlAccessCheck(ac_aide)) return;
600 if (strcmp(argbuf, FILE_MAILALIAS))
602 cprintf("%d No such file or directory\n",
603 ERROR + INTERNAL_ERROR);
606 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
607 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
608 memcpy(tempfilename, filename, len + 1);
613 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
614 /* users can edit the netconfigs for their own mailbox rooms */
616 else if (CtdlAccessCheck(ac_room_aide)) return;
618 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
619 memcpy(tempfilename, filename, len + 1);
621 memset(&StatBuf, 0, sizeof(struct stat));
622 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
623 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
625 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
627 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
629 if ((TmpFD > 0) && (errno == 0))
631 char *tmp = malloc(StatBuf.st_size * 2);
632 memset(tmp, ' ', StatBuf.st_size * 2);
633 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
635 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
638 cprintf("%d Unable to allocate the space required for %s: %s\n",
639 ERROR + INTERNAL_ERROR,
642 unlink(tempfilename);
645 lseek(TmpFD, SEEK_SET, 0);
648 cprintf("%d Unable to allocate the space required for %s: %s\n",
649 ERROR + INTERNAL_ERROR,
652 unlink(tempfilename);
657 cprintf("%d %s\n", SEND_LISTING, tempfilename);
660 while (rc = CtdlClientGetLine(Line),
663 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
669 for (i = 0; i < nForceAliases; i++)
671 if ((!MailAliasesFound[i]) &&
672 (strncmp(ForceAliases[i].Key,
674 ForceAliases[i].len) == 0)
677 MailAliasesFound[i] = 1;
683 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
684 write(TmpFD, ChrPtr(Line), StrLength(Line));
685 len += StrLength(Line);
688 ftruncate(TmpFD, len);
695 * Sanity check whether all aliases required by the RFCs were set
699 for (i = 0; i < nForceAliases; i++)
701 if (!MailAliasesFound[i])
706 cprintf("%d won't do this - you're missing an RFC required alias.\n",
707 ERROR + INTERNAL_ERROR);
708 unlink(tempfilename);
713 /* Now copy the temp file to its permanent location.
714 * (We copy instead of link because they may be on different filesystems)
716 begin_critical_section(S_NETCONFIGS);
717 rename(tempfilename, filename);
720 OneRoomNetCfg *RNCfg;
721 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
724 ReadRoomNetConfigFile(&RNCfg, filename);
728 ReadRoomNetConfigFile(&RNCfg, filename);
729 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
732 PerformRoomHooks(&CCC->room);
734 end_critical_section(S_NETCONFIGS);
738 /*-----------------------------------------------------------------------------*
739 * Per node network configs *
740 *-----------------------------------------------------------------------------*/
741 void DeleteCtdlNodeConf(void *vNode)
743 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
744 FreeStrBuf(&Node->NodeName);
745 FreeStrBuf(&Node->Secret);
746 FreeStrBuf(&Node->Host);
747 FreeStrBuf(&Node->Port);
751 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
753 const char *Pos = NULL;
756 /* we need at least 4 pipes and some other text so its invalid. */
757 if (StrLength(SerializedNode) < 8)
759 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
763 Node->NodeName=NewStrBuf();
764 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
766 Node->Secret=NewStrBuf();
767 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
769 Node->Host=NewStrBuf();
770 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
772 Node->Port=NewStrBuf();
773 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
779 * Load or refresh the Citadel network (IGnet) configuration for this node.
781 HashList* CtdlLoadIgNetCfg(void)
790 Cfg = CtdlGetSysConfig(IGNETCFG);
791 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
797 Hash = NewHash(1, NULL);
798 Buf = NewStrBufPlain(Cfg, -1);
800 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
804 StrBufSipLine(LineBuf, Buf, &LinePos);
805 if (StrLength(LineBuf) != 0) {
806 Node = NewNode(LineBuf);
808 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
811 } while (LinePos != StrBufNOTNULL);
813 FreeStrBuf(&LineBuf);
818 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
820 const RoomNetCfg RecipientCfgs[] = {
827 RoomNetCfgLine *nptr;
832 while (RecipientCfgs[i] != maxRoomNetCfg)
834 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
838 if ((StrLength(nptr->Value[0]) == len) &&
839 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
852 int CtdlNetconfigCheckRoomaccess(
855 const char* RemoteIdentifier)
857 OneRoomNetCfg *RNCfg;
860 if (RemoteIdentifier == NULL)
862 snprintf(errmsgbuf, n, "Need sender to permit access.");
863 return (ERROR + USERNAME_REQUIRED);
866 begin_critical_section(S_NETCONFIGS);
867 RNCfg = CtdlGetNetCfgForRoom (CC->room.QRnumber);
870 end_critical_section(S_NETCONFIGS);
871 snprintf(errmsgbuf, n,
872 "This mailing list only accepts posts from subscribers.");
873 return (ERROR + NO_SUCH_USER);
875 found = is_recipient (RNCfg, RemoteIdentifier);
876 end_critical_section(S_NETCONFIGS);
882 snprintf(errmsgbuf, n,
883 "This mailing list only accepts posts from subscribers.");
884 return (ERROR + NO_SUCH_USER);
891 * cmd_netp() - authenticate to the server as another Citadel node polling
892 * for network traffic
894 void cmd_netp(char *cmdbuf)
896 struct CitContext *CCC = CC;
897 HashList *working_ignetcfg;
905 const StrBuf *secret = NULL;
906 const StrBuf *nexthop = NULL;
907 char err_buf[SIZ] = "";
910 node = CCC->curr_user;
911 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
912 NodeStr = NewStrBufPlain(node, nodelen);
913 /* load the IGnet Configuration to check node validity */
914 working_ignetcfg = CtdlLoadIgNetCfg();
915 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
917 snprintf(err_buf, sizeof err_buf,
918 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
919 node, CCC->cs_host, CCC->cs_addr
921 syslog(LOG_WARNING, "%s", err_buf);
922 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
924 strs[0] = CCC->cs_addr;
925 lens[0] = strlen(CCC->cs_addr);
927 strs[1] = "SRV_UNKNOWN";
928 lens[1] = sizeof("SRV_UNKNOWN" - 1);
933 2, strs, (long*) &lens,
937 DeleteHash(&working_ignetcfg);
938 FreeStrBuf(&NodeStr);
942 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
943 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
944 snprintf(err_buf, sizeof err_buf,
945 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
946 CCC->cs_host, CCC->cs_addr, node
948 syslog(LOG_WARNING, "%s", err_buf);
949 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
951 strs[0] = CCC->cs_addr;
952 lens[0] = strlen(CCC->cs_addr);
955 lens[1] = sizeof("SRV_PW" - 1);
965 DeleteHash(&working_ignetcfg);
966 FreeStrBuf(&NodeStr);
970 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
971 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
972 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
973 DeleteHash(&working_ignetcfg);
974 FreeStrBuf(&NodeStr);
977 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
978 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
979 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
980 CCC->net_node, CCC->cs_host, CCC->cs_addr
982 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
983 DeleteHash(&working_ignetcfg);
984 FreeStrBuf(&NodeStr);
988 /*-----------------------------------------------------------------------------*
989 * Network maps: evaluate other nodes *
990 *-----------------------------------------------------------------------------*/
992 void DeleteNetMap(void *vNetMap)
994 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
995 FreeStrBuf(&TheNetMap->NodeName);
996 FreeStrBuf(&TheNetMap->NextHop);
1000 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
1002 const char *Pos = NULL;
1005 /* we need at least 3 pipes and some other text so its invalid. */
1006 if (StrLength(SerializedNetMap) < 6)
1008 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
1010 NM->NodeName=NewStrBuf();
1011 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
1013 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
1015 NM->NextHop=NewStrBuf();
1016 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
1021 HashList* CtdlReadNetworkMap(void)
1023 const char *LinePos;
1028 CtdlNetMap *TheNetMap;
1030 Hash = NewHash(1, NULL);
1031 Cfg = CtdlGetSysConfig(IGNETMAP);
1032 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1038 Buf = NewStrBufPlain(Cfg, -1);
1040 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1042 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1044 TheNetMap = NewNetMap(LineBuf);
1045 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1046 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1050 FreeStrBuf(&LineBuf);
1054 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1059 StrBuf *Ret = NewStrBuf();
1060 HashPos *Pos = GetNewHashPos(Map, 0);
1062 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1064 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1065 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1066 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1068 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1069 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1071 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1072 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1074 DeleteHashPos(&Pos);
1080 * Learn topology from path fields
1082 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1084 CtdlNetMap *pNM = NULL;
1089 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1090 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1092 pNM = (CtdlNetMap*)vptr;
1093 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1094 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1095 pNM->lastcontact = time(NULL);
1096 (*netmap_changed) ++;
1101 /* If we got here then it's not in the map, so add it. */
1102 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1103 nmptr->NodeName = NewStrBufPlain(node, -1);
1104 nmptr->lastcontact = time(NULL);
1105 nmptr->NextHop = NewStrBuf ();
1106 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1107 /* TODO: is the NodeName Uniq? */
1108 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1109 (*netmap_changed) ++;
1114 * Check the network map and determine whether the supplied node name is
1115 * valid. If it is not a neighbor node, supply the name of a neighbor node
1116 * which is the next hop. If it *is* a neighbor node, we also fill in the
1119 int CtdlIsValidNode(const StrBuf **nexthop,
1120 const StrBuf **secret,
1123 HashList *the_netmap)
1127 CtdlNodeConf *TheNode;
1128 CtdlNetMap *TheNetMap;
1130 if (StrLength(node) == 0) {
1135 * First try the neighbor nodes
1137 if (GetCount(IgnetCfg) == 0) {
1138 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1139 if (nexthop != NULL) {
1145 /* try to find a neigbour with the name 'node' */
1146 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1147 (vNodeConf != NULL))
1149 TheNode = (CtdlNodeConf*)vNodeConf;
1151 *secret = TheNode->Secret;
1152 return 0; /* yup, it's a direct neighbor */
1156 * If we get to this point we have to see if we know the next hop
1157 *//* TODO: is the NodeName Uniq? */
1158 if ((GetCount(the_netmap) > 0) &&
1159 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1161 TheNetMap = (CtdlNetMap*)vNetMap;
1162 if (nexthop != NULL)
1163 *nexthop = TheNetMap->NextHop;
1168 * If we get to this point, the supplied node name is bogus.
1170 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1175 void destroy_network_cfgs(void)
1177 HashList *pCfgTypeHash = CfgTypeHash;
1178 HashList *pRoomConfigs;
1180 begin_critical_section(S_NETCONFIGS);
1181 pRoomConfigs = RoomConfigs;
1183 end_critical_section(S_NETCONFIGS);
1184 DeleteHash(&pRoomConfigs);
1187 DeleteHash(&pCfgTypeHash);
1191 * Module entry point
1193 CTDL_MODULE_INIT(netconfig)
1197 CtdlRegisterCleanupHook(destroy_network_cfgs);
1198 LoadAllNetConfigs ();
1199 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1200 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1201 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");