37e825bee90194c3eb11832b51547baf8fd035cc
[metze/samba/wip.git] / auth / credentials / credentials_secrets.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    User credentials handling (as regards on-disk files)
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 "lib/events/events.h"
26 #include <ldb.h>
27 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
28 #include "param/secrets.h"
29 #include "system/filesys.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/credentials/credentials_proto.h"
32 #include "auth/credentials/credentials_krb5.h"
33 #include "auth/kerberos/kerberos_util.h"
34 #include "param/param.h"
35 #include "lib/events/events.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "source3/include/secrets.h"
38 #include "dbwrap/dbwrap.h"
39 #include "dbwrap/dbwrap_open.h"
40 #include "lib/util/util_tdb.h"
41
42
43 /**
44  * Fill in credentials for the machine trust account, from the secrets database.
45  * 
46  * @param cred Credentials structure to fill in
47  * @retval NTSTATUS error detailing any failure
48  */
49 _PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred, 
50                                               struct loadparm_context *lp_ctx,
51                                               struct ldb_context *ldb,
52                                               const char *base,
53                                               const char *filter, 
54                                               char **error_string)
55 {
56         TALLOC_CTX *mem_ctx;
57         
58         int ldb_ret;
59         struct ldb_message *msg;
60         
61         const char *machine_account;
62         const char *password;
63         const char *domain;
64         const char *realm;
65         enum netr_SchannelType sct;
66         const char *salt_principal;
67         char *keytab;
68         const struct ldb_val *whenChanged;
69
70         /* ok, we are going to get it now, don't recurse back here */
71         cred->machine_account_pending = false;
72
73         /* some other parts of the system will key off this */
74         cred->machine_account = true;
75
76         mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
77
78         if (!ldb) {
79                 /* Local secrets are stored in secrets.ldb */
80                 ldb = secrets_db_connect(mem_ctx, lp_ctx);
81                 if (!ldb) {
82                         /* set anonymous as the fallback, if the machine account won't work */
83                         cli_credentials_set_anonymous(cred);
84                         *error_string = talloc_strdup(cred, "Could not open secrets.ldb");
85                         talloc_free(mem_ctx);
86                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
87                 }
88         }
89
90         ldb_ret = dsdb_search_one(ldb, mem_ctx, &msg,
91                                   ldb_dn_new(mem_ctx, ldb, base),
92                                   LDB_SCOPE_SUBTREE,
93                                   NULL, 0, "%s", filter);
94
95         if (ldb_ret != LDB_SUCCESS) {
96                 *error_string = talloc_asprintf(cred, "Could not find entry to match filter: '%s' base: '%s': %s: %s",
97                                                 filter, base ? base : "",
98                                                 ldb_strerror(ldb_ret), ldb_errstring(ldb));
99                 /* set anonymous as the fallback, if the machine account won't work */
100                 cli_credentials_set_anonymous(cred);
101                 talloc_free(mem_ctx);
102                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
103         }
104
105         password = ldb_msg_find_attr_as_string(msg, "secret", NULL);
106
107         machine_account = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
108
109         if (!machine_account) {
110                 machine_account = ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL);
111                 
112                 if (!machine_account) {
113                         const char *ldap_bind_dn = ldb_msg_find_attr_as_string(msg, "ldapBindDn", NULL);
114                         if (!ldap_bind_dn) {
115                                 *error_string = talloc_asprintf(cred, 
116                                                                 "Could not find 'samAccountName', "
117                                                                 "'servicePrincipalName' or "
118                                                                 "'ldapBindDn' in secrets record: %s",
119                                                                 ldb_dn_get_linearized(msg->dn));
120                                 /* set anonymous as the fallback, if the machine account won't work */
121                                 cli_credentials_set_anonymous(cred);
122                                 talloc_free(mem_ctx);
123                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
124                         } else {
125                                 /* store bind dn in credentials */
126                                 cli_credentials_set_bind_dn(cred, ldap_bind_dn);
127                         }
128                 }
129         }
130
131         salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
132         cli_credentials_set_salt_principal(cred, salt_principal);
133         
134         sct = ldb_msg_find_attr_as_int(msg, "secureChannelType", 0);
135         if (sct) { 
136                 cli_credentials_set_secure_channel_type(cred, sct);
137         }
138         
139         if (!password) {
140                 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msg, "unicodePwd");
141                 struct samr_Password hash;
142                 ZERO_STRUCT(hash);
143                 if (nt_password_hash) {
144                         memcpy(hash.hash, nt_password_hash->data, 
145                                MIN(nt_password_hash->length, sizeof(hash.hash)));
146                 
147                         cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
148                 } else {
149                         cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
150                 }
151         } else {
152                 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
153         }
154
155         
156         domain = ldb_msg_find_attr_as_string(msg, "flatname", NULL);
157         if (domain) {
158                 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
159         }
160
161         realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
162         if (realm) {
163                 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
164         }
165
166         if (machine_account) {
167                 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
168         }
169
170         cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0));
171
172         whenChanged = ldb_msg_find_ldb_val(msg, "whenChanged");
173         if (whenChanged) {
174                 time_t lct;
175                 if (ldb_val_to_time(whenChanged, &lct) == LDB_SUCCESS) {
176                         cli_credentials_set_password_last_changed_time(cred, lct);
177                 }
178         }
179         
180         /* If there was an external keytab specified by reference in
181          * the LDB, then use this.  Otherwise we will make one up
182          * (chewing CPU time) from the password */
183         keytab = keytab_name_from_msg(cred, ldb, msg);
184         if (keytab) {
185                 cli_credentials_set_keytab_name(cred, lp_ctx, keytab, CRED_SPECIFIED);
186                 talloc_free(keytab);
187         }
188         talloc_free(mem_ctx);
189         
190         return NT_STATUS_OK;
191 }
192
193 /**
194  * Fill in credentials for the machine trust account, from the secrets database.
195  * 
196  * @param cred Credentials structure to fill in
197  * @retval NTSTATUS error detailing any failure
198  */
199 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
200                                                       struct loadparm_context *lp_ctx)
201 {
202         NTSTATUS status;
203         char *filter;
204         char *error_string;
205         const char *domain;
206         const char *realm;
207         bool secrets_tdb_password_more_recent;
208         time_t secrets_tdb_lct = 0;
209         char *secrets_tdb_password = NULL;
210         char *keystr;
211         char *keystr_upper = NULL;
212         char *secrets_tdb = lpcfg_private_path(cred, lp_ctx, "secrets.tdb");
213         struct db_context *db_ctx = dbwrap_local_open(cred, lp_ctx, secrets_tdb, 0,
214                                                       TDB_DEFAULT, O_RDWR, 0600,
215                                                       DBWRAP_LOCK_ORDER_1);
216         /* Bleh, nasty recursion issues: We are setting a machine
217          * account here, so we don't want the 'pending' flag around
218          * any more */
219         cred->machine_account_pending = false;
220
221         /* We have to do this, as the fallback in
222          * cli_credentials_set_secrets is to run as anonymous, so the domain is wiped */
223         domain = cli_credentials_get_domain(cred);
224         realm = cli_credentials_get_realm(cred);
225
226         if (db_ctx) {
227                 TDB_DATA dbuf;
228                 keystr = talloc_asprintf(cred, "%s/%s",
229                                          SECRETS_MACHINE_LAST_CHANGE_TIME,
230                                          domain);
231                 keystr_upper = strupper_talloc(cred, keystr);
232                 TALLOC_FREE(keystr);
233                 status = dbwrap_fetch(db_ctx, cred, string_tdb_data(keystr_upper),
234                                       &dbuf);
235                 TALLOC_FREE(keystr_upper);
236                 if (NT_STATUS_IS_OK(status) && dbuf.dsize == 4) {
237                         secrets_tdb_lct = IVAL(dbuf.dptr,0);
238                         TALLOC_FREE(dbuf.dptr);
239                 }
240
241                 keystr = talloc_asprintf(cred, "%s/%s",
242                                          SECRETS_MACHINE_PASSWORD,
243                                          domain);
244                 keystr_upper = strupper_talloc(cred, keystr);
245                 TALLOC_FREE(keystr);
246                 status = dbwrap_fetch(db_ctx, cred, string_tdb_data(keystr_upper),
247                                       &dbuf);
248                 if (NT_STATUS_IS_OK(status)) {
249                         secrets_tdb_password = (char *)dbuf.dptr;
250                 }
251         }
252
253         filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER, 
254                                  domain);
255         status = cli_credentials_set_secrets(cred, lp_ctx, NULL,
256                                              SECRETS_PRIMARY_DOMAIN_DN,
257                                              filter, &error_string);
258         if (secrets_tdb_password == NULL) {
259                 secrets_tdb_password_more_recent = false;
260         } else if (NT_STATUS_EQUAL(NT_STATUS_CANT_ACCESS_DOMAIN_INFO, status)
261             || NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, status)) {
262                 secrets_tdb_password_more_recent = true;
263         } else if (secrets_tdb_lct > cli_credentials_get_password_last_changed_time(cred)) {
264                 secrets_tdb_password_more_recent = true;
265         } else if (secrets_tdb_lct == cli_credentials_get_password_last_changed_time(cred)) {
266                 secrets_tdb_password_more_recent = strcmp(secrets_tdb_password, cli_credentials_get_password(cred)) != 0;
267         } else {
268                 secrets_tdb_password_more_recent = false;
269         }
270
271         if (secrets_tdb_password_more_recent) {
272                 char *machine_account = talloc_asprintf(cred, "%s$", lpcfg_netbios_name(lp_ctx));
273                 cli_credentials_set_password(cred, secrets_tdb_password, CRED_SPECIFIED);
274                 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
275                 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
276                 cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_SPECIFIED);
277                 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
278                 TALLOC_FREE(machine_account);
279         } else if (NT_STATUS_EQUAL(NT_STATUS_CANT_ACCESS_DOMAIN_INFO, status)
280                    || NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, status)) {
281                 if (db_ctx) {
282                         error_string = talloc_asprintf(cred,
283                                                        "Failed to fetch machine account password from "
284                                                        "secrets.ldb: %s and failed to fetch %s from %s",
285                                                        error_string, keystr_upper, secrets_tdb);
286                 } else {
287                         error_string = talloc_asprintf(cred,
288                                                        "Failed to fetch machine account password from "
289                                                        "secrets.ldb: %s and failed to open %s",
290                                                        error_string, secrets_tdb);
291                 }
292         }
293         
294         TALLOC_FREE(secrets_tdb_password);
295         TALLOC_FREE(secrets_tdb);
296         TALLOC_FREE(db_ctx);
297         if (!NT_STATUS_IS_OK(status)) {
298                 DEBUG(1, ("Could not find machine account in secrets database: %s: %s\n", 
299                           error_string, nt_errstr(status)));
300                 talloc_free(error_string);
301         }
302         return status;
303 }
304
305 /**
306  * Fill in credentials for the machine trust account, from the secrets database.
307  * 
308  * @param cred Credentials structure to fill in
309  * @retval NTSTATUS error detailing any failure
310  */
311 NTSTATUS cli_credentials_set_krbtgt(struct cli_credentials *cred,
312                                     struct loadparm_context *lp_ctx)
313 {
314         NTSTATUS status;
315         char *filter;
316         char *error_string;
317         /* Bleh, nasty recursion issues: We are setting a machine
318          * account here, so we don't want the 'pending' flag around
319          * any more */
320         cred->machine_account_pending = false;
321         filter = talloc_asprintf(cred, SECRETS_KRBTGT_SEARCH,
322                                        cli_credentials_get_realm(cred),
323                                        cli_credentials_get_domain(cred));
324         status = cli_credentials_set_secrets(cred, lp_ctx, NULL,
325                                              SECRETS_PRINCIPALS_DN,
326                                              filter, &error_string);
327         if (!NT_STATUS_IS_OK(status)) {
328                 DEBUG(1, ("Could not find krbtgt (master Kerberos) account in secrets database: %s: %s\n", nt_errstr(status), error_string));
329                 talloc_free(error_string);
330         }
331         return status;
332 }
333
334 /**
335  * Fill in credentials for a particular prinicpal, from the secrets database.
336  * 
337  * @param cred Credentials structure to fill in
338  * @retval NTSTATUS error detailing any failure
339  */
340 _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
341                                               struct loadparm_context *lp_ctx,
342                                               const char *serviceprincipal)
343 {
344         NTSTATUS status;
345         char *filter;
346         char *error_string;
347         /* Bleh, nasty recursion issues: We are setting a machine
348          * account here, so we don't want the 'pending' flag around
349          * any more */
350         cred->machine_account_pending = false;
351         filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
352                                  cli_credentials_get_realm(cred),
353                                  cli_credentials_get_domain(cred),
354                                  serviceprincipal);
355         status = cli_credentials_set_secrets(cred, lp_ctx, NULL,
356                                              SECRETS_PRINCIPALS_DN, filter,
357                                              &error_string);
358         if (!NT_STATUS_IS_OK(status)) {
359                 DEBUG(1, ("Could not find %s principal in secrets database: %s: %s\n", serviceprincipal, nt_errstr(status), error_string));
360         }
361         return status;
362 }
363
364 /**
365  * Ask that when required, the credentials system will be filled with
366  * machine trust account, from the secrets database.
367  * 
368  * @param cred Credentials structure to fill in
369  * @note This function is used to call the above function after, rather 
370  *       than during, popt processing.
371  *
372  */
373 _PUBLIC_ void cli_credentials_set_machine_account_pending(struct cli_credentials *cred,
374                                                  struct loadparm_context *lp_ctx)
375 {
376         cred->machine_account_pending = true;
377         cred->machine_account_pending_lp_ctx = lp_ctx;
378 }
379
380