|
| 1 | +/* |
| 2 | + * SNOOPY LOGGER |
| 3 | + * |
| 4 | + * File: snoopy/filter/exclude_comm.c |
| 5 | + * |
| 6 | + * Copyright (c) 2015 Datto, Inc. All rights reserved. |
| 7 | + * Author: Fred Mora - fmora@datto.com |
| 8 | + * |
| 9 | + * This program is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation; either version 2, or (at your option) |
| 12 | + * any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program; if not, write to the Free Software Foundation, |
| 21 | + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | + * |
| 23 | + * |
| 24 | + * changelog: |
| 25 | + * add exclude_comm filter; |
| 26 | + * arstercz |
| 27 | + * 2020-10-26 |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +/* |
| 33 | + * Includes order: from local to global |
| 34 | + */ |
| 35 | +#include "exclude_comm.h" |
| 36 | +#include "exclude_spawns_of.h" |
| 37 | +#include "snoopy.h" |
| 38 | + |
| 39 | +#include <stdio.h> |
| 40 | +#include <stdlib.h> |
| 41 | +#include <string.h> |
| 42 | +#include <sys/types.h> |
| 43 | +#include <unistd.h> |
| 44 | + |
| 45 | +char* extract_comm(const char *str, const char *needle); |
| 46 | + |
| 47 | +/* |
| 48 | + * SNOOPY FILTER: exclude_comm |
| 49 | + * |
| 50 | + * Description: |
| 51 | + * Excludes all log messages for executables that have the specified program name in their ancestors. |
| 52 | + * Strategy: We parse arg to create the "list of specified programs" (LoSP). |
| 53 | + * Then, we filter by filename from LoSP |
| 54 | + * |
| 55 | + * Params: |
| 56 | + * filename: command full path filename. |
| 57 | + * logMessage: Pointer to string that contains formatted log message (may be manipulated) |
| 58 | + * arg: Comma-separated list of program comm names for the spawns of which log messages are dropped. |
| 59 | + * |
| 60 | + * Return: |
| 61 | + * SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP |
| 62 | + */ |
| 63 | +int snoopy_filter_exclude_comm(const char *filename, char *msg, char const * const arg) |
| 64 | +{ |
| 65 | + char *argDup; // Must not alter arg |
| 66 | + char **losp; // List of specified programs derived from arg |
| 67 | + int is_comm_in_list = 0; |
| 68 | + char *comm; |
| 69 | + |
| 70 | + // Turn comma-separated arg into array of program name strings |
| 71 | + argDup = strdup(arg); |
| 72 | + losp = string_to_token_array(argDup); |
| 73 | + if (losp == NULL) { |
| 74 | + // If failure, we cannot filter anything, just pass the message |
| 75 | + return SNOOPY_FILTER_PASS; |
| 76 | + } |
| 77 | + |
| 78 | + const char* delimiter = "/"; // path delimiter in Unix/Linux |
| 79 | + comm = extract_comm(filename, delimiter); // get command name |
| 80 | + |
| 81 | + // Check if one of the program names in losp is an ancestor |
| 82 | + is_comm_in_list = find_string_in_array(comm, losp); |
| 83 | + free(losp); |
| 84 | + free(argDup); |
| 85 | + return (is_comm_in_list == 1) ? SNOOPY_FILTER_DROP : SNOOPY_FILTER_PASS; // Error means pass |
| 86 | +} |
| 87 | + |
| 88 | +// get comm name from filename |
| 89 | +char* extract_comm(const char *str, const char *needle) |
| 90 | +{ |
| 91 | + if (*needle == '\0') |
| 92 | + return (char *)str; |
| 93 | + |
| 94 | + char *result = NULL; |
| 95 | + for (;;) |
| 96 | + { |
| 97 | + char *p = strstr(str, needle); |
| 98 | + if (p == NULL) |
| 99 | + break; |
| 100 | + |
| 101 | + result = p + 1; |
| 102 | + str = p + 1; |
| 103 | + } |
| 104 | + return result; |
| 105 | +} |
0 commit comments