Skip to content

Commit 8231095

Browse files
committed
Add gssspi_mech_invoke method to turn on debugging
This allows to set a file of own chosing as well as turn on and off debugging as needed. Thread safe, and applies to all threads at once. Signed-off-by: Simo Sorce <[email protected]>
1 parent 1867f86 commit 8231095

File tree

6 files changed

+155
-4
lines changed

6 files changed

+155
-4
lines changed

src/debug.c

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
/* Copyright (C) 2014 GSS-NTLMSSP contributors, see COPYING for license */
22

33
#define _GNU_SOURCE
4+
#include <errno.h>
5+
#include <limits.h>
6+
#include <pthread.h>
47
#include <stdarg.h>
58
#include <stdbool.h>
69
#include <stdio.h>
710
#include <stdlib.h>
11+
#include <string.h>
812

13+
#include "gssapi_ntlmssp.h"
14+
15+
#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
16+
17+
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
918
bool gssntlm_debug_initialized = false;
1019
bool gssntlm_debug_enabled = false;
1120
static FILE *debug_fd = NULL;
@@ -14,20 +23,104 @@ void gssntlm_debug_init(void)
1423
{
1524
char *env;
1625

26+
pthread_mutex_lock(&debug_mutex);
27+
1728
env = secure_getenv("GSSNTLMSSP_DEBUG");
1829
if (env) {
1930
debug_fd = fopen(env, "a");
2031
if (debug_fd) gssntlm_debug_enabled = true;
2132
}
2233
gssntlm_debug_initialized = true;
34+
35+
pthread_mutex_unlock(&debug_mutex);
2336
}
2437

2538
void gssntlm_debug_printf(const char *fmt, ...)
2639
{
2740
va_list ap;
2841

29-
va_start(ap, fmt);
30-
vfprintf(debug_fd, fmt, ap);
31-
va_end(ap);
32-
fflush(debug_fd);
42+
pthread_mutex_lock(&debug_mutex);
43+
44+
if (debug_fd) {
45+
va_start(ap, fmt);
46+
vfprintf(debug_fd, fmt, ap);
47+
va_end(ap);
48+
fflush(debug_fd);
49+
}
50+
51+
pthread_mutex_unlock(&debug_mutex);
52+
}
53+
54+
static int gssntlm_debug_enable(const char *filename)
55+
{
56+
FILE *old_debug_fd = debug_fd;
57+
FILE *new_debug_fd = NULL;
58+
int ret = 0;
59+
60+
pthread_mutex_lock(&debug_mutex);
61+
62+
new_debug_fd = fopen(filename, "a");
63+
if (new_debug_fd) {
64+
gssntlm_debug_enabled = true;
65+
} else {
66+
gssntlm_debug_enabled = false;
67+
ret = errno;
68+
}
69+
70+
debug_fd = new_debug_fd;
71+
72+
if (old_debug_fd != NULL) {
73+
fclose(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+
FILE *old_debug_fd = debug_fd;
84+
int ret = 0;
85+
86+
pthread_mutex_lock(&debug_mutex);
87+
88+
gssntlm_debug_enabled = false;
89+
debug_fd = NULL;
90+
91+
if (old_debug_fd != NULL) {
92+
ret = fclose(old_debug_fd);
93+
}
94+
95+
pthread_mutex_unlock(&debug_mutex);
96+
97+
return ret;
98+
}
99+
100+
gss_OID_desc gssntlm_debug_oid = {
101+
GSS_NTLMSSP_DEBUG_OID_LENGTH,
102+
discard_const(GSS_NTLMSSP_DEBUG_OID_STRING)
103+
};
104+
105+
int gssntlm_debug_invoke(gss_buffer_t value)
106+
{
107+
char filename[PATH_MAX] = { 0 };
108+
109+
gssntlm_debug_initialized = true;
110+
111+
if (value->length > PATH_MAX - 1) {
112+
return EINVAL;
113+
}
114+
115+
if ((value->length != 0) &&
116+
(((char *)value->value)[0] != '\0')) {
117+
memcpy(filename, value->value, value->length);
118+
filename[value->length] = '\0';
119+
}
120+
121+
if (filename[0] == '\0') {
122+
return gssntlm_debug_disable();
123+
}
124+
125+
return gssntlm_debug_enable(filename);
33126
}

src/debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdbool.h>
77
#include <time.h>
88

9+
extern gss_OID_desc gssntlm_debug_oid;
910
extern bool gssntlm_debug_initialized;
1011
extern bool gssntlm_debug_enabled;
1112

@@ -34,4 +35,6 @@ static inline int debug_gss_errors(const char *function,
3435
#define DEBUG_GSS_ERRORS(maj, min) \
3536
debug_gss_errors(__FUNCTION__, __FILE__, __LINE__, maj, min)
3637

38+
int gssntlm_debug_invoke(gss_buffer_t value);
39+
3740
#endif /* _GSSNTLMSSP_DEBUG_H_ */

src/gss_ntlmssp.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,36 @@ int gssntlm_get_lm_compatibility_level(void)
178178
/* use 3 by default for better compatibility */
179179
return 3;
180180
}
181+
182+
uint32_t gssntlm_mech_invoke(uint32_t *minor_status,
183+
const gss_OID desired_mech,
184+
const gss_OID desired_object,
185+
gss_buffer_t value)
186+
{
187+
uint32_t retmaj = GSS_S_COMPLETE;
188+
uint32_t retmin = 0;
189+
190+
if (minor_status == NULL) {
191+
return GSS_S_CALL_INACCESSIBLE_WRITE;
192+
}
193+
194+
if (desired_mech != GSS_C_NO_OID &&
195+
!gss_oid_equal(desired_mech, &gssntlm_oid)) {
196+
return GSSERRS(0, GSS_S_BAD_MECH);
197+
}
198+
199+
if (desired_object == GSS_C_NO_OID) {
200+
return GSSERRS(0, GSS_S_CALL_INACCESSIBLE_READ);
201+
}
202+
203+
if (!gss_oid_equal(desired_object, &gssntlm_debug_oid)) {
204+
return GSSERRS(EINVAL, GSS_S_UNAVAILABLE);
205+
}
206+
207+
retmin = gssntlm_debug_invoke(value);
208+
if (retmin != 0) {
209+
retmaj = GSS_S_UNAVAILABLE;
210+
}
211+
212+
return GSSERR();
213+
}

src/gss_ntlmssp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ uint32_t gssntlm_context_is_valid(struct gssntlm_ctx *ctx,
176176

177177
int gssntlm_get_lm_compatibility_level(void);
178178

179+
uint32_t gssntlm_mech_invoke(uint32_t *minor_status,
180+
const gss_OID desired_mech,
181+
const gss_OID desired_object,
182+
gss_buffer_t value);
183+
179184
void gssntlm_int_release_name(struct gssntlm_name *name);
180185
void gssntlm_int_release_cred(struct gssntlm_cred *cred);
181186

src/gss_spi.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,12 @@ OM_uint32 gss_inquire_attrs_for_mech(OM_uint32 *minor_status,
443443
return gssntlm_inquire_attrs_for_mech(minor_status, mech_oid, mech_attrs,
444444
known_mech_attrs);
445445
}
446+
447+
OM_uint32 gssspi_mech_invoke(OM_uint32 *minor_status,
448+
const gss_OID desired_mech,
449+
const gss_OID desired_object,
450+
gss_buffer_t value)
451+
{
452+
return gssntlm_mech_invoke(minor_status, desired_mech, desired_object,
453+
value);
454+
}

src/gssapi_ntlmssp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ extern "C" {
5353
#define GSS_NTLMSSP_RESET_CRYPTO_OID_STRING GSS_NTLMSSP_BASE_OID_STRING "\x03"
5454
#define GSS_NTLMSSP_RESET_CRYPTO_OID_LENGTH GSS_NTLMSSP_BASE_OID_LENGTH + 1
5555

56+
/* Debug OID for mech_invoke
57+
* Use this with gsspi_mech_invoke, to pass a file name and enable debugging.
58+
*/
59+
#define GSS_NTLMSSP_DEBUG_OID_STRING GSS_NTLMSSP_BASE_OID_STRING "\x04"
60+
#define GSS_NTLMSSP_DEBUG_OID_LENGTH GSS_NTLMSSP_BASE_OID_LENGTH + 1
61+
62+
63+
5664
#define GSS_NTLMSSP_CS_DOMAIN "ntlmssp_domain"
5765
#define GSS_NTLMSSP_CS_NTHASH "ntlmssp_nthash"
5866
#define GSS_NTLMSSP_CS_PASSWORD "ntlmssp_password"

0 commit comments

Comments
 (0)