d595e27a60600a185f593337e0192ee7bb8acb19
[kai/samba.git] / source3 / auth / auth_netlogond.c
1 /*
2    Unix SMB/CIFS implementation.
3    Authenticate against a netlogon pipe listening on a unix domain socket
4    Copyright (C) Volker Lendecke 2008
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "../libcli/auth/libcli_auth.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_AUTH
25
26 static NTSTATUS netlogond_validate(TALLOC_CTX *mem_ctx,
27                                    const struct auth_context *auth_context,
28                                    const char *ncalrpc_sockname,
29                                    uint8_t schannel_key[16],
30                                    const auth_usersupplied_info *user_info,
31                                    struct netr_SamInfo3 **pinfo3,
32                                    NTSTATUS *schannel_bind_result)
33 {
34         struct rpc_pipe_client *p;
35         struct cli_pipe_auth_data *auth;
36         struct netr_SamInfo3 *info3 = NULL;
37         NTSTATUS status;
38
39         *schannel_bind_result = NT_STATUS_OK;
40
41         status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpc_sockname,
42                                        &ndr_table_netlogon.syntax_id, &p);
43         if (!NT_STATUS_IS_OK(status)) {
44                 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
45                            nt_errstr(status)));
46                 return status;
47         }
48
49         status = rpccli_schannel_bind_data(p, lp_workgroup(),
50                                            PIPE_AUTH_LEVEL_PRIVACY,
51                                            schannel_key, &auth);
52         if (!NT_STATUS_IS_OK(status)) {
53                 DEBUG(10, ("rpccli_schannel_bind_data failed: %s\n",
54                            nt_errstr(status)));
55                 TALLOC_FREE(p);
56                 return status;
57         }
58
59         status = rpc_pipe_bind(p, auth);
60         if (!NT_STATUS_IS_OK(status)) {
61                 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
62                 TALLOC_FREE(p);
63                 *schannel_bind_result = status;
64                 return status;
65         }
66
67         /*
68          * We have to fake a struct dcinfo, so that
69          * rpccli_netlogon_sam_network_logon_ex can decrypt the session keys.
70          */
71
72         p->dc = talloc(p, struct dcinfo);
73         if (p->dc == NULL) {
74                 DEBUG(0, ("talloc failed\n"));
75                 TALLOC_FREE(p);
76                 return NT_STATUS_NO_MEMORY;
77         }
78
79         memcpy(p->dc->sess_key, schannel_key, 16);
80
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->wksta_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 */
93
94         DEBUG(10, ("rpccli_netlogon_sam_network_logon_ex returned %s\n",
95                    nt_errstr(status)));
96
97         if (!NT_STATUS_IS_OK(status)) {
98                 TALLOC_FREE(p);
99                 return status;
100         }
101
102         *pinfo3 = talloc_move(mem_ctx, &info3);
103
104         TALLOC_FREE(p);
105         return NT_STATUS_OK;
106 }
107
108 static char *mymachinepw(TALLOC_CTX *mem_ctx)
109 {
110         fstring pwd;
111         const char *script;
112         char *to_free = NULL;
113         ssize_t nread;
114         int ret, fd;
115
116         script = lp_parm_const_string(
117                 GLOBAL_SECTION_SNUM, "auth_netlogond", "machinepwscript",
118                 NULL);
119
120         if (script == NULL) {
121                 to_free = talloc_asprintf(talloc_tos(), "%s/%s",
122                                           get_dyn_SBINDIR(), "mymachinepw");
123                 script = to_free;
124         }
125         if (script == NULL) {
126                 return NULL;
127         }
128
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);
133
134         if (ret != 0) {
135                 return NULL;
136         }
137
138         nread = read(fd, pwd, sizeof(pwd)-1);
139         close(fd);
140
141         if (nread <= 0) {
142                 DEBUG(3, ("mymachinepwd: Could not read password\n"));
143                 return NULL;
144         }
145
146         pwd[nread] = '\0';
147
148         if (pwd[nread-1] == '\n') {
149                 pwd[nread-1] = '\0';
150         }
151
152         return talloc_strdup(mem_ctx, pwd);
153 }
154
155 static NTSTATUS check_netlogond_security(const struct auth_context *auth_context,
156                                          void *my_private_data,
157                                          TALLOC_CTX *mem_ctx,
158                                          const auth_usersupplied_info *user_info,
159                                          auth_serversupplied_info **server_info)
160 {
161         TALLOC_CTX *frame = talloc_stackframe();
162         struct netr_SamInfo3 *info3 = NULL;
163         struct rpc_pipe_client *p;
164         struct cli_pipe_auth_data *auth;
165         uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
166         char *plaintext_machinepw;
167         uint8_t machine_password[16];
168         uint8_t schannel_key[16];
169         NTSTATUS schannel_bind_result, status;
170         struct named_mutex *mutex;
171         const char *ncalrpcsock;
172
173         ncalrpcsock = lp_parm_const_string(
174                 GLOBAL_SECTION_SNUM, "auth_netlogond", "socket", NULL);
175
176         if (ncalrpcsock == NULL) {
177                 ncalrpcsock = talloc_asprintf(talloc_tos(), "%s/%s",
178                                               get_dyn_NCALRPCDIR(), "DEFAULT");
179         }
180
181         if (ncalrpcsock == NULL) {
182                 status = NT_STATUS_NO_MEMORY;
183                 goto done;
184         }
185
186         if (!secrets_fetch_local_schannel_key(schannel_key)) {
187                 goto new_key;
188         }
189
190         status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
191                                     schannel_key, user_info, &info3,
192                                     &schannel_bind_result);
193
194         DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
195
196         if (NT_STATUS_IS_OK(status)) {
197                 goto okay;
198         }
199
200         if (NT_STATUS_IS_OK(schannel_bind_result)) {
201                 /*
202                  * This is a real failure from the DC
203                  */
204                 goto done;
205         }
206
207  new_key:
208
209         mutex = grab_named_mutex(talloc_tos(), "LOCAL_SCHANNEL_KEY", 60);
210         if (mutex == NULL) {
211                 DEBUG(10, ("Could not get mutex LOCAL_SCHANNEL_KEY\n"));
212                 status = NT_STATUS_ACCESS_DENIED;
213                 goto done;
214         }
215
216         DEBUG(10, ("schannel bind failed, setting up new key\n"));
217
218         status = rpc_pipe_open_ncalrpc(talloc_tos(), ncalrpcsock,
219                                        &ndr_table_netlogon.syntax_id, &p);
220
221         if (!NT_STATUS_IS_OK(status)) {
222                 DEBUG(10, ("rpc_pipe_open_ncalrpc failed: %s\n",
223                            nt_errstr(status)));
224                 goto done;
225         }
226
227         status = rpccli_anon_bind_data(p, &auth);
228         if (!NT_STATUS_IS_OK(status)) {
229                 DEBUG(10, ("rpccli_anon_bind_data failed: %s\n",
230                            nt_errstr(status)));
231                 goto done;
232         }
233
234         status = rpc_pipe_bind(p, auth);
235         if (!NT_STATUS_IS_OK(status)) {
236                 DEBUG(10, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
237                 goto done;
238         }
239
240         plaintext_machinepw = mymachinepw(talloc_tos());
241         if (plaintext_machinepw == NULL) {
242                 status = NT_STATUS_NO_MEMORY;
243                 goto done;
244         }
245
246         E_md4hash(plaintext_machinepw, machine_password);
247
248         TALLOC_FREE(plaintext_machinepw);
249
250         status = rpccli_netlogon_setup_creds(
251                 p, global_myname(), lp_workgroup(), global_myname(),
252                 global_myname(), machine_password, SEC_CHAN_BDC, &neg_flags);
253
254         if (!NT_STATUS_IS_OK(status)) {
255                 DEBUG(10, ("rpccli_netlogon_setup_creds failed: %s\n",
256                            nt_errstr(status)));
257                 goto done;
258         }
259
260         memcpy(schannel_key, p->dc->sess_key, 16);
261         secrets_store_local_schannel_key(schannel_key);
262
263         TALLOC_FREE(p);
264
265         /*
266          * Retry the authentication with the mutex held. This way nobody else
267          * can step on our toes.
268          */
269
270         status = netlogond_validate(talloc_tos(), auth_context, ncalrpcsock,
271                                     schannel_key, user_info, &info3,
272                                     &schannel_bind_result);
273
274         DEBUG(10, ("netlogond_validate returned %s\n", nt_errstr(status)));
275
276         if (!NT_STATUS_IS_OK(status)) {
277                 goto done;
278         }
279
280  okay:
281
282         status = make_server_info_info3(mem_ctx, user_info->smb_name,
283                                         user_info->domain, server_info,
284                                         info3);
285         if (!NT_STATUS_IS_OK(status)) {
286                 DEBUG(10, ("make_server_info_info3 failed: %s\n",
287                            nt_errstr(status)));
288                 TALLOC_FREE(frame);
289                 return status;
290         }
291
292         status = NT_STATUS_OK;
293
294  done:
295         TALLOC_FREE(frame);
296         return status;
297 }
298
299 /* module initialisation */
300 static NTSTATUS auth_init_netlogond(struct auth_context *auth_context,
301                                     const char *param,
302                                     auth_methods **auth_method)
303 {
304         if (!make_auth_methods(auth_context, auth_method)) {
305                 return NT_STATUS_NO_MEMORY;
306         }
307
308         (*auth_method)->name = "netlogond";
309         (*auth_method)->auth = check_netlogond_security;
310         return NT_STATUS_OK;
311 }
312
313 NTSTATUS auth_netlogond_init(void)
314 {
315         smb_register_auth(AUTH_INTERFACE_VERSION, "netlogond",
316                           auth_init_netlogond);
317         return NT_STATUS_OK;
318 }