s4-kdc: pass down only a samba_kdc_entry to samba_krbtgt_is_in_db().
[kai/samba-autobuild/.git] / source4 / kdc / pac-glue.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    PAC Glue between Samba and the KDC
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7    Copyright (C) Simo Sorce <idra@samba.org> 2010
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
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 "../libds/common/flags.h"
26 #include <ldb.h>
27 #include "auth/auth.h"
28 #include "auth/auth_sam_reply.h"
29 #include "system/kerberos.h"
30 #include "auth/kerberos/kerberos.h"
31 #include <hdb.h>
32 #include "kdc/samba_kdc.h"
33 #include "kdc/pac-glue.h"
34 #include "param/param.h"
35 #include "librpc/gen_ndr/ndr_krb5pac.h"
36 #include "libcli/security/security.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "auth/kerberos/pac_utils.h"
39
40 static
41 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
42                                        struct auth_user_info_dc *info,
43                                        DATA_BLOB *pac_data)
44 {
45         struct netr_SamInfo3 *info3;
46         union PAC_INFO pac_info;
47         enum ndr_err_code ndr_err;
48         NTSTATUS nt_status;
49
50         ZERO_STRUCT(pac_info);
51
52         nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx, info, &info3);
53         if (!NT_STATUS_IS_OK(nt_status)) {
54                 DEBUG(1, ("Getting Samba info failed: %s\n",
55                           nt_errstr(nt_status)));
56                 return nt_status;
57         }
58
59         pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
60         if (!pac_info.logon_info.info) {
61                 return NT_STATUS_NO_MEMORY;
62         }
63
64         pac_info.logon_info.info->info3 = *info3;
65
66         ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
67                                       PAC_TYPE_LOGON_INFO,
68                                       (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
69         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
70                 nt_status = ndr_map_error2ntstatus(ndr_err);
71                 DEBUG(1, ("PAC (presig) push failed: %s\n",
72                           nt_errstr(nt_status)));
73                 return nt_status;
74         }
75
76         return NT_STATUS_OK;
77 }
78
79 krb5_error_code samba_make_krb5_pac(krb5_context context,
80                                     DATA_BLOB *pac_blob,
81                                     DATA_BLOB *deleg_blob,
82                                     krb5_pac *pac)
83 {
84         krb5_data pac_data;
85         krb5_data deleg_data;
86         krb5_error_code ret;
87
88         /* The user account may be set not to want the PAC */
89         if (!pac_blob) {
90                 return 0;
91         }
92
93         ret = krb5_copy_data_contents(&pac_data,
94                                       pac_blob->data,
95                                       pac_blob->length);
96         if (ret != 0) {
97                 return ret;
98         }
99
100         ZERO_STRUCT(deleg_data);
101         if (deleg_blob) {
102                 ret = krb5_copy_data_contents(&deleg_data,
103                                               deleg_blob->data,
104                                               deleg_blob->length);
105                 if (ret != 0) {
106                         kerberos_free_data_contents(context, &pac_data);
107                         return ret;
108                 }
109         }
110
111         ret = krb5_pac_init(context, pac);
112         if (ret != 0) {
113                 kerberos_free_data_contents(context, &pac_data);
114                 kerberos_free_data_contents(context, &deleg_data);
115                 return ret;
116         }
117
118         ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
119         kerberos_free_data_contents(context, &pac_data);
120         if (ret != 0) {
121                 kerberos_free_data_contents(context, &deleg_data);
122                 return ret;
123         }
124
125         if (deleg_blob) {
126                 ret = krb5_pac_add_buffer(context, *pac,
127                                           PAC_TYPE_CONSTRAINED_DELEGATION,
128                                           &deleg_data);
129                 kerberos_free_data_contents(context, &deleg_data);
130                 if (ret != 0) {
131                         return ret;
132                 }
133         }
134
135         return ret;
136 }
137
138 bool samba_princ_needs_pac(struct samba_kdc_entry *skdc_entry)
139 {
140
141         uint32_t userAccountControl;
142
143         /* The service account may be set not to want the PAC */
144         userAccountControl = ldb_msg_find_attr_as_uint(skdc_entry->msg, "userAccountControl", 0);
145         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
146                 return false;
147         }
148
149         return true;
150 }
151
152 /* Was the krbtgt in this DB (ie, should we check the incoming signature) and was it an RODC */
153 int samba_krbtgt_is_in_db(struct samba_kdc_entry *p,
154                           bool *is_in_db,
155                           bool *is_untrusted)
156 {
157         NTSTATUS status;
158         int rodc_krbtgt_number, trust_direction;
159         uint32_t rid;
160
161         TALLOC_CTX *mem_ctx = talloc_new(NULL);
162         if (!mem_ctx) {
163                 return ENOMEM;
164         }
165         
166         trust_direction = ldb_msg_find_attr_as_int(p->msg, "trustDirection", 0);
167
168         if (trust_direction != 0) {
169                 /* Domain trust - we cannot check the sig, but we trust it for a correct PAC
170                    
171                    This is exactly where we should flag for SID
172                    validation when we do inter-foreest trusts
173                  */
174                 talloc_free(mem_ctx);
175                 *is_untrusted = false;
176                 *is_in_db = false;
177                 return 0;
178         }
179
180         /* The lack of password controls etc applies to krbtgt by
181          * virtue of being that particular RID */
182         status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, p->msg, "objectSid"), NULL, &rid);
183
184         if (!NT_STATUS_IS_OK(status)) {
185                 talloc_free(mem_ctx);
186                 return EINVAL;
187         }
188
189         rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
190
191         if (p->kdc_db_ctx->my_krbtgt_number == 0) {
192                 if (rid == DOMAIN_RID_KRBTGT) {
193                         *is_untrusted = false;
194                         *is_in_db = true;
195                         talloc_free(mem_ctx);
196                         return 0;
197                 } else if (rodc_krbtgt_number != -1) {
198                         *is_in_db = true;
199                         *is_untrusted = true;
200                         talloc_free(mem_ctx);
201                         return 0;
202                 }
203         } else if ((rid != DOMAIN_RID_KRBTGT) && (rodc_krbtgt_number == p->kdc_db_ctx->my_krbtgt_number)) {
204                 talloc_free(mem_ctx);
205                 *is_untrusted = false;
206                 *is_in_db = true;
207                 return 0;
208         } else if (rid == DOMAIN_RID_KRBTGT) {
209                 /* krbtgt viewed from an RODC */
210                 talloc_free(mem_ctx);
211                 *is_untrusted = false;
212                 *is_in_db = false;
213                 return 0;
214         }
215
216         /* Another RODC */
217         talloc_free(mem_ctx);
218         *is_untrusted = true;
219         *is_in_db = false;
220         return 0;
221 }
222
223 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
224                                 struct samba_kdc_entry *p,
225                                 DATA_BLOB **_pac_blob)
226 {
227         struct auth_user_info_dc *user_info_dc;
228         DATA_BLOB *pac_blob;
229         NTSTATUS nt_status;
230
231         /* The user account may be set not to want the PAC */
232         if ( ! samba_princ_needs_pac(p)) {
233                 *_pac_blob = NULL;
234                 return NT_STATUS_OK;
235         }
236
237         pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
238         if (!pac_blob) {
239                 return NT_STATUS_NO_MEMORY;
240         }
241
242         nt_status = authsam_make_user_info_dc(mem_ctx, p->kdc_db_ctx->samdb,
243                                              lpcfg_netbios_name(p->kdc_db_ctx->lp_ctx),
244                                              lpcfg_sam_name(p->kdc_db_ctx->lp_ctx),
245                                              p->realm_dn,
246                                              p->msg,
247                                              data_blob(NULL, 0),
248                                              data_blob(NULL, 0),
249                                              &user_info_dc);
250         if (!NT_STATUS_IS_OK(nt_status)) {
251                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
252                           nt_errstr(nt_status)));
253                 return nt_status;
254         }
255
256         nt_status = samba_get_logon_info_pac_blob(mem_ctx, user_info_dc, pac_blob);
257         if (!NT_STATUS_IS_OK(nt_status)) {
258                 DEBUG(0, ("Building PAC failed: %s\n",
259                           nt_errstr(nt_status)));
260                 return nt_status;
261         }
262
263         *_pac_blob = pac_blob;
264         return NT_STATUS_OK;
265 }
266
267 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
268                                    krb5_context context,
269                                    const krb5_pac pac, DATA_BLOB *pac_blob,
270                                    struct PAC_SIGNATURE_DATA *pac_srv_sig,
271                                    struct PAC_SIGNATURE_DATA *pac_kdc_sig)
272 {
273         struct auth_user_info_dc *user_info_dc;
274         krb5_error_code ret;
275         NTSTATUS nt_status;
276
277         ret = kerberos_pac_to_user_info_dc(mem_ctx, pac,
278                                            context, &user_info_dc, pac_srv_sig, pac_kdc_sig);
279         if (ret) {
280                 return NT_STATUS_UNSUCCESSFUL;
281         }
282
283         nt_status = samba_get_logon_info_pac_blob(mem_ctx, 
284                                                   user_info_dc, pac_blob);
285
286         return nt_status;
287 }
288
289 NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
290                                 krb5_context context,
291                                 const krb5_pac pac,
292                                 const krb5_principal server_principal,
293                                 const krb5_principal proxy_principal,
294                                 DATA_BLOB *new_blob)
295 {
296         krb5_data old_data;
297         DATA_BLOB old_blob;
298         krb5_error_code ret;
299         NTSTATUS nt_status;
300         enum ndr_err_code ndr_err;
301         union PAC_INFO info;
302         struct PAC_CONSTRAINED_DELEGATION _d;
303         struct PAC_CONSTRAINED_DELEGATION *d = NULL;
304         char *server = NULL;
305         char *proxy = NULL;
306         uint32_t i;
307         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
308
309         if (tmp_ctx == NULL) {
310                 return NT_STATUS_NO_MEMORY;
311         }
312
313         ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_CONSTRAINED_DELEGATION, &old_data);
314         if (ret == ENOENT) {
315                 ZERO_STRUCT(old_data);
316         } else if (ret) {
317                 talloc_free(tmp_ctx);
318                 return NT_STATUS_UNSUCCESSFUL;
319         }
320
321         old_blob.length = old_data.length;
322         old_blob.data = (uint8_t *)old_data.data;
323
324         ZERO_STRUCT(info);
325         if (old_blob.length > 0) {
326                 ndr_err = ndr_pull_union_blob(&old_blob, mem_ctx,
327                                 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
328                                 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
329                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
330                         kerberos_free_data_contents(context, &old_data);
331                         nt_status = ndr_map_error2ntstatus(ndr_err);
332                         DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
333                         talloc_free(tmp_ctx);
334                         return nt_status;
335                 }
336         } else {
337                 ZERO_STRUCT(_d);
338                 info.constrained_delegation.info = &_d;
339         }
340         kerberos_free_data_contents(context, &old_data);
341
342         ret = krb5_unparse_name(context, server_principal, &server);
343         if (ret) {
344                 talloc_free(tmp_ctx);
345                 return NT_STATUS_INTERNAL_ERROR;
346         }
347
348         ret = krb5_unparse_name_flags(context, proxy_principal,
349                                       KRB5_PRINCIPAL_UNPARSE_NO_REALM, &proxy);
350         if (ret) {
351                 SAFE_FREE(server);
352                 talloc_free(tmp_ctx);
353                 return NT_STATUS_INTERNAL_ERROR;
354         }
355
356         d = info.constrained_delegation.info;
357         i = d->num_transited_services;
358         d->proxy_target.string = server;
359         d->transited_services = talloc_realloc(mem_ctx, d->transited_services,
360                                                struct lsa_String, i + 1);
361         d->transited_services[i].string = proxy;
362         d->num_transited_services = i + 1;
363
364         ndr_err = ndr_push_union_blob(new_blob, mem_ctx,
365                                 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
366                                 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
367         SAFE_FREE(server);
368         SAFE_FREE(proxy);
369         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
370                 kerberos_free_data_contents(context, &old_data);
371                 nt_status = ndr_map_error2ntstatus(ndr_err);
372                 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
373                 talloc_free(tmp_ctx);
374                 return nt_status;
375         }
376
377         talloc_free(tmp_ctx);
378         return NT_STATUS_OK;
379 }
380
381 /* this function allocates 'data' using malloc.
382  * The caller is responsible for freeing it */
383 void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
384 {
385         PA_DATA pa;
386         unsigned char *buf;
387         size_t len;
388         krb5_error_code ret = 0;
389
390         if (!e_data)
391                 return;
392
393         pa.padata_type          = KRB5_PADATA_PW_SALT;
394         pa.padata_value.length  = 12;
395         pa.padata_value.data    = malloc(pa.padata_value.length);
396         if (!pa.padata_value.data) {
397                 e_data->length = 0;
398                 e_data->data = NULL;
399                 return;
400         }
401
402         SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
403         SIVAL(pa.padata_value.data, 4, 0);
404         SIVAL(pa.padata_value.data, 8, 1);
405
406         ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
407         free(pa.padata_value.data);
408
409         e_data->data   = buf;
410         e_data->length = len;
411
412         return;
413 }
414
415 /* function to map policy errors */
416 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
417 {
418         krb5_error_code ret;
419
420         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
421                 ret = KRB5KDC_ERR_KEY_EXP;
422         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
423                 ret = KRB5KDC_ERR_KEY_EXP;
424         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
425                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
426         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
427                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
428         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
429                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
430         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
431                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
432         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
433                 ret = KRB5KDC_ERR_POLICY;
434         else
435                 ret = KRB5KDC_ERR_POLICY;
436
437         return ret;
438 }
439
440 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
441  * for consistency */
442 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
443                                        const char *client_name,
444                                        const char *workstation,
445                                        bool password_change)
446 {
447         TALLOC_CTX *tmp_ctx;
448         NTSTATUS nt_status;
449
450         tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
451         if (!tmp_ctx) {
452                 return NT_STATUS_NO_MEMORY;
453         }
454
455         /* we allow all kinds of trusts here */
456         nt_status = authsam_account_ok(tmp_ctx,
457                                        kdc_entry->kdc_db_ctx->samdb,
458                                        MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
459                                        MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
460                                        kdc_entry->realm_dn, kdc_entry->msg,
461                                        workstation, client_name,
462                                        true, password_change);
463
464         talloc_free(tmp_ctx);
465         return nt_status;
466 }
467
468 int kdc_check_pac(krb5_context context,
469                   DATA_BLOB srv_sig,
470                   struct PAC_SIGNATURE_DATA *kdc_sig,
471                   hdb_entry_ex *ent)
472 {
473         krb5_enctype etype;
474         int ret;
475         krb5_keyblock keyblock;
476         Key *key;
477         if (kdc_sig->type == CKSUMTYPE_HMAC_MD5) {
478                 etype = ENCTYPE_ARCFOUR_HMAC;
479         } else {
480                 ret = krb5_cksumtype_to_enctype(context, 
481                                                 kdc_sig->type,
482                                                 &etype);
483                 if (ret != 0) {
484                         return ret;
485                 }
486         }
487
488 #if HDB_ENCTYPE2KEY_TAKES_KEYSET
489         ret = hdb_enctype2key(context, &ent->entry, NULL, etype, &key);
490 #else
491         ret = hdb_enctype2key(context, &ent->entry, etype, &key);
492 #endif
493
494         if (ret != 0) {
495                 return ret;
496         }
497
498         keyblock = key->key;
499
500         return check_pac_checksum(srv_sig, kdc_sig,
501                                  context, &keyblock);
502 }
503
504
505