s4-dsdb-tests: Remove unused method get_ldap_connection()
[Samba.git] / auth / credentials / credentials.c
bloba9e4fc864d46426de0d769fa0e5926fdd216bad6
1 /*
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/>.
24 #include "includes.h"
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"
29 #include "tevent.h"
30 #include "param/param.h"
31 #include "system/filesys.h"
33 /**
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);
40 if (cred == NULL) {
41 return cred;
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;
62 cred->domain = NULL;
63 cred->realm = 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;
70 cred->bind_dn = NULL;
72 cred->nt_hash = 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;
79 cred->ccache = NULL;
80 cred->client_gss_creds = NULL;
81 cred->keytab = 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;
96 cred->kvno = 0;
98 cred->password_last_changed_time = 0;
100 cred->smb_krb5_context = NULL;
102 cred->machine_account_pending = false;
103 cred->machine_account_pending_lp_ctx = NULL;
105 cred->machine_account = false;
107 cred->password_tries = 0;
109 cred->callback_running = false;
111 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
112 cli_credentials_set_gensec_features(cred, 0);
113 cli_credentials_set_krb_forwardable(cred, CRED_AUTO_KRB_FORWARDABLE);
115 cred->forced_sasl_mech = NULL;
117 return cred;
120 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
121 void *callback_data)
123 cred->priv_data = callback_data;
126 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
128 return cred->priv_data;
131 _PUBLIC_ struct cli_credentials *cli_credentials_shallow_copy(TALLOC_CTX *mem_ctx,
132 struct cli_credentials *src)
134 struct cli_credentials *dst;
136 dst = talloc(mem_ctx, struct cli_credentials);
137 if (dst == NULL) {
138 return NULL;
141 *dst = *src;
143 return dst;
147 * Create a new anonymous credential
148 * @param mem_ctx TALLOC_CTX parent for credentials structure
150 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
152 struct cli_credentials *anon_credentials;
154 anon_credentials = cli_credentials_init(mem_ctx);
155 cli_credentials_set_anonymous(anon_credentials);
157 return anon_credentials;
160 _PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
161 enum credentials_use_kerberos use_kerberos)
163 creds->use_kerberos = use_kerberos;
166 _PUBLIC_ void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
167 const char *sasl_mech)
169 TALLOC_FREE(creds->forced_sasl_mech);
170 creds->forced_sasl_mech = talloc_strdup(creds, sasl_mech);
173 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
174 enum credentials_krb_forwardable krb_forwardable)
176 creds->krb_forwardable = krb_forwardable;
179 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
181 return creds->use_kerberos;
184 _PUBLIC_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
186 return creds->forced_sasl_mech;
189 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
191 return creds->krb_forwardable;
194 _PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
196 creds->gensec_features = gensec_features;
199 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
201 return creds->gensec_features;
206 * Obtain the username for this credentials context.
207 * @param cred credentials context
208 * @retval The username set on this context.
209 * @note Return value will never be NULL except by programmer error.
211 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
213 if (cred->machine_account_pending) {
214 cli_credentials_set_machine_account(cred,
215 cred->machine_account_pending_lp_ctx);
218 if (cred->username_obtained == CRED_CALLBACK &&
219 !cred->callback_running) {
220 cred->callback_running = true;
221 cred->username = cred->username_cb(cred);
222 cred->callback_running = false;
223 if (cred->username_obtained == CRED_CALLBACK) {
224 cred->username_obtained = CRED_CALLBACK_RESULT;
225 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
229 return cred->username;
232 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
233 const char *val, enum credentials_obtained obtained)
235 if (obtained >= cred->username_obtained) {
236 cred->username = talloc_strdup(cred, val);
237 cred->username_obtained = obtained;
238 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
239 return true;
242 return false;
245 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
246 const char *(*username_cb) (struct cli_credentials *))
248 if (cred->username_obtained < CRED_CALLBACK) {
249 cred->username_cb = username_cb;
250 cred->username_obtained = CRED_CALLBACK;
251 return true;
254 return false;
257 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
258 const char *bind_dn)
260 cred->bind_dn = talloc_strdup(cred, bind_dn);
261 return true;
265 * Obtain the BIND DN for this credentials context.
266 * @param cred credentials context
267 * @retval The username set on this context.
268 * @note Return value will be NULL if not specified explictly
270 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
272 return cred->bind_dn;
277 * Obtain the client principal for this credentials context.
278 * @param cred credentials context
279 * @retval The username set on this context.
280 * @note Return value will never be NULL except by programmer error.
282 _PUBLIC_ const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
284 if (cred->machine_account_pending) {
285 cli_credentials_set_machine_account(cred,
286 cred->machine_account_pending_lp_ctx);
289 if (cred->principal_obtained == CRED_CALLBACK &&
290 !cred->callback_running) {
291 cred->callback_running = true;
292 cred->principal = cred->principal_cb(cred);
293 cred->callback_running = false;
294 if (cred->principal_obtained == CRED_CALLBACK) {
295 cred->principal_obtained = CRED_CALLBACK_RESULT;
296 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
300 if (cred->principal_obtained < cred->username_obtained
301 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
302 if (cred->domain_obtained > cred->realm_obtained) {
303 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
304 return talloc_asprintf(mem_ctx, "%s@%s",
305 cli_credentials_get_username(cred),
306 cli_credentials_get_domain(cred));
307 } else {
308 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
309 return talloc_asprintf(mem_ctx, "%s@%s",
310 cli_credentials_get_username(cred),
311 cli_credentials_get_realm(cred));
314 *obtained = cred->principal_obtained;
315 return talloc_strdup(mem_ctx, cred->principal);
319 * Obtain the client principal for this credentials context.
320 * @param cred credentials context
321 * @retval The username set on this context.
322 * @note Return value will never be NULL except by programmer error.
324 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
326 enum credentials_obtained obtained;
327 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
330 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
331 const char *val,
332 enum credentials_obtained obtained)
334 if (obtained >= cred->principal_obtained) {
335 cred->principal = talloc_strdup(cred, val);
336 cred->principal_obtained = obtained;
337 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
338 return true;
341 return false;
344 /* Set a callback to get the principal. This could be a popup dialog,
345 * a terminal prompt or similar. */
346 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
347 const char *(*principal_cb) (struct cli_credentials *))
349 if (cred->principal_obtained < CRED_CALLBACK) {
350 cred->principal_cb = principal_cb;
351 cred->principal_obtained = CRED_CALLBACK;
352 return true;
355 return false;
358 /* Some of our tools are 'anonymous by default'. This is a single
359 * function to determine if authentication has been explicitly
360 * requested */
362 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
364 if (cred->bind_dn) {
365 return true;
369 * If we forced the mech we clearly want authentication. E.g. to use
370 * SASL/EXTERNAL which has no credentials.
372 if (cred->forced_sasl_mech) {
373 return true;
376 if (cli_credentials_is_anonymous(cred)){
377 return false;
380 if (cred->principal_obtained >= CRED_SPECIFIED) {
381 return true;
383 if (cred->username_obtained >= CRED_SPECIFIED) {
384 return true;
387 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
388 return true;
391 return false;
395 * Obtain the password for this credentials context.
396 * @param cred credentials context
397 * @retval If set, the cleartext password, otherwise NULL
399 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
401 if (cred->machine_account_pending) {
402 cli_credentials_set_machine_account(cred,
403 cred->machine_account_pending_lp_ctx);
406 if (cred->password_obtained == CRED_CALLBACK &&
407 !cred->callback_running) {
408 cred->callback_running = true;
409 cred->password = cred->password_cb(cred);
410 cred->callback_running = false;
411 if (cred->password_obtained == CRED_CALLBACK) {
412 cred->password_obtained = CRED_CALLBACK_RESULT;
413 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
417 return cred->password;
420 /* Set a password on the credentials context, including an indication
421 * of 'how' the password was obtained */
423 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
424 const char *val,
425 enum credentials_obtained obtained)
427 if (obtained >= cred->password_obtained) {
428 cred->password_tries = 0;
429 cred->password = talloc_strdup(cred, val);
430 if (cred->password) {
431 /* Don't print the actual password in talloc memory dumps */
432 talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
434 cred->password_obtained = obtained;
435 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
437 cred->nt_hash = NULL;
438 cred->lm_response = data_blob(NULL, 0);
439 cred->nt_response = data_blob(NULL, 0);
440 return true;
443 return false;
446 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
447 const char *(*password_cb) (struct cli_credentials *))
449 if (cred->password_obtained < CRED_CALLBACK) {
450 cred->password_tries = 3;
451 cred->password_cb = password_cb;
452 cred->password_obtained = CRED_CALLBACK;
453 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
454 return true;
457 return false;
461 * Obtain the 'old' password for this credentials context (used for join accounts).
462 * @param cred credentials context
463 * @retval If set, the cleartext password, otherwise NULL
465 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
467 if (cred->machine_account_pending) {
468 cli_credentials_set_machine_account(cred,
469 cred->machine_account_pending_lp_ctx);
472 return cred->old_password;
475 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
476 const char *val,
477 enum credentials_obtained obtained)
479 cred->old_password = talloc_strdup(cred, val);
480 if (cred->old_password) {
481 /* Don't print the actual password in talloc memory dumps */
482 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
484 return true;
488 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
490 * Sometimes we only have this much of the password, while the rest of
491 * the time this call avoids calling E_md4hash themselves.
493 * @param cred credentials context
494 * @retval If set, the cleartext password, otherwise NULL
496 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
497 TALLOC_CTX *mem_ctx)
499 const char *password = NULL;
501 if (cred->nt_hash != NULL) {
502 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
503 if (!nt_hash) {
504 return NULL;
507 *nt_hash = *cred->nt_hash;
509 return nt_hash;
512 password = cli_credentials_get_password(cred);
513 if (password) {
514 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
515 if (!nt_hash) {
516 return NULL;
519 E_md4hash(password, nt_hash->hash);
521 return nt_hash;
524 return NULL;
528 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
529 * @param cred credentials context
530 * @retval The domain set on this context.
531 * @note Return value will never be NULL except by programmer error.
533 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
535 if (cred->machine_account_pending) {
536 cli_credentials_set_machine_account(cred,
537 cred->machine_account_pending_lp_ctx);
540 if (cred->domain_obtained == CRED_CALLBACK &&
541 !cred->callback_running) {
542 cred->callback_running = true;
543 cred->domain = cred->domain_cb(cred);
544 cred->callback_running = false;
545 if (cred->domain_obtained == CRED_CALLBACK) {
546 cred->domain_obtained = CRED_CALLBACK_RESULT;
547 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
551 return cred->domain;
555 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
556 const char *val,
557 enum credentials_obtained obtained)
559 if (obtained >= cred->domain_obtained) {
560 /* it is important that the domain be in upper case,
561 * particularly for the sensitive NTLMv2
562 * calculations */
563 cred->domain = strupper_talloc(cred, val);
564 cred->domain_obtained = obtained;
565 /* setting domain does not mean we have to invalidate ccache
566 * because domain in not used for Kerberos operations.
567 * If ccache invalidation is required, one will anyway specify
568 * a password to kinit, and that will force invalidation of the ccache
570 return true;
573 return false;
576 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
577 const char *(*domain_cb) (struct cli_credentials *))
579 if (cred->domain_obtained < CRED_CALLBACK) {
580 cred->domain_cb = domain_cb;
581 cred->domain_obtained = CRED_CALLBACK;
582 return true;
585 return false;
589 * Obtain the Kerberos realm for this credentials context.
590 * @param cred credentials context
591 * @retval The realm set on this context.
592 * @note Return value will never be NULL except by programmer error.
594 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
596 if (cred->machine_account_pending) {
597 cli_credentials_set_machine_account(cred,
598 cred->machine_account_pending_lp_ctx);
601 if (cred->realm_obtained == CRED_CALLBACK &&
602 !cred->callback_running) {
603 cred->callback_running = true;
604 cred->realm = cred->realm_cb(cred);
605 cred->callback_running = false;
606 if (cred->realm_obtained == CRED_CALLBACK) {
607 cred->realm_obtained = CRED_CALLBACK_RESULT;
608 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
612 return cred->realm;
616 * Set the realm for this credentials context, and force it to
617 * uppercase for the sainity of our local kerberos libraries
619 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
620 const char *val,
621 enum credentials_obtained obtained)
623 if (obtained >= cred->realm_obtained) {
624 cred->realm = strupper_talloc(cred, val);
625 cred->realm_obtained = obtained;
626 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
627 return true;
630 return false;
633 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
634 const char *(*realm_cb) (struct cli_credentials *))
636 if (cred->realm_obtained < CRED_CALLBACK) {
637 cred->realm_cb = realm_cb;
638 cred->realm_obtained = CRED_CALLBACK;
639 return true;
642 return false;
646 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
648 * @param cred credentials context
649 * @retval The workstation name set on this context.
650 * @note Return value will never be NULL except by programmer error.
652 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
654 if (cred->workstation_obtained == CRED_CALLBACK &&
655 !cred->callback_running) {
656 cred->callback_running = true;
657 cred->workstation = cred->workstation_cb(cred);
658 cred->callback_running = false;
659 if (cred->workstation_obtained == CRED_CALLBACK) {
660 cred->workstation_obtained = CRED_CALLBACK_RESULT;
664 return cred->workstation;
667 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
668 const char *val,
669 enum credentials_obtained obtained)
671 if (obtained >= cred->workstation_obtained) {
672 cred->workstation = talloc_strdup(cred, val);
673 cred->workstation_obtained = obtained;
674 return true;
677 return false;
680 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
681 const char *(*workstation_cb) (struct cli_credentials *))
683 if (cred->workstation_obtained < CRED_CALLBACK) {
684 cred->workstation_cb = workstation_cb;
685 cred->workstation_obtained = CRED_CALLBACK;
686 return true;
689 return false;
693 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
695 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
697 * @param credentials Credentials structure on which to set the password
698 * @param data the string containing the username, password etc
699 * @param obtained This enum describes how 'specified' this password is
702 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
704 char *uname, *p;
706 if (strcmp("%",data) == 0) {
707 cli_credentials_set_anonymous(credentials);
708 return;
711 uname = talloc_strdup(credentials, data);
712 if ((p = strchr_m(uname,'%'))) {
713 *p = 0;
714 cli_credentials_set_password(credentials, p+1, obtained);
717 if ((p = strchr_m(uname,'@'))) {
718 cli_credentials_set_principal(credentials, uname, obtained);
719 *p = 0;
720 cli_credentials_set_realm(credentials, p+1, obtained);
721 return;
722 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
723 *p = 0;
724 cli_credentials_set_domain(credentials, uname, obtained);
725 uname = p+1;
727 cli_credentials_set_username(credentials, uname, obtained);
731 * Given a a credentials structure, print it as a string
733 * The format output is [domain\\]user[%password] or user[@realm][%password]
735 * @param credentials Credentials structure on which to set the password
736 * @param mem_ctx The memory context to place the result on
739 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
741 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
742 const char *domain;
743 const char *username;
744 const char *name;
746 if (bind_dn) {
747 name = talloc_strdup(mem_ctx, bind_dn);
748 } else {
749 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
750 if (domain && domain[0]) {
751 name = talloc_asprintf(mem_ctx, "%s\\%s",
752 domain, username);
753 } else {
754 name = talloc_asprintf(mem_ctx, "%s",
755 username);
758 return name;
762 * Specifies default values for domain, workstation and realm
763 * from the smb.conf configuration file
765 * @param cred Credentials structure to fill in
767 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
768 struct loadparm_context *lp_ctx)
770 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
771 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
772 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
773 } else {
774 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
776 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
777 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
778 } else {
779 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
781 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
782 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
783 } else {
784 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
789 * Guess defaults for credentials from environment variables,
790 * and from the configuration file
792 * @param cred Credentials structure to fill in
794 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
795 struct loadparm_context *lp_ctx)
797 char *p;
798 const char *error_string;
800 if (lp_ctx != NULL) {
801 cli_credentials_set_conf(cred, lp_ctx);
804 if (getenv("LOGNAME")) {
805 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
808 if (getenv("USER")) {
809 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
810 if ((p = strchr_m(getenv("USER"),'%'))) {
811 memset(p,0,strlen(cred->password));
815 if (getenv("PASSWD")) {
816 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
819 if (getenv("PASSWD_FD")) {
820 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
821 CRED_GUESS_FILE);
824 p = getenv("PASSWD_FILE");
825 if (p && p[0]) {
826 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
829 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
830 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
831 &error_string);
836 * Attach NETLOGON credentials for use with SCHANNEL
839 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
840 struct netlogon_creds_CredentialState *netlogon_creds)
842 TALLOC_FREE(cred->netlogon_creds);
843 if (netlogon_creds == NULL) {
844 return;
846 cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
850 * Return attached NETLOGON credentials
853 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
855 return cred->netlogon_creds;
858 /**
859 * Set NETLOGON secure channel type
862 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
863 enum netr_SchannelType secure_channel_type)
865 cred->secure_channel_type = secure_channel_type;
869 * Return NETLOGON secure chanel type
872 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
874 return cred->password_last_changed_time;
877 /**
878 * Set NETLOGON secure channel type
881 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
882 time_t last_changed_time)
884 cred->password_last_changed_time = last_changed_time;
888 * Return NETLOGON secure chanel type
891 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
893 return cred->secure_channel_type;
897 * Fill in a credentials structure as the anonymous user
899 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
901 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
902 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
903 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
904 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
905 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
909 * Describe a credentials context as anonymous or authenticated
910 * @retval true if anonymous, false if a username is specified
913 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
915 const char *username;
917 /* if bind dn is set it's not anonymous */
918 if (cred->bind_dn) {
919 return false;
922 if (cred->machine_account_pending) {
923 cli_credentials_set_machine_account(cred,
924 cred->machine_account_pending_lp_ctx);
927 username = cli_credentials_get_username(cred);
929 /* Yes, it is deliberate that we die if we have a NULL pointer
930 * here - anonymous is "", not NULL, which is 'never specified,
931 * never guessed', ie programmer bug */
932 if (!username[0]) {
933 return true;
936 return false;
940 * Mark the current password for a credentials struct as wrong. This will
941 * cause the password to be prompted again (if a callback is set).
943 * This will decrement the number of times the password can be tried.
945 * @retval whether the credentials struct is finished
947 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
949 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
950 return false;
953 if (cred->password_tries == 0) {
954 return false;
957 cred->password_tries--;
959 if (cred->password_tries == 0) {
960 return false;
963 cred->password_obtained = CRED_CALLBACK;
964 return true;
967 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
968 const char **username,
969 const char **domain)
971 if (cred->principal_obtained > cred->username_obtained) {
972 *domain = talloc_strdup(mem_ctx, "");
973 *username = cli_credentials_get_principal(cred, mem_ctx);
974 } else {
975 *domain = cli_credentials_get_domain(cred);
976 *username = cli_credentials_get_username(cred);
981 * Read a named file, and parse it for username, domain, realm and password
983 * @param credentials Credentials structure on which to set the password
984 * @param file a named file to read the details from
985 * @param obtained This enum describes how 'specified' this password is
988 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
990 uint16_t len = 0;
991 char *ptr, *val, *param;
992 char **lines;
993 int i, numlines;
995 lines = file_lines_load(file, &numlines, 0, NULL);
997 if (lines == NULL)
999 /* fail if we can't open the credentials file */
1000 d_printf("ERROR: Unable to open credentials file!\n");
1001 return false;
1004 for (i = 0; i < numlines; i++) {
1005 len = strlen(lines[i]);
1007 if (len == 0)
1008 continue;
1010 /* break up the line into parameter & value.
1011 * will need to eat a little whitespace possibly */
1012 param = lines[i];
1013 if (!(ptr = strchr_m (lines[i], '=')))
1014 continue;
1016 val = ptr+1;
1017 *ptr = '\0';
1019 /* eat leading white space */
1020 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1021 val++;
1023 if (strwicmp("password", param) == 0) {
1024 cli_credentials_set_password(cred, val, obtained);
1025 } else if (strwicmp("username", param) == 0) {
1026 cli_credentials_set_username(cred, val, obtained);
1027 } else if (strwicmp("domain", param) == 0) {
1028 cli_credentials_set_domain(cred, val, obtained);
1029 } else if (strwicmp("realm", param) == 0) {
1030 cli_credentials_set_realm(cred, val, obtained);
1032 memset(lines[i], 0, len);
1035 talloc_free(lines);
1037 return true;
1041 * Read a named file, and parse it for a password
1043 * @param credentials Credentials structure on which to set the password
1044 * @param file a named file to read the password from
1045 * @param obtained This enum describes how 'specified' this password is
1048 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1050 int fd = open(file, O_RDONLY, 0);
1051 bool ret;
1053 if (fd < 0) {
1054 fprintf(stderr, "Error opening password file %s: %s\n",
1055 file, strerror(errno));
1056 return false;
1059 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1061 close(fd);
1063 return ret;
1068 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1070 * @param credentials Credentials structure on which to set the password
1071 * @param fd open file descriptor to read the password from
1072 * @param obtained This enum describes how 'specified' this password is
1075 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1076 int fd, enum credentials_obtained obtained)
1078 char *p;
1079 char pass[128];
1081 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1082 p && p - pass < sizeof(pass);) {
1083 switch (read(fd, p, 1)) {
1084 case 1:
1085 if (*p != '\n' && *p != '\0') {
1086 *++p = '\0'; /* advance p, and null-terminate pass */
1087 break;
1089 /* fall through */
1090 case 0:
1091 if (p - pass) {
1092 *p = '\0'; /* null-terminate it, just in case... */
1093 p = NULL; /* then force the loop condition to become false */
1094 break;
1095 } else {
1096 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1097 return false;
1100 default:
1101 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1102 fd, strerror(errno));
1103 return false;
1107 cli_credentials_set_password(credentials, pass, obtained);
1108 return true;