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