2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2011 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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
22 * This is a fairly high-level type of critical section. It ensures that no
23 * two threads work on the netconfigs files at the same time. Since we do
24 * so many things inside these, here are the rules:
25 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
26 * 2. Do *not* perform any I/O with the client during these sections.
31 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
32 * requests that have not been confirmed will be deleted.
34 #define EXP 259200 /* three days */
46 #include <sys/types.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
61 # if HAVE_SYS_SYSCALL_H
62 # include <sys/syscall.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
78 #include "internet_addressing.h"
79 #include "serv_network.h"
80 #include "clientsocket.h"
82 #include "citadel_dirs.h"
90 #include "netconfig.h"
92 #include "ctdl_module.h"
96 * We build a map of network nodes during processing.
98 NetMap *the_netmap = NULL;
99 int netmap_changed = 0;
100 char *working_ignetcfg = NULL;
103 * Load or refresh the Citadel network (IGnet) configuration for this node.
105 void load_working_ignetcfg(void) {
109 cfg = CtdlGetSysConfig(IGNETCFG);
114 oldcfg = working_ignetcfg;
115 working_ignetcfg = cfg;
116 if (oldcfg != NULL) {
122 * Read the network map from its configuration file into memory.
124 void read_network_map(void) {
125 char *serialized_map = NULL;
130 serialized_map = CtdlGetSysConfig(IGNETMAP);
131 if (serialized_map == NULL) return; /* if null, no entries */
133 /* Use the string tokenizer to grab one line at a time */
134 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
135 extract_token(buf, serialized_map, i, '\n', sizeof buf);
136 nmptr = (NetMap *) malloc(sizeof(NetMap));
137 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
138 nmptr->lastcontact = extract_long(buf, 1);
139 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
140 nmptr->next = the_netmap;
144 free(serialized_map);
150 * Write the network map from memory back to the configuration file.
152 void write_network_map(void) {
153 char *serialized_map = NULL;
157 if (netmap_changed) {
158 serialized_map = strdup("");
160 if (the_netmap != NULL) {
161 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
162 serialized_map = realloc(serialized_map,
163 (strlen(serialized_map)+SIZ) );
164 if (!IsEmptyStr(nmptr->nodename)) {
165 snprintf(&serialized_map[strlen(serialized_map)],
169 (long)nmptr->lastcontact,
175 CtdlPutSysConfig(IGNETMAP, serialized_map);
176 free(serialized_map);
179 /* Now free the list */
180 while (the_netmap != NULL) {
181 nmptr = the_netmap->next;
190 * Check the network map and determine whether the supplied node name is
191 * valid. If it is not a neighbor node, supply the name of a neighbor node
192 * which is the next hop. If it *is* a neighbor node, we also fill in the
195 int is_valid_node(char *nexthop, char *secret, char *node) {
207 * First try the neighbor nodes
209 if (working_ignetcfg == NULL) {
210 syslog(LOG_ERR, "working_ignetcfg is NULL!\n");
211 if (nexthop != NULL) {
218 if (nexthop != NULL) {
222 /* Use the string tokenizer to grab one line at a time */
223 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
224 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
225 extract_token(buf, linebuf, 0, '|', sizeof buf);
226 if (!strcasecmp(buf, node)) {
227 if (nexthop != NULL) {
230 if (secret != NULL) {
231 extract_token(secret, linebuf, 1, '|', 256);
238 return(retval); /* yup, it's a direct neighbor */
242 * If we get to this point we have to see if we know the next hop
244 if (the_netmap != NULL) {
245 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
246 if (!strcasecmp(nmptr->nodename, node)) {
247 if (nexthop != NULL) {
248 strcpy(nexthop, nmptr->nexthop);
256 * If we get to this point, the supplied node name is bogus.
258 syslog(LOG_ERR, "Invalid node name <%s>\n", node);
264 void cmd_gnet(char *argbuf) {
265 char filename[PATH_MAX];
269 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
270 /* users can edit the netconfigs for their own mailbox rooms */
272 else if (CtdlAccessCheck(ac_room_aide)) return;
274 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
275 cprintf("%d Network settings for room #%ld <%s>\n",
277 CC->room.QRnumber, CC->room.QRname);
279 fp = fopen(filename, "r");
281 while (fgets(buf, sizeof buf, fp) != NULL) {
282 buf[strlen(buf)-1] = 0;
283 cprintf("%s\n", buf);
292 void cmd_snet(char *argbuf) {
293 char tempfilename[PATH_MAX];
294 char filename[PATH_MAX];
303 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
304 /* users can edit the netconfigs for their own mailbox rooms */
306 else if (CtdlAccessCheck(ac_room_aide)) return;
308 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
309 memcpy(tempfilename, filename, len + 1);
311 memset(&StatBuf, 0, sizeof(struct stat));
312 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
313 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
315 sprintf(tempfilename + len, ".%d", CC->cs_pid);
317 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
319 if ((TmpFD > 0) && (errno == 0))
321 char *tmp = malloc(StatBuf.st_size * 2);
322 memset(tmp, ' ', StatBuf.st_size * 2);
323 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
325 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
328 cprintf("%d Unable to allocate the space required for %s: %s\n",
329 ERROR + INTERNAL_ERROR,
332 unlink(tempfilename);
335 lseek(TmpFD, SEEK_SET, 0);
338 cprintf("%d Unable to allocate the space required for %s: %s\n",
339 ERROR + INTERNAL_ERROR,
342 unlink(tempfilename);
347 cprintf("%d %s\n", SEND_LISTING, tempfilename);
350 while (rc = CtdlClientGetLine(Line),
353 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
355 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
356 write(TmpFD, ChrPtr(Line), StrLength(Line));
357 len += StrLength(Line);
360 ftruncate(TmpFD, len);
363 /* Now copy the temp file to its permanent location.
364 * (We copy instead of link because they may be on different filesystems)
366 begin_critical_section(S_NETCONFIGS);
367 rename(tempfilename, filename);
368 end_critical_section(S_NETCONFIGS);
372 * cmd_netp() - authenticate to the server as another Citadel node polling
373 * for network traffic
375 void cmd_netp(char *cmdbuf)
386 extract_token(node, cmdbuf, 0, '|', sizeof node);
387 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
389 /* load the IGnet Configuration to check node validity */
390 load_working_ignetcfg();
391 v = is_valid_node(nexthop, secret, node);
394 snprintf(err_buf, sizeof err_buf,
395 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
396 node, CC->cs_host, CC->cs_addr
398 syslog(LOG_WARNING, "%s", err_buf);
399 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
400 CtdlAideMessage(err_buf, "IGNet Networking.");
404 if (strcasecmp(pass, secret)) {
405 snprintf(err_buf, sizeof err_buf,
406 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
407 CC->cs_host, CC->cs_addr, node
409 syslog(LOG_WARNING, "%s", err_buf);
410 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
411 CtdlAideMessage(err_buf, "IGNet Networking.");
415 if (network_talking_to(node, NTT_CHECK)) {
416 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
417 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
421 safestrncpy(CC->net_node, node, sizeof CC->net_node);
422 network_talking_to(node, NTT_ADD);
423 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
424 CC->net_node, CC->cs_host, CC->cs_addr
426 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CC->net_node);
429 int netconfig_check_roomaccess(
432 const char* RemoteIdentifier)
438 if (RemoteIdentifier == NULL)
440 snprintf(errmsgbuf, n, "Need sender to permit access.");
441 return (ERROR + USERNAME_REQUIRED);
444 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
445 begin_critical_section(S_NETCONFIGS);
446 if (!read_spoolcontrol_file(&sc, filename))
448 end_critical_section(S_NETCONFIGS);
449 snprintf(errmsgbuf, n,
450 "This mailing list only accepts posts from subscribers.");
451 return (ERROR + NO_SUCH_USER);
453 end_critical_section(S_NETCONFIGS);
454 found = is_recipient (sc, RemoteIdentifier);
455 free_spoolcontrol_struct(&sc);
460 snprintf(errmsgbuf, n,
461 "This mailing list only accepts posts from subscribers.");
462 return (ERROR + NO_SUCH_USER);
468 CTDL_MODULE_INIT(netconfig)
472 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
473 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
474 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");