a3efde8b99e4f7d40f27db2146ac5feedafdab8d
[samba.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 <tevent.h>
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "auth/auth.h"
28 #include "auth/ntlm/auth_proto.h"
29 #include "librpc/gen_ndr/ndr_winbind_c.h"
30 #include "lib/messaging/irpc.h"
31 #include "param/param.h"
32 #include "nsswitch/libwbclient/wbclient.h"
33 #include "auth/auth_sam_reply.h"
34 #include "libcli/security/security.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "auth/auth_sam.h"
37
38 _PUBLIC_ NTSTATUS auth4_winbind_init(TALLOC_CTX *);
39
40 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
41                                    TALLOC_CTX *mem_ctx,
42                                    const struct auth_usersupplied_info *user_info)
43 {
44         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
45                 return NT_STATUS_NOT_IMPLEMENTED;
46         }
47
48         /* TODO: maybe limit the user scope to remote users only */
49         return NT_STATUS_OK;
50 }
51
52 struct winbind_check_password_state {
53         struct auth_method_context *ctx;
54         const struct auth_usersupplied_info *user_info;
55         struct winbind_SamLogon req;
56         struct auth_user_info_dc *user_info_dc;
57         bool authoritative;
58 };
59
60 static void winbind_check_password_done(struct tevent_req *subreq);
61
62 /*
63  Authenticate a user with a challenge/response
64  using IRPC to the winbind task
65 */
66 static struct tevent_req *winbind_check_password_send(TALLOC_CTX *mem_ctx,
67                                 struct tevent_context *ev,
68                                 struct auth_method_context *ctx,
69                                 const struct auth_usersupplied_info *user_info)
70 {
71         struct tevent_req *req = NULL;
72         struct winbind_check_password_state *state = NULL;
73         NTSTATUS status;
74         struct dcerpc_binding_handle *irpc_handle;
75         const struct auth_usersupplied_info *user_info_new;
76         struct netr_IdentityInfo *identity_info;
77         struct imessaging_context *msg_ctx;
78         struct tevent_req *subreq = NULL;
79
80         req = tevent_req_create(mem_ctx, &state,
81                                 struct winbind_check_password_state);
82         if (req == NULL) {
83                 return NULL;
84         }
85         state->ctx = ctx;
86         state->user_info = user_info;
87         state->authoritative = true;
88
89         msg_ctx = imessaging_client_init(state, ctx->auth_ctx->lp_ctx, ev);
90         if (msg_ctx == NULL) {
91                 DEBUG(1, ("imessaging_init failed\n"));
92                 tevent_req_nterror(req, NT_STATUS_INVALID_SERVER_STATE);
93                 return tevent_req_post(req, ev);
94         }
95
96         irpc_handle = irpc_binding_handle_by_name(state, msg_ctx,
97                                                   "winbind_server",
98                                                   &ndr_table_winbind);
99         if (irpc_handle == NULL) {
100                 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, " 
101                           "no winbind_server running!\n",
102                           user_info->client.domain_name, user_info->client.account_name));
103                 tevent_req_nterror(req, NT_STATUS_NO_LOGON_SERVERS);
104                 return tevent_req_post(req, ev);
105         }
106
107         /*
108          * 120 seconds should be enough even for trusted domains.
109          *
110          * Currently winbindd has a much lower limit.
111          * And tests with Windows RODCs show that it
112          * returns NO_LOGON_SERVERS after 90-100 seconds
113          * if it can't reach any RWDC.
114          */
115         dcerpc_binding_handle_set_timeout(irpc_handle, 120);
116
117         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
118                 struct netr_PasswordInfo *password_info;
119
120                 status = encrypt_user_info(state, ctx->auth_ctx, AUTH_PASSWORD_HASH,
121                                            user_info, &user_info_new);
122                 if (tevent_req_nterror(req, status)) {
123                         return tevent_req_post(req, ev);
124                 }
125                 user_info = user_info_new;
126
127                 password_info = talloc_zero(state, struct netr_PasswordInfo);
128                 if (tevent_req_nomem(password_info, req)) {
129                         return tevent_req_post(req, ev);
130                 }
131
132                 password_info->lmpassword = *user_info->password.hash.lanman;
133                 password_info->ntpassword = *user_info->password.hash.nt;
134
135                 identity_info = &password_info->identity_info;
136                 state->req.in.logon_level       = 1;
137                 state->req.in.logon.password= password_info;
138         } else {
139                 struct netr_NetworkInfo *network_info;
140                 uint8_t chal[8];
141
142                 status = encrypt_user_info(state, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
143                                            user_info, &user_info_new);
144                 if (tevent_req_nterror(req, status)) {
145                         return tevent_req_post(req, ev);
146                 }
147                 user_info = user_info_new;
148
149                 network_info = talloc_zero(state, struct netr_NetworkInfo);
150                 if (tevent_req_nomem(network_info, req)) {
151                         return tevent_req_post(req, ev);
152                 }
153
154                 status = auth_get_challenge(ctx->auth_ctx, chal);
155                 if (tevent_req_nterror(req, status)) {
156                         return tevent_req_post(req, ev);
157                 }
158
159                 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
160
161                 network_info->nt.length = user_info->password.response.nt.length;
162                 network_info->nt.data   = user_info->password.response.nt.data;
163
164                 network_info->lm.length = user_info->password.response.lanman.length;
165                 network_info->lm.data   = user_info->password.response.lanman.data;
166
167                 identity_info = &network_info->identity_info;
168                 state->req.in.logon_level       = 2;
169                 state->req.in.logon.network = network_info;
170         }
171
172         identity_info->domain_name.string       = user_info->client.domain_name;
173         identity_info->parameter_control        = user_info->logon_parameters; /* see MSV1_0_* */
174         identity_info->logon_id_low             = 0;
175         identity_info->logon_id_high            = 0;
176         identity_info->account_name.string      = user_info->client.account_name;
177         identity_info->workstation.string       = user_info->workstation_name;
178
179         state->req.in.validation_level = 6;
180
181         subreq = dcerpc_winbind_SamLogon_r_send(state, ev, irpc_handle,
182                                                 &state->req);
183         if (tevent_req_nomem(subreq, req)) {
184                 return tevent_req_post(req, ev);
185         }
186         tevent_req_set_callback(subreq,
187                                 winbind_check_password_done,
188                                 req);
189
190         return req;
191 }
192
193 static void winbind_check_password_done(struct tevent_req *subreq)
194 {
195         struct tevent_req *req =
196                 tevent_req_callback_data(subreq,
197                 struct tevent_req);
198         struct winbind_check_password_state *state =
199                 tevent_req_data(req,
200                 struct winbind_check_password_state);
201         struct auth_method_context *ctx = state->ctx;
202         const struct auth_usersupplied_info *user_info = state->user_info;
203         struct ldb_dn *domain_dn = NULL;
204         const char *nt4_domain = NULL;
205         const char *nt4_account = NULL;
206         struct ldb_message *msg = NULL;
207         NTSTATUS status;
208
209         status = dcerpc_winbind_SamLogon_r_recv(subreq, state);
210         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
211                 status = NT_STATUS_NO_LOGON_SERVERS;
212         }
213         TALLOC_FREE(subreq);
214         if (tevent_req_nterror(req, status)) {
215                 return;
216         }
217
218         status = state->req.out.result;
219         if (!NT_STATUS_IS_OK(status)) {
220                 if (!state->req.out.authoritative) {
221                         state->authoritative = false;
222                 }
223                 tevent_req_nterror(req, status);
224                 return;
225         }
226
227         status = make_user_info_dc_netlogon_validation(state,
228                                                       user_info->client.account_name,
229                                                       state->req.in.validation_level,
230                                                       &state->req.out.validation,
231                                                       true, /* This user was authenticated */
232                                                       &state->user_info_dc);
233         if (tevent_req_nterror(req, status)) {
234                 return;
235         }
236
237         nt4_domain = state->user_info_dc->info->domain_name;
238         nt4_account = state->user_info_dc->info->account_name;
239
240         if (lpcfg_is_mydomain(ctx->auth_ctx->lp_ctx, nt4_domain)) {
241                 domain_dn = ldb_get_default_basedn(ctx->auth_ctx->sam_ctx);
242         }
243
244         if (domain_dn != NULL) {
245                 /*
246                  * At best, reset the badPwdCount to 0 if the account exists.
247                  * This means that lockouts happen at a badPwdCount earlier than
248                  * normal, but makes it more fault tolerant.
249                  */
250                 status = authsam_search_account(state, ctx->auth_ctx->sam_ctx,
251                                                 nt4_account, domain_dn, &msg);
252                 if (NT_STATUS_IS_OK(status)) {
253                         authsam_logon_success_accounting(
254                                 ctx->auth_ctx->sam_ctx, msg,
255                                 domain_dn,
256                                 user_info->flags & USER_INFO_INTERACTIVE_LOGON,
257                                 NULL);
258                 }
259         }
260
261         /*
262          * We need to expand group memberships within our local domain,
263          * as the token might be generated by a trusted domain, unless we're
264          * an RODC.
265          */
266         status = authsam_update_user_info_dc(state->user_info_dc,
267                                              ctx->auth_ctx->sam_ctx,
268                                              state->user_info_dc);
269         if (tevent_req_nterror(req, status)) {
270                 return;
271         }
272
273         tevent_req_done(req);
274 }
275
276 static NTSTATUS winbind_check_password_recv(struct tevent_req *req,
277                                             TALLOC_CTX *mem_ctx,
278                                             struct auth_user_info_dc **user_info_dc,
279                                             bool *pauthoritative)
280 {
281         struct winbind_check_password_state *state =
282                 tevent_req_data(req,
283                 struct winbind_check_password_state);
284         NTSTATUS status = NT_STATUS_OK;
285
286         *pauthoritative = state->authoritative;
287
288         if (tevent_req_is_nterror(req, &status)) {
289                 tevent_req_received(req);
290                 return status;
291         }
292
293         *user_info_dc = talloc_move(mem_ctx, &state->user_info_dc);
294
295         tevent_req_received(req);
296         return NT_STATUS_OK;
297 }
298
299 static const struct auth_operations winbind_ops = {
300         .name                   = "winbind",
301         .want_check             = winbind_want_check,
302         .check_password_send    = winbind_check_password_send,
303         .check_password_recv    = winbind_check_password_recv
304 };
305
306 _PUBLIC_ NTSTATUS auth4_winbind_init(TALLOC_CTX *ctx)
307 {
308         NTSTATUS ret;
309
310         ret = auth_register(ctx, &winbind_ops);
311         if (!NT_STATUS_IS_OK(ret)) {
312                 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
313                 return ret;
314         }
315
316         return NT_STATUS_OK;
317 }