urlmon: Don't create stgmed_obj for binding to object.
[wine] / programs / net / net.c
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 <stdio.h>
20 #include <string.h>
21 #include <windows.h>
22 #include <lm.h>
23 #include "resources.h"
24
25 #define NET_START 0001
26 #define NET_STOP  0002
27
28 int output_string(int msg, ...)
29 {
30     char msg_buffer[8192];
31     va_list arguments;
32
33     LoadString(GetModuleHandle(NULL), msg, msg_buffer, sizeof(msg_buffer));
34     va_start(arguments, msg);
35     vprintf(msg_buffer, arguments);
36     va_end(arguments);
37     return 0;
38 }
39
40 static BOOL net_use(int argc, char *argv[])
41 {
42     USE_INFO_2 *buffer, *connection;
43     DWORD read, total, resume_handle, rc, i;
44     const char *status_description[] = { "OK", "Paused", "Disconnected", "An error occurred",
45                                          "A network error occurred", "Connection is being made",
46                                          "Reconnecting" };
47     resume_handle = 0;
48     buffer = NULL;
49
50     if(argc<3)
51     {
52         do {
53             rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
54             if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
55             {
56                 break;
57             }
58
59             if(total == 0)
60             {
61                 output_string(STRING_NO_ENTRIES);
62                 break;
63             }
64
65             output_string(STRING_USE_HEADER);
66             for (i = 0, connection = buffer; i < read; ++i, ++connection)
67                 output_string(STRING_USE_ENTRY, status_description[connection->ui2_status], connection->ui2_local,
68                                 connection->ui2_remote, connection->ui2_refcount);
69
70             if (buffer != NULL) NetApiBufferFree(buffer);
71         } while (rc == ERROR_MORE_DATA);
72
73         return TRUE;
74     }
75
76     return FALSE;
77 }
78
79 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
80 {
81     LPENUM_SERVICE_STATUS dependencies = NULL;
82     DWORD buffer_size = 0;
83     DWORD count = 0, counter;
84     BOOL result;
85     SC_HANDLE dependent_serviceHandle;
86     SERVICE_STATUS_PROCESS ssp;
87
88     result = EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
89
90     if(!result && (GetLastError() == ERROR_MORE_DATA))
91     {
92         dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
93         if(EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
94         {
95             for(counter = 0; counter < count; counter++)
96             {
97                 output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
98                 dependent_serviceHandle = OpenService(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
99                 if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
100                 CloseServiceHandle(dependent_serviceHandle);
101                 if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
102            }
103         }
104     }
105
106     if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
107     HeapFree(GetProcessHeap(), 0, dependencies);
108     return result;
109 }
110
111 static BOOL net_service(int operation, char *service_name)
112 {
113     SC_HANDLE SCManager, serviceHandle;
114     BOOL result = 0;
115     char service_display_name[4096];
116     DWORD buffer_size = sizeof(service_display_name);
117
118     SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
119     if(!SCManager)
120     {
121         output_string(STRING_NO_SCM);
122         return FALSE;
123     }
124     serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
125     if(!serviceHandle)
126     {
127         output_string(STRING_NO_SVCHANDLE);
128         CloseServiceHandle(SCManager);
129         return FALSE;
130     }
131
132
133     GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
134     if (!service_display_name[0]) strcpy(service_display_name, service_name);
135
136     switch(operation)
137     {
138     case NET_START:
139         output_string(STRING_START_SVC, service_display_name);
140         result = StartService(serviceHandle, 0, NULL);
141
142         if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
143         else output_string(STRING_START_SVC_FAIL, service_display_name);
144         break;
145     case NET_STOP:
146         output_string(STRING_STOP_SVC, service_display_name);
147         result = StopService(SCManager, serviceHandle);
148
149         if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
150         else output_string(STRING_STOP_SVC_FAIL, service_display_name);
151         break;
152     }
153
154     CloseServiceHandle(serviceHandle);
155     CloseServiceHandle(SCManager);
156     return result;
157 }
158
159 int main(int argc, char *argv[])
160 {
161     if (argc < 2)
162     {
163         output_string(STRING_USAGE);
164         return 1;
165     }
166
167     if(!strcasecmp(argv[1], "help"))
168     {
169         output_string(STRING_HELP_USAGE);
170     }
171
172     if(!strcasecmp(argv[1], "start"))
173     {
174         if(argc < 3)
175         {
176             output_string(STRING_START_USAGE);
177             return 1;
178         }
179
180         if(!net_service(NET_START, argv[2]))
181         {
182             return 1;
183         }
184         return 0;
185     }
186
187     if(!strcasecmp(argv[1], "stop"))
188     {
189         if(argc < 3)
190         {
191             output_string(STRING_STOP_USAGE);
192             return 1;
193         }
194
195         if(!net_service(NET_STOP, argv[2]))
196         {
197             return 1;
198         }
199         return 0;
200     }
201
202     if(!strcasecmp(argv[1], "use"))
203     {
204         if(!net_use(argc, argv)) return 1;
205     }
206
207     return 0;
208 }