GNUnet  0.10.x
Data Structures | Macros | Typedefs | Enumerations | Functions
win.c File Reference

Helper functions for MS Windows in C++. More...

#include "winproc.h"
#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_common.h"
#include "gnunet_connection_lib.h"
#include <ntdef.h>
Include dependency graph for win.c:

Go to the source code of this file.

Data Structures

struct  _IP_ADAPTER_UNICAST_ADDRESS_VISTA
 
struct  _IP_ADAPTER_WINS_SERVER_ADDRESS
 
struct  _IP_ADAPTER_GATEWAY_ADDRESS
 
union  _NET_LUID
 
struct  _IP_ADAPTER_DNS_SUFFIX
 
struct  _IP_ADAPTER_ADDRESSES_XPSP1
 
struct  _IP_ADAPTER_ADDRESSES_VISTA
 
struct  _IP_ADAPTER_ADDRESSES_2008_OR_VISTASP1
 

Macros

#define _WIN_C
 
#define INHERITED_ACE   0x10
 
#define _IP_ADAPTER_UNICAST_ADDRESS_HEAD
 
#define _IP_ADAPTER_UNICAST_ADDRESS_BASE
 
#define _IP_ADAPTER_UNICAST_ADDRESS_ADD_VISTA   UINT8 OnLinkPrefixLength;
 
#define _IP_ADAPTER_UNICAST_ADDRESS_DEFINE(suffix, addition)
 
#define MAX_DHCPV6_DUID_LENGTH   130
 
#define MAX_DNS_SUFFIX_STRING_LENGTH   246
 
#define _IP_ADAPTER_ADDRESSES_HEAD
 
#define _IP_ADAPTER_ADDRESSES_BASE
 
#define _IP_ADAPTER_ADDRESSES_ADD_XPSP1
 
#define _IP_ADAPTER_ADDRESSES_ADD_VISTA
 
#define _IP_ADAPTER_ADDRESSES_ADD_2008_OR_VISTASP1
 
#define _IP_ADAPTER_ADDRESSES_DEFINE(suffix, addition)
 
#define GNUNET_malloc(a)
 
#define GNUNET_free(a)   HeapFree(GetProcessHeap (), 0, a)
 
#define GNUNET_free_non_null(a)   do { if ((a) != NULL) GNUNET_free(a); } while (0)
 

Typedefs

typedef struct _IP_ADAPTER_UNICAST_ADDRESS_VISTA IP_ADAPTER_UNICAST_ADDRESS_VISTA
 
typedef struct _IP_ADAPTER_UNICAST_ADDRESS_VISTAPIP_ADAPTER_UNICAST_ADDRESS_VISTA
 
typedef struct _IP_ADAPTER_WINS_SERVER_ADDRESS IP_ADAPTER_WINS_SERVER_ADDRESS
 
typedef struct _IP_ADAPTER_WINS_SERVER_ADDRESSPIP_ADAPTER_WINS_SERVER_ADDRESS
 
typedef struct _IP_ADAPTER_WINS_SERVER_ADDRESSPIP_ADAPTER_WINS_SERVER_ADDRESS_LH
 
typedef struct _IP_ADAPTER_GATEWAY_ADDRESS IP_ADAPTER_GATEWAY_ADDRESS
 
typedef struct _IP_ADAPTER_GATEWAY_ADDRESSPIP_ADAPTER_GATEWAY_ADDRESS
 
typedef struct _IP_ADAPTER_GATEWAY_ADDRESSPIP_ADAPTER_GATEWAY_ADDRESS_LH
 
typedef UINT32 NET_IF_COMPARTMENT_ID
 
typedef GUID NET_IF_NETWORK_GUID
 
typedef enum _NET_IF_CONNECTION_TYPE NET_IF_CONNECTION_TYPE
 
typedef enum _NET_IF_CONNECTION_TYPEPNET_IF_CONNECTION_TYPE
 
typedef enum TUNNEL_TYPEPTUNNEL_TYPE
 
typedef union _NET_LUID NET_LUID
 
typedef union _NET_LUIDPNET_LUID
 
typedef union _NET_LUID IF_LUID
 
typedef struct _IP_ADAPTER_DNS_SUFFIX IP_ADAPTER_DNS_SUFFIX
 
typedef struct _IP_ADAPTER_DNS_SUFFIXPIP_ADAPTER_DNS_SUFFIX
 
typedef struct _IP_ADAPTER_ADDRESSES_XPSP1 IP_ADAPTER_ADDRESSES_XPSP1
 
typedef struct _IP_ADAPTER_ADDRESSES_XPSP1PIP_ADAPTER_ADDRESSES_XPSP1
 
typedef struct _IP_ADAPTER_ADDRESSES_VISTA IP_ADAPTER_ADDRESSES_VISTA
 
typedef struct _IP_ADAPTER_ADDRESSES_VISTAPIP_ADAPTER_ADDRESSES_VISTA
 
typedef struct _IP_ADAPTER_ADDRESSES_2008_OR_VISTASP1 IP_ADAPTER_ADDRESSES_2008_OR_VISTASP1
 
typedef struct _IP_ADAPTER_ADDRESSES_2008_OR_VISTASP1PIP_ADAPTER_ADDRESSES_2008_OR_VISTASP1
 

Enumerations

enum  _NET_IF_CONNECTION_TYPE { NET_IF_CONNECTION_DEDICATED = 1, NET_IF_CONNECTION_PASSIVE, NET_IF_CONNECTION_DEMAND, NET_IF_CONNECTION_MAXIMUM }
 
enum  TUNNEL_TYPE {
  TUNNEL_TYPE_NONE = 0, TUNNEL_TYPE_OTHER, TUNNEL_TYPE_DIRECT, TUNNEL_TYPE_6TO4,
  TUNNEL_TYPE_ISATAP, TUNNEL_TYPE_TEREDO, TUNNEL_TYPE_IPHTTPS
}
 

Functions

int plibc_conv_to_win_path (const char *pszUnix, char *pszWindows)
 
static int EnumNICs_IPv6_get_ifs_count (SOCKET s)
 
static int EnumNICs_IPv6_get_ifs (SOCKET s, SOCKET_ADDRESS_LIST *inf, int size)
 
static int EnumNICs_IPv4_get_ifs (SOCKET s, INTERFACE_INFO **inf, int *size)
 
int EnumNICs2 (INTERFACE_INFO **ifs4, int *ifs4_len, SOCKET_ADDRESS_LIST **ifs6)
 
int EnumNICs3 (struct EnumNICs3_results **results, int *results_count)
 
void EnumNICs3_free (struct EnumNICs3_results *r)
 
int ListNICs (void(*callback)(void *, const char *, int), void *callback_cls)
 Lists all network interfaces in a combo box Used by the basic GTK configurator. More...
 
int InstallAsService (char *servicename, char *application, char *username)
 Installs the Windows service. More...
 
int UninstallService (char *servicename)
 Uninstall Windows service. More...
 
void _InitLsaString (PLSA_UNICODE_STRING LsaString, LPWSTR String)
 
NTSTATUS _OpenPolicy (LPWSTR ServerName, DWORD DesiredAccess, PLSA_HANDLE PolicyHandle)
 
BOOL _GetAccountSid (LPCTSTR SystemName, LPCTSTR AccountName, PSID *Sid)
 Obtain a SID representing the supplied account on the supplied system. More...
 
NTSTATUS _SetPrivilegeOnAccount (LSA_HANDLE PolicyHandle, PSID AccountSid, LPWSTR PrivilegeName, BOOL bEnable)
 
int CreateServiceAccount (const char *pszName, const char *pszDesc)
 Create a Windows service account. More...
 
BOOL AddPathAccessRights (char *lpszFileName, char *lpszAccountName, DWORD dwAccessMask)
 Grant permission to a file. More...
 
char * winErrorStr (const char *prefix, int dwErr)
 
BOOL SafeTerminateProcess (HANDLE hProcess, UINT uExitCode, DWORD dwTimeout)
 Terminate a process by creating a remote thread within it, which proceeds to call ExitProcess() inside that process. More...
 

Detailed Description

Helper functions for MS Windows in C++.

Author
Nils Durner

Definition in file win.c.

Macro Definition Documentation

◆ _WIN_C

#define _WIN_C

Definition at line 28 of file win.c.

◆ INHERITED_ACE

#define INHERITED_ACE   0x10

Definition at line 38 of file win.c.

Referenced by AddPathAccessRights().

◆ _IP_ADAPTER_UNICAST_ADDRESS_HEAD

#define _IP_ADAPTER_UNICAST_ADDRESS_HEAD
Value:
union { \
struct { \
ULONG Length; \
DWORD Flags; \
}; \
};

Definition at line 43 of file win.c.

◆ _IP_ADAPTER_UNICAST_ADDRESS_BASE

#define _IP_ADAPTER_UNICAST_ADDRESS_BASE
Value:
SOCKET_ADDRESS Address; \
IP_PREFIX_ORIGIN PrefixOrigin; \
IP_SUFFIX_ORIGIN SuffixOrigin; \
IP_DAD_STATE DadState; \
ULONG ValidLifetime; \
ULONG PreferredLifetime; \
ULONG LeaseLifetime;

Definition at line 51 of file win.c.

◆ _IP_ADAPTER_UNICAST_ADDRESS_ADD_VISTA

#define _IP_ADAPTER_UNICAST_ADDRESS_ADD_VISTA   UINT8 OnLinkPrefixLength;

Definition at line 60 of file win.c.

◆ _IP_ADAPTER_UNICAST_ADDRESS_DEFINE

#define _IP_ADAPTER_UNICAST_ADDRESS_DEFINE (   suffix,
  addition 
)
Value:
typedef struct _IP_ADAPTER_UNICAST_ADDRESS##suffix { \
_IP_ADAPTER_UNICAST_ADDRESS_HEAD \
struct _IP_ADAPTER_UNICAST_ADDRESS##suffix *Next; \
_IP_ADAPTER_UNICAST_ADDRESS_BASE \
addition \
} IP_ADAPTER_UNICAST_ADDRESS##suffix, *PIP_ADAPTER_UNICAST_ADDRESS##suffix;

Definition at line 64 of file win.c.

◆ MAX_DHCPV6_DUID_LENGTH

#define MAX_DHCPV6_DUID_LENGTH   130

Definition at line 130 of file win.c.

◆ MAX_DNS_SUFFIX_STRING_LENGTH

#define MAX_DNS_SUFFIX_STRING_LENGTH   246

Definition at line 142 of file win.c.

◆ _IP_ADAPTER_ADDRESSES_HEAD

#define _IP_ADAPTER_ADDRESSES_HEAD
Value:
union { \
ULONGLONG Alignment; \
struct { \
ULONG Length; \
DWORD IfIndex; \
}; \
};

Definition at line 152 of file win.c.

◆ _IP_ADAPTER_ADDRESSES_BASE

#define _IP_ADAPTER_ADDRESSES_BASE
Value:
PCHAR AdapterName; \
PIP_ADAPTER_UNICAST_ADDRESS FirstUnicastAddress; \
PIP_ADAPTER_ANYCAST_ADDRESS FirstAnycastAddress; \
PIP_ADAPTER_MULTICAST_ADDRESS FirstMulticastAddress; \
PIP_ADAPTER_DNS_SERVER_ADDRESS FirstDnsServerAddress; \
PWCHAR DnsSuffix; \
PWCHAR Description; \
PWCHAR FriendlyName; \
BYTE PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH]; \
DWORD PhysicalAddressLength; \
DWORD Flags; \
DWORD Mtu; \
DWORD IfType; \
IF_OPER_STATUS OperStatus;

Definition at line 161 of file win.c.

◆ _IP_ADAPTER_ADDRESSES_ADD_XPSP1

#define _IP_ADAPTER_ADDRESSES_ADD_XPSP1
Value:
DWORD Ipv6IfIndex; \
DWORD ZoneIndices[16]; \
PIP_ADAPTER_PREFIX FirstPrefix; \

Definition at line 177 of file win.c.

◆ _IP_ADAPTER_ADDRESSES_ADD_VISTA

#define _IP_ADAPTER_ADDRESSES_ADD_VISTA
Value:
_IP_ADAPTER_ADDRESSES_ADD_XPSP1 \
ULONG64 TransmitLinkSpeed; \
ULONG64 ReceiveLinkSpeed; \
PIP_ADAPTER_WINS_SERVER_ADDRESS_LH FirstWinsServerAddress; \
PIP_ADAPTER_GATEWAY_ADDRESS_LH FirstGatewayAddress; \
ULONG Ipv4Metric; \
ULONG Ipv6Metric; \
IF_LUID Luid; \
SOCKET_ADDRESS Dhcpv4Server; \
NET_IF_COMPARTMENT_ID CompartmentId; \
NET_IF_NETWORK_GUID NetworkGuid; \
NET_IF_CONNECTION_TYPE ConnectionType; \
TUNNEL_TYPE TunnelType; \
SOCKET_ADDRESS Dhcpv6Server; \
BYTE Dhcpv6ClientDuid[MAX_DHCPV6_DUID_LENGTH]; \
ULONG Dhcpv6ClientDuidLength; \
ULONG Dhcpv6Iaid;
#define MAX_DHCPV6_DUID_LENGTH
Definition: win.c:130

Definition at line 183 of file win.c.

◆ _IP_ADAPTER_ADDRESSES_ADD_2008_OR_VISTASP1

#define _IP_ADAPTER_ADDRESSES_ADD_2008_OR_VISTASP1
Value:
_IP_ADAPTER_ADDRESSES_ADD_VISTA \
PIP_ADAPTER_DNS_SUFFIX FirstDnsSuffix;

Definition at line 202 of file win.c.

◆ _IP_ADAPTER_ADDRESSES_DEFINE

#define _IP_ADAPTER_ADDRESSES_DEFINE (   suffix,
  addition 
)
Value:
typedef struct _IP_ADAPTER_ADDRESSES##suffix { \
_IP_ADAPTER_ADDRESSES_HEAD \
struct _IP_ADAPTER_ADDRESSES##suffix *Next; \
_IP_ADAPTER_ADDRESSES_BASE \
addition \
} IP_ADAPTER_ADDRESSES##suffix, *PIP_ADAPTER_ADDRESSES##suffix;

Definition at line 206 of file win.c.

◆ GNUNET_malloc

#define GNUNET_malloc (   a)
Value:
HeapAlloc(GetProcessHeap (), HEAP_ZERO_MEMORY | \
HEAP_GENERATE_EXCEPTIONS, a)

Definition at line 252 of file win.c.

Referenced by EnumNICs2(), EnumNICs3(), and EnumNICs_IPv4_get_ifs().

◆ GNUNET_free

#define GNUNET_free (   a)    HeapFree(GetProcessHeap (), 0, a)

Definition at line 256 of file win.c.

Referenced by EnumNICs2(), EnumNICs3(), and EnumNICs_IPv4_get_ifs().

◆ GNUNET_free_non_null

#define GNUNET_free_non_null (   a)    do { if ((a) != NULL) GNUNET_free(a); } while (0)

Definition at line 259 of file win.c.

Referenced by EnumNICs3(), EnumNICs3_free(), and ListNICs().

Typedef Documentation

◆ IP_ADAPTER_UNICAST_ADDRESS_VISTA

◆ PIP_ADAPTER_UNICAST_ADDRESS_VISTA

◆ IP_ADAPTER_WINS_SERVER_ADDRESS

◆ PIP_ADAPTER_WINS_SERVER_ADDRESS

◆ PIP_ADAPTER_WINS_SERVER_ADDRESS_LH

◆ IP_ADAPTER_GATEWAY_ADDRESS

◆ PIP_ADAPTER_GATEWAY_ADDRESS

◆ PIP_ADAPTER_GATEWAY_ADDRESS_LH

◆ NET_IF_COMPARTMENT_ID

typedef UINT32 NET_IF_COMPARTMENT_ID

Definition at line 102 of file win.c.

◆ NET_IF_NETWORK_GUID

typedef GUID NET_IF_NETWORK_GUID

Definition at line 103 of file win.c.

◆ NET_IF_CONNECTION_TYPE

◆ PNET_IF_CONNECTION_TYPE

◆ PTUNNEL_TYPE

typedef enum TUNNEL_TYPE * PTUNNEL_TYPE

◆ NET_LUID

typedef union _NET_LUID NET_LUID

◆ PNET_LUID

typedef union _NET_LUID * PNET_LUID

◆ IF_LUID

typedef union _NET_LUID IF_LUID

◆ IP_ADAPTER_DNS_SUFFIX

◆ PIP_ADAPTER_DNS_SUFFIX

◆ IP_ADAPTER_ADDRESSES_XPSP1

◆ PIP_ADAPTER_ADDRESSES_XPSP1

◆ IP_ADAPTER_ADDRESSES_VISTA

◆ PIP_ADAPTER_ADDRESSES_VISTA

◆ IP_ADAPTER_ADDRESSES_2008_OR_VISTASP1

◆ PIP_ADAPTER_ADDRESSES_2008_OR_VISTASP1

Enumeration Type Documentation

◆ _NET_IF_CONNECTION_TYPE

Enumerator
NET_IF_CONNECTION_DEDICATED 
NET_IF_CONNECTION_PASSIVE 
NET_IF_CONNECTION_DEMAND 
NET_IF_CONNECTION_MAXIMUM 

Definition at line 106 of file win.c.

◆ TUNNEL_TYPE

Enumerator
TUNNEL_TYPE_NONE 
TUNNEL_TYPE_OTHER 
TUNNEL_TYPE_DIRECT 
TUNNEL_TYPE_6TO4 
TUNNEL_TYPE_ISATAP 
TUNNEL_TYPE_TEREDO 
TUNNEL_TYPE_IPHTTPS 

Definition at line 113 of file win.c.

Function Documentation

◆ plibc_conv_to_win_path()

int plibc_conv_to_win_path ( const char *  pszUnix,
char *  pszWindows 
)

Referenced by get_my_cnf_path(), GNUNET_FS_file_information_create_from_file(), GNUNET_STRINGS_filename_expand(), InstallAsService(), mktemp_name(), and start_process().

Here is the caller graph for this function:

◆ EnumNICs_IPv6_get_ifs_count()

static int EnumNICs_IPv6_get_ifs_count ( SOCKET  s)
static

Definition at line 221 of file win.c.

References GNUNET_SYSERR.

Referenced by EnumNICs2().

222 {
223  DWORD dwret = 0, err;
224  int iret;
225  iret = WSAIoctl (s, SIO_ADDRESS_LIST_QUERY, NULL, 0, NULL, 0,
226  &dwret, NULL, NULL);
227  err = GetLastError ();
228  if (iret == SOCKET_ERROR && err == WSAEFAULT)
229  return dwret;
230  else if (iret == 0)
231  return 0;
232  return GNUNET_SYSERR;
233 }
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
Here is the caller graph for this function:

◆ EnumNICs_IPv6_get_ifs()

static int EnumNICs_IPv6_get_ifs ( SOCKET  s,
SOCKET_ADDRESS_LIST *  inf,
int  size 
)
static

Definition at line 236 of file win.c.

References GNUNET_OK, and GNUNET_SYSERR.

Referenced by EnumNICs2().

237 {
238  int iret;
239  DWORD dwret = 0;
240  iret = WSAIoctl (s, SIO_ADDRESS_LIST_QUERY, NULL, 0, inf, size,
241  &dwret, NULL, NULL);
242 
243  if (iret != 0 || dwret != size)
244  {
245  /* It's supposed to succeed! And size should be the same */
246  return GNUNET_SYSERR;
247  }
248  return GNUNET_OK;
249 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the caller graph for this function:

◆ EnumNICs_IPv4_get_ifs()

static int EnumNICs_IPv4_get_ifs ( SOCKET  s,
INTERFACE_INFO **  inf,
int *  size 
)
static

Definition at line 262 of file win.c.

References GNUNET_free, GNUNET_malloc, GNUNET_OK, and GNUNET_SYSERR.

Referenced by EnumNICs2().

263 {
264  int iret;
265  DWORD dwret = 0;
266  DWORD error;
267  INTERFACE_INFO *ii = NULL;
268  DWORD ii_size = sizeof (INTERFACE_INFO) * 15;
269  while (TRUE)
270  {
271  if (ii_size >= sizeof (INTERFACE_INFO) * 1000)
272  return GNUNET_SYSERR;
273  ii = (INTERFACE_INFO *) GNUNET_malloc (ii_size);
274  dwret = 0;
275  iret = WSAIoctl (s, SIO_GET_INTERFACE_LIST, NULL, 0, ii, ii_size,
276  &dwret, NULL, NULL);
277  error = GetLastError ();
278  if (iret == SOCKET_ERROR)
279  {
280  if (error == WSAEFAULT)
281  {
282  GNUNET_free (ii);
283  ii_size *= 2;
284  continue;
285  }
286  GNUNET_free (ii);
287  return GNUNET_SYSERR;
288  }
289  else
290  {
291  *inf = ii;
292  *size = dwret;
293  return GNUNET_OK;
294  }
295  }
296  return GNUNET_SYSERR;
297 }
#define GNUNET_free(a)
Definition: win.c:256
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_malloc(a)
Definition: win.c:252
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the caller graph for this function:

◆ EnumNICs2()

int EnumNICs2 ( INTERFACE_INFO **  ifs4,
int *  ifs4_len,
SOCKET_ADDRESS_LIST **  ifs6 
)

Definition at line 300 of file win.c.

References EnumNICs_IPv4_get_ifs(), EnumNICs_IPv6_get_ifs(), EnumNICs_IPv6_get_ifs_count(), GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_SYSERR, INVALID_SOCKET, result, and SOCKET.

Referenced by EnumNICs3().

301 {
302  int result = 0;
303  SOCKET s4;
304  SOCKET s6;
305  int ifs4len = 0;
306  int ifs6len = 0;
307  INTERFACE_INFO *interfaces4 = NULL;
308  SOCKET_ADDRESS_LIST *interfaces6 = NULL;
309 
310  SetLastError (0);
311  s4 = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
312  (void) GetLastError ();
313  SetLastError (0);
314  s6 = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
315  (void) GetLastError ();
316  if (s6 != INVALID_SOCKET)
317  {
318  ifs6len = EnumNICs_IPv6_get_ifs_count (s6);
319  if (ifs6len > 0)
320  {
321  interfaces6 = (SOCKET_ADDRESS_LIST *) GNUNET_malloc (ifs6len);
322  result = EnumNICs_IPv6_get_ifs (s6, interfaces6, ifs6len) || result;
323  }
324  closesocket (s6);
325  s6 = INVALID_SOCKET;
326  }
327 
328  if (s4 != INVALID_SOCKET)
329  {
330  result = EnumNICs_IPv4_get_ifs (s4, &interfaces4, &ifs4len) || result;
331  closesocket (s4);
332  s4 = INVALID_SOCKET;
333  }
334  if (ifs6len + ifs4len == 0)
335  goto error;
336 
337  if (!result)
338  {
339  *ifs4 = interfaces4;
340  *ifs4_len = ifs4len;
341  *ifs6 = interfaces6;
342  return GNUNET_OK;
343  }
344 error:
345  if (interfaces4 != NULL)
346  GNUNET_free (interfaces4);
347  if (interfaces6 != NULL)
348  GNUNET_free (interfaces6);
349  if (s4 != INVALID_SOCKET)
350  closesocket (s4);
351  if (s6 != INVALID_SOCKET)
352  closesocket (s6);
353  return GNUNET_SYSERR;
354 }
#define GNUNET_free(a)
Definition: win.c:256
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_malloc(a)
Definition: win.c:252
#define INVALID_SOCKET
Definition: network.c:39
#define SOCKET(a, t, p)
Definition: plibc.h:711
static int result
Global testing status.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static int EnumNICs_IPv6_get_ifs(SOCKET s, SOCKET_ADDRESS_LIST *inf, int size)
Definition: win.c:236
static int EnumNICs_IPv6_get_ifs_count(SOCKET s)
Definition: win.c:221
static int EnumNICs_IPv4_get_ifs(SOCKET s, INTERFACE_INFO **inf, int *size)
Definition: win.c:262
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EnumNICs3()

int EnumNICs3 ( struct EnumNICs3_results **  results,
int *  results_count 
)
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 361 of file win.c.

References EnumNICs3_results::addr_size, EnumNICs3_results::address, EnumNICs3_results::broadcast, EnumNICs2(), ENUMNICS3_BCAST_OK, ENUMNICS3_MASK_OK, EnumNICs3_results::flags, GNGetBestInterfaceEx, GNUNET_free, GNUNET_free_non_null, GNUNET_malloc, GNUNET_memcpy, GNUNET_OK, GNUNET_SYSERR, inet_pton(), EnumNICs3_results::is_default, m, EnumNICs3_results::mask, EnumNICs3_results::pretty_name, and unicast().

Referenced by GNUNET_OS_network_interfaces_list(), and ListNICs().

362 {
363  DWORD dwRetVal = 0;
364  int count = 0;
365  ULONG flags = /*GAA_FLAG_INCLUDE_PREFIX |*/ GAA_FLAG_SKIP_ANYCAST |
366  GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
367  struct sockaddr_in6 examplecom6;
368  IPAddr examplecom;
369  DWORD best_interface = 0;
370  DWORD best_interface6 = 0;
371 
372  int use_enum2 = 0;
373  INTERFACE_INFO *interfaces4 = NULL;
374  int interfaces4_len = 0;
375  SOCKET_ADDRESS_LIST *interfaces6 = NULL;
376 
377  unsigned long outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
378  IP_ADAPTER_ADDRESSES *pCurrentAddress = NULL;
379  IP_ADAPTER_ADDRESSES *pAddresses = (IP_ADAPTER_ADDRESSES *) GNUNET_malloc (outBufLen);
380 
381  if (GetAdaptersAddresses (AF_UNSPEC, flags, NULL, pAddresses, &outBufLen)
382  == ERROR_BUFFER_OVERFLOW)
383  {
384  GNUNET_free (pAddresses);
385  pAddresses = (IP_ADAPTER_ADDRESSES *) GNUNET_malloc (outBufLen);
386  }
387 
388  dwRetVal = GetAdaptersAddresses (AF_UNSPEC, flags, NULL, pAddresses, &outBufLen);
389 
390  if (dwRetVal != NO_ERROR)
391  {
392  GNUNET_free (pAddresses);
393  return GNUNET_SYSERR;
394  }
395 
396  if (pAddresses->Length < sizeof (IP_ADAPTER_ADDRESSES_VISTA))
397  {
398  use_enum2 = 1;
399 
400  /* Enumerate NICs using WSAIoctl() */
401  if (GNUNET_OK != EnumNICs2 (&interfaces4, &interfaces4_len, &interfaces6))
402  {
403  GNUNET_free (pAddresses);
404  return GNUNET_SYSERR;
405  }
406  }
407 
408  examplecom = inet_addr("192.0.34.166"); /* www.example.com */
409  if (GetBestInterface (examplecom, &best_interface) != NO_ERROR)
410  best_interface = 0;
411 
412  if (GNGetBestInterfaceEx != NULL)
413  {
414  examplecom6.sin6_family = AF_INET6;
415  examplecom6.sin6_port = 0;
416  examplecom6.sin6_flowinfo = 0;
417  examplecom6.sin6_scope_id = 0;
418  inet_pton (AF_INET6, "2001:500:88:200:0:0:0:10",
419  (struct sockaddr *) &examplecom6.sin6_addr);
420  dwRetVal = GNGetBestInterfaceEx ((struct sockaddr *) &examplecom6,
421  &best_interface6);
422  if (dwRetVal != NO_ERROR)
423  best_interface6 = 0;
424  }
425 
426  /* Give IPv6 a priority */
427  if (best_interface6 != 0)
428  best_interface = best_interface6;
429 
430  count = 0;
431  for (pCurrentAddress = pAddresses;
432  pCurrentAddress != NULL; pCurrentAddress = pCurrentAddress->Next)
433  {
434  if (pCurrentAddress->OperStatus == IfOperStatusUp)
435  {
436  IP_ADAPTER_UNICAST_ADDRESS *unicast = NULL;
437  for (unicast = pCurrentAddress->FirstUnicastAddress; unicast != NULL;
438  unicast = unicast->Next)
439  {
440  if ((unicast->Address.lpSockaddr->sa_family == AF_INET ||
441  unicast->Address.lpSockaddr->sa_family == AF_INET6) &&
442  (unicast->DadState == IpDadStateDeprecated ||
443  unicast->DadState == IpDadStatePreferred))
444  count += 1;
445  }
446  }
447  }
448 
449  if (count == 0)
450  {
451  *results = NULL;
452  *results_count = 0;
453  GNUNET_free (pAddresses);
454  GNUNET_free_non_null (interfaces4);
455  GNUNET_free_non_null (interfaces6);
456  return GNUNET_OK;
457  }
458 
459  *results = (struct EnumNICs3_results *) GNUNET_malloc (
460  sizeof (struct EnumNICs3_results) * count);
461  *results_count = count;
462 
463  count = 0;
464  for (pCurrentAddress = pAddresses;
465  pCurrentAddress != NULL; pCurrentAddress = pCurrentAddress->Next)
466  {
467  struct EnumNICs3_results *r;
468  IP_ADAPTER_UNICAST_ADDRESS *unicast = NULL;
469  if (pCurrentAddress->OperStatus != IfOperStatusUp)
470  continue;
471  for (unicast = pCurrentAddress->FirstUnicastAddress; unicast != NULL;
472  unicast = unicast->Next)
473  {
474  int i, j;
475  int mask_length = -1;
476  char dst[INET6_ADDRSTRLEN + 1];
477 
478  if ((unicast->Address.lpSockaddr->sa_family != AF_INET &&
479  unicast->Address.lpSockaddr->sa_family != AF_INET6) ||
480  (unicast->DadState != IpDadStateDeprecated &&
481  unicast->DadState != IpDadStatePreferred))
482  continue;
483 
484  r = &(*results)[count];
485  r->flags = 0;
486  if (pCurrentAddress->IfIndex > 0 &&
487  pCurrentAddress->IfIndex == best_interface &&
488  unicast->Address.lpSockaddr->sa_family == AF_INET)
489  r->is_default = 1;
490  else if (pCurrentAddress->Ipv6IfIndex > 0 &&
491  pCurrentAddress->Ipv6IfIndex == best_interface6 &&
492  unicast->Address.lpSockaddr->sa_family == AF_INET6)
493  r->is_default = 1;
494  else
495  r->is_default = 0;
496 
497  /* Don't choose default interface twice */
498  if (r->is_default)
499  best_interface = best_interface6 = 0;
500 
501  if (!use_enum2)
502  {
503  GNUNET_memcpy (&r->address, unicast->Address.lpSockaddr,
504  unicast->Address.iSockaddrLength);
505  memset (&r->mask, 0, sizeof (struct sockaddr));
506  mask_length = ((IP_ADAPTER_UNICAST_ADDRESS_VISTA *) unicast)->
507  OnLinkPrefixLength;
508  /* OnLinkPrefixLength is the number of leading 1s in the mask.
509  * OnLinkPrefixLength is available on Vista and later (hence use_enum2).
510  */
511  if (unicast->Address.lpSockaddr->sa_family == AF_INET)
512  {
513  struct sockaddr_in *m = (struct sockaddr_in *) &r->mask;
514  for (i = 0; i < mask_length; i++)
515  ((unsigned char *) &m->sin_addr)[i / 8] |= 0x80 >> (i % 8);
516  }
517  else if (unicast->Address.lpSockaddr->sa_family == AF_INET6)
518  {
519  struct sockaddr_in6 *m = (struct sockaddr_in6 *) &r->mask;
520  struct sockaddr_in6 *b = (struct sockaddr_in6 *) &r->broadcast;
521  for (i = 0; i < mask_length; i++)
522  ((unsigned char *) &m->sin6_addr)[i / 8] |= 0x80 >> (i % 8);
523  GNUNET_memcpy (&r->broadcast, &r->address, unicast->Address.iSockaddrLength);
524  for (i = mask_length; i < 128; i++)
525  ((unsigned char *) &b->sin6_addr)[i / 8] |= 0x80 >> (i % 8);
526  }
527  r->flags |= ENUMNICS3_MASK_OK;
528  }
529  else
530  {
531  int found = 0;
532  if (unicast->Address.lpSockaddr->sa_family == AF_INET)
533  {
534  for (i = 0; !found && i < interfaces4_len / sizeof (INTERFACE_INFO); i++)
535  {
536  struct sockaddr_in *m = (struct sockaddr_in *) &r->mask;
537  GNUNET_memcpy (&interfaces4[i].iiAddress.Address,
538  unicast->Address.lpSockaddr,
539  unicast->Address.iSockaddrLength);
540  found = 1;
541  GNUNET_memcpy (&r->address, &interfaces4[i].iiAddress.Address,
542  sizeof (struct sockaddr_in));
543  GNUNET_memcpy (&r->mask, &interfaces4[i].iiNetmask.Address,
544  sizeof (struct sockaddr_in));
545  for (mask_length = 0;
546  ((unsigned char *) &m->sin_addr)[mask_length / 8] &
547  0x80 >> (mask_length % 8); mask_length++)
548  {
549  }
550  r->flags |= ENUMNICS3_MASK_OK;
551  }
552  }
553  else if (unicast->Address.lpSockaddr->sa_family == AF_INET6)
554  {
555  for (i = 0;
556  interfaces6 != NULL && !found && i < interfaces6->iAddressCount;
557  i++)
558  {
559  GNUNET_memcpy (interfaces6->Address[i].lpSockaddr,
560  unicast->Address.lpSockaddr,
561  unicast->Address.iSockaddrLength);
562  found = 1;
563  GNUNET_memcpy (&r->address, interfaces6->Address[i].lpSockaddr,
564  sizeof (struct sockaddr_in6));
565  /* TODO: Find a way to reliably get network mask for IPv6 on XP */
566  memset (&r->mask, 0, sizeof (struct sockaddr));
567  r->flags &= ~ENUMNICS3_MASK_OK;
568  }
569  }
570  if (!found)
571  {
572  DebugBreak ();
573  }
574  }
575  if (unicast->Address.lpSockaddr->sa_family == AF_INET)
576  {
577  struct sockaddr_in *m = (struct sockaddr_in *) &r->mask;
578  struct sockaddr_in *a = (struct sockaddr_in *) &r->address;
579  /* copy address to broadcast, then flip all the trailing bits not
580  * falling under netmask to 1,
581  * so we get, 192.168.0.255 from, say, 192.168.0.43 with mask == 24.
582  */
583  GNUNET_memcpy (&r->broadcast, &r->address, unicast->Address.iSockaddrLength);
584  for (i = mask_length; i < 32; i++)
585  ((unsigned char *) &m->sin_addr)[i / 8] |= 0x80 >> (i % 8);
587  r->addr_size = sizeof (struct sockaddr_in);
588  inet_ntop (AF_INET, &a->sin_addr, dst, INET_ADDRSTRLEN);
589  }
590  else if (unicast->Address.lpSockaddr->sa_family == AF_INET6)
591  {
592  struct sockaddr_in6 *a = (struct sockaddr_in6 *) &r->address;
593  /* for IPv6 broadcast is not defined, zero it down */
594  memset (&r->broadcast, 0, sizeof (struct sockaddr));
595  r->flags &= ~ENUMNICS3_BCAST_OK;
596  r->addr_size = sizeof (struct sockaddr_in6);
597  inet_ntop (AF_INET6, &a->sin6_addr, dst, INET6_ADDRSTRLEN);
598  }
599 
600  i = 0;
601  i += snprintf (&r->pretty_name[i], 1000 - i > 0 ? 1000 - i : 0,
602  "%S (%s", pCurrentAddress->FriendlyName, dst);
603  for (j = 0; j < pCurrentAddress->PhysicalAddressLength; j++)
604  i += snprintf (&r->pretty_name[i], 1000 - i > 0 ? 1000 - i : 0,
605  "%s%02X",j > 0 ? ":" : " - ", pCurrentAddress->PhysicalAddress[j]);
606  i += snprintf (&r->pretty_name[i], 1000 - i > 0 ? 1000 - i : 0, ")");
607  r->pretty_name[1000] = '\0';
608  count += 1;
609  }
610  }
611 
612  if (use_enum2)
613  {
614  GNUNET_free_non_null (interfaces4);
615  GNUNET_free_non_null (interfaces6);
616  }
617 
618  GNUNET_free (pAddresses);
619  return GNUNET_OK;
620 }
SOCKADDR_STORAGE address
Definition: winproc.h:221
#define GNUNET_free(a)
Definition: win.c:256
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_malloc(a)
Definition: win.c:252
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:99
#define GNUNET_memcpy(dst, src, n)
char pretty_name[1001]
Definition: winproc.h:219
size_t addr_size
Definition: winproc.h:220
SOCKADDR_STORAGE broadcast
Definition: winproc.h:223
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static void unicast(struct TransportClient *tc, const struct GNUNET_MessageHeader *msg, int may_drop)
Queue the given message for transmission to the given client.
#define GNUNET_free_non_null(a)
Definition: win.c:259
unsigned char flags
Definition: winproc.h:217
TGetBestInterfaceEx GNGetBestInterfaceEx
#define ENUMNICS3_BCAST_OK
Definition: winproc.h:213
static int inet_pton(int af, const char *cp, struct in_addr *buf)
Convert IPv4 address from text to binary form.
#define ENUMNICS3_MASK_OK
Definition: winproc.h:212
int EnumNICs2(INTERFACE_INFO **ifs4, int *ifs4_len, SOCKET_ADDRESS_LIST **ifs6)
Definition: win.c:300
SOCKADDR_STORAGE mask
Definition: winproc.h:222
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EnumNICs3_free()

void EnumNICs3_free ( struct EnumNICs3_results r)

Definition at line 623 of file win.c.

References GNUNET_free_non_null.

Referenced by GNUNET_OS_network_interfaces_list().

624 {
626 }
#define GNUNET_free_non_null(a)
Definition: win.c:259
Here is the caller graph for this function:

◆ ListNICs()

int ListNICs ( void(*)(void *, const char *, int)  callback,
void *  callback_cls 
)

Lists all network interfaces in a combo box Used by the basic GTK configurator.

Parameters
callbackfunction to call for each NIC
callback_clsclosure for callback

Definition at line 637 of file win.c.

References EnumNICs3(), GNUNET_free_non_null, GNUNET_NO, GNUNET_OK, GNUNET_YES, EnumNICs3_results::is_default, EnumNICs3_results::pretty_name, and results.

638 {
639  int r;
640  int i;
641  struct EnumNICs3_results *results = NULL;
642  int results_count;
643 
644  r = EnumNICs3 (&results, &results_count);
645  if (r != GNUNET_OK)
646  return GNUNET_NO;
647 
648  for (i = 0; i < results_count; i++)
649  callback (callback_cls, results[i].pretty_name, results[i].is_default);
650  GNUNET_free_non_null (results);
651  return GNUNET_YES;
652 }
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
char pretty_name[1001]
Definition: winproc.h:219
#define GNUNET_free_non_null(a)
Definition: win.c:259
static unsigned int results
Definition: gnunet-search.c:52
int EnumNICs3(struct EnumNICs3_results **results, int *results_count)
Definition: win.c:361
#define GNUNET_YES
Definition: gnunet_common.h:80
Here is the call graph for this function:

◆ InstallAsService()

int InstallAsService ( char *  servicename,
char *  application,
char *  username 
)

Installs the Windows service.

Parameters
servicenamename of the service as diplayed by the SCM
applicationpath to the application binary
usernamethe name of the service's user account
Returns
0 on success 1 if the Windows version doesn't support services 2 if the SCM could not be opened 3 if the service could not be created

Definition at line 664 of file win.c.

References GNCloseServiceHandle, GNCreateService, GNOpenSCManager, and plibc_conv_to_win_path().

665 {
666  SC_HANDLE hManager, hService;
667  char szEXE[_MAX_PATH + 17] = "\"";
668  char *user = NULL;
669 
670  if (! GNOpenSCManager)
671  return 1;
672 
673  plibc_conv_to_win_path(application, szEXE + 1);
674  strcat(szEXE, "\" --win-service");
675  hManager = GNOpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
676  if (! hManager)
677  return 2;
678 
679  if (username)
680  {
681  user = (char *) malloc(strlen(username) + 3);
682  sprintf(user, ".\\%s", username);
683  }
684 
685  hService = GNCreateService(hManager, (LPCTSTR) servicename, (LPCTSTR) servicename, 0,
686  SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, (LPCTSTR) szEXE,
687  NULL, NULL, NULL, (LPCTSTR) user, (LPCTSTR) username);
688 
689  if (user)
690  free(user);
691 
692  if (! hService)
693  return 3;
694 
695  GNCloseServiceHandle(hService);
696 
697  return 0;
698 }
TCreateService GNCreateService
TCloseServiceHandle GNCloseServiceHandle
int plibc_conv_to_win_path(const char *pszUnix, char *pszWindows)
TOpenSCManager GNOpenSCManager
Here is the call graph for this function:

◆ UninstallService()

int UninstallService ( char *  servicename)

Uninstall Windows service.

Parameters
servicenamename of the service to delete
Returns
0 on success 1 if the Windows version doesn't support services 2 if the SCM could not be openend 3 if the service cannot be accessed 4 if the service cannot be deleted

Definition at line 711 of file win.c.

References GNCloseServiceHandle, GNDeleteService, GNOpenSCManager, and GNOpenService.

712 {
713  SC_HANDLE hManager, hService;
714 
715  if (! GNOpenSCManager)
716  return 1;
717 
718  hManager = GNOpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
719  if (! hManager)
720  return 2;
721 
722  if (! (hService = GNOpenService(hManager, (LPCTSTR) servicename, DELETE)))
723  {
724  if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST)
725  return 3;
726  else
727  goto closeSCM;
728  }
729 
730  if (! GNDeleteService(hService))
731  if (GetLastError() != ERROR_SERVICE_MARKED_FOR_DELETE)
732  return 4;
733 
734 closeSCM:
735  GNCloseServiceHandle(hService);
736 
737  return 0;
738 }
TDeleteService GNDeleteService
TCloseServiceHandle GNCloseServiceHandle
TOpenService GNOpenService
TOpenSCManager GNOpenSCManager

◆ _InitLsaString()

void _InitLsaString ( PLSA_UNICODE_STRING  LsaString,
LPWSTR  String 
)
Author
Scott Field, Microsoft
See also
http://support.microsoft.com/?scid=kb;en-us;132958
Date
12-Jul-95

Definition at line 746 of file win.c.

Referenced by _OpenPolicy(), and _SetPrivilegeOnAccount().

747 {
748  DWORD StringLength;
749 
750  if(String == NULL)
751  {
752  LsaString->Buffer = NULL;
753  LsaString->Length = 0;
754  LsaString->MaximumLength = 0;
755  return;
756  }
757 
758  StringLength = wcslen(String);
759  LsaString->Buffer = String;
760  LsaString->Length = (USHORT) StringLength *sizeof(WCHAR);
761  LsaString->MaximumLength = (USHORT) (StringLength + 1) * sizeof(WCHAR);
762 }
Here is the caller graph for this function:

◆ _OpenPolicy()

NTSTATUS _OpenPolicy ( LPWSTR  ServerName,
DWORD  DesiredAccess,
PLSA_HANDLE  PolicyHandle 
)
Author
Scott Field, Microsoft
See also
http://support.microsoft.com/?scid=kb;en-us;132958
Date
12-Jul-95

Definition at line 771 of file win.c.

References _InitLsaString(), and GNLsaOpenPolicy.

Referenced by CreateServiceAccount().

772 {
773  LSA_OBJECT_ATTRIBUTES ObjectAttributes;
774  LSA_UNICODE_STRING ServerString;
775  PLSA_UNICODE_STRING Server = NULL;
776 
777  /* Always initialize the object attributes to all zeroes. */
778  ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
779 
780  if(ServerName != NULL)
781  {
782  /* Make a LSA_UNICODE_STRING out of the LPWSTR passed in */
783  _InitLsaString(&ServerString, ServerName);
784  Server = &ServerString;
785  }
786 
787  /* Attempt to open the policy. */
788  return GNLsaOpenPolicy(Server,
789  &ObjectAttributes, DesiredAccess, PolicyHandle);
790 }
void _InitLsaString(PLSA_UNICODE_STRING LsaString, LPWSTR String)
Definition: win.c:746
TLsaOpenPolicy GNLsaOpenPolicy
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _GetAccountSid()

BOOL _GetAccountSid ( LPCTSTR  SystemName,
LPCTSTR  AccountName,
PSID *  Sid 
)

Obtain a SID representing the supplied account on the supplied system.

Returns
TRUE on success, FALSE on failure
Author
Scott Field, Microsoft
Date
12-Jul-95
Remarks
A buffer is allocated which contains the SID representing the supplied account. This buffer should be freed when it is no longer needed by calling
HeapFree(GetProcessHeap(), 0, buffer)
Call GetLastError() to obtain extended error information.
See also
http://support.microsoft.com/?scid=kb;en-us;132958

Definition at line 805 of file win.c.

References end, and GNLookupAccountName.

Referenced by CreateServiceAccount().

806 {
807  LPTSTR ReferencedDomain = NULL;
808  DWORD cbSid = 128; /* initial allocation attempt */
809  DWORD cchReferencedDomain = 16; /* initial allocation size */
810  SID_NAME_USE peUse;
811  BOOL bSuccess = FALSE; /* assume this function will fail */
812 
813  /* initial memory allocations */
814  if ((*Sid = HeapAlloc (GetProcessHeap (), 0, cbSid)) == NULL)
815  return FALSE;
816 
817  if ((ReferencedDomain = (LPTSTR) HeapAlloc (GetProcessHeap (),
818  0,
819  cchReferencedDomain *
820  sizeof (TCHAR))) == NULL)
821  return FALSE;
822 
823  /* Obtain the SID of the specified account on the specified system. */
824  while (!GNLookupAccountName(SystemName, /* machine to lookup account on */
825  AccountName, /* account to lookup */
826  *Sid, /* SID of interest */
827  &cbSid, /* size of SID */
828  ReferencedDomain, /* domain account was found on */
829  &cchReferencedDomain, &peUse))
830  {
831  if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
832  {
833  /* reallocate memory */
834  if ((*Sid = HeapReAlloc (GetProcessHeap (), 0, *Sid, cbSid)) == NULL)
835  return FALSE;
836 
837  if ((ReferencedDomain = (LPTSTR) HeapReAlloc (GetProcessHeap (),
838  0,
839  ReferencedDomain,
840  cchReferencedDomain
841  * sizeof (TCHAR))) == NULL)
842  return FALSE;
843  }
844  else
845  goto end;
846  }
847 
848  /* Indicate success. */
849  bSuccess = TRUE;
850 
851 end:
852  /* Cleanup and indicate failure, if appropriate. */
853  HeapFree (GetProcessHeap (), 0, ReferencedDomain);
854 
855  if (!bSuccess)
856  {
857  if (*Sid != NULL)
858  {
859  HeapFree (GetProcessHeap (), 0, *Sid);
860  *Sid = NULL;
861  }
862  }
863 
864  return bSuccess;
865 }
TLookupAccountName GNLookupAccountName
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
Here is the caller graph for this function:

◆ _SetPrivilegeOnAccount()

NTSTATUS _SetPrivilegeOnAccount ( LSA_HANDLE  PolicyHandle,
PSID  AccountSid,
LPWSTR  PrivilegeName,
BOOL  bEnable 
)
Author
Scott Field, Microsoft
See also
http://support.microsoft.com/?scid=kb;en-us;132958
Date
12-Jul-95

Definition at line 873 of file win.c.

References _InitLsaString(), GNLsaAddAccountRights, and GNLsaRemoveAccountRights.

Referenced by CreateServiceAccount().

878 {
879  LSA_UNICODE_STRING PrivilegeString;
880 
881  /* Create a LSA_UNICODE_STRING for the privilege name. */
882  _InitLsaString(&PrivilegeString, PrivilegeName);
883 
884  /* grant or revoke the privilege, accordingly */
885  if(bEnable)
886  {
887  NTSTATUS i;
888 
889  i = GNLsaAddAccountRights(PolicyHandle, /* open policy handle */
890  AccountSid, /* target SID */
891  &PrivilegeString, /* privileges */
892  1 /* privilege count */
893  );
894  return i;
895  }
896  else
897  {
898  return GNLsaRemoveAccountRights(PolicyHandle, /* open policy handle */
899  AccountSid, /* target SID */
900  FALSE, /* do not disable all rights */
901  &PrivilegeString, /* privileges */
902  1 /* privilege count */
903  );
904  }
905 }
TLsaAddAccountRights GNLsaAddAccountRights
void _InitLsaString(PLSA_UNICODE_STRING LsaString, LPWSTR String)
Definition: win.c:746
TLsaRemoveAccountRights GNLsaRemoveAccountRights
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateServiceAccount()

int CreateServiceAccount ( const char *  pszName,
const char *  pszDesc 
)

Create a Windows service account.

Returns
0 on success, > 0 otherwise
Parameters
pszNamethe name of the account
pszDescdescription of the account

Definition at line 914 of file win.c.

References _GetAccountSid(), _OpenPolicy(), _SetPrivilegeOnAccount(), GNLsaClose, GNNetUserAdd, GNNetUserSetInfo, and MAX_NAME_LENGTH.

916 {
917  USER_INFO_1 ui;
918  USER_INFO_1008 ui2;
919  NET_API_STATUS nStatus;
920  wchar_t wszName[MAX_NAME_LENGTH], wszDesc[MAX_NAME_LENGTH];
921  LSA_HANDLE hPolicy;
922  PSID pSID;
923 
924  if (! GNNetUserAdd)
925  return 1;
926  mbstowcs(wszName, pszName, strlen(pszName) + 1);
927  mbstowcs(wszDesc, pszDesc, strlen(pszDesc) + 1);
928 
929  memset(&ui, 0, sizeof(ui));
930  ui.usri1_name = wszName;
931  ui.usri1_password = wszName; /* account is locked anyway */
932  ui.usri1_priv = USER_PRIV_USER;
933  ui.usri1_comment = wszDesc;
934  ui.usri1_flags = UF_SCRIPT;
935 
936  nStatus = GNNetUserAdd(NULL, 1, (LPBYTE)&ui, NULL);
937 
938  if (nStatus != NERR_Success && nStatus != NERR_UserExists)
939  return 2;
940 
941  ui2.usri1008_flags = UF_PASSWD_CANT_CHANGE | UF_DONT_EXPIRE_PASSWD;
942  GNNetUserSetInfo(NULL, wszName, 1008, (LPBYTE)&ui2, NULL);
943 
944  if (!NT_SUCCESS(_OpenPolicy(NULL, POLICY_ALL_ACCESS, &hPolicy)))
945  return 3;
946 
947  _GetAccountSid(NULL, (LPCTSTR) pszName, &pSID);
948 
949  if (!NT_SUCCESS(_SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR) L"SeServiceLogonRight", TRUE)))
950  return 4;
951 
952  _SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR) L"SeDenyInteractiveLogonRight", TRUE);
953  _SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR) L"SeDenyBatchLogonRight", TRUE);
954  _SetPrivilegeOnAccount(hPolicy, pSID, (LPWSTR) L"SeDenyNetworkLogonRight", TRUE);
955 
956  GNLsaClose(hPolicy);
957 
958  return 0;
959 }
BOOL _GetAccountSid(LPCTSTR SystemName, LPCTSTR AccountName, PSID *Sid)
Obtain a SID representing the supplied account on the supplied system.
Definition: win.c:805
#define MAX_NAME_LENGTH
Definition: winproc.h:60
NTSTATUS _SetPrivilegeOnAccount(LSA_HANDLE PolicyHandle, PSID AccountSid, LPWSTR PrivilegeName, BOOL bEnable)
Definition: win.c:873
TNetUserSetInfo GNNetUserSetInfo
TLsaClose GNLsaClose
TNetUserAdd GNNetUserAdd
NTSTATUS _OpenPolicy(LPWSTR ServerName, DWORD DesiredAccess, PLSA_HANDLE PolicyHandle)
Definition: win.c:771
Here is the call graph for this function:

◆ AddPathAccessRights()

BOOL AddPathAccessRights ( char *  lpszFileName,
char *  lpszAccountName,
DWORD  dwAccessMask 
)

Grant permission to a file.

Parameters
lpszFileNamethe name of the file or directory
lpszAccountNamethe user account
dwAccessMaskthe desired access (e.g. GENERIC_ALL)
Returns
TRUE on success
Remarks
based on http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q102102&

STEP 1: Get SID of the account name specified.

STEP 2: Get security descriptor (SD) of the file specified.

STEP 3: Initialize new SD.

STEP 4: Get DACL from the old SD.

STEP 5: Get size information for DACL.

STEP 6: Compute size needed for the new ACL.

STEP 7: Allocate memory for new ACL.

STEP 8: Initialize the new ACL.

STEP 9 If DACL is present, copy all the ACEs from the old DACL to the new DACL.

The following code assumes that the old DACL is already in Windows 2000 preferred order. To conform to the new Windows 2000 preferred order, first we will copy all non-inherited ACEs from the old DACL to the new DACL, irrespective of the ACE type.

TEP 10: Get an ACE.

STEP 11: Check if it is a non-inherited ACE. If it is an inherited ACE, break from the loop so that the new access allowed non-inherited ACE can be added in the correct position, immediately after all non-inherited ACEs.

STEP 12: Skip adding the ACE, if the SID matches with the account specified, as we are going to add an access allowed ACE with a different access mask.

STEP 13: Add the ACE to the new ACL.

STEP 14: Add the access-allowed ACE to the new DACL. The new ACE added here will be in the correct position, immediately after all existing non-inherited ACEs.

STEP 14.5: Make new ACE inheritable

STEP 15: To conform to the new Windows 2000 preferred order, we will now copy the rest of inherited ACEs from the old DACL to the new DACL.

STEP 16: Get an ACE.

STEP 17: Add the ACE to the new ACL.

STEP 18: Set permissions

STEP 19: Free allocated memory

Definition at line 969 of file win.c.

References end, GNAddAccessAllowedAce, GNAddAce, GNEqualSid, GNGetAce, GNGetAclInformation, GNGetFileSecurity, GNGetSecurityDescriptorDacl, GNInitializeAcl, GNInitializeSecurityDescriptor, GNLookupAccountName, GNSetNamedSecurityInfo, and INHERITED_ACE.

971 {
972  /* SID variables. */
973  SID_NAME_USE snuType;
974  TCHAR * szDomain = NULL;
975  DWORD cbDomain = 0;
976  LPVOID pUserSID = NULL;
977  DWORD cbUserSID = 0;
978 
979  /* File SD variables. */
980  PSECURITY_DESCRIPTOR pFileSD = NULL;
981  DWORD cbFileSD = 0;
982 
983  /* New SD variables. */
984  SECURITY_DESCRIPTOR newSD;
985 
986  /* ACL variables. */
987  PACL pACL = NULL;
988  BOOL fDaclPresent;
989  BOOL fDaclDefaulted;
990  ACL_SIZE_INFORMATION AclInfo;
991 
992  /* New ACL variables. */
993  PACL pNewACL = NULL;
994  DWORD cbNewACL = 0;
995 
996  /* Temporary ACE. */
997  LPVOID pTempAce = NULL;
998  UINT CurrentAceIndex = 0;
999 
1000  UINT newAceIndex = 0;
1001 
1002  /* Assume function will fail. */
1003  BOOL fResult = FALSE;
1004  BOOL fAPISuccess;
1005 
1006  SECURITY_INFORMATION secInfo = DACL_SECURITY_INFORMATION;
1007 
1011  fAPISuccess = GNLookupAccountName(NULL, (LPCTSTR) lpszAccountName,
1012  pUserSID, &cbUserSID, (LPTSTR) szDomain, &cbDomain, &snuType);
1013 
1014  /* API should have failed with insufficient buffer. */
1015  if (fAPISuccess)
1016  goto end;
1017  else if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1018  goto end;
1019  }
1020 
1021  pUserSID = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbUserSID);
1022  if (!pUserSID) {
1023  goto end;
1024  }
1025 
1026  szDomain = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbDomain * sizeof(TCHAR));
1027  if (!szDomain) {
1028  goto end;
1029  }
1030 
1031  fAPISuccess = GNLookupAccountName(NULL, (LPCTSTR) lpszAccountName,
1032  pUserSID, &cbUserSID, (LPTSTR) szDomain, &cbDomain, &snuType);
1033  if (!fAPISuccess) {
1034  goto end;
1035  }
1036 
1040  fAPISuccess = GNGetFileSecurity((LPCTSTR) lpszFileName,
1041  secInfo, pFileSD, 0, &cbFileSD);
1042 
1043  /* API should have failed with insufficient buffer. */
1044  if (fAPISuccess)
1045  goto end;
1046  else if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1047  goto end;
1048  }
1049 
1050  pFileSD = (PSECURITY_DESCRIPTOR) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1051  cbFileSD);
1052  if (!pFileSD) {
1053  goto end;
1054  }
1055 
1056  fAPISuccess = GNGetFileSecurity((LPCTSTR) lpszFileName,
1057  secInfo, pFileSD, cbFileSD, &cbFileSD);
1058  if (!fAPISuccess) {
1059  goto end;
1060  }
1061 
1065  if (!GNInitializeSecurityDescriptor(&newSD,
1066  SECURITY_DESCRIPTOR_REVISION)) {
1067  goto end;
1068  }
1069 
1073  if (!GNGetSecurityDescriptorDacl(pFileSD, &fDaclPresent, &pACL,
1074  &fDaclDefaulted)) {
1075  goto end;
1076  }
1077 
1081  AclInfo.AceCount = 0; // Assume NULL DACL.
1082  AclInfo.AclBytesFree = 0;
1083  AclInfo.AclBytesInUse = sizeof(ACL);
1084 
1085  if (pACL == NULL)
1086  fDaclPresent = FALSE;
1087 
1088  /* If not NULL DACL, gather size information from DACL. */
1089  if (fDaclPresent) {
1090 
1091  if (!GNGetAclInformation(pACL, &AclInfo,
1092  sizeof(ACL_SIZE_INFORMATION), AclSizeInformation)) {
1093  goto end;
1094  }
1095  }
1096 
1100  cbNewACL = AclInfo.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE)
1101  + GetLengthSid(pUserSID) - sizeof(DWORD);
1102 
1106  pNewACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbNewACL);
1107  if (!pNewACL) {
1108  goto end;
1109  }
1110 
1114  if (!GNInitializeAcl(pNewACL, cbNewACL, ACL_REVISION2)) {
1115  goto end;
1116  }
1117 
1129  newAceIndex = 0;
1130 
1131  if (fDaclPresent && AclInfo.AceCount) {
1132 
1133  for (CurrentAceIndex = 0;
1134  CurrentAceIndex < AclInfo.AceCount;
1135  CurrentAceIndex++) {
1136 
1140  if (!GNGetAce(pACL, CurrentAceIndex, &pTempAce)) {
1141  goto end;
1142  }
1143 
1151  if (((ACCESS_ALLOWED_ACE *)pTempAce)->Header.AceFlags
1152  & INHERITED_ACE)
1153  break;
1154 
1161  if (GNEqualSid(pUserSID,
1162  &(((ACCESS_ALLOWED_ACE *)pTempAce)->SidStart)))
1163  continue;
1164 
1168  if (!GNAddAce(pNewACL, ACL_REVISION, MAXDWORD, pTempAce,
1169  ((PACE_HEADER) pTempAce)->AceSize)) {
1170  goto end;
1171  }
1172 
1173  newAceIndex++;
1174  }
1175  }
1176 
1182  if (!GNAddAccessAllowedAce(pNewACL, ACL_REVISION2, dwAccessMask,
1183  pUserSID)) {
1184  goto end;
1185  }
1186 
1190  if (!GetAce(pNewACL, newAceIndex, &pTempAce))
1191  goto end;
1192  ((ACCESS_ALLOWED_ACE *)pTempAce)->Header.AceFlags |=
1193  (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE);
1194 
1200  if (fDaclPresent && AclInfo.AceCount) {
1201 
1202  for (;
1203  CurrentAceIndex < AclInfo.AceCount;
1204  CurrentAceIndex++) {
1205 
1209  if (!GNGetAce(pACL, CurrentAceIndex, &pTempAce)) {
1210  goto end;
1211  }
1212 
1216  if (!GNAddAce(pNewACL, ACL_REVISION, MAXDWORD, pTempAce,
1217  ((PACE_HEADER) pTempAce)->AceSize)) {
1218  goto end;
1219  }
1220  }
1221  }
1222 
1226  if (GNSetNamedSecurityInfo((LPTSTR) lpszFileName, SE_FILE_OBJECT,
1227  DACL_SECURITY_INFORMATION, NULL, NULL, pNewACL, NULL) != ERROR_SUCCESS) {
1228  goto end;
1229  }
1230 
1231  fResult = TRUE;
1232 
1233 end:
1234 
1238  if (pUserSID)
1239  HeapFree(GetProcessHeap(), 0, pUserSID);
1240 
1241  if (szDomain)
1242  HeapFree(GetProcessHeap(), 0, szDomain);
1243 
1244  if (pFileSD)
1245  HeapFree(GetProcessHeap(), 0, pFileSD);
1246 
1247  if (pNewACL)
1248  HeapFree(GetProcessHeap(), 0, pNewACL);
1249 
1250  return fResult;
1251 }
TEqualSid GNEqualSid
TLookupAccountName GNLookupAccountName
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
TGetAce GNGetAce
TGetAclInformation GNGetAclInformation
TInitializeSecurityDescriptor GNInitializeSecurityDescriptor
#define INHERITED_ACE
Definition: win.c:38
TSetNamedSecurityInfo GNSetNamedSecurityInfo
TAddAccessAllowedAce GNAddAccessAllowedAce
TAddAce GNAddAce
TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl
TGetFileSecurity GNGetFileSecurity
TInitializeAcl GNInitializeAcl

◆ winErrorStr()

char* winErrorStr ( const char *  prefix,
int  dwErr 
)

Definition at line 1253 of file win.c.

References ret.

1254 {
1255  char *err, *ret;
1256  int mem;
1257 
1258  if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1259  NULL, (DWORD) dwErr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &err,
1260  0, NULL ))
1261  {
1262  err = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, 1);
1263  }
1264 
1265  mem = strlen(err) + strlen(prefix) + 20;
1266  ret = (char *) malloc(mem);
1267 
1268  snprintf(ret, mem, "%s: %s (#%u)", prefix, err, dwErr);
1269 
1270  LocalFree(err);
1271 
1272  return ret;
1273 }
static int ret
Final status code.
Definition: gnunet-arm.c:89

◆ SafeTerminateProcess()

BOOL SafeTerminateProcess ( HANDLE  hProcess,
UINT  uExitCode,
DWORD  dwTimeout 
)

Terminate a process by creating a remote thread within it, which proceeds to call ExitProcess() inside that process.

Safer than TerminateProcess ().

Code is from From http://private-storm.de/2009/08/11/case-terminateprocess/

Parameters
hProcesshandle of a process to terminate
uExitCodeexit code to use for ExitProcess()
dwTimeoutnumber of ms to wait for the process to terminate
Returns
TRUE on success, FALSE on failure (check last error for the code)

Definition at line 1288 of file win.c.

Referenced by GNUNET_OS_process_kill(), and start_process().

1289 {
1290  DWORD dwTID, dwCode, dwErr = 0;
1291  HANDLE hProcessDup = INVALID_HANDLE_VALUE;
1292  HANDLE hRT = NULL;
1293  HINSTANCE hKernel = GetModuleHandle ("Kernel32");
1294  BOOL bSuccess = FALSE;
1295 
1296  BOOL bDup = DuplicateHandle (GetCurrentProcess (), hProcess,
1297  GetCurrentProcess (), &hProcessDup, PROCESS_ALL_ACCESS,
1298  FALSE, 0);
1299 
1300  /* Detect the special case where the process is
1301  * already dead...
1302  */
1303  if (GetExitCodeProcess (bDup ? hProcessDup : hProcess, &dwCode) &&
1304  (STILL_ACTIVE == dwCode))
1305  {
1306  FARPROC pfnExitProc;
1307 
1308  pfnExitProc = GetProcAddress (hKernel, "ExitProcess");
1309 
1310  hRT = CreateRemoteThread ((bDup) ? hProcessDup : hProcess, NULL, 0,
1311  (LPTHREAD_START_ROUTINE) pfnExitProc, (PVOID) uExitCode, 0, &dwTID);
1312 
1313  dwErr = GetLastError ();
1314  }
1315  else
1316  {
1317  dwErr = ERROR_PROCESS_ABORTED;
1318  }
1319 
1320  if (hRT)
1321  {
1322  /* Must wait process to terminate to
1323  * guarantee that it has exited...
1324  */
1325  DWORD dwWaitResult = WaitForSingleObject ((bDup) ? hProcessDup : hProcess,
1326  dwTimeout);
1327  if (dwWaitResult == WAIT_TIMEOUT)
1328  dwErr = WAIT_TIMEOUT;
1329  else
1330  dwErr = GetLastError ();
1331 
1332  CloseHandle (hRT);
1333  bSuccess = dwErr == NO_ERROR;
1334  }
1335 
1336  if (bDup)
1337  CloseHandle (hProcessDup);
1338 
1339  SetLastError (dwErr);
1340 
1341  return bSuccess;
1342 }
Here is the caller graph for this function: