auth/credentials: add cli_credentials_set_utf16_password()
[samba.git] / auth / credentials / credentials.c
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 "auth/credentials/credentials_internal.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "tevent.h"
30 #include "param/param.h"
31 #include "system/filesys.h"
32
33 /**
34  * Create a new credentials structure
35  * @param mem_ctx TALLOC_CTX parent for credentials structure 
36  */
37 _PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx) 
38 {
39         struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
40         if (cred == NULL) {
41                 return cred;
42         }
43
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;
54
55         cred->ccache_threshold = CRED_UNINITIALISED;
56         cred->client_gss_creds_threshold = CRED_UNINITIALISED;
57
58         cred->workstation = NULL;
59         cred->username = NULL;
60         cred->password = NULL;
61         cred->old_password = NULL;
62         cred->domain = NULL;
63         cred->realm = 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;
69
70         cred->bind_dn = NULL;
71
72         cred->nt_hash = NULL;
73
74         cred->lm_response.data = NULL;
75         cred->lm_response.length = 0;
76         cred->nt_response.data = NULL;
77         cred->nt_response.length = 0;
78
79         cred->ccache = NULL;
80         cred->client_gss_creds = NULL;
81         cred->keytab = NULL;
82         cred->server_gss_creds = NULL;
83
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;
90
91         cred->priv_data = NULL;
92
93         cred->netlogon_creds = NULL;
94         cred->secure_channel_type = SEC_CHAN_NULL;
95
96         cred->kvno = 0;
97
98         cred->password_last_changed_time = 0;
99
100         cred->smb_krb5_context = NULL;
101
102         cred->machine_account_pending = false;
103         cred->machine_account_pending_lp_ctx = NULL;
104
105         cred->machine_account = false;
106
107         cred->password_tries = 0;
108
109         cred->callback_running = false;
110
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);
114
115         cred->forced_sasl_mech = NULL;
116
117         return cred;
118 }
119
120 _PUBLIC_ void cli_credentials_set_callback_data(struct cli_credentials *cred,
121                                                 void *callback_data)
122 {
123         cred->priv_data = callback_data;
124 }
125
126 _PUBLIC_ void *_cli_credentials_callback_data(struct cli_credentials *cred)
127 {
128         return cred->priv_data;
129 }
130
131 _PUBLIC_ struct cli_credentials *cli_credentials_shallow_copy(TALLOC_CTX *mem_ctx,
132                                                 struct cli_credentials *src)
133 {
134         struct cli_credentials *dst;
135
136         dst = talloc(mem_ctx, struct cli_credentials);
137         if (dst == NULL) {
138                 return NULL;
139         }
140
141         *dst = *src;
142
143         return dst;
144 }
145
146 /**
147  * Create a new anonymous credential
148  * @param mem_ctx TALLOC_CTX parent for credentials structure 
149  */
150 _PUBLIC_ struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx)
151 {
152         struct cli_credentials *anon_credentials;
153
154         anon_credentials = cli_credentials_init(mem_ctx);
155         cli_credentials_set_anonymous(anon_credentials);
156
157         return anon_credentials;
158 }
159
160 _PUBLIC_ void cli_credentials_set_kerberos_state(struct cli_credentials *creds, 
161                                         enum credentials_use_kerberos use_kerberos)
162 {
163         creds->use_kerberos = use_kerberos;
164 }
165
166 _PUBLIC_ void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
167                                                    const char *sasl_mech)
168 {
169         TALLOC_FREE(creds->forced_sasl_mech);
170         creds->forced_sasl_mech = talloc_strdup(creds, sasl_mech);
171 }
172
173 _PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
174                                                   enum credentials_krb_forwardable krb_forwardable)
175 {
176         creds->krb_forwardable = krb_forwardable;
177 }
178
179 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
180 {
181         return creds->use_kerberos;
182 }
183
184 _PUBLIC_ const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *creds)
185 {
186         return creds->forced_sasl_mech;
187 }
188
189 _PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
190 {
191         return creds->krb_forwardable;
192 }
193
194 _PUBLIC_ void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features)
195 {
196         creds->gensec_features = gensec_features;
197 }
198
199 _PUBLIC_ uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds)
200 {
201         return creds->gensec_features;
202 }
203
204
205 /**
206  * Obtain the username for this credentials context.
207  * @param cred credentials context
208  * @retval The username set on this context.
209  * @note Return value will never be NULL except by programmer error.
210  */
211 _PUBLIC_ const char *cli_credentials_get_username(struct cli_credentials *cred)
212 {
213         if (cred->machine_account_pending) {
214                 cli_credentials_set_machine_account(cred, 
215                                         cred->machine_account_pending_lp_ctx);
216         }
217
218         if (cred->username_obtained == CRED_CALLBACK && 
219             !cred->callback_running) {
220                 cred->callback_running = true;
221                 cred->username = cred->username_cb(cred);
222                 cred->callback_running = false;
223                 if (cred->username_obtained == CRED_CALLBACK) {
224                         cred->username_obtained = CRED_CALLBACK_RESULT;
225                         cli_credentials_invalidate_ccache(cred, cred->username_obtained);
226                 }
227         }
228
229         return cred->username;
230 }
231
232 _PUBLIC_ bool cli_credentials_set_username(struct cli_credentials *cred, 
233                                   const char *val, enum credentials_obtained obtained)
234 {
235         if (obtained >= cred->username_obtained) {
236                 cred->username = talloc_strdup(cred, val);
237                 cred->username_obtained = obtained;
238                 cli_credentials_invalidate_ccache(cred, cred->username_obtained);
239                 return true;
240         }
241
242         return false;
243 }
244
245 _PUBLIC_ bool cli_credentials_set_username_callback(struct cli_credentials *cred,
246                                   const char *(*username_cb) (struct cli_credentials *))
247 {
248         if (cred->username_obtained < CRED_CALLBACK) {
249                 cred->username_cb = username_cb;
250                 cred->username_obtained = CRED_CALLBACK;
251                 return true;
252         }
253
254         return false;
255 }
256
257 _PUBLIC_ bool cli_credentials_set_bind_dn(struct cli_credentials *cred, 
258                                  const char *bind_dn)
259 {
260         cred->bind_dn = talloc_strdup(cred, bind_dn);
261         return true;
262 }
263
264 /**
265  * Obtain the BIND DN for this credentials context.
266  * @param cred credentials context
267  * @retval The username set on this context.
268  * @note Return value will be NULL if not specified explictly
269  */
270 _PUBLIC_ const char *cli_credentials_get_bind_dn(struct cli_credentials *cred)
271 {
272         return cred->bind_dn;
273 }
274
275
276 /**
277  * Obtain the client principal for this credentials context.
278  * @param cred credentials context
279  * @retval The username set on this context.
280  * @note Return value will never be NULL except by programmer error.
281  */
282 _PUBLIC_ const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
283 {
284         if (cred->machine_account_pending) {
285                 cli_credentials_set_machine_account(cred,
286                                         cred->machine_account_pending_lp_ctx);
287         }
288
289         if (cred->principal_obtained == CRED_CALLBACK && 
290             !cred->callback_running) {
291                 cred->callback_running = true;
292                 cred->principal = cred->principal_cb(cred);
293                 cred->callback_running = false;
294                 if (cred->principal_obtained == CRED_CALLBACK) {
295                         cred->principal_obtained = CRED_CALLBACK_RESULT;
296                         cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
297                 }
298         }
299
300         if (cred->principal_obtained < cred->username_obtained
301             || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
302                 if (cred->domain_obtained > cred->realm_obtained) {
303                         *obtained = MIN(cred->domain_obtained, cred->username_obtained);
304                         return talloc_asprintf(mem_ctx, "%s@%s", 
305                                                cli_credentials_get_username(cred),
306                                                cli_credentials_get_domain(cred));
307                 } else {
308                         *obtained = MIN(cred->domain_obtained, cred->username_obtained);
309                         return talloc_asprintf(mem_ctx, "%s@%s", 
310                                                cli_credentials_get_username(cred),
311                                                cli_credentials_get_realm(cred));
312                 }
313         }
314         *obtained = cred->principal_obtained;
315         return talloc_strdup(mem_ctx, cred->principal);
316 }
317
318 /**
319  * Obtain the client principal for this credentials context.
320  * @param cred credentials context
321  * @retval The username set on this context.
322  * @note Return value will never be NULL except by programmer error.
323  */
324 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
325 {
326         enum credentials_obtained obtained;
327         return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
328 }
329
330 _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred, 
331                                    const char *val, 
332                                    enum credentials_obtained obtained)
333 {
334         if (obtained >= cred->principal_obtained) {
335                 cred->principal = talloc_strdup(cred, val);
336                 cred->principal_obtained = obtained;
337                 cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
338                 return true;
339         }
340
341         return false;
342 }
343
344 /* Set a callback to get the principal.  This could be a popup dialog,
345  * a terminal prompt or similar.  */
346 _PUBLIC_ bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
347                                   const char *(*principal_cb) (struct cli_credentials *))
348 {
349         if (cred->principal_obtained < CRED_CALLBACK) {
350                 cred->principal_cb = principal_cb;
351                 cred->principal_obtained = CRED_CALLBACK;
352                 return true;
353         }
354
355         return false;
356 }
357
358 /* Some of our tools are 'anonymous by default'.  This is a single
359  * function to determine if authentication has been explicitly
360  * requested */
361
362 _PUBLIC_ bool cli_credentials_authentication_requested(struct cli_credentials *cred) 
363 {
364         if (cred->bind_dn) {
365                 return true;
366         }
367
368         /*
369          * If we forced the mech we clearly want authentication. E.g. to use
370          * SASL/EXTERNAL which has no credentials.
371          */
372         if (cred->forced_sasl_mech) {
373                 return true;
374         }
375
376         if (cli_credentials_is_anonymous(cred)){
377                 return false;
378         }
379
380         if (cred->principal_obtained >= CRED_SPECIFIED) {
381                 return true;
382         }
383         if (cred->username_obtained >= CRED_SPECIFIED) {
384                 return true;
385         }
386
387         if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
388                 return true;
389         }
390
391         return false;
392 }
393
394 /**
395  * Obtain the password for this credentials context.
396  * @param cred credentials context
397  * @retval If set, the cleartext password, otherwise NULL
398  */
399 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
400 {
401         if (cred->machine_account_pending) {
402                 cli_credentials_set_machine_account(cred,
403                                                     cred->machine_account_pending_lp_ctx);
404         }
405
406         if (cred->password_obtained == CRED_CALLBACK && 
407             !cred->callback_running) {
408                 cred->callback_running = true;
409                 cred->password = cred->password_cb(cred);
410                 cred->callback_running = false;
411                 if (cred->password_obtained == CRED_CALLBACK) {
412                         cred->password_obtained = CRED_CALLBACK_RESULT;
413                         cli_credentials_invalidate_ccache(cred, cred->password_obtained);
414                 }
415         }
416
417         return cred->password;
418 }
419
420 /* Set a password on the credentials context, including an indication
421  * of 'how' the password was obtained */
422
423 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred, 
424                                   const char *val, 
425                                   enum credentials_obtained obtained)
426 {
427         if (obtained >= cred->password_obtained) {
428                 cred->password_tries = 0;
429                 cred->password = talloc_strdup(cred, val);
430                 if (cred->password) {
431                         /* Don't print the actual password in talloc memory dumps */
432                         talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
433                 }
434                 cred->password_obtained = obtained;
435                 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
436
437                 cred->nt_hash = NULL;
438                 cred->lm_response = data_blob(NULL, 0);
439                 cred->nt_response = data_blob(NULL, 0);
440                 return true;
441         }
442
443         return false;
444 }
445
446 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
447                                            const char *(*password_cb) (struct cli_credentials *))
448 {
449         if (cred->password_obtained < CRED_CALLBACK) {
450                 cred->password_tries = 3;
451                 cred->password_cb = password_cb;
452                 cred->password_obtained = CRED_CALLBACK;
453                 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
454                 return true;
455         }
456
457         return false;
458 }
459
460 /**
461  * Obtain the 'old' password for this credentials context (used for join accounts).
462  * @param cred credentials context
463  * @retval If set, the cleartext password, otherwise NULL
464  */
465 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
466 {
467         if (cred->machine_account_pending) {
468                 cli_credentials_set_machine_account(cred,
469                                                     cred->machine_account_pending_lp_ctx);
470         }
471
472         return cred->old_password;
473 }
474
475 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred, 
476                                       const char *val, 
477                                       enum credentials_obtained obtained)
478 {
479         cred->old_password = talloc_strdup(cred, val);
480         if (cred->old_password) {
481                 /* Don't print the actual password in talloc memory dumps */
482                 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
483         }
484         return true;
485 }
486
487 /**
488  * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
489  *
490  * Sometimes we only have this much of the password, while the rest of
491  * the time this call avoids calling E_md4hash themselves.
492  *
493  * @param cred credentials context
494  * @retval If set, the cleartext password, otherwise NULL
495  */
496 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
497                                                            TALLOC_CTX *mem_ctx)
498 {
499         const char *password = NULL;
500
501         if (cred->nt_hash != NULL) {
502                 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
503                 if (!nt_hash) {
504                         return NULL;
505                 }
506
507                 *nt_hash = *cred->nt_hash;
508
509                 return nt_hash;
510         }
511
512         password = cli_credentials_get_password(cred);
513         if (password) {
514                 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
515                 if (!nt_hash) {
516                         return NULL;
517                 }
518
519                 E_md4hash(password, nt_hash->hash);
520
521                 return nt_hash;
522         }
523
524         return NULL;
525 }
526
527 /**
528  * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
529  * @param cred credentials context
530  * @retval The domain set on this context. 
531  * @note Return value will never be NULL except by programmer error.
532  */
533 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
534 {
535         if (cred->machine_account_pending) {
536                 cli_credentials_set_machine_account(cred,
537                                                     cred->machine_account_pending_lp_ctx);
538         }
539
540         if (cred->domain_obtained == CRED_CALLBACK && 
541             !cred->callback_running) {
542                 cred->callback_running = true;
543                 cred->domain = cred->domain_cb(cred);
544                 cred->callback_running = false;
545                 if (cred->domain_obtained == CRED_CALLBACK) {
546                         cred->domain_obtained = CRED_CALLBACK_RESULT;
547                         cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
548                 }
549         }
550
551         return cred->domain;
552 }
553
554
555 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred, 
556                                 const char *val, 
557                                 enum credentials_obtained obtained)
558 {
559         if (obtained >= cred->domain_obtained) {
560                 /* it is important that the domain be in upper case,
561                  * particularly for the sensitive NTLMv2
562                  * calculations */
563                 cred->domain = strupper_talloc(cred, val);
564                 cred->domain_obtained = obtained;
565                 /* setting domain does not mean we have to invalidate ccache 
566                  * because domain in not used for Kerberos operations.
567                  * If ccache invalidation is required, one will anyway specify
568                  * a password to kinit, and that will force invalidation of the ccache
569                  */
570                 return true;
571         }
572
573         return false;
574 }
575
576 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
577                                          const char *(*domain_cb) (struct cli_credentials *))
578 {
579         if (cred->domain_obtained < CRED_CALLBACK) {
580                 cred->domain_cb = domain_cb;
581                 cred->domain_obtained = CRED_CALLBACK;
582                 return true;
583         }
584
585         return false;
586 }
587
588 /**
589  * Obtain the Kerberos realm for this credentials context.
590  * @param cred credentials context
591  * @retval The realm set on this context. 
592  * @note Return value will never be NULL except by programmer error.
593  */
594 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
595 {       
596         if (cred->machine_account_pending) {
597                 cli_credentials_set_machine_account(cred,
598                                                     cred->machine_account_pending_lp_ctx);
599         }
600
601         if (cred->realm_obtained == CRED_CALLBACK && 
602             !cred->callback_running) {
603                 cred->callback_running = true;
604                 cred->realm = cred->realm_cb(cred);
605                 cred->callback_running = false;
606                 if (cred->realm_obtained == CRED_CALLBACK) {
607                         cred->realm_obtained = CRED_CALLBACK_RESULT;
608                         cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
609                 }
610         }
611
612         return cred->realm;
613 }
614
615 /**
616  * Set the realm for this credentials context, and force it to
617  * uppercase for the sainity of our local kerberos libraries 
618  */
619 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred, 
620                                const char *val, 
621                                enum credentials_obtained obtained)
622 {
623         if (obtained >= cred->realm_obtained) {
624                 cred->realm = strupper_talloc(cred, val);
625                 cred->realm_obtained = obtained;
626                 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
627                 return true;
628         }
629
630         return false;
631 }
632
633 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
634                                         const char *(*realm_cb) (struct cli_credentials *))
635 {
636         if (cred->realm_obtained < CRED_CALLBACK) {
637                 cred->realm_cb = realm_cb;
638                 cred->realm_obtained = CRED_CALLBACK;
639                 return true;
640         }
641
642         return false;
643 }
644
645 /**
646  * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
647  *
648  * @param cred credentials context
649  * @retval The workstation name set on this context. 
650  * @note Return value will never be NULL except by programmer error.
651  */
652 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
653 {
654         if (cred->workstation_obtained == CRED_CALLBACK && 
655             !cred->callback_running) {
656                 cred->callback_running = true;
657                 cred->workstation = cred->workstation_cb(cred);
658                 cred->callback_running = false;
659                 if (cred->workstation_obtained == CRED_CALLBACK) {
660                         cred->workstation_obtained = CRED_CALLBACK_RESULT;
661                 }
662         }
663
664         return cred->workstation;
665 }
666
667 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred, 
668                                      const char *val, 
669                                      enum credentials_obtained obtained)
670 {
671         if (obtained >= cred->workstation_obtained) {
672                 cred->workstation = talloc_strdup(cred, val);
673                 cred->workstation_obtained = obtained;
674                 return true;
675         }
676
677         return false;
678 }
679
680 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
681                                               const char *(*workstation_cb) (struct cli_credentials *))
682 {
683         if (cred->workstation_obtained < CRED_CALLBACK) {
684                 cred->workstation_cb = workstation_cb;
685                 cred->workstation_obtained = CRED_CALLBACK;
686                 return true;
687         }
688
689         return false;
690 }
691
692 /**
693  * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
694  *
695  * The format accepted is [domain\\]user[%password] or user[@realm][%password]
696  *
697  * @param credentials Credentials structure on which to set the password
698  * @param data the string containing the username, password etc
699  * @param obtained This enum describes how 'specified' this password is
700  */
701
702 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
703 {
704         char *uname, *p;
705
706         if (strcmp("%",data) == 0) {
707                 cli_credentials_set_anonymous(credentials);
708                 return;
709         }
710
711         uname = talloc_strdup(credentials, data); 
712         if ((p = strchr_m(uname,'%'))) {
713                 *p = 0;
714                 cli_credentials_set_password(credentials, p+1, obtained);
715         }
716
717         if ((p = strchr_m(uname,'@'))) {
718                 cli_credentials_set_principal(credentials, uname, obtained);
719                 *p = 0;
720                 cli_credentials_set_realm(credentials, p+1, obtained);
721                 return;
722         } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
723                 *p = 0;
724                 cli_credentials_set_domain(credentials, uname, obtained);
725                 uname = p+1;
726         }
727         cli_credentials_set_username(credentials, uname, obtained);
728 }
729
730 /**
731  * Given a a credentials structure, print it as a string
732  *
733  * The format output is [domain\\]user[%password] or user[@realm][%password]
734  *
735  * @param credentials Credentials structure on which to set the password
736  * @param mem_ctx The memory context to place the result on
737  */
738
739 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
740 {
741         const char *bind_dn = cli_credentials_get_bind_dn(credentials);
742         const char *domain;
743         const char *username;
744         const char *name;
745
746         if (bind_dn) {
747                 name = talloc_strdup(mem_ctx, bind_dn);
748         } else {
749                 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
750                 if (domain && domain[0]) {
751                         name = talloc_asprintf(mem_ctx, "%s\\%s", 
752                                                domain, username);
753                 } else {
754                         name = talloc_asprintf(mem_ctx, "%s", 
755                                                username);
756                 }
757         }
758         return name;
759 }
760
761 /**
762  * Specifies default values for domain, workstation and realm
763  * from the smb.conf configuration file
764  *
765  * @param cred Credentials structure to fill in
766  */
767 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred, 
768                               struct loadparm_context *lp_ctx)
769 {
770         cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
771         if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
772                 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
773         } else {
774                 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
775         }
776         if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
777                 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
778         } else {
779                 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
780         }
781         if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
782                 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
783         } else {
784                 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
785         }
786 }
787
788 /**
789  * Guess defaults for credentials from environment variables, 
790  * and from the configuration file
791  * 
792  * @param cred Credentials structure to fill in
793  */
794 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
795                            struct loadparm_context *lp_ctx)
796 {
797         char *p;
798         const char *error_string;
799
800         if (lp_ctx != NULL) {
801                 cli_credentials_set_conf(cred, lp_ctx);
802         }
803         
804         if (getenv("LOGNAME")) {
805                 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
806         }
807
808         if (getenv("USER")) {
809                 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
810                 if ((p = strchr_m(getenv("USER"),'%'))) {
811                         memset(p,0,strlen(cred->password));
812                 }
813         }
814
815         if (getenv("PASSWD")) {
816                 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
817         }
818
819         if (getenv("PASSWD_FD")) {
820                 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), 
821                                                   CRED_GUESS_FILE);
822         }
823         
824         p = getenv("PASSWD_FILE");
825         if (p && p[0]) {
826                 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
827         }
828         
829         if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
830                 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
831                                            &error_string);
832         }
833 }
834
835 /**
836  * Attach NETLOGON credentials for use with SCHANNEL
837  */
838
839 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred, 
840                                                  struct netlogon_creds_CredentialState *netlogon_creds)
841 {
842         TALLOC_FREE(cred->netlogon_creds);
843         if (netlogon_creds == NULL) {
844                 return;
845         }
846         cred->netlogon_creds = netlogon_creds_copy(cred, netlogon_creds);
847 }
848
849 /**
850  * Return attached NETLOGON credentials 
851  */
852
853 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
854 {
855         return cred->netlogon_creds;
856 }
857
858 /** 
859  * Set NETLOGON secure channel type
860  */
861
862 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
863                                              enum netr_SchannelType secure_channel_type)
864 {
865         cred->secure_channel_type = secure_channel_type;
866 }
867
868 /**
869  * Return NETLOGON secure chanel type
870  */
871
872 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
873 {
874         return cred->password_last_changed_time;
875 }
876
877 /** 
878  * Set NETLOGON secure channel type
879  */
880
881 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
882                                                              time_t last_changed_time)
883 {
884         cred->password_last_changed_time = last_changed_time;
885 }
886
887 /**
888  * Return NETLOGON secure chanel type
889  */
890
891 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
892 {
893         return cred->secure_channel_type;
894 }
895
896 /**
897  * Fill in a credentials structure as the anonymous user
898  */
899 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred) 
900 {
901         cli_credentials_set_username(cred, "", CRED_SPECIFIED);
902         cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
903         cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
904         cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
905         cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
906 }
907
908 /**
909  * Describe a credentials context as anonymous or authenticated
910  * @retval true if anonymous, false if a username is specified
911  */
912
913 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
914 {
915         const char *username;
916         
917         /* if bind dn is set it's not anonymous */
918         if (cred->bind_dn) {
919                 return false;
920         }
921
922         if (cred->machine_account_pending) {
923                 cli_credentials_set_machine_account(cred,
924                                                     cred->machine_account_pending_lp_ctx);
925         }
926
927         username = cli_credentials_get_username(cred);
928         
929         /* Yes, it is deliberate that we die if we have a NULL pointer
930          * here - anonymous is "", not NULL, which is 'never specified,
931          * never guessed', ie programmer bug */
932         if (!username[0]) {
933                 return true;
934         }
935
936         return false;
937 }
938
939 /**
940  * Mark the current password for a credentials struct as wrong. This will 
941  * cause the password to be prompted again (if a callback is set).
942  *
943  * This will decrement the number of times the password can be tried.
944  *
945  * @retval whether the credentials struct is finished
946  */
947 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
948 {
949         if (cred->password_obtained != CRED_CALLBACK_RESULT) {
950                 return false;
951         }
952
953         if (cred->password_tries == 0) {
954                 return false;
955         }
956
957         cred->password_tries--;
958
959         if (cred->password_tries == 0) {
960                 return false;
961         }
962
963         cred->password_obtained = CRED_CALLBACK;
964         return true;
965 }
966
967 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
968                                               const char **username, 
969                                               const char **domain) 
970 {
971         if (cred->principal_obtained > cred->username_obtained) {
972                 *domain = talloc_strdup(mem_ctx, "");
973                 *username = cli_credentials_get_principal(cred, mem_ctx);
974         } else {
975                 *domain = cli_credentials_get_domain(cred);
976                 *username = cli_credentials_get_username(cred);
977         }
978 }
979
980 /**
981  * Read a named file, and parse it for username, domain, realm and password
982  *
983  * @param credentials Credentials structure on which to set the password
984  * @param file a named file to read the details from 
985  * @param obtained This enum describes how 'specified' this password is
986  */
987
988 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained) 
989 {
990         uint16_t len = 0;
991         char *ptr, *val, *param;
992         char **lines;
993         int i, numlines;
994
995         lines = file_lines_load(file, &numlines, 0, NULL);
996
997         if (lines == NULL)
998         {
999                 /* fail if we can't open the credentials file */
1000                 d_printf("ERROR: Unable to open credentials file!\n");
1001                 return false;
1002         }
1003
1004         for (i = 0; i < numlines; i++) {
1005                 len = strlen(lines[i]);
1006
1007                 if (len == 0)
1008                         continue;
1009
1010                 /* break up the line into parameter & value.
1011                  * will need to eat a little whitespace possibly */
1012                 param = lines[i];
1013                 if (!(ptr = strchr_m (lines[i], '=')))
1014                         continue;
1015
1016                 val = ptr+1;
1017                 *ptr = '\0';
1018
1019                 /* eat leading white space */
1020                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1021                         val++;
1022
1023                 if (strwicmp("password", param) == 0) {
1024                         cli_credentials_set_password(cred, val, obtained);
1025                 } else if (strwicmp("username", param) == 0) {
1026                         cli_credentials_set_username(cred, val, obtained);
1027                 } else if (strwicmp("domain", param) == 0) {
1028                         cli_credentials_set_domain(cred, val, obtained);
1029                 } else if (strwicmp("realm", param) == 0) {
1030                         cli_credentials_set_realm(cred, val, obtained);
1031                 }
1032                 memset(lines[i], 0, len);
1033         }
1034
1035         talloc_free(lines);
1036
1037         return true;
1038 }
1039
1040 /**
1041  * Read a named file, and parse it for a password
1042  *
1043  * @param credentials Credentials structure on which to set the password
1044  * @param file a named file to read the password from 
1045  * @param obtained This enum describes how 'specified' this password is
1046  */
1047
1048 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1049 {
1050         int fd = open(file, O_RDONLY, 0);
1051         bool ret;
1052
1053         if (fd < 0) {
1054                 fprintf(stderr, "Error opening password file %s: %s\n",
1055                                 file, strerror(errno));
1056                 return false;
1057         }
1058
1059         ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1060
1061         close(fd);
1062         
1063         return ret;
1064 }
1065
1066
1067 /**
1068  * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1069  *
1070  * @param credentials Credentials structure on which to set the password
1071  * @param fd open file descriptor to read the password from 
1072  * @param obtained This enum describes how 'specified' this password is
1073  */
1074
1075 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials, 
1076                                        int fd, enum credentials_obtained obtained)
1077 {
1078         char *p;
1079         char pass[128];
1080
1081         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1082                 p && p - pass < sizeof(pass);) {
1083                 switch (read(fd, p, 1)) {
1084                 case 1:
1085                         if (*p != '\n' && *p != '\0') {
1086                                 *++p = '\0'; /* advance p, and null-terminate pass */
1087                                 break;
1088                         }
1089                         /* fall through */
1090                 case 0:
1091                         if (p - pass) {
1092                                 *p = '\0'; /* null-terminate it, just in case... */
1093                                 p = NULL; /* then force the loop condition to become false */
1094                                 break;
1095                         } else {
1096                                 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1097                                 return false;
1098                         }
1099
1100                 default:
1101                         fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1102                                         fd, strerror(errno));
1103                         return false;
1104                 }
1105         }
1106
1107         cli_credentials_set_password(credentials, pass, obtained);
1108         return true;
1109 }
1110
1111