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