auth4: add a "winbind_rodc" backend
[sfrench/samba-autobuild/.git] / source4 / auth / ntlm / auth_winbind.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind authentication mechnism
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Andrew Bartlett 2001 - 2002
8    Copyright (C) Stefan Metzmacher 2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "auth/auth.h"
26 #include "auth/ntlm/auth_proto.h"
27 #include "librpc/gen_ndr/ndr_winbind_c.h"
28 #include "lib/messaging/irpc.h"
29 #include "param/param.h"
30 #include "nsswitch/libwbclient/wbclient.h"
31 #include "auth/auth_sam_reply.h"
32 #include "libcli/security/security.h"
33 #include "dsdb/samdb/samdb.h"
34
35 _PUBLIC_ NTSTATUS auth4_winbind_init(void);
36
37 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
38                                    TALLOC_CTX *mem_ctx,
39                                    const struct auth_usersupplied_info *user_info)
40 {
41         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
42                 return NT_STATUS_NOT_IMPLEMENTED;
43         }
44
45         /* TODO: maybe limit the user scope to remote users only */
46         return NT_STATUS_OK;
47 }
48
49 static NTSTATUS winbind_rodc_want_check(struct auth_method_context *ctx,
50                                         TALLOC_CTX *mem_ctx,
51                                         const struct auth_usersupplied_info *user_info)
52 {
53         int ret;
54         bool am_rodc;
55
56         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
57                 return NT_STATUS_NOT_IMPLEMENTED;
58         }
59
60         if (ctx->auth_ctx->sam_ctx == NULL) {
61                 DBG_ERR("ctx->auth_ctx->sam_ctx == NULL, don't check.\n");
62                 return NT_STATUS_NOT_IMPLEMENTED;
63         }
64
65         ret = samdb_rodc(ctx->auth_ctx->sam_ctx, &am_rodc);
66         if (ret != LDB_SUCCESS) {
67                 DBG_ERR("samdb_rodc() failed %d %s, don't check.\n",
68                         ret, ldb_errstring(ctx->auth_ctx->sam_ctx));
69                 return NT_STATUS_NOT_IMPLEMENTED;
70         }
71
72         if (!am_rodc) {
73                 /*
74                  * We don't support trusts yet and we
75                  * don't want to add them using the
76                  * semi-async irpc call that uses
77                  * a nested event loop.
78                  */
79                 return NT_STATUS_NOT_IMPLEMENTED;
80         }
81
82         /*
83          * We're a RODC, so we forward the request to our winbind.
84          * As the RODC is not yet production ready anyway, we keep
85          * the semi-async behavior with nested event loops in order
86          * to keep autobuild happy.
87          */
88         return NT_STATUS_OK;
89 }
90
91 struct winbind_check_password_state {
92         struct winbind_SamLogon req;
93 };
94
95 /*
96  Authenticate a user with a challenge/response
97  using IRPC to the winbind task
98 */
99 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
100                                        TALLOC_CTX *mem_ctx,
101                                        const struct auth_usersupplied_info *user_info, 
102                                        struct auth_user_info_dc **user_info_dc)
103 {
104         NTSTATUS status;
105         struct dcerpc_binding_handle *irpc_handle;
106         struct winbind_check_password_state *s;
107         const struct auth_usersupplied_info *user_info_new;
108         struct netr_IdentityInfo *identity_info;
109
110         if (!ctx->auth_ctx->msg_ctx) {
111                 DEBUG(0,("winbind_check_password: auth_context_create was called with out messaging context\n"));
112                 return NT_STATUS_INTERNAL_ERROR;
113         }
114
115         s = talloc(mem_ctx, struct winbind_check_password_state);
116         NT_STATUS_HAVE_NO_MEMORY(s);
117
118         irpc_handle = irpc_binding_handle_by_name(s, ctx->auth_ctx->msg_ctx,
119                                                   "winbind_server",
120                                                   &ndr_table_winbind);
121         if (irpc_handle == NULL) {
122                 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, " 
123                           "no winbind_server running!\n",
124                           user_info->client.domain_name, user_info->client.account_name));
125                 return NT_STATUS_NO_LOGON_SERVERS;
126         }
127
128         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
129                 struct netr_PasswordInfo *password_info;
130
131                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
132                                            user_info, &user_info_new);
133                 NT_STATUS_NOT_OK_RETURN(status);
134                 user_info = user_info_new;
135
136                 password_info = talloc(s, struct netr_PasswordInfo);
137                 NT_STATUS_HAVE_NO_MEMORY(password_info);
138
139                 password_info->lmpassword = *user_info->password.hash.lanman;
140                 password_info->ntpassword = *user_info->password.hash.nt;
141
142                 identity_info = &password_info->identity_info;
143                 s->req.in.logon_level   = 1;
144                 s->req.in.logon.password= password_info;
145         } else {
146                 struct netr_NetworkInfo *network_info;
147                 uint8_t chal[8];
148
149                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
150                                            user_info, &user_info_new);
151                 NT_STATUS_NOT_OK_RETURN(status);
152                 user_info = user_info_new;
153
154                 network_info = talloc(s, struct netr_NetworkInfo);
155                 NT_STATUS_HAVE_NO_MEMORY(network_info);
156
157                 status = auth_get_challenge(ctx->auth_ctx, chal);
158                 NT_STATUS_NOT_OK_RETURN(status);
159
160                 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
161
162                 network_info->nt.length = user_info->password.response.nt.length;
163                 network_info->nt.data   = user_info->password.response.nt.data;
164
165                 network_info->lm.length = user_info->password.response.lanman.length;
166                 network_info->lm.data   = user_info->password.response.lanman.data;
167
168                 identity_info = &network_info->identity_info;
169                 s->req.in.logon_level   = 2;
170                 s->req.in.logon.network = network_info;
171         }
172
173         identity_info->domain_name.string       = user_info->client.domain_name;
174         identity_info->parameter_control        = user_info->logon_parameters; /* see MSV1_0_* */
175         identity_info->logon_id_low             = 0;
176         identity_info->logon_id_high            = 0;
177         identity_info->account_name.string      = user_info->client.account_name;
178         identity_info->workstation.string       = user_info->workstation_name;
179
180         s->req.in.validation_level      = 3;
181
182         /* Note: this makes use of nested event loops... */
183         dcerpc_binding_handle_set_sync_ev(irpc_handle, ctx->auth_ctx->event_ctx);
184         status = dcerpc_winbind_SamLogon_r(irpc_handle, s, &s->req);
185         NT_STATUS_NOT_OK_RETURN(status);
186
187         if (NT_STATUS_EQUAL(s->req.out.result, NT_STATUS_NO_SUCH_USER) &&
188             !s->req.out.authoritative) {
189                 return NT_STATUS_NOT_IMPLEMENTED;
190         }
191
192         status = make_user_info_dc_netlogon_validation(mem_ctx,
193                                                       user_info->client.account_name,
194                                                       s->req.in.validation_level,
195                                                       &s->req.out.validation,
196                                                        true, /* This user was authenticated */
197                                                       user_info_dc);
198         NT_STATUS_NOT_OK_RETURN(status);
199
200         return NT_STATUS_OK;
201 }
202
203 /*
204  Authenticate a user with a challenge/response
205  using the samba3 winbind protocol via libwbclient
206 */
207 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
208                                                 TALLOC_CTX *mem_ctx,
209                                                 const struct auth_usersupplied_info *user_info,
210                                                 struct auth_user_info_dc **user_info_dc)
211 {
212         struct wbcAuthUserParams params;
213         struct wbcAuthUserInfo *info = NULL;
214         struct wbcAuthErrorInfo *err = NULL;
215         wbcErr wbc_status;
216         NTSTATUS nt_status;
217         struct netr_SamInfo6 *info6 = NULL;
218         union netr_Validation validation;
219
220         /* Send off request */
221         const struct auth_usersupplied_info *user_info_temp;
222         nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
223                                       AUTH_PASSWORD_RESPONSE,
224                                       user_info, &user_info_temp);
225         if (!NT_STATUS_IS_OK(nt_status)) {
226                 return nt_status;
227         }
228         user_info = user_info_temp;
229
230         ZERO_STRUCT(params);
231         ZERO_STRUCT(validation);
232         /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
233
234         params.parameter_control = user_info->logon_parameters;
235         params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
236                                     WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
237         params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
238
239         params.account_name     = user_info->client.account_name;
240         params.domain_name      = user_info->client.domain_name;
241         params.workstation_name = user_info->workstation_name;
242
243         DEBUG(5,("looking up %s@%s logging in from %s\n",
244                   params.account_name, params.domain_name,
245                   params.workstation_name));
246
247         memcpy(params.password.response.challenge,
248                ctx->auth_ctx->challenge.data.data,
249                sizeof(params.password.response.challenge));
250
251         params.password.response.lm_length =
252                 user_info->password.response.lanman.length;
253         params.password.response.nt_length =
254                 user_info->password.response.nt.length;
255
256         params.password.response.lm_data =
257                 user_info->password.response.lanman.data;
258         params.password.response.nt_data =
259                 user_info->password.response.nt.data;
260
261         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
262         if (wbc_status == WBC_ERR_AUTH_ERROR) {
263                 if (err) {
264                         DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
265                               err->nt_string, err->nt_status, err->display_string));
266                         nt_status = NT_STATUS(err->nt_status);
267                         wbcFreeMemory(err);
268                 } else {
269                         nt_status = NT_STATUS_LOGON_FAILURE;
270                 }
271                 NT_STATUS_NOT_OK_RETURN(nt_status);
272         } else if (!WBC_ERROR_IS_OK(wbc_status)) {
273                 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
274                         wbc_status, wbcErrorString(wbc_status)));
275                 if (err) {
276                         DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
277                               err->nt_string, err->nt_status, err->display_string));
278                 }
279                 return NT_STATUS_LOGON_FAILURE;
280         }
281         info6 = wbcAuthUserInfo_to_netr_SamInfo6(mem_ctx, info);
282         wbcFreeMemory(info);
283         if (!info6) {
284                 DEBUG(1, ("wbcAuthUserInfo_to_netr_SamInfo6 failed\n"));
285                 return NT_STATUS_NO_MEMORY;
286         }
287
288         validation.sam6 = info6;
289         nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
290                                                           user_info->client.account_name,
291                                                           6, &validation,
292                                                           true, /* This user was authenticated */
293                                                           user_info_dc);
294         return nt_status;
295
296 }
297
298 static const struct auth_operations winbind_ops = {
299         .name           = "winbind",
300         .want_check     = winbind_want_check,
301         .check_password = winbind_check_password
302 };
303
304 static const struct auth_operations winbind_rodc_ops = {
305         .name           = "winbind_rodc",
306         .want_check     = winbind_rodc_want_check,
307         .check_password = winbind_check_password
308 };
309
310 static const struct auth_operations winbind_wbclient_ops = {
311         .name           = "winbind_wbclient",
312         .want_check     = winbind_want_check,
313         .check_password = winbind_check_password_wbclient
314 };
315
316 _PUBLIC_ NTSTATUS auth4_winbind_init(void)
317 {
318         NTSTATUS ret;
319
320         ret = auth_register(&winbind_ops);
321         if (!NT_STATUS_IS_OK(ret)) {
322                 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
323                 return ret;
324         }
325
326         ret = auth_register(&winbind_rodc_ops);
327         if (!NT_STATUS_IS_OK(ret)) {
328                 DEBUG(0,("Failed to register 'winbind_rodc' auth backend!\n"));
329                 return ret;
330         }
331
332         ret = auth_register(&winbind_wbclient_ops);
333         if (!NT_STATUS_IS_OK(ret)) {
334                 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
335                 return ret;
336         }
337
338         return NT_STATUS_OK;
339 }