| 1 | /* |
| 2 | * filter-visitor-ir-validate-globbing.c |
| 3 | * |
| 4 | * LTTng filter IR validate globbing |
| 5 | * |
| 6 | * Copyright 2017 - Philippe Proulx <pproulx@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 10 | * as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this library; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
| 27 | #include <errno.h> |
| 28 | #include <inttypes.h> |
| 29 | |
| 30 | #include <common/macros.h> |
| 31 | |
| 32 | #include "filter-ast.h" |
| 33 | #include "filter-parser.h" |
| 34 | #include "filter-ir.h" |
| 35 | |
| 36 | static |
| 37 | int validate_globbing(struct ir_op *node) |
| 38 | { |
| 39 | int ret; |
| 40 | |
| 41 | switch (node->op) { |
| 42 | case IR_OP_UNKNOWN: |
| 43 | default: |
| 44 | fprintf(stderr, "[error] %s: unknown op type\n", __func__); |
| 45 | return -EINVAL; |
| 46 | |
| 47 | case IR_OP_ROOT: |
| 48 | return validate_globbing(node->u.root.child); |
| 49 | case IR_OP_LOAD: |
| 50 | return 0; |
| 51 | case IR_OP_UNARY: |
| 52 | return validate_globbing(node->u.unary.child); |
| 53 | case IR_OP_BINARY: |
| 54 | { |
| 55 | struct ir_op *left = node->u.binary.left; |
| 56 | struct ir_op *right = node->u.binary.right; |
| 57 | |
| 58 | if (left->op == IR_OP_LOAD && right->op == IR_OP_LOAD && |
| 59 | left->data_type == IR_DATA_STRING && |
| 60 | right->data_type == IR_DATA_STRING) { |
| 61 | /* Test 1. */ |
| 62 | if (left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && |
| 63 | right->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { |
| 64 | fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | if (right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR && |
| 69 | left->u.load.u.string.type != IR_LOAD_STRING_TYPE_PLAIN) { |
| 70 | fprintf(stderr, "[error] Cannot compare two globbing patterns\n"); |
| 71 | return -1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if ((left->op == IR_OP_LOAD && left->data_type == IR_DATA_STRING) || |
| 76 | (right->op == IR_OP_LOAD && right->data_type == IR_DATA_STRING)) { |
| 77 | if ((left->op == IR_OP_LOAD && left->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR) || |
| 78 | (right->op == IR_OP_LOAD && right->u.load.u.string.type == IR_LOAD_STRING_TYPE_GLOB_STAR)) { |
| 79 | /* Test 2. */ |
| 80 | if (node->u.binary.type != AST_OP_EQ && |
| 81 | node->u.binary.type != AST_OP_NE) { |
| 82 | fprintf(stderr, "[error] Only the `==` and `!=` operators are allowed with a globbing pattern\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | ret = validate_globbing(left); |
| 89 | if (ret) { |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | return validate_globbing(right); |
| 94 | } |
| 95 | case IR_OP_LOGICAL: |
| 96 | ret = validate_globbing(node->u.logical.left); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | return validate_globbing(node->u.logical.right); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * This function recursively validates that: |
| 105 | * |
| 106 | * 1. When there's a binary operation between two literal strings, |
| 107 | * if one of them has the IR_LOAD_STRING_TYPE_GLOB_STAR type, |
| 108 | * the other one has the IR_LOAD_STRING_TYPE_PLAIN type. |
| 109 | * |
| 110 | * In other words, you cannot compare two globbing patterns, except |
| 111 | * for two globbing patterns with only a star at the end for backward |
| 112 | * compatibility reasons. |
| 113 | * |
| 114 | * 2. When there's a binary operation between two literal strings, if |
| 115 | * one of them is a (full) star globbing pattern, the binary |
| 116 | * operation is either == or !=. |
| 117 | */ |
| 118 | LTTNG_HIDDEN |
| 119 | int filter_visitor_ir_validate_globbing(struct filter_parser_ctx *ctx) |
| 120 | { |
| 121 | return validate_globbing(ctx->ir_root); |
| 122 | } |