Improve support for IPv4 vs IPv6 virtual hosts (fixes SF bugs #1087699, #1087668).
[ircu2.10.12-pk.git] / ircd / ircd_parser.y
index 3aa0afcad9c8b6fc5218b5eac54e0a0a7d8eae74..3514f69f0227862fdaf9d2b5bbb27da8a44b8625 100644 (file)
@@ -53,7 +53,6 @@
 #include "s_misc.h"
 #include "send.h"
 #include "struct.h"
-#include "support.h"
 #include "sys.h"
 #include <stdlib.h>
 #include <stdio.h>
   extern struct ServerConf* serverConfList;
   extern struct s_map*      GlobalServiceMapList;
   extern struct qline*      GlobalQuarantineList;
 
   int yylex(void);
   /* Now all the globals we need :/... */
-  int tping, tconn, maxlinks, sendq, port, invert;
-  int stringno;
-  char *name, *pass, *host, *origin;
+  int tping, tconn, maxlinks, sendq, port, invert, stringno;
+  char *name, *pass, *host, *ip, *username, *origin, *hub_limit;
   char *stringlist[MAX_STRINGS];
   struct ConnectionClass *c_class;
-  struct ConfItem *aconf;
   struct DenyConf *dconf;
   struct ServerConf *sconf;
   struct qline *qconf = NULL;
@@ -108,9 +104,12 @@ static void parse_error(char *pattern,...) {
 %token PINGFREQ
 %token CONNECTFREQ
 %token MAXLINKS
+%token MAXHOPS
 %token SENDQ
 %token NAME
 %token HOST
+%token IP
+%token USERNAME
 %token PASS
 %token LOCAL
 %token SECONDS
@@ -150,7 +149,6 @@ static void parse_error(char *pattern,...) {
 %token TFILE
 %token RULE
 %token ALL
-%token IP
 %token FEATURES
 %token QUARANTINE
 %token PSEUDO
@@ -182,7 +180,7 @@ static void parse_error(char *pattern,...) {
 /* Blocks in the config file... */
 blocks: blocks block | block;
 block: adminblock | generalblock | classblock | connectblock |
-       serverblock | operblock | portblock | jupeblock | clientblock |
+       uworldblock | operblock | portblock | jupeblock | clientblock |
        killblock | cruleblock | motdblock | featuresblock | quarantineblock |
        pseudoblock | iauthblock | error;
 
@@ -301,7 +299,13 @@ generaldesc: DESCRIPTION '=' QSTRING ';'
 
 generalvhost: VHOST '=' QSTRING ';'
 {
-  ircd_aton(&VirtualHost.addr, $3);
+  struct irc_in_addr addr;
+  if (!ircd_aton(&addr, $3))
+      parse_error("Invalid virtual host '%s'.", $3);
+  else if (irc_in_addr_is_ipv4(&addr))
+    memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
+  else
+    memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
 };
 
 adminblock: ADMIN '{' adminitems '}'
@@ -331,14 +335,7 @@ admincontact: CONTACT '=' QSTRING ';'
 };
 
 classblock: CLASS {
-  name = NULL;
   tping = 90;
-  tconn = 0;
-  maxlinks = 0;
-  sendq = 0;
-  pass = NULL;
-  memset(&privs, 0, sizeof(privs));
-  memset(&privs_dirty, 0, sizeof(privs_dirty));
 } '{' classitems '}'
 {
   if (name != NULL)
@@ -353,7 +350,13 @@ classblock: CLASS {
   else {
    parse_error("Missing name in class block");
   }
+  name = NULL;
   pass = NULL;
+  tconn = 0;
+  maxlinks = 0;
+  sendq = 0;
+  memset(&privs, 0, sizeof(privs));
+  memset(&privs_dirty, 0, sizeof(privs_dirty));
 } ';';
 classitems: classitem classitems | classitem;
 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
@@ -381,33 +384,28 @@ classsendq: SENDQ '=' sizespec ';'
 };
 classusermode: USERMODE '=' QSTRING ';'
 {
-  if (pass)
-    MyFree(pass);
+  MyFree(pass);
   DupString(pass, $3);
 };
 
 connectblock: CONNECT
 {
- name = pass = host = origin = NULL;
- c_class = NULL;
- port = 0;
+ maxlinks = 65535;
 } '{' connectitems '}'
 {
  if (name != NULL && pass != NULL && host != NULL && c_class != NULL
      && !strchr(host, '*') && !strchr(host, '?'))
  {
-   aconf = make_conf();
-   aconf->status = CONF_SERVER;
+   struct ConfItem *aconf = make_conf(CONF_SERVER);
    aconf->name = name;
    aconf->origin_name = origin;
    aconf->passwd = pass;
    aconf->conn_class = c_class;
    aconf->address.port = port;
-   aconf->status = CONF_SERVER;
    aconf->host = host;
-   aconf->next = GlobalConfList;
+   aconf->maximum = maxlinks;
+   aconf->hub_limit = hub_limit;
    lookup_confhost(aconf);
-   GlobalConfList = aconf;
  }
  else
  {
@@ -415,13 +413,17 @@ connectblock: CONNECT
    MyFree(pass);
    MyFree(host);
    MyFree(origin);
+   MyFree(hub_limit);
    parse_error("Bad connect block");
  }
- name = pass = host = origin = NULL;
+ name = pass = host = origin = hub_limit = NULL;
+ c_class = NULL;
+ port = 0;
 }';';
 connectitems: connectitem connectitems | connectitem;
 connectitem: connectname | connectpass | connectclass | connecthost
-              | connectport | connectvhost | error;
+              | connectport | connectvhost | connectleaf | connecthub
+              | connecthublimit | connectmaxhops | error;
 connectname: NAME '=' QSTRING ';'
 {
  MyFree(name);
@@ -450,144 +452,111 @@ connectvhost: VHOST '=' QSTRING ';'
  MyFree(origin);
  DupString(origin, $3);
 };
-
-serverblock: SERVER
+connectleaf: LEAF ';'
 {
- aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
- memset(aconf, 0, sizeof(*aconf));
- aconf->status = CONF_LEAF;
-} '{' serveritems '}'
-{
- if (aconf->status == 0)
- {
-   MyFree(aconf->host);
-   MyFree(aconf->name);
-   MyFree(aconf);
-   aconf = NULL;
-   parse_error("Bad server block");
- }
- else
- {
-   aconf->next = GlobalConfList;
-   GlobalConfList = aconf;
- }
-} ';';
-serveritems: serveritem serveritems | serveritem;
-serveritem: servername | servermask | serverhub | serverleaf |
-             serveruworld | error;
-servername: NAME '=' QSTRING
+ maxlinks = 0;
+};
+connecthub: HUB ';'
 {
- MyFree(aconf->name);
- DupString(aconf->name, $3);
-} ';' ;
-servermask: MASK '=' QSTRING
+ MyFree(hub_limit);
+ DupString(hub_limit, "*");
+};
+connecthublimit: HUB '=' QSTRING ';'
 {
- MyFree(aconf->host);
- DupString(aconf->host, $3);
-} ';' ;
-/* XXX - perhaps we should do this the hybrid way in connect blocks
- * instead -A1kmm. */
-serverhub: HUB '=' YES ';'
+ MyFree(hub_limit);
+ DupString(hub_limit, $3);
+};
+connectmaxhops: MAXHOPS '=' expr ';'
 {
- aconf->status |= CONF_HUB;
- aconf->status &= ~CONF_LEAF;
+  maxlinks = $3;
 }
-| HUB '=' NO
-{
- aconf->status &= ~CONF_HUB;
-} ';'; 
-serverleaf: LEAF '=' YES ';'
+
+uworldblock: UWORLD '{' uworlditems '}' ';'
 {
- if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
-  aconf->status |= CONF_LEAF;
+ if (name)
+ {
+  struct ConfItem *aconf = make_conf(CONF_UWORLD);
+  aconf->host = name;
+ }
  else
-  parse_error("Server is both leaf and a hub");
-}
-| LEAF '=' NO ';'
-{
aconf->status &= ~CONF_LEAF;
+ {
+  MyFree(name);
+  parse_error("Bad UWorld block");
+ }
name = NULL;
 };
-serveruworld: UWORLD '=' YES ';'
+uworlditems: uworlditem uworlditems | uworlditem;
+uworlditem: uworldname | error;
+uworldname: NAME '=' QSTRING ';'
 {
- aconf->status |= CONF_UWORLD;
- aconf->status &= ~CONF_LEAF;
-}
-| UWORLD '=' NO ';'
-{
-  aconf->status &= ~CONF_UWORLD;
+ MyFree(name);
+ DupString(name, $3);
 };
 
-operblock: OPER
+operblock: OPER '{' operitems '}' ';'
 {
-  aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
-  memset(aconf, 0, sizeof(*aconf));
-  memset(&privs, 0, sizeof(privs));
-  memset(&privs_dirty, 0, sizeof(privs_dirty));
-  aconf->status = CONF_OPERATOR;
-} '{' operitems '}' ';'
-{
-  if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
-      && aconf->conn_class != NULL)
+  if (name && pass && host && c_class)
   {
+    struct ConfItem *aconf = make_conf(CONF_OPERATOR);
+    aconf->name = name;
+    aconf->passwd = pass;
+    conf_parse_userhost(aconf, host);
+    aconf->conn_class = c_class;
     memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
     memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
-    if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
-        && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
-      parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
-    aconf->next = GlobalConfList;
-    GlobalConfList = aconf;
+    if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
+        && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
+      parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
   }
   else
   {
     log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
-    MyFree(aconf->name);
-    MyFree(aconf->passwd);
-    MyFree(aconf->host);
-    MyFree(aconf);
-    aconf = NULL;
+    MyFree(name);
+    MyFree(pass);
+    MyFree(host);
   }
+  name = pass = host = NULL;
+  c_class = NULL;
+  memset(&privs, 0, sizeof(privs));
+  memset(&privs_dirty, 0, sizeof(privs_dirty));
 };
 operitems: operitem | operitems operitem;
 operitem: opername | operpass | operhost | operclass | priv | error;
-
 opername: NAME '=' QSTRING ';'
 {
-  MyFree(aconf->name);
-  DupString(aconf->name, $3);
+  MyFree(name);
+  DupString(name, $3);
 };
-
 operpass: PASS '=' QSTRING ';'
 {
-  MyFree(aconf->passwd);
-  DupString(aconf->passwd, $3);
+  MyFree(pass);
+  DupString(pass, $3);
 };
-
 operhost: HOST '=' QSTRING ';'
 {
- MyFree(aconf->host);
+ MyFree(host);
  if (!strchr($3, '@'))
  {
    int uh_len;
    char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
    ircd_snprintf(0, b, uh_len, "*@%s", $3);
-   aconf->host = b;
+   host = b;
  }
  else
-   DupString(aconf->host, $3);
+   DupString(host, $3);
 };
-
 operclass: CLASS '=' QSTRING ';'
 {
aconf->conn_class = find_class($3);
c_class = find_class($3);
 };
 
 priv: privtype '=' yesorno ';'
 {
-  PrivSet(&privs_dirty, $1);
+  FlagSet(&privs_dirty, $1);
   if (($3 == 1) ^ invert)
-    PrivSet(&privs, $1);
+    FlagSet(&privs, $1);
   else
-    PrivClr(&privs, $1);
+    FlagClr(&privs, $1);
   invert = 0;
 };
 
@@ -625,15 +594,7 @@ privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
 
 /* The port block... */
-portblock: PORT {
-  port = 0;
-  host = NULL;
-  /* Hijack these for is_server, is_hidden to cut down on globals... */
-  tconn = 0;
-  tping = 0;
-  /* and this for mask... */
-  pass = NULL;
-} '{' portitems '}' ';'
+portblock: PORT '{' portitems '}' ';'
 {
   if (port > 0 && port <= 0xFFFF)
   {
@@ -646,6 +607,7 @@ portblock: PORT {
   MyFree(host);
   MyFree(pass);
   host = pass = NULL;
+  port = tconn = tping = 0;
 };
 portitems: portitem portitems | portitem;
 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
@@ -684,61 +646,59 @@ porthidden: HIDDEN '=' YES ';'
 
 clientblock: CLIENT
 {
-  aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
-  memset(aconf, 0, sizeof(*aconf));
-  aconf->status = CONF_CLIENT;
-} '{' clientitems '}'
-{
-  if ((aconf->host != NULL || aconf->name!=NULL))
-  {
-    if (aconf->host == NULL)
-      DupString(aconf->host, "");
-    if (aconf->name == NULL)
-      DupString(aconf->name, "");
-    if (aconf->conn_class == NULL)
-      aconf->conn_class = find_class("default");
-    aconf->next = GlobalConfList;
-    GlobalConfList = aconf;
-    aconf = NULL;
-  }
+  maxlinks = 65535;
+}
+'{' clientitems '}' ';'
+{
+  struct ConfItem *aconf = make_conf(CONF_CLIENT);
+  unsigned char addrbits;
+  aconf->username = username;
+  aconf->host = host;
+  if (ip && ipmask_parse(ip, &aconf->address.addr, &addrbits))
+    aconf->addrbits = addrbits;
   else
-  {
-   MyFree(aconf->host);
-   MyFree(aconf->passwd);
-   MyFree(aconf);
-   aconf = NULL;
-   parse_error("Bad client block");
-  }
-} ';';
-clientitems: clientitem clientitems | clientitem;
-clientitem: clienthost | clientclass | clientpass | clientip | error;
-clientip: IP '=' QSTRING ';'
-{
-  MyFree(aconf->host);
-  DupString(aconf->host, $3);
+    aconf->addrbits = -1;
+  aconf->conn_class = c_class ? c_class : find_class("default");
+  aconf->maximum = maxlinks;
+  host = NULL;
+  username = NULL;
+  c_class = NULL;
+  MyFree(ip);
 };
-
+clientitems: clientitem clientitems | clientitem;
+clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | error;
 clienthost: HOST '=' QSTRING ';'
 {
-  MyFree(aconf->name);
-  DupString(aconf->name, $3);
+  MyFree(host);
+  DupString(host, $3);
 };
-
+clientip: IP '=' QSTRING ';'
+{
+  MyFree(ip);
+  DupString(ip, $3);
+}
+clientusername: USERNAME '=' QSTRING ';'
+{
+  MyFree(username);
+  DupString(username, $3);
+}
 clientclass: CLASS '=' QSTRING ';'
 {
-  aconf->conn_class = find_class($3);
+  c_class = find_class($3);
 };
-
 clientpass: PASS '=' QSTRING ';'
 {
-  MyFree(aconf->passwd);
-  DupString(aconf->passwd, $3);
+  MyFree(pass);
+  DupString(pass, $3);
+};
+clientmaxlinks: MAXLINKS '=' expr ';'
+{
+  maxlinks = $3;
 };
 
 killblock: KILL
 {
-  dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
-  memset(dconf, 0, sizeof(*dconf));
+  dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
 } '{' killitems '}'
 {
   if (dconf->hostmask != NULL)
@@ -747,16 +707,15 @@ killblock: KILL
       DupString(dconf->usermask, "*");
     dconf->next = denyConfList;
     denyConfList = dconf;
-    dconf = NULL;
   }
   else
   {
     MyFree(dconf->hostmask);
     MyFree(dconf->message);
     MyFree(dconf);
-    dconf = NULL;
     parse_error("Bad kill block");
   }
+  dconf = NULL;
 } ';';
 killitems: killitem killitems | killitem;
 killitem: killuhost | killreal | killreasonfile | killreason | error;
@@ -806,9 +765,8 @@ killreasonfile: TFILE '=' QSTRING ';'
 
 cruleblock: CRULE
 {
-  host = pass = NULL;
   tconn = CRULE_AUTO;
-} '{' cruleitems '}'
+} '{' cruleitems '}' ';'
 {
   struct CRuleNode *node;
   if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
@@ -827,7 +785,8 @@ cruleblock: CRULE
     MyFree(pass);
     parse_error("Bad CRule block");
   }
-} ';';
+  host = pass = NULL;
+};
 
 cruleitems: cruleitem cruleitems | cruleitem;
 cruleitem: cruleserver | crulerule | cruleall | error;
@@ -853,16 +812,14 @@ cruleall: ALL '=' YES ';'
  tconn = CRULE_AUTO;
 };
 
-motdblock: MOTD {
- pass = host = NULL;
-} '{' motditems '}'
+motdblock: MOTD '{' motditems '}' ';'
 {
   if (host != NULL && pass != NULL)
     motd_add(host, pass);
   MyFree(host);
   MyFree(pass);
   host = pass = NULL;
-} ';';
+};
 
 motditems: motditem motditems | motditem;
 motditem: motdhost | motdfile | error;
@@ -903,16 +860,7 @@ extrastring: QSTRING
 
 quarantineblock: QUARANTINE '{'
 {
-  if (qconf != NULL)
-    qconf = (struct qline*) MyMalloc(sizeof(*qconf));
-  else
-  {
-    if (qconf->chname != NULL)
-      MyFree(qconf->chname);
-    if (qconf->reason != NULL)
-      MyFree(qconf->reason);
-  }
-  memset(qconf, 0, sizeof(*qconf));
+  qconf = (struct qline*) MyCalloc(1, sizeof(*qconf));
 } quarantineitems '}' ';'
 {
   if (qconf->chname == NULL || qconf->reason == NULL)
@@ -993,13 +941,11 @@ pseudonick: NICK '=' QSTRING ';'
 
 iauthblock: IAUTH '{'
 {
-  pass = host = NULL;
-  port = 0;
   tconn = 60;
   tping = 60;
 } iauthitems '}' ';'
 {
-  if (!name || !host || !port) {
+  if (!host || !port) {
     log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
     return 0;
   }
@@ -1007,6 +953,7 @@ iauthblock: IAUTH '{'
   MyFree(pass);
   MyFree(host);
   pass = host = NULL;
+  port = 0;
 };
 
 iauthitems: iauthitem iauthitems | iauthitem;