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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_internal.h"
28 #include "libcli/auth/libcli_auth.h"
30 #include "param/param.h"
31 #include "system/filesys.h"
34 * Create a new credentials structure
35 * @param mem_ctx TALLOC_CTX parent for credentials structure
37 _PUBLIC_
struct cli_credentials
*cli_credentials_init(TALLOC_CTX
*mem_ctx
)
39 struct cli_credentials
*cred
= talloc(mem_ctx
, struct cli_credentials
);
44 cred
->workstation_obtained
= CRED_UNINITIALISED
;
45 cred
->username_obtained
= CRED_UNINITIALISED
;
46 cred
->password_obtained
= CRED_UNINITIALISED
;
47 cred
->domain_obtained
= CRED_UNINITIALISED
;
48 cred
->realm_obtained
= CRED_UNINITIALISED
;
49 cred
->ccache_obtained
= CRED_UNINITIALISED
;
50 cred
->client_gss_creds_obtained
= CRED_UNINITIALISED
;
51 cred
->principal_obtained
= CRED_UNINITIALISED
;
52 cred
->keytab_obtained
= CRED_UNINITIALISED
;
53 cred
->server_gss_creds_obtained
= CRED_UNINITIALISED
;
55 cred
->ccache_threshold
= CRED_UNINITIALISED
;
56 cred
->client_gss_creds_threshold
= CRED_UNINITIALISED
;
58 cred
->workstation
= NULL
;
59 cred
->username
= NULL
;
60 cred
->password
= NULL
;
61 cred
->old_password
= NULL
;
64 cred
->principal
= NULL
;
65 cred
->salt_principal
= NULL
;
66 cred
->impersonate_principal
= NULL
;
67 cred
->self_service
= NULL
;
68 cred
->target_service
= NULL
;
74 cred
->lm_response
.data
= NULL
;
75 cred
->lm_response
.length
= 0;
76 cred
->nt_response
.data
= NULL
;
77 cred
->nt_response
.length
= 0;
80 cred
->client_gss_creds
= NULL
;
82 cred
->server_gss_creds
= NULL
;
84 cred
->workstation_cb
= NULL
;
85 cred
->password_cb
= NULL
;
86 cred
->username_cb
= NULL
;
87 cred
->domain_cb
= NULL
;
88 cred
->realm_cb
= NULL
;
89 cred
->principal_cb
= NULL
;
91 cred
->priv_data
= NULL
;
93 cred
->netlogon_creds
= NULL
;
94 cred
->secure_channel_type
= SEC_CHAN_NULL
;
98 cred
->password_last_changed_time
= 0;
100 cred
->smb_krb5_context
= NULL
;
102 cred
->machine_account_pending
= false;
103 cred
->machine_account_pending_lp_ctx
= NULL
;
105 cred
->machine_account
= false;
107 cred
->password_tries
= 0;
109 cred
->callback_running
= false;
111 cli_credentials_set_kerberos_state(cred
, CRED_AUTO_USE_KERBEROS
);
112 cli_credentials_set_gensec_features(cred
, 0);
113 cli_credentials_set_krb_forwardable(cred
, CRED_AUTO_KRB_FORWARDABLE
);
115 cred
->forced_sasl_mech
= NULL
;
120 _PUBLIC_
void cli_credentials_set_callback_data(struct cli_credentials
*cred
,
123 cred
->priv_data
= callback_data
;
126 _PUBLIC_
void *_cli_credentials_callback_data(struct cli_credentials
*cred
)
128 return cred
->priv_data
;
131 _PUBLIC_
struct cli_credentials
*cli_credentials_shallow_copy(TALLOC_CTX
*mem_ctx
,
132 struct cli_credentials
*src
)
134 struct cli_credentials
*dst
;
136 dst
= talloc(mem_ctx
, struct cli_credentials
);
147 * Create a new anonymous credential
148 * @param mem_ctx TALLOC_CTX parent for credentials structure
150 _PUBLIC_
struct cli_credentials
*cli_credentials_init_anon(TALLOC_CTX
*mem_ctx
)
152 struct cli_credentials
*anon_credentials
;
154 anon_credentials
= cli_credentials_init(mem_ctx
);
155 cli_credentials_set_anonymous(anon_credentials
);
157 return anon_credentials
;
160 _PUBLIC_
void cli_credentials_set_kerberos_state(struct cli_credentials
*creds
,
161 enum credentials_use_kerberos use_kerberos
)
163 creds
->use_kerberos
= use_kerberos
;
166 _PUBLIC_
void cli_credentials_set_forced_sasl_mech(struct cli_credentials
*creds
,
167 const char *sasl_mech
)
169 TALLOC_FREE(creds
->forced_sasl_mech
);
170 creds
->forced_sasl_mech
= talloc_strdup(creds
, sasl_mech
);
173 _PUBLIC_
void cli_credentials_set_krb_forwardable(struct cli_credentials
*creds
,
174 enum credentials_krb_forwardable krb_forwardable
)
176 creds
->krb_forwardable
= krb_forwardable
;
179 _PUBLIC_
enum credentials_use_kerberos
cli_credentials_get_kerberos_state(struct cli_credentials
*creds
)
181 return creds
->use_kerberos
;
184 _PUBLIC_
const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials
*creds
)
186 return creds
->forced_sasl_mech
;
189 _PUBLIC_
enum credentials_krb_forwardable
cli_credentials_get_krb_forwardable(struct cli_credentials
*creds
)
191 return creds
->krb_forwardable
;
194 _PUBLIC_
void cli_credentials_set_gensec_features(struct cli_credentials
*creds
, uint32_t gensec_features
)
196 creds
->gensec_features
= gensec_features
;
199 _PUBLIC_
uint32_t cli_credentials_get_gensec_features(struct cli_credentials
*creds
)
201 return creds
->gensec_features
;
206 * Obtain the username for this credentials context.
207 * @param cred credentials context
208 * @retval The username set on this context.
209 * @note Return value will never be NULL except by programmer error.
211 _PUBLIC_
const char *cli_credentials_get_username(struct cli_credentials
*cred
)
213 if (cred
->machine_account_pending
) {
214 cli_credentials_set_machine_account(cred
,
215 cred
->machine_account_pending_lp_ctx
);
218 if (cred
->username_obtained
== CRED_CALLBACK
&&
219 !cred
->callback_running
) {
220 cred
->callback_running
= true;
221 cred
->username
= cred
->username_cb(cred
);
222 cred
->callback_running
= false;
223 if (cred
->username_obtained
== CRED_CALLBACK
) {
224 cred
->username_obtained
= CRED_CALLBACK_RESULT
;
225 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
229 return cred
->username
;
232 _PUBLIC_
bool cli_credentials_set_username(struct cli_credentials
*cred
,
233 const char *val
, enum credentials_obtained obtained
)
235 if (obtained
>= cred
->username_obtained
) {
236 cred
->username
= talloc_strdup(cred
, val
);
237 cred
->username_obtained
= obtained
;
238 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
245 _PUBLIC_
bool cli_credentials_set_username_callback(struct cli_credentials
*cred
,
246 const char *(*username_cb
) (struct cli_credentials
*))
248 if (cred
->username_obtained
< CRED_CALLBACK
) {
249 cred
->username_cb
= username_cb
;
250 cred
->username_obtained
= CRED_CALLBACK
;
257 _PUBLIC_
bool cli_credentials_set_bind_dn(struct cli_credentials
*cred
,
260 cred
->bind_dn
= talloc_strdup(cred
, bind_dn
);
265 * Obtain the BIND DN for this credentials context.
266 * @param cred credentials context
267 * @retval The username set on this context.
268 * @note Return value will be NULL if not specified explictly
270 _PUBLIC_
const char *cli_credentials_get_bind_dn(struct cli_credentials
*cred
)
272 return cred
->bind_dn
;
277 * Obtain the client principal for this credentials context.
278 * @param cred credentials context
279 * @retval The username set on this context.
280 * @note Return value will never be NULL except by programmer error.
282 _PUBLIC_
const char *cli_credentials_get_principal_and_obtained(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
, enum credentials_obtained
*obtained
)
284 if (cred
->machine_account_pending
) {
285 cli_credentials_set_machine_account(cred
,
286 cred
->machine_account_pending_lp_ctx
);
289 if (cred
->principal_obtained
== CRED_CALLBACK
&&
290 !cred
->callback_running
) {
291 cred
->callback_running
= true;
292 cred
->principal
= cred
->principal_cb(cred
);
293 cred
->callback_running
= false;
294 if (cred
->principal_obtained
== CRED_CALLBACK
) {
295 cred
->principal_obtained
= CRED_CALLBACK_RESULT
;
296 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
300 if (cred
->principal_obtained
< cred
->username_obtained
301 || cred
->principal_obtained
< MAX(cred
->domain_obtained
, cred
->realm_obtained
)) {
302 if (cred
->domain_obtained
> cred
->realm_obtained
) {
303 *obtained
= MIN(cred
->domain_obtained
, cred
->username_obtained
);
304 return talloc_asprintf(mem_ctx
, "%s@%s",
305 cli_credentials_get_username(cred
),
306 cli_credentials_get_domain(cred
));
308 *obtained
= MIN(cred
->domain_obtained
, cred
->username_obtained
);
309 return talloc_asprintf(mem_ctx
, "%s@%s",
310 cli_credentials_get_username(cred
),
311 cli_credentials_get_realm(cred
));
314 *obtained
= cred
->principal_obtained
;
315 return talloc_strdup(mem_ctx
, cred
->principal
);
319 * Obtain the client principal for this credentials context.
320 * @param cred credentials context
321 * @retval The username set on this context.
322 * @note Return value will never be NULL except by programmer error.
324 _PUBLIC_
const char *cli_credentials_get_principal(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
)
326 enum credentials_obtained obtained
;
327 return cli_credentials_get_principal_and_obtained(cred
, mem_ctx
, &obtained
);
330 _PUBLIC_
bool cli_credentials_set_principal(struct cli_credentials
*cred
,
332 enum credentials_obtained obtained
)
334 if (obtained
>= cred
->principal_obtained
) {
335 cred
->principal
= talloc_strdup(cred
, val
);
336 cred
->principal_obtained
= obtained
;
337 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
344 /* Set a callback to get the principal. This could be a popup dialog,
345 * a terminal prompt or similar. */
346 _PUBLIC_
bool cli_credentials_set_principal_callback(struct cli_credentials
*cred
,
347 const char *(*principal_cb
) (struct cli_credentials
*))
349 if (cred
->principal_obtained
< CRED_CALLBACK
) {
350 cred
->principal_cb
= principal_cb
;
351 cred
->principal_obtained
= CRED_CALLBACK
;
358 /* Some of our tools are 'anonymous by default'. This is a single
359 * function to determine if authentication has been explicitly
362 _PUBLIC_
bool cli_credentials_authentication_requested(struct cli_credentials
*cred
)
369 * If we forced the mech we clearly want authentication. E.g. to use
370 * SASL/EXTERNAL which has no credentials.
372 if (cred
->forced_sasl_mech
) {
376 if (cli_credentials_is_anonymous(cred
)){
380 if (cred
->principal_obtained
>= CRED_SPECIFIED
) {
383 if (cred
->username_obtained
>= CRED_SPECIFIED
) {
387 if (cli_credentials_get_kerberos_state(cred
) == CRED_MUST_USE_KERBEROS
) {
395 * Obtain the password for this credentials context.
396 * @param cred credentials context
397 * @retval If set, the cleartext password, otherwise NULL
399 _PUBLIC_
const char *cli_credentials_get_password(struct cli_credentials
*cred
)
401 if (cred
->machine_account_pending
) {
402 cli_credentials_set_machine_account(cred
,
403 cred
->machine_account_pending_lp_ctx
);
406 if (cred
->password_obtained
== CRED_CALLBACK
&&
407 !cred
->callback_running
) {
408 cred
->callback_running
= true;
409 cred
->password
= cred
->password_cb(cred
);
410 cred
->callback_running
= false;
411 if (cred
->password_obtained
== CRED_CALLBACK
) {
412 cred
->password_obtained
= CRED_CALLBACK_RESULT
;
413 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
417 return cred
->password
;
420 /* Set a password on the credentials context, including an indication
421 * of 'how' the password was obtained */
423 _PUBLIC_
bool cli_credentials_set_password(struct cli_credentials
*cred
,
425 enum credentials_obtained obtained
)
427 if (obtained
>= cred
->password_obtained
) {
428 cred
->password_tries
= 0;
429 cred
->password
= talloc_strdup(cred
, val
);
430 if (cred
->password
) {
431 /* Don't print the actual password in talloc memory dumps */
432 talloc_set_name_const(cred
->password
, "password set via cli_credentials_set_password");
434 cred
->password_obtained
= obtained
;
435 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
437 cred
->nt_hash
= NULL
;
438 cred
->lm_response
= data_blob(NULL
, 0);
439 cred
->nt_response
= data_blob(NULL
, 0);
446 _PUBLIC_
bool cli_credentials_set_password_callback(struct cli_credentials
*cred
,
447 const char *(*password_cb
) (struct cli_credentials
*))
449 if (cred
->password_obtained
< CRED_CALLBACK
) {
450 cred
->password_tries
= 3;
451 cred
->password_cb
= password_cb
;
452 cred
->password_obtained
= CRED_CALLBACK
;
453 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
461 * Obtain the 'old' password for this credentials context (used for join accounts).
462 * @param cred credentials context
463 * @retval If set, the cleartext password, otherwise NULL
465 _PUBLIC_
const char *cli_credentials_get_old_password(struct cli_credentials
*cred
)
467 if (cred
->machine_account_pending
) {
468 cli_credentials_set_machine_account(cred
,
469 cred
->machine_account_pending_lp_ctx
);
472 return cred
->old_password
;
475 _PUBLIC_
bool cli_credentials_set_old_password(struct cli_credentials
*cred
,
477 enum credentials_obtained obtained
)
479 cred
->old_password
= talloc_strdup(cred
, val
);
480 if (cred
->old_password
) {
481 /* Don't print the actual password in talloc memory dumps */
482 talloc_set_name_const(cred
->old_password
, "password set via cli_credentials_set_old_password");
488 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
490 * Sometimes we only have this much of the password, while the rest of
491 * the time this call avoids calling E_md4hash themselves.
493 * @param cred credentials context
494 * @retval If set, the cleartext password, otherwise NULL
496 _PUBLIC_
struct samr_Password
*cli_credentials_get_nt_hash(struct cli_credentials
*cred
,
499 const char *password
= cli_credentials_get_password(cred
);
502 struct samr_Password
*nt_hash
= talloc(mem_ctx
, struct samr_Password
);
507 E_md4hash(password
, nt_hash
->hash
);
510 } else if (cred
->nt_hash
!= NULL
) {
511 struct samr_Password
*nt_hash
= talloc(mem_ctx
, struct samr_Password
);
516 *nt_hash
= *cred
->nt_hash
;
525 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
526 * @param cred credentials context
527 * @retval The domain set on this context.
528 * @note Return value will never be NULL except by programmer error.
530 _PUBLIC_
const char *cli_credentials_get_domain(struct cli_credentials
*cred
)
532 if (cred
->machine_account_pending
) {
533 cli_credentials_set_machine_account(cred
,
534 cred
->machine_account_pending_lp_ctx
);
537 if (cred
->domain_obtained
== CRED_CALLBACK
&&
538 !cred
->callback_running
) {
539 cred
->callback_running
= true;
540 cred
->domain
= cred
->domain_cb(cred
);
541 cred
->callback_running
= false;
542 if (cred
->domain_obtained
== CRED_CALLBACK
) {
543 cred
->domain_obtained
= CRED_CALLBACK_RESULT
;
544 cli_credentials_invalidate_ccache(cred
, cred
->domain_obtained
);
552 _PUBLIC_
bool cli_credentials_set_domain(struct cli_credentials
*cred
,
554 enum credentials_obtained obtained
)
556 if (obtained
>= cred
->domain_obtained
) {
557 /* it is important that the domain be in upper case,
558 * particularly for the sensitive NTLMv2
560 cred
->domain
= strupper_talloc(cred
, val
);
561 cred
->domain_obtained
= obtained
;
562 /* setting domain does not mean we have to invalidate ccache
563 * because domain in not used for Kerberos operations.
564 * If ccache invalidation is required, one will anyway specify
565 * a password to kinit, and that will force invalidation of the ccache
573 bool cli_credentials_set_domain_callback(struct cli_credentials
*cred
,
574 const char *(*domain_cb
) (struct cli_credentials
*))
576 if (cred
->domain_obtained
< CRED_CALLBACK
) {
577 cred
->domain_cb
= domain_cb
;
578 cred
->domain_obtained
= CRED_CALLBACK
;
586 * Obtain the Kerberos realm for this credentials context.
587 * @param cred credentials context
588 * @retval The realm set on this context.
589 * @note Return value will never be NULL except by programmer error.
591 _PUBLIC_
const char *cli_credentials_get_realm(struct cli_credentials
*cred
)
593 if (cred
->machine_account_pending
) {
594 cli_credentials_set_machine_account(cred
,
595 cred
->machine_account_pending_lp_ctx
);
598 if (cred
->realm_obtained
== CRED_CALLBACK
&&
599 !cred
->callback_running
) {
600 cred
->callback_running
= true;
601 cred
->realm
= cred
->realm_cb(cred
);
602 cred
->callback_running
= false;
603 if (cred
->realm_obtained
== CRED_CALLBACK
) {
604 cred
->realm_obtained
= CRED_CALLBACK_RESULT
;
605 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
613 * Set the realm for this credentials context, and force it to
614 * uppercase for the sainity of our local kerberos libraries
616 _PUBLIC_
bool cli_credentials_set_realm(struct cli_credentials
*cred
,
618 enum credentials_obtained obtained
)
620 if (obtained
>= cred
->realm_obtained
) {
621 cred
->realm
= strupper_talloc(cred
, val
);
622 cred
->realm_obtained
= obtained
;
623 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
630 bool cli_credentials_set_realm_callback(struct cli_credentials
*cred
,
631 const char *(*realm_cb
) (struct cli_credentials
*))
633 if (cred
->realm_obtained
< CRED_CALLBACK
) {
634 cred
->realm_cb
= realm_cb
;
635 cred
->realm_obtained
= CRED_CALLBACK
;
643 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
645 * @param cred credentials context
646 * @retval The workstation name set on this context.
647 * @note Return value will never be NULL except by programmer error.
649 _PUBLIC_
const char *cli_credentials_get_workstation(struct cli_credentials
*cred
)
651 if (cred
->workstation_obtained
== CRED_CALLBACK
&&
652 !cred
->callback_running
) {
653 cred
->callback_running
= true;
654 cred
->workstation
= cred
->workstation_cb(cred
);
655 cred
->callback_running
= false;
656 if (cred
->workstation_obtained
== CRED_CALLBACK
) {
657 cred
->workstation_obtained
= CRED_CALLBACK_RESULT
;
661 return cred
->workstation
;
664 _PUBLIC_
bool cli_credentials_set_workstation(struct cli_credentials
*cred
,
666 enum credentials_obtained obtained
)
668 if (obtained
>= cred
->workstation_obtained
) {
669 cred
->workstation
= talloc_strdup(cred
, val
);
670 cred
->workstation_obtained
= obtained
;
677 bool cli_credentials_set_workstation_callback(struct cli_credentials
*cred
,
678 const char *(*workstation_cb
) (struct cli_credentials
*))
680 if (cred
->workstation_obtained
< CRED_CALLBACK
) {
681 cred
->workstation_cb
= workstation_cb
;
682 cred
->workstation_obtained
= CRED_CALLBACK
;
690 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
692 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
694 * @param credentials Credentials structure on which to set the password
695 * @param data the string containing the username, password etc
696 * @param obtained This enum describes how 'specified' this password is
699 _PUBLIC_
void cli_credentials_parse_string(struct cli_credentials
*credentials
, const char *data
, enum credentials_obtained obtained
)
703 if (strcmp("%",data
) == 0) {
704 cli_credentials_set_anonymous(credentials
);
708 uname
= talloc_strdup(credentials
, data
);
709 if ((p
= strchr_m(uname
,'%'))) {
711 cli_credentials_set_password(credentials
, p
+1, obtained
);
714 if ((p
= strchr_m(uname
,'@'))) {
715 cli_credentials_set_principal(credentials
, uname
, obtained
);
717 cli_credentials_set_realm(credentials
, p
+1, obtained
);
719 } else if ((p
= strchr_m(uname
,'\\')) || (p
= strchr_m(uname
, '/'))) {
721 cli_credentials_set_domain(credentials
, uname
, obtained
);
724 cli_credentials_set_username(credentials
, uname
, obtained
);
728 * Given a a credentials structure, print it as a string
730 * The format output is [domain\\]user[%password] or user[@realm][%password]
732 * @param credentials Credentials structure on which to set the password
733 * @param mem_ctx The memory context to place the result on
736 _PUBLIC_
const char *cli_credentials_get_unparsed_name(struct cli_credentials
*credentials
, TALLOC_CTX
*mem_ctx
)
738 const char *bind_dn
= cli_credentials_get_bind_dn(credentials
);
740 const char *username
;
744 name
= talloc_strdup(mem_ctx
, bind_dn
);
746 cli_credentials_get_ntlm_username_domain(credentials
, mem_ctx
, &username
, &domain
);
747 if (domain
&& domain
[0]) {
748 name
= talloc_asprintf(mem_ctx
, "%s\\%s",
751 name
= talloc_asprintf(mem_ctx
, "%s",
759 * Specifies default values for domain, workstation and realm
760 * from the smb.conf configuration file
762 * @param cred Credentials structure to fill in
764 _PUBLIC_
void cli_credentials_set_conf(struct cli_credentials
*cred
,
765 struct loadparm_context
*lp_ctx
)
767 cli_credentials_set_username(cred
, "", CRED_UNINITIALISED
);
768 if (lpcfg_parm_is_cmdline(lp_ctx
, "workgroup")) {
769 cli_credentials_set_domain(cred
, lpcfg_workgroup(lp_ctx
), CRED_SPECIFIED
);
771 cli_credentials_set_domain(cred
, lpcfg_workgroup(lp_ctx
), CRED_UNINITIALISED
);
773 if (lpcfg_parm_is_cmdline(lp_ctx
, "netbios name")) {
774 cli_credentials_set_workstation(cred
, lpcfg_netbios_name(lp_ctx
), CRED_SPECIFIED
);
776 cli_credentials_set_workstation(cred
, lpcfg_netbios_name(lp_ctx
), CRED_UNINITIALISED
);
778 if (lpcfg_parm_is_cmdline(lp_ctx
, "realm")) {
779 cli_credentials_set_realm(cred
, lpcfg_realm(lp_ctx
), CRED_SPECIFIED
);
781 cli_credentials_set_realm(cred
, lpcfg_realm(lp_ctx
), CRED_UNINITIALISED
);
786 * Guess defaults for credentials from environment variables,
787 * and from the configuration file
789 * @param cred Credentials structure to fill in
791 _PUBLIC_
void cli_credentials_guess(struct cli_credentials
*cred
,
792 struct loadparm_context
*lp_ctx
)
795 const char *error_string
;
797 if (lp_ctx
!= NULL
) {
798 cli_credentials_set_conf(cred
, lp_ctx
);
801 if (getenv("LOGNAME")) {
802 cli_credentials_set_username(cred
, getenv("LOGNAME"), CRED_GUESS_ENV
);
805 if (getenv("USER")) {
806 cli_credentials_parse_string(cred
, getenv("USER"), CRED_GUESS_ENV
);
807 if ((p
= strchr_m(getenv("USER"),'%'))) {
808 memset(p
,0,strlen(cred
->password
));
812 if (getenv("PASSWD")) {
813 cli_credentials_set_password(cred
, getenv("PASSWD"), CRED_GUESS_ENV
);
816 if (getenv("PASSWD_FD")) {
817 cli_credentials_parse_password_fd(cred
, atoi(getenv("PASSWD_FD")),
821 p
= getenv("PASSWD_FILE");
823 cli_credentials_parse_password_file(cred
, p
, CRED_GUESS_FILE
);
826 if (cli_credentials_get_kerberos_state(cred
) != CRED_DONT_USE_KERBEROS
) {
827 cli_credentials_set_ccache(cred
, lp_ctx
, NULL
, CRED_GUESS_FILE
,
833 * Attach NETLOGON credentials for use with SCHANNEL
836 _PUBLIC_
void cli_credentials_set_netlogon_creds(struct cli_credentials
*cred
,
837 struct netlogon_creds_CredentialState
*netlogon_creds
)
839 TALLOC_FREE(cred
->netlogon_creds
);
840 if (netlogon_creds
== NULL
) {
843 cred
->netlogon_creds
= netlogon_creds_copy(cred
, netlogon_creds
);
847 * Return attached NETLOGON credentials
850 _PUBLIC_
struct netlogon_creds_CredentialState
*cli_credentials_get_netlogon_creds(struct cli_credentials
*cred
)
852 return cred
->netlogon_creds
;
856 * Set NETLOGON secure channel type
859 _PUBLIC_
void cli_credentials_set_secure_channel_type(struct cli_credentials
*cred
,
860 enum netr_SchannelType secure_channel_type
)
862 cred
->secure_channel_type
= secure_channel_type
;
866 * Return NETLOGON secure chanel type
869 _PUBLIC_
time_t cli_credentials_get_password_last_changed_time(struct cli_credentials
*cred
)
871 return cred
->password_last_changed_time
;
875 * Set NETLOGON secure channel type
878 _PUBLIC_
void cli_credentials_set_password_last_changed_time(struct cli_credentials
*cred
,
879 time_t last_changed_time
)
881 cred
->password_last_changed_time
= last_changed_time
;
885 * Return NETLOGON secure chanel type
888 _PUBLIC_
enum netr_SchannelType
cli_credentials_get_secure_channel_type(struct cli_credentials
*cred
)
890 return cred
->secure_channel_type
;
894 * Fill in a credentials structure as the anonymous user
896 _PUBLIC_
void cli_credentials_set_anonymous(struct cli_credentials
*cred
)
898 cli_credentials_set_username(cred
, "", CRED_SPECIFIED
);
899 cli_credentials_set_domain(cred
, "", CRED_SPECIFIED
);
900 cli_credentials_set_password(cred
, NULL
, CRED_SPECIFIED
);
901 cli_credentials_set_realm(cred
, NULL
, CRED_SPECIFIED
);
902 cli_credentials_set_workstation(cred
, "", CRED_UNINITIALISED
);
906 * Describe a credentials context as anonymous or authenticated
907 * @retval true if anonymous, false if a username is specified
910 _PUBLIC_
bool cli_credentials_is_anonymous(struct cli_credentials
*cred
)
912 const char *username
;
914 /* if bind dn is set it's not anonymous */
919 if (cred
->machine_account_pending
) {
920 cli_credentials_set_machine_account(cred
,
921 cred
->machine_account_pending_lp_ctx
);
924 username
= cli_credentials_get_username(cred
);
926 /* Yes, it is deliberate that we die if we have a NULL pointer
927 * here - anonymous is "", not NULL, which is 'never specified,
928 * never guessed', ie programmer bug */
937 * Mark the current password for a credentials struct as wrong. This will
938 * cause the password to be prompted again (if a callback is set).
940 * This will decrement the number of times the password can be tried.
942 * @retval whether the credentials struct is finished
944 _PUBLIC_
bool cli_credentials_wrong_password(struct cli_credentials
*cred
)
946 if (cred
->password_obtained
!= CRED_CALLBACK_RESULT
) {
950 if (cred
->password_tries
== 0) {
954 cred
->password_tries
--;
956 if (cred
->password_tries
== 0) {
960 cred
->password_obtained
= CRED_CALLBACK
;
964 _PUBLIC_
void cli_credentials_get_ntlm_username_domain(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
,
965 const char **username
,
968 if (cred
->principal_obtained
> cred
->username_obtained
) {
969 *domain
= talloc_strdup(mem_ctx
, "");
970 *username
= cli_credentials_get_principal(cred
, mem_ctx
);
972 *domain
= cli_credentials_get_domain(cred
);
973 *username
= cli_credentials_get_username(cred
);
978 * Read a named file, and parse it for username, domain, realm and password
980 * @param credentials Credentials structure on which to set the password
981 * @param file a named file to read the details from
982 * @param obtained This enum describes how 'specified' this password is
985 _PUBLIC_
bool cli_credentials_parse_file(struct cli_credentials
*cred
, const char *file
, enum credentials_obtained obtained
)
988 char *ptr
, *val
, *param
;
992 lines
= file_lines_load(file
, &numlines
, 0, NULL
);
996 /* fail if we can't open the credentials file */
997 d_printf("ERROR: Unable to open credentials file!\n");
1001 for (i
= 0; i
< numlines
; i
++) {
1002 len
= strlen(lines
[i
]);
1007 /* break up the line into parameter & value.
1008 * will need to eat a little whitespace possibly */
1010 if (!(ptr
= strchr_m (lines
[i
], '=')))
1016 /* eat leading white space */
1017 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
1020 if (strwicmp("password", param
) == 0) {
1021 cli_credentials_set_password(cred
, val
, obtained
);
1022 } else if (strwicmp("username", param
) == 0) {
1023 cli_credentials_set_username(cred
, val
, obtained
);
1024 } else if (strwicmp("domain", param
) == 0) {
1025 cli_credentials_set_domain(cred
, val
, obtained
);
1026 } else if (strwicmp("realm", param
) == 0) {
1027 cli_credentials_set_realm(cred
, val
, obtained
);
1029 memset(lines
[i
], 0, len
);
1038 * Read a named file, and parse it for a password
1040 * @param credentials Credentials structure on which to set the password
1041 * @param file a named file to read the password from
1042 * @param obtained This enum describes how 'specified' this password is
1045 _PUBLIC_
bool cli_credentials_parse_password_file(struct cli_credentials
*credentials
, const char *file
, enum credentials_obtained obtained
)
1047 int fd
= open(file
, O_RDONLY
, 0);
1051 fprintf(stderr
, "Error opening password file %s: %s\n",
1052 file
, strerror(errno
));
1056 ret
= cli_credentials_parse_password_fd(credentials
, fd
, obtained
);
1065 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1067 * @param credentials Credentials structure on which to set the password
1068 * @param fd open file descriptor to read the password from
1069 * @param obtained This enum describes how 'specified' this password is
1072 _PUBLIC_
bool cli_credentials_parse_password_fd(struct cli_credentials
*credentials
,
1073 int fd
, enum credentials_obtained obtained
)
1078 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
1079 p
&& p
- pass
< sizeof(pass
);) {
1080 switch (read(fd
, p
, 1)) {
1082 if (*p
!= '\n' && *p
!= '\0') {
1083 *++p
= '\0'; /* advance p, and null-terminate pass */
1089 *p
= '\0'; /* null-terminate it, just in case... */
1090 p
= NULL
; /* then force the loop condition to become false */
1093 fprintf(stderr
, "Error reading password from file descriptor %d: %s\n", fd
, "empty password\n");
1098 fprintf(stderr
, "Error reading password from file descriptor %d: %s\n",
1099 fd
, strerror(errno
));
1104 cli_credentials_set_password(credentials
, pass
, obtained
);