crypt32: Make some variables static and/or const.
[wine] / dlls / secur32 / dispatcher.c
1 /*
2  * Copyright 2005 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 <stdarg.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_WAIT_H
28 #include <sys/wait.h>
29 #endif 
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "sspi.h"
36 #include "secur32_priv.h"
37 #include "wine/debug.h"
38
39 #define INITIAL_BUFFER_SIZE 200
40
41 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
42
43 SECURITY_STATUS fork_helper(PNegoHelper *new_helper, const char *prog,
44         char* const argv[])
45 {
46     int pipe_in[2];
47     int pipe_out[2];
48     int i;
49     PNegoHelper helper;
50
51     TRACE("%s ", debugstr_a(prog));
52     for(i = 0; argv[i] != NULL; ++i)
53     {
54         TRACE("%s ", debugstr_a(argv[i]));
55     }
56     TRACE("\n");
57
58     if( pipe(pipe_in) < 0 )
59     {
60         return SEC_E_INTERNAL_ERROR;
61     }
62     if( pipe(pipe_out) < 0 )
63     {
64         close(pipe_in[0]);
65         close(pipe_in[1]);
66         return SEC_E_INTERNAL_ERROR;
67     }
68     if (!(helper = HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper))))
69     {
70         close(pipe_in[0]);
71         close(pipe_in[1]);
72         close(pipe_out[0]);
73         close(pipe_out[1]);
74         return SEC_E_INSUFFICIENT_MEMORY;
75     }
76
77     helper->helper_pid = fork();
78
79     if(helper->helper_pid == -1)
80     {
81         close(pipe_in[0]);
82         close(pipe_in[1]);
83         close(pipe_out[0]);
84         close(pipe_out[1]);
85         HeapFree( GetProcessHeap(), 0, helper );
86         return SEC_E_INTERNAL_ERROR;
87     }
88
89     if(helper->helper_pid == 0)
90     {
91         /* We're in the child now */
92         close(0);
93         close(1);
94
95         dup2(pipe_out[0], 0);
96         close(pipe_out[0]);
97         close(pipe_out[1]);
98
99         dup2(pipe_in[1], 1);
100         close(pipe_in[0]);
101         close(pipe_in[1]);
102
103         execvp(prog, argv);
104
105         /* Whoops, we shouldn't get here. Big badaboom.*/
106         write(STDOUT_FILENO, "BH\n", 3);
107         exit(1);
108     }
109     else
110     {
111         *new_helper = helper;
112         helper->version = -1;
113         helper->password = NULL;
114         helper->com_buf = NULL;
115         helper->com_buf_size = 0;
116         helper->com_buf_offset = 0;
117         helper->pipe_in = pipe_in[0];
118         close(pipe_in[1]);
119         helper->pipe_out = pipe_out[1];
120         close(pipe_out[0]);
121     }
122
123     return SEC_E_OK;
124 }
125
126 static SECURITY_STATUS read_line(PNegoHelper helper, int *offset_len)
127 {
128     char *newline;
129     int read_size;
130     
131     if(helper->com_buf == NULL)
132     {
133         TRACE("Creating a new buffer for the helper\n");
134         if((helper->com_buf = HeapAlloc(GetProcessHeap(), 0, INITIAL_BUFFER_SIZE)) == NULL)
135             return SEC_E_INSUFFICIENT_MEMORY;
136         
137         /* Created a new buffer, size is INITIAL_BUFFER_SIZE, offset is 0 */
138         helper->com_buf_size = INITIAL_BUFFER_SIZE;
139         helper->com_buf_offset = 0;
140     }
141
142     do
143     {
144         TRACE("offset = %d, size = %d\n", helper->com_buf_offset, helper->com_buf_size);
145         if(helper->com_buf_offset + INITIAL_BUFFER_SIZE > helper->com_buf_size)
146         {
147             /* increment buffer size in INITIAL_BUFFER_SIZE steps */
148             char *buf = HeapReAlloc(GetProcessHeap(), 0, helper->com_buf,
149                                     helper->com_buf_size + INITIAL_BUFFER_SIZE);
150             TRACE("Resizing buffer!\n");
151             if (!buf) return SEC_E_INSUFFICIENT_MEMORY;
152             helper->com_buf_size += INITIAL_BUFFER_SIZE;
153             helper->com_buf = buf;
154         }
155         if((read_size = read(helper->pipe_in, helper->com_buf + helper->com_buf_offset,
156                     helper->com_buf_size - helper->com_buf_offset)) <= 0)
157         {
158             return SEC_E_INTERNAL_ERROR;
159         }
160         
161         TRACE("read_size = %d, read: %s\n", read_size, 
162                 debugstr_a(helper->com_buf + helper->com_buf_offset));
163         helper->com_buf_offset += read_size;
164         newline = memchr(helper->com_buf, '\n', helper->com_buf_offset);
165     }while(newline == NULL);
166
167     /* Now, if there's a newline character, and we read more than that newline,
168      * we have to store the offset so we can preserve the additional data.*/
169     if( newline != helper->com_buf + helper->com_buf_offset)
170     {
171         TRACE("offset_len is calculated from %p - %p\n", 
172                 (helper->com_buf + helper->com_buf_offset), newline+1);
173         /* the length of the offset is the number of chars after the newline */
174         *offset_len = (helper->com_buf + helper->com_buf_offset) - (newline + 1);
175     }
176     else
177     {
178         *offset_len = 0;
179     }
180     
181     *newline = '\0';
182
183     return SEC_E_OK;
184 }
185
186 static SECURITY_STATUS preserve_unused(PNegoHelper helper, int offset_len)
187 {
188     TRACE("offset_len = %d\n", offset_len);
189
190     if(offset_len > 0)
191     {
192         memmove(helper->com_buf, helper->com_buf + helper->com_buf_offset, 
193                 offset_len);
194         helper->com_buf_offset = offset_len;
195     }
196     else
197     {
198         helper->com_buf_offset = 0;
199     }
200
201     TRACE("helper->com_buf_offset was set to: %d\n", helper->com_buf_offset);
202     return SEC_E_OK;
203 }
204
205 SECURITY_STATUS run_helper(PNegoHelper helper, char *buffer,
206         unsigned int max_buflen, int *buflen)
207 {
208     int offset_len;
209     SECURITY_STATUS sec_status = SEC_E_OK;
210
211     TRACE("In helper: sending %s\n", debugstr_a(buffer));
212
213     /* buffer + '\n' */
214     write(helper->pipe_out, buffer, lstrlenA(buffer));
215     write(helper->pipe_out, "\n", 1);
216
217     if((sec_status = read_line(helper, &offset_len)) != SEC_E_OK)
218     {
219         return sec_status;
220     }
221     
222     TRACE("In helper: received %s\n", debugstr_a(helper->com_buf));
223     *buflen = lstrlenA(helper->com_buf);
224
225     if( *buflen > max_buflen)
226     {   
227         ERR("Buffer size too small(%d given, %d required) dropping data!\n",
228                 max_buflen, *buflen);
229         return SEC_E_BUFFER_TOO_SMALL;
230     }
231
232     if( *buflen < 2 )
233     {
234         return SEC_E_ILLEGAL_MESSAGE;
235     }
236
237     if( (*buflen <= 3) && (strncmp(helper->com_buf, "BH", 2) == 0))
238     {
239         return SEC_E_INTERNAL_ERROR;
240     }
241
242     /* We only get ERR if the input size is too big. On a GENSEC error,
243      * ntlm_auth will return BH */
244     if(strncmp(helper->com_buf, "ERR", 3) == 0)
245     {
246         return SEC_E_INVALID_TOKEN;
247     }
248
249     memcpy(buffer, helper->com_buf, *buflen+1);
250
251     sec_status = preserve_unused(helper, offset_len);
252     
253     return sec_status;
254 }
255
256 void cleanup_helper(PNegoHelper helper)
257 {
258
259     TRACE("Killing helper %p\n", helper);
260     if( (helper == NULL) || (helper->helper_pid == 0))
261         return;
262       
263     HeapFree(GetProcessHeap(), 0, helper->password);
264     HeapFree(GetProcessHeap(), 0, helper->com_buf);
265
266     /* closing stdin will terminate ntlm_auth */
267     close(helper->pipe_out);
268     close(helper->pipe_in);
269
270     waitpid(helper->helper_pid, NULL, 0);
271
272     helper->helper_pid = 0;
273     HeapFree(GetProcessHeap(), 0, helper);
274 }
275
276 void check_version(PNegoHelper helper)
277 {
278     char temp[80];
279     char *newline;
280
281     TRACE("Checking version of helper\n");
282     if(helper != NULL)
283     {
284         int len = read(helper->pipe_in, temp, sizeof(temp)-1);
285         if (len > 8)
286         {
287             if((newline = memchr(temp, '\n', len)) != NULL)
288                 *newline = '\0';
289             else
290                 temp[len] = 0;
291
292             TRACE("Exact version is %s\n", debugstr_a(temp));
293             if(strncmp(temp+8, "4", 1) == 0)
294             {
295                 helper->version = 4;
296             }
297             else if(strncmp(temp+8, "3", 1) == 0)
298             {
299                 helper->version = 3;
300             }
301             else
302             {
303                 TRACE("Unknown version!\n");
304                 helper->version = -1;
305             }
306         }
307     }
308 }