| 1 | |
| 2 | /* |
| 3 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library 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 GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Authors: |
| 21 | * Lukas Kuklinek <[email protected]> |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | # include <stdio.h> |
| 29 | # include <stdarg.h> |
| 30 | # include <string.h> |
| 31 | # include <stdlib.h> |
| 32 | # include <sys/types.h> |
| 33 | #ifdef HAVE_FLOCK |
| 34 | # include <sys/file.h> |
| 35 | #endif |
| 36 | #ifdef OS_WINDOWS |
| 37 | #include <io.h> |
| 38 | #else |
| 39 | #include <unistd.h> |
| 40 | #endif |
| 41 | # include <time.h> |
| 42 | # include <errno.h> |
| 43 | # include <fcntl.h> |
| 44 | # include <sys/stat.h> |
| 45 | |
| 46 | #if defined(OVAL_PROBES_ENABLED) |
| 47 | # include <sexp.h> |
| 48 | # include <sexp-output.h> |
| 49 | #endif |
| 50 | |
| 51 | # include "debug_priv.h" |
| 52 | |
| 53 | #include <oscap_debug.h> |
| 54 | #include "_error.h" |
| 55 | |
| 56 | #ifndef PATH_SEPARATOR |
| 57 | # define PATH_SEPARATOR '/' |
| 58 | #endif |
| 59 | |
| 60 | #if defined(OS_WINDOWS) |
| 61 | # include <windows.h> |
| 62 | # define GET_PROGRAM_NAME get_program_name() |
| 63 | #elif defined(OS_APPLE) || defined(OS_FREEBSD) |
| 64 | # define GET_PROGRAM_NAME getprogname() |
| 65 | #else |
| 66 | # define GET_PROGRAM_NAME program_invocation_short_name |
| 67 | #endif |
| 68 | |
| 69 | static const struct oscap_string_map OSCAP_VERBOSITY_LEVELS[] = { |
| 70 | {DBG_E, "ERROR"}, |
| 71 | {DBG_W, "WARNING"}, |
| 72 | {DBG_I, "INFO"}, |
| 73 | {DBG_D, "DEVEL"}, |
| 74 | {DBG_UNKNOWN, NULL} |
| 75 | }; |
| 76 | |
| 77 | # if defined(OSCAP_THREAD_SAFE) |
| 78 | # include <pthread.h> |
| 79 | # if defined(OS_FREEBSD) |
| 80 | # include <pthread_np.h> |
| 81 | # endif /* OS_FREEBSD */ |
| 82 | static pthread_mutex_t __debuglog_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 83 | # endif /* OSCAP_THREAD_SAFE */ |
| 84 | |
| 85 | FILE *__debuglog_fp = NULL; |
| 86 | oscap_verbosity_levels __debuglog_level = DBG_UNKNOWN; |
| 87 | |
| 88 | #if defined(OSCAP_THREAD_SAFE) |
| 89 | # define __LOCK_FP do { if (pthread_mutex_lock (&__debuglog_mutex) != 0) abort(); } while(0) |
| 90 | # define __UNLOCK_FP do { if (pthread_mutex_unlock (&__debuglog_mutex) != 0) abort(); } while(0) |
| 91 | #else |
| 92 | # define __LOCK_FP while(0) |
| 93 | # define __UNLOCK_FP while(0) |
| 94 | #endif |
| 95 | |
| 96 | #define THREAD_NAME_LEN 16 |
| 97 | |
| 98 | #if defined(OS_WINDOWS) |
| 99 | static char * get_program_name() |
| 100 | { |
| 101 | char path[PATH_MAX + 1]; |
| 102 | int path_size = GetModuleFileName(NULL, path, sizeof(path) - 1); |
| 103 | |
| 104 | if(path_size < 0) |
| 105 | return NULL; |
| 106 | if(path_size == sizeof(path) - 1) |
| 107 | path[path_size] = '\0'; |
| 108 | return strdup(path); |
| 109 | } |
| 110 | |
| 111 | int setenv(const char *name, const char *value, int overwrite) |
| 112 | { |
| 113 | int errorcode = 0; |
| 114 | |
| 115 | if(!overwrite) { |
| 116 | size_t envsize = 0; |
| 117 | errorcode = getenv_s(&envsize, NULL, 0, name); |
| 118 | if(errorcode || envsize) |
| 119 | return errorcode; |
| 120 | } |
| 121 | |
| 122 | return _putenv_s(name, value); |
| 123 | } |
| 124 | #endif |
| 125 | |
| 126 | static void __oscap_debuglog_close(void) |
| 127 | { |
| 128 | fclose(__debuglog_fp); |
| 129 | } |
| 130 | |
| 131 | oscap_verbosity_levels oscap_verbosity_level_from_cstr(const char *level_name) |
| 132 | { |
| 133 | return oscap_string_to_enum(OSCAP_VERBOSITY_LEVELS, level_name); |
| 134 | } |
| 135 | |
| 136 | bool oscap_set_verbose(const char *verbosity_level, const char *filename) |
| 137 | { |
| 138 | if (verbosity_level == NULL) { |
| 139 | verbosity_level = "WARNING"; |
| 140 | } |
| 141 | __debuglog_level = oscap_verbosity_level_from_cstr(verbosity_level); |
| 142 | if (__debuglog_level == DBG_UNKNOWN) { |
| 143 | return false; |
| 144 | } |
| 145 | if (filename == NULL) { |
| 146 | __debuglog_fp = stderr; |
| 147 | return true; |
| 148 | } |
| 149 | int fd; |
| 150 | /* Open a file. If the file doesn't exist, create it. |
| 151 | * If the file exists, erase its content. |
| 152 | * File is opened in "append" mode. |
| 153 | * Append mode is necessary when more processes write to same file. |
| 154 | * Every process using the log file must open it in append mode, |
| 155 | * because otherwise some data may be missing on output. |
| 156 | */ |
| 157 | #ifdef OS_WINDOWS |
| 158 | fd = open(filename, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE); |
| 159 | #else |
| 160 | fd = open(filename, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, |
| 161 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 162 | #endif |
| 163 | |
| 164 | if (fd == -1) { |
| 165 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to open file %s: %s.", filename, strerror(errno)); |
| 166 | return false; |
| 167 | } |
| 168 | __debuglog_fp = fdopen(fd, "a"); |
| 169 | if (__debuglog_fp == NULL) { |
| 170 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to associate stream with file %s: %s.", filename, strerror(errno)); |
| 171 | return false; |
| 172 | } |
| 173 | setbuf(__debuglog_fp, NULL); |
| 174 | atexit(&__oscap_debuglog_close); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static const char *__oscap_path_rstrip(const char *path) |
| 180 | { |
| 181 | const char *separator = strrchr(path, PATH_SEPARATOR); |
| 182 | if (separator) { |
| 183 | return (separator + 1); |
| 184 | } |
| 185 | |
| 186 | return (path); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | static void debug_message_start(int level, int delta_indent) |
| 191 | { |
| 192 | char l; |
| 193 | static int indent = 0; |
| 194 | |
| 195 | __LOCK_FP; |
| 196 | |
| 197 | indent += delta_indent; |
| 198 | |
| 199 | switch (level) { |
| 200 | case DBG_E: |
| 201 | l = 'E'; |
| 202 | break; |
| 203 | case DBG_W: |
| 204 | l = 'W'; |
| 205 | break; |
| 206 | case DBG_I: |
| 207 | l = 'I'; |
| 208 | break; |
| 209 | case DBG_D: |
| 210 | l = 'D'; |
| 211 | break; |
| 212 | default: |
| 213 | l = '0'; |
| 214 | } |
| 215 | fprintf(__debuglog_fp, "%c: %s: ", l, GET_PROGRAM_NAME); |
| 216 | for (int i = 0; i < indent; i++) { |
| 217 | fprintf(__debuglog_fp, " "); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static void debug_message_devel_metadata(const char *file, const char *fn, size_t line) |
| 222 | { |
| 223 | const char *f = __oscap_path_rstrip(file); |
| 224 | #if defined(OSCAP_THREAD_SAFE) |
| 225 | char thread_name[THREAD_NAME_LEN]; |
| 226 | pthread_t thread = pthread_self(); |
| 227 | #if defined(HAVE_PTHREAD_GETNAME_NP) |
| 228 | pthread_getname_np(thread, thread_name, THREAD_NAME_LEN); |
| 229 | #else |
| 230 | snprintf(thread_name, THREAD_NAME_LEN, "unknown"); |
| 231 | #endif |
| 232 | #if !defined(__MINGW32__) && defined(OS_WINDOWS) |
| 233 | unsigned long long tid = pthread_getw32threadid_np(thread); |
| 234 | #else |
| 235 | /* XXX: non-portable usage of pthread_t */ |
| 236 | unsigned long long tid = (unsigned long long) thread; |
| 237 | #endif |
| 238 | fprintf(__debuglog_fp, " [%s(%ld):%s(%llx):%s:%zu:%s]", |
| 239 | GET_PROGRAM_NAME, (long) getpid(), thread_name, |
| 240 | tid, f, line, fn); |
| 241 | #else |
| 242 | fprintf(__debuglog_fp, " [%ld:%s:%zu:%s]", (long) getpid(), |
| 243 | f, line, fn); |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | static void debug_message_end() |
| 248 | { |
| 249 | fputc('\n', __debuglog_fp); |
| 250 | __UNLOCK_FP; |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | void __oscap_dlprintf(int level, const char *file, const char *fn, size_t line, int delta_indent, const char *fmt, ...) |
| 255 | { |
| 256 | va_list ap; |
| 257 | |
| 258 | if (__debuglog_fp == NULL) { |
| 259 | return; |
| 260 | } |
| 261 | if (fmt == NULL) { |
| 262 | return; |
| 263 | } |
| 264 | if (__debuglog_level < level) { |
| 265 | return; |
| 266 | } |
| 267 | va_start(ap, fmt); |
| 268 | debug_message_start(level, delta_indent); |
| 269 | vfprintf(__debuglog_fp, fmt, ap); |
| 270 | if (__debuglog_level == DBG_D) { |
| 271 | debug_message_devel_metadata(file, fn, line); |
| 272 | } |
| 273 | debug_message_end(); |
| 274 | va_end(ap); |
| 275 | } |
| 276 | |
| 277 | void __oscap_debuglog_object (const char *file, const char *fn, size_t line, int objtype, void *obj) |
| 278 | { |
| 279 | if (__debuglog_fp == NULL) { |
| 280 | return; |
| 281 | } |
| 282 | if (__debuglog_level < DBG_D) { |
| 283 | return; |
| 284 | } |
| 285 | debug_message_start(DBG_D, 0); |
| 286 | switch (objtype) { |
| 287 | case OSCAP_DEBUGOBJ_SEXP: |
| 288 | #if defined(OVAL_PROBES_ENABLED) |
| 289 | SEXP_fprintfa(__debuglog_fp, (SEXP_t *)obj); |
| 290 | #endif |
| 291 | break; |
| 292 | default: |
| 293 | fprintf(__debuglog_fp, "Attempt to dump a not supported object."); |
| 294 | } |
| 295 | debug_message_devel_metadata(file, fn, line); |
| 296 | debug_message_end(); |
| 297 | } |
| 298 | |
| 299 | void oscap_print_env_vars() |
| 300 | { |
| 301 | const char *known_env_vars[] = { |
| 302 | "OSCAP_CHECK_ENGINE_PLUGIN_DIR", |
| 303 | "OSCAP_CONTAINER_VARS", |
| 304 | "OSCAP_EVALUATION_TARGET", |
| 305 | "OSCAP_FULL_VALIDATION", |
| 306 | "OSCAP_OVAL_COMMAND_OPTIONS", |
| 307 | "OSCAP_PCRE_EXEC_RECURSION_LIMIT", |
| 308 | "OSCAP_PROBE_ROOT", |
| 309 | "SEXP_VALIDATE_DISABLE", |
| 310 | "SOURCE_DATE_EPOCH", |
| 311 | "OSCAP_PROBE_MEMORY_USAGE_RATIO", |
| 312 | "OSCAP_PROBE_MAX_COLLECTED_ITEMS", |
| 313 | "OSCAP_PROBE_IGNORE_PATHS", |
| 314 | "OSCAP_PREFERRED_ENGINE", |
| 315 | NULL |
| 316 | }; |
| 317 | dI("Using environment variables:"); |
| 318 | for (int i = 0; known_env_vars[i]; i++) { |
| 319 | char *env_var_val = getenv(known_env_vars[i]); |
| 320 | dI("%s='%s'", known_env_vars[i], env_var_val ? env_var_val : ""); |
| 321 | } |
| 322 | } |