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;
64 * Obtain the username for this credentials context.
65 * @param cred credentials context
66 * @retval The username set on this context.
67 * @note Return value will never be NULL except by programmer error.
69 const char *cli_credentials_get_username(struct cli_credentials *cred)
71 if (cred->machine_account_pending) {
72 cli_credentials_set_machine_account(cred);
75 if (cred->username_obtained == CRED_CALLBACK) {
76 cred->username = cred->username_cb(cred);
77 cred->username_obtained = CRED_SPECIFIED;
80 return cred->username;
83 BOOL cli_credentials_set_username(struct cli_credentials *cred,
84 const char *val, enum credentials_obtained obtained)
86 if (obtained >= cred->username_obtained) {
87 cred->username = talloc_strdup(cred, val);
88 cred->username_obtained = obtained;
95 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
96 const char *(*username_cb) (struct cli_credentials *))
98 if (cred->username_obtained < CRED_CALLBACK) {
99 cred->username_cb = username_cb;
100 cred->username_obtained = CRED_CALLBACK;
110 * Obtain the client principal for this credentials context.
111 * @param cred credentials context
112 * @retval The username set on this context.
113 * @note Return value will never be NULL except by programmer error.
115 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
117 if (cred->machine_account_pending) {
118 cli_credentials_set_machine_account(cred);
121 if (cred->principal_obtained == CRED_CALLBACK) {
122 cred->principal = cred->principal_cb(cred);
123 cred->principal_obtained = CRED_SPECIFIED;
126 if (cred->principal_obtained < cred->username_obtained) {
127 if (cred->domain_obtained > cred->realm_obtained) {
128 return talloc_asprintf(mem_ctx, "%s@%s",
129 cli_credentials_get_username(cred),
130 cli_credentials_get_domain(cred));
132 return talloc_asprintf(mem_ctx, "%s@%s",
133 cli_credentials_get_username(cred),
134 cli_credentials_get_realm(cred));
137 return talloc_reference(mem_ctx, cred->principal);
140 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
142 enum credentials_obtained obtained)
144 if (obtained >= cred->principal_obtained) {
145 cred->principal = talloc_strdup(cred, val);
146 cred->principal_obtained = obtained;
153 /* Set a callback to get the principal. This could be a popup dialog,
154 * a terminal prompt or similar. */
156 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
157 const char *(*principal_cb) (struct cli_credentials *))
159 if (cred->principal_obtained < CRED_CALLBACK) {
160 cred->principal_cb = principal_cb;
161 cred->principal_obtained = CRED_CALLBACK;
168 /* Some of our tools are 'anonymous by default'. This is a single
169 * function to determine if authentication has been explicitly
172 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
174 if (cred->machine_account_pending) {
175 cli_credentials_set_machine_account(cred);
178 if (cred->principal_obtained >= CRED_SPECIFIED) {
181 if (cred->username_obtained >= CRED_SPECIFIED) {
188 * Obtain the password for this credentials context.
189 * @param cred credentials context
190 * @retval If set, the cleartext password, otherwise NULL
192 const char *cli_credentials_get_password(struct cli_credentials *cred)
194 if (cred->machine_account_pending) {
195 cli_credentials_set_machine_account(cred);
198 if (cred->password_obtained == CRED_CALLBACK) {
199 cred->password = cred->password_cb(cred);
200 cred->password_obtained = CRED_SPECIFIED;
203 return cred->password;
206 /* Set a password on the credentials context, including an indication
207 * of 'how' the password was obtained */
209 BOOL cli_credentials_set_password(struct cli_credentials *cred,
211 enum credentials_obtained obtained)
213 if (obtained >= cred->password_obtained) {
214 cred->password = talloc_strdup(cred, val);
215 cred->password_obtained = obtained;
217 cred->nt_hash = NULL;
224 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
225 const char *(*password_cb) (struct cli_credentials *))
227 if (cred->password_obtained < CRED_CALLBACK) {
228 cred->password_cb = password_cb;
229 cred->password_obtained = CRED_CALLBACK;
237 * Obtain the 'old' password for this credentials context (used for join accounts).
238 * @param cred credentials context
239 * @retval If set, the cleartext password, otherwise NULL
241 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
243 if (cred->machine_account_pending) {
244 cli_credentials_set_machine_account(cred);
247 return cred->old_password;
250 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
252 enum credentials_obtained obtained)
254 cred->old_password = talloc_strdup(cred, val);
259 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
261 * Sometimes we only have this much of the password, while the rest of
262 * the time this call avoids calling E_md4hash themselves.
264 * @param cred credentials context
265 * @retval If set, the cleartext password, otherwise NULL
267 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
270 const char *password = cli_credentials_get_password(cred);
273 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
278 E_md4hash(password, nt_hash->hash);
282 return cred->nt_hash;
286 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
287 const struct samr_Password *nt_hash,
288 enum credentials_obtained obtained)
290 if (obtained >= cred->password_obtained) {
291 cli_credentials_set_password(cred, NULL, obtained);
292 cred->nt_hash = talloc(cred, struct samr_Password);
293 *cred->nt_hash = *nt_hash;
301 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
302 * @param cred credentials context
303 * @retval The domain set on this context.
304 * @note Return value will never be NULL except by programmer error.
306 const char *cli_credentials_get_domain(struct cli_credentials *cred)
308 if (cred->machine_account_pending) {
309 cli_credentials_set_machine_account(cred);
312 if (cred->domain_obtained == CRED_CALLBACK) {
313 cred->domain = cred->domain_cb(cred);
314 cred->domain_obtained = CRED_SPECIFIED;
321 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
323 enum credentials_obtained obtained)
325 if (obtained >= cred->domain_obtained) {
326 /* it is important that the domain be in upper case,
327 * particularly for the sensitive NTLMv2
329 cred->domain = strupper_talloc(cred, val);
330 cred->domain_obtained = obtained;
337 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
338 const char *(*domain_cb) (struct cli_credentials *))
340 if (cred->domain_obtained < CRED_CALLBACK) {
341 cred->domain_cb = domain_cb;
342 cred->domain_obtained = CRED_CALLBACK;
350 * Obtain the Kerberos realm for this credentials context.
351 * @param cred credentials context
352 * @retval The realm set on this context.
353 * @note Return value will never be NULL except by programmer error.
355 const char *cli_credentials_get_realm(struct cli_credentials *cred)
357 if (cred->machine_account_pending) {
358 cli_credentials_set_machine_account(cred);
361 if (cred->realm_obtained == CRED_CALLBACK) {
362 cred->realm = cred->realm_cb(cred);
363 cred->realm_obtained = CRED_SPECIFIED;
370 * Set the realm for this credentials context, and force it to
371 * uppercase for the sainity of our local kerberos libraries
373 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
375 enum credentials_obtained obtained)
377 if (obtained >= cred->realm_obtained) {
378 cred->realm = strupper_talloc(cred, val);
379 cred->realm_obtained = obtained;
386 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
387 const char *(*realm_cb) (struct cli_credentials *))
389 if (cred->realm_obtained < CRED_CALLBACK) {
390 cred->realm_cb = realm_cb;
391 cred->realm_obtained = CRED_CALLBACK;
399 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
401 * @param cred credentials context
402 * @retval The workstation name set on this context.
403 * @note Return value will never be NULL except by programmer error.
405 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
407 if (cred->workstation_obtained == CRED_CALLBACK) {
408 cred->workstation = cred->workstation_cb(cred);
409 cred->workstation_obtained = CRED_SPECIFIED;
412 return cred->workstation;
415 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
417 enum credentials_obtained obtained)
419 if (obtained >= cred->workstation_obtained) {
420 cred->workstation = talloc_strdup(cred, val);
421 cred->workstation_obtained = obtained;
428 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
429 const char *(*workstation_cb) (struct cli_credentials *))
431 if (cred->workstation_obtained < CRED_CALLBACK) {
432 cred->workstation_cb = workstation_cb;
433 cred->workstation_obtained = CRED_CALLBACK;
441 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
443 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
445 * @param credentials Credentials structure on which to set the password
446 * @param data the string containing the username, password etc
447 * @param obtained This enum describes how 'specified' this password is
450 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
454 if (strcmp("%",data) == 0) {
455 cli_credentials_set_anonymous(credentials);
459 uname = talloc_strdup(credentials, data);
460 if ((p = strchr_m(uname,'%'))) {
462 cli_credentials_set_password(credentials, p+1, obtained);
465 if ((p = strchr_m(uname,'@'))) {
466 cli_credentials_set_principal(credentials, uname, obtained);
468 cli_credentials_set_realm(credentials, p+1, obtained);
470 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
472 cli_credentials_set_domain(credentials, uname, obtained);
475 cli_credentials_set_username(credentials, uname, obtained);
479 * Specifies default values for domain, workstation and realm
480 * from the smb.conf configuration file
482 * @param cred Credentials structure to fill in
484 void cli_credentials_set_conf(struct cli_credentials *cred)
486 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
487 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
488 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
489 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
493 * Guess defaults for credentials from environment variables,
494 * and from the configuration file
496 * @param cred Credentials structure to fill in
498 void cli_credentials_guess(struct cli_credentials *cred)
502 cli_credentials_set_conf(cred);
504 if (getenv("LOGNAME")) {
505 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
508 if (getenv("USER")) {
509 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
510 if ((p = strchr_m(getenv("USER"),'%'))) {
511 memset(p,0,strlen(cred->password));
515 if (getenv("DOMAIN")) {
516 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESS_ENV);
519 if (getenv("PASSWD")) {
520 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
523 if (getenv("PASSWD_FD")) {
524 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
527 if (getenv("PASSWD_FILE")) {
528 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESS_FILE);
531 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
535 * Attach NETLOGON credentials for use with SCHANNEL
538 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
539 struct creds_CredentialState *netlogon_creds)
541 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
545 * Return attached NETLOGON credentials
548 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
550 return cred->netlogon_creds;
554 * Set NETLOGON secure channel type
557 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
558 enum netr_SchannelType secure_channel_type)
560 cred->secure_channel_type = secure_channel_type;
564 * Return NETLOGON secure chanel type
567 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
569 return cred->secure_channel_type;
573 * Fill in a credentials structure as the anonymous user
575 void cli_credentials_set_anonymous(struct cli_credentials *cred)
577 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
578 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
579 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
583 * Describe a credentials context as anonymous or authenticated
584 * @retval True if anonymous, False if a username is specified
587 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
589 const char *username;
591 if (cred->machine_account_pending) {
592 cli_credentials_set_machine_account(cred);
595 username = cli_credentials_get_username(cred);
597 /* Yes, it is deliberate that we die if we have a NULL pointer
598 * here - anonymous is "", not NULL, which is 'never specified,
599 * never guessed', ie programmer bug */