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_proto.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "lib/events/events.h"
30 #include "param/param.h"
31 #include "system/filesys.h"
34 * Create a new credentials structure
35 * @param mem_ctx TALLOC_CTX parent for credentials structure
37 _PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
39 struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
44 cred->workstation_obtained = CRED_UNINITIALISED;
45 cred->username_obtained = CRED_UNINITIALISED;
46 cred->password_obtained = CRED_UNINITIALISED;
47 cred->domain_obtained = CRED_UNINITIALISED;
48 cred->realm_obtained = CRED_UNINITIALISED;
49 cred->ccache_obtained = CRED_UNINITIALISED;
50 cred->client_gss_creds_obtained = CRED_UNINITIALISED;
51 cred->principal_obtained = CRED_UNINITIALISED;
52 cred->keytab_obtained = CRED_UNINITIALISED;
53 cred->server_gss_creds_obtained = CRED_UNINITIALISED;
55 cred->ccache_threshold = CRED_UNINITIALISED;
56 cred->client_gss_creds_threshold = CRED_UNINITIALISED;
58 cred->workstation = NULL;
59 cred->username = NULL;
60 cred->password = NULL;
61 cred->old_password = NULL;
64 cred->principal = NULL;
65 cred->salt_principal = NULL;
66 cred->impersonate_principal = NULL;
67 cred->self_service = NULL;
68 cred->target_service = NULL;
74 cred->lm_response.data = NULL;
75 cred->lm_response.length = 0;
76 cred->nt_response.data = NULL;
77 cred->nt_response.length = 0;
80 cred->client_gss_creds = NULL;
82 cred->server_gss_creds = NULL;
84 cred->workstation_cb = NULL;
85 cred->password_cb = NULL;
86 cred->username_cb = NULL;
87 cred->domain_cb = NULL;
88 cred->realm_cb = NULL;
89 cred->principal_cb = NULL;
91 cred->priv_data = NULL;
93 cred->netlogon_creds = NULL;
94 cred->secure_channel_type = SEC_CHAN_NULL;
98 cred->password_last_changed_time = 0;
100 cred->smb_krb5_context = NULL;
102 cred->machine_account_pending = false;
103 cred->machine_account_pending_lp_ctx = NULL;
105 cred->machine_account = false;
109 cred->callback_running = false;
111 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
112 cli_credentials_set_gensec_features(cred, 0);
113 cli_credentials_set_krb_forwardable(cred, CRED_AUTO_KRB_FORWARDABLE);
119 * Create a new anonymous credential
120 * @param mem_ctx TALLOC_CTX parent for credentials structure
122 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
124 struct cli_credentials *anon_credentials;
126 anon_credentials = cli_credentials_init(mem_ctx);
127 cli_credentials_set_anonymous(anon_credentials);
129 return anon_credentials;
132 _PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
133 enum credentials_use_kerberos use_kerberos)
135 creds->use_kerberos = use_kerberos;
138 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
139 enum credentials_krb_forwardable krb_forwardable)
141 creds->krb_forwardable = krb_forwardable;
144 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
146 return creds->use_kerberos;
149 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
151 return creds->krb_forwardable;
154 _PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
156 creds->gensec_features = gensec_features;
159 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
161 return creds->gensec_features;
166 * Obtain the username for this credentials context.
167 * @param cred credentials context
168 * @retval The username set on this context.
169 * @note Return value will never be NULL except by programmer error.
171 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
173 if (cred->machine_account_pending) {
174 cli_credentials_set_machine_account(cred,
175 cred->machine_account_pending_lp_ctx);
178 if (cred->username_obtained == CRED_CALLBACK &&
179 !cred->callback_running) {
180 cred->callback_running = true;
181 cred->username = cred->username_cb(cred);
182 cred->callback_running = false;
183 cred->username_obtained = CRED_SPECIFIED;
184 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
187 return cred->username;
190 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
191 const char *val, enum credentials_obtained obtained)
193 if (obtained >= cred->username_obtained) {
194 cred->username = talloc_strdup(cred, val);
195 cred->username_obtained = obtained;
196 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
203 bool cli_credentials_set_username_callback(struct cli_credentials *cred,
204 const char *(*username_cb) (struct cli_credentials *))
206 if (cred->username_obtained < CRED_CALLBACK) {
207 cred->username_cb = username_cb;
208 cred->username_obtained = CRED_CALLBACK;
215 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
218 cred->bind_dn = talloc_strdup(cred, bind_dn);
223 * Obtain the BIND DN for this credentials context.
224 * @param cred credentials context
225 * @retval The username set on this context.
226 * @note Return value will be NULL if not specified explictly
228 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
230 return cred->bind_dn;
235 * Obtain the client principal for this credentials context.
236 * @param cred credentials context
237 * @retval The username set on this context.
238 * @note Return value will never be NULL except by programmer error.
240 const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
242 if (cred->machine_account_pending) {
243 cli_credentials_set_machine_account(cred,
244 cred->machine_account_pending_lp_ctx);
247 if (cred->principal_obtained == CRED_CALLBACK &&
248 !cred->callback_running) {
249 cred->callback_running = true;
250 cred->principal = cred->principal_cb(cred);
251 cred->callback_running = false;
252 cred->principal_obtained = CRED_SPECIFIED;
253 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
256 if (cred->principal_obtained < cred->username_obtained
257 || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
258 if (cred->domain_obtained > cred->realm_obtained) {
259 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
260 return talloc_asprintf(mem_ctx, "%s@%s",
261 cli_credentials_get_username(cred),
262 cli_credentials_get_domain(cred));
264 *obtained = MIN(cred->domain_obtained, cred->username_obtained);
265 return talloc_asprintf(mem_ctx, "%s@%s",
266 cli_credentials_get_username(cred),
267 cli_credentials_get_realm(cred));
270 *obtained = cred->principal_obtained;
271 return talloc_reference(mem_ctx, cred->principal);
275 * Obtain the client principal for this credentials context.
276 * @param cred credentials context
277 * @retval The username set on this context.
278 * @note Return value will never be NULL except by programmer error.
280 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
282 enum credentials_obtained obtained;
283 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
286 bool cli_credentials_set_principal(struct cli_credentials *cred,
288 enum credentials_obtained obtained)
290 if (obtained >= cred->principal_obtained) {
291 cred->principal = talloc_strdup(cred, val);
292 cred->principal_obtained = obtained;
293 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
300 /* Set a callback to get the principal. This could be a popup dialog,
301 * a terminal prompt or similar. */
302 bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
303 const char *(*principal_cb) (struct cli_credentials *))
305 if (cred->principal_obtained < CRED_CALLBACK) {
306 cred->principal_cb = principal_cb;
307 cred->principal_obtained = CRED_CALLBACK;
314 /* Some of our tools are 'anonymous by default'. This is a single
315 * function to determine if authentication has been explicitly
318 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
324 if (cli_credentials_is_anonymous(cred)){
328 if (cred->principal_obtained >= CRED_SPECIFIED) {
331 if (cred->username_obtained >= CRED_SPECIFIED) {
335 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
343 * Obtain the password for this credentials context.
344 * @param cred credentials context
345 * @retval If set, the cleartext password, otherwise NULL
347 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
349 if (cred->machine_account_pending) {
350 cli_credentials_set_machine_account(cred,
351 cred->machine_account_pending_lp_ctx);
354 if (cred->password_obtained == CRED_CALLBACK &&
355 !cred->callback_running) {
356 cred->callback_running = true;
357 cred->password = cred->password_cb(cred);
358 cred->callback_running = false;
359 cred->password_obtained = CRED_CALLBACK_RESULT;
360 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
363 return cred->password;
366 /* Set a password on the credentials context, including an indication
367 * of 'how' the password was obtained */
369 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
371 enum credentials_obtained obtained)
373 if (obtained >= cred->password_obtained) {
374 cred->password = talloc_strdup(cred, val);
375 cred->password_obtained = obtained;
376 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
378 cred->nt_hash = NULL;
379 cred->lm_response = data_blob(NULL, 0);
380 cred->nt_response = data_blob(NULL, 0);
387 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
388 const char *(*password_cb) (struct cli_credentials *))
390 if (cred->password_obtained < CRED_CALLBACK) {
391 cred->password_cb = password_cb;
392 cred->password_obtained = CRED_CALLBACK;
393 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
401 * Obtain the 'old' password for this credentials context (used for join accounts).
402 * @param cred credentials context
403 * @retval If set, the cleartext password, otherwise NULL
405 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
407 if (cred->machine_account_pending) {
408 cli_credentials_set_machine_account(cred,
409 cred->machine_account_pending_lp_ctx);
412 return cred->old_password;
415 bool cli_credentials_set_old_password(struct cli_credentials *cred,
417 enum credentials_obtained obtained)
419 cred->old_password = talloc_strdup(cred, val);
424 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
426 * Sometimes we only have this much of the password, while the rest of
427 * the time this call avoids calling E_md4hash themselves.
429 * @param cred credentials context
430 * @retval If set, the cleartext password, otherwise NULL
432 _PUBLIC_ const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
435 const char *password = cli_credentials_get_password(cred);
438 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
443 E_md4hash(password, nt_hash->hash);
447 return cred->nt_hash;
452 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
453 * @param cred credentials context
454 * @retval The domain set on this context.
455 * @note Return value will never be NULL except by programmer error.
457 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
459 if (cred->machine_account_pending) {
460 cli_credentials_set_machine_account(cred,
461 cred->machine_account_pending_lp_ctx);
464 if (cred->domain_obtained == CRED_CALLBACK &&
465 !cred->callback_running) {
466 cred->callback_running = true;
467 cred->domain = cred->domain_cb(cred);
468 cred->callback_running = false;
469 cred->domain_obtained = CRED_SPECIFIED;
470 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
477 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
479 enum credentials_obtained obtained)
481 if (obtained >= cred->domain_obtained) {
482 /* it is important that the domain be in upper case,
483 * particularly for the sensitive NTLMv2
485 cred->domain = strupper_talloc(cred, val);
486 cred->domain_obtained = obtained;
487 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
494 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
495 const char *(*domain_cb) (struct cli_credentials *))
497 if (cred->domain_obtained < CRED_CALLBACK) {
498 cred->domain_cb = domain_cb;
499 cred->domain_obtained = CRED_CALLBACK;
507 * Obtain the Kerberos realm for this credentials context.
508 * @param cred credentials context
509 * @retval The realm set on this context.
510 * @note Return value will never be NULL except by programmer error.
512 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
514 if (cred->machine_account_pending) {
515 cli_credentials_set_machine_account(cred,
516 cred->machine_account_pending_lp_ctx);
519 if (cred->realm_obtained == CRED_CALLBACK &&
520 !cred->callback_running) {
521 cred->callback_running = true;
522 cred->realm = cred->realm_cb(cred);
523 cred->callback_running = false;
524 cred->realm_obtained = CRED_SPECIFIED;
525 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
532 * Set the realm for this credentials context, and force it to
533 * uppercase for the sainity of our local kerberos libraries
535 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
537 enum credentials_obtained obtained)
539 if (obtained >= cred->realm_obtained) {
540 cred->realm = strupper_talloc(cred, val);
541 cred->realm_obtained = obtained;
542 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
549 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
550 const char *(*realm_cb) (struct cli_credentials *))
552 if (cred->realm_obtained < CRED_CALLBACK) {
553 cred->realm_cb = realm_cb;
554 cred->realm_obtained = CRED_CALLBACK;
562 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
564 * @param cred credentials context
565 * @retval The workstation name set on this context.
566 * @note Return value will never be NULL except by programmer error.
568 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
570 if (cred->workstation_obtained == CRED_CALLBACK &&
571 !cred->callback_running) {
572 cred->callback_running = true;
573 cred->workstation = cred->workstation_cb(cred);
574 cred->callback_running = false;
575 cred->workstation_obtained = CRED_SPECIFIED;
578 return cred->workstation;
581 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
583 enum credentials_obtained obtained)
585 if (obtained >= cred->workstation_obtained) {
586 cred->workstation = talloc_strdup(cred, val);
587 cred->workstation_obtained = obtained;
594 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
595 const char *(*workstation_cb) (struct cli_credentials *))
597 if (cred->workstation_obtained < CRED_CALLBACK) {
598 cred->workstation_cb = workstation_cb;
599 cred->workstation_obtained = CRED_CALLBACK;
607 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
609 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
611 * @param credentials Credentials structure on which to set the password
612 * @param data the string containing the username, password etc
613 * @param obtained This enum describes how 'specified' this password is
616 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
620 if (strcmp("%",data) == 0) {
621 cli_credentials_set_anonymous(credentials);
625 uname = talloc_strdup(credentials, data);
626 if ((p = strchr_m(uname,'%'))) {
628 cli_credentials_set_password(credentials, p+1, obtained);
631 if ((p = strchr_m(uname,'@'))) {
632 cli_credentials_set_principal(credentials, uname, obtained);
634 cli_credentials_set_realm(credentials, p+1, obtained);
636 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
638 cli_credentials_set_domain(credentials, uname, obtained);
641 cli_credentials_set_username(credentials, uname, obtained);
645 * Given a a credentials structure, print it as a string
647 * The format output is [domain\\]user[%password] or user[@realm][%password]
649 * @param credentials Credentials structure on which to set the password
650 * @param mem_ctx The memory context to place the result on
653 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
655 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
657 const char *username;
661 name = talloc_reference(mem_ctx, bind_dn);
663 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
664 if (domain && domain[0]) {
665 name = talloc_asprintf(mem_ctx, "%s\\%s",
668 name = talloc_asprintf(mem_ctx, "%s",
676 * Specifies default values for domain, workstation and realm
677 * from the smb.conf configuration file
679 * @param cred Credentials structure to fill in
681 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
682 struct loadparm_context *lp_ctx)
684 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
685 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
686 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
687 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
691 * Guess defaults for credentials from environment variables,
692 * and from the configuration file
694 * @param cred Credentials structure to fill in
696 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
697 struct loadparm_context *lp_ctx)
700 const char *error_string;
702 if (lp_ctx != NULL) {
703 cli_credentials_set_conf(cred, lp_ctx);
706 if (getenv("LOGNAME")) {
707 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
710 if (getenv("USER")) {
711 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
712 if ((p = strchr_m(getenv("USER"),'%'))) {
713 memset(p,0,strlen(cred->password));
717 if (getenv("PASSWD")) {
718 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
721 if (getenv("PASSWD_FD")) {
722 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
726 p = getenv("PASSWD_FILE");
728 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
731 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
732 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
738 * Attach NETLOGON credentials for use with SCHANNEL
741 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
742 struct netlogon_creds_CredentialState *netlogon_creds)
744 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
748 * Return attached NETLOGON credentials
751 struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
753 return cred->netlogon_creds;
757 * Set NETLOGON secure channel type
760 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
761 enum netr_SchannelType secure_channel_type)
763 cred->secure_channel_type = secure_channel_type;
767 * Return NETLOGON secure chanel type
770 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
772 return cred->password_last_changed_time;
776 * Set NETLOGON secure channel type
779 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
780 time_t last_changed_time)
782 cred->password_last_changed_time = last_changed_time;
786 * Return NETLOGON secure chanel type
789 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
791 return cred->secure_channel_type;
795 * Fill in a credentials structure as the anonymous user
797 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
799 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
800 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
801 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
802 cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
803 cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
807 * Describe a credentials context as anonymous or authenticated
808 * @retval true if anonymous, false if a username is specified
811 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
813 const char *username;
815 /* if bind dn is set it's not anonymous */
820 if (cred->machine_account_pending) {
821 cli_credentials_set_machine_account(cred,
822 cred->machine_account_pending_lp_ctx);
825 username = cli_credentials_get_username(cred);
827 /* Yes, it is deliberate that we die if we have a NULL pointer
828 * here - anonymous is "", not NULL, which is 'never specified,
829 * never guessed', ie programmer bug */
838 * Mark the current password for a credentials struct as wrong. This will
839 * cause the password to be prompted again (if a callback is set).
841 * This will decrement the number of times the password can be tried.
843 * @retval whether the credentials struct is finished
845 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
847 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
851 cred->password_obtained = CRED_CALLBACK;
855 return (cred->tries > 0);
858 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
859 const char **username,
862 if (cred->principal_obtained > cred->username_obtained) {
863 *domain = talloc_strdup(mem_ctx, "");
864 *username = cli_credentials_get_principal(cred, mem_ctx);
866 *domain = cli_credentials_get_domain(cred);
867 *username = cli_credentials_get_username(cred);
872 * Read a named file, and parse it for username, domain, realm and password
874 * @param credentials Credentials structure on which to set the password
875 * @param file a named file to read the details from
876 * @param obtained This enum describes how 'specified' this password is
879 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
882 char *ptr, *val, *param;
886 lines = file_lines_load(file, &numlines, 0, NULL);
890 /* fail if we can't open the credentials file */
891 d_printf("ERROR: Unable to open credentials file!\n");
895 for (i = 0; i < numlines; i++) {
896 len = strlen(lines[i]);
901 /* break up the line into parameter & value.
902 * will need to eat a little whitespace possibly */
904 if (!(ptr = strchr_m (lines[i], '=')))
910 /* eat leading white space */
911 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
914 if (strwicmp("password", param) == 0) {
915 cli_credentials_set_password(cred, val, obtained);
916 } else if (strwicmp("username", param) == 0) {
917 cli_credentials_set_username(cred, val, obtained);
918 } else if (strwicmp("domain", param) == 0) {
919 cli_credentials_set_domain(cred, val, obtained);
920 } else if (strwicmp("realm", param) == 0) {
921 cli_credentials_set_realm(cred, val, obtained);
923 memset(lines[i], 0, len);
932 * Read a named file, and parse it for a password
934 * @param credentials Credentials structure on which to set the password
935 * @param file a named file to read the password from
936 * @param obtained This enum describes how 'specified' this password is
939 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
941 int fd = open(file, O_RDONLY, 0);
945 fprintf(stderr, "Error opening password file %s: %s\n",
946 file, strerror(errno));
950 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
959 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
961 * @param credentials Credentials structure on which to set the password
962 * @param fd open file descriptor to read the password from
963 * @param obtained This enum describes how 'specified' this password is
966 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
967 int fd, enum credentials_obtained obtained)
972 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
973 p && p - pass < sizeof(pass);) {
974 switch (read(fd, p, 1)) {
976 if (*p != '\n' && *p != '\0') {
977 *++p = '\0'; /* advance p, and null-terminate pass */
983 *p = '\0'; /* null-terminate it, just in case... */
984 p = NULL; /* then force the loop condition to become false */
987 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
992 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
993 fd, strerror(errno));
998 cli_credentials_set_password(credentials, pass, obtained);