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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/*
Unix SMB/CIFS implementation.
NTP packet signing server
Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
Copyright (C) Andrew Tridgell 2005
Copyright (C) Stefan Metzmacher 2005
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/>.
*/
#include "includes.h"
#include "smbd/service_task.h"
#include "smbd/service.h"
#include "smbd/service_stream.h"
#include "smbd/process_model.h"
#include "lib/stream/packet.h"
#include "lib/tsocket/tsocket.h"
#include "libcli/util/tstream.h"
#include "librpc/gen_ndr/ndr_ntp_signd.h"
#include "param/param.h"
#include "dsdb/samdb/samdb.h"
#include "auth/auth.h"
#include "libcli/security/security.h"
#include "libcli/ldap/ldap_ndr.h"
#include <ldb.h>
#include <ldb_errors.h>
#include "../lib/crypto/md5.h"
#include "system/network.h"
#include "system/passwd.h"
NTSTATUS server_service_ntp_signd_init(void);
/*
top level context structure for the ntp_signd server
*/
struct ntp_signd_server {
struct task_server *task;
struct ldb_context *samdb;
};
/*
state of an open connection
*/
struct ntp_signd_connection {
/* stream connection we belong to */
struct stream_connection *conn;
/* the ntp_signd_server the connection belongs to */
struct ntp_signd_server *ntp_signd;
struct tstream_context *tstream;
struct tevent_queue *send_queue;
};
static void ntp_signd_terminate_connection(struct ntp_signd_connection *ntp_signd_conn, const char *reason)
{
stream_terminate_connection(ntp_signd_conn->conn, reason);
}
static NTSTATUS signing_failure(struct ntp_signd_connection *ntp_signdconn,
TALLOC_CTX *mem_ctx,
DATA_BLOB *output,
uint32_t packet_id)
{
struct signed_reply signed_reply;
enum ndr_err_code ndr_err;
signed_reply.op = SIGNING_FAILURE;
signed_reply.packet_id = packet_id;
signed_reply.signed_packet = data_blob(NULL, 0);
ndr_err = ndr_push_struct_blob(output, mem_ctx, &signed_reply,
(ndr_push_flags_fn_t)ndr_push_signed_reply);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(1,("failed to push ntp error reply\n"));
return ndr_map_error2ntstatus(ndr_err);
}
return NT_STATUS_OK;
}
/*
receive a full packet on a NTP_SIGND connection
*/
static NTSTATUS ntp_signd_process(struct ntp_signd_connection *ntp_signd_conn,
TALLOC_CTX *mem_ctx,
DATA_BLOB *input,
DATA_BLOB *output)
{
const struct dom_sid *domain_sid;
struct dom_sid *sid;
struct sign_request sign_request;
struct signed_reply signed_reply;
enum ndr_err_code ndr_err;
struct ldb_result *res;
const char *attrs[] = { "unicodePwd", "userAccountControl", "cn", NULL };
struct MD5Context ctx;
struct samr_Password *nt_hash;
uint32_t user_account_control;
int ret;
ndr_err = ndr_pull_struct_blob_all(input, mem_ctx,
&sign_request,
(ndr_pull_flags_fn_t)ndr_pull_sign_request);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(1,("failed to parse ntp signing request\n"));
dump_data(1, input->data, input->length);
return ndr_map_error2ntstatus(ndr_err);
}
/* We need to implement 'check signature' and 'request server
* to sign' operations at some point */
if (sign_request.op != SIGN_TO_CLIENT) {
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
/* We need to implement 'check signature' and 'request server
* to sign' operations at some point */
if (sign_request.version != NTP_SIGND_PROTOCOL_VERSION_0) {
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
domain_sid = samdb_domain_sid(ntp_signd_conn->ntp_signd->samdb);
if (domain_sid == NULL) {
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
/* The top bit is a 'key selector' */
sid = dom_sid_add_rid(mem_ctx, domain_sid,
sign_request.key_id & 0x7FFFFFFF);
if (sid == NULL) {
talloc_free(mem_ctx);
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
ret = ldb_search(ntp_signd_conn->ntp_signd->samdb, mem_ctx,
&res,
ldb_get_default_basedn(ntp_signd_conn->ntp_signd->samdb),
LDB_SCOPE_SUBTREE,
attrs,
"(&(objectSid=%s)(objectClass=user))",
ldap_encode_ndr_dom_sid(mem_ctx, sid));
if (ret != LDB_SUCCESS) {
DEBUG(2, ("Failed to search for SID %s in SAM for NTP signing: "
"%s\n",
dom_sid_string(mem_ctx, sid),
ldb_errstring(ntp_signd_conn->ntp_signd->samdb)));
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
if (res->count == 0) {
DEBUG(2, ("Failed to find SID %s in SAM for NTP signing\n",
dom_sid_string(mem_ctx, sid)));
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
} else if (res->count != 1) {
DEBUG(1, ("Found SID %s %u times in SAM for NTP signing\n",
dom_sid_string(mem_ctx, sid), res->count));
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
user_account_control = ldb_msg_find_attr_as_uint(res->msgs[0],
"userAccountControl",
0);
if (user_account_control & UF_ACCOUNTDISABLE) {
DEBUG(1, ("Account %s for SID [%s] is disabled\n",
ldb_dn_get_linearized(res->msgs[0]->dn),
dom_sid_string(mem_ctx, sid)));
return NT_STATUS_ACCESS_DENIED;
}
if (!(user_account_control & (UF_INTERDOMAIN_TRUST_ACCOUNT|UF_SERVER_TRUST_ACCOUNT|UF_WORKSTATION_TRUST_ACCOUNT))) {
DEBUG(1, ("Account %s for SID [%s] is not a trust account\n",
ldb_dn_get_linearized(res->msgs[0]->dn),
dom_sid_string(mem_ctx, sid)));
return NT_STATUS_ACCESS_DENIED;
}
nt_hash = samdb_result_hash(mem_ctx, res->msgs[0], "unicodePwd");
if (!nt_hash) {
DEBUG(1, ("No unicodePwd found on record of SID %s "
"for NTP signing\n", dom_sid_string(mem_ctx, sid)));
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
/* Generate the reply packet */
signed_reply.packet_id = sign_request.packet_id;
signed_reply.op = SIGNING_SUCCESS;
signed_reply.signed_packet = data_blob_talloc(mem_ctx,
NULL,
sign_request.packet_to_sign.length + 20);
if (!signed_reply.signed_packet.data) {
return signing_failure(ntp_signd_conn,
mem_ctx,
output,
sign_request.packet_id);
}
memcpy(signed_reply.signed_packet.data, sign_request.packet_to_sign.data, sign_request.packet_to_sign.length);
SIVAL(signed_reply.signed_packet.data, sign_request.packet_to_sign.length, sign_request.key_id);
/* Sign the NTP response with the unicodePwd */
MD5Init(&ctx);
MD5Update(&ctx, nt_hash->hash, sizeof(nt_hash->hash));
MD5Update(&ctx, sign_request.packet_to_sign.data, sign_request.packet_to_sign.length);
MD5Final(signed_reply.signed_packet.data + sign_request.packet_to_sign.length + 4, &ctx);
/* Place it into the packet for the wire */
ndr_err = ndr_push_struct_blob(output, mem_ctx, &signed_reply,
(ndr_push_flags_fn_t)ndr_push_signed_reply);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(1,("failed to push ntp error reply\n"));
return ndr_map_error2ntstatus(ndr_err);
}
return NT_STATUS_OK;
}
/*
called on a tcp recv
*/
static void ntp_signd_recv(struct stream_connection *conn, uint16_t flags)
{
struct ntp_signd_connection *ntp_signd_conn = talloc_get_type(conn->private_data,
struct ntp_signd_connection);
ntp_signd_terminate_connection(ntp_signd_conn,
"ntp_signd_recv: called");
}
/*
called when we can write to a connection
*/
static void ntp_signd_send(struct stream_connection *conn, uint16_t flags)
{
struct ntp_signd_connection *ntp_signd_conn = talloc_get_type(conn->private_data,
struct ntp_signd_connection);
/* this should never be triggered! */
ntp_signd_terminate_connection(ntp_signd_conn,
"ntp_signd_send: called");
}
struct ntp_signd_call {
struct ntp_signd_connection *ntp_signd_conn;
DATA_BLOB in;
DATA_BLOB out;
uint8_t out_hdr[4];
struct iovec out_iov[2];
};
static void ntp_signd_call_writev_done(struct tevent_req *subreq);
static void ntp_signd_call_loop(struct tevent_req *subreq)
{
struct ntp_signd_connection *ntp_signd_conn = tevent_req_callback_data(subreq,
struct ntp_signd_connection);
struct ntp_signd_call *call;
NTSTATUS status;
call = talloc(ntp_signd_conn, struct ntp_signd_call);
if (call == NULL) {
ntp_signd_terminate_connection(ntp_signd_conn,
"ntp_signd_call_loop: "
"no memory for ntp_signd_call");
return;
}
call->ntp_signd_conn = ntp_signd_conn;
status = tstream_read_pdu_blob_recv(subreq,
call,
&call->in);
TALLOC_FREE(subreq);
if (!NT_STATUS_IS_OK(status)) {
const char *reason;
reason = talloc_asprintf(call, "ntp_signd_call_loop: "
"tstream_read_pdu_blob_recv() - %s",
nt_errstr(status));
if (reason == NULL) {
reason = nt_errstr(status);
}
ntp_signd_terminate_connection(ntp_signd_conn, reason);
return;
}
DEBUG(10,("Received NTP TCP packet of length %lu from %s\n",
(long) call->in.length,
tsocket_address_string(ntp_signd_conn->conn->remote_address, call)));
/* skip length header */
call->in.data +=4;
call->in.length -= 4;
status = ntp_signd_process(ntp_signd_conn,
call,
&call->in,
&call->out);
if (! NT_STATUS_IS_OK(status)) {
const char *reason;
reason = talloc_asprintf(call, "ntp_signd_process failed: %s",
nt_errstr(status));
if (reason == NULL) {
reason = nt_errstr(status);
}
ntp_signd_terminate_connection(ntp_signd_conn, reason);
return;
}
/* First add the length of the out buffer */
RSIVAL(call->out_hdr, 0, call->out.length);
call->out_iov[0].iov_base = (char *) call->out_hdr;
call->out_iov[0].iov_len = 4;
call->out_iov[1].iov_base = (char *) call->out.data;
call->out_iov[1].iov_len = call->out.length;
subreq = tstream_writev_queue_send(call,
ntp_signd_conn->conn->event.ctx,
ntp_signd_conn->tstream,
ntp_signd_conn->send_queue,
call->out_iov, 2);
if (subreq == NULL) {
ntp_signd_terminate_connection(ntp_signd_conn, "ntp_signd_call_loop: "
"no memory for tstream_writev_queue_send");
return;
}
tevent_req_set_callback(subreq, ntp_signd_call_writev_done, call);
/*
* The NTP tcp pdu's has the length as 4 byte (initial_read_size),
* packet_full_request_u32 provides the pdu length then.
*/
subreq = tstream_read_pdu_blob_send(ntp_signd_conn,
ntp_signd_conn->conn->event.ctx,
ntp_signd_conn->tstream,
4, /* initial_read_size */
packet_full_request_u32,
ntp_signd_conn);
if (subreq == NULL) {
ntp_signd_terminate_connection(ntp_signd_conn, "ntp_signd_call_loop: "
"no memory for tstream_read_pdu_blob_send");
return;
}
tevent_req_set_callback(subreq, ntp_signd_call_loop, ntp_signd_conn);
}
static void ntp_signd_call_writev_done(struct tevent_req *subreq)
{
struct ntp_signd_call *call = tevent_req_callback_data(subreq,
struct ntp_signd_call);
int sys_errno;
int rc;
rc = tstream_writev_queue_recv(subreq, &sys_errno);
TALLOC_FREE(subreq);
if (rc == -1) {
const char *reason;
reason = talloc_asprintf(call, "ntp_signd_call_writev_done: "
"tstream_writev_queue_recv() - %d:%s",
sys_errno, strerror(sys_errno));
if (!reason) {
reason = "ntp_signd_call_writev_done: "
"tstream_writev_queue_recv() failed";
}
ntp_signd_terminate_connection(call->ntp_signd_conn, reason);
return;
}
/* We don't care about errors */
talloc_free(call);
}
/*
called when we get a new connection
*/
static void ntp_signd_accept(struct stream_connection *conn)
{
struct ntp_signd_server *ntp_signd = talloc_get_type(conn->private_data,
struct ntp_signd_server);
struct ntp_signd_connection *ntp_signd_conn;
struct tevent_req *subreq;
int rc;
ntp_signd_conn = talloc_zero(conn, struct ntp_signd_connection);
if (ntp_signd_conn == NULL) {
stream_terminate_connection(conn,
"ntp_signd_accept: out of memory");
return;
}
ntp_signd_conn->send_queue = tevent_queue_create(conn,
"ntp_signd_accept");
if (ntp_signd_conn->send_queue == NULL) {
stream_terminate_connection(conn,
"ntp_signd_accept: out of memory");
return;
}
TALLOC_FREE(conn->event.fde);
rc = tstream_bsd_existing_socket(ntp_signd_conn,
socket_get_fd(conn->socket),
&ntp_signd_conn->tstream);
if (rc < 0) {
stream_terminate_connection(conn,
"ntp_signd_accept: out of memory");
return;
}
ntp_signd_conn->conn = conn;
ntp_signd_conn->ntp_signd = ntp_signd;
conn->private_data = ntp_signd_conn;
/*
* The NTP tcp pdu's has the length as 4 byte (initial_read_size),
* packet_full_request_u32 provides the pdu length then.
*/
subreq = tstream_read_pdu_blob_send(ntp_signd_conn,
ntp_signd_conn->conn->event.ctx,
ntp_signd_conn->tstream,
4, /* initial_read_size */
packet_full_request_u32,
ntp_signd_conn);
if (subreq == NULL) {
ntp_signd_terminate_connection(ntp_signd_conn,
"ntp_signd_accept: "
"no memory for tstream_read_pdu_blob_send");
return;
}
tevent_req_set_callback(subreq, ntp_signd_call_loop, ntp_signd_conn);
}
static const struct stream_server_ops ntp_signd_stream_ops = {
.name = "ntp_signd",
.accept_connection = ntp_signd_accept,
.recv_handler = ntp_signd_recv,
.send_handler = ntp_signd_send
};
/*
startup the ntp_signd task
*/
static void ntp_signd_task_init(struct task_server *task)
{
struct ntp_signd_server *ntp_signd;
NTSTATUS status;
const struct model_ops *model_ops;
const char *address;
if (!directory_create_or_exist(lpcfg_ntp_signd_socket_directory(task->lp_ctx), geteuid(), 0750)) {
char *error = talloc_asprintf(task, "Cannot create NTP signd pipe directory: %s",
lpcfg_ntp_signd_socket_directory(task->lp_ctx));
task_server_terminate(task,
error, true);
return;
}
/* within the ntp_signd task we want to be a single process, so
ask for the single process model ops and pass these to the
stream_setup_socket() call. */
model_ops = process_model_startup("single");
if (!model_ops) {
DEBUG(0,("Can't find 'single' process model_ops\n"));
return;
}
task_server_set_title(task, "task[ntp_signd]");
ntp_signd = talloc(task, struct ntp_signd_server);
if (ntp_signd == NULL) {
task_server_terminate(task, "ntp_signd: out of memory", true);
return;
}
ntp_signd->task = task;
/* Must be system to get at the password hashes */
ntp_signd->samdb = samdb_connect(ntp_signd, task->event_ctx, task->lp_ctx, system_session(task->lp_ctx), 0);
if (ntp_signd->samdb == NULL) {
task_server_terminate(task, "ntp_signd failed to open samdb", true);
return;
}
address = talloc_asprintf(ntp_signd, "%s/socket", lpcfg_ntp_signd_socket_directory(task->lp_ctx));
status = stream_setup_socket(ntp_signd->task,
ntp_signd->task->event_ctx,
ntp_signd->task->lp_ctx,
model_ops,
&ntp_signd_stream_ops,
"unix", address, NULL,
lpcfg_socket_options(ntp_signd->task->lp_ctx),
ntp_signd);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0,("Failed to bind to %s - %s\n",
address, nt_errstr(status)));
return;
}
}
/* called at smbd startup - register ourselves as a server service */
NTSTATUS server_service_ntp_signd_init(void)
{
return register_server_service("ntp_signd", ntp_signd_task_init);
}
|