lib/krb5_wrap: provide KRB5KDC_ERR_KEY_EXPIRED error code matching MIT.
[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 hdb_entry_ex *princ)
139 {
140
141         struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
142         uint32_t userAccountControl;
143
144
145         /* The service account may be set not to want the PAC */
146         userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
147         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
148                 return false;
149         }
150
151         return true;
152 }
153
154 /* Was the krbtgt in this DB (ie, should we check the incoming signature) and was it an RODC */
155 int samba_krbtgt_is_in_db(struct hdb_entry_ex *princ, bool *is_in_db, bool *is_untrusted)
156 {
157         NTSTATUS status;
158         struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
159         int rodc_krbtgt_number, trust_direction;
160         uint32_t rid;
161
162         TALLOC_CTX *mem_ctx = talloc_new(NULL);
163         if (!mem_ctx) {
164                 return ENOMEM;
165         }
166         
167         trust_direction = ldb_msg_find_attr_as_int(p->msg, "trustDirection", 0);
168
169         if (trust_direction != 0) {
170                 /* Domain trust - we cannot check the sig, but we trust it for a correct PAC
171                    
172                    This is exactly where we should flag for SID
173                    validation when we do inter-foreest trusts
174                  */
175                 talloc_free(mem_ctx);
176                 *is_untrusted = false;
177                 *is_in_db = false;
178                 return 0;
179         }
180
181         /* The lack of password controls etc applies to krbtgt by
182          * virtue of being that particular RID */
183         status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, p->msg, "objectSid"), NULL, &rid);
184
185         if (!NT_STATUS_IS_OK(status)) {
186                 talloc_free(mem_ctx);
187                 return EINVAL;
188         }
189
190         rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
191
192         if (p->kdc_db_ctx->my_krbtgt_number == 0) {
193                 if (rid == DOMAIN_RID_KRBTGT) {
194                         *is_untrusted = false;
195                         *is_in_db = true;
196                         talloc_free(mem_ctx);
197                         return 0;
198                 } else if (rodc_krbtgt_number != -1) {
199                         *is_in_db = true;
200                         *is_untrusted = true;
201                         talloc_free(mem_ctx);
202                         return 0;
203                 }
204         } else if ((rid != DOMAIN_RID_KRBTGT) && (rodc_krbtgt_number == p->kdc_db_ctx->my_krbtgt_number)) {
205                 talloc_free(mem_ctx);
206                 *is_untrusted = false;
207                 *is_in_db = true;
208                 return 0;
209         } else if (rid == DOMAIN_RID_KRBTGT) {
210                 /* krbtgt viewed from an RODC */
211                 talloc_free(mem_ctx);
212                 *is_untrusted = false;
213                 *is_in_db = false;
214                 return 0;
215         }
216
217         /* Another RODC */
218         talloc_free(mem_ctx);
219         *is_untrusted = true;
220         *is_in_db = false;
221         return 0;
222 }
223
224 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
225                                 struct hdb_entry_ex *client,
226                                 DATA_BLOB **_pac_blob)
227 {
228         struct samba_kdc_entry *p = talloc_get_type(client->ctx, struct samba_kdc_entry);
229         struct auth_user_info_dc *user_info_dc;
230         DATA_BLOB *pac_blob;
231         NTSTATUS nt_status;
232
233         /* The user account may be set not to want the PAC */
234         if ( ! samba_princ_needs_pac(client)) {
235                 *_pac_blob = NULL;
236                 return NT_STATUS_OK;
237         }
238
239         pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
240         if (!pac_blob) {
241                 return NT_STATUS_NO_MEMORY;
242         }
243
244         nt_status = authsam_make_user_info_dc(mem_ctx, p->kdc_db_ctx->samdb,
245                                              lpcfg_netbios_name(p->kdc_db_ctx->lp_ctx),
246                                              lpcfg_sam_name(p->kdc_db_ctx->lp_ctx),
247                                              p->realm_dn,
248                                              p->msg,
249                                              data_blob(NULL, 0),
250                                              data_blob(NULL, 0),
251                                              &user_info_dc);
252         if (!NT_STATUS_IS_OK(nt_status)) {
253                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
254                           nt_errstr(nt_status)));
255                 return nt_status;
256         }
257
258         nt_status = samba_get_logon_info_pac_blob(mem_ctx, user_info_dc, pac_blob);
259         if (!NT_STATUS_IS_OK(nt_status)) {
260                 DEBUG(0, ("Building PAC failed: %s\n",
261                           nt_errstr(nt_status)));
262                 return nt_status;
263         }
264
265         *_pac_blob = pac_blob;
266         return NT_STATUS_OK;
267 }
268
269 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
270                                    krb5_context context,
271                                    const krb5_pac pac, DATA_BLOB *pac_blob,
272                                    struct PAC_SIGNATURE_DATA *pac_srv_sig,
273                                    struct PAC_SIGNATURE_DATA *pac_kdc_sig)
274 {
275         struct auth_user_info_dc *user_info_dc;
276         krb5_error_code ret;
277         NTSTATUS nt_status;
278
279         ret = kerberos_pac_to_user_info_dc(mem_ctx, pac,
280                                            context, &user_info_dc, pac_srv_sig, pac_kdc_sig);
281         if (ret) {
282                 return NT_STATUS_UNSUCCESSFUL;
283         }
284
285         nt_status = samba_get_logon_info_pac_blob(mem_ctx, 
286                                                   user_info_dc, pac_blob);
287
288         return nt_status;
289 }
290
291 NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
292                                 krb5_context context,
293                                 const krb5_pac pac,
294                                 const krb5_principal server_principal,
295                                 const krb5_principal proxy_principal,
296                                 DATA_BLOB *new_blob)
297 {
298         krb5_data old_data;
299         DATA_BLOB old_blob;
300         krb5_error_code ret;
301         NTSTATUS nt_status;
302         enum ndr_err_code ndr_err;
303         union PAC_INFO info;
304         struct PAC_CONSTRAINED_DELEGATION _d;
305         struct PAC_CONSTRAINED_DELEGATION *d = NULL;
306         char *server = NULL;
307         char *proxy = NULL;
308         uint32_t i;
309         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
310
311         if (tmp_ctx == NULL) {
312                 return NT_STATUS_NO_MEMORY;
313         }
314
315         ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_CONSTRAINED_DELEGATION, &old_data);
316         if (ret == ENOENT) {
317                 ZERO_STRUCT(old_data);
318         } else if (ret) {
319                 talloc_free(tmp_ctx);
320                 return NT_STATUS_UNSUCCESSFUL;
321         }
322
323         old_blob.length = old_data.length;
324         old_blob.data = (uint8_t *)old_data.data;
325
326         ZERO_STRUCT(info);
327         if (old_blob.length > 0) {
328                 ndr_err = ndr_pull_union_blob(&old_blob, mem_ctx,
329                                 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
330                                 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
331                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
332                         kerberos_free_data_contents(context, &old_data);
333                         nt_status = ndr_map_error2ntstatus(ndr_err);
334                         DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
335                         talloc_free(tmp_ctx);
336                         return nt_status;
337                 }
338         } else {
339                 ZERO_STRUCT(_d);
340                 info.constrained_delegation.info = &_d;
341         }
342         kerberos_free_data_contents(context, &old_data);
343
344         ret = krb5_unparse_name(context, server_principal, &server);
345         if (ret) {
346                 talloc_free(tmp_ctx);
347                 return NT_STATUS_INTERNAL_ERROR;
348         }
349
350         ret = krb5_unparse_name_flags(context, proxy_principal,
351                                       KRB5_PRINCIPAL_UNPARSE_NO_REALM, &proxy);
352         if (ret) {
353                 SAFE_FREE(server);
354                 talloc_free(tmp_ctx);
355                 return NT_STATUS_INTERNAL_ERROR;
356         }
357
358         d = info.constrained_delegation.info;
359         i = d->num_transited_services;
360         d->proxy_target.string = server;
361         d->transited_services = talloc_realloc(mem_ctx, d->transited_services,
362                                                struct lsa_String, i + 1);
363         d->transited_services[i].string = proxy;
364         d->num_transited_services = i + 1;
365
366         ndr_err = ndr_push_union_blob(new_blob, mem_ctx,
367                                 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
368                                 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
369         SAFE_FREE(server);
370         SAFE_FREE(proxy);
371         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
372                 kerberos_free_data_contents(context, &old_data);
373                 nt_status = ndr_map_error2ntstatus(ndr_err);
374                 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
375                 talloc_free(tmp_ctx);
376                 return nt_status;
377         }
378
379         talloc_free(tmp_ctx);
380         return NT_STATUS_OK;
381 }
382
383 /* this function allocates 'data' using malloc.
384  * The caller is responsible for freeing it */
385 void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
386 {
387         PA_DATA pa;
388         unsigned char *buf;
389         size_t len;
390         krb5_error_code ret = 0;
391
392         if (!e_data)
393                 return;
394
395         pa.padata_type          = KRB5_PADATA_PW_SALT;
396         pa.padata_value.length  = 12;
397         pa.padata_value.data    = malloc(pa.padata_value.length);
398         if (!pa.padata_value.data) {
399                 e_data->length = 0;
400                 e_data->data = NULL;
401                 return;
402         }
403
404         SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
405         SIVAL(pa.padata_value.data, 4, 0);
406         SIVAL(pa.padata_value.data, 8, 1);
407
408         ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
409         free(pa.padata_value.data);
410
411         e_data->data   = buf;
412         e_data->length = len;
413
414         return;
415 }
416
417 /* function to map policy errors */
418 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
419 {
420         krb5_error_code ret;
421
422         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
423                 ret = KRB5KDC_ERR_KEY_EXP;
424         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
425                 ret = KRB5KDC_ERR_KEY_EXP;
426         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
427                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
428         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
429                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
430         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
431                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
432         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
433                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
434         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
435                 ret = KRB5KDC_ERR_POLICY;
436         else
437                 ret = KRB5KDC_ERR_POLICY;
438
439         return ret;
440 }
441
442 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
443  * for consistency */
444 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
445                                        const char *client_name,
446                                        const char *workstation,
447                                        bool password_change)
448 {
449         TALLOC_CTX *tmp_ctx;
450         NTSTATUS nt_status;
451
452         tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
453         if (!tmp_ctx) {
454                 return NT_STATUS_NO_MEMORY;
455         }
456
457         /* we allow all kinds of trusts here */
458         nt_status = authsam_account_ok(tmp_ctx,
459                                        kdc_entry->kdc_db_ctx->samdb,
460                                        MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
461                                        MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
462                                        kdc_entry->realm_dn, kdc_entry->msg,
463                                        workstation, client_name,
464                                        true, password_change);
465
466         talloc_free(tmp_ctx);
467         return nt_status;
468 }
469
470 int kdc_check_pac(krb5_context context,
471                   DATA_BLOB srv_sig,
472                   struct PAC_SIGNATURE_DATA *kdc_sig,
473                   hdb_entry_ex *ent)
474 {
475         krb5_enctype etype;
476         int ret;
477         krb5_keyblock keyblock;
478         Key *key;
479         if (kdc_sig->type == CKSUMTYPE_HMAC_MD5) {
480                 etype = ENCTYPE_ARCFOUR_HMAC;
481         } else {
482                 ret = krb5_cksumtype_to_enctype(context, 
483                                                 kdc_sig->type,
484                                                 &etype);
485                 if (ret != 0) {
486                         return ret;
487                 }
488         }
489
490 #if HDB_ENCTYPE2KEY_TAKES_KEYSET
491         ret = hdb_enctype2key(context, &ent->entry, NULL, etype, &key);
492 #else
493         ret = hdb_enctype2key(context, &ent->entry, etype, &key);
494 #endif
495
496         if (ret != 0) {
497                 return ret;
498         }
499
500         keyblock = key->key;
501
502         return check_pac_checksum(srv_sig, kdc_sig,
503                                  context, &keyblock);
504 }
505
506
507