"Fossies" - the Fresh Open Source Software Archive

Member "mod_auth_gssapi-1.6.3/src/parser.y" (25 Jul 2017, 5535 Bytes) of package /linux/www/apache_httpd_modules/mod_auth_gssapi-1.6.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bison source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2017 mod_auth_gssapi contributors - See COPYING for (C) terms
    2  *
    3  * Bison file for the GssapiRequiredNameAttributes option parser.
    4  *
    5  * Rule := (RequiredKV | "(" Rule ")"),  { ' ', (AND|OR), ' ', Rule } ;
    6  * RequiredKV := (Key, "=", Value) | (Key, ":=" BinValue) ;
    7  * Key := <string> ;
    8  * Value := <string> | '*' ;
    9  * BinValue := <base64> ;
   10  * AND := "and" | "AND" ;
   11  * OR := "or" | "OR" ;
   12  *
   13  */
   14 %{
   15 #include <stdio.h>
   16 #include <stdlib.h>
   17 #include <string.h>
   18 #include <apr_base64.h>
   19 int yylex(void);
   20 typedef struct yy_buffer_state * YY_BUFFER_STATE;
   21 extern void yyerror(const char **keys, const char **vals,
   22                     int *status, const char *s);
   23 extern int yyparse(const char **keys, const char **vals, int *status);
   24 extern YY_BUFFER_STATE yy_scan_string(char * str);
   25 extern void yy_delete_buffer(YY_BUFFER_STATE buffer);
   26 static size_t b64_len(const char *val);
   27 static char *b64_enc(const char *val, size_t len);
   28 %}
   29 
   30 %union {
   31     char *sval;
   32     int ival;
   33 }
   34 
   35 %token LPAREN
   36 %token RPAREN
   37 %token SPACE
   38 %token OR
   39 %token AND
   40 %token EQUAL
   41 %token EQUALBIN
   42 %token AST
   43 %token STRING
   44 %token INT
   45 
   46 %type <sval> STRING
   47 %type <ival> INT rule rule_start requiredkv
   48 
   49 %parse-param {const char **keys} {const char **vals} {int *status}
   50 
   51 %%
   52 
   53 expr: rule {
   54       if (status != NULL)
   55           *status = $1;
   56     }
   57     ;
   58 
   59 rule: rule_start
   60     | rule_start SPACE AND SPACE rule {
   61       $$ = $1 && $5;
   62     }
   63     | rule_start SPACE OR SPACE rule {
   64       $$ = $1 || $5;
   65     }
   66     ;
   67 
   68 rule_start: LPAREN rule RPAREN {
   69             $$ = $2;
   70           }
   71           | requiredkv {
   72             $$ = $1;
   73           }
   74           ;
   75 
   76 requiredkv: STRING EQUAL STRING {
   77             int ret = 0;
   78             size_t sz;
   79             if (keys != NULL && vals != NULL) {
   80                 for (int i = 0; keys[i] != NULL && vals[i] != NULL; i++) {
   81                     if (strcmp($1, keys[i]) != 0) {
   82                         continue;
   83                     }
   84                     sz = 0;
   85                     memcpy(&sz, vals[i], sizeof(sz));
   86                     if (sz == 0) {
   87                         continue;
   88                     }
   89                     if (!memcmp($3, vals[i] + sizeof(sz), sz)) {
   90                         ret = 1;
   91                         break;
   92                     }
   93                 }
   94             }
   95             $$ = ret;
   96           }
   97           | STRING EQUAL AST {
   98             int ret = 0;
   99             if (keys != NULL && vals != NULL) {
  100                 for (int i = 0; keys[i] != NULL && vals[i] != NULL; i++) {
  101                     if (strcmp($1, keys[i]) == 0) {
  102                        ret = 1;
  103                        break;
  104                     }
  105                 }
  106             }
  107             $$ = ret;
  108           }
  109           | STRING EQUALBIN STRING {
  110             int ret = 0;
  111             if (keys != NULL && vals != NULL) {
  112                 for (int i = 0; keys[i] != NULL && vals[i] != NULL; i++) {
  113                     if (strcmp($1, keys[i]) != 0) {
  114                         continue;
  115                     }
  116                     size_t b64len = b64_len(vals[i]);
  117                     /* b64len includes the NULL terminator. */
  118                     if (strlen($3) + 1 != b64len) {
  119                         continue;
  120                     }
  121                     char *b64val = b64_enc(vals[i], b64len);
  122                     if (!b64val) {
  123                         continue;
  124                     }
  125                     if (strcmp($3, b64val) == 0) {
  126                         ret = 1;
  127                     }
  128                     free(b64val);
  129                     if (ret) {
  130                         break;
  131                     }
  132                 }
  133             }
  134             $$ = ret;
  135           }
  136           ;
  137 
  138 %%
  139 
  140 static size_t b64_len(const char *val)
  141 {
  142     size_t sz = 0;
  143 
  144     memcpy(&sz, val, sizeof(sz));
  145     if (sz == 0)
  146         return sz;
  147 
  148     return apr_base64_encode_len(sz);
  149 }
  150 
  151 static char *b64_enc(const char *val, size_t len)
  152 {
  153     size_t sz = 0;
  154     char *b64val;
  155 
  156     memcpy(&sz, val, sizeof(sz));
  157     if (sz == 0)
  158         return NULL;
  159 
  160     b64val = calloc(1, len + 1);
  161     if (!b64val)
  162         return NULL;
  163 
  164     apr_base64_encode(b64val, val + sizeof(sz), sz);
  165     return b64val;
  166 }
  167 
  168 /* Return 1 if the given name attributes and values (NULL terminated arrays)
  169  * satisfy the expression.  This does not handle parsing errors from yyparse,
  170  * so expr should be checked by required_name_attr_expr_check() first. */
  171 int mag_verify_name_attributes(const char *expr, const char **attrs,
  172                                const char **vals)
  173 {
  174     int ret = 0, status = 0;
  175     YY_BUFFER_STATE buffer;
  176 
  177     /* No name attribute requirements. Pass. */
  178     if (expr == NULL) {
  179         return 1;
  180     }
  181 
  182     /* No name attributes but required attributes are specified. Fail. */
  183     if (attrs == NULL || vals == NULL ||
  184         attrs[0] == NULL || vals[0] == NULL) {
  185         return 0;
  186     }
  187 
  188     buffer = yy_scan_string((char *)expr);
  189     ret = yyparse(attrs, vals, &status);
  190     yy_delete_buffer(buffer);
  191 
  192     return ret == 0 && status;
  193 }
  194 
  195 /* Return 1 if the expression is provided and valid, else return 0. */
  196 int mag_check_name_attr_expr(const char *expr)
  197 {
  198     int ret;
  199     YY_BUFFER_STATE buffer = yy_scan_string((char *)expr);
  200 
  201     /* Just verify the syntax. */
  202     ret = yyparse(NULL, NULL, NULL);
  203     yy_delete_buffer(buffer);
  204 
  205     return ret == 0;
  206 }
  207 
  208 /* Define a no-op yyerror().  Syntax errors are logged outside of calling
  209  * required_name_attr_expr_check(). */
  210 void yyerror(const char **keys, const char **vals, int *status, const char *s)
  211 {
  212     return;
  213 }