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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
27 #include "auth/credentials/credentials.h"
28 #include "auth/credentials/credentials_krb5.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "lib/events/events.h"
33 * Create a new credentials structure
34 * @param mem_ctx TALLOC_CTX parent for credentials structure
36 struct cli_credentials
*cli_credentials_init(TALLOC_CTX
*mem_ctx
)
38 struct cli_credentials
*cred
= talloc(mem_ctx
, struct cli_credentials
);
43 cred
->netlogon_creds
= NULL
;
44 cred
->machine_account_pending
= False
;
45 cred
->workstation_obtained
= CRED_UNINITIALISED
;
46 cred
->username_obtained
= CRED_UNINITIALISED
;
47 cred
->password_obtained
= CRED_UNINITIALISED
;
48 cred
->domain_obtained
= CRED_UNINITIALISED
;
49 cred
->realm_obtained
= CRED_UNINITIALISED
;
50 cred
->ccache_obtained
= CRED_UNINITIALISED
;
51 cred
->client_gss_creds_obtained
= CRED_UNINITIALISED
;
52 cred
->server_gss_creds_obtained
= CRED_UNINITIALISED
;
53 cred
->keytab_obtained
= CRED_UNINITIALISED
;
54 cred
->principal_obtained
= CRED_UNINITIALISED
;
56 cred
->ccache_threshold
= CRED_UNINITIALISED
;
57 cred
->client_gss_creds_threshold
= CRED_UNINITIALISED
;
59 cred
->old_password
= NULL
;
60 cred
->smb_krb5_context
= NULL
;
61 cred
->salt_principal
= NULL
;
62 cred
->machine_account
= False
;
67 cred
->callback_running
= False
;
70 cli_credentials_set_kerberos_state(cred
, CRED_AUTO_USE_KERBEROS
);
71 cli_credentials_set_gensec_features(cred
, 0);
77 * Create a new anonymous credential
78 * @param mem_ctx TALLOC_CTX parent for credentials structure
80 struct cli_credentials
*cli_credentials_init_anon(TALLOC_CTX
*mem_ctx
)
82 struct cli_credentials
*anon_credentials
;
84 anon_credentials
= cli_credentials_init(mem_ctx
);
85 cli_credentials_set_conf(anon_credentials
);
86 cli_credentials_set_anonymous(anon_credentials
);
88 return anon_credentials
;
91 void cli_credentials_set_kerberos_state(struct cli_credentials
*creds
,
92 enum credentials_use_kerberos use_kerberos
)
94 creds
->use_kerberos
= use_kerberos
;
97 enum credentials_use_kerberos
cli_credentials_get_kerberos_state(struct cli_credentials
*creds
)
99 return creds
->use_kerberos
;
102 void cli_credentials_set_gensec_features(struct cli_credentials
*creds
, uint32_t gensec_features
)
104 creds
->gensec_features
= gensec_features
;
107 uint32_t cli_credentials_get_gensec_features(struct cli_credentials
*creds
)
109 return creds
->gensec_features
;
114 * Obtain the username for this credentials context.
115 * @param cred credentials context
116 * @retval The username set on this context.
117 * @note Return value will never be NULL except by programmer error.
119 const char *cli_credentials_get_username(struct cli_credentials
*cred
)
121 if (cred
->machine_account_pending
) {
122 cli_credentials_set_machine_account(cred
);
125 if (cred
->username_obtained
== CRED_CALLBACK
&&
126 !cred
->callback_running
) {
127 cred
->callback_running
= True
;
128 cred
->username
= cred
->username_cb(cred
);
129 cred
->callback_running
= False
;
130 cred
->username_obtained
= CRED_SPECIFIED
;
131 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
134 return cred
->username
;
137 BOOL
cli_credentials_set_username(struct cli_credentials
*cred
,
138 const char *val
, enum credentials_obtained obtained
)
140 if (obtained
>= cred
->username_obtained
) {
141 cred
->username
= talloc_strdup(cred
, val
);
142 cred
->username_obtained
= obtained
;
143 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
150 BOOL
cli_credentials_set_username_callback(struct cli_credentials
*cred
,
151 const char *(*username_cb
) (struct cli_credentials
*))
153 if (cred
->username_obtained
< CRED_CALLBACK
) {
154 cred
->username_cb
= username_cb
;
155 cred
->username_obtained
= CRED_CALLBACK
;
162 BOOL
cli_credentials_set_bind_dn(struct cli_credentials
*cred
,
165 cred
->bind_dn
= talloc_strdup(cred
, bind_dn
);
170 * Obtain the BIND DN for this credentials context.
171 * @param cred credentials context
172 * @retval The username set on this context.
173 * @note Return value will be NULL if not specified explictly
175 const char *cli_credentials_get_bind_dn(struct cli_credentials
*cred
)
177 return cred
->bind_dn
;
182 * Obtain the client principal for this credentials context.
183 * @param cred credentials context
184 * @retval The username set on this context.
185 * @note Return value will never be NULL except by programmer error.
187 const char *cli_credentials_get_principal(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
)
189 if (cred
->machine_account_pending
) {
190 cli_credentials_set_machine_account(cred
);
193 if (cred
->principal_obtained
== CRED_CALLBACK
&&
194 !cred
->callback_running
) {
195 cred
->callback_running
= True
;
196 cred
->principal
= cred
->principal_cb(cred
);
197 cred
->callback_running
= False
;
198 cred
->principal_obtained
= CRED_SPECIFIED
;
199 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
202 if (cred
->principal_obtained
< cred
->username_obtained
) {
203 if (cred
->domain_obtained
> cred
->realm_obtained
) {
204 return talloc_asprintf(mem_ctx
, "%s@%s",
205 cli_credentials_get_username(cred
),
206 cli_credentials_get_domain(cred
));
208 return talloc_asprintf(mem_ctx
, "%s@%s",
209 cli_credentials_get_username(cred
),
210 cli_credentials_get_realm(cred
));
213 return talloc_reference(mem_ctx
, cred
->principal
);
216 BOOL
cli_credentials_set_principal(struct cli_credentials
*cred
,
218 enum credentials_obtained obtained
)
220 if (obtained
>= cred
->principal_obtained
) {
221 cred
->principal
= talloc_strdup(cred
, val
);
222 cred
->principal_obtained
= obtained
;
223 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
230 /* Set a callback to get the principal. This could be a popup dialog,
231 * a terminal prompt or similar. */
233 BOOL
cli_credentials_set_principal_callback(struct cli_credentials
*cred
,
234 const char *(*principal_cb
) (struct cli_credentials
*))
236 if (cred
->principal_obtained
< CRED_CALLBACK
) {
237 cred
->principal_cb
= principal_cb
;
238 cred
->principal_obtained
= CRED_CALLBACK
;
245 /* Some of our tools are 'anonymous by default'. This is a single
246 * function to determine if authentication has been explicitly
249 BOOL
cli_credentials_authentication_requested(struct cli_credentials
*cred
)
255 if (cli_credentials_is_anonymous(cred
)){
259 if (cred
->principal_obtained
>= CRED_SPECIFIED
) {
262 if (cred
->username_obtained
>= CRED_SPECIFIED
) {
266 if (cli_credentials_get_kerberos_state(cred
) == CRED_MUST_USE_KERBEROS
) {
274 * Obtain the password for this credentials context.
275 * @param cred credentials context
276 * @retval If set, the cleartext password, otherwise NULL
278 const char *cli_credentials_get_password(struct cli_credentials
*cred
)
280 if (cred
->machine_account_pending
) {
281 cli_credentials_set_machine_account(cred
);
284 if (cred
->password_obtained
== CRED_CALLBACK
&&
285 !cred
->callback_running
) {
286 cred
->callback_running
= True
;
287 cred
->password
= cred
->password_cb(cred
);
288 cred
->callback_running
= False
;
289 cred
->password_obtained
= CRED_CALLBACK_RESULT
;
290 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
293 return cred
->password
;
296 /* Set a password on the credentials context, including an indication
297 * of 'how' the password was obtained */
299 BOOL
cli_credentials_set_password(struct cli_credentials
*cred
,
301 enum credentials_obtained obtained
)
303 if (obtained
>= cred
->password_obtained
) {
304 cred
->password
= talloc_strdup(cred
, val
);
305 cred
->password_obtained
= obtained
;
306 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
308 cred
->nt_hash
= NULL
;
315 BOOL
cli_credentials_set_password_callback(struct cli_credentials
*cred
,
316 const char *(*password_cb
) (struct cli_credentials
*))
318 if (cred
->password_obtained
< CRED_CALLBACK
) {
319 cred
->password_cb
= password_cb
;
320 cred
->password_obtained
= CRED_CALLBACK
;
321 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
329 * Obtain the 'old' password for this credentials context (used for join accounts).
330 * @param cred credentials context
331 * @retval If set, the cleartext password, otherwise NULL
333 const char *cli_credentials_get_old_password(struct cli_credentials
*cred
)
335 if (cred
->machine_account_pending
) {
336 cli_credentials_set_machine_account(cred
);
339 return cred
->old_password
;
342 BOOL
cli_credentials_set_old_password(struct cli_credentials
*cred
,
344 enum credentials_obtained obtained
)
346 cred
->old_password
= talloc_strdup(cred
, val
);
351 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
353 * Sometimes we only have this much of the password, while the rest of
354 * the time this call avoids calling E_md4hash themselves.
356 * @param cred credentials context
357 * @retval If set, the cleartext password, otherwise NULL
359 const struct samr_Password
*cli_credentials_get_nt_hash(struct cli_credentials
*cred
,
362 const char *password
= cli_credentials_get_password(cred
);
365 struct samr_Password
*nt_hash
= talloc(mem_ctx
, struct samr_Password
);
370 E_md4hash(password
, nt_hash
->hash
);
374 return cred
->nt_hash
;
378 BOOL
cli_credentials_set_nt_hash(struct cli_credentials
*cred
,
379 const struct samr_Password
*nt_hash
,
380 enum credentials_obtained obtained
)
382 if (obtained
>= cred
->password_obtained
) {
383 cli_credentials_set_password(cred
, NULL
, obtained
);
385 cred
->nt_hash
= talloc(cred
, struct samr_Password
);
386 *cred
->nt_hash
= *nt_hash
;
388 cred
->nt_hash
= NULL
;
397 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
398 * @param cred credentials context
399 * @retval The domain set on this context.
400 * @note Return value will never be NULL except by programmer error.
402 const char *cli_credentials_get_domain(struct cli_credentials
*cred
)
404 if (cred
->machine_account_pending
) {
405 cli_credentials_set_machine_account(cred
);
408 if (cred
->domain_obtained
== CRED_CALLBACK
&&
409 !cred
->callback_running
) {
410 cred
->callback_running
= True
;
411 cred
->domain
= cred
->domain_cb(cred
);
412 cred
->callback_running
= False
;
413 cred
->domain_obtained
= CRED_SPECIFIED
;
414 cli_credentials_invalidate_ccache(cred
, cred
->domain_obtained
);
421 BOOL
cli_credentials_set_domain(struct cli_credentials
*cred
,
423 enum credentials_obtained obtained
)
425 if (obtained
>= cred
->domain_obtained
) {
426 /* it is important that the domain be in upper case,
427 * particularly for the sensitive NTLMv2
429 cred
->domain
= strupper_talloc(cred
, val
);
430 cred
->domain_obtained
= obtained
;
431 cli_credentials_invalidate_ccache(cred
, cred
->domain_obtained
);
438 BOOL
cli_credentials_set_domain_callback(struct cli_credentials
*cred
,
439 const char *(*domain_cb
) (struct cli_credentials
*))
441 if (cred
->domain_obtained
< CRED_CALLBACK
) {
442 cred
->domain_cb
= domain_cb
;
443 cred
->domain_obtained
= CRED_CALLBACK
;
451 * Obtain the Kerberos realm for this credentials context.
452 * @param cred credentials context
453 * @retval The realm set on this context.
454 * @note Return value will never be NULL except by programmer error.
456 const char *cli_credentials_get_realm(struct cli_credentials
*cred
)
458 if (cred
->machine_account_pending
) {
459 cli_credentials_set_machine_account(cred
);
462 if (cred
->realm_obtained
== CRED_CALLBACK
&&
463 !cred
->callback_running
) {
464 cred
->callback_running
= True
;
465 cred
->realm
= cred
->realm_cb(cred
);
466 cred
->callback_running
= False
;
467 cred
->realm_obtained
= CRED_SPECIFIED
;
468 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
475 * Set the realm for this credentials context, and force it to
476 * uppercase for the sainity of our local kerberos libraries
478 BOOL
cli_credentials_set_realm(struct cli_credentials
*cred
,
480 enum credentials_obtained obtained
)
482 if (obtained
>= cred
->realm_obtained
) {
483 cred
->realm
= strupper_talloc(cred
, val
);
484 cred
->realm_obtained
= obtained
;
485 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
492 BOOL
cli_credentials_set_realm_callback(struct cli_credentials
*cred
,
493 const char *(*realm_cb
) (struct cli_credentials
*))
495 if (cred
->realm_obtained
< CRED_CALLBACK
) {
496 cred
->realm_cb
= realm_cb
;
497 cred
->realm_obtained
= CRED_CALLBACK
;
505 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
507 * @param cred credentials context
508 * @retval The workstation name set on this context.
509 * @note Return value will never be NULL except by programmer error.
511 const char *cli_credentials_get_workstation(struct cli_credentials
*cred
)
513 if (cred
->workstation_obtained
== CRED_CALLBACK
&&
514 !cred
->callback_running
) {
515 cred
->callback_running
= True
;
516 cred
->workstation
= cred
->workstation_cb(cred
);
517 cred
->callback_running
= False
;
518 cred
->workstation_obtained
= CRED_SPECIFIED
;
521 return cred
->workstation
;
524 BOOL
cli_credentials_set_workstation(struct cli_credentials
*cred
,
526 enum credentials_obtained obtained
)
528 if (obtained
>= cred
->workstation_obtained
) {
529 cred
->workstation
= talloc_strdup(cred
, val
);
530 cred
->workstation_obtained
= obtained
;
537 BOOL
cli_credentials_set_workstation_callback(struct cli_credentials
*cred
,
538 const char *(*workstation_cb
) (struct cli_credentials
*))
540 if (cred
->workstation_obtained
< CRED_CALLBACK
) {
541 cred
->workstation_cb
= workstation_cb
;
542 cred
->workstation_obtained
= CRED_CALLBACK
;
550 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
552 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
554 * @param credentials Credentials structure on which to set the password
555 * @param data the string containing the username, password etc
556 * @param obtained This enum describes how 'specified' this password is
559 void cli_credentials_parse_string(struct cli_credentials
*credentials
, const char *data
, enum credentials_obtained obtained
)
563 if (strcmp("%",data
) == 0) {
564 cli_credentials_set_anonymous(credentials
);
568 uname
= talloc_strdup(credentials
, data
);
569 if ((p
= strchr_m(uname
,'%'))) {
571 cli_credentials_set_password(credentials
, p
+1, obtained
);
574 if ((p
= strchr_m(uname
,'@'))) {
575 cli_credentials_set_principal(credentials
, uname
, obtained
);
577 cli_credentials_set_realm(credentials
, p
+1, obtained
);
579 } else if ((p
= strchr_m(uname
,'\\')) || (p
= strchr_m(uname
, '/'))) {
581 cli_credentials_set_domain(credentials
, uname
, obtained
);
584 cli_credentials_set_username(credentials
, uname
, obtained
);
588 * Given a a credentials structure, print it as a string
590 * The format output is [domain\\]user[%password] or user[@realm][%password]
592 * @param credentials Credentials structure on which to set the password
593 * @param mem_ctx The memory context to place the result on
596 const char *cli_credentials_get_unparsed_name(struct cli_credentials
*credentials
, TALLOC_CTX
*mem_ctx
)
598 const char *bind_dn
= cli_credentials_get_bind_dn(credentials
);
600 const char *username
;
604 name
= talloc_reference(mem_ctx
, bind_dn
);
606 cli_credentials_get_ntlm_username_domain(credentials
, mem_ctx
, &username
, &domain
);
607 if (domain
&& domain
[0]) {
608 name
= talloc_asprintf(mem_ctx
, "%s\\%s",
611 name
= talloc_asprintf(mem_ctx
, "%s",
619 * Specifies default values for domain, workstation and realm
620 * from the smb.conf configuration file
622 * @param cred Credentials structure to fill in
624 void cli_credentials_set_conf(struct cli_credentials
*cred
)
626 cli_credentials_set_username(cred
, "", CRED_UNINITIALISED
);
627 cli_credentials_set_domain(cred
, lp_workgroup(), CRED_UNINITIALISED
);
628 cli_credentials_set_workstation(cred
, lp_netbios_name(), CRED_UNINITIALISED
);
629 cli_credentials_set_realm(cred
, lp_realm(), CRED_UNINITIALISED
);
633 * Guess defaults for credentials from environment variables,
634 * and from the configuration file
636 * @param cred Credentials structure to fill in
638 void cli_credentials_guess(struct cli_credentials
*cred
)
642 cli_credentials_set_conf(cred
);
644 if (getenv("LOGNAME")) {
645 cli_credentials_set_username(cred
, getenv("LOGNAME"), CRED_GUESS_ENV
);
648 if (getenv("USER")) {
649 cli_credentials_parse_string(cred
, getenv("USER"), CRED_GUESS_ENV
);
650 if ((p
= strchr_m(getenv("USER"),'%'))) {
651 memset(p
,0,strlen(cred
->password
));
655 if (getenv("PASSWD")) {
656 cli_credentials_set_password(cred
, getenv("PASSWD"), CRED_GUESS_ENV
);
659 if (getenv("PASSWD_FD")) {
660 cli_credentials_parse_password_fd(cred
, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE
);
663 p
= getenv("PASSWD_FILE");
665 cli_credentials_parse_password_file(cred
, p
, CRED_GUESS_FILE
);
668 if (cli_credentials_get_kerberos_state(cred
) != CRED_DONT_USE_KERBEROS
) {
669 cli_credentials_set_ccache(cred
, NULL
, CRED_GUESS_FILE
);
674 * Attach NETLOGON credentials for use with SCHANNEL
677 void cli_credentials_set_netlogon_creds(struct cli_credentials
*cred
,
678 struct creds_CredentialState
*netlogon_creds
)
680 cred
->netlogon_creds
= talloc_reference(cred
, netlogon_creds
);
684 * Return attached NETLOGON credentials
687 struct creds_CredentialState
*cli_credentials_get_netlogon_creds(struct cli_credentials
*cred
)
689 return cred
->netlogon_creds
;
693 * Set NETLOGON secure channel type
696 void cli_credentials_set_secure_channel_type(struct cli_credentials
*cred
,
697 enum netr_SchannelType secure_channel_type
)
699 cred
->secure_channel_type
= secure_channel_type
;
703 * Return NETLOGON secure chanel type
706 enum netr_SchannelType
cli_credentials_get_secure_channel_type(struct cli_credentials
*cred
)
708 return cred
->secure_channel_type
;
712 * Fill in a credentials structure as the anonymous user
714 void cli_credentials_set_anonymous(struct cli_credentials
*cred
)
716 cli_credentials_set_username(cred
, "", CRED_SPECIFIED
);
717 cli_credentials_set_domain(cred
, "", CRED_SPECIFIED
);
718 cli_credentials_set_password(cred
, NULL
, CRED_SPECIFIED
);
722 * Describe a credentials context as anonymous or authenticated
723 * @retval True if anonymous, False if a username is specified
726 BOOL
cli_credentials_is_anonymous(struct cli_credentials
*cred
)
728 const char *username
;
730 if (cred
->machine_account_pending
) {
731 cli_credentials_set_machine_account(cred
);
734 username
= cli_credentials_get_username(cred
);
736 /* Yes, it is deliberate that we die if we have a NULL pointer
737 * here - anonymous is "", not NULL, which is 'never specified,
738 * never guessed', ie programmer bug */
747 * Mark the current password for a credentials struct as wrong. This will
748 * cause the password to be prompted again (if a callback is set).
750 * This will decrement the number of times the password can be tried.
752 * @retval whether the credentials struct is finished
754 BOOL
cli_credentials_wrong_password(struct cli_credentials
*cred
)
756 if (cred
->password_obtained
!= CRED_CALLBACK_RESULT
) {
760 cred
->password_obtained
= CRED_CALLBACK
;
764 return (cred
->tries
> 0);
768 set the common event context for this set of credentials
770 void cli_credentials_set_event_context(struct cli_credentials
*cred
, struct event_context
*ev
)
776 set the common event context for this set of credentials
778 struct event_context
*cli_credentials_get_event_context(struct cli_credentials
*cred
)
780 if (cred
->ev
== NULL
) {
781 cred
->ev
= event_context_find(cred
);