2 Unix SMB/CIFS implementation.
4 User credentials handling
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "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"
32 #include "param/param.h"
33 #include "system/filesys.h"
34 #include "system/passwd.h"
36 static bool str_is_ascii(const char *s) {
38 for (; s[0] != '\0'; s++) {
49 * Create a new credentials structure
50 * @param mem_ctx TALLOC_CTX parent for credentials structure
52 _PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
54 struct cli_credentials *cred = talloc_zero(mem_ctx, struct cli_credentials);
59 cred->winbind_separator = '\\';
61 cred->kerberos_state = CRED_USE_KERBEROS_DESIRED;
63 cred->signing_state = SMB_SIGNING_DEFAULT;
66 * The default value of lpcfg_client_ipc_signing() is REQUIRED, so use
67 * the same value here.
69 cred->ipc_signing_state = SMB_SIGNING_REQUIRED;
70 cred->encryption_state = SMB_ENCRYPTION_DEFAULT;
76 struct cli_credentials *cli_credentials_init_server(TALLOC_CTX *mem_ctx,
77 struct loadparm_context *lp_ctx)
79 struct cli_credentials *server_creds = NULL;
83 server_creds = cli_credentials_init(mem_ctx);
84 if (server_creds == NULL) {
88 ok = cli_credentials_set_conf(server_creds, lp_ctx);
90 TALLOC_FREE(server_creds);
94 status = cli_credentials_set_machine_account(server_creds, lp_ctx);
95 if (!NT_STATUS_IS_OK(status)) {
96 DEBUG(1, ("Failed to obtain server credentials: %s\n",
98 TALLOC_FREE(server_creds);
105 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
108 cred->priv_data = callback_data;
111 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
113 return cred->priv_data;
117 * Create a new anonymous credential
118 * @param mem_ctx TALLOC_CTX parent for credentials structure
120 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
122 struct cli_credentials *anon_credentials;
124 anon_credentials = cli_credentials_init(mem_ctx);
125 cli_credentials_set_anonymous(anon_credentials);
127 return anon_credentials;
130 _PUBLIC_ bool cli_credentials_set_kerberos_state(struct cli_credentials *creds,
131 enum credentials_use_kerberos kerberos_state,
132 enum credentials_obtained obtained)
134 if (obtained >= creds->kerberos_state_obtained) {
135 creds->kerberos_state = kerberos_state;
136 creds->kerberos_state_obtained = obtained;
144 _PUBLIC_ void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
145 const char *sasl_mech)
147 TALLOC_FREE(creds->forced_sasl_mech);
148 creds->forced_sasl_mech = talloc_strdup(creds, sasl_mech);
151 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
152 enum credentials_krb_forwardable krb_forwardable)
154 creds->krb_forwardable = krb_forwardable;
157 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
159 return creds->kerberos_state;
162 _PUBLIC_ enum credentials_obtained cli_credentials_get_kerberos_state_obtained(struct cli_credentials *creds)
164 return creds->kerberos_state_obtained;
167 _PUBLIC_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
169 return creds->forced_sasl_mech;
172 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
174 return creds->krb_forwardable;
177 _PUBLIC_ bool cli_credentials_set_gensec_features(struct cli_credentials *creds,
178 uint32_t gensec_features,
179 enum credentials_obtained obtained)
181 if (obtained >= creds->gensec_features_obtained) {
182 creds->gensec_features_obtained = obtained;
183 creds->gensec_features = gensec_features;
191 _PUBLIC_ bool cli_credentials_add_gensec_features(
192 struct cli_credentials *creds,
193 uint32_t gensec_features,
194 enum credentials_obtained obtained)
196 return cli_credentials_set_gensec_features(
197 creds, creds->gensec_features | gensec_features, obtained);
200 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
202 return creds->gensec_features;
206 * @brief Find out how the username was obtained.
208 * @param cred A credentials context.
210 * @return The obtained information for the username.
212 _PUBLIC_ enum credentials_obtained
213 cli_credentials_get_username_obtained(struct cli_credentials *cred)
215 return cred->username_obtained;
219 * Obtain the username for this credentials context.
220 * @param cred credentials context
221 * @retval The username set on this context.
222 * @note Return value will never be NULL except by programmer error.
224 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
226 if (cred->machine_account_pending) {
227 cli_credentials_set_machine_account(cred,
228 cred->machine_account_pending_lp_ctx);
231 if (cred->username_obtained == CRED_CALLBACK &&
232 !cred->callback_running) {
233 cred->callback_running = true;
234 cred->username = cred->username_cb(cred);
235 cred->callback_running = false;
236 if (cred->username_obtained == CRED_CALLBACK) {
237 cred->username_obtained = CRED_CALLBACK_RESULT;
238 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
242 return cred->username;
246 * @brief Obtain the username for this credentials context.
248 * @param[in] cred The credential context.
250 * @param[in] obtained A pointer to store the obtained information.
252 * return The user name or NULL if an error occurred.
254 _PUBLIC_ const char *
255 cli_credentials_get_username_and_obtained(struct cli_credentials *cred,
256 enum credentials_obtained *obtained)
258 if (obtained != NULL) {
259 *obtained = cred->username_obtained;
262 return cli_credentials_get_username(cred);
265 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
266 const char *val, enum credentials_obtained obtained)
268 if (obtained >= cred->username_obtained) {
269 cred->username = talloc_strdup(cred, val);
270 cred->username_obtained = obtained;
271 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
278 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
279 const char *(*username_cb) (struct cli_credentials *))
281 if (cred->username_obtained < CRED_CALLBACK) {
282 cred->username_cb = username_cb;
283 cred->username_obtained = CRED_CALLBACK;
290 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
293 cred->bind_dn = talloc_strdup(cred, bind_dn);
298 * Obtain the BIND DN for this credentials context.
299 * @param cred credentials context
300 * @retval The username set on this context.
301 * @note Return value will be NULL if not specified explicitly
303 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
305 return cred->bind_dn;
310 * @brief Find out how the principal was obtained.
312 * @param cred A credentials context.
314 * @return The obtained information for the principal.
316 _PUBLIC_ enum credentials_obtained
317 cli_credentials_get_principal_obtained(struct cli_credentials *cred)
319 if (cred->machine_account_pending) {
320 cli_credentials_set_machine_account(cred,
321 cred->machine_account_pending_lp_ctx);
324 if (cred->principal_obtained < cred->username_obtained
325 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
326 const char *effective_username = NULL;
327 const char *effective_realm = NULL;
328 enum credentials_obtained effective_obtained;
331 * We don't want to trigger a callbacks in
332 * cli_credentials_get_username()
333 * cli_credentials_get_domain()
335 * cli_credentials_get_realm()
338 effective_username = cred->username;
339 if (effective_username == NULL || strlen(effective_username) == 0) {
340 return cred->username_obtained;
343 if (cred->domain_obtained > cred->realm_obtained) {
344 effective_realm = cred->domain;
345 effective_obtained = MIN(cred->domain_obtained,
346 cred->username_obtained);
348 effective_realm = cred->realm;
349 effective_obtained = MIN(cred->realm_obtained,
350 cred->username_obtained);
353 if (effective_realm == NULL || strlen(effective_realm) == 0) {
354 effective_realm = cred->domain;
355 effective_obtained = MIN(cred->domain_obtained,
356 cred->username_obtained);
359 if (effective_realm != NULL && strlen(effective_realm) != 0) {
360 return effective_obtained;
364 return cred->principal_obtained;
368 * Obtain the client principal for this credentials context.
369 * @param cred credentials context
370 * @retval The username set on this context.
371 * @note Return value will never be NULL except by programmer error.
373 _PUBLIC_ char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
375 if (cred->machine_account_pending) {
376 cli_credentials_set_machine_account(cred,
377 cred->machine_account_pending_lp_ctx);
380 if (cred->principal_obtained == CRED_CALLBACK &&
381 !cred->callback_running) {
382 const char *princ = NULL;
384 cred->callback_running = true;
385 princ = cred->principal_cb(cred);
386 cred->callback_running = false;
388 cred->principal = NULL;
393 s = talloc_strdup(cred, princ);
402 for (; p[0] != '\0'; p++) {
409 if (cred->principal_obtained == CRED_CALLBACK) {
410 cred->principal_obtained = CRED_CALLBACK_RESULT;
411 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
415 if (cred->principal_obtained < cred->username_obtained
416 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
417 const char *effective_username = NULL;
418 const char *effective_realm = NULL;
419 enum credentials_obtained effective_obtained;
421 effective_username = cli_credentials_get_username(cred);
422 if (effective_username == NULL || strlen(effective_username) == 0) {
423 *obtained = cred->username_obtained;
427 if (cred->domain_obtained > cred->realm_obtained) {
428 effective_realm = cli_credentials_get_domain(cred);
429 effective_obtained = MIN(cred->domain_obtained,
430 cred->username_obtained);
432 effective_realm = cli_credentials_get_realm(cred);
433 effective_obtained = MIN(cred->realm_obtained,
434 cred->username_obtained);
437 if (effective_realm == NULL || strlen(effective_realm) == 0) {
438 effective_realm = cli_credentials_get_domain(cred);
439 effective_obtained = MIN(cred->domain_obtained,
440 cred->username_obtained);
443 if (effective_realm != NULL && strlen(effective_realm) != 0) {
444 *obtained = effective_obtained;
445 return talloc_asprintf(mem_ctx, "%s@%s",
450 *obtained = cred->principal_obtained;
451 return talloc_strdup(mem_ctx, cred->principal);
455 * Obtain the client principal for this credentials context.
456 * @param cred credentials context
457 * @retval The username set on this context.
458 * @note Return value will never be NULL except by programmer error.
460 _PUBLIC_ char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
462 enum credentials_obtained obtained;
463 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
467 * @brief Set the principal for the credentials context.
469 * The realm of the principal will be checked if it is ASCII only and upper
470 * cased if it isn't yet.
472 * @param cred The credential context.
474 * @param val The principal to set or NULL to reset.
476 * @param obtained This way the described principal was specified.
478 * @return true on success, false if the realm is not ASCII or the allocation
481 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
483 enum credentials_obtained obtained)
485 if (obtained >= cred->principal_obtained) {
486 /* If `val = NULL` is passed, principal is reset */
487 cred->principal = NULL;
489 const char *cp = NULL;
493 cp = strchr(val, '@');
495 /* For realm names, only ASCII is allowed */
496 if (!str_is_ascii(cp + 1)) {
501 s = talloc_strdup(cred, val);
510 for (; p[0] != '\0'; p++) {
516 cred->principal_obtained = obtained;
517 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
524 /* Set a callback to get the principal. This could be a popup dialog,
525 * a terminal prompt or similar. */
526 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
527 const char *(*principal_cb) (struct cli_credentials *))
529 if (cred->principal_obtained < CRED_CALLBACK) {
530 cred->principal_cb = principal_cb;
531 cred->principal_obtained = CRED_CALLBACK;
538 /* Some of our tools are 'anonymous by default'. This is a single
539 * function to determine if authentication has been explicitly
542 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
544 uint32_t gensec_features = 0;
551 * If we forced the mech we clearly want authentication. E.g. to use
552 * SASL/EXTERNAL which has no credentials.
554 if (cred->forced_sasl_mech) {
558 if (cli_credentials_is_anonymous(cred)){
562 if (cred->principal_obtained >= CRED_SPECIFIED) {
565 if (cred->username_obtained >= CRED_SPECIFIED) {
569 if (cli_credentials_get_kerberos_state(cred) == CRED_USE_KERBEROS_REQUIRED) {
573 gensec_features = cli_credentials_get_gensec_features(cred);
574 if (gensec_features & GENSEC_FEATURE_NTLM_CCACHE) {
578 if (gensec_features & GENSEC_FEATURE_SIGN) {
582 if (gensec_features & GENSEC_FEATURE_SEAL) {
590 * Obtain the password for this credentials context.
591 * @param cred credentials context
592 * @retval If set, the cleartext password, otherwise NULL
594 _PUBLIC_ const char *cli_credentials_get_password(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->password_obtained == CRED_CALLBACK &&
602 !cred->callback_running &&
603 !cred->password_will_be_nt_hash) {
604 cred->callback_running = true;
605 cred->password = cred->password_cb(cred);
606 cred->callback_running = false;
607 if (cred->password_obtained == CRED_CALLBACK) {
608 cred->password_obtained = CRED_CALLBACK_RESULT;
609 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
613 return cred->password;
617 * @brief Find out how the password was obtained.
619 * @param cred A credentials context.
621 * @return The obtained information for the password.
623 _PUBLIC_ enum credentials_obtained
624 cli_credentials_get_password_obtained(struct cli_credentials *cred)
626 return cred->password_obtained;
630 * @brief Obtain the password for this credentials context.
632 * @param[in] cred The credential context.
634 * @param[in] obtained A pointer to store the obtained information.
636 * return The password if there is one.
638 _PUBLIC_ const char *
639 cli_credentials_get_password_and_obtained(struct cli_credentials *cred,
640 enum credentials_obtained *obtained)
642 const char *password = cli_credentials_get_password(cred);
644 if (obtained != NULL) {
645 *obtained = cred->password_obtained;
651 /* Set a password on the credentials context, including an indication
652 * of 'how' the password was obtained */
654 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
656 enum credentials_obtained obtained)
658 if (obtained >= cred->password_obtained) {
660 cred->lm_response = data_blob_null;
661 cred->nt_response = data_blob_null;
662 cred->nt_hash = NULL;
663 cred->password = NULL;
665 cli_credentials_invalidate_ccache(cred, obtained);
667 cred->password_tries = 0;
670 cred->password_obtained = obtained;
674 if (cred->password_will_be_nt_hash) {
675 struct samr_Password *nt_hash = NULL;
676 size_t val_len = strlen(val);
679 nt_hash = talloc(cred, struct samr_Password);
680 if (nt_hash == NULL) {
683 talloc_keep_secret(nt_hash);
685 converted = strhex_to_str((char *)nt_hash->hash,
686 sizeof(nt_hash->hash),
688 if (converted != sizeof(nt_hash->hash)) {
689 TALLOC_FREE(nt_hash);
693 cred->nt_hash = nt_hash;
694 cred->password_obtained = obtained;
698 cred->password = talloc_strdup(cred, val);
699 if (cred->password == NULL) {
702 talloc_keep_secret(discard_const(cred->password));
703 cred->password_obtained = obtained;
711 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
712 const char *(*password_cb) (struct cli_credentials *))
714 if (cred->password_obtained < CRED_CALLBACK) {
715 cred->password_tries = 3;
716 cred->password_cb = password_cb;
717 cred->password_obtained = CRED_CALLBACK;
718 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
726 * Obtain the 'old' password for this credentials context (used for join accounts).
727 * @param cred credentials context
728 * @retval If set, the cleartext password, otherwise NULL
730 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
732 if (cred->machine_account_pending) {
733 cli_credentials_set_machine_account(cred,
734 cred->machine_account_pending_lp_ctx);
737 return cred->old_password;
740 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
742 enum credentials_obtained obtained)
744 cred->old_nt_hash = NULL;
746 cred->old_password = NULL;
749 cred->old_password = talloc_strdup(cred, val);
750 if (cred->old_password == NULL) {
753 talloc_keep_secret(discard_const(cred->old_password));
758 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
760 * Sometimes we only have this much of the password, while the rest of
761 * the time this call avoids calling E_md4hash themselves.
763 * @param cred credentials context
764 * @retval If set, the cleartext password, otherwise NULL
766 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
769 enum credentials_obtained password_obtained;
770 enum credentials_obtained ccache_threshold;
771 enum credentials_obtained client_gss_creds_threshold;
772 bool password_is_nt_hash;
773 const char *password = NULL;
774 struct samr_Password *nt_hash = NULL;
776 if (cred->nt_hash != NULL) {
778 * If we already have a hash it's easy.
784 * This is a bit tricky, with password_will_be_nt_hash
785 * we still need to get the value via the password_callback
786 * but if we did that we should not remember it's state
787 * in the long run so we need to undo it.
790 password_obtained = cred->password_obtained;
791 ccache_threshold = cred->ccache_threshold;
792 client_gss_creds_threshold = cred->client_gss_creds_threshold;
793 password_is_nt_hash = cred->password_will_be_nt_hash;
795 cred->password_will_be_nt_hash = false;
796 password = cli_credentials_get_password(cred);
798 cred->password_will_be_nt_hash = password_is_nt_hash;
799 if (password_is_nt_hash && password_obtained == CRED_CALLBACK) {
801 * We got the nt_hash as string via the callback,
802 * so we need to undo the state change.
804 * And also don't remember it as plaintext password.
806 cred->client_gss_creds_threshold = client_gss_creds_threshold;
807 cred->ccache_threshold = ccache_threshold;
808 cred->password_obtained = password_obtained;
809 cred->password = NULL;
812 if (password == NULL) {
816 nt_hash = talloc(cred, struct samr_Password);
817 if (nt_hash == NULL) {
820 talloc_keep_secret(nt_hash);
822 if (password_is_nt_hash) {
823 size_t password_len = strlen(password);
826 converted = strhex_to_str((char *)nt_hash->hash,
827 sizeof(nt_hash->hash),
828 password, password_len);
829 if (converted != sizeof(nt_hash->hash)) {
830 TALLOC_FREE(nt_hash);
834 E_md4hash(password, nt_hash->hash);
837 cred->nt_hash = nt_hash;
841 nt_hash = talloc(mem_ctx, struct samr_Password);
842 if (nt_hash == NULL) {
845 talloc_keep_secret(nt_hash);
847 *nt_hash = *cred->nt_hash;
853 * Obtain the old password, in the form MD4(unicode(password)) for this credentials context.
855 * Sometimes we only have this much of the password, while the rest of
856 * the time this call avoids calling E_md4hash themselves.
858 * @param cred credentials context
859 * @retval If set, the cleartext password, otherwise NULL
861 _PUBLIC_ struct samr_Password *cli_credentials_get_old_nt_hash(struct cli_credentials *cred,
864 const char *old_password = NULL;
866 if (cred->old_nt_hash != NULL) {
867 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
871 talloc_keep_secret(nt_hash);
873 *nt_hash = *cred->old_nt_hash;
878 old_password = cli_credentials_get_old_password(cred);
880 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
884 talloc_keep_secret(nt_hash);
886 E_md4hash(old_password, nt_hash->hash);
895 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
896 * @param cred credentials context
897 * @retval The domain set on this context.
898 * @note Return value will never be NULL except by programmer error.
900 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
902 if (cred->machine_account_pending) {
903 cli_credentials_set_machine_account(cred,
904 cred->machine_account_pending_lp_ctx);
907 if (cred->domain_obtained == CRED_CALLBACK &&
908 !cred->callback_running) {
909 cred->callback_running = true;
910 cred->domain = cred->domain_cb(cred);
911 cred->callback_running = false;
912 if (cred->domain_obtained == CRED_CALLBACK) {
913 cred->domain_obtained = CRED_CALLBACK_RESULT;
914 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
922 * @brief Obtain the domain for this credential context.
924 * @param[in] cred The credential context.
926 * @param[out] obtained A pointer to store the obtained information.
928 * @return The domain name or NULL if an error occurred.
930 _PUBLIC_ const char *cli_credentials_get_domain_and_obtained(
931 struct cli_credentials *cred,
932 enum credentials_obtained *obtained)
934 const char *domain = cli_credentials_get_domain(cred);
936 if (obtained != NULL) {
937 *obtained = cred->domain_obtained;
944 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
946 enum credentials_obtained obtained)
948 if (obtained >= cred->domain_obtained) {
949 /* it is important that the domain be in upper case,
950 * particularly for the sensitive NTLMv2
952 cred->domain = strupper_talloc(cred, val);
953 cred->domain_obtained = obtained;
954 /* setting domain does not mean we have to invalidate ccache
955 * because domain in not used for Kerberos operations.
956 * If ccache invalidation is required, one will anyway specify
957 * a password to kinit, and that will force invalidation of the ccache
965 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
966 const char *(*domain_cb) (struct cli_credentials *))
968 if (cred->domain_obtained < CRED_CALLBACK) {
969 cred->domain_cb = domain_cb;
970 cred->domain_obtained = CRED_CALLBACK;
978 * Obtain the Kerberos realm for this credentials context.
979 * @param cred credentials context
980 * @retval The realm set on this context.
981 * @note Return value will never be NULL except by programmer error.
983 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
985 if (cred->machine_account_pending) {
986 cli_credentials_set_machine_account(cred,
987 cred->machine_account_pending_lp_ctx);
990 if (cred->realm_obtained == CRED_CALLBACK &&
991 !cred->callback_running) {
992 const char *realm = NULL;
994 cred->callback_running = true;
995 realm = cred->realm_cb(cred);
996 cred->callback_running = false;
1000 cred->realm = strupper_talloc(cred, realm);
1001 if (cred->realm == NULL) {
1006 if (cred->realm_obtained == CRED_CALLBACK) {
1007 cred->realm_obtained = CRED_CALLBACK_RESULT;
1008 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
1016 * @brief Set the realm for this credentials context.
1018 * The realm be checked if it is ASCII only and upper cased if it isn't yet.
1020 * @param cred The credential context.
1022 * @param val The realm to set or NULL to reset.
1024 * @param obtained This way the described realm was specified.
1026 * @return true on success, false if the realm is not ASCII or the allocation
1029 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
1031 enum credentials_obtained obtained)
1033 if (obtained >= cred->realm_obtained) {
1034 /* If `val = NULL` is passed, realm is reset */
1037 /* For realm names, only ASCII is allowed */
1038 if (!str_is_ascii(val)) {
1042 cred->realm = strupper_talloc(cred, val);
1043 if (cred->realm == NULL) {
1047 cred->realm_obtained = obtained;
1048 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
1055 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
1056 const char *(*realm_cb) (struct cli_credentials *))
1058 if (cred->realm_obtained < CRED_CALLBACK) {
1059 cred->realm_cb = realm_cb;
1060 cred->realm_obtained = CRED_CALLBACK;
1068 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
1070 * @param cred credentials context
1071 * @retval The workstation name set on this context.
1072 * @note Return value will never be NULL except by programmer error.
1074 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
1076 if (cred->workstation_obtained == CRED_CALLBACK &&
1077 !cred->callback_running) {
1078 cred->callback_running = true;
1079 cred->workstation = cred->workstation_cb(cred);
1080 cred->callback_running = false;
1081 if (cred->workstation_obtained == CRED_CALLBACK) {
1082 cred->workstation_obtained = CRED_CALLBACK_RESULT;
1086 return cred->workstation;
1089 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
1091 enum credentials_obtained obtained)
1093 if (obtained >= cred->workstation_obtained) {
1094 cred->workstation = talloc_strdup(cred, val);
1095 cred->workstation_obtained = obtained;
1102 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
1103 const char *(*workstation_cb) (struct cli_credentials *))
1105 if (cred->workstation_obtained < CRED_CALLBACK) {
1106 cred->workstation_cb = workstation_cb;
1107 cred->workstation_obtained = CRED_CALLBACK;
1115 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
1117 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
1119 * @param credentials Credentials structure on which to set the password
1120 * @param data the string containing the username, password etc
1121 * @param obtained This enum describes how 'specified' this password is
1124 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
1127 char *uname_free = NULL;
1129 if (strcmp("%",data) == 0) {
1130 cli_credentials_set_anonymous(credentials);
1134 uname = talloc_strdup(credentials, data);
1135 if (uname == NULL) {
1140 if ((p = strchr_m(uname,'%'))) {
1142 cli_credentials_set_password(credentials, p+1, obtained);
1143 /* zero the copy if it contains password */
1144 talloc_keep_secret(uname_free);
1147 if ((p = strchr_m(uname,'@'))) {
1149 * We also need to set username and domain
1150 * in order to undo the effect of
1151 * cli_credentials_guess().
1153 cli_credentials_set_username(credentials, uname, obtained);
1154 cli_credentials_set_domain(credentials, "", obtained);
1156 cli_credentials_set_principal(credentials, uname, obtained);
1158 cli_credentials_set_realm(credentials, p+1, obtained);
1159 TALLOC_FREE(uname_free);
1161 } else if ((p = strchr_m(uname,'\\'))
1162 || (p = strchr_m(uname, '/'))
1163 || (p = strchr_m(uname, credentials->winbind_separator)))
1165 const char *domain = NULL;
1171 if (obtained == credentials->realm_obtained &&
1172 !strequal_m(credentials->domain, domain))
1175 * We need to undo a former set with the same level
1176 * in order to get the expected result from
1177 * cli_credentials_get_principal().
1179 * But we only need to do that if the domain
1182 cli_credentials_set_realm(credentials, domain, obtained);
1184 cli_credentials_set_domain(credentials, domain, obtained);
1186 if (obtained == credentials->principal_obtained &&
1187 !strequal_m(credentials->username, uname))
1190 * We need to undo a former set with the same level
1191 * in order to get the expected result from
1192 * cli_credentials_get_principal().
1194 * But we only need to do that if the username
1197 credentials->principal_obtained = CRED_UNINITIALISED;
1198 credentials->principal = NULL;
1200 cli_credentials_set_username(credentials, uname, obtained);
1202 TALLOC_FREE(uname_free);
1206 * Given a a credentials structure, print it as a string
1208 * The format output is [domain\\]user[%password] or user[@realm][%password]
1210 * @param credentials Credentials structure on which to set the password
1211 * @param mem_ctx The memory context to place the result on
1214 _PUBLIC_ char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
1216 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
1217 const char *domain = NULL;
1218 const char *username = NULL;
1222 name = talloc_strdup(mem_ctx, bind_dn);
1224 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
1225 if (domain && domain[0]) {
1226 name = talloc_asprintf(mem_ctx, "%s\\%s",
1229 name = talloc_asprintf(mem_ctx, "%s",
1238 * Specifies default values for domain, workstation and realm
1239 * from the smb.conf configuration file
1241 * @param cred Credentials structure to fill in
1243 * @return true on success, false on error.
1245 _PUBLIC_ bool cli_credentials_set_conf(struct cli_credentials *cred,
1246 struct loadparm_context *lp_ctx)
1248 const char *sep = NULL;
1249 const char *realm = lpcfg_realm(lp_ctx);
1250 enum credentials_client_protection protection =
1251 lpcfg_client_protection(lp_ctx);
1252 const char *workgroup = lpcfg_workgroup(lp_ctx);
1253 const char *netbios_name = lpcfg_netbios_name(lp_ctx);
1256 (void)cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
1258 if (workgroup != NULL && strlen(workgroup) == 0) {
1262 if (workgroup != NULL) {
1263 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
1264 ok = cli_credentials_set_domain(cred,
1268 DBG_ERR("Failed to set domain!\n");
1272 (void)cli_credentials_set_domain(cred,
1278 if (netbios_name != NULL && strlen(netbios_name) == 0) {
1279 netbios_name = NULL;
1282 if (netbios_name != NULL) {
1283 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
1284 ok = cli_credentials_set_workstation(cred,
1288 DBG_ERR("Failed to set workstation!\n");
1292 (void)cli_credentials_set_workstation(cred,
1298 if (realm != NULL && strlen(realm) == 0) {
1302 if (realm != NULL) {
1303 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
1304 ok = cli_credentials_set_realm(cred,
1308 DBG_ERR("Failed to set realm!\n");
1312 (void)cli_credentials_set_realm(cred,
1318 sep = lpcfg_winbind_separator(lp_ctx);
1319 if (sep != NULL && sep[0] != '\0') {
1320 cred->winbind_separator = *lpcfg_winbind_separator(lp_ctx);
1323 if (cred->signing_state_obtained <= CRED_SMB_CONF) {
1324 /* Will be set to default for invalid smb.conf values */
1325 cred->signing_state = lpcfg_client_signing(lp_ctx);
1326 if (cred->signing_state == SMB_SIGNING_DEFAULT) {
1327 switch (protection) {
1328 case CRED_CLIENT_PROTECTION_DEFAULT:
1330 case CRED_CLIENT_PROTECTION_PLAIN:
1331 cred->signing_state = SMB_SIGNING_OFF;
1333 case CRED_CLIENT_PROTECTION_SIGN:
1334 case CRED_CLIENT_PROTECTION_ENCRYPT:
1335 cred->signing_state = SMB_SIGNING_REQUIRED;
1340 cred->signing_state_obtained = CRED_SMB_CONF;
1343 if (cred->ipc_signing_state_obtained <= CRED_SMB_CONF) {
1344 /* Will be set to required for invalid smb.conf values */
1345 cred->ipc_signing_state = lpcfg_client_ipc_signing(lp_ctx);
1346 cred->ipc_signing_state_obtained = CRED_SMB_CONF;
1349 if (cred->encryption_state_obtained <= CRED_SMB_CONF) {
1350 /* Will be set to default for invalid smb.conf values */
1351 cred->encryption_state = lpcfg_client_smb_encrypt(lp_ctx);
1352 if (cred->encryption_state == SMB_ENCRYPTION_DEFAULT) {
1353 switch (protection) {
1354 case CRED_CLIENT_PROTECTION_DEFAULT:
1356 case CRED_CLIENT_PROTECTION_PLAIN:
1357 case CRED_CLIENT_PROTECTION_SIGN:
1358 cred->encryption_state = SMB_ENCRYPTION_OFF;
1360 case CRED_CLIENT_PROTECTION_ENCRYPT:
1361 cred->encryption_state = SMB_ENCRYPTION_REQUIRED;
1367 if (cred->kerberos_state_obtained <= CRED_SMB_CONF) {
1368 /* Will be set to default for invalid smb.conf values */
1369 cred->kerberos_state = lpcfg_client_use_kerberos(lp_ctx);
1370 cred->kerberos_state_obtained = CRED_SMB_CONF;
1373 if (cred->gensec_features_obtained <= CRED_SMB_CONF) {
1374 switch (protection) {
1375 case CRED_CLIENT_PROTECTION_DEFAULT:
1377 case CRED_CLIENT_PROTECTION_PLAIN:
1378 cred->gensec_features = 0;
1380 case CRED_CLIENT_PROTECTION_SIGN:
1381 cred->gensec_features = GENSEC_FEATURE_SIGN;
1383 case CRED_CLIENT_PROTECTION_ENCRYPT:
1384 cred->gensec_features =
1385 GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL;
1388 cred->gensec_features_obtained = CRED_SMB_CONF;
1395 * Guess defaults for credentials from environment variables,
1396 * and from the configuration file
1398 * @param cred Credentials structure to fill in
1400 _PUBLIC_ bool cli_credentials_guess(struct cli_credentials *cred,
1401 struct loadparm_context *lp_ctx)
1403 const char *error_string;
1404 const char *env = NULL;
1405 struct passwd *pwd = NULL;
1408 if (lp_ctx != NULL) {
1409 ok = cli_credentials_set_conf(cred, lp_ctx);
1415 pwd = getpwuid(getuid());
1417 size_t len = strlen(pwd->pw_name);
1419 if (len > 0 && len <= 1024) {
1420 (void)cli_credentials_parse_string(cred,
1426 env = getenv("LOGNAME");
1428 size_t len = strlen(env);
1430 if (len > 0 && len <= 1024) {
1431 (void)cli_credentials_set_username(cred,
1437 env = getenv("USER");
1439 size_t len = strlen(env);
1441 if (len > 0 && len <= 1024) {
1442 const char *p = NULL;
1444 (void)cli_credentials_parse_string(cred,
1447 p = strchr_m(env, '%');
1449 memset(discard_const_p(char, p),
1451 strlen(cred->password));
1456 env = getenv("PASSWD");
1458 size_t len = strlen(env);
1460 if (len > 0 && len <= 1024) {
1461 (void)cli_credentials_set_password(cred,
1467 env = getenv("PASSWD_FD");
1469 size_t len = strlen(env);
1471 if (len > 0 && len <= 1024) {
1474 (void)cli_credentials_parse_password_fd(cred,
1480 env = getenv("PASSWD_FILE");
1482 size_t len = strlen(env);
1484 if (len > 0 && len <= 4096) {
1485 (void)cli_credentials_parse_password_file(cred,
1491 if (lp_ctx != NULL &&
1492 cli_credentials_get_kerberos_state(cred) != CRED_USE_KERBEROS_DISABLED) {
1493 (void)cli_credentials_set_ccache(cred,
1504 * Attach NETLOGON credentials for use with SCHANNEL
1507 _PUBLIC_ void cli_credentials_set_netlogon_creds(
1508 struct cli_credentials *cred,
1509 const struct netlogon_creds_CredentialState *netlogon_creds)
1511 TALLOC_FREE(cred->netlogon_creds);
1512 if (netlogon_creds == NULL) {
1515 cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
1519 * Return attached NETLOGON credentials
1522 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
1524 return cred->netlogon_creds;
1528 * Set NETLOGON secure channel type
1531 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
1532 enum netr_SchannelType secure_channel_type)
1534 cred->secure_channel_type = secure_channel_type;
1538 * Return NETLOGON secure channel type
1541 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
1543 return cred->password_last_changed_time;
1547 * Set NETLOGON secure channel type
1550 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
1551 time_t last_changed_time)
1553 cred->password_last_changed_time = last_changed_time;
1557 * Return NETLOGON secure channel type
1560 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
1562 return cred->secure_channel_type;
1566 * Fill in a credentials structure as the anonymous user
1568 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
1570 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
1571 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
1572 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
1573 cli_credentials_set_principal(cred, NULL, CRED_SPECIFIED);
1574 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
1575 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
1576 cli_credentials_set_kerberos_state(cred,
1577 CRED_USE_KERBEROS_DISABLED,
1582 * Describe a credentials context as anonymous or authenticated
1583 * @retval true if anonymous, false if a username is specified
1586 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
1588 const char *username;
1590 /* if bind dn is set it's not anonymous */
1591 if (cred->bind_dn) {
1595 if (cred->machine_account_pending) {
1596 cli_credentials_set_machine_account(cred,
1597 cred->machine_account_pending_lp_ctx);
1600 /* if principal is set, it's not anonymous */
1601 if ((cred->principal != NULL) && cred->principal_obtained >= cred->username_obtained) {
1605 username = cli_credentials_get_username(cred);
1607 /* Yes, it is deliberate that we die if we have a NULL pointer
1608 * here - anonymous is "", not NULL, which is 'never specified,
1609 * never guessed', ie programmer bug */
1618 * Mark the current password for a credentials struct as wrong. This will
1619 * cause the password to be prompted again (if a callback is set).
1621 * This will decrement the number of times the password can be tried.
1623 * @retval whether the credentials struct is finished
1625 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
1627 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
1631 if (cred->password_tries == 0) {
1635 cred->password_tries--;
1637 if (cred->password_tries == 0) {
1641 cred->password_obtained = CRED_CALLBACK;
1645 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
1646 const char **username,
1647 const char **domain)
1649 if (!cli_credentials_is_anonymous(cred) &&
1650 cred->principal_obtained >= cred->username_obtained)
1652 *domain = talloc_strdup(mem_ctx, "");
1653 *username = cli_credentials_get_principal(cred, mem_ctx);
1655 *domain = cli_credentials_get_domain(cred);
1656 *username = cli_credentials_get_username(cred);
1661 * Read a named file, and parse it for username, domain, realm and password
1663 * @param credentials Credentials structure on which to set the password
1664 * @param file a named file to read the details from
1665 * @param obtained This enum describes how 'specified' this password is
1668 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
1671 char *ptr, *val, *param;
1674 const char *realm = NULL;
1675 const char *domain = NULL;
1676 const char *password = NULL;
1677 const char *username = NULL;
1679 lines = file_lines_load(file, &numlines, 0, NULL);
1683 /* fail if we can't open the credentials file */
1684 d_printf("ERROR: Unable to open credentials file!\n");
1688 for (i = 0; i < numlines; i++) {
1689 len = strlen(lines[i]);
1694 /* break up the line into parameter & value.
1695 * will need to eat a little whitespace possibly */
1697 if (!(ptr = strchr_m (lines[i], '=')))
1703 /* eat leading white space */
1704 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1707 if (strwicmp("password", param) == 0) {
1709 } else if (strwicmp("username", param) == 0) {
1711 } else if (strwicmp("domain", param) == 0) {
1713 } else if (strwicmp("realm", param) == 0) {
1718 * We need to readd '=' in order to let
1719 * the strlen() work in the last loop
1720 * that clears the memory.
1725 if (realm != NULL && strlen(realm) != 0) {
1727 * only overwrite with a valid string
1729 cli_credentials_set_realm(cred, realm, obtained);
1732 if (domain != NULL && strlen(domain) != 0) {
1734 * only overwrite with a valid string
1736 cli_credentials_set_domain(cred, domain, obtained);
1739 if (password != NULL) {
1743 cli_credentials_set_password(cred, password, obtained);
1746 if (username != NULL) {
1748 * The last "username" line takes preference
1749 * if the string also contains domain, realm or
1752 cli_credentials_parse_string(cred, username, obtained);
1755 for (i = 0; i < numlines; i++) {
1756 len = strlen(lines[i]);
1757 memset(lines[i], 0, len);
1765 * Read a named file, and parse it for a password
1767 * @param credentials Credentials structure on which to set the password
1768 * @param file a named file to read the password from
1769 * @param obtained This enum describes how 'specified' this password is
1772 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1774 int fd = open(file, O_RDONLY, 0);
1778 fprintf(stderr, "Error opening password file %s: %s\n",
1779 file, strerror(errno));
1783 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1792 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1794 * @param credentials Credentials structure on which to set the password
1795 * @param fd open file descriptor to read the password from
1796 * @param obtained This enum describes how 'specified' this password is
1799 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1800 int fd, enum credentials_obtained obtained)
1805 if (credentials->password_obtained >= obtained) {
1809 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1810 p && p - pass < sizeof(pass) - 1;) {
1811 switch (read(fd, p, 1)) {
1813 if (*p != '\n' && *p != '\0') {
1814 *++p = '\0'; /* advance p, and null-terminate pass */
1821 *p = '\0'; /* null-terminate it, just in case... */
1822 p = NULL; /* then force the loop condition to become false */
1827 "Error reading password from file descriptor "
1828 "%d: empty password\n",
1834 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1835 fd, strerror(errno));
1841 cli_credentials_set_password(credentials, pass, obtained);
1847 * @brief Set the SMB signing state to request for a SMB connection.
1849 * @param[in] creds The credentials structure to update.
1851 * @param[in] signing_state The signing state to set.
1853 * @param obtained This way the described signing state was specified.
1855 * @return true if we could set the signing state, false otherwise.
1857 _PUBLIC_ bool cli_credentials_set_smb_signing(struct cli_credentials *creds,
1858 enum smb_signing_setting signing_state,
1859 enum credentials_obtained obtained)
1861 if (obtained >= creds->signing_state_obtained) {
1862 creds->signing_state_obtained = obtained;
1863 creds->signing_state = signing_state;
1871 * @brief Obtain the SMB signing state from a credentials structure.
1873 * @param[in] creds The credential structure to obtain the SMB signing state
1876 * @return The SMB signing state.
1878 _PUBLIC_ enum smb_signing_setting
1879 cli_credentials_get_smb_signing(struct cli_credentials *creds)
1881 return creds->signing_state;
1885 * @brief Set the SMB IPC signing state to request for a SMB connection.
1887 * @param[in] creds The credentials structure to update.
1889 * @param[in] signing_state The signing state to set.
1891 * @param obtained This way the described signing state was specified.
1893 * @return true if we could set the signing state, false otherwise.
1896 cli_credentials_set_smb_ipc_signing(struct cli_credentials *creds,
1897 enum smb_signing_setting ipc_signing_state,
1898 enum credentials_obtained obtained)
1900 if (obtained >= creds->ipc_signing_state_obtained) {
1901 creds->ipc_signing_state_obtained = obtained;
1902 creds->ipc_signing_state = ipc_signing_state;
1910 * @brief Obtain the SMB IPC signing state from a credentials structure.
1912 * @param[in] creds The credential structure to obtain the SMB IPC signing
1915 * @return The SMB signing state.
1917 _PUBLIC_ enum smb_signing_setting
1918 cli_credentials_get_smb_ipc_signing(struct cli_credentials *creds)
1920 return creds->ipc_signing_state;
1924 * @brief Set the SMB encryption state to request for a SMB connection.
1926 * @param[in] creds The credentials structure to update.
1928 * @param[in] encryption_state The encryption state to set.
1930 * @param obtained This way the described encryption state was specified.
1932 * @return true if we could set the encryption state, false otherwise.
1934 _PUBLIC_ bool cli_credentials_set_smb_encryption(struct cli_credentials *creds,
1935 enum smb_encryption_setting encryption_state,
1936 enum credentials_obtained obtained)
1938 if (obtained >= creds->encryption_state_obtained) {
1939 creds->encryption_state_obtained = obtained;
1940 creds->encryption_state = encryption_state;
1947 static const char *obtained_to_str(enum credentials_obtained obtained)
1950 case CRED_UNINITIALISED:
1951 return "CRED_UNINITIALISED";
1953 return "CRED_SMB_CONF";
1955 return "CRED_CALLBACK";
1956 case CRED_GUESS_ENV:
1957 return "CRED_GUESS_ENV";
1958 case CRED_GUESS_FILE:
1959 return "CRED_GUESS_FILE";
1960 case CRED_CALLBACK_RESULT:
1961 return "CRED_CALLBACK_RESULT";
1962 case CRED_SPECIFIED:
1963 return "CRED_SPECIFIED";
1970 static const char *krb5_state_to_str(enum credentials_use_kerberos krb5_state)
1972 switch (krb5_state) {
1973 case CRED_USE_KERBEROS_DISABLED:
1974 return "CRED_USE_KERBEROS_DISABLED";
1975 case CRED_USE_KERBEROS_DESIRED:
1976 return "CRED_USE_KERBEROS_DESIRED";
1977 case CRED_USE_KERBEROS_REQUIRED:
1978 return "CRED_USE_KERBEROS_REQUIRED";
1985 static const char *krb5_fwd_to_str(enum credentials_krb_forwardable krb5_fwd)
1988 case CRED_AUTO_KRB_FORWARDABLE:
1989 return "CRED_AUTO_KRB_FORWARDABLE";
1990 case CRED_NO_KRB_FORWARDABLE:
1991 return "CRED_NO_KRB_FORWARDABLE";
1992 case CRED_FORCE_KRB_FORWARDABLE:
1993 return "CRED_FORCE_KRB_FORWARDABLE";
2000 static const char *signing_state_to_str(enum smb_signing_setting signing_state)
2002 switch(signing_state) {
2003 case SMB_SIGNING_IPC_DEFAULT:
2004 return "SMB_SIGNING_IPC_DEFAULT";
2005 case SMB_SIGNING_DEFAULT:
2006 return "SMB_SIGNING_DEFAULT";
2007 case SMB_SIGNING_OFF:
2008 return "SMB_SIGNING_OFF";
2009 case SMB_SIGNING_IF_REQUIRED:
2010 return "SMB_SIGNING_IF_REQUIRED";
2011 case SMB_SIGNING_DESIRED:
2012 return "SMB_SIGNING_DESIRED";
2013 case SMB_SIGNING_REQUIRED:
2014 return "SMB_SIGNING_REQUIRED";
2021 static const char *encryption_state_to_str(enum smb_encryption_setting encryption_state)
2023 switch(encryption_state) {
2024 case SMB_ENCRYPTION_DEFAULT:
2025 return "SMB_ENCRYPTION_DEFAULT";
2026 case SMB_ENCRYPTION_OFF:
2027 return "SMB_ENCRYPTION_OFF";
2028 case SMB_ENCRYPTION_IF_REQUIRED:
2029 return "SMB_ENCRYPTION_IF_REQUIRED";
2030 case SMB_ENCRYPTION_DESIRED:
2031 return "SMB_ENCRYPTION_DESIRED";
2032 case SMB_ENCRYPTION_REQUIRED:
2033 return "SMB_ENCRYPTION_REQUIRED";
2040 _PUBLIC_ void cli_credentials_dump(struct cli_credentials *creds)
2042 DBG_ERR("CLI_CREDENTIALS:\n");
2044 DBG_ERR(" Username: %s - %s\n",
2046 obtained_to_str(creds->username_obtained));
2047 DBG_ERR(" Workstation: %s - %s\n",
2049 obtained_to_str(creds->workstation_obtained));
2050 DBG_ERR(" Domain: %s - %s\n",
2052 obtained_to_str(creds->domain_obtained));
2053 DBG_ERR(" Password: %s - %s\n",
2054 creds->password != NULL ? "*SECRET*" : "NULL",
2055 obtained_to_str(creds->password_obtained));
2056 DBG_ERR(" Old password: %s\n",
2057 creds->old_password != NULL ? "*SECRET*" : "NULL");
2058 DBG_ERR(" Password tries: %u\n",
2059 creds->password_tries);
2060 DBG_ERR(" Realm: %s - %s\n",
2062 obtained_to_str(creds->realm_obtained));
2063 DBG_ERR(" Principal: %s - %s\n",
2065 obtained_to_str(creds->principal_obtained));
2066 DBG_ERR(" Salt principal: %s\n",
2067 creds->salt_principal);
2068 DBG_ERR(" Impersonate principal: %s\n",
2069 creds->impersonate_principal);
2070 DBG_ERR(" Self service: %s\n",
2071 creds->self_service);
2072 DBG_ERR(" Target service: %s\n",
2073 creds->target_service);
2074 DBG_ERR(" Kerberos state: %s - %s\n",
2075 krb5_state_to_str(creds->kerberos_state),
2076 obtained_to_str(creds->kerberos_state_obtained));
2077 DBG_ERR(" Kerberos forwardable ticket: %s\n",
2078 krb5_fwd_to_str(creds->krb_forwardable));
2079 DBG_ERR(" Signing state: %s - %s\n",
2080 signing_state_to_str(creds->signing_state),
2081 obtained_to_str(creds->signing_state_obtained));
2082 DBG_ERR(" IPC signing state: %s - %s\n",
2083 signing_state_to_str(creds->ipc_signing_state),
2084 obtained_to_str(creds->ipc_signing_state_obtained));
2085 DBG_ERR(" Encryption state: %s - %s\n",
2086 encryption_state_to_str(creds->encryption_state),
2087 obtained_to_str(creds->encryption_state_obtained));
2088 DBG_ERR(" Gensec features: %#X\n",
2089 creds->gensec_features);
2090 DBG_ERR(" Forced sasl mech: %s\n",
2091 creds->forced_sasl_mech);
2092 DBG_ERR(" CCACHE: %p - %s\n",
2094 obtained_to_str(creds->ccache_obtained));
2095 DBG_ERR(" CLIENT_GSS_CREDS: %p - %s\n",
2096 creds->client_gss_creds,
2097 obtained_to_str(creds->client_gss_creds_obtained));
2098 DBG_ERR(" SERVER_GSS_CREDS: %p - %s\n",
2099 creds->server_gss_creds,
2100 obtained_to_str(creds->server_gss_creds_obtained));
2101 DBG_ERR(" KEYTAB: %p - %s\n",
2103 obtained_to_str(creds->keytab_obtained));
2104 DBG_ERR(" KVNO: %u\n",
2110 * @brief Obtain the SMB encryption state from a credentials structure.
2112 * @param[in] creds The credential structure to obtain the SMB encryption state
2115 * @return The SMB signing state.
2117 _PUBLIC_ enum smb_encryption_setting
2118 cli_credentials_get_smb_encryption(struct cli_credentials *creds)
2120 return creds->encryption_state;