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/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;
61 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
66 void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
67 enum credentials_use_kerberos use_kerberos)
69 creds->use_kerberos = use_kerberos;
72 enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
74 return creds->use_kerberos;
79 * Obtain the username for this credentials context.
80 * @param cred credentials context
81 * @retval The username set on this context.
82 * @note Return value will never be NULL except by programmer error.
84 const char *cli_credentials_get_username(struct cli_credentials *cred)
86 if (cred->machine_account_pending) {
87 cli_credentials_set_machine_account(cred);
90 if (cred->username_obtained == CRED_CALLBACK) {
91 cred->username = cred->username_cb(cred);
92 cred->username_obtained = CRED_SPECIFIED;
95 return cred->username;
98 BOOL cli_credentials_set_username(struct cli_credentials *cred,
99 const char *val, enum credentials_obtained obtained)
101 if (obtained >= cred->username_obtained) {
102 cred->username = talloc_strdup(cred, val);
103 cred->username_obtained = obtained;
110 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
111 const char *(*username_cb) (struct cli_credentials *))
113 if (cred->username_obtained < CRED_CALLBACK) {
114 cred->username_cb = username_cb;
115 cred->username_obtained = CRED_CALLBACK;
122 BOOL cli_credentials_set_bind_dn(struct cli_credentials *cred,
125 cred->bind_dn = talloc_strdup(cred, bind_dn);
130 * Obtain the BIND DN for this credentials context.
131 * @param cred credentials context
132 * @retval The username set on this context.
133 * @note Return value will be NULL if not specified explictly
135 const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
137 return cred->bind_dn;
142 * Obtain the client principal for this credentials context.
143 * @param cred credentials context
144 * @retval The username set on this context.
145 * @note Return value will never be NULL except by programmer error.
147 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
149 if (cred->machine_account_pending) {
150 cli_credentials_set_machine_account(cred);
153 if (cred->principal_obtained == CRED_CALLBACK) {
154 cred->principal = cred->principal_cb(cred);
155 cred->principal_obtained = CRED_SPECIFIED;
158 if (cred->principal_obtained < cred->username_obtained) {
159 if (cred->domain_obtained > cred->realm_obtained) {
160 return talloc_asprintf(mem_ctx, "%s@%s",
161 cli_credentials_get_username(cred),
162 cli_credentials_get_domain(cred));
164 return talloc_asprintf(mem_ctx, "%s@%s",
165 cli_credentials_get_username(cred),
166 cli_credentials_get_realm(cred));
169 return talloc_reference(mem_ctx, cred->principal);
172 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
174 enum credentials_obtained obtained)
176 if (obtained >= cred->principal_obtained) {
177 cred->principal = talloc_strdup(cred, val);
178 cred->principal_obtained = obtained;
185 /* Set a callback to get the principal. This could be a popup dialog,
186 * a terminal prompt or similar. */
188 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
189 const char *(*principal_cb) (struct cli_credentials *))
191 if (cred->principal_obtained < CRED_CALLBACK) {
192 cred->principal_cb = principal_cb;
193 cred->principal_obtained = CRED_CALLBACK;
200 /* Some of our tools are 'anonymous by default'. This is a single
201 * function to determine if authentication has been explicitly
204 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
210 if (cred->machine_account_pending) {
211 cli_credentials_set_machine_account(cred);
214 if (cred->principal_obtained >= CRED_SPECIFIED) {
217 if (cred->username_obtained >= CRED_SPECIFIED) {
224 * Obtain the password for this credentials context.
225 * @param cred credentials context
226 * @retval If set, the cleartext password, otherwise NULL
228 const char *cli_credentials_get_password(struct cli_credentials *cred)
230 if (cred->machine_account_pending) {
231 cli_credentials_set_machine_account(cred);
234 if (cred->password_obtained == CRED_CALLBACK) {
235 cred->password = cred->password_cb(cred);
236 cred->password_obtained = CRED_SPECIFIED;
239 return cred->password;
242 /* Set a password on the credentials context, including an indication
243 * of 'how' the password was obtained */
245 BOOL cli_credentials_set_password(struct cli_credentials *cred,
247 enum credentials_obtained obtained)
249 if (obtained >= cred->password_obtained) {
250 cred->password = talloc_strdup(cred, val);
251 cred->password_obtained = obtained;
253 cred->nt_hash = NULL;
260 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
261 const char *(*password_cb) (struct cli_credentials *))
263 if (cred->password_obtained < CRED_CALLBACK) {
264 cred->password_cb = password_cb;
265 cred->password_obtained = CRED_CALLBACK;
273 * Obtain the 'old' password for this credentials context (used for join accounts).
274 * @param cred credentials context
275 * @retval If set, the cleartext password, otherwise NULL
277 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
279 if (cred->machine_account_pending) {
280 cli_credentials_set_machine_account(cred);
283 return cred->old_password;
286 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
288 enum credentials_obtained obtained)
290 cred->old_password = talloc_strdup(cred, val);
295 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
297 * Sometimes we only have this much of the password, while the rest of
298 * the time this call avoids calling E_md4hash themselves.
300 * @param cred credentials context
301 * @retval If set, the cleartext password, otherwise NULL
303 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
306 const char *password = cli_credentials_get_password(cred);
309 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
314 E_md4hash(password, nt_hash->hash);
318 return cred->nt_hash;
322 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
323 const struct samr_Password *nt_hash,
324 enum credentials_obtained obtained)
326 if (obtained >= cred->password_obtained) {
327 cli_credentials_set_password(cred, NULL, obtained);
328 cred->nt_hash = talloc(cred, struct samr_Password);
329 *cred->nt_hash = *nt_hash;
337 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
338 * @param cred credentials context
339 * @retval The domain set on this context.
340 * @note Return value will never be NULL except by programmer error.
342 const char *cli_credentials_get_domain(struct cli_credentials *cred)
344 if (cred->machine_account_pending) {
345 cli_credentials_set_machine_account(cred);
348 if (cred->domain_obtained == CRED_CALLBACK) {
349 cred->domain = cred->domain_cb(cred);
350 cred->domain_obtained = CRED_SPECIFIED;
357 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
359 enum credentials_obtained obtained)
361 if (obtained >= cred->domain_obtained) {
362 /* it is important that the domain be in upper case,
363 * particularly for the sensitive NTLMv2
365 cred->domain = strupper_talloc(cred, val);
366 cred->domain_obtained = obtained;
373 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
374 const char *(*domain_cb) (struct cli_credentials *))
376 if (cred->domain_obtained < CRED_CALLBACK) {
377 cred->domain_cb = domain_cb;
378 cred->domain_obtained = CRED_CALLBACK;
386 * Obtain the Kerberos realm for this credentials context.
387 * @param cred credentials context
388 * @retval The realm set on this context.
389 * @note Return value will never be NULL except by programmer error.
391 const char *cli_credentials_get_realm(struct cli_credentials *cred)
393 if (cred->machine_account_pending) {
394 cli_credentials_set_machine_account(cred);
397 if (cred->realm_obtained == CRED_CALLBACK) {
398 cred->realm = cred->realm_cb(cred);
399 cred->realm_obtained = CRED_SPECIFIED;
406 * Set the realm for this credentials context, and force it to
407 * uppercase for the sainity of our local kerberos libraries
409 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
411 enum credentials_obtained obtained)
413 if (obtained >= cred->realm_obtained) {
414 cred->realm = strupper_talloc(cred, val);
415 cred->realm_obtained = obtained;
422 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
423 const char *(*realm_cb) (struct cli_credentials *))
425 if (cred->realm_obtained < CRED_CALLBACK) {
426 cred->realm_cb = realm_cb;
427 cred->realm_obtained = CRED_CALLBACK;
435 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
437 * @param cred credentials context
438 * @retval The workstation name set on this context.
439 * @note Return value will never be NULL except by programmer error.
441 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
443 if (cred->workstation_obtained == CRED_CALLBACK) {
444 cred->workstation = cred->workstation_cb(cred);
445 cred->workstation_obtained = CRED_SPECIFIED;
448 return cred->workstation;
451 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
453 enum credentials_obtained obtained)
455 if (obtained >= cred->workstation_obtained) {
456 cred->workstation = talloc_strdup(cred, val);
457 cred->workstation_obtained = obtained;
464 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
465 const char *(*workstation_cb) (struct cli_credentials *))
467 if (cred->workstation_obtained < CRED_CALLBACK) {
468 cred->workstation_cb = workstation_cb;
469 cred->workstation_obtained = CRED_CALLBACK;
477 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
479 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
481 * @param credentials Credentials structure on which to set the password
482 * @param data the string containing the username, password etc
483 * @param obtained This enum describes how 'specified' this password is
486 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
490 if (strcmp("%",data) == 0) {
491 cli_credentials_set_anonymous(credentials);
495 uname = talloc_strdup(credentials, data);
496 if ((p = strchr_m(uname,'%'))) {
498 cli_credentials_set_password(credentials, p+1, obtained);
501 if ((p = strchr_m(uname,'@'))) {
502 cli_credentials_set_principal(credentials, uname, obtained);
504 cli_credentials_set_realm(credentials, p+1, obtained);
506 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
508 cli_credentials_set_domain(credentials, uname, obtained);
511 cli_credentials_set_username(credentials, uname, obtained);
515 * Specifies default values for domain, workstation and realm
516 * from the smb.conf configuration file
518 * @param cred Credentials structure to fill in
520 void cli_credentials_set_conf(struct cli_credentials *cred)
522 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
523 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
524 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
525 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
529 * Guess defaults for credentials from environment variables,
530 * and from the configuration file
532 * @param cred Credentials structure to fill in
534 void cli_credentials_guess(struct cli_credentials *cred)
538 cli_credentials_set_conf(cred);
540 if (getenv("LOGNAME")) {
541 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
544 if (getenv("USER")) {
545 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
546 if ((p = strchr_m(getenv("USER"),'%'))) {
547 memset(p,0,strlen(cred->password));
551 if (getenv("DOMAIN")) {
552 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESS_ENV);
555 if (getenv("PASSWD")) {
556 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
559 if (getenv("PASSWD_FD")) {
560 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
563 if (getenv("PASSWD_FILE")) {
564 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESS_FILE);
567 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
571 * Attach NETLOGON credentials for use with SCHANNEL
574 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
575 struct creds_CredentialState *netlogon_creds)
577 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
581 * Return attached NETLOGON credentials
584 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
586 return cred->netlogon_creds;
590 * Set NETLOGON secure channel type
593 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
594 enum netr_SchannelType secure_channel_type)
596 cred->secure_channel_type = secure_channel_type;
600 * Return NETLOGON secure chanel type
603 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
605 return cred->secure_channel_type;
609 * Fill in a credentials structure as the anonymous user
611 void cli_credentials_set_anonymous(struct cli_credentials *cred)
613 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
614 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
615 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
619 * Describe a credentials context as anonymous or authenticated
620 * @retval True if anonymous, False if a username is specified
623 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
625 const char *username;
627 if (cred->machine_account_pending) {
628 cli_credentials_set_machine_account(cred);
631 username = cli_credentials_get_username(cred);
633 /* Yes, it is deliberate that we die if we have a NULL pointer
634 * here - anonymous is "", not NULL, which is 'never specified,
635 * never guessed', ie programmer bug */