auth/credentials: try to use kerberos with the machine account unless we're in an...
[kai/samba-autobuild/.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_internal.h"
32 #include "auth/credentials/credentials_proto.h"
33 #include "auth/credentials/credentials_krb5.h"
34 #include "auth/kerberos/kerberos_util.h"
35 #include "param/param.h"
36 #include "lib/events/events.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "source3/include/secrets.h"
39 #include "dbwrap/dbwrap.h"
40 #include "dbwrap/dbwrap_open.h"
41 #include "lib/util/util_tdb.h"
42 #include "libds/common/roles.h"
43
44 /**
45  * Fill in credentials for the machine trust account, from the secrets database.
46  *
47  * @param cred Credentials structure to fill in
48  * @retval NTSTATUS error detailing any failure
49  */
50 static NTSTATUS cli_credentials_set_secrets_lct(struct cli_credentials *cred,
51                                                 struct loadparm_context *lp_ctx,
52                                                 struct ldb_context *ldb,
53                                                 const char *base,
54                                                 const char *filter,
55                                                 time_t secrets_tdb_last_change_time,
56                                                 const char *secrets_tdb_password,
57                                                 char **error_string)
58 {
59         TALLOC_CTX *mem_ctx;
60
61         int ldb_ret;
62         struct ldb_message *msg;
63
64         const char *machine_account;
65         const char *password;
66         const char *domain;
67         const char *realm;
68         enum netr_SchannelType sct;
69         const char *salt_principal;
70         char *keytab;
71         const struct ldb_val *whenChanged;
72         time_t lct;
73
74         /* ok, we are going to get it now, don't recurse back here */
75         cred->machine_account_pending = false;
76
77         /* some other parts of the system will key off this */
78         cred->machine_account = true;
79
80         mem_ctx = talloc_named(cred, 0, "cli_credentials_set_secrets from ldb");
81
82         if (!ldb) {
83                 /* Local secrets are stored in secrets.ldb */
84                 ldb = secrets_db_connect(mem_ctx, lp_ctx);
85                 if (!ldb) {
86                         *error_string = talloc_strdup(cred, "Could not open secrets.ldb");
87                         talloc_free(mem_ctx);
88                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
89                 }
90         }
91
92         ldb_ret = dsdb_search_one(ldb, mem_ctx, &msg,
93                                   ldb_dn_new(mem_ctx, ldb, base),
94                                   LDB_SCOPE_SUBTREE,
95                                   NULL, 0, "%s", filter);
96
97         if (ldb_ret != LDB_SUCCESS) {
98                 *error_string = talloc_asprintf(cred, "Could not find entry to match filter: '%s' base: '%s': %s: %s",
99                                                 filter, base ? base : "",
100                                                 ldb_strerror(ldb_ret), ldb_errstring(ldb));
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         whenChanged = ldb_msg_find_ldb_val(msg, "whenChanged");
108         if (!whenChanged || ldb_val_to_time(whenChanged, &lct) != LDB_SUCCESS) {
109                 /* This attribute is mandetory */
110                 talloc_free(mem_ctx);
111                 return NT_STATUS_NOT_FOUND;
112         }
113
114         /* Don't set secrets.ldb info if the secrets.tdb entry was more recent */
115         if (lct < secrets_tdb_last_change_time) {
116                 talloc_free(mem_ctx);
117                 return NT_STATUS_NOT_FOUND;
118         }
119
120         if (lct == secrets_tdb_last_change_time && secrets_tdb_password && strcmp(password, secrets_tdb_password) != 0) {
121                 talloc_free(mem_ctx);
122                 return NT_STATUS_NOT_FOUND;
123         }
124
125         cli_credentials_set_password_last_changed_time(cred, lct);
126
127         machine_account = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
128
129         if (!machine_account) {
130                 machine_account = ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL);
131
132                 if (!machine_account) {
133                         const char *ldap_bind_dn = ldb_msg_find_attr_as_string(msg, "ldapBindDn", NULL);
134                         if (!ldap_bind_dn) {
135                                 *error_string = talloc_asprintf(cred,
136                                                                 "Could not find 'samAccountName', "
137                                                                 "'servicePrincipalName' or "
138                                                                 "'ldapBindDn' in secrets record: %s",
139                                                                 ldb_dn_get_linearized(msg->dn));
140                                 talloc_free(mem_ctx);
141                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
142                         } else {
143                                 /* store bind dn in credentials */
144                                 cli_credentials_set_bind_dn(cred, ldap_bind_dn);
145                         }
146                 }
147         }
148
149         salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
150         cli_credentials_set_salt_principal(cred, salt_principal);
151
152         sct = ldb_msg_find_attr_as_int(msg, "secureChannelType", 0);
153         if (sct) {
154                 cli_credentials_set_secure_channel_type(cred, sct);
155         }
156
157         if (!password) {
158                 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msg, "unicodePwd");
159                 struct samr_Password hash;
160                 ZERO_STRUCT(hash);
161                 if (nt_password_hash) {
162                         memcpy(hash.hash, nt_password_hash->data,
163                                MIN(nt_password_hash->length, sizeof(hash.hash)));
164
165                         cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
166                 } else {
167                         cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
168                 }
169         } else {
170                 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
171         }
172
173         domain = ldb_msg_find_attr_as_string(msg, "flatname", NULL);
174         if (domain) {
175                 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
176         }
177
178         realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
179         if (realm) {
180                 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
181         }
182
183         if (machine_account) {
184                 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
185         }
186
187         cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0));
188
189         /* If there was an external keytab specified by reference in
190          * the LDB, then use this.  Otherwise we will make one up
191          * (chewing CPU time) from the password */
192         keytab = keytab_name_from_msg(cred, ldb, msg);
193         if (keytab) {
194                 cli_credentials_set_keytab_name(cred, lp_ctx, keytab, CRED_SPECIFIED);
195                 talloc_free(keytab);
196         }
197         talloc_free(mem_ctx);
198
199         return NT_STATUS_OK;
200 }
201
202
203 /**
204  * Fill in credentials for the machine trust account, from the secrets database.
205  *
206  * @param cred Credentials structure to fill in
207  * @retval NTSTATUS error detailing any failure
208  */
209 _PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
210                                               struct loadparm_context *lp_ctx,
211                                               struct ldb_context *ldb,
212                                               const char *base,
213                                               const char *filter,
214                                               char **error_string)
215 {
216         NTSTATUS status = cli_credentials_set_secrets_lct(cred, lp_ctx, ldb, base, filter, 0, NULL, error_string);
217         if (!NT_STATUS_IS_OK(status)) {
218                 /* set anonymous as the fallback, if the machine account won't work */
219                 cli_credentials_set_anonymous(cred);
220         }
221         return status;
222 }
223
224 /**
225  * Fill in credentials for the machine trust account, from the secrets database.
226  *
227  * @param cred Credentials structure to fill in
228  * @retval NTSTATUS error detailing any failure
229  */
230 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
231                                                       struct loadparm_context *lp_ctx)
232 {
233         struct db_context *db_ctx;
234         char *secrets_tdb_path;
235
236         secrets_tdb_path = lpcfg_private_db_path(cred, lp_ctx, "secrets");
237         if (secrets_tdb_path == NULL) {
238                 return NT_STATUS_NO_MEMORY;
239         }
240
241         db_ctx = dbwrap_local_open(cred, lp_ctx, secrets_tdb_path, 0,
242                                    TDB_DEFAULT, O_RDWR, 0600,
243                                    DBWRAP_LOCK_ORDER_1,
244                                    DBWRAP_FLAG_NONE);
245         TALLOC_FREE(secrets_tdb_path);
246
247         /*
248          * We do not check for errors here, we might not have a
249          * secrets.tdb at all, and so we just need to check the
250          * secrets.ldb
251          */
252         return cli_credentials_set_machine_account_db_ctx(cred, lp_ctx, db_ctx);
253 }
254
255 /**
256  * Fill in credentials for the machine trust account, from the
257  * secrets.ldb or passed in handle to secrets.tdb (perhaps in CTDB).
258  *
259  * This version is used in parts of the code that can link in the
260  * CTDB dbwrap backend, by passing down the already open handle.
261  *
262  * @param cred Credentials structure to fill in
263  * @param db_ctx dbwrap context for secrets.tdb
264  * @retval NTSTATUS error detailing any failure
265  */
266 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account_db_ctx(struct cli_credentials *cred,
267                                                              struct loadparm_context *lp_ctx,
268                                                              struct db_context *db_ctx)
269 {
270         NTSTATUS status;
271         char *filter;
272         char *error_string = NULL;
273         const char *domain;
274         bool secrets_tdb_password_more_recent;
275         time_t secrets_tdb_lct = 0;
276         char *secrets_tdb_password = NULL;
277         char *secrets_tdb_old_password = NULL;
278         uint32_t secrets_tdb_secure_channel_type = SEC_CHAN_NULL;
279         int server_role = lpcfg_server_role(lp_ctx);
280         int security = lpcfg_security(lp_ctx);
281         char *keystr;
282         char *keystr_upper = NULL;
283         TALLOC_CTX *tmp_ctx = talloc_named(cred, 0, "cli_credentials_set_secrets from ldb");
284         if (!tmp_ctx) {
285                 return NT_STATUS_NO_MEMORY;
286         }
287
288         /* Bleh, nasty recursion issues: We are setting a machine
289          * account here, so we don't want the 'pending' flag around
290          * any more */
291         cred->machine_account_pending = false;
292
293         /* We have to do this, as the fallback in
294          * cli_credentials_set_secrets is to run as anonymous, so the domain is wiped */
295         domain = cli_credentials_get_domain(cred);
296
297         if (db_ctx) {
298                 TDB_DATA dbuf;
299                 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
300                                          SECRETS_MACHINE_LAST_CHANGE_TIME,
301                                          domain);
302                 keystr_upper = strupper_talloc(tmp_ctx, keystr);
303                 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
304                                       &dbuf);
305                 if (NT_STATUS_IS_OK(status) && dbuf.dsize == 4) {
306                         secrets_tdb_lct = IVAL(dbuf.dptr,0);
307                 }
308
309                 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
310                                          SECRETS_MACHINE_PASSWORD,
311                                          domain);
312                 keystr_upper = strupper_talloc(tmp_ctx, keystr);
313                 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
314                                       &dbuf);
315                 if (NT_STATUS_IS_OK(status)) {
316                         secrets_tdb_password = (char *)dbuf.dptr;
317                 }
318
319                 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
320                                          SECRETS_MACHINE_PASSWORD_PREV,
321                                          domain);
322                 keystr_upper = strupper_talloc(tmp_ctx, keystr);
323                 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
324                                       &dbuf);
325                 if (NT_STATUS_IS_OK(status)) {
326                         secrets_tdb_old_password = (char *)dbuf.dptr;
327                 }
328
329                 keystr = talloc_asprintf(tmp_ctx, "%s/%s",
330                                          SECRETS_MACHINE_SEC_CHANNEL_TYPE,
331                                          domain);
332                 keystr_upper = strupper_talloc(tmp_ctx, keystr);
333                 status = dbwrap_fetch(db_ctx, tmp_ctx, string_tdb_data(keystr_upper),
334                                       &dbuf);
335                 if (NT_STATUS_IS_OK(status) && dbuf.dsize == 4) {
336                         secrets_tdb_secure_channel_type = IVAL(dbuf.dptr,0);
337                 }
338         }
339
340         filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER, 
341                                  domain);
342         status = cli_credentials_set_secrets_lct(cred, lp_ctx, NULL,
343                                                  SECRETS_PRIMARY_DOMAIN_DN,
344                                                  filter, secrets_tdb_lct, secrets_tdb_password, &error_string);
345         if (secrets_tdb_password == NULL) {
346                 secrets_tdb_password_more_recent = false;
347         } else if (NT_STATUS_EQUAL(NT_STATUS_CANT_ACCESS_DOMAIN_INFO, status)
348             || NT_STATUS_EQUAL(NT_STATUS_NOT_FOUND, status)) {
349                 secrets_tdb_password_more_recent = true;
350         } else if (secrets_tdb_lct > cli_credentials_get_password_last_changed_time(cred)) {
351                 secrets_tdb_password_more_recent = true;
352         } else if (secrets_tdb_lct == cli_credentials_get_password_last_changed_time(cred)) {
353                 secrets_tdb_password_more_recent = strcmp(secrets_tdb_password, cli_credentials_get_password(cred)) != 0;
354         } else {
355                 secrets_tdb_password_more_recent = false;
356         }
357
358         if (secrets_tdb_password_more_recent) {
359                 enum credentials_use_kerberos use_kerberos = CRED_DONT_USE_KERBEROS;
360                 char *machine_account = talloc_asprintf(tmp_ctx, "%s$", lpcfg_netbios_name(lp_ctx));
361                 cli_credentials_set_password(cred, secrets_tdb_password, CRED_SPECIFIED);
362                 cli_credentials_set_old_password(cred, secrets_tdb_old_password, CRED_SPECIFIED);
363                 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
364                 if (strequal(domain, lpcfg_workgroup(lp_ctx))) {
365                         cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_SPECIFIED);
366
367                         switch (server_role) {
368                         case ROLE_DOMAIN_MEMBER:
369                                 if (security != SEC_ADS) {
370                                         break;
371                                 }
372                                 /* fall through */
373                         case ROLE_ACTIVE_DIRECTORY_DC:
374                                 use_kerberos = CRED_AUTO_USE_KERBEROS;
375                                 break;
376                         }
377                 }
378                 cli_credentials_set_kerberos_state(cred, use_kerberos);
379                 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
380                 cli_credentials_set_password_last_changed_time(cred, secrets_tdb_lct);
381                 cli_credentials_set_secure_channel_type(cred, secrets_tdb_secure_channel_type);
382                 status = NT_STATUS_OK;
383         } else if (!NT_STATUS_IS_OK(status)) {
384                 if (db_ctx) {
385                         error_string
386                                 = talloc_asprintf(cred,
387                                                   "Failed to fetch machine account password for %s from both "
388                                                   "secrets.ldb (%s) and from %s",
389                                                   domain,
390                                                   error_string == NULL ? "error" : error_string,
391                                                   dbwrap_name(db_ctx));
392                 } else {
393                         char *secrets_tdb_path;
394
395                         secrets_tdb_path = lpcfg_private_db_path(tmp_ctx,
396                                                                  lp_ctx,
397                                                                  "secrets");
398                         if (secrets_tdb_path == NULL) {
399                                 return NT_STATUS_NO_MEMORY;
400                         }
401
402                         error_string = talloc_asprintf(cred,
403                                                        "Failed to fetch machine account password from "
404                                                        "secrets.ldb: %s and failed to open %s",
405                                                        error_string == NULL ? "error" : error_string,
406                                                        secrets_tdb_path);
407                 }
408                 DEBUG(1, ("Could not find machine account in secrets database: %s: %s\n",
409                           error_string == NULL ? "error" : error_string,
410                           nt_errstr(status)));
411                 /* set anonymous as the fallback, if the machine account won't work */
412                 cli_credentials_set_anonymous(cred);
413         }
414
415         TALLOC_FREE(tmp_ctx);
416         return status;
417 }
418
419 /**
420  * Fill in credentials for a particular principal, from the secrets database.
421  *
422  * @param cred Credentials structure to fill in
423  * @retval NTSTATUS error detailing any failure
424  */
425 _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
426                                               struct loadparm_context *lp_ctx,
427                                               const char *serviceprincipal)
428 {
429         NTSTATUS status;
430         char *filter;
431         char *error_string = NULL;
432         /* Bleh, nasty recursion issues: We are setting a machine
433          * account here, so we don't want the 'pending' flag around
434          * any more */
435         cred->machine_account_pending = false;
436         filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
437                                  cli_credentials_get_realm(cred),
438                                  cli_credentials_get_domain(cred),
439                                  serviceprincipal);
440         status = cli_credentials_set_secrets_lct(cred, lp_ctx, NULL,
441                                              SECRETS_PRINCIPALS_DN, filter,
442                                              0, NULL, &error_string);
443         if (!NT_STATUS_IS_OK(status)) {
444                 DEBUG(1, ("Could not find %s principal in secrets database: %s: %s\n",
445                           serviceprincipal, nt_errstr(status),
446                           error_string ? error_string : "<no error>"));
447         }
448         return status;
449 }
450
451 /**
452  * Ask that when required, the credentials system will be filled with
453  * machine trust account, from the secrets database.
454  *
455  * @param cred Credentials structure to fill in
456  * @note This function is used to call the above function after, rather
457  *       than during, popt processing.
458  *
459  */
460 _PUBLIC_ void cli_credentials_set_machine_account_pending(struct cli_credentials *cred,
461                                                  struct loadparm_context *lp_ctx)
462 {
463         cred->machine_account_pending = true;
464         cred->machine_account_pending_lp_ctx = lp_ctx;
465 }
466
467