r24366: Implemet backend for wbinfo -Y, sid2gid
[ira/wip.git] / source / winbind / wb_pam_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Authenticate a user
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27 #include "auth/credentials/credentials.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30 #include "librpc/gen_ndr/ndr_netlogon_c.h"
31 #include "librpc/gen_ndr/winbind.h"
32
33 /* Oh, there is so much to keep an eye on when authenticating a user.  Oh my! */
34 struct pam_auth_crap_state {
35         struct composite_context *ctx;
36         struct event_context *event_ctx;
37
38         struct winbind_SamLogon *req;
39         char *unix_username;
40
41         struct netr_NetworkInfo ninfo;
42         struct netr_LogonSamLogon r;
43
44         const char *user_name;
45         const char *domain_name;
46
47         struct netr_UserSessionKey user_session_key;
48         struct netr_LMSessionKey lm_key;
49         DATA_BLOB info3;
50 };
51
52 /*
53  * NTLM authentication.
54 */
55
56 static void pam_auth_crap_recv_logon(struct composite_context *ctx);
57
58 struct composite_context *wb_cmd_pam_auth_crap_send(TALLOC_CTX *mem_ctx,
59                                                     struct wbsrv_service *service,
60                                                     uint32_t logon_parameters,
61                                                     const char *domain,
62                                                     const char *user,
63                                                     const char *workstation,
64                                                     DATA_BLOB chal,
65                                                     DATA_BLOB nt_resp,
66                                                     DATA_BLOB lm_resp)
67 {
68         struct composite_context *result, *ctx;
69         struct pam_auth_crap_state *state;
70         struct netr_NetworkInfo *ninfo;
71         DATA_BLOB tmp_nt_resp, tmp_lm_resp;
72
73         result = composite_create(mem_ctx, service->task->event_ctx);
74         if (result == NULL) goto failed;
75
76         state = talloc(result, struct pam_auth_crap_state);
77         if (state == NULL) goto failed;
78         state->ctx = result;
79         result->private_data = state;
80
81         state->req = talloc(state, struct winbind_SamLogon);
82
83         state->req->in.logon_level = 2;
84         state->req->in.validation_level = 3;
85         ninfo = state->req->in.logon.network = talloc(state, struct netr_NetworkInfo);
86         if (ninfo == NULL) goto failed;
87         
88         ninfo->identity_info.account_name.string =  talloc_strdup(state, user);
89         ninfo->identity_info.domain_name.string =  talloc_strdup(state, domain);
90         ninfo->identity_info.parameter_control = logon_parameters;
91         ninfo->identity_info.logon_id_low = 0;
92         ninfo->identity_info.logon_id_high = 0;
93         ninfo->identity_info.workstation.string = talloc_strdup(state, workstation);
94
95         SMB_ASSERT(chal.length == sizeof(ninfo->challenge));
96         memcpy(ninfo->challenge, chal.data,
97                sizeof(ninfo->challenge));
98
99         tmp_nt_resp = data_blob_talloc(ninfo, nt_resp.data, nt_resp.length);
100         if ((nt_resp.data != NULL) &&
101             (tmp_nt_resp.data == NULL)) goto failed;
102
103         tmp_lm_resp = data_blob_talloc(ninfo, lm_resp.data, lm_resp.length);
104         if ((lm_resp.data != NULL) &&
105             (tmp_lm_resp.data == NULL)) goto failed;
106
107         ninfo->nt.length = tmp_nt_resp.length;
108         ninfo->nt.data = tmp_nt_resp.data;
109         ninfo->lm.length = tmp_lm_resp.length;
110         ninfo->lm.data = tmp_lm_resp.data;
111
112         state->unix_username = NULL;
113
114         ctx = wb_sam_logon_send(mem_ctx, service, state->req);
115         if (ctx == NULL) goto failed;
116
117         composite_continue(result, ctx, pam_auth_crap_recv_logon, state);
118         return result;
119
120  failed:
121         talloc_free(result);
122         return NULL;
123 }
124
125 /*  
126     NTLM Authentication
127
128     Send of a SamLogon request to authenticate a user.
129 */
130 static void pam_auth_crap_recv_logon(struct composite_context *ctx)
131 {
132         DATA_BLOB tmp_blob;
133         struct netr_SamBaseInfo *base;
134         struct pam_auth_crap_state *state =
135                 talloc_get_type(ctx->async.private_data,
136                                 struct pam_auth_crap_state);
137
138         state->ctx->status = wb_sam_logon_recv(ctx, state, state->req);
139         if (!composite_is_ok(state->ctx)) return;
140
141         state->ctx->status = ndr_push_struct_blob(
142                 &tmp_blob, state, state->req->out.validation.sam3,
143                 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
144         if (!composite_is_ok(state->ctx)) return;
145
146         /* The Samba3 protocol is a bit broken (due to non-IDL
147          * heritage, so for compatability we must add a non-zero 4
148          * bytes to the info3 */
149         state->info3 = data_blob_talloc(state, NULL, tmp_blob.length+4);
150         if (composite_nomem(state->info3.data, state->ctx)) return;
151
152         SIVAL(state->info3.data, 0, 1);
153         memcpy(state->info3.data+4, tmp_blob.data, tmp_blob.length);
154
155         base = &state->req->out.validation.sam3->base;
156
157         state->user_session_key = base->key;
158         state->lm_key = base->LMSessKey;
159
160         /* Give the caller the most accurate username possible.
161          * Assists where case sensitive comparisons may be done by our
162          * ntlm_auth callers */
163         if (base->account_name.string) {
164                 state->user_name = base->account_name.string;
165                 talloc_steal(state, base->account_name.string);
166         }
167         if (base->domain.string) {
168                 state->domain_name = base->domain.string;
169                 talloc_steal(state, base->domain.string);
170         }
171
172         state->unix_username = talloc_asprintf(state, "%s%s%s", 
173                                                state->domain_name,
174                                                lp_winbind_separator(),
175                                                state->user_name);
176         if (composite_nomem(state->unix_username, state->ctx)) return;
177
178         composite_done(state->ctx);
179 }
180
181 /* Having received a NTLM authentication reply, parse out the useful
182  * reply data for the caller */
183 NTSTATUS wb_cmd_pam_auth_crap_recv(struct composite_context *c,
184                                    TALLOC_CTX *mem_ctx,
185                                    DATA_BLOB *info3,
186                                    struct netr_UserSessionKey *user_session_key,
187                                    struct netr_LMSessionKey *lm_key,
188                                    char **unix_username)
189 {
190         struct pam_auth_crap_state *state =
191                 talloc_get_type(c->private_data, struct pam_auth_crap_state);
192         NTSTATUS status = composite_wait(c);
193         if (NT_STATUS_IS_OK(status)) {
194                 info3->length = state->info3.length;
195                 info3->data = talloc_steal(mem_ctx, state->info3.data);
196                 *user_session_key = state->user_session_key;
197                 *lm_key = state->lm_key;
198                 *unix_username = talloc_steal(mem_ctx, state->unix_username);
199         }
200         talloc_free(state);
201         return status;
202 }
203
204 /* Handle plaintext authentication, by encrypting the password and
205  * then sending via the NTLM calls */
206
207 struct composite_context *wb_cmd_pam_auth_send(TALLOC_CTX *mem_ctx,
208                                                struct wbsrv_service *service,
209                                                const char *domain,
210                                                const char *user,
211                                                const char *password)
212 {
213         struct cli_credentials *credentials;
214         const char *workstation;
215         NTSTATUS status;
216
217         DATA_BLOB chal, nt_resp, lm_resp, names_blob;
218         int flags = CLI_CRED_NTLM_AUTH;
219         if (lp_client_lanman_auth()) {
220                 flags |= CLI_CRED_LANMAN_AUTH;
221         }
222
223         if (lp_client_ntlmv2_auth()) {
224                 flags |= CLI_CRED_NTLMv2_AUTH;
225         }
226
227         DEBUG(5, ("wbsrv_samba3_pam_auth called\n"));
228
229         credentials = cli_credentials_init(mem_ctx);
230         if (!credentials) {
231                 return NULL;
232         }
233         cli_credentials_set_conf(credentials);
234         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
235         cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
236
237         cli_credentials_set_password(credentials, password, CRED_SPECIFIED);
238
239         chal = data_blob_talloc(mem_ctx, NULL, 8);
240         if (!chal.data) {
241                 return NULL;
242         }
243         generate_random_buffer(chal.data, chal.length);
244         cli_credentials_get_ntlm_username_domain(credentials, mem_ctx,
245                                                  &user, &domain);
246         /* for best compatability with multiple vitual netbios names
247          * on the host, this should be generated from the
248          * cli_credentials associated with the machine account */
249         workstation = cli_credentials_get_workstation(credentials);
250
251         names_blob = NTLMv2_generate_names_blob(
252                 mem_ctx,
253                 cli_credentials_get_workstation(credentials), 
254                 cli_credentials_get_domain(credentials));
255
256         status = cli_credentials_get_ntlm_response(
257                 credentials, mem_ctx, &flags, chal, names_blob,
258                 &lm_resp, &nt_resp, NULL, NULL);
259         if (!NT_STATUS_IS_OK(status)) {
260                 return NULL;
261         }
262         return wb_cmd_pam_auth_crap_send(mem_ctx, service,
263                                          0 /* logon parameters */, 
264                                          domain, user, workstation,
265                                          chal, nt_resp, lm_resp);
266 }
267
268 NTSTATUS wb_cmd_pam_auth_recv(struct composite_context *c)
269 {
270        struct pam_auth_crap_state *state =
271                talloc_get_type(c->private_data, struct pam_auth_crap_state);
272        NTSTATUS status = composite_wait(c);
273        talloc_free(state);
274        return status;
275 }