2 Unix SMB/CIFS implementation.
3 Authenticate against a netlogon pipe listening on a unix domain socket
4 Copyright (C) Volker Lendecke 2008
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.
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.
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/>.
21 #include "../libcli/auth/libcli_auth.h"
22 #include "../librpc/gen_ndr/ndr_netlogon.h"
23 #include "librpc/gen_ndr/ndr_schannel.h"
24 #include "rpc_client/cli_netlogon.h"
27 #include "tldap_util.h"
30 #define DBGC_CLASS DBGC_AUTH
32 static bool secrets_store_local_schannel_creds(
33 const struct netlogon_creds_CredentialState
*creds
)
36 enum ndr_err_code ndr_err
;
39 ndr_err
= ndr_push_struct_blob(
40 &blob
, talloc_tos(), creds
,
41 (ndr_push_flags_fn_t
)ndr_push_netlogon_creds_CredentialState
);
42 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
43 DEBUG(10, ("ndr_push_netlogon_creds_CredentialState failed: "
44 "%s\n", ndr_errstr(ndr_err
)));
47 ret
= secrets_store(SECRETS_LOCAL_SCHANNEL_KEY
,
48 blob
.data
, blob
.length
);
49 data_blob_free(&blob
);
53 static struct netlogon_creds_CredentialState
*
54 secrets_fetch_local_schannel_creds(TALLOC_CTX
*mem_ctx
)
56 struct netlogon_creds_CredentialState
*creds
;
57 enum ndr_err_code ndr_err
;
60 blob
.data
= (uint8_t *)secrets_fetch(SECRETS_LOCAL_SCHANNEL_KEY
,
62 if (blob
.data
== NULL
) {
63 DEBUG(10, ("secrets_fetch failed\n"));
67 creds
= talloc(mem_ctx
, struct netlogon_creds_CredentialState
);
69 DEBUG(10, ("talloc failed\n"));
73 ndr_err
= ndr_pull_struct_blob(
75 (ndr_pull_flags_fn_t
)ndr_pull_netlogon_creds_CredentialState
);
77 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
78 DEBUG(10, ("ndr_pull_netlogon_creds_CredentialState failed: "
79 "%s\n", ndr_errstr(ndr_err
)));
87 static NTSTATUS
netlogond_validate(TALLOC_CTX
*mem_ctx
,
88 const struct auth_context
*auth_context
,
89 const char *ncalrpc_sockname
,
90 struct netlogon_creds_CredentialState
*creds
,
91 const struct auth_usersupplied_info
*user_info
,
92 struct netr_SamInfo3
**pinfo3
,
93 NTSTATUS
*schannel_bind_result
)
95 struct rpc_pipe_client
*p
= NULL
;
96 struct pipe_auth_data
*auth
= NULL
;
97 struct netr_SamInfo3
*info3
= NULL
;
100 *schannel_bind_result
= NT_STATUS_OK
;
102 status
= rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpc_sockname
,
103 &ndr_table_netlogon
.syntax_id
, &p
);
104 if (!NT_STATUS_IS_OK(status
)) {
105 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
112 status
= rpccli_schannel_bind_data(p
, lp_workgroup(),
113 DCERPC_AUTH_LEVEL_PRIVACY
,
115 if (!NT_STATUS_IS_OK(status
)) {
116 DEBUG(10, ("rpccli_schannel_bind_data failed: %s\n",
122 status
= rpc_pipe_bind(p
, auth
);
123 if (!NT_STATUS_IS_OK(status
)) {
124 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status
)));
126 *schannel_bind_result
= status
;
130 status
= rpccli_netlogon_sam_network_logon_ex(
132 user_info
->logon_parameters
, /* flags such as 'allow
133 * workstation logon' */
134 global_myname(), /* server name */
135 user_info
->client
.account_name
, /* user name logging on. */
136 user_info
->client
.domain_name
, /* domain name */
137 user_info
->workstation_name
, /* workstation name */
138 (uchar
*)auth_context
->challenge
.data
, /* 8 byte challenge. */
139 3, /* validation level */
140 user_info
->password
.response
.lanman
, /* lanman 24 byte response */
141 user_info
->password
.response
.nt
, /* nt 24 byte response */
142 &info3
); /* info3 out */
144 DEBUG(10, ("rpccli_netlogon_sam_network_logon_ex returned %s\n",
147 if (!NT_STATUS_IS_OK(status
)) {
152 *pinfo3
= talloc_move(mem_ctx
, &info3
);
158 static NTSTATUS
get_ldapi_ctx(TALLOC_CTX
*mem_ctx
, struct tldap_context
**pld
)
160 struct tldap_context
*ld
;
161 struct sockaddr_un addr
;
167 sockaddr
= talloc_asprintf(talloc_tos(), "/%s/ldap_priv/ldapi",
169 if (sockaddr
== NULL
) {
170 DEBUG(10, ("talloc failed\n"));
171 return NT_STATUS_NO_MEMORY
;
175 addr
.sun_family
= AF_UNIX
;
176 strncpy(addr
.sun_path
, sockaddr
, sizeof(addr
.sun_path
));
177 TALLOC_FREE(sockaddr
);
179 status
= open_socket_out((struct sockaddr_storage
*)(void *)&addr
,
181 if (!NT_STATUS_IS_OK(status
)) {
182 DEBUG(10, ("Could not connect to %s: %s\n", addr
.sun_path
,
186 set_blocking(fd
, false);
188 ld
= tldap_context_create(mem_ctx
, fd
);
191 return NT_STATUS_NO_MEMORY
;
193 res
= tldap_fetch_rootdse(ld
);
194 if (res
!= TLDAP_SUCCESS
) {
195 DEBUG(10, ("tldap_fetch_rootdse failed: %s\n",
196 tldap_errstr(talloc_tos(), ld
, res
)));
198 return NT_STATUS_LDAP(res
);
201 return NT_STATUS_OK
;;
204 static NTSTATUS
mymachinepw(uint8_t pwd
[16])
206 TALLOC_CTX
*frame
= talloc_stackframe();
207 struct tldap_context
*ld
;
208 struct tldap_message
*rootdse
, **msg
;
209 const char *attrs
[1] = { "unicodePwd" };
210 char *default_nc
, *myname
;
215 status
= get_ldapi_ctx(talloc_tos(), &ld
);
216 if (!NT_STATUS_IS_OK(status
)) {
219 rootdse
= tldap_rootdse(ld
);
220 if (rootdse
== NULL
) {
221 DEBUG(10, ("Could not get rootdse\n"));
222 status
= NT_STATUS_INTERNAL_ERROR
;
225 default_nc
= tldap_talloc_single_attribute(
226 rootdse
, "defaultNamingContext", talloc_tos());
227 if (default_nc
== NULL
) {
228 DEBUG(10, ("Could not get defaultNamingContext\n"));
229 status
= NT_STATUS_NO_MEMORY
;
232 DEBUG(10, ("default_nc = %s\n", default_nc
));
234 myname
= talloc_asprintf_strupper_m(talloc_tos(), "%s$",
236 if (myname
== NULL
) {
237 DEBUG(10, ("talloc failed\n"));
238 status
= NT_STATUS_NO_MEMORY
;
242 rc
= tldap_search_fmt(
243 ld
, default_nc
, TLDAP_SCOPE_SUB
, attrs
, ARRAY_SIZE(attrs
), 0,
245 "(&(sAMAccountName=%s)(objectClass=computer))", myname
);
246 if (rc
!= TLDAP_SUCCESS
) {
247 DEBUG(10, ("Could not retrieve our account: %s\n",
248 tldap_errstr(talloc_tos(), ld
, rc
)));
249 status
= NT_STATUS_LDAP(rc
);
252 num_msg
= talloc_array_length(msg
);
254 DEBUG(10, ("Got %d accounts, expected one\n", num_msg
));
255 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
258 if (!tldap_get_single_valueblob(msg
[0], "unicodePwd", &pwdblob
)) {
260 tldap_entry_dn(msg
[0], &dn
);
261 DEBUG(10, ("No unicodePwd attribute in %s\n",
262 dn
? dn
: "<unknown DN>"));
263 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
266 if (pwdblob
.length
!= 16) {
267 DEBUG(10, ("Password hash hash has length %d, expected 16\n",
268 (int)pwdblob
.length
));
269 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
272 memcpy(pwd
, pwdblob
.data
, 16);
279 static NTSTATUS
check_netlogond_security(const struct auth_context
*auth_context
,
280 void *my_private_data
,
282 const struct auth_usersupplied_info
*user_info
,
283 struct auth_serversupplied_info
**server_info
)
285 TALLOC_CTX
*frame
= talloc_stackframe();
286 struct netr_SamInfo3
*info3
= NULL
;
287 struct rpc_pipe_client
*p
= NULL
;
288 struct pipe_auth_data
*auth
= NULL
;
289 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
290 uint8_t machine_password
[16];
291 struct netlogon_creds_CredentialState
*creds
;
292 NTSTATUS schannel_bind_result
, status
;
293 struct named_mutex
*mutex
= NULL
;
294 const char *ncalrpcsock
;
296 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
298 ncalrpcsock
= lp_parm_const_string(
299 GLOBAL_SECTION_SNUM
, "auth_netlogond", "socket", NULL
);
301 if (ncalrpcsock
== NULL
) {
302 ncalrpcsock
= talloc_asprintf(talloc_tos(), "%s/%s",
303 get_dyn_NCALRPCDIR(), "DEFAULT");
306 if (ncalrpcsock
== NULL
) {
307 status
= NT_STATUS_NO_MEMORY
;
311 creds
= secrets_fetch_local_schannel_creds(talloc_tos());
316 status
= netlogond_validate(talloc_tos(), auth_context
, ncalrpcsock
,
317 creds
, user_info
, &info3
,
318 &schannel_bind_result
);
320 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status
)));
322 if (NT_STATUS_IS_OK(status
)) {
326 if (NT_STATUS_IS_OK(schannel_bind_result
)) {
328 * This is a real failure from the DC
335 mutex
= grab_named_mutex(talloc_tos(), "LOCAL_SCHANNEL_KEY", 60);
337 DEBUG(10, ("Could not get mutex LOCAL_SCHANNEL_KEY\n"));
338 status
= NT_STATUS_ACCESS_DENIED
;
342 DEBUG(10, ("schannel bind failed, setting up new key\n"));
344 status
= rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpcsock
,
345 &ndr_table_netlogon
.syntax_id
, &p
);
347 if (!NT_STATUS_IS_OK(status
)) {
348 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
353 status
= rpccli_anon_bind_data(p
, &auth
);
354 if (!NT_STATUS_IS_OK(status
)) {
355 DEBUG(10, ("rpccli_anon_bind_data failed: %s\n",
360 status
= rpc_pipe_bind(p
, auth
);
361 if (!NT_STATUS_IS_OK(status
)) {
362 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status
)));
366 status
= mymachinepw(machine_password
);
367 if (!NT_STATUS_IS_OK(status
)) {
368 DEBUG(10, ("mymachinepw failed: %s\n", nt_errstr(status
)));
372 DEBUG(10, ("machinepw "));
373 dump_data(10, machine_password
, 16);
375 status
= rpccli_netlogon_setup_creds(
376 p
, global_myname(), lp_workgroup(), global_myname(),
377 global_myname(), machine_password
, SEC_CHAN_BDC
, &neg_flags
);
379 if (!NT_STATUS_IS_OK(status
)) {
380 DEBUG(10, ("rpccli_netlogon_setup_creds failed: %s\n",
385 secrets_store_local_schannel_creds(p
->dc
);
388 * Retry the authentication with the mutex held. This way nobody else
389 * can step on our toes.
392 status
= netlogond_validate(talloc_tos(), auth_context
, ncalrpcsock
,
393 p
->dc
, user_info
, &info3
,
394 &schannel_bind_result
);
398 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status
)));
400 if (!NT_STATUS_IS_OK(status
)) {
406 status
= make_server_info_info3(mem_ctx
, user_info
->client
.account_name
,
407 user_info
->mapped
.domain_name
, server_info
,
409 if (!NT_STATUS_IS_OK(status
)) {
410 DEBUG(10, ("make_server_info_info3 failed: %s\n",
416 status
= NT_STATUS_OK
;
423 /* module initialisation */
424 static NTSTATUS
auth_init_netlogond(struct auth_context
*auth_context
,
426 auth_methods
**auth_method
)
428 struct auth_methods
*result
;
430 result
= TALLOC_ZERO_P(auth_context
, struct auth_methods
);
431 if (result
== NULL
) {
432 return NT_STATUS_NO_MEMORY
;
434 result
->name
= "netlogond";
435 result
->auth
= check_netlogond_security
;
437 *auth_method
= result
;
441 NTSTATUS
auth_netlogond_init(void)
443 smb_register_auth(AUTH_INTERFACE_VERSION
, "netlogond",
444 auth_init_netlogond
);