Finish removal of iconv_convenience in public API's.
[mat/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 "nsswitch/winbind_client.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30 #include "librpc/gen_ndr/ndr_winbind.h"
31 #include "lib/messaging/irpc.h"
32 #include "param/param.h"
33 #include "nsswitch/libwbclient/wbclient.h"
34 #include "libcli/security/dom_sid.h"
35
36 static NTSTATUS get_info3_from_ndr(TALLOC_CTX *mem_ctx, struct winbindd_response *response, struct netr_SamInfo3 *info3)
37 {
38         size_t len = response->length - sizeof(struct winbindd_response);
39         if (len > 4) {
40                 enum ndr_err_code ndr_err;
41                 DATA_BLOB blob;
42                 blob.length = len - 4;
43                 blob.data = (uint8_t *)(((char *)response->extra_data.data) + 4);
44
45                 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, 
46                                info3,
47                               (ndr_pull_flags_fn_t)ndr_pull_netr_SamInfo3);
48                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
49                         return ndr_map_error2ntstatus(ndr_err);
50                 }
51
52                 return NT_STATUS_OK;
53         } else {
54                 DEBUG(2, ("get_info3_from_ndr: No info3 struct found!\n"));
55                 return NT_STATUS_UNSUCCESSFUL;
56         }
57 }
58
59 static NTSTATUS get_info3_from_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
60                                                struct wbcAuthUserInfo *info,
61                                                struct netr_SamInfo3 *info3)
62 {
63         int i, j;
64         struct samr_RidWithAttribute *rids = NULL;
65
66         info3->base.last_logon = info->logon_time;
67         info3->base.last_logoff = info->logoff_time;
68         info3->base.acct_expiry = info->kickoff_time;
69         info3->base.last_password_change = info->pass_last_set_time;
70         info3->base.allow_password_change = info->pass_can_change_time;
71         info3->base.force_password_change = info->pass_must_change_time;
72
73         info3->base.account_name.string = talloc_strdup(mem_ctx,
74                                                         info->account_name);
75         info3->base.full_name.string = talloc_strdup(mem_ctx,
76                                                      info->full_name);
77         info3->base.logon_script.string = talloc_strdup(mem_ctx,
78                                                         info->logon_script);
79         info3->base.profile_path.string = talloc_strdup(mem_ctx,
80                                                         info->profile_path);
81         info3->base.home_directory.string = talloc_strdup(mem_ctx,
82                                                           info->home_directory);
83         info3->base.home_drive.string = talloc_strdup(mem_ctx,
84                                                       info->home_drive);
85         info3->base.logon_server.string = talloc_strdup(mem_ctx,
86                                                         info->logon_server);
87         info3->base.domain.string = talloc_strdup(mem_ctx,
88                                                   info->domain_name);
89
90         info3->base.logon_count = info->logon_count;
91         info3->base.bad_password_count = info->bad_password_count;
92         info3->base.user_flags = info->user_flags;
93         memcpy(info3->base.key.key, info->user_session_key,
94                sizeof(info3->base.key.key));
95         memcpy(info3->base.LMSessKey.key, info->lm_session_key,
96                sizeof(info3->base.LMSessKey.key));
97         info3->base.acct_flags = info->acct_flags;
98         memset(info3->base.unknown, 0, sizeof(info3->base.unknown));
99
100         if (info->num_sids < 2) {
101                 return NT_STATUS_INVALID_PARAMETER;
102         }
103
104         dom_sid_split_rid(mem_ctx, (struct dom_sid2 *) &info->sids[0].sid,
105                           &info3->base.domain_sid,
106                           &info3->base.rid);
107         dom_sid_split_rid(mem_ctx, (struct dom_sid2 *) &info->sids[1].sid, NULL,
108                           &info3->base.primary_gid);
109
110         /* We already handled the first two, now take care of the rest */
111         info3->base.groups.count = info->num_sids - 2;
112
113         rids = talloc_array(mem_ctx, struct samr_RidWithAttribute,
114                             info3->base.groups.count);
115         NT_STATUS_HAVE_NO_MEMORY(rids);
116
117         for (i = 2, j = 0; i < info->num_sids; ++i, ++j) {
118                 rids[j].attributes = info->sids[i].attributes;
119                 dom_sid_split_rid(mem_ctx,
120                                   (struct dom_sid2 *) &info->sids[i].sid,
121                                   NULL, &rids[j].rid);
122         }
123         info3->base.groups.rids = rids;
124
125         return NT_STATUS_OK;
126 }
127
128
129 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
130                                    TALLOC_CTX *mem_ctx,
131                                    const struct auth_usersupplied_info *user_info)
132 {
133         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
134                 return NT_STATUS_NOT_IMPLEMENTED;
135         }
136
137         /* TODO: maybe limit the user scope to remote users only */
138         return NT_STATUS_OK;
139 }
140
141 /*
142  Authenticate a user with a challenge/response
143  using the samba3 winbind protocol
144 */
145 static NTSTATUS winbind_check_password_samba3(struct auth_method_context *ctx,
146                                               TALLOC_CTX *mem_ctx,
147                                               const struct auth_usersupplied_info *user_info, 
148                                               struct auth_serversupplied_info **server_info)
149 {
150         struct winbindd_request request;
151         struct winbindd_response response;
152         NSS_STATUS result;
153         NTSTATUS nt_status;
154         struct netr_SamInfo3 info3;             
155
156         /* Send off request */
157         const struct auth_usersupplied_info *user_info_temp;    
158         nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx, 
159                                       AUTH_PASSWORD_RESPONSE, 
160                                       user_info, &user_info_temp);
161         if (!NT_STATUS_IS_OK(nt_status)) {
162                 return nt_status;
163         }
164         user_info = user_info_temp;
165
166         ZERO_STRUCT(request);
167         ZERO_STRUCT(response);
168         request.flags = WBFLAG_PAM_INFO3_NDR;
169
170         request.data.auth_crap.logon_parameters = user_info->logon_parameters;
171
172         safe_strcpy(request.data.auth_crap.user,
173                        user_info->client.account_name, sizeof(fstring));
174         safe_strcpy(request.data.auth_crap.domain,
175                        user_info->client.domain_name, sizeof(fstring));
176         safe_strcpy(request.data.auth_crap.workstation,
177                        user_info->workstation_name, sizeof(fstring));
178
179         memcpy(request.data.auth_crap.chal, ctx->auth_ctx->challenge.data.data, sizeof(request.data.auth_crap.chal));
180
181         request.data.auth_crap.lm_resp_len = MIN(user_info->password.response.lanman.length,
182                                                  sizeof(request.data.auth_crap.lm_resp));
183         request.data.auth_crap.nt_resp_len = MIN(user_info->password.response.nt.length, 
184                                                  sizeof(request.data.auth_crap.nt_resp));
185
186         memcpy(request.data.auth_crap.lm_resp, user_info->password.response.lanman.data,
187                request.data.auth_crap.lm_resp_len);
188         memcpy(request.data.auth_crap.nt_resp, user_info->password.response.nt.data,
189                request.data.auth_crap.nt_resp_len);
190
191         result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
192
193         nt_status = NT_STATUS(response.data.auth.nt_status);
194         NT_STATUS_NOT_OK_RETURN(nt_status);
195
196         if (result == NSS_STATUS_SUCCESS && response.extra_data.data) {
197                 union netr_Validation validation;
198
199                 nt_status = get_info3_from_ndr(mem_ctx, &response, &info3);
200                 SAFE_FREE(response.extra_data.data);
201                 NT_STATUS_NOT_OK_RETURN(nt_status); 
202
203                 validation.sam3 = &info3;
204                 nt_status = make_server_info_netlogon_validation(mem_ctx, 
205                                                                  user_info->client.account_name, 
206                                                                  3, &validation,
207                                                                  server_info);
208                 return nt_status;
209         } else if (result == NSS_STATUS_SUCCESS && !response.extra_data.data) {
210                 DEBUG(0, ("Winbindd authenticated the user [%s]\\[%s], "
211                           "but did not include the required info3 reply!\n", 
212                           user_info->client.domain_name, user_info->client.account_name));
213                 return NT_STATUS_INSUFFICIENT_LOGON_INFO;
214         } else if (NT_STATUS_IS_OK(nt_status)) {
215                 DEBUG(1, ("Winbindd authentication for [%s]\\[%s] failed, "
216                           "but no error code is available!\n", 
217                           user_info->client.domain_name, user_info->client.account_name));
218                 return NT_STATUS_NO_LOGON_SERVERS;
219         }
220
221         return nt_status;
222 }
223
224 struct winbind_check_password_state {
225         struct winbind_SamLogon req;
226 };
227
228 /*
229  Authenticate a user with a challenge/response
230  using IRPC to the winbind task
231 */
232 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
233                                        TALLOC_CTX *mem_ctx,
234                                        const struct auth_usersupplied_info *user_info, 
235                                        struct auth_serversupplied_info **server_info)
236 {
237         NTSTATUS status;
238         struct server_id *winbind_servers;
239         struct winbind_check_password_state *s;
240         const struct auth_usersupplied_info *user_info_new;
241         struct netr_IdentityInfo *identity_info;
242
243         s = talloc(mem_ctx, struct winbind_check_password_state);
244         NT_STATUS_HAVE_NO_MEMORY(s);
245
246         winbind_servers = irpc_servers_byname(ctx->auth_ctx->msg_ctx, s, "winbind_server");
247         if ((winbind_servers == NULL) || (winbind_servers[0].id == 0)) {
248                 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, " 
249                           "no winbind_server running!\n",
250                           user_info->client.domain_name, user_info->client.account_name));
251                 return NT_STATUS_NO_LOGON_SERVERS;
252         }
253
254         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
255                 struct netr_PasswordInfo *password_info;
256
257                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
258                                            user_info, &user_info_new);
259                 NT_STATUS_NOT_OK_RETURN(status);
260                 user_info = user_info_new;
261
262                 password_info = talloc(s, struct netr_PasswordInfo);
263                 NT_STATUS_HAVE_NO_MEMORY(password_info);
264
265                 password_info->lmpassword = *user_info->password.hash.lanman;
266                 password_info->ntpassword = *user_info->password.hash.nt;
267
268                 identity_info = &password_info->identity_info;
269                 s->req.in.logon_level   = 1;
270                 s->req.in.logon.password= password_info;
271         } else {
272                 struct netr_NetworkInfo *network_info;
273                 uint8_t chal[8];
274
275                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
276                                            user_info, &user_info_new);
277                 NT_STATUS_NOT_OK_RETURN(status);
278                 user_info = user_info_new;
279
280                 network_info = talloc(s, struct netr_NetworkInfo);
281                 NT_STATUS_HAVE_NO_MEMORY(network_info);
282
283                 status = auth_get_challenge(ctx->auth_ctx, chal);
284                 NT_STATUS_NOT_OK_RETURN(status);
285
286                 memcpy(network_info->challenge, chal, sizeof(network_info->challenge));
287
288                 network_info->nt.length = user_info->password.response.nt.length;
289                 network_info->nt.data   = user_info->password.response.nt.data;
290
291                 network_info->lm.length = user_info->password.response.lanman.length;
292                 network_info->lm.data   = user_info->password.response.lanman.data;
293
294                 identity_info = &network_info->identity_info;
295                 s->req.in.logon_level   = 2;
296                 s->req.in.logon.network = network_info;
297         }
298
299         identity_info->domain_name.string       = user_info->client.domain_name;
300         identity_info->parameter_control        = user_info->logon_parameters; /* see MSV1_0_* */
301         identity_info->logon_id_low             = 0;
302         identity_info->logon_id_high            = 0;
303         identity_info->account_name.string      = user_info->client.account_name;
304         identity_info->workstation.string       = user_info->workstation_name;
305
306         s->req.in.validation_level      = 3;
307
308         status = IRPC_CALL(ctx->auth_ctx->msg_ctx, winbind_servers[0],
309                            winbind, WINBIND_SAMLOGON,
310                            &s->req, s);
311         NT_STATUS_NOT_OK_RETURN(status);
312
313         status = make_server_info_netlogon_validation(mem_ctx,
314                                                       user_info->client.account_name,
315                                                       s->req.in.validation_level,
316                                                       &s->req.out.validation,
317                                                       server_info);
318         NT_STATUS_NOT_OK_RETURN(status);
319
320         return NT_STATUS_OK;
321 }
322
323 /*
324  Authenticate a user with a challenge/response
325  using the samba3 winbind protocol via libwbclient
326 */
327 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
328                                                 TALLOC_CTX *mem_ctx,
329                                                 const struct auth_usersupplied_info *user_info,
330                                                 struct auth_serversupplied_info **server_info)
331 {
332         struct wbcAuthUserParams params;
333         struct wbcAuthUserInfo *info = NULL;
334         struct wbcAuthErrorInfo *err = NULL;
335         wbcErr wbc_status;
336         NTSTATUS nt_status;
337         struct netr_SamInfo3 info3;
338         union netr_Validation validation;
339
340
341         /* Send off request */
342         const struct auth_usersupplied_info *user_info_temp;
343         nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
344                                       AUTH_PASSWORD_RESPONSE,
345                                       user_info, &user_info_temp);
346         if (!NT_STATUS_IS_OK(nt_status)) {
347                 return nt_status;
348         }
349         user_info = user_info_temp;
350
351         ZERO_STRUCT(params);
352         ZERO_STRUCT(info3);
353         /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
354
355         params.parameter_control = user_info->logon_parameters;
356         params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
357                                     WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
358         params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
359
360         params.account_name     = user_info->client.account_name;
361         params.domain_name      = user_info->client.domain_name;
362         params.workstation_name = user_info->workstation_name;
363
364         d_fprintf(stderr, "looking up %s@%s logging in from %s\n",
365                   params.account_name, params.domain_name,
366                   params.workstation_name);
367
368         memcpy(params.password.response.challenge,
369                ctx->auth_ctx->challenge.data.data,
370                sizeof(params.password.response.challenge));
371
372         params.password.response.lm_length =
373                 user_info->password.response.lanman.length;
374         params.password.response.nt_length =
375                 user_info->password.response.nt.length;
376
377         params.password.response.lm_data =
378                 user_info->password.response.lanman.data;
379         params.password.response.nt_data =
380                 user_info->password.response.nt.data;
381
382         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
383         if (!WBC_ERROR_IS_OK(wbc_status)) {
384                 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
385                       err->nt_string, err->nt_status, err->display_string));
386
387                 nt_status = NT_STATUS(err->nt_status);
388                 wbcFreeMemory(err);
389                 NT_STATUS_NOT_OK_RETURN(nt_status);
390         }
391         nt_status = get_info3_from_wbcAuthUserInfo(mem_ctx, info, &info3);
392         wbcFreeMemory(info);
393         NT_STATUS_NOT_OK_RETURN(nt_status);
394
395         validation.sam3 = &info3;
396         nt_status = make_server_info_netlogon_validation(mem_ctx,
397                                         user_info->client.account_name,
398                                         3, &validation, server_info);
399         return nt_status;
400
401 }
402
403 static const struct auth_operations winbind_samba3_ops = {
404         .name           = "winbind_samba3",
405         .get_challenge  = auth_get_challenge_not_implemented,
406         .want_check     = winbind_want_check,
407         .check_password = winbind_check_password_samba3
408 };
409
410 static const struct auth_operations winbind_ops = {
411         .name           = "winbind",
412         .get_challenge  = auth_get_challenge_not_implemented,
413         .want_check     = winbind_want_check,
414         .check_password = winbind_check_password
415 };
416
417 static const struct auth_operations winbind_wbclient_ops = {
418         .name           = "winbind_wbclient",
419         .get_challenge  = auth_get_challenge_not_implemented,
420         .want_check     = winbind_want_check,
421         .check_password = winbind_check_password_wbclient
422 };
423
424 _PUBLIC_ NTSTATUS auth_winbind_init(void)
425 {
426         NTSTATUS ret;
427
428         ret = auth_register(&winbind_samba3_ops);
429         if (!NT_STATUS_IS_OK(ret)) {
430                 DEBUG(0,("Failed to register 'winbind_samba3' auth backend!\n"));
431                 return ret;
432         }
433
434         ret = auth_register(&winbind_ops);
435         if (!NT_STATUS_IS_OK(ret)) {
436                 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
437                 return ret;
438         }
439
440         ret = auth_register(&winbind_wbclient_ops);
441         if (!NT_STATUS_IS_OK(ret)) {
442                 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
443                 return ret;
444         }
445
446         return NT_STATUS_OK;
447 }