param: use the generated parameter table.
[Samba.git] / auth / credentials / credentials.c
blob3b7d42a29a5a0b525b687a11c71a196f96c86855
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;
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;
80 cred->ccache = NULL;
81 cred->client_gss_creds = NULL;
82 cred->keytab = 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;
97 cred->kvno = 0;
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;
118 return cred;
121 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
122 void *callback_data)
124 cred->priv_data = callback_data;
127 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
129 return cred->priv_data;
132 _PUBLIC_ struct cli_credentials *cli_credentials_shallow_copy(TALLOC_CTX *mem_ctx,
133 struct cli_credentials *src)
135 struct cli_credentials *dst;
137 dst = talloc(mem_ctx, struct cli_credentials);
138 if (dst == NULL) {
139 return NULL;
142 *dst = *src;
144 return dst;
148 * Create a new anonymous credential
149 * @param mem_ctx TALLOC_CTX parent for credentials structure
151 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
153 struct cli_credentials *anon_credentials;
155 anon_credentials = cli_credentials_init(mem_ctx);
156 cli_credentials_set_anonymous(anon_credentials);
158 return anon_credentials;
161 _PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
162 enum credentials_use_kerberos use_kerberos)
164 creds->use_kerberos = use_kerberos;
167 _PUBLIC_ void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
168 const char *sasl_mech)
170 TALLOC_FREE(creds->forced_sasl_mech);
171 creds->forced_sasl_mech = talloc_strdup(creds, sasl_mech);
174 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
175 enum credentials_krb_forwardable krb_forwardable)
177 creds->krb_forwardable = krb_forwardable;
180 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
182 return creds->use_kerberos;
185 _PUBLIC_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
187 return creds->forced_sasl_mech;
190 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
192 return creds->krb_forwardable;
195 _PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
197 creds->gensec_features = gensec_features;
200 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
202 return creds->gensec_features;
207 * Obtain the username for this credentials context.
208 * @param cred credentials context
209 * @retval The username set on this context.
210 * @note Return value will never be NULL except by programmer error.
212 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
214 if (cred->machine_account_pending) {
215 cli_credentials_set_machine_account(cred,
216 cred->machine_account_pending_lp_ctx);
219 if (cred->username_obtained == CRED_CALLBACK &&
220 !cred->callback_running) {
221 cred->callback_running = true;
222 cred->username = cred->username_cb(cred);
223 cred->callback_running = false;
224 if (cred->username_obtained == CRED_CALLBACK) {
225 cred->username_obtained = CRED_CALLBACK_RESULT;
226 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
230 return cred->username;
233 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
234 const char *val, enum credentials_obtained obtained)
236 if (obtained >= cred->username_obtained) {
237 cred->username = talloc_strdup(cred, val);
238 cred->username_obtained = obtained;
239 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
240 return true;
243 return false;
246 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
247 const char *(*username_cb) (struct cli_credentials *))
249 if (cred->username_obtained < CRED_CALLBACK) {
250 cred->username_cb = username_cb;
251 cred->username_obtained = CRED_CALLBACK;
252 return true;
255 return false;
258 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
259 const char *bind_dn)
261 cred->bind_dn = talloc_strdup(cred, bind_dn);
262 return true;
266 * Obtain the BIND DN for this credentials context.
267 * @param cred credentials context
268 * @retval The username set on this context.
269 * @note Return value will be NULL if not specified explictly
271 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
273 return cred->bind_dn;
278 * Obtain the client principal for this credentials context.
279 * @param cred credentials context
280 * @retval The username set on this context.
281 * @note Return value will never be NULL except by programmer error.
283 _PUBLIC_ const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
285 if (cred->machine_account_pending) {
286 cli_credentials_set_machine_account(cred,
287 cred->machine_account_pending_lp_ctx);
290 if (cred->principal_obtained == CRED_CALLBACK &&
291 !cred->callback_running) {
292 cred->callback_running = true;
293 cred->principal = cred->principal_cb(cred);
294 cred->callback_running = false;
295 if (cred->principal_obtained == CRED_CALLBACK) {
296 cred->principal_obtained = CRED_CALLBACK_RESULT;
297 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
301 if (cred->principal_obtained < cred->username_obtained
302 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
303 if (cred->domain_obtained > cred->realm_obtained) {
304 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
305 return talloc_asprintf(mem_ctx, "%s@%s",
306 cli_credentials_get_username(cred),
307 cli_credentials_get_domain(cred));
308 } else {
309 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
310 return talloc_asprintf(mem_ctx, "%s@%s",
311 cli_credentials_get_username(cred),
312 cli_credentials_get_realm(cred));
315 *obtained = cred->principal_obtained;
316 return talloc_strdup(mem_ctx, cred->principal);
320 * Obtain the client principal for this credentials context.
321 * @param cred credentials context
322 * @retval The username set on this context.
323 * @note Return value will never be NULL except by programmer error.
325 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
327 enum credentials_obtained obtained;
328 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
331 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
332 const char *val,
333 enum credentials_obtained obtained)
335 if (obtained >= cred->principal_obtained) {
336 cred->principal = talloc_strdup(cred, val);
337 cred->principal_obtained = obtained;
338 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
339 return true;
342 return false;
345 /* Set a callback to get the principal. This could be a popup dialog,
346 * a terminal prompt or similar. */
347 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
348 const char *(*principal_cb) (struct cli_credentials *))
350 if (cred->principal_obtained < CRED_CALLBACK) {
351 cred->principal_cb = principal_cb;
352 cred->principal_obtained = CRED_CALLBACK;
353 return true;
356 return false;
359 /* Some of our tools are 'anonymous by default'. This is a single
360 * function to determine if authentication has been explicitly
361 * requested */
363 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
365 if (cred->bind_dn) {
366 return true;
370 * If we forced the mech we clearly want authentication. E.g. to use
371 * SASL/EXTERNAL which has no credentials.
373 if (cred->forced_sasl_mech) {
374 return true;
377 if (cli_credentials_is_anonymous(cred)){
378 return false;
381 if (cred->principal_obtained >= CRED_SPECIFIED) {
382 return true;
384 if (cred->username_obtained >= CRED_SPECIFIED) {
385 return true;
388 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
389 return true;
392 return false;
396 * Obtain the password for this credentials context.
397 * @param cred credentials context
398 * @retval If set, the cleartext password, otherwise NULL
400 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
402 if (cred->machine_account_pending) {
403 cli_credentials_set_machine_account(cred,
404 cred->machine_account_pending_lp_ctx);
407 if (cred->password_obtained == CRED_CALLBACK &&
408 !cred->callback_running) {
409 cred->callback_running = true;
410 cred->password = cred->password_cb(cred);
411 cred->callback_running = false;
412 if (cred->password_obtained == CRED_CALLBACK) {
413 cred->password_obtained = CRED_CALLBACK_RESULT;
414 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
418 return cred->password;
421 /* Set a password on the credentials context, including an indication
422 * of 'how' the password was obtained */
424 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
425 const char *val,
426 enum credentials_obtained obtained)
428 if (obtained >= cred->password_obtained) {
429 cred->password_tries = 0;
430 cred->password = talloc_strdup(cred, val);
431 if (cred->password) {
432 /* Don't print the actual password in talloc memory dumps */
433 talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
435 cred->password_obtained = obtained;
436 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
438 cred->nt_hash = NULL;
439 cred->lm_response = data_blob(NULL, 0);
440 cred->nt_response = data_blob(NULL, 0);
441 return true;
444 return false;
447 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
448 const char *(*password_cb) (struct cli_credentials *))
450 if (cred->password_obtained < CRED_CALLBACK) {
451 cred->password_tries = 3;
452 cred->password_cb = password_cb;
453 cred->password_obtained = CRED_CALLBACK;
454 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
455 return true;
458 return false;
462 * Obtain the 'old' password for this credentials context (used for join accounts).
463 * @param cred credentials context
464 * @retval If set, the cleartext password, otherwise NULL
466 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
468 if (cred->machine_account_pending) {
469 cli_credentials_set_machine_account(cred,
470 cred->machine_account_pending_lp_ctx);
473 return cred->old_password;
476 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
477 const char *val,
478 enum credentials_obtained obtained)
480 cred->old_password = talloc_strdup(cred, val);
481 if (cred->old_password) {
482 /* Don't print the actual password in talloc memory dumps */
483 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
485 cred->old_nt_hash = NULL;
486 return true;
490 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
492 * Sometimes we only have this much of the password, while the rest of
493 * the time this call avoids calling E_md4hash themselves.
495 * @param cred credentials context
496 * @retval If set, the cleartext password, otherwise NULL
498 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
499 TALLOC_CTX *mem_ctx)
501 const char *password = NULL;
503 if (cred->nt_hash != NULL) {
504 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
505 if (!nt_hash) {
506 return NULL;
509 *nt_hash = *cred->nt_hash;
511 return nt_hash;
514 password = cli_credentials_get_password(cred);
515 if (password) {
516 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
517 if (!nt_hash) {
518 return NULL;
521 E_md4hash(password, nt_hash->hash);
523 return nt_hash;
526 return NULL;
530 * Obtain the old password, in the form MD4(unicode(password)) for this credentials context.
532 * Sometimes we only have this much of the password, while the rest of
533 * the time this call avoids calling E_md4hash themselves.
535 * @param cred credentials context
536 * @retval If set, the cleartext password, otherwise NULL
538 _PUBLIC_ struct samr_Password *cli_credentials_get_old_nt_hash(struct cli_credentials *cred,
539 TALLOC_CTX *mem_ctx)
541 const char *old_password = NULL;
543 if (cred->old_nt_hash != NULL) {
544 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
545 if (!nt_hash) {
546 return NULL;
549 *nt_hash = *cred->old_nt_hash;
551 return nt_hash;
554 old_password = cli_credentials_get_old_password(cred);
555 if (old_password) {
556 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
557 if (!nt_hash) {
558 return NULL;
561 E_md4hash(old_password, nt_hash->hash);
563 return nt_hash;
566 return NULL;
570 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
571 * @param cred credentials context
572 * @retval The domain set on this context.
573 * @note Return value will never be NULL except by programmer error.
575 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
577 if (cred->machine_account_pending) {
578 cli_credentials_set_machine_account(cred,
579 cred->machine_account_pending_lp_ctx);
582 if (cred->domain_obtained == CRED_CALLBACK &&
583 !cred->callback_running) {
584 cred->callback_running = true;
585 cred->domain = cred->domain_cb(cred);
586 cred->callback_running = false;
587 if (cred->domain_obtained == CRED_CALLBACK) {
588 cred->domain_obtained = CRED_CALLBACK_RESULT;
589 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
593 return cred->domain;
597 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
598 const char *val,
599 enum credentials_obtained obtained)
601 if (obtained >= cred->domain_obtained) {
602 /* it is important that the domain be in upper case,
603 * particularly for the sensitive NTLMv2
604 * calculations */
605 cred->domain = strupper_talloc(cred, val);
606 cred->domain_obtained = obtained;
607 /* setting domain does not mean we have to invalidate ccache
608 * because domain in not used for Kerberos operations.
609 * If ccache invalidation is required, one will anyway specify
610 * a password to kinit, and that will force invalidation of the ccache
612 return true;
615 return false;
618 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
619 const char *(*domain_cb) (struct cli_credentials *))
621 if (cred->domain_obtained < CRED_CALLBACK) {
622 cred->domain_cb = domain_cb;
623 cred->domain_obtained = CRED_CALLBACK;
624 return true;
627 return false;
631 * Obtain the Kerberos realm for this credentials context.
632 * @param cred credentials context
633 * @retval The realm set on this context.
634 * @note Return value will never be NULL except by programmer error.
636 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
638 if (cred->machine_account_pending) {
639 cli_credentials_set_machine_account(cred,
640 cred->machine_account_pending_lp_ctx);
643 if (cred->realm_obtained == CRED_CALLBACK &&
644 !cred->callback_running) {
645 cred->callback_running = true;
646 cred->realm = cred->realm_cb(cred);
647 cred->callback_running = false;
648 if (cred->realm_obtained == CRED_CALLBACK) {
649 cred->realm_obtained = CRED_CALLBACK_RESULT;
650 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
654 return cred->realm;
658 * Set the realm for this credentials context, and force it to
659 * uppercase for the sainity of our local kerberos libraries
661 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
662 const char *val,
663 enum credentials_obtained obtained)
665 if (obtained >= cred->realm_obtained) {
666 cred->realm = strupper_talloc(cred, val);
667 cred->realm_obtained = obtained;
668 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
669 return true;
672 return false;
675 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
676 const char *(*realm_cb) (struct cli_credentials *))
678 if (cred->realm_obtained < CRED_CALLBACK) {
679 cred->realm_cb = realm_cb;
680 cred->realm_obtained = CRED_CALLBACK;
681 return true;
684 return false;
688 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
690 * @param cred credentials context
691 * @retval The workstation name set on this context.
692 * @note Return value will never be NULL except by programmer error.
694 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
696 if (cred->workstation_obtained == CRED_CALLBACK &&
697 !cred->callback_running) {
698 cred->callback_running = true;
699 cred->workstation = cred->workstation_cb(cred);
700 cred->callback_running = false;
701 if (cred->workstation_obtained == CRED_CALLBACK) {
702 cred->workstation_obtained = CRED_CALLBACK_RESULT;
706 return cred->workstation;
709 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
710 const char *val,
711 enum credentials_obtained obtained)
713 if (obtained >= cred->workstation_obtained) {
714 cred->workstation = talloc_strdup(cred, val);
715 cred->workstation_obtained = obtained;
716 return true;
719 return false;
722 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
723 const char *(*workstation_cb) (struct cli_credentials *))
725 if (cred->workstation_obtained < CRED_CALLBACK) {
726 cred->workstation_cb = workstation_cb;
727 cred->workstation_obtained = CRED_CALLBACK;
728 return true;
731 return false;
735 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
737 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
739 * @param credentials Credentials structure on which to set the password
740 * @param data the string containing the username, password etc
741 * @param obtained This enum describes how 'specified' this password is
744 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
746 char *uname, *p;
748 if (strcmp("%",data) == 0) {
749 cli_credentials_set_anonymous(credentials);
750 return;
753 uname = talloc_strdup(credentials, data);
754 if ((p = strchr_m(uname,'%'))) {
755 *p = 0;
756 cli_credentials_set_password(credentials, p+1, obtained);
759 if ((p = strchr_m(uname,'@'))) {
760 cli_credentials_set_principal(credentials, uname, obtained);
761 *p = 0;
762 cli_credentials_set_realm(credentials, p+1, obtained);
763 return;
764 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
765 *p = 0;
766 cli_credentials_set_domain(credentials, uname, obtained);
767 uname = p+1;
769 cli_credentials_set_username(credentials, uname, obtained);
773 * Given a a credentials structure, print it as a string
775 * The format output is [domain\\]user[%password] or user[@realm][%password]
777 * @param credentials Credentials structure on which to set the password
778 * @param mem_ctx The memory context to place the result on
781 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
783 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
784 const char *domain;
785 const char *username;
786 const char *name;
788 if (bind_dn) {
789 name = talloc_strdup(mem_ctx, bind_dn);
790 } else {
791 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
792 if (domain && domain[0]) {
793 name = talloc_asprintf(mem_ctx, "%s\\%s",
794 domain, username);
795 } else {
796 name = talloc_asprintf(mem_ctx, "%s",
797 username);
800 return name;
804 * Specifies default values for domain, workstation and realm
805 * from the smb.conf configuration file
807 * @param cred Credentials structure to fill in
809 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
810 struct loadparm_context *lp_ctx)
812 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
813 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
814 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
815 } else {
816 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
818 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
819 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
820 } else {
821 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
823 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
824 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
825 } else {
826 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
831 * Guess defaults for credentials from environment variables,
832 * and from the configuration file
834 * @param cred Credentials structure to fill in
836 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
837 struct loadparm_context *lp_ctx)
839 char *p;
840 const char *error_string;
842 if (lp_ctx != NULL) {
843 cli_credentials_set_conf(cred, lp_ctx);
846 if (getenv("LOGNAME")) {
847 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
850 if (getenv("USER")) {
851 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
852 if ((p = strchr_m(getenv("USER"),'%'))) {
853 memset(p,0,strlen(cred->password));
857 if (getenv("PASSWD")) {
858 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
861 if (getenv("PASSWD_FD")) {
862 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
863 CRED_GUESS_FILE);
866 p = getenv("PASSWD_FILE");
867 if (p && p[0]) {
868 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
871 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
872 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
873 &error_string);
878 * Attach NETLOGON credentials for use with SCHANNEL
881 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
882 struct netlogon_creds_CredentialState *netlogon_creds)
884 TALLOC_FREE(cred->netlogon_creds);
885 if (netlogon_creds == NULL) {
886 return;
888 cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
892 * Return attached NETLOGON credentials
895 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
897 return cred->netlogon_creds;
900 /**
901 * Set NETLOGON secure channel type
904 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
905 enum netr_SchannelType secure_channel_type)
907 cred->secure_channel_type = secure_channel_type;
911 * Return NETLOGON secure chanel type
914 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
916 return cred->password_last_changed_time;
919 /**
920 * Set NETLOGON secure channel type
923 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
924 time_t last_changed_time)
926 cred->password_last_changed_time = last_changed_time;
930 * Return NETLOGON secure chanel type
933 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
935 return cred->secure_channel_type;
939 * Fill in a credentials structure as the anonymous user
941 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
943 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
944 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
945 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
946 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
947 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
948 cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
952 * Describe a credentials context as anonymous or authenticated
953 * @retval true if anonymous, false if a username is specified
956 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
958 const char *username;
960 /* if bind dn is set it's not anonymous */
961 if (cred->bind_dn) {
962 return false;
965 if (cred->machine_account_pending) {
966 cli_credentials_set_machine_account(cred,
967 cred->machine_account_pending_lp_ctx);
970 /* if principal is set, it's not anonymous */
971 if ((cred->principal != NULL) && cred->principal_obtained >= cred->username_obtained) {
972 return false;
975 username = cli_credentials_get_username(cred);
977 /* Yes, it is deliberate that we die if we have a NULL pointer
978 * here - anonymous is "", not NULL, which is 'never specified,
979 * never guessed', ie programmer bug */
980 if (!username[0]) {
981 return true;
984 return false;
988 * Mark the current password for a credentials struct as wrong. This will
989 * cause the password to be prompted again (if a callback is set).
991 * This will decrement the number of times the password can be tried.
993 * @retval whether the credentials struct is finished
995 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
997 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
998 return false;
1001 if (cred->password_tries == 0) {
1002 return false;
1005 cred->password_tries--;
1007 if (cred->password_tries == 0) {
1008 return false;
1011 cred->password_obtained = CRED_CALLBACK;
1012 return true;
1015 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
1016 const char **username,
1017 const char **domain)
1019 if (cred->principal_obtained > cred->username_obtained) {
1020 *domain = talloc_strdup(mem_ctx, "");
1021 *username = cli_credentials_get_principal(cred, mem_ctx);
1022 } else {
1023 *domain = cli_credentials_get_domain(cred);
1024 *username = cli_credentials_get_username(cred);
1029 * Read a named file, and parse it for username, domain, realm and password
1031 * @param credentials Credentials structure on which to set the password
1032 * @param file a named file to read the details from
1033 * @param obtained This enum describes how 'specified' this password is
1036 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
1038 uint16_t len = 0;
1039 char *ptr, *val, *param;
1040 char **lines;
1041 int i, numlines;
1043 lines = file_lines_load(file, &numlines, 0, NULL);
1045 if (lines == NULL)
1047 /* fail if we can't open the credentials file */
1048 d_printf("ERROR: Unable to open credentials file!\n");
1049 return false;
1052 for (i = 0; i < numlines; i++) {
1053 len = strlen(lines[i]);
1055 if (len == 0)
1056 continue;
1058 /* break up the line into parameter & value.
1059 * will need to eat a little whitespace possibly */
1060 param = lines[i];
1061 if (!(ptr = strchr_m (lines[i], '=')))
1062 continue;
1064 val = ptr+1;
1065 *ptr = '\0';
1067 /* eat leading white space */
1068 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1069 val++;
1071 if (strwicmp("password", param) == 0) {
1072 cli_credentials_set_password(cred, val, obtained);
1073 } else if (strwicmp("username", param) == 0) {
1074 cli_credentials_set_username(cred, val, obtained);
1075 } else if (strwicmp("domain", param) == 0) {
1076 cli_credentials_set_domain(cred, val, obtained);
1077 } else if (strwicmp("realm", param) == 0) {
1078 cli_credentials_set_realm(cred, val, obtained);
1080 memset(lines[i], 0, len);
1083 talloc_free(lines);
1085 return true;
1089 * Read a named file, and parse it for a password
1091 * @param credentials Credentials structure on which to set the password
1092 * @param file a named file to read the password from
1093 * @param obtained This enum describes how 'specified' this password is
1096 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1098 int fd = open(file, O_RDONLY, 0);
1099 bool ret;
1101 if (fd < 0) {
1102 fprintf(stderr, "Error opening password file %s: %s\n",
1103 file, strerror(errno));
1104 return false;
1107 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1109 close(fd);
1111 return ret;
1116 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1118 * @param credentials Credentials structure on which to set the password
1119 * @param fd open file descriptor to read the password from
1120 * @param obtained This enum describes how 'specified' this password is
1123 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1124 int fd, enum credentials_obtained obtained)
1126 char *p;
1127 char pass[128];
1129 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1130 p && p - pass < sizeof(pass);) {
1131 switch (read(fd, p, 1)) {
1132 case 1:
1133 if (*p != '\n' && *p != '\0') {
1134 *++p = '\0'; /* advance p, and null-terminate pass */
1135 break;
1137 /* fall through */
1138 case 0:
1139 if (p - pass) {
1140 *p = '\0'; /* null-terminate it, just in case... */
1141 p = NULL; /* then force the loop condition to become false */
1142 break;
1143 } else {
1144 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1145 return false;
1148 default:
1149 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1150 fd, strerror(errno));
1151 return false;
1155 cli_credentials_set_password(credentials, pass, obtained);
1156 return true;