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