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