rework the networking locking facility using the hashlist.
[citadel.git] / citadel / modules / network / serv_netconfig.c
index 234a1fe5dca60e7a9525c755b00d7327b417699e..4f5eac42194a6c97f9f36791640262cd807c5719 100644 (file)
 
 #include "context.h"
 #include "netconfig.h"
+#include "netspool.h"
 #include "ctdl_module.h"
 
 
-/*
- * We build a map of network nodes during processing.
- */
-NetMap *the_netmap = NULL;
-int netmap_changed = 0;
-char *working_ignetcfg = NULL;
-
 /*
  * Load or refresh the Citadel network (IGnet) configuration for this node.
  */
-void load_working_ignetcfg(void) {
-       char *cfg;
-       char *oldcfg;
-
-       cfg = CtdlGetSysConfig(IGNETCFG);
-       if (cfg == NULL) {
-               cfg = strdup("");
-       }
-
-       oldcfg = working_ignetcfg;
-       working_ignetcfg = cfg;
-       if (oldcfg != NULL) {
-               free(oldcfg);
-       }
+char* load_working_ignetcfg(void) {
+       return CtdlGetSysConfig(IGNETCFG);
 }
 
+
 /* 
  * Read the network map from its configuration file into memory.
  */
-void read_network_map(void) {
+NetMap *read_network_map(void) {
        char *serialized_map = NULL;
        int i;
        char buf[SIZ];
-       NetMap *nmptr;
+       NetMap *nmptr, *the_netmap;
 
+       the_netmap = NULL;
        serialized_map = CtdlGetSysConfig(IGNETMAP);
-       if (serialized_map == NULL) return;     /* if null, no entries */
+       if (serialized_map == NULL) return NULL;        /* if null, no entries */
 
        /* Use the string tokenizer to grab one line at a time */
        for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
@@ -141,14 +125,14 @@ void read_network_map(void) {
        }
 
        free(serialized_map);
-       netmap_changed = 0;
+       return the_netmap;
 }
 
 
 /*
  * Write the network map from memory back to the configuration file.
  */
-void write_network_map(void) {
+void write_network_map(NetMap *the_netmap, int netmap_changed) {
        char *serialized_map = NULL;
        NetMap *nmptr;
 
@@ -186,62 +170,18 @@ void write_network_map(void) {
 
 
 
-/*
- * Keep track of what messages to reject
- */
-FilterList *load_filter_list(void) {
-       char *serialized_list = NULL;
-       int i;
-       char buf[SIZ];
-       FilterList *newlist = NULL;
-       FilterList *nptr;
-
-       serialized_list = CtdlGetSysConfig(FILTERLIST);
-       if (serialized_list == NULL) return(NULL); /* if null, no entries */
-
-       /* Use the string tokenizer to grab one line at a time */
-       for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
-               extract_token(buf, serialized_list, i, '\n', sizeof buf);
-               nptr = (FilterList *) malloc(sizeof(FilterList));
-               extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
-               striplt(nptr->fl_user);
-               extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
-               striplt(nptr->fl_room);
-               extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
-               striplt(nptr->fl_node);
-
-               /* Cowardly refuse to add an any/any/any entry that would
-                * end up filtering every single message.
-                */
-               if (IsEmptyStr(nptr->fl_user) && 
-                   IsEmptyStr(nptr->fl_room) &&
-                   IsEmptyStr(nptr->fl_node)) {
-                       free(nptr);
-               }
-               else {
-                       nptr->next = newlist;
-                       newlist = nptr;
-               }
-       }
-
-       free(serialized_list);
-       return newlist;
-}
-
-
-void free_filter_list(FilterList *fl) {
-       if (fl == NULL) return;
-       free_filter_list(fl->next);
-       free(fl);
-}
-
 /* 
  * Check the network map and determine whether the supplied node name is
  * valid.  If it is not a neighbor node, supply the name of a neighbor node
  * which is the next hop.  If it *is* a neighbor node, we also fill in the
  * shared secret.
  */
-int is_valid_node(char *nexthop, char *secret, char *node) {
+int is_valid_node(char *nexthop, 
+                 char *secret, 
+                 char *node, 
+                 char *working_ignetcfg, 
+                 NetMap *the_netmap)
+{
        int i;
        char linebuf[SIZ];
        char buf[SIZ];
@@ -255,8 +195,8 @@ int is_valid_node(char *nexthop, char *secret, char *node) {
        /*
         * First try the neighbor nodes
         */
-       if (working_ignetcfg == NULL) {
-               syslog(LOG_ERR, "working_ignetcfg is NULL!\n");
+       if ((working_ignetcfg == NULL) || (*working_ignetcfg == '\0')) {
+               syslog(LOG_ERR, "working_ignetcfg is empty!\n");
                if (nexthop != NULL) {
                        strcpy(nexthop, "");
                }
@@ -423,7 +363,9 @@ void cmd_snet(char *argbuf) {
  */
 void cmd_netp(char *cmdbuf)
 {
+       char *working_ignetcfg;
        char node[256];
+       long nodelen;
        char pass[256];
        int v;
 
@@ -432,12 +374,12 @@ void cmd_netp(char *cmdbuf)
        char err_buf[SIZ];
 
        /* Authenticate */
-       extract_token(node, cmdbuf, 0, '|', sizeof node);
+       nodelen = extract_token(node, cmdbuf, 0, '|', sizeof node);
        extract_token(pass, cmdbuf, 1, '|', sizeof pass);
 
        /* load the IGnet Configuration to check node validity */
-       load_working_ignetcfg();
-       v = is_valid_node(nexthop, secret, node);
+       working_ignetcfg = load_working_ignetcfg();
+       v = is_valid_node(nexthop, secret, node, working_ignetcfg, NULL); //// TODO do we need the netmap?
 
        if (v != 0) {
                snprintf(err_buf, sizeof err_buf,
@@ -447,6 +389,7 @@ void cmd_netp(char *cmdbuf)
                syslog(LOG_WARNING, "%s", err_buf);
                cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
                CtdlAideMessage(err_buf, "IGNet Networking.");
+               free(working_ignetcfg);
                return;
        }
 
@@ -458,23 +401,62 @@ void cmd_netp(char *cmdbuf)
                syslog(LOG_WARNING, "%s", err_buf);
                cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
                CtdlAideMessage(err_buf, "IGNet Networking.");
+               free(working_ignetcfg);
                return;
        }
 
-       if (network_talking_to(node, NTT_CHECK)) {
+       if (network_talking_to(node, nodelen, NTT_CHECK)) {
                syslog(LOG_WARNING, "Duplicate session for network node <%s>", node);
                cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
+               free(working_ignetcfg);
                return;
        }
 
        safestrncpy(CC->net_node, node, sizeof CC->net_node);
-       network_talking_to(node, NTT_ADD);
+       network_talking_to(node, nodelen, NTT_ADD);
        syslog(LOG_NOTICE, "Network node <%s> logged in from %s [%s]\n",
                CC->net_node, CC->cs_host, CC->cs_addr
        );
        cprintf("%d authenticated as network node '%s'\n", CIT_OK, CC->net_node);
+       free(working_ignetcfg);
 }
 
+int netconfig_check_roomaccess(
+       char *errmsgbuf, 
+       size_t n,
+       const char* RemoteIdentifier)
+{
+       SpoolControl *sc;
+       char filename[SIZ];
+       int found;
+
+       if (RemoteIdentifier == NULL)
+       {
+               snprintf(errmsgbuf, n, "Need sender to permit access.");
+               return (ERROR + USERNAME_REQUIRED);
+       }
+
+       assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
+       begin_critical_section(S_NETCONFIGS);
+       if (!read_spoolcontrol_file(&sc, filename))
+       {
+               end_critical_section(S_NETCONFIGS);
+               snprintf(errmsgbuf, n,
+                        "This mailing list only accepts posts from subscribers.");
+               return (ERROR + NO_SUCH_USER);
+       }
+       end_critical_section(S_NETCONFIGS);
+       found = is_recipient (sc, RemoteIdentifier);
+       free_spoolcontrol_struct(&sc);
+       if (found) {
+               return (0);
+       }
+       else {
+               snprintf(errmsgbuf, n,
+                        "This mailing list only accepts posts from subscribers.");
+               return (ERROR + NO_SUCH_USER);
+       }
+}
 /*
  * Module entry point
  */