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"
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);
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;
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;
71 server_creds = cli_credentials_init(mem_ctx);
72 if (server_creds == NULL) {
76 ok = cli_credentials_set_conf(server_creds, lp_ctx);
78 TALLOC_FREE(server_creds);
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",
86 TALLOC_FREE(server_creds);
93 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
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;
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_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
152 return creds->forced_sasl_mech;
155 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
157 return creds->krb_forwardable;
160 _PUBLIC_ bool cli_credentials_set_gensec_features(struct cli_credentials *creds,
161 uint32_t gensec_features,
162 enum credentials_obtained obtained)
164 if (obtained >= creds->gensec_features_obtained) {
165 creds->gensec_features_obtained = obtained;
166 creds->gensec_features = gensec_features;
174 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
176 return creds->gensec_features;
181 * Obtain the username for this credentials context.
182 * @param cred credentials context
183 * @retval The username set on this context.
184 * @note Return value will never be NULL except by programmer error.
186 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
188 if (cred->machine_account_pending) {
189 cli_credentials_set_machine_account(cred,
190 cred->machine_account_pending_lp_ctx);
193 if (cred->username_obtained == CRED_CALLBACK &&
194 !cred->callback_running) {
195 cred->callback_running = true;
196 cred->username = cred->username_cb(cred);
197 cred->callback_running = false;
198 if (cred->username_obtained == CRED_CALLBACK) {
199 cred->username_obtained = CRED_CALLBACK_RESULT;
200 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
204 return cred->username;
208 * @brief Obtain the username for this credentials context.
210 * @param[in] cred The credential context.
212 * @param[in] obtained A pointer to store the obtained information.
214 * return The user name or NULL if an error occurred.
216 _PUBLIC_ const char *
217 cli_credentials_get_username_and_obtained(struct cli_credentials *cred,
218 enum credentials_obtained *obtained)
220 if (obtained != NULL) {
221 *obtained = cred->username_obtained;
224 return cli_credentials_get_username(cred);
227 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
228 const char *val, enum credentials_obtained obtained)
230 if (obtained >= cred->username_obtained) {
231 cred->username = talloc_strdup(cred, val);
232 cred->username_obtained = obtained;
233 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
240 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
241 const char *(*username_cb) (struct cli_credentials *))
243 if (cred->username_obtained < CRED_CALLBACK) {
244 cred->username_cb = username_cb;
245 cred->username_obtained = CRED_CALLBACK;
252 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
255 cred->bind_dn = talloc_strdup(cred, bind_dn);
260 * Obtain the BIND DN for this credentials context.
261 * @param cred credentials context
262 * @retval The username set on this context.
263 * @note Return value will be NULL if not specified explicitly
265 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
267 return cred->bind_dn;
272 * Obtain the client principal for this credentials context.
273 * @param cred credentials context
274 * @retval The username set on this context.
275 * @note Return value will never be NULL except by programmer error.
277 _PUBLIC_ char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
279 if (cred->machine_account_pending) {
280 cli_credentials_set_machine_account(cred,
281 cred->machine_account_pending_lp_ctx);
284 if (cred->principal_obtained == CRED_CALLBACK &&
285 !cred->callback_running) {
286 cred->callback_running = true;
287 cred->principal = cred->principal_cb(cred);
288 cred->callback_running = false;
289 if (cred->principal_obtained == CRED_CALLBACK) {
290 cred->principal_obtained = CRED_CALLBACK_RESULT;
291 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
295 if (cred->principal_obtained < cred->username_obtained
296 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
297 const char *effective_username = NULL;
298 const char *effective_realm = NULL;
299 enum credentials_obtained effective_obtained;
301 effective_username = cli_credentials_get_username(cred);
302 if (effective_username == NULL || strlen(effective_username) == 0) {
303 *obtained = cred->username_obtained;
307 if (cred->domain_obtained > cred->realm_obtained) {
308 effective_realm = cli_credentials_get_domain(cred);
309 effective_obtained = MIN(cred->domain_obtained,
310 cred->username_obtained);
312 effective_realm = cli_credentials_get_realm(cred);
313 effective_obtained = MIN(cred->realm_obtained,
314 cred->username_obtained);
317 if (effective_realm == NULL || strlen(effective_realm) == 0) {
318 effective_realm = cli_credentials_get_domain(cred);
319 effective_obtained = MIN(cred->domain_obtained,
320 cred->username_obtained);
323 if (effective_realm != NULL && strlen(effective_realm) != 0) {
324 *obtained = effective_obtained;
325 return talloc_asprintf(mem_ctx, "%s@%s",
330 *obtained = cred->principal_obtained;
331 return talloc_strdup(mem_ctx, cred->principal);
335 * Obtain the client principal for this credentials context.
336 * @param cred credentials context
337 * @retval The username set on this context.
338 * @note Return value will never be NULL except by programmer error.
340 _PUBLIC_ char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
342 enum credentials_obtained obtained;
343 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
346 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
348 enum credentials_obtained obtained)
350 if (obtained >= cred->principal_obtained) {
351 cred->principal = talloc_strdup(cred, val);
352 if (cred->principal == NULL) {
355 cred->principal_obtained = obtained;
357 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
364 /* Set a callback to get the principal. This could be a popup dialog,
365 * a terminal prompt or similar. */
366 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
367 const char *(*principal_cb) (struct cli_credentials *))
369 if (cred->principal_obtained < CRED_CALLBACK) {
370 cred->principal_cb = principal_cb;
371 cred->principal_obtained = CRED_CALLBACK;
378 /* Some of our tools are 'anonymous by default'. This is a single
379 * function to determine if authentication has been explicitly
382 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
384 uint32_t gensec_features = 0;
391 * If we forced the mech we clearly want authentication. E.g. to use
392 * SASL/EXTERNAL which has no credentials.
394 if (cred->forced_sasl_mech) {
398 if (cli_credentials_is_anonymous(cred)){
402 if (cred->principal_obtained >= CRED_SPECIFIED) {
405 if (cred->username_obtained >= CRED_SPECIFIED) {
409 if (cli_credentials_get_kerberos_state(cred) == CRED_USE_KERBEROS_REQUIRED) {
413 gensec_features = cli_credentials_get_gensec_features(cred);
414 if (gensec_features & GENSEC_FEATURE_NTLM_CCACHE) {
418 if (gensec_features & GENSEC_FEATURE_SIGN) {
422 if (gensec_features & GENSEC_FEATURE_SEAL) {
430 * Obtain the password for this credentials context.
431 * @param cred credentials context
432 * @retval If set, the cleartext password, otherwise NULL
434 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
436 if (cred->machine_account_pending) {
437 cli_credentials_set_machine_account(cred,
438 cred->machine_account_pending_lp_ctx);
441 if (cred->password_obtained == CRED_CALLBACK &&
442 !cred->callback_running &&
443 !cred->password_will_be_nt_hash) {
444 cred->callback_running = true;
445 cred->password = cred->password_cb(cred);
446 cred->callback_running = false;
447 if (cred->password_obtained == CRED_CALLBACK) {
448 cred->password_obtained = CRED_CALLBACK_RESULT;
449 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
453 return cred->password;
457 * @brief Obtain the password for this credentials context.
459 * @param[in] cred The credential context.
461 * @param[in] obtained A pointer to store the obtained information.
463 * return The user name or NULL if an error occurred.
465 _PUBLIC_ const char *
466 cli_credentials_get_password_and_obtained(struct cli_credentials *cred,
467 enum credentials_obtained *obtained)
469 const char *password = cli_credentials_get_password(cred);
471 if (obtained != NULL) {
472 *obtained = cred->password_obtained;
478 /* Set a password on the credentials context, including an indication
479 * of 'how' the password was obtained */
481 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
483 enum credentials_obtained obtained)
485 if (obtained >= cred->password_obtained) {
487 cred->lm_response = data_blob_null;
488 cred->nt_response = data_blob_null;
489 cred->nt_hash = NULL;
490 cred->password = NULL;
492 cli_credentials_invalidate_ccache(cred, obtained);
494 cred->password_tries = 0;
497 cred->password_obtained = obtained;
501 if (cred->password_will_be_nt_hash) {
502 struct samr_Password *nt_hash = NULL;
503 size_t val_len = strlen(val);
506 nt_hash = talloc(cred, struct samr_Password);
507 if (nt_hash == NULL) {
511 converted = strhex_to_str((char *)nt_hash->hash,
512 sizeof(nt_hash->hash),
514 if (converted != sizeof(nt_hash->hash)) {
515 TALLOC_FREE(nt_hash);
519 cred->nt_hash = nt_hash;
520 cred->password_obtained = obtained;
524 cred->password = talloc_strdup(cred, val);
525 if (cred->password == NULL) {
529 /* Don't print the actual password in talloc memory dumps */
530 talloc_set_name_const(cred->password,
531 "password set via cli_credentials_set_password");
532 cred->password_obtained = obtained;
540 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
541 const char *(*password_cb) (struct cli_credentials *))
543 if (cred->password_obtained < CRED_CALLBACK) {
544 cred->password_tries = 3;
545 cred->password_cb = password_cb;
546 cred->password_obtained = CRED_CALLBACK;
547 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
555 * Obtain the 'old' password for this credentials context (used for join accounts).
556 * @param cred credentials context
557 * @retval If set, the cleartext password, otherwise NULL
559 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
561 if (cred->machine_account_pending) {
562 cli_credentials_set_machine_account(cred,
563 cred->machine_account_pending_lp_ctx);
566 return cred->old_password;
569 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
571 enum credentials_obtained obtained)
573 cred->old_password = talloc_strdup(cred, val);
574 if (cred->old_password) {
575 /* Don't print the actual password in talloc memory dumps */
576 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
578 cred->old_nt_hash = NULL;
583 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
585 * Sometimes we only have this much of the password, while the rest of
586 * the time this call avoids calling E_md4hash themselves.
588 * @param cred credentials context
589 * @retval If set, the cleartext password, otherwise NULL
591 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
594 enum credentials_obtained password_obtained;
595 enum credentials_obtained ccache_threshold;
596 enum credentials_obtained client_gss_creds_threshold;
597 bool password_is_nt_hash;
598 const char *password = NULL;
599 struct samr_Password *nt_hash = NULL;
601 if (cred->nt_hash != NULL) {
603 * If we already have a hash it's easy.
609 * This is a bit tricky, with password_will_be_nt_hash
610 * we still need to get the value via the password_callback
611 * but if we did that we should not remember it's state
612 * in the long run so we need to undo it.
615 password_obtained = cred->password_obtained;
616 ccache_threshold = cred->ccache_threshold;
617 client_gss_creds_threshold = cred->client_gss_creds_threshold;
618 password_is_nt_hash = cred->password_will_be_nt_hash;
620 cred->password_will_be_nt_hash = false;
621 password = cli_credentials_get_password(cred);
623 cred->password_will_be_nt_hash = password_is_nt_hash;
624 if (password_is_nt_hash && password_obtained == CRED_CALLBACK) {
626 * We got the nt_hash as string via the callback,
627 * so we need to undo the state change.
629 * And also don't remember it as plaintext password.
631 cred->client_gss_creds_threshold = client_gss_creds_threshold;
632 cred->ccache_threshold = ccache_threshold;
633 cred->password_obtained = password_obtained;
634 cred->password = NULL;
637 if (password == NULL) {
641 nt_hash = talloc(cred, struct samr_Password);
642 if (nt_hash == NULL) {
646 if (password_is_nt_hash) {
647 size_t password_len = strlen(password);
650 converted = strhex_to_str((char *)nt_hash->hash,
651 sizeof(nt_hash->hash),
652 password, password_len);
653 if (converted != sizeof(nt_hash->hash)) {
654 TALLOC_FREE(nt_hash);
658 E_md4hash(password, nt_hash->hash);
661 cred->nt_hash = nt_hash;
665 nt_hash = talloc(mem_ctx, struct samr_Password);
666 if (nt_hash == NULL) {
670 *nt_hash = *cred->nt_hash;
676 * Obtain the old password, in the form MD4(unicode(password)) for this credentials context.
678 * Sometimes we only have this much of the password, while the rest of
679 * the time this call avoids calling E_md4hash themselves.
681 * @param cred credentials context
682 * @retval If set, the cleartext password, otherwise NULL
684 _PUBLIC_ struct samr_Password *cli_credentials_get_old_nt_hash(struct cli_credentials *cred,
687 const char *old_password = NULL;
689 if (cred->old_nt_hash != NULL) {
690 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
695 *nt_hash = *cred->old_nt_hash;
700 old_password = cli_credentials_get_old_password(cred);
702 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
707 E_md4hash(old_password, nt_hash->hash);
716 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
717 * @param cred credentials context
718 * @retval The domain set on this context.
719 * @note Return value will never be NULL except by programmer error.
721 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
723 if (cred->machine_account_pending) {
724 cli_credentials_set_machine_account(cred,
725 cred->machine_account_pending_lp_ctx);
728 if (cred->domain_obtained == CRED_CALLBACK &&
729 !cred->callback_running) {
730 cred->callback_running = true;
731 cred->domain = cred->domain_cb(cred);
732 cred->callback_running = false;
733 if (cred->domain_obtained == CRED_CALLBACK) {
734 cred->domain_obtained = CRED_CALLBACK_RESULT;
735 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
743 * @brief Obtain the domain for this credential context.
745 * @param[in] cred The credential context.
747 * @param[out] obtained A pointer to store the obtained information.
749 * @return The domain name or NULL if an error occurred.
751 _PUBLIC_ const char *cli_credentials_get_domain_and_obtained(
752 struct cli_credentials *cred,
753 enum credentials_obtained *obtained)
755 const char *domain = cli_credentials_get_domain(cred);
757 if (obtained != NULL) {
758 *obtained = cred->domain_obtained;
765 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
767 enum credentials_obtained obtained)
769 if (obtained >= cred->domain_obtained) {
770 /* it is important that the domain be in upper case,
771 * particularly for the sensitive NTLMv2
773 cred->domain = strupper_talloc(cred, val);
774 cred->domain_obtained = obtained;
775 /* setting domain does not mean we have to invalidate ccache
776 * because domain in not used for Kerberos operations.
777 * If ccache invalidation is required, one will anyway specify
778 * a password to kinit, and that will force invalidation of the ccache
786 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
787 const char *(*domain_cb) (struct cli_credentials *))
789 if (cred->domain_obtained < CRED_CALLBACK) {
790 cred->domain_cb = domain_cb;
791 cred->domain_obtained = CRED_CALLBACK;
799 * Obtain the Kerberos realm for this credentials context.
800 * @param cred credentials context
801 * @retval The realm set on this context.
802 * @note Return value will never be NULL except by programmer error.
804 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
806 if (cred->machine_account_pending) {
807 cli_credentials_set_machine_account(cred,
808 cred->machine_account_pending_lp_ctx);
811 if (cred->realm_obtained == CRED_CALLBACK &&
812 !cred->callback_running) {
813 cred->callback_running = true;
814 cred->realm = cred->realm_cb(cred);
815 cred->callback_running = false;
816 if (cred->realm_obtained == CRED_CALLBACK) {
817 cred->realm_obtained = CRED_CALLBACK_RESULT;
818 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
826 * Set the realm for this credentials context, and force it to
827 * uppercase for the sanity of our local kerberos libraries
829 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
831 enum credentials_obtained obtained)
833 if (obtained >= cred->realm_obtained) {
834 cred->realm = strupper_talloc(cred, val);
835 cred->realm_obtained = obtained;
836 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
843 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
844 const char *(*realm_cb) (struct cli_credentials *))
846 if (cred->realm_obtained < CRED_CALLBACK) {
847 cred->realm_cb = realm_cb;
848 cred->realm_obtained = CRED_CALLBACK;
856 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
858 * @param cred credentials context
859 * @retval The workstation name set on this context.
860 * @note Return value will never be NULL except by programmer error.
862 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
864 if (cred->workstation_obtained == CRED_CALLBACK &&
865 !cred->callback_running) {
866 cred->callback_running = true;
867 cred->workstation = cred->workstation_cb(cred);
868 cred->callback_running = false;
869 if (cred->workstation_obtained == CRED_CALLBACK) {
870 cred->workstation_obtained = CRED_CALLBACK_RESULT;
874 return cred->workstation;
877 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
879 enum credentials_obtained obtained)
881 if (obtained >= cred->workstation_obtained) {
882 cred->workstation = talloc_strdup(cred, val);
883 cred->workstation_obtained = obtained;
890 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
891 const char *(*workstation_cb) (struct cli_credentials *))
893 if (cred->workstation_obtained < CRED_CALLBACK) {
894 cred->workstation_cb = workstation_cb;
895 cred->workstation_obtained = CRED_CALLBACK;
903 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
905 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
907 * @param credentials Credentials structure on which to set the password
908 * @param data the string containing the username, password etc
909 * @param obtained This enum describes how 'specified' this password is
912 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
915 char *uname_free = NULL;
917 if (strcmp("%",data) == 0) {
918 cli_credentials_set_anonymous(credentials);
922 uname = talloc_strdup(credentials, data);
925 if ((p = strchr_m(uname,'%'))) {
927 cli_credentials_set_password(credentials, p+1, obtained);
930 if ((p = strchr_m(uname,'@'))) {
932 * We also need to set username and domain
933 * in order to undo the effect of
934 * cli_credentials_guess().
936 cli_credentials_set_username(credentials, uname, obtained);
937 cli_credentials_set_domain(credentials, "", obtained);
939 cli_credentials_set_principal(credentials, uname, obtained);
941 cli_credentials_set_realm(credentials, p+1, obtained);
942 TALLOC_FREE(uname_free);
944 } else if ((p = strchr_m(uname,'\\'))
945 || (p = strchr_m(uname, '/'))
946 || (p = strchr_m(uname, credentials->winbind_separator)))
948 const char *domain = NULL;
954 if (obtained == credentials->realm_obtained &&
955 !strequal_m(credentials->domain, domain))
958 * We need to undo a former set with the same level
959 * in order to get the expected result from
960 * cli_credentials_get_principal().
962 * But we only need to do that if the domain
965 cli_credentials_set_realm(credentials, domain, obtained);
967 cli_credentials_set_domain(credentials, domain, obtained);
969 if (obtained == credentials->principal_obtained &&
970 !strequal_m(credentials->username, uname))
973 * We need to undo a former set with the same level
974 * in order to get the expected result from
975 * cli_credentials_get_principal().
977 * But we only need to do that if the username
980 credentials->principal_obtained = CRED_UNINITIALISED;
981 credentials->principal = NULL;
983 cli_credentials_set_username(credentials, uname, obtained);
985 TALLOC_FREE(uname_free);
989 * Given a a credentials structure, print it as a string
991 * The format output is [domain\\]user[%password] or user[@realm][%password]
993 * @param credentials Credentials structure on which to set the password
994 * @param mem_ctx The memory context to place the result on
997 _PUBLIC_ char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
999 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
1000 const char *domain = NULL;
1001 const char *username = NULL;
1005 name = talloc_strdup(mem_ctx, bind_dn);
1007 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
1008 if (domain && domain[0]) {
1009 name = talloc_asprintf(mem_ctx, "%s\\%s",
1012 name = talloc_asprintf(mem_ctx, "%s",
1021 * Specifies default values for domain, workstation and realm
1022 * from the smb.conf configuration file
1024 * @param cred Credentials structure to fill in
1026 * @return true on success, false on error.
1028 _PUBLIC_ bool cli_credentials_set_conf(struct cli_credentials *cred,
1029 struct loadparm_context *lp_ctx)
1031 const char *sep = NULL;
1032 const char *realm = lpcfg_realm(lp_ctx);
1033 enum credentials_client_protection protection =
1034 lpcfg_client_protection(lp_ctx);
1035 const char *workgroup = lpcfg_workgroup(lp_ctx);
1036 const char *netbios_name = lpcfg_netbios_name(lp_ctx);
1039 (void)cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
1041 if (workgroup != NULL && strlen(workgroup) == 0) {
1045 if (workgroup != NULL) {
1046 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
1047 ok = cli_credentials_set_domain(cred,
1051 DBG_ERR("Failed to set domain!\n");
1055 (void)cli_credentials_set_domain(cred,
1061 if (netbios_name != NULL && strlen(netbios_name) == 0) {
1062 netbios_name = NULL;
1065 if (netbios_name != NULL) {
1066 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
1067 ok = cli_credentials_set_workstation(cred,
1071 DBG_ERR("Failed to set workstation!\n");
1075 (void)cli_credentials_set_workstation(cred,
1081 if (realm != NULL && strlen(realm) == 0) {
1085 if (realm != NULL) {
1086 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
1087 ok = cli_credentials_set_realm(cred,
1091 DBG_ERR("Failed to set realm!\n");
1095 (void)cli_credentials_set_realm(cred,
1101 sep = lpcfg_winbind_separator(lp_ctx);
1102 if (sep != NULL && sep[0] != '\0') {
1103 cred->winbind_separator = *lpcfg_winbind_separator(lp_ctx);
1106 if (cred->signing_state_obtained <= CRED_SMB_CONF) {
1107 /* Will be set to default for invalid smb.conf values */
1108 cred->signing_state = lpcfg_client_signing(lp_ctx);
1109 if (cred->signing_state == SMB_SIGNING_DEFAULT) {
1110 switch (protection) {
1111 case CRED_CLIENT_PROTECTION_DEFAULT:
1113 case CRED_CLIENT_PROTECTION_PLAIN:
1114 cred->signing_state = SMB_SIGNING_OFF;
1116 case CRED_CLIENT_PROTECTION_SIGN:
1117 case CRED_CLIENT_PROTECTION_ENCRYPT:
1118 cred->signing_state = SMB_SIGNING_REQUIRED;
1123 cred->signing_state_obtained = CRED_SMB_CONF;
1126 if (cred->ipc_signing_state_obtained <= CRED_SMB_CONF) {
1127 /* Will be set to required for invalid smb.conf values */
1128 cred->ipc_signing_state = lpcfg_client_ipc_signing(lp_ctx);
1129 cred->ipc_signing_state_obtained = CRED_SMB_CONF;
1132 if (cred->encryption_state_obtained <= CRED_SMB_CONF) {
1133 /* Will be set to default for invalid smb.conf values */
1134 cred->encryption_state = lpcfg_client_smb_encrypt(lp_ctx);
1135 if (cred->encryption_state == SMB_ENCRYPTION_DEFAULT) {
1136 switch (protection) {
1137 case CRED_CLIENT_PROTECTION_DEFAULT:
1139 case CRED_CLIENT_PROTECTION_PLAIN:
1140 case CRED_CLIENT_PROTECTION_SIGN:
1141 cred->encryption_state = SMB_ENCRYPTION_OFF;
1143 case CRED_CLIENT_PROTECTION_ENCRYPT:
1144 cred->encryption_state = SMB_ENCRYPTION_REQUIRED;
1150 if (cred->kerberos_state_obtained <= CRED_SMB_CONF) {
1151 /* Will be set to default for invalid smb.conf values */
1152 cred->kerberos_state = lpcfg_client_use_kerberos(lp_ctx);
1153 cred->kerberos_state_obtained = CRED_SMB_CONF;
1156 if (cred->gensec_features_obtained <= CRED_SMB_CONF) {
1157 switch (protection) {
1158 case CRED_CLIENT_PROTECTION_DEFAULT:
1160 case CRED_CLIENT_PROTECTION_PLAIN:
1161 cred->gensec_features = 0;
1163 case CRED_CLIENT_PROTECTION_SIGN:
1164 cred->gensec_features = GENSEC_FEATURE_SIGN;
1166 case CRED_CLIENT_PROTECTION_ENCRYPT:
1167 cred->gensec_features =
1168 GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL;
1171 cred->gensec_features_obtained = CRED_SMB_CONF;
1178 * Guess defaults for credentials from environment variables,
1179 * and from the configuration file
1181 * @param cred Credentials structure to fill in
1183 _PUBLIC_ bool cli_credentials_guess(struct cli_credentials *cred,
1184 struct loadparm_context *lp_ctx)
1186 const char *error_string;
1187 const char *env = NULL;
1188 struct passwd *pwd = NULL;
1191 if (lp_ctx != NULL) {
1192 ok = cli_credentials_set_conf(cred, lp_ctx);
1198 pwd = getpwuid(getuid());
1200 size_t len = strlen(pwd->pw_name);
1202 if (len > 0 && len <= 1024) {
1203 (void)cli_credentials_parse_string(cred,
1209 env = getenv("LOGNAME");
1211 size_t len = strlen(env);
1213 if (len > 0 && len <= 1024) {
1214 (void)cli_credentials_set_username(cred,
1220 env = getenv("USER");
1222 size_t len = strlen(env);
1224 if (len > 0 && len <= 1024) {
1227 (void)cli_credentials_parse_string(cred,
1230 if ((p = strchr_m(env, '%'))) {
1231 memset(p, '\0', strlen(cred->password));
1236 env = getenv("PASSWD");
1238 size_t len = strlen(env);
1240 if (len > 0 && len <= 1024) {
1241 (void)cli_credentials_set_password(cred,
1247 env = getenv("PASSWD_FD");
1249 size_t len = strlen(env);
1251 if (len > 0 && len <= 1024) {
1254 (void)cli_credentials_parse_password_fd(cred,
1260 env = getenv("PASSWD_FILE");
1262 size_t len = strlen(env);
1264 if (len > 0 && len <= 4096) {
1265 (void)cli_credentials_parse_password_file(cred,
1271 if (lp_ctx != NULL &&
1272 cli_credentials_get_kerberos_state(cred) != CRED_USE_KERBEROS_DISABLED) {
1273 (void)cli_credentials_set_ccache(cred,
1284 * Attach NETLOGON credentials for use with SCHANNEL
1287 _PUBLIC_ void cli_credentials_set_netlogon_creds(
1288 struct cli_credentials *cred,
1289 const struct netlogon_creds_CredentialState *netlogon_creds)
1291 TALLOC_FREE(cred->netlogon_creds);
1292 if (netlogon_creds == NULL) {
1295 cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
1299 * Return attached NETLOGON credentials
1302 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
1304 return cred->netlogon_creds;
1308 * Set NETLOGON secure channel type
1311 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
1312 enum netr_SchannelType secure_channel_type)
1314 cred->secure_channel_type = secure_channel_type;
1318 * Return NETLOGON secure channel type
1321 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
1323 return cred->password_last_changed_time;
1327 * Set NETLOGON secure channel type
1330 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
1331 time_t last_changed_time)
1333 cred->password_last_changed_time = last_changed_time;
1337 * Return NETLOGON secure channel type
1340 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
1342 return cred->secure_channel_type;
1346 * Fill in a credentials structure as the anonymous user
1348 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
1350 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
1351 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
1352 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
1353 cli_credentials_set_principal(cred, NULL, CRED_SPECIFIED);
1354 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
1355 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
1356 cli_credentials_set_kerberos_state(cred,
1357 CRED_USE_KERBEROS_DISABLED,
1362 * Describe a credentials context as anonymous or authenticated
1363 * @retval true if anonymous, false if a username is specified
1366 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
1368 const char *username;
1370 /* if bind dn is set it's not anonymous */
1371 if (cred->bind_dn) {
1375 if (cred->machine_account_pending) {
1376 cli_credentials_set_machine_account(cred,
1377 cred->machine_account_pending_lp_ctx);
1380 /* if principal is set, it's not anonymous */
1381 if ((cred->principal != NULL) && cred->principal_obtained >= cred->username_obtained) {
1385 username = cli_credentials_get_username(cred);
1387 /* Yes, it is deliberate that we die if we have a NULL pointer
1388 * here - anonymous is "", not NULL, which is 'never specified,
1389 * never guessed', ie programmer bug */
1398 * Mark the current password for a credentials struct as wrong. This will
1399 * cause the password to be prompted again (if a callback is set).
1401 * This will decrement the number of times the password can be tried.
1403 * @retval whether the credentials struct is finished
1405 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
1407 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
1411 if (cred->password_tries == 0) {
1415 cred->password_tries--;
1417 if (cred->password_tries == 0) {
1421 cred->password_obtained = CRED_CALLBACK;
1425 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
1426 const char **username,
1427 const char **domain)
1429 if (cred->principal_obtained >= cred->username_obtained) {
1430 *domain = talloc_strdup(mem_ctx, "");
1431 *username = cli_credentials_get_principal(cred, mem_ctx);
1433 *domain = cli_credentials_get_domain(cred);
1434 *username = cli_credentials_get_username(cred);
1439 * Read a named file, and parse it for username, domain, realm and password
1441 * @param credentials Credentials structure on which to set the password
1442 * @param file a named file to read the details from
1443 * @param obtained This enum describes how 'specified' this password is
1446 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
1449 char *ptr, *val, *param;
1452 const char *realm = NULL;
1453 const char *domain = NULL;
1454 const char *password = NULL;
1455 const char *username = NULL;
1457 lines = file_lines_load(file, &numlines, 0, NULL);
1461 /* fail if we can't open the credentials file */
1462 d_printf("ERROR: Unable to open credentials file!\n");
1466 for (i = 0; i < numlines; i++) {
1467 len = strlen(lines[i]);
1472 /* break up the line into parameter & value.
1473 * will need to eat a little whitespace possibly */
1475 if (!(ptr = strchr_m (lines[i], '=')))
1481 /* eat leading white space */
1482 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1485 if (strwicmp("password", param) == 0) {
1487 } else if (strwicmp("username", param) == 0) {
1489 } else if (strwicmp("domain", param) == 0) {
1491 } else if (strwicmp("realm", param) == 0) {
1496 * We need to readd '=' in order to let
1497 * the strlen() work in the last loop
1498 * that clears the memory.
1503 if (realm != NULL && strlen(realm) != 0) {
1505 * only overwrite with a valid string
1507 cli_credentials_set_realm(cred, realm, obtained);
1510 if (domain != NULL && strlen(domain) != 0) {
1512 * only overwrite with a valid string
1514 cli_credentials_set_domain(cred, domain, obtained);
1517 if (password != NULL) {
1521 cli_credentials_set_password(cred, password, obtained);
1524 if (username != NULL) {
1526 * The last "username" line takes preference
1527 * if the string also contains domain, realm or
1530 cli_credentials_parse_string(cred, username, obtained);
1533 for (i = 0; i < numlines; i++) {
1534 len = strlen(lines[i]);
1535 memset(lines[i], 0, len);
1543 * Read a named file, and parse it for a password
1545 * @param credentials Credentials structure on which to set the password
1546 * @param file a named file to read the password from
1547 * @param obtained This enum describes how 'specified' this password is
1550 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1552 int fd = open(file, O_RDONLY, 0);
1556 fprintf(stderr, "Error opening password file %s: %s\n",
1557 file, strerror(errno));
1561 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1570 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1572 * @param credentials Credentials structure on which to set the password
1573 * @param fd open file descriptor to read the password from
1574 * @param obtained This enum describes how 'specified' this password is
1577 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1578 int fd, enum credentials_obtained obtained)
1583 if (credentials->password_obtained >= obtained) {
1587 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1588 p && p - pass < sizeof(pass) - 1;) {
1589 switch (read(fd, p, 1)) {
1591 if (*p != '\n' && *p != '\0') {
1592 *++p = '\0'; /* advance p, and null-terminate pass */
1599 *p = '\0'; /* null-terminate it, just in case... */
1600 p = NULL; /* then force the loop condition to become false */
1605 "Error reading password from file descriptor "
1606 "%d: empty password\n",
1611 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1612 fd, strerror(errno));
1617 cli_credentials_set_password(credentials, pass, obtained);
1622 * @brief Set the SMB signing state to request for a SMB connection.
1624 * @param[in] creds The credentials structure to update.
1626 * @param[in] signing_state The signing state to set.
1628 * @param obtained This way the described signing state was specified.
1630 * @return true if we could set the signing state, false otherwise.
1632 _PUBLIC_ bool cli_credentials_set_smb_signing(struct cli_credentials *creds,
1633 enum smb_signing_setting signing_state,
1634 enum credentials_obtained obtained)
1636 if (obtained >= creds->signing_state_obtained) {
1637 creds->signing_state_obtained = obtained;
1638 creds->signing_state = signing_state;
1646 * @brief Obtain the SMB signing state from a credentials structure.
1648 * @param[in] creds The credential structure to obtain the SMB signing state
1651 * @return The SMB signing state.
1653 _PUBLIC_ enum smb_signing_setting
1654 cli_credentials_get_smb_signing(struct cli_credentials *creds)
1656 return creds->signing_state;
1660 * @brief Set the SMB IPC signing state to request for a SMB connection.
1662 * @param[in] creds The credentials structure to update.
1664 * @param[in] signing_state The signing state to set.
1666 * @param obtained This way the described signing state was specified.
1668 * @return true if we could set the signing state, false otherwise.
1671 cli_credentials_set_smb_ipc_signing(struct cli_credentials *creds,
1672 enum smb_signing_setting ipc_signing_state,
1673 enum credentials_obtained obtained)
1675 if (obtained >= creds->ipc_signing_state_obtained) {
1676 creds->ipc_signing_state_obtained = obtained;
1677 creds->ipc_signing_state = ipc_signing_state;
1685 * @brief Obtain the SMB IPC signing state from a credentials structure.
1687 * @param[in] creds The credential structure to obtain the SMB IPC signing
1690 * @return The SMB signing state.
1692 _PUBLIC_ enum smb_signing_setting
1693 cli_credentials_get_smb_ipc_signing(struct cli_credentials *creds)
1695 return creds->ipc_signing_state;
1699 * @brief Set the SMB encryption state to request for a SMB connection.
1701 * @param[in] creds The credentials structure to update.
1703 * @param[in] encryption_state The encryption state to set.
1705 * @param obtained This way the described encryption state was specified.
1707 * @return true if we could set the encryption state, false otherwise.
1709 _PUBLIC_ bool cli_credentials_set_smb_encryption(struct cli_credentials *creds,
1710 enum smb_encryption_setting encryption_state,
1711 enum credentials_obtained obtained)
1713 if (obtained >= creds->encryption_state_obtained) {
1714 creds->encryption_state_obtained = obtained;
1715 creds->encryption_state = encryption_state;
1722 static const char *obtained_to_str(enum credentials_obtained obtained)
1725 case CRED_UNINITIALISED:
1726 return "CRED_UNINITIALISED";
1728 return "CRED_SMB_CONF";
1730 return "CRED_CALLBACK";
1731 case CRED_GUESS_ENV:
1732 return "CRED_GUESS_ENV";
1733 case CRED_GUESS_FILE:
1734 return "CRED_GUESS_FILE";
1735 case CRED_CALLBACK_RESULT:
1736 return "CRED_CALLBACK_RESULT";
1737 case CRED_SPECIFIED:
1738 return "CRED_SPECIFIED";
1745 static const char *krb5_state_to_str(enum credentials_use_kerberos krb5_state)
1747 switch (krb5_state) {
1748 case CRED_USE_KERBEROS_DISABLED:
1749 return "CRED_USE_KERBEROS_DISABLED";
1750 case CRED_USE_KERBEROS_DESIRED:
1751 return "CRED_USE_KERBEROS_DESIRED";
1752 case CRED_USE_KERBEROS_REQUIRED:
1753 return "CRED_USE_KERBEROS_REQUIRED";
1760 static const char *krb5_fwd_to_str(enum credentials_krb_forwardable krb5_fwd)
1763 case CRED_AUTO_KRB_FORWARDABLE:
1764 return "CRED_AUTO_KRB_FORWARDABLE";
1765 case CRED_NO_KRB_FORWARDABLE:
1766 return "CRED_NO_KRB_FORWARDABLE";
1767 case CRED_FORCE_KRB_FORWARDABLE:
1768 return "CRED_FORCE_KRB_FORWARDABLE";
1775 static const char *signing_state_to_str(enum smb_signing_setting signing_state)
1777 switch(signing_state) {
1778 case SMB_SIGNING_IPC_DEFAULT:
1779 return "SMB_SIGNING_IPC_DEFAULT";
1780 case SMB_SIGNING_DEFAULT:
1781 return "SMB_SIGNING_DEFAULT";
1782 case SMB_SIGNING_OFF:
1783 return "SMB_SIGNING_OFF";
1784 case SMB_SIGNING_IF_REQUIRED:
1785 return "SMB_SIGNING_IF_REQUIRED";
1786 case SMB_SIGNING_DESIRED:
1787 return "SMB_SIGNING_DESIRED";
1788 case SMB_SIGNING_REQUIRED:
1789 return "SMB_SIGNING_REQUIRED";
1796 static const char *encryption_state_to_str(enum smb_encryption_setting encryption_state)
1798 switch(encryption_state) {
1799 case SMB_ENCRYPTION_DEFAULT:
1800 return "SMB_ENCRYPTION_DEFAULT";
1801 case SMB_ENCRYPTION_OFF:
1802 return "SMB_ENCRYPTION_OFF";
1803 case SMB_ENCRYPTION_IF_REQUIRED:
1804 return "SMB_ENCRYPTION_IF_REQUIRED";
1805 case SMB_ENCRYPTION_DESIRED:
1806 return "SMB_ENCRYPTION_DESIRED";
1807 case SMB_ENCRYPTION_REQUIRED:
1808 return "SMB_ENCRYPTION_REQUIRED";
1815 _PUBLIC_ void cli_credentials_dump(struct cli_credentials *creds)
1817 DBG_ERR("CLI_CREDENTIALS:\n");
1819 DBG_ERR(" Username: %s - %s\n",
1821 obtained_to_str(creds->username_obtained));
1822 DBG_ERR(" Workstation: %s - %s\n",
1824 obtained_to_str(creds->workstation_obtained));
1825 DBG_ERR(" Domain: %s - %s\n",
1827 obtained_to_str(creds->domain_obtained));
1828 DBG_ERR(" Password: %s - %s\n",
1829 creds->password != NULL ? "*SECRET*" : "NULL",
1830 obtained_to_str(creds->password_obtained));
1831 DBG_ERR(" Old password: %s\n",
1832 creds->old_password != NULL ? "*SECRET*" : "NULL");
1833 DBG_ERR(" Password tries: %u\n",
1834 creds->password_tries);
1835 DBG_ERR(" Realm: %s - %s\n",
1837 obtained_to_str(creds->realm_obtained));
1838 DBG_ERR(" Principal: %s - %s\n",
1840 obtained_to_str(creds->principal_obtained));
1841 DBG_ERR(" Salt principal: %s\n",
1842 creds->salt_principal);
1843 DBG_ERR(" Impersonate principal: %s\n",
1844 creds->impersonate_principal);
1845 DBG_ERR(" Self service: %s\n",
1846 creds->self_service);
1847 DBG_ERR(" Target service: %s\n",
1848 creds->target_service);
1849 DBG_ERR(" Kerberos state: %s - %s\n",
1850 krb5_state_to_str(creds->kerberos_state),
1851 obtained_to_str(creds->kerberos_state_obtained));
1852 DBG_ERR(" Kerberos forwardable ticket: %s\n",
1853 krb5_fwd_to_str(creds->krb_forwardable));
1854 DBG_ERR(" Signing state: %s - %s\n",
1855 signing_state_to_str(creds->signing_state),
1856 obtained_to_str(creds->signing_state_obtained));
1857 DBG_ERR(" IPC signing state: %s - %s\n",
1858 signing_state_to_str(creds->ipc_signing_state),
1859 obtained_to_str(creds->ipc_signing_state_obtained));
1860 DBG_ERR(" Encryption state: %s - %s\n",
1861 encryption_state_to_str(creds->encryption_state),
1862 obtained_to_str(creds->encryption_state_obtained));
1863 DBG_ERR(" Gensec features: %#X\n",
1864 creds->gensec_features);
1865 DBG_ERR(" Forced sasl mech: %s\n",
1866 creds->forced_sasl_mech);
1867 DBG_ERR(" CCACHE: %p - %s\n",
1869 obtained_to_str(creds->ccache_obtained));
1870 DBG_ERR(" CLIENT_GSS_CREDS: %p - %s\n",
1871 creds->client_gss_creds,
1872 obtained_to_str(creds->client_gss_creds_obtained));
1873 DBG_ERR(" SERVER_GSS_CREDS: %p - %s\n",
1874 creds->server_gss_creds,
1875 obtained_to_str(creds->server_gss_creds_obtained));
1876 DBG_ERR(" KEYTAB: %p - %s\n",
1878 obtained_to_str(creds->keytab_obtained));
1879 DBG_ERR(" KVNO: %u\n",
1885 * @brief Obtain the SMB encryption state from a credentials structure.
1887 * @param[in] creds The credential structure to obtain the SMB encryption state
1890 * @return The SMB signing state.
1892 _PUBLIC_ enum smb_encryption_setting
1893 cli_credentials_get_smb_encryption(struct cli_credentials *creds)
1895 return creds->encryption_state;
1899 * Encrypt a data blob using the session key and the negotiated encryption
1902 * @param state Credential state, contains the session key and algorithm
1903 * @param data Data blob containing the data to be encrypted.
1906 _PUBLIC_ NTSTATUS netlogon_creds_session_encrypt(
1907 struct netlogon_creds_CredentialState *state,
1912 if (data.data == NULL || data.length == 0) {
1913 DBG_ERR("Nothing to encrypt "
1914 "data.data == NULL or data.length == 0\n");
1915 return NT_STATUS_INVALID_PARAMETER;
1918 * Don't crypt an all-zero password it will give away the
1919 * NETLOGON pipe session key .
1921 if (all_zero(data.data, data.length)) {
1922 DBG_ERR("Supplied data all zeros, could leak session key\n");
1923 return NT_STATUS_INVALID_PARAMETER;
1925 if (state->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
1926 status = netlogon_creds_aes_encrypt(state,
1929 } else if (state->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1930 status = netlogon_creds_arcfour_crypt(state,
1934 DBG_ERR("Unsupported encryption option negotiated\n");
1935 status = NT_STATUS_NOT_SUPPORTED;
1937 if (!NT_STATUS_IS_OK(status)) {
1940 return NT_STATUS_OK;