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