2 * ircd_parser.y: A yacc/bison parser for ircd config files.
3 * This is part of ircu, an Internet Relay Chat server.
4 * The contents of this file are Copyright 2001 Diane Bruce,
5 * Andrew Miller, the ircd-hybrid team and the ircu team.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
29 #include "ircd_features.h"
34 #include "ircd_alloc.h"
35 #include "ircd_chattr.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #include "ircd_string.h"
60 #include <arpa/inet.h>
61 #define MAX_STRINGS 80 /* Maximum number of feature params. */
62 extern struct LocalConf localConf;
63 extern struct DenyConf* denyConfList;
64 extern struct CRuleConf* cruleConfList;
65 extern struct ServerConf* serverConfList;
66 extern struct s_map* GlobalServiceMapList;
67 extern struct qline* GlobalQuarantineList;
70 /* Now all the globals we need :/... */
71 int tping, tconn, maxlinks, sendq, port, invert, stringno, flags;
72 char *name, *pass, *host, *ip, *username, *origin, *hub_limit;
73 char *stringlist[MAX_STRINGS];
74 struct ListenerFlags listen_flags;
75 struct ConnectionClass *c_class;
76 struct DenyConf *dconf;
77 struct ServerConf *sconf;
80 struct Privs privs_dirty;
82 static void parse_error(char *pattern,...) {
83 static char error_buffer[1024];
86 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
88 yyerror(error_buffer);
161 /* and now a lot of privileges... */
162 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
163 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
164 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
165 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
166 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
167 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
168 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
169 %token TPRIV_LIST_CHAN
170 /* and some types... */
172 %type <num> timespec timefactor factoredtimes factoredtime
173 %type <num> expr yesorno privtype
183 /* Blocks in the config file... */
184 blocks: blocks block | block;
185 block: adminblock | generalblock | classblock | connectblock |
186 uworldblock | operblock | portblock | jupeblock | clientblock |
187 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
188 pseudoblock | iauthblock | error ';';
190 /* The timespec, sizespec and expr was ripped straight from
192 timespec: expr | factoredtimes;
194 factoredtimes: factoredtimes factoredtime
199 factoredtime: expr timefactor
204 timefactor: SECONDS { $$ = 1; }
205 | MINUTES { $$ = 60; }
206 | HOURS { $$ = 60 * 60; }
207 | DAYS { $$ = 60 * 60 * 24; }
208 | WEEKS { $$ = 60 * 60 * 24 * 7; }
209 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
210 | YEARS { $$ = 60 * 60 * 24 * 365; }
211 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
224 $$ = $1 * 1024 * 1024;
227 $$ = $1 * 1024 * 1024 * 1024;
230 $$ = $1 * 1024 * 1024 * 1024;
234 /* this is an arithmetic expression */
251 /* leave this out until we find why it makes BSD yacc dump core -larne
252 | '-' expr %prec NEG {
260 jupeblock: JUPE '{' jupeitems '}' ';' ;
261 jupeitems: jupeitem jupeitems | jupeitem;
263 jupenick: NICK '=' QSTRING ';'
269 generalblock: GENERAL
271 /* Zero out the vhost addresses, in case they were removed. */
272 memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
273 memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
274 } '{' generalitems '}' ';' {
275 if (localConf.name == NULL)
276 parse_error("Your General block must contain a name.");
277 if (localConf.numeric == 0)
278 parse_error("Your General block must contain a numeric (between 1 and 4095).");
280 generalitems: generalitem generalitems | generalitem;
281 generalitem: generalnumeric | generalname | generalvhost | generaldesc;
282 generalnumeric: NUMERIC '=' NUMBER ';'
284 if (localConf.numeric == 0)
285 localConf.numeric = $3;
286 else if (localConf.numeric != $3)
287 parse_error("Redefinition of server numeric %i (%i)", $3,
291 generalname: NAME '=' QSTRING ';'
293 if (localConf.name == NULL)
296 if (strcmp(localConf.name, $3))
297 parse_error("Redefinition of server name %s (%s)", $3,
303 generaldesc: DESCRIPTION '=' QSTRING ';'
305 MyFree(localConf.description);
306 localConf.description = $3;
307 ircd_strncpy(cli_info(&me), $3, REALLEN);
310 generalvhost: VHOST '=' QSTRING ';'
312 struct irc_in_addr addr;
315 if (!strcmp(vhost, "*")) {
316 /* This traditionally meant bind to all interfaces and connect
317 * from the default. */
318 } else if (!ircd_aton(&addr, vhost))
319 parse_error("Invalid virtual host '%s'.", vhost);
320 else if (irc_in_addr_is_ipv4(&addr))
321 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
323 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
329 MyFree(localConf.location1);
330 MyFree(localConf.location2);
331 MyFree(localConf.contact);
332 localConf.location1 = localConf.location2 = localConf.contact = NULL;
334 '{' adminitems '}' ';'
336 if (localConf.location1 == NULL)
337 DupString(localConf.location1, "");
338 if (localConf.location2 == NULL)
339 DupString(localConf.location2, "");
340 if (localConf.contact == NULL)
341 DupString(localConf.contact, "");
343 adminitems: adminitems adminitem | adminitem;
344 adminitem: adminlocation | admincontact;
345 adminlocation: LOCATION '=' QSTRING ';'
347 if (localConf.location1 == NULL)
348 localConf.location1 = $3;
349 else if (localConf.location2 == NULL)
350 localConf.location2 = $3;
351 else /* Otherwise just drop it. -A1kmm */
354 admincontact: CONTACT '=' QSTRING ';'
356 MyFree(localConf.contact);
357 localConf.contact = $3;
362 } '{' classitems '}' ';'
366 struct ConnectionClass *c_class;
367 add_class(name, tping, tconn, maxlinks, sendq);
368 c_class = find_class(name);
369 MyFree(c_class->default_umode);
370 c_class->default_umode = pass;
371 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
372 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
375 parse_error("Missing name in class block");
382 memset(&privs, 0, sizeof(privs));
383 memset(&privs_dirty, 0, sizeof(privs_dirty));
385 classitems: classitem classitems | classitem;
386 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
387 classsendq | classusermode | priv;
388 classname: NAME '=' QSTRING ';'
393 classpingfreq: PINGFREQ '=' timespec ';'
397 classconnfreq: CONNECTFREQ '=' timespec ';'
401 classmaxlinks: MAXLINKS '=' expr ';'
405 classsendq: SENDQ '=' sizespec ';'
409 classusermode: USERMODE '=' QSTRING ';'
415 connectblock: CONNECT
418 flags = CONF_AUTOCONNECT;
419 } '{' connectitems '}' ';'
421 struct ConfItem *aconf = NULL;
423 parse_error("Missing name in connect block");
424 else if (pass == NULL)
425 parse_error("Missing password in connect block");
426 else if (strlen(pass) > PASSWDLEN)
427 parse_error("Password too long in connect block");
428 else if (host == NULL)
429 parse_error("Missing host in connect block");
430 else if (strchr(host, '*') || strchr(host, '?'))
431 parse_error("Invalid host '%s' in connect block", host);
432 else if (c_class == NULL)
433 parse_error("Missing or non-existent class in connect block");
435 aconf = make_conf(CONF_SERVER);
437 aconf->origin_name = origin;
438 aconf->passwd = pass;
439 aconf->conn_class = c_class;
440 aconf->address.port = port;
442 aconf->maximum = maxlinks;
443 aconf->hub_limit = hub_limit;
444 aconf->flags = flags;
445 lookup_confhost(aconf);
454 name = pass = host = origin = hub_limit = NULL;
458 connectitems: connectitem connectitems | connectitem;
459 connectitem: connectname | connectpass | connectclass | connecthost
460 | connectport | connectvhost | connectleaf | connecthub
461 | connecthublimit | connectmaxhops | connectauto;
462 connectname: NAME '=' QSTRING ';'
467 connectpass: PASS '=' QSTRING ';'
472 connectclass: CLASS '=' QSTRING ';'
474 c_class = find_class($3);
476 parse_error("No such connection class '%s' for Connect block", $3);
479 connecthost: HOST '=' QSTRING ';'
484 connectport: PORT '=' NUMBER ';'
488 connectvhost: VHOST '=' QSTRING ';'
493 connectleaf: LEAF ';'
500 DupString(hub_limit, "*");
502 connecthublimit: HUB '=' QSTRING ';'
507 connectmaxhops: MAXHOPS '=' expr ';'
511 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
512 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
514 uworldblock: UWORLD '{' uworlditems '}' ';';
515 uworlditems: uworlditem uworlditems | uworlditem;
516 uworlditem: uworldname;
517 uworldname: NAME '=' QSTRING ';'
519 make_conf(CONF_UWORLD)->host = $3;
522 operblock: OPER '{' operitems '}' ';'
524 struct ConfItem *aconf = NULL;
526 parse_error("Missing name in operator block");
527 else if (pass == NULL)
528 parse_error("Missing password in operator block");
529 /* Do not check password length because it may be crypted. */
530 else if (host == NULL)
531 parse_error("Missing host in operator block");
532 else if (c_class == NULL)
533 parse_error("Invalid or missing class in operator block");
534 else if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
535 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
536 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
538 aconf = make_conf(CONF_OPERATOR);
540 aconf->passwd = pass;
541 conf_parse_userhost(aconf, host);
542 aconf->conn_class = c_class;
543 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
544 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
551 name = pass = host = NULL;
553 memset(&privs, 0, sizeof(privs));
554 memset(&privs_dirty, 0, sizeof(privs_dirty));
556 operitems: operitem | operitems operitem;
557 operitem: opername | operpass | operhost | operclass | priv;
558 opername: NAME '=' QSTRING ';'
563 operpass: PASS '=' QSTRING ';'
568 operhost: HOST '=' QSTRING ';'
571 if (!strchr($3, '@'))
574 host = (char*) MyMalloc((uh_len = strlen($3)+3));
575 ircd_snprintf(0, host, uh_len, "*@%s", $3);
581 operclass: CLASS '=' QSTRING ';'
583 c_class = find_class($3);
585 parse_error("No such connection class '%s' for Operator block", $3);
589 priv: privtype '=' yesorno ';'
591 FlagSet(&privs_dirty, $1);
592 if (($3 == 1) ^ invert)
599 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
600 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
601 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
602 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
603 KILL { $$ = PRIV_KILL; } |
604 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
605 TPRIV_REHASH { $$ = PRIV_REHASH; } |
606 TPRIV_RESTART { $$ = PRIV_RESTART; } |
607 TPRIV_DIE { $$ = PRIV_DIE; } |
608 TPRIV_GLINE { $$ = PRIV_GLINE; } |
609 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
610 JUPE { $$ = PRIV_JUPE; } |
611 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
612 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
613 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
614 TPRIV_SET { $$ = PRIV_SET; } |
615 TPRIV_WHOX { $$ = PRIV_WHOX; } |
616 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
617 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
618 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
619 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
620 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
621 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
622 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
623 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
624 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
625 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
626 TPRIV_LIST_CHAN { $$ = PRIV_LIST_CHAN; } |
627 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
628 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
629 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
630 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
632 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
634 /* The port block... */
635 portblock: PORT '{' portitems '}' ';'
637 if (port > 0 && port <= 0xFFFF)
638 add_listener(port, host, pass, &listen_flags);
640 parse_error("Port %d is out of range", port);
643 memset(&listen_flags, 0, sizeof(listen_flags));
647 portitems: portitem portitems | portitem;
648 portitem: portnumber | portvhost | portmask | portserver | porthidden;
649 portnumber: PORT '=' NUMBER ';'
654 portvhost: VHOST '=' QSTRING ';'
660 portmask: MASK '=' QSTRING ';'
666 portserver: SERVER '=' YES ';'
668 FlagSet(&listen_flags, LISTEN_SERVER);
669 } | SERVER '=' NO ';'
671 FlagClr(&listen_flags, LISTEN_SERVER);
674 porthidden: HIDDEN '=' YES ';'
676 FlagSet(&listen_flags, LISTEN_HIDDEN);
677 } | HIDDEN '=' NO ';'
679 FlagClr(&listen_flags, LISTEN_HIDDEN);
687 '{' clientitems '}' ';'
689 struct ConfItem *aconf = 0;
690 struct irc_in_addr addr;
691 unsigned char addrbits = 0;
694 parse_error("Invalid or missing class in Client block");
695 else if (pass && strlen(pass) > PASSWDLEN)
696 parse_error("Password too long in connect block");
697 else if (ip && !ipmask_parse(ip, &addr, &addrbits))
698 parse_error("Invalid IP address %s in Client block", ip);
700 aconf = make_conf(CONF_CLIENT);
701 aconf->username = username;
704 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
706 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
707 aconf->address.port = port;
708 aconf->addrbits = addrbits;
710 aconf->conn_class = c_class;
711 aconf->maximum = maxlinks;
712 aconf->passwd = pass;
727 clientitems: clientitem clientitems | clientitem;
728 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
729 clienthost: HOST '=' QSTRING ';'
731 char *sep = strchr($3, '@');
736 DupString(host, sep);
742 clientip: IP '=' QSTRING ';'
745 sep = strchr($3, '@');
756 clientusername: USERNAME '=' QSTRING ';'
761 clientclass: CLASS '=' QSTRING ';'
763 c_class = find_class($3);
765 parse_error("No such connection class '%s' for Client block", $3);
768 clientpass: PASS '=' QSTRING ';'
773 clientmaxlinks: MAXLINKS '=' expr ';'
777 clientport: PORT '=' expr ';'
784 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
785 } '{' killitems '}' ';'
787 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
788 dconf->next = denyConfList;
789 denyConfList = dconf;
793 MyFree(dconf->usermask);
794 MyFree(dconf->hostmask);
795 MyFree(dconf->realmask);
796 MyFree(dconf->message);
798 parse_error("Kill block must match on at least one of username, host or realname");
802 killitems: killitem killitems | killitem;
803 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
804 killuhost: HOST '=' QSTRING ';'
807 MyFree(dconf->hostmask);
808 MyFree(dconf->usermask);
809 if ((h = strchr($3, '@')) == NULL)
811 DupString(dconf->usermask, "*");
812 dconf->hostmask = $3;
817 DupString(dconf->hostmask, h);
818 dconf->usermask = $3;
820 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
823 killusername: USERNAME '=' QSTRING ';'
825 MyFree(dconf->usermask);
826 dconf->usermask = $3;
829 killreal: REAL '=' QSTRING ';'
831 MyFree(dconf->realmask);
832 dconf->realmask = $3;
835 killreason: REASON '=' QSTRING ';'
837 dconf->flags &= ~DENY_FLAGS_FILE;
838 MyFree(dconf->message);
842 killreasonfile: TFILE '=' QSTRING ';'
844 dconf->flags |= DENY_FLAGS_FILE;
845 MyFree(dconf->message);
852 } '{' cruleitems '}' ';'
854 struct CRuleNode *node = NULL;
856 parse_error("Missing host in crule block");
857 else if (pass == NULL)
858 parse_error("Missing rule in crule block");
859 else if ((node = crule_parse(pass)) == NULL)
860 parse_error("Invalid rule '%s' in crule block", pass);
863 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
868 p->next = cruleConfList;
880 cruleitems: cruleitem cruleitems | cruleitem;
881 cruleitem: cruleserver | crulerule | cruleall;
883 cruleserver: SERVER '=' QSTRING ';'
890 crulerule: RULE '=' QSTRING ';'
896 cruleall: ALL '=' YES ';'
904 motdblock: MOTD '{' motditems '}' ';'
906 if (host != NULL && pass != NULL)
907 motd_add(host, pass);
913 motditems: motditem motditems | motditem;
914 motditem: motdhost | motdfile;
915 motdhost: HOST '=' QSTRING ';'
920 motdfile: TFILE '=' QSTRING ';'
925 featuresblock: FEATURES '{' featureitems '}' ';';
926 featureitems: featureitems featureitem | featureitem;
932 } '=' stringlist ';' {
934 feature_set(NULL, (const char * const *)stringlist, stringno);
935 for (ii = 0; ii < stringno; ++ii)
936 MyFree(stringlist[ii]);
939 stringlist: stringlist extrastring | extrastring;
942 if (stringno < MAX_STRINGS)
943 stringlist[stringno++] = $1;
948 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
949 quarantineitems: quarantineitems quarantineitem | quarantineitem;
950 quarantineitem: QSTRING '=' QSTRING ';'
952 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
955 qconf->next = GlobalQuarantineList;
956 GlobalQuarantineList = qconf;
959 pseudoblock: PSEUDO QSTRING '{'
961 smap = MyCalloc(1, sizeof(struct s_map));
969 parse_error("Missing name in pseudo %s block", smap->command);
970 else if (!smap->services)
971 parse_error("Missing nick in pseudo %s block", smap->command);
974 if (valid && register_mapping(smap))
976 smap->next = GlobalServiceMapList;
977 GlobalServiceMapList = smap;
986 pseudoitems: pseudoitem pseudoitems | pseudoitem;
987 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
988 pseudoname: NAME '=' QSTRING ';'
993 pseudoprepend: PREPEND '=' QSTRING ';'
995 MyFree(smap->prepend);
998 pseudonick: NICK '=' QSTRING ';'
1000 char *sep = strchr($3, '@');
1003 size_t slen = strlen($3);
1004 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
1005 memcpy(nh->nick, $3, slen + 1);
1006 nh->nicklen = sep - $3;
1007 nh->next = smap->services;
1008 smap->services = nh;
1012 pseudoflags: FAST ';'
1014 smap->flags |= SMAP_FAST;
1017 iauthblock: IAUTH '{' iauthitems '}' ';'
1019 auth_spawn(stringno, stringlist);
1020 while (stringno > 0)
1021 MyFree(stringlist[--stringno]);
1024 iauthitems: iauthitem iauthitems | iauthitem;
1025 iauthitem: iauthprogram;
1026 iauthprogram: PROGRAM '='
1028 while (stringno > 0)
1029 MyFree(stringlist[--stringno]);