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 '}' ';' ;
269 generalitems: generalitem generalitems | generalitem;
270 generalitem: generalnumeric | generalname | generalvhost | generaldesc | error;
271 generalnumeric: NUMERIC '=' NUMBER ';'
273 if (localConf.numeric == 0)
274 localConf.numeric = $3;
275 else if (localConf.numeric != $3)
276 parse_error("Redefinition of server numeric %i (%i)", $3,
280 generalname: NAME '=' QSTRING ';'
282 if (localConf.name == NULL)
283 DupString(localConf.name, $3);
284 else if (strcmp(localConf.name, $3))
285 parse_error("Redefinition of server name %s (%s)", $3,
289 generaldesc: DESCRIPTION '=' QSTRING ';'
291 MyFree(localConf.description);
292 DupString(localConf.description, $3);
293 ircd_strncpy(cli_info(&me), $3, REALLEN);
296 generalvhost: VHOST '=' QSTRING ';'
298 ircd_aton(&localConf.vhost_address, $3);
301 adminblock: ADMIN '{' adminitems '}'
303 if (localConf.location1 == NULL)
304 DupString(localConf.location1, "");
305 if (localConf.location2 == NULL)
306 DupString(localConf.location2, "");
307 if (localConf.contact == NULL)
308 DupString(localConf.contact, "");
310 adminitems: adminitems adminitem | adminitem;
311 adminitem: adminlocation | admincontact | error;
312 adminlocation: LOCATION '=' QSTRING ';'
314 if (localConf.location1 == NULL)
315 DupString(localConf.location1, $3);
316 else if (localConf.location2 == NULL)
317 DupString(localConf.location2, $3);
318 /* Otherwise just drop it. -A1kmm */
320 admincontact: CONTACT '=' QSTRING ';'
322 if (localConf.contact != NULL)
323 MyFree(localConf.contact);
324 DupString(localConf.contact, $3);
334 memset(&privs, 0, sizeof(privs));
335 memset(&privs_dirty, 0, sizeof(privs_dirty));
340 struct ConnectionClass *c_class;
341 add_class(name, tping, tconn, maxlinks, sendq);
342 c_class = find_class(name);
343 c_class->default_umode = pass;
344 memcpy(&c_class->privs, &privs, sizeof(c_class->privs));
345 memcpy(&c_class->privs_dirty, &privs_dirty, sizeof(c_class->privs_dirty));
348 parse_error("Missing name in class block");
352 classitems: classitem classitems | classitem;
353 classitem: classname | classpingfreq | classconnfreq | classmaxlinks |
354 classsendq | classusermode | priv | error;
355 classname: NAME '=' QSTRING ';'
360 classpingfreq: PINGFREQ '=' timespec ';'
364 classconnfreq: CONNECTFREQ '=' timespec ';'
368 classmaxlinks: MAXLINKS '=' expr ';'
372 classsendq: SENDQ '=' sizespec ';'
376 classusermode: USERMODE '=' QSTRING ';'
383 connectblock: CONNECT
385 name = pass = host = origin = NULL;
388 } '{' connectitems '}'
390 if (name != NULL && pass != NULL && host != NULL && c_class != NULL
391 && !strchr(host, '*') && !strchr(host, '?'))
394 aconf->status = CONF_SERVER;
396 aconf->origin_name = origin;
397 aconf->passwd = pass;
398 aconf->conn_class = c_class;
399 aconf->address.port = port;
400 aconf->status = CONF_SERVER;
402 aconf->next = GlobalConfList;
403 lookup_confhost(aconf);
404 GlobalConfList = aconf;
412 parse_error("Bad connect block");
414 name = pass = host = origin = NULL;
416 connectitems: connectitem connectitems | connectitem;
417 connectitem: connectname | connectpass | connectclass | connecthost
418 | connectport | connectvhost | 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);
450 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
451 memset(aconf, 0, sizeof(*aconf));
452 aconf->status = CONF_LEAF;
453 } '{' serveritems '}'
455 if (aconf->status == 0)
461 parse_error("Bad server block");
465 aconf->next = GlobalConfList;
466 GlobalConfList = aconf;
469 serveritems: serveritem serveritems | serveritem;
470 serveritem: servername | servermask | serverhub | serverleaf |
471 serveruworld | error;
472 servername: NAME '=' QSTRING
475 DupString(aconf->name, $3);
477 servermask: MASK '=' QSTRING
480 DupString(aconf->host, $3);
482 /* XXX - perhaps we should do this the hybrid way in connect blocks
484 serverhub: HUB '=' YES ';'
486 aconf->status |= CONF_HUB;
487 aconf->status &= ~CONF_LEAF;
491 aconf->status &= ~CONF_HUB;
493 serverleaf: LEAF '=' YES ';'
495 if (!(aconf->status & CONF_HUB && aconf->status & CONF_UWORLD))
496 aconf->status |= CONF_LEAF;
498 parse_error("Server is both leaf and a hub");
502 aconf->status &= ~CONF_LEAF;
504 serveruworld: UWORLD '=' YES ';'
506 aconf->status |= CONF_UWORLD;
507 aconf->status &= ~CONF_LEAF;
511 aconf->status &= ~CONF_UWORLD;
516 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
517 memset(aconf, 0, sizeof(*aconf));
518 memset(&privs, 0, sizeof(privs));
519 memset(&privs_dirty, 0, sizeof(privs_dirty));
520 aconf->status = CONF_OPERATOR;
521 } '{' operitems '}' ';'
523 if (aconf->name != NULL && aconf->passwd != NULL && aconf->host != NULL
524 && aconf->conn_class != NULL)
526 memcpy(&aconf->privs, &privs, sizeof(aconf->privs));
527 memcpy(&aconf->privs_dirty, &privs_dirty, sizeof(aconf->privs_dirty));
528 if (!PrivHas(&privs_dirty, PRIV_PROPAGATE)
529 && !PrivHas(&aconf->conn_class->privs_dirty, PRIV_PROPAGATE))
530 parse_error("Operator block for %s and class %s have no LOCAL setting", aconf->name, aconf->conn_class->cc_name);
531 aconf->next = GlobalConfList;
532 GlobalConfList = aconf;
536 log_write(LS_CONFIG, L_ERROR, 0, "operator blocks need a name, password, and host.");
538 MyFree(aconf->passwd);
544 operitems: operitem | operitems operitem;
545 operitem: opername | operpass | operhost | operclass | priv | error;
547 opername: NAME '=' QSTRING ';'
550 DupString(aconf->name, $3);
553 operpass: PASS '=' QSTRING ';'
555 MyFree(aconf->passwd);
556 DupString(aconf->passwd, $3);
559 operhost: HOST '=' QSTRING ';'
562 if (!strchr($3, '@'))
565 char *b = (char*) MyMalloc((uh_len = strlen($3)+3));
566 ircd_snprintf(0, b, uh_len, "*@%s", $3);
570 DupString(aconf->host, $3);
573 operclass: CLASS '=' QSTRING ';'
575 aconf->conn_class = find_class($3);
578 priv: privtype '=' yesorno ';'
580 PrivSet(&privs_dirty, $1);
581 if (($3 == 1) ^ invert)
588 privtype: TPRIV_CHAN_LIMIT { $$ = PRIV_CHAN_LIMIT; } |
589 TPRIV_MODE_LCHAN { $$ = PRIV_MODE_LCHAN; } |
590 TPRIV_DEOP_LCHAN { $$ = PRIV_DEOP_LCHAN; } |
591 TPRIV_WALK_LCHAN { $$ = PRIV_WALK_LCHAN; } |
592 KILL { $$ = PRIV_KILL; } |
593 TPRIV_LOCAL_KILL { $$ = PRIV_LOCAL_KILL; } |
594 TPRIV_REHASH { $$ = PRIV_REHASH; } |
595 TPRIV_RESTART { $$ = PRIV_RESTART; } |
596 TPRIV_DIE { $$ = PRIV_DIE; } |
597 TPRIV_GLINE { $$ = PRIV_GLINE; } |
598 TPRIV_LOCAL_GLINE { $$ = PRIV_LOCAL_GLINE; } |
599 JUPE { $$ = PRIV_JUPE; } |
600 TPRIV_LOCAL_JUPE { $$ = PRIV_LOCAL_JUPE; } |
601 TPRIV_LOCAL_OPMODE { $$ = PRIV_LOCAL_OPMODE; } |
602 TPRIV_OPMODE { $$ = PRIV_OPMODE; }|
603 TPRIV_SET { $$ = PRIV_SET; } |
604 TPRIV_WHOX { $$ = PRIV_WHOX; } |
605 TPRIV_BADCHAN { $$ = PRIV_BADCHAN; } |
606 TPRIV_LOCAL_BADCHAN { $$ = TPRIV_LOCAL_BADCHAN; } |
607 TPRIV_SEE_CHAN { $$ = PRIV_SEE_CHAN; } |
608 TPRIV_SHOW_INVIS { $$ = PRIV_SHOW_INVIS; } |
609 TPRIV_SHOW_ALL_INVIS { $$ = PRIV_SHOW_ALL_INVIS; } |
610 TPRIV_PROPAGATE { $$ = PRIV_PROPAGATE; } |
611 TPRIV_UNLIMIT_QUERY { $$ = PRIV_UNLIMIT_QUERY; } |
612 TPRIV_DISPLAY { $$ = PRIV_DISPLAY; } |
613 TPRIV_SEE_OPERS { $$ = PRIV_SEE_OPERS; } |
614 TPRIV_WIDE_GLINE { $$ = PRIV_WIDE_GLINE; } |
615 LOCAL { $$ = PRIV_PROPAGATE; invert = 1; } |
616 TPRIV_FORCE_OPMODE { $$ = PRIV_FORCE_OPMODE; } |
617 TPRIV_FORCE_LOCAL_OPMODE { $$ = PRIV_FORCE_LOCAL_OPMODE; };
619 yesorno: YES { $$ = 1; } | NO { $$ = 0; };
621 /* The port block... */
625 /* Hijack these for is_server, is_hidden to cut down on globals... */
628 /* and this for mask... */
630 } '{' portitems '}' ';'
632 if (port > 0 && port <= 0xFFFF)
634 add_listener(port, host, pass, tconn, tping);
638 parse_error("Bad port block");
644 portitems: portitem portitems | portitem;
645 portitem: portnumber | portvhost | portmask | portserver | porthidden | error;
646 portnumber: PORT '=' NUMBER ';'
651 portvhost: VHOST '=' QSTRING ';'
657 portmask: MASK '=' QSTRING ';'
663 portserver: SERVER '=' YES ';'
666 } | SERVER '=' NO ';'
671 porthidden: HIDDEN '=' YES ';'
674 } | HIDDEN '=' NO ';'
681 aconf = (struct ConfItem*) MyMalloc(sizeof(*aconf));
682 memset(aconf, 0, sizeof(*aconf));
683 aconf->status = CONF_CLIENT;
684 } '{' clientitems '}'
686 if ((aconf->host != NULL || aconf->name!=NULL))
688 if (aconf->host == NULL)
689 DupString(aconf->host, "");
690 if (aconf->name == NULL)
691 DupString(aconf->name, "");
692 if (aconf->conn_class == NULL)
693 aconf->conn_class = find_class("default");
694 aconf->next = GlobalConfList;
695 GlobalConfList = aconf;
701 MyFree(aconf->passwd);
704 parse_error("Bad client block");
707 clientitems: clientitem clientitems | clientitem;
708 clientitem: clienthost | clientclass | clientpass | clientip | error;
709 clientip: IP '=' QSTRING ';'
712 DupString(aconf->host, $3);
715 clienthost: HOST '=' QSTRING ';'
718 DupString(aconf->name, $3);
721 clientclass: CLASS '=' QSTRING ';'
723 aconf->conn_class = find_class($3);
726 clientpass: PASS '=' QSTRING ';'
728 MyFree(aconf->passwd);
729 DupString(aconf->passwd, $3);
734 dconf = (struct DenyConf*) MyMalloc(sizeof(*dconf));
735 memset(dconf, 0, sizeof(*dconf));
738 if (dconf->hostmask != NULL)
740 if (dconf->usermask == NULL)
741 DupString(dconf->usermask, "*");
742 dconf->next = denyConfList;
743 denyConfList = dconf;
748 MyFree(dconf->hostmask);
749 MyFree(dconf->message);
752 parse_error("Bad kill block");
755 killitems: killitem killitems | killitem;
756 killitem: killuhost | killreal | killreasonfile | killreason | error;
757 killuhost: HOST '=' QSTRING ';'
760 dconf->flags &= ~DENY_FLAGS_REALNAME;
761 MyFree(dconf->hostmask);
762 MyFree(dconf->usermask);
763 if ((h = strchr($3, '@')) == NULL)
773 DupString(dconf->hostmask, h);
774 DupString(dconf->usermask, u);
775 ipmask_parse(dconf->hostmask, &dconf->address, &dconf->bits);
778 killreal: REAL '=' QSTRING ';'
780 dconf->flags &= ~DENY_FLAGS_IP;
781 dconf->flags |= DENY_FLAGS_REALNAME;
782 MyFree(dconf->hostmask);
783 /* Leave usermask so you can specify user and real... */
784 DupString(dconf->hostmask, $3);
787 killreason: REASON '=' QSTRING ';'
789 dconf->flags &= DENY_FLAGS_FILE;
790 MyFree(dconf->message);
791 DupString(dconf->message, $3);
794 killreasonfile: TFILE '=' QSTRING ';'
796 dconf->flags |= DENY_FLAGS_FILE;
797 MyFree(dconf->message);
798 DupString(dconf->message, $3);
807 struct CRuleNode *node;
808 if (host != NULL && pass != NULL && (node=crule_parse(pass)) != NULL)
810 struct CRuleConf *p = (struct CRuleConf*) MyMalloc(sizeof(*p));
815 p->next = cruleConfList;
822 parse_error("Bad CRule block");
826 cruleitems: cruleitem cruleitems | cruleitem;
827 cruleitem: cruleserver | crulerule | cruleall | error;
829 cruleserver: SERVER '=' QSTRING ';'
836 crulerule: RULE '=' QSTRING ';'
842 cruleall: ALL '=' YES ';'
854 if (host != NULL && pass != NULL)
855 motd_add(host, pass);
861 motditems: motditem motditems | motditem;
862 motditem: motdhost | motdfile | error;
863 motdhost: HOST '=' QSTRING ';'
868 motdfile: TFILE '=' QSTRING ';'
873 featuresblock: FEATURES '{' featureitems '}' ';';
874 featureitems: featureitems featureitem | featureitem;
880 } '=' stringlist ';';
888 feature_set(NULL, (const char * const *)stringlist, stringno);
890 posextrastrings: /* empty */ | extrastrings;
891 extrastrings: extrastrings extrastring | extrastring;
894 if (stringno < MAX_STRINGS)
895 stringlist[stringno++] = $1;
898 quarantineblock: QUARANTINE '{'
901 qconf = (struct qline*) MyMalloc(sizeof(*qconf));
904 if (qconf->chname != NULL)
905 MyFree(qconf->chname);
906 if (qconf->reason != NULL)
907 MyFree(qconf->reason);
909 memset(qconf, 0, sizeof(*qconf));
910 } quarantineitems '}' ';'
912 if (qconf->chname == NULL || qconf->reason == NULL)
914 log_write(LS_CONFIG, L_ERROR, 0, "quarantine blocks need a channel name "
918 qconf->next = GlobalQuarantineList;
919 GlobalQuarantineList = qconf;
923 quarantineitems: CHANNEL NAME '=' QSTRING ';'
925 DupString(qconf->chname, $4);
926 } | REASON '=' QSTRING ';'
928 DupString(qconf->reason, $3);
931 pseudoblock: PSEUDO QSTRING '{'
933 smap = MyCalloc(1, sizeof(struct s_map));
934 DupString(smap->command, $2);
938 if (!smap->name || !smap->services)
940 log_write(LS_CONFIG, L_ERROR, 0, "pseudo commands need a service name and list of target nicks.");
943 if (register_mapping(smap))
945 smap->next = GlobalServiceMapList;
946 GlobalServiceMapList = smap;
950 struct nick_host *nh, *next;
951 for (nh = smap->services; nh; nh = next)
957 MyFree(smap->command);
958 MyFree(smap->prepend);
964 pseudoitems: pseudoitem pseudoitems | pseudoitem;
965 pseudoitem: pseudoname | pseudoprepend | pseudonick | error;
966 pseudoname: NAME '=' QSTRING ';'
968 DupString(smap->name, $3);
970 pseudoprepend: PREPEND '=' QSTRING ';'
972 DupString(smap->prepend, $3);
974 pseudonick: NICK '=' QSTRING ';'
976 char *sep = strchr($3, '@');
979 size_t slen = strlen($3);
980 struct nick_host *nh = MyMalloc(sizeof(*nh) + slen);
981 memcpy(nh->nick, $3, slen + 1);
982 nh->nicklen = sep - $3;
983 nh->next = smap->services;
988 iauthblock: IAUTH '{'
996 if (!name || !host || !port) {
997 log_write(LS_CONFIG, L_ERROR, 0, "IAuth block needs a server name and port.");
1000 iauth_connect(host, port, pass, tconn, tping);
1006 iauthitems: iauthitem iauthitems | iauthitem;
1007 iauthitem: iauthpass | iauthhost | iauthport | iauthconnfreq | iauthtimeout | error;
1008 iauthpass: PASS '=' QSTRING ';'
1011 DupString(pass, $3);
1013 iauthhost: HOST '=' QSTRING ';'
1016 DupString(host, $3);
1018 iauthport: PORT '=' NUMBER ';'
1022 iauthconnfreq: CONNECTFREQ '=' timespec ';'
1026 iauthtimeout: TIMEOUT '=' timespec ';'