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);
337 memset(&privs, 0, sizeof(privs));
338 memset(&privs_dirty, 0, sizeof(privs_dirty));
343 struct ConnectionClass *c_class;
344 add_class(name, tping, tconn, maxlinks, sendq);
345 c_class = find_class(name);
346 c_class->default_umode = pass;
347 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
348 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
351 parse_error("Missing name in class block");
355 classitems: classitem classitems | classitem;
356 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
357 classsendq | classusermode | priv | error;
358 classname: NAME '=' QSTRING ';'
363 classpingfreq: PINGFREQ '=' timespec ';'
367 classconnfreq: CONNECTFREQ '=' timespec ';'
371 classmaxlinks: MAXLINKS '=' expr ';'
375 classsendq: SENDQ '=' sizespec ';'
379 classusermode: USERMODE '=' QSTRING ';'
386 connectblock: CONNECT
388 name = pass = host = origin = hub_limit = NULL;
392 } '{' connectitems '}'
394 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
395 && !strchr(host, '*') && !strchr(host, '?'))
397 struct ConfItem *aconf = make_conf(CONF_SERVER);
399 aconf->origin_name = origin;
400 aconf->passwd = pass;
401 aconf->conn_class = c_class;
402 aconf->address.port = port;
404 aconf->maximum = maxlinks;
405 aconf->hub_limit = hub_limit;
406 lookup_confhost(aconf);
415 parse_error("Bad connect block");
418 connectitems: connectitem connectitems | connectitem;
419 connectitem: connectname | connectpass | connectclass | connecthost
420 | connectport | connectvhost | connectleaf | connecthub
421 | connecthublimit | connectmaxhops | error;
422 connectname: NAME '=' QSTRING ';'
427 connectpass: PASS '=' QSTRING ';'
432 connectclass: CLASS '=' QSTRING ';'
434 c_class = find_class($3);
436 connecthost: HOST '=' QSTRING ';'
441 connectport: PORT '=' NUMBER ';'
445 connectvhost: VHOST '=' QSTRING ';'
448 DupString(origin, $3);
450 connectleaf: LEAF ';'
457 DupString(hub_limit, "*");
459 connecthublimit: HUB '=' QSTRING ';'
462 DupString(hub_limit, $3);
464 connectmaxhops: MAXHOPS '=' expr ';'
469 uworldblock: UWORLD '{' uworlditems '}' ';'
473 struct ConfItem *aconf = make_conf(CONF_UWORLD);
479 parse_error("Bad UWorld block");
482 uworlditems: uworlditem uworlditems | uworlditem;
483 uworlditem: uworldname | error;
484 uworldname: NAME '=' QSTRING ';'
492 name = pass = host = NULL;
494 memset(&privs, 0, sizeof(privs));
495 memset(&privs_dirty, 0, sizeof(privs_dirty));
496 } '{' operitems '}' ';'
498 if (name && pass && host && c_class)
500 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
502 aconf->passwd = pass;
504 aconf->conn_class = c_class;
505 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
506 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
507 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
508 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
509 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
513 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
519 operitems: operitem | operitems operitem;
520 operitem: opername | operpass | operhost | operclass | priv | error;
521 opername: NAME '=' QSTRING ';'
526 operpass: PASS '=' QSTRING ';'
531 operhost: HOST '=' QSTRING ';'
534 if (!strchr($3, '@'))
537 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
538 ircd_snprintf(0, b, uh_len, "*@%s", $3);
544 operclass: CLASS '=' QSTRING ';'
546 c_class = find_class($3);
549 priv: privtype '=' yesorno ';'
551 FlagSet(&privs_dirty, $1);
552 if (($3 == 1) ^ invert)
559 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
560 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
561 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
562 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
563 KILL { $$ = PRIV_KILL; } |
564 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
565 TPRIV_REHASH { $$ = PRIV_REHASH; } |
566 TPRIV_RESTART { $$ = PRIV_RESTART; } |
567 TPRIV_DIE { $$ = PRIV_DIE; } |
568 TPRIV_GLINE { $$ = PRIV_GLINE; } |
569 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
570 JUPE { $$ = PRIV_JUPE; } |
571 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
572 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
573 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
574 TPRIV_SET { $$ = PRIV_SET; } |
575 TPRIV_WHOX { $$ = PRIV_WHOX; } |
576 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
577 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
578 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
579 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
580 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
581 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
582 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
583 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
584 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
585 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
586 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
587 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
588 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
590 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
592 /* The port block... */
596 /* Hijack these for is_server, is_hidden to cut down on globals... */
599 /* and this for mask... */
601 } '{' portitems '}' ';'
603 if (port > 0 && port <= 0xFFFF)
605 add_listener(port, host, pass, tconn, tping);
609 parse_error("Bad port block");
615 portitems: portitem portitems | portitem;
616 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
617 portnumber: PORT '=' NUMBER ';'
622 portvhost: VHOST '=' QSTRING ';'
628 portmask: MASK '=' QSTRING ';'
634 portserver: SERVER '=' YES ';'
637 } | SERVER '=' NO ';'
642 porthidden: HIDDEN '=' YES ';'
645 } | HIDDEN '=' NO ';'
656 '{' clientitems '}' ';'
660 struct ConfItem *aconf = make_conf(CONF_CLIENT);
663 aconf->conn_class = c_class ? c_class : find_class("default");
664 aconf->maximum = maxlinks;
670 parse_error("Bad client block");
673 clientitems: clientitem clientitems | clientitem;
674 clientitem: clienthost | clientclass | clientpass | clientip
675 | clientmaxlinks | error;
676 clientip: IP '=' QSTRING ';'
681 clienthost: HOST '=' QSTRING ';'
686 clientclass: CLASS '=' QSTRING ';'
688 c_class = find_class($3);
690 clientpass: PASS '=' QSTRING ';'
695 clientmaxlinks: MAXLINKS '=' expr ';'
702 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
703 memset(dconf, 0, sizeof(*dconf));
706 if (dconf->hostmask != NULL)
708 if (dconf->usermask == NULL)
709 DupString(dconf->usermask, "*");
710 dconf->next = denyConfList;
711 denyConfList = dconf;
716 MyFree(dconf->hostmask);
717 MyFree(dconf->message);
720 parse_error("Bad kill block");
723 killitems: killitem killitems | killitem;
724 killitem: killuhost | killreal | killreasonfile | killreason | error;
725 killuhost: HOST '=' QSTRING ';'
728 dconf->flags &= ~DENY_FLAGS_REALNAME;
729 MyFree(dconf->hostmask);
730 MyFree(dconf->usermask);
731 if ((h = strchr($3, '@')) == NULL)
741 DupString(dconf->hostmask, h);
742 DupString(dconf->usermask, u);
743 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
746 killreal: REAL '=' QSTRING ';'
748 dconf->flags &= ~DENY_FLAGS_IP;
749 dconf->flags |= DENY_FLAGS_REALNAME;
750 MyFree(dconf->hostmask);
751 /* Leave usermask so you can specify user and real... */
752 DupString(dconf->hostmask, $3);
755 killreason: REASON '=' QSTRING ';'
757 dconf->flags &= DENY_FLAGS_FILE;
758 MyFree(dconf->message);
759 DupString(dconf->message, $3);
762 killreasonfile: TFILE '=' QSTRING ';'
764 dconf->flags |= DENY_FLAGS_FILE;
765 MyFree(dconf->message);
766 DupString(dconf->message, $3);
775 struct CRuleNode *node;
776 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
778 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
783 p->next = cruleConfList;
790 parse_error("Bad CRule block");
794 cruleitems: cruleitem cruleitems | cruleitem;
795 cruleitem: cruleserver | crulerule | cruleall | error;
797 cruleserver: SERVER '=' QSTRING ';'
804 crulerule: RULE '=' QSTRING ';'
810 cruleall: ALL '=' YES ';'
822 if (host != NULL && pass != NULL)
823 motd_add(host, pass);
829 motditems: motditem motditems | motditem;
830 motditem: motdhost | motdfile | error;
831 motdhost: HOST '=' QSTRING ';'
836 motdfile: TFILE '=' QSTRING ';'
841 featuresblock: FEATURES '{' featureitems '}' ';';
842 featureitems: featureitems featureitem | featureitem;
848 } '=' stringlist ';';
856 feature_set(NULL, (const char * const *)stringlist, stringno);
858 posextrastrings: /* empty */ | extrastrings;
859 extrastrings: extrastrings extrastring | extrastring;
862 if (stringno < MAX_STRINGS)
863 stringlist[stringno++] = $1;
866 quarantineblock: QUARANTINE '{'
869 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
872 if (qconf->chname != NULL)
873 MyFree(qconf->chname);
874 if (qconf->reason != NULL)
875 MyFree(qconf->reason);
877 memset(qconf, 0, sizeof(*qconf));
878 } quarantineitems '}' ';'
880 if (qconf->chname == NULL || qconf->reason == NULL)
882 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
886 qconf->next = GlobalQuarantineList;
887 GlobalQuarantineList = qconf;
891 quarantineitems: CHANNEL NAME '=' QSTRING ';'
893 DupString(qconf->chname, $4);
894 } | REASON '=' QSTRING ';'
896 DupString(qconf->reason, $3);
899 pseudoblock: PSEUDO QSTRING '{'
901 smap = MyCalloc(1, sizeof(struct s_map));
902 DupString(smap->command, $2);
906 if (!smap->name || !smap->services)
908 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
911 if (register_mapping(smap))
913 smap->next = GlobalServiceMapList;
914 GlobalServiceMapList = smap;
918 struct nick_host *nh, *next;
919 for (nh = smap->services; nh; nh = next)
925 MyFree(smap->command);
926 MyFree(smap->prepend);
932 pseudoitems: pseudoitem pseudoitems | pseudoitem;
933 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
934 pseudoname: NAME '=' QSTRING ';'
936 DupString(smap->name, $3);
938 pseudoprepend: PREPEND '=' QSTRING ';'
940 DupString(smap->prepend, $3);
942 pseudonick: NICK '=' QSTRING ';'
944 char *sep = strchr($3, '@');
947 size_t slen = strlen($3);
948 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
949 memcpy(nh->nick, $3, slen + 1);
950 nh->nicklen = sep - $3;
951 nh->next = smap->services;
956 iauthblock: IAUTH '{'
964 if (!host || !port) {
965 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
968 iauth_connect(host, port, pass, tconn, tping);
974 iauthitems: iauthitem iauthitems | iauthitem;
975 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
976 iauthpass: PASS '=' QSTRING ';'
981 iauthhost: HOST '=' QSTRING ';'
986 iauthport: PORT '=' NUMBER ';'
990 iauthconnfreq: CONNECTFREQ '=' timespec ';'
994 iauthtimeout: TIMEOUT '=' timespec ';'