63827ef75508743103783b48156459fcd6991f35
[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.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                                                        true, /* This user was authenticated */
224                                                       user_info_dc);
225         NT_STATUS_NOT_OK_RETURN(status);
226
227         return NT_STATUS_OK;
228 }
229
230 /*
231  Authenticate a user with a challenge/response
232  using the samba3 winbind protocol via libwbclient
233 */
234 static NTSTATUS winbind_check_password_wbclient(struct auth_method_context *ctx,
235                                                 TALLOC_CTX *mem_ctx,
236                                                 const struct auth_usersupplied_info *user_info,
237                                                 struct auth_user_info_dc **user_info_dc)
238 {
239         struct wbcAuthUserParams params;
240         struct wbcAuthUserInfo *info = NULL;
241         struct wbcAuthErrorInfo *err = NULL;
242         wbcErr wbc_status;
243         NTSTATUS nt_status;
244         struct netr_SamInfo3 info3;
245         union netr_Validation validation;
246
247
248         /* Send off request */
249         const struct auth_usersupplied_info *user_info_temp;
250         nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx,
251                                       AUTH_PASSWORD_RESPONSE,
252                                       user_info, &user_info_temp);
253         if (!NT_STATUS_IS_OK(nt_status)) {
254                 return nt_status;
255         }
256         user_info = user_info_temp;
257
258         ZERO_STRUCT(params);
259         ZERO_STRUCT(info3);
260         /*params.flags = WBFLAG_PAM_INFO3_NDR;*/
261
262         params.parameter_control = user_info->logon_parameters;
263         params.parameter_control |= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
264                                     WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
265         params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
266
267         params.account_name     = user_info->client.account_name;
268         params.domain_name      = user_info->client.domain_name;
269         params.workstation_name = user_info->workstation_name;
270
271         d_fprintf(stderr, "looking up %s@%s logging in from %s\n",
272                   params.account_name, params.domain_name,
273                   params.workstation_name);
274
275         memcpy(params.password.response.challenge,
276                ctx->auth_ctx->challenge.data.data,
277                sizeof(params.password.response.challenge));
278
279         params.password.response.lm_length =
280                 user_info->password.response.lanman.length;
281         params.password.response.nt_length =
282                 user_info->password.response.nt.length;
283
284         params.password.response.lm_data =
285                 user_info->password.response.lanman.data;
286         params.password.response.nt_data =
287                 user_info->password.response.nt.data;
288
289         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
290         if (wbc_status == WBC_ERR_AUTH_ERROR) {
291                 DEBUG(1, ("error was %s (0x%08x)\nerror message was '%s'\n",
292                       err->nt_string, err->nt_status, err->display_string));
293
294                 nt_status = NT_STATUS(err->nt_status);
295                 wbcFreeMemory(err);
296                 NT_STATUS_NOT_OK_RETURN(nt_status);
297         } else if (!WBC_ERROR_IS_OK(wbc_status)) {
298                 DEBUG(1, ("wbcAuthenticateUserEx: failed with %u - %s\n",
299                         wbc_status, wbcErrorString(wbc_status)));
300                 return NT_STATUS_LOGON_FAILURE;
301         }
302         nt_status = get_info3_from_wbcAuthUserInfo(mem_ctx, info, &info3);
303         wbcFreeMemory(info);
304         NT_STATUS_NOT_OK_RETURN(nt_status);
305
306         validation.sam3 = &info3;
307         nt_status = make_user_info_dc_netlogon_validation(mem_ctx,
308                                                           user_info->client.account_name,
309                                                           3, &validation,
310                                                           true, /* This user was authenticated */
311                                                           user_info_dc);
312         return nt_status;
313
314 }
315
316 static const struct auth_operations winbind_ops = {
317         .name           = "winbind",
318         .get_challenge  = auth_get_challenge_not_implemented,
319         .want_check     = winbind_want_check,
320         .check_password = winbind_check_password
321 };
322
323 static const struct auth_operations winbind_wbclient_ops = {
324         .name           = "winbind_wbclient",
325         .get_challenge  = auth_get_challenge_not_implemented,
326         .want_check     = winbind_want_check,
327         .check_password = winbind_check_password_wbclient
328 };
329
330 _PUBLIC_ NTSTATUS auth4_winbind_init(void)
331 {
332         NTSTATUS ret;
333
334         ret = auth_register(&winbind_ops);
335         if (!NT_STATUS_IS_OK(ret)) {
336                 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
337                 return ret;
338         }
339
340         ret = auth_register(&winbind_wbclient_ops);
341         if (!NT_STATUS_IS_OK(ret)) {
342                 DEBUG(0,("Failed to register 'winbind_wbclient' auth backend!\n"));
343                 return ret;
344         }
345
346         return NT_STATUS_OK;
347 }