s4-smbtorture: remove another incarnation of test_ClosePrinter.
[Samba.git] / source3 / auth / auth_netlogond.c
blob28ef93398ca5a691ca609e308783e16de6aff574
1 /*
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/>.
20 #include "includes.h"
21 #include "../libcli/auth/libcli_auth.h"
22 #include "../librpc/gen_ndr/ndr_netlogon.h"
23 #include "rpc_client/cli_netlogon.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_AUTH
28 static NTSTATUS netlogond_validate(TALLOC_CTX *mem_ctx,
29 const struct auth_context *auth_context,
30 const char *ncalrpc_sockname,
31 uint8_t schannel_key[16],
32 const struct auth_usersupplied_info *user_info,
33 struct netr_SamInfo3 **pinfo3,
34 NTSTATUS *schannel_bind_result)
36 struct rpc_pipe_client *p = NULL;
37 struct cli_pipe_auth_data *auth = NULL;
38 struct netr_SamInfo3 *info3 = NULL;
39 NTSTATUS status;
41 *schannel_bind_result = NT_STATUS_OK;
43 status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpc_sockname,
44 &ndr_table_netlogon.syntax_id, &p);
45 if (!NT_STATUS_IS_OK(status)) {
46 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
47 nt_errstr(status)));
48 return status;
52 * We have to fake a struct dcinfo, so that
53 * rpccli_netlogon_sam_network_logon_ex can decrypt the session keys.
56 p->dc = netlogon_creds_client_init_session_key(p, schannel_key);
57 if (p->dc == NULL) {
58 DEBUG(0, ("talloc failed\n"));
59 TALLOC_FREE(p);
60 return NT_STATUS_NO_MEMORY;
63 status = rpccli_schannel_bind_data(p, lp_workgroup(),
64 DCERPC_AUTH_LEVEL_PRIVACY,
65 p->dc, &auth);
66 if (!NT_STATUS_IS_OK(status)) {
67 DEBUG(10, ("rpccli_schannel_bind_data failed: %s\n",
68 nt_errstr(status)));
69 TALLOC_FREE(p);
70 return status;
73 status = rpc_pipe_bind(p, auth);
74 if (!NT_STATUS_IS_OK(status)) {
75 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
76 TALLOC_FREE(p);
77 *schannel_bind_result = status;
78 return status;
81 status = rpccli_netlogon_sam_network_logon_ex(
82 p, p,
83 user_info->logon_parameters, /* flags such as 'allow
84 * workstation logon' */
85 global_myname(), /* server name */
86 user_info->smb_name, /* user name logging on. */
87 user_info->client_domain, /* domain name */
88 user_info->workstation_name, /* workstation name */
89 (uchar *)auth_context->challenge.data, /* 8 byte challenge. */
90 user_info->lm_resp, /* lanman 24 byte response */
91 user_info->nt_resp, /* nt 24 byte response */
92 &info3); /* info3 out */
94 DEBUG(10, ("rpccli_netlogon_sam_network_logon_ex returned %s\n",
95 nt_errstr(status)));
97 if (!NT_STATUS_IS_OK(status)) {
98 TALLOC_FREE(p);
99 return status;
102 *pinfo3 = talloc_move(mem_ctx, &info3);
104 TALLOC_FREE(p);
105 return NT_STATUS_OK;
108 static char *mymachinepw(TALLOC_CTX *mem_ctx)
110 fstring pwd;
111 const char *script;
112 char *to_free = NULL;
113 ssize_t nread;
114 int ret, fd;
116 script = lp_parm_const_string(
117 GLOBAL_SECTION_SNUM, "auth_netlogond", "machinepwscript",
118 NULL);
120 if (script == NULL) {
121 to_free = talloc_asprintf(talloc_tos(), "%s/%s",
122 get_dyn_SBINDIR(), "mymachinepw");
123 script = to_free;
125 if (script == NULL) {
126 return NULL;
129 ret = smbrun(script, &fd);
130 DEBUG(ret ? 0 : 3, ("mymachinepw: Running the command `%s' gave %d\n",
131 script, ret));
132 TALLOC_FREE(to_free);
134 if (ret != 0) {
135 return NULL;
138 nread = read(fd, pwd, sizeof(pwd)-1);
139 close(fd);
141 if (nread <= 0) {
142 DEBUG(3, ("mymachinepwd: Could not read password\n"));
143 return NULL;
146 pwd[nread] = '\0';
148 if (pwd[nread-1] == '\n') {
149 pwd[nread-1] = '\0';
152 return talloc_strdup(mem_ctx, pwd);
155 static NTSTATUS check_netlogond_security(const struct auth_context *auth_context,
156 void *my_private_data,
157 TALLOC_CTX *mem_ctx,
158 const struct auth_usersupplied_info *user_info,
159 struct auth_serversupplied_info **server_info)
161 TALLOC_CTX *frame = talloc_stackframe();
162 struct netr_SamInfo3 *info3 = NULL;
163 struct rpc_pipe_client *p = NULL;
164 struct cli_pipe_auth_data *auth = NULL;
165 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
166 char *plaintext_machinepw = NULL;
167 uint8_t machine_password[16];
168 uint8_t schannel_key[16];
169 NTSTATUS schannel_bind_result, status;
170 struct named_mutex *mutex = NULL;
171 const char *ncalrpcsock;
173 DEBUG(10, ("Check auth for: [%s]\n", user_info->internal_username));
175 ncalrpcsock = lp_parm_const_string(
176 GLOBAL_SECTION_SNUM, "auth_netlogond", "socket", NULL);
178 if (ncalrpcsock == NULL) {
179 ncalrpcsock = talloc_asprintf(talloc_tos(), "%s/%s",
180 get_dyn_NCALRPCDIR(), "DEFAULT");
183 if (ncalrpcsock == NULL) {
184 status = NT_STATUS_NO_MEMORY;
185 goto done;
188 if (!secrets_fetch_local_schannel_key(schannel_key)) {
189 goto new_key;
192 status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
193 schannel_key, user_info, &info3,
194 &schannel_bind_result);
196 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
198 if (NT_STATUS_IS_OK(status)) {
199 goto okay;
202 if (NT_STATUS_IS_OK(schannel_bind_result)) {
204 * This is a real failure from the DC
206 goto done;
209 new_key:
211 mutex = grab_named_mutex(talloc_tos(), "LOCAL_SCHANNEL_KEY", 60);
212 if (mutex == NULL) {
213 DEBUG(10, ("Could not get mutex LOCAL_SCHANNEL_KEY\n"));
214 status = NT_STATUS_ACCESS_DENIED;
215 goto done;
218 DEBUG(10, ("schannel bind failed, setting up new key\n"));
220 status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpcsock,
221 &ndr_table_netlogon.syntax_id, &p);
223 if (!NT_STATUS_IS_OK(status)) {
224 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
225 nt_errstr(status)));
226 goto done;
229 status = rpccli_anon_bind_data(p, &auth);
230 if (!NT_STATUS_IS_OK(status)) {
231 DEBUG(10, ("rpccli_anon_bind_data failed: %s\n",
232 nt_errstr(status)));
233 goto done;
236 status = rpc_pipe_bind(p, auth);
237 if (!NT_STATUS_IS_OK(status)) {
238 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
239 goto done;
242 plaintext_machinepw = mymachinepw(talloc_tos());
243 if (plaintext_machinepw == NULL) {
244 status = NT_STATUS_NO_MEMORY;
245 goto done;
248 E_md4hash(plaintext_machinepw, machine_password);
250 TALLOC_FREE(plaintext_machinepw);
252 status = rpccli_netlogon_setup_creds(
253 p, global_myname(), lp_workgroup(), global_myname(),
254 global_myname(), machine_password, SEC_CHAN_BDC, &neg_flags);
256 if (!NT_STATUS_IS_OK(status)) {
257 DEBUG(10, ("rpccli_netlogon_setup_creds failed: %s\n",
258 nt_errstr(status)));
259 goto done;
262 memcpy(schannel_key, p->dc->session_key, 16);
263 secrets_store_local_schannel_key(schannel_key);
265 TALLOC_FREE(p);
268 * Retry the authentication with the mutex held. This way nobody else
269 * can step on our toes.
272 status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
273 schannel_key, user_info, &info3,
274 &schannel_bind_result);
276 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
278 if (!NT_STATUS_IS_OK(status)) {
279 goto done;
282 okay:
284 status = make_server_info_info3(mem_ctx, user_info->smb_name,
285 user_info->domain, server_info,
286 info3);
287 if (!NT_STATUS_IS_OK(status)) {
288 DEBUG(10, ("make_server_info_info3 failed: %s\n",
289 nt_errstr(status)));
290 TALLOC_FREE(frame);
291 return status;
294 status = NT_STATUS_OK;
296 done:
297 TALLOC_FREE(frame);
298 return status;
301 /* module initialisation */
302 static NTSTATUS auth_init_netlogond(struct auth_context *auth_context,
303 const char *param,
304 auth_methods **auth_method)
306 struct auth_methods *result;
308 result = TALLOC_ZERO_P(auth_context, struct auth_methods);
309 if (result == NULL) {
310 return NT_STATUS_NO_MEMORY;
312 result->name = "netlogond";
313 result->auth = check_netlogond_security;
315 *auth_method = result;
316 return NT_STATUS_OK;
319 NTSTATUS auth_netlogond_init(void)
321 smb_register_auth(AUTH_INTERFACE_VERSION, "netlogond",
322 auth_init_netlogond);
323 return NT_STATUS_OK;