Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/net/net.c
4389 views
1
/*
2
* Copyright 2007 Tim Schwartz
3
*
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
8
*
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
13
*
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17
*/
18
19
#include <windows.h>
20
#include <lm.h>
21
#include <wine/debug.h>
22
23
#include "resources.h"
24
25
WINE_DEFAULT_DEBUG_CHANNEL(net);
26
27
#define NET_START 0001
28
#define NET_STOP 0002
29
30
static int output_write(const WCHAR* str, int len)
31
{
32
DWORD count;
33
if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL))
34
{
35
DWORD lenA;
36
char* strA;
37
38
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
39
* back to WriteFile() with OEM code page.
40
*/
41
lenA = WideCharToMultiByte(GetOEMCP(), 0, str, len,
42
NULL, 0, NULL, NULL);
43
strA = HeapAlloc(GetProcessHeap(), 0, lenA);
44
if (!strA)
45
return 0;
46
47
WideCharToMultiByte(GetOEMCP(), 0, str, len, strA, lenA, NULL, NULL);
48
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
49
HeapFree(GetProcessHeap(), 0, strA);
50
}
51
return count;
52
}
53
54
static int output_vprintf(const WCHAR* fmt, va_list va_args)
55
{
56
WCHAR str[8192];
57
int len;
58
59
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt, 0, 0, str, ARRAY_SIZE(str), &va_args);
60
if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
61
WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
62
else
63
output_write(str, len);
64
return 0;
65
}
66
67
static int WINAPIV output_printf(const WCHAR* fmt, ...)
68
{
69
va_list arguments;
70
71
va_start(arguments, fmt);
72
output_vprintf(fmt, arguments);
73
va_end(arguments);
74
return 0;
75
}
76
77
static int WINAPIV output_string(int msg, ...)
78
{
79
WCHAR fmt[8192];
80
va_list arguments;
81
82
LoadStringW(GetModuleHandleW(NULL), msg, fmt, ARRAY_SIZE(fmt));
83
va_start(arguments, msg);
84
output_vprintf(fmt, arguments);
85
va_end(arguments);
86
return 0;
87
}
88
89
static BOOL output_error_string(DWORD error)
90
{
91
LPWSTR pBuffer;
92
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
93
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
94
NULL, error, 0, (LPWSTR)&pBuffer, 0, NULL))
95
{
96
output_write(pBuffer, lstrlenW(pBuffer));
97
LocalFree(pBuffer);
98
return TRUE;
99
}
100
return FALSE;
101
}
102
103
static BOOL net_use(int argc, const WCHAR* argv[])
104
{
105
USE_INFO_2 *buffer, *connection;
106
DWORD read, total, resume_handle, rc, i;
107
WCHAR* status[STRING_RECONN-STRING_OK+1];
108
resume_handle = 0;
109
buffer = NULL;
110
111
if(argc<3)
112
{
113
HMODULE hmod = GetModuleHandleW(NULL);
114
115
/* Load the status strings */
116
for (i = 0; i < ARRAY_SIZE(status); i++)
117
{
118
status[i] = HeapAlloc(GetProcessHeap(), 0, 1024 * sizeof(**status));
119
LoadStringW(hmod, STRING_OK+i, status[i], 1024);
120
}
121
122
do {
123
rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
124
if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
125
{
126
break;
127
}
128
129
if(total == 0)
130
{
131
output_string(STRING_NO_ENTRIES);
132
break;
133
}
134
135
output_string(STRING_USE_HEADER);
136
for (i = 0, connection = buffer; i < read; ++i, ++connection)
137
output_string(STRING_USE_ENTRY, status[connection->ui2_status], connection->ui2_local,
138
connection->ui2_remote, connection->ui2_refcount);
139
140
if (buffer != NULL) NetApiBufferFree(buffer);
141
} while (rc == ERROR_MORE_DATA);
142
143
/* Release the status strings */
144
for (i = 0; i < ARRAY_SIZE(status); i++)
145
HeapFree(GetProcessHeap(), 0, status[i]);
146
147
return TRUE;
148
}
149
150
return FALSE;
151
}
152
153
static BOOL net_enum_services(void)
154
{
155
SC_HANDLE SCManager;
156
LPENUM_SERVICE_STATUS_PROCESSW services;
157
DWORD size, i, count, resume;
158
BOOL success = FALSE;
159
160
SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
161
if(!SCManager)
162
{
163
output_string(STRING_NO_SCM);
164
return FALSE;
165
}
166
167
EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0, &size, &count, NULL, NULL);
168
if(GetLastError() != ERROR_MORE_DATA)
169
{
170
output_error_string(GetLastError());
171
goto end;
172
}
173
services = HeapAlloc(GetProcessHeap(), 0, size);
174
resume = 0;
175
if(!EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, (LPBYTE)services, size, &size, &count, &resume, NULL))
176
{
177
output_error_string(GetLastError());
178
goto end;
179
}
180
output_string(STRING_RUNNING_HEADER);
181
for(i = 0; i < count; i++)
182
{
183
output_printf(L" %1\n", services[i].lpDisplayName);
184
WINE_TRACE("service=%s state=%ld controls=%lx\n",
185
wine_dbgstr_w(services[i].lpServiceName),
186
services[i].ServiceStatusProcess.dwCurrentState,
187
services[i].ServiceStatusProcess.dwControlsAccepted);
188
}
189
success = TRUE;
190
191
end:
192
CloseServiceHandle(SCManager);
193
return success;
194
}
195
196
static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
197
{
198
LPENUM_SERVICE_STATUSW dependencies = NULL;
199
DWORD buffer_size = 0;
200
DWORD count = 0, counter;
201
BOOL result;
202
SC_HANDLE dependent_serviceHandle;
203
SERVICE_STATUS_PROCESS ssp;
204
205
result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
206
207
if(!result && (GetLastError() == ERROR_MORE_DATA))
208
{
209
dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
210
if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
211
{
212
for(counter = 0; counter < count; counter++)
213
{
214
output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
215
dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
216
if(dependent_serviceHandle)
217
{
218
result = StopService(SCManager, dependent_serviceHandle);
219
CloseServiceHandle(dependent_serviceHandle);
220
}
221
if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
222
}
223
}
224
}
225
226
if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
227
HeapFree(GetProcessHeap(), 0, dependencies);
228
return result;
229
}
230
231
static BOOL net_service(int operation, const WCHAR* service_name)
232
{
233
SC_HANDLE SCManager, serviceHandle;
234
BOOL result = FALSE;
235
WCHAR service_display_name[4096];
236
DWORD buffer_size;
237
238
SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
239
if(!SCManager)
240
{
241
output_string(STRING_NO_SCM);
242
return FALSE;
243
}
244
serviceHandle = OpenServiceW(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
245
if(!serviceHandle)
246
{
247
output_string(STRING_NO_SVCHANDLE);
248
CloseServiceHandle(SCManager);
249
return FALSE;
250
}
251
252
buffer_size = ARRAY_SIZE(service_display_name);
253
GetServiceDisplayNameW(SCManager, service_name, service_display_name, &buffer_size);
254
if (!service_display_name[0]) lstrcpyW(service_display_name, service_name);
255
256
switch(operation)
257
{
258
case NET_START:
259
output_string(STRING_START_SVC, service_display_name);
260
result = StartServiceW(serviceHandle, 0, NULL);
261
262
if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
263
else
264
{
265
if (!output_error_string(GetLastError()))
266
output_string(STRING_START_SVC_FAIL, service_display_name);
267
}
268
break;
269
case NET_STOP:
270
output_string(STRING_STOP_SVC, service_display_name);
271
result = StopService(SCManager, serviceHandle);
272
273
if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
274
else
275
{
276
if (!output_error_string(GetLastError()))
277
output_string(STRING_STOP_SVC_FAIL, service_display_name);
278
}
279
break;
280
}
281
282
CloseServiceHandle(serviceHandle);
283
CloseServiceHandle(SCManager);
284
return result;
285
}
286
287
static BOOL arg_is(const WCHAR* str1, const WCHAR* str2)
288
{
289
return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, str1, -1, str2, -1) == CSTR_EQUAL;
290
}
291
292
int __cdecl wmain(int argc, const WCHAR* argv[])
293
{
294
BOOL switch_yes = FALSE;
295
BOOL switch_no = FALSE;
296
int i;
297
298
for (i = 1; i < argc; i++)
299
{
300
if (arg_is(argv[i], L"/y") || arg_is(argv[i], L"/ye") || arg_is(argv[i], L"/yes"))
301
switch_yes = TRUE;
302
else if (arg_is(argv[i], L"/n") || arg_is(argv[i], L"/no"))
303
switch_no = TRUE;
304
else
305
continue;
306
307
/* remove the argument */
308
memmove( &argv[i], &argv[i + 1], (argc - i) * sizeof(*argv) );
309
i--;
310
argc--;
311
}
312
313
if (switch_yes && switch_no)
314
{
315
output_string(STRING_CONFLICT_SWITCHES);
316
return 1;
317
}
318
319
if (argc < 2)
320
{
321
output_string(STRING_USAGE);
322
return 1;
323
}
324
325
if(arg_is(argv[1], L"help"))
326
{
327
if(argc > 3)
328
{
329
output_string(STRING_USAGE);
330
return 1;
331
}
332
if(argc == 2)
333
output_string(STRING_USAGE);
334
else if(arg_is(argv[2], L"start"))
335
output_string(STRING_START_USAGE);
336
else if(arg_is(argv[2], L"stop"))
337
output_string(STRING_STOP_USAGE);
338
else
339
output_string(STRING_USAGE);
340
}
341
else if(arg_is(argv[1], L"start"))
342
{
343
if(argc > 3)
344
{
345
output_string(STRING_START_USAGE);
346
return 1;
347
}
348
if (argc == 2)
349
{
350
if (!net_enum_services())
351
return 1;
352
}
353
else if(arg_is(argv[2], L"/help"))
354
output_string(STRING_START_USAGE);
355
else if(!net_service(NET_START, argv[2]))
356
return 1;
357
}
358
else if(arg_is(argv[1], L"stop"))
359
{
360
if(argc != 3)
361
{
362
output_string(STRING_STOP_USAGE);
363
return 1;
364
}
365
if(arg_is(argv[2], L"/help"))
366
output_string(STRING_STOP_USAGE);
367
else if(!net_service(NET_STOP, argv[2]))
368
return 2;
369
}
370
else if(arg_is(argv[1], L"use"))
371
{
372
if(!net_use(argc, argv)) return 1;
373
}
374
else
375
output_string(STRING_USAGE);
376
377
return 0;
378
}
379
380