use putenv instead of setenv for portability
[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 == 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 =
104         (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
105                                                     "NtQuerySystemInformation");
106   }
107   else
108   {
109     GNNtQuerySystemInformation = NULL;
110   }
111
112   /* Functions to get information about a network adapter */
113   hIphlpapi = LoadLibrary ("iphlpapi.dll");
114   if (hIphlpapi)
115   {
116     GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
117     GNGetIpAddrTable =
118         (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
119     GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
120     GNGetBestInterface =
121         (TGetBestInterface) GetProcAddress (hIphlpapi, "GetBestInterface");
122     GGetAdaptersInfo =
123         (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
124   }
125   else
126   {
127     GNGetIfEntry = NULL;
128     GNGetIpAddrTable = NULL;
129     GNGetIfTable = NULL;
130     GNGetBestInterface = NULL;
131     GGetAdaptersInfo = NULL;
132   }
133
134   /* Service & Account functions */
135   hAdvapi = LoadLibrary ("advapi32.dll");
136   if (hAdvapi)
137   {
138     GNOpenSCManager =
139         (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
140     GNCreateService =
141         (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
142     GNCloseServiceHandle =
143         (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
144     GNDeleteService =
145         (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
146     GNRegisterServiceCtrlHandler =
147         (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
148                                                       "RegisterServiceCtrlHandlerA");
149     GNSetServiceStatus =
150         (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
151     GNStartServiceCtrlDispatcher =
152         (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
153                                                       "StartServiceCtrlDispatcherA");
154     GNControlService =
155         (TControlService) GetProcAddress (hAdvapi, "ControlService");
156     GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
157
158     GNLsaOpenPolicy =
159         (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
160     GNLsaAddAccountRights =
161         (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights");
162     GNLsaRemoveAccountRights =
163         (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
164                                                   "LsaRemoveAccountRights");
165     GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
166     GNLookupAccountName =
167         (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
168
169     GNGetFileSecurity =
170         (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
171     GNInitializeSecurityDescriptor =
172         (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
173                                                         "InitializeSecurityDescriptor");
174     GNGetSecurityDescriptorDacl =
175         (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
176                                                      "GetSecurityDescriptorDacl");
177     GNGetAclInformation =
178         (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
179     GNInitializeAcl =
180         (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
181     GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
182     GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
183     GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
184     GNAddAccessAllowedAce =
185         (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce");
186     GNSetNamedSecurityInfo =
187         (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
188                                                 "SetNamedSecurityInfoA");
189   }
190   else
191   {
192     GNOpenSCManager = NULL;
193     GNCreateService = NULL;
194     GNCloseServiceHandle = NULL;
195     GNDeleteService = NULL;
196     GNRegisterServiceCtrlHandler = NULL;
197     GNSetServiceStatus = NULL;
198     GNStartServiceCtrlDispatcher = NULL;
199     GNControlService = NULL;
200     GNOpenService = NULL;
201
202     GNLsaOpenPolicy = NULL;
203     GNLsaAddAccountRights = NULL;
204     GNLsaRemoveAccountRights = NULL;
205     GNLsaClose = NULL;
206     GNLookupAccountName = NULL;
207
208     GNGetFileSecurity = NULL;
209     GNInitializeSecurityDescriptor = NULL;
210     GNGetSecurityDescriptorDacl = NULL;
211     GNGetAclInformation = NULL;
212     GNInitializeAcl = NULL;
213     GNGetAce = NULL;
214     GNEqualSid = NULL;
215     GNAddAce = NULL;
216     GNAddAccessAllowedAce = NULL;
217     GNSetNamedSecurityInfo = NULL;
218   }
219
220   /* Account function */
221   hNetapi = LoadLibrary ("netapi32.dll");
222   if (hNetapi)
223   {
224     GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
225     GNNetUserSetInfo =
226         (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
227   }
228   else
229   {
230     GNNetUserAdd = NULL;
231     GNNetUserSetInfo = NULL;
232   }
233
234   return ret;
235 }
236
237 /**
238  * Clean up Windows environment
239  */
240 void
241 GNShutdownWinEnv ()
242 {
243   plibc_shutdown ();
244
245   FreeLibrary (hNTDLL);
246   FreeLibrary (hIphlpapi);
247   FreeLibrary (hAdvapi);
248   FreeLibrary (hNetapi);
249
250   CoUninitialize ();
251 }
252
253 #endif /* MINGW */
254
255 #if !HAVE_ATOLL
256 long long
257 atoll (const char *nptr)
258 {
259   return atol (nptr);
260 }
261 #endif