Coverage Report

Created: 2024-06-18 06:03

/src/gss-ntlmssp/src/debug.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2014 GSS-NTLMSSP contributors, see COPYING for license */
2
3
#define _GNU_SOURCE
4
#include <errno.h>
5
#include <fcntl.h>
6
#include <limits.h>
7
#include <pthread.h>
8
#include <stdarg.h>
9
#include <stdbool.h>
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <string.h>
13
#include <sys/stat.h>
14
#include <sys/types.h>
15
#include <unistd.h>
16
17
#include "gssapi_ntlmssp.h"
18
19
0
#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;
23
bool gssntlm_debug_initialized = false;
24
int gssntlm_debug_fd = -1;
25
26
void gssntlm_debug_init(void)
27
1
{
28
1
    char *env;
29
30
1
    if (gssntlm_debug_initialized) return;
31
32
1
    pthread_mutex_lock(&debug_mutex);
33
34
1
    env = secure_getenv("GSSNTLMSSP_DEBUG");
35
1
    if (env) {
36
0
        gssntlm_debug_fd = open(env, OPEN_FLAGS, 0660);
37
0
    }
38
1
    gssntlm_debug_initialized = true;
39
40
1
    pthread_mutex_unlock(&debug_mutex);
41
1
}
42
43
void gssntlm_debug_printf(const char *fmt, ...)
44
0
{
45
0
    va_list ap;
46
47
0
    if (gssntlm_debug_fd == -1) return;
48
49
0
    va_start(ap, fmt);
50
0
    vdprintf(gssntlm_debug_fd, fmt, ap);
51
0
    va_end(ap);
52
0
    fdatasync(gssntlm_debug_fd);
53
0
}
54
55
static int gssntlm_debug_enable(const char *filename)
56
0
{
57
0
    int old_debug_fd = gssntlm_debug_fd;
58
0
    int new_debug_fd = -1;
59
0
    int ret = 0;
60
61
0
    pthread_mutex_lock(&debug_mutex);
62
63
0
    gssntlm_debug_initialized = true;
64
65
0
    new_debug_fd = open(filename, OPEN_FLAGS, 0660);
66
0
    if (new_debug_fd == -1) {
67
0
        ret = errno;
68
0
    }
69
70
0
    gssntlm_debug_fd = new_debug_fd;
71
72
0
    if (old_debug_fd != -1) {
73
0
        close(old_debug_fd);
74
0
    }
75
76
0
    pthread_mutex_unlock(&debug_mutex);
77
78
0
    return ret;
79
0
}
80
81
static int gssntlm_debug_disable(void)
82
0
{
83
0
    int old_debug_fd = gssntlm_debug_fd;
84
0
    int ret = 0;
85
86
0
    pthread_mutex_lock(&debug_mutex);
87
88
0
    gssntlm_debug_fd = -1;
89
90
0
    if (old_debug_fd != -1) {
91
0
        ret = close(old_debug_fd);
92
0
    }
93
94
0
    pthread_mutex_unlock(&debug_mutex);
95
96
0
    return ret;
97
0
}
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
0
{
106
0
    char filename[PATH_MAX] = { 0 };
107
108
0
    if (value->length > PATH_MAX - 1) {
109
0
        return EINVAL;
110
0
    }
111
112
0
    if ((value->length != 0) &&
113
0
        (((char *)value->value)[0] != '\0')) {
114
0
        memcpy(filename, value->value, value->length);
115
0
        filename[value->length] = '\0';
116
0
    }
117
118
0
    if (filename[0] == '\0') {
119
0
        return gssntlm_debug_disable();
120
0
    }
121
122
0
    return gssntlm_debug_enable(filename);
123
0
}