secur32: Avoid double HeapFree() of password and session key.
[wine/hacks.git] / dlls / secur32 / dispatcher.c
blobb7b9f9d23f939e9c77f6638b48fa1cda34493a3e
1 /*
2 * Copyright 2005, 2006 Kai Blin
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.
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.
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
18 * A dispatcher to run ntlm_auth for wine's sspi module.
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"
39 #define INITIAL_BUFFER_SIZE 200
41 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
43 SECURITY_STATUS fork_helper(PNegoHelper *new_helper, const char *prog,
44 char* const argv[])
46 int pipe_in[2];
47 int pipe_out[2];
48 int i;
49 PNegoHelper helper;
51 TRACE("%s ", debugstr_a(prog));
52 for(i = 0; argv[i] != NULL; ++i)
54 TRACE("%s ", debugstr_a(argv[i]));
56 TRACE("\n");
58 if( pipe(pipe_in) < 0 )
60 return SEC_E_INTERNAL_ERROR;
62 if( pipe(pipe_out) < 0 )
64 close(pipe_in[0]);
65 close(pipe_in[1]);
66 return SEC_E_INTERNAL_ERROR;
68 if (!(helper = HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper))))
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;
77 helper->helper_pid = fork();
79 if(helper->helper_pid == -1)
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;
89 if(helper->helper_pid == 0)
91 /* We're in the child now */
92 close(0);
93 close(1);
95 dup2(pipe_out[0], 0);
96 close(pipe_out[0]);
97 close(pipe_out[1]);
99 dup2(pipe_in[1], 1);
100 close(pipe_in[0]);
101 close(pipe_in[1]);
103 execvp(prog, argv);
105 /* Whoops, we shouldn't get here. Big badaboom.*/
106 write(STDOUT_FILENO, "BH\n", 3);
107 _exit(1);
109 else
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->session_key = NULL;
118 helper->neg_flags = 0;
119 helper->pipe_in = pipe_in[0];
120 close(pipe_in[1]);
121 helper->pipe_out = pipe_out[1];
122 close(pipe_out[0]);
125 return SEC_E_OK;
128 static SECURITY_STATUS read_line(PNegoHelper helper, int *offset_len)
130 char *newline;
131 int read_size;
133 if(helper->com_buf == NULL)
135 TRACE("Creating a new buffer for the helper\n");
136 if((helper->com_buf = HeapAlloc(GetProcessHeap(), 0, INITIAL_BUFFER_SIZE)) == NULL)
137 return SEC_E_INSUFFICIENT_MEMORY;
139 /* Created a new buffer, size is INITIAL_BUFFER_SIZE, offset is 0 */
140 helper->com_buf_size = INITIAL_BUFFER_SIZE;
141 helper->com_buf_offset = 0;
146 TRACE("offset = %d, size = %d\n", helper->com_buf_offset, helper->com_buf_size);
147 if(helper->com_buf_offset + INITIAL_BUFFER_SIZE > helper->com_buf_size)
149 /* increment buffer size in INITIAL_BUFFER_SIZE steps */
150 char *buf = HeapReAlloc(GetProcessHeap(), 0, helper->com_buf,
151 helper->com_buf_size + INITIAL_BUFFER_SIZE);
152 TRACE("Resizing buffer!\n");
153 if (!buf) return SEC_E_INSUFFICIENT_MEMORY;
154 helper->com_buf_size += INITIAL_BUFFER_SIZE;
155 helper->com_buf = buf;
157 if((read_size = read(helper->pipe_in, helper->com_buf + helper->com_buf_offset,
158 helper->com_buf_size - helper->com_buf_offset)) <= 0)
160 return SEC_E_INTERNAL_ERROR;
163 TRACE("read_size = %d, read: %s\n", read_size,
164 debugstr_a(helper->com_buf + helper->com_buf_offset));
165 helper->com_buf_offset += read_size;
166 newline = memchr(helper->com_buf, '\n', helper->com_buf_offset);
167 }while(newline == NULL);
169 /* Now, if there's a newline character, and we read more than that newline,
170 * we have to store the offset so we can preserve the additional data.*/
171 if( newline != helper->com_buf + helper->com_buf_offset)
173 TRACE("offset_len is calculated from %p - %p\n",
174 (helper->com_buf + helper->com_buf_offset), newline+1);
175 /* the length of the offset is the number of chars after the newline */
176 *offset_len = (helper->com_buf + helper->com_buf_offset) - (newline + 1);
178 else
180 *offset_len = 0;
183 *newline = '\0';
185 return SEC_E_OK;
188 static SECURITY_STATUS preserve_unused(PNegoHelper helper, int offset_len)
190 TRACE("offset_len = %d\n", offset_len);
192 if(offset_len > 0)
194 memmove(helper->com_buf, helper->com_buf + helper->com_buf_offset,
195 offset_len);
196 helper->com_buf_offset = offset_len;
198 else
200 helper->com_buf_offset = 0;
203 TRACE("helper->com_buf_offset was set to: %d\n", helper->com_buf_offset);
204 return SEC_E_OK;
207 SECURITY_STATUS run_helper(PNegoHelper helper, char *buffer,
208 unsigned int max_buflen, int *buflen)
210 int offset_len;
211 SECURITY_STATUS sec_status = SEC_E_OK;
213 TRACE("In helper: sending %s\n", debugstr_a(buffer));
215 /* buffer + '\n' */
216 write(helper->pipe_out, buffer, lstrlenA(buffer));
217 write(helper->pipe_out, "\n", 1);
219 if((sec_status = read_line(helper, &offset_len)) != SEC_E_OK)
221 return sec_status;
224 TRACE("In helper: received %s\n", debugstr_a(helper->com_buf));
225 *buflen = lstrlenA(helper->com_buf);
227 if( *buflen > max_buflen)
229 ERR("Buffer size too small(%d given, %d required) dropping data!\n",
230 max_buflen, *buflen);
231 return SEC_E_BUFFER_TOO_SMALL;
234 if( *buflen < 2 )
236 return SEC_E_ILLEGAL_MESSAGE;
239 /* We only get ERR if the input size is too big. On a GENSEC error,
240 * ntlm_auth will return BH */
241 if(strncmp(helper->com_buf, "ERR", 3) == 0)
243 return SEC_E_INVALID_TOKEN;
246 memcpy(buffer, helper->com_buf, *buflen+1);
248 sec_status = preserve_unused(helper, offset_len);
250 return sec_status;
253 void cleanup_helper(PNegoHelper helper)
256 TRACE("Killing helper %p\n", helper);
257 if( (helper == NULL) || (helper->helper_pid == 0))
258 return;
260 HeapFree(GetProcessHeap(), 0, helper->com_buf);
261 HeapFree(GetProcessHeap(), 0, helper->session_key);
263 /* closing stdin will terminate ntlm_auth */
264 close(helper->pipe_out);
265 close(helper->pipe_in);
267 waitpid(helper->helper_pid, NULL, 0);
269 helper->helper_pid = 0;
270 HeapFree(GetProcessHeap(), 0, helper);
273 void check_version(PNegoHelper helper)
275 char temp[80];
276 char *newline;
278 TRACE("Checking version of helper\n");
279 if(helper != NULL)
281 int len = read(helper->pipe_in, temp, sizeof(temp)-1);
282 if (len > 8)
284 if((newline = memchr(temp, '\n', len)) != NULL)
285 *newline = '\0';
286 else
287 temp[len] = 0;
289 TRACE("Exact version is %s\n", debugstr_a(temp));
290 if(strncmp(temp+8, "4", 1) == 0)
292 helper->version = 4;
294 else if(strncmp(temp+8, "3", 1) == 0)
296 helper->version = 3;
298 else
300 TRACE("Unknown version!\n");
301 helper->version = -1;