2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
35 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
39 * SMB Session Setup, and related.
52 #include <sys/types.h>
54 #include <sys/byteorder.h>
55 #include <sys/socket.h>
56 #include <sys/fcntl.h>
58 #include <netinet/in.h>
59 #include <netinet/tcp.h>
60 #include <arpa/inet.h>
62 #include <netsmb/mchain.h>
63 #include <netsmb/netbios.h>
64 #include <netsmb/smb_dev.h>
65 #include <netsmb/smb.h>
67 #include <netsmb/smb_lib.h>
68 #include <netsmb/nb_lib.h>
73 #include "smb_crypt.h"
77 smb__ssnsetup(struct smb_ctx
*ctx
,
78 struct mbdata
*mbc1
, struct mbdata
*mbc2
,
79 uint32_t *statusp
, uint16_t *actionp
);
82 * Session Setup: NULL session (anonymous)
85 smb_ssnsetup_null(struct smb_ctx
*ctx
)
91 if (ctx
->ct_clnt_caps
& SMB_CAP_EXT_SECURITY
) {
92 /* Should not get here with... */
97 err
= smb__ssnsetup(ctx
, NULL
, NULL
, &ntstatus
, &action
);
101 DPRINT("status 0x%x action 0x%x", ntstatus
, (int)action
);
111 * SMB Session Setup, using NTLMv1 (and maybe LMv1)
114 smb_ssnsetup_ntlm1(struct smb_ctx
*ctx
)
116 struct mbdata lm_mbc
, nt_mbc
;
121 if (ctx
->ct_clnt_caps
& SMB_CAP_EXT_SECURITY
) {
122 /* Should not get here with... */
127 /* Make mb_done calls at out safe. */
128 bzero(&lm_mbc
, sizeof (lm_mbc
));
129 bzero(&nt_mbc
, sizeof (nt_mbc
));
131 /* Put the LM,NTLM responses (as mbdata). */
132 err
= ntlm_put_v1_responses(ctx
, &lm_mbc
, &nt_mbc
);
136 if ((ctx
->ct_vcflags
& SMBV_WILL_SIGN
) != 0 &&
137 (ctx
->ct_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
) == 0) {
138 err
= ntlm_build_mac_key(ctx
, &nt_mbc
);
141 /* OK, start signing! */
142 ctx
->ct_hflags2
|= SMB_FLAGS2_SECURITY_SIGNATURE
;
145 err
= smb__ssnsetup(ctx
, &lm_mbc
, &nt_mbc
, &ntstatus
, &action
);
149 DPRINT("status 0x%x action 0x%x", ntstatus
, (int)action
);
161 * SMB Session Setup, using NTLMv2 (and LMv2)
164 smb_ssnsetup_ntlm2(struct smb_ctx
*ctx
)
166 struct mbdata lm_mbc
, nt_mbc
, ti_mbc
;
171 if (ctx
->ct_clnt_caps
& SMB_CAP_EXT_SECURITY
) {
172 /* Should not get here with... */
177 /* Make mb_done calls at out safe. */
178 bzero(&lm_mbc
, sizeof (lm_mbc
));
179 bzero(&nt_mbc
, sizeof (nt_mbc
));
180 bzero(&ti_mbc
, sizeof (ti_mbc
));
182 /* Build the NTLMv2 "target info" blob (as mbdata) */
183 err
= ntlm_build_target_info(ctx
, NULL
, &ti_mbc
);
187 /* Put the LMv2, NTLMv2 responses (as mbdata). */
188 err
= ntlm_put_v2_responses(ctx
, &ti_mbc
, &lm_mbc
, &nt_mbc
);
192 if ((ctx
->ct_vcflags
& SMBV_WILL_SIGN
) != 0 &&
193 (ctx
->ct_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
) == 0) {
194 err
= ntlm_build_mac_key(ctx
, &nt_mbc
);
197 /* OK, start signing! */
198 ctx
->ct_hflags2
|= SMB_FLAGS2_SECURITY_SIGNATURE
;
201 err
= smb__ssnsetup(ctx
, &lm_mbc
, &nt_mbc
, &ntstatus
, &action
);
205 DPRINT("status 0x%x action 0x%x", ntstatus
, (int)action
);
218 smb_ssnsetup_spnego(struct smb_ctx
*ctx
, struct mbdata
*hint_mb
)
220 struct mbdata send_mb
, recv_mb
;
225 err
= ssp_ctx_create_client(ctx
, hint_mb
);
229 bzero(&send_mb
, sizeof (send_mb
));
230 bzero(&recv_mb
, sizeof (recv_mb
));
232 /* NULL input indicates first call. */
233 err
= ssp_ctx_next_token(ctx
, NULL
, &send_mb
);
238 err
= smb__ssnsetup(ctx
, &send_mb
, &recv_mb
,
243 break; /* normal loop termination */
244 if (ntstatus
!= NT_STATUS_MORE_PROCESSING_REQUIRED
) {
249 /* middle calls get both in, out */
250 err
= ssp_ctx_next_token(ctx
, &recv_mb
, &send_mb
);
254 DPRINT("status 0x%x action 0x%x", ntstatus
, (int)action
);
256 /* NULL output indicates last call. */
257 (void) ssp_ctx_next_token(ctx
, &recv_mb
, NULL
);
260 ssp_ctx_destroy(ctx
);
266 * Session Setup function used for all the forms we support.
267 * To allow this sharing, the crypto stuff is computed by
268 * callers and passed in as mbdata chains. Also, the args
269 * have different meanings for extended security vs. old.
270 * Some may be used as either IN or OUT parameters.
272 * For NTLM (v1, v2), all parameters are inputs
273 * mbc1: [in] LM password hash
274 * mbc2: [in] NT password hash
275 * For Extended security (spnego)
276 * mbc1: [in] outgoing blob data
277 * mbc2: [out] received blob data
278 * For both forms, these are optional:
279 * statusp: [out] NT status
280 * actionp: [out] Logon Action (i.e. SMB_ACT_GUEST)
283 smb__ssnsetup(struct smb_ctx
*ctx
,
284 struct mbdata
*mbc1
, struct mbdata
*mbc2
,
285 uint32_t *statusp
, uint16_t *actionp
)
287 static const char NativeOS
[] = "Solaris";
288 static const char LanMan
[] = "NETSMB";
289 struct smb_sopt
*sv
= &ctx
->ct_sopt
;
290 struct smb_iods
*is
= &ctx
->ct_iods
;
291 struct smb_rq
*rqp
= NULL
;
296 uint16_t bc
, len1
, len2
, sblen
;
299 caps
= ctx
->ct_clnt_caps
;
300 uc
= ctx
->ct_hflags2
& SMB_FLAGS2_UNICODE
;
302 err
= smb_rq_init(ctx
, SMB_COM_SESSION_SETUP_ANDX
, &rqp
);
307 * Build the SMB request.
311 mb_put_uint16le(mbp
, 0xff); /* 0: AndXCommand */
312 mb_put_uint16le(mbp
, 0); /* 1: AndXOffset */
313 mb_put_uint16le(mbp
, sv
->sv_maxtx
); /* 2: MaxBufferSize */
314 mb_put_uint16le(mbp
, sv
->sv_maxmux
); /* 3: MaxMpxCount */
315 mb_put_uint16le(mbp
, 1); /* 4: VcNumber */
316 mb_put_uint32le(mbp
, sv
->sv_skey
); /* 5,6: Session Key */
318 if (caps
& SMB_CAP_EXT_SECURITY
) {
319 len1
= mbc1
? mbc1
->mb_count
: 0;
320 mb_put_uint16le(mbp
, len1
); /* 7: Sec. Blob Len */
321 mb_put_uint32le(mbp
, 0); /* 8,9: reserved */
322 mb_put_uint32le(mbp
, caps
); /* 10,11: Capabilities */
323 smb_rq_wend(rqp
); /* 12: Byte Count */
325 if (mbc1
&& mbc1
->mb_top
) {
326 mb_put_mbuf(mbp
, mbc1
->mb_top
); /* sec. blob */
327 mbc1
->mb_top
= NULL
; /* consumed */
329 /* mbc2 is required below */
335 len1
= mbc1
? mbc1
->mb_count
: 0;
336 len2
= mbc2
? mbc2
->mb_count
: 0;
337 mb_put_uint16le(mbp
, len1
); /* 7: LM pass. len */
338 mb_put_uint16le(mbp
, len2
); /* 8: NT pass. len */
339 mb_put_uint32le(mbp
, 0); /* 9,10: reserved */
340 mb_put_uint32le(mbp
, caps
); /* 11,12: Capabilities */
341 smb_rq_wend(rqp
); /* 13: Byte Count */
343 if (mbc1
&& mbc1
->mb_top
) {
344 mb_put_mbuf(mbp
, mbc1
->mb_top
); /* LM password */
345 mbc1
->mb_top
= NULL
; /* consumed */
347 if (mbc2
&& mbc2
->mb_top
) {
348 mb_put_mbuf(mbp
, mbc2
->mb_top
); /* NT password */
349 mbc2
->mb_top
= NULL
; /* consumed */
351 mb_put_string(mbp
, ctx
->ct_user
, uc
);
352 mb_put_string(mbp
, ctx
->ct_domain
, uc
);
354 mb_put_string(mbp
, NativeOS
, uc
);
355 mb_put_string(mbp
, LanMan
, uc
);
358 err
= smb_rq_internal(ctx
, rqp
);
363 *statusp
= rqp
->rq_status
;
366 * If we have a real error, the response probably has
367 * no more data, so don't try to parse any more.
368 * Note: err=0, means rq_status is valid.
370 if (rqp
->rq_status
!= 0 &&
371 rqp
->rq_status
!= NT_STATUS_MORE_PROCESSING_REQUIRED
) {
378 uc
= rqp
->rq_hflags2
& SMB_FLAGS2_UNICODE
;
379 is
->is_smbuid
= rqp
->rq_uid
;
382 err
= md_get_uint8(mbp
, &wc
);
386 err
= EBADRPC
; /* for any problems in this section */
387 if (caps
& SMB_CAP_EXT_SECURITY
) {
390 md_get_uint16le(mbp
, NULL
); /* secondary cmd */
391 md_get_uint16le(mbp
, NULL
); /* andxoffset */
392 md_get_uint16le(mbp
, actionp
); /* action */
393 md_get_uint16le(mbp
, &sblen
); /* sec. blob len */
394 md_get_uint16le(mbp
, &bc
); /* byte count */
396 * Get the security blob, after
397 * sanity-checking the length.
399 if (sblen
== 0 || bc
< sblen
)
401 err
= md_get_mbuf(mbp
, sblen
, &m
);
405 mbc2
->mb_count
= sblen
;
409 md_get_uint16le(mbp
, NULL
); /* secondary cmd */
410 md_get_uint16le(mbp
, NULL
); /* andxoffset */
411 md_get_uint16le(mbp
, actionp
); /* action */
412 err
= md_get_uint16le(mbp
, &bc
); /* byte count */
418 * Native OS, LANMGR, & Domain follow here.
419 * Parse these strings and store for later.
420 * If unicode, they should be aligned.
422 * Note that with Extended security, we may use
423 * multiple calls to this function. Only parse
424 * these strings on the last one (status == 0).
425 * Ditto for the CAP_LARGE work-around.
427 if (rqp
->rq_status
!= 0)
430 /* Ignore any parsing errors for these strings. */
431 err
= md_get_string(mbp
, &ctx
->ct_srv_OS
, uc
);
432 DPRINT("server OS: %s", err
? "?" : ctx
->ct_srv_OS
);
433 err
= md_get_string(mbp
, &ctx
->ct_srv_LM
, uc
);
434 DPRINT("server LM: %s", err
? "?" : ctx
->ct_srv_LM
);
436 * There's sometimes a server domain folloing
437 * at this point, but we don't need it.
440 /* Success! (See Ignore any ... above) */
444 * Windows systems don't suport CAP_LARGE_READX,WRITEX
445 * when signing is enabled, so adjust sv_caps.
447 if (ctx
->ct_srv_OS
&&
448 0 == strncmp(ctx
->ct_srv_OS
, "Windows ", 8)) {
449 DPRINT("Server is Windows");
450 if (ctx
->ct_vcflags
& SMBV_WILL_SIGN
) {
451 DPRINT("disable CAP_LARGE_(r/w)");
452 ctx
->ct_sopt
.sv_caps
&=
453 ~(SMB_CAP_LARGE_READX
| SMB_CAP_LARGE_WRITEX
);