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);
157 /* and now a lot of priviledges... */
158 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
159 %token TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
160 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_LOCAL_JUPE TPRIV_LOCAL_BADCHAN
161 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
162 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
163 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
164 %token TPRIV_FORCE_OPMODE TPRIV_FORCE_LOCAL_OPMODE
165 /* and some types... */
167 %type <num> timespec timefactor factoredtimes factoredtime
168 %type <num> expr yesorno privtype
178 /* Blocks in the config file... */
179 blocks: blocks block | block;
180 block: adminblock | generalblock | classblock | connectblock |
181 uworldblock | operblock | portblock | jupeblock | clientblock |
182 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
183 pseudoblock | iauthblock | error;
185 /* The timespec, sizespec and expr was ripped straight from
187 timespec: expr | factoredtimes;
189 factoredtimes: factoredtimes factoredtime
194 factoredtime: expr timefactor
199 timefactor: SECONDS { $$ = 1; }
200 | MINUTES { $$ = 60; }
201 | HOURS { $$ = 60 * 60; }
202 | DAYS { $$ = 60 * 60 * 24; }
203 | WEEKS { $$ = 60 * 60 * 24 * 7; }
204 | MONTHS { $$ = 60 * 60 * 24 * 7 * 4; }
205 | YEARS { $$ = 60 * 60 * 24 * 365; }
206 | DECADES { $$ = 60 * 60 * 24 * 365 * 10; };
219 $$ = $1 * 1024 * 1024;
222 $$ = $1 * 1024 * 1024 * 1024;
225 $$ = $1 * 1024 * 1024 * 1024;
229 /* this is an arithmatic expression */
246 /* leave this out until we find why it makes BSD yacc dump core -larne
256 jupeblock: JUPE '{' jupeitems '}' ';' ;
257 jupeitems: jupeitem jupeitems | jupeitem;
258 jupeitem: jupenick | error;
259 jupenick: NICK '=' QSTRING
264 generalblock: GENERAL '{' generalitems '}'
266 if (localConf.name == NULL)
267 parse_error("Your General block must contain a name.");
268 if (localConf.numeric == 0)
269 parse_error("Your General block must contain a numeric (between 1 and 4095).");
271 generalitems: generalitem generalitems | generalitem;
272 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
273 generalnumeric: NUMERIC '=' NUMBER ';'
275 if (localConf.numeric == 0)
276 localConf.numeric = $3;
277 else if (localConf.numeric != $3)
278 parse_error("Redefinition of server numeric %i (%i)", $3,
282 generalname: NAME '=' QSTRING ';'
284 if (localConf.name == NULL)
285 DupString(localConf.name, $3);
286 else if (strcmp(localConf.name, $3))
287 parse_error("Redefinition of server name %s (%s)", $3,
291 generaldesc: DESCRIPTION '=' QSTRING ';'
293 MyFree(localConf.description);
294 DupString(localConf.description, $3);
295 ircd_strncpy(cli_info(&me), $3, REALLEN);
298 generalvhost: VHOST '=' QSTRING ';'
300 ircd_aton(&VirtualHost.addr, $3);
303 adminblock: ADMIN '{' adminitems '}'
305 if (localConf.location1 == NULL)
306 DupString(localConf.location1, "");
307 if (localConf.location2 == NULL)
308 DupString(localConf.location2, "");
309 if (localConf.contact == NULL)
310 DupString(localConf.contact, "");
312 adminitems: adminitems adminitem | adminitem;
313 adminitem: adminlocation | admincontact | error;
314 adminlocation: LOCATION '=' QSTRING ';'
316 if (localConf.location1 == NULL)
317 DupString(localConf.location1, $3);
318 else if (localConf.location2 == NULL)
319 DupString(localConf.location2, $3);
320 /* Otherwise just drop it. -A1kmm */
322 admincontact: CONTACT '=' QSTRING ';'
324 if (localConf.contact != NULL)
325 MyFree(localConf.contact);
326 DupString(localConf.contact, $3);
335 struct ConnectionClass *c_class;
336 add_class(name, tping, tconn, maxlinks, sendq);
337 c_class = find_class(name);
338 c_class->default_umode = pass;
339 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
340 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
343 parse_error("Missing name in class block");
350 memset(&privs, 0, sizeof(privs));
351 memset(&privs_dirty, 0, sizeof(privs_dirty));
353 classitems: classitem classitems | classitem;
354 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
355 classsendq | classusermode | priv | error;
356 classname: NAME '=' QSTRING ';'
361 classpingfreq: PINGFREQ '=' timespec ';'
365 classconnfreq: CONNECTFREQ '=' timespec ';'
369 classmaxlinks: MAXLINKS '=' expr ';'
373 classsendq: SENDQ '=' sizespec ';'
377 classusermode: USERMODE '=' QSTRING ';'
383 connectblock: CONNECT
386 } '{' connectitems '}'
388 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
389 && !strchr(host, '*') && !strchr(host, '?'))
391 struct ConfItem *aconf = make_conf(CONF_SERVER);
393 aconf->origin_name = origin;
394 aconf->passwd = pass;
395 aconf->conn_class = c_class;
396 aconf->address.port = port;
398 aconf->maximum = maxlinks;
399 aconf->hub_limit = hub_limit;
400 lookup_confhost(aconf);
409 parse_error("Bad connect block");
411 name = pass = host = origin = hub_limit = NULL;
415 connectitems: connectitem connectitems | connectitem;
416 connectitem: connectname | connectpass | connectclass | connecthost
417 | connectport | connectvhost | connectleaf | connecthub
418 | connecthublimit | connectmaxhops | error;
419 connectname: NAME '=' QSTRING ';'
424 connectpass: PASS '=' QSTRING ';'
429 connectclass: CLASS '=' QSTRING ';'
431 c_class = find_class($3);
433 connecthost: HOST '=' QSTRING ';'
438 connectport: PORT '=' NUMBER ';'
442 connectvhost: VHOST '=' QSTRING ';'
445 DupString(origin, $3);
447 connectleaf: LEAF ';'
454 DupString(hub_limit, "*");
456 connecthublimit: HUB '=' QSTRING ';'
459 DupString(hub_limit, $3);
461 connectmaxhops: MAXHOPS '=' expr ';'
466 uworldblock: UWORLD '{' uworlditems '}' ';'
470 struct ConfItem *aconf = make_conf(CONF_UWORLD);
476 parse_error("Bad UWorld block");
480 uworlditems: uworlditem uworlditems | uworlditem;
481 uworlditem: uworldname | error;
482 uworldname: NAME '=' QSTRING ';'
488 operblock: OPER '{' operitems '}' ';'
490 if (name && pass && host && c_class)
492 struct ConfItem *aconf = make_conf(CONF_OPERATOR);
494 aconf->passwd = pass;
495 conf_parse_userhost(aconf, host);
496 aconf->conn_class = c_class;
497 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
498 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
499 if (!FlagHas(&privs_dirty, PRIV_PROPAGATE)
500 && !FlagHas(&c_class->privs_dirty, PRIV_PROPAGATE))
501 parse_error("Operator block for %s and class %s have no LOCAL setting", name, c_class->cc_name);
505 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
510 name = pass = host = NULL;
512 memset(&privs, 0, sizeof(privs));
513 memset(&privs_dirty, 0, sizeof(privs_dirty));
515 operitems: operitem | operitems operitem;
516 operitem: opername | operpass | operhost | operclass | priv | error;
517 opername: NAME '=' QSTRING ';'
522 operpass: PASS '=' QSTRING ';'
527 operhost: HOST '=' QSTRING ';'
530 if (!strchr($3, '@'))
533 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
534 ircd_snprintf(0, b, uh_len, "*@%s", $3);
540 operclass: CLASS '=' QSTRING ';'
542 c_class = find_class($3);
545 priv: privtype '=' yesorno ';'
547 FlagSet(&privs_dirty, $1);
548 if (($3 == 1) ^ invert)
555 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
556 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
557 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
558 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
559 KILL { $$ = PRIV_KILL; } |
560 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
561 TPRIV_REHASH { $$ = PRIV_REHASH; } |
562 TPRIV_RESTART { $$ = PRIV_RESTART; } |
563 TPRIV_DIE { $$ = PRIV_DIE; } |
564 TPRIV_GLINE { $$ = PRIV_GLINE; } |
565 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
566 JUPE { $$ = PRIV_JUPE; } |
567 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
568 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
569 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
570 TPRIV_SET { $$ = PRIV_SET; } |
571 TPRIV_WHOX { $$ = PRIV_WHOX; } |
572 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
573 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
574 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
575 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
576 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
577 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
578 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
579 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
580 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
581 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
582 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
583 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
584 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
586 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
588 /* The port block... */
589 portblock: PORT '{' portitems '}' ';'
591 if (port > 0 && port <= 0xFFFF)
593 add_listener(port, host, pass, tconn, tping);
597 parse_error("Bad port block");
602 port = tconn = tping = 0;
604 portitems: portitem portitems | portitem;
605 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
606 portnumber: PORT '=' NUMBER ';'
611 portvhost: VHOST '=' QSTRING ';'
617 portmask: MASK '=' QSTRING ';'
623 portserver: SERVER '=' YES ';'
626 } | SERVER '=' NO ';'
631 porthidden: HIDDEN '=' YES ';'
634 } | HIDDEN '=' NO ';'
643 '{' clientitems '}' ';'
647 struct ConfItem *aconf = make_conf(CONF_CLIENT);
648 conf_parse_userhost(aconf, host);
649 aconf->conn_class = c_class ? c_class : find_class("default");
650 aconf->maximum = maxlinks;
655 parse_error("Bad client block");
660 clientitems: clientitem clientitems | clientitem;
661 clientitem: clienthost | clientclass | clientpass | clientmaxlinks | error;
662 clienthost: HOST '=' QSTRING ';'
667 clientclass: CLASS '=' QSTRING ';'
669 c_class = find_class($3);
671 clientpass: PASS '=' QSTRING ';'
676 clientmaxlinks: MAXLINKS '=' expr ';'
683 dconf = (struct DenyConf*) MyCalloc(1, sizeof(*dconf));
686 if (dconf->hostmask != NULL)
688 if (dconf->usermask == NULL)
689 DupString(dconf->usermask, "*");
690 dconf->next = denyConfList;
691 denyConfList = dconf;
695 MyFree(dconf->hostmask);
696 MyFree(dconf->message);
698 parse_error("Bad kill block");
702 killitems: killitem killitems | killitem;
703 killitem: killuhost | killreal | killreasonfile | killreason | error;
704 killuhost: HOST '=' QSTRING ';'
707 dconf->flags &= ~DENY_FLAGS_REALNAME;
708 MyFree(dconf->hostmask);
709 MyFree(dconf->usermask);
710 if ((h = strchr($3, '@')) == NULL)
720 DupString(dconf->hostmask, h);
721 DupString(dconf->usermask, u);
722 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
725 killreal: REAL '=' QSTRING ';'
727 dconf->flags &= ~DENY_FLAGS_IP;
728 dconf->flags |= DENY_FLAGS_REALNAME;
729 MyFree(dconf->hostmask);
730 /* Leave usermask so you can specify user and real... */
731 DupString(dconf->hostmask, $3);
734 killreason: REASON '=' QSTRING ';'
736 dconf->flags &= DENY_FLAGS_FILE;
737 MyFree(dconf->message);
738 DupString(dconf->message, $3);
741 killreasonfile: TFILE '=' QSTRING ';'
743 dconf->flags |= DENY_FLAGS_FILE;
744 MyFree(dconf->message);
745 DupString(dconf->message, $3);
751 } '{' cruleitems '}' ';'
753 struct CRuleNode *node;
754 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
756 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
761 p->next = cruleConfList;
768 parse_error("Bad CRule block");
773 cruleitems: cruleitem cruleitems | cruleitem;
774 cruleitem: cruleserver | crulerule | cruleall | error;
776 cruleserver: SERVER '=' QSTRING ';'
783 crulerule: RULE '=' QSTRING ';'
789 cruleall: ALL '=' YES ';'
797 motdblock: MOTD '{' motditems '}' ';'
799 if (host != NULL && pass != NULL)
800 motd_add(host, pass);
806 motditems: motditem motditems | motditem;
807 motditem: motdhost | motdfile | error;
808 motdhost: HOST '=' QSTRING ';'
813 motdfile: TFILE '=' QSTRING ';'
818 featuresblock: FEATURES '{' featureitems '}' ';';
819 featureitems: featureitems featureitem | featureitem;
825 } '=' stringlist ';';
833 feature_set(NULL, (const char * const *)stringlist, stringno);
835 posextrastrings: /* empty */ | extrastrings;
836 extrastrings: extrastrings extrastring | extrastring;
839 if (stringno < MAX_STRINGS)
840 stringlist[stringno++] = $1;
843 quarantineblock: QUARANTINE '{'
845 qconf = (struct qline*) MyCalloc(1, sizeof(*qconf));
846 } quarantineitems '}' ';'
848 if (qconf->chname == NULL || qconf->reason == NULL)
850 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
854 qconf->next = GlobalQuarantineList;
855 GlobalQuarantineList = qconf;
859 quarantineitems: CHANNEL NAME '=' QSTRING ';'
861 DupString(qconf->chname, $4);
862 } | REASON '=' QSTRING ';'
864 DupString(qconf->reason, $3);
867 pseudoblock: PSEUDO QSTRING '{'
869 smap = MyCalloc(1, sizeof(struct s_map));
870 DupString(smap->command, $2);
874 if (!smap->name || !smap->services)
876 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
879 if (register_mapping(smap))
881 smap->next = GlobalServiceMapList;
882 GlobalServiceMapList = smap;
886 struct nick_host *nh, *next;
887 for (nh = smap->services; nh; nh = next)
893 MyFree(smap->command);
894 MyFree(smap->prepend);
900 pseudoitems: pseudoitem pseudoitems | pseudoitem;
901 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
902 pseudoname: NAME '=' QSTRING ';'
904 DupString(smap->name, $3);
906 pseudoprepend: PREPEND '=' QSTRING ';'
908 DupString(smap->prepend, $3);
910 pseudonick: NICK '=' QSTRING ';'
912 char *sep = strchr($3, '@');
915 size_t slen = strlen($3);
916 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
917 memcpy(nh->nick, $3, slen + 1);
918 nh->nicklen = sep - $3;
919 nh->next = smap->services;
924 iauthblock: IAUTH '{'
930 if (!host || !port) {
931 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
934 iauth_connect(host, port, pass, tconn, tping);
941 iauthitems: iauthitem iauthitems | iauthitem;
942 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
943 iauthpass: PASS '=' QSTRING ';'
948 iauthhost: HOST '=' QSTRING ';'
953 iauthport: PORT '=' NUMBER ';'
957 iauthconnfreq: CONNECTFREQ '=' timespec ';'
961 iauthtimeout: TIMEOUT '=' timespec ';'