rpcrt4: Validate the uuid portion of the string passed to RpcStringBindingParseA/W.
[wine] / dlls / rpcrt4 / rpc_binding.c
1 /*
2  * RPC binding API
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  * Copyright 2003 Mike Hearn
6  * Copyright 2004 Filip Navara
7  * Copyright 2006 CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32 #include "winerror.h"
33 #include "winternl.h"
34 #include "wine/unicode.h"
35
36 #include "rpc.h"
37 #include "rpcndr.h"
38
39 #include "wine/debug.h"
40
41 #include "rpc_binding.h"
42 #include "rpc_assoc.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
45
46 LPSTR RPCRT4_strndupA(LPCSTR src, INT slen)
47 {
48   DWORD len;
49   LPSTR s;
50   if (!src) return NULL;
51   if (slen == -1) slen = strlen(src);
52   len = slen;
53   s = HeapAlloc(GetProcessHeap(), 0, len+1);
54   memcpy(s, src, len);
55   s[len] = 0;
56   return s;
57 }
58
59 LPSTR RPCRT4_strdupWtoA(LPCWSTR src)
60 {
61   DWORD len;
62   LPSTR s;
63   if (!src) return NULL;
64   len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
65   s = HeapAlloc(GetProcessHeap(), 0, len);
66   WideCharToMultiByte(CP_ACP, 0, src, -1, s, len, NULL, NULL);
67   return s;
68 }
69
70 LPWSTR RPCRT4_strdupAtoW(LPCSTR src)
71 {
72   DWORD len;
73   LPWSTR s;
74   if (!src) return NULL;
75   len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
76   s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
77   MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
78   return s;
79 }
80
81 static LPWSTR RPCRT4_strndupAtoW(LPCSTR src, INT slen)
82 {
83   DWORD len;
84   LPWSTR s;
85   if (!src) return NULL;
86   len = MultiByteToWideChar(CP_ACP, 0, src, slen, NULL, 0);
87   s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
88   MultiByteToWideChar(CP_ACP, 0, src, slen, s, len);
89   return s;
90 }
91
92 LPWSTR RPCRT4_strndupW(LPCWSTR src, INT slen)
93 {
94   DWORD len;
95   LPWSTR s;
96   if (!src) return NULL;
97   if (slen == -1) slen = strlenW(src);
98   len = slen;
99   s = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
100   memcpy(s, src, len*sizeof(WCHAR));
101   s[len] = 0;
102   return s;
103 }
104
105 void RPCRT4_strfree(LPSTR src)
106 {
107   HeapFree(GetProcessHeap(), 0, src);
108 }
109
110 static RPC_STATUS RPCRT4_AllocBinding(RpcBinding** Binding, BOOL server)
111 {
112   RpcBinding* NewBinding;
113
114   NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
115   NewBinding->refs = 1;
116   NewBinding->server = server;
117
118   *Binding = NewBinding;
119
120   return RPC_S_OK;
121 }
122
123 static RPC_STATUS RPCRT4_CreateBindingA(RpcBinding** Binding, BOOL server, LPCSTR Protseq)
124 {
125   RpcBinding* NewBinding;
126
127   RPCRT4_AllocBinding(&NewBinding, server);
128   NewBinding->Protseq = RPCRT4_strdupA(Protseq);
129
130   TRACE("binding: %p\n", NewBinding);
131   *Binding = NewBinding;
132
133   return RPC_S_OK;
134 }
135
136 static RPC_STATUS RPCRT4_CreateBindingW(RpcBinding** Binding, BOOL server, LPCWSTR Protseq)
137 {
138   RpcBinding* NewBinding;
139
140   RPCRT4_AllocBinding(&NewBinding, server);
141   NewBinding->Protseq = RPCRT4_strdupWtoA(Protseq);
142
143   TRACE("binding: %p\n", NewBinding);
144   *Binding = NewBinding;
145
146   return RPC_S_OK;
147 }
148
149 static RPC_STATUS RPCRT4_CompleteBindingA(RpcBinding* Binding, LPCSTR NetworkAddr,
150                                           LPCSTR Endpoint, LPCSTR NetworkOptions)
151 {
152   RPC_STATUS status;
153
154   TRACE("(RpcBinding == ^%p, NetworkAddr == %s, EndPoint == %s, NetworkOptions == %s)\n", Binding,
155    debugstr_a(NetworkAddr), debugstr_a(Endpoint), debugstr_a(NetworkOptions));
156
157   RPCRT4_strfree(Binding->NetworkAddr);
158   Binding->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
159   RPCRT4_strfree(Binding->Endpoint);
160   if (Endpoint) {
161     Binding->Endpoint = RPCRT4_strdupA(Endpoint);
162   } else {
163     Binding->Endpoint = RPCRT4_strdupA("");
164   }
165   HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
166   Binding->NetworkOptions = RPCRT4_strdupAtoW(NetworkOptions);
167   if (!Binding->Endpoint) ERR("out of memory?\n");
168
169   status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
170                                  Binding->Endpoint, Binding->NetworkOptions,
171                                  &Binding->Assoc);
172   if (status != RPC_S_OK)
173       return status;
174
175   return RPC_S_OK;
176 }
177
178 static RPC_STATUS RPCRT4_CompleteBindingW(RpcBinding* Binding, LPCWSTR NetworkAddr,
179                                           LPCWSTR Endpoint, LPCWSTR NetworkOptions)
180 {
181   RPC_STATUS status;
182
183   TRACE("(RpcBinding == ^%p, NetworkAddr == %s, EndPoint == %s, NetworkOptions == %s)\n", Binding, 
184    debugstr_w(NetworkAddr), debugstr_w(Endpoint), debugstr_w(NetworkOptions));
185
186   RPCRT4_strfree(Binding->NetworkAddr);
187   Binding->NetworkAddr = RPCRT4_strdupWtoA(NetworkAddr);
188   RPCRT4_strfree(Binding->Endpoint);
189   if (Endpoint) {
190     Binding->Endpoint = RPCRT4_strdupWtoA(Endpoint);
191   } else {
192     Binding->Endpoint = RPCRT4_strdupA("");
193   }
194   if (!Binding->Endpoint) ERR("out of memory?\n");
195   HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
196   Binding->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
197
198   status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
199                                  Binding->Endpoint, Binding->NetworkOptions,
200                                  &Binding->Assoc);
201   if (status != RPC_S_OK)
202       return status;
203
204   return RPC_S_OK;
205 }
206
207 RPC_STATUS RPCRT4_ResolveBinding(RpcBinding* Binding, LPCSTR Endpoint)
208 {
209   RPC_STATUS status;
210
211   TRACE("(RpcBinding == ^%p, EndPoint == \"%s\"\n", Binding, Endpoint);
212
213   RPCRT4_strfree(Binding->Endpoint);
214   Binding->Endpoint = RPCRT4_strdupA(Endpoint);
215
216   RpcAssoc_Release(Binding->Assoc);
217   Binding->Assoc = NULL;
218   status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
219                                  Binding->Endpoint, Binding->NetworkOptions,
220                                  &Binding->Assoc);
221   if (status != RPC_S_OK)
222       return status;
223
224   return RPC_S_OK;
225 }
226
227 RPC_STATUS RPCRT4_SetBindingObject(RpcBinding* Binding, const UUID* ObjectUuid)
228 {
229   TRACE("(*RpcBinding == ^%p, UUID == %s)\n", Binding, debugstr_guid(ObjectUuid)); 
230   if (ObjectUuid) Binding->ObjectUuid = *ObjectUuid;
231   else UuidCreateNil(&Binding->ObjectUuid);
232   return RPC_S_OK;
233 }
234
235 RPC_STATUS RPCRT4_MakeBinding(RpcBinding** Binding, RpcConnection* Connection)
236 {
237   RpcBinding* NewBinding;
238   TRACE("(RpcBinding == ^%p, Connection == ^%p)\n", Binding, Connection);
239
240   RPCRT4_AllocBinding(&NewBinding, Connection->server);
241   NewBinding->Protseq = RPCRT4_strdupA(rpcrt4_conn_get_name(Connection));
242   NewBinding->NetworkAddr = RPCRT4_strdupA(Connection->NetworkAddr);
243   NewBinding->Endpoint = RPCRT4_strdupA(Connection->Endpoint);
244   NewBinding->FromConn = Connection;
245
246   TRACE("binding: %p\n", NewBinding);
247   *Binding = NewBinding;
248
249   return RPC_S_OK;
250 }
251
252 void RPCRT4_AddRefBinding(RpcBinding* Binding)
253 {
254   InterlockedIncrement(&Binding->refs);
255 }
256
257 RPC_STATUS RPCRT4_ReleaseBinding(RpcBinding* Binding)
258 {
259   if (InterlockedDecrement(&Binding->refs))
260     return RPC_S_OK;
261
262   TRACE("binding: %p\n", Binding);
263   if (Binding->Assoc) RpcAssoc_Release(Binding->Assoc);
264   RPCRT4_strfree(Binding->Endpoint);
265   RPCRT4_strfree(Binding->NetworkAddr);
266   RPCRT4_strfree(Binding->Protseq);
267   HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
268   if (Binding->AuthInfo) RpcAuthInfo_Release(Binding->AuthInfo);
269   if (Binding->QOS) RpcQualityOfService_Release(Binding->QOS);
270   HeapFree(GetProcessHeap(), 0, Binding);
271   return RPC_S_OK;
272 }
273
274 RPC_STATUS RPCRT4_OpenBinding(RpcBinding* Binding, RpcConnection** Connection,
275                               const RPC_SYNTAX_IDENTIFIER *TransferSyntax,
276                               const RPC_SYNTAX_IDENTIFIER *InterfaceId)
277 {
278   TRACE("(Binding == ^%p)\n", Binding);
279
280   if (!Binding->server) {
281      return RpcAssoc_GetClientConnection(Binding->Assoc, InterfaceId,
282          TransferSyntax, Binding->AuthInfo, Binding->QOS, Connection);
283   } else {
284     /* we already have a connection with acceptable binding, so use it */
285     if (Binding->FromConn) {
286       *Connection = Binding->FromConn;
287       return RPC_S_OK;
288     } else {
289        ERR("no connection in binding\n");
290        return RPC_S_INTERNAL_ERROR;
291     }
292   }
293 }
294
295 RPC_STATUS RPCRT4_CloseBinding(RpcBinding* Binding, RpcConnection* Connection)
296 {
297   TRACE("(Binding == ^%p)\n", Binding);
298   if (!Connection) return RPC_S_OK;
299   if (Binding->server) {
300     /* don't destroy a connection that is cached in the binding */
301     if (Binding->FromConn == Connection)
302       return RPC_S_OK;
303     return RPCRT4_DestroyConnection(Connection);
304   }
305   else {
306     RpcAssoc_ReleaseIdleConnection(Binding->Assoc, Connection);
307     return RPC_S_OK;
308   }
309 }
310
311 /* utility functions for string composing and parsing */
312 static unsigned RPCRT4_strcopyA(LPSTR data, LPCSTR src)
313 {
314   unsigned len = strlen(src);
315   memcpy(data, src, len*sizeof(CHAR));
316   return len;
317 }
318
319 static unsigned RPCRT4_strcopyW(LPWSTR data, LPCWSTR src)
320 {
321   unsigned len = strlenW(src);
322   memcpy(data, src, len*sizeof(WCHAR));
323   return len;
324 }
325
326 static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
327 {
328   DWORD len = strlen(dst), slen = strlen(src);
329   LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
330   if (!ndst)
331   {
332     HeapFree(GetProcessHeap(), 0, dst);
333     return NULL;
334   }
335   ndst[len] = ',';
336   memcpy(ndst+len+1, src, slen+1);
337   return ndst;
338 }
339
340 static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
341 {
342   DWORD len = strlenW(dst), slen = strlenW(src);
343   LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
344   if (!ndst) 
345   {
346     HeapFree(GetProcessHeap(), 0, dst);
347     return NULL;
348   }
349   ndst[len] = ',';
350   memcpy(ndst+len+1, src, (slen+1)*sizeof(WCHAR));
351   return ndst;
352 }
353
354
355 /***********************************************************************
356  *             RpcStringBindingComposeA (RPCRT4.@)
357  */
358 RPC_STATUS WINAPI RpcStringBindingComposeA(RPC_CSTR ObjUuid, RPC_CSTR Protseq,
359                                            RPC_CSTR NetworkAddr, RPC_CSTR Endpoint,
360                                            RPC_CSTR Options, RPC_CSTR *StringBinding )
361 {
362   DWORD len = 1;
363   LPSTR data;
364
365   TRACE( "(%s,%s,%s,%s,%s,%p)\n",
366         debugstr_a( (char*)ObjUuid ), debugstr_a( (char*)Protseq ),
367         debugstr_a( (char*)NetworkAddr ), debugstr_a( (char*)Endpoint ),
368         debugstr_a( (char*)Options ), StringBinding );
369
370   if (ObjUuid && *ObjUuid) len += strlen((char*)ObjUuid) + 1;
371   if (Protseq && *Protseq) len += strlen((char*)Protseq) + 1;
372   if (NetworkAddr && *NetworkAddr) len += strlen((char*)NetworkAddr);
373   if (Endpoint && *Endpoint) len += strlen((char*)Endpoint) + 2;
374   if (Options && *Options) len += strlen((char*)Options) + 2;
375
376   data = HeapAlloc(GetProcessHeap(), 0, len);
377   *StringBinding = (unsigned char*)data;
378
379   if (ObjUuid && *ObjUuid) {
380     data += RPCRT4_strcopyA(data, (char*)ObjUuid);
381     *data++ = '@';
382   }
383   if (Protseq && *Protseq) {
384     data += RPCRT4_strcopyA(data, (char*)Protseq);
385     *data++ = ':';
386   }
387   if (NetworkAddr && *NetworkAddr)
388     data += RPCRT4_strcopyA(data, (char*)NetworkAddr);
389
390   if ((Endpoint && *Endpoint) ||
391       (Options && *Options)) {
392     *data++ = '[';
393     if (Endpoint && *Endpoint) {
394       data += RPCRT4_strcopyA(data, (char*)Endpoint);
395       if (Options && *Options) *data++ = ',';
396     }
397     if (Options && *Options) {
398       data += RPCRT4_strcopyA(data, (char*)Options);
399     }
400     *data++ = ']';
401   }
402   *data = 0;
403
404   return RPC_S_OK;
405 }
406
407 /***********************************************************************
408  *             RpcStringBindingComposeW (RPCRT4.@)
409  */
410 RPC_STATUS WINAPI RpcStringBindingComposeW( RPC_WSTR ObjUuid, RPC_WSTR Protseq,
411                                             RPC_WSTR NetworkAddr, RPC_WSTR Endpoint,
412                                             RPC_WSTR Options, RPC_WSTR* StringBinding )
413 {
414   DWORD len = 1;
415   RPC_WSTR data;
416
417   TRACE("(%s,%s,%s,%s,%s,%p)\n",
418        debugstr_w( ObjUuid ), debugstr_w( Protseq ),
419        debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
420        debugstr_w( Options ), StringBinding);
421
422   if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) + 1;
423   if (Protseq && *Protseq) len += strlenW(Protseq) + 1;
424   if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr);
425   if (Endpoint && *Endpoint) len += strlenW(Endpoint) + 2;
426   if (Options && *Options) len += strlenW(Options) + 2;
427
428   data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
429   *StringBinding = data;
430
431   if (ObjUuid && *ObjUuid) {
432     data += RPCRT4_strcopyW(data, ObjUuid);
433     *data++ = '@';
434   }
435   if (Protseq && *Protseq) {
436     data += RPCRT4_strcopyW(data, Protseq);
437     *data++ = ':';
438   }
439   if (NetworkAddr && *NetworkAddr) {
440     data += RPCRT4_strcopyW(data, NetworkAddr);
441   }
442   if ((Endpoint && *Endpoint) ||
443       (Options && *Options)) {
444     *data++ = '[';
445     if (Endpoint && *Endpoint) {
446       data += RPCRT4_strcopyW(data, Endpoint);
447       if (Options && *Options) *data++ = ',';
448     }
449     if (Options && *Options) {
450       data += RPCRT4_strcopyW(data, Options);
451     }
452     *data++ = ']';
453   }
454   *data = 0;
455
456   return RPC_S_OK;
457 }
458
459
460 /***********************************************************************
461  *             RpcStringBindingParseA (RPCRT4.@)
462  */
463 RPC_STATUS WINAPI RpcStringBindingParseA( RPC_CSTR StringBinding, RPC_CSTR *ObjUuid,
464                                           RPC_CSTR *Protseq, RPC_CSTR *NetworkAddr,
465                                           RPC_CSTR *Endpoint, RPC_CSTR *Options)
466 {
467   CHAR *data, *next;
468   static const char ep_opt[] = "endpoint=";
469   BOOL endpoint_already_found = FALSE;
470
471   TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a((char*)StringBinding),
472        ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
473
474   if (ObjUuid) *ObjUuid = NULL;
475   if (Protseq) *Protseq = NULL;
476   if (NetworkAddr) *NetworkAddr = NULL;
477   if (Endpoint) *Endpoint = NULL;
478   if (Options) *Options = NULL;
479
480   data = (char*) StringBinding;
481
482   next = strchr(data, '@');
483   if (next) {
484     UUID uuid;
485     RPC_STATUS status;
486     RPC_CSTR str_uuid = (unsigned char*)RPCRT4_strndupA(data, next - data);
487     status = UuidFromStringA(str_uuid, &uuid);
488     if (status != RPC_S_OK) {
489       HeapFree(GetProcessHeap(), 0, str_uuid);
490       return status;
491     }
492     if (ObjUuid)
493       *ObjUuid = str_uuid;
494     else
495       HeapFree(GetProcessHeap(), 0, str_uuid);
496     data = next+1;
497   }
498
499   next = strchr(data, ':');
500   if (next) {
501     if (Protseq) *Protseq = (unsigned char*)RPCRT4_strndupA(data, next - data);
502     data = next+1;
503   }
504
505   next = strchr(data, '[');
506   if (next) {
507     CHAR *close, *opt;
508
509     if (NetworkAddr) *NetworkAddr = (unsigned char*)RPCRT4_strndupA(data, next - data);
510     data = next+1;
511     close = strchr(data, ']');
512     if (!close) goto fail;
513
514     /* tokenize options */
515     while (data < close) {
516       next = strchr(data, ',');
517       if (!next || next > close) next = close;
518       /* FIXME: this is kind of inefficient */
519       opt = RPCRT4_strndupA(data, next - data);
520       data = next+1;
521
522       /* parse option */
523       next = strchr(opt, '=');
524       if (!next) {
525         /* not an option, must be an endpoint */
526         if (endpoint_already_found) goto fail;
527         if (Endpoint) *Endpoint = (unsigned char*) opt;
528         else HeapFree(GetProcessHeap(), 0, opt);
529         endpoint_already_found = TRUE;
530       } else {
531         if (strncmp(opt, ep_opt, strlen(ep_opt)) == 0) {
532           /* endpoint option */
533           if (endpoint_already_found) goto fail;
534           if (Endpoint) *Endpoint = (unsigned char*) RPCRT4_strdupA(next+1);
535           HeapFree(GetProcessHeap(), 0, opt);
536           endpoint_already_found = TRUE;
537         } else {
538           /* network option */
539           if (Options) {
540             if (*Options) {
541               /* FIXME: this is kind of inefficient */
542               *Options = (unsigned char*) RPCRT4_strconcatA( (char*)*Options, opt);
543               HeapFree(GetProcessHeap(), 0, opt);
544             } else
545               *Options = (unsigned char*) opt;
546           } else
547             HeapFree(GetProcessHeap(), 0, opt);
548         }
549       }
550     }
551
552     data = close+1;
553     if (*data) goto fail;
554   }
555   else if (NetworkAddr) 
556     *NetworkAddr = (unsigned char*)RPCRT4_strdupA(data);
557
558   return RPC_S_OK;
559
560 fail:
561   if (ObjUuid) RpcStringFreeA((unsigned char**)ObjUuid);
562   if (Protseq) RpcStringFreeA((unsigned char**)Protseq);
563   if (NetworkAddr) RpcStringFreeA((unsigned char**)NetworkAddr);
564   if (Endpoint) RpcStringFreeA((unsigned char**)Endpoint);
565   if (Options) RpcStringFreeA((unsigned char**)Options);
566   return RPC_S_INVALID_STRING_BINDING;
567 }
568
569 /***********************************************************************
570  *             RpcStringBindingParseW (RPCRT4.@)
571  */
572 RPC_STATUS WINAPI RpcStringBindingParseW( RPC_WSTR StringBinding, RPC_WSTR *ObjUuid,
573                                           RPC_WSTR *Protseq, RPC_WSTR *NetworkAddr,
574                                           RPC_WSTR *Endpoint, RPC_WSTR *Options)
575 {
576   WCHAR *data, *next;
577   static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
578   BOOL endpoint_already_found = FALSE;
579
580   TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
581        ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
582
583   if (ObjUuid) *ObjUuid = NULL;
584   if (Protseq) *Protseq = NULL;
585   if (NetworkAddr) *NetworkAddr = NULL;
586   if (Endpoint) *Endpoint = NULL;
587   if (Options) *Options = NULL;
588
589   data = StringBinding;
590
591   next = strchrW(data, '@');
592   if (next) {
593     UUID uuid;
594     RPC_STATUS status;
595     RPC_WSTR str_uuid = RPCRT4_strndupW(data, next - data);
596     status = UuidFromStringW(str_uuid, &uuid);
597     if (status != RPC_S_OK) {
598       HeapFree(GetProcessHeap(), 0, str_uuid);
599       return status;
600     }
601     if (ObjUuid)
602       *ObjUuid = str_uuid;
603     else
604       HeapFree(GetProcessHeap(), 0, str_uuid);
605     data = next+1;
606   }
607
608   next = strchrW(data, ':');
609   if (next) {
610     if (Protseq) *Protseq = RPCRT4_strndupW(data, next - data);
611     data = next+1;
612   }
613
614   next = strchrW(data, '[');
615   if (next) {
616     WCHAR *close, *opt;
617
618     if (NetworkAddr) *NetworkAddr = RPCRT4_strndupW(data, next - data);
619     data = next+1;
620     close = strchrW(data, ']');
621     if (!close) goto fail;
622
623     /* tokenize options */
624     while (data < close) {
625       next = strchrW(data, ',');
626       if (!next || next > close) next = close;
627       /* FIXME: this is kind of inefficient */
628       opt = RPCRT4_strndupW(data, next - data);
629       data = next+1;
630
631       /* parse option */
632       next = strchrW(opt, '=');
633       if (!next) {
634         /* not an option, must be an endpoint */
635         if (endpoint_already_found) goto fail;
636         if (Endpoint) *Endpoint = opt;
637         else HeapFree(GetProcessHeap(), 0, opt);
638         endpoint_already_found = TRUE;
639       } else {
640         if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
641           /* endpoint option */
642           if (endpoint_already_found) goto fail;
643           if (Endpoint) *Endpoint = RPCRT4_strdupW(next+1);
644           HeapFree(GetProcessHeap(), 0, opt);
645           endpoint_already_found = TRUE;
646         } else {
647           /* network option */
648           if (Options) {
649             if (*Options) {
650               /* FIXME: this is kind of inefficient */
651               *Options = RPCRT4_strconcatW(*Options, opt);
652               HeapFree(GetProcessHeap(), 0, opt);
653             } else
654               *Options = opt;
655           } else
656             HeapFree(GetProcessHeap(), 0, opt);
657         }
658       }
659     }
660
661     data = close+1;
662     if (*data) goto fail;
663   } else if (NetworkAddr) 
664     *NetworkAddr = RPCRT4_strdupW(data);
665
666   return RPC_S_OK;
667
668 fail:
669   if (ObjUuid) RpcStringFreeW(ObjUuid);
670   if (Protseq) RpcStringFreeW(Protseq);
671   if (NetworkAddr) RpcStringFreeW(NetworkAddr);
672   if (Endpoint) RpcStringFreeW(Endpoint);
673   if (Options) RpcStringFreeW(Options);
674   return RPC_S_INVALID_STRING_BINDING;
675 }
676
677 /***********************************************************************
678  *             RpcBindingFree (RPCRT4.@)
679  */
680 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
681 {
682   RPC_STATUS status;
683   TRACE("(%p) = %p\n", Binding, *Binding);
684   status = RPCRT4_ReleaseBinding(*Binding);
685   if (status == RPC_S_OK) *Binding = 0;
686   return status;
687 }
688   
689 /***********************************************************************
690  *             RpcBindingVectorFree (RPCRT4.@)
691  */
692 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
693 {
694   RPC_STATUS status;
695   unsigned long c;
696
697   TRACE("(%p)\n", BindingVector);
698   for (c=0; c<(*BindingVector)->Count; c++) {
699     status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
700   }
701   HeapFree(GetProcessHeap(), 0, *BindingVector);
702   *BindingVector = NULL;
703   return RPC_S_OK;
704 }
705   
706 /***********************************************************************
707  *             RpcBindingInqObject (RPCRT4.@)
708  */
709 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
710 {
711   RpcBinding* bind = (RpcBinding*)Binding;
712
713   TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
714   *ObjectUuid = bind->ObjectUuid;
715   return RPC_S_OK;
716 }
717
718 /***********************************************************************
719  *             RpcBindingSetObject (RPCRT4.@)
720  */
721 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
722 {
723   RpcBinding* bind = (RpcBinding*)Binding;
724
725   TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
726   if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
727   return RPCRT4_SetBindingObject(Binding, ObjectUuid);
728 }
729
730 /***********************************************************************
731  *             RpcBindingFromStringBindingA (RPCRT4.@)
732  */
733 RPC_STATUS WINAPI RpcBindingFromStringBindingA( RPC_CSTR StringBinding, RPC_BINDING_HANDLE* Binding )
734 {
735   RPC_STATUS ret;
736   RpcBinding* bind = NULL;
737   RPC_CSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
738   UUID Uuid;
739
740   TRACE("(%s,%p)\n", debugstr_a((char*)StringBinding), Binding);
741
742   ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
743                               &NetworkAddr, &Endpoint, &Options);
744   if (ret != RPC_S_OK) return ret;
745
746   ret = UuidFromStringA(ObjectUuid, &Uuid);
747
748   if (ret == RPC_S_OK)
749     ret = RPCRT4_CreateBindingA(&bind, FALSE, (char*)Protseq);
750   if (ret != RPC_S_OK) return ret;
751   ret = RPCRT4_SetBindingObject(bind, &Uuid);
752   if (ret == RPC_S_OK)
753     ret = RPCRT4_CompleteBindingA(bind, (char*)NetworkAddr, (char*)Endpoint, (char*)Options);
754
755   RpcStringFreeA((unsigned char**)&Options);
756   RpcStringFreeA((unsigned char**)&Endpoint);
757   RpcStringFreeA((unsigned char**)&NetworkAddr);
758   RpcStringFreeA((unsigned char**)&Protseq);
759   RpcStringFreeA((unsigned char**)&ObjectUuid);
760
761   if (ret == RPC_S_OK) 
762     *Binding = (RPC_BINDING_HANDLE)bind;
763   else 
764     RPCRT4_ReleaseBinding(bind);
765
766   return ret;
767 }
768
769 /***********************************************************************
770  *             RpcBindingFromStringBindingW (RPCRT4.@)
771  */
772 RPC_STATUS WINAPI RpcBindingFromStringBindingW( RPC_WSTR StringBinding, RPC_BINDING_HANDLE* Binding )
773 {
774   RPC_STATUS ret;
775   RpcBinding* bind = NULL;
776   RPC_WSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
777   UUID Uuid;
778
779   TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
780
781   ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
782                               &NetworkAddr, &Endpoint, &Options);
783   if (ret != RPC_S_OK) return ret;
784
785   ret = UuidFromStringW(ObjectUuid, &Uuid);
786
787   if (ret == RPC_S_OK)
788     ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
789   if (ret != RPC_S_OK) return ret;
790   ret = RPCRT4_SetBindingObject(bind, &Uuid);
791   if (ret == RPC_S_OK)
792     ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
793
794   RpcStringFreeW(&Options);
795   RpcStringFreeW(&Endpoint);
796   RpcStringFreeW(&NetworkAddr);
797   RpcStringFreeW(&Protseq);
798   RpcStringFreeW(&ObjectUuid);
799
800   if (ret == RPC_S_OK)
801     *Binding = (RPC_BINDING_HANDLE)bind;
802   else
803     RPCRT4_ReleaseBinding(bind);
804
805   return ret;
806 }
807   
808 /***********************************************************************
809  *             RpcBindingToStringBindingA (RPCRT4.@)
810  */
811 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, RPC_CSTR *StringBinding )
812 {
813   RPC_STATUS ret;
814   RpcBinding* bind = (RpcBinding*)Binding;
815   RPC_CSTR ObjectUuid;
816
817   TRACE("(%p,%p)\n", Binding, StringBinding);
818
819   ret = UuidToStringA(&bind->ObjectUuid, &ObjectUuid);
820   if (ret != RPC_S_OK) return ret;
821
822   ret = RpcStringBindingComposeA(ObjectUuid, (unsigned char*)bind->Protseq, (unsigned char*) bind->NetworkAddr,
823                                  (unsigned char*) bind->Endpoint, NULL, StringBinding);
824
825   RpcStringFreeA(&ObjectUuid);
826
827   return ret;
828 }
829   
830 /***********************************************************************
831  *             RpcBindingToStringBindingW (RPCRT4.@)
832  */
833 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, RPC_WSTR *StringBinding )
834 {
835   RPC_STATUS ret;
836   unsigned char *str = NULL;
837   TRACE("(%p,%p)\n", Binding, StringBinding);
838   ret = RpcBindingToStringBindingA(Binding, &str);
839   *StringBinding = RPCRT4_strdupAtoW((char*)str);
840   RpcStringFreeA((unsigned char**)&str);
841   return ret;
842 }
843
844 /***********************************************************************
845  *             I_RpcBindingInqTransportType (RPCRT4.@)
846  */
847 RPC_STATUS WINAPI I_RpcBindingInqTransportType( RPC_BINDING_HANDLE Binding, unsigned int * Type )
848 {
849
850   FIXME( "(%p,%p): stub\n", Binding, Type);
851   *Type = TRANSPORT_TYPE_LPC;
852   return RPC_S_OK;
853 }
854
855 /***********************************************************************
856  *             I_RpcBindingSetAsync (RPCRT4.@)
857  * NOTES
858  *  Exists in win9x and winNT, but with different number of arguments
859  *  (9x version has 3 arguments, NT has 2).
860  */
861 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
862 {
863   RpcBinding* bind = (RpcBinding*)Binding;
864
865   TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
866
867   bind->BlockingFn = BlockingFn;
868
869   return RPC_S_OK;
870 }
871
872 /***********************************************************************
873  *             RpcBindingCopy (RPCRT4.@)
874  */
875 RPC_STATUS RPC_ENTRY RpcBindingCopy(
876   RPC_BINDING_HANDLE SourceBinding,
877   RPC_BINDING_HANDLE* DestinationBinding)
878 {
879   RpcBinding *DestBinding;
880   RpcBinding *SrcBinding = (RpcBinding*)SourceBinding;
881   RPC_STATUS status;
882
883   TRACE("(%p, %p)\n", SourceBinding, DestinationBinding);
884
885   status = RPCRT4_AllocBinding(&DestBinding, SrcBinding->server);
886   if (status != RPC_S_OK) return status;
887
888   DestBinding->ObjectUuid = SrcBinding->ObjectUuid;
889   DestBinding->BlockingFn = SrcBinding->BlockingFn;
890   DestBinding->Protseq = RPCRT4_strndupA(SrcBinding->Protseq, -1);
891   DestBinding->NetworkAddr = RPCRT4_strndupA(SrcBinding->NetworkAddr, -1);
892   DestBinding->Endpoint = RPCRT4_strndupA(SrcBinding->Endpoint, -1);
893   DestBinding->NetworkOptions = RPCRT4_strdupW(SrcBinding->NetworkOptions);
894   if (SrcBinding->Assoc) SrcBinding->Assoc->refs++;
895   DestBinding->Assoc = SrcBinding->Assoc;
896
897   if (SrcBinding->AuthInfo) RpcAuthInfo_AddRef(SrcBinding->AuthInfo);
898   DestBinding->AuthInfo = SrcBinding->AuthInfo;
899   if (SrcBinding->QOS) RpcQualityOfService_AddRef(SrcBinding->QOS);
900   DestBinding->QOS = SrcBinding->QOS;
901
902   *DestinationBinding = DestBinding;
903   return RPC_S_OK;
904 }
905
906 /***********************************************************************
907  *             RpcImpersonateClient (RPCRT4.@)
908  *
909  * Impersonates the client connected via a binding handle so that security
910  * checks are done in the context of the client.
911  *
912  * PARAMS
913  *  BindingHandle [I] Handle to the binding to the client.
914  *
915  * RETURNS
916  *  Success: RPS_S_OK.
917  *  Failure: RPC_STATUS value.
918  *
919  * NOTES
920  *
921  * If BindingHandle is NULL then the function impersonates the client
922  * connected to the binding handle of the current thread.
923  */
924 RPC_STATUS WINAPI RpcImpersonateClient(RPC_BINDING_HANDLE BindingHandle)
925 {
926     FIXME("(%p): stub\n", BindingHandle);
927     ImpersonateSelf(SecurityImpersonation);
928     return RPC_S_OK;
929 }
930
931 /***********************************************************************
932  *             RpcRevertToSelfEx (RPCRT4.@)
933  *
934  * Stops impersonating the client connected to the binding handle so that security
935  * checks are no longer done in the context of the client.
936  *
937  * PARAMS
938  *  BindingHandle [I] Handle to the binding to the client.
939  *
940  * RETURNS
941  *  Success: RPS_S_OK.
942  *  Failure: RPC_STATUS value.
943  *
944  * NOTES
945  *
946  * If BindingHandle is NULL then the function stops impersonating the client
947  * connected to the binding handle of the current thread.
948  */
949 RPC_STATUS WINAPI RpcRevertToSelfEx(RPC_BINDING_HANDLE BindingHandle)
950 {
951     FIXME("(%p): stub\n", BindingHandle);
952     return RPC_S_OK;
953 }
954
955 static inline BOOL has_nt_auth_identity(ULONG AuthnLevel)
956 {
957     switch (AuthnLevel)
958     {
959     case RPC_C_AUTHN_GSS_NEGOTIATE:
960     case RPC_C_AUTHN_WINNT:
961     case RPC_C_AUTHN_GSS_KERBEROS:
962         return TRUE;
963     default:
964         return FALSE;
965     }
966 }
967
968 static RPC_STATUS RpcAuthInfo_Create(ULONG AuthnLevel, ULONG AuthnSvc,
969                                      CredHandle cred, TimeStamp exp,
970                                      ULONG cbMaxToken,
971                                      RPC_AUTH_IDENTITY_HANDLE identity,
972                                      RpcAuthInfo **ret)
973 {
974     RpcAuthInfo *AuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo));
975     if (!AuthInfo)
976         return ERROR_OUTOFMEMORY;
977
978     AuthInfo->refs = 1;
979     AuthInfo->AuthnLevel = AuthnLevel;
980     AuthInfo->AuthnSvc = AuthnSvc;
981     AuthInfo->cred = cred;
982     AuthInfo->exp = exp;
983     AuthInfo->cbMaxToken = cbMaxToken;
984     AuthInfo->identity = identity;
985     AuthInfo->server_principal_name = NULL;
986
987     /* duplicate the SEC_WINNT_AUTH_IDENTITY structure, if applicable, to
988      * enable better matching in RpcAuthInfo_IsEqual */
989     if (identity && has_nt_auth_identity(AuthnSvc))
990     {
991         const SEC_WINNT_AUTH_IDENTITY_W *nt_identity = identity;
992         AuthInfo->nt_identity = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo->nt_identity));
993         if (!AuthInfo->nt_identity)
994         {
995             HeapFree(GetProcessHeap(), 0, AuthInfo);
996             return ERROR_OUTOFMEMORY;
997         }
998
999         AuthInfo->nt_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1000         if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1001             AuthInfo->nt_identity->User = RPCRT4_strndupW(nt_identity->User, nt_identity->UserLength);
1002         else
1003             AuthInfo->nt_identity->User = RPCRT4_strndupAtoW((const char *)nt_identity->User, nt_identity->UserLength);
1004         AuthInfo->nt_identity->UserLength = nt_identity->UserLength;
1005         if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1006             AuthInfo->nt_identity->Domain = RPCRT4_strndupW(nt_identity->Domain, nt_identity->DomainLength);
1007         else
1008             AuthInfo->nt_identity->Domain = RPCRT4_strndupAtoW((const char *)nt_identity->Domain, nt_identity->DomainLength);
1009         AuthInfo->nt_identity->DomainLength = nt_identity->DomainLength;
1010         if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1011             AuthInfo->nt_identity->Password = RPCRT4_strndupW(nt_identity->Password, nt_identity->PasswordLength);
1012         else
1013             AuthInfo->nt_identity->Password = RPCRT4_strndupAtoW((const char *)nt_identity->Password, nt_identity->PasswordLength);
1014         AuthInfo->nt_identity->PasswordLength = nt_identity->PasswordLength;
1015
1016         if ((nt_identity->User && !AuthInfo->nt_identity->User) ||
1017             (nt_identity->Domain && !AuthInfo->nt_identity->Domain) ||
1018             (nt_identity->Password && !AuthInfo->nt_identity->Password))
1019         {
1020             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
1021             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1022             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1023             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
1024             HeapFree(GetProcessHeap(), 0, AuthInfo);
1025             return ERROR_OUTOFMEMORY;
1026         }
1027     }
1028     else
1029         AuthInfo->nt_identity = NULL;
1030     *ret = AuthInfo;
1031     return RPC_S_OK;
1032 }
1033
1034 ULONG RpcAuthInfo_AddRef(RpcAuthInfo *AuthInfo)
1035 {
1036     return InterlockedIncrement(&AuthInfo->refs);
1037 }
1038
1039 ULONG RpcAuthInfo_Release(RpcAuthInfo *AuthInfo)
1040 {
1041     ULONG refs = InterlockedDecrement(&AuthInfo->refs);
1042
1043     if (!refs)
1044     {
1045         FreeCredentialsHandle(&AuthInfo->cred);
1046         if (AuthInfo->nt_identity)
1047         {
1048             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
1049             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1050             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1051             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
1052         }
1053         HeapFree(GetProcessHeap(), 0, AuthInfo->server_principal_name);
1054         HeapFree(GetProcessHeap(), 0, AuthInfo);
1055     }
1056
1057     return refs;
1058 }
1059
1060 BOOL RpcAuthInfo_IsEqual(const RpcAuthInfo *AuthInfo1, const RpcAuthInfo *AuthInfo2)
1061 {
1062     if (AuthInfo1 == AuthInfo2)
1063         return TRUE;
1064
1065     if (!AuthInfo1 || !AuthInfo2)
1066         return FALSE;
1067
1068     if ((AuthInfo1->AuthnLevel != AuthInfo2->AuthnLevel) ||
1069         (AuthInfo1->AuthnSvc != AuthInfo2->AuthnSvc))
1070         return FALSE;
1071
1072     if (AuthInfo1->identity == AuthInfo2->identity)
1073         return TRUE;
1074
1075     if (!AuthInfo1->identity || !AuthInfo2->identity)
1076         return FALSE;
1077
1078     if (has_nt_auth_identity(AuthInfo1->AuthnSvc))
1079     {
1080         const SEC_WINNT_AUTH_IDENTITY_W *identity1 = AuthInfo1->nt_identity;
1081         const SEC_WINNT_AUTH_IDENTITY_W *identity2 = AuthInfo2->nt_identity;
1082         /* compare user names */
1083         if (identity1->UserLength != identity2->UserLength ||
1084             memcmp(identity1->User, identity2->User, identity1->UserLength))
1085             return FALSE;
1086         /* compare domain names */
1087         if (identity1->DomainLength != identity2->DomainLength ||
1088             memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1089             return FALSE;
1090         /* compare passwords */
1091         if (identity1->PasswordLength != identity2->PasswordLength ||
1092             memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1093             return FALSE;
1094     }
1095     else
1096         return FALSE;
1097
1098     return TRUE;
1099 }
1100
1101 static RPC_STATUS RpcQualityOfService_Create(const RPC_SECURITY_QOS *qos_src, BOOL unicode, RpcQualityOfService **qos_dst)
1102 {
1103     RpcQualityOfService *qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos));
1104
1105     if (!qos)
1106         return RPC_S_OUT_OF_RESOURCES;
1107
1108     qos->refs = 1;
1109     qos->qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos->qos));
1110     if (!qos->qos) goto error;
1111     qos->qos->Version = qos_src->Version;
1112     qos->qos->Capabilities = qos_src->Capabilities;
1113     qos->qos->IdentityTracking = qos_src->IdentityTracking;
1114     qos->qos->ImpersonationType = qos_src->ImpersonationType;
1115     qos->qos->AdditionalSecurityInfoType = 0;
1116
1117     if (qos_src->Version >= 2)
1118     {
1119         const RPC_SECURITY_QOS_V2_W *qos_src2 = (const RPC_SECURITY_QOS_V2_W *)qos_src;
1120         qos->qos->AdditionalSecurityInfoType = qos_src2->AdditionalSecurityInfoType;
1121         if (qos_src2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1122         {
1123             const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_src = qos_src2->u.HttpCredentials;
1124             RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_dst;
1125
1126             http_credentials_dst = HeapAlloc(GetProcessHeap(), 0, sizeof(*http_credentials_dst));
1127             qos->qos->u.HttpCredentials = http_credentials_dst;
1128             if (!http_credentials_dst) goto error;
1129             http_credentials_dst->TransportCredentials = NULL;
1130             http_credentials_dst->Flags = http_credentials_src->Flags;
1131             http_credentials_dst->AuthenticationTarget = http_credentials_src->AuthenticationTarget;
1132             http_credentials_dst->NumberOfAuthnSchemes = http_credentials_src->NumberOfAuthnSchemes;
1133             http_credentials_dst->AuthnSchemes = NULL;
1134             http_credentials_dst->ServerCertificateSubject = NULL;
1135             if (http_credentials_src->TransportCredentials)
1136             {
1137                 SEC_WINNT_AUTH_IDENTITY_W *cred_dst;
1138                 cred_dst = http_credentials_dst->TransportCredentials = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cred_dst));
1139                 if (!cred_dst) goto error;
1140                 cred_dst->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1141                 if (unicode)
1142                 {
1143                     const SEC_WINNT_AUTH_IDENTITY_W *cred_src = http_credentials_src->TransportCredentials;
1144                     cred_dst->UserLength = cred_src->UserLength;
1145                     cred_dst->PasswordLength = cred_src->PasswordLength;
1146                     cred_dst->DomainLength = cred_src->DomainLength;
1147                     cred_dst->User = RPCRT4_strndupW(cred_src->User, cred_src->UserLength);
1148                     cred_dst->Password = RPCRT4_strndupW(cred_src->Password, cred_src->PasswordLength);
1149                     cred_dst->Domain = RPCRT4_strndupW(cred_src->Domain, cred_src->DomainLength);
1150                 }
1151                 else
1152                 {
1153                     const SEC_WINNT_AUTH_IDENTITY_A *cred_src = (const SEC_WINNT_AUTH_IDENTITY_A *)http_credentials_src->TransportCredentials;
1154                     cred_dst->UserLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, NULL, 0);
1155                     cred_dst->DomainLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, NULL, 0);
1156                     cred_dst->PasswordLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, NULL, 0);
1157                     cred_dst->User = HeapAlloc(GetProcessHeap(), 0, cred_dst->UserLength * sizeof(WCHAR));
1158                     cred_dst->Password = HeapAlloc(GetProcessHeap(), 0, cred_dst->PasswordLength * sizeof(WCHAR));
1159                     cred_dst->Domain = HeapAlloc(GetProcessHeap(), 0, cred_dst->DomainLength * sizeof(WCHAR));
1160                     if (!cred_dst || !cred_dst->Password || !cred_dst->Domain) goto error;
1161                     MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, cred_dst->User, cred_dst->UserLength);
1162                     MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, cred_dst->Domain, cred_dst->DomainLength);
1163                     MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, cred_dst->Password, cred_dst->PasswordLength);
1164                 }
1165             }
1166             if (http_credentials_src->NumberOfAuthnSchemes)
1167             {
1168                 http_credentials_dst->AuthnSchemes = HeapAlloc(GetProcessHeap(), 0, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1169                 if (!http_credentials_dst->AuthnSchemes) goto error;
1170                 memcpy(http_credentials_dst->AuthnSchemes, http_credentials_src->AuthnSchemes, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1171             }
1172             if (http_credentials_src->ServerCertificateSubject)
1173             {
1174                 if (unicode)
1175                     http_credentials_dst->ServerCertificateSubject =
1176                         RPCRT4_strndupW(http_credentials_src->ServerCertificateSubject,
1177                                         strlenW(http_credentials_src->ServerCertificateSubject));
1178                 else
1179                     http_credentials_dst->ServerCertificateSubject =
1180                         RPCRT4_strdupAtoW((char *)http_credentials_src->ServerCertificateSubject);
1181                 if (!http_credentials_dst->ServerCertificateSubject) goto error;
1182             }
1183         }
1184     }
1185     *qos_dst = qos;
1186     return RPC_S_OK;
1187
1188 error:
1189     if (qos->qos)
1190     {
1191         if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP &&
1192             qos->qos->u.HttpCredentials)
1193         {
1194             if (qos->qos->u.HttpCredentials->TransportCredentials)
1195             {
1196                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1197                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1198                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1199                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1200             }
1201             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1202             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1203             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1204         }
1205         HeapFree(GetProcessHeap(), 0, qos->qos);
1206     }
1207     HeapFree(GetProcessHeap(), 0, qos);
1208     return RPC_S_OUT_OF_RESOURCES;
1209 }
1210
1211 ULONG RpcQualityOfService_AddRef(RpcQualityOfService *qos)
1212 {
1213     return InterlockedIncrement(&qos->refs);
1214 }
1215
1216 ULONG RpcQualityOfService_Release(RpcQualityOfService *qos)
1217 {
1218     ULONG refs = InterlockedDecrement(&qos->refs);
1219
1220     if (!refs)
1221     {
1222         if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1223         {
1224             if (qos->qos->u.HttpCredentials->TransportCredentials)
1225             {
1226                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1227                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1228                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1229                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1230             }
1231             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1232             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1233             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1234         }
1235         HeapFree(GetProcessHeap(), 0, qos->qos);
1236         HeapFree(GetProcessHeap(), 0, qos);
1237     }
1238     return refs;
1239 }
1240
1241 BOOL RpcQualityOfService_IsEqual(const RpcQualityOfService *qos1, const RpcQualityOfService *qos2)
1242 {
1243     if (qos1 == qos2)
1244         return TRUE;
1245
1246     if (!qos1 || !qos2)
1247         return FALSE;
1248
1249     TRACE("qos1 = { %ld %ld %ld %ld }, qos2 = { %ld %ld %ld %ld }\n",
1250         qos1->qos->Capabilities, qos1->qos->IdentityTracking,
1251         qos1->qos->ImpersonationType, qos1->qos->AdditionalSecurityInfoType,
1252         qos2->qos->Capabilities, qos2->qos->IdentityTracking,
1253         qos2->qos->ImpersonationType, qos2->qos->AdditionalSecurityInfoType);
1254
1255     if ((qos1->qos->Capabilities != qos2->qos->Capabilities) ||
1256         (qos1->qos->IdentityTracking != qos2->qos->IdentityTracking) ||
1257         (qos1->qos->ImpersonationType != qos2->qos->ImpersonationType) ||
1258         (qos1->qos->AdditionalSecurityInfoType != qos2->qos->AdditionalSecurityInfoType))
1259         return FALSE;
1260
1261     if (qos1->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1262     {
1263         const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials1 = qos1->qos->u.HttpCredentials;
1264         const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials2 = qos2->qos->u.HttpCredentials;
1265
1266         if (http_credentials1->Flags != http_credentials2->Flags)
1267             return FALSE;
1268
1269         if (http_credentials1->AuthenticationTarget != http_credentials2->AuthenticationTarget)
1270             return FALSE;
1271
1272         /* authentication schemes and server certificate subject not currently used */
1273
1274         if (http_credentials1->TransportCredentials != http_credentials2->TransportCredentials)
1275         {
1276             const SEC_WINNT_AUTH_IDENTITY_W *identity1 = http_credentials1->TransportCredentials;
1277             const SEC_WINNT_AUTH_IDENTITY_W *identity2 = http_credentials2->TransportCredentials;
1278
1279             if (!identity1 || !identity2)
1280                 return FALSE;
1281
1282             /* compare user names */
1283             if (identity1->UserLength != identity2->UserLength ||
1284                 memcmp(identity1->User, identity2->User, identity1->UserLength))
1285                 return FALSE;
1286             /* compare domain names */
1287             if (identity1->DomainLength != identity2->DomainLength ||
1288                 memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1289                 return FALSE;
1290             /* compare passwords */
1291             if (identity1->PasswordLength != identity2->PasswordLength ||
1292                 memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1293                 return FALSE;
1294         }
1295     }
1296
1297     return TRUE;
1298 }
1299
1300 /***********************************************************************
1301  *             RpcRevertToSelf (RPCRT4.@)
1302  */
1303 RPC_STATUS WINAPI RpcRevertToSelf(void)
1304 {
1305     FIXME("stub\n");
1306     RevertToSelf();
1307     return RPC_S_OK;
1308 }
1309
1310 /***********************************************************************
1311  *             RpcMgmtSetComTimeout (RPCRT4.@)
1312  */
1313 RPC_STATUS WINAPI RpcMgmtSetComTimeout(RPC_BINDING_HANDLE BindingHandle, unsigned int Timeout)
1314 {
1315     FIXME("(%p, %d): stub\n", BindingHandle, Timeout);
1316     return RPC_S_OK;
1317 }
1318
1319 /***********************************************************************
1320  *             RpcBindingInqAuthInfoExA (RPCRT4.@)
1321  */
1322 RPCRTAPI RPC_STATUS RPC_ENTRY
1323 RpcBindingInqAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1324                           ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1325                           ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1326 {
1327     FIXME("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1328           AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1329     return RPC_S_INVALID_BINDING;
1330 }
1331
1332 /***********************************************************************
1333  *             RpcBindingInqAuthInfoExW (RPCRT4.@)
1334  */
1335 RPCRTAPI RPC_STATUS RPC_ENTRY
1336 RpcBindingInqAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1337                           ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1338                           ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1339 {
1340     FIXME("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1341           AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1342     return RPC_S_INVALID_BINDING;
1343 }
1344
1345 /***********************************************************************
1346  *             RpcBindingInqAuthInfoA (RPCRT4.@)
1347  */
1348 RPCRTAPI RPC_STATUS RPC_ENTRY
1349 RpcBindingInqAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1350                         ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1351 {
1352     FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
1353           AuthnSvc, AuthIdentity, AuthzSvc);
1354     return RPC_S_INVALID_BINDING;
1355 }
1356
1357 /***********************************************************************
1358  *             RpcBindingInqAuthInfoW (RPCRT4.@)
1359  */
1360 RPCRTAPI RPC_STATUS RPC_ENTRY
1361 RpcBindingInqAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1362                         ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1363 {
1364     FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
1365           AuthnSvc, AuthIdentity, AuthzSvc);
1366     return RPC_S_INVALID_BINDING;
1367 }
1368
1369 /***********************************************************************
1370  *             RpcBindingSetAuthInfoExA (RPCRT4.@)
1371  */
1372 RPCRTAPI RPC_STATUS RPC_ENTRY
1373 RpcBindingSetAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName,
1374                           ULONG AuthnLevel, ULONG AuthnSvc,
1375                           RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1376                           RPC_SECURITY_QOS *SecurityQos )
1377 {
1378   RpcBinding* bind = (RpcBinding*)Binding;
1379   SECURITY_STATUS r;
1380   CredHandle cred;
1381   TimeStamp exp;
1382   ULONG package_count;
1383   ULONG i;
1384   PSecPkgInfoA packages;
1385   ULONG cbMaxToken;
1386
1387   TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_a((const char*)ServerPrincName),
1388         AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1389
1390   if (SecurityQos)
1391   {
1392       RPC_STATUS status;
1393
1394       TRACE("SecurityQos { Version=%ld, Capabilties=0x%lx, IdentityTracking=%ld, ImpersonationLevel=%ld",
1395             SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1396       if (SecurityQos->Version >= 2)
1397       {
1398           const RPC_SECURITY_QOS_V2_A *SecurityQos2 = (const RPC_SECURITY_QOS_V2_A *)SecurityQos;
1399           TRACE(", AdditionalSecurityInfoType=%ld", SecurityQos2->AdditionalSecurityInfoType);
1400           if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1401               TRACE(", { %p, 0x%lx, %ld, %ld, %p, %s }",
1402                     SecurityQos2->u.HttpCredentials->TransportCredentials,
1403                     SecurityQos2->u.HttpCredentials->Flags,
1404                     SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1405                     SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1406                     SecurityQos2->u.HttpCredentials->AuthnSchemes,
1407                     SecurityQos2->u.HttpCredentials->ServerCertificateSubject);
1408       }
1409       TRACE("}\n");
1410       status = RpcQualityOfService_Create(SecurityQos, FALSE, &bind->QOS);
1411       if (status != RPC_S_OK)
1412           return status;
1413   }
1414   else
1415   {
1416       if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1417       bind->QOS = NULL;
1418   }
1419
1420   if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1421     AuthnSvc = RPC_C_AUTHN_WINNT;
1422
1423   /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1424   if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1425     AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1426
1427   if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1428   {
1429     if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1430     bind->AuthInfo = NULL;
1431     return RPC_S_OK;
1432   }
1433
1434   if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1435   {
1436     FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1437     return RPC_S_UNKNOWN_AUTHN_LEVEL;
1438   }
1439
1440   /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1441   if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1442   {
1443     FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1444     return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1445   }
1446
1447   r = EnumerateSecurityPackagesA(&package_count, &packages);
1448   if (r != SEC_E_OK)
1449   {
1450     ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n", r);
1451     return RPC_S_SEC_PKG_ERROR;
1452   }
1453
1454   for (i = 0; i < package_count; i++)
1455     if (packages[i].wRPCID == AuthnSvc)
1456         break;
1457
1458   if (i == package_count)
1459   {
1460     FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1461     FreeContextBuffer(packages);
1462     return RPC_S_UNKNOWN_AUTHN_SERVICE;
1463   }
1464
1465   TRACE("found package %s for service %u\n", packages[i].Name, AuthnSvc);
1466   r = AcquireCredentialsHandleA(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1467                                 AuthIdentity, NULL, NULL, &cred, &exp);
1468   cbMaxToken = packages[i].cbMaxToken;
1469   FreeContextBuffer(packages);
1470   if (r == ERROR_SUCCESS)
1471   {
1472     RpcAuthInfo *new_auth_info;
1473     r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1474                            AuthIdentity, &new_auth_info);
1475     if (r == RPC_S_OK)
1476     {
1477       new_auth_info->server_principal_name = RPCRT4_strdupAtoW((char *)ServerPrincName);
1478       if (new_auth_info->server_principal_name)
1479       {
1480         if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1481         bind->AuthInfo = new_auth_info;
1482       }
1483       else
1484       {
1485         RpcAuthInfo_Release(new_auth_info);
1486         r = ERROR_OUTOFMEMORY;
1487       }
1488     }
1489     else
1490       FreeCredentialsHandle(&cred);
1491     return r;
1492   }
1493   else
1494   {
1495     ERR("AcquireCredentialsHandleA failed with error 0x%08x\n", r);
1496     return RPC_S_SEC_PKG_ERROR;
1497   }
1498 }
1499
1500 /***********************************************************************
1501  *             RpcBindingSetAuthInfoExW (RPCRT4.@)
1502  */
1503 RPCRTAPI RPC_STATUS RPC_ENTRY
1504 RpcBindingSetAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1505                           ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1506                           RPC_SECURITY_QOS *SecurityQos )
1507 {
1508   RpcBinding* bind = (RpcBinding*)Binding;
1509   SECURITY_STATUS r;
1510   CredHandle cred;
1511   TimeStamp exp;
1512   ULONG package_count;
1513   ULONG i;
1514   PSecPkgInfoW packages;
1515   ULONG cbMaxToken;
1516
1517   TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1518         AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1519
1520   if (SecurityQos)
1521   {
1522       RPC_STATUS status;
1523
1524       TRACE("SecurityQos { Version=%ld, Capabilties=0x%lx, IdentityTracking=%ld, ImpersonationLevel=%ld",
1525             SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1526       if (SecurityQos->Version >= 2)
1527       {
1528           const RPC_SECURITY_QOS_V2_W *SecurityQos2 = (const RPC_SECURITY_QOS_V2_W *)SecurityQos;
1529           TRACE(", AdditionalSecurityInfoType=%ld", SecurityQos2->AdditionalSecurityInfoType);
1530           if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1531               TRACE(", { %p, 0x%lx, %ld, %ld, %p, %s }",
1532                     SecurityQos2->u.HttpCredentials->TransportCredentials,
1533                     SecurityQos2->u.HttpCredentials->Flags,
1534                     SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1535                     SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1536                     SecurityQos2->u.HttpCredentials->AuthnSchemes,
1537                     debugstr_w(SecurityQos2->u.HttpCredentials->ServerCertificateSubject));
1538       }
1539       TRACE("}\n");
1540       status = RpcQualityOfService_Create(SecurityQos, TRUE, &bind->QOS);
1541       if (status != RPC_S_OK)
1542           return status;
1543   }
1544   else
1545   {
1546       if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1547       bind->QOS = NULL;
1548   }
1549
1550   if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1551     AuthnSvc = RPC_C_AUTHN_WINNT;
1552
1553   /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1554   if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1555     AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1556
1557   if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1558   {
1559     if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1560     bind->AuthInfo = NULL;
1561     return RPC_S_OK;
1562   }
1563
1564   if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1565   {
1566     FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1567     return RPC_S_UNKNOWN_AUTHN_LEVEL;
1568   }
1569
1570   /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1571   if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1572   {
1573     FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1574     return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1575   }
1576
1577   r = EnumerateSecurityPackagesW(&package_count, &packages);
1578   if (r != SEC_E_OK)
1579   {
1580     ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", r);
1581     return RPC_S_SEC_PKG_ERROR;
1582   }
1583
1584   for (i = 0; i < package_count; i++)
1585     if (packages[i].wRPCID == AuthnSvc)
1586         break;
1587
1588   if (i == package_count)
1589   {
1590     FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1591     FreeContextBuffer(packages);
1592     return RPC_S_UNKNOWN_AUTHN_SERVICE;
1593   }
1594
1595   TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name), AuthnSvc);
1596   r = AcquireCredentialsHandleW(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1597                                 AuthIdentity, NULL, NULL, &cred, &exp);
1598   cbMaxToken = packages[i].cbMaxToken;
1599   FreeContextBuffer(packages);
1600   if (r == ERROR_SUCCESS)
1601   {
1602     RpcAuthInfo *new_auth_info;
1603     r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1604                            AuthIdentity, &new_auth_info);
1605     if (r == RPC_S_OK)
1606     {
1607       new_auth_info->server_principal_name = RPCRT4_strdupW(ServerPrincName);
1608       if (!ServerPrincName || new_auth_info->server_principal_name)
1609       {
1610         if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1611         bind->AuthInfo = new_auth_info;
1612       }
1613       else
1614       {
1615         RpcAuthInfo_Release(new_auth_info);
1616         r = ERROR_OUTOFMEMORY;
1617       }
1618     }
1619     else
1620       FreeCredentialsHandle(&cred);
1621     return r;
1622   }
1623   else
1624   {
1625     ERR("AcquireCredentialsHandleW failed with error 0x%08x\n", r);
1626     return RPC_S_SEC_PKG_ERROR;
1627   }
1628 }
1629
1630 /***********************************************************************
1631  *             RpcBindingSetAuthInfoA (RPCRT4.@)
1632  */
1633 RPCRTAPI RPC_STATUS RPC_ENTRY
1634 RpcBindingSetAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName, ULONG AuthnLevel,
1635                         ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1636 {
1637     TRACE("%p %s %u %u %p %u\n", Binding, debugstr_a((const char*)ServerPrincName),
1638           AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1639     return RpcBindingSetAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1640 }
1641
1642 /***********************************************************************
1643  *             RpcBindingSetAuthInfoW (RPCRT4.@)
1644  */
1645 RPCRTAPI RPC_STATUS RPC_ENTRY
1646 RpcBindingSetAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1647                         ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1648 {
1649     TRACE("%p %s %u %u %p %u\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1650           AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1651     return RpcBindingSetAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1652 }
1653
1654 /***********************************************************************
1655  *             RpcBindingSetOption (RPCRT4.@)
1656  */
1657 RPC_STATUS WINAPI RpcBindingSetOption(RPC_BINDING_HANDLE BindingHandle, ULONG Option, ULONG_PTR OptionValue)
1658 {
1659     FIXME("(%p, %d, %ld): stub\n", BindingHandle, Option, OptionValue);
1660     return RPC_S_OK;
1661 }