Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / s_conf.c
index 53baa8e8ba99f7bc6f753678a59ca219b0ce931c..83b0b0fae09bedb0d2b82dbc7e6729e63ef49b6a 100644 (file)
@@ -32,6 +32,8 @@
 #include "ircd_alloc.h"
 #include "ircd_chattr.h"
 #include "ircd_log.h"
+#include "ircd_reply.h"
+#include "ircd_snprintf.h"
 #include "ircd_string.h"
 #include "list.h"
 #include "listener.h"
@@ -90,7 +92,7 @@ static int is_comment(const char *comment)
 /*
  *  check against a set of time intervals
  */
-static int check_time_interval(char *interval, char *reply)
+static int check_time_interval(char *interval, char *reply, size_t reply_size)
 {
   struct tm* tptr;
   char*      p;
@@ -127,19 +129,18 @@ static int check_time_interval(char *interval, char *reply)
         ? (perm_min <= nowm && nowm <= perm_max)
         : (perm_min <= nowm || nowm <= perm_max))
     {
-      sprintf_irc(reply, ":%%s %%d %%s :%s %d:%02d to %d:%02d.",
-                  "You are not allowed to connect from",
-                  perm_min_hours, perm_min_minutes,
-                  perm_max_hours, perm_max_minutes);
+      ircd_snprintf(0, reply, reply_size, ":You are not allowed to connect "
+                   "from %d:%02d to %d:%02d.", perm_min_hours,
+                   perm_min_minutes, perm_max_hours, perm_max_minutes);
       return (ERR_YOUREBANNEDCREEP);
     }
     if ((perm_min < perm_max)
         ? (perm_min <= nowm + 5 && nowm + 5 <= perm_max)
         : (perm_min <= nowm + 5 || nowm + 5 <= perm_max))
     {
-      sprintf_irc(reply, ":%%s %%d %%s :%d minute%s%s",
-                  perm_min - nowm, (perm_min - nowm) > 1 ? "s " : " ",
-                  "and you will be denied for further access");
+      ircd_snprintf(0, reply, reply_size, ":%d minute%s and you will be "
+                   "denied for further access", perm_min - nowm,
+                   (perm_min - nowm) > 1 ? "s" : "");
       return (ERR_YOUWILLBEBANNED);
     }
     interval = p;
@@ -157,15 +158,14 @@ static void killcomment(struct Client *sptr, char *parv, char *filename)
 {
   FBFILE*     file = NULL;
   char        line[80];
-  char*       tmp;
+  char*       tmp = NULL;
   struct stat sb;
   struct tm*  tm;
 
   if (NULL == (file = fbopen(filename, "r"))) {
-    sendto_one(sptr, err_str(ERR_NOMOTD), me.name, parv);
-    sendto_one(sptr,
-        ":%s %d %s :Connection from your host is refused on this server.",
-        me.name, ERR_YOUREBANNEDCREEP, parv);
+    send_reply(sptr, ERR_NOMOTD);
+    send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
+              ":Connection from your host is refused on this server.");
     return;
   }
   fbstat(&sb, file);
@@ -175,14 +175,10 @@ static void killcomment(struct Client *sptr, char *parv, char *filename)
       *tmp = '\0';
     if ((tmp = strchr(line, '\r')))
       *tmp = '\0';
-    /* sendto_one(sptr,
-     * ":%s %d %s : %s.",
-     * me.name, ERR_YOUREBANNEDCREEP, parv,line); */
-    sendto_one(sptr, rpl_str(RPL_MOTD), me.name, parv, line);
+    send_reply(sptr, RPL_MOTD, line);
   }
-  sendto_one(sptr,
-      ":%s %d %s :Connection from your host is refused on this server.",
-      me.name, ERR_YOUREBANNEDCREEP, parv);
+  send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
+            ":Connection from your host is refused on this server.");
   fbclose(file);
 }
 
@@ -301,6 +297,7 @@ static void lookup_confhost(struct ConfItem *aconf)
   if (IsDigit(*aconf->host)) {
     /*
      * rfc 1035 sez host names may not start with a digit
+     * XXX - this has changed code needs to be updated
      */
     aconf->ipnum.s_addr = inet_addr(aconf->host);
     if (INADDR_NONE == aconf->ipnum.s_addr) {
@@ -489,38 +486,33 @@ static int validate_hostent(struct hostent* hp)
 
 /*
  * check_limit_and_attach - check client limits and attach I:line
+ *
+ * Made it accept 1 charactor, and 2 charactor limits (0->99 now), 
+ * and dislallow more than 255 people here as well as in ipcheck.
+ * removed the old "ONE" scheme too.
+ *  -- Isomer 2000-06-22
  */
-static int check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
+static enum AuthorizationCheckResult
+check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
 {
+  int number = 255;
+  
   if (aconf->passwd) {
-    /* Special case: exactly one digit */
-    if (IsDigit(*aconf->passwd) && !aconf->passwd[1]) {
-      /*
-       * Refuse connections when there are already <digit>
-       * clients connected with the same IP number
-       */
-      unsigned short nr = *aconf->passwd - '0';
-      if (IPcheck_nr(cptr) > nr)
-        return ACR_TOO_MANY_FROM_IP; /* Already got nr with that ip# */
-    }
-#ifdef USEONE
-    else if (0 == strcmp(aconf->passwd, "ONE")) {
-      int i;
-      for (i = HighestFd; i > -1; --i) {
-        if (LocalClientArray[i] && MyUser(LocalClientArray[i]) &&
-            LocalClientArray[i]->ip.s_addr == cptr->ip.s_addr)
-          return ACR_TOO_MANY_FROM_IP; /* Already got one with that ip# */
-      }
-    }
-#endif
+    if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
+      number = *aconf->passwd-'0';
+    else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) && 
+             !aconf->passwd[2])
+      number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
   }
+  if (ip_registry_count(cptr->ip.s_addr) > number)
+    return ACR_TOO_MANY_FROM_IP;
   return attach_conf(cptr, aconf);
 }
 
 /*
  * Find the first (best) I line to attach.
  */
-int attach_iline(struct Client*  cptr)
+enum AuthorizationCheckResult attach_iline(struct Client*  cptr)
 {
   struct ConfItem* aconf;
   const char*      hname;
@@ -633,7 +625,7 @@ static int is_attached(struct ConfItem *aconf, struct Client *cptr)
  * connection). Note, that this automaticly changes the
  * attachment if there was an old one...
  */
-int attach_conf(struct Client *cptr, struct ConfItem *aconf)
+enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
 {
   struct SLink *lp;
 
@@ -739,11 +731,8 @@ struct ConfItem* find_conf_exact(const char* name, const char* user,
   struct ConfItem *tmp;
   char userhost[USERLEN + HOSTLEN + 3];
 
-  /*
-   * XXX - buffer overflow possible, unchecked variables
-   */
   if (user)
-    sprintf_irc(userhost, "%s@%s", user, host);
+    ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
   else
     ircd_strncpy(userhost, host, sizeof(userhost) - 1);
 
@@ -796,7 +785,7 @@ struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
                                   int statmask)
 {
-  struct ConfItem* tmp;
+  struct ConfItem* tmp = NULL;
   assert(0 != host);
 
   if (HOSTLEN < strlen(host))
@@ -896,7 +885,8 @@ int rehash(struct Client *cptr, int sig)
   int               found_g = 0;
 
   if (1 == sig)
-    sendto_ops("Got signal SIGHUP, reloading ircd conf. file");
+    sendto_opmask_butone(0, SNO_OLDSNO,
+                        "Got signal SIGHUP, reloading ircd conf. file");
 
   while ((tmp2 = *tmp)) {
     if (tmp2->clients) {
@@ -941,8 +931,8 @@ int rehash(struct Client *cptr, int sig)
 
   mark_listeners_closing();
 
-  if (initconf(0) == -1)        /* This calls check_class(), */
-    check_class();                /* unless it fails */
+  if (!conf_init())        /* This calls check_class(), */
+    check_class();         /* unless it fails */
 
   /*
    * make sure that the server listener is re-added so it doesn't get
@@ -973,10 +963,16 @@ int rehash(struct Client *cptr, int sig)
         attach_confs_byname(acptr, acptr->name,
                             CONF_HUB | CONF_LEAF | CONF_UWORLD);
       }
+      /* Because admin's are getting so uppity about people managing to
+       * get past K/G's etc, we'll "fix" the bug by actually explaining
+       * whats going on.
+       */
       if ((found_g = find_kill(acptr))) {
-        sendto_op_mask(found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
-            found_g == -2 ? "G-line active for %s" : "K-line active for %s",
-            get_client_name(acptr, HIDE_IP));
+        sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
+                            found_g == -2 ? "G-line active for %s%s" :
+                            "K-line active for %s%s",
+                            IsUnknown(acptr) ? "Unregistered Client ":"",                   
+                            get_client_name(acptr, HIDE_IP));
         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
             "K-lined") == CPTR_KILLED)
           ret = CPTR_KILLED;
@@ -1004,18 +1000,18 @@ int rehash(struct Client *cptr, int sig)
 }
 
 /*
- * initconf
+ * conf_init
  *
  * Read configuration file.
  *
- * returns -1, if file cannot be opened
- *          0, if file opened
+ * returns 0, if file cannot be opened
+ *         1, if file read
  */
 
 #define MAXCONFLINKS 150
 
 
-int initconf(int opt)
+int conf_init(void)
 {
   static char quotes[9][2] = {
     {'b', '\b'},
@@ -1034,9 +1030,9 @@ int initconf(int opt)
   int ccount = 0;
   struct ConfItem *aconf = 0;
 
-  Debug((DEBUG_DEBUG, "initconf(): ircd.conf = %s", configfile));
+  Debug((DEBUG_DEBUG, "conf_init: ircd.conf = %s", configfile));
   if (0 == (file = fbopen(configfile, "r"))) {
-    return -1;
+    return 0;
   }
   while (fbgets(line, sizeof(line) - 1, file)) {
     if ((tmp = strchr(line, '\n')))
@@ -1250,7 +1246,7 @@ int initconf(int opt)
         len += strlen(aconf->host);
         newhost = (char*) MyMalloc(len);
         assert(0 != newhost);
-        sprintf_irc(newhost, "*@%s", aconf->host);
+       ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
         MyFree(aconf->host);
         aconf->host = newhost;
       }
@@ -1258,8 +1254,7 @@ int initconf(int opt)
     if (aconf->status & CONF_SERVER) {
       if (EmptyString(aconf->passwd))
         continue;
-      else if (!(opt & BOOT_QUICK))
-        lookup_confhost(aconf);
+      lookup_confhost(aconf);
     }
 
     /* Create expression tree from connect rule...
@@ -1315,7 +1310,7 @@ int initconf(int opt)
   fbclose(file);
   check_class();
   nextping = nextconnect = CurrentTime;
-  return 0;
+  return 1;
 }
 
 /* read_tlines 
@@ -1412,24 +1407,20 @@ int find_kill(struct Client *cptr)
         break;
       else if (is_comment(tmp->passwd))
         break;
-      else if (check_time_interval(tmp->passwd, reply))
+      else if (check_time_interval(tmp->passwd, reply, sizeof(reply)))
         break;
     }
   }
   if (reply[0])
-    sendto_one(cptr, reply, me.name, ERR_YOUREBANNEDCREEP, cptr->name);
+    send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, reply);
   else if (tmp) {
     if (EmptyString(tmp->passwd))
-      sendto_one(cptr,
-          ":%s %d %s :Connection from your host is refused on this server.",
-          me.name, ERR_YOUREBANNEDCREEP, cptr->name);
+      send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
+                ":Connection from your host is refused on this server.");
     else {
       if (*tmp->passwd == '"') {
-        char *sbuf =
-            sprintf_irc(sendbuf, ":%s %d %s :%s", me.name, ERR_YOUREBANNEDCREEP,
-            cptr->name, &tmp->passwd[1]);
-        sbuf[-1] = '.';                /* Overwrite last quote with a dot */
-        sendbufto_one(cptr);
+       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
+                  strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
       }
       else if (*tmp->passwd == '!')
         killcomment(cptr, cptr->name, &tmp->passwd[1]);
@@ -1437,17 +1428,17 @@ int find_kill(struct Client *cptr)
 #ifdef COMMENT_IS_FILE
         killcomment(cptr, cptr->name, tmp->passwd);
 #else
-        sendto_one(cptr, ":%s %d %s :%s.", me.name, ERR_YOUREBANNEDCREEP,
-            cptr->name, tmp->passwd);
+       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
+                  tmp->passwd);
 #endif
     }
   }
 
   /* find active glines */
   /* added a check against the user's IP address to find_gline() -Kev */
-  else if ((agline = find_gline(cptr, NULL)) && GlineIsActive(agline))
-    sendto_one(cptr, ":%s %d %s :%s.", me.name, ERR_YOUREBANNEDCREEP,
-        cptr->name, agline->reason);
+  else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
+    send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
+              GlineReason(agline));
   else
     agline = NULL;                /* if a gline was found, it was inactive */
 
@@ -1500,7 +1491,7 @@ struct MotdItem* read_motd(const char* motdfile)
  */
 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
 {
-  int acr;
+  enum AuthorizationCheckResult acr = ACR_OK;
 
   ClearAccess(cptr);
 
@@ -1547,7 +1538,8 @@ int conf_check_server(struct Client *cptr)
   if (IsConnecting(cptr) || IsHandshake(cptr)) {
     c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
     if (!c_conf) {
-      sendto_ops("Connect Error: lost C:line for %s", cptr->name);
+      sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
+                          cptr->name);
       det_confs_butmask(cptr, 0);
       return -1;
     }