Returns now GNUNET_SYSERR
[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 /**
70  * Log (panic) messages from PlibC
71  */
72 void
73 plibc_panic (int err, char *msg)
74 {
75   GNUNET_log (((err ==
76                 INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
77               "%s", msg);
78 }
79
80 /**
81  * @brief Initialize PlibC and set up Windows environment
82  * @param logging context, NULL means stderr
83  * @return Error code from winerror.h, ERROR_SUCCESS on success
84 */
85 int
86 GNInitWinEnv ()
87 {
88   int ret;
89
90   plibc_initialized ();
91   plibc_set_panic_proc (plibc_panic);
92   ret = plibc_init ("GNU", PACKAGE);
93
94   /* don't load other DLLs twice */
95   if (hNTDLL)
96     return ret;
97
98   hNTDLL = LoadLibrary ("ntdll.dll");
99
100   /* Function to get CPU usage under Win NT */
101   if (hNTDLL)
102     {
103       GNNtQuerySystemInformation = (TNtQuerySystemInformation)
104         GetProcAddress (hNTDLL, "NtQuerySystemInformation");
105     }
106   else
107     {
108       GNNtQuerySystemInformation = NULL;
109     }
110
111   /* Functions to get information about a network adapter */
112   hIphlpapi = LoadLibrary ("iphlpapi.dll");
113   if (hIphlpapi)
114     {
115       GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
116       GNGetIpAddrTable = (TGetIpAddrTable) GetProcAddress (hIphlpapi,
117                                                            "GetIpAddrTable");
118       GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
119       GNGetBestInterface = (TGetBestInterface) GetProcAddress (hIphlpapi,
120                                                                "GetBestInterface");
121       GGetAdaptersInfo = (TGetAdaptersInfo) GetProcAddress (hIphlpapi,
122                                                             "GetAdaptersInfo");
123     }
124   else
125     {
126       GNGetIfEntry = NULL;
127       GNGetIpAddrTable = NULL;
128       GNGetIfTable = NULL;
129       GNGetBestInterface = NULL;
130       GGetAdaptersInfo = NULL;
131     }
132
133   /* Service & Account functions */
134   hAdvapi = LoadLibrary ("advapi32.dll");
135   if (hAdvapi)
136     {
137       GNOpenSCManager = (TOpenSCManager)
138         GetProcAddress (hAdvapi, "OpenSCManagerA");
139       GNCreateService = (TCreateService)
140         GetProcAddress (hAdvapi, "CreateServiceA");
141       GNCloseServiceHandle = (TCloseServiceHandle)
142         GetProcAddress (hAdvapi, "CloseServiceHandle");
143       GNDeleteService = (TDeleteService)
144         GetProcAddress (hAdvapi, "DeleteService");
145       GNRegisterServiceCtrlHandler = (TRegisterServiceCtrlHandler)
146         GetProcAddress (hAdvapi, "RegisterServiceCtrlHandlerA");
147       GNSetServiceStatus = (TSetServiceStatus)
148         GetProcAddress (hAdvapi, "SetServiceStatus");
149       GNStartServiceCtrlDispatcher = (TStartServiceCtrlDispatcher)
150         GetProcAddress (hAdvapi, "StartServiceCtrlDispatcherA");
151       GNControlService = (TControlService)
152         GetProcAddress (hAdvapi, "ControlService");
153       GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
154
155       GNLsaOpenPolicy = (TLsaOpenPolicy)
156         GetProcAddress (hAdvapi, "LsaOpenPolicy");
157       GNLsaAddAccountRights = (TLsaAddAccountRights)
158         GetProcAddress (hAdvapi, "LsaAddAccountRights");
159       GNLsaRemoveAccountRights = (TLsaRemoveAccountRights)
160         GetProcAddress (hAdvapi, "LsaRemoveAccountRights");
161       GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
162       GNLookupAccountName = (TLookupAccountName)
163         GetProcAddress (hAdvapi, "LookupAccountNameA");
164
165       GNGetFileSecurity = (TGetFileSecurity)
166         GetProcAddress (hAdvapi, "GetFileSecurityA");
167       GNInitializeSecurityDescriptor = (TInitializeSecurityDescriptor)
168         GetProcAddress (hAdvapi, "InitializeSecurityDescriptor");
169       GNGetSecurityDescriptorDacl = (TGetSecurityDescriptorDacl)
170         GetProcAddress (hAdvapi, "GetSecurityDescriptorDacl");
171       GNGetAclInformation = (TGetAclInformation)
172         GetProcAddress (hAdvapi, "GetAclInformation");
173       GNInitializeAcl = (TInitializeAcl)
174         GetProcAddress (hAdvapi, "InitializeAcl");
175       GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
176       GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
177       GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
178       GNAddAccessAllowedAce = (TAddAccessAllowedAce)
179         GetProcAddress (hAdvapi, "AddAccessAllowedAce");
180       GNSetNamedSecurityInfo = (TSetNamedSecurityInfo)
181         GetProcAddress (hAdvapi, "SetNamedSecurityInfoA");
182     }
183   else
184     {
185       GNOpenSCManager = NULL;
186       GNCreateService = NULL;
187       GNCloseServiceHandle = NULL;
188       GNDeleteService = NULL;
189       GNRegisterServiceCtrlHandler = NULL;
190       GNSetServiceStatus = NULL;
191       GNStartServiceCtrlDispatcher = NULL;
192       GNControlService = NULL;
193       GNOpenService = NULL;
194
195       GNLsaOpenPolicy = NULL;
196       GNLsaAddAccountRights = NULL;
197       GNLsaRemoveAccountRights = NULL;
198       GNLsaClose = NULL;
199       GNLookupAccountName = NULL;
200
201       GNGetFileSecurity = NULL;
202       GNInitializeSecurityDescriptor = NULL;
203       GNGetSecurityDescriptorDacl = NULL;
204       GNGetAclInformation = NULL;
205       GNInitializeAcl = NULL;
206       GNGetAce = NULL;
207       GNEqualSid = NULL;
208       GNAddAce = NULL;
209       GNAddAccessAllowedAce = NULL;
210       GNSetNamedSecurityInfo = NULL;
211     }
212
213   /* Account function */
214   hNetapi = LoadLibrary ("netapi32.dll");
215   if (hNetapi)
216     {
217       GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
218       GNNetUserSetInfo = (TNetUserSetInfo)
219         GetProcAddress (hNetapi, "NetUserSetInfo");
220     }
221   else
222     {
223       GNNetUserAdd = NULL;
224       GNNetUserSetInfo = NULL;
225     }
226
227   return ret;
228 }
229
230 /**
231  * Clean up Windows environment
232  */
233 void
234 GNShutdownWinEnv ()
235 {
236   plibc_shutdown ();
237
238   FreeLibrary (hNTDLL);
239   FreeLibrary (hIphlpapi);
240   FreeLibrary (hAdvapi);
241   FreeLibrary (hNetapi);
242
243   CoUninitialize ();
244 }
245
246 #endif /* MINGW */
247
248 #if !HAVE_ATOLL
249 long long
250 atoll (const char *nptr)
251 {
252   return atol (nptr);
253 }
254 #endif