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;
62 cred->callback_running = False;
64 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
69 void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
70 enum credentials_use_kerberos use_kerberos)
72 creds->use_kerberos = use_kerberos;
75 enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
77 return creds->use_kerberos;
82 * Obtain the username for this credentials context.
83 * @param cred credentials context
84 * @retval The username set on this context.
85 * @note Return value will never be NULL except by programmer error.
87 const char *cli_credentials_get_username(struct cli_credentials *cred)
89 if (cred->machine_account_pending) {
90 cli_credentials_set_machine_account(cred);
93 if (cred->username_obtained == CRED_CALLBACK &&
94 !cred->callback_running) {
95 cred->callback_running = True;
96 cred->username = cred->username_cb(cred);
97 cred->callback_running = False;
98 cred->username_obtained = CRED_SPECIFIED;
101 return cred->username;
104 BOOL cli_credentials_set_username(struct cli_credentials *cred,
105 const char *val, enum credentials_obtained obtained)
107 if (obtained >= cred->username_obtained) {
108 cred->username = talloc_strdup(cred, val);
109 cred->username_obtained = obtained;
116 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
117 const char *(*username_cb) (struct cli_credentials *))
119 if (cred->username_obtained < CRED_CALLBACK) {
120 cred->username_cb = username_cb;
121 cred->username_obtained = CRED_CALLBACK;
128 BOOL cli_credentials_set_bind_dn(struct cli_credentials *cred,
131 cred->bind_dn = talloc_strdup(cred, bind_dn);
136 * Obtain the BIND DN for this credentials context.
137 * @param cred credentials context
138 * @retval The username set on this context.
139 * @note Return value will be NULL if not specified explictly
141 const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
143 return cred->bind_dn;
148 * Obtain the client principal for this credentials context.
149 * @param cred credentials context
150 * @retval The username set on this context.
151 * @note Return value will never be NULL except by programmer error.
153 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
155 if (cred->machine_account_pending) {
156 cli_credentials_set_machine_account(cred);
159 if (cred->principal_obtained == CRED_CALLBACK &&
160 !cred->callback_running) {
161 cred->callback_running = True;
162 cred->principal = cred->principal_cb(cred);
163 cred->callback_running = False;
164 cred->principal_obtained = CRED_SPECIFIED;
167 if (cred->principal_obtained < cred->username_obtained) {
168 if (cred->domain_obtained > cred->realm_obtained) {
169 return talloc_asprintf(mem_ctx, "%s@%s",
170 cli_credentials_get_username(cred),
171 cli_credentials_get_domain(cred));
173 return talloc_asprintf(mem_ctx, "%s@%s",
174 cli_credentials_get_username(cred),
175 cli_credentials_get_realm(cred));
178 return talloc_reference(mem_ctx, cred->principal);
181 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
183 enum credentials_obtained obtained)
185 if (obtained >= cred->principal_obtained) {
186 cred->principal = talloc_strdup(cred, val);
187 cred->principal_obtained = obtained;
194 /* Set a callback to get the principal. This could be a popup dialog,
195 * a terminal prompt or similar. */
197 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
198 const char *(*principal_cb) (struct cli_credentials *))
200 if (cred->principal_obtained < CRED_CALLBACK) {
201 cred->principal_cb = principal_cb;
202 cred->principal_obtained = CRED_CALLBACK;
209 /* Some of our tools are 'anonymous by default'. This is a single
210 * function to determine if authentication has been explicitly
213 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
219 if (cli_credentials_is_anonymous(cred)){
223 if (cred->principal_obtained >= CRED_SPECIFIED) {
226 if (cred->username_obtained >= CRED_SPECIFIED) {
233 * Obtain the password for this credentials context.
234 * @param cred credentials context
235 * @retval If set, the cleartext password, otherwise NULL
237 const char *cli_credentials_get_password(struct cli_credentials *cred)
239 if (cred->machine_account_pending) {
240 cli_credentials_set_machine_account(cred);
243 if (cred->password_obtained == CRED_CALLBACK &&
244 !cred->callback_running) {
245 cred->callback_running = True;
246 cred->password = cred->password_cb(cred);
247 cred->callback_running = False;
248 cred->password_obtained = CRED_CALLBACK_RESULT;
251 return cred->password;
254 /* Set a password on the credentials context, including an indication
255 * of 'how' the password was obtained */
257 BOOL cli_credentials_set_password(struct cli_credentials *cred,
259 enum credentials_obtained obtained)
261 if (obtained >= cred->password_obtained) {
262 cred->password = talloc_strdup(cred, val);
263 cred->password_obtained = obtained;
265 cred->nt_hash = NULL;
272 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
273 const char *(*password_cb) (struct cli_credentials *))
275 if (cred->password_obtained < CRED_CALLBACK) {
276 cred->password_cb = password_cb;
277 cred->password_obtained = CRED_CALLBACK;
285 * Obtain the 'old' password for this credentials context (used for join accounts).
286 * @param cred credentials context
287 * @retval If set, the cleartext password, otherwise NULL
289 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
291 if (cred->machine_account_pending) {
292 cli_credentials_set_machine_account(cred);
295 return cred->old_password;
298 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
300 enum credentials_obtained obtained)
302 cred->old_password = talloc_strdup(cred, val);
307 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
309 * Sometimes we only have this much of the password, while the rest of
310 * the time this call avoids calling E_md4hash themselves.
312 * @param cred credentials context
313 * @retval If set, the cleartext password, otherwise NULL
315 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
318 const char *password = cli_credentials_get_password(cred);
321 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
326 E_md4hash(password, nt_hash->hash);
330 return cred->nt_hash;
334 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
335 const struct samr_Password *nt_hash,
336 enum credentials_obtained obtained)
338 if (obtained >= cred->password_obtained) {
339 cli_credentials_set_password(cred, NULL, obtained);
340 cred->nt_hash = talloc(cred, struct samr_Password);
341 *cred->nt_hash = *nt_hash;
349 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
350 * @param cred credentials context
351 * @retval The domain set on this context.
352 * @note Return value will never be NULL except by programmer error.
354 const char *cli_credentials_get_domain(struct cli_credentials *cred)
356 if (cred->machine_account_pending) {
357 cli_credentials_set_machine_account(cred);
360 if (cred->domain_obtained == CRED_CALLBACK &&
361 !cred->callback_running) {
362 cred->callback_running = True;
363 cred->domain = cred->domain_cb(cred);
364 cred->callback_running = False;
365 cred->domain_obtained = CRED_SPECIFIED;
372 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
374 enum credentials_obtained obtained)
376 if (obtained >= cred->domain_obtained) {
377 /* it is important that the domain be in upper case,
378 * particularly for the sensitive NTLMv2
380 cred->domain = strupper_talloc(cred, val);
381 cred->domain_obtained = obtained;
388 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
389 const char *(*domain_cb) (struct cli_credentials *))
391 if (cred->domain_obtained < CRED_CALLBACK) {
392 cred->domain_cb = domain_cb;
393 cred->domain_obtained = CRED_CALLBACK;
401 * Obtain the Kerberos realm for this credentials context.
402 * @param cred credentials context
403 * @retval The realm set on this context.
404 * @note Return value will never be NULL except by programmer error.
406 const char *cli_credentials_get_realm(struct cli_credentials *cred)
408 if (cred->machine_account_pending) {
409 cli_credentials_set_machine_account(cred);
412 if (cred->realm_obtained == CRED_CALLBACK &&
413 !cred->callback_running) {
414 cred->callback_running = True;
415 cred->realm = cred->realm_cb(cred);
416 cred->callback_running = False;
417 cred->realm_obtained = CRED_SPECIFIED;
424 * Set the realm for this credentials context, and force it to
425 * uppercase for the sainity of our local kerberos libraries
427 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
429 enum credentials_obtained obtained)
431 if (obtained >= cred->realm_obtained) {
432 cred->realm = strupper_talloc(cred, val);
433 cred->realm_obtained = obtained;
440 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
441 const char *(*realm_cb) (struct cli_credentials *))
443 if (cred->realm_obtained < CRED_CALLBACK) {
444 cred->realm_cb = realm_cb;
445 cred->realm_obtained = CRED_CALLBACK;
453 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
455 * @param cred credentials context
456 * @retval The workstation name set on this context.
457 * @note Return value will never be NULL except by programmer error.
459 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
461 if (cred->workstation_obtained == CRED_CALLBACK &&
462 !cred->callback_running) {
463 cred->callback_running = True;
464 cred->workstation = cred->workstation_cb(cred);
465 cred->callback_running = False;
466 cred->workstation_obtained = CRED_SPECIFIED;
469 return cred->workstation;
472 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
474 enum credentials_obtained obtained)
476 if (obtained >= cred->workstation_obtained) {
477 cred->workstation = talloc_strdup(cred, val);
478 cred->workstation_obtained = obtained;
485 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
486 const char *(*workstation_cb) (struct cli_credentials *))
488 if (cred->workstation_obtained < CRED_CALLBACK) {
489 cred->workstation_cb = workstation_cb;
490 cred->workstation_obtained = CRED_CALLBACK;
498 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
500 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
502 * @param credentials Credentials structure on which to set the password
503 * @param data the string containing the username, password etc
504 * @param obtained This enum describes how 'specified' this password is
507 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
511 if (strcmp("%",data) == 0) {
512 cli_credentials_set_anonymous(credentials);
516 uname = talloc_strdup(credentials, data);
517 if ((p = strchr_m(uname,'%'))) {
519 cli_credentials_set_password(credentials, p+1, obtained);
522 if ((p = strchr_m(uname,'@'))) {
523 cli_credentials_set_principal(credentials, uname, obtained);
525 cli_credentials_set_realm(credentials, p+1, obtained);
527 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
529 cli_credentials_set_domain(credentials, uname, obtained);
532 cli_credentials_set_username(credentials, uname, obtained);
536 * Given a a credentials structure, print it as a string
538 * The format output is [domain\\]user[%password] or user[@realm][%password]
540 * @param credentials Credentials structure on which to set the password
541 * @param mem_ctx The memory context to place the result on
544 const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
546 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
548 const char *username;
552 name = talloc_reference(mem_ctx, bind_dn);
554 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
555 if (domain && domain[0]) {
556 name = talloc_asprintf(mem_ctx, "%s\\%s",
559 name = talloc_asprintf(mem_ctx, "%s",
567 * Specifies default values for domain, workstation and realm
568 * from the smb.conf configuration file
570 * @param cred Credentials structure to fill in
572 void cli_credentials_set_conf(struct cli_credentials *cred)
574 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
575 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
576 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
577 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
581 * Guess defaults for credentials from environment variables,
582 * and from the configuration file
584 * @param cred Credentials structure to fill in
586 void cli_credentials_guess(struct cli_credentials *cred)
590 cli_credentials_set_conf(cred);
592 if (getenv("LOGNAME")) {
593 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
596 if (getenv("USER")) {
597 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
598 if ((p = strchr_m(getenv("USER"),'%'))) {
599 memset(p,0,strlen(cred->password));
603 if (getenv("DOMAIN")) {
604 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESS_ENV);
607 if (getenv("PASSWD")) {
608 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
611 if (getenv("PASSWD_FD")) {
612 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
615 if (getenv("PASSWD_FILE")) {
616 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESS_FILE);
619 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
620 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
625 * Attach NETLOGON credentials for use with SCHANNEL
628 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
629 struct creds_CredentialState *netlogon_creds)
631 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
635 * Return attached NETLOGON credentials
638 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
640 return cred->netlogon_creds;
644 * Set NETLOGON secure channel type
647 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
648 enum netr_SchannelType secure_channel_type)
650 cred->secure_channel_type = secure_channel_type;
654 * Return NETLOGON secure chanel type
657 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
659 return cred->secure_channel_type;
663 * Fill in a credentials structure as the anonymous user
665 void cli_credentials_set_anonymous(struct cli_credentials *cred)
667 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
668 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
669 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
673 * Describe a credentials context as anonymous or authenticated
674 * @retval True if anonymous, False if a username is specified
677 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
679 const char *username;
681 if (cred->machine_account_pending) {
682 cli_credentials_set_machine_account(cred);
685 username = cli_credentials_get_username(cred);
687 /* Yes, it is deliberate that we die if we have a NULL pointer
688 * here - anonymous is "", not NULL, which is 'never specified,
689 * never guessed', ie programmer bug */
698 * Mark the current password for a credentials struct as wrong. This will
699 * cause the password to be prompted again (if a callback is set).
701 * This will decrement the number of times the password can be tried.
703 * @retval whether the credentials struct is finished
705 BOOL cli_credentials_wrong_password(struct cli_credentials *cred)
707 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
711 cred->password_obtained = CRED_CALLBACK;
715 return (cred->tries > 0);