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