|
1 | 1 | /* Copyright (C) 2014 GSS-NTLMSSP contributors, see COPYING for license */
|
2 | 2 |
|
3 | 3 | #define _GNU_SOURCE
|
| 4 | +#include <errno.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <limits.h> |
| 7 | +#include <pthread.h> |
4 | 8 | #include <stdarg.h>
|
5 | 9 | #include <stdbool.h>
|
6 | 10 | #include <stdio.h>
|
7 | 11 | #include <stdlib.h>
|
| 12 | +#include <string.h> |
| 13 | +#include <sys/stat.h> |
| 14 | +#include <sys/types.h> |
| 15 | +#include <unistd.h> |
8 | 16 |
|
| 17 | +#include "gssapi_ntlmssp.h" |
| 18 | + |
| 19 | +#define OPEN_FLAGS O_WRONLY | O_CREAT | O_APPEND| O_CLOEXEC |
| 20 | +#define discard_const(ptr) ((void *)((uintptr_t)(ptr))) |
| 21 | + |
| 22 | +static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
9 | 23 | bool gssntlm_debug_initialized = false;
|
10 |
| -bool gssntlm_debug_enabled = false; |
11 |
| -static FILE *debug_fd = NULL; |
| 24 | +int gssntlm_debug_fd = -1; |
12 | 25 |
|
13 | 26 | void gssntlm_debug_init(void)
|
14 | 27 | {
|
15 | 28 | char *env;
|
16 | 29 |
|
| 30 | + if (gssntlm_debug_initialized) return; |
| 31 | + |
| 32 | + pthread_mutex_lock(&debug_mutex); |
| 33 | + |
17 | 34 | env = secure_getenv("GSSNTLMSSP_DEBUG");
|
18 | 35 | if (env) {
|
19 |
| - debug_fd = fopen(env, "a"); |
20 |
| - if (debug_fd) gssntlm_debug_enabled = true; |
| 36 | + gssntlm_debug_fd = open(env, OPEN_FLAGS, 0660); |
21 | 37 | }
|
22 | 38 | gssntlm_debug_initialized = true;
|
| 39 | + |
| 40 | + pthread_mutex_unlock(&debug_mutex); |
23 | 41 | }
|
24 | 42 |
|
25 | 43 | void gssntlm_debug_printf(const char *fmt, ...)
|
26 | 44 | {
|
27 | 45 | va_list ap;
|
28 | 46 |
|
| 47 | + if (gssntlm_debug_fd == -1) return; |
| 48 | + |
29 | 49 | va_start(ap, fmt);
|
30 |
| - vfprintf(debug_fd, fmt, ap); |
| 50 | + vdprintf(gssntlm_debug_fd, fmt, ap); |
31 | 51 | va_end(ap);
|
32 |
| - fflush(debug_fd); |
| 52 | + fdatasync(gssntlm_debug_fd); |
| 53 | +} |
| 54 | + |
| 55 | +static int gssntlm_debug_enable(const char *filename) |
| 56 | +{ |
| 57 | + int old_debug_fd = gssntlm_debug_fd; |
| 58 | + int new_debug_fd = -1; |
| 59 | + int ret = 0; |
| 60 | + |
| 61 | + pthread_mutex_lock(&debug_mutex); |
| 62 | + |
| 63 | + gssntlm_debug_initialized = true; |
| 64 | + |
| 65 | + new_debug_fd = open(filename, OPEN_FLAGS, 0660); |
| 66 | + if (new_debug_fd == -1) { |
| 67 | + ret = errno; |
| 68 | + } |
| 69 | + |
| 70 | + gssntlm_debug_fd = new_debug_fd; |
| 71 | + |
| 72 | + if (old_debug_fd != -1) { |
| 73 | + close(old_debug_fd); |
| 74 | + } |
| 75 | + |
| 76 | + pthread_mutex_unlock(&debug_mutex); |
| 77 | + |
| 78 | + return ret; |
| 79 | +} |
| 80 | + |
| 81 | +static int gssntlm_debug_disable(void) |
| 82 | +{ |
| 83 | + int old_debug_fd = gssntlm_debug_fd; |
| 84 | + int ret = 0; |
| 85 | + |
| 86 | + pthread_mutex_lock(&debug_mutex); |
| 87 | + |
| 88 | + gssntlm_debug_fd = -1; |
| 89 | + |
| 90 | + if (old_debug_fd != -1) { |
| 91 | + ret = close(old_debug_fd); |
| 92 | + } |
| 93 | + |
| 94 | + pthread_mutex_unlock(&debug_mutex); |
| 95 | + |
| 96 | + return ret; |
| 97 | +} |
| 98 | + |
| 99 | +gss_OID_desc gssntlm_debug_oid = { |
| 100 | + GSS_NTLMSSP_DEBUG_OID_LENGTH, |
| 101 | + discard_const(GSS_NTLMSSP_DEBUG_OID_STRING) |
| 102 | +}; |
| 103 | + |
| 104 | +int gssntlm_debug_invoke(gss_buffer_t value) |
| 105 | +{ |
| 106 | + char filename[PATH_MAX] = { 0 }; |
| 107 | + |
| 108 | + if (value->length > PATH_MAX - 1) { |
| 109 | + return EINVAL; |
| 110 | + } |
| 111 | + |
| 112 | + if ((value->length != 0) && |
| 113 | + (((char *)value->value)[0] != '\0')) { |
| 114 | + memcpy(filename, value->value, value->length); |
| 115 | + filename[value->length] = '\0'; |
| 116 | + } |
| 117 | + |
| 118 | + if (filename[0] == '\0') { |
| 119 | + return gssntlm_debug_disable(); |
| 120 | + } |
| 121 | + |
| 122 | + return gssntlm_debug_enable(filename); |
33 | 123 | }
|
0 commit comments