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"
25 #define DBGC_CLASS DBGC_AUTH
27 static NTSTATUS netlogond_validate(TALLOC_CTX *mem_ctx,
28 const struct auth_context *auth_context,
29 const char *ncalrpc_sockname,
30 uint8_t schannel_key[16],
31 const struct auth_usersupplied_info *user_info,
32 struct netr_SamInfo3 **pinfo3,
33 NTSTATUS *schannel_bind_result)
35 struct rpc_pipe_client *p = NULL;
36 struct cli_pipe_auth_data *auth = NULL;
37 struct netr_SamInfo3 *info3 = NULL;
40 *schannel_bind_result = NT_STATUS_OK;
42 status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpc_sockname,
43 &ndr_table_netlogon.syntax_id, &p);
44 if (!NT_STATUS_IS_OK(status)) {
45 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
51 * We have to fake a struct dcinfo, so that
52 * rpccli_netlogon_sam_network_logon_ex can decrypt the session keys.
55 p->dc = netlogon_creds_client_init_session_key(p, schannel_key);
57 DEBUG(0, ("talloc failed\n"));
59 return NT_STATUS_NO_MEMORY;
62 status = rpccli_schannel_bind_data(p, lp_workgroup(),
63 DCERPC_AUTH_LEVEL_PRIVACY,
65 if (!NT_STATUS_IS_OK(status)) {
66 DEBUG(10, ("rpccli_schannel_bind_data failed: %s\n",
72 status = rpc_pipe_bind(p, auth);
73 if (!NT_STATUS_IS_OK(status)) {
74 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
76 *schannel_bind_result = status;
80 status = rpccli_netlogon_sam_network_logon_ex(
82 user_info->logon_parameters,/* flags such as 'allow
83 * workstation logon' */
84 global_myname(), /* server name */
85 user_info->smb_name, /* user name logging on. */
86 user_info->client_domain, /* domain name */
87 user_info->wksta_name, /* workstation name */
88 (uchar *)auth_context->challenge.data, /* 8 byte challenge. */
89 user_info->lm_resp, /* lanman 24 byte response */
90 user_info->nt_resp, /* nt 24 byte response */
91 &info3); /* info3 out */
93 DEBUG(10, ("rpccli_netlogon_sam_network_logon_ex returned %s\n",
96 if (!NT_STATUS_IS_OK(status)) {
101 *pinfo3 = talloc_move(mem_ctx, &info3);
107 static char *mymachinepw(TALLOC_CTX *mem_ctx)
111 char *to_free = NULL;
115 script = lp_parm_const_string(
116 GLOBAL_SECTION_SNUM, "auth_netlogond", "machinepwscript",
119 if (script == NULL) {
120 to_free = talloc_asprintf(talloc_tos(), "%s/%s",
121 get_dyn_SBINDIR(), "mymachinepw");
124 if (script == NULL) {
128 ret = smbrun(script, &fd);
129 DEBUG(ret ? 0 : 3, ("mymachinepw: Running the command `%s' gave %d\n",
131 TALLOC_FREE(to_free);
137 nread = read(fd, pwd, sizeof(pwd)-1);
141 DEBUG(3, ("mymachinepwd: Could not read password\n"));
147 if (pwd[nread-1] == '\n') {
151 return talloc_strdup(mem_ctx, pwd);
154 static NTSTATUS check_netlogond_security(const struct auth_context *auth_context,
155 void *my_private_data,
157 const struct auth_usersupplied_info *user_info,
158 struct auth_serversupplied_info **server_info)
160 TALLOC_CTX *frame = talloc_stackframe();
161 struct netr_SamInfo3 *info3 = NULL;
162 struct rpc_pipe_client *p = NULL;
163 struct cli_pipe_auth_data *auth = NULL;
164 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
165 char *plaintext_machinepw = NULL;
166 uint8_t machine_password[16];
167 uint8_t schannel_key[16];
168 NTSTATUS schannel_bind_result, status;
169 struct named_mutex *mutex = NULL;
170 const char *ncalrpcsock;
172 ncalrpcsock = lp_parm_const_string(
173 GLOBAL_SECTION_SNUM, "auth_netlogond", "socket", NULL);
175 if (ncalrpcsock == NULL) {
176 ncalrpcsock = talloc_asprintf(talloc_tos(), "%s/%s",
177 get_dyn_NCALRPCDIR(), "DEFAULT");
180 if (ncalrpcsock == NULL) {
181 status = NT_STATUS_NO_MEMORY;
185 if (!secrets_fetch_local_schannel_key(schannel_key)) {
189 status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
190 schannel_key, user_info, &info3,
191 &schannel_bind_result);
193 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
195 if (NT_STATUS_IS_OK(status)) {
199 if (NT_STATUS_IS_OK(schannel_bind_result)) {
201 * This is a real failure from the DC
208 mutex = grab_named_mutex(talloc_tos(), "LOCAL_SCHANNEL_KEY", 60);
210 DEBUG(10, ("Could not get mutex LOCAL_SCHANNEL_KEY\n"));
211 status = NT_STATUS_ACCESS_DENIED;
215 DEBUG(10, ("schannel bind failed, setting up new key\n"));
217 status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpcsock,
218 &ndr_table_netlogon.syntax_id, &p);
220 if (!NT_STATUS_IS_OK(status)) {
221 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
226 status = rpccli_anon_bind_data(p, &auth);
227 if (!NT_STATUS_IS_OK(status)) {
228 DEBUG(10, ("rpccli_anon_bind_data failed: %s\n",
233 status = rpc_pipe_bind(p, auth);
234 if (!NT_STATUS_IS_OK(status)) {
235 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
239 plaintext_machinepw = mymachinepw(talloc_tos());
240 if (plaintext_machinepw == NULL) {
241 status = NT_STATUS_NO_MEMORY;
245 E_md4hash(plaintext_machinepw, machine_password);
247 TALLOC_FREE(plaintext_machinepw);
249 status = rpccli_netlogon_setup_creds(
250 p, global_myname(), lp_workgroup(), global_myname(),
251 global_myname(), machine_password, SEC_CHAN_BDC, &neg_flags);
253 if (!NT_STATUS_IS_OK(status)) {
254 DEBUG(10, ("rpccli_netlogon_setup_creds failed: %s\n",
259 memcpy(schannel_key, p->dc->session_key, 16);
260 secrets_store_local_schannel_key(schannel_key);
265 * Retry the authentication with the mutex held. This way nobody else
266 * can step on our toes.
269 status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
270 schannel_key, user_info, &info3,
271 &schannel_bind_result);
273 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
275 if (!NT_STATUS_IS_OK(status)) {
281 status = make_server_info_info3(mem_ctx, user_info->smb_name,
282 user_info->domain, server_info,
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(10, ("make_server_info_info3 failed: %s\n",
291 status = NT_STATUS_OK;
298 /* module initialisation */
299 static NTSTATUS auth_init_netlogond(struct auth_context *auth_context,
301 auth_methods **auth_method)
303 struct auth_methods *result;
305 result = TALLOC_ZERO_P(auth_context, struct auth_methods);
306 if (result == NULL) {
307 return NT_STATUS_NO_MEMORY;
309 result->name = "netlogond";
310 result->auth = check_netlogond_security;
312 *auth_method = result;
316 NTSTATUS auth_netlogond_init(void)
318 smb_register_auth(AUTH_INTERFACE_VERSION, "netlogond",
319 auth_init_netlogond);