GNUnet  0.10.x
winproc.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
27 #include "platform.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_common.h"
30 
31 
32 #ifdef MINGW
33 
34 static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi;
35 #ifdef W32_VEH
36 static void *GNWinVEH_handle = NULL;
37 #endif
38 
71 
72 #define LOG(kind,...) GNUNET_log_from (kind, "util-winproc", __VA_ARGS__)
73 
76 void
77 plibc_panic (int err, char *msg)
78 {
80  "%s", msg);
81 }
82 
83 #ifdef W32_VEH
84 
93 LONG __stdcall
94 GNWinVEH (PEXCEPTION_POINTERS ExceptionInfo)
95 {
96  char debugger[MAX_PATH + 1];
97  char *debugger_env = NULL;
98  if (IsDebuggerPresent ())
99  {
100  DebugBreak ();
101  return EXCEPTION_CONTINUE_EXECUTION;
102  }
103  debugger_env = getenv ("GNUNET_DEBUGGER");
104  if (debugger_env != NULL)
105  {
106  STARTUPINFO si;
107  PROCESS_INFORMATION pi;
108  HANDLE event;
109  SECURITY_ATTRIBUTES sa;
110  memset (&si, 0, sizeof (si));
111  si.cb = sizeof (si);
112  memset (&pi, 0, sizeof (pi));
113  memset (&sa, 0, sizeof (sa));
114  sa.nLength = sizeof (sa);
115  sa.bInheritHandle = TRUE;
116  event = CreateEvent (&sa, FALSE, FALSE, NULL);
117  snprintf (debugger, MAX_PATH + 1, debugger_env, GetCurrentProcessId (), (uintptr_t) event);
118  debugger[MAX_PATH] = '\0';
119  if (0 != CreateProcessA (NULL, debugger, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
120  {
121  CloseHandle (pi.hProcess);
122  CloseHandle (pi.hThread);
123  WaitForSingleObject (event, 60000);
124  CloseHandle (event);
125  if (IsDebuggerPresent ())
126  {
127  return EXCEPTION_CONTINUE_EXECUTION;
128  }
129  }
130  else
131  CloseHandle (event);
132  }
133  return EXCEPTION_CONTINUE_SEARCH;
134 }
135 #endif
136 
142 int
143 GNInitWinEnv ()
144 {
145  int ret;
146 
147  plibc_initialized ();
148  plibc_set_panic_proc (plibc_panic);
149  ret = plibc_init_utf8 ("GNU", PACKAGE, 1);
150  plibc_set_stat_size_size (sizeof (((struct stat *) 0)->st_size));
151  plibc_set_stat_time_size (sizeof (((struct stat *) 0)->st_mtime));
152  /* don't load other DLLs twice */
153  if (hNTDLL)
154  return ret;
155 
156 #ifdef W32_VEH
157  if (GNWinVEH_handle == NULL)
158  {
159  GNWinVEH_handle = AddVectoredExceptionHandler (1, &GNWinVEH);
160  if (GNWinVEH_handle == NULL)
161  {
162  /* This is bad, but what can we do? */
163  printf ("Failed to set up an exception handler!\n");
164  }
165  }
166 #endif
167 
168  hNTDLL = LoadLibrary ("ntdll.dll");
169 
170  /* Function to get CPU usage under Win NT */
171  if (hNTDLL)
172  {
174  (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
175  "NtQuerySystemInformation");
176  }
177  else
178  {
180  }
181 
182  /* Functions to get information about a network adapter */
183  hIphlpapi = LoadLibrary ("iphlpapi.dll");
184  if (hIphlpapi)
185  {
186  GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
188  (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
189  GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
191  (TGetBestInterfaceEx) GetProcAddress (hIphlpapi, "GetBestInterfaceEx");
193  (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
194  }
195  else
196  {
197  GNGetIfEntry = NULL;
198  GNGetIpAddrTable = NULL;
199  GNGetIfTable = NULL;
200  GNGetBestInterfaceEx = NULL;
201  GNGetAdaptersInfo = NULL;
202  }
203 
204  /* Service & Account functions */
205  hAdvapi = LoadLibrary ("advapi32.dll");
206  if (hAdvapi)
207  {
209  (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
211  (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
213  (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
215  (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
217  (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
218  "RegisterServiceCtrlHandlerA");
220  (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
222  (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
223  "StartServiceCtrlDispatcherA");
225  (TControlService) GetProcAddress (hAdvapi, "ControlService");
226  GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
227 
229  (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
231  (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights");
233  (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
234  "LsaRemoveAccountRights");
235  GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
237  (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
238 
240  (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
242  (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
243  "InitializeSecurityDescriptor");
245  (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
246  "GetSecurityDescriptorDacl");
248  (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
250  (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
251  GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
252  GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
253  GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
255  (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce");
257  (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
258  "SetNamedSecurityInfoA");
259  }
260  else
261  {
262  GNOpenSCManager = NULL;
263  GNCreateService = NULL;
264  GNCloseServiceHandle = NULL;
265  GNDeleteService = NULL;
267  GNSetServiceStatus = NULL;
269  GNControlService = NULL;
270  GNOpenService = NULL;
271 
272  GNLsaOpenPolicy = NULL;
273  GNLsaAddAccountRights = NULL;
275  GNLsaClose = NULL;
276  GNLookupAccountName = NULL;
277 
278  GNGetFileSecurity = NULL;
281  GNGetAclInformation = NULL;
282  GNInitializeAcl = NULL;
283  GNGetAce = NULL;
284  GNEqualSid = NULL;
285  GNAddAce = NULL;
286  GNAddAccessAllowedAce = NULL;
287  GNSetNamedSecurityInfo = NULL;
288  }
289 
290  /* Account function */
291  hNetapi = LoadLibrary ("netapi32.dll");
292  if (hNetapi)
293  {
294  GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
296  (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
297  }
298  else
299  {
300  GNNetUserAdd = NULL;
301  GNNetUserSetInfo = NULL;
302  }
303 
304  return ret;
305 }
306 
310 void
312 {
313  plibc_shutdown ();
314 
315 #ifdef W32_VEH
316  if (GNWinVEH_handle != NULL)
317  {
318  RemoveVectoredExceptionHandler (GNWinVEH_handle);
319  GNWinVEH_handle = NULL;
320  }
321 #endif
322 
323  FreeLibrary (hNTDLL);
324  FreeLibrary (hIphlpapi);
325  FreeLibrary (hAdvapi);
326  FreeLibrary (hNetapi);
327 
328  CoUninitialize ();
329 }
330 
331 #endif /* MINGW */
332 
333 #if !HAVE_ATOLL
334 long long
335 atoll (const char *nptr)
336 {
337  return atol (nptr);
338 }
339 #endif
NTSTATUS NTAPI(* TLsaClose)(LSA_HANDLE)
Definition: winproc.h:125
TEqualSid GNEqualSid
BOOL WINAPI(* TGetAclInformation)(PACL pAcl, LPVOID pAclInformation, DWORD nAclInformationLength, ACL_INFORMATION_CLASS dwAclInformationClass)
Definition: winproc.h:147
TLsaAddAccountRights GNLsaAddAccountRights
NET_API_STATUS WINAPI(* TNetUserAdd)(LPCWSTR, DWORD, PBYTE, PDWORD)
Definition: winproc.h:112
BOOL WINAPI(* TInitializeSecurityDescriptor)(PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD dwRevision)
Definition: winproc.h:139
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
BOOL WINAPI(* TSetServiceStatus)(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: winproc.h:99
TLookupAccountName GNLookupAccountName
BOOL WINAPI(* TCloseServiceHandle)(SC_HANDLE hSCObject)
Definition: winproc.h:93
TDeleteService GNDeleteService
NTSTATUS NTAPI(* TLsaAddAccountRights)(LSA_HANDLE, PSID, PLSA_UNICODE_STRING, ULONG)
Definition: winproc.h:120
DWORD WINAPI(* TNtQuerySystemInformation)(int, PVOID, ULONG, PULONG)
Definition: winproc.h:63
TRegisterServiceCtrlHandler GNRegisterServiceCtrlHandler
#define LOG(kind,...)
Definition: arm_api.c:33
SERVICE_STATUS_HANDLE WINAPI(* TRegisterServiceCtrlHandler)(LPCTSTR lpServiceName, LPHANDLER_FUNCTION lpHandlerProc)
Definition: winproc.h:95
TGetAdaptersInfo GNGetAdaptersInfo
TStartServiceCtrlDispatcher GNStartServiceCtrlDispatcher
TCreateService GNCreateService
SC_HANDLE WINAPI(* TCreateService)(SC_HANDLE hSCManager, LPCTSTR lpServiceName, LPCTSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCTSTR lpBinaryPathName, LPCTSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR lpDependencies, LPCTSTR lpServiceStartName, LPCTSTR lpPassword)
Definition: winproc.h:80
long long atoll(const char *nptr)
Definition: winproc.c:335
TGetAce GNGetAce
TSetServiceStatus GNSetServiceStatus
DWORD WINAPI(* TGetBestInterfaceEx)(struct sockaddr *, PDWORD)
Definition: winproc.h:69
static struct GNUNET_PEERINFO_Handle * pi
Handle to peerinfo service.
static int ret
Final status code.
Definition: gnunet-arm.c:89
SC_HANDLE WINAPI(* TOpenService)(SC_HANDLE hSCManager, LPCTSTR lpServiceName, DWORD dwDesiredAccess)
Definition: winproc.h:107
SC_HANDLE WINAPI(* TOpenSCManager)(LPCTSTR lpMachineName, LPCTSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: winproc.h:77
BOOL WINAPI(* TInitializeAcl)(PACL pAcl, DWORD nAclLength, DWORD dwAclRevision)
Definition: winproc.h:151
#define INT_MAX
cryptographic primitives for GNUnet
TNetUserSetInfo GNNetUserSetInfo
NTSTATUS NTAPI(* TLsaRemoveAccountRights)(LSA_HANDLE, PSID, BOOLEAN, PLSA_UNICODE_STRING, ULONG)
Definition: winproc.h:122
DWORD WINAPI(* TGetIpAddrTable)(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
Definition: winproc.h:65
TGetAclInformation GNGetAclInformation
TCloseServiceHandle GNCloseServiceHandle
TLsaClose GNLsaClose
TGetIfEntry GNGetIfEntry
TInitializeSecurityDescriptor GNInitializeSecurityDescriptor
BOOL WINAPI(* TGetFileSecurity)(LPCTSTR lpFileName, SECURITY_INFORMATION RequestedInformation, PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD nLength, LPDWORD lpnLengthNeeded)
Definition: winproc.h:133
NET_API_STATUS WINAPI(* TNetUserSetInfo)(LPCWSTR servername, LPCWSTR username, DWORD level, LPBYTE buf, LPDWORD param_err)
Definition: winproc.h:113
TLsaOpenPolicy GNLsaOpenPolicy
void GNShutdownWinEnv()
int GNInitWinEnv()
TOpenService GNOpenService
TNetUserAdd GNNetUserAdd
TSetNamedSecurityInfo GNSetNamedSecurityInfo
char * getenv()
DWORD WINAPI(* TGetIfEntry)(PMIB_IFROW pIfRow)
Definition: winproc.h:64
BOOL WINAPI(* TGetAce)(PACL pAcl, DWORD dwAceIndex, LPVOID *pAce)
Definition: winproc.h:153
BOOL WINAPI(* TLookupAccountName)(LPCTSTR lpSystemName, LPCTSTR lpAccountName, PSID Sid, LPDWORD cbSid, LPTSTR ReferencedDomainName, LPDWORD cchReferencedDomainName, PSID_NAME_USE peUse)
Definition: winproc.h:126
BOOL WINAPI(* TGetSecurityDescriptorDacl)(PSECURITY_DESCRIPTOR pSecurityDescriptor, LPBOOL lpbDaclPresent, PACL *pDacl, LPBOOL lpbDaclDefaulted)
Definition: winproc.h:142
BOOL WINAPI(* TEqualSid)(PSID pSid1, PSID pSid2)
Definition: winproc.h:154
TGetBestInterfaceEx GNGetBestInterfaceEx
TGetIfTable GNGetIfTable
TAddAccessAllowedAce GNAddAccessAllowedAce
BOOL WINAPI(* TAddAce)(PACL pAcl, DWORD dwAceRevision, DWORD dwStartingAceIndex, LPVOID pAceList, DWORD nAceListLength)
Definition: winproc.h:155
TNtQuerySystemInformation GNNtQuerySystemInformation
BOOL WINAPI(* TStartServiceCtrlDispatcher)(const LPSERVICE_TABLE_ENTRY lpServiceTable)
Definition: winproc.h:102
TOpenSCManager GNOpenSCManager
BOOL WINAPI(* TAddAccessAllowedAce)(PACL pAcl, DWORD dwAceRevision, DWORD AccessMask, PSID pSid)
Definition: winproc.h:158
DWORD WINAPI(* TGetAdaptersInfo)(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
Definition: winproc.h:110
TAddAce GNAddAce
BOOL WINAPI(* TDeleteService)(SC_HANDLE hService)
Definition: winproc.h:94
TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl
TLsaRemoveAccountRights GNLsaRemoveAccountRights
TGetFileSecurity GNGetFileSecurity
NTSTATUS NTAPI(* TLsaOpenPolicy)(PLSA_UNICODE_STRING, PLSA_OBJECT_ATTRIBUTES, ACCESS_MASK, PLSA_HANDLE)
Definition: winproc.h:117
BOOL WINAPI(* TSetNamedSecurityInfo)(LPTSTR pObjectName, SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo, PSID psidOwner, PSID psidGroup, PACL pDacl, PACL pSacl)
Definition: winproc.h:160
TInitializeAcl GNInitializeAcl
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
TControlService GNControlService
TGetIpAddrTable GNGetIpAddrTable
BOOL WINAPI(* TControlService)(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: winproc.h:105
DWORD WINAPI(* TGetIfTable)(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
Definition: winproc.h:67