Storing an IP address in a signed int results in bugs if it starts
[wine] / dlls / rpcrt4 / rpcrt4_main.c
1 /*
2  *  RPCRT4
3  *
4  */
5
6 #include "config.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <sys/time.h>
13 #include <unistd.h>
14
15 #include "windef.h"
16 #include "wine/windef16.h"
17 #include "winerror.h"
18 #include "winbase.h"
19 #include "rpc.h"
20
21 #include "ole2.h"
22 #include "rpcndr.h"
23 #include "rpcproxy.h"
24
25 #ifdef HAVE_SYS_FILE_H
26 # include <sys/file.h>
27 #endif
28 #include <sys/ioctl.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
31 #endif
32 #ifdef HAVE_SYS_SOCKIO_H
33 # include <sys/sockio.h>
34 #endif
35 #ifdef HAVE_NET_IF_H
36 # include <net/if.h>
37 #endif
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include "debugtools.h"
43
44 DEFAULT_DEBUG_CHANNEL(ole);
45
46 /***********************************************************************
47  * RPCRT4_LibMain
48  *
49  * PARAMS
50  *     hinstDLL    [I] handle to the DLL's instance
51  *     fdwReason   [I]
52  *     lpvReserved [I] reserved, must be NULL
53  *
54  * RETURNS
55  *     Success: TRUE
56  *     Failure: FALSE
57  */
58
59 BOOL WINAPI
60 RPCRT4_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
61 {
62     switch (fdwReason) {
63     case DLL_PROCESS_ATTACH:
64         break;
65
66     case DLL_PROCESS_DETACH:
67         break;      
68     }
69
70     return TRUE;
71 }
72
73 /*************************************************************************
74  *           UuidCreate   [RPCRT4.@]
75  *
76  * Creates a 128bit UUID.
77  * Implemented according the DCE specification for UUID generation.
78  * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
79  * Copyright (C) 1996, 1997 Theodore Ts'o.
80  *
81  * RETURNS
82  *
83  *  S_OK if successful.
84  */
85 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
86 {
87    static char has_init = 0;
88    static unsigned char a[6];
89    static int                      adjustment = 0;
90    static struct timeval           last = {0, 0};
91    static UINT16                   clock_seq;
92    struct timeval                  tv;
93    unsigned long long              clock_reg;
94    UINT clock_high, clock_low;
95    UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
96 #ifdef HAVE_NET_IF_H
97    int             sd;
98    struct ifreq    ifr, *ifrp;
99    struct ifconf   ifc;
100    char buf[1024];
101    int             n, i;
102 #endif
103
104    /* Have we already tried to get the MAC address? */
105    if (!has_init) {
106 #ifdef HAVE_NET_IF_H
107       /* BSD 4.4 defines the size of an ifreq to be
108        * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
109        * However, under earlier systems, sa_len isn't present, so
110        *  the size is just sizeof(struct ifreq)
111        */
112 #ifdef HAVE_SOCKADDR_SA_LEN
113 #  ifndef max
114 #   define max(a,b) ((a) > (b) ? (a) : (b))
115 #  endif
116 #  define ifreq_size(i) max(sizeof(struct ifreq),\
117 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
118 # else
119 #  define ifreq_size(i) sizeof(struct ifreq)
120 # endif /* defined(HAVE_SOCKADDR_SA_LEN) */
121
122       sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
123       if (sd < 0) {
124          /* if we can't open a socket, just use random numbers */
125          /* set the multicast bit to prevent conflicts with real cards */
126          a[0] = (rand() & 0xff) | 0x80;
127          a[1] = rand() & 0xff;
128          a[2] = rand() & 0xff;
129          a[3] = rand() & 0xff;
130          a[4] = rand() & 0xff;
131          a[5] = rand() & 0xff;
132       } else {
133          memset(buf, 0, sizeof(buf));
134          ifc.ifc_len = sizeof(buf);
135          ifc.ifc_buf = buf;
136          /* get the ifconf interface */
137          if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
138             close(sd);
139             /* no ifconf, so just use random numbers */
140             /* set the multicast bit to prevent conflicts with real cards */
141             a[0] = (rand() & 0xff) | 0x80;
142             a[1] = rand() & 0xff;
143             a[2] = rand() & 0xff;
144             a[3] = rand() & 0xff;
145             a[4] = rand() & 0xff;
146             a[5] = rand() & 0xff;
147          } else {
148             /* loop through the interfaces, looking for a valid one */
149             n = ifc.ifc_len;
150             for (i = 0; i < n; i+= ifreq_size(ifr) ) {
151                ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
152                strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
153                /* try to get the address for this interface */
154 # ifdef SIOCGIFHWADDR
155                if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
156                    continue;
157                memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
158 # else
159 #  ifdef SIOCGENADDR
160                if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
161                    continue;
162                memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
163 #  else
164                /* XXX we don't have a way of getting the hardware address */
165                close(sd);
166                a[0] = 0;
167                break;
168 #  endif /* SIOCGENADDR */
169 # endif /* SIOCGIFHWADDR */
170                /* make sure it's not blank */
171                if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
172                    continue;
173                                                                 
174                goto valid_address;
175             }
176             /* if we didn't find a valid address, make a random one */
177             /* once again, set multicast bit to avoid conflicts */
178             a[0] = (rand() & 0xff) | 0x80;
179             a[1] = rand() & 0xff;
180             a[2] = rand() & 0xff;
181             a[3] = rand() & 0xff;
182             a[4] = rand() & 0xff;
183             a[5] = rand() & 0xff;
184
185             valid_address:
186             close(sd);
187          }
188       }
189 #else
190       /* no networking info, so generate a random address */
191       a[0] = (rand() & 0xff) | 0x80;
192       a[1] = rand() & 0xff;
193       a[2] = rand() & 0xff;
194       a[3] = rand() & 0xff;
195       a[4] = rand() & 0xff;
196       a[5] = rand() & 0xff;
197 #endif /* HAVE_NET_IF_H */
198       has_init = 1;
199    }
200    
201    /* generate time element of GUID */
202    
203    /* Assume that the gettimeofday() has microsecond granularity */
204 #define MAX_ADJUSTMENT 10
205                      
206    try_again:
207    gettimeofday(&tv, 0);
208    if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
209       clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
210       clock_seq &= 0x1FFF;
211       last = tv;
212       last.tv_sec--;
213    }
214    if ((tv.tv_sec < last.tv_sec) ||
215        ((tv.tv_sec == last.tv_sec) &&
216         (tv.tv_usec < last.tv_usec))) {
217       clock_seq = (clock_seq+1) & 0x1FFF;
218       adjustment = 0;
219    } else if ((tv.tv_sec == last.tv_sec) &&
220               (tv.tv_usec == last.tv_usec)) {
221       if (adjustment >= MAX_ADJUSTMENT)
222           goto try_again;
223       adjustment++;
224    } else
225        adjustment = 0;
226    
227    clock_reg = tv.tv_usec*10 + adjustment;
228    clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
229    clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
230    
231    clock_high = clock_reg >> 32;
232    clock_low = clock_reg;
233    temp_clock_seq = clock_seq | 0x8000;
234    temp_clock_mid = (UINT16)clock_high;
235    temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
236    
237    /* pack the information into the GUID structure */
238    
239    ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
240    clock_low >>= 8;
241    ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
242    clock_low >>= 8;
243    ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
244    clock_low >>= 8;
245    ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
246    
247    ((unsigned char*)&Uuid->Data2)[1] = (unsigned char)temp_clock_mid;
248    temp_clock_mid >>= 8;
249    ((unsigned char*)&Uuid->Data2)[0] = (unsigned char)temp_clock_mid;
250    
251    ((unsigned char*)&Uuid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
252    temp_clock_hi_and_version >>= 8;
253    ((unsigned char*)&Uuid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
254       
255    ((unsigned char*)Uuid->Data4)[1] = (unsigned char)temp_clock_seq;
256    temp_clock_seq >>= 8;
257    ((unsigned char*)Uuid->Data4)[0] = (unsigned char)temp_clock_seq;
258    
259    ((unsigned char*)Uuid->Data4)[2] = a[0];
260    ((unsigned char*)Uuid->Data4)[3] = a[1];
261    ((unsigned char*)Uuid->Data4)[4] = a[2];
262    ((unsigned char*)Uuid->Data4)[5] = a[3];
263    ((unsigned char*)Uuid->Data4)[6] = a[4];
264    ((unsigned char*)Uuid->Data4)[7] = a[5];
265    
266    TRACE("%s\n", debugstr_guid(Uuid));
267    
268    return RPC_S_OK;
269 }
270
271
272 /*************************************************************************
273  *           UuidCreateSequential   [RPCRT4.@]
274  *
275  * Creates a 128bit UUID by calling UuidCreate.
276  * New API in Win 2000
277  */
278
279 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
280 {
281    return UuidCreate (Uuid);
282 }
283
284
285 /*************************************************************************
286  *           RpcStringFreeA   [RPCRT4.@]
287  *
288  * Frees a character string allocated by the RPC run-time library.
289  *
290  * RETURNS
291  *
292  *  S_OK if successful.
293  */
294 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
295 {
296   HeapFree( GetProcessHeap(), 0, *String);
297
298   return RPC_S_OK;
299 }
300
301
302 /*************************************************************************
303  *           UuidHash   [RPCRT4.@]
304  *
305  * Generates a hash value for a given UUID
306  *
307  */
308 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
309 {
310   FIXME("stub:\n");
311   *Status = RPC_S_OK;
312   return 0xabcd;
313 }
314
315 /*************************************************************************
316  *           UuidToStringA   [RPCRT4.@]
317  *
318  * Converts a UUID to a string.
319  *
320  * UUID format is 8 hex digits, followed by a hyphen then three groups of
321  * 4 hex digits each followed by a hyphen and then 12 hex digits
322  *
323  * RETURNS
324  *
325  *  S_OK if successful.
326  *  S_OUT_OF_MEMORY if unsucessful.
327  */
328 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
329 {
330   *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
331
332   if(!(*StringUuid))
333     return RPC_S_OUT_OF_MEMORY;
334
335   sprintf(*StringUuid, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
336                  Uuid->Data1, Uuid->Data2, Uuid->Data3,
337                  Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
338                  Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
339                  Uuid->Data4[6], Uuid->Data4[7] );
340
341   return RPC_S_OK;
342 }
343
344 /***********************************************************************
345  *              NdrDllRegisterProxy (RPCRT4.@)
346  */
347 HRESULT WINAPI NdrDllRegisterProxy(
348   HMODULE hDll,          /* [in] */
349   const ProxyFileInfo **pProxyFileList, /* [in] */
350   const CLSID *pclsid    /* [in] */
351
352 {
353   FIXME("(%x,%p,%s), stub!\n",hDll,pProxyFileList,debugstr_guid(pclsid));
354   return S_OK;
355 }
356
357 /***********************************************************************
358  *              RpcServerUseProtseqEpA (RPCRT4.@)
359  */
360
361 RPC_STATUS WINAPI RpcServerUseProtseqEpA( LPSTR Protseq, UINT MaxCalls, LPSTR Endpoint, LPVOID SecurityDescriptor )
362 {
363   RPC_POLICY policy;
364   
365   TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
366   
367   /* This should provide the default behaviour */
368   policy.Length        = sizeof( policy );
369   policy.EndpointFlags = 0;
370   policy.NICFlags      = 0;
371   
372   return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
373 }
374
375 /***********************************************************************
376  *              RpcServerUseProtseqEpW (RPCRT4.@)
377  */
378 RPC_STATUS WINAPI RpcServerUseProtseqEpW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor )
379 {
380   RPC_POLICY policy;
381   
382   TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
383   
384   /* This should provide the default behaviour */
385   policy.Length        = sizeof( policy );
386   policy.EndpointFlags = 0;
387   policy.NICFlags      = 0;
388   
389   return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
390 }
391
392 /***********************************************************************
393  *              RpcServerUseProtseqEpExA (RPCRT4.@)
394  */
395 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( LPSTR Protseq, UINT MaxCalls, LPSTR Endpoint, LPVOID SecurityDescriptor,
396                           PRPC_POLICY lpPolicy )
397 {
398   FIXME( "(%s,%u,%s,%p,{%u,%lu,%lu}): stub\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor,
399                                                lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
400   
401   return RPC_S_PROTSEQ_NOT_SUPPORTED; /* We don't support anything at this point */                       
402 }
403
404 /***********************************************************************
405  *              RpcServerUseProtseqEpExW (RPCRT4.@)
406  */
407 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor,
408                           PRPC_POLICY lpPolicy )
409 {
410   FIXME( "(%s,%u,%s,%p,{%u,%lu,%lu}): stub\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ),
411                                                SecurityDescriptor,
412                                                lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
413   
414   return RPC_S_PROTSEQ_NOT_SUPPORTED; /* We don't support anything at this point */                       
415 }
416
417 /***********************************************************************
418  *              RpcServerRegisterIf (RPCRT4.@)
419  */
420 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
421 {
422   /* FIXME: Dump UUID using UuidToStringA */
423   TRACE( "(%p,%p,%p)\n", IfSpec, MgrTypeUuid, MgrEpv );
424   
425   return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
426 }
427
428 /***********************************************************************
429  *              RpcServerRegisterIfEx (RPCRT4.@)
430  */
431 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
432                        UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
433 {
434   /* FIXME: Dump UUID using UuidToStringA */
435   TRACE( "(%p,%p,%p,%u,%u,%p)\n", IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, IfCallbackFn );
436   
437   return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
438 }
439
440 /***********************************************************************
441  *              RpcServerRegisterIf2 (RPCRT4.@)
442  */
443 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
444                       UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
445 {
446   /* FIXME: Dump UUID using UuidToStringA */
447   FIXME( "(%p,%p,%p,%u,%u,%u,%p): stub\n", IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, MaxRpcSize, IfCallbackFn );
448   
449   return RPC_S_UNKNOWN_IF; /* I guess this return code is as good as any failure */
450 }
451
452
453 /***********************************************************************
454  *              RpcServerRegisterAuthInfoA (RPCRT4.@)
455  */
456 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( LPSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
457                             LPVOID Arg )
458 {
459   FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
460   
461   return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
462 }
463
464 /***********************************************************************
465  *              RpcServerRegisterAuthInfoW (RPCRT4.@)
466  */
467 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( LPWSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
468                             LPVOID Arg )
469 {
470   FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
471   
472   return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
473 }
474
475 /***********************************************************************
476  *              RpcServerListen (RPCRT4.@)
477  */
478 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
479 {
480   FIXME( "(%u,%u,%u): stub\n", MinimumCallThreads, MaxCalls, DontWait );
481   
482   return RPC_S_NO_PROTSEQS_REGISTERED; /* Since we don't allow registration this seems reasonable */
483 }
484
485 /***********************************************************************
486  *              RpcStringBindingComposeA (RPCRT4.@)
487  */
488 RPC_STATUS WINAPI RpcStringBindingComposeA( LPSTR ObjUuid, LPSTR Protseq, LPSTR NetworkAddr, LPSTR Endpoint,
489                           LPSTR Options, LPSTR* StringBinding )
490 {
491   FIXME( "(%s,%s,%s,%s,%s,%p): stub\n", ObjUuid, Protseq, NetworkAddr, Endpoint, Options, StringBinding );
492   *StringBinding = NULL;
493   
494   return RPC_S_INVALID_STRING_UUID; /* Failure */
495 }
496
497 /***********************************************************************
498  *              RpcStringBindingComposeW (RPCRT4.@)
499  */
500 RPC_STATUS WINAPI RpcStringBindingComposeW( LPWSTR ObjUuid, LPWSTR Protseq, LPWSTR NetworkAddr, LPWSTR Endpoint,
501                           LPWSTR Options, LPWSTR* StringBinding )
502 {
503   FIXME( "(%s,%s,%s,%s,%s,%p): stub\n", debugstr_w( ObjUuid ), debugstr_w( Protseq ), debugstr_w( NetworkAddr ),
504                                         debugstr_w( Endpoint ), debugstr_w( Options ), StringBinding );
505   *StringBinding = NULL;
506   
507   return RPC_S_INVALID_STRING_UUID; /* Failure */
508 }
509
510 /***********************************************************************
511  *              RpcBindingFree (RPCRT4.@)
512  */
513 RPC_STATUS WINAPI RpcBindingFree(RPC_BINDING_HANDLE* Binding)
514 {
515   FIXME("(%p): stub\n", Binding);
516   return RPC_S_OK;
517 }
518 /***********************************************************************
519  *              RpcBindingFromStringBindingA (RPCRT4.@)
520  */
521 RPC_STATUS WINAPI RpcBindingFromStringBindingA( LPSTR StringBinding, RPC_BINDING_HANDLE* Binding )
522 {
523   FIXME( "(%s,%p): stub\n", StringBinding, Binding );
524   
525   return RPC_S_INVALID_STRING_BINDING; /* As good as any failure code */
526 }
527   
528 /***********************************************************************
529  *              RpcBindingFromStringBindingW (RPCRT4.@)
530  */
531 RPC_STATUS WINAPI RpcBindingFromStringBindingW( LPWSTR StringBinding, RPC_BINDING_HANDLE* Binding )
532 {
533   FIXME( "(%s,%p): stub\n", debugstr_w( StringBinding ), Binding );
534
535   return RPC_S_INVALID_STRING_BINDING; /* As good as any failure code */
536 }
537
538 /***********************************************************************
539  *              NdrDllCanUnloadNow (RPCRT4.@)
540  */
541 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
542 {
543     FIXME("%p\n",pPSFactoryBuffer);
544     return FALSE;
545 }
546
547 /***********************************************************************
548  *              NdrDllGetClassObject (RPCRT4.@)
549  */
550 HRESULT WINAPI NdrDllGetClassObject(
551     REFCLSID rclsid, REFIID riid , LPVOID *ppv, 
552     const ProxyFileInfo **   pProxyFileList,
553     const CLSID *            pclsid,
554     CStdPSFactoryBuffer *    pPSFactoryBuffer)
555 {
556     if(ppv)  
557         *ppv = NULL;
558     return RPC_S_UNKNOWN_IF;
559 }
560