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