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 * Load or refresh the Citadel network (IGnet) configuration for this node.
98 char* load_working_ignetcfg(void) {
99 return CtdlGetSysConfig(IGNETCFG);
104 * Read the network map from its configuration file into memory.
106 NetMap *read_network_map(void) {
107 char *serialized_map = NULL;
110 NetMap *nmptr, *the_netmap;
113 serialized_map = CtdlGetSysConfig(IGNETMAP);
114 if (serialized_map == NULL) return NULL; /* if null, no entries */
116 /* Use the string tokenizer to grab one line at a time */
117 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
118 extract_token(buf, serialized_map, i, '\n', sizeof buf);
119 nmptr = (NetMap *) malloc(sizeof(NetMap));
120 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
121 nmptr->lastcontact = extract_long(buf, 1);
122 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
123 nmptr->next = the_netmap;
127 free(serialized_map);
133 * Write the network map from memory back to the configuration file.
135 void write_network_map(NetMap *the_netmap, int netmap_changed) {
136 char *serialized_map = NULL;
140 if (netmap_changed) {
141 serialized_map = strdup("");
143 if (the_netmap != NULL) {
144 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
145 serialized_map = realloc(serialized_map,
146 (strlen(serialized_map)+SIZ) );
147 if (!IsEmptyStr(nmptr->nodename)) {
148 snprintf(&serialized_map[strlen(serialized_map)],
152 (long)nmptr->lastcontact,
158 CtdlPutSysConfig(IGNETMAP, serialized_map);
159 free(serialized_map);
162 /* Now free the list */
163 while (the_netmap != NULL) {
164 nmptr = the_netmap->next;
173 * Check the network map and determine whether the supplied node name is
174 * valid. If it is not a neighbor node, supply the name of a neighbor node
175 * which is the next hop. If it *is* a neighbor node, we also fill in the
178 int is_valid_node(char *nexthop,
181 char *working_ignetcfg,
195 * First try the neighbor nodes
197 if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
198 syslog(LOG_ERR, "working_ignetcfg is empty!\n");
199 if (nexthop != NULL) {
206 if (nexthop != NULL) {
210 /* Use the string tokenizer to grab one line at a time */
211 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
212 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
213 extract_token(buf, linebuf, 0, '|', sizeof buf);
214 if (!strcasecmp(buf, node)) {
215 if (nexthop != NULL) {
218 if (secret != NULL) {
219 extract_token(secret, linebuf, 1, '|', 256);
226 return(retval); /* yup, it's a direct neighbor */
230 * If we get to this point we have to see if we know the next hop
232 if (the_netmap != NULL) {
233 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
234 if (!strcasecmp(nmptr->nodename, node)) {
235 if (nexthop != NULL) {
236 strcpy(nexthop, nmptr->nexthop);
244 * If we get to this point, the supplied node name is bogus.
246 syslog(LOG_ERR, "Invalid node name <%s>\n", node);
252 void cmd_gnet(char *argbuf) {
253 char filename[PATH_MAX];
257 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
258 /* users can edit the netconfigs for their own mailbox rooms */
260 else if (CtdlAccessCheck(ac_room_aide)) return;
262 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
263 cprintf("%d Network settings for room #%ld <%s>\n",
265 CC->room.QRnumber, CC->room.QRname);
267 fp = fopen(filename, "r");
269 while (fgets(buf, sizeof buf, fp) != NULL) {
270 buf[strlen(buf)-1] = 0;
271 cprintf("%s\n", buf);
280 void cmd_snet(char *argbuf) {
281 char tempfilename[PATH_MAX];
282 char filename[PATH_MAX];
291 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
292 /* users can edit the netconfigs for their own mailbox rooms */
294 else if (CtdlAccessCheck(ac_room_aide)) return;
296 len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
297 memcpy(tempfilename, filename, len + 1);
299 memset(&StatBuf, 0, sizeof(struct stat));
300 if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
301 StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
303 sprintf(tempfilename + len, ".%d", CC->cs_pid);
305 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
307 if ((TmpFD > 0) && (errno == 0))
309 char *tmp = malloc(StatBuf.st_size * 2);
310 memset(tmp, ' ', StatBuf.st_size * 2);
311 rc = write(TmpFD, tmp, StatBuf.st_size * 2);
313 if ((rc <= 0) || (rc != StatBuf.st_size * 2))
316 cprintf("%d Unable to allocate the space required for %s: %s\n",
317 ERROR + INTERNAL_ERROR,
320 unlink(tempfilename);
323 lseek(TmpFD, SEEK_SET, 0);
326 cprintf("%d Unable to allocate the space required for %s: %s\n",
327 ERROR + INTERNAL_ERROR,
330 unlink(tempfilename);
335 cprintf("%d %s\n", SEND_LISTING, tempfilename);
338 while (rc = CtdlClientGetLine(Line),
341 if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
343 StrBufAppendBufPlain(Line, HKEY("\n"), 0);
344 write(TmpFD, ChrPtr(Line), StrLength(Line));
345 len += StrLength(Line);
348 ftruncate(TmpFD, len);
351 /* Now copy the temp file to its permanent location.
352 * (We copy instead of link because they may be on different filesystems)
354 begin_critical_section(S_NETCONFIGS);
355 rename(tempfilename, filename);
356 end_critical_section(S_NETCONFIGS);
360 * cmd_netp() - authenticate to the server as another Citadel node polling
361 * for network traffic
363 void cmd_netp(char *cmdbuf)
365 char *working_ignetcfg;
376 nodelen = extract_token(node, cmdbuf, 0, '|', sizeof node);
377 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
379 /* load the IGnet Configuration to check node validity */
380 working_ignetcfg = load_working_ignetcfg();
381 v = is_valid_node(nexthop, secret, node, working_ignetcfg, NULL); //// TODO do we need the netmap?
384 snprintf(err_buf, sizeof err_buf,
385 "An unknown Citadel server called \"%s\" attempted to connect from %s [%s].\n",
386 node, CC->cs_host, CC->cs_addr
388 syslog(LOG_WARNING, "%s", err_buf);
389 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
390 CtdlAideMessage(err_buf, "IGNet Networking.");
391 free(working_ignetcfg);
395 if (strcasecmp(pass, secret)) {
396 snprintf(err_buf, sizeof err_buf,
397 "A Citadel server at %s [%s] failed to authenticate as network node \"%s\".\n",
398 CC->cs_host, CC->cs_addr, node
400 syslog(LOG_WARNING, "%s", err_buf);
401 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
402 CtdlAideMessage(err_buf, "IGNet Networking.");
403 free(working_ignetcfg);
407 if (network_talking_to(node, nodelen, NTT_CHECK)) {
408 syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
409 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
410 free(working_ignetcfg);
414 safestrncpy(CC->net_node, node, sizeof CC->net_node);
415 network_talking_to(node, nodelen, NTT_ADD);
416 syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
417 CC->net_node, CC->cs_host, CC->cs_addr
419 cprintf("%d authenticated as network node '%s'\n", CIT_OK, CC->net_node);
420 free(working_ignetcfg);
423 int netconfig_check_roomaccess(
426 const char* RemoteIdentifier)
432 if (RemoteIdentifier == NULL)
434 snprintf(errmsgbuf, n, "Need sender to permit access.");
435 return (ERROR + USERNAME_REQUIRED);
438 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
439 begin_critical_section(S_NETCONFIGS);
440 if (!read_spoolcontrol_file(&sc, filename))
442 end_critical_section(S_NETCONFIGS);
443 snprintf(errmsgbuf, n,
444 "This mailing list only accepts posts from subscribers.");
445 return (ERROR + NO_SUCH_USER);
447 end_critical_section(S_NETCONFIGS);
448 found = is_recipient (sc, RemoteIdentifier);
449 free_spoolcontrol_struct(&sc);
454 snprintf(errmsgbuf, n,
455 "This mailing list only accepts posts from subscribers.");
456 return (ERROR + NO_SUCH_USER);
462 CTDL_MODULE_INIT(netconfig)
466 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
467 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
468 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");