fix
[oweals/gnunet.git] / src / util / winproc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/winproc.c
23  * @brief Functions for MS Windows
24  * @author Nils Durner
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29
30 #define DEBUG_WINPROC 0
31
32 #ifdef MINGW
33
34 static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi;
35
36 TNtQuerySystemInformation GNNtQuerySystemInformation;
37 TGetIfEntry GNGetIfEntry;
38 TGetIpAddrTable GNGetIpAddrTable;
39 TGetIfTable GNGetIfTable;
40 TOpenSCManager GNOpenSCManager;
41 TCreateService GNCreateService;
42 TCloseServiceHandle GNCloseServiceHandle;
43 TDeleteService GNDeleteService;
44 TRegisterServiceCtrlHandler GNRegisterServiceCtrlHandler;
45 TSetServiceStatus GNSetServiceStatus;
46 TStartServiceCtrlDispatcher GNStartServiceCtrlDispatcher;
47 TControlService GNControlService;
48 TOpenService GNOpenService;
49 TGetBestInterface GNGetBestInterface;
50 TGetAdaptersInfo GGetAdaptersInfo;
51 TNetUserAdd GNNetUserAdd;
52 TNetUserSetInfo GNNetUserSetInfo;
53 TLsaOpenPolicy GNLsaOpenPolicy;
54 TLsaAddAccountRights GNLsaAddAccountRights;
55 TLsaRemoveAccountRights GNLsaRemoveAccountRights;
56 TLsaClose GNLsaClose;
57 TLookupAccountName GNLookupAccountName;
58 TGetFileSecurity GNGetFileSecurity;
59 TInitializeSecurityDescriptor GNInitializeSecurityDescriptor;
60 TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl;
61 TGetAclInformation GNGetAclInformation;
62 TInitializeAcl GNInitializeAcl;
63 TGetAce GNGetAce;
64 TEqualSid GNEqualSid;
65 TAddAce GNAddAce;
66 TAddAccessAllowedAce GNAddAccessAllowedAce;
67 TSetNamedSecurityInfo GNSetNamedSecurityInfo;
68
69 #define LOG(kind,...) GNUNET_log_from (kind, "winproc", __VA_ARGS__)
70 /**
71  * Log (panic) messages from PlibC
72  */
73 void
74 plibc_panic (int err, char *msg)
75 {
76   LOG (((err ==
77          INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
78        "%s", msg);
79 }
80
81 /**
82  * @brief Initialize PlibC and set up Windows environment
83  * @param logging context, NULL means stderr
84  * @return Error code from winerror.h, ERROR_SUCCESS on success
85 */
86 int
87 GNInitWinEnv ()
88 {
89   int ret;
90
91   plibc_initialized ();
92   plibc_set_panic_proc (plibc_panic);
93   ret = plibc_init ("GNU", PACKAGE);
94
95   /* don't load other DLLs twice */
96   if (hNTDLL)
97     return ret;
98
99   hNTDLL = LoadLibrary ("ntdll.dll");
100
101   /* Function to get CPU usage under Win NT */
102   if (hNTDLL)
103     {
104       GNNtQuerySystemInformation =
105         (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
106                                                     "NtQuerySystemInformation");
107     }
108   else
109     {
110       GNNtQuerySystemInformation = NULL;
111     }
112
113   /* Functions to get information about a network adapter */
114   hIphlpapi = LoadLibrary ("iphlpapi.dll");
115   if (hIphlpapi)
116     {
117       GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
118       GNGetIpAddrTable =
119         (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
120       GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
121       GNGetBestInterface =
122         (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface");
123       GGetAdaptersInfo =
124         (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
125     }
126   else
127     {
128       GNGetIfEntry = NULL;
129       GNGetIpAddrTable = NULL;
130       GNGetIfTable = NULL;
131       GNGetBestInterface = NULL;
132       GGetAdaptersInfo = NULL;
133     }
134
135   /* Service & Account functions */
136   hAdvapi = LoadLibrary ("advapi32.dll");
137   if (hAdvapi)
138     {
139       GNOpenSCManager =
140         (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
141       GNCreateService =
142         (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
143       GNCloseServiceHandle =
144         (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
145       GNDeleteService =
146         (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
147       GNRegisterServiceCtrlHandler =
148         (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
149                                                       "RegisterServiceCtrlHandlerA");
150       GNSetServiceStatus =
151         (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
152       GNStartServiceCtrlDispatcher =
153         (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
154                                                       "StartServiceCtrlDispatcherA");
155       GNControlService =
156         (TControlService) GetProcAddress (hAdvapi, "ControlService");
157       GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
158
159       GNLsaOpenPolicy =
160         (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
161       GNLsaAddAccountRights =
162         (TLsaAddAccountRights) GetProcAddress (hAdvapi,
163                                                "LsaAddAccountRights");
164       GNLsaRemoveAccountRights =
165         (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
166                                                   "LsaRemoveAccountRights");
167       GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
168       GNLookupAccountName =
169         (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
170
171       GNGetFileSecurity =
172         (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
173       GNInitializeSecurityDescriptor =
174         (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
175                                                         "InitializeSecurityDescriptor");
176       GNGetSecurityDescriptorDacl =
177         (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
178                                                      "GetSecurityDescriptorDacl");
179       GNGetAclInformation =
180         (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
181       GNInitializeAcl =
182         (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
183       GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
184       GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
185       GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
186       GNAddAccessAllowedAce =
187         (TAddAccessAllowedAce) GetProcAddress (hAdvapi,
188                                                "AddAccessAllowedAce");
189       GNSetNamedSecurityInfo =
190         (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
191                                                 "SetNamedSecurityInfoA");
192     }
193   else
194     {
195       GNOpenSCManager = NULL;
196       GNCreateService = NULL;
197       GNCloseServiceHandle = NULL;
198       GNDeleteService = NULL;
199       GNRegisterServiceCtrlHandler = NULL;
200       GNSetServiceStatus = NULL;
201       GNStartServiceCtrlDispatcher = NULL;
202       GNControlService = NULL;
203       GNOpenService = NULL;
204
205       GNLsaOpenPolicy = NULL;
206       GNLsaAddAccountRights = NULL;
207       GNLsaRemoveAccountRights = NULL;
208       GNLsaClose = NULL;
209       GNLookupAccountName = NULL;
210
211       GNGetFileSecurity = NULL;
212       GNInitializeSecurityDescriptor = NULL;
213       GNGetSecurityDescriptorDacl = NULL;
214       GNGetAclInformation = NULL;
215       GNInitializeAcl = NULL;
216       GNGetAce = NULL;
217       GNEqualSid = NULL;
218       GNAddAce = NULL;
219       GNAddAccessAllowedAce = NULL;
220       GNSetNamedSecurityInfo = NULL;
221     }
222
223   /* Account function */
224   hNetapi = LoadLibrary ("netapi32.dll");
225   if (hNetapi)
226     {
227       GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
228       GNNetUserSetInfo =
229         (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
230     }
231   else
232     {
233       GNNetUserAdd = NULL;
234       GNNetUserSetInfo = NULL;
235     }
236
237   return ret;
238 }
239
240 /**
241  * Clean up Windows environment
242  */
243 void
244 GNShutdownWinEnv ()
245 {
246   plibc_shutdown ();
247
248   FreeLibrary (hNTDLL);
249   FreeLibrary (hIphlpapi);
250   FreeLibrary (hAdvapi);
251   FreeLibrary (hNetapi);
252
253   CoUninitialize ();
254 }
255
256 #endif /* MINGW */
257
258 #if !HAVE_ATOLL
259 long long
260 atoll (const char *nptr)
261 {
262   return atol (nptr);
263 }
264 #endif