1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/*
MIT-Samba4 library
Copyright (c) 2010, Simo Sorce <idra@samba.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define TEVENT_DEPRECATED 1
#include "includes.h"
#include "param/param.h"
#include "dsdb/samdb/samdb.h"
#include "system/kerberos.h"
#include <hdb.h>
#include "mit_samba_interface.h"
#include "auth/kerberos/kerberos.h"
#include "kdc/samba_kdc.h"
#include "kdc/pac-glue.h"
#include "kdc/db-glue.h"
const int mit_samba_interface_version = MIT_SAMBA_INTERFACE_VERSION;
struct mit_samba_context {
struct auth_session_info *session_info;
/* for compat with hdb plugin common code */
krb5_context context;
struct samba_kdc_db_context *db_ctx;
};
static void mit_samba_context_free(struct mit_samba_context *ctx)
{
/* free heimdal's krb5_context */
if (ctx->context) {
krb5_free_context(ctx->context);
}
/* then free everything else */
talloc_free(ctx);
}
static int mit_samba_context_init(struct mit_samba_context **_ctx)
{
NTSTATUS status;
struct mit_samba_context *ctx;
const char *s4_conf_file;
int ret;
struct samba_kdc_base_context base_ctx;
ctx = talloc(NULL, struct mit_samba_context);
if (!ctx) {
ret = ENOMEM;
goto done;
}
base_ctx.ev_ctx = tevent_context_init(ctx);
if (!base_ctx.ev_ctx) {
ret = ENOMEM;
goto done;
}
tevent_loop_allow_nesting(base_ctx.ev_ctx);
base_ctx.lp_ctx = loadparm_init_global(false);
if (!base_ctx.lp_ctx) {
ret = ENOMEM;
goto done;
}
/* init s4 configuration */
s4_conf_file = lpcfg_configfile(base_ctx.lp_ctx);
if (s4_conf_file) {
lpcfg_load(base_ctx.lp_ctx, s4_conf_file);
} else {
lpcfg_load_default(base_ctx.lp_ctx);
}
status = samba_kdc_setup_db_ctx(ctx, &base_ctx, &ctx->db_ctx);
if (!NT_STATUS_IS_OK(status)) {
ret = EINVAL;
goto done;
}
/* init heimdal's krb_context and log facilities */
ret = smb_krb5_init_context_basic(ctx,
ctx->db_ctx->lp_ctx,
&ctx->context);
if (ret) {
goto done;
}
ret = 0;
done:
if (ret) {
mit_samba_context_free(ctx);
} else {
*_ctx = ctx;
}
return ret;
}
static int mit_samba_get_principal(struct mit_samba_context *ctx,
char *principal_string,
unsigned int flags,
hdb_entry_ex **_hentry)
{
krb5_principal principal;
hdb_entry_ex *hentry;
int ret;
hentry = talloc(ctx, hdb_entry_ex);
if (!hentry) {
return ENOMEM;
}
ret = krb5_parse_name(ctx->context, principal_string, &principal);
if (ret) {
goto done;
}
ret = samba_kdc_fetch(ctx->context, ctx->db_ctx,
principal, flags, 0, hentry);
krb5_free_principal(ctx->context, principal);
done:
if (ret) {
talloc_free(hentry);
} else {
talloc_steal(hentry->ctx, hentry);
*_hentry = hentry;
}
return ret;
}
static int mit_samba_get_firstkey(struct mit_samba_context *ctx,
hdb_entry_ex **_hentry)
{
hdb_entry_ex *hentry;
int ret;
hentry = talloc(ctx, hdb_entry_ex);
if (!hentry) {
return ENOMEM;
}
ret = samba_kdc_firstkey(ctx->context, ctx->db_ctx, hentry);
if (ret) {
talloc_free(hentry);
} else {
talloc_steal(hentry->ctx, hentry);
*_hentry = hentry;
}
return ret;
}
static int mit_samba_get_nextkey(struct mit_samba_context *ctx,
hdb_entry_ex **_hentry)
{
hdb_entry_ex *hentry;
int ret;
hentry = talloc(ctx, hdb_entry_ex);
if (!hentry) {
return ENOMEM;
}
ret = samba_kdc_nextkey(ctx->context, ctx->db_ctx, hentry);
if (ret) {
talloc_free(hentry);
} else {
talloc_steal(hentry->ctx, hentry);
*_hentry = hentry;
}
return ret;
}
static int mit_samba_get_pac_data(struct mit_samba_context *ctx,
hdb_entry_ex *client,
DATA_BLOB *data)
{
TALLOC_CTX *tmp_ctx;
DATA_BLOB *pac_blob;
NTSTATUS nt_status;
struct samba_kdc_entry *skdc_entry;
skdc_entry = talloc_get_type_abort(client->ctx,
struct samba_kdc_entry);
tmp_ctx = talloc_named(ctx, 0, "mit_samba_get_pac_data context");
if (!tmp_ctx) {
return ENOMEM;
}
nt_status = samba_kdc_get_pac_blob(tmp_ctx, skdc_entry, &pac_blob);
if (!NT_STATUS_IS_OK(nt_status)) {
talloc_free(tmp_ctx);
return EINVAL;
}
data->data = (uint8_t *)malloc(pac_blob->length);
if (!data->data) {
talloc_free(tmp_ctx);
return ENOMEM;
}
memcpy(data->data, pac_blob->data, pac_blob->length);
data->length = pac_blob->length;
talloc_free(tmp_ctx);
return 0;
}
static int mit_samba_update_pac_data(struct mit_samba_context *ctx,
hdb_entry_ex *client,
DATA_BLOB *pac_data,
DATA_BLOB *logon_data)
{
TALLOC_CTX *tmp_ctx;
DATA_BLOB *logon_blob;
krb5_error_code code;
NTSTATUS nt_status;
krb5_pac pac = NULL;
int ret;
struct samba_kdc_entry *skdc_entry = NULL;
if (client) {
skdc_entry = talloc_get_type_abort(client->ctx,
struct samba_kdc_entry);
}
/* The user account may be set not to want the PAC */
if (client && !samba_princ_needs_pac(skdc_entry)) {
return EINVAL;
}
tmp_ctx = talloc_named(ctx, 0, "mit_samba_update_pac_data context");
if (!tmp_ctx) {
return ENOMEM;
}
logon_blob = talloc_zero(tmp_ctx, DATA_BLOB);
if (!logon_blob) {
ret = ENOMEM;
goto done;
}
code = krb5_pac_parse(ctx->context,
pac_data->data, pac_data->length, &pac);
if (code) {
ret = EINVAL;
goto done;
}
/* TODO: An implementation-specific decision will need to be
* made as to when to check the KDC pac signature, and how to
* untrust untrusted RODCs */
nt_status = samba_kdc_update_pac_blob(tmp_ctx, ctx->context,
pac, logon_blob, NULL, NULL);
if (!NT_STATUS_IS_OK(nt_status)) {
DEBUG(0, ("Building PAC failed: %s\n",
nt_errstr(nt_status)));
ret = EINVAL;
goto done;
}
logon_data->data = (uint8_t *)malloc(logon_blob->length);
if (!logon_data->data) {
ret = ENOMEM;
goto done;
}
memcpy(logon_data->data, logon_blob->data, logon_blob->length);
logon_data->length = logon_blob->length;
ret = 0;
done:
if (pac) krb5_pac_free(ctx->context, pac);
talloc_free(tmp_ctx);
return ret;
}
static int mit_samba_check_client_access(struct mit_samba_context *ctx,
hdb_entry_ex *client,
const char *client_name,
hdb_entry_ex *server,
const char *server_name,
const char *netbios_name,
bool password_change,
DATA_BLOB *e_data)
{
struct samba_kdc_entry *kdc_entry;
NTSTATUS nt_status;
kdc_entry = talloc_get_type(client->ctx, struct samba_kdc_entry);
nt_status = samba_kdc_check_client_access(kdc_entry,
client_name,
netbios_name,
password_change);
if (!NT_STATUS_IS_OK(nt_status)) {
if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
return ENOMEM;
}
samba_kdc_build_edata_reply(nt_status, e_data);
return samba_kdc_map_policy_err(nt_status);
}
return 0;
}
static int mit_samba_check_s4u2proxy(struct mit_samba_context *ctx,
hdb_entry_ex *entry,
const char *target_name,
bool is_nt_enterprise_name)
{
#if 1
/*
* This is disabled because mit_samba_update_pac_data() does not handle
* S4U_DELEGATION_INFO
*/
return KRB5KDC_ERR_BADOPTION;
#else
krb5_principal target_principal;
int flags = 0;
int ret;
if (is_nt_enterprise_name) {
flags = KRB5_PRINCIPAL_PARSE_ENTERPRISE;
}
ret = krb5_parse_name_flags(ctx->context, target_name,
flags, &target_principal);
if (ret) {
return ret;
}
ret = samba_kdc_check_s4u2proxy(ctx->context,
ctx->db_ctx,
skdc_entry,
target_principal);
krb5_free_principal(ctx->context, target_principal);
return ret;
#endif
}
struct mit_samba_function_table mit_samba_function_table = {
mit_samba_context_init,
mit_samba_context_free,
mit_samba_get_principal,
mit_samba_get_firstkey,
mit_samba_get_nextkey,
mit_samba_get_pac_data,
mit_samba_update_pac_data,
mit_samba_check_client_access,
mit_samba_check_s4u2proxy
};
|