dfb8fce2a6b10bdc9c3be90ff81446f9fe6ee31b
[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 "auth/auth.h"
26 #include "auth/ntlm/auth_proto.h"
27 #include "auth/auth_sam_reply.h"
28 #include "librpc/gen_ndr/ndr_winbind_c.h"
29 #include "lib/messaging/irpc.h"
30 #include "param/param.h"
31 #include "nsswitch/libwbclient/wbclient.h"
32 #include "libcli/security/security.h"
33
34 _PUBLIC_ NTSTATUS auth_winbind_init(void);
35
36 static NTSTATUS get_info3_from_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
37                                                struct wbcAuthUserInfo *info,
38                                                struct netr_SamInfo3 *info3)
39 {
40         int i, j;
41         struct samr_RidWithAttribute *rids = NULL;
42         struct dom_sid *user_sid;
43         struct dom_sid *group_sid;
44
45         user_sid = (struct dom_sid *)(void *)&info->sids[0].sid;
46         group_sid = (struct dom_sid *)(void *)&info->sids[1].sid;
47
48         info3->base.last_logon = info->logon_time;
49         info3->base.last_logoff = info->logoff_time;
50         info3->base.acct_expiry = info->kickoff_time;
51         info3->base.last_password_change = info->pass_last_set_time;
52         info3->base.allow_password_change = info->pass_can_change_time;
53         info3->base.force_password_change = info->pass_must_change_time;
54
55         info3->base.account_name.string = talloc_strdup(mem_ctx,
56                                                         info->account_name);
57         info3->base.full_name.string = talloc_strdup(mem_ctx,
58                                                      info->full_name);
59         info3->base.logon_script.string = talloc_strdup(mem_ctx,
60                                                         info->logon_script);
61         info3->base.profile_path.string = talloc_strdup(mem_ctx,
62                                                         info->profile_path);
63         info3->base.home_directory.string = talloc_strdup(mem_ctx,
64                                                           info->home_directory);
65         info3->base.home_drive.string = talloc_strdup(mem_ctx,
66                                                       info->home_drive);
67         info3->base.logon_server.string = talloc_strdup(mem_ctx,
68                                                         info->logon_server);
69         info3->base.domain.string = talloc_strdup(mem_ctx,
70                                                   info->domain_name);
71
72         info3->base.logon_count = info->logon_count;
73         info3->base.bad_password_count = info->bad_password_count;
74         info3->base.user_flags = info->user_flags;
75         memcpy(info3->base.key.key, info->user_session_key,
76                sizeof(info3->base.key.key));
77         memcpy(info3->base.LMSessKey.key, info->lm_session_key,
78                sizeof(info3->base.LMSessKey.key));
79         info3->base.acct_flags = info->acct_flags;
80         memset(info3->base.unknown, 0, sizeof(info3->base.unknown));
81
82         if (info->num_sids < 2) {
83                 return NT_STATUS_INVALID_PARAMETER;
84         }
85
86         dom_sid_split_rid(mem_ctx, user_sid,
87                           &info3->base.domain_sid,
88                           &info3->base.rid);
89         dom_sid_split_rid(mem_ctx, group_sid, NULL,
90                           &info3->base.primary_gid);
91
92         /* We already handled the first two, now take care of the rest */
93         info3->base.groups.count = info->num_sids - 2;
94
95         rids = talloc_array(mem_ctx, struct samr_RidWithAttribute,
96                             info3->base.groups.count);
97         NT_STATUS_HAVE_NO_MEMORY(rids);
98
99         for (i = 2, j = 0; i < info->num_sids; ++i, ++j) {
100                 struct dom_sid *tmp_sid;
101                 tmp_sid = (struct dom_sid *)(void *)&info->sids[1].sid;
102
103                 rids[j].attributes = info->sids[i].attributes;
104                 dom_sid_split_rid(mem_ctx, tmp_sid,
105                                   NULL, &rids[j].rid);
106         }
107         info3->base.groups.rids = rids;
108
109         return NT_STATUS_OK;
110 }
111
112
113 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
114                                    TALLOC_CTX *mem_ctx,
115                                    const struct auth_usersupplied_info *user_info)
116 {
117         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
118                 return NT_STATUS_NOT_IMPLEMENTED;
119         }
120
121         /* TODO: maybe limit the user scope to remote users only */
122         return NT_STATUS_OK;
123 }
124
125 struct winbind_check_password_state {
126         struct winbind_SamLogon req;
127 };
128
129 /*
130  Authenticate a user with a challenge/response
131  using IRPC to the winbind task
132 */
133 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
134                                        TALLOC_CTX *mem_ctx,
135                                        const struct auth_usersupplied_info *user_info, 
136                                        struct auth_user_info_dc **user_info_dc)
137 {
138         NTSTATUS status;
139         struct dcerpc_binding_handle *irpc_handle;
140         struct winbind_check_password_state *s;
141         const struct auth_usersupplied_info *user_info_new;
142         struct netr_IdentityInfo *identity_info;
143
144         if (!ctx->auth_ctx->msg_ctx) {
145                 DEBUG(0,("winbind_check_password: auth_context_create was called with out messaging context\n"));
146                 return NT_STATUS_INTERNAL_ERROR;
147         }
148
149         s = talloc(mem_ctx, struct winbind_check_password_state);
150         NT_STATUS_HAVE_NO_MEMORY(s);
151
152         irpc_handle = irpc_binding_handle_by_name(s, ctx->auth_ctx->msg_ctx,
153                                                   "winbind_server",
154                                                   &ndr_table_winbind);
155         if (irpc_handle == NULL) {
156                 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, " 
157                           "no winbind_server running!\n",
158                           user_info->client.domain_name, user_info->client.account_name));
159                 return NT_STATUS_NO_LOGON_SERVERS;
160         }
161
162         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
163                 struct netr_PasswordInfo *password_info;
164
165                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
166                                            user_info, &user_info_new);
167                 NT_STATUS_NOT_OK_RETURN(status);
168                 user_info = user_info_new;
169
170                 password_info = talloc(s, struct netr_PasswordInfo);
171                 NT_STATUS_HAVE_NO_MEMORY(password_info);
172
173                 password_info->lmpassword = *user_info->password.hash.lanman;
174                 password_info->ntpassword = *user_info->password.hash.nt;
175
176                 identity_info = &password_info->identity_info;
177                 s->req.in.logon_level   = 1;
178                 s->req.in.logon.password= password_info;
179         } else {
180                 struct netr_NetworkInfo *network_info;
181                 uint8_t chal[8];
182
183                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
184                                            user_info, &user_info_new);
185                 NT_STATUS_NOT_OK_RETURN(status);
186                 user_info = user_info_new;
187
188                 network_info = talloc(s, struct netr_NetworkInfo);
189                 NT_STATUS_HAVE_NO_MEMORY(network_info);
190
191                 status = auth_get_challenge(ctx->auth_ctx, chal);
192                 NT_STATUS_NOT_OK_RETURN(status);
193
194                 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
195
196                 network_info->nt.length = user_info->password.response.nt.length;
197                 network_info->nt.data   = user_info->password.response.nt.data;
198
199                 network_info->lm.length = user_info->password.response.lanman.length;
200                 network_info->lm.data   = user_info->password.response.lanman.data;
201
202                 identity_info = &network_info->identity_info;
203                 s->req.in.logon_level   = 2;
204                 s->req.in.logon.network = network_info;
205         }
206
207         identity_info->domain_name.string       = user_info->client.domain_name;
208         identity_info->parameter_control        = user_info->logon_parameters; /* see MSV1_0_* */
209         identity_info->logon_id_low             = 0;
210         identity_info->logon_id_high            = 0;
211         identity_info->account_name.string      = user_info->client.account_name;
212         identity_info->workstation.string       = user_info->workstation_name;
213
214         s->req.in.validation_level      = 3;
215
216         status = dcerpc_winbind_SamLogon_r(irpc_handle, s, &s->req);
217         NT_STATUS_NOT_OK_RETURN(status);
218
219         status = make_user_info_dc_netlogon_validation(mem_ctx,
220                                                       user_info->client.account_name,
221                                                       s->req.in.validation_level,
222                                                       &s->req.out.validation,
223                                                       user_info_dc);
224         NT_STATUS_NOT_OK_RETURN(status);
225
226         return NT_STATUS_OK;
227 }
228
229 /*
230  Authenticate a user with a challenge/response
231  using the samba3 winbind protocol via libwbclient
232 */
233 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
234                                                 TALLOC_CTX *mem_ctx,
235                                                 const struct auth_usersupplied_info *user_info,
236                                                 struct auth_user_info_dc **user_info_dc)
237 {
238         struct wbcAuthUserParams params;
239         struct wbcAuthUserInfo *info = NULL;
240         struct wbcAuthErrorInfo *err = NULL;
241         wbcErr wbc_status;
242         NTSTATUS nt_status;
243         struct netr_SamInfo3 info3;
244         union netr_Validation validation;
245
246
247         /* Send off request */
248         const struct auth_usersupplied_info *user_info_temp;
249         nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
250                                       AUTH_PASSWORD_RESPONSE,
251                                       user_info, &user_info_temp);
252         if (!NT_STATUS_IS_OK(nt_status)) {
253                 return nt_status;
254         }
255         user_info = user_info_temp;
256
257         ZERO_STRUCT(params);
258         ZERO_STRUCT(info3);
259         /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
260
261         params.parameter_control = user_info->logon_parameters;
262         params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
263                                     WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
264         params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
265
266         params.account_name     = user_info->client.account_name;
267         params.domain_name      = user_info->client.domain_name;
268         params.workstation_name = user_info->workstation_name;
269
270         d_fprintf(stderr, "looking up %s@%s logging in from %s\n",
271                   params.account_name, params.domain_name,
272                   params.workstation_name);
273
274         memcpy(params.password.response.challenge,
275                ctx->auth_ctx->challenge.data.data,
276                sizeof(params.password.response.challenge));
277
278         params.password.response.lm_length =
279                 user_info->password.response.lanman.length;
280         params.password.response.nt_length =
281                 user_info->password.response.nt.length;
282
283         params.password.response.lm_data =
284                 user_info->password.response.lanman.data;
285         params.password.response.nt_data =
286                 user_info->password.response.nt.data;
287
288         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
289         if (wbc_status == WBC_ERR_AUTH_ERROR) {
290                 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
291                       err->nt_string, err->nt_status, err->display_string));
292
293                 nt_status = NT_STATUS(err->nt_status);
294                 wbcFreeMemory(err);
295                 NT_STATUS_NOT_OK_RETURN(nt_status);
296         } else if (!WBC_ERROR_IS_OK(wbc_status)) {
297                 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
298                         wbc_status, wbcErrorString(wbc_status)));
299                 return NT_STATUS_LOGON_FAILURE;
300         }
301         nt_status = get_info3_from_wbcAuthUserInfo(mem_ctx, info, &info3);
302         wbcFreeMemory(info);
303         NT_STATUS_NOT_OK_RETURN(nt_status);
304
305         validation.sam3 = &info3;
306         nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
307                                         user_info->client.account_name,
308                                         3, &validation, user_info_dc);
309         return nt_status;
310
311 }
312
313 static const struct auth_operations winbind_ops = {
314         .name           = "winbind",
315         .get_challenge  = auth_get_challenge_not_implemented,
316         .want_check     = winbind_want_check,
317         .check_password = winbind_check_password
318 };
319
320 static const struct auth_operations winbind_wbclient_ops = {
321         .name           = "winbind_wbclient",
322         .get_challenge  = auth_get_challenge_not_implemented,
323         .want_check     = winbind_want_check,
324         .check_password = winbind_check_password_wbclient
325 };
326
327 _PUBLIC_ NTSTATUS auth_winbind_init(void)
328 {
329         NTSTATUS ret;
330
331         ret = auth_register(&winbind_ops);
332         if (!NT_STATUS_IS_OK(ret)) {
333                 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
334                 return ret;
335         }
336
337         ret = auth_register(&winbind_wbclient_ops);
338         if (!NT_STATUS_IS_OK(ret)) {
339                 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
340                 return ret;
341         }
342
343         return NT_STATUS_OK;
344 }