removed some debugs
[citadel.git] / citadel / netconfig.c
1 // This module handles loading, saving, and parsing of room network configurations.
2 //
3 // Copyright (c) 2000-2021 by the citadel.org team
4 //
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.
7 //
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.
12
13 #include "sysdep.h"
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17
18 #ifdef HAVE_SYSCALL_H
19 # include <syscall.h>
20 #else 
21 # if HAVE_SYS_SYSCALL_H
22 #  include <sys/syscall.h>
23 # endif
24 #endif
25 #include <dirent.h>
26 #include <assert.h>
27
28 #include <libcitadel.h>
29
30 #include "ctdl_module.h"
31 #include "serv_extensions.h"
32 #include "config.h"
33
34
35 // Create a config key for a room's netconfig entry
36 void netcfg_keyname(char *keybuf, long roomnum) {
37         if (!keybuf) return;
38         sprintf(keybuf, "c_netconfig_%010ld", roomnum);
39 }
40
41
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) {
44         char keyname[25];
45         char *enc;
46         int enc_len;
47         int len;
48
49         len = strlen(raw_netconfig);
50         netcfg_keyname(keyname, roomnum);
51         enc = malloc(len * 2);
52
53         if (enc) {
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;
57                 enc[enc_len] = 0;
58                 syslog(LOG_DEBUG, "netconfig: writing key '%s' (length=%d)", keyname, enc_len);
59                 CtdlSetConfigStr(keyname, enc);
60                 free(enc);
61         }
62 }
63
64
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) {
69         char keyname[25];
70         char *encoded_netconfig = NULL;
71         char *decoded_netconfig = NULL;
72
73         netcfg_keyname(keyname, roomnum);
74         encoded_netconfig = CtdlGetConfigStr(keyname);
75         if (!encoded_netconfig) return NULL;
76
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;
80 }
81
82
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
89         }
90         else if (CtdlAccessCheck(ac_room_aide)) return;
91         
92         cprintf("%d Network settings for room #%ld <%s>\n", LISTING_FOLLOWS, CC->room.QRnumber, CC->room.QRname);
93
94         char *c = LoadRoomNetConfigFile(CC->room.QRnumber);
95         if (c) {
96                 int len = strlen(c);
97                 client_write(c, len);                   // Can't use cprintf() here, it has a limit of 1024 bytes
98                 if (c[len] != '\n') {
99                         client_write(HKEY("\n"));
100                 }
101                 free(c);
102         }
103         cprintf("000\n");
104 }
105
106
107 void cmd_snet(char *argbuf) {
108         StrBuf *Line = NULL;
109         StrBuf *TheConfig = NULL;
110         int rc;
111
112         unbuffer_output();
113         Line = NewStrBuf();
114         TheConfig = NewStrBuf();
115         cprintf("%d send new netconfig now\n", SEND_LISTING);
116
117         while (rc = CtdlClientGetLine(Line), (rc >= 0)) {
118                 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
119                         break;
120
121                 StrBufAppendBuf(TheConfig, Line, 0);
122                 StrBufAppendBufPlain(TheConfig, HKEY("\n"), 0);
123         }
124         FreeStrBuf(&Line);
125
126         begin_critical_section(S_NETCONFIGS);
127         SaveRoomNetConfigFile(CC->room.QRnumber, ChrPtr(TheConfig));
128         end_critical_section(S_NETCONFIGS);
129         FreeStrBuf(&TheConfig);
130 }
131
132
133 // Convert any legacy configuration files in the "netconfigs" directory
134 void convert_legacy_netcfg_files(void) {
135         DIR *dh = NULL;
136         struct dirent *dit = NULL;
137         char filename[PATH_MAX];
138         long roomnum;
139         FILE *fp;
140         long len;
141         char *v;
142
143         dh = opendir(ctdl_netcfg_dir);
144         if (!dh) return;
145
146         syslog(LOG_INFO, "netconfig: legacy netconfig files exist - converting them!");
147
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);
150                 if (roomnum > 0) {
151                         snprintf(filename, sizeof filename, "%s/%ld", ctdl_netcfg_dir, roomnum);
152                         fp = fopen(filename, "r");
153                         if (fp) {
154                                 fseek(fp, 0L, SEEK_END);
155                                 len = ftell(fp);
156                                 if (len > 0) {
157                                         v = malloc(len);
158                                         if (v) {
159                                                 rewind(fp);
160                                                 if (fread(v, len, 1, fp)) {
161                                                         SaveRoomNetConfigFile(roomnum, v);
162                                                         unlink(filename);
163                                                 }
164                                                 free(v);
165                                         }
166                                 }
167                                 else {
168                                         unlink(filename);       // zero length netconfig, just delete it
169                                 }
170                                 fclose(fp);
171                         }
172                 }
173         }
174
175         closedir(dh);
176         rmdir(ctdl_netcfg_dir);
177 }
178
179
180 /*
181  * Module entry point
182  */
183 CTDL_MODULE_INIT(netconfig)
184 {
185         if (!threading)
186         {
187                 convert_legacy_netcfg_files();
188                 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
189                 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
190         }
191         return "netconfig";
192 }