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"
61 #include <arpa/inet.h>
62 #define MAX_STRINGS 80 /* Maximum number of feature params. */
63 extern struct LocalConf localConf;
64 extern struct DenyConf* denyConfList;
65 extern struct CRuleConf* cruleConfList;
66 extern struct ServerConf* serverConfList;
67 extern struct s_map* GlobalServiceMapList;
68 extern struct qline* GlobalQuarantineList;
72 /* Now all the globals we need :/... */
73 int tping, tconn, maxlinks, sendq, port, invert;
75 char *name, *pass, *host, *origin;
76 char *stringlist[MAX_STRINGS];
77 struct ConnectionClass *c_class;
78 struct ConfItem *aconf;
79 struct DenyConf *dconf;
80 struct ServerConf *sconf;
81 struct qline *qconf = NULL;
84 struct Privs privs_dirty;
86 static void parse_error(char *pattern,...) {
87 static char error_buffer[1024];
90 ircd_vsnprintf(NULL, error_buffer, sizeof(error_buffer), pattern, vl);
92 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 serverblock | 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 | 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 adminblock: ADMIN '{' adminitems '}'
309 if (localConf.location1 == NULL)
310 DupString(localConf.location1, "");
311 if (localConf.location2 == NULL)
312 DupString(localConf.location2, "");
313 if (localConf.contact == NULL)
314 DupString(localConf.contact, "");
316 adminitems: adminitems adminitem | adminitem;
317 adminitem: adminlocation | admincontact | error;
318 adminlocation: LOCATION '=' QSTRING ';'
320 if (localConf.location1 == NULL)
321 DupString(localConf.location1, $3);
322 else if (localConf.location2 == NULL)
323 DupString(localConf.location2, $3);
324 /* Otherwise just drop it. -A1kmm */
326 admincontact: CONTACT '=' QSTRING ';'
328 if (localConf.contact != NULL)
329 MyFree(localConf.contact);
330 DupString(localConf.contact, $3);
340 memset(&privs, 0, sizeof(privs));
341 memset(&privs_dirty, 0, sizeof(privs_dirty));
346 struct ConnectionClass *c_class;
347 add_class(name, tping, tconn, maxlinks, sendq);
348 c_class = find_class(name);
349 c_class->default_umode = pass;
350 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
351 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
354 parse_error("Missing name in class block");
358 classitems: classitem classitems | classitem;
359 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
360 classsendq | classusermode | priv | error;
361 classname: NAME '=' QSTRING ';'
366 classpingfreq: PINGFREQ '=' timespec ';'
370 classconnfreq: CONNECTFREQ '=' timespec ';'
374 classmaxlinks: MAXLINKS '=' expr ';'
378 classsendq: SENDQ '=' sizespec ';'
382 classusermode: USERMODE '=' QSTRING ';'
389 connectblock: CONNECT
391 name = pass = host = origin = NULL;
394 } '{' connectitems '}'
396 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
397 && !strchr(host, '*') && !strchr(host, '?'))
400 aconf->status = CONF_SERVER;
402 aconf->origin_name = origin;
403 aconf->passwd = pass;
404 aconf->conn_class = c_class;
405 aconf->address.port = port;
406 aconf->status = CONF_SERVER;
408 aconf->next = GlobalConfList;
409 lookup_confhost(aconf);
410 GlobalConfList = aconf;
418 parse_error("Bad connect block");
420 name = pass = host = origin = NULL;
422 connectitems: connectitem connectitems | connectitem;
423 connectitem: connectname | connectpass | connectclass | connecthost
424 | connectport | connectvhost | error;
425 connectname: NAME '=' QSTRING ';'
430 connectpass: PASS '=' QSTRING ';'
435 connectclass: CLASS '=' QSTRING ';'
437 c_class = find_class($3);
439 connecthost: HOST '=' QSTRING ';'
444 connectport: PORT '=' NUMBER ';'
448 connectvhost: VHOST '=' QSTRING ';'
451 DupString(origin, $3);
456 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
457 memset(aconf, 0, sizeof(*aconf));
458 aconf->status = CONF_LEAF;
459 } '{' serveritems '}'
461 if (aconf->status == 0)
467 parse_error("Bad server block");
471 aconf->next = GlobalConfList;
472 GlobalConfList = aconf;
475 serveritems: serveritem serveritems | serveritem;
476 serveritem: servername | servermask | serverhub | serverleaf |
477 serveruworld | error;
478 servername: NAME '=' QSTRING
481 DupString(aconf->name, $3);
483 servermask: MASK '=' QSTRING
486 DupString(aconf->host, $3);
488 /* XXX - perhaps we should do this the hybrid way in connect blocks
490 serverhub: HUB '=' YES ';'
492 aconf->status |= CONF_HUB;
493 aconf->status &= ~CONF_LEAF;
497 aconf->status &= ~CONF_HUB;
499 serverleaf: LEAF '=' YES ';'
501 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
502 aconf->status |= CONF_LEAF;
504 parse_error("Server is both leaf and a hub");
508 aconf->status &= ~CONF_LEAF;
510 serveruworld: UWORLD '=' YES ';'
512 aconf->status |= CONF_UWORLD;
513 aconf->status &= ~CONF_LEAF;
517 aconf->status &= ~CONF_UWORLD;
522 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
523 memset(aconf, 0, sizeof(*aconf));
524 memset(&privs, 0, sizeof(privs));
525 memset(&privs_dirty, 0, sizeof(privs_dirty));
526 aconf->status = CONF_OPERATOR;
527 } '{' operitems '}' ';'
529 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
530 && aconf->conn_class != NULL)
532 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
533 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
534 if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
535 && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
536 parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
537 aconf->next = GlobalConfList;
538 GlobalConfList = aconf;
542 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
544 MyFree(aconf->passwd);
550 operitems: operitem | operitems operitem;
551 operitem: opername | operpass | operhost | operclass | priv | error;
553 opername: NAME '=' QSTRING ';'
556 DupString(aconf->name, $3);
559 operpass: PASS '=' QSTRING ';'
561 MyFree(aconf->passwd);
562 DupString(aconf->passwd, $3);
565 operhost: HOST '=' QSTRING ';'
568 if (!strchr($3, '@'))
571 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
572 ircd_snprintf(0, b, uh_len, "*@%s", $3);
576 DupString(aconf->host, $3);
579 operclass: CLASS '=' QSTRING ';'
581 aconf->conn_class = find_class($3);
584 priv: privtype '=' yesorno ';'
586 PrivSet(&privs_dirty, $1);
587 if (($3 == 1) ^ invert)
594 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
595 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
596 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
597 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
598 KILL { $$ = PRIV_KILL; } |
599 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
600 TPRIV_REHASH { $$ = PRIV_REHASH; } |
601 TPRIV_RESTART { $$ = PRIV_RESTART; } |
602 TPRIV_DIE { $$ = PRIV_DIE; } |
603 TPRIV_GLINE { $$ = PRIV_GLINE; } |
604 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
605 JUPE { $$ = PRIV_JUPE; } |
606 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
607 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
608 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
609 TPRIV_SET { $$ = PRIV_SET; } |
610 TPRIV_WHOX { $$ = PRIV_WHOX; } |
611 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
612 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
613 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
614 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
615 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
616 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
617 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
618 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
619 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
620 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
621 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
622 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
623 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
625 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
627 /* The port block... */
631 /* Hijack these for is_server, is_hidden to cut down on globals... */
634 /* and this for mask... */
636 } '{' portitems '}' ';'
638 if (port > 0 && port <= 0xFFFF)
640 add_listener(port, host, pass, tconn, tping);
644 parse_error("Bad port block");
650 portitems: portitem portitems | portitem;
651 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
652 portnumber: PORT '=' NUMBER ';'
657 portvhost: VHOST '=' QSTRING ';'
663 portmask: MASK '=' QSTRING ';'
669 portserver: SERVER '=' YES ';'
672 } | SERVER '=' NO ';'
677 porthidden: HIDDEN '=' YES ';'
680 } | HIDDEN '=' NO ';'
687 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
688 memset(aconf, 0, sizeof(*aconf));
689 aconf->status = CONF_CLIENT;
690 } '{' clientitems '}'
692 if ((aconf->host != NULL || aconf->name!=NULL))
694 if (aconf->host == NULL)
695 DupString(aconf->host, "");
696 if (aconf->name == NULL)
697 DupString(aconf->name, "");
698 if (aconf->conn_class == NULL)
699 aconf->conn_class = find_class("default");
700 aconf->next = GlobalConfList;
701 GlobalConfList = aconf;
707 MyFree(aconf->passwd);
710 parse_error("Bad client block");
713 clientitems: clientitem clientitems | clientitem;
714 clientitem: clienthost | clientclass | clientpass | clientip | error;
715 clientip: IP '=' QSTRING ';'
718 DupString(aconf->host, $3);
721 clienthost: HOST '=' QSTRING ';'
724 DupString(aconf->name, $3);
727 clientclass: CLASS '=' QSTRING ';'
729 aconf->conn_class = find_class($3);
732 clientpass: PASS '=' QSTRING ';'
734 MyFree(aconf->passwd);
735 DupString(aconf->passwd, $3);
740 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
741 memset(dconf, 0, sizeof(*dconf));
744 if (dconf->hostmask != NULL)
746 if (dconf->usermask == NULL)
747 DupString(dconf->usermask, "*");
748 dconf->next = denyConfList;
749 denyConfList = dconf;
754 MyFree(dconf->hostmask);
755 MyFree(dconf->message);
758 parse_error("Bad kill block");
761 killitems: killitem killitems | killitem;
762 killitem: killuhost | killreal | killreasonfile | killreason | error;
763 killuhost: HOST '=' QSTRING ';'
766 dconf->flags &= ~DENY_FLAGS_REALNAME;
767 MyFree(dconf->hostmask);
768 MyFree(dconf->usermask);
769 if ((h = strchr($3, '@')) == NULL)
779 DupString(dconf->hostmask, h);
780 DupString(dconf->usermask, u);
781 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
784 killreal: REAL '=' QSTRING ';'
786 dconf->flags &= ~DENY_FLAGS_IP;
787 dconf->flags |= DENY_FLAGS_REALNAME;
788 MyFree(dconf->hostmask);
789 /* Leave usermask so you can specify user and real... */
790 DupString(dconf->hostmask, $3);
793 killreason: REASON '=' QSTRING ';'
795 dconf->flags &= DENY_FLAGS_FILE;
796 MyFree(dconf->message);
797 DupString(dconf->message, $3);
800 killreasonfile: TFILE '=' QSTRING ';'
802 dconf->flags |= DENY_FLAGS_FILE;
803 MyFree(dconf->message);
804 DupString(dconf->message, $3);
813 struct CRuleNode *node;
814 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
816 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
821 p->next = cruleConfList;
828 parse_error("Bad CRule block");
832 cruleitems: cruleitem cruleitems | cruleitem;
833 cruleitem: cruleserver | crulerule | cruleall | error;
835 cruleserver: SERVER '=' QSTRING ';'
842 crulerule: RULE '=' QSTRING ';'
848 cruleall: ALL '=' YES ';'
860 if (host != NULL && pass != NULL)
861 motd_add(host, pass);
867 motditems: motditem motditems | motditem;
868 motditem: motdhost | motdfile | error;
869 motdhost: HOST '=' QSTRING ';'
874 motdfile: TFILE '=' QSTRING ';'
879 featuresblock: FEATURES '{' featureitems '}' ';';
880 featureitems: featureitems featureitem | featureitem;
886 } '=' stringlist ';';
894 feature_set(NULL, (const char * const *)stringlist, stringno);
896 posextrastrings: /* empty */ | extrastrings;
897 extrastrings: extrastrings extrastring | extrastring;
900 if (stringno < MAX_STRINGS)
901 stringlist[stringno++] = $1;
904 quarantineblock: QUARANTINE '{'
907 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
910 if (qconf->chname != NULL)
911 MyFree(qconf->chname);
912 if (qconf->reason != NULL)
913 MyFree(qconf->reason);
915 memset(qconf, 0, sizeof(*qconf));
916 } quarantineitems '}' ';'
918 if (qconf->chname == NULL || qconf->reason == NULL)
920 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
924 qconf->next = GlobalQuarantineList;
925 GlobalQuarantineList = qconf;
929 quarantineitems: CHANNEL NAME '=' QSTRING ';'
931 DupString(qconf->chname, $4);
932 } | REASON '=' QSTRING ';'
934 DupString(qconf->reason, $3);
937 pseudoblock: PSEUDO QSTRING '{'
939 smap = MyCalloc(1, sizeof(struct s_map));
940 DupString(smap->command, $2);
944 if (!smap->name || !smap->services)
946 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
949 if (register_mapping(smap))
951 smap->next = GlobalServiceMapList;
952 GlobalServiceMapList = smap;
956 struct nick_host *nh, *next;
957 for (nh = smap->services; nh; nh = next)
963 MyFree(smap->command);
964 MyFree(smap->prepend);
970 pseudoitems: pseudoitem pseudoitems | pseudoitem;
971 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
972 pseudoname: NAME '=' QSTRING ';'
974 DupString(smap->name, $3);
976 pseudoprepend: PREPEND '=' QSTRING ';'
978 DupString(smap->prepend, $3);
980 pseudonick: NICK '=' QSTRING ';'
982 char *sep = strchr($3, '@');
985 size_t slen = strlen($3);
986 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
987 memcpy(nh->nick, $3, slen + 1);
988 nh->nicklen = sep - $3;
989 nh->next = smap->services;
994 iauthblock: IAUTH '{'
1000 } iauthitems '}' ';'
1002 if (!host || !port) {
1003 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
1006 iauth_connect(host, port, pass, tconn, tping);
1012 iauthitems: iauthitem iauthitems | iauthitem;
1013 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
1014 iauthpass: PASS '=' QSTRING ';'
1017 DupString(pass, $3);
1019 iauthhost: HOST '=' QSTRING ';'
1022 DupString(host, $3);
1024 iauthport: PORT '=' NUMBER ';'
1028 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1032 iauthtimeout: TIMEOUT '=' timespec ';'