2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan (metze) Metzmacher 2003
5 Copyright (C) Jeremy Allison 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /****************************************************************************
25 Get UNIX extensions version info.
26 ****************************************************************************/
28 BOOL
cli_unix_extensions_version(struct cli_state
*cli
, uint16
*pmajor
, uint16
*pminor
,
29 uint32
*pcaplow
, uint32
*pcaphigh
)
34 char *rparam
=NULL
, *rdata
=NULL
;
35 unsigned int rparam_count
=0, rdata_count
=0;
37 setup
= TRANSACT2_QFSINFO
;
39 SSVAL(param
,0,SMB_QUERY_CIFS_UNIX_INFO
);
41 if (!cli_send_trans(cli
, SMBtrans2
,
50 if (!cli_receive_trans(cli
, SMBtrans2
,
51 &rparam
, &rparam_count
,
52 &rdata
, &rdata_count
)) {
56 if (cli_is_error(cli
)) {
63 if (rdata_count
< 12) {
67 *pmajor
= SVAL(rdata
,0);
68 *pminor
= SVAL(rdata
,2);
69 cli
->posix_capabilities
= *pcaplow
= IVAL(rdata
,4);
70 *pcaphigh
= IVAL(rdata
,8);
72 /* todo: but not yet needed
73 * return the other stuff
83 /****************************************************************************
84 Set UNIX extensions capabilities.
85 ****************************************************************************/
87 BOOL
cli_set_unix_extensions_capabilities(struct cli_state
*cli
, uint16 major
, uint16 minor
,
88 uint32 caplow
, uint32 caphigh
)
94 char *rparam
=NULL
, *rdata
=NULL
;
95 unsigned int rparam_count
=0, rdata_count
=0;
97 setup
= TRANSACT2_SETFSINFO
;
100 SSVAL(param
,2,SMB_SET_CIFS_UNIX_INFO
);
104 SIVAL(data
,4,caplow
);
105 SIVAL(data
,8,caphigh
);
107 if (!cli_send_trans(cli
, SMBtrans2
,
116 if (!cli_receive_trans(cli
, SMBtrans2
,
117 &rparam
, &rparam_count
,
118 &rdata
, &rdata_count
)) {
122 if (cli_is_error(cli
)) {
136 BOOL
cli_get_fs_attr_info(struct cli_state
*cli
, uint32
*fs_attr
)
141 char *rparam
=NULL
, *rdata
=NULL
;
142 unsigned int rparam_count
=0, rdata_count
=0;
145 smb_panic("cli_get_fs_attr_info() called with NULL Pionter!");
147 setup
= TRANSACT2_QFSINFO
;
149 SSVAL(param
,0,SMB_QUERY_FS_ATTRIBUTE_INFO
);
151 if (!cli_send_trans(cli
, SMBtrans2
,
160 if (!cli_receive_trans(cli
, SMBtrans2
,
161 &rparam
, &rparam_count
,
162 &rdata
, &rdata_count
)) {
166 if (cli_is_error(cli
)) {
173 if (rdata_count
< 12) {
177 *fs_attr
= IVAL(rdata
,0);
179 /* todo: but not yet needed
180 * return the other stuff
190 BOOL
cli_get_fs_volume_info_old(struct cli_state
*cli
, fstring volume_name
, uint32
*pserial_number
)
195 char *rparam
=NULL
, *rdata
=NULL
;
196 unsigned int rparam_count
=0, rdata_count
=0;
199 setup
= TRANSACT2_QFSINFO
;
201 SSVAL(param
,0,SMB_INFO_VOLUME
);
203 if (!cli_send_trans(cli
, SMBtrans2
,
212 if (!cli_receive_trans(cli
, SMBtrans2
,
213 &rparam
, &rparam_count
,
214 &rdata
, &rdata_count
)) {
218 if (cli_is_error(cli
)) {
225 if (rdata_count
< 5) {
229 if (pserial_number
) {
230 *pserial_number
= IVAL(rdata
,0);
232 nlen
= CVAL(rdata
,l2_vol_cch
);
233 clistr_pull(cli
, volume_name
, rdata
+ l2_vol_szVolLabel
, sizeof(fstring
), nlen
, STR_NOALIGN
);
235 /* todo: but not yet needed
236 * return the other stuff
246 BOOL
cli_get_fs_volume_info(struct cli_state
*cli
, fstring volume_name
, uint32
*pserial_number
, time_t *pdate
)
251 char *rparam
=NULL
, *rdata
=NULL
;
252 unsigned int rparam_count
=0, rdata_count
=0;
255 setup
= TRANSACT2_QFSINFO
;
257 SSVAL(param
,0,SMB_QUERY_FS_VOLUME_INFO
);
259 if (!cli_send_trans(cli
, SMBtrans2
,
268 if (!cli_receive_trans(cli
, SMBtrans2
,
269 &rparam
, &rparam_count
,
270 &rdata
, &rdata_count
)) {
274 if (cli_is_error(cli
)) {
281 if (rdata_count
< 19) {
287 ts
= interpret_long_date(rdata
);
290 if (pserial_number
) {
291 *pserial_number
= IVAL(rdata
,8);
293 nlen
= IVAL(rdata
,12);
294 clistr_pull(cli
, volume_name
, rdata
+ 18, sizeof(fstring
), nlen
, STR_UNICODE
);
296 /* todo: but not yet needed
297 * return the other stuff
307 /******************************************************************************
308 Send/receive the request encryption blob.
309 ******************************************************************************/
311 static NTSTATUS
enc_blob_send_receive(struct cli_state
*cli
, DATA_BLOB
*in
, DATA_BLOB
*out
, DATA_BLOB
*param_out
)
315 char *rparam
=NULL
, *rdata
=NULL
;
316 unsigned int rparam_count
=0, rdata_count
=0;
317 NTSTATUS status
= NT_STATUS_OK
;
319 setup
= TRANSACT2_SETFSINFO
;
322 SSVAL(param
,2,SMB_REQUEST_TRANSPORT_ENCRYPTION
);
324 if (!cli_send_trans(cli
, SMBtrans2
,
329 (char *)in
->data
, in
->length
, CLI_BUFFER_SIZE
)) {
330 status
= cli_nt_error(cli
);
334 if (!cli_receive_trans(cli
, SMBtrans2
,
335 &rparam
, &rparam_count
,
336 &rdata
, &rdata_count
)) {
337 status
= cli_nt_error(cli
);
341 if (cli_is_error(cli
)) {
342 status
= cli_nt_error(cli
);
343 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
348 *out
= data_blob(rdata
, rdata_count
);
349 *param_out
= data_blob(rparam
, rparam_count
);
358 /******************************************************************************
359 Make a client state struct.
360 ******************************************************************************/
362 static struct smb_trans_enc_state
*make_cli_enc_state(enum smb_trans_enc_type smb_enc_type
)
364 struct smb_trans_enc_state
*es
= NULL
;
365 es
= SMB_MALLOC_P(struct smb_trans_enc_state
);
370 es
->smb_enc_type
= smb_enc_type
;
372 if (smb_enc_type
== SMB_TRANS_ENC_GSS
) {
373 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
374 es
->s
.gss_state
= SMB_MALLOC_P(struct smb_tran_enc_state_gss
);
375 if (!es
->s
.gss_state
) {
379 ZERO_STRUCTP(es
->s
.gss_state
);
381 DEBUG(0,("make_cli_enc_state: no krb5 compiled.\n"));
389 /******************************************************************************
390 Start a raw ntlmssp encryption.
391 ******************************************************************************/
393 NTSTATUS
cli_raw_ntlm_smb_encryption_start(struct cli_state
*cli
,
398 DATA_BLOB blob_in
= data_blob_null
;
399 DATA_BLOB blob_out
= data_blob_null
;
400 DATA_BLOB param_out
= data_blob_null
;
401 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
402 struct smb_trans_enc_state
*es
= make_cli_enc_state(SMB_TRANS_ENC_NTLM
);
405 return NT_STATUS_NO_MEMORY
;
407 status
= ntlmssp_client_start(&es
->s
.ntlmssp_state
);
408 if (!NT_STATUS_IS_OK(status
)) {
412 ntlmssp_want_feature(es
->s
.ntlmssp_state
, NTLMSSP_FEATURE_SESSION_KEY
);
413 es
->s
.ntlmssp_state
->neg_flags
|= (NTLMSSP_NEGOTIATE_SIGN
|NTLMSSP_NEGOTIATE_SEAL
);
415 if (!NT_STATUS_IS_OK(status
= ntlmssp_set_username(es
->s
.ntlmssp_state
, user
))) {
418 if (!NT_STATUS_IS_OK(status
= ntlmssp_set_domain(es
->s
.ntlmssp_state
, domain
))) {
421 if (!NT_STATUS_IS_OK(status
= ntlmssp_set_password(es
->s
.ntlmssp_state
, pass
))) {
426 status
= ntlmssp_update(es
->s
.ntlmssp_state
, blob_in
, &blob_out
);
427 data_blob_free(&blob_in
);
428 data_blob_free(¶m_out
);
429 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) || NT_STATUS_IS_OK(status
)) {
430 status
= enc_blob_send_receive(cli
, &blob_out
, &blob_in
, ¶m_out
);
432 if (param_out
.length
== 2) {
433 es
->enc_ctx_num
= SVAL(param_out
.data
, 0);
435 data_blob_free(&blob_out
);
436 } while (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
));
438 data_blob_free(&blob_in
);
440 if (NT_STATUS_IS_OK(status
)) {
441 /* Replace the old state, if any. */
442 if (cli
->trans_enc_state
) {
443 common_free_encryption_state(&cli
->trans_enc_state
);
445 cli
->trans_enc_state
= es
;
446 cli
->trans_enc_state
->enc_on
= True
;
452 common_free_encryption_state(&es
);
456 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
458 #ifndef SMB_GSS_REQUIRED_FLAGS
459 #define SMB_GSS_REQUIRED_FLAGS (GSS_C_CONF_FLAG|GSS_C_INTEG_FLAG|GSS_C_MUTUAL_FLAG|GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG)
462 /******************************************************************************
463 Get client gss blob to send to a server.
464 ******************************************************************************/
466 static NTSTATUS
make_cli_gss_blob(struct smb_trans_enc_state
*es
,
470 DATA_BLOB spnego_blob_in
,
471 DATA_BLOB
*p_blob_out
)
473 const char *krb_mechs
[] = {OID_KERBEROS5
, NULL
};
477 gss_buffer_desc input_name
;
478 gss_buffer_desc
*p_tok_in
;
479 gss_buffer_desc tok_out
, tok_in
;
480 DATA_BLOB blob_out
= data_blob_null
;
481 DATA_BLOB blob_in
= data_blob_null
;
482 char *host_princ_s
= NULL
;
483 OM_uint32 ret_flags
= 0;
484 NTSTATUS status
= NT_STATUS_OK
;
486 gss_OID_desc nt_hostbased_service
=
487 {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
489 memset(&tok_out
, '\0', sizeof(tok_out
));
491 /* Get a ticket for the service@host */
492 asprintf(&host_princ_s
, "%s@%s", service
, host
);
493 if (host_princ_s
== NULL
) {
494 return NT_STATUS_NO_MEMORY
;
497 input_name
.value
= host_princ_s
;
498 input_name
.length
= strlen(host_princ_s
) + 1;
500 ret
= gss_import_name(&min
,
502 &nt_hostbased_service
,
505 if (ret
!= GSS_S_COMPLETE
) {
506 SAFE_FREE(host_princ_s
);
507 return map_nt_error_from_gss(ret
, min
);
510 if (spnego_blob_in
.length
== 0) {
511 p_tok_in
= GSS_C_NO_BUFFER
;
513 /* Remove the SPNEGO wrapper */
514 if (!spnego_parse_auth_response(spnego_blob_in
, status_in
, OID_KERBEROS5
, &blob_in
)) {
515 status
= NT_STATUS_UNSUCCESSFUL
;
518 tok_in
.value
= blob_in
.data
;
519 tok_in
.length
= blob_in
.length
;
523 ret
= gss_init_sec_context(&min
,
524 GSS_C_NO_CREDENTIAL
, /* Use our default cred. */
525 &es
->s
.gss_state
->gss_ctx
,
527 GSS_C_NO_OID
, /* default OID. */
528 GSS_C_MUTUAL_FLAG
| GSS_C_REPLAY_FLAG
| GSS_C_SEQUENCE_FLAG
,
529 GSS_C_INDEFINITE
, /* requested ticket lifetime. */
530 NULL
, /* no channel bindings */
532 NULL
, /* ignore mech type */
535 NULL
); /* ignore time_rec */
537 status
= map_nt_error_from_gss(ret
, min
);
538 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
539 ADS_STATUS adss
= ADS_ERROR_GSS(ret
, min
);
540 DEBUG(10,("make_cli_gss_blob: gss_init_sec_context failed with %s\n",
545 if ((ret_flags
& SMB_GSS_REQUIRED_FLAGS
) != SMB_GSS_REQUIRED_FLAGS
) {
546 status
= NT_STATUS_ACCESS_DENIED
;
549 blob_out
= data_blob(tok_out
.value
, tok_out
.length
);
551 /* Wrap in an SPNEGO wrapper */
552 *p_blob_out
= gen_negTokenTarg(krb_mechs
, blob_out
);
556 data_blob_free(&blob_out
);
557 data_blob_free(&blob_in
);
558 SAFE_FREE(host_princ_s
);
559 gss_release_name(&min
, &srv_name
);
561 gss_release_buffer(&min
, &tok_out
);
566 /******************************************************************************
567 Start a SPNEGO gssapi encryption context.
568 ******************************************************************************/
570 NTSTATUS
cli_gss_smb_encryption_start(struct cli_state
*cli
)
572 DATA_BLOB blob_recv
= data_blob_null
;
573 DATA_BLOB blob_send
= data_blob_null
;
574 DATA_BLOB param_out
= data_blob_null
;
575 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
577 const char *servicename
;
578 struct smb_trans_enc_state
*es
= make_cli_enc_state(SMB_TRANS_ENC_GSS
);
581 return NT_STATUS_NO_MEMORY
;
584 name_to_fqdn(fqdn
, cli
->desthost
);
587 servicename
= "cifs";
588 status
= make_cli_gss_blob(es
, servicename
, fqdn
, NT_STATUS_OK
, blob_recv
, &blob_send
);
589 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
590 servicename
= "host";
591 status
= make_cli_gss_blob(es
, servicename
, fqdn
, NT_STATUS_OK
, blob_recv
, &blob_send
);
592 if (!NT_STATUS_EQUAL(status
,NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
598 data_blob_free(&blob_recv
);
599 status
= enc_blob_send_receive(cli
, &blob_send
, &blob_recv
, ¶m_out
);
600 if (param_out
.length
== 2) {
601 es
->enc_ctx_num
= SVAL(param_out
.data
, 0);
603 data_blob_free(&blob_send
);
604 status
= make_cli_gss_blob(es
, servicename
, fqdn
, status
, blob_recv
, &blob_send
);
605 } while (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
));
606 data_blob_free(&blob_recv
);
608 if (NT_STATUS_IS_OK(status
)) {
609 /* Replace the old state, if any. */
610 if (cli
->trans_enc_state
) {
611 common_free_encryption_state(&cli
->trans_enc_state
);
613 cli
->trans_enc_state
= es
;
614 cli
->trans_enc_state
->enc_on
= True
;
620 common_free_encryption_state(&es
);
624 NTSTATUS
cli_gss_smb_encryption_start(struct cli_state
*cli
)
626 return NT_STATUS_NOT_SUPPORTED
;