rpcrt4: Ignore the AuthzSvr parameter for RPC_C_AUTHN_WINNT.
[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     if (ObjUuid) *ObjUuid = (unsigned char*)RPCRT4_strndupA(data, next - data);
485     data = next+1;
486   }
487
488   next = strchr(data, ':');
489   if (next) {
490     if (Protseq) *Protseq = (unsigned char*)RPCRT4_strndupA(data, next - data);
491     data = next+1;
492   }
493
494   next = strchr(data, '[');
495   if (next) {
496     CHAR *close, *opt;
497
498     if (NetworkAddr) *NetworkAddr = (unsigned char*)RPCRT4_strndupA(data, next - data);
499     data = next+1;
500     close = strchr(data, ']');
501     if (!close) goto fail;
502
503     /* tokenize options */
504     while (data < close) {
505       next = strchr(data, ',');
506       if (!next || next > close) next = close;
507       /* FIXME: this is kind of inefficient */
508       opt = RPCRT4_strndupA(data, next - data);
509       data = next+1;
510
511       /* parse option */
512       next = strchr(opt, '=');
513       if (!next) {
514         /* not an option, must be an endpoint */
515         if (endpoint_already_found) goto fail;
516         if (Endpoint) *Endpoint = (unsigned char*) opt;
517         else HeapFree(GetProcessHeap(), 0, opt);
518         endpoint_already_found = TRUE;
519       } else {
520         if (strncmp(opt, ep_opt, strlen(ep_opt)) == 0) {
521           /* endpoint option */
522           if (endpoint_already_found) goto fail;
523           if (Endpoint) *Endpoint = (unsigned char*) RPCRT4_strdupA(next+1);
524           HeapFree(GetProcessHeap(), 0, opt);
525           endpoint_already_found = TRUE;
526         } else {
527           /* network option */
528           if (Options) {
529             if (*Options) {
530               /* FIXME: this is kind of inefficient */
531               *Options = (unsigned char*) RPCRT4_strconcatA( (char*)*Options, opt);
532               HeapFree(GetProcessHeap(), 0, opt);
533             } else
534               *Options = (unsigned char*) opt;
535           } else
536             HeapFree(GetProcessHeap(), 0, opt);
537         }
538       }
539     }
540
541     data = close+1;
542     if (*data) goto fail;
543   }
544   else if (NetworkAddr) 
545     *NetworkAddr = (unsigned char*)RPCRT4_strdupA(data);
546
547   return RPC_S_OK;
548
549 fail:
550   if (ObjUuid) RpcStringFreeA((unsigned char**)ObjUuid);
551   if (Protseq) RpcStringFreeA((unsigned char**)Protseq);
552   if (NetworkAddr) RpcStringFreeA((unsigned char**)NetworkAddr);
553   if (Endpoint) RpcStringFreeA((unsigned char**)Endpoint);
554   if (Options) RpcStringFreeA((unsigned char**)Options);
555   return RPC_S_INVALID_STRING_BINDING;
556 }
557
558 /***********************************************************************
559  *             RpcStringBindingParseW (RPCRT4.@)
560  */
561 RPC_STATUS WINAPI RpcStringBindingParseW( RPC_WSTR StringBinding, RPC_WSTR *ObjUuid,
562                                           RPC_WSTR *Protseq, RPC_WSTR *NetworkAddr,
563                                           RPC_WSTR *Endpoint, RPC_WSTR *Options)
564 {
565   WCHAR *data, *next;
566   static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
567   BOOL endpoint_already_found = FALSE;
568
569   TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
570        ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
571
572   if (ObjUuid) *ObjUuid = NULL;
573   if (Protseq) *Protseq = NULL;
574   if (NetworkAddr) *NetworkAddr = NULL;
575   if (Endpoint) *Endpoint = NULL;
576   if (Options) *Options = NULL;
577
578   data = StringBinding;
579
580   next = strchrW(data, '@');
581   if (next) {
582     if (ObjUuid) *ObjUuid = RPCRT4_strndupW(data, next - data);
583     data = next+1;
584   }
585
586   next = strchrW(data, ':');
587   if (next) {
588     if (Protseq) *Protseq = RPCRT4_strndupW(data, next - data);
589     data = next+1;
590   }
591
592   next = strchrW(data, '[');
593   if (next) {
594     WCHAR *close, *opt;
595
596     if (NetworkAddr) *NetworkAddr = RPCRT4_strndupW(data, next - data);
597     data = next+1;
598     close = strchrW(data, ']');
599     if (!close) goto fail;
600
601     /* tokenize options */
602     while (data < close) {
603       next = strchrW(data, ',');
604       if (!next || next > close) next = close;
605       /* FIXME: this is kind of inefficient */
606       opt = RPCRT4_strndupW(data, next - data);
607       data = next+1;
608
609       /* parse option */
610       next = strchrW(opt, '=');
611       if (!next) {
612         /* not an option, must be an endpoint */
613         if (endpoint_already_found) goto fail;
614         if (Endpoint) *Endpoint = opt;
615         else HeapFree(GetProcessHeap(), 0, opt);
616         endpoint_already_found = TRUE;
617       } else {
618         if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
619           /* endpoint option */
620           if (endpoint_already_found) goto fail;
621           if (Endpoint) *Endpoint = RPCRT4_strdupW(next+1);
622           HeapFree(GetProcessHeap(), 0, opt);
623           endpoint_already_found = TRUE;
624         } else {
625           /* network option */
626           if (Options) {
627             if (*Options) {
628               /* FIXME: this is kind of inefficient */
629               *Options = RPCRT4_strconcatW(*Options, opt);
630               HeapFree(GetProcessHeap(), 0, opt);
631             } else
632               *Options = opt;
633           } else
634             HeapFree(GetProcessHeap(), 0, opt);
635         }
636       }
637     }
638
639     data = close+1;
640     if (*data) goto fail;
641   } else if (NetworkAddr) 
642     *NetworkAddr = RPCRT4_strdupW(data);
643
644   return RPC_S_OK;
645
646 fail:
647   if (ObjUuid) RpcStringFreeW(ObjUuid);
648   if (Protseq) RpcStringFreeW(Protseq);
649   if (NetworkAddr) RpcStringFreeW(NetworkAddr);
650   if (Endpoint) RpcStringFreeW(Endpoint);
651   if (Options) RpcStringFreeW(Options);
652   return RPC_S_INVALID_STRING_BINDING;
653 }
654
655 /***********************************************************************
656  *             RpcBindingFree (RPCRT4.@)
657  */
658 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
659 {
660   RPC_STATUS status;
661   TRACE("(%p) = %p\n", Binding, *Binding);
662   status = RPCRT4_ReleaseBinding(*Binding);
663   if (status == RPC_S_OK) *Binding = 0;
664   return status;
665 }
666   
667 /***********************************************************************
668  *             RpcBindingVectorFree (RPCRT4.@)
669  */
670 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
671 {
672   RPC_STATUS status;
673   unsigned long c;
674
675   TRACE("(%p)\n", BindingVector);
676   for (c=0; c<(*BindingVector)->Count; c++) {
677     status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
678   }
679   HeapFree(GetProcessHeap(), 0, *BindingVector);
680   *BindingVector = NULL;
681   return RPC_S_OK;
682 }
683   
684 /***********************************************************************
685  *             RpcBindingInqObject (RPCRT4.@)
686  */
687 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
688 {
689   RpcBinding* bind = (RpcBinding*)Binding;
690
691   TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
692   *ObjectUuid = bind->ObjectUuid;
693   return RPC_S_OK;
694 }
695
696 /***********************************************************************
697  *             RpcBindingSetObject (RPCRT4.@)
698  */
699 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
700 {
701   RpcBinding* bind = (RpcBinding*)Binding;
702
703   TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
704   if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
705   return RPCRT4_SetBindingObject(Binding, ObjectUuid);
706 }
707
708 /***********************************************************************
709  *             RpcBindingFromStringBindingA (RPCRT4.@)
710  */
711 RPC_STATUS WINAPI RpcBindingFromStringBindingA( RPC_CSTR StringBinding, RPC_BINDING_HANDLE* Binding )
712 {
713   RPC_STATUS ret;
714   RpcBinding* bind = NULL;
715   RPC_CSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
716   UUID Uuid;
717
718   TRACE("(%s,%p)\n", debugstr_a((char*)StringBinding), Binding);
719
720   ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
721                               &NetworkAddr, &Endpoint, &Options);
722   if (ret != RPC_S_OK) return ret;
723
724   ret = UuidFromStringA(ObjectUuid, &Uuid);
725
726   if (ret == RPC_S_OK)
727     ret = RPCRT4_CreateBindingA(&bind, FALSE, (char*)Protseq);
728   if (ret != RPC_S_OK) return ret;
729   ret = RPCRT4_SetBindingObject(bind, &Uuid);
730   if (ret == RPC_S_OK)
731     ret = RPCRT4_CompleteBindingA(bind, (char*)NetworkAddr, (char*)Endpoint, (char*)Options);
732
733   RpcStringFreeA((unsigned char**)&Options);
734   RpcStringFreeA((unsigned char**)&Endpoint);
735   RpcStringFreeA((unsigned char**)&NetworkAddr);
736   RpcStringFreeA((unsigned char**)&Protseq);
737   RpcStringFreeA((unsigned char**)&ObjectUuid);
738
739   if (ret == RPC_S_OK) 
740     *Binding = (RPC_BINDING_HANDLE)bind;
741   else 
742     RPCRT4_ReleaseBinding(bind);
743
744   return ret;
745 }
746
747 /***********************************************************************
748  *             RpcBindingFromStringBindingW (RPCRT4.@)
749  */
750 RPC_STATUS WINAPI RpcBindingFromStringBindingW( RPC_WSTR StringBinding, RPC_BINDING_HANDLE* Binding )
751 {
752   RPC_STATUS ret;
753   RpcBinding* bind = NULL;
754   RPC_WSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
755   UUID Uuid;
756
757   TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
758
759   ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
760                               &NetworkAddr, &Endpoint, &Options);
761   if (ret != RPC_S_OK) return ret;
762
763   ret = UuidFromStringW(ObjectUuid, &Uuid);
764
765   if (ret == RPC_S_OK)
766     ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
767   if (ret != RPC_S_OK) return ret;
768   ret = RPCRT4_SetBindingObject(bind, &Uuid);
769   if (ret == RPC_S_OK)
770     ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
771
772   RpcStringFreeW(&Options);
773   RpcStringFreeW(&Endpoint);
774   RpcStringFreeW(&NetworkAddr);
775   RpcStringFreeW(&Protseq);
776   RpcStringFreeW(&ObjectUuid);
777
778   if (ret == RPC_S_OK)
779     *Binding = (RPC_BINDING_HANDLE)bind;
780   else
781     RPCRT4_ReleaseBinding(bind);
782
783   return ret;
784 }
785   
786 /***********************************************************************
787  *             RpcBindingToStringBindingA (RPCRT4.@)
788  */
789 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, RPC_CSTR *StringBinding )
790 {
791   RPC_STATUS ret;
792   RpcBinding* bind = (RpcBinding*)Binding;
793   RPC_CSTR ObjectUuid;
794
795   TRACE("(%p,%p)\n", Binding, StringBinding);
796
797   ret = UuidToStringA(&bind->ObjectUuid, &ObjectUuid);
798   if (ret != RPC_S_OK) return ret;
799
800   ret = RpcStringBindingComposeA(ObjectUuid, (unsigned char*)bind->Protseq, (unsigned char*) bind->NetworkAddr,
801                                  (unsigned char*) bind->Endpoint, NULL, StringBinding);
802
803   RpcStringFreeA(&ObjectUuid);
804
805   return ret;
806 }
807   
808 /***********************************************************************
809  *             RpcBindingToStringBindingW (RPCRT4.@)
810  */
811 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, RPC_WSTR *StringBinding )
812 {
813   RPC_STATUS ret;
814   unsigned char *str = NULL;
815   TRACE("(%p,%p)\n", Binding, StringBinding);
816   ret = RpcBindingToStringBindingA(Binding, &str);
817   *StringBinding = RPCRT4_strdupAtoW((char*)str);
818   RpcStringFreeA((unsigned char**)&str);
819   return ret;
820 }
821
822 /***********************************************************************
823  *             I_RpcBindingSetAsync (RPCRT4.@)
824  * NOTES
825  *  Exists in win9x and winNT, but with different number of arguments
826  *  (9x version has 3 arguments, NT has 2).
827  */
828 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
829 {
830   RpcBinding* bind = (RpcBinding*)Binding;
831
832   TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
833
834   bind->BlockingFn = BlockingFn;
835
836   return RPC_S_OK;
837 }
838
839 /***********************************************************************
840  *             RpcBindingCopy (RPCRT4.@)
841  */
842 RPC_STATUS RPC_ENTRY RpcBindingCopy(
843   RPC_BINDING_HANDLE SourceBinding,
844   RPC_BINDING_HANDLE* DestinationBinding)
845 {
846   RpcBinding *DestBinding;
847   RpcBinding *SrcBinding = (RpcBinding*)SourceBinding;
848   RPC_STATUS status;
849
850   TRACE("(%p, %p)\n", SourceBinding, DestinationBinding);
851
852   status = RPCRT4_AllocBinding(&DestBinding, SrcBinding->server);
853   if (status != RPC_S_OK) return status;
854
855   DestBinding->ObjectUuid = SrcBinding->ObjectUuid;
856   DestBinding->BlockingFn = SrcBinding->BlockingFn;
857   DestBinding->Protseq = RPCRT4_strndupA(SrcBinding->Protseq, -1);
858   DestBinding->NetworkAddr = RPCRT4_strndupA(SrcBinding->NetworkAddr, -1);
859   DestBinding->Endpoint = RPCRT4_strndupA(SrcBinding->Endpoint, -1);
860   DestBinding->NetworkOptions = RPCRT4_strdupW(SrcBinding->NetworkOptions);
861   if (SrcBinding->Assoc) SrcBinding->Assoc->refs++;
862   DestBinding->Assoc = SrcBinding->Assoc;
863
864   if (SrcBinding->AuthInfo) RpcAuthInfo_AddRef(SrcBinding->AuthInfo);
865   DestBinding->AuthInfo = SrcBinding->AuthInfo;
866   if (SrcBinding->QOS) RpcQualityOfService_AddRef(SrcBinding->QOS);
867   DestBinding->QOS = SrcBinding->QOS;
868
869   *DestinationBinding = DestBinding;
870   return RPC_S_OK;
871 }
872
873 /***********************************************************************
874  *             RpcImpersonateClient (RPCRT4.@)
875  *
876  * Impersonates the client connected via a binding handle so that security
877  * checks are done in the context of the client.
878  *
879  * PARAMS
880  *  BindingHandle [I] Handle to the binding to the client.
881  *
882  * RETURNS
883  *  Success: RPS_S_OK.
884  *  Failure: RPC_STATUS value.
885  *
886  * NOTES
887  *
888  * If BindingHandle is NULL then the function impersonates the client
889  * connected to the binding handle of the current thread.
890  */
891 RPC_STATUS WINAPI RpcImpersonateClient(RPC_BINDING_HANDLE BindingHandle)
892 {
893     FIXME("(%p): stub\n", BindingHandle);
894     ImpersonateSelf(SecurityImpersonation);
895     return RPC_S_OK;
896 }
897
898 /***********************************************************************
899  *             RpcRevertToSelfEx (RPCRT4.@)
900  *
901  * Stops impersonating the client connected to the binding handle so that security
902  * checks are no longer done in the context of the client.
903  *
904  * PARAMS
905  *  BindingHandle [I] Handle to the binding to the client.
906  *
907  * RETURNS
908  *  Success: RPS_S_OK.
909  *  Failure: RPC_STATUS value.
910  *
911  * NOTES
912  *
913  * If BindingHandle is NULL then the function stops impersonating the client
914  * connected to the binding handle of the current thread.
915  */
916 RPC_STATUS WINAPI RpcRevertToSelfEx(RPC_BINDING_HANDLE BindingHandle)
917 {
918     FIXME("(%p): stub\n", BindingHandle);
919     return RPC_S_OK;
920 }
921
922 static inline BOOL has_nt_auth_identity(ULONG AuthnLevel)
923 {
924     switch (AuthnLevel)
925     {
926     case RPC_C_AUTHN_GSS_NEGOTIATE:
927     case RPC_C_AUTHN_WINNT:
928     case RPC_C_AUTHN_GSS_KERBEROS:
929         return TRUE;
930     default:
931         return FALSE;
932     }
933 }
934
935 static RPC_STATUS RpcAuthInfo_Create(ULONG AuthnLevel, ULONG AuthnSvc,
936                                      CredHandle cred, TimeStamp exp,
937                                      ULONG cbMaxToken,
938                                      RPC_AUTH_IDENTITY_HANDLE identity,
939                                      RpcAuthInfo **ret)
940 {
941     RpcAuthInfo *AuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo));
942     if (!AuthInfo)
943         return ERROR_OUTOFMEMORY;
944
945     AuthInfo->refs = 1;
946     AuthInfo->AuthnLevel = AuthnLevel;
947     AuthInfo->AuthnSvc = AuthnSvc;
948     AuthInfo->cred = cred;
949     AuthInfo->exp = exp;
950     AuthInfo->cbMaxToken = cbMaxToken;
951     AuthInfo->identity = identity;
952     AuthInfo->server_principal_name = NULL;
953
954     /* duplicate the SEC_WINNT_AUTH_IDENTITY structure, if applicable, to
955      * enable better matching in RpcAuthInfo_IsEqual */
956     if (identity && has_nt_auth_identity(AuthnSvc))
957     {
958         const SEC_WINNT_AUTH_IDENTITY_W *nt_identity = identity;
959         AuthInfo->nt_identity = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo->nt_identity));
960         if (!AuthInfo->nt_identity)
961         {
962             HeapFree(GetProcessHeap(), 0, AuthInfo);
963             return ERROR_OUTOFMEMORY;
964         }
965
966         AuthInfo->nt_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
967         if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
968             AuthInfo->nt_identity->User = RPCRT4_strndupW(nt_identity->User, nt_identity->UserLength);
969         else
970             AuthInfo->nt_identity->User = RPCRT4_strndupAtoW((const char *)nt_identity->User, nt_identity->UserLength);
971         AuthInfo->nt_identity->UserLength = nt_identity->UserLength;
972         if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
973             AuthInfo->nt_identity->Domain = RPCRT4_strndupW(nt_identity->Domain, nt_identity->DomainLength);
974         else
975             AuthInfo->nt_identity->Domain = RPCRT4_strndupAtoW((const char *)nt_identity->Domain, nt_identity->DomainLength);
976         AuthInfo->nt_identity->DomainLength = nt_identity->DomainLength;
977         if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
978             AuthInfo->nt_identity->Password = RPCRT4_strndupW(nt_identity->Password, nt_identity->PasswordLength);
979         else
980             AuthInfo->nt_identity->Password = RPCRT4_strndupAtoW((const char *)nt_identity->Password, nt_identity->PasswordLength);
981         AuthInfo->nt_identity->PasswordLength = nt_identity->PasswordLength;
982
983         if ((nt_identity->User && !AuthInfo->nt_identity->User) ||
984             (nt_identity->Domain && !AuthInfo->nt_identity->Domain) ||
985             (nt_identity->Password && !AuthInfo->nt_identity->Password))
986         {
987             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
988             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
989             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
990             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
991             HeapFree(GetProcessHeap(), 0, AuthInfo);
992             return ERROR_OUTOFMEMORY;
993         }
994     }
995     else
996         AuthInfo->nt_identity = NULL;
997     *ret = AuthInfo;
998     return RPC_S_OK;
999 }
1000
1001 ULONG RpcAuthInfo_AddRef(RpcAuthInfo *AuthInfo)
1002 {
1003     return InterlockedIncrement(&AuthInfo->refs);
1004 }
1005
1006 ULONG RpcAuthInfo_Release(RpcAuthInfo *AuthInfo)
1007 {
1008     ULONG refs = InterlockedDecrement(&AuthInfo->refs);
1009
1010     if (!refs)
1011     {
1012         FreeCredentialsHandle(&AuthInfo->cred);
1013         if (AuthInfo->nt_identity)
1014         {
1015             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
1016             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1017             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1018             HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
1019         }
1020         HeapFree(GetProcessHeap(), 0, AuthInfo->server_principal_name);
1021         HeapFree(GetProcessHeap(), 0, AuthInfo);
1022     }
1023
1024     return refs;
1025 }
1026
1027 BOOL RpcAuthInfo_IsEqual(const RpcAuthInfo *AuthInfo1, const RpcAuthInfo *AuthInfo2)
1028 {
1029     if (AuthInfo1 == AuthInfo2)
1030         return TRUE;
1031
1032     if (!AuthInfo1 || !AuthInfo2)
1033         return FALSE;
1034
1035     if ((AuthInfo1->AuthnLevel != AuthInfo2->AuthnLevel) ||
1036         (AuthInfo1->AuthnSvc != AuthInfo2->AuthnSvc))
1037         return FALSE;
1038
1039     if (AuthInfo1->identity == AuthInfo2->identity)
1040         return TRUE;
1041
1042     if (!AuthInfo1->identity || !AuthInfo2->identity)
1043         return FALSE;
1044
1045     if (has_nt_auth_identity(AuthInfo1->AuthnSvc))
1046     {
1047         const SEC_WINNT_AUTH_IDENTITY_W *identity1 = AuthInfo1->nt_identity;
1048         const SEC_WINNT_AUTH_IDENTITY_W *identity2 = AuthInfo2->nt_identity;
1049         /* compare user names */
1050         if (identity1->UserLength != identity2->UserLength ||
1051             memcmp(identity1->User, identity2->User, identity1->UserLength))
1052             return FALSE;
1053         /* compare domain names */
1054         if (identity1->DomainLength != identity2->DomainLength ||
1055             memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1056             return FALSE;
1057         /* compare passwords */
1058         if (identity1->PasswordLength != identity2->PasswordLength ||
1059             memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1060             return FALSE;
1061     }
1062     else
1063         return FALSE;
1064
1065     return TRUE;
1066 }
1067
1068 static RPC_STATUS RpcQualityOfService_Create(const RPC_SECURITY_QOS *qos_src, BOOL unicode, RpcQualityOfService **qos_dst)
1069 {
1070     RpcQualityOfService *qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos));
1071
1072     if (!qos)
1073         return RPC_S_OUT_OF_RESOURCES;
1074
1075     qos->refs = 1;
1076     qos->qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos->qos));
1077     if (!qos->qos) goto error;
1078     qos->qos->Version = qos_src->Version;
1079     qos->qos->Capabilities = qos_src->Capabilities;
1080     qos->qos->IdentityTracking = qos_src->IdentityTracking;
1081     qos->qos->ImpersonationType = qos_src->ImpersonationType;
1082     qos->qos->AdditionalSecurityInfoType = 0;
1083
1084     if (qos_src->Version >= 2)
1085     {
1086         const RPC_SECURITY_QOS_V2_W *qos_src2 = (const RPC_SECURITY_QOS_V2_W *)qos_src;
1087         qos->qos->AdditionalSecurityInfoType = qos_src2->AdditionalSecurityInfoType;
1088         if (qos_src2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1089         {
1090             const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_src = qos_src2->u.HttpCredentials;
1091             RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_dst;
1092
1093             http_credentials_dst = HeapAlloc(GetProcessHeap(), 0, sizeof(*http_credentials_dst));
1094             qos->qos->u.HttpCredentials = http_credentials_dst;
1095             if (!http_credentials_dst) goto error;
1096             http_credentials_dst->TransportCredentials = NULL;
1097             http_credentials_dst->Flags = http_credentials_src->Flags;
1098             http_credentials_dst->AuthenticationTarget = http_credentials_src->AuthenticationTarget;
1099             http_credentials_dst->NumberOfAuthnSchemes = http_credentials_src->NumberOfAuthnSchemes;
1100             http_credentials_dst->AuthnSchemes = NULL;
1101             http_credentials_dst->ServerCertificateSubject = NULL;
1102             if (http_credentials_src->TransportCredentials)
1103             {
1104                 SEC_WINNT_AUTH_IDENTITY_W *cred_dst;
1105                 cred_dst = http_credentials_dst->TransportCredentials = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cred_dst));
1106                 if (!cred_dst) goto error;
1107                 cred_dst->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1108                 if (unicode)
1109                 {
1110                     const SEC_WINNT_AUTH_IDENTITY_W *cred_src = http_credentials_src->TransportCredentials;
1111                     cred_dst->UserLength = cred_src->UserLength;
1112                     cred_dst->PasswordLength = cred_src->PasswordLength;
1113                     cred_dst->DomainLength = cred_src->DomainLength;
1114                     cred_dst->User = RPCRT4_strndupW(cred_src->User, cred_src->UserLength);
1115                     cred_dst->Password = RPCRT4_strndupW(cred_src->Password, cred_src->PasswordLength);
1116                     cred_dst->Domain = RPCRT4_strndupW(cred_src->Domain, cred_src->DomainLength);
1117                 }
1118                 else
1119                 {
1120                     const SEC_WINNT_AUTH_IDENTITY_A *cred_src = (const SEC_WINNT_AUTH_IDENTITY_A *)http_credentials_src->TransportCredentials;
1121                     cred_dst->UserLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, NULL, 0);
1122                     cred_dst->DomainLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, NULL, 0);
1123                     cred_dst->PasswordLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, NULL, 0);
1124                     cred_dst->User = HeapAlloc(GetProcessHeap(), 0, cred_dst->UserLength * sizeof(WCHAR));
1125                     cred_dst->Password = HeapAlloc(GetProcessHeap(), 0, cred_dst->PasswordLength * sizeof(WCHAR));
1126                     cred_dst->Domain = HeapAlloc(GetProcessHeap(), 0, cred_dst->DomainLength * sizeof(WCHAR));
1127                     if (!cred_dst || !cred_dst->Password || !cred_dst->Domain) goto error;
1128                     MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, cred_dst->User, cred_dst->UserLength);
1129                     MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, cred_dst->Domain, cred_dst->DomainLength);
1130                     MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, cred_dst->Password, cred_dst->PasswordLength);
1131                 }
1132             }
1133             if (http_credentials_src->NumberOfAuthnSchemes)
1134             {
1135                 http_credentials_dst->AuthnSchemes = HeapAlloc(GetProcessHeap(), 0, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1136                 if (!http_credentials_dst->AuthnSchemes) goto error;
1137                 memcpy(http_credentials_dst->AuthnSchemes, http_credentials_src->AuthnSchemes, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1138             }
1139             if (http_credentials_src->ServerCertificateSubject)
1140             {
1141                 if (unicode)
1142                     http_credentials_dst->ServerCertificateSubject =
1143                         RPCRT4_strndupW(http_credentials_src->ServerCertificateSubject,
1144                                         strlenW(http_credentials_src->ServerCertificateSubject));
1145                 else
1146                     http_credentials_dst->ServerCertificateSubject =
1147                         RPCRT4_strdupAtoW((char *)http_credentials_src->ServerCertificateSubject);
1148                 if (!http_credentials_dst->ServerCertificateSubject) goto error;
1149             }
1150         }
1151     }
1152     *qos_dst = qos;
1153     return RPC_S_OK;
1154
1155 error:
1156     if (qos->qos)
1157     {
1158         if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP &&
1159             qos->qos->u.HttpCredentials)
1160         {
1161             if (qos->qos->u.HttpCredentials->TransportCredentials)
1162             {
1163                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1164                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1165                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1166                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1167             }
1168             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1169             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1170             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1171         }
1172         HeapFree(GetProcessHeap(), 0, qos->qos);
1173     }
1174     HeapFree(GetProcessHeap(), 0, qos);
1175     return RPC_S_OUT_OF_RESOURCES;
1176 }
1177
1178 ULONG RpcQualityOfService_AddRef(RpcQualityOfService *qos)
1179 {
1180     return InterlockedIncrement(&qos->refs);
1181 }
1182
1183 ULONG RpcQualityOfService_Release(RpcQualityOfService *qos)
1184 {
1185     ULONG refs = InterlockedDecrement(&qos->refs);
1186
1187     if (!refs)
1188     {
1189         if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1190         {
1191             if (qos->qos->u.HttpCredentials->TransportCredentials)
1192             {
1193                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1194                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1195                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1196                 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1197             }
1198             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1199             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1200             HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1201         }
1202         HeapFree(GetProcessHeap(), 0, qos->qos);
1203         HeapFree(GetProcessHeap(), 0, qos);
1204     }
1205     return refs;
1206 }
1207
1208 BOOL RpcQualityOfService_IsEqual(const RpcQualityOfService *qos1, const RpcQualityOfService *qos2)
1209 {
1210     if (qos1 == qos2)
1211         return TRUE;
1212
1213     if (!qos1 || !qos2)
1214         return FALSE;
1215
1216     TRACE("qos1 = { %ld %ld %ld %ld }, qos2 = { %ld %ld %ld %ld }\n",
1217         qos1->qos->Capabilities, qos1->qos->IdentityTracking,
1218         qos1->qos->ImpersonationType, qos1->qos->AdditionalSecurityInfoType,
1219         qos2->qos->Capabilities, qos2->qos->IdentityTracking,
1220         qos2->qos->ImpersonationType, qos2->qos->AdditionalSecurityInfoType);
1221
1222     if ((qos1->qos->Capabilities != qos2->qos->Capabilities) ||
1223         (qos1->qos->IdentityTracking != qos2->qos->IdentityTracking) ||
1224         (qos1->qos->ImpersonationType != qos2->qos->ImpersonationType) ||
1225         (qos1->qos->AdditionalSecurityInfoType != qos2->qos->AdditionalSecurityInfoType))
1226         return FALSE;
1227
1228     if (qos1->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1229     {
1230         const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials1 = qos1->qos->u.HttpCredentials;
1231         const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials2 = qos2->qos->u.HttpCredentials;
1232
1233         if (http_credentials1->Flags != http_credentials2->Flags)
1234             return FALSE;
1235
1236         if (http_credentials1->AuthenticationTarget != http_credentials2->AuthenticationTarget)
1237             return FALSE;
1238
1239         /* authentication schemes and server certificate subject not currently used */
1240
1241         if (http_credentials1->TransportCredentials != http_credentials2->TransportCredentials)
1242         {
1243             const SEC_WINNT_AUTH_IDENTITY_W *identity1 = http_credentials1->TransportCredentials;
1244             const SEC_WINNT_AUTH_IDENTITY_W *identity2 = http_credentials2->TransportCredentials;
1245
1246             if (!identity1 || !identity2)
1247                 return FALSE;
1248
1249             /* compare user names */
1250             if (identity1->UserLength != identity2->UserLength ||
1251                 memcmp(identity1->User, identity2->User, identity1->UserLength))
1252                 return FALSE;
1253             /* compare domain names */
1254             if (identity1->DomainLength != identity2->DomainLength ||
1255                 memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1256                 return FALSE;
1257             /* compare passwords */
1258             if (identity1->PasswordLength != identity2->PasswordLength ||
1259                 memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1260                 return FALSE;
1261         }
1262     }
1263
1264     return TRUE;
1265 }
1266
1267 /***********************************************************************
1268  *             RpcRevertToSelf (RPCRT4.@)
1269  */
1270 RPC_STATUS WINAPI RpcRevertToSelf(void)
1271 {
1272     FIXME("stub\n");
1273     RevertToSelf();
1274     return RPC_S_OK;
1275 }
1276
1277 /***********************************************************************
1278  *             RpcMgmtSetComTimeout (RPCRT4.@)
1279  */
1280 RPC_STATUS WINAPI RpcMgmtSetComTimeout(RPC_BINDING_HANDLE BindingHandle, unsigned int Timeout)
1281 {
1282     FIXME("(%p, %d): stub\n", BindingHandle, Timeout);
1283     return RPC_S_OK;
1284 }
1285
1286 /***********************************************************************
1287  *             RpcBindingInqAuthInfoExA (RPCRT4.@)
1288  */
1289 RPCRTAPI RPC_STATUS RPC_ENTRY
1290 RpcBindingInqAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1291                           ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1292                           ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1293 {
1294     FIXME("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1295           AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1296     return RPC_S_INVALID_BINDING;
1297 }
1298
1299 /***********************************************************************
1300  *             RpcBindingInqAuthInfoExW (RPCRT4.@)
1301  */
1302 RPCRTAPI RPC_STATUS RPC_ENTRY
1303 RpcBindingInqAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1304                           ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1305                           ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1306 {
1307     FIXME("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1308           AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1309     return RPC_S_INVALID_BINDING;
1310 }
1311
1312 /***********************************************************************
1313  *             RpcBindingInqAuthInfoA (RPCRT4.@)
1314  */
1315 RPCRTAPI RPC_STATUS RPC_ENTRY
1316 RpcBindingInqAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1317                         ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1318 {
1319     FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
1320           AuthnSvc, AuthIdentity, AuthzSvc);
1321     return RPC_S_INVALID_BINDING;
1322 }
1323
1324 /***********************************************************************
1325  *             RpcBindingInqAuthInfoW (RPCRT4.@)
1326  */
1327 RPCRTAPI RPC_STATUS RPC_ENTRY
1328 RpcBindingInqAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1329                         ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1330 {
1331     FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
1332           AuthnSvc, AuthIdentity, AuthzSvc);
1333     return RPC_S_INVALID_BINDING;
1334 }
1335
1336 /***********************************************************************
1337  *             RpcBindingSetAuthInfoExA (RPCRT4.@)
1338  */
1339 RPCRTAPI RPC_STATUS RPC_ENTRY
1340 RpcBindingSetAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName,
1341                           ULONG AuthnLevel, ULONG AuthnSvc,
1342                           RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1343                           RPC_SECURITY_QOS *SecurityQos )
1344 {
1345   RpcBinding* bind = (RpcBinding*)Binding;
1346   SECURITY_STATUS r;
1347   CredHandle cred;
1348   TimeStamp exp;
1349   ULONG package_count;
1350   ULONG i;
1351   PSecPkgInfoA packages;
1352   ULONG cbMaxToken;
1353
1354   TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_a((const char*)ServerPrincName),
1355         AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1356
1357   if (SecurityQos)
1358   {
1359       RPC_STATUS status;
1360
1361       TRACE("SecurityQos { Version=%ld, Capabilties=0x%lx, IdentityTracking=%ld, ImpersonationLevel=%ld",
1362             SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1363       if (SecurityQos->Version >= 2)
1364       {
1365           const RPC_SECURITY_QOS_V2_A *SecurityQos2 = (const RPC_SECURITY_QOS_V2_A *)SecurityQos;
1366           TRACE(", AdditionalSecurityInfoType=%ld", SecurityQos2->AdditionalSecurityInfoType);
1367           if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1368               TRACE(", { %p, 0x%lx, %ld, %ld, %p, %s }",
1369                     SecurityQos2->u.HttpCredentials->TransportCredentials,
1370                     SecurityQos2->u.HttpCredentials->Flags,
1371                     SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1372                     SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1373                     SecurityQos2->u.HttpCredentials->AuthnSchemes,
1374                     SecurityQos2->u.HttpCredentials->ServerCertificateSubject);
1375       }
1376       TRACE("}\n");
1377       status = RpcQualityOfService_Create(SecurityQos, FALSE, &bind->QOS);
1378       if (status != RPC_S_OK)
1379           return status;
1380   }
1381   else
1382   {
1383       if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1384       bind->QOS = NULL;
1385   }
1386
1387   if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1388     AuthnSvc = RPC_C_AUTHN_WINNT;
1389
1390   /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1391   if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1392     AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1393
1394   if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1395   {
1396     if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1397     bind->AuthInfo = NULL;
1398     return RPC_S_OK;
1399   }
1400
1401   if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1402   {
1403     FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1404     return RPC_S_UNKNOWN_AUTHN_LEVEL;
1405   }
1406
1407   /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1408   if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1409   {
1410     FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1411     return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1412   }
1413
1414   r = EnumerateSecurityPackagesA(&package_count, &packages);
1415   if (r != SEC_E_OK)
1416   {
1417     ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n", r);
1418     return RPC_S_SEC_PKG_ERROR;
1419   }
1420
1421   for (i = 0; i < package_count; i++)
1422     if (packages[i].wRPCID == AuthnSvc)
1423         break;
1424
1425   if (i == package_count)
1426   {
1427     FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1428     FreeContextBuffer(packages);
1429     return RPC_S_UNKNOWN_AUTHN_SERVICE;
1430   }
1431
1432   TRACE("found package %s for service %u\n", packages[i].Name, AuthnSvc);
1433   r = AcquireCredentialsHandleA(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1434                                 AuthIdentity, NULL, NULL, &cred, &exp);
1435   cbMaxToken = packages[i].cbMaxToken;
1436   FreeContextBuffer(packages);
1437   if (r == ERROR_SUCCESS)
1438   {
1439     RpcAuthInfo *new_auth_info;
1440     r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1441                            AuthIdentity, &new_auth_info);
1442     if (r == RPC_S_OK)
1443     {
1444       new_auth_info->server_principal_name = RPCRT4_strdupAtoW((char *)ServerPrincName);
1445       if (new_auth_info->server_principal_name)
1446       {
1447         if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1448         bind->AuthInfo = new_auth_info;
1449       }
1450       else
1451       {
1452         RpcAuthInfo_Release(new_auth_info);
1453         r = ERROR_OUTOFMEMORY;
1454       }
1455     }
1456     else
1457       FreeCredentialsHandle(&cred);
1458     return r;
1459   }
1460   else
1461   {
1462     ERR("AcquireCredentialsHandleA failed with error 0x%08x\n", r);
1463     return RPC_S_SEC_PKG_ERROR;
1464   }
1465 }
1466
1467 /***********************************************************************
1468  *             RpcBindingSetAuthInfoExW (RPCRT4.@)
1469  */
1470 RPCRTAPI RPC_STATUS RPC_ENTRY
1471 RpcBindingSetAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1472                           ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1473                           RPC_SECURITY_QOS *SecurityQos )
1474 {
1475   RpcBinding* bind = (RpcBinding*)Binding;
1476   SECURITY_STATUS r;
1477   CredHandle cred;
1478   TimeStamp exp;
1479   ULONG package_count;
1480   ULONG i;
1481   PSecPkgInfoW packages;
1482   ULONG cbMaxToken;
1483
1484   TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1485         AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1486
1487   if (SecurityQos)
1488   {
1489       RPC_STATUS status;
1490
1491       TRACE("SecurityQos { Version=%ld, Capabilties=0x%lx, IdentityTracking=%ld, ImpersonationLevel=%ld",
1492             SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1493       if (SecurityQos->Version >= 2)
1494       {
1495           const RPC_SECURITY_QOS_V2_W *SecurityQos2 = (const RPC_SECURITY_QOS_V2_W *)SecurityQos;
1496           TRACE(", AdditionalSecurityInfoType=%ld", SecurityQos2->AdditionalSecurityInfoType);
1497           if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1498               TRACE(", { %p, 0x%lx, %ld, %ld, %p, %s }",
1499                     SecurityQos2->u.HttpCredentials->TransportCredentials,
1500                     SecurityQos2->u.HttpCredentials->Flags,
1501                     SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1502                     SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1503                     SecurityQos2->u.HttpCredentials->AuthnSchemes,
1504                     debugstr_w(SecurityQos2->u.HttpCredentials->ServerCertificateSubject));
1505       }
1506       TRACE("}\n");
1507       status = RpcQualityOfService_Create(SecurityQos, TRUE, &bind->QOS);
1508       if (status != RPC_S_OK)
1509           return status;
1510   }
1511   else
1512   {
1513       if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1514       bind->QOS = NULL;
1515   }
1516
1517   if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1518     AuthnSvc = RPC_C_AUTHN_WINNT;
1519
1520   /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1521   if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1522     AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1523
1524   if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1525   {
1526     if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1527     bind->AuthInfo = NULL;
1528     return RPC_S_OK;
1529   }
1530
1531   if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1532   {
1533     FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1534     return RPC_S_UNKNOWN_AUTHN_LEVEL;
1535   }
1536
1537   /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1538   if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1539   {
1540     FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1541     return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1542   }
1543
1544   r = EnumerateSecurityPackagesW(&package_count, &packages);
1545   if (r != SEC_E_OK)
1546   {
1547     ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", r);
1548     return RPC_S_SEC_PKG_ERROR;
1549   }
1550
1551   for (i = 0; i < package_count; i++)
1552     if (packages[i].wRPCID == AuthnSvc)
1553         break;
1554
1555   if (i == package_count)
1556   {
1557     FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1558     FreeContextBuffer(packages);
1559     return RPC_S_UNKNOWN_AUTHN_SERVICE;
1560   }
1561
1562   TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name), AuthnSvc);
1563   r = AcquireCredentialsHandleW(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1564                                 AuthIdentity, NULL, NULL, &cred, &exp);
1565   cbMaxToken = packages[i].cbMaxToken;
1566   FreeContextBuffer(packages);
1567   if (r == ERROR_SUCCESS)
1568   {
1569     RpcAuthInfo *new_auth_info;
1570     r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1571                            AuthIdentity, &new_auth_info);
1572     if (r == RPC_S_OK)
1573     {
1574       new_auth_info->server_principal_name = RPCRT4_strdupW(ServerPrincName);
1575       if (!ServerPrincName || new_auth_info->server_principal_name)
1576       {
1577         if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1578         bind->AuthInfo = new_auth_info;
1579       }
1580       else
1581       {
1582         RpcAuthInfo_Release(new_auth_info);
1583         r = ERROR_OUTOFMEMORY;
1584       }
1585     }
1586     else
1587       FreeCredentialsHandle(&cred);
1588     return r;
1589   }
1590   else
1591   {
1592     ERR("AcquireCredentialsHandleW failed with error 0x%08x\n", r);
1593     return RPC_S_SEC_PKG_ERROR;
1594   }
1595 }
1596
1597 /***********************************************************************
1598  *             RpcBindingSetAuthInfoA (RPCRT4.@)
1599  */
1600 RPCRTAPI RPC_STATUS RPC_ENTRY
1601 RpcBindingSetAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName, ULONG AuthnLevel,
1602                         ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1603 {
1604     TRACE("%p %s %u %u %p %u\n", Binding, debugstr_a((const char*)ServerPrincName),
1605           AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1606     return RpcBindingSetAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1607 }
1608
1609 /***********************************************************************
1610  *             RpcBindingSetAuthInfoW (RPCRT4.@)
1611  */
1612 RPCRTAPI RPC_STATUS RPC_ENTRY
1613 RpcBindingSetAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1614                         ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1615 {
1616     TRACE("%p %s %u %u %p %u\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1617           AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1618     return RpcBindingSetAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1619 }
1620
1621 /***********************************************************************
1622  *             RpcBindingSetOption (RPCRT4.@)
1623  */
1624 RPC_STATUS WINAPI RpcBindingSetOption(RPC_BINDING_HANDLE BindingHandle, ULONG Option, ULONG_PTR OptionValue)
1625 {
1626     FIXME("(%p, %d, %ld): stub\n", BindingHandle, Option, OptionValue);
1627     return RPC_S_OK;
1628 }