summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsmbfs/smb/ssp.c
blob: d0b0d86d4f675447fbbc03a39be917bf74692bb7 (plain)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Security Provider glue
 *
 * Modeled after SSPI for now, only because we're currently
 * using the Microsoft sample spnego code.
 *
 * ToDo: Port all of this to GSS-API plugins.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <netdb.h>
#include <libintl.h>
#include <xti.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/byteorder.h>
#include <sys/socket.h>
#include <sys/fcntl.h>

#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#include <netsmb/smb_lib.h>
#include <netsmb/mchain.h>

#include "private.h"
#include "charsets.h"
#include "spnego.h"
#include "derparse.h"
#include "ssp.h"


/*
 * ssp_ctx_create_client
 *
 * This is the first function called for SMB "extended security".
 * Here we select a security support provider (SSP), or mechanism,
 * and build the security context used throughout authentication.
 *
 * Note that we receive a "hint" in the SMB Negotiate response
 * that contains the list of mechanisms supported by the server.
 * We use this to help us select a mechanism.
 *
 * With SSPI this would call:
 *	ssp->InitSecurityInterface()
 *	ssp->AcquireCredentialsHandle()
 *	ssp->InitializeSecurityContext()
 * With GSS-API this will become:
 *	gss_import_name(... service_principal_name)
 *	gss_init_sec_context(), etc.
 */
int
ssp_ctx_create_client(struct smb_ctx *ctx, struct mbdata *hint_mb)
{
	struct ssp_ctx *sp;
	mbuf_t *m;
	SPNEGO_MECH_OID oid;
	int indx, rc;
	int err = ENOTSUP; /* in case nothing matches */

	sp = malloc(sizeof (*sp));
	if (sp == NULL)
		return (ENOMEM);
	bzero(sp, sizeof (*sp));
	ctx->ct_ssp_ctx = sp;
	sp->smb_ctx = ctx;

	/*
	 * Parse the SPNEGO "hint" to get the server's list of
	 * supported mechanisms.  If the "hint" is empty,
	 * assume NTLMSSP.  (Or could use "raw NTLMSSP")
	 */
	m = hint_mb->mb_top;
	if (m == NULL)
		goto use_ntlm;
	rc = spnegoInitFromBinary((uchar_t *)m->m_data, m->m_len,
	    &sp->sp_hint);
	if (rc) {
		DPRINT("parse hint, rc %d", rc);
		goto use_ntlm;
	}

	/*
	 * Did the server offer Kerberos?
	 * Either spec. OID or legacy is OK,
	 * but have to remember what we got.
	 */
	oid = spnego_mech_oid_NotUsed;
	if (0 == spnegoIsMechTypeAvailable(sp->sp_hint,
	    spnego_mech_oid_Kerberos_V5, &indx))
		oid = spnego_mech_oid_Kerberos_V5;
	else if (0 == spnegoIsMechTypeAvailable(sp->sp_hint,
	    spnego_mech_oid_Kerberos_V5_Legacy, &indx))
		oid = spnego_mech_oid_Kerberos_V5_Legacy;
	if (oid != spnego_mech_oid_NotUsed) {
		/*
		 * Yes! Server offers Kerberos.
		 * Try to init our krb5 mechanism.
		 * It will fail if the calling user
		 * does not have krb5 credentials.
		 */
		sp->sp_mech = oid;
		err = krb5ssp_init_client(sp);
		if (err == 0) {
			DPRINT("using Kerberos");
			return (0);
		}
		/* else fall back to NTLMSSP */
	}

	/*
	 * Did the server offer NTLMSSP?
	 */
	if (0 == spnegoIsMechTypeAvailable(sp->sp_hint,
	    spnego_mech_oid_NTLMSSP, &indx)) {
		/*
		 * OK, we'll use NTLMSSP
		 */
	use_ntlm:
		sp->sp_mech = spnego_mech_oid_NTLMSSP;
		err = ntlmssp_init_client(sp);
		if (err == 0) {
			DPRINT("using NTLMSSP");
			return (0);
		}
	}

	/* No supported mechanisms! */
	return (err);
}


/*
 * ssp_ctx_destroy
 *
 * Dispatch to the mechanism-specific destroy.
 */
void
ssp_ctx_destroy(struct smb_ctx *ctx)
{
	ssp_ctx_t *sp;

	sp = ctx->ct_ssp_ctx;
	ctx->ct_ssp_ctx = NULL;

	if (sp == NULL)
		return;

	if (sp->sp_destroy != NULL)
		(sp->sp_destroy)(sp);

	if (sp->sp_hint != NULL)
		spnegoFreeData(sp->sp_hint);

	free(sp);
}


/*
 * ssp_ctx_next_token
 *
 * This is the function called to generate the next token to send,
 * given a token just received, using the selected back-end method.
 * The back-end method is called a security service provider (SSP).
 *
 * This is also called to generate the first token to send
 * (when called with caller_in == NULL) and to handle the last
 * token received (when called with caller_out == NULL).
 * See caller: smb_ssnsetup_spnego
 *
 * Note that if the back-end SSP "next token" function ever
 * returns an error, the conversation ends, and there are
 * no further calls to this function for this context.
 *
 * General outline of this funcion:
 *	if (caller_in)
 *		Unwrap caller_in spnego blob,
 *		store payload in body_in
 *	Call back-end SSP "next token" method (body_in, body_out)
 *	if (caller_out)
 *		Wrap returned body_out in spnego,
 *		store in caller_out
 *
 * With SSPI this would call:
 *	ssp->InitializeSecurityContext()
 * With GSS-API this will become:
 *	gss_init_sec_context()
 */
int
ssp_ctx_next_token(struct smb_ctx *ctx,
	struct mbdata *caller_in,
	struct mbdata *caller_out)
{
	struct mbdata body_in, body_out;
	SPNEGO_TOKEN_HANDLE stok_in, stok_out;
	SPNEGO_NEGRESULT result;
	ssp_ctx_t *sp;
	struct mbuf *m;
	ulong_t toklen;
	int err, rc;

	bzero(&body_in, sizeof (body_in));
	bzero(&body_out, sizeof (body_out));
	stok_out = stok_in = NULL;
	sp = ctx->ct_ssp_ctx;

	/*
	 * If we have an spnego input token, parse it,
	 * extract the payload for the back-end SSP.
	 */
	if (caller_in != NULL) {

		/*
		 * Let the spnego code parse it.
		 */
		m = caller_in->mb_top;
		rc = spnegoInitFromBinary((uchar_t *)m->m_data,
		    m->m_len, &stok_in);
		if (rc) {
			DPRINT("parse reply, rc %d", rc);
			err = EBADRPC;
			goto out;
		}
		/* Note: Allocated stok_in  */

		/*
		 * Now get the payload.  Two calls:
		 * first gets the size, 2nd the data.
		 *
		 * Expect SPNEGO_E_BUFFER_TOO_SMALL here,
		 * but if the payload is missing, we'll
		 * get SPNEGO_E_ELEMENT_UNAVAILABLE.
		 */
		rc = spnegoGetMechToken(stok_in, NULL, &toklen);
		switch (rc) {
		case SPNEGO_E_ELEMENT_UNAVAILABLE:
			toklen = 0;
			break;
		case SPNEGO_E_BUFFER_TOO_SMALL:
			/* have toklen */
			break;
		default:
			DPRINT("GetMechTok1, rc %d", rc);
			err = EBADRPC;
			goto out;
		}
		err = mb_init_sz(&body_in, (size_t)toklen);
		if (err)
			goto out;
		m = body_in.mb_top;
		if (toklen > 0) {
			rc = spnegoGetMechToken(stok_in,
			    (uchar_t *)m->m_data, &toklen);
			if (rc) {
				DPRINT("GetMechTok2, rc %d", rc);
				err = EBADRPC;
				goto out;
			}
			body_in.mb_count = m->m_len = (size_t)toklen;
		}
	}

	/*
	 * Call the back-end security provider (SSP) to
	 * handle the received token (if present) and
	 * generate an output token (if requested).
	 */
	err = sp->sp_nexttok(sp,
	    caller_in ? &body_in : NULL,
	    caller_out ? &body_out : NULL);
	if (err)
		goto out;

	/*
	 * Wrap the outgoing body if requested,
	 * either negTokenInit on first call, or
	 * negTokenTarg on subsequent calls.
	 */
	if (caller_out != NULL) {
		m = body_out.mb_top;

		if (caller_in == NULL) {
			/*
			 * This is the first call, so create a
			 * negTokenInit.
			 */
			rc = spnegoCreateNegTokenInit(
			    sp->sp_mech, 0,
			    (uchar_t *)m->m_data, m->m_len,
			    NULL, 0, &stok_out);
			/* Note: allocated stok_out */
		} else {
			/*
			 * Note: must pass spnego_mech_oid_NotUsed,
			 * instead of sp->sp_mech so that the spnego
			 * code will not marshal a mech OID list.
			 * The mechanism is determined at this point,
			 * and some servers won't parse an unexpected
			 * mech. OID list in a negTokenTarg
			 */
			rc = spnegoCreateNegTokenTarg(
			    spnego_mech_oid_NotUsed,
			    spnego_negresult_NotUsed,
			    (uchar_t *)m->m_data, m->m_len,
			    NULL, 0, &stok_out);
			/* Note: allocated stok_out */
		}
		if (rc) {
			DPRINT("CreateNegTokenX, rc 0x%x", rc);
			err = EBADRPC;
			goto out;
		}

		/*
		 * Copy binary from stok_out to caller_out
		 * Two calls: get the size, get the data.
		 */
		rc = spnegoTokenGetBinary(stok_out, NULL, &toklen);
		if (rc != SPNEGO_E_BUFFER_TOO_SMALL) {
			DPRINT("GetBinary1, rc 0x%x", rc);
			err = EBADRPC;
			goto out;
		}
		err = mb_init_sz(caller_out, (size_t)toklen);
		if (err)
			goto out;
		m = caller_out->mb_top;
		rc = spnegoTokenGetBinary(stok_out,
		    (uchar_t *)m->m_data, &toklen);
		if (rc) {
			DPRINT("GetBinary2, rc 0x%x", rc);
			err = EBADRPC;
			goto out;
		}
		caller_out->mb_count = m->m_len = (size_t)toklen;
	} else {
		/*
		 * caller_out == NULL, so this is the "final" call.
		 * Get final SPNEGO result from the INPUT token.
		 */
		rc = spnegoGetNegotiationResult(stok_in, &result);
		if (rc) {
			DPRINT("rc 0x%x", rc);
			err = EBADRPC;
			goto out;
		}
		DPRINT("spnego result: 0x%x", result);
		if (result != spnego_negresult_success) {
			err = EAUTH;
			goto out;
		}
	}
	err = 0;

out:
	mb_done(&body_in);
	mb_done(&body_out);
	spnegoFreeData(stok_in);
	spnegoFreeData(stok_out);

	return (err);
}