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;
68 extern struct irc_sockaddr ResolverAddr;
71 /* Now all the globals we need :/... */
72 int tping, tconn, maxlinks, sendq, port, invert, stringno;
73 char *name, *pass, *host, *ip, *username, *origin, *hub_limit;
74 char *stringlist[MAX_STRINGS];
75 struct ConnectionClass *c_class;
76 struct DenyConf *dconf;
77 struct ServerConf *sconf;
78 struct qline *qconf = NULL;
81 struct Privs privs_dirty;
83 static void parse_error(char *pattern,...) {
84 static char error_buffer[1024];
87 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
89 yyerror(error_buffer);
161 /* and now a lot of priviledges... */
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
169 /* and some types... */
171 %type <num> timespec timefactor factoredtimes factoredtime
172 %type <num> expr yesorno privtype
182 /* Blocks in the config file... */
183 blocks: blocks block | block;
184 block: adminblock | generalblock | classblock | connectblock |
185 uworldblock | operblock | portblock | jupeblock | clientblock |
186 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
187 pseudoblock | iauthblock | error;
189 /* The timespec, sizespec and expr was ripped straight from
191 timespec: expr | factoredtimes;
193 factoredtimes: factoredtimes factoredtime
198 factoredtime: expr timefactor
203 timefactor: SECONDS { $$ = 1; }
204 | MINUTES { $$ = 60; }
205 | HOURS { $$ = 60 * 60; }
206 | DAYS { $$ = 60 * 60 * 24; }
207 | WEEKS { $$ = 60 * 60 * 24 * 7; }
208 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
209 | YEARS { $$ = 60 * 60 * 24 * 365; }
210 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
223 $$ = $1 * 1024 * 1024;
226 $$ = $1 * 1024 * 1024 * 1024;
229 $$ = $1 * 1024 * 1024 * 1024;
233 /* this is an arithmatic expression */
250 /* leave this out until we find why it makes BSD yacc dump core -larne
260 jupeblock: JUPE '{' jupeitems '}' ';' ;
261 jupeitems: jupeitem jupeitems | jupeitem;
262 jupeitem: jupenick | error;
263 jupenick: NICK '=' QSTRING
268 generalblock: GENERAL '{' generalitems '}'
270 if (localConf.name == NULL)
271 parse_error("Your General block must contain a name.");
272 if (localConf.numeric == 0)
273 parse_error("Your General block must contain a numeric (between 1 and 4095).");
275 generalitems: generalitem generalitems | generalitem;
276 generalitem: generalnumeric | generalname | generalvhost | generalresolver | generaldesc | error;
277 generalnumeric: NUMERIC '=' NUMBER ';'
279 if (localConf.numeric == 0)
280 localConf.numeric = $3;
281 else if (localConf.numeric != $3)
282 parse_error("Redefinition of server numeric %i (%i)", $3,
286 generalname: NAME '=' QSTRING ';'
288 if (localConf.name == NULL)
289 DupString(localConf.name, $3);
290 else if (strcmp(localConf.name, $3))
291 parse_error("Redefinition of server name %s (%s)", $3,
295 generaldesc: DESCRIPTION '=' QSTRING ';'
297 MyFree(localConf.description);
298 DupString(localConf.description, $3);
299 ircd_strncpy(cli_info(&me), $3, REALLEN);
302 generalvhost: VHOST '=' QSTRING ';'
304 ircd_aton(&VirtualHost.addr, $3);
307 generalresolver: RESOLVER '=' QSTRING ';'
309 ircd_aton(&ResolverAddr.addr, $3);
312 adminblock: ADMIN '{' adminitems '}'
314 if (localConf.location1 == NULL)
315 DupString(localConf.location1, "");
316 if (localConf.location2 == NULL)
317 DupString(localConf.location2, "");
318 if (localConf.contact == NULL)
319 DupString(localConf.contact, "");
321 adminitems: adminitems adminitem | adminitem;
322 adminitem: adminlocation | admincontact | error;
323 adminlocation: LOCATION '=' QSTRING ';'
325 if (localConf.location1 == NULL)
326 DupString(localConf.location1, $3);
327 else if (localConf.location2 == NULL)
328 DupString(localConf.location2, $3);
329 /* Otherwise just drop it. -A1kmm */
331 admincontact: CONTACT '=' QSTRING ';'
333 if (localConf.contact != NULL)
334 MyFree(localConf.contact);
335 DupString(localConf.contact, $3);
344 struct ConnectionClass *c_class;
345 add_class(name, tping, tconn, maxlinks, sendq);
346 c_class = find_class(name);
347 c_class->default_umode = pass;
348 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
349 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
352 parse_error("Missing name in class block");
359 memset(&privs, 0, sizeof(privs));
360 memset(&privs_dirty, 0, sizeof(privs_dirty));
362 classitems: classitem classitems | classitem;
363 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
364 classsendq | classusermode | priv | error;
365 classname: NAME '=' QSTRING ';'
370 classpingfreq: PINGFREQ '=' timespec ';'
374 classconnfreq: CONNECTFREQ '=' timespec ';'
378 classmaxlinks: MAXLINKS '=' expr ';'
382 classsendq: SENDQ '=' sizespec ';'
386 classusermode: USERMODE '=' QSTRING ';'
392 connectblock: CONNECT
395 } '{' connectitems '}'
397 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
398 && !strchr(host, '*') && !strchr(host, '?'))
400 struct ConfItem *aconf = make_conf(CONF_SERVER);
402 aconf->origin_name = origin;
403 aconf->passwd = pass;
404 aconf->conn_class = c_class;
405 aconf->address.port = port;
407 aconf->maximum = maxlinks;
408 aconf->hub_limit = hub_limit;
409 lookup_confhost(aconf);
418 parse_error("Bad connect block");
420 name = pass = host = origin = hub_limit = NULL;
424 connectitems: connectitem connectitems | connectitem;
425 connectitem: connectname | connectpass | connectclass | connecthost
426 | connectport | connectvhost | connectleaf | connecthub
427 | connecthublimit | connectmaxhops | error;
428 connectname: NAME '=' QSTRING ';'
433 connectpass: PASS '=' QSTRING ';'
438 connectclass: CLASS '=' QSTRING ';'
440 c_class = find_class($3);
442 connecthost: HOST '=' QSTRING ';'
447 connectport: PORT '=' NUMBER ';'
451 connectvhost: VHOST '=' QSTRING ';'
454 DupString(origin, $3);
456 connectleaf: LEAF ';'
463 DupString(hub_limit, "*");
465 connecthublimit: HUB '=' QSTRING ';'
468 DupString(hub_limit, $3);
470 connectmaxhops: MAXHOPS '=' expr ';'
475 uworldblock: UWORLD '{' uworlditems '}' ';'
479 struct ConfItem *aconf = make_conf(CONF_UWORLD);
485 parse_error("Bad UWorld block");
489 uworlditems: uworlditem uworlditems | uworlditem;
490 uworlditem: uworldname | error;
491 uworldname: NAME '=' QSTRING ';'
497 operblock: OPER '{' operitems '}' ';'
499 if (name && pass && host && c_class)
501 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
503 aconf->passwd = pass;
504 conf_parse_userhost(aconf, host);
505 aconf->conn_class = c_class;
506 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
507 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
508 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
509 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
510 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
514 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
519 name = pass = host = NULL;
521 memset(&privs, 0, sizeof(privs));
522 memset(&privs_dirty, 0, sizeof(privs_dirty));
524 operitems: operitem | operitems operitem;
525 operitem: opername | operpass | operhost | operclass | priv | error;
526 opername: NAME '=' QSTRING ';'
531 operpass: PASS '=' QSTRING ';'
536 operhost: HOST '=' QSTRING ';'
539 if (!strchr($3, '@'))
542 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
543 ircd_snprintf(0, b, uh_len, "*@%s", $3);
549 operclass: CLASS '=' QSTRING ';'
551 c_class = find_class($3);
554 priv: privtype '=' yesorno ';'
556 FlagSet(&privs_dirty, $1);
557 if (($3 == 1) ^ invert)
564 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
565 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
566 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
567 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
568 KILL { $$ = PRIV_KILL; } |
569 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
570 TPRIV_REHASH { $$ = PRIV_REHASH; } |
571 TPRIV_RESTART { $$ = PRIV_RESTART; } |
572 TPRIV_DIE { $$ = PRIV_DIE; } |
573 TPRIV_GLINE { $$ = PRIV_GLINE; } |
574 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
575 JUPE { $$ = PRIV_JUPE; } |
576 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
577 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
578 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
579 TPRIV_SET { $$ = PRIV_SET; } |
580 TPRIV_WHOX { $$ = PRIV_WHOX; } |
581 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
582 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
583 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
584 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
585 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
586 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
587 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
588 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
589 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
590 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
591 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
592 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
593 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
595 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
597 /* The port block... */
598 portblock: PORT '{' portitems '}' ';'
600 if (port > 0 && port <= 0xFFFF)
602 add_listener(port, host, pass, tconn, tping);
606 parse_error("Bad port block");
611 port = tconn = tping = 0;
613 portitems: portitem portitems | portitem;
614 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
615 portnumber: PORT '=' NUMBER ';'
620 portvhost: VHOST '=' QSTRING ';'
626 portmask: MASK '=' QSTRING ';'
632 portserver: SERVER '=' YES ';'
635 } | SERVER '=' NO ';'
640 porthidden: HIDDEN '=' YES ';'
643 } | HIDDEN '=' NO ';'
652 '{' clientitems '}' ';'
654 struct ConfItem *aconf = make_conf(CONF_CLIENT);
655 unsigned char addrbits;
656 aconf->username = username;
658 if (ip && ipmask_parse(ip, &aconf->address.addr, &addrbits))
659 aconf->addrbits = addrbits;
661 aconf->addrbits = -1;
662 aconf->conn_class = c_class ? c_class : find_class("default");
663 aconf->maximum = maxlinks;
669 clientitems: clientitem clientitems | clientitem;
670 clientitem: clienthost | clientip | clientusername | clientclass | clientpass | clientmaxlinks | error;
671 clienthost: HOST '=' QSTRING ';'
676 clientip: IP '=' QSTRING ';'
681 clientusername: USERNAME '=' QSTRING ';'
684 DupString(username, $3);
686 clientclass: CLASS '=' QSTRING ';'
688 c_class = find_class($3);
690 clientpass: PASS '=' QSTRING ';'
695 clientmaxlinks: MAXLINKS '=' expr ';'
702 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
705 if (dconf->hostmask != NULL)
707 if (dconf->usermask == NULL)
708 DupString(dconf->usermask, "*");
709 dconf->next = denyConfList;
710 denyConfList = dconf;
714 MyFree(dconf->hostmask);
715 MyFree(dconf->message);
717 parse_error("Bad kill block");
721 killitems: killitem killitems | killitem;
722 killitem: killuhost | killreal | killreasonfile | killreason | error;
723 killuhost: HOST '=' QSTRING ';'
726 dconf->flags &= ~DENY_FLAGS_REALNAME;
727 MyFree(dconf->hostmask);
728 MyFree(dconf->usermask);
729 if ((h = strchr($3, '@')) == NULL)
739 DupString(dconf->hostmask, h);
740 DupString(dconf->usermask, u);
741 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
744 killreal: REAL '=' QSTRING ';'
746 dconf->flags &= ~DENY_FLAGS_IP;
747 dconf->flags |= DENY_FLAGS_REALNAME;
748 MyFree(dconf->hostmask);
749 /* Leave usermask so you can specify user and real... */
750 DupString(dconf->hostmask, $3);
753 killreason: REASON '=' QSTRING ';'
755 dconf->flags &= DENY_FLAGS_FILE;
756 MyFree(dconf->message);
757 DupString(dconf->message, $3);
760 killreasonfile: TFILE '=' QSTRING ';'
762 dconf->flags |= DENY_FLAGS_FILE;
763 MyFree(dconf->message);
764 DupString(dconf->message, $3);
770 } '{' cruleitems '}' ';'
772 struct CRuleNode *node;
773 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
775 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
780 p->next = cruleConfList;
787 parse_error("Bad CRule block");
792 cruleitems: cruleitem cruleitems | cruleitem;
793 cruleitem: cruleserver | crulerule | cruleall | error;
795 cruleserver: SERVER '=' QSTRING ';'
802 crulerule: RULE '=' QSTRING ';'
808 cruleall: ALL '=' YES ';'
816 motdblock: MOTD '{' motditems '}' ';'
818 if (host != NULL && pass != NULL)
819 motd_add(host, pass);
825 motditems: motditem motditems | motditem;
826 motditem: motdhost | motdfile | error;
827 motdhost: HOST '=' QSTRING ';'
832 motdfile: TFILE '=' QSTRING ';'
837 featuresblock: FEATURES '{' featureitems '}' ';';
838 featureitems: featureitems featureitem | featureitem;
844 } '=' stringlist ';';
852 feature_set(NULL, (const char * const *)stringlist, stringno);
854 posextrastrings: /* empty */ | extrastrings;
855 extrastrings: extrastrings extrastring | extrastring;
858 if (stringno < MAX_STRINGS)
859 stringlist[stringno++] = $1;
862 quarantineblock: QUARANTINE '{'
864 qconf = (struct qline*) MyCalloc(1, sizeof(*qconf));
865 } quarantineitems '}' ';'
867 if (qconf->chname == NULL || qconf->reason == NULL)
869 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
873 qconf->next = GlobalQuarantineList;
874 GlobalQuarantineList = qconf;
878 quarantineitems: CHANNEL NAME '=' QSTRING ';'
880 DupString(qconf->chname, $4);
881 } | REASON '=' QSTRING ';'
883 DupString(qconf->reason, $3);
886 pseudoblock: PSEUDO QSTRING '{'
888 smap = MyCalloc(1, sizeof(struct s_map));
889 DupString(smap->command, $2);
893 if (!smap->name || !smap->services)
895 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
898 if (register_mapping(smap))
900 smap->next = GlobalServiceMapList;
901 GlobalServiceMapList = smap;
905 struct nick_host *nh, *next;
906 for (nh = smap->services; nh; nh = next)
912 MyFree(smap->command);
913 MyFree(smap->prepend);
919 pseudoitems: pseudoitem pseudoitems | pseudoitem;
920 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
921 pseudoname: NAME '=' QSTRING ';'
923 DupString(smap->name, $3);
925 pseudoprepend: PREPEND '=' QSTRING ';'
927 DupString(smap->prepend, $3);
929 pseudonick: NICK '=' QSTRING ';'
931 char *sep = strchr($3, '@');
934 size_t slen = strlen($3);
935 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
936 memcpy(nh->nick, $3, slen + 1);
937 nh->nicklen = sep - $3;
938 nh->next = smap->services;
943 iauthblock: IAUTH '{'
949 if (!host || !port) {
950 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
953 iauth_connect(host, port, pass, tconn, tping);
960 iauthitems: iauthitem iauthitems | iauthitem;
961 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
962 iauthpass: PASS '=' QSTRING ';'
967 iauthhost: HOST '=' QSTRING ';'
972 iauthport: PORT '=' NUMBER ';'
976 iauthconnfreq: CONNECTFREQ '=' timespec ';'
980 iauthtimeout: TIMEOUT '=' timespec ';'