1 // This module handles loading, saving, and parsing of room network configurations.
3 // Copyright (c) 2000-2021 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
15 #include <sys/types.h>
21 # if HAVE_SYS_SYSCALL_H
22 # include <sys/syscall.h>
28 #include <libcitadel.h>
30 #include "ctdl_module.h"
31 #include "serv_extensions.h"
35 // Create a config key for a room's netconfig entry
36 void netcfg_keyname(char *keybuf, long roomnum) {
38 sprintf(keybuf, "c_netconfig_%010ld", roomnum);
42 // Given a room number and a textual netconfig, convert to base64 and write to the configdb
43 void SaveRoomNetConfigFile(long roomnum, const char *raw_netconfig) {
49 len = strlen(raw_netconfig);
50 netcfg_keyname(keyname, roomnum);
51 enc = malloc(len * 2);
54 enc_len = CtdlEncodeBase64(enc, raw_netconfig, len, 0);
55 if ((enc_len > 1) && (enc[enc_len-2] == 13)) enc[enc_len-2] = 0;
56 if ((enc_len > 0) && (enc[enc_len-1] == 10)) enc[enc_len-1] = 0;
58 syslog(LOG_DEBUG, "netconfig: writing key '%s' (length=%d)", keyname, enc_len);
59 CtdlSetConfigStr(keyname, enc);
65 // Given a room number, attempt to load the netconfig configdb entry for that room.
66 // If it returns NULL, there is no netconfig.
67 // Otherwise the caller owns the returned memory and is responsible for freeing it.
68 char *LoadRoomNetConfigFile(long roomnum) {
70 char *encoded_netconfig = NULL;
71 char *decoded_netconfig = NULL;
73 netcfg_keyname(keyname, roomnum);
74 encoded_netconfig = CtdlGetConfigStr(keyname);
75 if (!encoded_netconfig) return NULL;
77 decoded_netconfig = malloc(strlen(encoded_netconfig)); // yeah, way bigger than it needs to be, but safe
78 CtdlDecodeBase64(decoded_netconfig, encoded_netconfig, strlen(encoded_netconfig));
79 return decoded_netconfig;
83 //-----------------------------------------------------------------------------
84 // Per room network configs : exchange with client
85 //-----------------------------------------------------------------------------
86 void cmd_gnet(char *argbuf) {
87 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
88 // users can edit the netconfigs for their own mailbox rooms
90 else if (CtdlAccessCheck(ac_room_aide)) return;
92 cprintf("%d Network settings for room #%ld <%s>\n", LISTING_FOLLOWS, CC->room.QRnumber, CC->room.QRname);
94 char *c = LoadRoomNetConfigFile(CC->room.QRnumber);
97 client_write(c, len); // Can't use cprintf() here, it has a limit of 1024 bytes
99 client_write(HKEY("\n"));
107 void cmd_snet(char *argbuf) {
109 StrBuf *TheConfig = NULL;
114 TheConfig = NewStrBuf();
115 cprintf("%d send new netconfig now\n", SEND_LISTING);
117 while (rc = CtdlClientGetLine(Line), (rc >= 0)) {
118 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
121 StrBufAppendBuf(TheConfig, Line, 0);
122 StrBufAppendBufPlain(TheConfig, HKEY("\n"), 0);
126 begin_critical_section(S_NETCONFIGS);
127 SaveRoomNetConfigFile(CC->room.QRnumber, ChrPtr(TheConfig));
128 end_critical_section(S_NETCONFIGS);
129 FreeStrBuf(&TheConfig);
133 // Convert any legacy configuration files in the "netconfigs" directory
134 void convert_legacy_netcfg_files(void) {
136 struct dirent *dit = NULL;
137 char filename[PATH_MAX];
143 dh = opendir(ctdl_netcfg_dir);
146 syslog(LOG_INFO, "netconfig: legacy netconfig files exist - converting them!");
148 while (dit = readdir(dh), dit != NULL) { // yes, we use the non-reentrant version; we're not in threaded mode yet
149 roomnum = atol(dit->d_name);
151 snprintf(filename, sizeof filename, "%s/%ld", ctdl_netcfg_dir, roomnum);
152 fp = fopen(filename, "r");
154 fseek(fp, 0L, SEEK_END);
160 if (fread(v, len, 1, fp)) {
161 SaveRoomNetConfigFile(roomnum, v);
168 unlink(filename); // zero length netconfig, just delete it
176 rmdir(ctdl_netcfg_dir);
183 CTDL_MODULE_INIT(netconfig)
187 convert_legacy_netcfg_files();
188 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
189 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");