auth/credentials: keep cli_credentials private
[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->tries = 3;
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 = talloc_strdup(cred, val);
401                 if (cred->password) {
402                         /* Don't print the actual password in talloc memory dumps */
403                         talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
404                 }
405                 cred->password_obtained = obtained;
406                 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
407
408                 cred->nt_hash = NULL;
409                 cred->lm_response = data_blob(NULL, 0);
410                 cred->nt_response = data_blob(NULL, 0);
411                 return true;
412         }
413
414         return false;
415 }
416
417 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
418                                            const char *(*password_cb) (struct cli_credentials *))
419 {
420         if (cred->password_obtained < CRED_CALLBACK) {
421                 cred->password_cb = password_cb;
422                 cred->password_obtained = CRED_CALLBACK;
423                 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
424                 return true;
425         }
426
427         return false;
428 }
429
430 /**
431  * Obtain the 'old' password for this credentials context (used for join accounts).
432  * @param cred credentials context
433  * @retval If set, the cleartext password, otherwise NULL
434  */
435 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
436 {
437         if (cred->machine_account_pending) {
438                 cli_credentials_set_machine_account(cred,
439                                                     cred->machine_account_pending_lp_ctx);
440         }
441
442         return cred->old_password;
443 }
444
445 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred, 
446                                       const char *val, 
447                                       enum credentials_obtained obtained)
448 {
449         cred->old_password = talloc_strdup(cred, val);
450         if (cred->old_password) {
451                 /* Don't print the actual password in talloc memory dumps */
452                 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
453         }
454         return true;
455 }
456
457 /**
458  * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
459  *
460  * Sometimes we only have this much of the password, while the rest of
461  * the time this call avoids calling E_md4hash themselves.
462  *
463  * @param cred credentials context
464  * @retval If set, the cleartext password, otherwise NULL
465  */
466 _PUBLIC_ const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred, 
467                                                         TALLOC_CTX *mem_ctx)
468 {
469         const char *password = cli_credentials_get_password(cred);
470
471         if (password) {
472                 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
473                 if (!nt_hash) {
474                         return NULL;
475                 }
476                 
477                 E_md4hash(password, nt_hash->hash);    
478
479                 return nt_hash;
480         } else {
481                 return cred->nt_hash;
482         }
483 }
484
485 /**
486  * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
487  * @param cred credentials context
488  * @retval The domain set on this context. 
489  * @note Return value will never be NULL except by programmer error.
490  */
491 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
492 {
493         if (cred->machine_account_pending) {
494                 cli_credentials_set_machine_account(cred,
495                                                     cred->machine_account_pending_lp_ctx);
496         }
497
498         if (cred->domain_obtained == CRED_CALLBACK && 
499             !cred->callback_running) {
500                 cred->callback_running = true;
501                 cred->domain = cred->domain_cb(cred);
502                 cred->callback_running = false;
503                 cred->domain_obtained = CRED_SPECIFIED;
504                 cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
505         }
506
507         return cred->domain;
508 }
509
510
511 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred, 
512                                 const char *val, 
513                                 enum credentials_obtained obtained)
514 {
515         if (obtained >= cred->domain_obtained) {
516                 /* it is important that the domain be in upper case,
517                  * particularly for the sensitive NTLMv2
518                  * calculations */
519                 cred->domain = strupper_talloc(cred, val);
520                 cred->domain_obtained = obtained;
521                 /* setting domain does not mean we have to invalidate ccache 
522                  * because domain in not used for Kerberos operations.
523                  * If ccache invalidation is required, one will anyway specify
524                  * a password to kinit, and that will force invalidation of the ccache
525                  */
526                 return true;
527         }
528
529         return false;
530 }
531
532 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
533                                          const char *(*domain_cb) (struct cli_credentials *))
534 {
535         if (cred->domain_obtained < CRED_CALLBACK) {
536                 cred->domain_cb = domain_cb;
537                 cred->domain_obtained = CRED_CALLBACK;
538                 return true;
539         }
540
541         return false;
542 }
543
544 /**
545  * Obtain the Kerberos realm for this credentials context.
546  * @param cred credentials context
547  * @retval The realm set on this context. 
548  * @note Return value will never be NULL except by programmer error.
549  */
550 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
551 {       
552         if (cred->machine_account_pending) {
553                 cli_credentials_set_machine_account(cred,
554                                                     cred->machine_account_pending_lp_ctx);
555         }
556
557         if (cred->realm_obtained == CRED_CALLBACK && 
558             !cred->callback_running) {
559                 cred->callback_running = true;
560                 cred->realm = cred->realm_cb(cred);
561                 cred->callback_running = false;
562                 cred->realm_obtained = CRED_SPECIFIED;
563                 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
564         }
565
566         return cred->realm;
567 }
568
569 /**
570  * Set the realm for this credentials context, and force it to
571  * uppercase for the sainity of our local kerberos libraries 
572  */
573 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred, 
574                                const char *val, 
575                                enum credentials_obtained obtained)
576 {
577         if (obtained >= cred->realm_obtained) {
578                 cred->realm = strupper_talloc(cred, val);
579                 cred->realm_obtained = obtained;
580                 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
581                 return true;
582         }
583
584         return false;
585 }
586
587 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
588                                         const char *(*realm_cb) (struct cli_credentials *))
589 {
590         if (cred->realm_obtained < CRED_CALLBACK) {
591                 cred->realm_cb = realm_cb;
592                 cred->realm_obtained = CRED_CALLBACK;
593                 return true;
594         }
595
596         return false;
597 }
598
599 /**
600  * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
601  *
602  * @param cred credentials context
603  * @retval The workstation name set on this context. 
604  * @note Return value will never be NULL except by programmer error.
605  */
606 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
607 {
608         if (cred->workstation_obtained == CRED_CALLBACK && 
609             !cred->callback_running) {
610                 cred->callback_running = true;
611                 cred->workstation = cred->workstation_cb(cred);
612                 cred->callback_running = false;
613                 cred->workstation_obtained = CRED_SPECIFIED;
614         }
615
616         return cred->workstation;
617 }
618
619 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred, 
620                                      const char *val, 
621                                      enum credentials_obtained obtained)
622 {
623         if (obtained >= cred->workstation_obtained) {
624                 cred->workstation = talloc_strdup(cred, val);
625                 cred->workstation_obtained = obtained;
626                 return true;
627         }
628
629         return false;
630 }
631
632 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
633                                               const char *(*workstation_cb) (struct cli_credentials *))
634 {
635         if (cred->workstation_obtained < CRED_CALLBACK) {
636                 cred->workstation_cb = workstation_cb;
637                 cred->workstation_obtained = CRED_CALLBACK;
638                 return true;
639         }
640
641         return false;
642 }
643
644 /**
645  * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
646  *
647  * The format accepted is [domain\\]user[%password] or user[@realm][%password]
648  *
649  * @param credentials Credentials structure on which to set the password
650  * @param data the string containing the username, password etc
651  * @param obtained This enum describes how 'specified' this password is
652  */
653
654 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
655 {
656         char *uname, *p;
657
658         if (strcmp("%",data) == 0) {
659                 cli_credentials_set_anonymous(credentials);
660                 return;
661         }
662
663         uname = talloc_strdup(credentials, data); 
664         if ((p = strchr_m(uname,'%'))) {
665                 *p = 0;
666                 cli_credentials_set_password(credentials, p+1, obtained);
667         }
668
669         if ((p = strchr_m(uname,'@'))) {
670                 cli_credentials_set_principal(credentials, uname, obtained);
671                 *p = 0;
672                 cli_credentials_set_realm(credentials, p+1, obtained);
673                 return;
674         } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
675                 *p = 0;
676                 cli_credentials_set_domain(credentials, uname, obtained);
677                 uname = p+1;
678         }
679         cli_credentials_set_username(credentials, uname, obtained);
680 }
681
682 /**
683  * Given a a credentials structure, print it as a string
684  *
685  * The format output is [domain\\]user[%password] or user[@realm][%password]
686  *
687  * @param credentials Credentials structure on which to set the password
688  * @param mem_ctx The memory context to place the result on
689  */
690
691 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
692 {
693         const char *bind_dn = cli_credentials_get_bind_dn(credentials);
694         const char *domain;
695         const char *username;
696         const char *name;
697
698         if (bind_dn) {
699                 name = talloc_strdup(mem_ctx, bind_dn);
700         } else {
701                 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
702                 if (domain && domain[0]) {
703                         name = talloc_asprintf(mem_ctx, "%s\\%s", 
704                                                domain, username);
705                 } else {
706                         name = talloc_asprintf(mem_ctx, "%s", 
707                                                username);
708                 }
709         }
710         return name;
711 }
712
713 /**
714  * Specifies default values for domain, workstation and realm
715  * from the smb.conf configuration file
716  *
717  * @param cred Credentials structure to fill in
718  */
719 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred, 
720                               struct loadparm_context *lp_ctx)
721 {
722         cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
723         if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
724                 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
725         } else {
726                 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
727         }
728         if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
729                 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
730         } else {
731                 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
732         }
733         if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
734                 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
735         } else {
736                 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
737         }
738 }
739
740 /**
741  * Guess defaults for credentials from environment variables, 
742  * and from the configuration file
743  * 
744  * @param cred Credentials structure to fill in
745  */
746 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
747                            struct loadparm_context *lp_ctx)
748 {
749         char *p;
750         const char *error_string;
751
752         if (lp_ctx != NULL) {
753                 cli_credentials_set_conf(cred, lp_ctx);
754         }
755         
756         if (getenv("LOGNAME")) {
757                 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
758         }
759
760         if (getenv("USER")) {
761                 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
762                 if ((p = strchr_m(getenv("USER"),'%'))) {
763                         memset(p,0,strlen(cred->password));
764                 }
765         }
766
767         if (getenv("PASSWD")) {
768                 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
769         }
770
771         if (getenv("PASSWD_FD")) {
772                 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), 
773                                                   CRED_GUESS_FILE);
774         }
775         
776         p = getenv("PASSWD_FILE");
777         if (p && p[0]) {
778                 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
779         }
780         
781         if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
782                 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
783                                            &error_string);
784         }
785 }
786
787 /**
788  * Attach NETLOGON credentials for use with SCHANNEL
789  */
790
791 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred, 
792                                                  struct netlogon_creds_CredentialState *netlogon_creds)
793 {
794         cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
795 }
796
797 /**
798  * Return attached NETLOGON credentials 
799  */
800
801 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
802 {
803         return cred->netlogon_creds;
804 }
805
806 /** 
807  * Set NETLOGON secure channel type
808  */
809
810 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
811                                              enum netr_SchannelType secure_channel_type)
812 {
813         cred->secure_channel_type = secure_channel_type;
814 }
815
816 /**
817  * Return NETLOGON secure chanel type
818  */
819
820 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
821 {
822         return cred->password_last_changed_time;
823 }
824
825 /** 
826  * Set NETLOGON secure channel type
827  */
828
829 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
830                                                              time_t last_changed_time)
831 {
832         cred->password_last_changed_time = last_changed_time;
833 }
834
835 /**
836  * Return NETLOGON secure chanel type
837  */
838
839 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
840 {
841         return cred->secure_channel_type;
842 }
843
844 /**
845  * Fill in a credentials structure as the anonymous user
846  */
847 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred) 
848 {
849         cli_credentials_set_username(cred, "", CRED_SPECIFIED);
850         cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
851         cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
852         cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
853         cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
854 }
855
856 /**
857  * Describe a credentials context as anonymous or authenticated
858  * @retval true if anonymous, false if a username is specified
859  */
860
861 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
862 {
863         const char *username;
864         
865         /* if bind dn is set it's not anonymous */
866         if (cred->bind_dn) {
867                 return false;
868         }
869
870         if (cred->machine_account_pending) {
871                 cli_credentials_set_machine_account(cred,
872                                                     cred->machine_account_pending_lp_ctx);
873         }
874
875         username = cli_credentials_get_username(cred);
876         
877         /* Yes, it is deliberate that we die if we have a NULL pointer
878          * here - anonymous is "", not NULL, which is 'never specified,
879          * never guessed', ie programmer bug */
880         if (!username[0]) {
881                 return true;
882         }
883
884         return false;
885 }
886
887 /**
888  * Mark the current password for a credentials struct as wrong. This will 
889  * cause the password to be prompted again (if a callback is set).
890  *
891  * This will decrement the number of times the password can be tried.
892  *
893  * @retval whether the credentials struct is finished
894  */
895 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
896 {
897         if (cred->password_obtained != CRED_CALLBACK_RESULT) {
898                 return false;
899         }
900         
901         cred->password_obtained = CRED_CALLBACK;
902
903         cred->tries--;
904
905         return (cred->tries > 0);
906 }
907
908 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
909                                               const char **username, 
910                                               const char **domain) 
911 {
912         if (cred->principal_obtained > cred->username_obtained) {
913                 *domain = talloc_strdup(mem_ctx, "");
914                 *username = cli_credentials_get_principal(cred, mem_ctx);
915         } else {
916                 *domain = cli_credentials_get_domain(cred);
917                 *username = cli_credentials_get_username(cred);
918         }
919 }
920
921 /**
922  * Read a named file, and parse it for username, domain, realm and password
923  *
924  * @param credentials Credentials structure on which to set the password
925  * @param file a named file to read the details from 
926  * @param obtained This enum describes how 'specified' this password is
927  */
928
929 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained) 
930 {
931         uint16_t len = 0;
932         char *ptr, *val, *param;
933         char **lines;
934         int i, numlines;
935
936         lines = file_lines_load(file, &numlines, 0, NULL);
937
938         if (lines == NULL)
939         {
940                 /* fail if we can't open the credentials file */
941                 d_printf("ERROR: Unable to open credentials file!\n");
942                 return false;
943         }
944
945         for (i = 0; i < numlines; i++) {
946                 len = strlen(lines[i]);
947
948                 if (len == 0)
949                         continue;
950
951                 /* break up the line into parameter & value.
952                  * will need to eat a little whitespace possibly */
953                 param = lines[i];
954                 if (!(ptr = strchr_m (lines[i], '=')))
955                         continue;
956
957                 val = ptr+1;
958                 *ptr = '\0';
959
960                 /* eat leading white space */
961                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
962                         val++;
963
964                 if (strwicmp("password", param) == 0) {
965                         cli_credentials_set_password(cred, val, obtained);
966                 } else if (strwicmp("username", param) == 0) {
967                         cli_credentials_set_username(cred, val, obtained);
968                 } else if (strwicmp("domain", param) == 0) {
969                         cli_credentials_set_domain(cred, val, obtained);
970                 } else if (strwicmp("realm", param) == 0) {
971                         cli_credentials_set_realm(cred, val, obtained);
972                 }
973                 memset(lines[i], 0, len);
974         }
975
976         talloc_free(lines);
977
978         return true;
979 }
980
981 /**
982  * Read a named file, and parse it for a password
983  *
984  * @param credentials Credentials structure on which to set the password
985  * @param file a named file to read the password from 
986  * @param obtained This enum describes how 'specified' this password is
987  */
988
989 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
990 {
991         int fd = open(file, O_RDONLY, 0);
992         bool ret;
993
994         if (fd < 0) {
995                 fprintf(stderr, "Error opening password file %s: %s\n",
996                                 file, strerror(errno));
997                 return false;
998         }
999
1000         ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1001
1002         close(fd);
1003         
1004         return ret;
1005 }
1006
1007
1008 /**
1009  * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1010  *
1011  * @param credentials Credentials structure on which to set the password
1012  * @param fd open file descriptor to read the password from 
1013  * @param obtained This enum describes how 'specified' this password is
1014  */
1015
1016 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials, 
1017                                        int fd, enum credentials_obtained obtained)
1018 {
1019         char *p;
1020         char pass[128];
1021
1022         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1023                 p && p - pass < sizeof(pass);) {
1024                 switch (read(fd, p, 1)) {
1025                 case 1:
1026                         if (*p != '\n' && *p != '\0') {
1027                                 *++p = '\0'; /* advance p, and null-terminate pass */
1028                                 break;
1029                         }
1030                         /* fall through */
1031                 case 0:
1032                         if (p - pass) {
1033                                 *p = '\0'; /* null-terminate it, just in case... */
1034                                 p = NULL; /* then force the loop condition to become false */
1035                                 break;
1036                         } else {
1037                                 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1038                                 return false;
1039                         }
1040
1041                 default:
1042                         fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1043                                         fd, strerror(errno));
1044                         return false;
1045                 }
1046         }
1047
1048         cli_credentials_set_password(credentials, pass, obtained);
1049         return true;
1050 }
1051
1052