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"
30 #include "param/param.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, global_loadparm);
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. */
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,
624 struct loadparm_context *lp_ctx)
626 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
627 cli_credentials_set_domain(cred, lp_workgroup(lp_ctx), CRED_UNINITIALISED);
628 cli_credentials_set_workstation(cred, lp_netbios_name(lp_ctx), CRED_UNINITIALISED);
629 cli_credentials_set_realm(cred, lp_realm(lp_ctx), 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, global_loadparm);
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")),
664 p = getenv("PASSWD_FILE");
666 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
669 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
670 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
675 * Attach NETLOGON credentials for use with SCHANNEL
678 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
679 struct creds_CredentialState *netlogon_creds)
681 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
685 * Return attached NETLOGON credentials
688 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
690 return cred->netlogon_creds;
694 * Set NETLOGON secure channel type
697 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
698 enum netr_SchannelType secure_channel_type)
700 cred->secure_channel_type = secure_channel_type;
704 * Return NETLOGON secure chanel type
707 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
709 return cred->secure_channel_type;
713 * Fill in a credentials structure as the anonymous user
715 void cli_credentials_set_anonymous(struct cli_credentials *cred)
717 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
718 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
719 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
723 * Describe a credentials context as anonymous or authenticated
724 * @retval true if anonymous, false if a username is specified
727 bool cli_credentials_is_anonymous(struct cli_credentials *cred)
729 const char *username;
731 if (cred->machine_account_pending) {
732 cli_credentials_set_machine_account(cred);
735 username = cli_credentials_get_username(cred);
737 /* Yes, it is deliberate that we die if we have a NULL pointer
738 * here - anonymous is "", not NULL, which is 'never specified,
739 * never guessed', ie programmer bug */
748 * Mark the current password for a credentials struct as wrong. This will
749 * cause the password to be prompted again (if a callback is set).
751 * This will decrement the number of times the password can be tried.
753 * @retval whether the credentials struct is finished
755 bool cli_credentials_wrong_password(struct cli_credentials *cred)
757 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
761 cred->password_obtained = CRED_CALLBACK;
765 return (cred->tries > 0);
769 set the common event context for this set of credentials
771 void cli_credentials_set_event_context(struct cli_credentials *cred, struct event_context *ev)
777 set the common event context for this set of credentials
779 struct event_context *cli_credentials_get_event_context(struct cli_credentials *cred)
781 if (cred->ev == NULL) {
782 cred->ev = event_context_find(cred);