mit_samba: Directly pass the principal and kflags
[ambi/samba-autobuild/.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(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
106 int mit_samba_get_principal(struct mit_samba_context *ctx,
107                             krb5_const_principal principal,
108                             unsigned int kflags,
109                             krb5_db_entry **_kentry)
110 {
111         struct sdb_entry_ex sentry;
112         krb5_db_entry *kentry;
113         int ret;
114         int sflags = 0;
115
116         kentry = malloc(sizeof(krb5_db_entry));
117         if (kentry == NULL) {
118                 return ENOMEM;
119         }
120
121         if (kflags & KRB5_KDB_FLAG_CANONICALIZE) {
122                 sflags |= SDB_F_CANON;
123         }
124         if (kflags & (KRB5_KDB_FLAG_CLIENT_REFERRALS_ONLY |
125                       KRB5_KDB_FLAG_INCLUDE_PAC)) {
126                 sflags |= SDB_F_GET_CLIENT;
127         } else if (ks_is_tgs_principal(ctx, principal)) {
128                 sflags |= SDB_F_GET_KRBTGT;
129         } else {
130                 sflags |= SDB_F_GET_ANY;
131         }
132
133         /* always set this or the created_by data will not be populated by samba's
134          * backend and we will fail to parse the entry later */
135         sflags |= SDB_F_ADMIN_DATA;
136
137         ret = samba_kdc_fetch(ctx->context, ctx->db_ctx,
138                               principal, sflags, 0, &sentry);
139         switch (ret) {
140         case 0:
141                 break;
142         case SDB_ERR_NOENTRY:
143                 ret = KRB5_KDB_NOENTRY;
144                 goto done;
145         case SDB_ERR_WRONG_REALM:
146                 ret = KRB5KDC_ERR_WRONG_REALM;
147                 break;
148         case SDB_ERR_NOT_FOUND_HERE:
149                 /* FIXME: RODC support */
150         default:
151                 goto done;
152         }
153
154         ret = sdb_entry_ex_to_kdb_entry_ex(ctx->context, &sentry, kentry);
155
156         sdb_free_entry(&sentry);
157
158 done:
159         if (ret) {
160                 free(kentry);
161         } else {
162                 *_kentry = kentry;
163         }
164         return ret;
165 }
166
167 int mit_samba_get_firstkey(struct mit_samba_context *ctx,
168                            krb5_db_entry **_kentry)
169 {
170         struct sdb_entry_ex sentry;
171         krb5_db_entry *kentry;
172         int ret;
173
174         kentry = malloc(sizeof(krb5_db_entry));
175         if (kentry == NULL) {
176                 return ENOMEM;
177         }
178
179         ret = samba_kdc_firstkey(ctx->context, ctx->db_ctx, &sentry);
180         switch (ret) {
181         case 0:
182                 break;
183         case SDB_ERR_NOENTRY:
184                 free(kentry);
185                 return KRB5_KDB_NOENTRY;
186         case SDB_ERR_NOT_FOUND_HERE:
187                 /* FIXME: RODC support */
188         default:
189                 free(kentry);
190                 return ret;
191         }
192
193         ret = sdb_entry_ex_to_kdb_entry_ex(ctx->context, &sentry, kentry);
194
195         sdb_free_entry(&sentry);
196
197         if (ret) {
198                 free(kentry);
199         } else {
200                 *_kentry = kentry;
201         }
202         return ret;
203 }
204
205 int mit_samba_get_nextkey(struct mit_samba_context *ctx,
206                           krb5_db_entry **_kentry)
207 {
208         struct sdb_entry_ex sentry;
209         krb5_db_entry *kentry;
210         int ret;
211
212         kentry = malloc(sizeof(krb5_db_entry));
213         if (kentry == NULL) {
214                 return ENOMEM;
215         }
216
217         ret = samba_kdc_nextkey(ctx->context, ctx->db_ctx, &sentry);
218         switch (ret) {
219         case 0:
220                 break;
221         case SDB_ERR_NOENTRY:
222                 free(kentry);
223                 return KRB5_KDB_NOENTRY;
224         case SDB_ERR_NOT_FOUND_HERE:
225                 /* FIXME: RODC support */
226         default:
227                 free(kentry);
228                 return ret;
229         }
230
231         ret = sdb_entry_ex_to_kdb_entry_ex(ctx->context, &sentry, kentry);
232
233         sdb_free_entry(&sentry);
234
235         if (ret) {
236                 free(kentry);
237         } else {
238                 *_kentry = kentry;
239         }
240         return ret;
241 }
242
243 int mit_samba_get_pac_data(struct mit_samba_context *ctx,
244                            krb5_db_entry *client,
245                            DATA_BLOB *data)
246 {
247         TALLOC_CTX *tmp_ctx;
248         DATA_BLOB *pac_blob;
249         NTSTATUS nt_status;
250         struct samba_kdc_entry *skdc_entry;
251
252         skdc_entry = talloc_get_type_abort(client->e_data,
253                                            struct samba_kdc_entry);
254
255         tmp_ctx = talloc_named(ctx, 0, "mit_samba_get_pac_data context");
256         if (!tmp_ctx) {
257                 return ENOMEM;
258         }
259
260         nt_status = samba_kdc_get_pac_blob(tmp_ctx, skdc_entry, &pac_blob);
261         if (!NT_STATUS_IS_OK(nt_status)) {
262                 talloc_free(tmp_ctx);
263                 return EINVAL;
264         }
265
266         data->data = (uint8_t *)malloc(pac_blob->length);
267         if (!data->data) {
268                 talloc_free(tmp_ctx);
269                 return ENOMEM;
270         }
271         memcpy(data->data, pac_blob->data, pac_blob->length);
272         data->length = pac_blob->length;
273
274         talloc_free(tmp_ctx);
275         return 0;
276 }
277
278 int mit_samba_update_pac_data(struct mit_samba_context *ctx,
279                               krb5_db_entry *client,
280                               DATA_BLOB *pac_data,
281                               DATA_BLOB *logon_data)
282 {
283         TALLOC_CTX *tmp_ctx;
284         DATA_BLOB *logon_blob;
285         krb5_error_code code;
286         NTSTATUS nt_status;
287         krb5_pac pac = NULL;
288         int ret;
289         struct samba_kdc_entry *skdc_entry = NULL;
290
291         if (client) {
292                 skdc_entry = talloc_get_type_abort(client->e_data,
293                                                    struct samba_kdc_entry);
294         }
295
296         /* The user account may be set not to want the PAC */
297         if (client && !samba_princ_needs_pac(skdc_entry)) {
298                 return EINVAL;
299         }
300
301         tmp_ctx = talloc_named(ctx, 0, "mit_samba_update_pac_data context");
302         if (!tmp_ctx) {
303                 return ENOMEM;
304         }
305
306         logon_blob = talloc_zero(tmp_ctx, DATA_BLOB);
307         if (!logon_blob) {
308                 ret = ENOMEM;
309                 goto done;
310         }
311
312         code = krb5_pac_parse(ctx->context,
313                               pac_data->data, pac_data->length, &pac);
314         if (code) {
315                 ret = EINVAL;
316                 goto done;
317         }
318
319         /* TODO: An implementation-specific decision will need to be
320          * made as to when to check the KDC pac signature, and how to
321          * untrust untrusted RODCs */
322         nt_status = samba_kdc_update_pac_blob(tmp_ctx, ctx->context,
323                                               pac, logon_blob, NULL, NULL);
324         if (!NT_STATUS_IS_OK(nt_status)) {
325                 DEBUG(0, ("Building PAC failed: %s\n",
326                           nt_errstr(nt_status)));
327                 ret = EINVAL;
328                 goto done;
329         }
330
331         logon_data->data = (uint8_t *)malloc(logon_blob->length);
332         if (!logon_data->data) {
333                 ret = ENOMEM;
334                 goto done;
335         }
336         memcpy(logon_data->data, logon_blob->data, logon_blob->length);
337         logon_data->length = logon_blob->length;
338
339         ret = 0;
340
341 done:
342         if (pac) krb5_pac_free(ctx->context, pac);
343         talloc_free(tmp_ctx);
344         return ret;
345 }
346
347 /* provide header, function is exported but there are no public headers */
348
349 krb5_error_code encode_krb5_padata_sequence(krb5_pa_data *const *rep, krb5_data **code);
350
351 /* this function allocates 'data' using malloc.
352  * The caller is responsible for freeing it */
353 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
354 {
355         krb5_error_code ret = 0;
356         krb5_pa_data pa, *ppa = NULL;
357         krb5_data *d = NULL;
358
359         if (!e_data)
360                 return;
361
362         e_data->data   = NULL;
363         e_data->length = 0;
364
365         pa.magic                = KV5M_PA_DATA;
366         pa.pa_type              = KRB5_PADATA_PW_SALT;
367         pa.length               = 12;
368         pa.contents             = malloc(pa.length);
369         if (!pa.contents) {
370                 return;
371         }
372
373         SIVAL(pa.contents, 0, NT_STATUS_V(nt_status));
374         SIVAL(pa.contents, 4, 0);
375         SIVAL(pa.contents, 8, 1);
376
377         ppa = &pa;
378
379         ret = encode_krb5_padata_sequence(&ppa, &d);
380         free(pa.contents);
381         if (ret) {
382                 return;
383         }
384
385         e_data->data   = (uint8_t *)d->data;
386         e_data->length = d->length;
387
388         /* free d, not d->data - gd */
389         free(d);
390
391         return;
392 }
393
394 int mit_samba_check_client_access(struct mit_samba_context *ctx,
395                                   krb5_db_entry *client,
396                                   const char *client_name,
397                                   krb5_db_entry *server,
398                                   const char *server_name,
399                                   const char *netbios_name,
400                                   bool password_change,
401                                   DATA_BLOB *e_data)
402 {
403         struct samba_kdc_entry *skdc_entry;
404         NTSTATUS nt_status;
405
406         skdc_entry = talloc_get_type(client->e_data, struct samba_kdc_entry);
407
408         nt_status = samba_kdc_check_client_access(skdc_entry,
409                                                   client_name,
410                                                   netbios_name,
411                                                   password_change);
412
413         if (!NT_STATUS_IS_OK(nt_status)) {
414                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
415                         return ENOMEM;
416                 }
417
418                 samba_kdc_build_edata_reply(nt_status, e_data);
419
420                 return samba_kdc_map_policy_err(nt_status);
421         }
422
423         return 0;
424 }
425
426 int mit_samba_check_s4u2proxy(struct mit_samba_context *ctx,
427                               krb5_db_entry *kentry,
428                               const char *target_name,
429                               bool is_nt_enterprise_name)
430 {
431 #if 1
432         /*
433          * This is disabled because mit_samba_update_pac_data() does not handle
434          * S4U_DELEGATION_INFO
435          */
436
437         return KRB5KDC_ERR_BADOPTION;
438 #else
439         krb5_principal target_principal;
440         int flags = 0;
441         int ret;
442
443         if (is_nt_enterprise_name) {
444                 flags = KRB5_PRINCIPAL_PARSE_ENTERPRISE;
445         }
446
447         ret = krb5_parse_name_flags(ctx->context, target_name,
448                                     flags, &target_principal);
449         if (ret) {
450                 return ret;
451         }
452
453         ret = samba_kdc_check_s4u2proxy(ctx->context,
454                                         ctx->db_ctx,
455                                         skdc_entry,
456                                         target_principal);
457
458         krb5_free_principal(ctx->context, target_principal);
459
460         return ret;
461 #endif
462 }