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;
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);
118 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
121 cred->priv_data = callback_data;
124 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
126 return cred->priv_data;
129 _PUBLIC_ struct cli_credentials *cli_credentials_shallow_copy(TALLOC_CTX *mem_ctx,
130 struct cli_credentials *src)
132 struct cli_credentials *dst;
134 dst = talloc(mem_ctx, struct cli_credentials);
145 * Create a new anonymous credential
146 * @param mem_ctx TALLOC_CTX parent for credentials structure
148 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
150 struct cli_credentials *anon_credentials;
152 anon_credentials = cli_credentials_init(mem_ctx);
153 cli_credentials_set_anonymous(anon_credentials);
155 return anon_credentials;
158 _PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
159 enum credentials_use_kerberos use_kerberos)
161 creds->use_kerberos = use_kerberos;
164 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
165 enum credentials_krb_forwardable krb_forwardable)
167 creds->krb_forwardable = krb_forwardable;
170 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
172 return creds->use_kerberos;
175 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
177 return creds->krb_forwardable;
180 _PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
182 creds->gensec_features = gensec_features;
185 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
187 return creds->gensec_features;
192 * Obtain the username for this credentials context.
193 * @param cred credentials context
194 * @retval The username set on this context.
195 * @note Return value will never be NULL except by programmer error.
197 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
199 if (cred->machine_account_pending) {
200 cli_credentials_set_machine_account(cred,
201 cred->machine_account_pending_lp_ctx);
204 if (cred->username_obtained == CRED_CALLBACK &&
205 !cred->callback_running) {
206 cred->callback_running = true;
207 cred->username = cred->username_cb(cred);
208 cred->callback_running = false;
209 cred->username_obtained = CRED_SPECIFIED;
210 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
213 return cred->username;
216 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
217 const char *val, enum credentials_obtained obtained)
219 if (obtained >= cred->username_obtained) {
220 cred->username = talloc_strdup(cred, val);
221 cred->username_obtained = obtained;
222 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
229 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
230 const char *(*username_cb) (struct cli_credentials *))
232 if (cred->username_obtained < CRED_CALLBACK) {
233 cred->username_cb = username_cb;
234 cred->username_obtained = CRED_CALLBACK;
241 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
244 cred->bind_dn = talloc_strdup(cred, bind_dn);
249 * Obtain the BIND DN for this credentials context.
250 * @param cred credentials context
251 * @retval The username set on this context.
252 * @note Return value will be NULL if not specified explictly
254 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
256 return cred->bind_dn;
261 * Obtain the client principal for this credentials context.
262 * @param cred credentials context
263 * @retval The username set on this context.
264 * @note Return value will never be NULL except by programmer error.
266 _PUBLIC_ const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
268 if (cred->machine_account_pending) {
269 cli_credentials_set_machine_account(cred,
270 cred->machine_account_pending_lp_ctx);
273 if (cred->principal_obtained == CRED_CALLBACK &&
274 !cred->callback_running) {
275 cred->callback_running = true;
276 cred->principal = cred->principal_cb(cred);
277 cred->callback_running = false;
278 cred->principal_obtained = CRED_SPECIFIED;
279 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
282 if (cred->principal_obtained < cred->username_obtained
283 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
284 if (cred->domain_obtained > cred->realm_obtained) {
285 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
286 return talloc_asprintf(mem_ctx, "%s@%s",
287 cli_credentials_get_username(cred),
288 cli_credentials_get_domain(cred));
290 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
291 return talloc_asprintf(mem_ctx, "%s@%s",
292 cli_credentials_get_username(cred),
293 cli_credentials_get_realm(cred));
296 *obtained = cred->principal_obtained;
297 return talloc_strdup(mem_ctx, cred->principal);
301 * Obtain the client principal for this credentials context.
302 * @param cred credentials context
303 * @retval The username set on this context.
304 * @note Return value will never be NULL except by programmer error.
306 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
308 enum credentials_obtained obtained;
309 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
312 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
314 enum credentials_obtained obtained)
316 if (obtained >= cred->principal_obtained) {
317 cred->principal = talloc_strdup(cred, val);
318 cred->principal_obtained = obtained;
319 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
326 /* Set a callback to get the principal. This could be a popup dialog,
327 * a terminal prompt or similar. */
328 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
329 const char *(*principal_cb) (struct cli_credentials *))
331 if (cred->principal_obtained < CRED_CALLBACK) {
332 cred->principal_cb = principal_cb;
333 cred->principal_obtained = CRED_CALLBACK;
340 /* Some of our tools are 'anonymous by default'. This is a single
341 * function to determine if authentication has been explicitly
344 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
350 if (cli_credentials_is_anonymous(cred)){
354 if (cred->principal_obtained >= CRED_SPECIFIED) {
357 if (cred->username_obtained >= CRED_SPECIFIED) {
361 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
369 * Obtain the password for this credentials context.
370 * @param cred credentials context
371 * @retval If set, the cleartext password, otherwise NULL
373 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
375 if (cred->machine_account_pending) {
376 cli_credentials_set_machine_account(cred,
377 cred->machine_account_pending_lp_ctx);
380 if (cred->password_obtained == CRED_CALLBACK &&
381 !cred->callback_running) {
382 cred->callback_running = true;
383 cred->password = cred->password_cb(cred);
384 cred->callback_running = false;
385 cred->password_obtained = CRED_CALLBACK_RESULT;
386 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
389 return cred->password;
392 /* Set a password on the credentials context, including an indication
393 * of 'how' the password was obtained */
395 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
397 enum credentials_obtained obtained)
399 if (obtained >= cred->password_obtained) {
400 cred->password = talloc_strdup(cred, val);
401 if (cred->password) {
402 /* Don't print the actual password in talloc memory dumps */
403 talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
405 cred->password_obtained = obtained;
406 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
408 cred->nt_hash = NULL;
409 cred->lm_response = data_blob(NULL, 0);
410 cred->nt_response = data_blob(NULL, 0);
417 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
418 const char *(*password_cb) (struct cli_credentials *))
420 if (cred->password_obtained < CRED_CALLBACK) {
421 cred->password_cb = password_cb;
422 cred->password_obtained = CRED_CALLBACK;
423 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
431 * Obtain the 'old' password for this credentials context (used for join accounts).
432 * @param cred credentials context
433 * @retval If set, the cleartext password, otherwise NULL
435 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
437 if (cred->machine_account_pending) {
438 cli_credentials_set_machine_account(cred,
439 cred->machine_account_pending_lp_ctx);
442 return cred->old_password;
445 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
447 enum credentials_obtained obtained)
449 cred->old_password = talloc_strdup(cred, val);
450 if (cred->old_password) {
451 /* Don't print the actual password in talloc memory dumps */
452 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
458 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
460 * Sometimes we only have this much of the password, while the rest of
461 * the time this call avoids calling E_md4hash themselves.
463 * @param cred credentials context
464 * @retval If set, the cleartext password, otherwise NULL
466 _PUBLIC_ const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
469 const char *password = cli_credentials_get_password(cred);
472 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
477 E_md4hash(password, nt_hash->hash);
481 return cred->nt_hash;
486 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
487 * @param cred credentials context
488 * @retval The domain set on this context.
489 * @note Return value will never be NULL except by programmer error.
491 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
493 if (cred->machine_account_pending) {
494 cli_credentials_set_machine_account(cred,
495 cred->machine_account_pending_lp_ctx);
498 if (cred->domain_obtained == CRED_CALLBACK &&
499 !cred->callback_running) {
500 cred->callback_running = true;
501 cred->domain = cred->domain_cb(cred);
502 cred->callback_running = false;
503 cred->domain_obtained = CRED_SPECIFIED;
504 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
511 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
513 enum credentials_obtained obtained)
515 if (obtained >= cred->domain_obtained) {
516 /* it is important that the domain be in upper case,
517 * particularly for the sensitive NTLMv2
519 cred->domain = strupper_talloc(cred, val);
520 cred->domain_obtained = obtained;
521 /* setting domain does not mean we have to invalidate ccache
522 * because domain in not used for Kerberos operations.
523 * If ccache invalidation is required, one will anyway specify
524 * a password to kinit, and that will force invalidation of the ccache
532 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
533 const char *(*domain_cb) (struct cli_credentials *))
535 if (cred->domain_obtained < CRED_CALLBACK) {
536 cred->domain_cb = domain_cb;
537 cred->domain_obtained = CRED_CALLBACK;
545 * Obtain the Kerberos realm for this credentials context.
546 * @param cred credentials context
547 * @retval The realm set on this context.
548 * @note Return value will never be NULL except by programmer error.
550 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
552 if (cred->machine_account_pending) {
553 cli_credentials_set_machine_account(cred,
554 cred->machine_account_pending_lp_ctx);
557 if (cred->realm_obtained == CRED_CALLBACK &&
558 !cred->callback_running) {
559 cred->callback_running = true;
560 cred->realm = cred->realm_cb(cred);
561 cred->callback_running = false;
562 cred->realm_obtained = CRED_SPECIFIED;
563 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
570 * Set the realm for this credentials context, and force it to
571 * uppercase for the sainity of our local kerberos libraries
573 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
575 enum credentials_obtained obtained)
577 if (obtained >= cred->realm_obtained) {
578 cred->realm = strupper_talloc(cred, val);
579 cred->realm_obtained = obtained;
580 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
587 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
588 const char *(*realm_cb) (struct cli_credentials *))
590 if (cred->realm_obtained < CRED_CALLBACK) {
591 cred->realm_cb = realm_cb;
592 cred->realm_obtained = CRED_CALLBACK;
600 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
602 * @param cred credentials context
603 * @retval The workstation name set on this context.
604 * @note Return value will never be NULL except by programmer error.
606 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
608 if (cred->workstation_obtained == CRED_CALLBACK &&
609 !cred->callback_running) {
610 cred->callback_running = true;
611 cred->workstation = cred->workstation_cb(cred);
612 cred->callback_running = false;
613 cred->workstation_obtained = CRED_SPECIFIED;
616 return cred->workstation;
619 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
621 enum credentials_obtained obtained)
623 if (obtained >= cred->workstation_obtained) {
624 cred->workstation = talloc_strdup(cred, val);
625 cred->workstation_obtained = obtained;
632 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
633 const char *(*workstation_cb) (struct cli_credentials *))
635 if (cred->workstation_obtained < CRED_CALLBACK) {
636 cred->workstation_cb = workstation_cb;
637 cred->workstation_obtained = CRED_CALLBACK;
645 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
647 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
649 * @param credentials Credentials structure on which to set the password
650 * @param data the string containing the username, password etc
651 * @param obtained This enum describes how 'specified' this password is
654 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
658 if (strcmp("%",data) == 0) {
659 cli_credentials_set_anonymous(credentials);
663 uname = talloc_strdup(credentials, data);
664 if ((p = strchr_m(uname,'%'))) {
666 cli_credentials_set_password(credentials, p+1, obtained);
669 if ((p = strchr_m(uname,'@'))) {
670 cli_credentials_set_principal(credentials, uname, obtained);
672 cli_credentials_set_realm(credentials, p+1, obtained);
674 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
676 cli_credentials_set_domain(credentials, uname, obtained);
679 cli_credentials_set_username(credentials, uname, obtained);
683 * Given a a credentials structure, print it as a string
685 * The format output is [domain\\]user[%password] or user[@realm][%password]
687 * @param credentials Credentials structure on which to set the password
688 * @param mem_ctx The memory context to place the result on
691 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
693 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
695 const char *username;
699 name = talloc_strdup(mem_ctx, bind_dn);
701 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
702 if (domain && domain[0]) {
703 name = talloc_asprintf(mem_ctx, "%s\\%s",
706 name = talloc_asprintf(mem_ctx, "%s",
714 * Specifies default values for domain, workstation and realm
715 * from the smb.conf configuration file
717 * @param cred Credentials structure to fill in
719 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
720 struct loadparm_context *lp_ctx)
722 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
723 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
724 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
726 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
728 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
729 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
731 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
733 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
734 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
736 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
741 * Guess defaults for credentials from environment variables,
742 * and from the configuration file
744 * @param cred Credentials structure to fill in
746 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
747 struct loadparm_context *lp_ctx)
750 const char *error_string;
752 if (lp_ctx != NULL) {
753 cli_credentials_set_conf(cred, lp_ctx);
756 if (getenv("LOGNAME")) {
757 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
760 if (getenv("USER")) {
761 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
762 if ((p = strchr_m(getenv("USER"),'%'))) {
763 memset(p,0,strlen(cred->password));
767 if (getenv("PASSWD")) {
768 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
771 if (getenv("PASSWD_FD")) {
772 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
776 p = getenv("PASSWD_FILE");
778 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
781 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
782 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
788 * Attach NETLOGON credentials for use with SCHANNEL
791 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
792 struct netlogon_creds_CredentialState *netlogon_creds)
794 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
798 * Return attached NETLOGON credentials
801 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
803 return cred->netlogon_creds;
807 * Set NETLOGON secure channel type
810 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
811 enum netr_SchannelType secure_channel_type)
813 cred->secure_channel_type = secure_channel_type;
817 * Return NETLOGON secure chanel type
820 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
822 return cred->password_last_changed_time;
826 * Set NETLOGON secure channel type
829 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
830 time_t last_changed_time)
832 cred->password_last_changed_time = last_changed_time;
836 * Return NETLOGON secure chanel type
839 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
841 return cred->secure_channel_type;
845 * Fill in a credentials structure as the anonymous user
847 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
849 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
850 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
851 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
852 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
853 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
857 * Describe a credentials context as anonymous or authenticated
858 * @retval true if anonymous, false if a username is specified
861 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
863 const char *username;
865 /* if bind dn is set it's not anonymous */
870 if (cred->machine_account_pending) {
871 cli_credentials_set_machine_account(cred,
872 cred->machine_account_pending_lp_ctx);
875 username = cli_credentials_get_username(cred);
877 /* Yes, it is deliberate that we die if we have a NULL pointer
878 * here - anonymous is "", not NULL, which is 'never specified,
879 * never guessed', ie programmer bug */
888 * Mark the current password for a credentials struct as wrong. This will
889 * cause the password to be prompted again (if a callback is set).
891 * This will decrement the number of times the password can be tried.
893 * @retval whether the credentials struct is finished
895 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
897 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
901 cred->password_obtained = CRED_CALLBACK;
905 return (cred->tries > 0);
908 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
909 const char **username,
912 if (cred->principal_obtained > cred->username_obtained) {
913 *domain = talloc_strdup(mem_ctx, "");
914 *username = cli_credentials_get_principal(cred, mem_ctx);
916 *domain = cli_credentials_get_domain(cred);
917 *username = cli_credentials_get_username(cred);
922 * Read a named file, and parse it for username, domain, realm and password
924 * @param credentials Credentials structure on which to set the password
925 * @param file a named file to read the details from
926 * @param obtained This enum describes how 'specified' this password is
929 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
932 char *ptr, *val, *param;
936 lines = file_lines_load(file, &numlines, 0, NULL);
940 /* fail if we can't open the credentials file */
941 d_printf("ERROR: Unable to open credentials file!\n");
945 for (i = 0; i < numlines; i++) {
946 len = strlen(lines[i]);
951 /* break up the line into parameter & value.
952 * will need to eat a little whitespace possibly */
954 if (!(ptr = strchr_m (lines[i], '=')))
960 /* eat leading white space */
961 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
964 if (strwicmp("password", param) == 0) {
965 cli_credentials_set_password(cred, val, obtained);
966 } else if (strwicmp("username", param) == 0) {
967 cli_credentials_set_username(cred, val, obtained);
968 } else if (strwicmp("domain", param) == 0) {
969 cli_credentials_set_domain(cred, val, obtained);
970 } else if (strwicmp("realm", param) == 0) {
971 cli_credentials_set_realm(cred, val, obtained);
973 memset(lines[i], 0, len);
982 * Read a named file, and parse it for a password
984 * @param credentials Credentials structure on which to set the password
985 * @param file a named file to read the password from
986 * @param obtained This enum describes how 'specified' this password is
989 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
991 int fd = open(file, O_RDONLY, 0);
995 fprintf(stderr, "Error opening password file %s: %s\n",
996 file, strerror(errno));
1000 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1009 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1011 * @param credentials Credentials structure on which to set the password
1012 * @param fd open file descriptor to read the password from
1013 * @param obtained This enum describes how 'specified' this password is
1016 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1017 int fd, enum credentials_obtained obtained)
1022 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1023 p && p - pass < sizeof(pass);) {
1024 switch (read(fd, p, 1)) {
1026 if (*p != '\n' && *p != '\0') {
1027 *++p = '\0'; /* advance p, and null-terminate pass */
1033 *p = '\0'; /* null-terminate it, just in case... */
1034 p = NULL; /* then force the loop condition to become false */
1037 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1042 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1043 fd, strerror(errno));
1048 cli_credentials_set_password(credentials, pass, obtained);