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);
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_tries = 0;
401 cred->password = talloc_strdup(cred, val);
402 if (cred->password) {
403 /* Don't print the actual password in talloc memory dumps */
404 talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
406 cred->password_obtained = obtained;
407 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
409 cred->nt_hash = NULL;
410 cred->lm_response = data_blob(NULL, 0);
411 cred->nt_response = data_blob(NULL, 0);
418 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
419 const char *(*password_cb) (struct cli_credentials *))
421 if (cred->password_obtained < CRED_CALLBACK) {
422 cred->password_tries = 3;
423 cred->password_cb = password_cb;
424 cred->password_obtained = CRED_CALLBACK;
425 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
433 * Obtain the 'old' password for this credentials context (used for join accounts).
434 * @param cred credentials context
435 * @retval If set, the cleartext password, otherwise NULL
437 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
439 if (cred->machine_account_pending) {
440 cli_credentials_set_machine_account(cred,
441 cred->machine_account_pending_lp_ctx);
444 return cred->old_password;
447 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
449 enum credentials_obtained obtained)
451 cred->old_password = talloc_strdup(cred, val);
452 if (cred->old_password) {
453 /* Don't print the actual password in talloc memory dumps */
454 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
460 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
462 * Sometimes we only have this much of the password, while the rest of
463 * the time this call avoids calling E_md4hash themselves.
465 * @param cred credentials context
466 * @retval If set, the cleartext password, otherwise NULL
468 _PUBLIC_ const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
471 const char *password = cli_credentials_get_password(cred);
474 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
479 E_md4hash(password, nt_hash->hash);
483 return cred->nt_hash;
488 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
489 * @param cred credentials context
490 * @retval The domain set on this context.
491 * @note Return value will never be NULL except by programmer error.
493 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
495 if (cred->machine_account_pending) {
496 cli_credentials_set_machine_account(cred,
497 cred->machine_account_pending_lp_ctx);
500 if (cred->domain_obtained == CRED_CALLBACK &&
501 !cred->callback_running) {
502 cred->callback_running = true;
503 cred->domain = cred->domain_cb(cred);
504 cred->callback_running = false;
505 cred->domain_obtained = CRED_SPECIFIED;
506 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
513 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
515 enum credentials_obtained obtained)
517 if (obtained >= cred->domain_obtained) {
518 /* it is important that the domain be in upper case,
519 * particularly for the sensitive NTLMv2
521 cred->domain = strupper_talloc(cred, val);
522 cred->domain_obtained = obtained;
523 /* setting domain does not mean we have to invalidate ccache
524 * because domain in not used for Kerberos operations.
525 * If ccache invalidation is required, one will anyway specify
526 * a password to kinit, and that will force invalidation of the ccache
534 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
535 const char *(*domain_cb) (struct cli_credentials *))
537 if (cred->domain_obtained < CRED_CALLBACK) {
538 cred->domain_cb = domain_cb;
539 cred->domain_obtained = CRED_CALLBACK;
547 * Obtain the Kerberos realm for this credentials context.
548 * @param cred credentials context
549 * @retval The realm set on this context.
550 * @note Return value will never be NULL except by programmer error.
552 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
554 if (cred->machine_account_pending) {
555 cli_credentials_set_machine_account(cred,
556 cred->machine_account_pending_lp_ctx);
559 if (cred->realm_obtained == CRED_CALLBACK &&
560 !cred->callback_running) {
561 cred->callback_running = true;
562 cred->realm = cred->realm_cb(cred);
563 cred->callback_running = false;
564 cred->realm_obtained = CRED_SPECIFIED;
565 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
572 * Set the realm for this credentials context, and force it to
573 * uppercase for the sainity of our local kerberos libraries
575 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
577 enum credentials_obtained obtained)
579 if (obtained >= cred->realm_obtained) {
580 cred->realm = strupper_talloc(cred, val);
581 cred->realm_obtained = obtained;
582 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
589 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
590 const char *(*realm_cb) (struct cli_credentials *))
592 if (cred->realm_obtained < CRED_CALLBACK) {
593 cred->realm_cb = realm_cb;
594 cred->realm_obtained = CRED_CALLBACK;
602 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
604 * @param cred credentials context
605 * @retval The workstation name set on this context.
606 * @note Return value will never be NULL except by programmer error.
608 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
610 if (cred->workstation_obtained == CRED_CALLBACK &&
611 !cred->callback_running) {
612 cred->callback_running = true;
613 cred->workstation = cred->workstation_cb(cred);
614 cred->callback_running = false;
615 cred->workstation_obtained = CRED_SPECIFIED;
618 return cred->workstation;
621 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
623 enum credentials_obtained obtained)
625 if (obtained >= cred->workstation_obtained) {
626 cred->workstation = talloc_strdup(cred, val);
627 cred->workstation_obtained = obtained;
634 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
635 const char *(*workstation_cb) (struct cli_credentials *))
637 if (cred->workstation_obtained < CRED_CALLBACK) {
638 cred->workstation_cb = workstation_cb;
639 cred->workstation_obtained = CRED_CALLBACK;
647 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
649 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
651 * @param credentials Credentials structure on which to set the password
652 * @param data the string containing the username, password etc
653 * @param obtained This enum describes how 'specified' this password is
656 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
660 if (strcmp("%",data) == 0) {
661 cli_credentials_set_anonymous(credentials);
665 uname = talloc_strdup(credentials, data);
666 if ((p = strchr_m(uname,'%'))) {
668 cli_credentials_set_password(credentials, p+1, obtained);
671 if ((p = strchr_m(uname,'@'))) {
672 cli_credentials_set_principal(credentials, uname, obtained);
674 cli_credentials_set_realm(credentials, p+1, obtained);
676 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
678 cli_credentials_set_domain(credentials, uname, obtained);
681 cli_credentials_set_username(credentials, uname, obtained);
685 * Given a a credentials structure, print it as a string
687 * The format output is [domain\\]user[%password] or user[@realm][%password]
689 * @param credentials Credentials structure on which to set the password
690 * @param mem_ctx The memory context to place the result on
693 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
695 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
697 const char *username;
701 name = talloc_strdup(mem_ctx, bind_dn);
703 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
704 if (domain && domain[0]) {
705 name = talloc_asprintf(mem_ctx, "%s\\%s",
708 name = talloc_asprintf(mem_ctx, "%s",
716 * Specifies default values for domain, workstation and realm
717 * from the smb.conf configuration file
719 * @param cred Credentials structure to fill in
721 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
722 struct loadparm_context *lp_ctx)
724 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
725 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
726 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
728 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
730 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
731 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
733 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
735 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
736 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
738 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
743 * Guess defaults for credentials from environment variables,
744 * and from the configuration file
746 * @param cred Credentials structure to fill in
748 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
749 struct loadparm_context *lp_ctx)
752 const char *error_string;
754 if (lp_ctx != NULL) {
755 cli_credentials_set_conf(cred, lp_ctx);
758 if (getenv("LOGNAME")) {
759 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
762 if (getenv("USER")) {
763 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
764 if ((p = strchr_m(getenv("USER"),'%'))) {
765 memset(p,0,strlen(cred->password));
769 if (getenv("PASSWD")) {
770 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
773 if (getenv("PASSWD_FD")) {
774 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
778 p = getenv("PASSWD_FILE");
780 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
783 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
784 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
790 * Attach NETLOGON credentials for use with SCHANNEL
793 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
794 struct netlogon_creds_CredentialState *netlogon_creds)
796 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
800 * Return attached NETLOGON credentials
803 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
805 return cred->netlogon_creds;
809 * Set NETLOGON secure channel type
812 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
813 enum netr_SchannelType secure_channel_type)
815 cred->secure_channel_type = secure_channel_type;
819 * Return NETLOGON secure chanel type
822 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
824 return cred->password_last_changed_time;
828 * Set NETLOGON secure channel type
831 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
832 time_t last_changed_time)
834 cred->password_last_changed_time = last_changed_time;
838 * Return NETLOGON secure chanel type
841 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
843 return cred->secure_channel_type;
847 * Fill in a credentials structure as the anonymous user
849 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
851 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
852 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
853 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
854 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
855 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
859 * Describe a credentials context as anonymous or authenticated
860 * @retval true if anonymous, false if a username is specified
863 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
865 const char *username;
867 /* if bind dn is set it's not anonymous */
872 if (cred->machine_account_pending) {
873 cli_credentials_set_machine_account(cred,
874 cred->machine_account_pending_lp_ctx);
877 username = cli_credentials_get_username(cred);
879 /* Yes, it is deliberate that we die if we have a NULL pointer
880 * here - anonymous is "", not NULL, which is 'never specified,
881 * never guessed', ie programmer bug */
890 * Mark the current password for a credentials struct as wrong. This will
891 * cause the password to be prompted again (if a callback is set).
893 * This will decrement the number of times the password can be tried.
895 * @retval whether the credentials struct is finished
897 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
899 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
903 if (cred->password_tries == 0) {
907 cred->password_tries--;
909 if (cred->password_tries == 0) {
913 cred->password_obtained = CRED_CALLBACK;
917 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
918 const char **username,
921 if (cred->principal_obtained > cred->username_obtained) {
922 *domain = talloc_strdup(mem_ctx, "");
923 *username = cli_credentials_get_principal(cred, mem_ctx);
925 *domain = cli_credentials_get_domain(cred);
926 *username = cli_credentials_get_username(cred);
931 * Read a named file, and parse it for username, domain, realm and password
933 * @param credentials Credentials structure on which to set the password
934 * @param file a named file to read the details from
935 * @param obtained This enum describes how 'specified' this password is
938 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
941 char *ptr, *val, *param;
945 lines = file_lines_load(file, &numlines, 0, NULL);
949 /* fail if we can't open the credentials file */
950 d_printf("ERROR: Unable to open credentials file!\n");
954 for (i = 0; i < numlines; i++) {
955 len = strlen(lines[i]);
960 /* break up the line into parameter & value.
961 * will need to eat a little whitespace possibly */
963 if (!(ptr = strchr_m (lines[i], '=')))
969 /* eat leading white space */
970 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
973 if (strwicmp("password", param) == 0) {
974 cli_credentials_set_password(cred, val, obtained);
975 } else if (strwicmp("username", param) == 0) {
976 cli_credentials_set_username(cred, val, obtained);
977 } else if (strwicmp("domain", param) == 0) {
978 cli_credentials_set_domain(cred, val, obtained);
979 } else if (strwicmp("realm", param) == 0) {
980 cli_credentials_set_realm(cred, val, obtained);
982 memset(lines[i], 0, len);
991 * Read a named file, and parse it for a password
993 * @param credentials Credentials structure on which to set the password
994 * @param file a named file to read the password from
995 * @param obtained This enum describes how 'specified' this password is
998 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1000 int fd = open(file, O_RDONLY, 0);
1004 fprintf(stderr, "Error opening password file %s: %s\n",
1005 file, strerror(errno));
1009 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1018 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1020 * @param credentials Credentials structure on which to set the password
1021 * @param fd open file descriptor to read the password from
1022 * @param obtained This enum describes how 'specified' this password is
1025 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1026 int fd, enum credentials_obtained obtained)
1031 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1032 p && p - pass < sizeof(pass);) {
1033 switch (read(fd, p, 1)) {
1035 if (*p != '\n' && *p != '\0') {
1036 *++p = '\0'; /* advance p, and null-terminate pass */
1042 *p = '\0'; /* null-terminate it, just in case... */
1043 p = NULL; /* then force the loop condition to become false */
1046 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1051 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1052 fd, strerror(errno));
1057 cli_credentials_set_password(credentials, pass, obtained);