Allow specification of DNS vhost and server in ircd.conf.
[ircu2.10.12-pk.git] / ircd / s_conf.c
index afcfa6c93b655e4a80a7e4fc6525b02fe5559e21..9b37dc2d7917c2ab39f012f9739f10196bfb050e 100644 (file)
@@ -34,7 +34,6 @@
 #include "hash.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
-#include "ircd_auth.h"
 #include "ircd_chattr.h"
 #include "ircd_log.h"
 #include "ircd_reply.h"
@@ -49,6 +48,7 @@
 #include "opercmds.h"
 #include "parse.h"
 #include "res.h"
+#include "s_auth.h"
 #include "s_bsd.h"
 #include "s_debug.h"
 #include "s_misc.h"
@@ -129,9 +129,7 @@ struct ConfItem* make_conf(int type)
 
   aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
   assert(0 != aconf);
-#ifdef        DEBUGMODE
   ++GlobalConfCount;
-#endif
   memset(aconf, 0, sizeof(struct ConfItem));
   aconf->status  = type;
   aconf->next    = GlobalConfList;
@@ -159,9 +157,7 @@ void free_conf(struct ConfItem *aconf)
   MyFree(aconf->name);
   MyFree(aconf->hub_limit);
   MyFree(aconf);
-#ifdef        DEBUGMODE
   --GlobalConfCount;
-#endif
 }
 
 /** Disassociate configuration from the client.
@@ -232,15 +228,13 @@ void conf_parse_userhost(struct ConfItem *aconf, char *host)
  * @param vptr Pointer to struct ConfItem for the block.
  * @param hp DNS reply, or NULL if the lookup failed.
  */
-static void conf_dns_callback(void* vptr, struct DNSReply* hp)
+static void conf_dns_callback(void* vptr, const struct irc_in_addr *addr, const char *h_name)
 {
   struct ConfItem* aconf = (struct ConfItem*) vptr;
   assert(aconf);
   aconf->dns_pending = 0;
-  if (hp) {
-    memcpy(&aconf->address.addr, &hp->addr, sizeof(aconf->address.addr));
-    MyFree(hp);
-  }
+  if (addr)
+    memcpy(&aconf->address.addr, addr, sizeof(aconf->address.addr));
 }
 
 /** Start a nameserver lookup of the conf host.  If the conf entry is
@@ -251,13 +245,9 @@ static void conf_dns_lookup(struct ConfItem* aconf)
 {
   if (!aconf->dns_pending) {
     char            buf[HOSTLEN + 1];
-    struct DNSQuery query;
-    query.vptr     = aconf;
-    query.callback = conf_dns_callback;
-    host_from_uh(buf, aconf->host, HOSTLEN);
-    buf[HOSTLEN] = '\0';
 
-    gethost_byname(buf, &query);
+    host_from_uh(buf, aconf->host, HOSTLEN);
+    gethost_byname(buf, conf_dns_callback, aconf);
     aconf->dns_pending = 1;
   }
 }
@@ -355,22 +345,6 @@ void det_confs_butmask(struct Client* cptr, int mask)
   }
 }
 
-/** Check client limits and attach Client block.
- * If there are more connections from the IP than \a aconf->maximum
- * allows, return ACR_TOO_MANY_FROM_IP.  Otherwise, attach \a aconf to
- * \a cptr.
- * @param cptr Client getting \a aconf.
- * @param aconf Configuration item to attach.
- * @return Authorization check result.
- */
-static enum AuthorizationCheckResult
-check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
-{
-  if (IPcheck_nr(cptr) > aconf->maximum)
-    return ACR_TOO_MANY_FROM_IP;
-  return attach_conf(cptr, aconf);
-}
-
 /** Find the first (best) Client block to attach.
  * @param cptr Client for whom to check rules.
  * @return Authorization check result.
@@ -378,11 +352,9 @@ check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
 {
   struct ConfItem* aconf;
-  struct DNSReply* hp;
 
   assert(0 != cptr);
 
-  hp = cli_dns_reply(cptr);
   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
     if (aconf->status != CONF_CLIENT)
       continue;
@@ -391,17 +363,18 @@ enum AuthorizationCheckResult attach_iline(struct Client* cptr)
      */
     if (aconf->address.port && aconf->address.port != cli_listener(cptr)->addr.port)
       continue;
-    if (aconf->username) {
-      SetFlag(cptr, FLAG_DOID);
-      if (match(aconf->username, cli_username(cptr)))
-        continue;
-    }
-    if (aconf->host && (!hp || match(aconf->host, hp->h_name)))
+    if (aconf->username && match(aconf->username, cli_username(cptr)))
+      continue;
+    if (aconf->host && match(aconf->host, cli_sockhost(cptr)))
       continue;
     if ((aconf->addrbits >= 0)
         && !ipmask_check(&cli_ip(cptr), &aconf->address.addr, aconf->addrbits))
       continue;
-    return check_limit_and_attach(cptr, aconf);
+    if (IPcheck_nr(cptr) > aconf->maximum)
+      return ACR_TOO_MANY_FROM_IP;
+    if (aconf->username)
+      SetFlag(cptr, FLAG_DOID);
+    return attach_conf(cptr, aconf);
   }
   return ACR_NO_AUTHORIZATION;
 }
@@ -509,7 +482,8 @@ struct ConfItem *conf_debug_iline(const char *client)
             (aconf->username ? aconf->username : "(null)"),
             (aconf->host ? aconf->host : "(null)"),
             (aconf->name ? aconf->name : "(null)"),
-            ConfClass(aconf), aconf->maximum,  aconf->passwd);
+            ConfClass(aconf), aconf->maximum,
+            (aconf->passwd ? aconf->passwd : "(null)"));
     break;
   }
 
@@ -852,6 +826,7 @@ int read_configuration_file(void)
 {
   conf_error = 0;
   feature_unmark(); /* unmark all features for resetting later */
+  clear_nameservers(); /* clear previous list of DNS servers */
   /* Now just open an fd. The buffering isn't really needed... */
   init_lexer();
   yyparse();
@@ -888,6 +863,36 @@ attach_conf_uworld(struct Client *cptr)
     attach_conf_uworld(lp->value.cptr);
 }
 
+/** Free all memory associated with service mapping \a smap.
+ * @param smap[in] The mapping to free.
+ */
+void free_mapping(struct s_map *smap)
+{
+  struct nick_host *nh, *next;
+  for (nh = smap->services; nh; nh = next)
+  {
+    next = nh->next;
+    MyFree(nh);
+  }
+  MyFree(smap->name);
+  MyFree(smap->command);
+  MyFree(smap->prepend);
+  MyFree(smap);
+}
+
+/** Unregister and free all current service mappings. */
+static void close_mappings(void)
+{
+  struct s_map *map, *next;
+
+  for (map = GlobalServiceMapList; map; map = next) {
+    next = map->next;
+    unregister_mapping(map);
+    free_mapping(map);
+  }
+  GlobalServiceMapList = NULL;
+}
+
 /** Reload the configuration file.
  * @param cptr Client that requested rehash (if a signal, &me).
  * @param sig Type of rehash (0 = oper-requested, 1 = signal, 2 =
@@ -945,13 +950,14 @@ int rehash(struct Client *cptr, int sig)
 
   class_mark_delete();
   mark_listeners_closing();
-  iauth_mark_closing();
+  auth_mark_closing();
+  close_mappings();
 
   read_configuration_file();
 
   log_reopen(); /* reopen log files */
 
-  iauth_close_unused();
+  auth_close_unused();
   close_listeners();
   class_delete_marked();         /* unless it fails */
 
@@ -1145,28 +1151,10 @@ int conf_check_server(struct Client *cptr)
     }
   }
 
-  if (!c_conf) {
-    if (cli_dns_reply(cptr)) {
-      struct DNSReply* hp = cli_dns_reply(cptr);
-      const char*     name = hp->h_name;
-      /*
-       * If we are missing a C or N line from above, search for
-       * it under all known hostnames we have for this ip#.
-       */
-      if ((c_conf = find_conf_byhost(lp, hp->h_name, CONF_SERVER)))
-        ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
-      else
-        c_conf = find_conf_byip(lp, &hp->addr, CONF_SERVER);
-    }
-    else {
-      /*
-       * Check for C lines with the hostname portion the ip number
-       * of the host the server runs on. This also checks the case where
-       * there is a server connecting from 'localhost'.
-       */
-      c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
-    }
-  }
+  /* Try finding the Connect block by DNS name and IP next. */
+  if (!c_conf && !(c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER)))
+        c_conf = find_conf_byip(lp, &cli_ip(cptr), CONF_SERVER);
+
   /*
    * Attach by IP# only if all other checks have failed.
    * It is quite possible to get here with the strange things that can