mit_samba: Add ks_is_tgs_principal()
[amitay/samba.git] / source4 / kdc / mit_samba.c
1 /*
2    MIT-Samba4 library
3
4    Copyright (c) 2010, Simo Sorce <idra@samba.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #define TEVENT_DEPRECATED 1
21
22 #include "includes.h"
23 #include "param/param.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "system/kerberos.h"
26 #include <kdb.h>
27 #include "kdc/sdb.h"
28 #include "kdc/sdb_kdb.h"
29 #include "auth/kerberos/kerberos.h"
30 #include "kdc/samba_kdc.h"
31 #include "kdc/pac-glue.h"
32 #include "kdc/db-glue.h"
33
34 #include "mit_samba.h"
35
36 void mit_samba_context_free(struct mit_samba_context *ctx)
37 {
38         /* free heimdal's krb5_context */
39         if (ctx->context) {
40                 krb5_free_context(ctx->context);
41         }
42
43         /* then free everything else */
44         talloc_free(ctx);
45 }
46
47 int mit_samba_context_init(struct mit_samba_context **_ctx)
48 {
49         NTSTATUS status;
50         struct mit_samba_context *ctx;
51         const char *s4_conf_file;
52         int ret;
53         struct samba_kdc_base_context base_ctx;
54
55         ctx = talloc_zero(NULL, struct mit_samba_context);
56         if (!ctx) {
57                 ret = ENOMEM;
58                 goto done;
59         }
60
61         base_ctx.ev_ctx = tevent_context_init(ctx);
62         if (!base_ctx.ev_ctx) {
63                 ret = ENOMEM;
64                 goto done;
65         }
66         tevent_loop_allow_nesting(base_ctx.ev_ctx);
67         base_ctx.lp_ctx = loadparm_init_global(false);
68         if (!base_ctx.lp_ctx) {
69                 ret = ENOMEM;
70                 goto done;
71         }
72         /* init s4 configuration */
73         s4_conf_file = lpcfg_configfile(base_ctx.lp_ctx);
74         if (s4_conf_file) {
75                 lpcfg_load(base_ctx.lp_ctx, s4_conf_file);
76         } else {
77                 lpcfg_load_default(base_ctx.lp_ctx);
78         }
79
80         status = samba_kdc_setup_db_ctx(ctx, &base_ctx, &ctx->db_ctx);
81         if (!NT_STATUS_IS_OK(status)) {
82                 ret = EINVAL;
83                 goto done;
84         }
85
86         /* init heimdal's krb_context and log facilities */
87         ret = smb_krb5_init_context_basic(ctx,
88                                           ctx->db_ctx->lp_ctx,
89                                           &ctx->context);
90         if (ret) {
91                 goto done;
92         }
93
94         ret = 0;
95
96 done:
97         if (ret) {
98                 mit_samba_context_free(ctx);
99         } else {
100                 *_ctx = ctx;
101         }
102         return ret;
103 }
104
105 static krb5_error_code ks_is_tgs_principal(struct mit_samba_context *ctx,
106                                            krb5_const_principal principal)
107 {
108         char *p;
109         int eq = -1;
110
111         p = smb_krb5_principal_get_comp_string(ctx, ctx->context, principal, 0);
112
113         eq = krb5_princ_size(ctx->context, principal) == 2 &&
114              (strcmp(p, KRB5_TGS_NAME) == 0);
115
116         talloc_free(p);
117
118         return eq;
119 }
120
121 int mit_samba_get_principal(struct mit_samba_context *ctx,
122                             krb5_const_principal principal,
123                             unsigned int kflags,
124                             krb5_db_entry **_kentry)
125 {
126         struct sdb_entry_ex sentry;
127         krb5_db_entry *kentry;
128         int ret;
129         int sflags = 0;
130
131         kentry = malloc(sizeof(krb5_db_entry));
132         if (kentry == NULL) {
133                 return ENOMEM;
134         }
135
136         if (kflags & KRB5_KDB_FLAG_CANONICALIZE) {
137                 sflags |= SDB_F_CANON;
138         }
139         if (kflags & (KRB5_KDB_FLAG_CLIENT_REFERRALS_ONLY |
140                       KRB5_KDB_FLAG_INCLUDE_PAC)) {
141                 sflags |= SDB_F_GET_CLIENT;
142         } else if (ks_is_tgs_principal(ctx, principal)) {
143                 sflags |= SDB_F_GET_KRBTGT;
144         } else {
145                 sflags |= SDB_F_GET_ANY;
146         }
147
148         /* always set this or the created_by data will not be populated by samba's
149          * backend and we will fail to parse the entry later */
150         sflags |= SDB_F_ADMIN_DATA;
151
152         ret = samba_kdc_fetch(ctx->context, ctx->db_ctx,
153                               principal, sflags, 0, &sentry);
154         switch (ret) {
155         case 0:
156                 break;
157         case SDB_ERR_NOENTRY:
158                 ret = KRB5_KDB_NOENTRY;
159                 goto done;
160         case SDB_ERR_WRONG_REALM:
161                 ret = KRB5KDC_ERR_WRONG_REALM;
162                 break;
163         case SDB_ERR_NOT_FOUND_HERE:
164                 /* FIXME: RODC support */
165         default:
166                 goto done;
167         }
168
169         ret = sdb_entry_ex_to_kdb_entry_ex(ctx->context, &sentry, kentry);
170
171         sdb_free_entry(&sentry);
172
173 done:
174         if (ret) {
175                 free(kentry);
176         } else {
177                 *_kentry = kentry;
178         }
179         return ret;
180 }
181
182 int mit_samba_get_firstkey(struct mit_samba_context *ctx,
183                            krb5_db_entry **_kentry)
184 {
185         struct sdb_entry_ex sentry;
186         krb5_db_entry *kentry;
187         int ret;
188
189         kentry = malloc(sizeof(krb5_db_entry));
190         if (kentry == NULL) {
191                 return ENOMEM;
192         }
193
194         ret = samba_kdc_firstkey(ctx->context, ctx->db_ctx, &sentry);
195         switch (ret) {
196         case 0:
197                 break;
198         case SDB_ERR_NOENTRY:
199                 free(kentry);
200                 return KRB5_KDB_NOENTRY;
201         case SDB_ERR_NOT_FOUND_HERE:
202                 /* FIXME: RODC support */
203         default:
204                 free(kentry);
205                 return ret;
206         }
207
208         ret = sdb_entry_ex_to_kdb_entry_ex(ctx->context, &sentry, kentry);
209
210         sdb_free_entry(&sentry);
211
212         if (ret) {
213                 free(kentry);
214         } else {
215                 *_kentry = kentry;
216         }
217         return ret;
218 }
219
220 int mit_samba_get_nextkey(struct mit_samba_context *ctx,
221                           krb5_db_entry **_kentry)
222 {
223         struct sdb_entry_ex sentry;
224         krb5_db_entry *kentry;
225         int ret;
226
227         kentry = malloc(sizeof(krb5_db_entry));
228         if (kentry == NULL) {
229                 return ENOMEM;
230         }
231
232         ret = samba_kdc_nextkey(ctx->context, ctx->db_ctx, &sentry);
233         switch (ret) {
234         case 0:
235                 break;
236         case SDB_ERR_NOENTRY:
237                 free(kentry);
238                 return KRB5_KDB_NOENTRY;
239         case SDB_ERR_NOT_FOUND_HERE:
240                 /* FIXME: RODC support */
241         default:
242                 free(kentry);
243                 return ret;
244         }
245
246         ret = sdb_entry_ex_to_kdb_entry_ex(ctx->context, &sentry, kentry);
247
248         sdb_free_entry(&sentry);
249
250         if (ret) {
251                 free(kentry);
252         } else {
253                 *_kentry = kentry;
254         }
255         return ret;
256 }
257
258 int mit_samba_get_pac_data(struct mit_samba_context *ctx,
259                            krb5_db_entry *client,
260                            DATA_BLOB *data)
261 {
262         TALLOC_CTX *tmp_ctx;
263         DATA_BLOB *pac_blob;
264         NTSTATUS nt_status;
265         struct samba_kdc_entry *skdc_entry;
266
267         skdc_entry = talloc_get_type_abort(client->e_data,
268                                            struct samba_kdc_entry);
269
270         tmp_ctx = talloc_named(ctx, 0, "mit_samba_get_pac_data context");
271         if (!tmp_ctx) {
272                 return ENOMEM;
273         }
274
275         nt_status = samba_kdc_get_pac_blob(tmp_ctx, skdc_entry, &pac_blob);
276         if (!NT_STATUS_IS_OK(nt_status)) {
277                 talloc_free(tmp_ctx);
278                 return EINVAL;
279         }
280
281         data->data = (uint8_t *)malloc(pac_blob->length);
282         if (!data->data) {
283                 talloc_free(tmp_ctx);
284                 return ENOMEM;
285         }
286         memcpy(data->data, pac_blob->data, pac_blob->length);
287         data->length = pac_blob->length;
288
289         talloc_free(tmp_ctx);
290         return 0;
291 }
292
293 int mit_samba_update_pac_data(struct mit_samba_context *ctx,
294                               krb5_db_entry *client,
295                               DATA_BLOB *pac_data,
296                               DATA_BLOB *logon_data)
297 {
298         TALLOC_CTX *tmp_ctx;
299         DATA_BLOB *logon_blob;
300         krb5_error_code code;
301         NTSTATUS nt_status;
302         krb5_pac pac = NULL;
303         int ret;
304         struct samba_kdc_entry *skdc_entry = NULL;
305
306         if (client) {
307                 skdc_entry = talloc_get_type_abort(client->e_data,
308                                                    struct samba_kdc_entry);
309         }
310
311         /* The user account may be set not to want the PAC */
312         if (client && !samba_princ_needs_pac(skdc_entry)) {
313                 return EINVAL;
314         }
315
316         tmp_ctx = talloc_named(ctx, 0, "mit_samba_update_pac_data context");
317         if (!tmp_ctx) {
318                 return ENOMEM;
319         }
320
321         logon_blob = talloc_zero(tmp_ctx, DATA_BLOB);
322         if (!logon_blob) {
323                 ret = ENOMEM;
324                 goto done;
325         }
326
327         code = krb5_pac_parse(ctx->context,
328                               pac_data->data, pac_data->length, &pac);
329         if (code) {
330                 ret = EINVAL;
331                 goto done;
332         }
333
334         /* TODO: An implementation-specific decision will need to be
335          * made as to when to check the KDC pac signature, and how to
336          * untrust untrusted RODCs */
337         nt_status = samba_kdc_update_pac_blob(tmp_ctx, ctx->context,
338                                               pac, logon_blob, NULL, NULL);
339         if (!NT_STATUS_IS_OK(nt_status)) {
340                 DEBUG(0, ("Building PAC failed: %s\n",
341                           nt_errstr(nt_status)));
342                 ret = EINVAL;
343                 goto done;
344         }
345
346         logon_data->data = (uint8_t *)malloc(logon_blob->length);
347         if (!logon_data->data) {
348                 ret = ENOMEM;
349                 goto done;
350         }
351         memcpy(logon_data->data, logon_blob->data, logon_blob->length);
352         logon_data->length = logon_blob->length;
353
354         ret = 0;
355
356 done:
357         if (pac) krb5_pac_free(ctx->context, pac);
358         talloc_free(tmp_ctx);
359         return ret;
360 }
361
362 /* provide header, function is exported but there are no public headers */
363
364 krb5_error_code encode_krb5_padata_sequence(krb5_pa_data *const *rep, krb5_data **code);
365
366 /* this function allocates 'data' using malloc.
367  * The caller is responsible for freeing it */
368 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
369 {
370         krb5_error_code ret = 0;
371         krb5_pa_data pa, *ppa = NULL;
372         krb5_data *d = NULL;
373
374         if (!e_data)
375                 return;
376
377         e_data->data   = NULL;
378         e_data->length = 0;
379
380         pa.magic                = KV5M_PA_DATA;
381         pa.pa_type              = KRB5_PADATA_PW_SALT;
382         pa.length               = 12;
383         pa.contents             = malloc(pa.length);
384         if (!pa.contents) {
385                 return;
386         }
387
388         SIVAL(pa.contents, 0, NT_STATUS_V(nt_status));
389         SIVAL(pa.contents, 4, 0);
390         SIVAL(pa.contents, 8, 1);
391
392         ppa = &pa;
393
394         ret = encode_krb5_padata_sequence(&ppa, &d);
395         free(pa.contents);
396         if (ret) {
397                 return;
398         }
399
400         e_data->data   = (uint8_t *)d->data;
401         e_data->length = d->length;
402
403         /* free d, not d->data - gd */
404         free(d);
405
406         return;
407 }
408
409 int mit_samba_check_client_access(struct mit_samba_context *ctx,
410                                   krb5_db_entry *client,
411                                   const char *client_name,
412                                   krb5_db_entry *server,
413                                   const char *server_name,
414                                   const char *netbios_name,
415                                   bool password_change,
416                                   DATA_BLOB *e_data)
417 {
418         struct samba_kdc_entry *skdc_entry;
419         NTSTATUS nt_status;
420
421         skdc_entry = talloc_get_type(client->e_data, struct samba_kdc_entry);
422
423         nt_status = samba_kdc_check_client_access(skdc_entry,
424                                                   client_name,
425                                                   netbios_name,
426                                                   password_change);
427
428         if (!NT_STATUS_IS_OK(nt_status)) {
429                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
430                         return ENOMEM;
431                 }
432
433                 samba_kdc_build_edata_reply(nt_status, e_data);
434
435                 return samba_kdc_map_policy_err(nt_status);
436         }
437
438         return 0;
439 }
440
441 int mit_samba_check_s4u2proxy(struct mit_samba_context *ctx,
442                               krb5_db_entry *kentry,
443                               const char *target_name,
444                               bool is_nt_enterprise_name)
445 {
446 #if 1
447         /*
448          * This is disabled because mit_samba_update_pac_data() does not handle
449          * S4U_DELEGATION_INFO
450          */
451
452         return KRB5KDC_ERR_BADOPTION;
453 #else
454         krb5_principal target_principal;
455         int flags = 0;
456         int ret;
457
458         if (is_nt_enterprise_name) {
459                 flags = KRB5_PRINCIPAL_PARSE_ENTERPRISE;
460         }
461
462         ret = krb5_parse_name_flags(ctx->context, target_name,
463                                     flags, &target_principal);
464         if (ret) {
465                 return ret;
466         }
467
468         ret = samba_kdc_check_s4u2proxy(ctx->context,
469                                         ctx->db_ctx,
470                                         skdc_entry,
471                                         target_principal);
472
473         krb5_free_principal(ctx->context, target_principal);
474
475         return ret;
476 #endif
477 }