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/gensec/gensec.h"
28 #include "libcli/auth/libcli_auth.h"
31 * Create a new credentials structure
32 * @param mem_ctx TALLOC_CTX parent for credentials structure
34 struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
36 struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
41 cred->netlogon_creds = NULL;
42 cred->machine_account_pending = False;
43 cred->workstation_obtained = CRED_UNINITIALISED;
44 cred->username_obtained = CRED_UNINITIALISED;
45 cred->password_obtained = CRED_UNINITIALISED;
46 cred->domain_obtained = CRED_UNINITIALISED;
47 cred->realm_obtained = CRED_UNINITIALISED;
48 cred->ccache_obtained = CRED_UNINITIALISED;
49 cred->client_gss_creds_obtained = CRED_UNINITIALISED;
50 cred->server_gss_creds_obtained = CRED_UNINITIALISED;
51 cred->keytab_obtained = CRED_UNINITIALISED;
52 cred->principal_obtained = CRED_UNINITIALISED;
54 cred->old_password = NULL;
55 cred->smb_krb5_context = NULL;
56 cred->salt_principal = NULL;
57 cred->machine_account = False;
63 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
68 void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
69 enum credentials_use_kerberos use_kerberos)
71 creds->use_kerberos = use_kerberos;
74 enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
76 return creds->use_kerberos;
81 * Obtain the username for this credentials context.
82 * @param cred credentials context
83 * @retval The username set on this context.
84 * @note Return value will never be NULL except by programmer error.
86 const char *cli_credentials_get_username(struct cli_credentials *cred)
88 if (cred->machine_account_pending) {
89 cli_credentials_set_machine_account(cred);
92 if (cred->username_obtained == CRED_CALLBACK &&
93 !cred->callback_running) {
94 cred->callback_running = True;
95 cred->username = cred->username_cb(cred);
96 cred->callback_running = False;
97 cred->username_obtained = CRED_SPECIFIED;
100 return cred->username;
103 BOOL cli_credentials_set_username(struct cli_credentials *cred,
104 const char *val, enum credentials_obtained obtained)
106 if (obtained >= cred->username_obtained) {
107 cred->username = talloc_strdup(cred, val);
108 cred->username_obtained = obtained;
115 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
116 const char *(*username_cb) (struct cli_credentials *))
118 if (cred->username_obtained < CRED_CALLBACK) {
119 cred->username_cb = username_cb;
120 cred->username_obtained = CRED_CALLBACK;
127 BOOL cli_credentials_set_bind_dn(struct cli_credentials *cred,
130 cred->bind_dn = talloc_strdup(cred, bind_dn);
135 * Obtain the BIND DN for this credentials context.
136 * @param cred credentials context
137 * @retval The username set on this context.
138 * @note Return value will be NULL if not specified explictly
140 const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
142 return cred->bind_dn;
147 * Obtain the client principal for this credentials context.
148 * @param cred credentials context
149 * @retval The username set on this context.
150 * @note Return value will never be NULL except by programmer error.
152 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
154 if (cred->machine_account_pending) {
155 cli_credentials_set_machine_account(cred);
158 if (cred->principal_obtained == CRED_CALLBACK &&
159 !cred->callback_running) {
160 cred->callback_running = False;
161 cred->principal = cred->principal_cb(cred);
162 cred->callback_running = True;
163 cred->principal_obtained = CRED_SPECIFIED;
166 if (cred->principal_obtained < cred->username_obtained) {
167 if (cred->domain_obtained > cred->realm_obtained) {
168 return talloc_asprintf(mem_ctx, "%s@%s",
169 cli_credentials_get_username(cred),
170 cli_credentials_get_domain(cred));
172 return talloc_asprintf(mem_ctx, "%s@%s",
173 cli_credentials_get_username(cred),
174 cli_credentials_get_realm(cred));
177 return talloc_reference(mem_ctx, cred->principal);
180 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
182 enum credentials_obtained obtained)
184 if (obtained >= cred->principal_obtained) {
185 cred->principal = talloc_strdup(cred, val);
186 cred->principal_obtained = obtained;
193 /* Set a callback to get the principal. This could be a popup dialog,
194 * a terminal prompt or similar. */
196 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
197 const char *(*principal_cb) (struct cli_credentials *))
199 if (cred->principal_obtained < CRED_CALLBACK) {
200 cred->principal_cb = principal_cb;
201 cred->principal_obtained = CRED_CALLBACK;
208 /* Some of our tools are 'anonymous by default'. This is a single
209 * function to determine if authentication has been explicitly
212 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
218 if (cred->machine_account_pending) {
219 cli_credentials_set_machine_account(cred);
222 if (cred->principal_obtained >= CRED_SPECIFIED) {
225 if (cred->username_obtained >= CRED_SPECIFIED) {
232 * Obtain the password for this credentials context.
233 * @param cred credentials context
234 * @retval If set, the cleartext password, otherwise NULL
236 const char *cli_credentials_get_password(struct cli_credentials *cred)
238 if (cred->machine_account_pending) {
239 cli_credentials_set_machine_account(cred);
242 if (cred->password_obtained == CRED_CALLBACK &&
243 !cred->callback_running) {
244 cred->callback_running = False;
245 cred->password = cred->password_cb(cred);
246 cred->callback_running = True;
247 cred->password_obtained = CRED_CALLBACK_RESULT;
250 return cred->password;
253 /* Set a password on the credentials context, including an indication
254 * of 'how' the password was obtained */
256 BOOL cli_credentials_set_password(struct cli_credentials *cred,
258 enum credentials_obtained obtained)
260 if (obtained >= cred->password_obtained) {
261 cred->password = talloc_strdup(cred, val);
262 cred->password_obtained = obtained;
264 cred->nt_hash = NULL;
271 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
272 const char *(*password_cb) (struct cli_credentials *))
274 if (cred->password_obtained < CRED_CALLBACK) {
275 cred->password_cb = password_cb;
276 cred->password_obtained = CRED_CALLBACK;
284 * Obtain the 'old' password for this credentials context (used for join accounts).
285 * @param cred credentials context
286 * @retval If set, the cleartext password, otherwise NULL
288 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
290 if (cred->machine_account_pending) {
291 cli_credentials_set_machine_account(cred);
294 return cred->old_password;
297 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
299 enum credentials_obtained obtained)
301 cred->old_password = talloc_strdup(cred, val);
306 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
308 * Sometimes we only have this much of the password, while the rest of
309 * the time this call avoids calling E_md4hash themselves.
311 * @param cred credentials context
312 * @retval If set, the cleartext password, otherwise NULL
314 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
317 const char *password = cli_credentials_get_password(cred);
320 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
325 E_md4hash(password, nt_hash->hash);
329 return cred->nt_hash;
333 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
334 const struct samr_Password *nt_hash,
335 enum credentials_obtained obtained)
337 if (obtained >= cred->password_obtained) {
338 cli_credentials_set_password(cred, NULL, obtained);
339 cred->nt_hash = talloc(cred, struct samr_Password);
340 *cred->nt_hash = *nt_hash;
348 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
349 * @param cred credentials context
350 * @retval The domain set on this context.
351 * @note Return value will never be NULL except by programmer error.
353 const char *cli_credentials_get_domain(struct cli_credentials *cred)
355 if (cred->machine_account_pending) {
356 cli_credentials_set_machine_account(cred);
359 if (cred->domain_obtained == CRED_CALLBACK &&
360 !cred->callback_running) {
361 cred->callback_running = True;
362 cred->domain = cred->domain_cb(cred);
363 cred->callback_running = False;
364 cred->domain_obtained = CRED_SPECIFIED;
371 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
373 enum credentials_obtained obtained)
375 if (obtained >= cred->domain_obtained) {
376 /* it is important that the domain be in upper case,
377 * particularly for the sensitive NTLMv2
379 cred->domain = strupper_talloc(cred, val);
380 cred->domain_obtained = obtained;
387 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
388 const char *(*domain_cb) (struct cli_credentials *))
390 if (cred->domain_obtained < CRED_CALLBACK) {
391 cred->domain_cb = domain_cb;
392 cred->domain_obtained = CRED_CALLBACK;
400 * Obtain the Kerberos realm for this credentials context.
401 * @param cred credentials context
402 * @retval The realm set on this context.
403 * @note Return value will never be NULL except by programmer error.
405 const char *cli_credentials_get_realm(struct cli_credentials *cred)
407 if (cred->machine_account_pending) {
408 cli_credentials_set_machine_account(cred);
411 if (cred->realm_obtained == CRED_CALLBACK &&
412 !cred->callback_running) {
413 cred->callback_running = True;
414 cred->realm = cred->realm_cb(cred);
415 cred->callback_running = False;
416 cred->realm_obtained = CRED_SPECIFIED;
423 * Set the realm for this credentials context, and force it to
424 * uppercase for the sainity of our local kerberos libraries
426 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
428 enum credentials_obtained obtained)
430 if (obtained >= cred->realm_obtained) {
431 cred->realm = strupper_talloc(cred, val);
432 cred->realm_obtained = obtained;
439 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
440 const char *(*realm_cb) (struct cli_credentials *))
442 if (cred->realm_obtained < CRED_CALLBACK) {
443 cred->realm_cb = realm_cb;
444 cred->realm_obtained = CRED_CALLBACK;
452 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
454 * @param cred credentials context
455 * @retval The workstation name set on this context.
456 * @note Return value will never be NULL except by programmer error.
458 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
460 if (cred->workstation_obtained == CRED_CALLBACK &&
461 !cred->callback_running) {
462 cred->callback_running = True;
463 cred->workstation = cred->workstation_cb(cred);
464 cred->callback_running = False;
465 cred->workstation_obtained = CRED_SPECIFIED;
468 return cred->workstation;
471 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
473 enum credentials_obtained obtained)
475 if (obtained >= cred->workstation_obtained) {
476 cred->workstation = talloc_strdup(cred, val);
477 cred->workstation_obtained = obtained;
484 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
485 const char *(*workstation_cb) (struct cli_credentials *))
487 if (cred->workstation_obtained < CRED_CALLBACK) {
488 cred->workstation_cb = workstation_cb;
489 cred->workstation_obtained = CRED_CALLBACK;
497 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
499 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
501 * @param credentials Credentials structure on which to set the password
502 * @param data the string containing the username, password etc
503 * @param obtained This enum describes how 'specified' this password is
506 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
510 if (strcmp("%",data) == 0) {
511 cli_credentials_set_anonymous(credentials);
515 uname = talloc_strdup(credentials, data);
516 if ((p = strchr_m(uname,'%'))) {
518 cli_credentials_set_password(credentials, p+1, obtained);
521 if ((p = strchr_m(uname,'@'))) {
522 cli_credentials_set_principal(credentials, uname, obtained);
524 cli_credentials_set_realm(credentials, p+1, obtained);
526 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
528 cli_credentials_set_domain(credentials, uname, obtained);
531 cli_credentials_set_username(credentials, uname, obtained);
535 * Given a a credentials structure, print it as a string
537 * The format output is [domain\\]user[%password] or user[@realm][%password]
539 * @param credentials Credentials structure on which to set the password
540 * @param mem_ctx The memory context to place the result on
543 const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
545 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
547 const char *username;
551 name = talloc_reference(mem_ctx, bind_dn);
553 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
554 if (domain && domain[0]) {
555 name = talloc_asprintf(mem_ctx, "%s\\%s",
558 name = talloc_asprintf(mem_ctx, "%s",
566 * Specifies default values for domain, workstation and realm
567 * from the smb.conf configuration file
569 * @param cred Credentials structure to fill in
571 void cli_credentials_set_conf(struct cli_credentials *cred)
573 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
574 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
575 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
576 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
580 * Guess defaults for credentials from environment variables,
581 * and from the configuration file
583 * @param cred Credentials structure to fill in
585 void cli_credentials_guess(struct cli_credentials *cred)
589 cli_credentials_set_conf(cred);
591 if (getenv("LOGNAME")) {
592 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
595 if (getenv("USER")) {
596 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
597 if ((p = strchr_m(getenv("USER"),'%'))) {
598 memset(p,0,strlen(cred->password));
602 if (getenv("DOMAIN")) {
603 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESS_ENV);
606 if (getenv("PASSWD")) {
607 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
610 if (getenv("PASSWD_FD")) {
611 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
614 if (getenv("PASSWD_FILE")) {
615 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESS_FILE);
618 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
619 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
624 * Attach NETLOGON credentials for use with SCHANNEL
627 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
628 struct creds_CredentialState *netlogon_creds)
630 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
634 * Return attached NETLOGON credentials
637 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
639 return cred->netlogon_creds;
643 * Set NETLOGON secure channel type
646 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
647 enum netr_SchannelType secure_channel_type)
649 cred->secure_channel_type = secure_channel_type;
653 * Return NETLOGON secure chanel type
656 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
658 return cred->secure_channel_type;
662 * Fill in a credentials structure as the anonymous user
664 void cli_credentials_set_anonymous(struct cli_credentials *cred)
666 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
667 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
668 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
672 * Describe a credentials context as anonymous or authenticated
673 * @retval True if anonymous, False if a username is specified
676 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
678 const char *username;
680 if (cred->machine_account_pending) {
681 cli_credentials_set_machine_account(cred);
684 username = cli_credentials_get_username(cred);
686 /* Yes, it is deliberate that we die if we have a NULL pointer
687 * here - anonymous is "", not NULL, which is 'never specified,
688 * never guessed', ie programmer bug */
697 * Mark the current password for a credentials struct as wrong. This will
698 * cause the password to be prompted again (if a callback is set).
700 * This will decrement the number of times the password can be tried.
702 * @retval whether the credentials struct is finished
704 BOOL cli_credentials_wrong_password(struct cli_credentials *cred)
706 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
710 cred->password_obtained = CRED_CALLBACK;
714 return (cred->tries > 0);