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_auth.h"
36 #include "ircd_chattr.h"
38 #include "ircd_reply.h"
39 #include "ircd_snprintf.h"
40 #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 ConnectionClass *c_class;
75 struct DenyConf *dconf;
76 struct ServerConf *sconf;
79 struct Privs privs_dirty;
81 static void parse_error(char *pattern,...) {
82 static char error_buffer[1024];
85 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
87 yyerror(error_buffer);
159 /* and now a lot of privileges... */
160 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
161 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
162 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
163 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
164 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
165 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
166 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
167 /* and some types... */
169 %type <num> timespec timefactor factoredtimes factoredtime
170 %type <num> expr yesorno privtype
180 /* Blocks in the config file... */
181 blocks: blocks block | block;
182 block: adminblock | generalblock | classblock | connectblock |
183 uworldblock | operblock | portblock | jupeblock | clientblock |
184 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
185 pseudoblock | iauthblock | error ';';
187 /* The timespec, sizespec and expr was ripped straight from
189 timespec: expr | factoredtimes;
191 factoredtimes: factoredtimes factoredtime
196 factoredtime: expr timefactor
201 timefactor: SECONDS { $$ = 1; }
202 | MINUTES { $$ = 60; }
203 | HOURS { $$ = 60 * 60; }
204 | DAYS { $$ = 60 * 60 * 24; }
205 | WEEKS { $$ = 60 * 60 * 24 * 7; }
206 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
207 | YEARS { $$ = 60 * 60 * 24 * 365; }
208 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
221 $$ = $1 * 1024 * 1024;
224 $$ = $1 * 1024 * 1024 * 1024;
227 $$ = $1 * 1024 * 1024 * 1024;
231 /* this is an arithmetic expression */
248 /* leave this out until we find why it makes BSD yacc dump core -larne
249 | '-' expr %prec NEG {
257 jupeblock: JUPE '{' jupeitems '}' ';' ;
258 jupeitems: jupeitem jupeitems | jupeitem;
260 jupenick: NICK '=' QSTRING ';'
266 generalblock: GENERAL
268 /* Zero out the vhost addresses, in case they were removed. */
269 memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
270 memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
271 } '{' generalitems '}' ';' {
272 if (localConf.name == NULL)
273 parse_error("Your General block must contain a name.");
274 if (localConf.numeric == 0)
275 parse_error("Your General block must contain a numeric (between 1 and 4095).");
277 generalitems: generalitem generalitems | generalitem;
278 generalitem: generalnumeric | generalname | generalvhost | generaldesc;
279 generalnumeric: NUMERIC '=' NUMBER ';'
281 if (localConf.numeric == 0)
282 localConf.numeric = $3;
283 else if (localConf.numeric != $3)
284 parse_error("Redefinition of server numeric %i (%i)", $3,
288 generalname: NAME '=' QSTRING ';'
290 if (localConf.name == NULL)
293 if (strcmp(localConf.name, $3))
294 parse_error("Redefinition of server name %s (%s)", $3,
300 generaldesc: DESCRIPTION '=' QSTRING ';'
302 MyFree(localConf.description);
303 localConf.description = $3;
304 ircd_strncpy(cli_info(&me), $3, REALLEN);
307 generalvhost: VHOST '=' QSTRING ';'
309 struct irc_in_addr addr;
310 if (!strcmp($3, "*")) {
311 /* This traditionally meant bind to all interfaces and connect
312 * from the default. */
313 } else if (!ircd_aton(&addr, $3))
314 parse_error("Invalid virtual host '%s'.", $3);
315 else if (irc_in_addr_is_ipv4(&addr))
316 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
318 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
322 adminblock: ADMIN '{' adminitems '}' ';'
324 if (localConf.location1 == NULL)
325 DupString(localConf.location1, "");
326 if (localConf.location2 == NULL)
327 DupString(localConf.location2, "");
328 if (localConf.contact == NULL)
329 DupString(localConf.contact, "");
331 adminitems: adminitems adminitem | adminitem;
332 adminitem: adminlocation | admincontact;
333 adminlocation: LOCATION '=' QSTRING ';'
335 if (localConf.location1 == NULL)
336 localConf.location1 = $3;
337 else if (localConf.location2 == NULL)
338 localConf.location2 = $3;
339 else /* Otherwise just drop it. -A1kmm */
342 admincontact: CONTACT '=' QSTRING ';'
344 MyFree(localConf.contact);
345 localConf.contact = $3;
350 } '{' classitems '}' ';'
354 struct ConnectionClass *c_class;
355 add_class(name, tping, tconn, maxlinks, sendq);
356 c_class = find_class(name);
357 MyFree(c_class->default_umode);
358 c_class->default_umode = pass;
359 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
360 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
363 parse_error("Missing name in class block");
370 memset(&privs, 0, sizeof(privs));
371 memset(&privs_dirty, 0, sizeof(privs_dirty));
373 classitems: classitem classitems | classitem;
374 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
375 classsendq | classusermode | priv;
376 classname: NAME '=' QSTRING ';'
381 classpingfreq: PINGFREQ '=' timespec ';'
385 classconnfreq: CONNECTFREQ '=' timespec ';'
389 classmaxlinks: MAXLINKS '=' expr ';'
393 classsendq: SENDQ '=' sizespec ';'
397 classusermode: USERMODE '=' QSTRING ';'
403 connectblock: CONNECT
406 flags = CONF_AUTOCONNECT;
407 } '{' connectitems '}' ';'
409 struct ConfItem *aconf = NULL;
411 parse_error("Missing name in connect block");
412 else if (pass == NULL)
413 parse_error("Missing password in connect block");
414 else if (host == NULL)
415 parse_error("Missing host in connect block");
416 else if (strchr(host, '*') || strchr(host, '?'))
417 parse_error("Invalid host '%s' in connect block", host);
418 else if (c_class == NULL)
419 parse_error("Missing or non-existent class in connect block");
421 aconf = make_conf(CONF_SERVER);
423 aconf->origin_name = origin;
424 aconf->passwd = pass;
425 aconf->conn_class = c_class;
426 aconf->address.port = port;
428 aconf->maximum = maxlinks;
429 aconf->hub_limit = hub_limit;
430 aconf->flags = flags;
431 lookup_confhost(aconf);
440 name = pass = host = origin = hub_limit = NULL;
444 connectitems: connectitem connectitems | connectitem;
445 connectitem: connectname | connectpass | connectclass | connecthost
446 | connectport | connectvhost | connectleaf | connecthub
447 | connecthublimit | connectmaxhops | connectauto;
448 connectname: NAME '=' QSTRING ';'
453 connectpass: PASS '=' QSTRING ';'
458 connectclass: CLASS '=' QSTRING ';'
460 c_class = find_class($3);
462 parse_error("No such connection class '%s' for Connect block", $3);
465 connecthost: HOST '=' QSTRING ';'
470 connectport: PORT '=' NUMBER ';'
474 connectvhost: VHOST '=' QSTRING ';'
479 connectleaf: LEAF ';'
486 DupString(hub_limit, "*");
488 connecthublimit: HUB '=' QSTRING ';'
493 connectmaxhops: MAXHOPS '=' expr ';'
497 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
498 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
500 uworldblock: UWORLD '{' uworlditems '}' ';';
501 uworlditems: uworlditem uworlditems | uworlditem;
502 uworlditem: uworldname;
503 uworldname: NAME '=' QSTRING ';'
505 make_conf(CONF_UWORLD)->host = $3;
508 operblock: OPER '{' operitems '}' ';'
510 struct ConfItem *aconf = NULL;
512 parse_error("Missing name in operator block");
513 else if (pass == NULL)
514 parse_error("Missing password in operator block");
515 else if (host == NULL)
516 parse_error("Missing host in operator block");
517 else if (c_class == NULL)
518 parse_error("Invalid or missing class in operator block");
520 aconf = make_conf(CONF_OPERATOR);
522 aconf->passwd = pass;
523 conf_parse_userhost(aconf, host);
524 aconf->conn_class = c_class;
525 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
526 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
527 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
528 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
529 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
536 name = pass = host = NULL;
538 memset(&privs, 0, sizeof(privs));
539 memset(&privs_dirty, 0, sizeof(privs_dirty));
541 operitems: operitem | operitems operitem;
542 operitem: opername | operpass | operhost | operclass | priv;
543 opername: NAME '=' QSTRING ';'
548 operpass: PASS '=' QSTRING ';'
553 operhost: HOST '=' QSTRING ';'
556 if (!strchr($3, '@'))
559 host = (char*) MyMalloc((uh_len = strlen($3)+3));
560 ircd_snprintf(0, host, uh_len, "*@%s", $3);
566 operclass: CLASS '=' QSTRING ';'
568 c_class = find_class($3);
570 parse_error("No such connection class '%s' for Operator block", $3);
574 priv: privtype '=' yesorno ';'
576 FlagSet(&privs_dirty, $1);
577 if (($3 == 1) ^ invert)
584 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
585 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
586 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
587 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
588 KILL { $$ = PRIV_KILL; } |
589 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
590 TPRIV_REHASH { $$ = PRIV_REHASH; } |
591 TPRIV_RESTART { $$ = PRIV_RESTART; } |
592 TPRIV_DIE { $$ = PRIV_DIE; } |
593 TPRIV_GLINE { $$ = PRIV_GLINE; } |
594 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
595 JUPE { $$ = PRIV_JUPE; } |
596 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
597 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
598 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
599 TPRIV_SET { $$ = PRIV_SET; } |
600 TPRIV_WHOX { $$ = PRIV_WHOX; } |
601 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
602 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
603 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
604 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
605 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
606 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
607 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
608 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
609 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
610 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
611 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
612 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
613 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
614 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
616 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
618 /* The port block... */
619 portblock: PORT '{' portitems '}' ';'
621 if (port > 0 && port <= 0xFFFF)
622 add_listener(port, host, pass, tconn, tping);
624 parse_error("Port %d is out of range", port);
628 port = tconn = tping = 0;
630 portitems: portitem portitems | portitem;
631 portitem: portnumber | portvhost | portmask | portserver | porthidden;
632 portnumber: PORT '=' NUMBER ';'
637 portvhost: VHOST '=' QSTRING ';'
643 portmask: MASK '=' QSTRING ';'
649 portserver: SERVER '=' YES ';'
652 } | SERVER '=' NO ';'
657 porthidden: HIDDEN '=' YES ';'
660 } | HIDDEN '=' NO ';'
669 '{' clientitems '}' ';'
671 struct ConfItem *aconf = 0;
672 struct irc_in_addr addr;
673 unsigned char addrbits = 0;
676 parse_error("Invalid or missing class in Client block");
677 else if (ip && !ipmask_parse(ip, &addr, &addrbits))
678 parse_error("Invalid IP address %s in Client block", ip);
680 aconf = make_conf(CONF_CLIENT);
681 aconf->username = username;
684 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
686 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
687 aconf->addrbits = addrbits;
689 aconf->conn_class = c_class;
690 aconf->maximum = maxlinks;
691 aconf->passwd = pass;
705 clientitems: clientitem clientitems | clientitem;
706 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks;
707 clienthost: HOST '=' QSTRING ';'
709 char *sep = strchr($3, '@');
714 DupString(host, sep);
720 clientip: IP '=' QSTRING ';'
723 sep = strchr($3, '@');
734 clientusername: USERNAME '=' QSTRING ';'
739 clientclass: CLASS '=' QSTRING ';'
741 c_class = find_class($3);
743 parse_error("No such connection class '%s' for Class block", $3);
746 clientpass: PASS '=' QSTRING ';'
751 clientmaxlinks: MAXLINKS '=' expr ';'
758 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
759 } '{' killitems '}' ';'
761 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
762 dconf->next = denyConfList;
763 denyConfList = dconf;
767 MyFree(dconf->usermask);
768 MyFree(dconf->hostmask);
769 MyFree(dconf->realmask);
770 MyFree(dconf->message);
772 parse_error("Kill block must match on at least one of username, host or realname");
776 killitems: killitem killitems | killitem;
777 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
778 killuhost: HOST '=' QSTRING ';'
781 MyFree(dconf->hostmask);
782 MyFree(dconf->usermask);
783 if ((h = strchr($3, '@')) == NULL)
785 DupString(dconf->usermask, "*");
786 dconf->hostmask = $3;
791 DupString(dconf->hostmask, h);
792 dconf->usermask = $3;
794 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
797 killusername: USERNAME '=' QSTRING ';'
799 MyFree(dconf->usermask);
800 dconf->usermask = $3;
803 killreal: REAL '=' QSTRING ';'
805 MyFree(dconf->realmask);
806 dconf->realmask = $3;
809 killreason: REASON '=' QSTRING ';'
811 dconf->flags &= ~DENY_FLAGS_FILE;
812 MyFree(dconf->message);
816 killreasonfile: TFILE '=' QSTRING ';'
818 dconf->flags |= DENY_FLAGS_FILE;
819 MyFree(dconf->message);
826 } '{' cruleitems '}' ';'
828 struct CRuleNode *node = NULL;
830 parse_error("Missing host in crule block");
831 else if (pass == NULL)
832 parse_error("Missing rule in crule block");
833 else if ((node = crule_parse(pass)) == NULL)
834 parse_error("Invalid rule '%s' in crule block", pass);
837 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
842 p->next = cruleConfList;
854 cruleitems: cruleitem cruleitems | cruleitem;
855 cruleitem: cruleserver | crulerule | cruleall;
857 cruleserver: SERVER '=' QSTRING ';'
864 crulerule: RULE '=' QSTRING ';'
870 cruleall: ALL '=' YES ';'
878 motdblock: MOTD '{' motditems '}' ';'
880 if (host != NULL && pass != NULL)
881 motd_add(host, pass);
887 motditems: motditem motditems | motditem;
888 motditem: motdhost | motdfile;
889 motdhost: HOST '=' QSTRING ';'
894 motdfile: TFILE '=' QSTRING ';'
899 featuresblock: FEATURES '{' featureitems '}' ';';
900 featureitems: featureitems featureitem | featureitem;
906 } '=' stringlist ';';
915 feature_set(NULL, (const char * const *)stringlist, stringno);
916 for (ii = 0; ii < stringno; ++ii)
917 MyFree(stringlist[ii]);
919 posextrastrings: /* empty */ | extrastrings;
920 extrastrings: extrastrings extrastring | extrastring;
923 if (stringno < MAX_STRINGS)
924 stringlist[stringno++] = $1;
929 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
930 quarantineitems: quarantineitems quarantineitem | quarantineitem;
931 quarantineitem: QSTRING '=' QSTRING ';'
933 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
936 qconf->next = GlobalQuarantineList;
937 GlobalQuarantineList = qconf;
940 pseudoblock: PSEUDO QSTRING '{'
942 smap = MyCalloc(1, sizeof(struct s_map));
950 parse_error("Missing name in pseudo %s block", smap->command);
951 else if (!smap->services)
952 parse_error("Missing nick in pseudo %s block", smap->command);
955 if (valid && register_mapping(smap))
957 smap->next = GlobalServiceMapList;
958 GlobalServiceMapList = smap;
967 pseudoitems: pseudoitem pseudoitems | pseudoitem;
968 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
969 pseudoname: NAME '=' QSTRING ';'
974 pseudoprepend: PREPEND '=' QSTRING ';'
976 MyFree(smap->prepend);
979 pseudonick: NICK '=' QSTRING ';'
981 char *sep = strchr($3, '@');
984 size_t slen = strlen($3);
985 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
986 memcpy(nh->nick, $3, slen + 1);
987 nh->nicklen = sep - $3;
988 nh->next = smap->services;
993 pseudoflags: FAST ';'
995 smap->flags |= SMAP_FAST;
998 iauthblock: IAUTH '{'
1002 } iauthitems '}' ';'
1005 parse_error("Missing host in iauth block");
1007 parse_error("Missing port in iauth block");
1009 iauth_connect(host, port, pass, tconn, tping);
1013 port = tconn = tping = 0;
1016 iauthitems: iauthitem iauthitems | iauthitem;
1017 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout;
1018 iauthpass: PASS '=' QSTRING ';'
1023 iauthhost: HOST '=' QSTRING ';'
1028 iauthport: PORT '=' NUMBER ';'
1032 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1036 iauthtimeout: TIMEOUT '=' timespec ';'