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;
73 cred->old_nt_hash = NULL;
75 cred->lm_response.data = NULL;
76 cred->lm_response.length = 0;
77 cred->nt_response.data = NULL;
78 cred->nt_response.length = 0;
81 cred->client_gss_creds = NULL;
83 cred->server_gss_creds = NULL;
85 cred->workstation_cb = NULL;
86 cred->password_cb = NULL;
87 cred->username_cb = NULL;
88 cred->domain_cb = NULL;
89 cred->realm_cb = NULL;
90 cred->principal_cb = NULL;
92 cred->priv_data = NULL;
94 cred->netlogon_creds = NULL;
95 cred->secure_channel_type = SEC_CHAN_NULL;
99 cred->password_last_changed_time = 0;
101 cred->smb_krb5_context = NULL;
103 cred->machine_account_pending = false;
104 cred->machine_account_pending_lp_ctx = NULL;
106 cred->machine_account = false;
108 cred->password_tries = 0;
110 cred->callback_running = false;
112 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
113 cli_credentials_set_gensec_features(cred, 0);
114 cli_credentials_set_krb_forwardable(cred, CRED_AUTO_KRB_FORWARDABLE);
116 cred->forced_sasl_mech = NULL;
121 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
124 cred->priv_data = callback_data;
127 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
129 return cred->priv_data;
133 * Create a new anonymous credential
134 * @param mem_ctx TALLOC_CTX parent for credentials structure
136 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
138 struct cli_credentials *anon_credentials;
140 anon_credentials = cli_credentials_init(mem_ctx);
141 cli_credentials_set_anonymous(anon_credentials);
143 return anon_credentials;
146 _PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
147 enum credentials_use_kerberos use_kerberos)
149 creds->use_kerberos = use_kerberos;
152 _PUBLIC_ void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
153 const char *sasl_mech)
155 TALLOC_FREE(creds->forced_sasl_mech);
156 creds->forced_sasl_mech = talloc_strdup(creds, sasl_mech);
159 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
160 enum credentials_krb_forwardable krb_forwardable)
162 creds->krb_forwardable = krb_forwardable;
165 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
167 return creds->use_kerberos;
170 _PUBLIC_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
172 return creds->forced_sasl_mech;
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 if (cred->username_obtained == CRED_CALLBACK) {
210 cred->username_obtained = CRED_CALLBACK_RESULT;
211 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
215 return cred->username;
218 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
219 const char *val, enum credentials_obtained obtained)
221 if (obtained >= cred->username_obtained) {
222 cred->username = talloc_strdup(cred, val);
223 cred->username_obtained = obtained;
224 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
231 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
232 const char *(*username_cb) (struct cli_credentials *))
234 if (cred->username_obtained < CRED_CALLBACK) {
235 cred->username_cb = username_cb;
236 cred->username_obtained = CRED_CALLBACK;
243 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
246 cred->bind_dn = talloc_strdup(cred, bind_dn);
251 * Obtain the BIND DN for this credentials context.
252 * @param cred credentials context
253 * @retval The username set on this context.
254 * @note Return value will be NULL if not specified explictly
256 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
258 return cred->bind_dn;
263 * Obtain the client principal for this credentials context.
264 * @param cred credentials context
265 * @retval The username set on this context.
266 * @note Return value will never be NULL except by programmer error.
268 _PUBLIC_ const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
270 if (cred->machine_account_pending) {
271 cli_credentials_set_machine_account(cred,
272 cred->machine_account_pending_lp_ctx);
275 if (cred->principal_obtained == CRED_CALLBACK &&
276 !cred->callback_running) {
277 cred->callback_running = true;
278 cred->principal = cred->principal_cb(cred);
279 cred->callback_running = false;
280 if (cred->principal_obtained == CRED_CALLBACK) {
281 cred->principal_obtained = CRED_CALLBACK_RESULT;
282 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
286 if (cred->principal_obtained < cred->username_obtained
287 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
288 if (cred->domain_obtained > cred->realm_obtained) {
289 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
290 return talloc_asprintf(mem_ctx, "%s@%s",
291 cli_credentials_get_username(cred),
292 cli_credentials_get_domain(cred));
294 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
295 return talloc_asprintf(mem_ctx, "%s@%s",
296 cli_credentials_get_username(cred),
297 cli_credentials_get_realm(cred));
300 *obtained = cred->principal_obtained;
301 return talloc_strdup(mem_ctx, cred->principal);
305 * Obtain the client principal for this credentials context.
306 * @param cred credentials context
307 * @retval The username set on this context.
308 * @note Return value will never be NULL except by programmer error.
310 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
312 enum credentials_obtained obtained;
313 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
316 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
318 enum credentials_obtained obtained)
320 if (obtained >= cred->principal_obtained) {
321 cred->principal = talloc_strdup(cred, val);
322 cred->principal_obtained = obtained;
323 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
330 /* Set a callback to get the principal. This could be a popup dialog,
331 * a terminal prompt or similar. */
332 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
333 const char *(*principal_cb) (struct cli_credentials *))
335 if (cred->principal_obtained < CRED_CALLBACK) {
336 cred->principal_cb = principal_cb;
337 cred->principal_obtained = CRED_CALLBACK;
344 /* Some of our tools are 'anonymous by default'. This is a single
345 * function to determine if authentication has been explicitly
348 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
355 * If we forced the mech we clearly want authentication. E.g. to use
356 * SASL/EXTERNAL which has no credentials.
358 if (cred->forced_sasl_mech) {
362 if (cli_credentials_is_anonymous(cred)){
366 if (cred->principal_obtained >= CRED_SPECIFIED) {
369 if (cred->username_obtained >= CRED_SPECIFIED) {
373 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
381 * Obtain the password for this credentials context.
382 * @param cred credentials context
383 * @retval If set, the cleartext password, otherwise NULL
385 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
387 if (cred->machine_account_pending) {
388 cli_credentials_set_machine_account(cred,
389 cred->machine_account_pending_lp_ctx);
392 if (cred->password_obtained == CRED_CALLBACK &&
393 !cred->callback_running) {
394 cred->callback_running = true;
395 cred->password = cred->password_cb(cred);
396 cred->callback_running = false;
397 if (cred->password_obtained == CRED_CALLBACK) {
398 cred->password_obtained = CRED_CALLBACK_RESULT;
399 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
403 return cred->password;
406 /* Set a password on the credentials context, including an indication
407 * of 'how' the password was obtained */
409 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
411 enum credentials_obtained obtained)
413 if (obtained >= cred->password_obtained) {
414 cred->password_tries = 0;
415 cred->password = talloc_strdup(cred, val);
416 if (cred->password) {
417 /* Don't print the actual password in talloc memory dumps */
418 talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
420 cred->password_obtained = obtained;
421 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
423 cred->nt_hash = NULL;
424 cred->lm_response = data_blob(NULL, 0);
425 cred->nt_response = data_blob(NULL, 0);
432 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
433 const char *(*password_cb) (struct cli_credentials *))
435 if (cred->password_obtained < CRED_CALLBACK) {
436 cred->password_tries = 3;
437 cred->password_cb = password_cb;
438 cred->password_obtained = CRED_CALLBACK;
439 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
447 * Obtain the 'old' password for this credentials context (used for join accounts).
448 * @param cred credentials context
449 * @retval If set, the cleartext password, otherwise NULL
451 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
453 if (cred->machine_account_pending) {
454 cli_credentials_set_machine_account(cred,
455 cred->machine_account_pending_lp_ctx);
458 return cred->old_password;
461 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
463 enum credentials_obtained obtained)
465 cred->old_password = talloc_strdup(cred, val);
466 if (cred->old_password) {
467 /* Don't print the actual password in talloc memory dumps */
468 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
470 cred->old_nt_hash = NULL;
475 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
477 * Sometimes we only have this much of the password, while the rest of
478 * the time this call avoids calling E_md4hash themselves.
480 * @param cred credentials context
481 * @retval If set, the cleartext password, otherwise NULL
483 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
486 const char *password = NULL;
488 if (cred->nt_hash != NULL) {
489 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
494 *nt_hash = *cred->nt_hash;
499 password = cli_credentials_get_password(cred);
501 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
506 E_md4hash(password, nt_hash->hash);
515 * Obtain the old password, in the form MD4(unicode(password)) for this credentials context.
517 * Sometimes we only have this much of the password, while the rest of
518 * the time this call avoids calling E_md4hash themselves.
520 * @param cred credentials context
521 * @retval If set, the cleartext password, otherwise NULL
523 _PUBLIC_ struct samr_Password *cli_credentials_get_old_nt_hash(struct cli_credentials *cred,
526 const char *old_password = NULL;
528 if (cred->old_nt_hash != NULL) {
529 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
534 *nt_hash = *cred->old_nt_hash;
539 old_password = cli_credentials_get_old_password(cred);
541 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
546 E_md4hash(old_password, nt_hash->hash);
555 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
556 * @param cred credentials context
557 * @retval The domain set on this context.
558 * @note Return value will never be NULL except by programmer error.
560 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
562 if (cred->machine_account_pending) {
563 cli_credentials_set_machine_account(cred,
564 cred->machine_account_pending_lp_ctx);
567 if (cred->domain_obtained == CRED_CALLBACK &&
568 !cred->callback_running) {
569 cred->callback_running = true;
570 cred->domain = cred->domain_cb(cred);
571 cred->callback_running = false;
572 if (cred->domain_obtained == CRED_CALLBACK) {
573 cred->domain_obtained = CRED_CALLBACK_RESULT;
574 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
582 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
584 enum credentials_obtained obtained)
586 if (obtained >= cred->domain_obtained) {
587 /* it is important that the domain be in upper case,
588 * particularly for the sensitive NTLMv2
590 cred->domain = strupper_talloc(cred, val);
591 cred->domain_obtained = obtained;
592 /* setting domain does not mean we have to invalidate ccache
593 * because domain in not used for Kerberos operations.
594 * If ccache invalidation is required, one will anyway specify
595 * a password to kinit, and that will force invalidation of the ccache
603 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
604 const char *(*domain_cb) (struct cli_credentials *))
606 if (cred->domain_obtained < CRED_CALLBACK) {
607 cred->domain_cb = domain_cb;
608 cred->domain_obtained = CRED_CALLBACK;
616 * Obtain the Kerberos realm for this credentials context.
617 * @param cred credentials context
618 * @retval The realm set on this context.
619 * @note Return value will never be NULL except by programmer error.
621 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
623 if (cred->machine_account_pending) {
624 cli_credentials_set_machine_account(cred,
625 cred->machine_account_pending_lp_ctx);
628 if (cred->realm_obtained == CRED_CALLBACK &&
629 !cred->callback_running) {
630 cred->callback_running = true;
631 cred->realm = cred->realm_cb(cred);
632 cred->callback_running = false;
633 if (cred->realm_obtained == CRED_CALLBACK) {
634 cred->realm_obtained = CRED_CALLBACK_RESULT;
635 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
643 * Set the realm for this credentials context, and force it to
644 * uppercase for the sainity of our local kerberos libraries
646 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
648 enum credentials_obtained obtained)
650 if (obtained >= cred->realm_obtained) {
651 cred->realm = strupper_talloc(cred, val);
652 cred->realm_obtained = obtained;
653 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
660 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
661 const char *(*realm_cb) (struct cli_credentials *))
663 if (cred->realm_obtained < CRED_CALLBACK) {
664 cred->realm_cb = realm_cb;
665 cred->realm_obtained = CRED_CALLBACK;
673 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
675 * @param cred credentials context
676 * @retval The workstation name set on this context.
677 * @note Return value will never be NULL except by programmer error.
679 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
681 if (cred->workstation_obtained == CRED_CALLBACK &&
682 !cred->callback_running) {
683 cred->callback_running = true;
684 cred->workstation = cred->workstation_cb(cred);
685 cred->callback_running = false;
686 if (cred->workstation_obtained == CRED_CALLBACK) {
687 cred->workstation_obtained = CRED_CALLBACK_RESULT;
691 return cred->workstation;
694 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
696 enum credentials_obtained obtained)
698 if (obtained >= cred->workstation_obtained) {
699 cred->workstation = talloc_strdup(cred, val);
700 cred->workstation_obtained = obtained;
707 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
708 const char *(*workstation_cb) (struct cli_credentials *))
710 if (cred->workstation_obtained < CRED_CALLBACK) {
711 cred->workstation_cb = workstation_cb;
712 cred->workstation_obtained = CRED_CALLBACK;
720 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
722 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
724 * @param credentials Credentials structure on which to set the password
725 * @param data the string containing the username, password etc
726 * @param obtained This enum describes how 'specified' this password is
729 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
733 if (strcmp("%",data) == 0) {
734 cli_credentials_set_anonymous(credentials);
738 uname = talloc_strdup(credentials, data);
739 if ((p = strchr_m(uname,'%'))) {
741 cli_credentials_set_password(credentials, p+1, obtained);
744 if ((p = strchr_m(uname,'@'))) {
745 cli_credentials_set_principal(credentials, uname, obtained);
747 cli_credentials_set_realm(credentials, p+1, obtained);
749 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
751 cli_credentials_set_domain(credentials, uname, obtained);
754 cli_credentials_set_username(credentials, uname, obtained);
758 * Given a a credentials structure, print it as a string
760 * The format output is [domain\\]user[%password] or user[@realm][%password]
762 * @param credentials Credentials structure on which to set the password
763 * @param mem_ctx The memory context to place the result on
766 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
768 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
770 const char *username;
774 name = talloc_strdup(mem_ctx, bind_dn);
776 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
777 if (domain && domain[0]) {
778 name = talloc_asprintf(mem_ctx, "%s\\%s",
781 name = talloc_asprintf(mem_ctx, "%s",
789 * Specifies default values for domain, workstation and realm
790 * from the smb.conf configuration file
792 * @param cred Credentials structure to fill in
794 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
795 struct loadparm_context *lp_ctx)
797 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
798 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
799 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
801 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
803 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
804 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
806 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
808 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
809 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
811 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
816 * Guess defaults for credentials from environment variables,
817 * and from the configuration file
819 * @param cred Credentials structure to fill in
821 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
822 struct loadparm_context *lp_ctx)
825 const char *error_string;
827 if (lp_ctx != NULL) {
828 cli_credentials_set_conf(cred, lp_ctx);
831 if (getenv("LOGNAME")) {
832 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
835 if (getenv("USER")) {
836 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
837 if ((p = strchr_m(getenv("USER"),'%'))) {
838 memset(p,0,strlen(cred->password));
842 if (getenv("PASSWD")) {
843 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
846 if (getenv("PASSWD_FD")) {
847 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
851 p = getenv("PASSWD_FILE");
853 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
856 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
857 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
863 * Attach NETLOGON credentials for use with SCHANNEL
866 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
867 struct netlogon_creds_CredentialState *netlogon_creds)
869 TALLOC_FREE(cred->netlogon_creds);
870 if (netlogon_creds == NULL) {
873 cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
877 * Return attached NETLOGON credentials
880 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
882 return cred->netlogon_creds;
886 * Set NETLOGON secure channel type
889 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
890 enum netr_SchannelType secure_channel_type)
892 cred->secure_channel_type = secure_channel_type;
896 * Return NETLOGON secure chanel type
899 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
901 return cred->password_last_changed_time;
905 * Set NETLOGON secure channel type
908 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
909 time_t last_changed_time)
911 cred->password_last_changed_time = last_changed_time;
915 * Return NETLOGON secure chanel type
918 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
920 return cred->secure_channel_type;
924 * Fill in a credentials structure as the anonymous user
926 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
928 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
929 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
930 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
931 cli_credentials_set_principal(cred, NULL, CRED_SPECIFIED);
932 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
933 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
934 cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
938 * Describe a credentials context as anonymous or authenticated
939 * @retval true if anonymous, false if a username is specified
942 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
944 const char *username;
946 /* if bind dn is set it's not anonymous */
951 if (cred->machine_account_pending) {
952 cli_credentials_set_machine_account(cred,
953 cred->machine_account_pending_lp_ctx);
956 /* if principal is set, it's not anonymous */
957 if ((cred->principal != NULL) && cred->principal_obtained >= cred->username_obtained) {
961 username = cli_credentials_get_username(cred);
963 /* Yes, it is deliberate that we die if we have a NULL pointer
964 * here - anonymous is "", not NULL, which is 'never specified,
965 * never guessed', ie programmer bug */
974 * Mark the current password for a credentials struct as wrong. This will
975 * cause the password to be prompted again (if a callback is set).
977 * This will decrement the number of times the password can be tried.
979 * @retval whether the credentials struct is finished
981 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
983 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
987 if (cred->password_tries == 0) {
991 cred->password_tries--;
993 if (cred->password_tries == 0) {
997 cred->password_obtained = CRED_CALLBACK;
1001 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
1002 const char **username,
1003 const char **domain)
1005 if (cred->principal_obtained > cred->username_obtained) {
1006 *domain = talloc_strdup(mem_ctx, "");
1007 *username = cli_credentials_get_principal(cred, mem_ctx);
1009 *domain = cli_credentials_get_domain(cred);
1010 *username = cli_credentials_get_username(cred);
1015 * Read a named file, and parse it for username, domain, realm and password
1017 * @param credentials Credentials structure on which to set the password
1018 * @param file a named file to read the details from
1019 * @param obtained This enum describes how 'specified' this password is
1022 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
1025 char *ptr, *val, *param;
1029 lines = file_lines_load(file, &numlines, 0, NULL);
1033 /* fail if we can't open the credentials file */
1034 d_printf("ERROR: Unable to open credentials file!\n");
1038 for (i = 0; i < numlines; i++) {
1039 len = strlen(lines[i]);
1044 /* break up the line into parameter & value.
1045 * will need to eat a little whitespace possibly */
1047 if (!(ptr = strchr_m (lines[i], '=')))
1053 /* eat leading white space */
1054 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1057 if (strwicmp("password", param) == 0) {
1058 cli_credentials_set_password(cred, val, obtained);
1059 } else if (strwicmp("username", param) == 0) {
1060 cli_credentials_set_username(cred, val, obtained);
1061 } else if (strwicmp("domain", param) == 0) {
1062 cli_credentials_set_domain(cred, val, obtained);
1063 } else if (strwicmp("realm", param) == 0) {
1064 cli_credentials_set_realm(cred, val, obtained);
1066 memset(lines[i], 0, len);
1075 * Read a named file, and parse it for a password
1077 * @param credentials Credentials structure on which to set the password
1078 * @param file a named file to read the password from
1079 * @param obtained This enum describes how 'specified' this password is
1082 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1084 int fd = open(file, O_RDONLY, 0);
1088 fprintf(stderr, "Error opening password file %s: %s\n",
1089 file, strerror(errno));
1093 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1102 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1104 * @param credentials Credentials structure on which to set the password
1105 * @param fd open file descriptor to read the password from
1106 * @param obtained This enum describes how 'specified' this password is
1109 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1110 int fd, enum credentials_obtained obtained)
1115 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1116 p && p - pass < sizeof(pass);) {
1117 switch (read(fd, p, 1)) {
1119 if (*p != '\n' && *p != '\0') {
1120 *++p = '\0'; /* advance p, and null-terminate pass */
1126 *p = '\0'; /* null-terminate it, just in case... */
1127 p = NULL; /* then force the loop condition to become false */
1130 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1135 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1136 fd, strerror(errno));
1141 cli_credentials_set_password(credentials, pass, obtained);