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