2 * This module handles loading, saving, and parsing of room network configurations.
4 * Copyright (c) 2000-2018 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <sys/types.h>
24 # if HAVE_SYS_SYSCALL_H
25 # 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;
42 /*-----------------------------------------------------------------------------*
43 * Per room network configs *
44 *-----------------------------------------------------------------------------*/
47 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d)
51 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
54 pCfg->DeAllocator = d;
58 pCfg->IsSingleLine = uniq;
59 pCfg->nSegments = nSegments;
60 if (CfgTypeHash == NULL) {
61 CfgTypeHash = NewHash(1, NULL);
63 Put(CfgTypeHash, Name, len, pCfg, NULL);
67 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
71 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
73 return (const CfgLineType *) pv;
82 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
89 RewindHashPos(CfgTypeHash, It, 1);
90 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
92 pCfg = (CfgLineType*) pv;
100 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCfg)
102 RoomNetCfgLine *nptr;
105 nptr = (RoomNetCfgLine *)
106 malloc(sizeof(RoomNetCfgLine));
107 nptr->next = OneRNCfg->NetConfigs[ThisOne->C];
108 nptr->Value = malloc(sizeof(StrBuf*) * ThisOne->nSegments);
110 memset(nptr->Value, 0, sizeof(StrBuf*) * ThisOne->nSegments);
111 if (ThisOne->nSegments == 1)
113 nptr->Value[0] = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
116 else for (i = 0; i < ThisOne->nSegments; i++)
119 nptr->Value[i] = NewStrBufPlain(NULL, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
120 StrBufExtract_NextToken(nptr->Value[i], Line, &LinePos, '|');
123 OneRNCfg->NetConfigs[ThisOne->C] = nptr;
127 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *OneRNCfg, RoomNetCfgLine *data)
131 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
132 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
133 for (i = 0; i < ThisOne->nSegments; i++)
135 StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
136 if (i + 1 < ThisOne->nSegments)
137 StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
139 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
143 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
150 for (i = 0; i < (*data)->nValues; i++)
152 FreeStrBuf(&(*data)->Value[i]);
154 free ((*data)->Value);
160 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data)
163 RoomNetCfgLine *NewData;
165 NewData = (RoomNetCfgLine*)malloc(sizeof(RoomNetCfgLine));
166 memset(NewData, 0, sizeof(RoomNetCfgLine));
167 NewData->Value = (StrBuf **)malloc(sizeof(StrBuf*) * data->nValues);
168 memset(NewData->Value, 0, sizeof(StrBuf*) * data->nValues);
170 for (i = 0; i < data->nValues; i++)
172 NewData->Value[i] = NewStrBufDup(data->Value[i]);
174 NewData->nValues = data->nValues;
180 * Create a config key for a room's netconfig entry
182 void netcfg_keyname(char *keybuf, long roomnum)
185 sprintf(keybuf, "c_netconfig_%010ld", roomnum);
190 * Given a room number and a textual netconfig, convert to base64 and write to the configdb
192 void write_netconfig_to_configdb(long roomnum, const char *raw_netconfig)
199 len = strlen(raw_netconfig);
200 netcfg_keyname(keyname, roomnum);
201 enc = malloc(len * 2);
204 enc_len = CtdlEncodeBase64(enc, raw_netconfig, len, 0);
205 if ((enc_len > 1) && (enc[enc_len-2] == 13)) enc[enc_len-2] = 0;
206 if ((enc_len > 0) && (enc[enc_len-1] == 10)) enc[enc_len-1] = 0;
208 syslog(LOG_DEBUG, "netconfig: writing key '%s' (length=%d)", keyname, enc_len);
209 CtdlSetConfigStr(keyname, enc);
216 * Given a room number, attempt to load the netconfig configdb entry for that room.
217 * If it returns NULL, there is no netconfig.
218 * Otherwise the caller owns the returned memory and is responsible for freeing it.
220 char *LoadRoomNetConfigFile(long roomnum)
223 char *encoded_netconfig = NULL;
224 char *decoded_netconfig = NULL;
226 netcfg_keyname(keyname, roomnum);
227 encoded_netconfig = CtdlGetConfigStr(keyname);
228 if (!encoded_netconfig) return NULL;
230 decoded_netconfig = malloc(strlen(encoded_netconfig)); // yeah, way bigger than it needs to be, but safe
231 CtdlDecodeBase64(decoded_netconfig, encoded_netconfig, strlen(encoded_netconfig));
232 return decoded_netconfig;
237 * Deserialize a netconfig , allocate and return structured data
239 OneRoomNetCfg *ParseRoomNetConfigFile(char *serialized_data)
241 const char *Pos = NULL;
242 const CfgLineType *pCfg = NULL;
244 StrBuf *InStr = NULL;
246 OneRoomNetCfg *OneRNCfg = NULL;
250 OneRNCfg = malloc(sizeof(OneRoomNetCfg));
251 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
255 Cfg = NewStrBufPlain(serialized_data, -1);
256 num_lines = num_tokens(ChrPtr(Cfg), '\n');
258 for (i=0; i<num_lines; ++i) {
259 StrBufExtract_token(Line, Cfg, i, '\n');
260 if (StrLength(Line) > 0) {
262 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
264 pCfg = GetCfgTypeByStr(SKEY(InStr));
267 pCfg->Parser(pCfg, Line, Pos, OneRNCfg);
271 if (OneRNCfg->misc == NULL)
273 OneRNCfg->misc = NewStrBufDup(Line);
277 if (StrLength(OneRNCfg->misc) > 0) {
278 StrBufAppendBufPlain(OneRNCfg->misc, HKEY("\n"), 0);
280 StrBufAppendBuf(OneRNCfg->misc, Line, 0);
292 void SaveRoomNetConfigFile(OneRoomNetCfg *OneRNCfg, long roomnum)
296 StrBuf *OutBuffer = NULL;
300 OutBuffer = NewStrBuf();
301 CfgIt = GetNewHashPos(CfgTypeHash, 1);
302 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
304 const CfgLineType *pCfg;
305 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
308 if (pCfg->IsSingleLine)
310 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, NULL);
314 RoomNetCfgLine *pName = OneRNCfg->NetConfigs[pCfg->C];
315 while (pName != NULL)
317 pCfg->Serializer(pCfg, OutBuffer, OneRNCfg, pName);
324 DeleteHashPos(&CfgIt);
326 if (OneRNCfg->misc != NULL) {
327 StrBufAppendBuf(OutBuffer, OneRNCfg->misc, 0);
330 write_netconfig_to_configdb(roomnum, ChrPtr(OutBuffer));
332 FreeStrBuf(&OutBuffer);
337 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
339 RoomNetCfgLine **pLine;
341 if (OneRNCfg == NULL)
343 OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
344 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
346 pLine = &OneRNCfg->NetConfigs[LineType];
348 while(*pLine != NULL) pLine = &((*pLine)->next);
353 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
358 CfgIt = GetNewHashPos(CfgTypeHash, 1);
359 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
361 const CfgLineType *pCfg;
362 RoomNetCfgLine *pNext, *pName;
364 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
365 pName= OneRNCfg->NetConfigs[eCfg];
366 while (pName != NULL)
371 pCfg->DeAllocator(pCfg, &pName);
375 DeleteGenericCfgLine(NULL, &pName);
380 DeleteHashPos(&CfgIt);
382 FreeStrBuf(&OneRNCfg->Sender);
383 FreeStrBuf(&OneRNCfg->RoomInfo);
384 FreeStrBuf(&OneRNCfg->misc);
385 memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
389 void vFreeRoomNetworkStruct(void *vOneRoomNetCfg)
391 OneRoomNetCfg *OneRNCfg;
392 OneRNCfg = (OneRoomNetCfg*)vOneRoomNetCfg;
393 FreeRoomNetworkStructContent(OneRNCfg);
398 void FreeRoomNetworkStruct(OneRoomNetCfg **pOneRNCfg)
400 vFreeRoomNetworkStruct(*pOneRNCfg);
406 * Fetch the netconfig entry for a room, parse it, and return the data.
407 * Caller owns the returned memory and MUST free it using FreeRoomNetworkStruct()
409 OneRoomNetCfg *CtdlGetNetCfgForRoom(long roomnum)
411 OneRoomNetCfg *OneRNCfg = NULL;
412 char *serialized_config = NULL;
414 serialized_config = LoadRoomNetConfigFile(roomnum);
415 if (!serialized_config) return NULL;
417 OneRNCfg = ParseRoomNetConfigFile(serialized_config);
418 free(serialized_config);
423 /*-----------------------------------------------------------------------------*
424 * Per room network configs : exchange with client *
425 *-----------------------------------------------------------------------------*/
427 void cmd_gnet(char *argbuf)
429 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
430 /* users can edit the netconfigs for their own mailbox rooms */
432 else if (CtdlAccessCheck(ac_room_aide)) return;
434 cprintf("%d Network settings for room #%ld <%s>\n", LISTING_FOLLOWS, CC->room.QRnumber, CC->room.QRname);
436 char *c = LoadRoomNetConfigFile(CC->room.QRnumber);
439 client_write(c, len); // Can't use cprintf() here, it has a limit of 1024 bytes
440 if (c[len] != '\n') {
441 client_write(HKEY("\n"));
449 void cmd_snet(char *argbuf)
451 struct CitContext *CCC = CC;
453 StrBuf *TheConfig = NULL;
458 TheConfig = NewStrBuf();
459 cprintf("%d send new netconfig now\n", SEND_LISTING);
461 while (rc = CtdlClientGetLine(Line), (rc >= 0))
463 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
466 StrBufAppendBuf(TheConfig, Line, 0);
467 StrBufAppendBufPlain(TheConfig, HKEY("\n"), 0);
471 write_netconfig_to_configdb(CCC->room.QRnumber, ChrPtr(TheConfig));
472 FreeStrBuf(&TheConfig);
476 /*-----------------------------------------------------------------------------*
477 * Per node network configs *
478 *-----------------------------------------------------------------------------*/
479 void DeleteCtdlNodeConf(void *vNode)
481 CtdlNodeConf *Node = (CtdlNodeConf*) vNode;
482 FreeStrBuf(&Node->NodeName);
483 FreeStrBuf(&Node->Secret);
484 FreeStrBuf(&Node->Host);
485 FreeStrBuf(&Node->Port);
490 CtdlNodeConf *NewNode(StrBuf *SerializedNode)
492 const char *Pos = NULL;
495 /* we need at least 4 pipes and some other text so its invalid. */
496 if (StrLength(SerializedNode) < 8)
498 Node = (CtdlNodeConf *) malloc(sizeof(CtdlNodeConf));
502 Node->NodeName=NewStrBuf();
503 StrBufExtract_NextToken(Node->NodeName, SerializedNode, &Pos, '|');
505 Node->Secret=NewStrBuf();
506 StrBufExtract_NextToken(Node->Secret, SerializedNode, &Pos, '|');
508 Node->Host=NewStrBuf();
509 StrBufExtract_NextToken(Node->Host, SerializedNode, &Pos, '|');
511 Node->Port=NewStrBuf();
512 StrBufExtract_NextToken(Node->Port, SerializedNode, &Pos, '|');
518 * Load or refresh the Citadel network (IGnet) configuration for this node.
520 HashList* CtdlLoadIgNetCfg(void)
529 Cfg = CtdlGetSysConfig(IGNETCFG);
530 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
536 Hash = NewHash(1, NULL);
537 Buf = NewStrBufPlain(Cfg, -1);
539 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
543 StrBufSipLine(LineBuf, Buf, &LinePos);
544 if (StrLength(LineBuf) != 0) {
545 Node = NewNode(LineBuf);
547 Put(Hash, SKEY(Node->NodeName), Node, DeleteCtdlNodeConf);
550 } while (LinePos != StrBufNOTNULL);
552 FreeStrBuf(&LineBuf);
557 int is_recipient(OneRoomNetCfg *RNCfg, const char *Name)
559 const RoomNetCfg RecipientCfgs[] = {
566 RoomNetCfgLine *nptr;
571 while (RecipientCfgs[i] != maxRoomNetCfg)
573 nptr = RNCfg->NetConfigs[RecipientCfgs[i]];
577 if ((StrLength(nptr->Value[0]) == len) &&
578 (!strcmp(Name, ChrPtr(nptr->Value[0]))))
590 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, size_t n, const char* RemoteIdentifier)
592 OneRoomNetCfg *RNCfg;
595 if (RemoteIdentifier == NULL)
597 snprintf(errmsgbuf, n, "Need sender to permit access.");
598 return (ERROR + USERNAME_REQUIRED);
601 begin_critical_section(S_NETCONFIGS);
602 RNCfg = CtdlGetNetCfgForRoom (CC->room.QRnumber);
605 end_critical_section(S_NETCONFIGS);
606 snprintf(errmsgbuf, n,
607 "This mailing list only accepts posts from subscribers.");
608 return (ERROR + NO_SUCH_USER);
610 found = is_recipient (RNCfg, RemoteIdentifier);
611 FreeRoomNetworkStruct(&RNCfg);
612 end_critical_section(S_NETCONFIGS);
618 snprintf(errmsgbuf, n,
619 "This mailing list only accepts posts from subscribers.");
620 return (ERROR + NO_SUCH_USER);
625 /*-----------------------------------------------------------------------------*
626 * Network maps: evaluate other nodes *
627 *-----------------------------------------------------------------------------*/
629 void DeleteNetMap(void *vNetMap)
631 CtdlNetMap *TheNetMap = (CtdlNetMap*) vNetMap;
632 FreeStrBuf(&TheNetMap->NodeName);
633 FreeStrBuf(&TheNetMap->NextHop);
638 CtdlNetMap *NewNetMap(StrBuf *SerializedNetMap)
640 const char *Pos = NULL;
643 /* we need at least 3 pipes and some other text so its invalid. */
644 if (StrLength(SerializedNetMap) < 6)
646 NM = (CtdlNetMap *) malloc(sizeof(CtdlNetMap));
648 NM->NodeName=NewStrBuf();
649 StrBufExtract_NextToken(NM->NodeName, SerializedNetMap, &Pos, '|');
651 NM->lastcontact = StrBufExtractNext_long(SerializedNetMap, &Pos, '|');
653 NM->NextHop=NewStrBuf();
654 StrBufExtract_NextToken(NM->NextHop, SerializedNetMap, &Pos, '|');
660 HashList* CtdlReadNetworkMap(void)
667 CtdlNetMap *TheNetMap;
669 Hash = NewHash(1, NULL);
670 Cfg = CtdlGetSysConfig(IGNETMAP);
671 if ((Cfg == NULL) || IsEmptyStr(Cfg)) {
677 Buf = NewStrBufPlain(Cfg, -1);
679 LineBuf = NewStrBufPlain(NULL, StrLength(Buf));
681 while (StrBufSipLine(Buf, LineBuf, &LinePos))
683 TheNetMap = NewNetMap(LineBuf);
684 if (TheNetMap != NULL) { /* TODO: is the NodeName Uniq? */
685 Put(Hash, SKEY(TheNetMap->NodeName), TheNetMap, DeleteNetMap);
689 FreeStrBuf(&LineBuf);
694 StrBuf *CtdlSerializeNetworkMap(HashList *Map)
699 StrBuf *Ret = NewStrBuf();
700 HashPos *Pos = GetNewHashPos(Map, 0);
702 while (GetNextHashPos(Map, Pos, &len, &key, &vMap))
704 CtdlNetMap *pMap = (CtdlNetMap*) vMap;
705 StrBufAppendBuf(Ret, pMap->NodeName, 0);
706 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
708 StrBufAppendPrintf(Ret, "%ld", pMap->lastcontact, 0);
709 StrBufAppendBufPlain(Ret, HKEY("|"), 0);
711 StrBufAppendBuf(Ret, pMap->NextHop, 0);
712 StrBufAppendBufPlain(Ret, HKEY("\n"), 0);
720 * Convert any legacy configuration files in the "netconfigs" directory
722 void convert_legacy_netcfg_files(void)
725 struct dirent *dit = NULL;
726 char filename[PATH_MAX];
732 dh = opendir(ctdl_netcfg_dir);
735 syslog(LOG_INFO, "netconfig: legacy netconfig files exist - converting them!");
737 while (dit = readdir(dh), dit != NULL) // yes, we use the non-reentrant version; we're not in threaded mode yet
739 roomnum = atol(dit->d_name);
741 snprintf(filename, sizeof filename, "%s/%ld", ctdl_netcfg_dir, roomnum);
742 fp = fopen(filename, "r");
745 fseek(fp, 0L, SEEK_END);
753 if (fread(v, len, 1, fp))
755 write_netconfig_to_configdb(roomnum, v);
763 unlink(filename); // zero length netconfig, just delete it
771 rmdir(ctdl_netcfg_dir);
778 CTDL_MODULE_INIT(netconfig)
782 convert_legacy_netcfg_files();
783 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
784 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");