7c815fc020e2c7a77c9597d47ab76529cf2fc6a7
[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(TALLOC_CTX *);
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                                        bool *authoritative)
104 {
105         NTSTATUS status;
106         struct dcerpc_binding_handle *irpc_handle;
107         struct winbind_check_password_state *s;
108         const struct auth_usersupplied_info *user_info_new;
109         struct netr_IdentityInfo *identity_info;
110
111         if (!ctx->auth_ctx->msg_ctx) {
112                 DEBUG(0,("winbind_check_password: auth_context_create was called with out messaging context\n"));
113                 return NT_STATUS_INTERNAL_ERROR;
114         }
115
116         s = talloc(mem_ctx, struct winbind_check_password_state);
117         NT_STATUS_HAVE_NO_MEMORY(s);
118
119         irpc_handle = irpc_binding_handle_by_name(s, ctx->auth_ctx->msg_ctx,
120                                                   "winbind_server",
121                                                   &ndr_table_winbind);
122         if (irpc_handle == NULL) {
123                 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, " 
124                           "no winbind_server running!\n",
125                           user_info->client.domain_name, user_info->client.account_name));
126                 return NT_STATUS_NO_LOGON_SERVERS;
127         }
128
129         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
130                 struct netr_PasswordInfo *password_info;
131
132                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
133                                            user_info, &user_info_new);
134                 NT_STATUS_NOT_OK_RETURN(status);
135                 user_info = user_info_new;
136
137                 password_info = talloc(s, struct netr_PasswordInfo);
138                 NT_STATUS_HAVE_NO_MEMORY(password_info);
139
140                 password_info->lmpassword = *user_info->password.hash.lanman;
141                 password_info->ntpassword = *user_info->password.hash.nt;
142
143                 identity_info = &password_info->identity_info;
144                 s->req.in.logon_level   = 1;
145                 s->req.in.logon.password= password_info;
146         } else {
147                 struct netr_NetworkInfo *network_info;
148                 uint8_t chal[8];
149
150                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
151                                            user_info, &user_info_new);
152                 NT_STATUS_NOT_OK_RETURN(status);
153                 user_info = user_info_new;
154
155                 network_info = talloc(s, struct netr_NetworkInfo);
156                 NT_STATUS_HAVE_NO_MEMORY(network_info);
157
158                 status = auth_get_challenge(ctx->auth_ctx, chal);
159                 NT_STATUS_NOT_OK_RETURN(status);
160
161                 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
162
163                 network_info->nt.length = user_info->password.response.nt.length;
164                 network_info->nt.data   = user_info->password.response.nt.data;
165
166                 network_info->lm.length = user_info->password.response.lanman.length;
167                 network_info->lm.data   = user_info->password.response.lanman.data;
168
169                 identity_info = &network_info->identity_info;
170                 s->req.in.logon_level   = 2;
171                 s->req.in.logon.network = network_info;
172         }
173
174         identity_info->domain_name.string       = user_info->client.domain_name;
175         identity_info->parameter_control        = user_info->logon_parameters; /* see MSV1_0_* */
176         identity_info->logon_id_low             = 0;
177         identity_info->logon_id_high            = 0;
178         identity_info->account_name.string      = user_info->client.account_name;
179         identity_info->workstation.string       = user_info->workstation_name;
180
181         s->req.in.validation_level      = 3;
182
183         /* Note: this makes use of nested event loops... */
184         dcerpc_binding_handle_set_sync_ev(irpc_handle, ctx->auth_ctx->event_ctx);
185         status = dcerpc_winbind_SamLogon_r(irpc_handle, s, &s->req);
186         NT_STATUS_NOT_OK_RETURN(status);
187
188         if (NT_STATUS_EQUAL(s->req.out.result, NT_STATUS_NO_SUCH_USER) &&
189             !s->req.out.authoritative) {
190                 return NT_STATUS_NOT_IMPLEMENTED;
191         }
192
193         status = make_user_info_dc_netlogon_validation(mem_ctx,
194                                                       user_info->client.account_name,
195                                                       s->req.in.validation_level,
196                                                       &s->req.out.validation,
197                                                        true, /* This user was authenticated */
198                                                       user_info_dc);
199         NT_STATUS_NOT_OK_RETURN(status);
200
201         return NT_STATUS_OK;
202 }
203
204 /*
205  Authenticate a user with a challenge/response
206  using the samba3 winbind protocol via libwbclient
207 */
208 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
209                                                 TALLOC_CTX *mem_ctx,
210                                                 const struct auth_usersupplied_info *user_info,
211                                                 struct auth_user_info_dc **user_info_dc,
212                                                 bool *authoritative)
213 {
214         struct wbcAuthUserParams params;
215         struct wbcAuthUserInfo *info = NULL;
216         struct wbcAuthErrorInfo *err = NULL;
217         wbcErr wbc_status;
218         NTSTATUS nt_status;
219         struct netr_SamInfo6 *info6 = NULL;
220         union netr_Validation validation;
221
222         /* Send off request */
223         const struct auth_usersupplied_info *user_info_temp;
224         nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
225                                       AUTH_PASSWORD_RESPONSE,
226                                       user_info, &user_info_temp);
227         if (!NT_STATUS_IS_OK(nt_status)) {
228                 return nt_status;
229         }
230         user_info = user_info_temp;
231
232         ZERO_STRUCT(params);
233         ZERO_STRUCT(validation);
234         /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
235
236         params.parameter_control = user_info->logon_parameters;
237         params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
238                                     WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
239         params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
240
241         params.account_name     = user_info->client.account_name;
242         params.domain_name      = user_info->client.domain_name;
243         params.workstation_name = user_info->workstation_name;
244
245         DEBUG(5,("looking up %s@%s logging in from %s\n",
246                   params.account_name, params.domain_name,
247                   params.workstation_name));
248
249         memcpy(params.password.response.challenge,
250                ctx->auth_ctx->challenge.data.data,
251                sizeof(params.password.response.challenge));
252
253         params.password.response.lm_length =
254                 user_info->password.response.lanman.length;
255         params.password.response.nt_length =
256                 user_info->password.response.nt.length;
257
258         params.password.response.lm_data =
259                 user_info->password.response.lanman.data;
260         params.password.response.nt_data =
261                 user_info->password.response.nt.data;
262
263         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
264         if (wbc_status == WBC_ERR_AUTH_ERROR) {
265                 if (err) {
266                         DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
267                               err->nt_string, err->nt_status, err->display_string));
268                         nt_status = NT_STATUS(err->nt_status);
269                         wbcFreeMemory(err);
270                 } else {
271                         nt_status = NT_STATUS_LOGON_FAILURE;
272                 }
273                 NT_STATUS_NOT_OK_RETURN(nt_status);
274         } else if (!WBC_ERROR_IS_OK(wbc_status)) {
275                 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
276                         wbc_status, wbcErrorString(wbc_status)));
277                 if (err) {
278                         DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
279                               err->nt_string, err->nt_status, err->display_string));
280                 }
281                 return NT_STATUS_LOGON_FAILURE;
282         }
283         info6 = wbcAuthUserInfo_to_netr_SamInfo6(mem_ctx, info);
284         wbcFreeMemory(info);
285         if (!info6) {
286                 DEBUG(1, ("wbcAuthUserInfo_to_netr_SamInfo6 failed\n"));
287                 return NT_STATUS_NO_MEMORY;
288         }
289
290         validation.sam6 = info6;
291         nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
292                                                           user_info->client.account_name,
293                                                           6, &validation,
294                                                           true, /* This user was authenticated */
295                                                           user_info_dc);
296         return nt_status;
297
298 }
299
300 static const struct auth_operations winbind_ops = {
301         .name           = "winbind",
302         .want_check     = winbind_want_check,
303         .check_password = winbind_check_password
304 };
305
306 static const struct auth_operations winbind_rodc_ops = {
307         .name           = "winbind_rodc",
308         .want_check     = winbind_rodc_want_check,
309         .check_password = winbind_check_password
310 };
311
312 static const struct auth_operations winbind_wbclient_ops = {
313         .name           = "winbind_wbclient",
314         .want_check     = winbind_want_check,
315         .check_password = winbind_check_password_wbclient
316 };
317
318 _PUBLIC_ NTSTATUS auth4_winbind_init(TALLOC_CTX *ctx)
319 {
320         NTSTATUS ret;
321
322         ret = auth_register(ctx, &winbind_ops);
323         if (!NT_STATUS_IS_OK(ret)) {
324                 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
325                 return ret;
326         }
327
328         ret = auth_register(ctx, &winbind_rodc_ops);
329         if (!NT_STATUS_IS_OK(ret)) {
330                 DEBUG(0,("Failed to register 'winbind_rodc' auth backend!\n"));
331                 return ret;
332         }
333
334         ret = auth_register(ctx, &winbind_wbclient_ops);
335         if (!NT_STATUS_IS_OK(ret)) {
336                 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
337                 return ret;
338         }
339
340         return NT_STATUS_OK;
341 }