2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2012 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
23 # if HAVE_SYS_SYSCALL_H
24 # include <sys/syscall.h>
29 #include <libcitadel.h>
31 #include "include/ctdl_module.h"
34 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg);
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)
149 RoomNetCfgLine *NewData;
151 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
153 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
155 for (i = 0; i < data->nValues; i++)
157 NewData->Value[i] = NewStrBufDup(data->Value[i]);
161 int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
164 const char *ErrStr = NULL;
166 const CfgLineType *pCfg;
169 OneRoomNetCfg *OneRNCfg = NULL;
171 fd = open(filename, O_NONBLOCK|O_RDONLY);
176 if (*pOneRNCfg != NULL)
178 OneRNCfg = *pOneRNCfg;
179 FreeRoomNetworkStructContent (OneRNCfg);
182 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
183 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
184 *pOneRNCfg = OneRNCfg;
188 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
189 if (StrLength(Line) == 0)
192 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
194 pCfg = GetCfgTypeByStr(SKEY(InStr));
197 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
201 if (OneRNCfg->misc == NULL)
203 OneRNCfg->misc = NewStrBufDup(Line);
207 if(StrLength(OneRNCfg->misc) > 0)
208 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
209 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
220 int SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, char *filename)
224 StrBuf *OutBuffer = NULL;
225 char tempfilename[PATH_MAX];
230 long reltid; /* if we don't have SYS_gettid, use "random" value */
234 len = strlen(filename);
235 memcpy(tempfilename, filename, len + 1);
237 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
238 reltid = syscall(SYS_gettid);
240 gettimeofday(&tv, NULL);
241 /* Promote to time_t; types differ on some OSes (like darwin) */
242 unixtime = tv.tv_sec;
244 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
246 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
248 if ((TmpFD < 0) || (errno != 0)) {
249 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
250 filename, strerror(errno));
251 unlink(tempfilename);
256 OutBuffer = NewStrBuf();
257 CfgIt = GetNewHashPos(CfgTypeHash, 1);
258 fchown(TmpFD, config.c_ctdluid, 0);
259 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
261 const CfgLineType *pCfg;
262 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
263 if (pCfg->IsSingleLine)
265 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
269 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
270 while (pName != NULL)
272 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
280 DeleteHashPos(&CfgIt);
283 if (OneRNCfg->misc != NULL) {
284 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
287 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
288 if ((rc >=0 ) && (rc == StrLength(OutBuffer)))
291 rename(tempfilename, filename);
296 "unable to write %s; [%s]; not enough space on the disk?\n",
300 unlink(tempfilename);
303 FreeStrBuf(&OutBuffer);
310 void SaveModifiedRooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
312 char filename[PATH_MAX];
314 if (OneRNCfg->changed)
316 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
317 SaveRoomNetConfigFile(OneRNCfg, filename);
318 OneRNCfg->changed = 0;
321 void SaveChangedConfigs(void)
323 CtdlForEachNetCfgRoom(SaveModifiedRooms,
329 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
332 RoomNetCfgLine **pLine;
333 char filename[PATH_MAX];
335 if (OneRNCfg == NULL)
338 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
339 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
341 pLine = &OneRNCfg->NetConfigs[LineType];
343 while(*pLine != NULL) pLine = &((*pLine)->next);
346 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
347 SaveRoomNetConfigFile(OneRNCfg, filename);
348 OneRNCfg->changed = 0;
351 Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
355 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
360 CfgIt = GetNewHashPos(CfgTypeHash, 1);
361 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
363 const CfgLineType *pCfg;
364 RoomNetCfgLine *pNext, *pName;
366 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
367 pName= OneRNCfg->NetConfigs[eCfg];
368 while (pName != NULL)
373 pCfg->DeAllocator(pCfg, &pName);
377 DeleteGenericCfgLine(NULL, &pName);
382 DeleteHashPos(&CfgIt);
384 FreeStrBuf(&OneRNCfg->Sender);
385 FreeStrBuf(&OneRNCfg->RoomInfo);
386 FreeStrBuf(&OneRNCfg->misc);
388 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
390 OneRoomNetCfg *OneRNCfg;
391 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
392 FreeRoomNetworkStructContent(OneRNCfg);
395 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
397 vFreeRoomNetworkStruct(*pOneRNCfg);
401 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber)
404 GetHash(RoomConfigs, LKEY(QRNumber), &pv);
405 return (OneRoomNetCfg*)pv;
409 void LoadAllNetConfigs(void)
413 struct dirent *filedir_entry;
417 OneRoomNetCfg *OneRNCfg;
422 RoomConfigs = NewHash(1, NULL);
423 filedir = opendir (ctdl_netcfg_dir);
424 if (filedir == NULL) {
425 return ; /// todo: panic!
428 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
434 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
435 (filedir_entry != NULL))
437 #ifdef _DIRENT_HAVE_D_NAMLEN
438 d_namelen = filedir_entry->d_namelen;
440 d_namelen = strlen(filedir_entry->d_name);
443 #ifdef _DIRENT_HAVE_D_TYPE
444 d_type = filedir_entry->d_type;
453 #define IFTODT(mode) (((mode) & 0170000) >> 12)
454 #define DTTOIF(dirtype) ((dirtype) << 12)
458 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
459 continue; /* Ignore backup files... */
461 if ((d_namelen == 1) &&
462 (filedir_entry->d_name[0] == '.'))
465 if ((d_namelen == 2) &&
466 (filedir_entry->d_name[0] == '.') &&
467 (filedir_entry->d_name[1] == '.'))
470 snprintf(path, PATH_MAX, "%s/%s",
471 ctdl_netcfg_dir, filedir_entry->d_name);
473 if (d_type == DT_UNKNOWN) {
475 if (lstat(path, &s) == 0) {
476 d_type = IFTODT(s.st_mode);
484 case DT_LNK: /* TODO: check whether its a file or a directory */
487 pch = filedir_entry->d_name;
499 RoomNumber = atol(filedir_entry->d_name);
500 ReadRoomNetConfigFile(&OneRNCfg, path);
502 if (OneRNCfg != NULL)
503 Put(RoomConfigs, LKEY(RoomNumber), OneRNCfg, vFreeRoomNetworkStruct);
504 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
518 /*-----------------------------------------------------------------------------*
519 * Per room network configs : exchange with client *
520 *-----------------------------------------------------------------------------*/
521 void cmd_gnet(char *argbuf)
523 char filename[PATH_MAX];
528 if (!IsEmptyStr(argbuf))
530 if (CtdlAccessCheck(ac_aide)) return;
531 if (strcmp(argbuf, FILE_MAILALIAS))
533 cprintf("%d No such file or directory\n",
534 ERROR + INTERNAL_ERROR);
537 safestrncpy(filename, file_mail_aliases, sizeof(filename));
538 cprintf("%d Settings for <%s>\n",
544 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
545 /* users can edit the netconfigs for their own mailbox rooms */
547 else if (CtdlAccessCheck(ac_room_aide)) return;
549 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
550 cprintf("%d Network settings for room #%ld <%s>\n",
552 CC->room.QRnumber, CC->room.QRname);
555 fp = fopen(filename, "r");
557 while (fgets(buf, sizeof buf, fp) != NULL) {
558 buf[strlen(buf)-1] = 0;
559 cprintf("%s\n", buf);
567 #define nForceAliases 5
568 const ConstStr ForceAliases[nForceAliases] = {
572 {HKEY("postmaster,")},
576 void cmd_snet(char *argbuf)
578 struct CitContext *CCC = CC;
579 char tempfilename[PATH_MAX];
580 char filename[PATH_MAX];
587 int MailAliasesFound[nForceAliases];
591 if (!IsEmptyStr(argbuf))
593 if (CtdlAccessCheck(ac_aide)) return;
594 if (strcmp(argbuf, FILE_MAILALIAS))
596 cprintf("%d No such file or directory\n",
597 ERROR + INTERNAL_ERROR);
600 len = safestrncpy(filename, file_mail_aliases, sizeof(filename));
601 memset(MailAliasesFound, 0, sizeof(MailAliasesFound));
602 memcpy(tempfilename, filename, len + 1);
607 if ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->user.usernum == atol(CCC->room.QRname)) ) {
608 /* users can edit the netconfigs for their own mailbox rooms */
610 else if (CtdlAccessCheck(ac_room_aide)) return;
612 len = assoc_file_name(filename, sizeof filename, &CCC->room, ctdl_netcfg_dir);
613 memcpy(tempfilename, filename, len + 1);
615 memset(&StatBuf, 0, sizeof(struct stat));
616 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
617 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
619 sprintf(tempfilename + len, ".%d", CCC->cs_pid);
621 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
623 if ((TmpFD > 0) && (errno == 0))
625 char *tmp = malloc(StatBuf.st_size * 2);
626 memset(tmp, ' ', StatBuf.st_size * 2);
627 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
629 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
632 cprintf("%d Unable to allocate the space required for %s: %s\n",
633 ERROR + INTERNAL_ERROR,
636 unlink(tempfilename);
639 lseek(TmpFD, SEEK_SET, 0);
642 cprintf("%d Unable to allocate the space required for %s: %s\n",
643 ERROR + INTERNAL_ERROR,
646 unlink(tempfilename);
651 cprintf("%d %s\n", SEND_LISTING, tempfilename);
654 while (rc = CtdlClientGetLine(Line),
657 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
663 for (i = 0; i < nForceAliases; i++)
665 if ((!MailAliasesFound[i]) &&
666 (strncmp(ForceAliases[i].Key,
668 ForceAliases[i].len) == 0)
671 MailAliasesFound[i] = 1;
677 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
678 write(TmpFD, ChrPtr(Line), StrLength(Line));
679 len += StrLength(Line);
682 ftruncate(TmpFD, len);
689 * Sanity check whether all aliases required by the RFCs were set
693 for (i = 0; i < nForceAliases; i++)
695 if (!MailAliasesFound[i])
700 cprintf("%d won't do this - you're missing an RFC required alias.\n",
701 ERROR + INTERNAL_ERROR);
702 unlink(tempfilename);
707 /* Now copy the temp file to its permanent location.
708 * (We copy instead of link because they may be on different filesystems)
710 begin_critical_section(S_NETCONFIGS);
711 rename(tempfilename, filename);
714 OneRoomNetCfg *RNCfg;
715 RNCfg = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
718 FreeRoomNetworkStructContent(RNCfg);
719 ReadRoomNetConfigFile(&RNCfg, filename);
723 ReadRoomNetConfigFile(&RNCfg, filename);
724 Put(RoomConfigs, LKEY(CCC->room.QRnumber), RNCfg, vFreeRoomNetworkStruct);
727 end_critical_section(S_NETCONFIGS);
731 /*-----------------------------------------------------------------------------*
732 * Per node network configs *
733 *-----------------------------------------------------------------------------*/
734 void DeleteCtdlNodeConf(void *vNode)
736 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
737 FreeStrBuf(&Node->NodeName);
738 FreeStrBuf(&Node->Secret);
739 FreeStrBuf(&Node->Host);
740 FreeStrBuf(&Node->Port);
744 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
746 const char *Pos = NULL;
749 /* we need at least 4 pipes and some other text so its invalid. */
750 if (StrLength(SerializedNode) < 8)
752 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
756 Node->NodeName=NewStrBuf();
757 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
759 Node->Secret=NewStrBuf();
760 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
762 Node->Host=NewStrBuf();
763 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
765 Node->Port=NewStrBuf();
766 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
772 * Load or refresh the Citadel network (IGnet) configuration for this node.
774 HashList* CtdlLoadIgNetCfg(void)
783 Cfg = CtdlGetSysConfig(IGNETCFG);
784 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
790 Hash = NewHash(1, NULL);
791 Buf = NewStrBufPlain(Cfg, -1);
793 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
797 StrBufSipLine(LineBuf, Buf, &LinePos);
798 if (StrLength(LineBuf) != 0) {
799 Node = NewNode(LineBuf);
801 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
804 } while (LinePos != StrBufNOTNULL);
806 FreeStrBuf(&LineBuf);
811 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
813 const RoomNetCfg RecipientCfgs[] = {
820 RoomNetCfgLine *nptr;
825 while (RecipientCfgs[i] != maxRoomNetCfg)
827 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
831 if ((StrLength(nptr->Value[0]) == len) &&
832 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
844 int CtdlNetconfigCheckRoomaccess(
847 const char* RemoteIdentifier)
849 OneRoomNetCfg *RNCfg;
852 if (RemoteIdentifier == NULL)
854 snprintf(errmsgbuf, n, "Need sender to permit access.");
855 return (ERROR + USERNAME_REQUIRED);
858 begin_critical_section(S_NETCONFIGS);
859 RNCfg = CtdlGetNetCfgForRoom (CC->room.QRnumber);
862 end_critical_section(S_NETCONFIGS);
863 snprintf(errmsgbuf, n,
864 "This mailing list only accepts posts from subscribers.");
865 return (ERROR + NO_SUCH_USER);
867 found = is_recipient (RNCfg, RemoteIdentifier);
868 end_critical_section(S_NETCONFIGS);
874 snprintf(errmsgbuf, n,
875 "This mailing list only accepts posts from subscribers.");
876 return (ERROR + NO_SUCH_USER);
883 * cmd_netp() - authenticate to the server as another Citadel node polling
884 * for network traffic
886 void cmd_netp(char *cmdbuf)
888 struct CitContext *CCC = CC;
889 HashList *working_ignetcfg;
897 const StrBuf *secret = NULL;
898 const StrBuf *nexthop = NULL;
899 char err_buf[SIZ] = "";
902 node = CCC->curr_user;
903 nodelen = extract_token(CCC->curr_user, cmdbuf, 0, '|', sizeof CCC->curr_user);
904 NodeStr = NewStrBufPlain(node, nodelen);
905 /* load the IGnet Configuration to check node validity */
906 working_ignetcfg = CtdlLoadIgNetCfg();
907 v = CtdlIsValidNode(&nexthop, &secret, NodeStr, working_ignetcfg, NULL);
909 snprintf(err_buf, sizeof err_buf,
910 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
911 node, CCC->cs_host, CCC->cs_addr
913 syslog(LOG_WARNING, "%s", err_buf);
914 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
916 strs[0] = CCC->cs_addr;
917 lens[0] = strlen(CCC->cs_addr);
919 strs[1] = "SRV_UNKNOWN";
920 lens[1] = sizeof("SRV_UNKNOWN" - 1);
925 2, strs, (long*) &lens);
927 DeleteHash(&working_ignetcfg);
928 FreeStrBuf(&NodeStr);
932 extract_token(CCC->user.password, cmdbuf, 1, '|', sizeof CCC->user.password);
933 if (strcasecmp(CCC->user.password, ChrPtr(secret))) {
934 snprintf(err_buf, sizeof err_buf,
935 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
936 CCC->cs_host, CCC->cs_addr, node
938 syslog(LOG_WARNING, "%s", err_buf);
939 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
941 strs[0] = CCC->cs_addr;
942 lens[0] = strlen(CCC->cs_addr);
945 lens[1] = sizeof("SRV_PW" - 1);
950 2, strs, (long*) &lens);
952 DeleteHash(&working_ignetcfg);
953 FreeStrBuf(&NodeStr);
957 if (CtdlNetworkTalkingTo(node, nodelen, NTT_CHECK)) {
958 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
959 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
960 DeleteHash(&working_ignetcfg);
961 FreeStrBuf(&NodeStr);
964 nodelen = safestrncpy(CCC->net_node, node, sizeof CCC->net_node);
965 CtdlNetworkTalkingTo(CCC->net_node, nodelen, NTT_ADD);
966 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
967 CCC->net_node, CCC->cs_host, CCC->cs_addr
969 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CCC->net_node);
970 DeleteHash(&working_ignetcfg);
971 FreeStrBuf(&NodeStr);
975 /*-----------------------------------------------------------------------------*
976 * Network maps: evaluate other nodes *
977 *-----------------------------------------------------------------------------*/
979 void DeleteNetMap(void *vNetMap)
981 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
982 FreeStrBuf(&TheNetMap->NodeName);
983 FreeStrBuf(&TheNetMap->NextHop);
987 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
989 const char *Pos = NULL;
992 /* we need at least 3 pipes and some other text so its invalid. */
993 if (StrLength(SerializedNetMap) < 6)
995 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
997 NM->NodeName=NewStrBuf();
998 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
1000 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
1002 NM->NextHop=NewStrBuf();
1003 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
1008 HashList* CtdlReadNetworkMap(void)
1010 const char *LinePos;
1015 CtdlNetMap *TheNetMap;
1017 Hash = NewHash(1, NULL);
1018 Cfg = CtdlGetSysConfig(IGNETMAP);
1019 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
1025 Buf = NewStrBufPlain(Cfg, -1);
1027 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
1029 while (StrBufSipLine(Buf, LineBuf, &LinePos))
1031 TheNetMap = NewNetMap(LineBuf);
1032 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
1033 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
1037 FreeStrBuf(&LineBuf);
1041 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
1046 StrBuf *Ret = NewStrBuf();
1047 HashPos *Pos = GetNewHashPos(Map, 0);
1049 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
1051 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
1052 StrBufAppendBuf(Ret, pMap->NodeName, 0);
1053 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1055 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
1056 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
1058 StrBufAppendBuf(Ret, pMap->NextHop, 0);
1059 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
1061 DeleteHashPos(&Pos);
1067 * Learn topology from path fields
1069 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed)
1071 CtdlNetMap *pNM = NULL;
1076 if (GetHash(the_netmap, node, strlen(node), &vptr) &&
1077 (vptr != NULL))/* TODO: is the NodeName Uniq? */
1079 pNM = (CtdlNetMap*)vptr;
1080 extract_token(nexthop, path, 0, '!', sizeof nexthop);
1081 if (!strcmp(nexthop, ChrPtr(pNM->NextHop))) {
1082 pNM->lastcontact = time(NULL);
1083 (*netmap_changed) ++;
1088 /* If we got here then it's not in the map, so add it. */
1089 nmptr = (CtdlNetMap *) malloc(sizeof (CtdlNetMap));
1090 nmptr->NodeName = NewStrBufPlain(node, -1);
1091 nmptr->lastcontact = time(NULL);
1092 nmptr->NextHop = NewStrBuf ();
1093 StrBufExtract_tokenFromStr(nmptr->NextHop, path, strlen(path), 0, '!');
1094 /* TODO: is the NodeName Uniq? */
1095 Put(the_netmap, SKEY(nmptr->NodeName), nmptr, DeleteNetMap);
1096 (*netmap_changed) ++;
1101 * Check the network map and determine whether the supplied node name is
1102 * valid. If it is not a neighbor node, supply the name of a neighbor node
1103 * which is the next hop. If it *is* a neighbor node, we also fill in the
1106 int CtdlIsValidNode(const StrBuf **nexthop,
1107 const StrBuf **secret,
1110 HashList *the_netmap)
1114 CtdlNodeConf *TheNode;
1115 CtdlNetMap *TheNetMap;
1117 if (StrLength(node) == 0) {
1122 * First try the neighbor nodes
1124 if (GetCount(IgnetCfg) == 0) {
1125 syslog(LOG_INFO, "IgnetCfg is empty!\n");
1126 if (nexthop != NULL) {
1132 /* try to find a neigbour with the name 'node' */
1133 if (GetHash(IgnetCfg, SKEY(node), &vNodeConf) &&
1134 (vNodeConf != NULL))
1136 TheNode = (CtdlNodeConf*)vNodeConf;
1138 *secret = TheNode->Secret;
1139 return 0; /* yup, it's a direct neighbor */
1143 * If we get to this point we have to see if we know the next hop
1144 *//* TODO: is the NodeName Uniq? */
1145 if ((GetCount(the_netmap) > 0) &&
1146 (GetHash(the_netmap, SKEY(node), &vNetMap)))
1148 TheNetMap = (CtdlNetMap*)vNetMap;
1149 if (nexthop != NULL)
1150 *nexthop = TheNetMap->NextHop;
1155 * If we get to this point, the supplied node name is bogus.
1157 syslog(LOG_ERR, "Invalid node name <%s>\n", ChrPtr(node));
1162 void destroy_network_cfgs(void)
1164 HashList *pCfgTypeHash = CfgTypeHash;
1165 HashList *pRoomConfigs = RoomConfigs;
1170 DeleteHash(&pCfgTypeHash);
1171 DeleteHash(&pRoomConfigs);
1175 * Module entry point
1177 CTDL_MODULE_INIT(netconfig)
1181 CtdlRegisterCleanupHook(destroy_network_cfgs);
1182 LoadAllNetConfigs ();
1183 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1184 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1185 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");