secur32: Validate buffers passed to schan_DecryptMessage.
[wine] / dlls / secur32 / dispatcher.c
1 /*
2  * Copyright 2005, 2006 Kai Blin
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  * A dispatcher to run ntlm_auth for wine's sspi module.
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_WAIT_H
30 #include <sys/wait.h>
31 #endif 
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "sspi.h"
38 #include "secur32_priv.h"
39 #include "wine/debug.h"
40
41 #define INITIAL_BUFFER_SIZE 200
42
43 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
44
45 SECURITY_STATUS fork_helper(PNegoHelper *new_helper, const char *prog,
46         char* const argv[])
47 {
48 #ifdef HAVE_FORK
49     int pipe_in[2];
50     int pipe_out[2];
51     int i;
52     PNegoHelper helper;
53
54     TRACE("%s ", debugstr_a(prog));
55     for(i = 0; argv[i] != NULL; ++i)
56     {
57         TRACE("%s ", debugstr_a(argv[i]));
58     }
59     TRACE("\n");
60
61 #ifdef HAVE_PIPE2
62     if (pipe2( pipe_in, O_CLOEXEC ) < 0 )
63 #endif
64     {
65         if( pipe(pipe_in) < 0 ) return SEC_E_INTERNAL_ERROR;
66         fcntl( pipe_in[0], F_SETFD, FD_CLOEXEC );
67         fcntl( pipe_in[1], F_SETFD, FD_CLOEXEC );
68     }
69 #ifdef HAVE_PIPE2
70     if (pipe2( pipe_out, O_CLOEXEC ) < 0 )
71 #endif
72     {
73         if( pipe(pipe_out) < 0 )
74         {
75             close(pipe_in[0]);
76             close(pipe_in[1]);
77             return SEC_E_INTERNAL_ERROR;
78         }
79         fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
80         fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
81     }
82
83     if (!(helper = HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper))))
84     {
85         close(pipe_in[0]);
86         close(pipe_in[1]);
87         close(pipe_out[0]);
88         close(pipe_out[1]);
89         return SEC_E_INSUFFICIENT_MEMORY;
90     }
91
92     helper->helper_pid = fork();
93
94     if(helper->helper_pid == -1)
95     {
96         close(pipe_in[0]);
97         close(pipe_in[1]);
98         close(pipe_out[0]);
99         close(pipe_out[1]);
100         HeapFree( GetProcessHeap(), 0, helper );
101         return SEC_E_INTERNAL_ERROR;
102     }
103
104     if(helper->helper_pid == 0)
105     {
106         /* We're in the child now */
107         dup2(pipe_out[0], 0);
108         close(pipe_out[0]);
109         close(pipe_out[1]);
110
111         dup2(pipe_in[1], 1);
112         close(pipe_in[0]);
113         close(pipe_in[1]);
114
115         execvp(prog, argv);
116
117         /* Whoops, we shouldn't get here. Big badaboom.*/
118         write(STDOUT_FILENO, "BH\n", 3);
119         _exit(1);
120     }
121     else
122     {
123         *new_helper = helper;
124         helper->major = helper->minor = helper->micro = -1;
125         helper->com_buf = NULL;
126         helper->com_buf_size = 0;
127         helper->com_buf_offset = 0;
128         helper->session_key = NULL;
129         helper->neg_flags = 0;
130         helper->crypt.ntlm.a4i = NULL;
131         helper->crypt.ntlm2.send_a4i = NULL;
132         helper->crypt.ntlm2.recv_a4i = NULL;
133         helper->crypt.ntlm2.send_sign_key = NULL;
134         helper->crypt.ntlm2.send_seal_key = NULL;
135         helper->crypt.ntlm2.recv_sign_key = NULL;
136         helper->crypt.ntlm2.recv_seal_key = NULL;
137         helper->pipe_in = pipe_in[0];
138         close(pipe_in[1]);
139         helper->pipe_out = pipe_out[1];
140         close(pipe_out[0]);
141     }
142
143     return SEC_E_OK;
144 #else
145     ERR( "no fork support on this platform\n" );
146     return SEC_E_INTERNAL_ERROR;
147 #endif
148 }
149
150 static SECURITY_STATUS read_line(PNegoHelper helper, int *offset_len)
151 {
152     char *newline;
153     int read_size;
154     
155     if(helper->com_buf == NULL)
156     {
157         TRACE("Creating a new buffer for the helper\n");
158         if((helper->com_buf = HeapAlloc(GetProcessHeap(), 0, INITIAL_BUFFER_SIZE)) == NULL)
159             return SEC_E_INSUFFICIENT_MEMORY;
160         
161         /* Created a new buffer, size is INITIAL_BUFFER_SIZE, offset is 0 */
162         helper->com_buf_size = INITIAL_BUFFER_SIZE;
163         helper->com_buf_offset = 0;
164     }
165
166     do
167     {
168         TRACE("offset = %d, size = %d\n", helper->com_buf_offset, helper->com_buf_size);
169         if(helper->com_buf_offset + INITIAL_BUFFER_SIZE > helper->com_buf_size)
170         {
171             /* increment buffer size in INITIAL_BUFFER_SIZE steps */
172             char *buf = HeapReAlloc(GetProcessHeap(), 0, helper->com_buf,
173                                     helper->com_buf_size + INITIAL_BUFFER_SIZE);
174             TRACE("Resizing buffer!\n");
175             if (!buf) return SEC_E_INSUFFICIENT_MEMORY;
176             helper->com_buf_size += INITIAL_BUFFER_SIZE;
177             helper->com_buf = buf;
178         }
179         if((read_size = read(helper->pipe_in, helper->com_buf + helper->com_buf_offset,
180                     helper->com_buf_size - helper->com_buf_offset)) <= 0)
181         {
182             return SEC_E_INTERNAL_ERROR;
183         }
184         
185         TRACE("read_size = %d, read: %s\n", read_size, 
186                 debugstr_a(helper->com_buf + helper->com_buf_offset));
187         helper->com_buf_offset += read_size;
188         newline = memchr(helper->com_buf, '\n', helper->com_buf_offset);
189     }while(newline == NULL);
190
191     /* Now, if there's a newline character, and we read more than that newline,
192      * we have to store the offset so we can preserve the additional data.*/
193     if( newline != helper->com_buf + helper->com_buf_offset)
194     {
195         TRACE("offset_len is calculated from %p - %p\n", 
196                 (helper->com_buf + helper->com_buf_offset), newline+1);
197         /* the length of the offset is the number of chars after the newline */
198         *offset_len = (helper->com_buf + helper->com_buf_offset) - (newline + 1);
199     }
200     else
201     {
202         *offset_len = 0;
203     }
204     
205     *newline = '\0';
206
207     return SEC_E_OK;
208 }
209
210 static SECURITY_STATUS preserve_unused(PNegoHelper helper, int offset_len)
211 {
212     TRACE("offset_len = %d\n", offset_len);
213
214     if(offset_len > 0)
215     {
216         memmove(helper->com_buf, helper->com_buf + helper->com_buf_offset, 
217                 offset_len);
218         helper->com_buf_offset = offset_len;
219     }
220     else
221     {
222         helper->com_buf_offset = 0;
223     }
224
225     TRACE("helper->com_buf_offset was set to: %d\n", helper->com_buf_offset);
226     return SEC_E_OK;
227 }
228
229 SECURITY_STATUS run_helper(PNegoHelper helper, char *buffer,
230         unsigned int max_buflen, int *buflen)
231 {
232     int offset_len;
233     SECURITY_STATUS sec_status = SEC_E_OK;
234
235     TRACE("In helper: sending %s\n", debugstr_a(buffer));
236
237     /* buffer + '\n' */
238     write(helper->pipe_out, buffer, lstrlenA(buffer));
239     write(helper->pipe_out, "\n", 1);
240
241     if((sec_status = read_line(helper, &offset_len)) != SEC_E_OK)
242     {
243         return sec_status;
244     }
245     
246     TRACE("In helper: received %s\n", debugstr_a(helper->com_buf));
247     *buflen = lstrlenA(helper->com_buf);
248
249     if( *buflen > max_buflen)
250     {   
251         ERR("Buffer size too small(%d given, %d required) dropping data!\n",
252                 max_buflen, *buflen);
253         return SEC_E_BUFFER_TOO_SMALL;
254     }
255
256     if( *buflen < 2 )
257     {
258         return SEC_E_ILLEGAL_MESSAGE;
259     }
260
261     /* We only get ERR if the input size is too big. On a GENSEC error,
262      * ntlm_auth will return BH */
263     if(strncmp(helper->com_buf, "ERR", 3) == 0)
264     {
265         return SEC_E_INVALID_TOKEN;
266     }
267
268     memcpy(buffer, helper->com_buf, *buflen+1);
269
270     sec_status = preserve_unused(helper, offset_len);
271     
272     return sec_status;
273 }
274
275 void cleanup_helper(PNegoHelper helper)
276 {
277
278     TRACE("Killing helper %p\n", helper);
279     if( (helper == NULL) || (helper->helper_pid == 0))
280         return;
281
282     HeapFree(GetProcessHeap(), 0, helper->com_buf);
283
284     /* closing stdin will terminate ntlm_auth */
285     close(helper->pipe_out);
286     close(helper->pipe_in);
287
288     helper->helper_pid = 0;
289     HeapFree(GetProcessHeap(), 0, helper);
290 }
291
292 void check_version(PNegoHelper helper)
293 {
294     char temp[80];
295     char *newline;
296     int major = 0, minor = 0, micro = 0, ret;
297
298     TRACE("Checking version of helper\n");
299     if(helper != NULL)
300     {
301         int len = read(helper->pipe_in, temp, sizeof(temp)-1);
302         if (len > 8)
303         {
304             if((newline = memchr(temp, '\n', len)) != NULL)
305                 *newline = '\0';
306             else
307                 temp[len] = 0;
308
309             TRACE("Exact version is %s\n", debugstr_a(temp));
310             ret = sscanf(temp, "Version %d.%d.%d", &major, &minor, &micro);
311             if(ret != 3)
312             {
313                 ERR("Failed to get the helper version.\n");
314                 helper->major = helper->minor = helper->micro = -1;
315             }
316             else
317             {
318                 TRACE("Version recognized: %d.%d.%d\n", major, minor, micro);
319                 helper->major = major;
320                 helper->minor = minor;
321                 helper->micro = micro;
322             }
323         }
324     }
325 }