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 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);
160 /* and now a lot of privileges... */
161 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
162 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
163 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
164 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
165 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
166 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
167 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE TPRIV_APASS_OPMODE
168 /* and some types... */
170 %type <num> timespec timefactor factoredtimes factoredtime
171 %type <num> expr yesorno privtype
181 /* Blocks in the config file... */
182 blocks: blocks block | block;
183 block: adminblock | generalblock | classblock | connectblock |
184 uworldblock | operblock | portblock | jupeblock | clientblock |
185 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
186 pseudoblock | iauthblock | error ';';
188 /* The timespec, sizespec and expr was ripped straight from
190 timespec: expr | factoredtimes;
192 factoredtimes: factoredtimes factoredtime
197 factoredtime: expr timefactor
202 timefactor: SECONDS { $$ = 1; }
203 | MINUTES { $$ = 60; }
204 | HOURS { $$ = 60 * 60; }
205 | DAYS { $$ = 60 * 60 * 24; }
206 | WEEKS { $$ = 60 * 60 * 24 * 7; }
207 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
208 | YEARS { $$ = 60 * 60 * 24 * 365; }
209 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
222 $$ = $1 * 1024 * 1024;
225 $$ = $1 * 1024 * 1024 * 1024;
228 $$ = $1 * 1024 * 1024 * 1024;
232 /* this is an arithmetic expression */
249 /* leave this out until we find why it makes BSD yacc dump core -larne
250 | '-' expr %prec NEG {
258 jupeblock: JUPE '{' jupeitems '}' ';' ;
259 jupeitems: jupeitem jupeitems | jupeitem;
261 jupenick: NICK '=' QSTRING ';'
267 generalblock: GENERAL
269 /* Zero out the vhost addresses, in case they were removed. */
270 memset(&VirtualHost_v4.addr, 0, sizeof(VirtualHost_v4.addr));
271 memset(&VirtualHost_v6.addr, 0, sizeof(VirtualHost_v6.addr));
272 } '{' generalitems '}' ';' {
273 if (localConf.name == NULL)
274 parse_error("Your General block must contain a name.");
275 if (localConf.numeric == 0)
276 parse_error("Your General block must contain a numeric (between 1 and 4095).");
278 generalitems: generalitem generalitems | generalitem;
279 generalitem: generalnumeric | generalname | generalvhost | generaldesc;
280 generalnumeric: NUMERIC '=' NUMBER ';'
282 if (localConf.numeric == 0)
283 localConf.numeric = $3;
284 else if (localConf.numeric != $3)
285 parse_error("Redefinition of server numeric %i (%i)", $3,
289 generalname: NAME '=' QSTRING ';'
291 if (localConf.name == NULL)
294 if (strcmp(localConf.name, $3))
295 parse_error("Redefinition of server name %s (%s)", $3,
301 generaldesc: DESCRIPTION '=' QSTRING ';'
303 MyFree(localConf.description);
304 localConf.description = $3;
305 ircd_strncpy(cli_info(&me), $3, REALLEN);
308 generalvhost: VHOST '=' QSTRING ';'
310 struct irc_in_addr addr;
311 if (!strcmp($3, "*")) {
312 /* This traditionally meant bind to all interfaces and connect
313 * from the default. */
314 } else if (!ircd_aton(&addr, $3))
315 parse_error("Invalid virtual host '%s'.", $3);
316 else if (irc_in_addr_is_ipv4(&addr))
317 memcpy(&VirtualHost_v4.addr, &addr, sizeof(addr));
319 memcpy(&VirtualHost_v6.addr, &addr, sizeof(addr));
325 MyFree(localConf.location1);
326 MyFree(localConf.location2);
327 MyFree(localConf.contact);
328 localConf.location1 = localConf.location2 = localConf.contact = NULL;
330 '{' adminitems '}' ';'
332 if (localConf.location1 == NULL)
333 DupString(localConf.location1, "");
334 if (localConf.location2 == NULL)
335 DupString(localConf.location2, "");
336 if (localConf.contact == NULL)
337 DupString(localConf.contact, "");
339 adminitems: adminitems adminitem | adminitem;
340 adminitem: adminlocation | admincontact;
341 adminlocation: LOCATION '=' QSTRING ';'
343 if (localConf.location1 == NULL)
344 localConf.location1 = $3;
345 else if (localConf.location2 == NULL)
346 localConf.location2 = $3;
347 else /* Otherwise just drop it. -A1kmm */
350 admincontact: CONTACT '=' QSTRING ';'
352 MyFree(localConf.contact);
353 localConf.contact = $3;
358 } '{' classitems '}' ';'
362 struct ConnectionClass *c_class;
363 add_class(name, tping, tconn, maxlinks, sendq);
364 c_class = find_class(name);
365 MyFree(c_class->default_umode);
366 c_class->default_umode = pass;
367 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
368 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
371 parse_error("Missing name in class block");
378 memset(&privs, 0, sizeof(privs));
379 memset(&privs_dirty, 0, sizeof(privs_dirty));
381 classitems: classitem classitems | classitem;
382 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
383 classsendq | classusermode | priv;
384 classname: NAME '=' QSTRING ';'
389 classpingfreq: PINGFREQ '=' timespec ';'
393 classconnfreq: CONNECTFREQ '=' timespec ';'
397 classmaxlinks: MAXLINKS '=' expr ';'
401 classsendq: SENDQ '=' sizespec ';'
405 classusermode: USERMODE '=' QSTRING ';'
411 connectblock: CONNECT
414 flags = CONF_AUTOCONNECT;
415 } '{' connectitems '}' ';'
417 struct ConfItem *aconf = NULL;
419 parse_error("Missing name in connect block");
420 else if (pass == NULL)
421 parse_error("Missing password in connect block");
422 else if (host == NULL)
423 parse_error("Missing host in connect block");
424 else if (strchr(host, '*') || strchr(host, '?'))
425 parse_error("Invalid host '%s' in connect block", host);
426 else if (c_class == NULL)
427 parse_error("Missing or non-existent class in connect block");
429 aconf = make_conf(CONF_SERVER);
431 aconf->origin_name = origin;
432 aconf->passwd = pass;
433 aconf->conn_class = c_class;
434 aconf->address.port = port;
436 aconf->maximum = maxlinks;
437 aconf->hub_limit = hub_limit;
438 aconf->flags = flags;
439 lookup_confhost(aconf);
448 name = pass = host = origin = hub_limit = NULL;
452 connectitems: connectitem connectitems | connectitem;
453 connectitem: connectname | connectpass | connectclass | connecthost
454 | connectport | connectvhost | connectleaf | connecthub
455 | connecthublimit | connectmaxhops | connectauto;
456 connectname: NAME '=' QSTRING ';'
461 connectpass: PASS '=' QSTRING ';'
466 connectclass: CLASS '=' QSTRING ';'
468 c_class = find_class($3);
470 parse_error("No such connection class '%s' for Connect block", $3);
473 connecthost: HOST '=' QSTRING ';'
478 connectport: PORT '=' NUMBER ';'
482 connectvhost: VHOST '=' QSTRING ';'
487 connectleaf: LEAF ';'
494 DupString(hub_limit, "*");
496 connecthublimit: HUB '=' QSTRING ';'
501 connectmaxhops: MAXHOPS '=' expr ';'
505 connectauto: AUTOCONNECT '=' YES ';' { flags |= CONF_AUTOCONNECT; }
506 | AUTOCONNECT '=' NO ';' { flags &= ~CONF_AUTOCONNECT; };
508 uworldblock: UWORLD '{' uworlditems '}' ';';
509 uworlditems: uworlditem uworlditems | uworlditem;
510 uworlditem: uworldname;
511 uworldname: NAME '=' QSTRING ';'
513 make_conf(CONF_UWORLD)->host = $3;
516 operblock: OPER '{' operitems '}' ';'
518 struct ConfItem *aconf = NULL;
520 parse_error("Missing name in operator block");
521 else if (pass == NULL)
522 parse_error("Missing password in operator block");
523 else if (host == NULL)
524 parse_error("Missing host in operator block");
525 else if (c_class == NULL)
526 parse_error("Invalid or missing class in operator block");
528 aconf = make_conf(CONF_OPERATOR);
530 aconf->passwd = pass;
531 conf_parse_userhost(aconf, host);
532 aconf->conn_class = c_class;
533 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
534 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
535 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
536 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
537 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
544 name = pass = host = NULL;
546 memset(&privs, 0, sizeof(privs));
547 memset(&privs_dirty, 0, sizeof(privs_dirty));
549 operitems: operitem | operitems operitem;
550 operitem: opername | operpass | operhost | operclass | priv;
551 opername: NAME '=' QSTRING ';'
556 operpass: PASS '=' QSTRING ';'
561 operhost: HOST '=' QSTRING ';'
564 if (!strchr($3, '@'))
567 host = (char*) MyMalloc((uh_len = strlen($3)+3));
568 ircd_snprintf(0, host, uh_len, "*@%s", $3);
574 operclass: CLASS '=' QSTRING ';'
576 c_class = find_class($3);
578 parse_error("No such connection class '%s' for Operator block", $3);
582 priv: privtype '=' yesorno ';'
584 FlagSet(&privs_dirty, $1);
585 if (($3 == 1) ^ invert)
592 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
593 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
594 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
595 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
596 KILL { $$ = PRIV_KILL; } |
597 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
598 TPRIV_REHASH { $$ = PRIV_REHASH; } |
599 TPRIV_RESTART { $$ = PRIV_RESTART; } |
600 TPRIV_DIE { $$ = PRIV_DIE; } |
601 TPRIV_GLINE { $$ = PRIV_GLINE; } |
602 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
603 JUPE { $$ = PRIV_JUPE; } |
604 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
605 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
606 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
607 TPRIV_SET { $$ = PRIV_SET; } |
608 TPRIV_WHOX { $$ = PRIV_WHOX; } |
609 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
610 TPRIV_LOCAL_BADCHAN { $$ = PRIV_LOCAL_BADCHAN; } |
611 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
612 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
613 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
614 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
615 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
616 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
617 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
618 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
619 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
620 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
621 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; } |
622 TPRIV_APASS_OPMODE { $$ = PRIV_APASS_OPMODE; } ;
624 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
626 /* The port block... */
627 portblock: PORT '{' portitems '}' ';'
629 if (port > 0 && port <= 0xFFFF)
630 add_listener(port, host, pass, tconn, tping);
632 parse_error("Port %d is out of range", port);
636 port = tconn = tping = 0;
638 portitems: portitem portitems | portitem;
639 portitem: portnumber | portvhost | portmask | portserver | porthidden;
640 portnumber: PORT '=' NUMBER ';'
645 portvhost: VHOST '=' QSTRING ';'
651 portmask: MASK '=' QSTRING ';'
657 portserver: SERVER '=' YES ';'
660 } | SERVER '=' NO ';'
665 porthidden: HIDDEN '=' YES ';'
668 } | HIDDEN '=' NO ';'
678 '{' clientitems '}' ';'
680 struct ConfItem *aconf = 0;
681 struct irc_in_addr addr;
682 unsigned char addrbits = 0;
685 parse_error("Invalid or missing class in Client block");
686 else if (ip && !ipmask_parse(ip, &addr, &addrbits))
687 parse_error("Invalid IP address %s in Client block", ip);
689 aconf = make_conf(CONF_CLIENT);
690 aconf->username = username;
693 memcpy(&aconf->address.addr, &addr, sizeof(aconf->address.addr));
695 memset(&aconf->address.addr, 0, sizeof(aconf->address.addr));
696 aconf->address.port = port;
697 aconf->addrbits = addrbits;
699 aconf->conn_class = c_class;
700 aconf->maximum = maxlinks;
701 aconf->passwd = pass;
716 clientitems: clientitem clientitems | clientitem;
717 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | clientport;
718 clienthost: HOST '=' QSTRING ';'
720 char *sep = strchr($3, '@');
725 DupString(host, sep);
731 clientip: IP '=' QSTRING ';'
734 sep = strchr($3, '@');
745 clientusername: USERNAME '=' QSTRING ';'
750 clientclass: CLASS '=' QSTRING ';'
752 c_class = find_class($3);
754 parse_error("No such connection class '%s' for Client block", $3);
757 clientpass: PASS '=' QSTRING ';'
762 clientmaxlinks: MAXLINKS '=' expr ';'
766 clientport: PORT '=' expr ';'
773 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
774 } '{' killitems '}' ';'
776 if (dconf->usermask || dconf->hostmask ||dconf->realmask) {
777 dconf->next = denyConfList;
778 denyConfList = dconf;
782 MyFree(dconf->usermask);
783 MyFree(dconf->hostmask);
784 MyFree(dconf->realmask);
785 MyFree(dconf->message);
787 parse_error("Kill block must match on at least one of username, host or realname");
791 killitems: killitem killitems | killitem;
792 killitem: killuhost | killreal | killusername | killreasonfile | killreason;
793 killuhost: HOST '=' QSTRING ';'
796 MyFree(dconf->hostmask);
797 MyFree(dconf->usermask);
798 if ((h = strchr($3, '@')) == NULL)
800 DupString(dconf->usermask, "*");
801 dconf->hostmask = $3;
806 DupString(dconf->hostmask, h);
807 dconf->usermask = $3;
809 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
812 killusername: USERNAME '=' QSTRING ';'
814 MyFree(dconf->usermask);
815 dconf->usermask = $3;
818 killreal: REAL '=' QSTRING ';'
820 MyFree(dconf->realmask);
821 dconf->realmask = $3;
824 killreason: REASON '=' QSTRING ';'
826 dconf->flags &= ~DENY_FLAGS_FILE;
827 MyFree(dconf->message);
831 killreasonfile: TFILE '=' QSTRING ';'
833 dconf->flags |= DENY_FLAGS_FILE;
834 MyFree(dconf->message);
841 } '{' cruleitems '}' ';'
843 struct CRuleNode *node = NULL;
845 parse_error("Missing host in crule block");
846 else if (pass == NULL)
847 parse_error("Missing rule in crule block");
848 else if ((node = crule_parse(pass)) == NULL)
849 parse_error("Invalid rule '%s' in crule block", pass);
852 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
857 p->next = cruleConfList;
869 cruleitems: cruleitem cruleitems | cruleitem;
870 cruleitem: cruleserver | crulerule | cruleall;
872 cruleserver: SERVER '=' QSTRING ';'
879 crulerule: RULE '=' QSTRING ';'
885 cruleall: ALL '=' YES ';'
893 motdblock: MOTD '{' motditems '}' ';'
895 if (host != NULL && pass != NULL)
896 motd_add(host, pass);
902 motditems: motditem motditems | motditem;
903 motditem: motdhost | motdfile;
904 motdhost: HOST '=' QSTRING ';'
909 motdfile: TFILE '=' QSTRING ';'
914 featuresblock: FEATURES '{' featureitems '}' ';';
915 featureitems: featureitems featureitem | featureitem;
921 } '=' stringlist ';' {
923 feature_set(NULL, (const char * const *)stringlist, stringno);
924 for (ii = 0; ii < stringno; ++ii)
925 MyFree(stringlist[ii]);
928 stringlist: stringlist extrastring | extrastring;
931 if (stringno < MAX_STRINGS)
932 stringlist[stringno++] = $1;
937 quarantineblock: QUARANTINE '{' quarantineitems '}' ';';
938 quarantineitems: quarantineitems quarantineitem | quarantineitem;
939 quarantineitem: QSTRING '=' QSTRING ';'
941 struct qline *qconf = MyCalloc(1, sizeof(*qconf));
944 qconf->next = GlobalQuarantineList;
945 GlobalQuarantineList = qconf;
948 pseudoblock: PSEUDO QSTRING '{'
950 smap = MyCalloc(1, sizeof(struct s_map));
958 parse_error("Missing name in pseudo %s block", smap->command);
959 else if (!smap->services)
960 parse_error("Missing nick in pseudo %s block", smap->command);
963 if (valid && register_mapping(smap))
965 smap->next = GlobalServiceMapList;
966 GlobalServiceMapList = smap;
975 pseudoitems: pseudoitem pseudoitems | pseudoitem;
976 pseudoitem: pseudoname | pseudoprepend | pseudonick | pseudoflags;
977 pseudoname: NAME '=' QSTRING ';'
982 pseudoprepend: PREPEND '=' QSTRING ';'
984 MyFree(smap->prepend);
987 pseudonick: NICK '=' QSTRING ';'
989 char *sep = strchr($3, '@');
992 size_t slen = strlen($3);
993 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
994 memcpy(nh->nick, $3, slen + 1);
995 nh->nicklen = sep - $3;
996 nh->next = smap->services;
1001 pseudoflags: FAST ';'
1003 smap->flags |= SMAP_FAST;
1006 iauthblock: IAUTH '{' iauthitems '}' ';'
1008 auth_spawn(stringno, stringlist);
1009 while (stringno > 0)
1010 MyFree(stringlist[stringno--]);
1013 iauthitems: iauthitem iauthitems | iauthitem;
1014 iauthitem: iauthprogram;
1015 iauthprogram: PROGRAM '='
1017 while (stringno > 0)
1018 MyFree(stringlist[stringno--]);