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(C) 2001 by Andrew Miller, the
5 * 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;
71 /* Now all the globals we need :/... */
72 int tping, tconn, maxlinks, sendq, port, invert;
74 char *name, *pass, *host;
75 char *stringlist[MAX_STRINGS];
76 struct ConnectionClass *c_class;
77 struct ConfItem *aconf;
78 struct DenyConf *dconf;
79 struct ServerConf *sconf;
80 struct qline *qconf = NULL;
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);
158 /* and now a lot of priviledges... */
159 %token TPRIV_CHAN_LIMIT TPRIV_MODE_LCHAN TPRIV_DEOP_LCHAN TPRIV_WALK_LCHAN
160 %token TPRIV_KILL TPRIV_LOCAL_KILL TPRIV_REHASH TPRIV_RESTART TPRIV_DIE
161 %token TPRIV_GLINE TPRIV_LOCAL_GLINE TPRIV_JUPE TPRIV_LOCAL_JUPE
162 %token TPRIV_LOCAL_OPMODE TPRIV_OPMODE TPRIV_SET TPRIV_WHOX TPRIV_BADCHAN
163 %token TPRIV_LOCAL_BADCHAN
164 %token TPRIV_SEE_CHAN TPRIV_SHOW_INVIS TPRIV_SHOW_ALL_INVIS TPRIV_PROPAGATE
165 %token TPRIV_UNLIMIT_QUERY TPRIV_DISPLAY TPRIV_SEE_OPERS TPRIV_WIDE_GLINE
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 serverblock | operblock | portblock | jupeblock | clientblock |
183 killblock | cruleblock | motdblock | featuresblock | quarantineblock |
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
262 addNickJupes(yylval.text);
265 generalblock: GENERAL '{' generalitems '}' ';' ;
266 generalitems: generalitem generalitems | generalitem;
267 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
268 generalnumeric: NUMERIC '=' NUMBER ';'
270 if (localConf.numeric == 0)
271 localConf.numeric = yylval.num;
273 parse_error("Redefinition of server numeric %i (%i)",yylval.num,
277 generalname: NAME '=' QSTRING ';'
279 if (localConf.name == NULL)
280 DupString(localConf.name, yylval.text);
282 parse_error("Redefinition of server name %s (%s)",yylval.text,
286 generaldesc: DESCRIPTION '=' QSTRING ';'
288 MyFree(localConf.description);
289 DupString(localConf.description, yylval.text);
290 ircd_strncpy(cli_info(&me), yylval.text, REALLEN);
293 generalvhost: VHOST '=' QSTRING ';'
296 (localConf.vhost_address.s_addr = inet_addr(yylval.text)))
297 localConf.vhost_address.s_addr = INADDR_ANY;
300 adminblock: ADMIN '{' adminitems '}'
302 if (localConf.location1 == NULL)
303 DupString(localConf.location1, "");
304 if (localConf.location2 == NULL)
305 DupString(localConf.location2, "");
306 if (localConf.contact == NULL)
307 DupString(localConf.contact, "");
309 adminitems: adminitems adminitem | adminitem;
310 adminitem: adminlocation | admincontact | error;
311 adminlocation: LOCATION '=' QSTRING ';'
313 if (localConf.location1 == NULL)
314 DupString(localConf.location1, yylval.text);
315 else if (localConf.location2 == NULL)
316 DupString(localConf.location2, yylval.text);
317 /* Otherwise just drop it. -A1kmm */
319 admincontact: CONTACT '=' QSTRING ';'
321 if (localConf.contact != NULL)
322 MyFree(localConf.contact);
323 DupString(localConf.contact, yylval.text);
337 add_class(name, tping, tconn, maxlinks, sendq);
338 find_class(name)->default_umode = pass;
341 parse_error("Missing name in class block");
345 classitems: classitem classitems | classitem;
346 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
347 classsendq | classusermode | error;
348 classname: NAME '=' QSTRING ';'
351 DupString(name, yylval.text);
353 classpingfreq: PINGFREQ '=' timespec ';'
357 classconnfreq: CONNECTFREQ '=' timespec ';'
361 classmaxlinks: MAXLINKS '=' expr ';'
363 maxlinks = yylval.num;
365 classsendq: SENDQ '=' sizespec ';'
369 classusermode: USERMODE '=' QSTRING ';'
373 DupString(pass, yylval.text);
376 connectblock: CONNECT
378 name = pass = host = NULL;
381 } '{' connectitems '}'
383 if (name != NULL && pass != NULL && host != NULL && c_class != NULL &&
384 /*ccount < MAXCONFLINKS &&*/ !strchr(host, '*') &&
388 aconf->status = CONF_SERVER;
390 aconf->passwd = pass;
391 aconf->conn_class = c_class;
393 aconf->status = CONF_SERVER;
395 aconf->next = GlobalConfList;
396 aconf->ipnum.s_addr = INADDR_NONE;
397 lookup_confhost(aconf);
398 GlobalConfList = aconf;
405 name = pass = host = NULL;
406 parse_error("Bad connect block");
409 connectitems: connectitem connectitems | connectitem;
410 connectitem: connectname | connectpass | connectclass | connecthost
411 | connectport | error;
412 connectname: NAME '=' QSTRING ';'
415 DupString(name, yylval.text);
417 connectpass: PASS '=' QSTRING ';'
420 DupString(pass, yylval.text);
422 connectclass: CLASS '=' QSTRING ';'
424 c_class = find_class(yylval.text);
426 connecthost: HOST '=' QSTRING ';'
429 DupString(host, yylval.text);
431 connectport: PORT '=' NUMBER ';'
438 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
439 memset(aconf, 0, sizeof(*aconf));
440 } '{' serveritems '}'
442 if (aconf->status == 0)
448 parse_error("Bad server block");
452 aconf->next = GlobalConfList;
453 GlobalConfList = aconf;
456 serveritems: serveritem serveritems | serveritem;
457 serveritem: servername | servermask | serverhub | serverleaf |
458 serveruworld | error;
459 servername: NAME '=' QSTRING
462 DupString(aconf->name, yylval.text);
464 servermask: MASK '=' QSTRING
467 DupString(aconf->host, yylval.text);
469 /* XXX - perhaps we should do this the hybrid way in connect blocks
471 serverhub: HUB '=' YES ';'
473 aconf->status |= CONF_HUB;
474 aconf->status &= ~CONF_LEAF;
478 aconf->status &= ~CONF_HUB;
480 serverleaf: LEAF '=' YES ';'
482 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
483 aconf->status |= CONF_LEAF;
485 parse_error("Server is both leaf and a hub");
489 aconf->status &= ~CONF_LEAF;
491 serveruworld: UWORLD '=' YES ';'
493 aconf->status |= CONF_UWORLD;
494 aconf->status &= ~CONF_LEAF;
498 aconf->status &= ~CONF_UWORLD;
503 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
504 memset(aconf, 0, sizeof(*aconf));
505 aconf->status = CONF_OPERATOR;
506 } '{' operitems '}' ';'
508 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL)
510 aconf->next = GlobalConfList;
511 GlobalConfList = aconf;
515 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
517 MyFree(aconf->passwd);
523 operitems: operitem | operitems operitem;
524 operitem: opername | operpass | operhost | operclass | operpriv | error;
526 opername: NAME '=' QSTRING ';'
529 DupString(aconf->name, yylval.text);
532 operpass: PASS '=' QSTRING ';'
534 MyFree(aconf->passwd);
535 DupString(aconf->passwd, yylval.text);
538 operhost: HOST '=' QSTRING ';'
541 if (!strchr(yylval.text, '@'))
544 char *b = (char*) MyMalloc((uh_len = strlen(yylval.text)+3));
545 ircd_snprintf(0, b, uh_len, "*@%s", yylval.text);
549 DupString(aconf->host, yylval.text);
552 operclass: CLASS '=' QSTRING ';'
554 aconf->conn_class = find_class(yylval.text);
557 operpriv: privtype '=' yesorno ';'
559 PrivSet(&aconf->privs_dirty, $1);
560 if (($3 == 1) ^ invert)
561 PrivSet(&aconf->privs, $1);
563 PrivClr(&aconf->privs, $1);
567 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
568 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
569 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
570 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
571 TPRIV_KILL { $$ = PRIV_KILL; } |
572 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
573 TPRIV_REHASH { $$ = PRIV_REHASH; } |
574 TPRIV_RESTART { $$ = PRIV_RESTART; } |
575 TPRIV_DIE { $$ = PRIV_DIE; } |
576 TPRIV_GLINE { $$ = PRIV_GLINE; } |
577 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
578 TPRIV_JUPE { $$ = PRIV_JUPE; } |
579 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
580 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
581 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
582 TPRIV_SET { $$ = PRIV_SET; } |
583 TPRIV_WHOX { $$ = PRIV_WHOX; } |
584 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
585 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
586 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
587 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
588 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
589 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
590 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
591 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
592 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
593 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
594 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; };
596 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
598 /* The port block... */
602 /* Hijack these for is_server, is_hidden to cut down on globals... */
605 /* and this for mask... */
607 } '{' portitems '}' ';'
609 if (port > 0 && port <= 0xFFFF)
611 add_listener(port, host, pass, tconn, tping);
618 parse_error("Bad port block");
621 portitems: portitem portitems | portitem;
622 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
623 portnumber: PORT '=' NUMBER ';'
628 portvhost: VHOST '=' QSTRING ';'
631 DupString(host, yylval.text);
634 portmask: MASK '=' QSTRING ';'
637 DupString(pass, yylval.text);
640 portserver: SERVER '=' YES ';'
643 } | SERVER '=' NO ';'
648 porthidden: HIDDEN '=' YES ';'
651 } | HIDDEN '=' NO ';'
658 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
659 memset(aconf, 0, sizeof(*aconf));
660 aconf->status = CONF_CLIENT;
661 } '{' clientitems '}'
663 if ((aconf->host != NULL || aconf->name!=NULL))
665 if (aconf->host == NULL)
666 DupString(aconf->host, "");
667 if (aconf->name == NULL)
668 DupString(aconf->name, "");
669 if (aconf->conn_class == NULL)
670 aconf->conn_class = find_class("default");
671 aconf->next = GlobalConfList;
672 GlobalConfList = aconf;
678 MyFree(aconf->passwd);
681 parse_error("Bad client block");
684 clientitems: clientitem clientitems | clientitem;
685 clientitem: clienthost | clientclass | clientpass | clientip | error;
686 clientip: IP '=' QSTRING ';'
689 DupString(aconf->host, yylval.text);
692 clienthost: HOST '=' QSTRING ';'
695 DupString(aconf->name, yylval.text);
698 clientclass: CLASS '=' QSTRING ';'
700 aconf->conn_class = find_class(yylval.text);
703 clientpass: PASS '=' QSTRING ';'
705 MyFree(aconf->passwd);
706 DupString(aconf->passwd, yylval.text);
711 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
712 memset(dconf, 0, sizeof(*dconf));
715 if (dconf->hostmask != NULL)
717 if (dconf->usermask == NULL)
718 DupString(dconf->usermask, "*");
719 dconf->next = denyConfList;
720 denyConfList = dconf;
725 MyFree(dconf->hostmask);
726 MyFree(dconf->message);
729 parse_error("Bad kill block");
732 killitems: killitem killitems | killitem;
733 killitem: killuhost | killreal | killreasonfile | killreason | error;
734 killuhost: HOST '=' QSTRING ';'
737 dconf->flags &= ~DENY_FLAGS_REALNAME;
738 MyFree(dconf->hostmask);
739 MyFree(dconf->usermask);
740 if ((h = strchr(yylval.text, '@')) == NULL)
750 DupString(dconf->hostmask, h);
751 DupString(dconf->usermask, u);
752 if (strchr(yylval.text, '.'))
758 dconf->flags |= DENY_FLAGS_IP;
759 c_class = sscanf(dconf->hostmask, "%d.%d.%d.%d/%d",
760 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
762 dconf->bits = c_class * 8;
767 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
769 dconf->address = inet_addr(ipname);
773 killreal: REAL '=' QSTRING ';'
775 dconf->flags &= ~DENY_FLAGS_IP;
776 dconf->flags |= DENY_FLAGS_REALNAME;
777 MyFree(dconf->hostmask);
778 /* Leave usermask so you can specify user and real... */
779 DupString(dconf->hostmask, yylval.text);
782 killreason: REASON '=' QSTRING ';'
784 dconf->flags &= DENY_FLAGS_FILE;
785 MyFree(dconf->message);
786 DupString(dconf->message, yylval.text);
789 killreasonfile: TFILE '=' QSTRING ';'
791 dconf->flags |= DENY_FLAGS_FILE;
792 MyFree(dconf->message);
793 DupString(dconf->message, yylval.text);
802 struct CRuleNode *node;
803 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
805 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
810 p->next = cruleConfList;
817 parse_error("Bad CRule block");
821 cruleitems: cruleitem cruleitems | cruleitem;
822 cruleitem: cruleserver | crulerule | cruleall | error;
824 cruleserver: SERVER '=' QSTRING ';'
827 collapse(yylval.text);
828 DupString(host, yylval.text);
831 crulerule: RULE '=' QSTRING ';'
834 DupString(pass, yylval.text);
837 cruleall: ALL '=' YES ';'
849 if (host != NULL && pass != NULL)
850 motd_add(host, pass);
856 motditems: motditem motditems | motditem;
857 motditem: motdhost | motdfile | error;
858 motdhost: HOST '=' QSTRING ';'
860 DupString(host, yylval.text);
863 motdfile: TFILE '=' QSTRING ';'
865 DupString(pass, yylval.text);
868 featuresblock: FEATURES '{' featureitems '}' ';';
869 featureitems: featureitems featureitem | featureitem;
875 } '=' stringlist ';';
883 feature_set(NULL, (const char * const *)stringlist, stringno);
885 posextrastrings: /* empty */ | extrastrings;
886 extrastrings: extrastrings extrastring | extrastring;
889 if (stringno < MAX_STRINGS)
890 stringlist[stringno++] = $1;
893 quarantineblock: QUARANTINE '{'
896 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
899 if (qconf->chname != NULL)
900 MyFree(qconf->chname);
901 if (qconf->reason != NULL)
902 MyFree(qconf->reason);
904 memset(qconf, 0, sizeof(*qconf));
905 } quarantineitems '}' ';'
907 if (qconf->chname == NULL || qconf->reason == NULL)
909 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
913 qconf->next = GlobalQuarantineList;
914 GlobalQuarantineList = qconf;
918 quarantineitems: CHANNEL NAME '=' QSTRING ';'
920 DupString(qconf->chname, yylval.text);
921 } | REASON '=' QSTRING ';'
923 DupString(qconf->reason, yylval.text);
926 pseudoblock: PSEUDO QSTRING '{'
928 smap = MyCalloc(1, sizeof(struct s_map));
929 DupString(smap->command, $2);
933 if (!smap->name || !smap->services)
935 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
938 if (register_mapping(smap))
940 smap->next = GlobalServiceMapList;
941 GlobalServiceMapList = smap;
945 struct nick_host *nh, *next;
946 for (nh = smap->services; nh; nh = next)
952 MyFree(smap->command);
953 MyFree(smap->prepend);
959 pseudoitems: pseudoitem pseudoitems | pseudoitem;
960 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
961 pseudoname: NAME '=' QSTRING ';'
963 DupString(smap->name, yylval.text);
965 pseudoprepend: PREPEND '=' QSTRING ';'
967 DupString(smap->prepend, yylval.text);
969 pseudonick: NICK '=' QSTRING ';'
971 char *sep = strchr(yylval.text, '@');
974 size_t slen = strlen(yylval.text);
975 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
976 memcpy(nh->nick, yylval.text, slen + 1);
977 nh->nicklen = sep - yylval.text;
978 nh->next = smap->services;