/src/FreeRDP/libfreerdp/core/childsession.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * FreeRDP: A Remote Desktop Protocol Implementation  | 
3  |  |  * Named pipe transport  | 
4  |  |  *  | 
5  |  |  * Copyright 2023-2024 David Fort <contact@hardening-consulting.com>  | 
6  |  |  *  | 
7  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
8  |  |  * you may not use this file except in compliance with the License.  | 
9  |  |  * You may obtain a copy of the License at  | 
10  |  |  *  | 
11  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
12  |  |  *  | 
13  |  |  * Unless required by applicable law or agreed to in writing, software  | 
14  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
15  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
16  |  |  * See the License for the specific language governing permissions and  | 
17  |  |  * limitations under the License.  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #include "tcp.h"  | 
21  |  |  | 
22  |  | #include <winpr/library.h>  | 
23  |  | #include <winpr/assert.h>  | 
24  |  | #include <winpr/print.h>  | 
25  |  | #include <winpr/sysinfo.h>  | 
26  |  |  | 
27  |  | #include <freerdp/utils/ringbuffer.h>  | 
28  |  |  | 
29  |  | #include "childsession.h"  | 
30  |  |  | 
31  |  | #define TAG FREERDP_TAG("childsession") | 
32  |  |  | 
33  |  | typedef struct  | 
34  |  | { | 
35  |  |   OVERLAPPED readOverlapped;  | 
36  |  |   HANDLE hFile;  | 
37  |  |   BOOL opInProgress;  | 
38  |  |   BOOL lastOpClosed;  | 
39  |  |   RingBuffer readBuffer;  | 
40  |  |   BOOL blocking;  | 
41  |  |   BYTE tmpReadBuffer[4096];  | 
42  |  |  | 
43  |  |   HANDLE readEvent;  | 
44  |  | } WINPR_BIO_NAMED;  | 
45  |  |  | 
46  |  | static int transport_bio_named_uninit(BIO* bio);  | 
47  |  |  | 
48  |  | static int transport_bio_named_write(BIO* bio, const char* buf, int size)  | 
49  | 0  | { | 
50  | 0  |   WINPR_ASSERT(bio);  | 
51  | 0  |   WINPR_ASSERT(buf);  | 
52  |  |  | 
53  | 0  |   WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);  | 
54  |  | 
  | 
55  | 0  |   if (!buf)  | 
56  | 0  |     return 0;  | 
57  |  |  | 
58  | 0  |   BIO_clear_flags(bio, BIO_FLAGS_WRITE);  | 
59  | 0  |   DWORD written = 0;  | 
60  |  | 
  | 
61  | 0  |   UINT64 start = GetTickCount64();  | 
62  | 0  |   BOOL ret = WriteFile(ptr->hFile, buf, size, &written, NULL);  | 
63  |  |   // winpr_HexDump(TAG, WLOG_DEBUG, buf, size);  | 
64  |  | 
  | 
65  | 0  |   if (!ret)  | 
66  | 0  |   { | 
67  | 0  |     WLog_VRB(TAG, "error or deferred");  | 
68  | 0  |     return 0;  | 
69  | 0  |   }  | 
70  |  |  | 
71  | 0  |   WLog_VRB(TAG, "(%d)=%d written=%d duration=%d", size, ret, written, GetTickCount64() - start);  | 
72  |  | 
  | 
73  | 0  |   if (written == 0)  | 
74  | 0  |   { | 
75  | 0  |     WLog_VRB(TAG, "closed on write");  | 
76  | 0  |     return 0;  | 
77  | 0  |   }  | 
78  |  |  | 
79  | 0  |   return written;  | 
80  | 0  | }  | 
81  |  |  | 
82  |  | static BOOL treatReadResult(WINPR_BIO_NAMED* ptr, DWORD readBytes)  | 
83  | 0  | { | 
84  | 0  |   WLog_VRB(TAG, "treatReadResult(readBytes=%" PRIu32 ")", readBytes);  | 
85  | 0  |   ptr->opInProgress = FALSE;  | 
86  | 0  |   if (readBytes == 0)  | 
87  | 0  |   { | 
88  | 0  |     WLog_VRB(TAG, "readBytes == 0");  | 
89  | 0  |     return TRUE;  | 
90  | 0  |   }  | 
91  |  |  | 
92  | 0  |   if (!ringbuffer_write(&ptr->readBuffer, ptr->tmpReadBuffer, readBytes))  | 
93  | 0  |   { | 
94  | 0  |     WLog_VRB(TAG, "ringbuffer_write()");  | 
95  | 0  |     return FALSE;  | 
96  | 0  |   }  | 
97  |  |  | 
98  | 0  |   return SetEvent(ptr->readEvent);  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | static BOOL doReadOp(WINPR_BIO_NAMED* ptr)  | 
102  | 0  | { | 
103  | 0  |   DWORD readBytes = 0;  | 
104  |  | 
  | 
105  | 0  |   if (!ResetEvent(ptr->readEvent))  | 
106  | 0  |     return FALSE;  | 
107  |  |  | 
108  | 0  |   ptr->opInProgress = TRUE;  | 
109  | 0  |   if (!ReadFile(ptr->hFile, ptr->tmpReadBuffer, sizeof(ptr->tmpReadBuffer), &readBytes,  | 
110  | 0  |                 &ptr->readOverlapped))  | 
111  | 0  |   { | 
112  | 0  |     DWORD error = GetLastError();  | 
113  | 0  |     switch (error)  | 
114  | 0  |     { | 
115  | 0  |       case ERROR_NO_DATA:  | 
116  | 0  |         WLog_VRB(TAG, "No Data, unexpected");  | 
117  | 0  |         return TRUE;  | 
118  | 0  |       case ERROR_IO_PENDING:  | 
119  | 0  |         WLog_VRB(TAG, "ERROR_IO_PENDING");  | 
120  | 0  |         return TRUE;  | 
121  | 0  |       case ERROR_BROKEN_PIPE:  | 
122  | 0  |         WLog_VRB(TAG, "broken pipe");  | 
123  | 0  |         ptr->lastOpClosed = TRUE;  | 
124  | 0  |         return TRUE;  | 
125  | 0  |       default:  | 
126  | 0  |         return FALSE;  | 
127  | 0  |     }  | 
128  | 0  |   }  | 
129  |  |  | 
130  | 0  |   return treatReadResult(ptr, readBytes);  | 
131  | 0  | }  | 
132  |  |  | 
133  |  | static int transport_bio_named_read(BIO* bio, char* buf, int size)  | 
134  | 0  | { | 
135  | 0  |   WINPR_ASSERT(bio);  | 
136  | 0  |   WINPR_ASSERT(buf);  | 
137  |  |  | 
138  | 0  |   WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);  | 
139  | 0  |   if (!buf)  | 
140  | 0  |     return 0;  | 
141  |  |  | 
142  | 0  |   BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ);  | 
143  |  | 
  | 
144  | 0  |   if (ptr->blocking)  | 
145  | 0  |   { | 
146  | 0  |     while (!ringbuffer_used(&ptr->readBuffer))  | 
147  | 0  |     { | 
148  | 0  |       if (ptr->lastOpClosed)  | 
149  | 0  |         return 0;  | 
150  |  |  | 
151  | 0  |       if (ptr->opInProgress)  | 
152  | 0  |       { | 
153  | 0  |         DWORD status = WaitForSingleObjectEx(ptr->readEvent, 500, TRUE);  | 
154  | 0  |         switch (status)  | 
155  | 0  |         { | 
156  | 0  |           case WAIT_TIMEOUT:  | 
157  | 0  |           case WAIT_IO_COMPLETION:  | 
158  | 0  |             continue;  | 
159  | 0  |           case WAIT_OBJECT_0:  | 
160  | 0  |             break;  | 
161  | 0  |           default:  | 
162  | 0  |             return -1;  | 
163  | 0  |         }  | 
164  |  |  | 
165  | 0  |         DWORD readBytes = 0;  | 
166  | 0  |         if (!GetOverlappedResult(ptr->hFile, &ptr->readOverlapped, &readBytes, FALSE))  | 
167  | 0  |         { | 
168  | 0  |           WLog_ERR(TAG, "GetOverlappedResult blocking(lastError=%" PRIu32 ")",  | 
169  | 0  |                    GetLastError());  | 
170  | 0  |           return -1;  | 
171  | 0  |         }  | 
172  |  |  | 
173  | 0  |         if (!treatReadResult(ptr, readBytes))  | 
174  | 0  |         { | 
175  | 0  |           WLog_ERR(TAG, "treatReadResult blocking");  | 
176  | 0  |           return -1;  | 
177  | 0  |         }  | 
178  | 0  |       }  | 
179  | 0  |     }  | 
180  | 0  |   }  | 
181  | 0  |   else  | 
182  | 0  |   { | 
183  | 0  |     if (ptr->opInProgress)  | 
184  | 0  |     { | 
185  | 0  |       DWORD status = WaitForSingleObject(ptr->readEvent, 0);  | 
186  | 0  |       switch (status)  | 
187  | 0  |       { | 
188  | 0  |         case WAIT_OBJECT_0:  | 
189  | 0  |           break;  | 
190  | 0  |         case WAIT_TIMEOUT:  | 
191  | 0  |           BIO_set_flags(bio, (BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ));  | 
192  | 0  |           return -1;  | 
193  | 0  |         default:  | 
194  | 0  |           WLog_ERR(TAG, "error WaitForSingleObject(readEvent)=0x%" PRIx32 "", status);  | 
195  | 0  |           return -1;  | 
196  | 0  |       }  | 
197  |  |  | 
198  | 0  |       DWORD readBytes = 0;  | 
199  | 0  |       if (!GetOverlappedResult(ptr->hFile, &ptr->readOverlapped, &readBytes, FALSE))  | 
200  | 0  |       { | 
201  | 0  |         WLog_ERR(TAG, "GetOverlappedResult non blocking(lastError=%" PRIu32 ")",  | 
202  | 0  |                  GetLastError());  | 
203  | 0  |         return -1;  | 
204  | 0  |       }  | 
205  |  |  | 
206  | 0  |       if (!treatReadResult(ptr, readBytes))  | 
207  | 0  |       { | 
208  | 0  |         WLog_ERR(TAG, "error treatReadResult non blocking");  | 
209  | 0  |         return -1;  | 
210  | 0  |       }  | 
211  | 0  |     }  | 
212  | 0  |   }  | 
213  |  |  | 
214  | 0  |   SSIZE_T ret = -1;  | 
215  | 0  |   if (size >= 0)  | 
216  | 0  |   { | 
217  | 0  |     size_t rsize = ringbuffer_used(&ptr->readBuffer);  | 
218  | 0  |     if (rsize <= SSIZE_MAX)  | 
219  | 0  |       ret = MIN(size, (SSIZE_T)rsize);  | 
220  | 0  |   }  | 
221  | 0  |   if ((size >= 0) && ret)  | 
222  | 0  |   { | 
223  | 0  |     DataChunk chunks[2] = { 0 }; | 
224  | 0  |     int nchunks = ringbuffer_peek(&ptr->readBuffer, chunks, ret);  | 
225  | 0  |     for (int i = 0; i < nchunks; i++)  | 
226  | 0  |     { | 
227  | 0  |       memcpy(buf, chunks[i].data, chunks[i].size);  | 
228  | 0  |       buf += chunks[i].size;  | 
229  | 0  |     }  | 
230  |  | 
  | 
231  | 0  |     ringbuffer_commit_read_bytes(&ptr->readBuffer, ret);  | 
232  |  | 
  | 
233  | 0  |     WLog_VRB(TAG, "(%d)=%d nchunks=%d", size, ret, nchunks);  | 
234  | 0  |   }  | 
235  |  | 
  | 
236  | 0  |   if (!ringbuffer_used(&ptr->readBuffer))  | 
237  | 0  |   { | 
238  | 0  |     if (!ptr->opInProgress && !doReadOp(ptr))  | 
239  | 0  |     { | 
240  | 0  |       WLog_ERR(TAG, "error rearming read");  | 
241  | 0  |       return -1;  | 
242  | 0  |     }  | 
243  | 0  |   }  | 
244  |  |  | 
245  | 0  |   if (ret <= 0)  | 
246  | 0  |     BIO_set_flags(bio, (BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ));  | 
247  |  | 
  | 
248  | 0  |   return ret;  | 
249  | 0  | }  | 
250  |  |  | 
251  |  | static int transport_bio_named_puts(BIO* bio, const char* str)  | 
252  | 0  | { | 
253  | 0  |   WINPR_ASSERT(bio);  | 
254  | 0  |   WINPR_ASSERT(str);  | 
255  |  |  | 
256  | 0  |   return transport_bio_named_write(bio, str, strlen(str));  | 
257  | 0  | }  | 
258  |  |  | 
259  |  | static int transport_bio_named_gets(BIO* bio, char* str, int size)  | 
260  | 0  | { | 
261  | 0  |   WINPR_ASSERT(bio);  | 
262  | 0  |   WINPR_ASSERT(str);  | 
263  |  |  | 
264  | 0  |   return transport_bio_named_read(bio, str, size);  | 
265  | 0  | }  | 
266  |  |  | 
267  |  | static long transport_bio_named_ctrl(BIO* bio, int cmd, long arg1, void* arg2)  | 
268  | 0  | { | 
269  | 0  |   WINPR_ASSERT(bio);  | 
270  |  |  | 
271  | 0  |   int status = -1;  | 
272  | 0  |   WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);  | 
273  |  | 
  | 
274  | 0  |   switch (cmd)  | 
275  | 0  |   { | 
276  | 0  |     case BIO_C_SET_SOCKET:  | 
277  | 0  |     case BIO_C_GET_SOCKET:  | 
278  | 0  |       return -1;  | 
279  | 0  |     case BIO_C_GET_EVENT:  | 
280  | 0  |       if (!BIO_get_init(bio) || !arg2)  | 
281  | 0  |         return 0;  | 
282  |  |  | 
283  | 0  |       *((HANDLE*)arg2) = ptr->readEvent;  | 
284  | 0  |       return 1;  | 
285  | 0  |     case BIO_C_SET_HANDLE:  | 
286  | 0  |       BIO_set_init(bio, 1);  | 
287  | 0  |       if (!BIO_get_init(bio) || !arg2)  | 
288  | 0  |         return 0;  | 
289  |  |  | 
290  | 0  |       ptr->hFile = (HANDLE)arg2;  | 
291  | 0  |       ptr->blocking = TRUE;  | 
292  | 0  |       if (!doReadOp(ptr))  | 
293  | 0  |         return -1;  | 
294  | 0  |       return 1;  | 
295  | 0  |     case BIO_C_SET_NONBLOCK:  | 
296  | 0  |     { | 
297  | 0  |       WLog_DBG(TAG, "BIO_C_SET_NONBLOCK");  | 
298  | 0  |       ptr->blocking = FALSE;  | 
299  | 0  |       return 1;  | 
300  | 0  |     }  | 
301  | 0  |     case BIO_C_WAIT_READ:  | 
302  | 0  |     { | 
303  | 0  |       WLog_DBG(TAG, "BIO_C_WAIT_READ");  | 
304  | 0  |       return 1;  | 
305  | 0  |     }  | 
306  |  |  | 
307  | 0  |     case BIO_C_WAIT_WRITE:  | 
308  | 0  |     { | 
309  | 0  |       WLog_DBG(TAG, "BIO_C_WAIT_WRITE");  | 
310  | 0  |       return 1;  | 
311  | 0  |     }  | 
312  |  |  | 
313  | 0  |     default:  | 
314  | 0  |       break;  | 
315  | 0  |   }  | 
316  |  |  | 
317  | 0  |   switch (cmd)  | 
318  | 0  |   { | 
319  | 0  |     case BIO_CTRL_GET_CLOSE:  | 
320  | 0  |       status = BIO_get_shutdown(bio);  | 
321  | 0  |       break;  | 
322  |  |  | 
323  | 0  |     case BIO_CTRL_SET_CLOSE:  | 
324  | 0  |       BIO_set_shutdown(bio, (int)arg1);  | 
325  | 0  |       status = 1;  | 
326  | 0  |       break;  | 
327  |  |  | 
328  | 0  |     case BIO_CTRL_DUP:  | 
329  | 0  |       status = 1;  | 
330  | 0  |       break;  | 
331  |  |  | 
332  | 0  |     case BIO_CTRL_FLUSH:  | 
333  | 0  |       status = 1;  | 
334  | 0  |       break;  | 
335  |  |  | 
336  | 0  |     default:  | 
337  | 0  |       status = 0;  | 
338  | 0  |       break;  | 
339  | 0  |   }  | 
340  |  |  | 
341  | 0  |   return status;  | 
342  | 0  | }  | 
343  |  |  | 
344  |  | static void BIO_NAMED_free(WINPR_BIO_NAMED* ptr)  | 
345  | 0  | { | 
346  | 0  |   if (!ptr)  | 
347  | 0  |     return;  | 
348  |  |  | 
349  | 0  |   if (ptr->hFile)  | 
350  | 0  |   { | 
351  | 0  |     CloseHandle(ptr->hFile);  | 
352  | 0  |     ptr->hFile = NULL;  | 
353  | 0  |   }  | 
354  |  | 
  | 
355  | 0  |   if (ptr->readEvent)  | 
356  | 0  |   { | 
357  | 0  |     CloseHandle(ptr->readEvent);  | 
358  | 0  |     ptr->readEvent = NULL;  | 
359  | 0  |   }  | 
360  |  | 
  | 
361  | 0  |   ringbuffer_destroy(&ptr->readBuffer);  | 
362  | 0  |   free(ptr);  | 
363  | 0  | }  | 
364  |  |  | 
365  |  | static int transport_bio_named_uninit(BIO* bio)  | 
366  | 0  | { | 
367  | 0  |   WINPR_ASSERT(bio);  | 
368  | 0  |   WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);  | 
369  |  | 
  | 
370  | 0  |   BIO_NAMED_free(ptr);  | 
371  |  | 
  | 
372  | 0  |   BIO_set_init(bio, 0);  | 
373  | 0  |   BIO_set_flags(bio, 0);  | 
374  | 0  |   return 1;  | 
375  | 0  | }  | 
376  |  |  | 
377  |  | static int transport_bio_named_new(BIO* bio)  | 
378  | 0  | { | 
379  | 0  |   WINPR_ASSERT(bio);  | 
380  |  |  | 
381  | 0  |   WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)calloc(1, sizeof(WINPR_BIO_NAMED));  | 
382  | 0  |   if (!ptr)  | 
383  | 0  |     return 0;  | 
384  |  |  | 
385  | 0  |   if (!ringbuffer_init(&ptr->readBuffer, 0xfffff))  | 
386  | 0  |     goto error;  | 
387  |  |  | 
388  | 0  |   ptr->readEvent = CreateEventA(NULL, TRUE, FALSE, NULL);  | 
389  | 0  |   if (!ptr->readEvent || ptr->readEvent == INVALID_HANDLE_VALUE)  | 
390  | 0  |     goto error;  | 
391  |  |  | 
392  | 0  |   ptr->readOverlapped.hEvent = ptr->readEvent;  | 
393  |  | 
  | 
394  | 0  |   BIO_set_data(bio, ptr);  | 
395  | 0  |   BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);  | 
396  | 0  |   return 1;  | 
397  |  |  | 
398  | 0  | error:  | 
399  | 0  |   BIO_NAMED_free(ptr);  | 
400  | 0  |   return 0;  | 
401  | 0  | }  | 
402  |  |  | 
403  |  | static int transport_bio_named_free(BIO* bio)  | 
404  | 0  | { | 
405  | 0  |   WINPR_BIO_NAMED* ptr = NULL;  | 
406  |  | 
  | 
407  | 0  |   if (!bio)  | 
408  | 0  |     return 0;  | 
409  |  |  | 
410  | 0  |   transport_bio_named_uninit(bio);  | 
411  |  | 
  | 
412  | 0  |   ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);  | 
413  | 0  |   if (ptr)  | 
414  | 0  |     BIO_set_data(bio, NULL);  | 
415  |  | 
  | 
416  | 0  |   return 1;  | 
417  | 0  | }  | 
418  |  |  | 
419  |  | static BIO_METHOD* BIO_s_namedpipe(void)  | 
420  | 0  | { | 
421  | 0  |   static BIO_METHOD* bio_methods = NULL;  | 
422  |  | 
  | 
423  | 0  |   if (bio_methods == NULL)  | 
424  | 0  |   { | 
425  | 0  |     if (!(bio_methods = BIO_meth_new(BIO_TYPE_NAMEDPIPE, "NamedPipe")))  | 
426  | 0  |       return NULL;  | 
427  |  |  | 
428  | 0  |     BIO_meth_set_write(bio_methods, transport_bio_named_write);  | 
429  | 0  |     BIO_meth_set_read(bio_methods, transport_bio_named_read);  | 
430  | 0  |     BIO_meth_set_puts(bio_methods, transport_bio_named_puts);  | 
431  | 0  |     BIO_meth_set_gets(bio_methods, transport_bio_named_gets);  | 
432  | 0  |     BIO_meth_set_ctrl(bio_methods, transport_bio_named_ctrl);  | 
433  | 0  |     BIO_meth_set_create(bio_methods, transport_bio_named_new);  | 
434  | 0  |     BIO_meth_set_destroy(bio_methods, transport_bio_named_free);  | 
435  | 0  |   }  | 
436  |  |  | 
437  | 0  |   return bio_methods;  | 
438  | 0  | }  | 
439  |  |  | 
440  |  | typedef NTSTATUS (*WinStationCreateChildSessionTransportFn)(WCHAR* path, DWORD len);  | 
441  |  | static BOOL createChildSessionTransport(HANDLE* pFile)  | 
442  | 0  | { | 
443  | 0  |   WINPR_ASSERT(pFile);  | 
444  |  |  | 
445  | 0  |   HANDLE hModule = NULL;  | 
446  | 0  |   BOOL ret = FALSE;  | 
447  | 0  |   *pFile = INVALID_HANDLE_VALUE;  | 
448  |  | 
  | 
449  | 0  |   BOOL childEnabled = 0;  | 
450  | 0  |   if (!WTSIsChildSessionsEnabled(&childEnabled))  | 
451  | 0  |   { | 
452  | 0  |     WLog_ERR(TAG, "error when calling WTSIsChildSessionsEnabled");  | 
453  | 0  |     goto out;  | 
454  | 0  |   }  | 
455  |  |  | 
456  | 0  |   if (!childEnabled)  | 
457  | 0  |   { | 
458  | 0  |     WLog_INFO(TAG, "child sessions aren't enabled");  | 
459  | 0  |     if (!WTSEnableChildSessions(TRUE))  | 
460  | 0  |     { | 
461  | 0  |       WLog_ERR(TAG, "error when calling WTSEnableChildSessions");  | 
462  | 0  |       goto out;  | 
463  | 0  |     }  | 
464  | 0  |     WLog_INFO(TAG, "successfully enabled child sessions");  | 
465  | 0  |   }  | 
466  |  |  | 
467  | 0  |   hModule = LoadLibraryA("winsta.dll"); | 
468  | 0  |   if (!hModule)  | 
469  | 0  |     return FALSE;  | 
470  | 0  |   WCHAR pipePath[0x80] = { 0 }; | 
471  | 0  |   char pipePathA[0x80] = { 0 }; | 
472  |  | 
  | 
473  | 0  |   WinStationCreateChildSessionTransportFn createChildSessionFn = GetProcAddressAs(  | 
474  | 0  |       hModule, "WinStationCreateChildSessionTransport", WinStationCreateChildSessionTransportFn);  | 
475  | 0  |   if (!createChildSessionFn)  | 
476  | 0  |   { | 
477  | 0  |     WLog_ERR(TAG, "unable to retrieve WinStationCreateChildSessionTransport function");  | 
478  | 0  |     goto out;  | 
479  | 0  |   }  | 
480  |  |  | 
481  | 0  |   HRESULT hStatus = createChildSessionFn(pipePath, 0x80);  | 
482  | 0  |   if (!SUCCEEDED(hStatus))  | 
483  | 0  |   { | 
484  | 0  |     WLog_ERR(TAG, "error 0x%x when creating childSessionTransport", hStatus);  | 
485  | 0  |     goto out;  | 
486  | 0  |   }  | 
487  |  |  | 
488  | 0  |   const BYTE startOfPath[] = { '\\', 0, '\\', 0, '.', 0, '\\', 0 }; | 
489  | 0  |   if (_wcsncmp(pipePath, (WCHAR*)startOfPath, 4))  | 
490  | 0  |   { | 
491  |  |     /* when compiled under 32 bits, the path may miss "\\.\" at the beginning of the string  | 
492  |  |      * so add it if it's not there  | 
493  |  |      */  | 
494  | 0  |     size_t len = _wcslen(pipePath);  | 
495  | 0  |     if (len > 0x80 - (4 + 1))  | 
496  | 0  |     { | 
497  | 0  |       WLog_ERR(TAG, "pipePath is too long to be adjusted");  | 
498  | 0  |       goto out;  | 
499  | 0  |     }  | 
500  |  |  | 
501  | 0  |     memmove(pipePath + 4, pipePath, (len + 1) * sizeof(WCHAR));  | 
502  | 0  |     memcpy(pipePath, startOfPath, 8);  | 
503  | 0  |   }  | 
504  |  |  | 
505  | 0  |   ConvertWCharNToUtf8(pipePath, 0x80, pipePathA, sizeof(pipePathA));  | 
506  | 0  |   WLog_DBG(TAG, "child session is at '%s'", pipePathA);  | 
507  |  | 
  | 
508  | 0  |   HANDLE f = CreateFileW(pipePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,  | 
509  | 0  |                          FILE_FLAG_OVERLAPPED, NULL);  | 
510  | 0  |   if (f == INVALID_HANDLE_VALUE)  | 
511  | 0  |   { | 
512  | 0  |     WLog_ERR(TAG, "error when connecting to local named pipe");  | 
513  | 0  |     goto out;  | 
514  | 0  |   }  | 
515  |  |  | 
516  | 0  |   *pFile = f;  | 
517  | 0  |   ret = TRUE;  | 
518  |  | 
  | 
519  | 0  | out:  | 
520  | 0  |   FreeLibrary(hModule);  | 
521  | 0  |   return ret;  | 
522  | 0  | }  | 
523  |  |  | 
524  |  | BIO* createChildSessionBio(void)  | 
525  | 0  | { | 
526  | 0  |   HANDLE f = INVALID_HANDLE_VALUE;  | 
527  | 0  |   if (!createChildSessionTransport(&f))  | 
528  | 0  |     return NULL;  | 
529  |  |  | 
530  | 0  |   BIO* lowLevelBio = BIO_new(BIO_s_namedpipe());  | 
531  | 0  |   if (!lowLevelBio)  | 
532  | 0  |   { | 
533  | 0  |     CloseHandle(f);  | 
534  | 0  |     return NULL;  | 
535  | 0  |   }  | 
536  |  |  | 
537  | 0  |   BIO_set_handle(lowLevelBio, f);  | 
538  | 0  |   BIO* bufferedBio = BIO_new(BIO_s_buffered_socket());  | 
539  |  | 
  | 
540  | 0  |   if (!bufferedBio)  | 
541  | 0  |   { | 
542  | 0  |     BIO_free_all(lowLevelBio);  | 
543  | 0  |     return FALSE;  | 
544  | 0  |   }  | 
545  |  |  | 
546  | 0  |   bufferedBio = BIO_push(bufferedBio, lowLevelBio);  | 
547  |  | 
  | 
548  | 0  |   return bufferedBio;  | 
549  | 0  | }  |