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;
72 char *name, *pass, *host, *origin, *hub_limit;
73 char *stringlist[MAX_STRINGS];
74 struct ConnectionClass *c_class;
75 struct DenyConf *dconf;
76 struct ServerConf *sconf;
77 struct qline *qconf = NULL;
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);
158 /* and now a lot of priviledges... */
159 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
160 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
161 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
162 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
163 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
164 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
165 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE
166 /* and some types... */
168 %type <num> timespec timefactor factoredtimes factoredtime
169 %type <num> expr yesorno privtype
179 /* Blocks in the config file... */
180 blocks: blocks block | block;
181 block: adminblock | generalblock | classblock | connectblock |
182 uworldblock | operblock | portblock | jupeblock | clientblock |
183 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
184 pseudoblock | iauthblock | error;
186 /* The timespec, sizespec and expr was ripped straight from
188 timespec: expr | factoredtimes;
190 factoredtimes: factoredtimes factoredtime
195 factoredtime: expr timefactor
200 timefactor: SECONDS { $$ = 1; }
201 | MINUTES { $$ = 60; }
202 | HOURS { $$ = 60 * 60; }
203 | DAYS { $$ = 60 * 60 * 24; }
204 | WEEKS { $$ = 60 * 60 * 24 * 7; }
205 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
206 | YEARS { $$ = 60 * 60 * 24 * 365; }
207 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
220 $$ = $1 * 1024 * 1024;
223 $$ = $1 * 1024 * 1024 * 1024;
226 $$ = $1 * 1024 * 1024 * 1024;
230 /* this is an arithmatic expression */
247 /* leave this out until we find why it makes BSD yacc dump core -larne
257 jupeblock: JUPE '{' jupeitems '}' ';' ;
258 jupeitems: jupeitem jupeitems | jupeitem;
259 jupeitem: jupenick | error;
260 jupenick: NICK '=' QSTRING
265 generalblock: GENERAL '{' generalitems '}'
267 if (localConf.name == NULL)
268 parse_error("Your General block must contain a name.");
269 if (localConf.numeric == 0)
270 parse_error("Your General block must contain a numeric (between 1 and 4095).");
272 generalitems: generalitem generalitems | generalitem;
273 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
274 generalnumeric: NUMERIC '=' NUMBER ';'
276 if (localConf.numeric == 0)
277 localConf.numeric = $3;
278 else if (localConf.numeric != $3)
279 parse_error("Redefinition of server numeric %i (%i)", $3,
283 generalname: NAME '=' QSTRING ';'
285 if (localConf.name == NULL)
286 DupString(localConf.name, $3);
287 else if (strcmp(localConf.name, $3))
288 parse_error("Redefinition of server name %s (%s)", $3,
292 generaldesc: DESCRIPTION '=' QSTRING ';'
294 MyFree(localConf.description);
295 DupString(localConf.description, $3);
296 ircd_strncpy(cli_info(&me), $3, REALLEN);
299 generalvhost: VHOST '=' QSTRING ';'
301 ircd_aton(&VirtualHost.addr, $3);
304 adminblock: ADMIN '{' adminitems '}'
306 if (localConf.location1 == NULL)
307 DupString(localConf.location1, "");
308 if (localConf.location2 == NULL)
309 DupString(localConf.location2, "");
310 if (localConf.contact == NULL)
311 DupString(localConf.contact, "");
313 adminitems: adminitems adminitem | adminitem;
314 adminitem: adminlocation | admincontact | error;
315 adminlocation: LOCATION '=' QSTRING ';'
317 if (localConf.location1 == NULL)
318 DupString(localConf.location1, $3);
319 else if (localConf.location2 == NULL)
320 DupString(localConf.location2, $3);
321 /* Otherwise just drop it. -A1kmm */
323 admincontact: CONTACT '=' QSTRING ';'
325 if (localConf.contact != NULL)
326 MyFree(localConf.contact);
327 DupString(localConf.contact, $3);
336 struct ConnectionClass *c_class;
337 add_class(name, tping, tconn, maxlinks, sendq);
338 c_class = find_class(name);
339 c_class->default_umode = pass;
340 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
341 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
344 parse_error("Missing name in class block");
351 memset(&privs, 0, sizeof(privs));
352 memset(&privs_dirty, 0, sizeof(privs_dirty));
354 classitems: classitem classitems | classitem;
355 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
356 classsendq | classusermode | priv | error;
357 classname: NAME '=' QSTRING ';'
362 classpingfreq: PINGFREQ '=' timespec ';'
366 classconnfreq: CONNECTFREQ '=' timespec ';'
370 classmaxlinks: MAXLINKS '=' expr ';'
374 classsendq: SENDQ '=' sizespec ';'
378 classusermode: USERMODE '=' QSTRING ';'
384 connectblock: CONNECT
387 } '{' connectitems '}'
389 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
390 && !strchr(host, '*') && !strchr(host, '?'))
392 struct ConfItem *aconf = make_conf(CONF_SERVER);
394 aconf->origin_name = origin;
395 aconf->passwd = pass;
396 aconf->conn_class = c_class;
397 aconf->address.port = port;
399 aconf->maximum = maxlinks;
400 aconf->hub_limit = hub_limit;
401 lookup_confhost(aconf);
410 parse_error("Bad connect block");
412 name = pass = host = origin = hub_limit = NULL;
416 connectitems: connectitem connectitems | connectitem;
417 connectitem: connectname | connectpass | connectclass | connecthost
418 | connectport | connectvhost | connectleaf | connecthub
419 | connecthublimit | connectmaxhops | error;
420 connectname: NAME '=' QSTRING ';'
425 connectpass: PASS '=' QSTRING ';'
430 connectclass: CLASS '=' QSTRING ';'
432 c_class = find_class($3);
434 connecthost: HOST '=' QSTRING ';'
439 connectport: PORT '=' NUMBER ';'
443 connectvhost: VHOST '=' QSTRING ';'
446 DupString(origin, $3);
448 connectleaf: LEAF ';'
455 DupString(hub_limit, "*");
457 connecthublimit: HUB '=' QSTRING ';'
460 DupString(hub_limit, $3);
462 connectmaxhops: MAXHOPS '=' expr ';'
467 uworldblock: UWORLD '{' uworlditems '}' ';'
471 struct ConfItem *aconf = make_conf(CONF_UWORLD);
477 parse_error("Bad UWorld block");
481 uworlditems: uworlditem uworlditems | uworlditem;
482 uworlditem: uworldname | error;
483 uworldname: NAME '=' QSTRING ';'
489 operblock: OPER '{' operitems '}' ';'
491 if (name && pass && host && c_class)
493 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
495 aconf->passwd = pass;
497 aconf->conn_class = c_class;
498 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
499 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
500 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
501 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
502 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
506 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
511 name = pass = host = NULL;
513 memset(&privs, 0, sizeof(privs));
514 memset(&privs_dirty, 0, sizeof(privs_dirty));
516 operitems: operitem | operitems operitem;
517 operitem: opername | operpass | operhost | operclass | priv | error;
518 opername: NAME '=' QSTRING ';'
523 operpass: PASS '=' QSTRING ';'
528 operhost: HOST '=' QSTRING ';'
531 if (!strchr($3, '@'))
534 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
535 ircd_snprintf(0, b, uh_len, "*@%s", $3);
541 operclass: CLASS '=' QSTRING ';'
543 c_class = find_class($3);
546 priv: privtype '=' yesorno ';'
548 FlagSet(&privs_dirty, $1);
549 if (($3 == 1) ^ invert)
556 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
557 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
558 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
559 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
560 KILL { $$ = PRIV_KILL; } |
561 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
562 TPRIV_REHASH { $$ = PRIV_REHASH; } |
563 TPRIV_RESTART { $$ = PRIV_RESTART; } |
564 TPRIV_DIE { $$ = PRIV_DIE; } |
565 TPRIV_GLINE { $$ = PRIV_GLINE; } |
566 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
567 JUPE { $$ = PRIV_JUPE; } |
568 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
569 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
570 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
571 TPRIV_SET { $$ = PRIV_SET; } |
572 TPRIV_WHOX { $$ = PRIV_WHOX; } |
573 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
574 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
575 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
576 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
577 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
578 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
579 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
580 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
581 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
582 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
583 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
584 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
585 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
587 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
589 /* The port block... */
590 portblock: PORT '{' portitems '}' ';'
592 if (port > 0 && port <= 0xFFFF)
594 add_listener(port, host, pass, tconn, tping);
598 parse_error("Bad port block");
603 port = tconn = tping = 0;
605 portitems: portitem portitems | portitem;
606 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
607 portnumber: PORT '=' NUMBER ';'
612 portvhost: VHOST '=' QSTRING ';'
618 portmask: MASK '=' QSTRING ';'
624 portserver: SERVER '=' YES ';'
627 } | SERVER '=' NO ';'
632 porthidden: HIDDEN '=' YES ';'
635 } | HIDDEN '=' NO ';'
644 '{' clientitems '}' ';'
648 struct ConfItem *aconf = make_conf(CONF_CLIENT);
651 aconf->conn_class = c_class ? c_class : find_class("default");
652 aconf->maximum = maxlinks;
658 parse_error("Bad client block");
663 clientitems: clientitem clientitems | clientitem;
664 clientitem: clienthost | clientclass | clientpass | clientip
665 | clientmaxlinks | error;
666 clientip: IP '=' QSTRING ';'
671 clienthost: HOST '=' QSTRING ';'
676 clientclass: CLASS '=' QSTRING ';'
678 c_class = find_class($3);
680 clientpass: PASS '=' QSTRING ';'
685 clientmaxlinks: MAXLINKS '=' expr ';'
692 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
695 if (dconf->hostmask != NULL)
697 if (dconf->usermask == NULL)
698 DupString(dconf->usermask, "*");
699 dconf->next = denyConfList;
700 denyConfList = dconf;
704 MyFree(dconf->hostmask);
705 MyFree(dconf->message);
707 parse_error("Bad kill block");
711 killitems: killitem killitems | killitem;
712 killitem: killuhost | killreal | killreasonfile | killreason | error;
713 killuhost: HOST '=' QSTRING ';'
716 dconf->flags &= ~DENY_FLAGS_REALNAME;
717 MyFree(dconf->hostmask);
718 MyFree(dconf->usermask);
719 if ((h = strchr($3, '@')) == NULL)
729 DupString(dconf->hostmask, h);
730 DupString(dconf->usermask, u);
731 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
734 killreal: REAL '=' QSTRING ';'
736 dconf->flags &= ~DENY_FLAGS_IP;
737 dconf->flags |= DENY_FLAGS_REALNAME;
738 MyFree(dconf->hostmask);
739 /* Leave usermask so you can specify user and real... */
740 DupString(dconf->hostmask, $3);
743 killreason: REASON '=' QSTRING ';'
745 dconf->flags &= DENY_FLAGS_FILE;
746 MyFree(dconf->message);
747 DupString(dconf->message, $3);
750 killreasonfile: TFILE '=' QSTRING ';'
752 dconf->flags |= DENY_FLAGS_FILE;
753 MyFree(dconf->message);
754 DupString(dconf->message, $3);
760 } '{' cruleitems '}' ';'
762 struct CRuleNode *node;
763 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
765 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
770 p->next = cruleConfList;
777 parse_error("Bad CRule block");
782 cruleitems: cruleitem cruleitems | cruleitem;
783 cruleitem: cruleserver | crulerule | cruleall | error;
785 cruleserver: SERVER '=' QSTRING ';'
792 crulerule: RULE '=' QSTRING ';'
798 cruleall: ALL '=' YES ';'
806 motdblock: MOTD '{' motditems '}' ';'
808 if (host != NULL && pass != NULL)
809 motd_add(host, pass);
815 motditems: motditem motditems | motditem;
816 motditem: motdhost | motdfile | error;
817 motdhost: HOST '=' QSTRING ';'
822 motdfile: TFILE '=' QSTRING ';'
827 featuresblock: FEATURES '{' featureitems '}' ';';
828 featureitems: featureitems featureitem | featureitem;
834 } '=' stringlist ';';
842 feature_set(NULL, (const char * const *)stringlist, stringno);
844 posextrastrings: /* empty */ | extrastrings;
845 extrastrings: extrastrings extrastring | extrastring;
848 if (stringno < MAX_STRINGS)
849 stringlist[stringno++] = $1;
852 quarantineblock: QUARANTINE '{'
854 qconf = (struct qline*) MyCalloc(1, sizeof(*qconf));
855 } quarantineitems '}' ';'
857 if (qconf->chname == NULL || qconf->reason == NULL)
859 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
863 qconf->next = GlobalQuarantineList;
864 GlobalQuarantineList = qconf;
868 quarantineitems: CHANNEL NAME '=' QSTRING ';'
870 DupString(qconf->chname, $4);
871 } | REASON '=' QSTRING ';'
873 DupString(qconf->reason, $3);
876 pseudoblock: PSEUDO QSTRING '{'
878 smap = MyCalloc(1, sizeof(struct s_map));
879 DupString(smap->command, $2);
883 if (!smap->name || !smap->services)
885 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
888 if (register_mapping(smap))
890 smap->next = GlobalServiceMapList;
891 GlobalServiceMapList = smap;
895 struct nick_host *nh, *next;
896 for (nh = smap->services; nh; nh = next)
902 MyFree(smap->command);
903 MyFree(smap->prepend);
909 pseudoitems: pseudoitem pseudoitems | pseudoitem;
910 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
911 pseudoname: NAME '=' QSTRING ';'
913 DupString(smap->name, $3);
915 pseudoprepend: PREPEND '=' QSTRING ';'
917 DupString(smap->prepend, $3);
919 pseudonick: NICK '=' QSTRING ';'
921 char *sep = strchr($3, '@');
924 size_t slen = strlen($3);
925 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
926 memcpy(nh->nick, $3, slen + 1);
927 nh->nicklen = sep - $3;
928 nh->next = smap->services;
933 iauthblock: IAUTH '{'
939 if (!host || !port) {
940 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
943 iauth_connect(host, port, pass, tconn, tping);
950 iauthitems: iauthitem iauthitems | iauthitem;
951 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
952 iauthpass: PASS '=' QSTRING ';'
957 iauthhost: HOST '=' QSTRING ';'
962 iauthport: PORT '=' NUMBER ';'
966 iauthconnfreq: CONNECTFREQ '=' timespec ';'
970 iauthtimeout: TIMEOUT '=' timespec ';'