credentials: Avoid unnecessary includes.
[Samba/vl.git] / source4 / auth / credentials / credentials.c
blob2bd041450c0cc1d153cd860173ac3ca976045e3e
1 /*
2 Unix SMB/CIFS implementation.
4 User credentials handling
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
26 #include "auth/credentials/credentials.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "lib/events/events.h"
29 #include "param/param.h"
30 #include "system/filesys.h"
32 /**
33 * Create a new credentials structure
34 * @param mem_ctx TALLOC_CTX parent for credentials structure
36 _PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
38 struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
39 if (cred == NULL) {
40 return cred;
43 cred->workstation_obtained = CRED_UNINITIALISED;
44 cred->username_obtained = CRED_UNINITIALISED;
45 cred->password_obtained = CRED_UNINITIALISED;
46 cred->domain_obtained = CRED_UNINITIALISED;
47 cred->realm_obtained = CRED_UNINITIALISED;
48 cred->ccache_obtained = CRED_UNINITIALISED;
49 cred->client_gss_creds_obtained = CRED_UNINITIALISED;
50 cred->principal_obtained = CRED_UNINITIALISED;
51 cred->keytab_obtained = CRED_UNINITIALISED;
52 cred->server_gss_creds_obtained = CRED_UNINITIALISED;
54 cred->ccache_threshold = CRED_UNINITIALISED;
55 cred->client_gss_creds_threshold = CRED_UNINITIALISED;
57 cred->workstation = NULL;
58 cred->username = NULL;
59 cred->password = NULL;
60 cred->old_password = NULL;
61 cred->domain = NULL;
62 cred->realm = NULL;
63 cred->principal = NULL;
64 cred->salt_principal = NULL;
65 cred->impersonate_principal = NULL;
66 cred->target_service = NULL;
68 cred->bind_dn = NULL;
70 cred->nt_hash = NULL;
72 cred->lm_response.data = NULL;
73 cred->lm_response.length = 0;
74 cred->nt_response.data = NULL;
75 cred->nt_response.length = 0;
77 cred->ccache = NULL;
78 cred->client_gss_creds = NULL;
79 cred->keytab = NULL;
80 cred->server_gss_creds = NULL;
82 cred->workstation_cb = NULL;
83 cred->password_cb = NULL;
84 cred->username_cb = NULL;
85 cred->domain_cb = NULL;
86 cred->realm_cb = NULL;
87 cred->principal_cb = NULL;
89 cred->priv_data = NULL;
91 cred->netlogon_creds = NULL;
92 cred->secure_channel_type = SEC_CHAN_NULL;
94 cred->kvno = 0;
96 cred->password_last_changed_time = 0;
98 cred->smb_krb5_context = NULL;
100 cred->machine_account_pending = false;
101 cred->machine_account_pending_lp_ctx = NULL;
103 cred->machine_account = false;
105 cred->tries = 3;
107 cred->callback_running = false;
109 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
110 cli_credentials_set_gensec_features(cred, 0);
111 cli_credentials_set_krb_forwardable(cred, CRED_AUTO_KRB_FORWARDABLE);
113 return cred;
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_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
131 enum credentials_use_kerberos use_kerberos)
133 creds->use_kerberos = use_kerberos;
136 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
137 enum credentials_krb_forwardable krb_forwardable)
139 creds->krb_forwardable = krb_forwardable;
142 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
144 return creds->use_kerberos;
147 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
149 return creds->krb_forwardable;
152 _PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
154 creds->gensec_features = gensec_features;
157 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
159 return creds->gensec_features;
164 * Obtain the username for this credentials context.
165 * @param cred credentials context
166 * @retval The username set on this context.
167 * @note Return value will never be NULL except by programmer error.
169 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
171 if (cred->machine_account_pending) {
172 cli_credentials_set_machine_account(cred,
173 cred->machine_account_pending_lp_ctx);
176 if (cred->username_obtained == CRED_CALLBACK &&
177 !cred->callback_running) {
178 cred->callback_running = true;
179 cred->username = cred->username_cb(cred);
180 cred->callback_running = false;
181 cred->username_obtained = CRED_SPECIFIED;
182 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
185 return cred->username;
188 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
189 const char *val, enum credentials_obtained obtained)
191 if (obtained >= cred->username_obtained) {
192 cred->username = talloc_strdup(cred, val);
193 cred->username_obtained = obtained;
194 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
195 return true;
198 return false;
201 bool cli_credentials_set_username_callback(struct cli_credentials *cred,
202 const char *(*username_cb) (struct cli_credentials *))
204 if (cred->username_obtained < CRED_CALLBACK) {
205 cred->username_cb = username_cb;
206 cred->username_obtained = CRED_CALLBACK;
207 return true;
210 return false;
213 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
214 const char *bind_dn)
216 cred->bind_dn = talloc_strdup(cred, bind_dn);
217 return true;
221 * Obtain the BIND DN for this credentials context.
222 * @param cred credentials context
223 * @retval The username set on this context.
224 * @note Return value will be NULL if not specified explictly
226 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
228 return cred->bind_dn;
233 * Obtain the client principal for this credentials context.
234 * @param cred credentials context
235 * @retval The username set on this context.
236 * @note Return value will never be NULL except by programmer error.
238 const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
240 if (cred->machine_account_pending) {
241 cli_credentials_set_machine_account(cred,
242 cred->machine_account_pending_lp_ctx);
245 if (cred->principal_obtained == CRED_CALLBACK &&
246 !cred->callback_running) {
247 cred->callback_running = true;
248 cred->principal = cred->principal_cb(cred);
249 cred->callback_running = false;
250 cred->principal_obtained = CRED_SPECIFIED;
251 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
254 if (cred->principal_obtained < cred->username_obtained
255 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
256 if (cred->domain_obtained > cred->realm_obtained) {
257 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
258 return talloc_asprintf(mem_ctx, "%s@%s",
259 cli_credentials_get_username(cred),
260 cli_credentials_get_domain(cred));
261 } else {
262 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
263 return talloc_asprintf(mem_ctx, "%s@%s",
264 cli_credentials_get_username(cred),
265 cli_credentials_get_realm(cred));
268 *obtained = cred->principal_obtained;
269 return talloc_reference(mem_ctx, cred->principal);
273 * Obtain the client principal for this credentials context.
274 * @param cred credentials context
275 * @retval The username set on this context.
276 * @note Return value will never be NULL except by programmer error.
278 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
280 enum credentials_obtained obtained;
281 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
284 bool cli_credentials_set_principal(struct cli_credentials *cred,
285 const char *val,
286 enum credentials_obtained obtained)
288 if (obtained >= cred->principal_obtained) {
289 cred->principal = talloc_strdup(cred, val);
290 cred->principal_obtained = obtained;
291 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
292 return true;
295 return false;
298 /* Set a callback to get the principal. This could be a popup dialog,
299 * a terminal prompt or similar. */
300 bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
301 const char *(*principal_cb) (struct cli_credentials *))
303 if (cred->principal_obtained < CRED_CALLBACK) {
304 cred->principal_cb = principal_cb;
305 cred->principal_obtained = CRED_CALLBACK;
306 return true;
309 return false;
312 /* Some of our tools are 'anonymous by default'. This is a single
313 * function to determine if authentication has been explicitly
314 * requested */
316 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
318 if (cred->bind_dn) {
319 return true;
322 if (cli_credentials_is_anonymous(cred)){
323 return false;
326 if (cred->principal_obtained >= CRED_SPECIFIED) {
327 return true;
329 if (cred->username_obtained >= CRED_SPECIFIED) {
330 return true;
333 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
334 return true;
337 return false;
341 * Obtain the password for this credentials context.
342 * @param cred credentials context
343 * @retval If set, the cleartext password, otherwise NULL
345 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
347 if (cred->machine_account_pending) {
348 cli_credentials_set_machine_account(cred,
349 cred->machine_account_pending_lp_ctx);
352 if (cred->password_obtained == CRED_CALLBACK &&
353 !cred->callback_running) {
354 cred->callback_running = true;
355 cred->password = cred->password_cb(cred);
356 cred->callback_running = false;
357 cred->password_obtained = CRED_CALLBACK_RESULT;
358 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
361 return cred->password;
364 /* Set a password on the credentials context, including an indication
365 * of 'how' the password was obtained */
367 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
368 const char *val,
369 enum credentials_obtained obtained)
371 if (obtained >= cred->password_obtained) {
372 cred->password = talloc_strdup(cred, val);
373 cred->password_obtained = obtained;
374 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
376 cred->nt_hash = NULL;
377 cred->lm_response = data_blob(NULL, 0);
378 cred->nt_response = data_blob(NULL, 0);
379 return true;
382 return false;
385 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
386 const char *(*password_cb) (struct cli_credentials *))
388 if (cred->password_obtained < CRED_CALLBACK) {
389 cred->password_cb = password_cb;
390 cred->password_obtained = CRED_CALLBACK;
391 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
392 return true;
395 return false;
399 * Obtain the 'old' password for this credentials context (used for join accounts).
400 * @param cred credentials context
401 * @retval If set, the cleartext password, otherwise NULL
403 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
405 if (cred->machine_account_pending) {
406 cli_credentials_set_machine_account(cred,
407 cred->machine_account_pending_lp_ctx);
410 return cred->old_password;
413 bool cli_credentials_set_old_password(struct cli_credentials *cred,
414 const char *val,
415 enum credentials_obtained obtained)
417 cred->old_password = talloc_strdup(cred, val);
418 return true;
422 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
424 * Sometimes we only have this much of the password, while the rest of
425 * the time this call avoids calling E_md4hash themselves.
427 * @param cred credentials context
428 * @retval If set, the cleartext password, otherwise NULL
430 _PUBLIC_ const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
431 TALLOC_CTX *mem_ctx)
433 const char *password = cli_credentials_get_password(cred);
435 if (password) {
436 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
437 if (!nt_hash) {
438 return NULL;
441 E_md4hash(password, nt_hash->hash);
443 return nt_hash;
444 } else {
445 return cred->nt_hash;
450 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
451 * @param cred credentials context
452 * @retval The domain set on this context.
453 * @note Return value will never be NULL except by programmer error.
455 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
457 if (cred->machine_account_pending) {
458 cli_credentials_set_machine_account(cred,
459 cred->machine_account_pending_lp_ctx);
462 if (cred->domain_obtained == CRED_CALLBACK &&
463 !cred->callback_running) {
464 cred->callback_running = true;
465 cred->domain = cred->domain_cb(cred);
466 cred->callback_running = false;
467 cred->domain_obtained = CRED_SPECIFIED;
468 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
471 return cred->domain;
475 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
476 const char *val,
477 enum credentials_obtained obtained)
479 if (obtained >= cred->domain_obtained) {
480 /* it is important that the domain be in upper case,
481 * particularly for the sensitive NTLMv2
482 * calculations */
483 cred->domain = strupper_talloc(cred, val);
484 cred->domain_obtained = obtained;
485 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
486 return true;
489 return false;
492 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
493 const char *(*domain_cb) (struct cli_credentials *))
495 if (cred->domain_obtained < CRED_CALLBACK) {
496 cred->domain_cb = domain_cb;
497 cred->domain_obtained = CRED_CALLBACK;
498 return true;
501 return false;
505 * Obtain the Kerberos realm for this credentials context.
506 * @param cred credentials context
507 * @retval The realm set on this context.
508 * @note Return value will never be NULL except by programmer error.
510 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
512 if (cred->machine_account_pending) {
513 cli_credentials_set_machine_account(cred,
514 cred->machine_account_pending_lp_ctx);
517 if (cred->realm_obtained == CRED_CALLBACK &&
518 !cred->callback_running) {
519 cred->callback_running = true;
520 cred->realm = cred->realm_cb(cred);
521 cred->callback_running = false;
522 cred->realm_obtained = CRED_SPECIFIED;
523 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
526 return cred->realm;
530 * Set the realm for this credentials context, and force it to
531 * uppercase for the sainity of our local kerberos libraries
533 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
534 const char *val,
535 enum credentials_obtained obtained)
537 if (obtained >= cred->realm_obtained) {
538 cred->realm = strupper_talloc(cred, val);
539 cred->realm_obtained = obtained;
540 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
541 return true;
544 return false;
547 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
548 const char *(*realm_cb) (struct cli_credentials *))
550 if (cred->realm_obtained < CRED_CALLBACK) {
551 cred->realm_cb = realm_cb;
552 cred->realm_obtained = CRED_CALLBACK;
553 return true;
556 return false;
560 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
562 * @param cred credentials context
563 * @retval The workstation name set on this context.
564 * @note Return value will never be NULL except by programmer error.
566 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
568 if (cred->workstation_obtained == CRED_CALLBACK &&
569 !cred->callback_running) {
570 cred->callback_running = true;
571 cred->workstation = cred->workstation_cb(cred);
572 cred->callback_running = false;
573 cred->workstation_obtained = CRED_SPECIFIED;
576 return cred->workstation;
579 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
580 const char *val,
581 enum credentials_obtained obtained)
583 if (obtained >= cred->workstation_obtained) {
584 cred->workstation = talloc_strdup(cred, val);
585 cred->workstation_obtained = obtained;
586 return true;
589 return false;
592 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
593 const char *(*workstation_cb) (struct cli_credentials *))
595 if (cred->workstation_obtained < CRED_CALLBACK) {
596 cred->workstation_cb = workstation_cb;
597 cred->workstation_obtained = CRED_CALLBACK;
598 return true;
601 return false;
605 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
607 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
609 * @param credentials Credentials structure on which to set the password
610 * @param data the string containing the username, password etc
611 * @param obtained This enum describes how 'specified' this password is
614 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
616 char *uname, *p;
618 if (strcmp("%",data) == 0) {
619 cli_credentials_set_anonymous(credentials);
620 return;
623 uname = talloc_strdup(credentials, data);
624 if ((p = strchr_m(uname,'%'))) {
625 *p = 0;
626 cli_credentials_set_password(credentials, p+1, obtained);
629 if ((p = strchr_m(uname,'@'))) {
630 cli_credentials_set_principal(credentials, uname, obtained);
631 *p = 0;
632 cli_credentials_set_realm(credentials, p+1, obtained);
633 return;
634 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
635 *p = 0;
636 cli_credentials_set_domain(credentials, uname, obtained);
637 uname = p+1;
639 cli_credentials_set_username(credentials, uname, obtained);
643 * Given a a credentials structure, print it as a string
645 * The format output is [domain\\]user[%password] or user[@realm][%password]
647 * @param credentials Credentials structure on which to set the password
648 * @param mem_ctx The memory context to place the result on
651 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
653 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
654 const char *domain;
655 const char *username;
656 const char *name;
658 if (bind_dn) {
659 name = talloc_reference(mem_ctx, bind_dn);
660 } else {
661 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
662 if (domain && domain[0]) {
663 name = talloc_asprintf(mem_ctx, "%s\\%s",
664 domain, username);
665 } else {
666 name = talloc_asprintf(mem_ctx, "%s",
667 username);
670 return name;
674 * Specifies default values for domain, workstation and realm
675 * from the smb.conf configuration file
677 * @param cred Credentials structure to fill in
679 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
680 struct loadparm_context *lp_ctx)
682 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
683 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
684 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
685 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
689 * Guess defaults for credentials from environment variables,
690 * and from the configuration file
692 * @param cred Credentials structure to fill in
694 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
695 struct loadparm_context *lp_ctx)
697 char *p;
698 const char *error_string;
700 if (lp_ctx != NULL) {
701 cli_credentials_set_conf(cred, lp_ctx);
704 if (getenv("LOGNAME")) {
705 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
708 if (getenv("USER")) {
709 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
710 if ((p = strchr_m(getenv("USER"),'%'))) {
711 memset(p,0,strlen(cred->password));
715 if (getenv("PASSWD")) {
716 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
719 if (getenv("PASSWD_FD")) {
720 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
721 CRED_GUESS_FILE);
724 p = getenv("PASSWD_FILE");
725 if (p && p[0]) {
726 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
729 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
730 cli_credentials_set_ccache(cred, event_context_find(cred), lp_ctx, NULL, CRED_GUESS_FILE,
731 &error_string);
736 * Attach NETLOGON credentials for use with SCHANNEL
739 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
740 struct netlogon_creds_CredentialState *netlogon_creds)
742 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
746 * Return attached NETLOGON credentials
749 struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
751 return cred->netlogon_creds;
754 /**
755 * Set NETLOGON secure channel type
758 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
759 enum netr_SchannelType secure_channel_type)
761 cred->secure_channel_type = secure_channel_type;
765 * Return NETLOGON secure chanel type
768 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
770 return cred->password_last_changed_time;
773 /**
774 * Set NETLOGON secure channel type
777 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
778 time_t last_changed_time)
780 cred->password_last_changed_time = last_changed_time;
784 * Return NETLOGON secure chanel type
787 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
789 return cred->secure_channel_type;
793 * Fill in a credentials structure as the anonymous user
795 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
797 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
798 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
799 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
800 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
801 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
805 * Describe a credentials context as anonymous or authenticated
806 * @retval true if anonymous, false if a username is specified
809 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
811 const char *username;
813 /* if bind dn is set it's not anonymous */
814 if (cred->bind_dn) {
815 return false;
818 if (cred->machine_account_pending) {
819 cli_credentials_set_machine_account(cred,
820 cred->machine_account_pending_lp_ctx);
823 username = cli_credentials_get_username(cred);
825 /* Yes, it is deliberate that we die if we have a NULL pointer
826 * here - anonymous is "", not NULL, which is 'never specified,
827 * never guessed', ie programmer bug */
828 if (!username[0]) {
829 return true;
832 return false;
836 * Mark the current password for a credentials struct as wrong. This will
837 * cause the password to be prompted again (if a callback is set).
839 * This will decrement the number of times the password can be tried.
841 * @retval whether the credentials struct is finished
843 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
845 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
846 return false;
849 cred->password_obtained = CRED_CALLBACK;
851 cred->tries--;
853 return (cred->tries > 0);
856 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
857 const char **username,
858 const char **domain)
860 if (cred->principal_obtained > cred->username_obtained) {
861 *domain = talloc_strdup(mem_ctx, "");
862 *username = cli_credentials_get_principal(cred, mem_ctx);
863 } else {
864 *domain = cli_credentials_get_domain(cred);
865 *username = cli_credentials_get_username(cred);
870 * Read a named file, and parse it for username, domain, realm and password
872 * @param credentials Credentials structure on which to set the password
873 * @param file a named file to read the details from
874 * @param obtained This enum describes how 'specified' this password is
877 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
879 uint16_t len = 0;
880 char *ptr, *val, *param;
881 char **lines;
882 int i, numlines;
884 lines = file_lines_load(file, &numlines, 0, NULL);
886 if (lines == NULL)
888 /* fail if we can't open the credentials file */
889 d_printf("ERROR: Unable to open credentials file!\n");
890 return false;
893 for (i = 0; i < numlines; i++) {
894 len = strlen(lines[i]);
896 if (len == 0)
897 continue;
899 /* break up the line into parameter & value.
900 * will need to eat a little whitespace possibly */
901 param = lines[i];
902 if (!(ptr = strchr_m (lines[i], '=')))
903 continue;
905 val = ptr+1;
906 *ptr = '\0';
908 /* eat leading white space */
909 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
910 val++;
912 if (strwicmp("password", param) == 0) {
913 cli_credentials_set_password(cred, val, obtained);
914 } else if (strwicmp("username", param) == 0) {
915 cli_credentials_set_username(cred, val, obtained);
916 } else if (strwicmp("domain", param) == 0) {
917 cli_credentials_set_domain(cred, val, obtained);
918 } else if (strwicmp("realm", param) == 0) {
919 cli_credentials_set_realm(cred, val, obtained);
921 memset(lines[i], 0, len);
924 talloc_free(lines);
926 return true;
930 * Read a named file, and parse it for a password
932 * @param credentials Credentials structure on which to set the password
933 * @param file a named file to read the password from
934 * @param obtained This enum describes how 'specified' this password is
937 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
939 int fd = open(file, O_RDONLY, 0);
940 bool ret;
942 if (fd < 0) {
943 fprintf(stderr, "Error opening password file %s: %s\n",
944 file, strerror(errno));
945 return false;
948 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
950 close(fd);
952 return ret;
957 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
959 * @param credentials Credentials structure on which to set the password
960 * @param fd open file descriptor to read the password from
961 * @param obtained This enum describes how 'specified' this password is
964 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
965 int fd, enum credentials_obtained obtained)
967 char *p;
968 char pass[128];
970 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
971 p && p - pass < sizeof(pass);) {
972 switch (read(fd, p, 1)) {
973 case 1:
974 if (*p != '\n' && *p != '\0') {
975 *++p = '\0'; /* advance p, and null-terminate pass */
976 break;
978 /* fall through */
979 case 0:
980 if (p - pass) {
981 *p = '\0'; /* null-terminate it, just in case... */
982 p = NULL; /* then force the loop condition to become false */
983 break;
984 } else {
985 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
986 return false;
989 default:
990 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
991 fd, strerror(errno));
992 return false;
996 cli_credentials_set_password(credentials, pass, obtained);
997 return true;