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