2 Unix SMB/CIFS implementation.
4 NTP packet signing server
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Andrew Tridgell 2005
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
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/>.
25 #include "smbd/service_task.h"
26 #include "smbd/service.h"
27 #include "smbd/service_stream.h"
28 #include "smbd/process_model.h"
29 #include "lib/stream/packet.h"
30 #include "lib/tsocket/tsocket.h"
31 #include "libcli/util/tstream.h"
32 #include "librpc/gen_ndr/ndr_ntp_signd.h"
33 #include "param/param.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "auth/auth.h"
36 #include "libcli/security/security.h"
37 #include "libcli/ldap/ldap_ndr.h"
39 #include <ldb_errors.h>
40 #include "../lib/crypto/md5.h"
41 #include "system/network.h"
42 #include "system/passwd.h"
45 top level context structure for the ntp_signd server
47 struct ntp_signd_server
{
48 struct task_server
*task
;
49 struct ldb_context
*samdb
;
53 state of an open connection
55 struct ntp_signd_connection
{
56 /* stream connection we belong to */
57 struct stream_connection
*conn
;
59 /* the ntp_signd_server the connection belongs to */
60 struct ntp_signd_server
*ntp_signd
;
62 struct tstream_context
*tstream
;
64 struct tevent_queue
*send_queue
;
67 static void ntp_signd_terminate_connection(struct ntp_signd_connection
*ntp_signd_conn
, const char *reason
)
69 stream_terminate_connection(ntp_signd_conn
->conn
, reason
);
72 static NTSTATUS
signing_failure(struct ntp_signd_connection
*ntp_signdconn
,
77 struct signed_reply signed_reply
;
78 enum ndr_err_code ndr_err
;
80 signed_reply
.op
= SIGNING_FAILURE
;
81 signed_reply
.packet_id
= packet_id
;
82 signed_reply
.signed_packet
= data_blob(NULL
, 0);
84 ndr_err
= ndr_push_struct_blob(output
, mem_ctx
, &signed_reply
,
85 (ndr_push_flags_fn_t
)ndr_push_signed_reply
);
87 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
88 DEBUG(1,("failed to push ntp error reply\n"));
89 return ndr_map_error2ntstatus(ndr_err
);
96 receive a full packet on a NTP_SIGND connection
98 static NTSTATUS
ntp_signd_process(struct ntp_signd_connection
*ntp_signd_conn
,
103 const struct dom_sid
*domain_sid
;
105 struct sign_request sign_request
;
106 struct signed_reply signed_reply
;
107 enum ndr_err_code ndr_err
;
108 struct ldb_result
*res
;
109 const char *attrs
[] = { "unicodePwd", "userAccountControl", "cn", NULL
};
110 struct MD5Context ctx
;
111 struct samr_Password
*nt_hash
;
112 uint32_t user_account_control
;
115 ndr_err
= ndr_pull_struct_blob_all(input
, mem_ctx
,
117 (ndr_pull_flags_fn_t
)ndr_pull_sign_request
);
119 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
120 DEBUG(1,("failed to parse ntp signing request\n"));
121 dump_data(1, input
->data
, input
->length
);
122 return ndr_map_error2ntstatus(ndr_err
);
125 /* We need to implement 'check signature' and 'request server
126 * to sign' operations at some point */
127 if (sign_request
.op
!= SIGN_TO_CLIENT
) {
128 return signing_failure(ntp_signd_conn
,
131 sign_request
.packet_id
);
134 /* We need to implement 'check signature' and 'request server
135 * to sign' operations at some point */
136 if (sign_request
.version
!= NTP_SIGND_PROTOCOL_VERSION_0
) {
137 return signing_failure(ntp_signd_conn
,
140 sign_request
.packet_id
);
143 domain_sid
= samdb_domain_sid(ntp_signd_conn
->ntp_signd
->samdb
);
144 if (domain_sid
== NULL
) {
145 return signing_failure(ntp_signd_conn
,
148 sign_request
.packet_id
);
151 /* The top bit is a 'key selector' */
152 sid
= dom_sid_add_rid(mem_ctx
, domain_sid
,
153 sign_request
.key_id
& 0x7FFFFFFF);
155 talloc_free(mem_ctx
);
156 return signing_failure(ntp_signd_conn
,
159 sign_request
.packet_id
);
162 ret
= ldb_search(ntp_signd_conn
->ntp_signd
->samdb
, mem_ctx
,
164 ldb_get_default_basedn(ntp_signd_conn
->ntp_signd
->samdb
),
167 "(&(objectSid=%s)(objectClass=user))",
168 ldap_encode_ndr_dom_sid(mem_ctx
, sid
));
169 if (ret
!= LDB_SUCCESS
) {
170 DEBUG(2, ("Failed to search for SID %s in SAM for NTP signing: "
172 dom_sid_string(mem_ctx
, sid
),
173 ldb_errstring(ntp_signd_conn
->ntp_signd
->samdb
)));
174 return signing_failure(ntp_signd_conn
,
177 sign_request
.packet_id
);
180 if (res
->count
== 0) {
181 DEBUG(5, ("Failed to find SID %s in SAM for NTP signing\n",
182 dom_sid_string(mem_ctx
, sid
)));
183 } else if (res
->count
!= 1) {
184 DEBUG(1, ("Found SID %s %u times in SAM for NTP signing\n",
185 dom_sid_string(mem_ctx
, sid
), res
->count
));
186 return signing_failure(ntp_signd_conn
,
189 sign_request
.packet_id
);
192 user_account_control
= ldb_msg_find_attr_as_uint(res
->msgs
[0],
193 "userAccountControl",
196 if (user_account_control
& UF_ACCOUNTDISABLE
) {
197 DEBUG(1, ("Account %s for SID [%s] is disabled\n",
198 ldb_dn_get_linearized(res
->msgs
[0]->dn
),
199 dom_sid_string(mem_ctx
, sid
)));
200 return NT_STATUS_ACCESS_DENIED
;
203 if (!(user_account_control
& (UF_INTERDOMAIN_TRUST_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
|UF_WORKSTATION_TRUST_ACCOUNT
))) {
204 DEBUG(1, ("Account %s for SID [%s] is not a trust account\n",
205 ldb_dn_get_linearized(res
->msgs
[0]->dn
),
206 dom_sid_string(mem_ctx
, sid
)));
207 return NT_STATUS_ACCESS_DENIED
;
210 nt_hash
= samdb_result_hash(mem_ctx
, res
->msgs
[0], "unicodePwd");
212 DEBUG(1, ("No unicodePwd found on record of SID %s "
213 "for NTP signing\n", dom_sid_string(mem_ctx
, sid
)));
214 return signing_failure(ntp_signd_conn
,
217 sign_request
.packet_id
);
220 /* Generate the reply packet */
221 signed_reply
.packet_id
= sign_request
.packet_id
;
222 signed_reply
.op
= SIGNING_SUCCESS
;
223 signed_reply
.signed_packet
= data_blob_talloc(mem_ctx
,
225 sign_request
.packet_to_sign
.length
+ 20);
227 if (!signed_reply
.signed_packet
.data
) {
228 return signing_failure(ntp_signd_conn
,
231 sign_request
.packet_id
);
234 memcpy(signed_reply
.signed_packet
.data
, sign_request
.packet_to_sign
.data
, sign_request
.packet_to_sign
.length
);
235 SIVAL(signed_reply
.signed_packet
.data
, sign_request
.packet_to_sign
.length
, sign_request
.key_id
);
237 /* Sign the NTP response with the unicodePwd */
239 MD5Update(&ctx
, nt_hash
->hash
, sizeof(nt_hash
->hash
));
240 MD5Update(&ctx
, sign_request
.packet_to_sign
.data
, sign_request
.packet_to_sign
.length
);
241 MD5Final(signed_reply
.signed_packet
.data
+ sign_request
.packet_to_sign
.length
+ 4, &ctx
);
244 /* Place it into the packet for the wire */
245 ndr_err
= ndr_push_struct_blob(output
, mem_ctx
, &signed_reply
,
246 (ndr_push_flags_fn_t
)ndr_push_signed_reply
);
248 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
249 DEBUG(1,("failed to push ntp error reply\n"));
250 return ndr_map_error2ntstatus(ndr_err
);
259 static void ntp_signd_recv(struct stream_connection
*conn
, uint16_t flags
)
261 struct ntp_signd_connection
*ntp_signd_conn
= talloc_get_type(conn
->private_data
,
262 struct ntp_signd_connection
);
263 ntp_signd_terminate_connection(ntp_signd_conn
,
264 "ntp_signd_recv: called");
268 called when we can write to a connection
270 static void ntp_signd_send(struct stream_connection
*conn
, uint16_t flags
)
272 struct ntp_signd_connection
*ntp_signd_conn
= talloc_get_type(conn
->private_data
,
273 struct ntp_signd_connection
);
274 /* this should never be triggered! */
275 ntp_signd_terminate_connection(ntp_signd_conn
,
276 "ntp_signd_send: called");
279 struct ntp_signd_call
{
280 struct ntp_signd_connection
*ntp_signd_conn
;
284 struct iovec out_iov
[2];
287 static void ntp_signd_call_writev_done(struct tevent_req
*subreq
);
289 static void ntp_signd_call_loop(struct tevent_req
*subreq
)
291 struct ntp_signd_connection
*ntp_signd_conn
= tevent_req_callback_data(subreq
,
292 struct ntp_signd_connection
);
293 struct ntp_signd_call
*call
;
296 call
= talloc(ntp_signd_conn
, struct ntp_signd_call
);
298 ntp_signd_terminate_connection(ntp_signd_conn
,
299 "ntp_signd_call_loop: "
300 "no memory for ntp_signd_call");
303 call
->ntp_signd_conn
= ntp_signd_conn
;
305 status
= tstream_read_pdu_blob_recv(subreq
,
309 if (!NT_STATUS_IS_OK(status
)) {
312 reason
= talloc_asprintf(call
, "ntp_signd_call_loop: "
313 "tstream_read_pdu_blob_recv() - %s",
315 if (reason
== NULL
) {
316 reason
= nt_errstr(status
);
319 ntp_signd_terminate_connection(ntp_signd_conn
, reason
);
323 DEBUG(10,("Received NTP TCP packet of length %lu from %s\n",
324 (long) call
->in
.length
,
325 tsocket_address_string(ntp_signd_conn
->conn
->remote_address
, call
)));
327 /* skip length header */
329 call
->in
.length
-= 4;
331 status
= ntp_signd_process(ntp_signd_conn
,
335 if (! NT_STATUS_IS_OK(status
)) {
338 reason
= talloc_asprintf(call
, "ntp_signd_process failed: %s",
340 if (reason
== NULL
) {
341 reason
= nt_errstr(status
);
344 ntp_signd_terminate_connection(ntp_signd_conn
, reason
);
348 /* First add the length of the out buffer */
349 RSIVAL(call
->out_hdr
, 0, call
->out
.length
);
350 call
->out_iov
[0].iov_base
= (char *) call
->out_hdr
;
351 call
->out_iov
[0].iov_len
= 4;
353 call
->out_iov
[1].iov_base
= (char *) call
->out
.data
;
354 call
->out_iov
[1].iov_len
= call
->out
.length
;
356 subreq
= tstream_writev_queue_send(call
,
357 ntp_signd_conn
->conn
->event
.ctx
,
358 ntp_signd_conn
->tstream
,
359 ntp_signd_conn
->send_queue
,
361 if (subreq
== NULL
) {
362 ntp_signd_terminate_connection(ntp_signd_conn
, "ntp_signd_call_loop: "
363 "no memory for tstream_writev_queue_send");
367 tevent_req_set_callback(subreq
, ntp_signd_call_writev_done
, call
);
370 * The NTP tcp pdu's has the length as 4 byte (initial_read_size),
371 * packet_full_request_u32 provides the pdu length then.
373 subreq
= tstream_read_pdu_blob_send(ntp_signd_conn
,
374 ntp_signd_conn
->conn
->event
.ctx
,
375 ntp_signd_conn
->tstream
,
376 4, /* initial_read_size */
377 packet_full_request_u32
,
379 if (subreq
== NULL
) {
380 ntp_signd_terminate_connection(ntp_signd_conn
, "ntp_signd_call_loop: "
381 "no memory for tstream_read_pdu_blob_send");
384 tevent_req_set_callback(subreq
, ntp_signd_call_loop
, ntp_signd_conn
);
387 static void ntp_signd_call_writev_done(struct tevent_req
*subreq
)
389 struct ntp_signd_call
*call
= tevent_req_callback_data(subreq
,
390 struct ntp_signd_call
);
394 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
399 reason
= talloc_asprintf(call
, "ntp_signd_call_writev_done: "
400 "tstream_writev_queue_recv() - %d:%s",
401 sys_errno
, strerror(sys_errno
));
403 reason
= "ntp_signd_call_writev_done: "
404 "tstream_writev_queue_recv() failed";
407 ntp_signd_terminate_connection(call
->ntp_signd_conn
, reason
);
411 /* We don't care about errors */
417 called when we get a new connection
419 static void ntp_signd_accept(struct stream_connection
*conn
)
421 struct ntp_signd_server
*ntp_signd
= talloc_get_type(conn
->private_data
,
422 struct ntp_signd_server
);
423 struct ntp_signd_connection
*ntp_signd_conn
;
424 struct tevent_req
*subreq
;
427 ntp_signd_conn
= talloc_zero(conn
, struct ntp_signd_connection
);
428 if (ntp_signd_conn
== NULL
) {
429 stream_terminate_connection(conn
,
430 "ntp_signd_accept: out of memory");
434 ntp_signd_conn
->send_queue
= tevent_queue_create(conn
,
436 if (ntp_signd_conn
->send_queue
== NULL
) {
437 stream_terminate_connection(conn
,
438 "ntp_signd_accept: out of memory");
442 TALLOC_FREE(conn
->event
.fde
);
444 rc
= tstream_bsd_existing_socket(ntp_signd_conn
,
445 socket_get_fd(conn
->socket
),
446 &ntp_signd_conn
->tstream
);
448 stream_terminate_connection(conn
,
449 "ntp_signd_accept: out of memory");
453 ntp_signd_conn
->conn
= conn
;
454 ntp_signd_conn
->ntp_signd
= ntp_signd
;
455 conn
->private_data
= ntp_signd_conn
;
458 * The NTP tcp pdu's has the length as 4 byte (initial_read_size),
459 * packet_full_request_u32 provides the pdu length then.
461 subreq
= tstream_read_pdu_blob_send(ntp_signd_conn
,
462 ntp_signd_conn
->conn
->event
.ctx
,
463 ntp_signd_conn
->tstream
,
464 4, /* initial_read_size */
465 packet_full_request_u32
,
467 if (subreq
== NULL
) {
468 ntp_signd_terminate_connection(ntp_signd_conn
,
470 "no memory for tstream_read_pdu_blob_send");
473 tevent_req_set_callback(subreq
, ntp_signd_call_loop
, ntp_signd_conn
);
476 static const struct stream_server_ops ntp_signd_stream_ops
= {
478 .accept_connection
= ntp_signd_accept
,
479 .recv_handler
= ntp_signd_recv
,
480 .send_handler
= ntp_signd_send
484 startup the ntp_signd task
486 static void ntp_signd_task_init(struct task_server
*task
)
488 struct ntp_signd_server
*ntp_signd
;
491 const struct model_ops
*model_ops
;
495 if (!directory_create_or_exist(lpcfg_ntp_signd_socket_directory(task
->lp_ctx
), geteuid(), 0755)) {
496 char *error
= talloc_asprintf(task
, "Cannot create NTP signd pipe directory: %s",
497 lpcfg_ntp_signd_socket_directory(task
->lp_ctx
));
498 task_server_terminate(task
,
503 /* within the ntp_signd task we want to be a single process, so
504 ask for the single process model ops and pass these to the
505 stream_setup_socket() call. */
506 model_ops
= process_model_startup("single");
508 DEBUG(0,("Can't find 'single' process model_ops\n"));
512 task_server_set_title(task
, "task[ntp_signd]");
514 ntp_signd
= talloc(task
, struct ntp_signd_server
);
515 if (ntp_signd
== NULL
) {
516 task_server_terminate(task
, "ntp_signd: out of memory", true);
520 ntp_signd
->task
= task
;
522 /* Must be system to get at the password hashes */
523 ntp_signd
->samdb
= samdb_connect(ntp_signd
, task
->event_ctx
, task
->lp_ctx
, system_session(task
->lp_ctx
), 0);
524 if (ntp_signd
->samdb
== NULL
) {
525 task_server_terminate(task
, "ntp_signd failed to open samdb", true);
529 address
= talloc_asprintf(ntp_signd
, "%s/socket", lpcfg_ntp_signd_socket_directory(task
->lp_ctx
));
531 status
= stream_setup_socket(ntp_signd
->task
,
532 ntp_signd
->task
->event_ctx
,
533 ntp_signd
->task
->lp_ctx
,
535 &ntp_signd_stream_ops
,
536 "unix", address
, NULL
,
537 lpcfg_socket_options(ntp_signd
->task
->lp_ctx
),
539 if (!NT_STATUS_IS_OK(status
)) {
540 DEBUG(0,("Failed to bind to %s - %s\n",
541 address
, nt_errstr(status
)));
548 /* called at smbd startup - register ourselves as a server service */
549 NTSTATUS
server_service_ntp_signd_init(void)
551 return register_server_service("ntp_signd", ntp_signd_task_init
);