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 
99  if (IsDebuggerPresent())
100  {
101  DebugBreak();
102  return EXCEPTION_CONTINUE_EXECUTION;
103  }
104  debugger_env = getenv("GNUNET_DEBUGGER");
105  if (debugger_env != NULL)
106  {
107  STARTUPINFO si;
108  PROCESS_INFORMATION pi;
109  HANDLE event;
110  SECURITY_ATTRIBUTES sa;
111  memset(&si, 0, sizeof(si));
112  si.cb = sizeof(si);
113  memset(&pi, 0, sizeof(pi));
114  memset(&sa, 0, sizeof(sa));
115  sa.nLength = sizeof(sa);
116  sa.bInheritHandle = TRUE;
117  event = CreateEvent(&sa, FALSE, FALSE, NULL);
118  snprintf(debugger, MAX_PATH + 1, debugger_env, GetCurrentProcessId(), (uintptr_t)event);
119  debugger[MAX_PATH] = '\0';
120  if (0 != CreateProcessA(NULL, debugger, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
121  {
122  CloseHandle(pi.hProcess);
123  CloseHandle(pi.hThread);
124  WaitForSingleObject(event, 60000);
125  CloseHandle(event);
126  if (IsDebuggerPresent())
127  {
128  return EXCEPTION_CONTINUE_EXECUTION;
129  }
130  }
131  else
132  CloseHandle(event);
133  }
134  return EXCEPTION_CONTINUE_SEARCH;
135 }
136 #endif
137 
143 int
144 GNInitWinEnv()
145 {
146  int ret;
147 
148  plibc_initialized();
149  plibc_set_panic_proc(plibc_panic);
150  ret = plibc_init_utf8("GNU", PACKAGE, 1);
151  plibc_set_stat_size_size(sizeof(((struct stat *)0)->st_size));
152  plibc_set_stat_time_size(sizeof(((struct stat *)0)->st_mtime));
153  /* don't load other DLLs twice */
154  if (hNTDLL)
155  return ret;
156 
157 #ifdef W32_VEH
158  if (GNWinVEH_handle == NULL)
159  {
160  GNWinVEH_handle = AddVectoredExceptionHandler(1, &GNWinVEH);
161  if (GNWinVEH_handle == NULL)
162  {
163  /* This is bad, but what can we do? */
164  printf("Failed to set up an exception handler!\n");
165  }
166  }
167 #endif
168 
169  hNTDLL = LoadLibrary("ntdll.dll");
170 
171  /* Function to get CPU usage under Win NT */
172  if (hNTDLL)
173  {
175  (TNtQuerySystemInformation)GetProcAddress(hNTDLL,
176  "NtQuerySystemInformation");
177  }
178  else
179  {
181  }
182 
183  /* Functions to get information about a network adapter */
184  hIphlpapi = LoadLibrary("iphlpapi.dll");
185  if (hIphlpapi)
186  {
187  GNGetIfEntry = (TGetIfEntry)GetProcAddress(hIphlpapi, "GetIfEntry");
189  (TGetIpAddrTable)GetProcAddress(hIphlpapi, "GetIpAddrTable");
190  GNGetIfTable = (TGetIfTable)GetProcAddress(hIphlpapi, "GetIfTable");
192  (TGetBestInterfaceEx)GetProcAddress(hIphlpapi, "GetBestInterfaceEx");
194  (TGetAdaptersInfo)GetProcAddress(hIphlpapi, "GetAdaptersInfo");
195  }
196  else
197  {
198  GNGetIfEntry = NULL;
199  GNGetIpAddrTable = NULL;
200  GNGetIfTable = NULL;
201  GNGetBestInterfaceEx = NULL;
202  GNGetAdaptersInfo = NULL;
203  }
204 
205  /* Service & Account functions */
206  hAdvapi = LoadLibrary("advapi32.dll");
207  if (hAdvapi)
208  {
210  (TOpenSCManager)GetProcAddress(hAdvapi, "OpenSCManagerA");
212  (TCreateService)GetProcAddress(hAdvapi, "CreateServiceA");
214  (TCloseServiceHandle)GetProcAddress(hAdvapi, "CloseServiceHandle");
216  (TDeleteService)GetProcAddress(hAdvapi, "DeleteService");
218  (TRegisterServiceCtrlHandler)GetProcAddress(hAdvapi,
219  "RegisterServiceCtrlHandlerA");
221  (TSetServiceStatus)GetProcAddress(hAdvapi, "SetServiceStatus");
223  (TStartServiceCtrlDispatcher)GetProcAddress(hAdvapi,
224  "StartServiceCtrlDispatcherA");
226  (TControlService)GetProcAddress(hAdvapi, "ControlService");
227  GNOpenService = (TOpenService)GetProcAddress(hAdvapi, "OpenServiceA");
228 
230  (TLsaOpenPolicy)GetProcAddress(hAdvapi, "LsaOpenPolicy");
232  (TLsaAddAccountRights)GetProcAddress(hAdvapi, "LsaAddAccountRights");
234  (TLsaRemoveAccountRights)GetProcAddress(hAdvapi,
235  "LsaRemoveAccountRights");
236  GNLsaClose = (TLsaClose)GetProcAddress(hAdvapi, "LsaClose");
238  (TLookupAccountName)GetProcAddress(hAdvapi, "LookupAccountNameA");
239 
241  (TGetFileSecurity)GetProcAddress(hAdvapi, "GetFileSecurityA");
243  (TInitializeSecurityDescriptor)GetProcAddress(hAdvapi,
244  "InitializeSecurityDescriptor");
246  (TGetSecurityDescriptorDacl)GetProcAddress(hAdvapi,
247  "GetSecurityDescriptorDacl");
249  (TGetAclInformation)GetProcAddress(hAdvapi, "GetAclInformation");
251  (TInitializeAcl)GetProcAddress(hAdvapi, "InitializeAcl");
252  GNGetAce = (TGetAce)GetProcAddress(hAdvapi, "GetAce");
253  GNEqualSid = (TEqualSid)GetProcAddress(hAdvapi, "EqualSid");
254  GNAddAce = (TAddAce)GetProcAddress(hAdvapi, "AddAce");
256  (TAddAccessAllowedAce)GetProcAddress(hAdvapi, "AddAccessAllowedAce");
258  (TSetNamedSecurityInfo)GetProcAddress(hAdvapi,
259  "SetNamedSecurityInfoA");
260  }
261  else
262  {
263  GNOpenSCManager = NULL;
264  GNCreateService = NULL;
265  GNCloseServiceHandle = NULL;
266  GNDeleteService = NULL;
268  GNSetServiceStatus = NULL;
270  GNControlService = NULL;
271  GNOpenService = NULL;
272 
273  GNLsaOpenPolicy = NULL;
274  GNLsaAddAccountRights = NULL;
276  GNLsaClose = NULL;
277  GNLookupAccountName = NULL;
278 
279  GNGetFileSecurity = NULL;
282  GNGetAclInformation = NULL;
283  GNInitializeAcl = NULL;
284  GNGetAce = NULL;
285  GNEqualSid = NULL;
286  GNAddAce = NULL;
287  GNAddAccessAllowedAce = NULL;
288  GNSetNamedSecurityInfo = NULL;
289  }
290 
291  /* Account function */
292  hNetapi = LoadLibrary("netapi32.dll");
293  if (hNetapi)
294  {
295  GNNetUserAdd = (TNetUserAdd)GetProcAddress(hNetapi, "NetUserAdd");
297  (TNetUserSetInfo)GetProcAddress(hNetapi, "NetUserSetInfo");
298  }
299  else
300  {
301  GNNetUserAdd = NULL;
302  GNNetUserSetInfo = NULL;
303  }
304 
305  return ret;
306 }
307 
311 void
313 {
314  plibc_shutdown();
315 
316 #ifdef W32_VEH
317  if (GNWinVEH_handle != NULL)
318  {
319  RemoveVectoredExceptionHandler(GNWinVEH_handle);
320  GNWinVEH_handle = NULL;
321  }
322 #endif
323 
324  FreeLibrary(hNTDLL);
325  FreeLibrary(hIphlpapi);
326  FreeLibrary(hAdvapi);
327  FreeLibrary(hNetapi);
328 
329  CoUninitialize();
330 }
331 
332 #endif /* MINGW */
333 
334 #if !HAVE_ATOLL
335 long long
336 atoll(const char *nptr)
337 {
338  return atol(nptr);
339 }
340 #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:336
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