2 Unix SMB/CIFS implementation.
4 User credentials handling
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_internal.h"
28 #include "auth/gensec/gensec.h"
29 #include "libcli/auth/libcli_auth.h"
31 #include "param/param.h"
32 #include "system/filesys.h"
35 * Create a new credentials structure
36 * @param mem_ctx TALLOC_CTX parent for credentials structure
38 _PUBLIC_
struct cli_credentials
*cli_credentials_init(TALLOC_CTX
*mem_ctx
)
40 struct cli_credentials
*cred
= talloc_zero(mem_ctx
, struct cli_credentials
);
45 cred
->winbind_separator
= '\\';
50 _PUBLIC_
void cli_credentials_set_callback_data(struct cli_credentials
*cred
,
53 cred
->priv_data
= callback_data
;
56 _PUBLIC_
void *_cli_credentials_callback_data(struct cli_credentials
*cred
)
58 return cred
->priv_data
;
62 * Create a new anonymous credential
63 * @param mem_ctx TALLOC_CTX parent for credentials structure
65 _PUBLIC_
struct cli_credentials
*cli_credentials_init_anon(TALLOC_CTX
*mem_ctx
)
67 struct cli_credentials
*anon_credentials
;
69 anon_credentials
= cli_credentials_init(mem_ctx
);
70 cli_credentials_set_anonymous(anon_credentials
);
72 return anon_credentials
;
75 _PUBLIC_
void cli_credentials_set_kerberos_state(struct cli_credentials
*creds
,
76 enum credentials_use_kerberos use_kerberos
)
78 creds
->use_kerberos
= use_kerberos
;
81 _PUBLIC_
void cli_credentials_set_forced_sasl_mech(struct cli_credentials
*creds
,
82 const char *sasl_mech
)
84 TALLOC_FREE(creds
->forced_sasl_mech
);
85 creds
->forced_sasl_mech
= talloc_strdup(creds
, sasl_mech
);
88 _PUBLIC_
void cli_credentials_set_krb_forwardable(struct cli_credentials
*creds
,
89 enum credentials_krb_forwardable krb_forwardable
)
91 creds
->krb_forwardable
= krb_forwardable
;
94 _PUBLIC_
enum credentials_use_kerberos
cli_credentials_get_kerberos_state(struct cli_credentials
*creds
)
96 return creds
->use_kerberos
;
99 _PUBLIC_
const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials
*creds
)
101 return creds
->forced_sasl_mech
;
104 _PUBLIC_
enum credentials_krb_forwardable
cli_credentials_get_krb_forwardable(struct cli_credentials
*creds
)
106 return creds
->krb_forwardable
;
109 _PUBLIC_
void cli_credentials_set_gensec_features(struct cli_credentials
*creds
, uint32_t gensec_features
)
111 creds
->gensec_features
= gensec_features
;
114 _PUBLIC_
uint32_t cli_credentials_get_gensec_features(struct cli_credentials
*creds
)
116 return creds
->gensec_features
;
121 * Obtain the username for this credentials context.
122 * @param cred credentials context
123 * @retval The username set on this context.
124 * @note Return value will never be NULL except by programmer error.
126 _PUBLIC_
const char *cli_credentials_get_username(struct cli_credentials
*cred
)
128 if (cred
->machine_account_pending
) {
129 cli_credentials_set_machine_account(cred
,
130 cred
->machine_account_pending_lp_ctx
);
133 if (cred
->username_obtained
== CRED_CALLBACK
&&
134 !cred
->callback_running
) {
135 cred
->callback_running
= true;
136 cred
->username
= cred
->username_cb(cred
);
137 cred
->callback_running
= false;
138 if (cred
->username_obtained
== CRED_CALLBACK
) {
139 cred
->username_obtained
= CRED_CALLBACK_RESULT
;
140 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
144 return cred
->username
;
147 _PUBLIC_
bool cli_credentials_set_username(struct cli_credentials
*cred
,
148 const char *val
, enum credentials_obtained obtained
)
150 if (obtained
>= cred
->username_obtained
) {
151 cred
->username
= talloc_strdup(cred
, val
);
152 cred
->username_obtained
= obtained
;
153 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
160 _PUBLIC_
bool cli_credentials_set_username_callback(struct cli_credentials
*cred
,
161 const char *(*username_cb
) (struct cli_credentials
*))
163 if (cred
->username_obtained
< CRED_CALLBACK
) {
164 cred
->username_cb
= username_cb
;
165 cred
->username_obtained
= CRED_CALLBACK
;
172 _PUBLIC_
bool cli_credentials_set_bind_dn(struct cli_credentials
*cred
,
175 cred
->bind_dn
= talloc_strdup(cred
, bind_dn
);
180 * Obtain the BIND DN for this credentials context.
181 * @param cred credentials context
182 * @retval The username set on this context.
183 * @note Return value will be NULL if not specified explictly
185 _PUBLIC_
const char *cli_credentials_get_bind_dn(struct cli_credentials
*cred
)
187 return cred
->bind_dn
;
192 * Obtain the client principal for this credentials context.
193 * @param cred credentials context
194 * @retval The username set on this context.
195 * @note Return value will never be NULL except by programmer error.
197 _PUBLIC_
char *cli_credentials_get_principal_and_obtained(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
, enum credentials_obtained
*obtained
)
199 if (cred
->machine_account_pending
) {
200 cli_credentials_set_machine_account(cred
,
201 cred
->machine_account_pending_lp_ctx
);
204 if (cred
->principal_obtained
== CRED_CALLBACK
&&
205 !cred
->callback_running
) {
206 cred
->callback_running
= true;
207 cred
->principal
= cred
->principal_cb(cred
);
208 cred
->callback_running
= false;
209 if (cred
->principal_obtained
== CRED_CALLBACK
) {
210 cred
->principal_obtained
= CRED_CALLBACK_RESULT
;
211 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
215 if (cred
->principal_obtained
< cred
->username_obtained
216 || cred
->principal_obtained
< MAX(cred
->domain_obtained
, cred
->realm_obtained
)) {
217 const char *effective_username
= NULL
;
218 const char *effective_realm
= NULL
;
219 enum credentials_obtained effective_obtained
;
221 effective_username
= cli_credentials_get_username(cred
);
222 if (effective_username
== NULL
|| strlen(effective_username
) == 0) {
223 *obtained
= cred
->username_obtained
;
227 if (cred
->domain_obtained
> cred
->realm_obtained
) {
228 effective_realm
= cli_credentials_get_domain(cred
);
229 effective_obtained
= MIN(cred
->domain_obtained
,
230 cred
->username_obtained
);
232 effective_realm
= cli_credentials_get_realm(cred
);
233 effective_obtained
= MIN(cred
->realm_obtained
,
234 cred
->username_obtained
);
237 if (effective_realm
== NULL
|| strlen(effective_realm
) == 0) {
238 effective_realm
= cli_credentials_get_domain(cred
);
239 effective_obtained
= MIN(cred
->domain_obtained
,
240 cred
->username_obtained
);
243 if (effective_realm
!= NULL
&& strlen(effective_realm
) != 0) {
244 *obtained
= effective_obtained
;
245 return talloc_asprintf(mem_ctx
, "%s@%s",
250 *obtained
= cred
->principal_obtained
;
251 return talloc_strdup(mem_ctx
, cred
->principal
);
255 * Obtain the client principal for this credentials context.
256 * @param cred credentials context
257 * @retval The username set on this context.
258 * @note Return value will never be NULL except by programmer error.
260 _PUBLIC_
char *cli_credentials_get_principal(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
)
262 enum credentials_obtained obtained
;
263 return cli_credentials_get_principal_and_obtained(cred
, mem_ctx
, &obtained
);
266 _PUBLIC_
bool cli_credentials_set_principal(struct cli_credentials
*cred
,
268 enum credentials_obtained obtained
)
270 if (obtained
>= cred
->principal_obtained
) {
271 cred
->principal
= talloc_strdup(cred
, val
);
272 if (cred
->principal
== NULL
) {
275 cred
->principal_obtained
= obtained
;
277 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
284 /* Set a callback to get the principal. This could be a popup dialog,
285 * a terminal prompt or similar. */
286 _PUBLIC_
bool cli_credentials_set_principal_callback(struct cli_credentials
*cred
,
287 const char *(*principal_cb
) (struct cli_credentials
*))
289 if (cred
->principal_obtained
< CRED_CALLBACK
) {
290 cred
->principal_cb
= principal_cb
;
291 cred
->principal_obtained
= CRED_CALLBACK
;
298 /* Some of our tools are 'anonymous by default'. This is a single
299 * function to determine if authentication has been explicitly
302 _PUBLIC_
bool cli_credentials_authentication_requested(struct cli_credentials
*cred
)
304 uint32_t gensec_features
= 0;
311 * If we forced the mech we clearly want authentication. E.g. to use
312 * SASL/EXTERNAL which has no credentials.
314 if (cred
->forced_sasl_mech
) {
318 if (cli_credentials_is_anonymous(cred
)){
322 if (cred
->principal_obtained
>= CRED_SPECIFIED
) {
325 if (cred
->username_obtained
>= CRED_SPECIFIED
) {
329 if (cli_credentials_get_kerberos_state(cred
) == CRED_MUST_USE_KERBEROS
) {
333 gensec_features
= cli_credentials_get_gensec_features(cred
);
334 if (gensec_features
& GENSEC_FEATURE_NTLM_CCACHE
) {
338 if (gensec_features
& GENSEC_FEATURE_SIGN
) {
342 if (gensec_features
& GENSEC_FEATURE_SEAL
) {
350 * Obtain the password for this credentials context.
351 * @param cred credentials context
352 * @retval If set, the cleartext password, otherwise NULL
354 _PUBLIC_
const char *cli_credentials_get_password(struct cli_credentials
*cred
)
356 if (cred
->machine_account_pending
) {
357 cli_credentials_set_machine_account(cred
,
358 cred
->machine_account_pending_lp_ctx
);
361 if (cred
->password_obtained
== CRED_CALLBACK
&&
362 !cred
->callback_running
&&
363 !cred
->password_will_be_nt_hash
) {
364 cred
->callback_running
= true;
365 cred
->password
= cred
->password_cb(cred
);
366 cred
->callback_running
= false;
367 if (cred
->password_obtained
== CRED_CALLBACK
) {
368 cred
->password_obtained
= CRED_CALLBACK_RESULT
;
369 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
373 return cred
->password
;
376 /* Set a password on the credentials context, including an indication
377 * of 'how' the password was obtained */
379 _PUBLIC_
bool cli_credentials_set_password(struct cli_credentials
*cred
,
381 enum credentials_obtained obtained
)
383 if (obtained
>= cred
->password_obtained
) {
385 cred
->lm_response
= data_blob_null
;
386 cred
->nt_response
= data_blob_null
;
387 cred
->nt_hash
= NULL
;
388 cred
->password
= NULL
;
390 cli_credentials_invalidate_ccache(cred
, obtained
);
392 cred
->password_tries
= 0;
395 cred
->password_obtained
= obtained
;
399 if (cred
->password_will_be_nt_hash
) {
400 struct samr_Password
*nt_hash
= NULL
;
401 size_t val_len
= strlen(val
);
404 nt_hash
= talloc(cred
, struct samr_Password
);
405 if (nt_hash
== NULL
) {
409 converted
= strhex_to_str((char *)nt_hash
->hash
,
410 sizeof(nt_hash
->hash
),
412 if (converted
!= sizeof(nt_hash
->hash
)) {
413 TALLOC_FREE(nt_hash
);
417 cred
->nt_hash
= nt_hash
;
418 cred
->password_obtained
= obtained
;
422 cred
->password
= talloc_strdup(cred
, val
);
423 if (cred
->password
== NULL
) {
427 /* Don't print the actual password in talloc memory dumps */
428 talloc_set_name_const(cred
->password
,
429 "password set via cli_credentials_set_password");
430 cred
->password_obtained
= obtained
;
438 _PUBLIC_
bool cli_credentials_set_password_callback(struct cli_credentials
*cred
,
439 const char *(*password_cb
) (struct cli_credentials
*))
441 if (cred
->password_obtained
< CRED_CALLBACK
) {
442 cred
->password_tries
= 3;
443 cred
->password_cb
= password_cb
;
444 cred
->password_obtained
= CRED_CALLBACK
;
445 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
453 * Obtain the 'old' password for this credentials context (used for join accounts).
454 * @param cred credentials context
455 * @retval If set, the cleartext password, otherwise NULL
457 _PUBLIC_
const char *cli_credentials_get_old_password(struct cli_credentials
*cred
)
459 if (cred
->machine_account_pending
) {
460 cli_credentials_set_machine_account(cred
,
461 cred
->machine_account_pending_lp_ctx
);
464 return cred
->old_password
;
467 _PUBLIC_
bool cli_credentials_set_old_password(struct cli_credentials
*cred
,
469 enum credentials_obtained obtained
)
471 cred
->old_password
= talloc_strdup(cred
, val
);
472 if (cred
->old_password
) {
473 /* Don't print the actual password in talloc memory dumps */
474 talloc_set_name_const(cred
->old_password
, "password set via cli_credentials_set_old_password");
476 cred
->old_nt_hash
= NULL
;
481 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
483 * Sometimes we only have this much of the password, while the rest of
484 * the time this call avoids calling E_md4hash themselves.
486 * @param cred credentials context
487 * @retval If set, the cleartext password, otherwise NULL
489 _PUBLIC_
struct samr_Password
*cli_credentials_get_nt_hash(struct cli_credentials
*cred
,
492 enum credentials_obtained password_obtained
;
493 enum credentials_obtained ccache_threshold
;
494 enum credentials_obtained client_gss_creds_threshold
;
495 bool password_is_nt_hash
;
496 const char *password
= NULL
;
497 struct samr_Password
*nt_hash
= NULL
;
499 if (cred
->nt_hash
!= NULL
) {
501 * If we already have a hash it's easy.
507 * This is a bit tricky, with password_will_be_nt_hash
508 * we still need to get the value via the password_callback
509 * but if we did that we should not remember it's state
510 * in the long run so we need to undo it.
513 password_obtained
= cred
->password_obtained
;
514 ccache_threshold
= cred
->ccache_threshold
;
515 client_gss_creds_threshold
= cred
->client_gss_creds_threshold
;
516 password_is_nt_hash
= cred
->password_will_be_nt_hash
;
518 cred
->password_will_be_nt_hash
= false;
519 password
= cli_credentials_get_password(cred
);
521 cred
->password_will_be_nt_hash
= password_is_nt_hash
;
522 if (password_is_nt_hash
&& password_obtained
== CRED_CALLBACK
) {
524 * We got the nt_hash as string via the callback,
525 * so we need to undo the state change.
527 * And also don't remember it as plaintext password.
529 cred
->client_gss_creds_threshold
= client_gss_creds_threshold
;
530 cred
->ccache_threshold
= ccache_threshold
;
531 cred
->password_obtained
= password_obtained
;
532 cred
->password
= NULL
;
535 if (password
== NULL
) {
539 nt_hash
= talloc(cred
, struct samr_Password
);
540 if (nt_hash
== NULL
) {
544 if (password_is_nt_hash
) {
545 size_t password_len
= strlen(password
);
548 converted
= strhex_to_str((char *)nt_hash
->hash
,
549 sizeof(nt_hash
->hash
),
550 password
, password_len
);
551 if (converted
!= sizeof(nt_hash
->hash
)) {
552 TALLOC_FREE(nt_hash
);
556 E_md4hash(password
, nt_hash
->hash
);
559 cred
->nt_hash
= nt_hash
;
563 nt_hash
= talloc(mem_ctx
, struct samr_Password
);
564 if (nt_hash
== NULL
) {
568 *nt_hash
= *cred
->nt_hash
;
574 * Obtain the old password, in the form MD4(unicode(password)) for this credentials context.
576 * Sometimes we only have this much of the password, while the rest of
577 * the time this call avoids calling E_md4hash themselves.
579 * @param cred credentials context
580 * @retval If set, the cleartext password, otherwise NULL
582 _PUBLIC_
struct samr_Password
*cli_credentials_get_old_nt_hash(struct cli_credentials
*cred
,
585 const char *old_password
= NULL
;
587 if (cred
->old_nt_hash
!= NULL
) {
588 struct samr_Password
*nt_hash
= talloc(mem_ctx
, struct samr_Password
);
593 *nt_hash
= *cred
->old_nt_hash
;
598 old_password
= cli_credentials_get_old_password(cred
);
600 struct samr_Password
*nt_hash
= talloc(mem_ctx
, struct samr_Password
);
605 E_md4hash(old_password
, nt_hash
->hash
);
614 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
615 * @param cred credentials context
616 * @retval The domain set on this context.
617 * @note Return value will never be NULL except by programmer error.
619 _PUBLIC_
const char *cli_credentials_get_domain(struct cli_credentials
*cred
)
621 if (cred
->machine_account_pending
) {
622 cli_credentials_set_machine_account(cred
,
623 cred
->machine_account_pending_lp_ctx
);
626 if (cred
->domain_obtained
== CRED_CALLBACK
&&
627 !cred
->callback_running
) {
628 cred
->callback_running
= true;
629 cred
->domain
= cred
->domain_cb(cred
);
630 cred
->callback_running
= false;
631 if (cred
->domain_obtained
== CRED_CALLBACK
) {
632 cred
->domain_obtained
= CRED_CALLBACK_RESULT
;
633 cli_credentials_invalidate_ccache(cred
, cred
->domain_obtained
);
641 _PUBLIC_
bool cli_credentials_set_domain(struct cli_credentials
*cred
,
643 enum credentials_obtained obtained
)
645 if (obtained
>= cred
->domain_obtained
) {
646 /* it is important that the domain be in upper case,
647 * particularly for the sensitive NTLMv2
649 cred
->domain
= strupper_talloc(cred
, val
);
650 cred
->domain_obtained
= obtained
;
651 /* setting domain does not mean we have to invalidate ccache
652 * because domain in not used for Kerberos operations.
653 * If ccache invalidation is required, one will anyway specify
654 * a password to kinit, and that will force invalidation of the ccache
662 bool cli_credentials_set_domain_callback(struct cli_credentials
*cred
,
663 const char *(*domain_cb
) (struct cli_credentials
*))
665 if (cred
->domain_obtained
< CRED_CALLBACK
) {
666 cred
->domain_cb
= domain_cb
;
667 cred
->domain_obtained
= CRED_CALLBACK
;
675 * Obtain the Kerberos realm for this credentials context.
676 * @param cred credentials context
677 * @retval The realm set on this context.
678 * @note Return value will never be NULL except by programmer error.
680 _PUBLIC_
const char *cli_credentials_get_realm(struct cli_credentials
*cred
)
682 if (cred
->machine_account_pending
) {
683 cli_credentials_set_machine_account(cred
,
684 cred
->machine_account_pending_lp_ctx
);
687 if (cred
->realm_obtained
== CRED_CALLBACK
&&
688 !cred
->callback_running
) {
689 cred
->callback_running
= true;
690 cred
->realm
= cred
->realm_cb(cred
);
691 cred
->callback_running
= false;
692 if (cred
->realm_obtained
== CRED_CALLBACK
) {
693 cred
->realm_obtained
= CRED_CALLBACK_RESULT
;
694 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
702 * Set the realm for this credentials context, and force it to
703 * uppercase for the sanity of our local kerberos libraries
705 _PUBLIC_
bool cli_credentials_set_realm(struct cli_credentials
*cred
,
707 enum credentials_obtained obtained
)
709 if (obtained
>= cred
->realm_obtained
) {
710 cred
->realm
= strupper_talloc(cred
, val
);
711 cred
->realm_obtained
= obtained
;
712 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
719 bool cli_credentials_set_realm_callback(struct cli_credentials
*cred
,
720 const char *(*realm_cb
) (struct cli_credentials
*))
722 if (cred
->realm_obtained
< CRED_CALLBACK
) {
723 cred
->realm_cb
= realm_cb
;
724 cred
->realm_obtained
= CRED_CALLBACK
;
732 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
734 * @param cred credentials context
735 * @retval The workstation name set on this context.
736 * @note Return value will never be NULL except by programmer error.
738 _PUBLIC_
const char *cli_credentials_get_workstation(struct cli_credentials
*cred
)
740 if (cred
->workstation_obtained
== CRED_CALLBACK
&&
741 !cred
->callback_running
) {
742 cred
->callback_running
= true;
743 cred
->workstation
= cred
->workstation_cb(cred
);
744 cred
->callback_running
= false;
745 if (cred
->workstation_obtained
== CRED_CALLBACK
) {
746 cred
->workstation_obtained
= CRED_CALLBACK_RESULT
;
750 return cred
->workstation
;
753 _PUBLIC_
bool cli_credentials_set_workstation(struct cli_credentials
*cred
,
755 enum credentials_obtained obtained
)
757 if (obtained
>= cred
->workstation_obtained
) {
758 cred
->workstation
= talloc_strdup(cred
, val
);
759 cred
->workstation_obtained
= obtained
;
766 bool cli_credentials_set_workstation_callback(struct cli_credentials
*cred
,
767 const char *(*workstation_cb
) (struct cli_credentials
*))
769 if (cred
->workstation_obtained
< CRED_CALLBACK
) {
770 cred
->workstation_cb
= workstation_cb
;
771 cred
->workstation_obtained
= CRED_CALLBACK
;
779 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
781 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
783 * @param credentials Credentials structure on which to set the password
784 * @param data the string containing the username, password etc
785 * @param obtained This enum describes how 'specified' this password is
788 _PUBLIC_
void cli_credentials_parse_string(struct cli_credentials
*credentials
, const char *data
, enum credentials_obtained obtained
)
792 if (strcmp("%",data
) == 0) {
793 cli_credentials_set_anonymous(credentials
);
797 uname
= talloc_strdup(credentials
, data
);
798 if ((p
= strchr_m(uname
,'%'))) {
800 cli_credentials_set_password(credentials
, p
+1, obtained
);
803 if ((p
= strchr_m(uname
,'@'))) {
805 * We also need to set username and domain
806 * in order to undo the effect of
807 * cli_credentials_guess().
809 cli_credentials_set_username(credentials
, uname
, obtained
);
810 cli_credentials_set_domain(credentials
, "", obtained
);
812 cli_credentials_set_principal(credentials
, uname
, obtained
);
814 cli_credentials_set_realm(credentials
, p
+1, obtained
);
816 } else if ((p
= strchr_m(uname
,'\\'))
817 || (p
= strchr_m(uname
, '/'))
818 || (p
= strchr_m(uname
, credentials
->winbind_separator
)))
820 const char *domain
= NULL
;
826 if (obtained
== credentials
->realm_obtained
&&
827 !strequal_m(credentials
->domain
, domain
))
830 * We need to undo a former set with the same level
831 * in order to get the expected result from
832 * cli_credentials_get_principal().
834 * But we only need to do that if the domain
837 cli_credentials_set_realm(credentials
, domain
, obtained
);
839 cli_credentials_set_domain(credentials
, domain
, obtained
);
841 if (obtained
== credentials
->principal_obtained
&&
842 !strequal_m(credentials
->username
, uname
))
845 * We need to undo a former set with the same level
846 * in order to get the expected result from
847 * cli_credentials_get_principal().
849 * But we only need to do that if the username
852 credentials
->principal_obtained
= CRED_UNINITIALISED
;
853 credentials
->principal
= NULL
;
855 cli_credentials_set_username(credentials
, uname
, obtained
);
859 * Given a a credentials structure, print it as a string
861 * The format output is [domain\\]user[%password] or user[@realm][%password]
863 * @param credentials Credentials structure on which to set the password
864 * @param mem_ctx The memory context to place the result on
867 _PUBLIC_
char *cli_credentials_get_unparsed_name(struct cli_credentials
*credentials
, TALLOC_CTX
*mem_ctx
)
869 const char *bind_dn
= cli_credentials_get_bind_dn(credentials
);
870 const char *domain
= NULL
;
871 const char *username
= NULL
;
875 name
= talloc_strdup(mem_ctx
, bind_dn
);
877 cli_credentials_get_ntlm_username_domain(credentials
, mem_ctx
, &username
, &domain
);
878 if (domain
&& domain
[0]) {
879 name
= talloc_asprintf(mem_ctx
, "%s\\%s",
882 name
= talloc_asprintf(mem_ctx
, "%s",
890 * Specifies default values for domain, workstation and realm
891 * from the smb.conf configuration file
893 * @param cred Credentials structure to fill in
895 _PUBLIC_
void cli_credentials_set_conf(struct cli_credentials
*cred
,
896 struct loadparm_context
*lp_ctx
)
898 const char *sep
= NULL
;
899 const char *realm
= lpcfg_realm(lp_ctx
);
901 cli_credentials_set_username(cred
, "", CRED_UNINITIALISED
);
902 if (lpcfg_parm_is_cmdline(lp_ctx
, "workgroup")) {
903 cli_credentials_set_domain(cred
, lpcfg_workgroup(lp_ctx
), CRED_SPECIFIED
);
905 cli_credentials_set_domain(cred
, lpcfg_workgroup(lp_ctx
), CRED_UNINITIALISED
);
907 if (lpcfg_parm_is_cmdline(lp_ctx
, "netbios name")) {
908 cli_credentials_set_workstation(cred
, lpcfg_netbios_name(lp_ctx
), CRED_SPECIFIED
);
910 cli_credentials_set_workstation(cred
, lpcfg_netbios_name(lp_ctx
), CRED_UNINITIALISED
);
912 if (realm
!= NULL
&& strlen(realm
) == 0) {
915 if (lpcfg_parm_is_cmdline(lp_ctx
, "realm")) {
916 cli_credentials_set_realm(cred
, realm
, CRED_SPECIFIED
);
918 cli_credentials_set_realm(cred
, realm
, CRED_UNINITIALISED
);
921 sep
= lpcfg_winbind_separator(lp_ctx
);
922 if (sep
!= NULL
&& sep
[0] != '\0') {
923 cred
->winbind_separator
= *lpcfg_winbind_separator(lp_ctx
);
928 * Guess defaults for credentials from environment variables,
929 * and from the configuration file
931 * @param cred Credentials structure to fill in
933 _PUBLIC_
void cli_credentials_guess(struct cli_credentials
*cred
,
934 struct loadparm_context
*lp_ctx
)
937 const char *error_string
;
939 if (lp_ctx
!= NULL
) {
940 cli_credentials_set_conf(cred
, lp_ctx
);
943 if (getenv("LOGNAME")) {
944 cli_credentials_set_username(cred
, getenv("LOGNAME"), CRED_GUESS_ENV
);
947 if (getenv("USER")) {
948 cli_credentials_parse_string(cred
, getenv("USER"), CRED_GUESS_ENV
);
949 if ((p
= strchr_m(getenv("USER"),'%'))) {
950 memset(p
,0,strlen(cred
->password
));
954 if (getenv("PASSWD")) {
955 cli_credentials_set_password(cred
, getenv("PASSWD"), CRED_GUESS_ENV
);
958 if (getenv("PASSWD_FD")) {
959 cli_credentials_parse_password_fd(cred
, atoi(getenv("PASSWD_FD")),
963 p
= getenv("PASSWD_FILE");
965 cli_credentials_parse_password_file(cred
, p
, CRED_GUESS_FILE
);
968 if (cli_credentials_get_kerberos_state(cred
) != CRED_DONT_USE_KERBEROS
) {
969 cli_credentials_set_ccache(cred
, lp_ctx
, NULL
, CRED_GUESS_FILE
,
975 * Attach NETLOGON credentials for use with SCHANNEL
978 _PUBLIC_
void cli_credentials_set_netlogon_creds(
979 struct cli_credentials
*cred
,
980 const struct netlogon_creds_CredentialState
*netlogon_creds
)
982 TALLOC_FREE(cred
->netlogon_creds
);
983 if (netlogon_creds
== NULL
) {
986 cred
->netlogon_creds
= netlogon_creds_copy(cred
, netlogon_creds
);
990 * Return attached NETLOGON credentials
993 _PUBLIC_
struct netlogon_creds_CredentialState
*cli_credentials_get_netlogon_creds(struct cli_credentials
*cred
)
995 return cred
->netlogon_creds
;
999 * Set NETLOGON secure channel type
1002 _PUBLIC_
void cli_credentials_set_secure_channel_type(struct cli_credentials
*cred
,
1003 enum netr_SchannelType secure_channel_type
)
1005 cred
->secure_channel_type
= secure_channel_type
;
1009 * Return NETLOGON secure chanel type
1012 _PUBLIC_
time_t cli_credentials_get_password_last_changed_time(struct cli_credentials
*cred
)
1014 return cred
->password_last_changed_time
;
1018 * Set NETLOGON secure channel type
1021 _PUBLIC_
void cli_credentials_set_password_last_changed_time(struct cli_credentials
*cred
,
1022 time_t last_changed_time
)
1024 cred
->password_last_changed_time
= last_changed_time
;
1028 * Return NETLOGON secure chanel type
1031 _PUBLIC_
enum netr_SchannelType
cli_credentials_get_secure_channel_type(struct cli_credentials
*cred
)
1033 return cred
->secure_channel_type
;
1037 * Fill in a credentials structure as the anonymous user
1039 _PUBLIC_
void cli_credentials_set_anonymous(struct cli_credentials
*cred
)
1041 cli_credentials_set_username(cred
, "", CRED_SPECIFIED
);
1042 cli_credentials_set_domain(cred
, "", CRED_SPECIFIED
);
1043 cli_credentials_set_password(cred
, NULL
, CRED_SPECIFIED
);
1044 cli_credentials_set_principal(cred
, NULL
, CRED_SPECIFIED
);
1045 cli_credentials_set_realm(cred
, NULL
, CRED_SPECIFIED
);
1046 cli_credentials_set_workstation(cred
, "", CRED_UNINITIALISED
);
1047 cli_credentials_set_kerberos_state(cred
, CRED_DONT_USE_KERBEROS
);
1051 * Describe a credentials context as anonymous or authenticated
1052 * @retval true if anonymous, false if a username is specified
1055 _PUBLIC_
bool cli_credentials_is_anonymous(struct cli_credentials
*cred
)
1057 const char *username
;
1059 /* if bind dn is set it's not anonymous */
1060 if (cred
->bind_dn
) {
1064 if (cred
->machine_account_pending
) {
1065 cli_credentials_set_machine_account(cred
,
1066 cred
->machine_account_pending_lp_ctx
);
1069 /* if principal is set, it's not anonymous */
1070 if ((cred
->principal
!= NULL
) && cred
->principal_obtained
>= cred
->username_obtained
) {
1074 username
= cli_credentials_get_username(cred
);
1076 /* Yes, it is deliberate that we die if we have a NULL pointer
1077 * here - anonymous is "", not NULL, which is 'never specified,
1078 * never guessed', ie programmer bug */
1087 * Mark the current password for a credentials struct as wrong. This will
1088 * cause the password to be prompted again (if a callback is set).
1090 * This will decrement the number of times the password can be tried.
1092 * @retval whether the credentials struct is finished
1094 _PUBLIC_
bool cli_credentials_wrong_password(struct cli_credentials
*cred
)
1096 if (cred
->password_obtained
!= CRED_CALLBACK_RESULT
) {
1100 if (cred
->password_tries
== 0) {
1104 cred
->password_tries
--;
1106 if (cred
->password_tries
== 0) {
1110 cred
->password_obtained
= CRED_CALLBACK
;
1114 _PUBLIC_
void cli_credentials_get_ntlm_username_domain(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
,
1115 const char **username
,
1116 const char **domain
)
1118 if (cred
->principal_obtained
> cred
->username_obtained
) {
1119 *domain
= talloc_strdup(mem_ctx
, "");
1120 *username
= cli_credentials_get_principal(cred
, mem_ctx
);
1122 *domain
= cli_credentials_get_domain(cred
);
1123 *username
= cli_credentials_get_username(cred
);
1128 * Read a named file, and parse it for username, domain, realm and password
1130 * @param credentials Credentials structure on which to set the password
1131 * @param file a named file to read the details from
1132 * @param obtained This enum describes how 'specified' this password is
1135 _PUBLIC_
bool cli_credentials_parse_file(struct cli_credentials
*cred
, const char *file
, enum credentials_obtained obtained
)
1138 char *ptr
, *val
, *param
;
1141 const char *realm
= NULL
;
1142 const char *domain
= NULL
;
1143 const char *password
= NULL
;
1144 const char *username
= NULL
;
1146 lines
= file_lines_load(file
, &numlines
, 0, NULL
);
1150 /* fail if we can't open the credentials file */
1151 d_printf("ERROR: Unable to open credentials file!\n");
1155 for (i
= 0; i
< numlines
; i
++) {
1156 len
= strlen(lines
[i
]);
1161 /* break up the line into parameter & value.
1162 * will need to eat a little whitespace possibly */
1164 if (!(ptr
= strchr_m (lines
[i
], '=')))
1170 /* eat leading white space */
1171 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
1174 if (strwicmp("password", param
) == 0) {
1176 } else if (strwicmp("username", param
) == 0) {
1178 } else if (strwicmp("domain", param
) == 0) {
1180 } else if (strwicmp("realm", param
) == 0) {
1185 * We need to readd '=' in order to let
1186 * the strlen() work in the last loop
1187 * that clears the memory.
1192 if (realm
!= NULL
&& strlen(realm
) != 0) {
1194 * only overwrite with a valid string
1196 cli_credentials_set_realm(cred
, realm
, obtained
);
1199 if (domain
!= NULL
&& strlen(domain
) != 0) {
1201 * only overwrite with a valid string
1203 cli_credentials_set_domain(cred
, domain
, obtained
);
1206 if (password
!= NULL
) {
1210 cli_credentials_set_password(cred
, password
, obtained
);
1213 if (username
!= NULL
) {
1215 * The last "username" line takes preference
1216 * if the string also contains domain, realm or
1219 cli_credentials_parse_string(cred
, username
, obtained
);
1222 for (i
= 0; i
< numlines
; i
++) {
1223 len
= strlen(lines
[i
]);
1224 memset(lines
[i
], 0, len
);
1232 * Read a named file, and parse it for a password
1234 * @param credentials Credentials structure on which to set the password
1235 * @param file a named file to read the password from
1236 * @param obtained This enum describes how 'specified' this password is
1239 _PUBLIC_
bool cli_credentials_parse_password_file(struct cli_credentials
*credentials
, const char *file
, enum credentials_obtained obtained
)
1241 int fd
= open(file
, O_RDONLY
, 0);
1245 fprintf(stderr
, "Error opening password file %s: %s\n",
1246 file
, strerror(errno
));
1250 ret
= cli_credentials_parse_password_fd(credentials
, fd
, obtained
);
1259 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1261 * @param credentials Credentials structure on which to set the password
1262 * @param fd open file descriptor to read the password from
1263 * @param obtained This enum describes how 'specified' this password is
1266 _PUBLIC_
bool cli_credentials_parse_password_fd(struct cli_credentials
*credentials
,
1267 int fd
, enum credentials_obtained obtained
)
1272 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
1273 p
&& p
- pass
< sizeof(pass
);) {
1274 switch (read(fd
, p
, 1)) {
1276 if (*p
!= '\n' && *p
!= '\0') {
1277 *++p
= '\0'; /* advance p, and null-terminate pass */
1284 *p
= '\0'; /* null-terminate it, just in case... */
1285 p
= NULL
; /* then force the loop condition to become false */
1290 "Error reading password from file descriptor "
1291 "%d: empty password\n",
1296 fprintf(stderr
, "Error reading password from file descriptor %d: %s\n",
1297 fd
, strerror(errno
));
1302 cli_credentials_set_password(credentials
, pass
, obtained
);
1308 * Encrypt a data blob using the session key and the negotiated encryption
1311 * @param state Credential state, contains the session key and algorithm
1312 * @param data Data blob containing the data to be encrypted.
1315 _PUBLIC_ NTSTATUS
netlogon_creds_session_encrypt(
1316 struct netlogon_creds_CredentialState
*state
,
1319 if (data
.data
== NULL
|| data
.length
== 0) {
1320 DBG_ERR("Nothing to encrypt "
1321 "data.data == NULL or data.length == 0");
1322 return NT_STATUS_INVALID_PARAMETER
;
1325 * Don't crypt an all-zero password it will give away the
1326 * NETLOGON pipe session key .
1328 if (all_zero(data
.data
, data
.length
)) {
1329 DBG_ERR("Supplied data all zeros, could leak session key");
1330 return NT_STATUS_INVALID_PARAMETER
;
1332 if (state
->negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
1333 netlogon_creds_aes_encrypt(state
,
1336 } else if (state
->negotiate_flags
& NETLOGON_NEG_ARCFOUR
) {
1337 netlogon_creds_arcfour_crypt(state
,
1341 DBG_ERR("Unsupported encryption option negotiated");
1342 return NT_STATUS_NOT_SUPPORTED
;
1344 return NT_STATUS_OK
;