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