rpcrt4: Don't declare functions that aren't exported by rpcrt4.dll in include/rpcndr.h.
[wine] / dlls / rpcrt4 / rpcrt4_main.c
1 /*
2  *  RPCRT4
3  *
4  * Copyright 2000 Huw D M Davies for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  * 
20  * WINE RPC TODO's (and a few TODONT's)
21  *
22  * - Statistics: we are supposed to be keeping various counters.  we aren't.
23  *
24  * - Async RPC: Unimplemented.
25  *
26  * - The NT "ports" API, aka LPC.  Greg claims this is on his radar.  Might (or
27  *   might not) enable users to get some kind of meaningful result out of
28  *   NT-based native rpcrt4's.  Commonly-used transport for self-to-self RPC's.
29  */
30
31 #include "config.h"
32
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "ntstatus.h"
39 #define WIN32_NO_STATUS
40 #include "windef.h"
41 #include "winerror.h"
42 #include "winbase.h"
43 #include "winuser.h"
44 #include "winnt.h"
45 #include "winternl.h"
46 #include "ntsecapi.h"
47 #include "wine/unicode.h"
48 #include "rpc.h"
49
50 #include "ole2.h"
51 #include "rpcndr.h"
52 #include "rpcproxy.h"
53
54 #include "rpc_binding.h"
55
56 #include "wine/debug.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
59
60 static UUID uuid_nil;
61
62 static CRITICAL_SECTION threaddata_cs;
63 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
64 {
65     0, 0, &threaddata_cs,
66     { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
67       0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
68 };
69 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
70
71 static struct list threaddata_list = LIST_INIT(threaddata_list);
72
73 struct context_handle_list
74 {
75     struct context_handle_list *next;
76     NDR_SCONTEXT context_handle;
77 };
78
79 struct threaddata
80 {
81     struct list entry;
82     CRITICAL_SECTION cs;
83     DWORD thread_id;
84     RpcConnection *connection;
85     RpcBinding *server_binding;
86     struct context_handle_list *context_handle_list;
87 };
88
89 /***********************************************************************
90  * DllMain
91  *
92  * PARAMS
93  *     hinstDLL    [I] handle to the DLL's instance
94  *     fdwReason   [I]
95  *     lpvReserved [I] reserved, must be NULL
96  *
97  * RETURNS
98  *     Success: TRUE
99  *     Failure: FALSE
100  */
101
102 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
103 {
104     struct threaddata *tdata;
105
106     switch (fdwReason) {
107     case DLL_PROCESS_ATTACH:
108         break;
109
110     case DLL_THREAD_DETACH:
111         tdata = NtCurrentTeb()->ReservedForNtRpc;
112         if (tdata)
113         {
114             EnterCriticalSection(&threaddata_cs);
115             list_remove(&tdata->entry);
116             LeaveCriticalSection(&threaddata_cs);
117
118             DeleteCriticalSection(&tdata->cs);
119             if (tdata->connection)
120                 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
121             if (tdata->server_binding)
122                 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
123             HeapFree(GetProcessHeap(), 0, tdata);
124         }
125         break;
126
127     case DLL_PROCESS_DETACH:
128         break;
129     }
130
131     return TRUE;
132 }
133
134 /*************************************************************************
135  *           RpcStringFreeA   [RPCRT4.@]
136  *
137  * Frees a character string allocated by the RPC run-time library.
138  *
139  * RETURNS
140  *
141  *  S_OK if successful.
142  */
143 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
144 {
145   HeapFree( GetProcessHeap(), 0, *String);
146
147   return RPC_S_OK;
148 }
149
150 /*************************************************************************
151  *           RpcStringFreeW   [RPCRT4.@]
152  *
153  * Frees a character string allocated by the RPC run-time library.
154  *
155  * RETURNS
156  *
157  *  S_OK if successful.
158  */
159 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
160 {
161   HeapFree( GetProcessHeap(), 0, *String);
162
163   return RPC_S_OK;
164 }
165
166 /*************************************************************************
167  *           RpcRaiseException   [RPCRT4.@]
168  *
169  * Raises an exception.
170  */
171 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
172 {
173   /* shouldn't return */
174   RaiseException(exception, 0, 0, NULL);
175   ERR("handler continued execution\n");
176   ExitProcess(1);
177 }
178
179 /*************************************************************************
180  * UuidCompare [RPCRT4.@]
181  *
182  * PARAMS
183  *     UUID *Uuid1        [I] Uuid to compare
184  *     UUID *Uuid2        [I] Uuid to compare
185  *     RPC_STATUS *Status [O] returns RPC_S_OK
186  * 
187  * RETURNS
188  *    -1  if Uuid1 is less than Uuid2
189  *     0  if Uuid1 and Uuid2 are equal
190  *     1  if Uuid1 is greater than Uuid2
191  */
192 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
193 {
194   int i;
195
196   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
197
198   *Status = RPC_S_OK;
199
200   if (!Uuid1) Uuid1 = &uuid_nil;
201   if (!Uuid2) Uuid2 = &uuid_nil;
202
203   if (Uuid1 == Uuid2) return 0;
204
205   if (Uuid1->Data1 != Uuid2->Data1)
206     return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
207
208   if (Uuid1->Data2 != Uuid2->Data2)
209     return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
210
211   if (Uuid1->Data3 != Uuid2->Data3)
212     return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
213
214   for (i = 0; i < 8; i++) {
215     if (Uuid1->Data4[i] < Uuid2->Data4[i])
216       return -1;
217     if (Uuid1->Data4[i] > Uuid2->Data4[i])
218       return 1;
219   }
220
221   return 0;
222 }
223
224 /*************************************************************************
225  * UuidEqual [RPCRT4.@]
226  *
227  * PARAMS
228  *     UUID *Uuid1        [I] Uuid to compare
229  *     UUID *Uuid2        [I] Uuid to compare
230  *     RPC_STATUS *Status [O] returns RPC_S_OK
231  *
232  * RETURNS
233  *     TRUE/FALSE
234  */
235 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
236 {
237   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
238   return !UuidCompare(Uuid1, Uuid2, Status);
239 }
240
241 /*************************************************************************
242  * UuidIsNil [RPCRT4.@]
243  *
244  * PARAMS
245  *     UUID *Uuid         [I] Uuid to compare
246  *     RPC_STATUS *Status [O] returns RPC_S_OK
247  *
248  * RETURNS
249  *     TRUE/FALSE
250  */
251 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
252 {
253   TRACE("(%s)\n", debugstr_guid(Uuid));
254   if (!Uuid) return TRUE;
255   return !UuidCompare(Uuid, &uuid_nil, Status);
256 }
257
258  /*************************************************************************
259  * UuidCreateNil [RPCRT4.@]
260  *
261  * PARAMS
262  *     UUID *Uuid [O] returns a nil UUID
263  *
264  * RETURNS
265  *     RPC_S_OK
266  */
267 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
268 {
269   *Uuid = uuid_nil;
270   return RPC_S_OK;
271 }
272
273 /*************************************************************************
274  *           UuidCreate   [RPCRT4.@]
275  *
276  * Creates a 128bit UUID.
277  *
278  * RETURNS
279  *
280  *  RPC_S_OK if successful.
281  *  RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
282  *
283  * NOTES
284  *
285  *  Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
286  *  Truly Random or Pseudo-Random Numbers)
287  */
288 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
289 {
290     RtlGenRandom(Uuid, sizeof(*Uuid));
291     /* Clear the version bits and set the version (4) */
292     Uuid->Data3 &= 0x0fff;
293     Uuid->Data3 |= (4 << 12);
294     /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
295      * specified in RFC 4122, section 4.4.
296      */
297     Uuid->Data4[0] &= 0x3f;
298     Uuid->Data4[0] |= 0x80;
299
300     TRACE("%s\n", debugstr_guid(Uuid));
301
302     return RPC_S_OK;
303 }
304
305 /*************************************************************************
306  *           UuidCreateSequential   [RPCRT4.@]
307  *
308  * Creates a 128bit UUID.
309  *
310  * RETURNS
311  *
312  *  RPC_S_OK if successful.
313  *  RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
314  *
315  */
316 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
317 {
318    return UuidCreate(Uuid);
319 }
320
321
322 /*************************************************************************
323  *           UuidHash   [RPCRT4.@]
324  *
325  * Generates a hash value for a given UUID
326  *
327  * Code based on FreeDCE implementation
328  *
329  */
330 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
331 {
332   BYTE *data = (BYTE*)uuid;
333   short c0 = 0, c1 = 0, x, y;
334   unsigned int i;
335
336   if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
337
338   TRACE("(%s)\n", debugstr_guid(uuid));
339
340   for (i=0; i<sizeof(UUID); i++) {
341     c0 += data[i];
342     c1 += c0;
343   }
344
345   x = -c1 % 255;
346   if (x < 0) x += 255;
347
348   y = (c1 - c0) % 255;
349   if (y < 0) y += 255;
350
351   *Status = RPC_S_OK;
352   return y*256 + x;
353 }
354
355 /*************************************************************************
356  *           UuidToStringA   [RPCRT4.@]
357  *
358  * Converts a UUID to a string.
359  *
360  * UUID format is 8 hex digits, followed by a hyphen then three groups of
361  * 4 hex digits each followed by a hyphen and then 12 hex digits
362  *
363  * RETURNS
364  *
365  *  S_OK if successful.
366  *  S_OUT_OF_MEMORY if unsuccessful.
367  */
368 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
369 {
370   *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
371
372   if(!(*StringUuid))
373     return RPC_S_OUT_OF_MEMORY;
374
375   if (!Uuid) Uuid = &uuid_nil;
376
377   sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
378                  Uuid->Data1, Uuid->Data2, Uuid->Data3,
379                  Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
380                  Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
381                  Uuid->Data4[6], Uuid->Data4[7] );
382
383   return RPC_S_OK;
384 }
385
386 /*************************************************************************
387  *           UuidToStringW   [RPCRT4.@]
388  *
389  * Converts a UUID to a string.
390  *
391  *  S_OK if successful.
392  *  S_OUT_OF_MEMORY if unsuccessful.
393  */
394 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
395 {
396   char buf[37];
397
398   if (!Uuid) Uuid = &uuid_nil;
399
400   sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
401                Uuid->Data1, Uuid->Data2, Uuid->Data3,
402                Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
403                Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
404                Uuid->Data4[6], Uuid->Data4[7] );
405
406   *StringUuid = RPCRT4_strdupAtoW(buf);
407
408   if(!(*StringUuid))
409     return RPC_S_OUT_OF_MEMORY;
410
411   return RPC_S_OK;
412 }
413
414 static const BYTE hex2bin[] =
415 {
416     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x00 */
417     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x10 */
418     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x20 */
419     0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,        /* 0x30 */
420     0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,  /* 0x40 */
421     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x50 */
422     0,10,11,12,13,14,15                     /* 0x60 */
423 };
424
425 /***********************************************************************
426  *              UuidFromStringA (RPCRT4.@)
427  */
428 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
429 {
430     int i;
431
432     if (!s) return UuidCreateNil( uuid );
433
434     if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
435
436     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
437         return RPC_S_INVALID_STRING_UUID;
438
439     for (i=0; i<36; i++)
440     {
441         if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
442         if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
443     }
444
445     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
446
447     uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
448                    hex2bin[s[4]] << 12 | hex2bin[s[5]]  << 8 | hex2bin[s[6]]  << 4 | hex2bin[s[7]]);
449     uuid->Data2 =  hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
450     uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
451
452     /* these are just sequential bytes */
453     uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
454     uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
455     uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
456     uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
457     uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
458     uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
459     uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
460     uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
461     return RPC_S_OK;
462 }
463
464
465 /***********************************************************************
466  *              UuidFromStringW (RPCRT4.@)
467  */
468 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
469 {
470     int i;
471
472     if (!s) return UuidCreateNil( uuid );
473
474     if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
475
476     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
477         return RPC_S_INVALID_STRING_UUID;
478
479     for (i=0; i<36; i++)
480     {
481         if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
482         if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
483     }
484
485     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
486
487     uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
488                    hex2bin[s[4]] << 12 | hex2bin[s[5]]  << 8 | hex2bin[s[6]]  << 4 | hex2bin[s[7]]);
489     uuid->Data2 =  hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
490     uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
491
492     /* these are just sequential bytes */
493     uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
494     uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
495     uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
496     uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
497     uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
498     uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
499     uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
500     uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
501     return RPC_S_OK;
502 }
503
504 /***********************************************************************
505  *              DllRegisterServer (RPCRT4.@)
506  */
507
508 HRESULT WINAPI DllRegisterServer( void )
509 {
510     FIXME( "(): stub\n" );
511     return S_OK;
512 }
513
514 #define MAX_RPC_ERROR_TEXT 256
515
516 /******************************************************************************
517  * DceErrorInqTextW   (rpcrt4.@)
518  *
519  * Notes
520  * 1. On passing a NULL pointer the code does bomb out.
521  * 2. The size of the required buffer is not defined in the documentation.
522  *    It appears to be 256.
523  * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
524  *    of any value for which it does.
525  * 4. The MSDN documentation currently declares that the second argument is
526  *    unsigned char *, even for the W version.  I don't believe it.
527  */
528 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
529 {
530     DWORD count;
531     count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
532                 FORMAT_MESSAGE_IGNORE_INSERTS,
533                 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
534     if (!count)
535     {
536         count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
537                 FORMAT_MESSAGE_IGNORE_INSERTS,
538                 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
539         if (!count)
540         {
541             ERR ("Failed to translate error\n");
542             return RPC_S_INVALID_ARG;
543         }
544     }
545     return RPC_S_OK;
546 }
547
548 /******************************************************************************
549  * DceErrorInqTextA   (rpcrt4.@)
550  */
551 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
552 {
553     RPC_STATUS status;
554     WCHAR bufferW [MAX_RPC_ERROR_TEXT];
555     if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
556     {
557         if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
558                 NULL, NULL))
559         {
560             ERR ("Failed to translate error\n");
561             status = RPC_S_INVALID_ARG;
562         }
563     }
564     return status;
565 }
566
567 /******************************************************************************
568  * I_RpcAllocate   (rpcrt4.@)
569  */
570 void * WINAPI I_RpcAllocate(unsigned int Size)
571 {
572     return HeapAlloc(GetProcessHeap(), 0, Size);
573 }
574
575 /******************************************************************************
576  * I_RpcFree   (rpcrt4.@)
577  */
578 void WINAPI I_RpcFree(void *Object)
579 {
580     HeapFree(GetProcessHeap(), 0, Object);
581 }
582
583 /******************************************************************************
584  * I_RpcMapWin32Status   (rpcrt4.@)
585  *
586  * Maps Win32 RPC error codes to NT statuses.
587  *
588  * PARAMS
589  *  status [I] Win32 RPC error code.
590  *
591  * RETURNS
592  *  Appropriate translation into an NT status code.
593  */
594 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
595 {
596     TRACE("(%d)\n", status);
597     switch (status)
598     {
599     case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
600     case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
601     case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
602     case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
603     case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
604     case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
605     case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
606     case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
607     case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
608     case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
609     case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
610     case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
611     case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
612     case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
613     case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
614     case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
615     case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
616     case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
617     case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
618     case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
619     case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
620     case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
621     case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
622     case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
623     case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
624     case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
625     case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
626     case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
627     case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
628     case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
629     case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
630     case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
631     case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
632     case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
633     case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
634     case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
635     case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
636     case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
637     case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
638     case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
639     case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
640     case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
641     case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
642     case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
643     case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
644     case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
645     case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
646     case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
647     case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
648     case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
649     case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
650     case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
651     case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
652     case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
653     case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
654     case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
655     case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
656     case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
657     case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
658     case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
659     case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
660     case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
661     case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
662     case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
663     case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
664     case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
665     case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
666     case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
667     case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
668     case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
669     case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
670     case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
671     case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
672     case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
673     case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
674     case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
675     case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
676     case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
677     case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
678     case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
679     case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
680     case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
681     case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
682     case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
683     case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
684     case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
685     case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
686     case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
687     case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
688     case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
689     case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
690     case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
691     case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
692     case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
693     case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
694     case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
695     case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
696     case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
697     case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
698     case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
699     case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
700     case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
701     case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
702     case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
703     case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
704     case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
705     default: return status;
706     }
707 }
708
709 /******************************************************************************
710  * I_RpcExceptionFilter   (rpcrt4.@)
711  */
712 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
713 {
714     TRACE("0x%x\n", ExceptionCode);
715     switch (ExceptionCode)
716     {
717     case STATUS_DATATYPE_MISALIGNMENT:
718     case STATUS_BREAKPOINT:
719     case STATUS_ACCESS_VIOLATION:
720     case STATUS_ILLEGAL_INSTRUCTION:
721     case STATUS_PRIVILEGED_INSTRUCTION:
722     case STATUS_INSTRUCTION_MISALIGNMENT:
723     case STATUS_STACK_OVERFLOW:
724     case STATUS_POSSIBLE_DEADLOCK:
725         return EXCEPTION_CONTINUE_SEARCH;
726     default:
727         return EXCEPTION_EXECUTE_HANDLER;
728     }
729 }
730
731 /******************************************************************************
732  * RpcErrorStartEnumeration   (rpcrt4.@)
733  */
734 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
735 {
736     FIXME("(%p): stub\n", EnumHandle);
737     return RPC_S_ENTRY_NOT_FOUND;
738 }
739
740 /******************************************************************************
741  * RpcMgmtSetCancelTimeout   (rpcrt4.@)
742  */
743 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
744 {
745     FIXME("(%d): stub\n", Timeout);
746     return RPC_S_OK;
747 }
748
749 static struct threaddata *get_or_create_threaddata(void)
750 {
751     struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
752     if (!tdata)
753     {
754         tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
755         if (!tdata) return NULL;
756
757         InitializeCriticalSection(&tdata->cs);
758         tdata->thread_id = GetCurrentThreadId();
759
760         EnterCriticalSection(&threaddata_cs);
761         list_add_tail(&threaddata_list, &tdata->entry);
762         LeaveCriticalSection(&threaddata_cs);
763
764         NtCurrentTeb()->ReservedForNtRpc = tdata;
765         return tdata;
766     }
767     return tdata;
768 }
769
770 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
771 {
772     struct threaddata *tdata = get_or_create_threaddata();
773     if (!tdata) return;
774
775     EnterCriticalSection(&tdata->cs);
776     tdata->connection = Connection;
777     LeaveCriticalSection(&tdata->cs);
778 }
779
780 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
781 {
782     struct threaddata *tdata = get_or_create_threaddata();
783     if (!tdata) return;
784
785     tdata->server_binding = Binding;
786 }
787
788 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
789 {
790     struct threaddata *tdata = get_or_create_threaddata();
791     if (!tdata) return NULL;
792
793     return tdata->server_binding;
794 }
795
796 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
797 {
798     struct threaddata *tdata = get_or_create_threaddata();
799     struct context_handle_list *context_handle_list;
800
801     if (!tdata) return;
802
803     context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
804     if (!context_handle_list) return;
805
806     context_handle_list->context_handle = SContext;
807     context_handle_list->next = tdata->context_handle_list;
808     tdata->context_handle_list = context_handle_list;
809 }
810
811 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
812 {
813     struct threaddata *tdata = get_or_create_threaddata();
814     struct context_handle_list *current, *prev;
815
816     if (!tdata) return;
817
818     for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
819     {
820         if (current->context_handle == SContext)
821         {
822             if (prev)
823                 prev->next = current->next;
824             else
825                 tdata->context_handle_list = current->next;
826             HeapFree(GetProcessHeap(), 0, current);
827             return;
828         }
829     }
830 }
831
832 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
833 {
834     struct threaddata *tdata = get_or_create_threaddata();
835     struct context_handle_list *context_handle_list;
836     NDR_SCONTEXT context_handle;
837
838     if (!tdata) return NULL;
839
840     context_handle_list = tdata->context_handle_list;
841     if (!context_handle_list) return NULL;
842     tdata->context_handle_list = context_handle_list->next;
843
844     context_handle = context_handle_list->context_handle;
845     HeapFree(GetProcessHeap(), 0, context_handle_list);
846     return context_handle;
847 }
848
849 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
850 {
851     struct threaddata *tdata;
852
853     EnterCriticalSection(&threaddata_cs);
854     LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
855         if (tdata->thread_id == target_tid)
856         {
857             EnterCriticalSection(&tdata->cs);
858             if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
859             LeaveCriticalSection(&tdata->cs);
860             break;
861         }
862     LeaveCriticalSection(&threaddata_cs);
863
864     return RPC_S_OK;
865 }
866
867 /******************************************************************************
868  * RpcCancelThread   (rpcrt4.@)
869  */
870 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
871 {
872     TRACE("(%p)\n", ThreadHandle);
873     return RpcCancelThreadEx(ThreadHandle, 0);
874 }
875
876 /******************************************************************************
877  * RpcCancelThreadEx   (rpcrt4.@)
878  */
879 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
880 {
881     DWORD target_tid;
882
883     FIXME("(%p, %d)\n", ThreadHandle, Timeout);
884
885     target_tid = GetThreadId(ThreadHandle);
886     if (!target_tid)
887         return RPC_S_INVALID_ARG;
888
889     if (Timeout)
890     {
891         FIXME("(%p, %d)\n", ThreadHandle, Timeout);
892         return RPC_S_OK;
893     }
894     else
895         return rpc_cancel_thread(target_tid);
896 }