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