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_krb5.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "lib/events/events.h"
32 * Create a new credentials structure
33 * @param mem_ctx TALLOC_CTX parent for credentials structure
35 struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
37 struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
42 cred->netlogon_creds = NULL;
43 cred->machine_account_pending = False;
44 cred->workstation_obtained = CRED_UNINITIALISED;
45 cred->username_obtained = CRED_UNINITIALISED;
46 cred->password_obtained = CRED_UNINITIALISED;
47 cred->domain_obtained = CRED_UNINITIALISED;
48 cred->realm_obtained = CRED_UNINITIALISED;
49 cred->ccache_obtained = CRED_UNINITIALISED;
50 cred->client_gss_creds_obtained = CRED_UNINITIALISED;
51 cred->server_gss_creds_obtained = CRED_UNINITIALISED;
52 cred->keytab_obtained = CRED_UNINITIALISED;
53 cred->principal_obtained = CRED_UNINITIALISED;
55 cred->ccache_threshold = CRED_UNINITIALISED;
56 cred->client_gss_creds_threshold = CRED_UNINITIALISED;
58 cred->old_password = NULL;
59 cred->smb_krb5_context = NULL;
60 cred->salt_principal = NULL;
61 cred->machine_account = False;
66 cred->callback_running = False;
69 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
70 cli_credentials_set_gensec_features(cred, 0);
76 * Create a new anonymous credential
77 * @param mem_ctx TALLOC_CTX parent for credentials structure
79 struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
81 struct cli_credentials *anon_credentials;
83 anon_credentials = cli_credentials_init(mem_ctx);
84 cli_credentials_set_conf(anon_credentials);
85 cli_credentials_set_anonymous(anon_credentials);
87 return anon_credentials;
90 void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
91 enum credentials_use_kerberos use_kerberos)
93 creds->use_kerberos = use_kerberos;
96 enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
98 return creds->use_kerberos;
101 void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
103 creds->gensec_features = gensec_features;
106 uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
108 return creds->gensec_features;
113 * Obtain the username for this credentials context.
114 * @param cred credentials context
115 * @retval The username set on this context.
116 * @note Return value will never be NULL except by programmer error.
118 const char *cli_credentials_get_username(struct cli_credentials *cred)
120 if (cred->machine_account_pending) {
121 cli_credentials_set_machine_account(cred);
124 if (cred->username_obtained == CRED_CALLBACK &&
125 !cred->callback_running) {
126 cred->callback_running = True;
127 cred->username = cred->username_cb(cred);
128 cred->callback_running = False;
129 cred->username_obtained = CRED_SPECIFIED;
130 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
133 return cred->username;
136 BOOL cli_credentials_set_username(struct cli_credentials *cred,
137 const char *val, enum credentials_obtained obtained)
139 if (obtained >= cred->username_obtained) {
140 cred->username = talloc_strdup(cred, val);
141 cred->username_obtained = obtained;
142 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
149 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
150 const char *(*username_cb) (struct cli_credentials *))
152 if (cred->username_obtained < CRED_CALLBACK) {
153 cred->username_cb = username_cb;
154 cred->username_obtained = CRED_CALLBACK;
161 BOOL cli_credentials_set_bind_dn(struct cli_credentials *cred,
164 cred->bind_dn = talloc_strdup(cred, bind_dn);
169 * Obtain the BIND DN for this credentials context.
170 * @param cred credentials context
171 * @retval The username set on this context.
172 * @note Return value will be NULL if not specified explictly
174 const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
176 return cred->bind_dn;
181 * Obtain the client principal for this credentials context.
182 * @param cred credentials context
183 * @retval The username set on this context.
184 * @note Return value will never be NULL except by programmer error.
186 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
188 if (cred->machine_account_pending) {
189 cli_credentials_set_machine_account(cred);
192 if (cred->principal_obtained == CRED_CALLBACK &&
193 !cred->callback_running) {
194 cred->callback_running = True;
195 cred->principal = cred->principal_cb(cred);
196 cred->callback_running = False;
197 cred->principal_obtained = CRED_SPECIFIED;
198 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
201 if (cred->principal_obtained < cred->username_obtained) {
202 if (cred->domain_obtained > cred->realm_obtained) {
203 return talloc_asprintf(mem_ctx, "%s@%s",
204 cli_credentials_get_username(cred),
205 cli_credentials_get_domain(cred));
207 return talloc_asprintf(mem_ctx, "%s@%s",
208 cli_credentials_get_username(cred),
209 cli_credentials_get_realm(cred));
212 return talloc_reference(mem_ctx, cred->principal);
215 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
217 enum credentials_obtained obtained)
219 if (obtained >= cred->principal_obtained) {
220 cred->principal = talloc_strdup(cred, val);
221 cred->principal_obtained = obtained;
222 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
229 /* Set a callback to get the principal. This could be a popup dialog,
230 * a terminal prompt or similar. */
232 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
233 const char *(*principal_cb) (struct cli_credentials *))
235 if (cred->principal_obtained < CRED_CALLBACK) {
236 cred->principal_cb = principal_cb;
237 cred->principal_obtained = CRED_CALLBACK;
244 /* Some of our tools are 'anonymous by default'. This is a single
245 * function to determine if authentication has been explicitly
248 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
254 if (cli_credentials_is_anonymous(cred)){
258 if (cred->principal_obtained >= CRED_SPECIFIED) {
261 if (cred->username_obtained >= CRED_SPECIFIED) {
265 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
273 * Obtain the password for this credentials context.
274 * @param cred credentials context
275 * @retval If set, the cleartext password, otherwise NULL
277 const char *cli_credentials_get_password(struct cli_credentials *cred)
279 if (cred->machine_account_pending) {
280 cli_credentials_set_machine_account(cred);
283 if (cred->password_obtained == CRED_CALLBACK &&
284 !cred->callback_running) {
285 cred->callback_running = True;
286 cred->password = cred->password_cb(cred);
287 cred->callback_running = False;
288 cred->password_obtained = CRED_CALLBACK_RESULT;
289 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
292 return cred->password;
295 /* Set a password on the credentials context, including an indication
296 * of 'how' the password was obtained */
298 BOOL cli_credentials_set_password(struct cli_credentials *cred,
300 enum credentials_obtained obtained)
302 if (obtained >= cred->password_obtained) {
303 cred->password = talloc_strdup(cred, val);
304 cred->password_obtained = obtained;
305 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
307 cred->nt_hash = NULL;
314 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
315 const char *(*password_cb) (struct cli_credentials *))
317 if (cred->password_obtained < CRED_CALLBACK) {
318 cred->password_cb = password_cb;
319 cred->password_obtained = CRED_CALLBACK;
320 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
328 * Obtain the 'old' password for this credentials context (used for join accounts).
329 * @param cred credentials context
330 * @retval If set, the cleartext password, otherwise NULL
332 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
334 if (cred->machine_account_pending) {
335 cli_credentials_set_machine_account(cred);
338 return cred->old_password;
341 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
343 enum credentials_obtained obtained)
345 cred->old_password = talloc_strdup(cred, val);
350 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
352 * Sometimes we only have this much of the password, while the rest of
353 * the time this call avoids calling E_md4hash themselves.
355 * @param cred credentials context
356 * @retval If set, the cleartext password, otherwise NULL
358 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
361 const char *password = cli_credentials_get_password(cred);
364 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
369 E_md4hash(password, nt_hash->hash);
373 return cred->nt_hash;
377 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
378 const struct samr_Password *nt_hash,
379 enum credentials_obtained obtained)
381 if (obtained >= cred->password_obtained) {
382 cli_credentials_set_password(cred, NULL, obtained);
384 cred->nt_hash = talloc(cred, struct samr_Password);
385 *cred->nt_hash = *nt_hash;
387 cred->nt_hash = NULL;
396 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
397 * @param cred credentials context
398 * @retval The domain set on this context.
399 * @note Return value will never be NULL except by programmer error.
401 const char *cli_credentials_get_domain(struct cli_credentials *cred)
403 if (cred->machine_account_pending) {
404 cli_credentials_set_machine_account(cred);
407 if (cred->domain_obtained == CRED_CALLBACK &&
408 !cred->callback_running) {
409 cred->callback_running = True;
410 cred->domain = cred->domain_cb(cred);
411 cred->callback_running = False;
412 cred->domain_obtained = CRED_SPECIFIED;
413 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
420 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
422 enum credentials_obtained obtained)
424 if (obtained >= cred->domain_obtained) {
425 /* it is important that the domain be in upper case,
426 * particularly for the sensitive NTLMv2
428 cred->domain = strupper_talloc(cred, val);
429 cred->domain_obtained = obtained;
430 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
437 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
438 const char *(*domain_cb) (struct cli_credentials *))
440 if (cred->domain_obtained < CRED_CALLBACK) {
441 cred->domain_cb = domain_cb;
442 cred->domain_obtained = CRED_CALLBACK;
450 * Obtain the Kerberos realm for this credentials context.
451 * @param cred credentials context
452 * @retval The realm set on this context.
453 * @note Return value will never be NULL except by programmer error.
455 const char *cli_credentials_get_realm(struct cli_credentials *cred)
457 if (cred->machine_account_pending) {
458 cli_credentials_set_machine_account(cred);
461 if (cred->realm_obtained == CRED_CALLBACK &&
462 !cred->callback_running) {
463 cred->callback_running = True;
464 cred->realm = cred->realm_cb(cred);
465 cred->callback_running = False;
466 cred->realm_obtained = CRED_SPECIFIED;
467 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
474 * Set the realm for this credentials context, and force it to
475 * uppercase for the sainity of our local kerberos libraries
477 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
479 enum credentials_obtained obtained)
481 if (obtained >= cred->realm_obtained) {
482 cred->realm = strupper_talloc(cred, val);
483 cred->realm_obtained = obtained;
484 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
491 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
492 const char *(*realm_cb) (struct cli_credentials *))
494 if (cred->realm_obtained < CRED_CALLBACK) {
495 cred->realm_cb = realm_cb;
496 cred->realm_obtained = CRED_CALLBACK;
504 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
506 * @param cred credentials context
507 * @retval The workstation name set on this context.
508 * @note Return value will never be NULL except by programmer error.
510 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
512 if (cred->workstation_obtained == CRED_CALLBACK &&
513 !cred->callback_running) {
514 cred->callback_running = True;
515 cred->workstation = cred->workstation_cb(cred);
516 cred->callback_running = False;
517 cred->workstation_obtained = CRED_SPECIFIED;
520 return cred->workstation;
523 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
525 enum credentials_obtained obtained)
527 if (obtained >= cred->workstation_obtained) {
528 cred->workstation = talloc_strdup(cred, val);
529 cred->workstation_obtained = obtained;
536 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
537 const char *(*workstation_cb) (struct cli_credentials *))
539 if (cred->workstation_obtained < CRED_CALLBACK) {
540 cred->workstation_cb = workstation_cb;
541 cred->workstation_obtained = CRED_CALLBACK;
549 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
551 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
553 * @param credentials Credentials structure on which to set the password
554 * @param data the string containing the username, password etc
555 * @param obtained This enum describes how 'specified' this password is
558 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
562 if (strcmp("%",data) == 0) {
563 cli_credentials_set_anonymous(credentials);
567 uname = talloc_strdup(credentials, data);
568 if ((p = strchr_m(uname,'%'))) {
570 cli_credentials_set_password(credentials, p+1, obtained);
573 if ((p = strchr_m(uname,'@'))) {
574 cli_credentials_set_principal(credentials, uname, obtained);
576 cli_credentials_set_realm(credentials, p+1, obtained);
578 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
580 cli_credentials_set_domain(credentials, uname, obtained);
583 cli_credentials_set_username(credentials, uname, obtained);
587 * Given a a credentials structure, print it as a string
589 * The format output is [domain\\]user[%password] or user[@realm][%password]
591 * @param credentials Credentials structure on which to set the password
592 * @param mem_ctx The memory context to place the result on
595 const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
597 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
599 const char *username;
603 name = talloc_reference(mem_ctx, bind_dn);
605 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
606 if (domain && domain[0]) {
607 name = talloc_asprintf(mem_ctx, "%s\\%s",
610 name = talloc_asprintf(mem_ctx, "%s",
618 * Specifies default values for domain, workstation and realm
619 * from the smb.conf configuration file
621 * @param cred Credentials structure to fill in
623 void cli_credentials_set_conf(struct cli_credentials *cred)
625 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
626 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
627 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
628 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
632 * Guess defaults for credentials from environment variables,
633 * and from the configuration file
635 * @param cred Credentials structure to fill in
637 void cli_credentials_guess(struct cli_credentials *cred)
641 cli_credentials_set_conf(cred);
643 if (getenv("LOGNAME")) {
644 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
647 if (getenv("USER")) {
648 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
649 if ((p = strchr_m(getenv("USER"),'%'))) {
650 memset(p,0,strlen(cred->password));
654 if (getenv("PASSWD")) {
655 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
658 if (getenv("PASSWD_FD")) {
659 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
662 p = getenv("PASSWD_FILE");
664 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
667 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
668 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
673 * Attach NETLOGON credentials for use with SCHANNEL
676 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
677 struct creds_CredentialState *netlogon_creds)
679 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
683 * Return attached NETLOGON credentials
686 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
688 return cred->netlogon_creds;
692 * Set NETLOGON secure channel type
695 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
696 enum netr_SchannelType secure_channel_type)
698 cred->secure_channel_type = secure_channel_type;
702 * Return NETLOGON secure chanel type
705 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
707 return cred->secure_channel_type;
711 * Fill in a credentials structure as the anonymous user
713 void cli_credentials_set_anonymous(struct cli_credentials *cred)
715 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
716 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
717 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
721 * Describe a credentials context as anonymous or authenticated
722 * @retval True if anonymous, False if a username is specified
725 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
727 const char *username;
729 if (cred->machine_account_pending) {
730 cli_credentials_set_machine_account(cred);
733 username = cli_credentials_get_username(cred);
735 /* Yes, it is deliberate that we die if we have a NULL pointer
736 * here - anonymous is "", not NULL, which is 'never specified,
737 * never guessed', ie programmer bug */
746 * Mark the current password for a credentials struct as wrong. This will
747 * cause the password to be prompted again (if a callback is set).
749 * This will decrement the number of times the password can be tried.
751 * @retval whether the credentials struct is finished
753 BOOL cli_credentials_wrong_password(struct cli_credentials *cred)
755 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
759 cred->password_obtained = CRED_CALLBACK;
763 return (cred->tries > 0);
767 set the common event context for this set of credentials
769 void cli_credentials_set_event_context(struct cli_credentials *cred, struct event_context *ev)
775 set the common event context for this set of credentials
777 struct event_context *cli_credentials_get_event_context(struct cli_credentials *cred)
779 if (cred->ev == NULL) {
780 cred->ev = event_context_find(cred);