Add an entry for the "check" command to the tdbtool manpage.
[Samba/gebeck_regimport.git] / source3 / auth / auth_netlogond.c
bloba57f3b74a3662a0353acc62a181ee4ef29d24b2f
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"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_AUTH
25 static NTSTATUS netlogond_validate(TALLOC_CTX *mem_ctx,
26 const struct auth_context *auth_context,
27 const char *ncalrpc_sockname,
28 uint8_t schannel_key[16],
29 const auth_usersupplied_info *user_info,
30 struct netr_SamInfo3 **pinfo3,
31 NTSTATUS *schannel_bind_result)
33 struct rpc_pipe_client *p;
34 struct cli_pipe_auth_data *auth;
35 struct netr_SamInfo3 *info3 = NULL;
36 NTSTATUS status;
38 *schannel_bind_result = NT_STATUS_OK;
40 status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpc_sockname,
41 &ndr_table_netlogon.syntax_id, &p);
42 if (!NT_STATUS_IS_OK(status)) {
43 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
44 nt_errstr(status)));
45 return status;
48 status = rpccli_schannel_bind_data(p, lp_workgroup(),
49 PIPE_AUTH_LEVEL_PRIVACY,
50 schannel_key, &auth);
51 if (!NT_STATUS_IS_OK(status)) {
52 DEBUG(10, ("rpccli_schannel_bind_data failed: %s\n",
53 nt_errstr(status)));
54 TALLOC_FREE(p);
55 return status;
58 status = rpc_pipe_bind(p, auth);
59 if (!NT_STATUS_IS_OK(status)) {
60 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
61 TALLOC_FREE(p);
62 *schannel_bind_result = status;
63 return status;
67 * We have to fake a struct dcinfo, so that
68 * rpccli_netlogon_sam_network_logon_ex can decrypt the session keys.
71 p->dc = talloc(p, struct dcinfo);
72 if (p->dc == NULL) {
73 DEBUG(0, ("talloc failed\n"));
74 TALLOC_FREE(p);
75 return NT_STATUS_NO_MEMORY;
78 memcpy(p->dc->sess_key, schannel_key, 16);
80 status = rpccli_netlogon_sam_network_logon_ex(
81 p, p,
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",
94 nt_errstr(status)));
96 if (!NT_STATUS_IS_OK(status)) {
97 TALLOC_FREE(p);
98 return status;
101 *pinfo3 = talloc_move(mem_ctx, &info3);
103 TALLOC_FREE(p);
104 return NT_STATUS_OK;
107 static char *mymachinepw(TALLOC_CTX *mem_ctx)
109 fstring pwd;
110 const char *script;
111 char *to_free = NULL;
112 ssize_t nread;
113 int ret, fd;
115 script = lp_parm_const_string(
116 GLOBAL_SECTION_SNUM, "auth_netlogond", "machinepwscript",
117 NULL);
119 if (script == NULL) {
120 to_free = talloc_asprintf(talloc_tos(), "%s/%s",
121 get_dyn_SBINDIR(), "mymachinepw");
122 script = to_free;
124 if (script == NULL) {
125 return NULL;
128 ret = smbrun(script, &fd);
129 DEBUG(ret ? 0 : 3, ("mymachinepw: Running the command `%s' gave %d\n",
130 script, ret));
131 TALLOC_FREE(to_free);
133 if (ret != 0) {
134 return NULL;
137 pwd[sizeof(pwd)-1] = '\0';
139 nread = read(fd, pwd, sizeof(pwd)-1);
140 close(fd);
142 if (nread <= 0) {
143 DEBUG(3, ("mymachinepwd: Could not read password\n"));
144 return NULL;
147 DEBUG(0, ("pwd: %d [%s]\n", (int)nread, pwd));
149 if (pwd[nread-1] == '\n') {
150 pwd[nread-1] = '\0';
153 return talloc_strdup(mem_ctx, pwd);
156 static NTSTATUS check_netlogond_security(const struct auth_context *auth_context,
157 void *my_private_data,
158 TALLOC_CTX *mem_ctx,
159 const auth_usersupplied_info *user_info,
160 auth_serversupplied_info **server_info)
162 TALLOC_CTX *frame = talloc_stackframe();
163 struct netr_SamInfo3 *info3 = NULL;
164 struct rpc_pipe_client *p;
165 struct cli_pipe_auth_data *auth;
166 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
167 char *plaintext_machinepw;
168 uint8_t machine_password[16];
169 uint8_t schannel_key[16];
170 NTSTATUS schannel_bind_result, status;
171 struct named_mutex *mutex;
172 const char *ncalrpcsock;
174 ncalrpcsock = lp_parm_const_string(
175 GLOBAL_SECTION_SNUM, "auth_netlogond", "socket", NULL);
177 if (ncalrpcsock == NULL) {
178 ncalrpcsock = talloc_asprintf(talloc_tos(), "%s/%s",
179 get_dyn_NCALRPCDIR(), "DEFAULT");
182 if (ncalrpcsock == NULL) {
183 status = NT_STATUS_NO_MEMORY;
184 goto done;
187 if (!secrets_fetch_local_schannel_key(schannel_key)) {
188 goto new_key;
191 status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
192 schannel_key, user_info, &info3,
193 &schannel_bind_result);
195 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
197 if (NT_STATUS_IS_OK(status)) {
198 goto okay;
201 if (NT_STATUS_IS_OK(schannel_bind_result)) {
203 * This is a real failure from the DC
205 goto done;
208 new_key:
210 mutex = grab_named_mutex(talloc_tos(), "LOCAL_SCHANNEL_KEY", 60);
211 if (mutex == NULL) {
212 DEBUG(10, ("Could not get mutex LOCAL_SCHANNEL_KEY\n"));
213 status = NT_STATUS_ACCESS_DENIED;
214 goto done;
217 DEBUG(10, ("schannel bind failed, setting up new key\n"));
219 status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpcsock,
220 &ndr_table_netlogon.syntax_id, &p);
222 if (!NT_STATUS_IS_OK(status)) {
223 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
224 nt_errstr(status)));
225 goto done;
228 status = rpccli_anon_bind_data(p, &auth);
229 if (!NT_STATUS_IS_OK(status)) {
230 DEBUG(10, ("rpccli_anon_bind_data failed: %s\n",
231 nt_errstr(status)));
232 goto done;
235 status = rpc_pipe_bind(p, auth);
236 if (!NT_STATUS_IS_OK(status)) {
237 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
238 goto done;
241 TALLOC_FREE(auth);
243 plaintext_machinepw = mymachinepw(talloc_tos());
244 if (plaintext_machinepw == NULL) {
245 status = NT_STATUS_NO_MEMORY;
246 goto done;
249 E_md4hash(plaintext_machinepw, machine_password);
251 TALLOC_FREE(plaintext_machinepw);
253 status = rpccli_netlogon_setup_creds(
254 p, global_myname(), lp_workgroup(), global_myname(),
255 global_myname(), machine_password, SEC_CHAN_BDC, &neg_flags);
257 if (!NT_STATUS_IS_OK(status)) {
258 DEBUG(10, ("rpccli_netlogon_setup_creds failed: %s\n",
259 nt_errstr(status)));
260 goto done;
263 memcpy(schannel_key, p->dc->sess_key, 16);
264 secrets_store_local_schannel_key(schannel_key);
266 TALLOC_FREE(p);
269 * Retry the authentication with the mutex held. This way nobody else
270 * can step on our toes.
273 status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
274 schannel_key, user_info, &info3,
275 &schannel_bind_result);
277 DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
279 if (!NT_STATUS_IS_OK(status)) {
280 goto done;
283 okay:
285 status = make_server_info_info3(mem_ctx, user_info->smb_name,
286 user_info->domain, server_info,
287 info3);
288 if (!NT_STATUS_IS_OK(status)) {
289 DEBUG(10, ("make_server_info_info3 failed: %s\n",
290 nt_errstr(status)));
291 TALLOC_FREE(frame);
292 return status;
295 status = NT_STATUS_OK;
297 done:
298 TALLOC_FREE(frame);
299 return status;
302 /* module initialisation */
303 static NTSTATUS auth_init_netlogond(struct auth_context *auth_context,
304 const char *param,
305 auth_methods **auth_method)
307 if (!make_auth_methods(auth_context, auth_method)) {
308 return NT_STATUS_NO_MEMORY;
311 (*auth_method)->name = "netlogond";
312 (*auth_method)->auth = check_netlogond_security;
313 return NT_STATUS_OK;
316 NTSTATUS auth_netlogond_init(void)
318 smb_register_auth(AUTH_INTERFACE_VERSION, "netlogond",
319 auth_init_netlogond);
320 return NT_STATUS_OK;