auth/credentials: Add cli_credentials_{set,get}_forced_sasl_mech()
[metze/samba/wip.git] / auth / credentials / credentials.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    User credentials handling
5
6    Copyright (C) Jelmer Vernooij 2005
7    Copyright (C) Tim Potter 2001
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_internal.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "tevent.h"
30 #include "param/param.h"
31 #include "system/filesys.h"
32
33 /**
34  * Create a new credentials structure
35  * @param mem_ctx TALLOC_CTX parent for credentials structure 
36  */
37 _PUBLIC_ struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx) 
38 {
39         struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
40         if (cred == NULL) {
41                 return cred;
42         }
43
44         cred->workstation_obtained = CRED_UNINITIALISED;
45         cred->username_obtained = CRED_UNINITIALISED;
46         cred->password_obtained = CRED_UNINITIALISED;
47         cred->domain_obtained = CRED_UNINITIALISED;
48         cred->realm_obtained = CRED_UNINITIALISED;
49         cred->ccache_obtained = CRED_UNINITIALISED;
50         cred->client_gss_creds_obtained = CRED_UNINITIALISED;
51         cred->principal_obtained = CRED_UNINITIALISED;
52         cred->keytab_obtained = CRED_UNINITIALISED;
53         cred->server_gss_creds_obtained = CRED_UNINITIALISED;
54
55         cred->ccache_threshold = CRED_UNINITIALISED;
56         cred->client_gss_creds_threshold = CRED_UNINITIALISED;
57
58         cred->workstation = NULL;
59         cred->username = NULL;
60         cred->password = NULL;
61         cred->old_password = NULL;
62         cred->domain = NULL;
63         cred->realm = NULL;
64         cred->principal = NULL;
65         cred->salt_principal = NULL;
66         cred->impersonate_principal = NULL;
67         cred->self_service = NULL;
68         cred->target_service = NULL;
69
70         cred->bind_dn = NULL;
71
72         cred->nt_hash = NULL;
73
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         if (cli_credentials_is_anonymous(cred)){
369                 return false;
370         }
371
372         if (cred->principal_obtained >= CRED_SPECIFIED) {
373                 return true;
374         }
375         if (cred->username_obtained >= CRED_SPECIFIED) {
376                 return true;
377         }
378
379         if (cli_credentials_get_kerberos_state(cred) == CRED_MUST_USE_KERBEROS) {
380                 return true;
381         }
382
383         return false;
384 }
385
386 /**
387  * Obtain the password for this credentials context.
388  * @param cred credentials context
389  * @retval If set, the cleartext password, otherwise NULL
390  */
391 _PUBLIC_ const char *cli_credentials_get_password(struct cli_credentials *cred)
392 {
393         if (cred->machine_account_pending) {
394                 cli_credentials_set_machine_account(cred,
395                                                     cred->machine_account_pending_lp_ctx);
396         }
397
398         if (cred->password_obtained == CRED_CALLBACK && 
399             !cred->callback_running) {
400                 cred->callback_running = true;
401                 cred->password = cred->password_cb(cred);
402                 cred->callback_running = false;
403                 if (cred->password_obtained == CRED_CALLBACK) {
404                         cred->password_obtained = CRED_CALLBACK_RESULT;
405                         cli_credentials_invalidate_ccache(cred, cred->password_obtained);
406                 }
407         }
408
409         return cred->password;
410 }
411
412 /* Set a password on the credentials context, including an indication
413  * of 'how' the password was obtained */
414
415 _PUBLIC_ bool cli_credentials_set_password(struct cli_credentials *cred, 
416                                   const char *val, 
417                                   enum credentials_obtained obtained)
418 {
419         if (obtained >= cred->password_obtained) {
420                 cred->password_tries = 0;
421                 cred->password = talloc_strdup(cred, val);
422                 if (cred->password) {
423                         /* Don't print the actual password in talloc memory dumps */
424                         talloc_set_name_const(cred->password, "password set via cli_credentials_set_password");
425                 }
426                 cred->password_obtained = obtained;
427                 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
428
429                 cred->nt_hash = NULL;
430                 cred->lm_response = data_blob(NULL, 0);
431                 cred->nt_response = data_blob(NULL, 0);
432                 return true;
433         }
434
435         return false;
436 }
437
438 _PUBLIC_ bool cli_credentials_set_password_callback(struct cli_credentials *cred,
439                                            const char *(*password_cb) (struct cli_credentials *))
440 {
441         if (cred->password_obtained < CRED_CALLBACK) {
442                 cred->password_tries = 3;
443                 cred->password_cb = password_cb;
444                 cred->password_obtained = CRED_CALLBACK;
445                 cli_credentials_invalidate_ccache(cred, cred->password_obtained);
446                 return true;
447         }
448
449         return false;
450 }
451
452 /**
453  * Obtain the 'old' password for this credentials context (used for join accounts).
454  * @param cred credentials context
455  * @retval If set, the cleartext password, otherwise NULL
456  */
457 _PUBLIC_ const char *cli_credentials_get_old_password(struct cli_credentials *cred)
458 {
459         if (cred->machine_account_pending) {
460                 cli_credentials_set_machine_account(cred,
461                                                     cred->machine_account_pending_lp_ctx);
462         }
463
464         return cred->old_password;
465 }
466
467 _PUBLIC_ bool cli_credentials_set_old_password(struct cli_credentials *cred, 
468                                       const char *val, 
469                                       enum credentials_obtained obtained)
470 {
471         cred->old_password = talloc_strdup(cred, val);
472         if (cred->old_password) {
473                 /* Don't print the actual password in talloc memory dumps */
474                 talloc_set_name_const(cred->old_password, "password set via cli_credentials_set_old_password");
475         }
476         return true;
477 }
478
479 /**
480  * Obtain the password, in the form MD4(unicode(password)) for this credentials context.
481  *
482  * Sometimes we only have this much of the password, while the rest of
483  * the time this call avoids calling E_md4hash themselves.
484  *
485  * @param cred credentials context
486  * @retval If set, the cleartext password, otherwise NULL
487  */
488 _PUBLIC_ struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
489                                                            TALLOC_CTX *mem_ctx)
490 {
491         const char *password = cli_credentials_get_password(cred);
492
493         if (password) {
494                 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
495                 if (!nt_hash) {
496                         return NULL;
497                 }
498
499                 E_md4hash(password, nt_hash->hash);    
500
501                 return nt_hash;
502         } else if (cred->nt_hash != NULL) {
503                 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
504                 if (!nt_hash) {
505                         return NULL;
506                 }
507
508                 *nt_hash = *cred->nt_hash;
509
510                 return nt_hash;
511         }
512
513         return NULL;
514 }
515
516 /**
517  * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
518  * @param cred credentials context
519  * @retval The domain set on this context. 
520  * @note Return value will never be NULL except by programmer error.
521  */
522 _PUBLIC_ const char *cli_credentials_get_domain(struct cli_credentials *cred)
523 {
524         if (cred->machine_account_pending) {
525                 cli_credentials_set_machine_account(cred,
526                                                     cred->machine_account_pending_lp_ctx);
527         }
528
529         if (cred->domain_obtained == CRED_CALLBACK && 
530             !cred->callback_running) {
531                 cred->callback_running = true;
532                 cred->domain = cred->domain_cb(cred);
533                 cred->callback_running = false;
534                 if (cred->domain_obtained == CRED_CALLBACK) {
535                         cred->domain_obtained = CRED_CALLBACK_RESULT;
536                         cli_credentials_invalidate_ccache(cred, cred->domain_obtained);
537                 }
538         }
539
540         return cred->domain;
541 }
542
543
544 _PUBLIC_ bool cli_credentials_set_domain(struct cli_credentials *cred, 
545                                 const char *val, 
546                                 enum credentials_obtained obtained)
547 {
548         if (obtained >= cred->domain_obtained) {
549                 /* it is important that the domain be in upper case,
550                  * particularly for the sensitive NTLMv2
551                  * calculations */
552                 cred->domain = strupper_talloc(cred, val);
553                 cred->domain_obtained = obtained;
554                 /* setting domain does not mean we have to invalidate ccache 
555                  * because domain in not used for Kerberos operations.
556                  * If ccache invalidation is required, one will anyway specify
557                  * a password to kinit, and that will force invalidation of the ccache
558                  */
559                 return true;
560         }
561
562         return false;
563 }
564
565 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
566                                          const char *(*domain_cb) (struct cli_credentials *))
567 {
568         if (cred->domain_obtained < CRED_CALLBACK) {
569                 cred->domain_cb = domain_cb;
570                 cred->domain_obtained = CRED_CALLBACK;
571                 return true;
572         }
573
574         return false;
575 }
576
577 /**
578  * Obtain the Kerberos realm for this credentials context.
579  * @param cred credentials context
580  * @retval The realm set on this context. 
581  * @note Return value will never be NULL except by programmer error.
582  */
583 _PUBLIC_ const char *cli_credentials_get_realm(struct cli_credentials *cred)
584 {       
585         if (cred->machine_account_pending) {
586                 cli_credentials_set_machine_account(cred,
587                                                     cred->machine_account_pending_lp_ctx);
588         }
589
590         if (cred->realm_obtained == CRED_CALLBACK && 
591             !cred->callback_running) {
592                 cred->callback_running = true;
593                 cred->realm = cred->realm_cb(cred);
594                 cred->callback_running = false;
595                 if (cred->realm_obtained == CRED_CALLBACK) {
596                         cred->realm_obtained = CRED_CALLBACK_RESULT;
597                         cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
598                 }
599         }
600
601         return cred->realm;
602 }
603
604 /**
605  * Set the realm for this credentials context, and force it to
606  * uppercase for the sainity of our local kerberos libraries 
607  */
608 _PUBLIC_ bool cli_credentials_set_realm(struct cli_credentials *cred, 
609                                const char *val, 
610                                enum credentials_obtained obtained)
611 {
612         if (obtained >= cred->realm_obtained) {
613                 cred->realm = strupper_talloc(cred, val);
614                 cred->realm_obtained = obtained;
615                 cli_credentials_invalidate_ccache(cred, cred->realm_obtained);
616                 return true;
617         }
618
619         return false;
620 }
621
622 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
623                                         const char *(*realm_cb) (struct cli_credentials *))
624 {
625         if (cred->realm_obtained < CRED_CALLBACK) {
626                 cred->realm_cb = realm_cb;
627                 cred->realm_obtained = CRED_CALLBACK;
628                 return true;
629         }
630
631         return false;
632 }
633
634 /**
635  * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
636  *
637  * @param cred credentials context
638  * @retval The workstation name set on this context. 
639  * @note Return value will never be NULL except by programmer error.
640  */
641 _PUBLIC_ const char *cli_credentials_get_workstation(struct cli_credentials *cred)
642 {
643         if (cred->workstation_obtained == CRED_CALLBACK && 
644             !cred->callback_running) {
645                 cred->callback_running = true;
646                 cred->workstation = cred->workstation_cb(cred);
647                 cred->callback_running = false;
648                 if (cred->workstation_obtained == CRED_CALLBACK) {
649                         cred->workstation_obtained = CRED_CALLBACK_RESULT;
650                 }
651         }
652
653         return cred->workstation;
654 }
655
656 _PUBLIC_ bool cli_credentials_set_workstation(struct cli_credentials *cred, 
657                                      const char *val, 
658                                      enum credentials_obtained obtained)
659 {
660         if (obtained >= cred->workstation_obtained) {
661                 cred->workstation = talloc_strdup(cred, val);
662                 cred->workstation_obtained = obtained;
663                 return true;
664         }
665
666         return false;
667 }
668
669 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
670                                               const char *(*workstation_cb) (struct cli_credentials *))
671 {
672         if (cred->workstation_obtained < CRED_CALLBACK) {
673                 cred->workstation_cb = workstation_cb;
674                 cred->workstation_obtained = CRED_CALLBACK;
675                 return true;
676         }
677
678         return false;
679 }
680
681 /**
682  * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
683  *
684  * The format accepted is [domain\\]user[%password] or user[@realm][%password]
685  *
686  * @param credentials Credentials structure on which to set the password
687  * @param data the string containing the username, password etc
688  * @param obtained This enum describes how 'specified' this password is
689  */
690
691 _PUBLIC_ void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
692 {
693         char *uname, *p;
694
695         if (strcmp("%",data) == 0) {
696                 cli_credentials_set_anonymous(credentials);
697                 return;
698         }
699
700         uname = talloc_strdup(credentials, data); 
701         if ((p = strchr_m(uname,'%'))) {
702                 *p = 0;
703                 cli_credentials_set_password(credentials, p+1, obtained);
704         }
705
706         if ((p = strchr_m(uname,'@'))) {
707                 cli_credentials_set_principal(credentials, uname, obtained);
708                 *p = 0;
709                 cli_credentials_set_realm(credentials, p+1, obtained);
710                 return;
711         } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
712                 *p = 0;
713                 cli_credentials_set_domain(credentials, uname, obtained);
714                 uname = p+1;
715         }
716         cli_credentials_set_username(credentials, uname, obtained);
717 }
718
719 /**
720  * Given a a credentials structure, print it as a string
721  *
722  * The format output is [domain\\]user[%password] or user[@realm][%password]
723  *
724  * @param credentials Credentials structure on which to set the password
725  * @param mem_ctx The memory context to place the result on
726  */
727
728 _PUBLIC_ const char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx)
729 {
730         const char *bind_dn = cli_credentials_get_bind_dn(credentials);
731         const char *domain;
732         const char *username;
733         const char *name;
734
735         if (bind_dn) {
736                 name = talloc_strdup(mem_ctx, bind_dn);
737         } else {
738                 cli_credentials_get_ntlm_username_domain(credentials, mem_ctx, &username, &domain);
739                 if (domain && domain[0]) {
740                         name = talloc_asprintf(mem_ctx, "%s\\%s", 
741                                                domain, username);
742                 } else {
743                         name = talloc_asprintf(mem_ctx, "%s", 
744                                                username);
745                 }
746         }
747         return name;
748 }
749
750 /**
751  * Specifies default values for domain, workstation and realm
752  * from the smb.conf configuration file
753  *
754  * @param cred Credentials structure to fill in
755  */
756 _PUBLIC_ void cli_credentials_set_conf(struct cli_credentials *cred, 
757                               struct loadparm_context *lp_ctx)
758 {
759         cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
760         if (lpcfg_parm_is_cmdline(lp_ctx, "workgroup")) {
761                 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_SPECIFIED);
762         } else {
763                 cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
764         }
765         if (lpcfg_parm_is_cmdline(lp_ctx, "netbios name")) {
766                 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
767         } else {
768                 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
769         }
770         if (lpcfg_parm_is_cmdline(lp_ctx, "realm")) {
771                 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
772         } else {
773                 cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
774         }
775 }
776
777 /**
778  * Guess defaults for credentials from environment variables, 
779  * and from the configuration file
780  * 
781  * @param cred Credentials structure to fill in
782  */
783 _PUBLIC_ void cli_credentials_guess(struct cli_credentials *cred,
784                            struct loadparm_context *lp_ctx)
785 {
786         char *p;
787         const char *error_string;
788
789         if (lp_ctx != NULL) {
790                 cli_credentials_set_conf(cred, lp_ctx);
791         }
792         
793         if (getenv("LOGNAME")) {
794                 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
795         }
796
797         if (getenv("USER")) {
798                 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
799                 if ((p = strchr_m(getenv("USER"),'%'))) {
800                         memset(p,0,strlen(cred->password));
801                 }
802         }
803
804         if (getenv("PASSWD")) {
805                 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
806         }
807
808         if (getenv("PASSWD_FD")) {
809                 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), 
810                                                   CRED_GUESS_FILE);
811         }
812         
813         p = getenv("PASSWD_FILE");
814         if (p && p[0]) {
815                 cli_credentials_parse_password_file(cred, p, CRED_GUESS_FILE);
816         }
817         
818         if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
819                 cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
820                                            &error_string);
821         }
822 }
823
824 /**
825  * Attach NETLOGON credentials for use with SCHANNEL
826  */
827
828 _PUBLIC_ void cli_credentials_set_netlogon_creds(struct cli_credentials *cred, 
829                                                  struct netlogon_creds_CredentialState *netlogon_creds)
830 {
831         cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
832 }
833
834 /**
835  * Return attached NETLOGON credentials 
836  */
837
838 _PUBLIC_ struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
839 {
840         return cred->netlogon_creds;
841 }
842
843 /** 
844  * Set NETLOGON secure channel type
845  */
846
847 _PUBLIC_ void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
848                                              enum netr_SchannelType secure_channel_type)
849 {
850         cred->secure_channel_type = secure_channel_type;
851 }
852
853 /**
854  * Return NETLOGON secure chanel type
855  */
856
857 _PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
858 {
859         return cred->password_last_changed_time;
860 }
861
862 /** 
863  * Set NETLOGON secure channel type
864  */
865
866 _PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
867                                                              time_t last_changed_time)
868 {
869         cred->password_last_changed_time = last_changed_time;
870 }
871
872 /**
873  * Return NETLOGON secure chanel type
874  */
875
876 _PUBLIC_ enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
877 {
878         return cred->secure_channel_type;
879 }
880
881 /**
882  * Fill in a credentials structure as the anonymous user
883  */
884 _PUBLIC_ void cli_credentials_set_anonymous(struct cli_credentials *cred) 
885 {
886         cli_credentials_set_username(cred, "", CRED_SPECIFIED);
887         cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
888         cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
889         cli_credentials_set_realm(cred, NULL, CRED_SPECIFIED);
890         cli_credentials_set_workstation(cred, "", CRED_UNINITIALISED);
891 }
892
893 /**
894  * Describe a credentials context as anonymous or authenticated
895  * @retval true if anonymous, false if a username is specified
896  */
897
898 _PUBLIC_ bool cli_credentials_is_anonymous(struct cli_credentials *cred)
899 {
900         const char *username;
901         
902         /* if bind dn is set it's not anonymous */
903         if (cred->bind_dn) {
904                 return false;
905         }
906
907         if (cred->machine_account_pending) {
908                 cli_credentials_set_machine_account(cred,
909                                                     cred->machine_account_pending_lp_ctx);
910         }
911
912         username = cli_credentials_get_username(cred);
913         
914         /* Yes, it is deliberate that we die if we have a NULL pointer
915          * here - anonymous is "", not NULL, which is 'never specified,
916          * never guessed', ie programmer bug */
917         if (!username[0]) {
918                 return true;
919         }
920
921         return false;
922 }
923
924 /**
925  * Mark the current password for a credentials struct as wrong. This will 
926  * cause the password to be prompted again (if a callback is set).
927  *
928  * This will decrement the number of times the password can be tried.
929  *
930  * @retval whether the credentials struct is finished
931  */
932 _PUBLIC_ bool cli_credentials_wrong_password(struct cli_credentials *cred)
933 {
934         if (cred->password_obtained != CRED_CALLBACK_RESULT) {
935                 return false;
936         }
937
938         if (cred->password_tries == 0) {
939                 return false;
940         }
941
942         cred->password_tries--;
943
944         if (cred->password_tries == 0) {
945                 return false;
946         }
947
948         cred->password_obtained = CRED_CALLBACK;
949         return true;
950 }
951
952 _PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
953                                               const char **username, 
954                                               const char **domain) 
955 {
956         if (cred->principal_obtained > cred->username_obtained) {
957                 *domain = talloc_strdup(mem_ctx, "");
958                 *username = cli_credentials_get_principal(cred, mem_ctx);
959         } else {
960                 *domain = cli_credentials_get_domain(cred);
961                 *username = cli_credentials_get_username(cred);
962         }
963 }
964
965 /**
966  * Read a named file, and parse it for username, domain, realm and password
967  *
968  * @param credentials Credentials structure on which to set the password
969  * @param file a named file to read the details from 
970  * @param obtained This enum describes how 'specified' this password is
971  */
972
973 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained) 
974 {
975         uint16_t len = 0;
976         char *ptr, *val, *param;
977         char **lines;
978         int i, numlines;
979
980         lines = file_lines_load(file, &numlines, 0, NULL);
981
982         if (lines == NULL)
983         {
984                 /* fail if we can't open the credentials file */
985                 d_printf("ERROR: Unable to open credentials file!\n");
986                 return false;
987         }
988
989         for (i = 0; i < numlines; i++) {
990                 len = strlen(lines[i]);
991
992                 if (len == 0)
993                         continue;
994
995                 /* break up the line into parameter & value.
996                  * will need to eat a little whitespace possibly */
997                 param = lines[i];
998                 if (!(ptr = strchr_m (lines[i], '=')))
999                         continue;
1000
1001                 val = ptr+1;
1002                 *ptr = '\0';
1003
1004                 /* eat leading white space */
1005                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
1006                         val++;
1007
1008                 if (strwicmp("password", param) == 0) {
1009                         cli_credentials_set_password(cred, val, obtained);
1010                 } else if (strwicmp("username", param) == 0) {
1011                         cli_credentials_set_username(cred, val, obtained);
1012                 } else if (strwicmp("domain", param) == 0) {
1013                         cli_credentials_set_domain(cred, val, obtained);
1014                 } else if (strwicmp("realm", param) == 0) {
1015                         cli_credentials_set_realm(cred, val, obtained);
1016                 }
1017                 memset(lines[i], 0, len);
1018         }
1019
1020         talloc_free(lines);
1021
1022         return true;
1023 }
1024
1025 /**
1026  * Read a named file, and parse it for a password
1027  *
1028  * @param credentials Credentials structure on which to set the password
1029  * @param file a named file to read the password from 
1030  * @param obtained This enum describes how 'specified' this password is
1031  */
1032
1033 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
1034 {
1035         int fd = open(file, O_RDONLY, 0);
1036         bool ret;
1037
1038         if (fd < 0) {
1039                 fprintf(stderr, "Error opening password file %s: %s\n",
1040                                 file, strerror(errno));
1041                 return false;
1042         }
1043
1044         ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
1045
1046         close(fd);
1047         
1048         return ret;
1049 }
1050
1051
1052 /**
1053  * Read a file descriptor, and parse it for a password (eg from a file or stdin)
1054  *
1055  * @param credentials Credentials structure on which to set the password
1056  * @param fd open file descriptor to read the password from 
1057  * @param obtained This enum describes how 'specified' this password is
1058  */
1059
1060 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials, 
1061                                        int fd, enum credentials_obtained obtained)
1062 {
1063         char *p;
1064         char pass[128];
1065
1066         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1067                 p && p - pass < sizeof(pass);) {
1068                 switch (read(fd, p, 1)) {
1069                 case 1:
1070                         if (*p != '\n' && *p != '\0') {
1071                                 *++p = '\0'; /* advance p, and null-terminate pass */
1072                                 break;
1073                         }
1074                         /* fall through */
1075                 case 0:
1076                         if (p - pass) {
1077                                 *p = '\0'; /* null-terminate it, just in case... */
1078                                 p = NULL; /* then force the loop condition to become false */
1079                                 break;
1080                         } else {
1081                                 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
1082                                 return false;
1083                         }
1084
1085                 default:
1086                         fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
1087                                         fd, strerror(errno));
1088                         return false;
1089                 }
1090         }
1091
1092         cli_credentials_set_password(credentials, pass, obtained);
1093         return true;
1094 }
1095
1096