2f8074d3cbb22a560cd33a7b832f3c0142b67b84
[tprouty/samba.git] / source4 / auth / 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 "nsswitch/winbind_client.h"
27 #include "librpc/gen_ndr/ndr_netlogon.h"
28 #include "librpc/gen_ndr/ndr_winbind.h"
29 #include "lib/messaging/irpc.h"
30 #include "param/param.h"
31
32 static NTSTATUS get_info3_from_ndr(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, struct winbindd_response *response, struct netr_SamInfo3 *info3)
33 {
34         size_t len = response->length - sizeof(struct winbindd_response);
35         if (len > 4) {
36                 enum ndr_err_code ndr_err;
37                 DATA_BLOB blob;
38                 blob.length = len - 4;
39                 blob.data = (uint8_t *)(((char *)response->extra_data.data) + 4);
40
41                 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, 
42                                iconv_convenience, info3,
43                               (ndr_pull_flags_fn_t)ndr_pull_netr_SamInfo3);
44                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
45                         return ndr_map_error2ntstatus(ndr_err);
46                 }
47
48                 return NT_STATUS_OK;
49         } else {
50                 DEBUG(2, ("get_info3_from_ndr: No info3 struct found!\n"));
51                 return NT_STATUS_UNSUCCESSFUL;
52         }
53 }
54
55 static NTSTATUS winbind_want_check(struct auth_method_context *ctx,
56                                    TALLOC_CTX *mem_ctx,
57                                    const struct auth_usersupplied_info *user_info)
58 {
59         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
60                 return NT_STATUS_NOT_IMPLEMENTED;
61         }
62
63         /* TODO: maybe limit the user scope to remote users only */
64         return NT_STATUS_OK;
65 }
66
67 /*
68  Authenticate a user with a challenge/response
69  using the samba3 winbind protocol
70 */
71 static NTSTATUS winbind_check_password_samba3(struct auth_method_context *ctx,
72                                               TALLOC_CTX *mem_ctx,
73                                               const struct auth_usersupplied_info *user_info, 
74                                               struct auth_serversupplied_info **server_info)
75 {
76         struct winbindd_request request;
77         struct winbindd_response response;
78         NSS_STATUS result;
79         NTSTATUS nt_status;
80         struct netr_SamInfo3 info3;             
81
82         /* Send off request */
83         const struct auth_usersupplied_info *user_info_temp;    
84         nt_status = encrypt_user_info(mem_ctx, ctx->auth_ctx, 
85                                       AUTH_PASSWORD_RESPONSE, 
86                                       user_info, &user_info_temp);
87         if (!NT_STATUS_IS_OK(nt_status)) {
88                 return nt_status;
89         }
90         user_info = user_info_temp;
91
92         ZERO_STRUCT(request);
93         ZERO_STRUCT(response);
94         request.flags = WBFLAG_PAM_INFO3_NDR;
95
96         request.data.auth_crap.logon_parameters = user_info->logon_parameters;
97
98         safe_strcpy(request.data.auth_crap.user,
99                        user_info->client.account_name, sizeof(fstring));
100         safe_strcpy(request.data.auth_crap.domain,
101                        user_info->client.domain_name, sizeof(fstring));
102         safe_strcpy(request.data.auth_crap.workstation,
103                        user_info->workstation_name, sizeof(fstring));
104
105         memcpy(request.data.auth_crap.chal, ctx->auth_ctx->challenge.data.data, sizeof(request.data.auth_crap.chal));
106
107         request.data.auth_crap.lm_resp_len = MIN(user_info->password.response.lanman.length,
108                                                  sizeof(request.data.auth_crap.lm_resp));
109         request.data.auth_crap.nt_resp_len = MIN(user_info->password.response.nt.length, 
110                                                  sizeof(request.data.auth_crap.nt_resp));
111
112         memcpy(request.data.auth_crap.lm_resp, user_info->password.response.lanman.data,
113                request.data.auth_crap.lm_resp_len);
114         memcpy(request.data.auth_crap.nt_resp, user_info->password.response.nt.data,
115                request.data.auth_crap.nt_resp_len);
116
117         result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
118
119         nt_status = NT_STATUS(response.data.auth.nt_status);
120         NT_STATUS_NOT_OK_RETURN(nt_status);
121
122         if (result == NSS_STATUS_SUCCESS && response.extra_data.data) {
123                 union netr_Validation validation;
124
125                 nt_status = get_info3_from_ndr(mem_ctx, lp_iconv_convenience(ctx->auth_ctx->lp_ctx), &response, &info3);
126                 SAFE_FREE(response.extra_data.data);
127                 NT_STATUS_NOT_OK_RETURN(nt_status); 
128
129                 validation.sam3 = &info3;
130                 nt_status = make_server_info_netlogon_validation(mem_ctx, 
131                                                                  user_info->client.account_name, 
132                                                                  3, &validation,
133                                                                  server_info);
134                 return nt_status;
135         } else if (result == NSS_STATUS_SUCCESS && !response.extra_data.data) {
136                 DEBUG(0, ("Winbindd authenticated the user [%s]\\[%s], "
137                           "but did not include the required info3 reply!\n", 
138                           user_info->client.domain_name, user_info->client.account_name));
139                 return NT_STATUS_INSUFFICIENT_LOGON_INFO;
140         } else if (NT_STATUS_IS_OK(nt_status)) {
141                 DEBUG(1, ("Winbindd authentication for [%s]\\[%s] failed, "
142                           "but no error code is available!\n", 
143                           user_info->client.domain_name, user_info->client.account_name));
144                 return NT_STATUS_NO_LOGON_SERVERS;
145         }
146
147         return nt_status;
148 }
149
150 struct winbind_check_password_state {
151         struct winbind_SamLogon req;
152 };
153
154 /*
155  Authenticate a user with a challenge/response
156  using IRPC to the winbind task
157 */
158 static NTSTATUS winbind_check_password(struct auth_method_context *ctx,
159                                        TALLOC_CTX *mem_ctx,
160                                        const struct auth_usersupplied_info *user_info, 
161                                        struct auth_serversupplied_info **server_info)
162 {
163         NTSTATUS status;
164         struct server_id *winbind_servers;
165         struct winbind_check_password_state *s;
166         const struct auth_usersupplied_info *user_info_new;
167         struct netr_IdentityInfo *identity_info;
168
169         s = talloc(mem_ctx, struct winbind_check_password_state);
170         NT_STATUS_HAVE_NO_MEMORY(s);
171
172         winbind_servers = irpc_servers_byname(ctx->auth_ctx->msg_ctx, s, "winbind_server");
173         if ((winbind_servers == NULL) || (winbind_servers[0].id == 0)) {
174                 DEBUG(0, ("Winbind authentication for [%s]\\[%s] failed, " 
175                           "no winbind_server running!\n",
176                           user_info->client.domain_name, user_info->client.account_name));
177                 return NT_STATUS_NO_LOGON_SERVERS;
178         }
179
180         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
181                 struct netr_PasswordInfo *password_info;
182
183                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_HASH,
184                                            user_info, &user_info_new);
185                 NT_STATUS_NOT_OK_RETURN(status);
186                 user_info = user_info_new;
187
188                 password_info = talloc(s, struct netr_PasswordInfo);
189                 NT_STATUS_HAVE_NO_MEMORY(password_info);
190
191                 password_info->lmpassword = *user_info->password.hash.lanman;
192                 password_info->ntpassword = *user_info->password.hash.nt;
193
194                 identity_info = &password_info->identity_info;
195                 s->req.in.logon_level   = 1;
196                 s->req.in.logon.password= password_info;
197         } else {
198                 struct netr_NetworkInfo *network_info;
199                 const uint8_t *challenge;
200
201                 status = encrypt_user_info(s, ctx->auth_ctx, AUTH_PASSWORD_RESPONSE,
202                                            user_info, &user_info_new);
203                 NT_STATUS_NOT_OK_RETURN(status);
204                 user_info = user_info_new;
205
206                 network_info = talloc(s, struct netr_NetworkInfo);
207                 NT_STATUS_HAVE_NO_MEMORY(network_info);
208
209                 status = auth_get_challenge(ctx->auth_ctx, &challenge);
210                 NT_STATUS_NOT_OK_RETURN(status);
211
212                 memcpy(network_info->challenge, challenge, sizeof(network_info->challenge));
213
214                 network_info->nt.length = user_info->password.response.nt.length;
215                 network_info->nt.data   = user_info->password.response.nt.data;
216
217                 network_info->lm.length = user_info->password.response.lanman.length;
218                 network_info->lm.data   = user_info->password.response.lanman.data;
219
220                 identity_info = &network_info->identity_info;
221                 s->req.in.logon_level   = 2;
222                 s->req.in.logon.network = network_info;
223         }
224
225         identity_info->domain_name.string       = user_info->client.domain_name;
226         identity_info->parameter_control        = user_info->logon_parameters; /* see MSV1_0_* */
227         identity_info->logon_id_low             = 0;
228         identity_info->logon_id_high            = 0;
229         identity_info->account_name.string      = user_info->client.account_name;
230         identity_info->workstation.string       = user_info->workstation_name;
231
232         s->req.in.validation_level      = 3;
233
234         status = IRPC_CALL(ctx->auth_ctx->msg_ctx, winbind_servers[0],
235                            winbind, WINBIND_SAMLOGON,
236                            &s->req, s);
237         NT_STATUS_NOT_OK_RETURN(status);
238
239         status = make_server_info_netlogon_validation(mem_ctx,
240                                                       user_info->client.account_name,
241                                                       s->req.in.validation_level,
242                                                       &s->req.out.validation,
243                                                       server_info);
244         NT_STATUS_NOT_OK_RETURN(status);
245
246         return NT_STATUS_OK;
247 }
248
249 static const struct auth_operations winbind_samba3_ops = {
250         .name           = "winbind_samba3",
251         .get_challenge  = auth_get_challenge_not_implemented,
252         .want_check     = winbind_want_check,
253         .check_password = winbind_check_password_samba3
254 };
255
256 static const struct auth_operations winbind_ops = {
257         .name           = "winbind",
258         .get_challenge  = auth_get_challenge_not_implemented,
259         .want_check     = winbind_want_check,
260         .check_password = winbind_check_password
261 };
262
263 _PUBLIC_ NTSTATUS auth_winbind_init(void)
264 {
265         NTSTATUS ret;
266
267         ret = auth_register(&winbind_samba3_ops);
268         if (!NT_STATUS_IS_OK(ret)) {
269                 DEBUG(0,("Failed to register 'winbind_samba3' auth backend!\n"));
270                 return ret;
271         }
272
273         ret = auth_register(&winbind_ops);
274         if (!NT_STATUS_IS_OK(ret)) {
275                 DEBUG(0,("Failed to register 'winbind' auth backend!\n"));
276                 return ret;
277         }
278
279         return NT_STATUS_OK;
280 }