1 // This module handles loading, saving, and parsing of room network configurations.
3 // Copyright (c) 2000-2023 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.
10 #include <sys/types.h>
13 #include <libcitadel.h>
14 #include "ctdl_module.h"
15 #include "serv_extensions.h"
19 // Create a config key for a room's netconfig entry
20 void netcfg_keyname(char *keybuf, long roomnum) {
22 sprintf(keybuf, "c_netconfig_%010ld", roomnum);
26 // Given a room number and a textual netconfig, convert to base64 and write to the configdb
27 void SaveRoomNetConfigFile(long roomnum, const char *raw_netconfig) {
33 len = strlen(raw_netconfig);
34 netcfg_keyname(keyname, roomnum);
35 enc = malloc(len * 2);
38 enc_len = CtdlEncodeBase64(enc, raw_netconfig, len, BASE64_NO_LINEBREAKS);
39 if ((enc_len > 1) && (enc[enc_len-2] == 13)) enc[enc_len-2] = 0;
40 if ((enc_len > 0) && (enc[enc_len-1] == 10)) enc[enc_len-1] = 0;
42 syslog(LOG_DEBUG, "netconfig: writing key '%s' (length=%d)", keyname, enc_len);
43 CtdlSetConfigStr(keyname, enc);
49 // Given a room number, attempt to load the netconfig configdb entry for that room.
50 // If it returns NULL, there is no netconfig.
51 // Otherwise the caller owns the returned memory and is responsible for freeing it.
52 char *LoadRoomNetConfigFile(long roomnum) {
54 char *encoded_netconfig = NULL;
55 char *decoded_netconfig = NULL;
57 netcfg_keyname(keyname, roomnum);
58 encoded_netconfig = CtdlGetConfigStr(keyname);
59 if (!encoded_netconfig) return NULL;
61 decoded_netconfig = malloc(strlen(encoded_netconfig)); // yeah, way bigger than it needs to be, but safe
62 CtdlDecodeBase64(decoded_netconfig, encoded_netconfig, strlen(encoded_netconfig));
63 return decoded_netconfig;
67 //-----------------------------------------------------------------------------
68 // Per room network configs : exchange with client
69 //-----------------------------------------------------------------------------
70 void cmd_gnet(char *argbuf) {
71 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
72 // users can edit the netconfigs for their own mailbox rooms
74 else if (CtdlAccessCheck(ac_room_aide)) return;
76 cprintf("%d Network settings for room #%ld <%s>\n", LISTING_FOLLOWS, CC->room.QRnumber, CC->room.QRname);
78 char *c = LoadRoomNetConfigFile(CC->room.QRnumber);
81 client_write(c, len); // Can't use cprintf() here, it has a limit of 1024 bytes
83 client_write(HKEY("\n"));
91 void cmd_snet(char *argbuf) {
93 StrBuf *TheConfig = NULL;
98 TheConfig = NewStrBuf();
99 cprintf("%d send new netconfig now\n", SEND_LISTING);
101 while (rc = CtdlClientGetLine(Line), (rc >= 0)) {
102 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
105 StrBufAppendBuf(TheConfig, Line, 0);
106 StrBufAppendBufPlain(TheConfig, HKEY("\n"), 0);
110 begin_critical_section(S_NETCONFIGS);
111 SaveRoomNetConfigFile(CC->room.QRnumber, ChrPtr(TheConfig));
112 end_critical_section(S_NETCONFIGS);
113 FreeStrBuf(&TheConfig);
117 // Convert any legacy configuration files in the "netconfigs" directory
118 void convert_legacy_netcfg_files(void) {
120 struct dirent *dit = NULL;
121 char filename[PATH_MAX];
127 dh = opendir(ctdl_netcfg_dir);
130 syslog(LOG_INFO, "netconfig: legacy netconfig files exist - converting them!");
132 while (dit = readdir(dh), dit != NULL) { // yes, we use the non-reentrant version; we're not in threaded mode yet
133 roomnum = atol(dit->d_name);
135 snprintf(filename, sizeof filename, "%s/%ld", ctdl_netcfg_dir, roomnum);
136 fp = fopen(filename, "r");
138 fseek(fp, 0L, SEEK_END);
144 if (fread(v, len, 1, fp)) {
145 SaveRoomNetConfigFile(roomnum, v);
152 unlink(filename); // zero length netconfig, just delete it
160 rmdir(ctdl_netcfg_dir);
164 // Module entry point
165 char *ctdl_module_init_netconfig(void) {
167 convert_legacy_netcfg_files();
168 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
169 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");