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 "librpc/gen_ndr/samr.h" /* for struct samrPassword */
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_internal.h"
28 #include "auth/gensec/gensec.h"
29 #include "libcli/auth/libcli_auth.h"
31 #include "param/param.h"
32 #include "system/filesys.h"
35 * Create a new credentials structure
36 * @param mem_ctx TALLOC_CTX parent for credentials structure
38 _PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
40 struct cli_credentials *cred = talloc_zero(mem_ctx, struct cli_credentials);
45 cred->winbind_separator = '\\';
47 cred->use_kerberos = CRED_USE_KERBEROS_DESIRED;
49 cred->signing_state = SMB_SIGNING_DEFAULT;
52 * The default value of lpcfg_client_ipc_signing() is REQUIRED, so use
53 * the same value here.
55 cred->ipc_signing_state = SMB_SIGNING_REQUIRED;
56 cred->encryption_state = SMB_ENCRYPTION_DEFAULT;
62 struct cli_credentials *cli_credentials_init_server(TALLOC_CTX *mem_ctx,
63 struct loadparm_context *lp_ctx)
65 struct cli_credentials *server_creds = NULL;
68 server_creds = cli_credentials_init(mem_ctx);
69 if (server_creds == NULL) {
73 cli_credentials_set_conf(server_creds, lp_ctx);
75 status = cli_credentials_set_machine_account(server_creds, lp_ctx);
76 if (!NT_STATUS_IS_OK(status)) {
77 DEBUG(1, ("Failed to obtain server credentials: %s\n",
79 TALLOC_FREE(server_creds);
86 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
89 cred->priv_data = callback_data;
92 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
94 return cred->priv_data;
98 * Create a new anonymous credential
99 * @param mem_ctx TALLOC_CTX parent for credentials structure
101 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
103 struct cli_credentials *anon_credentials;
105 anon_credentials = cli_credentials_init(mem_ctx);
106 cli_credentials_set_anonymous(anon_credentials);
108 return anon_credentials;
111 _PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
112 enum credentials_use_kerberos use_kerberos)
114 creds->use_kerberos = use_kerberos;
117 _PUBLIC_ void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
118 const char *sasl_mech)
120 TALLOC_FREE(creds->forced_sasl_mech);
121 creds->forced_sasl_mech = talloc_strdup(creds, sasl_mech);
124 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
125 enum credentials_krb_forwardable krb_forwardable)
127 creds->krb_forwardable = krb_forwardable;
130 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
132 return creds->use_kerberos;
135 _PUBLIC_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
137 return creds->forced_sasl_mech;
140 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
142 return creds->krb_forwardable;
145 _PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
147 creds->gensec_features = gensec_features;
150 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
152 return creds->gensec_features;
157 * Obtain the username for this credentials context.
158 * @param cred credentials context
159 * @retval The username set on this context.
160 * @note Return value will never be NULL except by programmer error.
162 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
164 if (cred->machine_account_pending) {
165 cli_credentials_set_machine_account(cred,
166 cred->machine_account_pending_lp_ctx);
169 if (cred->username_obtained == CRED_CALLBACK &&
170 !cred->callback_running) {
171 cred->callback_running = true;
172 cred->username = cred->username_cb(cred);
173 cred->callback_running = false;
174 if (cred->username_obtained == CRED_CALLBACK) {
175 cred->username_obtained = CRED_CALLBACK_RESULT;
176 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
180 return cred->username;
183 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
184 const char *val, enum credentials_obtained obtained)
186 if (obtained >= cred->username_obtained) {
187 cred->username = talloc_strdup(cred, val);
188 cred->username_obtained = obtained;
189 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
196 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
197 const char *(*username_cb) (struct cli_credentials *))
199 if (cred->username_obtained < CRED_CALLBACK) {
200 cred->username_cb = username_cb;
201 cred->username_obtained = CRED_CALLBACK;
208 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
211 cred->bind_dn = talloc_strdup(cred, bind_dn);
216 * Obtain the BIND DN for this credentials context.
217 * @param cred credentials context
218 * @retval The username set on this context.
219 * @note Return value will be NULL if not specified explictly
221 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
223 return cred->bind_dn;
228 * Obtain the client principal for this credentials context.
229 * @param cred credentials context
230 * @retval The username set on this context.
231 * @note Return value will never be NULL except by programmer error.
233 _PUBLIC_ char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
235 if (cred->machine_account_pending) {
236 cli_credentials_set_machine_account(cred,
237 cred->machine_account_pending_lp_ctx);
240 if (cred->principal_obtained == CRED_CALLBACK &&
241 !cred->callback_running) {
242 cred->callback_running = true;
243 cred->principal = cred->principal_cb(cred);
244 cred->callback_running = false;
245 if (cred->principal_obtained == CRED_CALLBACK) {
246 cred->principal_obtained = CRED_CALLBACK_RESULT;
247 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
251 if (cred->principal_obtained < cred->username_obtained
252 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
253 const char *effective_username = NULL;
254 const char *effective_realm = NULL;
255 enum credentials_obtained effective_obtained;
257 effective_username = cli_credentials_get_username(cred);
258 if (effective_username == NULL || strlen(effective_username) == 0) {
259 *obtained = cred->username_obtained;
263 if (cred->domain_obtained > cred->realm_obtained) {
264 effective_realm = cli_credentials_get_domain(cred);
265 effective_obtained = MIN(cred->domain_obtained,
266 cred->username_obtained);
268 effective_realm = cli_credentials_get_realm(cred);
269 effective_obtained = MIN(cred->realm_obtained,
270 cred->username_obtained);
273 if (effective_realm == NULL || strlen(effective_realm) == 0) {
274 effective_realm = cli_credentials_get_domain(cred);
275 effective_obtained = MIN(cred->domain_obtained,
276 cred->username_obtained);
279 if (effective_realm != NULL && strlen(effective_realm) != 0) {
280 *obtained = effective_obtained;
281 return talloc_asprintf(mem_ctx, "%s@%s",
286 *obtained = cred->principal_obtained;
287 return talloc_strdup(mem_ctx, cred->principal);
291 * Obtain the client principal for this credentials context.
292 * @param cred credentials context
293 * @retval The username set on this context.
294 * @note Return value will never be NULL except by programmer error.
296 _PUBLIC_ char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
298 enum credentials_obtained obtained;
299 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
302 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
304 enum credentials_obtained obtained)
306 if (obtained >= cred->principal_obtained) {
307 cred->principal = talloc_strdup(cred, val);
308 if (cred->principal == NULL) {
311 cred->principal_obtained = obtained;
313 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
320 /* Set a callback to get the principal. This could be a popup dialog,
321 * a terminal prompt or similar. */
322 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
323 const char *(*principal_cb) (struct cli_credentials *))
325 if (cred->principal_obtained < CRED_CALLBACK) {
326 cred->principal_cb = principal_cb;
327 cred->principal_obtained = CRED_CALLBACK;
334 /* Some of our tools are 'anonymous by default'. This is a single
335 * function to determine if authentication has been explicitly
338 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
340 uint32_t gensec_features = 0;
347 * If we forced the mech we clearly want authentication. E.g. to use
348 * SASL/EXTERNAL which has no credentials.
350 if (cred->forced_sasl_mech) {
354 if (cli_credentials_is_anonymous(cred)){
358 if (cred->principal_obtained >= CRED_SPECIFIED) {
361 if (cred->username_obtained >= CRED_SPECIFIED) {
365 if (cli_credentials_get_kerberos_state(cred) == CRED_USE_KERBEROS_REQUIRED) {
369 gensec_features = cli_credentials_get_gensec_features(cred);
370 if (gensec_features & GENSEC_FEATURE_NTLM_CCACHE) {
374 if (gensec_features & GENSEC_FEATURE_SIGN) {
378 if (gensec_features & GENSEC_FEATURE_SEAL) {
386 * Obtain the password for this credentials context.
387 * @param cred credentials context
388 * @retval If set, the cleartext password, otherwise NULL
390 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
392 if (cred->machine_account_pending) {
393 cli_credentials_set_machine_account(cred,
394 cred->machine_account_pending_lp_ctx);
397 if (cred->password_obtained == CRED_CALLBACK &&
398 !cred->callback_running &&
399 !cred->password_will_be_nt_hash) {
400 cred->callback_running = true;
401 cred->password = cred->password_cb(cred);
402 cred->callback_running = false;
403 if (cred->password_obtained == CRED_CALLBACK) {
404 cred->password_obtained = CRED_CALLBACK_RESULT;
405 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
409 return cred->password;
412 /* Set a password on the credentials context, including an indication
413 * of 'how' the password was obtained */
415 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
417 enum credentials_obtained obtained)
419 if (obtained >= cred->password_obtained) {
421 cred->lm_response = data_blob_null;
422 cred->nt_response = data_blob_null;
423 cred->nt_hash = NULL;
424 cred->password = NULL;
426 cli_credentials_invalidate_ccache(cred, obtained);
428 cred->password_tries = 0;
431 cred->password_obtained = obtained;
435 if (cred->password_will_be_nt_hash) {
436 struct samr_Password *nt_hash = NULL;
437 size_t val_len = strlen(val);
440 nt_hash = talloc(cred, struct samr_Password);
441 if (nt_hash == NULL) {
445 converted = strhex_to_str((char *)nt_hash->hash,
446 sizeof(nt_hash->hash),
448 if (converted != sizeof(nt_hash->hash)) {
449 TALLOC_FREE(nt_hash);
453 cred->nt_hash = nt_hash;
454 cred->password_obtained = obtained;
458 cred->password = talloc_strdup(cred, val);
459 if (cred->password == NULL) {
463 /* Don't print the actual password in talloc memory dumps */
464 talloc_set_name_const(cred->password,
465 "password set via cli_credentials_set_password");
466 cred->password_obtained = obtained;
474 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
475 const char *(*password_cb) (struct cli_credentials *))
477 if (cred->password_obtained < CRED_CALLBACK) {
478 cred->password_tries = 3;
479 cred->password_cb = password_cb;
480 cred->password_obtained = CRED_CALLBACK;
481 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
489 * Obtain the 'old' password for this credentials context (used for join accounts).
490 * @param cred credentials context
491 * @retval If set, the cleartext password, otherwise NULL
493 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
495 if (cred->machine_account_pending) {
496 cli_credentials_set_machine_account(cred,
497 cred->machine_account_pending_lp_ctx);
500 return cred->old_password;
503 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred,
505 enum credentials_obtained obtained)
507 cred->old_password = talloc_strdup(cred, val);
508 if (cred->old_password) {
509 /* Don't print the actual password in talloc memory dumps */
510 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
512 cred->old_nt_hash = NULL;
517 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
519 * Sometimes we only have this much of the password, while the rest of
520 * the time this call avoids calling E_md4hash themselves.
522 * @param cred credentials context
523 * @retval If set, the cleartext password, otherwise NULL
525 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
528 enum credentials_obtained password_obtained;
529 enum credentials_obtained ccache_threshold;
530 enum credentials_obtained client_gss_creds_threshold;
531 bool password_is_nt_hash;
532 const char *password = NULL;
533 struct samr_Password *nt_hash = NULL;
535 if (cred->nt_hash != NULL) {
537 * If we already have a hash it's easy.
543 * This is a bit tricky, with password_will_be_nt_hash
544 * we still need to get the value via the password_callback
545 * but if we did that we should not remember it's state
546 * in the long run so we need to undo it.
549 password_obtained = cred->password_obtained;
550 ccache_threshold = cred->ccache_threshold;
551 client_gss_creds_threshold = cred->client_gss_creds_threshold;
552 password_is_nt_hash = cred->password_will_be_nt_hash;
554 cred->password_will_be_nt_hash = false;
555 password = cli_credentials_get_password(cred);
557 cred->password_will_be_nt_hash = password_is_nt_hash;
558 if (password_is_nt_hash && password_obtained == CRED_CALLBACK) {
560 * We got the nt_hash as string via the callback,
561 * so we need to undo the state change.
563 * And also don't remember it as plaintext password.
565 cred->client_gss_creds_threshold = client_gss_creds_threshold;
566 cred->ccache_threshold = ccache_threshold;
567 cred->password_obtained = password_obtained;
568 cred->password = NULL;
571 if (password == NULL) {
575 nt_hash = talloc(cred, struct samr_Password);
576 if (nt_hash == NULL) {
580 if (password_is_nt_hash) {
581 size_t password_len = strlen(password);
584 converted = strhex_to_str((char *)nt_hash->hash,
585 sizeof(nt_hash->hash),
586 password, password_len);
587 if (converted != sizeof(nt_hash->hash)) {
588 TALLOC_FREE(nt_hash);
592 E_md4hash(password, nt_hash->hash);
595 cred->nt_hash = nt_hash;
599 nt_hash = talloc(mem_ctx, struct samr_Password);
600 if (nt_hash == NULL) {
604 *nt_hash = *cred->nt_hash;
610 * Obtain the old password, in the form MD4(unicode(password)) for this credentials context.
612 * Sometimes we only have this much of the password, while the rest of
613 * the time this call avoids calling E_md4hash themselves.
615 * @param cred credentials context
616 * @retval If set, the cleartext password, otherwise NULL
618 _PUBLIC_ struct samr_Password *cli_credentials_get_old_nt_hash(struct cli_credentials *cred,
621 const char *old_password = NULL;
623 if (cred->old_nt_hash != NULL) {
624 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
629 *nt_hash = *cred->old_nt_hash;
634 old_password = cli_credentials_get_old_password(cred);
636 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
641 E_md4hash(old_password, nt_hash->hash);
650 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
651 * @param cred credentials context
652 * @retval The domain set on this context.
653 * @note Return value will never be NULL except by programmer error.
655 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
657 if (cred->machine_account_pending) {
658 cli_credentials_set_machine_account(cred,
659 cred->machine_account_pending_lp_ctx);
662 if (cred->domain_obtained == CRED_CALLBACK &&
663 !cred->callback_running) {
664 cred->callback_running = true;
665 cred->domain = cred->domain_cb(cred);
666 cred->callback_running = false;
667 if (cred->domain_obtained == CRED_CALLBACK) {
668 cred->domain_obtained = CRED_CALLBACK_RESULT;
669 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
677 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
679 enum credentials_obtained obtained)
681 if (obtained >= cred->domain_obtained) {
682 /* it is important that the domain be in upper case,
683 * particularly for the sensitive NTLMv2
685 cred->domain = strupper_talloc(cred, val);
686 cred->domain_obtained = obtained;
687 /* setting domain does not mean we have to invalidate ccache
688 * because domain in not used for Kerberos operations.
689 * If ccache invalidation is required, one will anyway specify
690 * a password to kinit, and that will force invalidation of the ccache
698 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
699 const char *(*domain_cb) (struct cli_credentials *))
701 if (cred->domain_obtained < CRED_CALLBACK) {
702 cred->domain_cb = domain_cb;
703 cred->domain_obtained = CRED_CALLBACK;
711 * Obtain the Kerberos realm for this credentials context.
712 * @param cred credentials context
713 * @retval The realm set on this context.
714 * @note Return value will never be NULL except by programmer error.
716 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
718 if (cred->machine_account_pending) {
719 cli_credentials_set_machine_account(cred,
720 cred->machine_account_pending_lp_ctx);
723 if (cred->realm_obtained == CRED_CALLBACK &&
724 !cred->callback_running) {
725 cred->callback_running = true;
726 cred->realm = cred->realm_cb(cred);
727 cred->callback_running = false;
728 if (cred->realm_obtained == CRED_CALLBACK) {
729 cred->realm_obtained = CRED_CALLBACK_RESULT;
730 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
738 * Set the realm for this credentials context, and force it to
739 * uppercase for the sanity of our local kerberos libraries
741 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
743 enum credentials_obtained obtained)
745 if (obtained >= cred->realm_obtained) {
746 cred->realm = strupper_talloc(cred, val);
747 cred->realm_obtained = obtained;
748 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
755 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
756 const char *(*realm_cb) (struct cli_credentials *))
758 if (cred->realm_obtained < CRED_CALLBACK) {
759 cred->realm_cb = realm_cb;
760 cred->realm_obtained = CRED_CALLBACK;
768 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
770 * @param cred credentials context
771 * @retval The workstation name set on this context.
772 * @note Return value will never be NULL except by programmer error.
774 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
776 if (cred->workstation_obtained == CRED_CALLBACK &&
777 !cred->callback_running) {
778 cred->callback_running = true;
779 cred->workstation = cred->workstation_cb(cred);
780 cred->callback_running = false;
781 if (cred->workstation_obtained == CRED_CALLBACK) {
782 cred->workstation_obtained = CRED_CALLBACK_RESULT;
786 return cred->workstation;
789 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
791 enum credentials_obtained obtained)
793 if (obtained >= cred->workstation_obtained) {
794 cred->workstation = talloc_strdup(cred, val);
795 cred->workstation_obtained = obtained;
802 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
803 const char *(*workstation_cb) (struct cli_credentials *))
805 if (cred->workstation_obtained < CRED_CALLBACK) {
806 cred->workstation_cb = workstation_cb;
807 cred->workstation_obtained = CRED_CALLBACK;
815 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
817 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
819 * @param credentials Credentials structure on which to set the password
820 * @param data the string containing the username, password etc
821 * @param obtained This enum describes how 'specified' this password is
824 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
828 if (strcmp("%",data) == 0) {
829 cli_credentials_set_anonymous(credentials);
833 uname = talloc_strdup(credentials, data);
834 if ((p = strchr_m(uname,'%'))) {
836 cli_credentials_set_password(credentials, p+1, obtained);
839 if ((p = strchr_m(uname,'@'))) {
841 * We also need to set username and domain
842 * in order to undo the effect of
843 * cli_credentials_guess().
845 cli_credentials_set_username(credentials, uname, obtained);
846 cli_credentials_set_domain(credentials, "", obtained);
848 cli_credentials_set_principal(credentials, uname, obtained);
850 cli_credentials_set_realm(credentials, p+1, obtained);
852 } else if ((p = strchr_m(uname,'\\'))
853 || (p = strchr_m(uname, '/'))
854 || (p = strchr_m(uname, credentials->winbind_separator)))
856 const char *domain = NULL;
862 if (obtained == credentials->realm_obtained &&
863 !strequal_m(credentials->domain, domain))
866 * We need to undo a former set with the same level
867 * in order to get the expected result from
868 * cli_credentials_get_principal().
870 * But we only need to do that if the domain
873 cli_credentials_set_realm(credentials, domain, obtained);
875 cli_credentials_set_domain(credentials, domain, obtained);
877 if (obtained == credentials->principal_obtained &&
878 !strequal_m(credentials->username, uname))
881 * We need to undo a former set with the same level
882 * in order to get the expected result from
883 * cli_credentials_get_principal().
885 * But we only need to do that if the username
888 credentials->principal_obtained = CRED_UNINITIALISED;
889 credentials->principal = NULL;
891 cli_credentials_set_username(credentials, uname, obtained);
895 * Given a a credentials structure, print it as a string
897 * The format output is [domain\\]user[%password] or user[@realm][%password]
899 * @param credentials Credentials structure on which to set the password
900 * @param mem_ctx The memory context to place the result on
903 _PUBLIC_ char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
905 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
906 const char *domain = NULL;
907 const char *username = NULL;
911 name = talloc_strdup(mem_ctx, bind_dn);
913 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
914 if (domain && domain[0]) {
915 name = talloc_asprintf(mem_ctx, "%s\\%s",
918 name = talloc_asprintf(mem_ctx, "%s",
926 * Specifies default values for domain, workstation and realm
927 * from the smb.conf configuration file
929 * @param cred Credentials structure to fill in
931 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
932 struct loadparm_context *lp_ctx)
934 const char *sep = NULL;
935 const char *realm = lpcfg_realm(lp_ctx);
937 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
938 if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
939 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
941 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SMB_CONF);
943 if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
944 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
946 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SMB_CONF);
948 if (realm != NULL && strlen(realm) == 0) {
951 if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
952 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
954 cli_credentials_set_realm(cred, realm, CRED_SMB_CONF);
957 sep = lpcfg_winbind_separator(lp_ctx);
958 if (sep != NULL && sep[0] != '\0') {
959 cred->winbind_separator = *lpcfg_winbind_separator(lp_ctx);
962 if (cred->signing_state_obtained <= CRED_SMB_CONF) {
963 /* Will be set to default for invalid smb.conf values */
964 cred->signing_state = lpcfg_client_signing(lp_ctx);
965 cred->signing_state_obtained = CRED_SMB_CONF;
968 if (cred->ipc_signing_state_obtained <= CRED_SMB_CONF) {
969 /* Will be set to required for invalid smb.conf values */
970 cred->ipc_signing_state = lpcfg_client_ipc_signing(lp_ctx);
971 cred->ipc_signing_state_obtained = CRED_SMB_CONF;
974 if (cred->encryption_state_obtained <= CRED_SMB_CONF) {
975 /* Will be set to default for invalid smb.conf values */
976 cred->encryption_state = lpcfg_client_smb_encrypt(lp_ctx);
977 cred->encryption_state_obtained = CRED_SMB_CONF;
982 * Guess defaults for credentials from environment variables,
983 * and from the configuration file
985 * @param cred Credentials structure to fill in
987 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
988 struct loadparm_context *lp_ctx)
991 const char *error_string;
993 if (lp_ctx != NULL) {
994 cli_credentials_set_conf(cred, lp_ctx);
997 if (getenv("LOGNAME")) {
998 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
1001 if (getenv("USER")) {
1002 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
1003 if ((p = strchr_m(getenv("USER"),'%'))) {
1004 memset(p,0,strlen(cred->password));
1008 if (getenv("PASSWD")) {
1009 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
1012 if (getenv("PASSWD_FD")) {
1013 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
1017 p = getenv("PASSWD_FILE");
1019 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
1022 if (lp_ctx != NULL &&
1023 cli_credentials_get_kerberos_state(cred) != CRED_USE_KERBEROS_DISABLED) {
1024 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
1030 * Attach NETLOGON credentials for use with SCHANNEL
1033 _PUBLIC_ void cli_credentials_set_netlogon_creds(
1034 struct cli_credentials *cred,
1035 const struct netlogon_creds_CredentialState *netlogon_creds)
1037 TALLOC_FREE(cred->netlogon_creds);
1038 if (netlogon_creds == NULL) {
1041 cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
1045 * Return attached NETLOGON credentials
1048 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
1050 return cred->netlogon_creds;
1054 * Set NETLOGON secure channel type
1057 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
1058 enum netr_SchannelType secure_channel_type)
1060 cred->secure_channel_type = secure_channel_type;
1064 * Return NETLOGON secure chanel type
1067 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
1069 return cred->password_last_changed_time;
1073 * Set NETLOGON secure channel type
1076 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
1077 time_t last_changed_time)
1079 cred->password_last_changed_time = last_changed_time;
1083 * Return NETLOGON secure chanel type
1086 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
1088 return cred->secure_channel_type;
1092 * Fill in a credentials structure as the anonymous user
1094 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
1096 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
1097 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
1098 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
1099 cli_credentials_set_principal(cred, NULL, CRED_SPECIFIED);
1100 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
1101 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
1102 cli_credentials_set_kerberos_state(cred, CRED_USE_KERBEROS_DISABLED);
1106 * Describe a credentials context as anonymous or authenticated
1107 * @retval true if anonymous, false if a username is specified
1110 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
1112 const char *username;
1114 /* if bind dn is set it's not anonymous */
1115 if (cred->bind_dn) {
1119 if (cred->machine_account_pending) {
1120 cli_credentials_set_machine_account(cred,
1121 cred->machine_account_pending_lp_ctx);
1124 /* if principal is set, it's not anonymous */
1125 if ((cred->principal != NULL) && cred->principal_obtained >= cred->username_obtained) {
1129 username = cli_credentials_get_username(cred);
1131 /* Yes, it is deliberate that we die if we have a NULL pointer
1132 * here - anonymous is "", not NULL, which is 'never specified,
1133 * never guessed', ie programmer bug */
1142 * Mark the current password for a credentials struct as wrong. This will
1143 * cause the password to be prompted again (if a callback is set).
1145 * This will decrement the number of times the password can be tried.
1147 * @retval whether the credentials struct is finished
1149 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
1151 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
1155 if (cred->password_tries == 0) {
1159 cred->password_tries--;
1161 if (cred->password_tries == 0) {
1165 cred->password_obtained = CRED_CALLBACK;
1169 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
1170 const char **username,
1171 const char **domain)
1173 if (cred->principal_obtained >= cred->username_obtained) {
1174 *domain = talloc_strdup(mem_ctx, "");
1175 *username = cli_credentials_get_principal(cred, mem_ctx);
1177 *domain = cli_credentials_get_domain(cred);
1178 *username = cli_credentials_get_username(cred);
1183 * Read a named file, and parse it for username, domain, realm and password
1185 * @param credentials Credentials structure on which to set the password
1186 * @param file a named file to read the details from
1187 * @param obtained This enum describes how 'specified' this password is
1190 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
1193 char *ptr, *val, *param;
1196 const char *realm = NULL;
1197 const char *domain = NULL;
1198 const char *password = NULL;
1199 const char *username = NULL;
1201 lines = file_lines_load(file, &numlines, 0, NULL);
1205 /* fail if we can't open the credentials file */
1206 d_printf("ERROR: Unable to open credentials file!\n");
1210 for (i = 0; i < numlines; i++) {
1211 len = strlen(lines[i]);
1216 /* break up the line into parameter & value.
1217 * will need to eat a little whitespace possibly */
1219 if (!(ptr = strchr_m (lines[i], '=')))
1225 /* eat leading white space */
1226 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1229 if (strwicmp("password", param) == 0) {
1231 } else if (strwicmp("username", param) == 0) {
1233 } else if (strwicmp("domain", param) == 0) {
1235 } else if (strwicmp("realm", param) == 0) {
1240 * We need to readd '=' in order to let
1241 * the strlen() work in the last loop
1242 * that clears the memory.
1247 if (realm != NULL && strlen(realm) != 0) {
1249 * only overwrite with a valid string
1251 cli_credentials_set_realm(cred, realm, obtained);
1254 if (domain != NULL && strlen(domain) != 0) {
1256 * only overwrite with a valid string
1258 cli_credentials_set_domain(cred, domain, obtained);
1261 if (password != NULL) {
1265 cli_credentials_set_password(cred, password, obtained);
1268 if (username != NULL) {
1270 * The last "username" line takes preference
1271 * if the string also contains domain, realm or
1274 cli_credentials_parse_string(cred, username, obtained);
1277 for (i = 0; i < numlines; i++) {
1278 len = strlen(lines[i]);
1279 memset(lines[i], 0, len);
1287 * Read a named file, and parse it for a password
1289 * @param credentials Credentials structure on which to set the password
1290 * @param file a named file to read the password from
1291 * @param obtained This enum describes how 'specified' this password is
1294 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1296 int fd = open(file, O_RDONLY, 0);
1300 fprintf(stderr, "Error opening password file %s: %s\n",
1301 file, strerror(errno));
1305 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1314 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1316 * @param credentials Credentials structure on which to set the password
1317 * @param fd open file descriptor to read the password from
1318 * @param obtained This enum describes how 'specified' this password is
1321 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
1322 int fd, enum credentials_obtained obtained)
1327 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1328 p && p - pass < sizeof(pass);) {
1329 switch (read(fd, p, 1)) {
1331 if (*p != '\n' && *p != '\0') {
1332 *++p = '\0'; /* advance p, and null-terminate pass */
1339 *p = '\0'; /* null-terminate it, just in case... */
1340 p = NULL; /* then force the loop condition to become false */
1345 "Error reading password from file descriptor "
1346 "%d: empty password\n",
1351 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1352 fd, strerror(errno));
1357 cli_credentials_set_password(credentials, pass, obtained);
1362 * @brief Set the SMB signing state to request for a SMB connection.
1364 * @param[in] creds The credentials structure to update.
1366 * @param[in] signing_state The signing state to set.
1368 * @param obtained This way the described signing state was specified.
1370 * @return true if we could set the signing state, false otherwise.
1372 _PUBLIC_ bool cli_credentials_set_smb_signing(struct cli_credentials *creds,
1373 enum smb_signing_setting signing_state,
1374 enum credentials_obtained obtained)
1376 if (obtained >= creds->signing_state_obtained) {
1377 creds->signing_state_obtained = obtained;
1378 creds->signing_state = signing_state;
1386 * @brief Obtain the SMB signing state from a credentials structure.
1388 * @param[in] creds The credential structure to obtain the SMB signing state
1391 * @return The SMB singing state.
1393 _PUBLIC_ enum smb_signing_setting
1394 cli_credentials_get_smb_signing(struct cli_credentials *creds)
1396 return creds->signing_state;
1400 * @brief Set the SMB IPC signing state to request for a SMB connection.
1402 * @param[in] creds The credentials structure to update.
1404 * @param[in] signing_state The signing state to set.
1406 * @param obtained This way the described signing state was specified.
1408 * @return true if we could set the signing state, false otherwise.
1411 cli_credentials_set_smb_ipc_signing(struct cli_credentials *creds,
1412 enum smb_signing_setting ipc_signing_state,
1413 enum credentials_obtained obtained)
1415 if (obtained >= creds->ipc_signing_state_obtained) {
1416 creds->ipc_signing_state_obtained = obtained;
1417 creds->ipc_signing_state = ipc_signing_state;
1425 * @brief Obtain the SMB IPC signing state from a credentials structure.
1427 * @param[in] creds The credential structure to obtain the SMB IPC signing
1430 * @return The SMB singing state.
1432 _PUBLIC_ enum smb_signing_setting
1433 cli_credentials_get_smb_ipc_signing(struct cli_credentials *creds)
1435 return creds->ipc_signing_state;
1439 * @brief Set the SMB encryption state to request for a SMB connection.
1441 * @param[in] creds The credentials structure to update.
1443 * @param[in] encryption_state The encryption state to set.
1445 * @param obtained This way the described encryption state was specified.
1447 * @return true if we could set the encryption state, false otherwise.
1449 _PUBLIC_ bool cli_credentials_set_smb_encryption(struct cli_credentials *creds,
1450 enum smb_encryption_setting encryption_state,
1451 enum credentials_obtained obtained)
1453 if (obtained >= creds->encryption_state_obtained) {
1454 creds->encryption_state_obtained = obtained;
1455 creds->encryption_state = encryption_state;
1463 * @brief Obtain the SMB encryption state from a credentials structure.
1465 * @param[in] creds The credential structure to obtain the SMB encryption state
1468 * @return The SMB singing state.
1470 _PUBLIC_ enum smb_encryption_setting
1471 cli_credentials_get_smb_encryption(struct cli_credentials *creds)
1473 return creds->encryption_state;
1477 * Encrypt a data blob using the session key and the negotiated encryption
1480 * @param state Credential state, contains the session key and algorithm
1481 * @param data Data blob containing the data to be encrypted.
1484 _PUBLIC_ NTSTATUS netlogon_creds_session_encrypt(
1485 struct netlogon_creds_CredentialState *state,
1490 if (data.data == NULL || data.length == 0) {
1491 DBG_ERR("Nothing to encrypt "
1492 "data.data == NULL or data.length == 0");
1493 return NT_STATUS_INVALID_PARAMETER;
1496 * Don't crypt an all-zero password it will give away the
1497 * NETLOGON pipe session key .
1499 if (all_zero(data.data, data.length)) {
1500 DBG_ERR("Supplied data all zeros, could leak session key");
1501 return NT_STATUS_INVALID_PARAMETER;
1503 if (state->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
1504 status = netlogon_creds_aes_encrypt(state,
1507 } else if (state->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1508 status = netlogon_creds_arcfour_crypt(state,
1512 DBG_ERR("Unsupported encryption option negotiated");
1513 status = NT_STATUS_NOT_SUPPORTED;
1515 if (!NT_STATUS_IS_OK(status)) {
1518 return NT_STATUS_OK;