d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / secur32 / dispatcher.c
blob5a9e26ae19acb308fd1e54d09d0114932a0d73da
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 "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 <errno.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "sspi.h"
39 #include "secur32_priv.h"
40 #include "wine/debug.h"
42 #define INITIAL_BUFFER_SIZE 200
44 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
46 SECURITY_STATUS fork_helper(PNegoHelper *new_helper, const char *prog,
47 char* const argv[])
49 #ifdef HAVE_FORK
50 int pipe_in[2];
51 int pipe_out[2];
52 int i;
53 PNegoHelper helper;
55 TRACE("%s ", debugstr_a(prog));
56 for(i = 0; argv[i] != NULL; ++i)
58 TRACE("%s ", debugstr_a(argv[i]));
60 TRACE("\n");
62 #ifdef HAVE_PIPE2
63 if (pipe2( pipe_in, O_CLOEXEC ) < 0 )
64 #endif
66 if( pipe(pipe_in) < 0 ) return SEC_E_INTERNAL_ERROR;
67 fcntl( pipe_in[0], F_SETFD, FD_CLOEXEC );
68 fcntl( pipe_in[1], F_SETFD, FD_CLOEXEC );
70 #ifdef HAVE_PIPE2
71 if (pipe2( pipe_out, O_CLOEXEC ) < 0 )
72 #endif
74 if( pipe(pipe_out) < 0 )
76 close(pipe_in[0]);
77 close(pipe_in[1]);
78 return SEC_E_INTERNAL_ERROR;
80 fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
81 fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
84 if (!(helper = HeapAlloc(GetProcessHeap(),0, sizeof(NegoHelper))))
86 close(pipe_in[0]);
87 close(pipe_in[1]);
88 close(pipe_out[0]);
89 close(pipe_out[1]);
90 return SEC_E_INSUFFICIENT_MEMORY;
93 helper->helper_pid = fork();
95 if(helper->helper_pid == -1)
97 close(pipe_in[0]);
98 close(pipe_in[1]);
99 close(pipe_out[0]);
100 close(pipe_out[1]);
101 HeapFree( GetProcessHeap(), 0, helper );
102 return SEC_E_INTERNAL_ERROR;
105 if(helper->helper_pid == 0)
107 /* We're in the child now */
108 dup2(pipe_out[0], 0);
109 close(pipe_out[0]);
110 close(pipe_out[1]);
112 dup2(pipe_in[1], 1);
113 close(pipe_in[0]);
114 close(pipe_in[1]);
116 execvp(prog, argv);
118 /* Whoops, we shouldn't get here. Big badaboom.*/
119 write(STDOUT_FILENO, "BH\n", 3);
120 _exit(1);
122 else
124 *new_helper = helper;
125 helper->major = helper->minor = helper->micro = -1;
126 helper->com_buf = NULL;
127 helper->com_buf_size = 0;
128 helper->com_buf_offset = 0;
129 helper->session_key = NULL;
130 helper->neg_flags = 0;
131 helper->crypt.ntlm.a4i = NULL;
132 helper->crypt.ntlm2.send_a4i = NULL;
133 helper->crypt.ntlm2.recv_a4i = NULL;
134 helper->crypt.ntlm2.send_sign_key = NULL;
135 helper->crypt.ntlm2.send_seal_key = NULL;
136 helper->crypt.ntlm2.recv_sign_key = NULL;
137 helper->crypt.ntlm2.recv_seal_key = NULL;
138 helper->pipe_in = pipe_in[0];
139 close(pipe_in[1]);
140 helper->pipe_out = pipe_out[1];
141 close(pipe_out[0]);
144 return SEC_E_OK;
145 #else
146 ERR( "no fork support on this platform\n" );
147 return SEC_E_INTERNAL_ERROR;
148 #endif
151 static SECURITY_STATUS read_line(PNegoHelper helper, int *offset_len)
153 char *newline;
154 int read_size;
156 if(helper->com_buf == NULL)
158 TRACE("Creating a new buffer for the helper\n");
159 if((helper->com_buf = HeapAlloc(GetProcessHeap(), 0, INITIAL_BUFFER_SIZE)) == NULL)
160 return SEC_E_INSUFFICIENT_MEMORY;
162 /* Created a new buffer, size is INITIAL_BUFFER_SIZE, offset is 0 */
163 helper->com_buf_size = INITIAL_BUFFER_SIZE;
164 helper->com_buf_offset = 0;
169 TRACE("offset = %d, size = %d\n", helper->com_buf_offset, helper->com_buf_size);
170 if(helper->com_buf_offset + INITIAL_BUFFER_SIZE > helper->com_buf_size)
172 /* increment buffer size in INITIAL_BUFFER_SIZE steps */
173 char *buf = HeapReAlloc(GetProcessHeap(), 0, helper->com_buf,
174 helper->com_buf_size + INITIAL_BUFFER_SIZE);
175 TRACE("Resizing buffer!\n");
176 if (!buf) return SEC_E_INSUFFICIENT_MEMORY;
177 helper->com_buf_size += INITIAL_BUFFER_SIZE;
178 helper->com_buf = buf;
180 if((read_size = read(helper->pipe_in, helper->com_buf + helper->com_buf_offset,
181 helper->com_buf_size - helper->com_buf_offset)) <= 0)
183 return SEC_E_INTERNAL_ERROR;
186 TRACE("read_size = %d, read: %s\n", read_size,
187 debugstr_a(helper->com_buf + helper->com_buf_offset));
188 helper->com_buf_offset += read_size;
189 newline = memchr(helper->com_buf, '\n', helper->com_buf_offset);
190 }while(newline == NULL);
192 /* Now, if there's a newline character, and we read more than that newline,
193 * we have to store the offset so we can preserve the additional data.*/
194 if( newline != helper->com_buf + helper->com_buf_offset)
196 TRACE("offset_len is calculated from %p - %p\n",
197 (helper->com_buf + helper->com_buf_offset), newline+1);
198 /* the length of the offset is the number of chars after the newline */
199 *offset_len = (helper->com_buf + helper->com_buf_offset) - (newline + 1);
201 else
203 *offset_len = 0;
206 *newline = '\0';
208 return SEC_E_OK;
211 static SECURITY_STATUS preserve_unused(PNegoHelper helper, int offset_len)
213 TRACE("offset_len = %d\n", offset_len);
215 if(offset_len > 0)
217 memmove(helper->com_buf, helper->com_buf + helper->com_buf_offset,
218 offset_len);
219 helper->com_buf_offset = offset_len;
221 else
223 helper->com_buf_offset = 0;
226 TRACE("helper->com_buf_offset was set to: %d\n", helper->com_buf_offset);
227 return SEC_E_OK;
230 SECURITY_STATUS run_helper(PNegoHelper helper, char *buffer,
231 unsigned int max_buflen, int *buflen)
233 int offset_len;
234 SECURITY_STATUS sec_status = SEC_E_OK;
236 TRACE("In helper: sending %s\n", debugstr_a(buffer));
238 /* buffer + '\n' */
239 write(helper->pipe_out, buffer, lstrlenA(buffer));
240 write(helper->pipe_out, "\n", 1);
242 if((sec_status = read_line(helper, &offset_len)) != SEC_E_OK)
244 return sec_status;
247 TRACE("In helper: received %s\n", debugstr_a(helper->com_buf));
248 *buflen = lstrlenA(helper->com_buf);
250 if( *buflen > max_buflen)
252 ERR("Buffer size too small(%d given, %d required) dropping data!\n",
253 max_buflen, *buflen);
254 return SEC_E_BUFFER_TOO_SMALL;
257 if( *buflen < 2 )
259 return SEC_E_ILLEGAL_MESSAGE;
262 /* We only get ERR if the input size is too big. On a GENSEC error,
263 * ntlm_auth will return BH */
264 if(strncmp(helper->com_buf, "ERR", 3) == 0)
266 return SEC_E_INVALID_TOKEN;
269 memcpy(buffer, helper->com_buf, *buflen+1);
271 sec_status = preserve_unused(helper, offset_len);
273 return sec_status;
276 void cleanup_helper(PNegoHelper helper)
279 TRACE("Killing helper %p\n", helper);
280 if(helper == NULL)
281 return;
283 HeapFree(GetProcessHeap(), 0, helper->com_buf);
284 HeapFree(GetProcessHeap(), 0, helper->session_key);
286 /* closing stdin will terminate ntlm_auth */
287 close(helper->pipe_out);
288 close(helper->pipe_in);
290 #ifdef HAVE_FORK
291 if (helper->helper_pid > 0) /* reap child */
293 pid_t wret;
294 do {
295 wret = waitpid(helper->helper_pid, NULL, 0);
296 } while (wret < 0 && errno == EINTR);
298 #endif
300 HeapFree(GetProcessHeap(), 0, helper);
303 void check_version(PNegoHelper helper)
305 char temp[80];
306 char *newline;
307 int major = 0, minor = 0, micro = 0, ret;
309 TRACE("Checking version of helper\n");
310 if(helper != NULL)
312 int len = read(helper->pipe_in, temp, sizeof(temp)-1);
313 if (len > 8)
315 if((newline = memchr(temp, '\n', len)) != NULL)
316 *newline = '\0';
317 else
318 temp[len] = 0;
320 TRACE("Exact version is %s\n", debugstr_a(temp));
321 ret = sscanf(temp, "Version %d.%d.%d", &major, &minor, &micro);
322 if(ret != 3)
324 ERR("Failed to get the helper version.\n");
325 helper->major = helper->minor = helper->micro = -1;
327 else
329 TRACE("Version recognized: %d.%d.%d\n", major, minor, micro);
330 helper->major = major;
331 helper->minor = minor;
332 helper->micro = micro;