3 * connection rule patch
4 * by Tony Vencill (Tonto on IRC) <vencill@bga.com>
6 * The majority of this file is a recusive descent parser used to convert
7 * connection rules into expression trees when the conf file is read.
8 * All parsing structures and types are hidden in the interest of good
9 * programming style and to make possible future data structure changes
10 * without affecting the interface between this patch and the rest of the
11 * server. The only functions accessible externally are crule_parse,
12 * crule_free, and crule_eval. Prototypes for these functions can be
15 * Please direct any connection rule or SmartRoute questions to Tonto on
16 * IRC or by email to vencill@bga.com.
18 * For parser testing, defining CR_DEBUG generates a stand-alone parser
19 * that takes rules from stdin and prints out memory allocation
20 * information and the parsed rule. This stand alone parser is ignorant
21 * of the irc server and thus cannot do rule evaluation. Do not define
22 * this flag when compiling the server! If you wish to generate the
23 * test parser, compile from the ircd directory with a line similar to
24 * cc -o parser -DCR_DEBUG crule.c
26 * The define CR_CHKCONF is provided to generate routines needed in
27 * chkconf. These consist of the parser, a different crule_parse that
28 * prints errors to stderr, and crule_free (just for good style and to
29 * more closely simulate the actual ircd environment). crule_eval and
30 * the rule functions are made empty functions as in the stand-alone
38 /* ircd functions and types we need */
41 #include "ircd_alloc.h"
42 #include "ircd_chattr.h"
43 #include "ircd_string.h"
52 #else /* includes and defines to make the stand-alone test parser */
57 #define BadPtr(x) (!(x) || (*(x) == '\0'))
58 #define DupString(x,y) \
60 x = (char*) MyMalloc(strlen(y)+1); \
64 /* We don't care about collation discrepacies here, it seems.... */
65 #define ircd_strcmp strcasecmp
72 #if defined(CR_DEBUG) || defined(CR_CHKCONF)
75 #define MyMalloc malloc
81 /* some constants and shared data types */
82 #define CR_MAXARGLEN 80 /* why 80? why not? it's > hostname lengths */
83 #define CR_MAXARGS 3 /* There's a better way to do this,
87 * Some symbols for easy reading
91 CR_UNKNOWN, CR_END, CR_AND, CR_OR, CR_NOT, CR_OPENPAREN, CR_CLOSEPAREN,
96 CR_NOERR, CR_UNEXPCTTOK, CR_UNKNWTOK, CR_EXPCTAND, CR_EXPCTOR,
97 CR_EXPCTPRIM, CR_EXPCTOPEN, CR_EXPCTCLOSE, CR_UNKNWFUNC, CR_ARGMISMAT
101 * Expression tree structure, function pointer, and tree pointer local!
103 typedef int (*crule_funcptr) (int, void **);
106 crule_funcptr funcptr;
108 void *arg[CR_MAXARGS]; /* For operators arg points to a tree element;
109 for functions arg points to a char string. */
112 typedef struct CRuleNode* CRuleNodePtr;
114 /* local rule function prototypes */
115 static int crule_connected(int, void **);
116 static int crule_directcon(int, void **);
117 static int crule_via(int, void **);
118 static int crule_directop(int, void **);
119 static int crule__andor(int, void **);
120 static int crule__not(int, void **);
122 /* local parsing function prototypes */
123 static int crule_gettoken(int* token, const char** str);
124 static void crule_getword(char*, int*, size_t, const char**);
125 static int crule_parseandexpr(CRuleNodePtr*, int *, const char**);
126 static int crule_parseorexpr(CRuleNodePtr*, int *, const char**);
127 static int crule_parseprimary(CRuleNodePtr*, int *, const char**);
128 static int crule_parsefunction(CRuleNodePtr*, int *, const char**);
129 static int crule_parsearglist(CRuleNodePtr, int *, const char**);
131 #if defined(CR_DEBUG) || defined(CR_CHKCONF)
133 * Prototypes for the test parser; if not debugging,
134 * these are defined in h.h
136 struct CRuleNode* crule_parse(const char*);
137 void crule_free(struct CRuleNode**);
139 void print_tree(CRuleNodePtr);
144 char *crule_errstr[] = {
145 "Unknown error", /* NOERR? - for completeness */
146 "Unexpected token", /* UNEXPCTTOK */
147 "Unknown token", /* UNKNWTOK */
148 "And expr expected", /* EXPCTAND */
149 "Or expr expected", /* EXPCTOR */
150 "Primary expected", /* EXPCTPRIM */
151 "( expected", /* EXPCTOPEN */
152 ") expected", /* EXPCTCLOSE */
153 "Unknown function", /* UNKNWFUNC */
154 "Argument mismatch" /* ARGMISMAT */
157 /* function table - null terminated */
158 struct crule_funclistent {
159 char name[15]; /* MAXIMUM FUNCTION NAME LENGTH IS 14 CHARS!! */
161 crule_funcptr funcptr;
164 struct crule_funclistent crule_funclist[] = {
165 /* maximum function name length is 14 chars */
166 {"connected", 1, crule_connected},
167 {"directcon", 1, crule_directcon},
168 {"via", 2, crule_via},
169 {"directop", 0, crule_directop},
170 {"", 0, NULL} /* this must be here to mark end of list */
173 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
174 static int crule_connected(int numargs, void *crulearg[])
176 struct Client *acptr;
178 /* taken from m_links */
179 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
181 if (!IsServer(acptr) && !IsMe(acptr))
183 if (match((char *)crulearg[0], cli_name(acptr)))
190 static int crule_connected(int numargs, void **crulearg)
196 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
197 static int crule_directcon(int numargs, void *crulearg[])
200 struct Client *acptr;
202 /* adapted from m_trace and exit_one_client */
203 for (i = 0; i <= HighestFd; i++)
205 if (!(acptr = LocalClientArray[i]) || !IsServer(acptr))
207 if (match((char *)crulearg[0], cli_name(acptr)))
214 static int crule_directcon(int numargs, void **crulearg)
220 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
221 static int crule_via(int numargs, void *crulearg[])
223 struct Client *acptr;
225 /* adapted from m_links */
226 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
228 if (!IsServer(acptr) && !IsMe(acptr))
230 if (match((char *)crulearg[1], cli_name(acptr)))
232 if (match((char *)crulearg[0],
233 cli_name(LocalClientArray[cli_fd(cli_from(acptr))])))
240 static int crule_via(int numargs, void **crulearg)
246 static int crule_directop(int numargs, void **crulearg)
248 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
250 struct Client *acptr;
252 /* adapted from m_trace */
253 for (i = 0; i <= HighestFd; i++)
255 if (!(acptr = LocalClientArray[i]) || !IsAnOper(acptr))
263 static int crule__andor(int numargs, void *crulearg[])
267 result1 = ((CRuleNodePtr) crulearg[0])->funcptr
268 (((CRuleNodePtr) crulearg[0])->numargs,
269 ((CRuleNodePtr) crulearg[0])->arg);
270 if (crulearg[2]) /* or */
272 ((CRuleNodePtr) crulearg[1])->funcptr
273 (((CRuleNodePtr) crulearg[1])->numargs,
274 ((CRuleNodePtr) crulearg[1])->arg));
277 ((CRuleNodePtr) crulearg[1])->funcptr
278 (((CRuleNodePtr) crulearg[1])->numargs,
279 ((CRuleNodePtr) crulearg[1])->arg));
282 static int crule__not(int numargs, void *crulearg[])
284 return (!((CRuleNodePtr) crulearg[0])->funcptr
285 (((CRuleNodePtr) crulearg[0])->numargs,
286 ((CRuleNodePtr) crulearg[0])->arg));
289 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
290 int crule_eval(struct CRuleNode* rule)
292 return (rule->funcptr(rule->numargs, rule->arg));
296 static int crule_gettoken(int* next_tokp, const char** ruleptr)
300 *next_tokp = CR_UNKNOWN;
301 while (*next_tokp == CR_UNKNOWN)
302 switch (*(*ruleptr)++)
310 else if (pending == '&')
313 return (CR_UNKNWTOK);
318 else if (pending == '|')
321 return (CR_UNKNWTOK);
327 *next_tokp = CR_OPENPAREN;
330 *next_tokp = CR_CLOSEPAREN;
333 *next_tokp = CR_COMMA;
343 if ((IsAlpha(*(--(*ruleptr)))) || (**ruleptr == '*') ||
344 (**ruleptr == '?') || (**ruleptr == '.') || (**ruleptr == '-'))
345 *next_tokp = CR_WORD;
347 return (CR_UNKNWTOK);
353 static void crule_getword(char* word, int* wordlenp, size_t maxlen, const char** ruleptr)
358 while ((size_t)(word_ptr - word) < maxlen
359 && (IsAlnum(**ruleptr)
360 || **ruleptr == '*' || **ruleptr == '?'
361 || **ruleptr == '.' || **ruleptr == '-'))
362 *word_ptr++ = *(*ruleptr)++;
364 *wordlenp = word_ptr - word;
370 * orexpr END END is end of input or :
382 * word ( ) word is alphanumeric string, first character
383 * word ( arglist ) must be a letter
388 struct CRuleNode* crule_parse(const char *rule)
390 const char* ruleptr = rule;
392 struct CRuleNode* ruleroot = 0;
393 int errcode = CR_NOERR;
395 if ((errcode = crule_gettoken(&next_tok, &ruleptr)) == CR_NOERR) {
396 if ((errcode = crule_parseorexpr(&ruleroot, &next_tok, &ruleptr)) == CR_NOERR) {
397 if (ruleroot != NULL) {
398 if (next_tok == CR_END)
401 errcode = CR_UNEXPCTTOK;
404 errcode = CR_EXPCTOR;
407 if (ruleroot != NULL)
408 crule_free(&ruleroot);
409 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
410 Debug((DEBUG_ERROR, "%s in rule: %s", crule_errstr[errcode], rule));
412 fprintf(stderr, "%s in rule: %s\n", crule_errstr[errcode], rule);
417 static int crule_parseorexpr(CRuleNodePtr * orrootp, int *next_tokp, const char** ruleptr)
419 int errcode = CR_NOERR;
420 CRuleNodePtr andexpr;
424 while (errcode == CR_NOERR)
426 errcode = crule_parseandexpr(&andexpr, next_tokp, ruleptr);
427 if ((errcode == CR_NOERR) && (*next_tokp == CR_OR))
429 orptr = (CRuleNodePtr) MyMalloc(sizeof(struct CRuleNode));
431 fprintf(stderr, "allocating or element at %ld\n", orptr);
433 orptr->funcptr = crule__andor;
435 orptr->arg[2] = (void *)1;
436 if (*orrootp != NULL)
438 (*orrootp)->arg[1] = andexpr;
439 orptr->arg[0] = *orrootp;
442 orptr->arg[0] = andexpr;
447 if (*orrootp != NULL)
451 (*orrootp)->arg[1] = andexpr;
456 (*orrootp)->arg[1] = NULL; /* so free doesn't seg fault */
457 return (CR_EXPCTAND);
466 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
472 static int crule_parseandexpr(CRuleNodePtr * androotp, int *next_tokp, const char** ruleptr)
474 int errcode = CR_NOERR;
475 CRuleNodePtr primary;
479 while (errcode == CR_NOERR)
481 errcode = crule_parseprimary(&primary, next_tokp, ruleptr);
482 if ((errcode == CR_NOERR) && (*next_tokp == CR_AND))
484 andptr = (CRuleNodePtr) MyMalloc(sizeof(struct CRuleNode));
486 fprintf(stderr, "allocating and element at %ld\n", andptr);
488 andptr->funcptr = crule__andor;
490 andptr->arg[2] = (void *)0;
491 if (*androotp != NULL)
493 (*androotp)->arg[1] = primary;
494 andptr->arg[0] = *androotp;
497 andptr->arg[0] = primary;
502 if (*androotp != NULL)
506 (*androotp)->arg[1] = primary;
511 (*androotp)->arg[1] = NULL; /* so free doesn't seg fault */
512 return (CR_EXPCTPRIM);
521 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
527 static int crule_parseprimary(CRuleNodePtr* primrootp, int *next_tokp, const char** ruleptr)
529 CRuleNodePtr *insertionp;
530 int errcode = CR_NOERR;
533 insertionp = primrootp;
534 while (errcode == CR_NOERR)
539 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
541 if ((errcode = crule_parseorexpr(insertionp, next_tokp, ruleptr)) != CR_NOERR)
543 if (*insertionp == NULL)
545 errcode = CR_EXPCTAND;
548 if (*next_tokp != CR_CLOSEPAREN)
550 errcode = CR_EXPCTCLOSE;
553 errcode = crule_gettoken(next_tokp, ruleptr);
556 *insertionp = (CRuleNodePtr) MyMalloc(sizeof(struct CRuleNode));
558 fprintf(stderr, "allocating primary element at %ld\n", *insertionp);
560 (*insertionp)->funcptr = crule__not;
561 (*insertionp)->numargs = 1;
562 (*insertionp)->arg[0] = NULL;
563 insertionp = (CRuleNodePtr *) & ((*insertionp)->arg[0]);
564 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
568 errcode = crule_parsefunction(insertionp, next_tokp, ruleptr);
571 if (*primrootp == NULL)
574 errcode = CR_EXPCTPRIM;
582 static int crule_parsefunction(CRuleNodePtr* funcrootp, int* next_tokp, const char** ruleptr)
584 int errcode = CR_NOERR;
585 char funcname[CR_MAXARGLEN];
590 crule_getword(funcname, &namelen, CR_MAXARGLEN - 1, ruleptr);
591 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
593 if (*next_tokp == CR_OPENPAREN)
595 for (funcnum = 0;; funcnum++)
597 if (0 == ircd_strcmp(crule_funclist[funcnum].name, funcname))
599 if (crule_funclist[funcnum].name[0] == '\0')
600 return (CR_UNKNWFUNC);
602 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
604 *funcrootp = (CRuleNodePtr) MyMalloc(sizeof(struct CRuleNode));
606 fprintf(stderr, "allocating function element at %ld\n", *funcrootp);
608 (*funcrootp)->funcptr = NULL; /* for freeing aborted trees */
610 crule_parsearglist(*funcrootp, next_tokp, ruleptr)) != CR_NOERR)
612 if (*next_tokp != CR_CLOSEPAREN)
613 return (CR_EXPCTCLOSE);
614 if ((crule_funclist[funcnum].reqnumargs != (*funcrootp)->numargs) &&
615 (crule_funclist[funcnum].reqnumargs != -1))
616 return (CR_ARGMISMAT);
617 if ((errcode = crule_gettoken(next_tokp, ruleptr)) != CR_NOERR)
619 (*funcrootp)->funcptr = crule_funclist[funcnum].funcptr;
623 return (CR_EXPCTOPEN);
626 static int crule_parsearglist(CRuleNodePtr argrootp, int *next_tokp, const char** ruleptr)
628 int errcode = CR_NOERR;
629 char *argelemp = NULL;
630 char currarg[CR_MAXARGLEN];
632 char word[CR_MAXARGLEN];
635 argrootp->numargs = 0;
637 while (errcode == CR_NOERR)
642 crule_getword(word, &wordlen, CR_MAXARGLEN - 1, ruleptr);
643 if (currarg[0] != '\0')
645 if ((arglen + wordlen) < (CR_MAXARGLEN - 1))
647 strcat(currarg, " ");
648 strcat(currarg, word);
649 arglen += wordlen + 1;
654 strcpy(currarg, word);
657 errcode = crule_gettoken(next_tokp, ruleptr);
660 #if !defined(CR_DEBUG) && !defined(CR_CHKCONF)
663 if (!BadPtr(currarg))
665 DupString(argelemp, currarg);
666 argrootp->arg[argrootp->numargs++] = (void *)argelemp;
668 if (*next_tokp != CR_COMMA)
671 errcode = crule_gettoken(next_tokp, ruleptr);
679 * This function is recursive.. I wish I knew a nonrecursive way but
680 * I dont. anyway, recursion is fun.. :)
681 * DO NOT CALL THIS FUNTION WITH A POINTER TO A NULL POINTER
682 * (ie: If *elem is NULL, you're doing it wrong - seg fault)
684 void crule_free(struct CRuleNode** elem)
688 if ((*(elem))->funcptr == crule__not)
690 /* type conversions and ()'s are fun! ;) here have an asprin.. */
691 if ((*(elem))->arg[0] != NULL)
692 crule_free((struct CRuleNode**) &((*(elem))->arg[0]));
694 else if ((*(elem))->funcptr == crule__andor)
696 crule_free((struct CRuleNode**) &((*(elem))->arg[0]));
697 if ((*(elem))->arg[1] != NULL)
698 crule_free((struct CRuleNode**) &((*(elem))->arg[1]));
702 numargs = (*(elem))->numargs;
703 for (arg = 0; arg < numargs; arg++)
704 MyFree((*(elem))->arg[arg]);
707 fprintf(stderr, "freeing element at %ld\n", *elem);
714 static void print_tree(CRuleNodePtr printelem)
718 if (printelem->funcptr == crule__not)
721 print_tree((CRuleNodePtr) printelem->arg[0]);
724 else if (printelem->funcptr == crule__andor)
727 print_tree((CRuleNodePtr) printelem->arg[0]);
728 if (printelem->arg[2])
732 print_tree((CRuleNodePtr) printelem->arg[1]);
737 for (funcnum = 0;; funcnum++)
739 if (printelem->funcptr == crule_funclist[funcnum].funcptr)
741 if (crule_funclist[funcnum].funcptr == NULL)
744 printf("%s(", crule_funclist[funcnum].name);
745 for (arg = 0; arg < printelem->numargs; arg++)
749 printf("%s", (char *)printelem->arg[arg]);
764 while (fgets(indata, 256, stdin) != NULL)
766 indata[strlen(indata) - 1] = '\0'; /* lose the newline */
767 if ((rule = crule_parse(indata)) != NULL)
769 printf("equivalent rule: ");
770 print_tree((CRuleNodePtr) rule);