s4-credentials Allocate ldb result on correct memory context
[abartlet/samba.git/.git] / source4 / 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 "lib/ldb/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 "../lib/util/util_ldb.h"
31 #include "auth/credentials/credentials.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
38 /**
39  * Fill in credentials for the machine trust account, from the secrets database.
40  * 
41  * @param cred Credentials structure to fill in
42  * @retval NTSTATUS error detailing any failure
43  */
44 _PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred, 
45                                               struct loadparm_context *lp_ctx,
46                                               struct ldb_context *ldb,
47                                               const char *base,
48                                               const char *filter, 
49                                               char **error_string)
50 {
51         TALLOC_CTX *mem_ctx;
52         
53         int ldb_ret;
54         struct ldb_message *msg;
55         
56         const char *machine_account;
57         const char *password;
58         const char *old_password;
59         const char *domain;
60         const char *realm;
61         enum netr_SchannelType sct;
62         const char *salt_principal;
63         char *keytab;
64         const struct ldb_val *whenChanged;
65
66         /* ok, we are going to get it now, don't recurse back here */
67         cred->machine_account_pending = false;
68
69         /* some other parts of the system will key off this */
70         cred->machine_account = true;
71
72         mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
73
74         if (!ldb) {
75                 /* Local secrets are stored in secrets.ldb */
76                 ldb = secrets_db_connect(mem_ctx, lp_ctx);
77                 if (!ldb) {
78                         /* set anonymous as the fallback, if the machine account won't work */
79                         cli_credentials_set_anonymous(cred);
80                         *error_string = talloc_strdup(cred, "Could not open secrets.ldb");
81                         talloc_free(mem_ctx);
82                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
83                 }
84         }
85
86         ldb_ret = dsdb_search_one(ldb, mem_ctx, &msg,
87                                   ldb_dn_new(mem_ctx, ldb, base),
88                                   LDB_SCOPE_SUBTREE,
89                                   NULL, 0, "%s", filter);
90
91         if (ldb_ret != LDB_SUCCESS) {
92                 *error_string = talloc_asprintf(cred, "Could not find entry to match filter: '%s' base: '%s': %s: %s\n",
93                                                 filter, base ? base : "",
94                                                 ldb_strerror(ldb_ret), ldb_errstring(ldb));
95                 /* set anonymous as the fallback, if the machine account won't work */
96                 cli_credentials_set_anonymous(cred);
97                 talloc_free(mem_ctx);
98                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
99         }
100
101         password = ldb_msg_find_attr_as_string(msg, "secret", NULL);
102         old_password = ldb_msg_find_attr_as_string(msg, "priorSecret", NULL);
103
104         machine_account = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
105
106         if (!machine_account) {
107                 machine_account = ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL);
108                 
109                 if (!machine_account) {
110                         const char *ldap_bind_dn = ldb_msg_find_attr_as_string(msg, "ldapBindDn", NULL);
111                         if (!ldap_bind_dn) {
112                                 *error_string = talloc_asprintf(cred, 
113                                                                 "Could not find 'samAccountName', "
114                                                                 "'servicePrincipalName' or "
115                                                                 "'ldapBindDn' in secrets record: %s",
116                                                                 ldb_dn_get_linearized(msg->dn));
117                                 /* set anonymous as the fallback, if the machine account won't work */
118                                 cli_credentials_set_anonymous(cred);
119                                 talloc_free(mem_ctx);
120                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
121                         } else {
122                                 /* store bind dn in credentials */
123                                 cli_credentials_set_bind_dn(cred, ldap_bind_dn);
124                         }
125                 }
126         }
127
128         salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
129         cli_credentials_set_salt_principal(cred, salt_principal);
130         
131         sct = ldb_msg_find_attr_as_int(msg, "secureChannelType", 0);
132         if (sct) { 
133                 cli_credentials_set_secure_channel_type(cred, sct);
134         }
135         
136         if (!password) {
137                 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msg, "unicodePwd");
138                 struct samr_Password hash;
139                 ZERO_STRUCT(hash);
140                 if (nt_password_hash) {
141                         memcpy(hash.hash, nt_password_hash->data, 
142                                MIN(nt_password_hash->length, sizeof(hash.hash)));
143                 
144                         cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
145                 } else {
146                         cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
147                 }
148         } else {
149                 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
150         }
151
152         
153         domain = ldb_msg_find_attr_as_string(msg, "flatname", NULL);
154         if (domain) {
155                 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
156         }
157
158         realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
159         if (realm) {
160                 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
161         }
162
163         if (machine_account) {
164                 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
165         }
166
167         cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0));
168
169         whenChanged = ldb_msg_find_ldb_val(msg, "whenChanged");
170         if (whenChanged) {
171                 time_t lct;
172                 if (ldb_val_to_time(whenChanged, &lct) == LDB_SUCCESS) {
173                         cli_credentials_set_password_last_changed_time(cred, lct);
174                 }
175         }
176         
177         /* If there was an external keytab specified by reference in
178          * the LDB, then use this.  Otherwise we will make one up
179          * (chewing CPU time) from the password */
180         keytab = keytab_name_from_msg(cred, ldb, msg);
181         if (keytab) {
182                 cli_credentials_set_keytab_name(cred, lp_ctx, keytab, CRED_SPECIFIED);
183                 talloc_free(keytab);
184         }
185         talloc_free(mem_ctx);
186         
187         return NT_STATUS_OK;
188 }
189
190 /**
191  * Fill in credentials for the machine trust account, from the secrets database.
192  * 
193  * @param cred Credentials structure to fill in
194  * @retval NTSTATUS error detailing any failure
195  */
196 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
197                                                       struct loadparm_context *lp_ctx)
198 {
199         NTSTATUS status;
200         char *filter;
201         char *error_string;
202         /* Bleh, nasty recursion issues: We are setting a machine
203          * account here, so we don't want the 'pending' flag around
204          * any more */
205         cred->machine_account_pending = false;
206         filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER, 
207                                  cli_credentials_get_domain(cred));
208         status = cli_credentials_set_secrets(cred, lp_ctx, NULL,
209                                              SECRETS_PRIMARY_DOMAIN_DN,
210                                              filter, &error_string);
211         if (!NT_STATUS_IS_OK(status)) {
212                 DEBUG(1, ("Could not find machine account in secrets database: %s: %s", nt_errstr(status), error_string));
213                 talloc_free(error_string);
214         }
215         return status;
216 }
217
218 /**
219  * Fill in credentials for the machine trust account, from the secrets database.
220  * 
221  * @param cred Credentials structure to fill in
222  * @retval NTSTATUS error detailing any failure
223  */
224 NTSTATUS cli_credentials_set_krbtgt(struct cli_credentials *cred,
225                                     struct loadparm_context *lp_ctx)
226 {
227         NTSTATUS status;
228         char *filter;
229         char *error_string;
230         /* Bleh, nasty recursion issues: We are setting a machine
231          * account here, so we don't want the 'pending' flag around
232          * any more */
233         cred->machine_account_pending = false;
234         filter = talloc_asprintf(cred, SECRETS_KRBTGT_SEARCH,
235                                        cli_credentials_get_realm(cred),
236                                        cli_credentials_get_domain(cred));
237         status = cli_credentials_set_secrets(cred, lp_ctx, NULL,
238                                              SECRETS_PRINCIPALS_DN,
239                                              filter, &error_string);
240         if (!NT_STATUS_IS_OK(status)) {
241                 DEBUG(1, ("Could not find krbtgt (master Kerberos) account in secrets database: %s: %s", nt_errstr(status), error_string));
242                 talloc_free(error_string);
243         }
244         return status;
245 }
246
247 /**
248  * Fill in credentials for a particular prinicpal, from the secrets database.
249  * 
250  * @param cred Credentials structure to fill in
251  * @retval NTSTATUS error detailing any failure
252  */
253 _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
254                                               struct loadparm_context *lp_ctx,
255                                               const char *serviceprincipal)
256 {
257         NTSTATUS status;
258         char *filter;
259         char *error_string;
260         /* Bleh, nasty recursion issues: We are setting a machine
261          * account here, so we don't want the 'pending' flag around
262          * any more */
263         cred->machine_account_pending = false;
264         filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
265                                  cli_credentials_get_realm(cred),
266                                  cli_credentials_get_domain(cred),
267                                  serviceprincipal);
268         status = cli_credentials_set_secrets(cred, lp_ctx, NULL,
269                                              SECRETS_PRINCIPALS_DN, filter,
270                                              &error_string);
271         if (!NT_STATUS_IS_OK(status)) {
272                 DEBUG(1, ("Could not find %s principal in secrets database: %s: %s", serviceprincipal, nt_errstr(status), error_string));
273         }
274         return status;
275 }
276
277 /**
278  * Ask that when required, the credentials system will be filled with
279  * machine trust account, from the secrets database.
280  * 
281  * @param cred Credentials structure to fill in
282  * @note This function is used to call the above function after, rather 
283  *       than during, popt processing.
284  *
285  */
286 _PUBLIC_ void cli_credentials_set_machine_account_pending(struct cli_credentials *cred,
287                                                  struct loadparm_context *lp_ctx)
288 {
289         cred->machine_account_pending = true;
290         cred->machine_account_pending_lp_ctx = lp_ctx;
291 }
292
293