source: trunk/server/source4/auth/credentials/credentials.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 28.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 User credentials handling
5
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9
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.
14
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.
19
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/>.
22*/
23
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"
31
32/**
33 * Create a new credentials structure
34 * @param mem_ctx TALLOC_CTX parent for credentials structure
35 */
36_PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
37{
38 struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
39 if (cred == NULL) {
40 return cred;
41 }
42
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;
53
54 cred->ccache_threshold = CRED_UNINITIALISED;
55 cred->client_gss_creds_threshold = CRED_UNINITIALISED;
56
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;
67
68 cred->bind_dn = NULL;
69
70 cred->nt_hash = NULL;
71
72 cred->lm_response.data = NULL;
73 cred->lm_response.length = 0;
74 cred->nt_response.data = NULL;
75 cred->nt_response.length = 0;
76
77 cred->ccache = NULL;
78 cred->client_gss_creds = NULL;
79 cred->keytab = NULL;
80 cred->server_gss_creds = NULL;
81
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;
88
89 cred->priv_data = NULL;
90
91 cred->netlogon_creds = NULL;
92 cred->secure_channel_type = SEC_CHAN_NULL;
93
94 cred->kvno = 0;
95
96 cred->password_last_changed_time = 0;
97
98 cred->smb_krb5_context = NULL;
99
100 cred->machine_account_pending = false;
101 cred->machine_account_pending_lp_ctx = NULL;
102
103 cred->machine_account = false;
104
105 cred->tries = 3;
106
107 cred->callback_running = false;
108
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);
112
113 return cred;
114}
115
116/**
117 * Create a new anonymous credential
118 * @param mem_ctx TALLOC_CTX parent for credentials structure
119 */
120_PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
121{
122 struct cli_credentials *anon_credentials;
123
124 anon_credentials = cli_credentials_init(mem_ctx);
125 cli_credentials_set_anonymous(anon_credentials);
126
127 return anon_credentials;
128}
129
130_PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds,
131 enum credentials_use_kerberos use_kerberos)
132{
133 creds->use_kerberos = use_kerberos;
134}
135
136_PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
137 enum credentials_krb_forwardable krb_forwardable)
138{
139 creds->krb_forwardable = krb_forwardable;
140}
141
142_PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
143{
144 return creds->use_kerberos;
145}
146
147_PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
148{
149 return creds->krb_forwardable;
150}
151
152_PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
153{
154 creds->gensec_features = gensec_features;
155}
156
157_PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
158{
159 return creds->gensec_features;
160}
161
162
163/**
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.
168 */
169_PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
170{
171 if (cred->machine_account_pending) {
172 cli_credentials_set_machine_account(cred,
173 cred->machine_account_pending_lp_ctx);
174 }
175
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);
183 }
184
185 return cred->username;
186}
187
188_PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred,
189 const char *val, enum credentials_obtained obtained)
190{
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;
196 }
197
198 return false;
199}
200
201bool cli_credentials_set_username_callback(struct cli_credentials *cred,
202 const char *(*username_cb) (struct cli_credentials *))
203{
204 if (cred->username_obtained < CRED_CALLBACK) {
205 cred->username_cb = username_cb;
206 cred->username_obtained = CRED_CALLBACK;
207 return true;
208 }
209
210 return false;
211}
212
213_PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred,
214 const char *bind_dn)
215{
216 cred->bind_dn = talloc_strdup(cred, bind_dn);
217 return true;
218}
219
220/**
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
225 */
226_PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
227{
228 return cred->bind_dn;
229}
230
231
232/**
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.
237 */
238const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
239{
240 if (cred->machine_account_pending) {
241 cli_credentials_set_machine_account(cred,
242 cred->machine_account_pending_lp_ctx);
243 }
244
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);
252 }
253
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));
266 }
267 }
268 *obtained = cred->principal_obtained;
269 return talloc_reference(mem_ctx, cred->principal);
270}
271
272/**
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.
277 */
278_PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
279{
280 enum credentials_obtained obtained;
281 return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
282}
283
284bool cli_credentials_set_principal(struct cli_credentials *cred,
285 const char *val,
286 enum credentials_obtained obtained)
287{
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;
293 }
294
295 return false;
296}
297
298/* Set a callback to get the principal. This could be a popup dialog,
299 * a terminal prompt or similar. */
300bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
301 const char *(*principal_cb) (struct cli_credentials *))
302{
303 if (cred->principal_obtained < CRED_CALLBACK) {
304 cred->principal_cb = principal_cb;
305 cred->principal_obtained = CRED_CALLBACK;
306 return true;
307 }
308
309 return false;
310}
311
312/* Some of our tools are 'anonymous by default'. This is a single
313 * function to determine if authentication has been explicitly
314 * requested */
315
316_PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred)
317{
318 if (cred->bind_dn) {
319 return true;
320 }
321
322 if (cli_credentials_is_anonymous(cred)){
323 return false;
324 }
325
326 if (cred->principal_obtained >= CRED_SPECIFIED) {
327 return true;
328 }
329 if (cred->username_obtained >= CRED_SPECIFIED) {
330 return true;
331 }
332
333 if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
334 return true;
335 }
336
337 return false;
338}
339
340/**
341 * Obtain the password for this credentials context.
342 * @param cred credentials context
343 * @retval If set, the cleartext password, otherwise NULL
344 */
345_PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
346{
347 if (cred->machine_account_pending) {
348 cli_credentials_set_machine_account(cred,
349 cred->machine_account_pending_lp_ctx);
350 }
351
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);
359 }
360
361 return cred->password;
362}
363
364/* Set a password on the credentials context, including an indication
365 * of 'how' the password was obtained */
366
367_PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred,
368 const char *val,
369 enum credentials_obtained obtained)
370{
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);
375
376 cred->nt_hash = NULL;
377 cred->lm_response = data_blob(NULL, 0);
378 cred->nt_response = data_blob(NULL, 0);
379 return true;
380 }
381
382 return false;
383}
384
385_PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
386 const char *(*password_cb) (struct cli_credentials *))
387{
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;
393 }
394
395 return false;
396}
397
398/**
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
402 */
403const char *cli_credentials_get_old_password(struct cli_credentials *cred)
404{
405 if (cred->machine_account_pending) {
406 cli_credentials_set_machine_account(cred,
407 cred->machine_account_pending_lp_ctx);
408 }
409
410 return cred->old_password;
411}
412
413bool cli_credentials_set_old_password(struct cli_credentials *cred,
414 const char *val,
415 enum credentials_obtained obtained)
416{
417 cred->old_password = talloc_strdup(cred, val);
418 return true;
419}
420
421/**
422 * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
423 *
424 * Sometimes we only have this much of the password, while the rest of
425 * the time this call avoids calling E_md4hash themselves.
426 *
427 * @param cred credentials context
428 * @retval If set, the cleartext password, otherwise NULL
429 */
430_PUBLIC_ const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
431 TALLOC_CTX *mem_ctx)
432{
433 const char *password = cli_credentials_get_password(cred);
434
435 if (password) {
436 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
437 if (!nt_hash) {
438 return NULL;
439 }
440
441 E_md4hash(password, nt_hash->hash);
442
443 return nt_hash;
444 } else {
445 return cred->nt_hash;
446 }
447}
448
449/**
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.
454 */
455_PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
456{
457 if (cred->machine_account_pending) {
458 cli_credentials_set_machine_account(cred,
459 cred->machine_account_pending_lp_ctx);
460 }
461
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);
469 }
470
471 return cred->domain;
472}
473
474
475_PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred,
476 const char *val,
477 enum credentials_obtained obtained)
478{
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;
487 }
488
489 return false;
490}
491
492bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
493 const char *(*domain_cb) (struct cli_credentials *))
494{
495 if (cred->domain_obtained < CRED_CALLBACK) {
496 cred->domain_cb = domain_cb;
497 cred->domain_obtained = CRED_CALLBACK;
498 return true;
499 }
500
501 return false;
502}
503
504/**
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.
509 */
510_PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
511{
512 if (cred->machine_account_pending) {
513 cli_credentials_set_machine_account(cred,
514 cred->machine_account_pending_lp_ctx);
515 }
516
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);
524 }
525
526 return cred->realm;
527}
528
529/**
530 * Set the realm for this credentials context, and force it to
531 * uppercase for the sainity of our local kerberos libraries
532 */
533_PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred,
534 const char *val,
535 enum credentials_obtained obtained)
536{
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;
542 }
543
544 return false;
545}
546
547bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
548 const char *(*realm_cb) (struct cli_credentials *))
549{
550 if (cred->realm_obtained < CRED_CALLBACK) {
551 cred->realm_cb = realm_cb;
552 cred->realm_obtained = CRED_CALLBACK;
553 return true;
554 }
555
556 return false;
557}
558
559/**
560 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
561 *
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.
565 */
566_PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
567{
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;
574 }
575
576 return cred->workstation;
577}
578
579_PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred,
580 const char *val,
581 enum credentials_obtained obtained)
582{
583 if (obtained >= cred->workstation_obtained) {
584 cred->workstation = talloc_strdup(cred, val);
585 cred->workstation_obtained = obtained;
586 return true;
587 }
588
589 return false;
590}
591
592bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
593 const char *(*workstation_cb) (struct cli_credentials *))
594{
595 if (cred->workstation_obtained < CRED_CALLBACK) {
596 cred->workstation_cb = workstation_cb;
597 cred->workstation_obtained = CRED_CALLBACK;
598 return true;
599 }
600
601 return false;
602}
603
604/**
605 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
606 *
607 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
608 *
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
612 */
613
614_PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
615{
616 char *uname, *p;
617
618 if (strcmp("%",data) == 0) {
619 cli_credentials_set_anonymous(credentials);
620 return;
621 }
622
623 uname = talloc_strdup(credentials, data);
624 if ((p = strchr_m(uname,'%'))) {
625 *p = 0;
626 cli_credentials_set_password(credentials, p+1, obtained);
627 }
628
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;
638 }
639 cli_credentials_set_username(credentials, uname, obtained);
640}
641
642/**
643 * Given a a credentials structure, print it as a string
644 *
645 * The format output is [domain\\]user[%password] or user[@realm][%password]
646 *
647 * @param credentials Credentials structure on which to set the password
648 * @param mem_ctx The memory context to place the result on
649 */
650
651_PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
652{
653 const char *bind_dn = cli_credentials_get_bind_dn(credentials);
654 const char *domain;
655 const char *username;
656 const char *name;
657
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);
668 }
669 }
670 return name;
671}
672
673/**
674 * Specifies default values for domain, workstation and realm
675 * from the smb.conf configuration file
676 *
677 * @param cred Credentials structure to fill in
678 */
679_PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred,
680 struct loadparm_context *lp_ctx)
681{
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);
686}
687
688/**
689 * Guess defaults for credentials from environment variables,
690 * and from the configuration file
691 *
692 * @param cred Credentials structure to fill in
693 */
694_PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
695 struct loadparm_context *lp_ctx)
696{
697 char *p;
698 const char *error_string;
699
700 if (lp_ctx != NULL) {
701 cli_credentials_set_conf(cred, lp_ctx);
702 }
703
704 if (getenv("LOGNAME")) {
705 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
706 }
707
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));
712 }
713 }
714
715 if (getenv("PASSWD")) {
716 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
717 }
718
719 if (getenv("PASSWD_FD")) {
720 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")),
721 CRED_GUESS_FILE);
722 }
723
724 p = getenv("PASSWD_FILE");
725 if (p && p[0]) {
726 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
727 }
728
729 if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
730 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
731 &error_string);
732 }
733}
734
735/**
736 * Attach NETLOGON credentials for use with SCHANNEL
737 */
738
739_PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
740 struct netlogon_creds_CredentialState *netlogon_creds)
741{
742 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
743}
744
745/**
746 * Return attached NETLOGON credentials
747 */
748
749struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
750{
751 return cred->netlogon_creds;
752}
753
754/**
755 * Set NETLOGON secure channel type
756 */
757
758_PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
759 enum netr_SchannelType secure_channel_type)
760{
761 cred->secure_channel_type = secure_channel_type;
762}
763
764/**
765 * Return NETLOGON secure chanel type
766 */
767
768_PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
769{
770 return cred->password_last_changed_time;
771}
772
773/**
774 * Set NETLOGON secure channel type
775 */
776
777_PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
778 time_t last_changed_time)
779{
780 cred->password_last_changed_time = last_changed_time;
781}
782
783/**
784 * Return NETLOGON secure chanel type
785 */
786
787_PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
788{
789 return cred->secure_channel_type;
790}
791
792/**
793 * Fill in a credentials structure as the anonymous user
794 */
795_PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred)
796{
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);
802}
803
804/**
805 * Describe a credentials context as anonymous or authenticated
806 * @retval true if anonymous, false if a username is specified
807 */
808
809_PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
810{
811 const char *username;
812
813 /* if bind dn is set it's not anonymous */
814 if (cred->bind_dn) {
815 return false;
816 }
817
818 if (cred->machine_account_pending) {
819 cli_credentials_set_machine_account(cred,
820 cred->machine_account_pending_lp_ctx);
821 }
822
823 username = cli_credentials_get_username(cred);
824
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;
830 }
831
832 return false;
833}
834
835/**
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).
838 *
839 * This will decrement the number of times the password can be tried.
840 *
841 * @retval whether the credentials struct is finished
842 */
843_PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
844{
845 if (cred->password_obtained != CRED_CALLBACK_RESULT) {
846 return false;
847 }
848
849 cred->password_obtained = CRED_CALLBACK;
850
851 cred->tries--;
852
853 return (cred->tries > 0);
854}
855
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)
859{
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);
866 }
867}
868
869/**
870 * Read a named file, and parse it for username, domain, realm and password
871 *
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
875 */
876
877_PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
878{
879 uint16_t len = 0;
880 char *ptr, *val, *param;
881 char **lines;
882 int i, numlines;
883
884 lines = file_lines_load(file, &numlines, 0, NULL);
885
886 if (lines == NULL)
887 {
888 /* fail if we can't open the credentials file */
889 d_printf("ERROR: Unable to open credentials file!\n");
890 return false;
891 }
892
893 for (i = 0; i < numlines; i++) {
894 len = strlen(lines[i]);
895
896 if (len == 0)
897 continue;
898
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;
904
905 val = ptr+1;
906 *ptr = '\0';
907
908 /* eat leading white space */
909 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
910 val++;
911
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);
920 }
921 memset(lines[i], 0, len);
922 }
923
924 talloc_free(lines);
925
926 return true;
927}
928
929/**
930 * Read a named file, and parse it for a password
931 *
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
935 */
936
937_PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
938{
939 int fd = open(file, O_RDONLY, 0);
940 bool ret;
941
942 if (fd < 0) {
943 fprintf(stderr, "Error opening password file %s: %s\n",
944 file, strerror(errno));
945 return false;
946 }
947
948 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
949
950 close(fd);
951
952 return ret;
953}
954
955
956/**
957 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
958 *
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
962 */
963
964_PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
965 int fd, enum credentials_obtained obtained)
966{
967 char *p;
968 char pass[128];
969
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;
977 }
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;
987 }
988
989 default:
990 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
991 fd, strerror(errno));
992 return false;
993 }
994 }
995
996 cli_credentials_set_password(credentials, pass, obtained);
997 return true;
998}
999
1000
Note: See TracBrowser for help on using the repository browser.