-
Notifications
You must be signed in to change notification settings - Fork 21
/
kerberos.c
366 lines (311 loc) · 10.4 KB
/
kerberos.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* CNTLM is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* CNTLM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2007 David Kubicek
*
*/
//http://docs.oracle.com/cd/E18752_01/html/816-4863/sampleprogs-1.html
/*
* Copyright 1994 by OpenVision Technologies, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appears in all copies and
* that both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of OpenVision not be used
* in advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. OpenVision makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "globals.h"
#include "auth.h"
#include "kerberos.h"
#include <string.h>
#include <stdio.h>
#include <syslog.h>
#include <gssapi/gssapi.h>
#include <stdlib.h>
/*
* Function: display_ctx_flags
*
* Purpose: displays the flags returned by context initiation in
* a human-readable form
*
* Arguments:
*
* int ret_flags
*
* Effects:
*
* Strings corresponding to the context flags are printed on
* stdout, preceded by "context flag: " and followed by a newline
*/
void display_ctx_flags(OM_uint32 flags) {
if (flags & GSS_C_DELEG_FLAG)
syslog(LOG_INFO, "context flag: GSS_C_DELEG_FLAG\n");
if (flags & GSS_C_MUTUAL_FLAG)
syslog(LOG_INFO, "context flag: GSS_C_MUTUAL_FLAG\n");
if (flags & GSS_C_REPLAY_FLAG)
syslog(LOG_INFO, "context flag: GSS_C_REPLAY_FLAG\n");
if (flags & GSS_C_SEQUENCE_FLAG)
syslog(LOG_INFO, "context flag: GSS_C_SEQUENCE_FLAG\n");
if (flags & GSS_C_CONF_FLAG)
syslog(LOG_INFO, "context flag: GSS_C_CONF_FLAG\n");
if (flags & GSS_C_INTEG_FLAG)
syslog(LOG_INFO, "context flag: GSS_C_INTEG_FLAG\n");
}
static void display_status_1(char *m, OM_uint32 code, int type) {
OM_uint32 maj_stat, min_stat;
gss_buffer_desc msg;
OM_uint32 msg_ctx;
msg_ctx = 0;
while (1) {
maj_stat = gss_display_status(&min_stat, code, type, GSS_C_NULL_OID,
&msg_ctx, &msg);
if (1)
syslog(LOG_ERR, "GSS-API error %s: %s\n", m, (char *) msg.value);
(void) gss_release_buffer(&min_stat, &msg);
if (!msg_ctx)
break;
}
}
/*
* Function: display_status
*
* Purpose: displays GSS-API messages
*
* Arguments:
*
* msg a string to be displayed with the message
* maj_stat the GSS-API major status code
* min_stat the GSS-API minor status code
*
* Effects:
*
* The GSS-API messages associated with maj_stat and min_stat are
* displayed on stderr, each preceded by "GSS-API error <msg>: " and
* followed by a newline.
*/
void display_status(char *msg, OM_uint32 maj_stat, OM_uint32 min_stat) {
display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
if (maj_stat != GSS_S_COMPLETE)
display_status_1(msg, min_stat, GSS_C_MECH_CODE);
}
void display_name(char* txt, gss_name_t *name) {
gss_OID mechOid = GSS_C_NO_OID;
OM_uint32 maj_stat;
OM_uint32 min_stat;
gss_buffer_desc out_name;
// maj_stat = gss_display_name(&min_stat, *name, &out_name, &mechOid);
maj_stat = gss_display_name(&min_stat, *name, &out_name, NULL);
if (maj_stat != GSS_S_COMPLETE) {
display_status("Display name", maj_stat, min_stat);
}
syslog(LOG_INFO, txt, (char *) out_name.value);
(void) gss_release_buffer(&min_stat, &out_name);
if (mechOid != GSS_C_NO_OID)
(void) gss_release_oid(&min_stat, &mechOid);
}
int acquire_name(gss_name_t *target_name, char *service_name, gss_OID oid) {
gss_buffer_desc tmp_tok;
OM_uint32 maj_stat, min_stat;
tmp_tok.value = service_name;
tmp_tok.length = strlen(service_name) + 1;
maj_stat = gss_import_name(&min_stat, &tmp_tok, oid, target_name);
if (maj_stat != GSS_S_COMPLETE) {
display_status("Parsing name", maj_stat, min_stat);
} else if (debug){
display_name("Acquired kerberos name %s\n", target_name);
}
return maj_stat;
}
/*
* Function: client_establish_context
*
* Purpose: establishes a GSS-API context with a specified service and
* returns the context handle
*
* Arguments:
*
* service_name (r) the ASCII service name of the service
* context (w) the established GSS-API context
* ret_flags (w) the returned flags from init_sec_context
*
* Returns: 0 on success, -1 on failure
*
* Effects:
*
* service_name is imported as a GSS-API name and a GSS-API context is
* established with the corresponding service; the service should be
* listening on the TCP connection s. The default GSS-API mechanism
* is used, and mutual authentication and replay detection are
* requested.
*
* If successful, the context handle is returned in context. If
* unsuccessful, the GSS-API error messages are displayed on stderr
* and -1 is returned.
*/
int client_establish_context(char *service_name,
OM_uint32 *ret_flags, gss_buffer_desc* send_tok) {
gss_name_t target_name;
gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
OM_uint32 maj_stat, min_stat, init_min_stat;
if ((maj_stat = acquire_name(&target_name, service_name,
GSS_C_NT_HOSTBASED_SERVICE)) != GSS_S_COMPLETE)
return maj_stat;
if (debug)
display_name("SPN name %s\n", &target_name);
maj_stat = gss_init_sec_context(&init_min_stat, GSS_C_NO_CREDENTIAL,
&gss_context,
target_name,
GSS_C_NULL_OID,// use default mech
0, 0, // no special flags requested, no time req
GSS_C_NO_CHANNEL_BINDINGS, /* no channel bindings */
GSS_C_NO_BUFFER, // no input buffer
NULL, /* ignore mech type */
send_tok, ret_flags, //the returned token, the token flags
NULL /* ignore time_rec */
);
gss_release_name(&min_stat, &target_name);
if (maj_stat != GSS_S_COMPLETE) {
if(maj_stat == GSS_S_CONTINUE_NEEDED){
//TODO
}
display_status("Initializing context", maj_stat, init_min_stat);
if (gss_context == GSS_C_NO_CONTEXT)
gss_delete_sec_context(&min_stat, &gss_context, GSS_C_NO_BUFFER);
return maj_stat;
}
if (debug)
syslog(LOG_INFO, "Got token (size=%d)\n", (int) send_tok->length);
maj_stat = gss_delete_sec_context(&min_stat, &gss_context, GSS_C_NO_BUFFER);
if (maj_stat != GSS_S_COMPLETE) {
display_status("Deleting context", maj_stat, min_stat);
}
return GSS_S_COMPLETE;//maj_stat;
}
/**
* acquires a kerberos token for default credential using SPN HTTP@<thost>
*/
int acquire_kerberos_token(proxy_t* proxy, struct auth_s *credentials,
char* buf) {
char service_name[BUFSIZE], token[BUFSIZE];
OM_uint32 ret_flags, min_stat;
if (credentials->haskrb == KRB_KO) {
if (debug)
syslog(LOG_INFO, "Skipping already failed gss auth for %s\n",
proxy->hostname);
return 0;
}
if (!(credentials->haskrb & KRB_CREDENTIAL_AVAILABLE)) {
//try to get credential
// if(acquire_credential(credentials)){
credentials->haskrb |= check_credential();
if (!(credentials->haskrb & KRB_CREDENTIAL_AVAILABLE)){
//no credential -> no token
if (debug)
syslog(LOG_INFO, "No valid credential available\n");
return 0;
}
// }
}
gss_buffer_desc send_tok;
strcpy(service_name, "HTTP@");
strcat(service_name, proxy->hostname);
int rc = client_establish_context(service_name, &ret_flags, &send_tok);
if (rc == GSS_S_COMPLETE) {
credentials->haskrb = KRB_OK;
to_base64((unsigned char *) token, send_tok.value, send_tok.length,
BUFSIZE);
if (debug) {
syslog(LOG_INFO, "Token B64 (size=%d)... %s\n",
(int) strlen(token), token);
display_ctx_flags(ret_flags);
}
strcpy(buf, "NEGOTIATE ");
strcat(buf, token);
rc=1;
} else {
credentials->haskrb = KRB_KO;
if (debug)
syslog(LOG_INFO, "No valid token acquired for %s\n", service_name);
rc=0;
}
(void) gss_release_buffer(&min_stat, &send_tok);
return rc;
}
/**
* checks if a default cached credential is cached
*/
int check_credential() {
OM_uint32 min_stat;
gss_name_t name;
OM_uint32 lifetime;
gss_cred_usage_t cred_usage;
gss_OID_set mechanisms;
OM_uint32 maj_stat;
maj_stat = gss_inquire_cred(&min_stat, GSS_C_NO_CREDENTIAL, &name,
&lifetime, &cred_usage, &mechanisms);
if (maj_stat != GSS_S_COMPLETE) {
display_status("Inquire credential", maj_stat, min_stat);
return 0;
}
(void) gss_release_oid_set(&min_stat, &mechanisms);
if (name != NULL) {
display_name("Available cached credential %s\n", &name);
(void) gss_release_name(&min_stat, &name);
return KRB_CREDENTIAL_AVAILABLE;
}
return 0;
}
int acquire_credential(struct auth_s *credentials) {
OM_uint32 min_stat, maj_stat;
gss_name_t target_name;
OM_uint32 lifetime = GSS_C_INDEFINITE;
gss_cred_id_t *id;
char *password = credentials->passnt;
//!(g_creds->haskrb & KRB_CREDENTIAL_AVAILABLE)
if (credentials->user && password) {
char name[BUFSIZ];
strcpy(name, credentials->user);
if (credentials->domain) {
strcat(name, "@");
strcat(name, credentials->domain);
}
if ((maj_stat = acquire_name(&target_name, name, GSS_C_NT_USER_NAME))
!= GSS_S_COMPLETE)
return KRB_NO_CREDS;
//TODO
maj_stat = gss_acquire_cred(&min_stat, target_name, lifetime,
GSS_C_NO_OID_SET, GSS_C_INITIATE, id, NULL, NULL);
if (maj_stat != GSS_S_COMPLETE) {
display_status("Acquire credential", maj_stat, min_stat);
return KRB_NO_CREDS;
}
(void) gss_release_cred(&min_stat, id);
(void) gss_release_name(&min_stat, &target_name);
return KRB_CREDENTIAL_AVAILABLE;
}
return KRB_NO_CREDS;
}