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