r1294: A nice, large, commit...
[samba.git] / source4 / rpc_server / dcesrv_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    server side dcerpc authentication code
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2004
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /*
27   startup the cryptographic side of an authenticated dcerpc server
28 */
29 NTSTATUS dcesrv_crypto_select_type(struct dcesrv_connection *dce_conn,
30                                struct dcesrv_auth *auth)
31 {
32         NTSTATUS status;
33         if (auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_INTEGRITY &&
34             auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
35                 DEBUG(2,("auth_level %d not supported in dcesrv auth\n", 
36                          auth->auth_info->auth_level));
37                 return NT_STATUS_INVALID_PARAMETER;
38         }
39
40         if (auth->gensec_security != NULL) {
41                 /* TODO:
42                  * this this function should not be called
43                  * twice per dcesrv_connection!
44                  * 
45                  * so we need to find out the right
46                  * dcerpc error to return
47                  */
48         }
49
50         status = gensec_server_start(&auth->gensec_security);
51         if (!NT_STATUS_IS_OK(status)) {
52                 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
53                 return status;
54         }
55
56         status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type);
57
58         if (!NT_STATUS_IS_OK(status)) {
59                 DEBUG(1, ("Failed to start GENSEC mech-specific server code (%d): %s\n", 
60                           (int)auth->auth_info->auth_type,
61                           nt_errstr(status)));
62                 return status;
63         }
64
65         return status;
66 }
67
68 /*
69   parse any auth information from a dcerpc bind request
70   return False if we can't handle the auth request for some 
71   reason (in which case we send a bind_nak)
72 */
73 BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
74 {
75         struct dcerpc_packet *pkt = &call->pkt;
76         struct dcesrv_connection *dce_conn = call->conn;
77         NTSTATUS status;
78
79         if (pkt->u.bind.auth_info.length == 0) {
80                 dce_conn->auth_state.auth_info = NULL;
81                 return True;
82         }
83
84         dce_conn->auth_state.auth_info = talloc_p(dce_conn->mem_ctx, struct dcerpc_auth);
85         if (!dce_conn->auth_state.auth_info) {
86                 return False;
87         }
88
89         status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
90                                       call->mem_ctx,
91                                       dce_conn->auth_state.auth_info,
92                                       (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
93         if (!NT_STATUS_IS_OK(status)) {
94                 return False;
95         }
96
97         status = dcesrv_crypto_select_type(dce_conn, &dce_conn->auth_state);
98         if (!NT_STATUS_IS_OK(status)) {
99                 return False;
100         }
101
102         return True;
103 }
104
105 /*
106   add any auth information needed in a bind ack, and process the authentication
107   information found in the bind.
108 */
109 BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct dcerpc_packet *pkt)
110 {
111         struct dcesrv_connection *dce_conn = call->conn;
112         NTSTATUS status;
113
114         if (!call->conn->auth_state.gensec_security) {
115                 return True;
116         }
117
118         status = gensec_update(dce_conn->auth_state.gensec_security,
119                                call->mem_ctx,
120                                dce_conn->auth_state.auth_info->credentials, 
121                                &dce_conn->auth_state.auth_info->credentials);
122         
123         if (NT_STATUS_IS_OK(status)) {
124                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
125                                              &dce_conn->auth_state.session_info);
126                 if (!NT_STATUS_IS_OK(status)) {
127                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
128                         return False;
129                 }
130                 return True;
131         } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
132                 dce_conn->auth_state.auth_info->auth_pad_length = 0;
133                 dce_conn->auth_state.auth_info->auth_reserved = 0;
134                 return True;
135         } else {
136                 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
137                 return False;
138         }
139 }
140
141
142 /*
143   process the final stage of a auth request
144 */
145 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
146 {
147         struct dcerpc_packet *pkt = &call->pkt;
148         struct dcesrv_connection *dce_conn = call->conn;
149         NTSTATUS status;
150
151         if (!dce_conn->auth_state.auth_info ||
152             !dce_conn->auth_state.gensec_security ||
153             pkt->u.auth.auth_info.length == 0) {
154                 return False;
155         }
156
157         status = ndr_pull_struct_blob(&pkt->u.auth.auth_info,
158                                       call->mem_ctx,
159                                       dce_conn->auth_state.auth_info,
160                                       (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
161         if (!NT_STATUS_IS_OK(status)) {
162                 return False;
163         }
164
165         status = gensec_update(dce_conn->auth_state.gensec_security,
166                                call->mem_ctx,
167                                dce_conn->auth_state.auth_info->credentials, 
168                                &dce_conn->auth_state.auth_info->credentials);
169         if (NT_STATUS_IS_OK(status)) {
170                 status = gensec_session_info(dce_conn->auth_state.gensec_security,
171                                              &dce_conn->auth_state.session_info);
172                 if (!NT_STATUS_IS_OK(status)) {
173                         DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
174                         return False;
175                 }
176                 return True;
177         } else {
178                 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n", 
179                           nt_errstr(status)));
180                 return False;
181         }
182
183         return True;
184 }
185
186
187 /*
188   check credentials on a request
189 */
190 BOOL dcesrv_auth_request(struct dcesrv_call_state *call)
191 {
192         struct dcerpc_packet *pkt = &call->pkt;
193         struct dcesrv_connection *dce_conn = call->conn;
194         DATA_BLOB auth_blob;
195         struct dcerpc_auth auth;
196         struct ndr_pull *ndr;
197         NTSTATUS status;
198
199         if (!dce_conn->auth_state.auth_info ||
200             !dce_conn->auth_state.gensec_security) {
201                 return True;
202         }
203
204         auth_blob.length = 8 + pkt->auth_length;
205
206         /* check for a valid length */
207         if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
208                 return False;
209         }
210
211         auth_blob.data = 
212                 pkt->u.request.stub_and_verifier.data + 
213                 pkt->u.request.stub_and_verifier.length - auth_blob.length;
214         pkt->u.request.stub_and_verifier.length -= auth_blob.length;
215
216         /* pull the auth structure */
217         ndr = ndr_pull_init_blob(&auth_blob, call->mem_ctx);
218         if (!ndr) {
219                 return False;
220         }
221
222         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
223                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
224         }
225
226         status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
227         if (!NT_STATUS_IS_OK(status)) {
228                 return False;
229         }
230
231         /* check signature or unseal the packet */
232         switch (dce_conn->auth_state.auth_info->auth_level) {
233         case DCERPC_AUTH_LEVEL_PRIVACY:
234                 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
235                                               call->mem_ctx,
236                                               pkt->u.request.stub_and_verifier.data, 
237                                               pkt->u.request.stub_and_verifier.length, 
238                                               &auth.credentials);
239                 break;
240
241         case DCERPC_AUTH_LEVEL_INTEGRITY:
242                 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
243                                              call->mem_ctx,
244                                              pkt->u.request.stub_and_verifier.data, 
245                                              pkt->u.request.stub_and_verifier.length,
246                                              &auth.credentials);
247                 break;
248
249         default:
250                 status = NT_STATUS_INVALID_LEVEL;
251                 break;
252         }
253
254         /* remove the indicated amount of paddiing */
255         if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
256                 return False;
257         }
258         pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
259
260         return NT_STATUS_IS_OK(status);
261 }
262
263
264 /* 
265    push a signed or sealed dcerpc request packet into a blob
266 */
267 BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
268                           DATA_BLOB *blob, struct dcerpc_packet *pkt)
269 {
270         struct dcesrv_connection *dce_conn = call->conn;
271         NTSTATUS status;
272         struct ndr_push *ndr;
273
274         /* non-signed packets are simple */
275         if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
276                 status = dcerpc_push_auth(blob, call->mem_ctx, pkt, NULL);
277                 return NT_STATUS_IS_OK(status);
278         }
279
280         ndr = ndr_push_init_ctx(call->mem_ctx);
281         if (!ndr) {
282                 return False;
283         }
284
285         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
286                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
287         }
288
289         status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
290         if (!NT_STATUS_IS_OK(status)) {
291                 return False;
292         }
293
294         /* pad to 8 byte multiple */
295         dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 8);
296         ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
297
298         /* sign or seal the packet */
299         switch (dce_conn->auth_state.auth_info->auth_level) {
300         case DCERPC_AUTH_LEVEL_PRIVACY:
301                 status = gensec_seal_packet(dce_conn->auth_state.gensec_security, 
302                                               call->mem_ctx,
303                                               ndr->data + DCERPC_REQUEST_LENGTH, 
304                                               ndr->offset - DCERPC_REQUEST_LENGTH,
305                                               &dce_conn->auth_state.auth_info->credentials);
306                 break;
307
308         case DCERPC_AUTH_LEVEL_INTEGRITY:
309                 status = gensec_sign_packet(dce_conn->auth_state.gensec_security, 
310                                             call->mem_ctx,
311                                             ndr->data + DCERPC_REQUEST_LENGTH, 
312                                             ndr->offset - DCERPC_REQUEST_LENGTH,
313                                             &dce_conn->auth_state.auth_info->credentials);
314                 break;
315         default:
316                 status = NT_STATUS_INVALID_LEVEL;
317                 break;
318         }
319
320         if (!NT_STATUS_IS_OK(status)) {
321                 return False;
322         }       
323
324         /* add the auth verifier */
325         status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
326         if (!NT_STATUS_IS_OK(status)) {
327                 return False;
328         }
329
330         /* extract the whole packet as a blob */
331         *blob = ndr_push_blob(ndr);
332
333         /* fill in the fragment length and auth_length, we can't fill
334            in these earlier as we don't know the signature length (it
335            could be variable length) */
336         dcerpc_set_frag_length(blob, blob->length);
337         dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
338
339         data_blob_free(&dce_conn->auth_state.auth_info->credentials);
340
341         return True;
342 }