auth/credentials: don't ignore "client use kerberos" and --use-kerberos for machine...
[Samba.git] / auth / credentials / credentials.c
blobd57096c57070d6b3740e64e24205d523396577d7
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 "lib/util/util_file.h"
26 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
27 #include "auth/credentials/credentials.h"
28 #include "auth/credentials/credentials_internal.h"
29 #include "auth/gensec/gensec.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "tevent.h"
32 #include "param/param.h"
33 #include "system/filesys.h"
34 #include "system/passwd.h"
36 /**
37 * Create a new credentials structure
38 * @param mem_ctx TALLOC_CTX parent for credentials structure
40 _PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
42 struct cli_credentials *cred = talloc_zero(mem_ctx, struct cli_credentials);
43 if (cred == NULL) {
44 return cred;
47 cred->winbind_separator = '\\';
49 cred->kerberos_state = CRED_USE_KERBEROS_DESIRED;
51 cred->signing_state = SMB_SIGNING_DEFAULT;
54 * The default value of lpcfg_client_ipc_signing() is REQUIRED, so use
55 * the same value here.
57 cred->ipc_signing_state = SMB_SIGNING_REQUIRED;
58 cred->encryption_state = SMB_ENCRYPTION_DEFAULT;
60 return cred;
63 _PUBLIC_
64 struct cli_credentials *cli_credentials_init_server(TALLOC_CTX *mem_ctx,
65 struct loadparm_context *lp_ctx)
67 struct cli_credentials *server_creds = NULL;
68 NTSTATUS status;
69 bool ok;
71 server_creds = cli_credentials_init(mem_ctx);
72 if (server_creds == NULL) {
73 return NULL;
76 ok = cli_credentials_set_conf(server_creds, lp_ctx);
77 if (!ok) {
78 TALLOC_FREE(server_creds);
79 return NULL;
82 status = cli_credentials_set_machine_account(server_creds, lp_ctx);
83 if (!NT_STATUS_IS_OK(status)) {
84 DEBUG(1, ("Failed to obtain server credentials: %s\n",
85 nt_errstr(status)));
86 TALLOC_FREE(server_creds);
87 return NULL;
90 return server_creds;
93 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
94 void *callback_data)
96 cred->priv_data = callback_data;
99 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
101 return cred->priv_data;
105 * Create a new anonymous credential
106 * @param mem_ctx TALLOC_CTX parent for credentials structure
108 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
110 struct cli_credentials *anon_credentials;
112 anon_credentials = cli_credentials_init(mem_ctx);
113 cli_credentials_set_anonymous(anon_credentials);
115 return anon_credentials;
118 _PUBLIC_ bool cli_credentials_set_kerberos_state(struct cli_credentials *creds,
119 enum credentials_use_kerberos kerberos_state,
120 enum credentials_obtained obtained)
122 if (obtained >= creds->kerberos_state_obtained) {
123 creds->kerberos_state = kerberos_state;
124 creds->kerberos_state_obtained = obtained;
126 return true;
129 return false;
132 _PUBLIC_ void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
133 const char *sasl_mech)
135 TALLOC_FREE(creds->forced_sasl_mech);
136 creds->forced_sasl_mech = talloc_strdup(creds, sasl_mech);
139 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
140 enum credentials_krb_forwardable krb_forwardable)
142 creds->krb_forwardable = krb_forwardable;
145 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
147 return creds->kerberos_state;
150 _PUBLIC_ enum credentials_obtained cli_credentials_get_kerberos_state_obtained(struct cli_credentials *creds)
152 return creds->kerberos_state_obtained;
155 _PUBLIC_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
157 return creds->forced_sasl_mech;
160 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
162 return creds->krb_forwardable;
165 _PUBLIC_ bool cli_credentials_set_gensec_features(struct cli_credentials *creds,
166 uint32_t gensec_features,
167 enum credentials_obtained obtained)
169 if (obtained >= creds->gensec_features_obtained) {
170 creds->gensec_features_obtained = obtained;
171 creds->gensec_features = gensec_features;
173 return true;
176 return false;
179 _PUBLIC_ bool cli_credentials_add_gensec_features(
180 struct cli_credentials *creds,
181 uint32_t gensec_features,
182 enum credentials_obtained obtained)
184 return cli_credentials_set_gensec_features(
185 creds, creds->gensec_features | gensec_features, obtained);
188 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
190 return creds->gensec_features;
194 * @brief Find out how the username was obtained.
196 * @param cred A credentials context.
198 * @return The obtained information for the username.
200 _PUBLIC_ enum credentials_obtained
201 cli_credentials_get_username_obtained(struct cli_credentials *cred)
203 return cred->username_obtained;
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;
234 * @brief Obtain the username for this credentials context.
236 * @param[in] cred The credential context.
238 * @param[in] obtained A pointer to store the obtained information.
240 * return The user name or NULL if an error occurred.
242 _PUBLIC_ const char *
243 cli_credentials_get_username_and_obtained(struct cli_credentials *cred,
244 enum credentials_obtained *obtained)
246 if (obtained != NULL) {
247 *obtained = cred->username_obtained;
250 return cli_credentials_get_username(cred);
253 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
254 const char *val, enum credentials_obtained obtained)
256 if (obtained >= cred->username_obtained) {
257 cred->username = talloc_strdup(cred, val);
258 cred->username_obtained = obtained;
259 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
260 return true;
263 return false;
266 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
267 const char *(*username_cb) (struct cli_credentials *))
269 if (cred->username_obtained < CRED_CALLBACK) {
270 cred->username_cb = username_cb;
271 cred->username_obtained = CRED_CALLBACK;
272 return true;
275 return false;
278 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
279 const char *bind_dn)
281 cred->bind_dn = talloc_strdup(cred, bind_dn);
282 return true;
286 * Obtain the BIND DN for this credentials context.
287 * @param cred credentials context
288 * @retval The username set on this context.
289 * @note Return value will be NULL if not specified explicitly
291 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
293 return cred->bind_dn;
298 * @brief Find out how the principal was obtained.
300 * @param cred A credentials context.
302 * @return The obtained information for the principal.
304 _PUBLIC_ enum credentials_obtained
305 cli_credentials_get_principal_obtained(struct cli_credentials *cred)
307 if (cred->machine_account_pending) {
308 cli_credentials_set_machine_account(cred,
309 cred->machine_account_pending_lp_ctx);
312 if (cred->principal_obtained < cred->username_obtained
313 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
314 const char *effective_username = NULL;
315 const char *effective_realm = NULL;
316 enum credentials_obtained effective_obtained;
319 * We don't want to trigger a callbacks in
320 * cli_credentials_get_username()
321 * cli_credentials_get_domain()
322 * nor
323 * cli_credentials_get_realm()
326 effective_username = cred->username;
327 if (effective_username == NULL || strlen(effective_username) == 0) {
328 return cred->username_obtained;
331 if (cred->domain_obtained > cred->realm_obtained) {
332 effective_realm = cred->domain;
333 effective_obtained = MIN(cred->domain_obtained,
334 cred->username_obtained);
335 } else {
336 effective_realm = cred->realm;
337 effective_obtained = MIN(cred->realm_obtained,
338 cred->username_obtained);
341 if (effective_realm == NULL || strlen(effective_realm) == 0) {
342 effective_realm = cred->domain;
343 effective_obtained = MIN(cred->domain_obtained,
344 cred->username_obtained);
347 if (effective_realm != NULL && strlen(effective_realm) != 0) {
348 return effective_obtained;
352 return cred->principal_obtained;
356 * Obtain the client principal for this credentials context.
357 * @param cred credentials context
358 * @retval The username set on this context.
359 * @note Return value will never be NULL except by programmer error.
361 _PUBLIC_ char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
363 if (cred->machine_account_pending) {
364 cli_credentials_set_machine_account(cred,
365 cred->machine_account_pending_lp_ctx);
368 if (cred->principal_obtained == CRED_CALLBACK &&
369 !cred->callback_running) {
370 cred->callback_running = true;
371 cred->principal = cred->principal_cb(cred);
372 cred->callback_running = false;
373 if (cred->principal_obtained == CRED_CALLBACK) {
374 cred->principal_obtained = CRED_CALLBACK_RESULT;
375 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
379 if (cred->principal_obtained < cred->username_obtained
380 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
381 const char *effective_username = NULL;
382 const char *effective_realm = NULL;
383 enum credentials_obtained effective_obtained;
385 effective_username = cli_credentials_get_username(cred);
386 if (effective_username == NULL || strlen(effective_username) == 0) {
387 *obtained = cred->username_obtained;
388 return NULL;
391 if (cred->domain_obtained > cred->realm_obtained) {
392 effective_realm = cli_credentials_get_domain(cred);
393 effective_obtained = MIN(cred->domain_obtained,
394 cred->username_obtained);
395 } else {
396 effective_realm = cli_credentials_get_realm(cred);
397 effective_obtained = MIN(cred->realm_obtained,
398 cred->username_obtained);
401 if (effective_realm == NULL || strlen(effective_realm) == 0) {
402 effective_realm = cli_credentials_get_domain(cred);
403 effective_obtained = MIN(cred->domain_obtained,
404 cred->username_obtained);
407 if (effective_realm != NULL && strlen(effective_realm) != 0) {
408 *obtained = effective_obtained;
409 return talloc_asprintf(mem_ctx, "%s@%s",
410 effective_username,
411 effective_realm);
414 *obtained = cred->principal_obtained;
415 return talloc_strdup(mem_ctx, cred->principal);
419 * Obtain the client principal for this credentials context.
420 * @param cred credentials context
421 * @retval The username set on this context.
422 * @note Return value will never be NULL except by programmer error.
424 _PUBLIC_ char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
426 enum credentials_obtained obtained;
427 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
430 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
431 const char *val,
432 enum credentials_obtained obtained)
434 if (obtained >= cred->principal_obtained) {
435 cred->principal = talloc_strdup(cred, val);
436 if (cred->principal == NULL) {
437 return false;
439 cred->principal_obtained = obtained;
441 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
442 return true;
445 return false;
448 /* Set a callback to get the principal. This could be a popup dialog,
449 * a terminal prompt or similar. */
450 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
451 const char *(*principal_cb) (struct cli_credentials *))
453 if (cred->principal_obtained < CRED_CALLBACK) {
454 cred->principal_cb = principal_cb;
455 cred->principal_obtained = CRED_CALLBACK;
456 return true;
459 return false;
462 /* Some of our tools are 'anonymous by default'. This is a single
463 * function to determine if authentication has been explicitly
464 * requested */
466 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
468 uint32_t gensec_features = 0;
470 if (cred->bind_dn) {
471 return true;
475 * If we forced the mech we clearly want authentication. E.g. to use
476 * SASL/EXTERNAL which has no credentials.
478 if (cred->forced_sasl_mech) {
479 return true;
482 if (cli_credentials_is_anonymous(cred)){
483 return false;
486 if (cred->principal_obtained >= CRED_SPECIFIED) {
487 return true;
489 if (cred->username_obtained >= CRED_SPECIFIED) {
490 return true;
493 if (cli_credentials_get_kerberos_state(cred) == CRED_USE_KERBEROS_REQUIRED) {
494 return true;
497 gensec_features = cli_credentials_get_gensec_features(cred);
498 if (gensec_features & GENSEC_FEATURE_NTLM_CCACHE) {
499 return true;
502 if (gensec_features & GENSEC_FEATURE_SIGN) {
503 return true;
506 if (gensec_features & GENSEC_FEATURE_SEAL) {
507 return true;
510 return false;
514 * Obtain the password for this credentials context.
515 * @param cred credentials context
516 * @retval If set, the cleartext password, otherwise NULL
518 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
520 if (cred->machine_account_pending) {
521 cli_credentials_set_machine_account(cred,
522 cred->machine_account_pending_lp_ctx);
525 if (cred->password_obtained == CRED_CALLBACK &&
526 !cred->callback_running &&
527 !cred->password_will_be_nt_hash) {
528 cred->callback_running = true;
529 cred->password = cred->password_cb(cred);
530 cred->callback_running = false;
531 if (cred->password_obtained == CRED_CALLBACK) {
532 cred->password_obtained = CRED_CALLBACK_RESULT;
533 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
537 return cred->password;
541 * @brief Find out how the password was obtained.
543 * @param cred A credentials context.
545 * @return The obtained information for the password.
547 _PUBLIC_ enum credentials_obtained
548 cli_credentials_get_password_obtained(struct cli_credentials *cred)
550 return cred->password_obtained;
554 * @brief Obtain the password for this credentials context.
556 * @param[in] cred The credential context.
558 * @param[in] obtained A pointer to store the obtained information.
560 * return The user name or NULL if an error occurred.
562 _PUBLIC_ const char *
563 cli_credentials_get_password_and_obtained(struct cli_credentials *cred,
564 enum credentials_obtained *obtained)
566 const char *password = cli_credentials_get_password(cred);
568 if (obtained != NULL) {
569 *obtained = cred->password_obtained;
572 return password;
575 /* Set a password on the credentials context, including an indication
576 * of 'how' the password was obtained */
578 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
579 const char *val,
580 enum credentials_obtained obtained)
582 if (obtained >= cred->password_obtained) {
584 cred->lm_response = data_blob_null;
585 cred->nt_response = data_blob_null;
586 cred->nt_hash = NULL;
587 cred->password = NULL;
589 cli_credentials_invalidate_ccache(cred, obtained);
591 cred->password_tries = 0;
593 if (val == NULL) {
594 cred->password_obtained = obtained;
595 return true;
598 if (cred->password_will_be_nt_hash) {
599 struct samr_Password *nt_hash = NULL;
600 size_t val_len = strlen(val);
601 size_t converted;
603 nt_hash = talloc(cred, struct samr_Password);
604 if (nt_hash == NULL) {
605 return false;
608 converted = strhex_to_str((char *)nt_hash->hash,
609 sizeof(nt_hash->hash),
610 val, val_len);
611 if (converted != sizeof(nt_hash->hash)) {
612 TALLOC_FREE(nt_hash);
613 return false;
616 cred->nt_hash = nt_hash;
617 cred->password_obtained = obtained;
618 return true;
621 cred->password = talloc_strdup(cred, val);
622 if (cred->password == NULL) {
623 return false;
626 /* Don't print the actual password in talloc memory dumps */
627 talloc_set_name_const(cred->password,
628 "password set via cli_credentials_set_password");
629 cred->password_obtained = obtained;
631 return true;
634 return false;
637 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
638 const char *(*password_cb) (struct cli_credentials *))
640 if (cred->password_obtained < CRED_CALLBACK) {
641 cred->password_tries = 3;
642 cred->password_cb = password_cb;
643 cred->password_obtained = CRED_CALLBACK;
644 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
645 return true;
648 return false;
652 * Obtain the 'old' password for this credentials context (used for join accounts).
653 * @param cred credentials context
654 * @retval If set, the cleartext password, otherwise NULL
656 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
658 if (cred->machine_account_pending) {
659 cli_credentials_set_machine_account(cred,
660 cred->machine_account_pending_lp_ctx);
663 return cred->old_password;
666 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
667 const char *val,
668 enum credentials_obtained obtained)
670 cred->old_password = talloc_strdup(cred, val);
671 if (cred->old_password) {
672 /* Don't print the actual password in talloc memory dumps */
673 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
675 cred->old_nt_hash = NULL;
676 return true;
680 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
682 * Sometimes we only have this much of the password, while the rest of
683 * the time this call avoids calling E_md4hash themselves.
685 * @param cred credentials context
686 * @retval If set, the cleartext password, otherwise NULL
688 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
689 TALLOC_CTX *mem_ctx)
691 enum credentials_obtained password_obtained;
692 enum credentials_obtained ccache_threshold;
693 enum credentials_obtained client_gss_creds_threshold;
694 bool password_is_nt_hash;
695 const char *password = NULL;
696 struct samr_Password *nt_hash = NULL;
698 if (cred->nt_hash != NULL) {
700 * If we already have a hash it's easy.
702 goto return_hash;
706 * This is a bit tricky, with password_will_be_nt_hash
707 * we still need to get the value via the password_callback
708 * but if we did that we should not remember it's state
709 * in the long run so we need to undo it.
712 password_obtained = cred->password_obtained;
713 ccache_threshold = cred->ccache_threshold;
714 client_gss_creds_threshold = cred->client_gss_creds_threshold;
715 password_is_nt_hash = cred->password_will_be_nt_hash;
717 cred->password_will_be_nt_hash = false;
718 password = cli_credentials_get_password(cred);
720 cred->password_will_be_nt_hash = password_is_nt_hash;
721 if (password_is_nt_hash && password_obtained == CRED_CALLBACK) {
723 * We got the nt_hash as string via the callback,
724 * so we need to undo the state change.
726 * And also don't remember it as plaintext password.
728 cred->client_gss_creds_threshold = client_gss_creds_threshold;
729 cred->ccache_threshold = ccache_threshold;
730 cred->password_obtained = password_obtained;
731 cred->password = NULL;
734 if (password == NULL) {
735 return NULL;
738 nt_hash = talloc(cred, struct samr_Password);
739 if (nt_hash == NULL) {
740 return NULL;
743 if (password_is_nt_hash) {
744 size_t password_len = strlen(password);
745 size_t converted;
747 converted = strhex_to_str((char *)nt_hash->hash,
748 sizeof(nt_hash->hash),
749 password, password_len);
750 if (converted != sizeof(nt_hash->hash)) {
751 TALLOC_FREE(nt_hash);
752 return NULL;
754 } else {
755 E_md4hash(password, nt_hash->hash);
758 cred->nt_hash = nt_hash;
759 nt_hash = NULL;
761 return_hash:
762 nt_hash = talloc(mem_ctx, struct samr_Password);
763 if (nt_hash == NULL) {
764 return NULL;
767 *nt_hash = *cred->nt_hash;
769 return nt_hash;
773 * Obtain the old password, in the form MD4(unicode(password)) for this credentials context.
775 * Sometimes we only have this much of the password, while the rest of
776 * the time this call avoids calling E_md4hash themselves.
778 * @param cred credentials context
779 * @retval If set, the cleartext password, otherwise NULL
781 _PUBLIC_ struct samr_Password *cli_credentials_get_old_nt_hash(struct cli_credentials *cred,
782 TALLOC_CTX *mem_ctx)
784 const char *old_password = NULL;
786 if (cred->old_nt_hash != NULL) {
787 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
788 if (!nt_hash) {
789 return NULL;
792 *nt_hash = *cred->old_nt_hash;
794 return nt_hash;
797 old_password = cli_credentials_get_old_password(cred);
798 if (old_password) {
799 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
800 if (!nt_hash) {
801 return NULL;
804 E_md4hash(old_password, nt_hash->hash);
806 return nt_hash;
809 return NULL;
813 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
814 * @param cred credentials context
815 * @retval The domain set on this context.
816 * @note Return value will never be NULL except by programmer error.
818 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
820 if (cred->machine_account_pending) {
821 cli_credentials_set_machine_account(cred,
822 cred->machine_account_pending_lp_ctx);
825 if (cred->domain_obtained == CRED_CALLBACK &&
826 !cred->callback_running) {
827 cred->callback_running = true;
828 cred->domain = cred->domain_cb(cred);
829 cred->callback_running = false;
830 if (cred->domain_obtained == CRED_CALLBACK) {
831 cred->domain_obtained = CRED_CALLBACK_RESULT;
832 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
836 return cred->domain;
840 * @brief Obtain the domain for this credential context.
842 * @param[in] cred The credential context.
844 * @param[out] obtained A pointer to store the obtained information.
846 * @return The domain name or NULL if an error occurred.
848 _PUBLIC_ const char *cli_credentials_get_domain_and_obtained(
849 struct cli_credentials *cred,
850 enum credentials_obtained *obtained)
852 const char *domain = cli_credentials_get_domain(cred);
854 if (obtained != NULL) {
855 *obtained = cred->domain_obtained;
858 return domain;
862 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
863 const char *val,
864 enum credentials_obtained obtained)
866 if (obtained >= cred->domain_obtained) {
867 /* it is important that the domain be in upper case,
868 * particularly for the sensitive NTLMv2
869 * calculations */
870 cred->domain = strupper_talloc(cred, val);
871 cred->domain_obtained = obtained;
872 /* setting domain does not mean we have to invalidate ccache
873 * because domain in not used for Kerberos operations.
874 * If ccache invalidation is required, one will anyway specify
875 * a password to kinit, and that will force invalidation of the ccache
877 return true;
880 return false;
883 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
884 const char *(*domain_cb) (struct cli_credentials *))
886 if (cred->domain_obtained < CRED_CALLBACK) {
887 cred->domain_cb = domain_cb;
888 cred->domain_obtained = CRED_CALLBACK;
889 return true;
892 return false;
896 * Obtain the Kerberos realm for this credentials context.
897 * @param cred credentials context
898 * @retval The realm set on this context.
899 * @note Return value will never be NULL except by programmer error.
901 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
903 if (cred->machine_account_pending) {
904 cli_credentials_set_machine_account(cred,
905 cred->machine_account_pending_lp_ctx);
908 if (cred->realm_obtained == CRED_CALLBACK &&
909 !cred->callback_running) {
910 cred->callback_running = true;
911 cred->realm = cred->realm_cb(cred);
912 cred->callback_running = false;
913 if (cred->realm_obtained == CRED_CALLBACK) {
914 cred->realm_obtained = CRED_CALLBACK_RESULT;
915 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
919 return cred->realm;
923 * Set the realm for this credentials context, and force it to
924 * uppercase for the sanity of our local kerberos libraries
926 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
927 const char *val,
928 enum credentials_obtained obtained)
930 if (obtained >= cred->realm_obtained) {
931 cred->realm = strupper_talloc(cred, val);
932 cred->realm_obtained = obtained;
933 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
934 return true;
937 return false;
940 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
941 const char *(*realm_cb) (struct cli_credentials *))
943 if (cred->realm_obtained < CRED_CALLBACK) {
944 cred->realm_cb = realm_cb;
945 cred->realm_obtained = CRED_CALLBACK;
946 return true;
949 return false;
953 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
955 * @param cred credentials context
956 * @retval The workstation name set on this context.
957 * @note Return value will never be NULL except by programmer error.
959 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
961 if (cred->workstation_obtained == CRED_CALLBACK &&
962 !cred->callback_running) {
963 cred->callback_running = true;
964 cred->workstation = cred->workstation_cb(cred);
965 cred->callback_running = false;
966 if (cred->workstation_obtained == CRED_CALLBACK) {
967 cred->workstation_obtained = CRED_CALLBACK_RESULT;
971 return cred->workstation;
974 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
975 const char *val,
976 enum credentials_obtained obtained)
978 if (obtained >= cred->workstation_obtained) {
979 cred->workstation = talloc_strdup(cred, val);
980 cred->workstation_obtained = obtained;
981 return true;
984 return false;
987 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
988 const char *(*workstation_cb) (struct cli_credentials *))
990 if (cred->workstation_obtained < CRED_CALLBACK) {
991 cred->workstation_cb = workstation_cb;
992 cred->workstation_obtained = CRED_CALLBACK;
993 return true;
996 return false;
1000 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
1002 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
1004 * @param credentials Credentials structure on which to set the password
1005 * @param data the string containing the username, password etc
1006 * @param obtained This enum describes how 'specified' this password is
1009 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
1011 char *uname, *p;
1012 char *uname_free = NULL;
1014 if (strcmp("%",data) == 0) {
1015 cli_credentials_set_anonymous(credentials);
1016 return;
1019 uname = talloc_strdup(credentials, data);
1020 uname_free = uname;
1022 if ((p = strchr_m(uname,'%'))) {
1023 *p = 0;
1024 cli_credentials_set_password(credentials, p+1, obtained);
1027 if ((p = strchr_m(uname,'@'))) {
1029 * We also need to set username and domain
1030 * in order to undo the effect of
1031 * cli_credentials_guess().
1033 cli_credentials_set_username(credentials, uname, obtained);
1034 cli_credentials_set_domain(credentials, "", obtained);
1036 cli_credentials_set_principal(credentials, uname, obtained);
1037 *p = 0;
1038 cli_credentials_set_realm(credentials, p+1, obtained);
1039 TALLOC_FREE(uname_free);
1040 return;
1041 } else if ((p = strchr_m(uname,'\\'))
1042 || (p = strchr_m(uname, '/'))
1043 || (p = strchr_m(uname, credentials->winbind_separator)))
1045 const char *domain = NULL;
1047 domain = uname;
1048 *p = 0;
1049 uname = p+1;
1051 if (obtained == credentials->realm_obtained &&
1052 !strequal_m(credentials->domain, domain))
1055 * We need to undo a former set with the same level
1056 * in order to get the expected result from
1057 * cli_credentials_get_principal().
1059 * But we only need to do that if the domain
1060 * actually changes.
1062 cli_credentials_set_realm(credentials, domain, obtained);
1064 cli_credentials_set_domain(credentials, domain, obtained);
1066 if (obtained == credentials->principal_obtained &&
1067 !strequal_m(credentials->username, uname))
1070 * We need to undo a former set with the same level
1071 * in order to get the expected result from
1072 * cli_credentials_get_principal().
1074 * But we only need to do that if the username
1075 * actually changes.
1077 credentials->principal_obtained = CRED_UNINITIALISED;
1078 credentials->principal = NULL;
1080 cli_credentials_set_username(credentials, uname, obtained);
1082 TALLOC_FREE(uname_free);
1086 * Given a a credentials structure, print it as a string
1088 * The format output is [domain\\]user[%password] or user[@realm][%password]
1090 * @param credentials Credentials structure on which to set the password
1091 * @param mem_ctx The memory context to place the result on
1094 _PUBLIC_ char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
1096 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
1097 const char *domain = NULL;
1098 const char *username = NULL;
1099 char *name = NULL;
1101 if (bind_dn) {
1102 name = talloc_strdup(mem_ctx, bind_dn);
1103 } else {
1104 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
1105 if (domain && domain[0]) {
1106 name = talloc_asprintf(mem_ctx, "%s\\%s",
1107 domain, username);
1108 } else {
1109 name = talloc_asprintf(mem_ctx, "%s",
1110 username);
1113 return name;
1118 * Specifies default values for domain, workstation and realm
1119 * from the smb.conf configuration file
1121 * @param cred Credentials structure to fill in
1123 * @return true on success, false on error.
1125 _PUBLIC_ bool cli_credentials_set_conf(struct cli_credentials *cred,
1126 struct loadparm_context *lp_ctx)
1128 const char *sep = NULL;
1129 const char *realm = lpcfg_realm(lp_ctx);
1130 enum credentials_client_protection protection =
1131 lpcfg_client_protection(lp_ctx);
1132 const char *workgroup = lpcfg_workgroup(lp_ctx);
1133 const char *netbios_name = lpcfg_netbios_name(lp_ctx);
1134 bool ok;
1136 (void)cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
1138 if (workgroup != NULL && strlen(workgroup) == 0) {
1139 workgroup = NULL;
1142 if (workgroup != NULL) {
1143 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
1144 ok = cli_credentials_set_domain(cred,
1145 workgroup,
1146 CRED_SPECIFIED);
1147 if (!ok) {
1148 DBG_ERR("Failed to set domain!\n");
1149 return false;
1151 } else {
1152 (void)cli_credentials_set_domain(cred,
1153 workgroup,
1154 CRED_SMB_CONF);
1158 if (netbios_name != NULL && strlen(netbios_name) == 0) {
1159 netbios_name = NULL;
1162 if (netbios_name != NULL) {
1163 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
1164 ok = cli_credentials_set_workstation(cred,
1165 netbios_name,
1166 CRED_SPECIFIED);
1167 if (!ok) {
1168 DBG_ERR("Failed to set workstation!\n");
1169 return false;
1171 } else {
1172 (void)cli_credentials_set_workstation(cred,
1173 netbios_name,
1174 CRED_SMB_CONF);
1178 if (realm != NULL && strlen(realm) == 0) {
1179 realm = NULL;
1182 if (realm != NULL) {
1183 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
1184 ok = cli_credentials_set_realm(cred,
1185 realm,
1186 CRED_SPECIFIED);
1187 if (!ok) {
1188 DBG_ERR("Failed to set realm!\n");
1189 return false;
1191 } else {
1192 (void)cli_credentials_set_realm(cred,
1193 realm,
1194 CRED_SMB_CONF);
1198 sep = lpcfg_winbind_separator(lp_ctx);
1199 if (sep != NULL && sep[0] != '\0') {
1200 cred->winbind_separator = *lpcfg_winbind_separator(lp_ctx);
1203 if (cred->signing_state_obtained <= CRED_SMB_CONF) {
1204 /* Will be set to default for invalid smb.conf values */
1205 cred->signing_state = lpcfg_client_signing(lp_ctx);
1206 if (cred->signing_state == SMB_SIGNING_DEFAULT) {
1207 switch (protection) {
1208 case CRED_CLIENT_PROTECTION_DEFAULT:
1209 break;
1210 case CRED_CLIENT_PROTECTION_PLAIN:
1211 cred->signing_state = SMB_SIGNING_OFF;
1212 break;
1213 case CRED_CLIENT_PROTECTION_SIGN:
1214 case CRED_CLIENT_PROTECTION_ENCRYPT:
1215 cred->signing_state = SMB_SIGNING_REQUIRED;
1216 break;
1220 cred->signing_state_obtained = CRED_SMB_CONF;
1223 if (cred->ipc_signing_state_obtained <= CRED_SMB_CONF) {
1224 /* Will be set to required for invalid smb.conf values */
1225 cred->ipc_signing_state = lpcfg_client_ipc_signing(lp_ctx);
1226 cred->ipc_signing_state_obtained = CRED_SMB_CONF;
1229 if (cred->encryption_state_obtained <= CRED_SMB_CONF) {
1230 /* Will be set to default for invalid smb.conf values */
1231 cred->encryption_state = lpcfg_client_smb_encrypt(lp_ctx);
1232 if (cred->encryption_state == SMB_ENCRYPTION_DEFAULT) {
1233 switch (protection) {
1234 case CRED_CLIENT_PROTECTION_DEFAULT:
1235 break;
1236 case CRED_CLIENT_PROTECTION_PLAIN:
1237 case CRED_CLIENT_PROTECTION_SIGN:
1238 cred->encryption_state = SMB_ENCRYPTION_OFF;
1239 break;
1240 case CRED_CLIENT_PROTECTION_ENCRYPT:
1241 cred->encryption_state = SMB_ENCRYPTION_REQUIRED;
1242 break;
1247 if (cred->kerberos_state_obtained <= CRED_SMB_CONF) {
1248 /* Will be set to default for invalid smb.conf values */
1249 cred->kerberos_state = lpcfg_client_use_kerberos(lp_ctx);
1250 cred->kerberos_state_obtained = CRED_SMB_CONF;
1253 if (cred->gensec_features_obtained <= CRED_SMB_CONF) {
1254 switch (protection) {
1255 case CRED_CLIENT_PROTECTION_DEFAULT:
1256 break;
1257 case CRED_CLIENT_PROTECTION_PLAIN:
1258 cred->gensec_features = 0;
1259 break;
1260 case CRED_CLIENT_PROTECTION_SIGN:
1261 cred->gensec_features = GENSEC_FEATURE_SIGN;
1262 break;
1263 case CRED_CLIENT_PROTECTION_ENCRYPT:
1264 cred->gensec_features =
1265 GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL;
1266 break;
1268 cred->gensec_features_obtained = CRED_SMB_CONF;
1271 return true;
1275 * Guess defaults for credentials from environment variables,
1276 * and from the configuration file
1278 * @param cred Credentials structure to fill in
1280 _PUBLIC_ bool cli_credentials_guess(struct cli_credentials *cred,
1281 struct loadparm_context *lp_ctx)
1283 const char *error_string;
1284 const char *env = NULL;
1285 struct passwd *pwd = NULL;
1286 bool ok;
1288 if (lp_ctx != NULL) {
1289 ok = cli_credentials_set_conf(cred, lp_ctx);
1290 if (!ok) {
1291 return false;
1295 pwd = getpwuid(getuid());
1296 if (pwd != NULL) {
1297 size_t len = strlen(pwd->pw_name);
1299 if (len > 0 && len <= 1024) {
1300 (void)cli_credentials_parse_string(cred,
1301 pwd->pw_name,
1302 CRED_GUESS_ENV);
1306 env = getenv("LOGNAME");
1307 if (env != NULL) {
1308 size_t len = strlen(env);
1310 if (len > 0 && len <= 1024) {
1311 (void)cli_credentials_set_username(cred,
1312 env,
1313 CRED_GUESS_ENV);
1317 env = getenv("USER");
1318 if (env != NULL) {
1319 size_t len = strlen(env);
1321 if (len > 0 && len <= 1024) {
1322 char *p = NULL;
1324 (void)cli_credentials_parse_string(cred,
1325 env,
1326 CRED_GUESS_ENV);
1327 if ((p = strchr_m(env, '%'))) {
1328 memset(p, '\0', strlen(cred->password));
1333 env = getenv("PASSWD");
1334 if (env != NULL) {
1335 size_t len = strlen(env);
1337 if (len > 0 && len <= 1024) {
1338 (void)cli_credentials_set_password(cred,
1339 env,
1340 CRED_GUESS_ENV);
1344 env = getenv("PASSWD_FD");
1345 if (env != NULL) {
1346 size_t len = strlen(env);
1348 if (len > 0 && len <= 1024) {
1349 int fd = atoi(env);
1351 (void)cli_credentials_parse_password_fd(cred,
1353 CRED_GUESS_FILE);
1357 env = getenv("PASSWD_FILE");
1358 if (env != NULL) {
1359 size_t len = strlen(env);
1361 if (len > 0 && len <= 4096) {
1362 (void)cli_credentials_parse_password_file(cred,
1363 env,
1364 CRED_GUESS_FILE);
1368 if (lp_ctx != NULL &&
1369 cli_credentials_get_kerberos_state(cred) != CRED_USE_KERBEROS_DISABLED) {
1370 (void)cli_credentials_set_ccache(cred,
1371 lp_ctx,
1372 NULL,
1373 CRED_GUESS_FILE,
1374 &error_string);
1377 return true;
1381 * Attach NETLOGON credentials for use with SCHANNEL
1384 _PUBLIC_ void cli_credentials_set_netlogon_creds(
1385 struct cli_credentials *cred,
1386 const struct netlogon_creds_CredentialState *netlogon_creds)
1388 TALLOC_FREE(cred->netlogon_creds);
1389 if (netlogon_creds == NULL) {
1390 return;
1392 cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
1396 * Return attached NETLOGON credentials
1399 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
1401 return cred->netlogon_creds;
1405 * Set NETLOGON secure channel type
1408 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
1409 enum netr_SchannelType secure_channel_type)
1411 cred->secure_channel_type = secure_channel_type;
1415 * Return NETLOGON secure channel type
1418 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
1420 return cred->password_last_changed_time;
1424 * Set NETLOGON secure channel type
1427 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
1428 time_t last_changed_time)
1430 cred->password_last_changed_time = last_changed_time;
1434 * Return NETLOGON secure channel type
1437 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
1439 return cred->secure_channel_type;
1443 * Fill in a credentials structure as the anonymous user
1445 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
1447 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
1448 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
1449 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
1450 cli_credentials_set_principal(cred, NULL, CRED_SPECIFIED);
1451 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
1452 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
1453 cli_credentials_set_kerberos_state(cred,
1454 CRED_USE_KERBEROS_DISABLED,
1455 CRED_SPECIFIED);
1459 * Describe a credentials context as anonymous or authenticated
1460 * @retval true if anonymous, false if a username is specified
1463 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
1465 const char *username;
1467 /* if bind dn is set it's not anonymous */
1468 if (cred->bind_dn) {
1469 return false;
1472 if (cred->machine_account_pending) {
1473 cli_credentials_set_machine_account(cred,
1474 cred->machine_account_pending_lp_ctx);
1477 /* if principal is set, it's not anonymous */
1478 if ((cred->principal != NULL) && cred->principal_obtained >= cred->username_obtained) {
1479 return false;
1482 username = cli_credentials_get_username(cred);
1484 /* Yes, it is deliberate that we die if we have a NULL pointer
1485 * here - anonymous is "", not NULL, which is 'never specified,
1486 * never guessed', ie programmer bug */
1487 if (!username[0]) {
1488 return true;
1491 return false;
1495 * Mark the current password for a credentials struct as wrong. This will
1496 * cause the password to be prompted again (if a callback is set).
1498 * This will decrement the number of times the password can be tried.
1500 * @retval whether the credentials struct is finished
1502 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
1504 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
1505 return false;
1508 if (cred->password_tries == 0) {
1509 return false;
1512 cred->password_tries--;
1514 if (cred->password_tries == 0) {
1515 return false;
1518 cred->password_obtained = CRED_CALLBACK;
1519 return true;
1522 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
1523 const char **username,
1524 const char **domain)
1526 if (cred->principal_obtained >= cred->username_obtained) {
1527 *domain = talloc_strdup(mem_ctx, "");
1528 *username = cli_credentials_get_principal(cred, mem_ctx);
1529 } else {
1530 *domain = cli_credentials_get_domain(cred);
1531 *username = cli_credentials_get_username(cred);
1536 * Read a named file, and parse it for username, domain, realm and password
1538 * @param credentials Credentials structure on which to set the password
1539 * @param file a named file to read the details from
1540 * @param obtained This enum describes how 'specified' this password is
1543 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
1545 uint16_t len = 0;
1546 char *ptr, *val, *param;
1547 char **lines;
1548 int i, numlines;
1549 const char *realm = NULL;
1550 const char *domain = NULL;
1551 const char *password = NULL;
1552 const char *username = NULL;
1554 lines = file_lines_load(file, &numlines, 0, NULL);
1556 if (lines == NULL)
1558 /* fail if we can't open the credentials file */
1559 d_printf("ERROR: Unable to open credentials file!\n");
1560 return false;
1563 for (i = 0; i < numlines; i++) {
1564 len = strlen(lines[i]);
1566 if (len == 0)
1567 continue;
1569 /* break up the line into parameter & value.
1570 * will need to eat a little whitespace possibly */
1571 param = lines[i];
1572 if (!(ptr = strchr_m (lines[i], '=')))
1573 continue;
1575 val = ptr+1;
1576 *ptr = '\0';
1578 /* eat leading white space */
1579 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1580 val++;
1582 if (strwicmp("password", param) == 0) {
1583 password = val;
1584 } else if (strwicmp("username", param) == 0) {
1585 username = val;
1586 } else if (strwicmp("domain", param) == 0) {
1587 domain = val;
1588 } else if (strwicmp("realm", param) == 0) {
1589 realm = val;
1593 * We need to readd '=' in order to let
1594 * the strlen() work in the last loop
1595 * that clears the memory.
1597 *ptr = '=';
1600 if (realm != NULL && strlen(realm) != 0) {
1602 * only overwrite with a valid string
1604 cli_credentials_set_realm(cred, realm, obtained);
1607 if (domain != NULL && strlen(domain) != 0) {
1609 * only overwrite with a valid string
1611 cli_credentials_set_domain(cred, domain, obtained);
1614 if (password != NULL) {
1616 * Here we allow "".
1618 cli_credentials_set_password(cred, password, obtained);
1621 if (username != NULL) {
1623 * The last "username" line takes preference
1624 * if the string also contains domain, realm or
1625 * password.
1627 cli_credentials_parse_string(cred, username, obtained);
1630 for (i = 0; i < numlines; i++) {
1631 len = strlen(lines[i]);
1632 memset(lines[i], 0, len);
1634 talloc_free(lines);
1636 return true;
1640 * Read a named file, and parse it for a password
1642 * @param credentials Credentials structure on which to set the password
1643 * @param file a named file to read the password from
1644 * @param obtained This enum describes how 'specified' this password is
1647 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1649 int fd = open(file, O_RDONLY, 0);
1650 bool ret;
1652 if (fd < 0) {
1653 fprintf(stderr, "Error opening password file %s: %s\n",
1654 file, strerror(errno));
1655 return false;
1658 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1660 close(fd);
1662 return ret;
1667 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1669 * @param credentials Credentials structure on which to set the password
1670 * @param fd open file descriptor to read the password from
1671 * @param obtained This enum describes how 'specified' this password is
1674 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1675 int fd, enum credentials_obtained obtained)
1677 char *p;
1678 char pass[128];
1680 if (credentials->password_obtained >= obtained) {
1681 return false;
1684 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1685 p && p - pass < sizeof(pass) - 1;) {
1686 switch (read(fd, p, 1)) {
1687 case 1:
1688 if (*p != '\n' && *p != '\0') {
1689 *++p = '\0'; /* advance p, and null-terminate pass */
1690 break;
1693 FALL_THROUGH;
1694 case 0:
1695 if (p - pass) {
1696 *p = '\0'; /* null-terminate it, just in case... */
1697 p = NULL; /* then force the loop condition to become false */
1698 break;
1701 fprintf(stderr,
1702 "Error reading password from file descriptor "
1703 "%d: empty password\n",
1704 fd);
1705 return false;
1707 default:
1708 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1709 fd, strerror(errno));
1710 return false;
1714 cli_credentials_set_password(credentials, pass, obtained);
1715 return true;
1719 * @brief Set the SMB signing state to request for a SMB connection.
1721 * @param[in] creds The credentials structure to update.
1723 * @param[in] signing_state The signing state to set.
1725 * @param obtained This way the described signing state was specified.
1727 * @return true if we could set the signing state, false otherwise.
1729 _PUBLIC_ bool cli_credentials_set_smb_signing(struct cli_credentials *creds,
1730 enum smb_signing_setting signing_state,
1731 enum credentials_obtained obtained)
1733 if (obtained >= creds->signing_state_obtained) {
1734 creds->signing_state_obtained = obtained;
1735 creds->signing_state = signing_state;
1736 return true;
1739 return false;
1743 * @brief Obtain the SMB signing state from a credentials structure.
1745 * @param[in] creds The credential structure to obtain the SMB signing state
1746 * from.
1748 * @return The SMB signing state.
1750 _PUBLIC_ enum smb_signing_setting
1751 cli_credentials_get_smb_signing(struct cli_credentials *creds)
1753 return creds->signing_state;
1757 * @brief Set the SMB IPC signing state to request for a SMB connection.
1759 * @param[in] creds The credentials structure to update.
1761 * @param[in] signing_state The signing state to set.
1763 * @param obtained This way the described signing state was specified.
1765 * @return true if we could set the signing state, false otherwise.
1767 _PUBLIC_ bool
1768 cli_credentials_set_smb_ipc_signing(struct cli_credentials *creds,
1769 enum smb_signing_setting ipc_signing_state,
1770 enum credentials_obtained obtained)
1772 if (obtained >= creds->ipc_signing_state_obtained) {
1773 creds->ipc_signing_state_obtained = obtained;
1774 creds->ipc_signing_state = ipc_signing_state;
1775 return true;
1778 return false;
1782 * @brief Obtain the SMB IPC signing state from a credentials structure.
1784 * @param[in] creds The credential structure to obtain the SMB IPC signing
1785 * state from.
1787 * @return The SMB signing state.
1789 _PUBLIC_ enum smb_signing_setting
1790 cli_credentials_get_smb_ipc_signing(struct cli_credentials *creds)
1792 return creds->ipc_signing_state;
1796 * @brief Set the SMB encryption state to request for a SMB connection.
1798 * @param[in] creds The credentials structure to update.
1800 * @param[in] encryption_state The encryption state to set.
1802 * @param obtained This way the described encryption state was specified.
1804 * @return true if we could set the encryption state, false otherwise.
1806 _PUBLIC_ bool cli_credentials_set_smb_encryption(struct cli_credentials *creds,
1807 enum smb_encryption_setting encryption_state,
1808 enum credentials_obtained obtained)
1810 if (obtained >= creds->encryption_state_obtained) {
1811 creds->encryption_state_obtained = obtained;
1812 creds->encryption_state = encryption_state;
1813 return true;
1816 return false;
1819 static const char *obtained_to_str(enum credentials_obtained obtained)
1821 switch (obtained) {
1822 case CRED_UNINITIALISED:
1823 return "CRED_UNINITIALISED";
1824 case CRED_SMB_CONF:
1825 return "CRED_SMB_CONF";
1826 case CRED_CALLBACK:
1827 return "CRED_CALLBACK";
1828 case CRED_GUESS_ENV:
1829 return "CRED_GUESS_ENV";
1830 case CRED_GUESS_FILE:
1831 return "CRED_GUESS_FILE";
1832 case CRED_CALLBACK_RESULT:
1833 return "CRED_CALLBACK_RESULT";
1834 case CRED_SPECIFIED:
1835 return "CRED_SPECIFIED";
1838 /* Never reached */
1839 return "";
1842 static const char *krb5_state_to_str(enum credentials_use_kerberos krb5_state)
1844 switch (krb5_state) {
1845 case CRED_USE_KERBEROS_DISABLED:
1846 return "CRED_USE_KERBEROS_DISABLED";
1847 case CRED_USE_KERBEROS_DESIRED:
1848 return "CRED_USE_KERBEROS_DESIRED";
1849 case CRED_USE_KERBEROS_REQUIRED:
1850 return "CRED_USE_KERBEROS_REQUIRED";
1853 /* Never reached */
1854 return "";
1857 static const char *krb5_fwd_to_str(enum credentials_krb_forwardable krb5_fwd)
1859 switch (krb5_fwd) {
1860 case CRED_AUTO_KRB_FORWARDABLE:
1861 return "CRED_AUTO_KRB_FORWARDABLE";
1862 case CRED_NO_KRB_FORWARDABLE:
1863 return "CRED_NO_KRB_FORWARDABLE";
1864 case CRED_FORCE_KRB_FORWARDABLE:
1865 return "CRED_FORCE_KRB_FORWARDABLE";
1868 /* Never reached */
1869 return "";
1872 static const char *signing_state_to_str(enum smb_signing_setting signing_state)
1874 switch(signing_state) {
1875 case SMB_SIGNING_IPC_DEFAULT:
1876 return "SMB_SIGNING_IPC_DEFAULT";
1877 case SMB_SIGNING_DEFAULT:
1878 return "SMB_SIGNING_DEFAULT";
1879 case SMB_SIGNING_OFF:
1880 return "SMB_SIGNING_OFF";
1881 case SMB_SIGNING_IF_REQUIRED:
1882 return "SMB_SIGNING_IF_REQUIRED";
1883 case SMB_SIGNING_DESIRED:
1884 return "SMB_SIGNING_DESIRED";
1885 case SMB_SIGNING_REQUIRED:
1886 return "SMB_SIGNING_REQUIRED";
1889 /* Never reached */
1890 return "";
1893 static const char *encryption_state_to_str(enum smb_encryption_setting encryption_state)
1895 switch(encryption_state) {
1896 case SMB_ENCRYPTION_DEFAULT:
1897 return "SMB_ENCRYPTION_DEFAULT";
1898 case SMB_ENCRYPTION_OFF:
1899 return "SMB_ENCRYPTION_OFF";
1900 case SMB_ENCRYPTION_IF_REQUIRED:
1901 return "SMB_ENCRYPTION_IF_REQUIRED";
1902 case SMB_ENCRYPTION_DESIRED:
1903 return "SMB_ENCRYPTION_DESIRED";
1904 case SMB_ENCRYPTION_REQUIRED:
1905 return "SMB_ENCRYPTION_REQUIRED";
1908 /* Never reached */
1909 return "";
1912 _PUBLIC_ void cli_credentials_dump(struct cli_credentials *creds)
1914 DBG_ERR("CLI_CREDENTIALS:\n");
1915 DBG_ERR("\n");
1916 DBG_ERR(" Username: %s - %s\n",
1917 creds->username,
1918 obtained_to_str(creds->username_obtained));
1919 DBG_ERR(" Workstation: %s - %s\n",
1920 creds->workstation,
1921 obtained_to_str(creds->workstation_obtained));
1922 DBG_ERR(" Domain: %s - %s\n",
1923 creds->domain,
1924 obtained_to_str(creds->domain_obtained));
1925 DBG_ERR(" Password: %s - %s\n",
1926 creds->password != NULL ? "*SECRET*" : "NULL",
1927 obtained_to_str(creds->password_obtained));
1928 DBG_ERR(" Old password: %s\n",
1929 creds->old_password != NULL ? "*SECRET*" : "NULL");
1930 DBG_ERR(" Password tries: %u\n",
1931 creds->password_tries);
1932 DBG_ERR(" Realm: %s - %s\n",
1933 creds->realm,
1934 obtained_to_str(creds->realm_obtained));
1935 DBG_ERR(" Principal: %s - %s\n",
1936 creds->principal,
1937 obtained_to_str(creds->principal_obtained));
1938 DBG_ERR(" Salt principal: %s\n",
1939 creds->salt_principal);
1940 DBG_ERR(" Impersonate principal: %s\n",
1941 creds->impersonate_principal);
1942 DBG_ERR(" Self service: %s\n",
1943 creds->self_service);
1944 DBG_ERR(" Target service: %s\n",
1945 creds->target_service);
1946 DBG_ERR(" Kerberos state: %s - %s\n",
1947 krb5_state_to_str(creds->kerberos_state),
1948 obtained_to_str(creds->kerberos_state_obtained));
1949 DBG_ERR(" Kerberos forwardable ticket: %s\n",
1950 krb5_fwd_to_str(creds->krb_forwardable));
1951 DBG_ERR(" Signing state: %s - %s\n",
1952 signing_state_to_str(creds->signing_state),
1953 obtained_to_str(creds->signing_state_obtained));
1954 DBG_ERR(" IPC signing state: %s - %s\n",
1955 signing_state_to_str(creds->ipc_signing_state),
1956 obtained_to_str(creds->ipc_signing_state_obtained));
1957 DBG_ERR(" Encryption state: %s - %s\n",
1958 encryption_state_to_str(creds->encryption_state),
1959 obtained_to_str(creds->encryption_state_obtained));
1960 DBG_ERR(" Gensec features: %#X\n",
1961 creds->gensec_features);
1962 DBG_ERR(" Forced sasl mech: %s\n",
1963 creds->forced_sasl_mech);
1964 DBG_ERR(" CCACHE: %p - %s\n",
1965 creds->ccache,
1966 obtained_to_str(creds->ccache_obtained));
1967 DBG_ERR(" CLIENT_GSS_CREDS: %p - %s\n",
1968 creds->client_gss_creds,
1969 obtained_to_str(creds->client_gss_creds_obtained));
1970 DBG_ERR(" SERVER_GSS_CREDS: %p - %s\n",
1971 creds->server_gss_creds,
1972 obtained_to_str(creds->server_gss_creds_obtained));
1973 DBG_ERR(" KEYTAB: %p - %s\n",
1974 creds->keytab,
1975 obtained_to_str(creds->keytab_obtained));
1976 DBG_ERR(" KVNO: %u\n",
1977 creds->kvno);
1978 DBG_ERR("\n");
1982 * @brief Obtain the SMB encryption state from a credentials structure.
1984 * @param[in] creds The credential structure to obtain the SMB encryption state
1985 * from.
1987 * @return The SMB signing state.
1989 _PUBLIC_ enum smb_encryption_setting
1990 cli_credentials_get_smb_encryption(struct cli_credentials *creds)
1992 return creds->encryption_state;
1996 * Encrypt a data blob using the session key and the negotiated encryption
1997 * algorithm
1999 * @param state Credential state, contains the session key and algorithm
2000 * @param data Data blob containing the data to be encrypted.
2003 _PUBLIC_ NTSTATUS netlogon_creds_session_encrypt(
2004 struct netlogon_creds_CredentialState *state,
2005 DATA_BLOB data)
2007 NTSTATUS status;
2009 if (data.data == NULL || data.length == 0) {
2010 DBG_ERR("Nothing to encrypt "
2011 "data.data == NULL or data.length == 0\n");
2012 return NT_STATUS_INVALID_PARAMETER;
2015 * Don't crypt an all-zero password it will give away the
2016 * NETLOGON pipe session key .
2018 if (all_zero(data.data, data.length)) {
2019 DBG_ERR("Supplied data all zeros, could leak session key\n");
2020 return NT_STATUS_INVALID_PARAMETER;
2022 if (state->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
2023 status = netlogon_creds_aes_encrypt(state,
2024 data.data,
2025 data.length);
2026 } else if (state->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
2027 status = netlogon_creds_arcfour_crypt(state,
2028 data.data,
2029 data.length);
2030 } else {
2031 DBG_ERR("Unsupported encryption option negotiated\n");
2032 status = NT_STATUS_NOT_SUPPORTED;
2034 if (!NT_STATUS_IS_OK(status)) {
2035 return status;
2037 return NT_STATUS_OK;