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 "libcli/auth/libcli_auth.h"
28 #include "lib/events/events.h"
29 #include "param/param.h"
30 #include "system/filesys.h"
33 * Create a new credentials structure
34 * @param mem_ctx TALLOC_CTX parent for credentials structure
36 _PUBLIC_
struct cli_credentials
*cli_credentials_init(TALLOC_CTX
*mem_ctx
)
38 struct cli_credentials
*cred
= talloc(mem_ctx
, struct cli_credentials
);
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
->principal_obtained
= CRED_UNINITIALISED
;
51 cred
->keytab_obtained
= CRED_UNINITIALISED
;
52 cred
->server_gss_creds_obtained
= CRED_UNINITIALISED
;
54 cred
->ccache_threshold
= CRED_UNINITIALISED
;
55 cred
->client_gss_creds_threshold
= CRED_UNINITIALISED
;
57 cred
->workstation
= NULL
;
58 cred
->username
= NULL
;
59 cred
->password
= NULL
;
60 cred
->old_password
= NULL
;
63 cred
->principal
= NULL
;
64 cred
->salt_principal
= NULL
;
65 cred
->impersonate_principal
= NULL
;
66 cred
->target_service
= NULL
;
72 cred
->lm_response
.data
= NULL
;
73 cred
->lm_response
.length
= 0;
74 cred
->nt_response
.data
= NULL
;
75 cred
->nt_response
.length
= 0;
78 cred
->client_gss_creds
= NULL
;
80 cred
->server_gss_creds
= NULL
;
82 cred
->workstation_cb
= NULL
;
83 cred
->password_cb
= NULL
;
84 cred
->username_cb
= NULL
;
85 cred
->domain_cb
= NULL
;
86 cred
->realm_cb
= NULL
;
87 cred
->principal_cb
= NULL
;
89 cred
->priv_data
= NULL
;
91 cred
->netlogon_creds
= NULL
;
92 cred
->secure_channel_type
= SEC_CHAN_NULL
;
96 cred
->password_last_changed_time
= 0;
98 cred
->smb_krb5_context
= NULL
;
100 cred
->machine_account_pending
= false;
101 cred
->machine_account_pending_lp_ctx
= NULL
;
103 cred
->machine_account
= false;
107 cred
->callback_running
= false;
109 cli_credentials_set_kerberos_state(cred
, CRED_AUTO_USE_KERBEROS
);
110 cli_credentials_set_gensec_features(cred
, 0);
111 cli_credentials_set_krb_forwardable(cred
, CRED_AUTO_KRB_FORWARDABLE
);
117 * Create a new anonymous credential
118 * @param mem_ctx TALLOC_CTX parent for credentials structure
120 _PUBLIC_
struct cli_credentials
*cli_credentials_init_anon(TALLOC_CTX
*mem_ctx
)
122 struct cli_credentials
*anon_credentials
;
124 anon_credentials
= cli_credentials_init(mem_ctx
);
125 cli_credentials_set_anonymous(anon_credentials
);
127 return anon_credentials
;
130 _PUBLIC_
void cli_credentials_set_kerberos_state(struct cli_credentials
*creds
,
131 enum credentials_use_kerberos use_kerberos
)
133 creds
->use_kerberos
= use_kerberos
;
136 _PUBLIC_
void cli_credentials_set_krb_forwardable(struct cli_credentials
*creds
,
137 enum credentials_krb_forwardable krb_forwardable
)
139 creds
->krb_forwardable
= krb_forwardable
;
142 _PUBLIC_
enum credentials_use_kerberos
cli_credentials_get_kerberos_state(struct cli_credentials
*creds
)
144 return creds
->use_kerberos
;
147 _PUBLIC_
enum credentials_krb_forwardable
cli_credentials_get_krb_forwardable(struct cli_credentials
*creds
)
149 return creds
->krb_forwardable
;
152 _PUBLIC_
void cli_credentials_set_gensec_features(struct cli_credentials
*creds
, uint32_t gensec_features
)
154 creds
->gensec_features
= gensec_features
;
157 _PUBLIC_
uint32_t cli_credentials_get_gensec_features(struct cli_credentials
*creds
)
159 return creds
->gensec_features
;
164 * Obtain the username for this credentials context.
165 * @param cred credentials context
166 * @retval The username set on this context.
167 * @note Return value will never be NULL except by programmer error.
169 _PUBLIC_
const char *cli_credentials_get_username(struct cli_credentials
*cred
)
171 if (cred
->machine_account_pending
) {
172 cli_credentials_set_machine_account(cred
,
173 cred
->machine_account_pending_lp_ctx
);
176 if (cred
->username_obtained
== CRED_CALLBACK
&&
177 !cred
->callback_running
) {
178 cred
->callback_running
= true;
179 cred
->username
= cred
->username_cb(cred
);
180 cred
->callback_running
= false;
181 cred
->username_obtained
= CRED_SPECIFIED
;
182 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
185 return cred
->username
;
188 _PUBLIC_
bool cli_credentials_set_username(struct cli_credentials
*cred
,
189 const char *val
, enum credentials_obtained obtained
)
191 if (obtained
>= cred
->username_obtained
) {
192 cred
->username
= talloc_strdup(cred
, val
);
193 cred
->username_obtained
= obtained
;
194 cli_credentials_invalidate_ccache(cred
, cred
->username_obtained
);
201 bool cli_credentials_set_username_callback(struct cli_credentials
*cred
,
202 const char *(*username_cb
) (struct cli_credentials
*))
204 if (cred
->username_obtained
< CRED_CALLBACK
) {
205 cred
->username_cb
= username_cb
;
206 cred
->username_obtained
= CRED_CALLBACK
;
213 _PUBLIC_
bool cli_credentials_set_bind_dn(struct cli_credentials
*cred
,
216 cred
->bind_dn
= talloc_strdup(cred
, bind_dn
);
221 * Obtain the BIND DN for this credentials context.
222 * @param cred credentials context
223 * @retval The username set on this context.
224 * @note Return value will be NULL if not specified explictly
226 _PUBLIC_
const char *cli_credentials_get_bind_dn(struct cli_credentials
*cred
)
228 return cred
->bind_dn
;
233 * Obtain the client principal for this credentials context.
234 * @param cred credentials context
235 * @retval The username set on this context.
236 * @note Return value will never be NULL except by programmer error.
238 const char *cli_credentials_get_principal_and_obtained(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
, enum credentials_obtained
*obtained
)
240 if (cred
->machine_account_pending
) {
241 cli_credentials_set_machine_account(cred
,
242 cred
->machine_account_pending_lp_ctx
);
245 if (cred
->principal_obtained
== CRED_CALLBACK
&&
246 !cred
->callback_running
) {
247 cred
->callback_running
= true;
248 cred
->principal
= cred
->principal_cb(cred
);
249 cred
->callback_running
= false;
250 cred
->principal_obtained
= CRED_SPECIFIED
;
251 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
254 if (cred
->principal_obtained
< cred
->username_obtained
255 || cred
->principal_obtained
< MAX(cred
->domain_obtained
, cred
->realm_obtained
)) {
256 if (cred
->domain_obtained
> cred
->realm_obtained
) {
257 *obtained
= MIN(cred
->domain_obtained
, cred
->username_obtained
);
258 return talloc_asprintf(mem_ctx
, "%s@%s",
259 cli_credentials_get_username(cred
),
260 cli_credentials_get_domain(cred
));
262 *obtained
= MIN(cred
->domain_obtained
, cred
->username_obtained
);
263 return talloc_asprintf(mem_ctx
, "%s@%s",
264 cli_credentials_get_username(cred
),
265 cli_credentials_get_realm(cred
));
268 *obtained
= cred
->principal_obtained
;
269 return talloc_reference(mem_ctx
, cred
->principal
);
273 * Obtain the client principal for this credentials context.
274 * @param cred credentials context
275 * @retval The username set on this context.
276 * @note Return value will never be NULL except by programmer error.
278 _PUBLIC_
const char *cli_credentials_get_principal(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
)
280 enum credentials_obtained obtained
;
281 return cli_credentials_get_principal_and_obtained(cred
, mem_ctx
, &obtained
);
284 bool cli_credentials_set_principal(struct cli_credentials
*cred
,
286 enum credentials_obtained obtained
)
288 if (obtained
>= cred
->principal_obtained
) {
289 cred
->principal
= talloc_strdup(cred
, val
);
290 cred
->principal_obtained
= obtained
;
291 cli_credentials_invalidate_ccache(cred
, cred
->principal_obtained
);
298 /* Set a callback to get the principal. This could be a popup dialog,
299 * a terminal prompt or similar. */
300 bool cli_credentials_set_principal_callback(struct cli_credentials
*cred
,
301 const char *(*principal_cb
) (struct cli_credentials
*))
303 if (cred
->principal_obtained
< CRED_CALLBACK
) {
304 cred
->principal_cb
= principal_cb
;
305 cred
->principal_obtained
= CRED_CALLBACK
;
312 /* Some of our tools are 'anonymous by default'. This is a single
313 * function to determine if authentication has been explicitly
316 _PUBLIC_
bool cli_credentials_authentication_requested(struct cli_credentials
*cred
)
322 if (cli_credentials_is_anonymous(cred
)){
326 if (cred
->principal_obtained
>= CRED_SPECIFIED
) {
329 if (cred
->username_obtained
>= CRED_SPECIFIED
) {
333 if (cli_credentials_get_kerberos_state(cred
) == CRED_MUST_USE_KERBEROS
) {
341 * Obtain the password for this credentials context.
342 * @param cred credentials context
343 * @retval If set, the cleartext password, otherwise NULL
345 _PUBLIC_
const char *cli_credentials_get_password(struct cli_credentials
*cred
)
347 if (cred
->machine_account_pending
) {
348 cli_credentials_set_machine_account(cred
,
349 cred
->machine_account_pending_lp_ctx
);
352 if (cred
->password_obtained
== CRED_CALLBACK
&&
353 !cred
->callback_running
) {
354 cred
->callback_running
= true;
355 cred
->password
= cred
->password_cb(cred
);
356 cred
->callback_running
= false;
357 cred
->password_obtained
= CRED_CALLBACK_RESULT
;
358 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
361 return cred
->password
;
364 /* Set a password on the credentials context, including an indication
365 * of 'how' the password was obtained */
367 _PUBLIC_
bool cli_credentials_set_password(struct cli_credentials
*cred
,
369 enum credentials_obtained obtained
)
371 if (obtained
>= cred
->password_obtained
) {
372 cred
->password
= talloc_strdup(cred
, val
);
373 cred
->password_obtained
= obtained
;
374 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
376 cred
->nt_hash
= NULL
;
377 cred
->lm_response
= data_blob(NULL
, 0);
378 cred
->nt_response
= data_blob(NULL
, 0);
385 _PUBLIC_
bool cli_credentials_set_password_callback(struct cli_credentials
*cred
,
386 const char *(*password_cb
) (struct cli_credentials
*))
388 if (cred
->password_obtained
< CRED_CALLBACK
) {
389 cred
->password_cb
= password_cb
;
390 cred
->password_obtained
= CRED_CALLBACK
;
391 cli_credentials_invalidate_ccache(cred
, cred
->password_obtained
);
399 * Obtain the 'old' password for this credentials context (used for join accounts).
400 * @param cred credentials context
401 * @retval If set, the cleartext password, otherwise NULL
403 const char *cli_credentials_get_old_password(struct cli_credentials
*cred
)
405 if (cred
->machine_account_pending
) {
406 cli_credentials_set_machine_account(cred
,
407 cred
->machine_account_pending_lp_ctx
);
410 return cred
->old_password
;
413 bool cli_credentials_set_old_password(struct cli_credentials
*cred
,
415 enum credentials_obtained obtained
)
417 cred
->old_password
= talloc_strdup(cred
, val
);
422 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
424 * Sometimes we only have this much of the password, while the rest of
425 * the time this call avoids calling E_md4hash themselves.
427 * @param cred credentials context
428 * @retval If set, the cleartext password, otherwise NULL
430 _PUBLIC_
const struct samr_Password
*cli_credentials_get_nt_hash(struct cli_credentials
*cred
,
433 const char *password
= cli_credentials_get_password(cred
);
436 struct samr_Password
*nt_hash
= talloc(mem_ctx
, struct samr_Password
);
441 E_md4hash(password
, nt_hash
->hash
);
445 return cred
->nt_hash
;
450 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
451 * @param cred credentials context
452 * @retval The domain set on this context.
453 * @note Return value will never be NULL except by programmer error.
455 _PUBLIC_
const char *cli_credentials_get_domain(struct cli_credentials
*cred
)
457 if (cred
->machine_account_pending
) {
458 cli_credentials_set_machine_account(cred
,
459 cred
->machine_account_pending_lp_ctx
);
462 if (cred
->domain_obtained
== CRED_CALLBACK
&&
463 !cred
->callback_running
) {
464 cred
->callback_running
= true;
465 cred
->domain
= cred
->domain_cb(cred
);
466 cred
->callback_running
= false;
467 cred
->domain_obtained
= CRED_SPECIFIED
;
468 cli_credentials_invalidate_ccache(cred
, cred
->domain_obtained
);
475 _PUBLIC_
bool cli_credentials_set_domain(struct cli_credentials
*cred
,
477 enum credentials_obtained obtained
)
479 if (obtained
>= cred
->domain_obtained
) {
480 /* it is important that the domain be in upper case,
481 * particularly for the sensitive NTLMv2
483 cred
->domain
= strupper_talloc(cred
, val
);
484 cred
->domain_obtained
= obtained
;
485 cli_credentials_invalidate_ccache(cred
, cred
->domain_obtained
);
492 bool cli_credentials_set_domain_callback(struct cli_credentials
*cred
,
493 const char *(*domain_cb
) (struct cli_credentials
*))
495 if (cred
->domain_obtained
< CRED_CALLBACK
) {
496 cred
->domain_cb
= domain_cb
;
497 cred
->domain_obtained
= CRED_CALLBACK
;
505 * Obtain the Kerberos realm for this credentials context.
506 * @param cred credentials context
507 * @retval The realm set on this context.
508 * @note Return value will never be NULL except by programmer error.
510 _PUBLIC_
const char *cli_credentials_get_realm(struct cli_credentials
*cred
)
512 if (cred
->machine_account_pending
) {
513 cli_credentials_set_machine_account(cred
,
514 cred
->machine_account_pending_lp_ctx
);
517 if (cred
->realm_obtained
== CRED_CALLBACK
&&
518 !cred
->callback_running
) {
519 cred
->callback_running
= true;
520 cred
->realm
= cred
->realm_cb(cred
);
521 cred
->callback_running
= false;
522 cred
->realm_obtained
= CRED_SPECIFIED
;
523 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
530 * Set the realm for this credentials context, and force it to
531 * uppercase for the sainity of our local kerberos libraries
533 _PUBLIC_
bool cli_credentials_set_realm(struct cli_credentials
*cred
,
535 enum credentials_obtained obtained
)
537 if (obtained
>= cred
->realm_obtained
) {
538 cred
->realm
= strupper_talloc(cred
, val
);
539 cred
->realm_obtained
= obtained
;
540 cli_credentials_invalidate_ccache(cred
, cred
->realm_obtained
);
547 bool cli_credentials_set_realm_callback(struct cli_credentials
*cred
,
548 const char *(*realm_cb
) (struct cli_credentials
*))
550 if (cred
->realm_obtained
< CRED_CALLBACK
) {
551 cred
->realm_cb
= realm_cb
;
552 cred
->realm_obtained
= CRED_CALLBACK
;
560 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
562 * @param cred credentials context
563 * @retval The workstation name set on this context.
564 * @note Return value will never be NULL except by programmer error.
566 _PUBLIC_
const char *cli_credentials_get_workstation(struct cli_credentials
*cred
)
568 if (cred
->workstation_obtained
== CRED_CALLBACK
&&
569 !cred
->callback_running
) {
570 cred
->callback_running
= true;
571 cred
->workstation
= cred
->workstation_cb(cred
);
572 cred
->callback_running
= false;
573 cred
->workstation_obtained
= CRED_SPECIFIED
;
576 return cred
->workstation
;
579 _PUBLIC_
bool cli_credentials_set_workstation(struct cli_credentials
*cred
,
581 enum credentials_obtained obtained
)
583 if (obtained
>= cred
->workstation_obtained
) {
584 cred
->workstation
= talloc_strdup(cred
, val
);
585 cred
->workstation_obtained
= obtained
;
592 bool cli_credentials_set_workstation_callback(struct cli_credentials
*cred
,
593 const char *(*workstation_cb
) (struct cli_credentials
*))
595 if (cred
->workstation_obtained
< CRED_CALLBACK
) {
596 cred
->workstation_cb
= workstation_cb
;
597 cred
->workstation_obtained
= CRED_CALLBACK
;
605 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
607 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
609 * @param credentials Credentials structure on which to set the password
610 * @param data the string containing the username, password etc
611 * @param obtained This enum describes how 'specified' this password is
614 _PUBLIC_
void cli_credentials_parse_string(struct cli_credentials
*credentials
, const char *data
, enum credentials_obtained obtained
)
618 if (strcmp("%",data
) == 0) {
619 cli_credentials_set_anonymous(credentials
);
623 uname
= talloc_strdup(credentials
, data
);
624 if ((p
= strchr_m(uname
,'%'))) {
626 cli_credentials_set_password(credentials
, p
+1, obtained
);
629 if ((p
= strchr_m(uname
,'@'))) {
630 cli_credentials_set_principal(credentials
, uname
, obtained
);
632 cli_credentials_set_realm(credentials
, p
+1, obtained
);
634 } else if ((p
= strchr_m(uname
,'\\')) || (p
= strchr_m(uname
, '/'))) {
636 cli_credentials_set_domain(credentials
, uname
, obtained
);
639 cli_credentials_set_username(credentials
, uname
, obtained
);
643 * Given a a credentials structure, print it as a string
645 * The format output is [domain\\]user[%password] or user[@realm][%password]
647 * @param credentials Credentials structure on which to set the password
648 * @param mem_ctx The memory context to place the result on
651 _PUBLIC_
const char *cli_credentials_get_unparsed_name(struct cli_credentials
*credentials
, TALLOC_CTX
*mem_ctx
)
653 const char *bind_dn
= cli_credentials_get_bind_dn(credentials
);
655 const char *username
;
659 name
= talloc_reference(mem_ctx
, bind_dn
);
661 cli_credentials_get_ntlm_username_domain(credentials
, mem_ctx
, &username
, &domain
);
662 if (domain
&& domain
[0]) {
663 name
= talloc_asprintf(mem_ctx
, "%s\\%s",
666 name
= talloc_asprintf(mem_ctx
, "%s",
674 * Specifies default values for domain, workstation and realm
675 * from the smb.conf configuration file
677 * @param cred Credentials structure to fill in
679 _PUBLIC_
void cli_credentials_set_conf(struct cli_credentials
*cred
,
680 struct loadparm_context
*lp_ctx
)
682 cli_credentials_set_username(cred
, "", CRED_UNINITIALISED
);
683 cli_credentials_set_domain(cred
, lpcfg_workgroup(lp_ctx
), CRED_UNINITIALISED
);
684 cli_credentials_set_workstation(cred
, lpcfg_netbios_name(lp_ctx
), CRED_UNINITIALISED
);
685 cli_credentials_set_realm(cred
, lpcfg_realm(lp_ctx
), CRED_UNINITIALISED
);
689 * Guess defaults for credentials from environment variables,
690 * and from the configuration file
692 * @param cred Credentials structure to fill in
694 _PUBLIC_
void cli_credentials_guess(struct cli_credentials
*cred
,
695 struct loadparm_context
*lp_ctx
)
698 const char *error_string
;
700 if (lp_ctx
!= NULL
) {
701 cli_credentials_set_conf(cred
, lp_ctx
);
704 if (getenv("LOGNAME")) {
705 cli_credentials_set_username(cred
, getenv("LOGNAME"), CRED_GUESS_ENV
);
708 if (getenv("USER")) {
709 cli_credentials_parse_string(cred
, getenv("USER"), CRED_GUESS_ENV
);
710 if ((p
= strchr_m(getenv("USER"),'%'))) {
711 memset(p
,0,strlen(cred
->password
));
715 if (getenv("PASSWD")) {
716 cli_credentials_set_password(cred
, getenv("PASSWD"), CRED_GUESS_ENV
);
719 if (getenv("PASSWD_FD")) {
720 cli_credentials_parse_password_fd(cred
, atoi(getenv("PASSWD_FD")),
724 p
= getenv("PASSWD_FILE");
726 cli_credentials_parse_password_file(cred
, p
, CRED_GUESS_FILE
);
729 if (cli_credentials_get_kerberos_state(cred
) != CRED_DONT_USE_KERBEROS
) {
730 cli_credentials_set_ccache(cred
, lp_ctx
, NULL
, CRED_GUESS_FILE
,
736 * Attach NETLOGON credentials for use with SCHANNEL
739 _PUBLIC_
void cli_credentials_set_netlogon_creds(struct cli_credentials
*cred
,
740 struct netlogon_creds_CredentialState
*netlogon_creds
)
742 cred
->netlogon_creds
= talloc_reference(cred
, netlogon_creds
);
746 * Return attached NETLOGON credentials
749 struct netlogon_creds_CredentialState
*cli_credentials_get_netlogon_creds(struct cli_credentials
*cred
)
751 return cred
->netlogon_creds
;
755 * Set NETLOGON secure channel type
758 _PUBLIC_
void cli_credentials_set_secure_channel_type(struct cli_credentials
*cred
,
759 enum netr_SchannelType secure_channel_type
)
761 cred
->secure_channel_type
= secure_channel_type
;
765 * Return NETLOGON secure chanel type
768 _PUBLIC_
time_t cli_credentials_get_password_last_changed_time(struct cli_credentials
*cred
)
770 return cred
->password_last_changed_time
;
774 * Set NETLOGON secure channel type
777 _PUBLIC_
void cli_credentials_set_password_last_changed_time(struct cli_credentials
*cred
,
778 time_t last_changed_time
)
780 cred
->password_last_changed_time
= last_changed_time
;
784 * Return NETLOGON secure chanel type
787 _PUBLIC_
enum netr_SchannelType
cli_credentials_get_secure_channel_type(struct cli_credentials
*cred
)
789 return cred
->secure_channel_type
;
793 * Fill in a credentials structure as the anonymous user
795 _PUBLIC_
void cli_credentials_set_anonymous(struct cli_credentials
*cred
)
797 cli_credentials_set_username(cred
, "", CRED_SPECIFIED
);
798 cli_credentials_set_domain(cred
, "", CRED_SPECIFIED
);
799 cli_credentials_set_password(cred
, NULL
, CRED_SPECIFIED
);
800 cli_credentials_set_realm(cred
, NULL
, CRED_SPECIFIED
);
801 cli_credentials_set_workstation(cred
, "", CRED_UNINITIALISED
);
805 * Describe a credentials context as anonymous or authenticated
806 * @retval true if anonymous, false if a username is specified
809 _PUBLIC_
bool cli_credentials_is_anonymous(struct cli_credentials
*cred
)
811 const char *username
;
813 /* if bind dn is set it's not anonymous */
818 if (cred
->machine_account_pending
) {
819 cli_credentials_set_machine_account(cred
,
820 cred
->machine_account_pending_lp_ctx
);
823 username
= cli_credentials_get_username(cred
);
825 /* Yes, it is deliberate that we die if we have a NULL pointer
826 * here - anonymous is "", not NULL, which is 'never specified,
827 * never guessed', ie programmer bug */
836 * Mark the current password for a credentials struct as wrong. This will
837 * cause the password to be prompted again (if a callback is set).
839 * This will decrement the number of times the password can be tried.
841 * @retval whether the credentials struct is finished
843 _PUBLIC_
bool cli_credentials_wrong_password(struct cli_credentials
*cred
)
845 if (cred
->password_obtained
!= CRED_CALLBACK_RESULT
) {
849 cred
->password_obtained
= CRED_CALLBACK
;
853 return (cred
->tries
> 0);
856 _PUBLIC_
void cli_credentials_get_ntlm_username_domain(struct cli_credentials
*cred
, TALLOC_CTX
*mem_ctx
,
857 const char **username
,
860 if (cred
->principal_obtained
> cred
->username_obtained
) {
861 *domain
= talloc_strdup(mem_ctx
, "");
862 *username
= cli_credentials_get_principal(cred
, mem_ctx
);
864 *domain
= cli_credentials_get_domain(cred
);
865 *username
= cli_credentials_get_username(cred
);
870 * Read a named file, and parse it for username, domain, realm and password
872 * @param credentials Credentials structure on which to set the password
873 * @param file a named file to read the details from
874 * @param obtained This enum describes how 'specified' this password is
877 _PUBLIC_
bool cli_credentials_parse_file(struct cli_credentials
*cred
, const char *file
, enum credentials_obtained obtained
)
880 char *ptr
, *val
, *param
;
884 lines
= file_lines_load(file
, &numlines
, 0, NULL
);
888 /* fail if we can't open the credentials file */
889 d_printf("ERROR: Unable to open credentials file!\n");
893 for (i
= 0; i
< numlines
; i
++) {
894 len
= strlen(lines
[i
]);
899 /* break up the line into parameter & value.
900 * will need to eat a little whitespace possibly */
902 if (!(ptr
= strchr_m (lines
[i
], '=')))
908 /* eat leading white space */
909 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
912 if (strwicmp("password", param
) == 0) {
913 cli_credentials_set_password(cred
, val
, obtained
);
914 } else if (strwicmp("username", param
) == 0) {
915 cli_credentials_set_username(cred
, val
, obtained
);
916 } else if (strwicmp("domain", param
) == 0) {
917 cli_credentials_set_domain(cred
, val
, obtained
);
918 } else if (strwicmp("realm", param
) == 0) {
919 cli_credentials_set_realm(cred
, val
, obtained
);
921 memset(lines
[i
], 0, len
);
930 * Read a named file, and parse it for a password
932 * @param credentials Credentials structure on which to set the password
933 * @param file a named file to read the password from
934 * @param obtained This enum describes how 'specified' this password is
937 _PUBLIC_
bool cli_credentials_parse_password_file(struct cli_credentials
*credentials
, const char *file
, enum credentials_obtained obtained
)
939 int fd
= open(file
, O_RDONLY
, 0);
943 fprintf(stderr
, "Error opening password file %s: %s\n",
944 file
, strerror(errno
));
948 ret
= cli_credentials_parse_password_fd(credentials
, fd
, obtained
);
957 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
959 * @param credentials Credentials structure on which to set the password
960 * @param fd open file descriptor to read the password from
961 * @param obtained This enum describes how 'specified' this password is
964 _PUBLIC_
bool cli_credentials_parse_password_fd(struct cli_credentials
*credentials
,
965 int fd
, enum credentials_obtained obtained
)
970 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
971 p
&& p
- pass
< sizeof(pass
);) {
972 switch (read(fd
, p
, 1)) {
974 if (*p
!= '\n' && *p
!= '\0') {
975 *++p
= '\0'; /* advance p, and null-terminate pass */
981 *p
= '\0'; /* null-terminate it, just in case... */
982 p
= NULL
; /* then force the loop condition to become false */
985 fprintf(stderr
, "Error reading password from file descriptor %d: %s\n", fd
, "empty password\n");
990 fprintf(stderr
, "Error reading password from file descriptor %d: %s\n",
991 fd
, strerror(errno
));
996 cli_credentials_set_password(credentials
, pass
, obtained
);