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 "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_samr.h" /* for struct samrPassword */
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;
58 cred->gensec_list = NULL;
66 * Obtain the username for this credentials context.
67 * @param cred credentials context
68 * @retval The username set on this context.
69 * @note Return value will never be NULL except by programmer error.
71 const char *cli_credentials_get_username(struct cli_credentials *cred)
73 if (cred->machine_account_pending) {
74 cli_credentials_set_machine_account(cred);
77 if (cred->username_obtained == CRED_CALLBACK) {
78 cred->username = cred->username_cb(cred);
79 cred->username_obtained = CRED_SPECIFIED;
82 return cred->username;
85 BOOL cli_credentials_set_username(struct cli_credentials *cred,
86 const char *val, enum credentials_obtained obtained)
88 if (obtained >= cred->username_obtained) {
89 cred->username = talloc_strdup(cred, val);
90 cred->username_obtained = obtained;
97 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
98 const char *(*username_cb) (struct cli_credentials *))
100 if (cred->username_obtained < CRED_CALLBACK) {
101 cred->username_cb = username_cb;
102 cred->username_obtained = CRED_CALLBACK;
109 BOOL cli_credentials_set_bind_dn(struct cli_credentials *cred,
112 cred->bind_dn = talloc_strdup(cred, bind_dn);
117 * Obtain the BIND DN for this credentials context.
118 * @param cred credentials context
119 * @retval The username set on this context.
120 * @note Return value will be NULL if not specified explictly
122 const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
124 return cred->bind_dn;
129 * Obtain the client principal for this credentials context.
130 * @param cred credentials context
131 * @retval The username set on this context.
132 * @note Return value will never be NULL except by programmer error.
134 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
136 if (cred->machine_account_pending) {
137 cli_credentials_set_machine_account(cred);
140 if (cred->principal_obtained == CRED_CALLBACK) {
141 cred->principal = cred->principal_cb(cred);
142 cred->principal_obtained = CRED_SPECIFIED;
145 if (cred->principal_obtained < cred->username_obtained) {
146 if (cred->domain_obtained > cred->realm_obtained) {
147 return talloc_asprintf(mem_ctx, "%s@%s",
148 cli_credentials_get_username(cred),
149 cli_credentials_get_domain(cred));
151 return talloc_asprintf(mem_ctx, "%s@%s",
152 cli_credentials_get_username(cred),
153 cli_credentials_get_realm(cred));
156 return talloc_reference(mem_ctx, cred->principal);
159 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
161 enum credentials_obtained obtained)
163 if (obtained >= cred->principal_obtained) {
164 cred->principal = talloc_strdup(cred, val);
165 cred->principal_obtained = obtained;
172 /* Set a callback to get the principal. This could be a popup dialog,
173 * a terminal prompt or similar. */
175 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
176 const char *(*principal_cb) (struct cli_credentials *))
178 if (cred->principal_obtained < CRED_CALLBACK) {
179 cred->principal_cb = principal_cb;
180 cred->principal_obtained = CRED_CALLBACK;
187 /* Some of our tools are 'anonymous by default'. This is a single
188 * function to determine if authentication has been explicitly
191 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
197 if (cred->machine_account_pending) {
198 cli_credentials_set_machine_account(cred);
201 if (cred->principal_obtained >= CRED_SPECIFIED) {
204 if (cred->username_obtained >= CRED_SPECIFIED) {
211 * Obtain the password for this credentials context.
212 * @param cred credentials context
213 * @retval If set, the cleartext password, otherwise NULL
215 const char *cli_credentials_get_password(struct cli_credentials *cred)
217 if (cred->machine_account_pending) {
218 cli_credentials_set_machine_account(cred);
221 if (cred->password_obtained == CRED_CALLBACK) {
222 cred->password = cred->password_cb(cred);
223 cred->password_obtained = CRED_SPECIFIED;
226 return cred->password;
229 /* Set a password on the credentials context, including an indication
230 * of 'how' the password was obtained */
232 BOOL cli_credentials_set_password(struct cli_credentials *cred,
234 enum credentials_obtained obtained)
236 if (obtained >= cred->password_obtained) {
237 cred->password = talloc_strdup(cred, val);
238 cred->password_obtained = obtained;
240 cred->nt_hash = NULL;
247 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
248 const char *(*password_cb) (struct cli_credentials *))
250 if (cred->password_obtained < CRED_CALLBACK) {
251 cred->password_cb = password_cb;
252 cred->password_obtained = CRED_CALLBACK;
260 * Obtain the 'old' password for this credentials context (used for join accounts).
261 * @param cred credentials context
262 * @retval If set, the cleartext password, otherwise NULL
264 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
266 if (cred->machine_account_pending) {
267 cli_credentials_set_machine_account(cred);
270 return cred->old_password;
273 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
275 enum credentials_obtained obtained)
277 cred->old_password = talloc_strdup(cred, val);
282 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
284 * Sometimes we only have this much of the password, while the rest of
285 * the time this call avoids calling E_md4hash themselves.
287 * @param cred credentials context
288 * @retval If set, the cleartext password, otherwise NULL
290 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
293 const char *password = cli_credentials_get_password(cred);
296 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
301 E_md4hash(password, nt_hash->hash);
305 return cred->nt_hash;
309 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
310 const struct samr_Password *nt_hash,
311 enum credentials_obtained obtained)
313 if (obtained >= cred->password_obtained) {
314 cli_credentials_set_password(cred, NULL, obtained);
315 cred->nt_hash = talloc(cred, struct samr_Password);
316 *cred->nt_hash = *nt_hash;
324 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
325 * @param cred credentials context
326 * @retval The domain set on this context.
327 * @note Return value will never be NULL except by programmer error.
329 const char *cli_credentials_get_domain(struct cli_credentials *cred)
331 if (cred->machine_account_pending) {
332 cli_credentials_set_machine_account(cred);
335 if (cred->domain_obtained == CRED_CALLBACK) {
336 cred->domain = cred->domain_cb(cred);
337 cred->domain_obtained = CRED_SPECIFIED;
344 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
346 enum credentials_obtained obtained)
348 if (obtained >= cred->domain_obtained) {
349 /* it is important that the domain be in upper case,
350 * particularly for the sensitive NTLMv2
352 cred->domain = strupper_talloc(cred, val);
353 cred->domain_obtained = obtained;
360 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
361 const char *(*domain_cb) (struct cli_credentials *))
363 if (cred->domain_obtained < CRED_CALLBACK) {
364 cred->domain_cb = domain_cb;
365 cred->domain_obtained = CRED_CALLBACK;
373 * Obtain the Kerberos realm for this credentials context.
374 * @param cred credentials context
375 * @retval The realm set on this context.
376 * @note Return value will never be NULL except by programmer error.
378 const char *cli_credentials_get_realm(struct cli_credentials *cred)
380 if (cred->machine_account_pending) {
381 cli_credentials_set_machine_account(cred);
384 if (cred->realm_obtained == CRED_CALLBACK) {
385 cred->realm = cred->realm_cb(cred);
386 cred->realm_obtained = CRED_SPECIFIED;
393 * Set the realm for this credentials context, and force it to
394 * uppercase for the sainity of our local kerberos libraries
396 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
398 enum credentials_obtained obtained)
400 if (obtained >= cred->realm_obtained) {
401 cred->realm = strupper_talloc(cred, val);
402 cred->realm_obtained = obtained;
409 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
410 const char *(*realm_cb) (struct cli_credentials *))
412 if (cred->realm_obtained < CRED_CALLBACK) {
413 cred->realm_cb = realm_cb;
414 cred->realm_obtained = CRED_CALLBACK;
422 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
424 * @param cred credentials context
425 * @retval The workstation name set on this context.
426 * @note Return value will never be NULL except by programmer error.
428 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
430 if (cred->workstation_obtained == CRED_CALLBACK) {
431 cred->workstation = cred->workstation_cb(cred);
432 cred->workstation_obtained = CRED_SPECIFIED;
435 return cred->workstation;
438 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
440 enum credentials_obtained obtained)
442 if (obtained >= cred->workstation_obtained) {
443 cred->workstation = talloc_strdup(cred, val);
444 cred->workstation_obtained = obtained;
451 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
452 const char *(*workstation_cb) (struct cli_credentials *))
454 if (cred->workstation_obtained < CRED_CALLBACK) {
455 cred->workstation_cb = workstation_cb;
456 cred->workstation_obtained = CRED_CALLBACK;
464 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
466 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
468 * @param credentials Credentials structure on which to set the password
469 * @param data the string containing the username, password etc
470 * @param obtained This enum describes how 'specified' this password is
473 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
477 if (strcmp("%",data) == 0) {
478 cli_credentials_set_anonymous(credentials);
482 uname = talloc_strdup(credentials, data);
483 if ((p = strchr_m(uname,'%'))) {
485 cli_credentials_set_password(credentials, p+1, obtained);
488 if ((p = strchr_m(uname,'@'))) {
489 cli_credentials_set_principal(credentials, uname, obtained);
491 cli_credentials_set_realm(credentials, p+1, obtained);
493 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
495 cli_credentials_set_domain(credentials, uname, obtained);
498 cli_credentials_set_username(credentials, uname, obtained);
502 * Specifies default values for domain, workstation and realm
503 * from the smb.conf configuration file
505 * @param cred Credentials structure to fill in
507 void cli_credentials_set_conf(struct cli_credentials *cred)
509 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
510 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
511 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
512 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
516 * Guess defaults for credentials from environment variables,
517 * and from the configuration file
519 * @param cred Credentials structure to fill in
521 void cli_credentials_guess(struct cli_credentials *cred)
525 cli_credentials_set_conf(cred);
527 if (getenv("LOGNAME")) {
528 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
531 if (getenv("USER")) {
532 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
533 if ((p = strchr_m(getenv("USER"),'%'))) {
534 memset(p,0,strlen(cred->password));
538 if (getenv("DOMAIN")) {
539 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESS_ENV);
542 if (getenv("PASSWD")) {
543 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
546 if (getenv("PASSWD_FD")) {
547 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
550 if (getenv("PASSWD_FILE")) {
551 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESS_FILE);
554 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
558 * Attach NETLOGON credentials for use with SCHANNEL
561 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
562 struct creds_CredentialState *netlogon_creds)
564 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
568 * Return attached NETLOGON credentials
571 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
573 return cred->netlogon_creds;
577 * Set NETLOGON secure channel type
580 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
581 enum netr_SchannelType secure_channel_type)
583 cred->secure_channel_type = secure_channel_type;
587 * Return NETLOGON secure chanel type
590 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
592 return cred->secure_channel_type;
596 * Fill in a credentials structure as the anonymous user
598 void cli_credentials_set_anonymous(struct cli_credentials *cred)
600 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
601 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
602 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
606 * Describe a credentials context as anonymous or authenticated
607 * @retval True if anonymous, False if a username is specified
610 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
612 const char *username;
614 if (cred->machine_account_pending) {
615 cli_credentials_set_machine_account(cred);
618 username = cli_credentials_get_username(cred);
620 /* Yes, it is deliberate that we die if we have a NULL pointer
621 * here - anonymous is "", not NULL, which is 'never specified,
622 * never guessed', ie programmer bug */