This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
336 lines (295 loc) · 7.05 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <objbase.h>
#include <stdio.h>
#include <wchar.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#endif /* _MSC_VER */
#include "wslutil.h"
#include "my_gets.h"
#define DISTOR_NAME L"AOSC-OS"
void print_help(void)
{
puts(
"AOSC OS for WSL Utility 0.1.1\n"
"Usage: aosc-os [<command>]\n\n"
"Commands:\n"
" run/exec [<command line>] - Run command line in WSL.\n\n"
" config [<option> <value>] - Configure or show distor settings.\n"
" options:\n"
" enable-interop <true/false>\n"
" append-nt-path <true/false>\n"
" enable-drive-mounting <true/false>\n"
" default-uid <uid>\n"
" default-user <username> - set only\n\n"
" uninstall/clean - Uninstall the distro.\n\n"
" help - Print this message."
);
}
HRESULT new_user(void)
{
HRESULT hr;
DWORD exit_code;
char username[33];
wchar_t command[64];
do {
fputs("Enter new UNIX username: ", stdout);
my_gets_s(username, sizeof(username));
if (strlen(username) == 0)
continue;
swprintf(command, ARRAYSIZE(command), L"useradd -m %hs", username);
hr = _WslLaunchInteractive(DISTOR_NAME, command, FALSE, &exit_code);
} while (FAILED(hr) || exit_code != 0);
swprintf(command, ARRAYSIZE(command), L"usermod -aG sudo %hs", username);
hr = _WslLaunchInteractive(DISTOR_NAME, command, FALSE, &exit_code);
if (SUCCEEDED(hr) && exit_code == 0)
{
swprintf(command, ARRAYSIZE(command), L"passwd %hs", username);
do {
hr = _WslLaunchInteractive(DISTOR_NAME, command, FALSE, &exit_code);
} while (FAILED(hr) || exit_code != 0);
wchar_t username_w[33];
for (int i = 0; i < ARRAYSIZE(username_w) && username[i]; i++)
username_w[i] = username[i];
ULONG uid = uid_from_username(DISTOR_NAME, username_w);
if (uid != ULONG_MAX)
hr = _WslConfigureDistribution(DISTOR_NAME, uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
}
return hr;
}
bool install_distor(void)
{
fputs("Will install AOSC OS for WSL, continue? [y/N] ", stdout);
char ch = getchar();
char c;
while ((c = getchar()) != '\n' && c != EOF);
if (tolower(ch) == 'y')
{
puts("Installing, this may take a few minutes...");
HRESULT hr = _WslRegisterDistribution(DISTOR_NAME, L"install.tar.gz");
if (SUCCEEDED(hr))
{
DWORD exit_code;
hr = _WslLaunchInteractive(DISTOR_NAME, L"groupadd sudo && sed -i 's/^#\\s*\\(%sudo\\)/\\1/' /etc/sudoers", FALSE, &exit_code);
if (SUCCEEDED(hr))
{
hr = new_user();
}
}
if (SUCCEEDED(hr))
{
puts("Installation successful!");
return true;
}
else
{
printf("Installation failed! (0x%X)\n", hr);
}
}
else
{
puts("Abort.");
}
return false;
}
bool uninstall_distor(void)
{
fputs("Will uninstall AOSC OS for WSL, continue? [y/N] ", stdout);
char ch = getchar();
if (tolower(ch) == 'y')
{
puts("Removing filesystem...");
HRESULT hr = _WslUnregisterDistribution(DISTOR_NAME);
if (SUCCEEDED(hr))
{
puts("Successfully removed distro.");
return true;
}
else
{
printf("Failed to remove distor! (0x%X)\n", hr);
}
}
else
{
puts("Abort.");
}
return false;
}
int run_wsl(int argc, wchar_t *argv[])
{
size_t command_len = 0;
for (int i = 0; i < argc; i++)
command_len += wcslen(argv[i]) + 1;
wchar_t *command_line = NULL;
if (command_len != 0)
{
command_line = (wchar_t*)malloc(command_len * sizeof(wchar_t));
if (!command_line)
return false;
command_line[0] = L'\0';
for (int i = 0; i < argc; i++)
{
wcscat(command_line, argv[i]);
size_t len = wcslen(command_line);
if (i < (argc - 1))
{
command_line[len] = L' ';
command_line[len + 1] = L'\0';
}
}
command_line[command_len - 1] = L'\0';
}
DWORD exit_code;
HRESULT hr = _WslLaunchInteractive(DISTOR_NAME, command_line, TRUE, &exit_code);
free(command_line);
if (FAILED(hr))
{
printf("Error: 0x%X\n", hr);
return 1;
}
return exit_code;
}
bool config(int argc, wchar_t *argv[])
{
if (argc < 2)
{
ULONG default_uid;
WSL_DISTRIBUTION_FLAGS distor_flags;
if (!get_wsl_distro_config(DISTOR_NAME, &default_uid, &distor_flags))
return false;
bool enable_interop = (distor_flags & WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP);
bool append_nt_path = (distor_flags & WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH);
bool enable_drive_mounting = (distor_flags & WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING);
printf(
"enable-interop %s\n"
"append-nt-path %s\n"
"enable-drive-mounting %s\n"
"default-uid %lu\n",
enable_interop ? "true" : "false",
append_nt_path ? "true" : "false",
enable_drive_mounting ? "true" : "false",
default_uid);
}
else
{
wchar_t *option = argv[0];
wchar_t *value = argv[1];
WSL_DISTRIBUTION_FLAGS flag = 0;
ULONG uid;
if (wcscmp(option, L"enable-interop") == 0)
flag = WSL_DISTRIBUTION_FLAGS_ENABLE_INTEROP;
else if (wcscmp(option, L"append-nt-path") == 0)
flag = WSL_DISTRIBUTION_FLAGS_APPEND_NT_PATH;
else if (wcscmp(option, L"enable-drive-mounting") == 0)
flag = WSL_DISTRIBUTION_FLAGS_ENABLE_DRIVE_MOUNTING;
else if (wcscmp(option, L"default-uid") == 0)
{
uid = wcstoul(value, NULL, 10);
}
else if (wcscmp(option, L"default-user") == 0)
{
uid = uid_from_username(DISTOR_NAME, value);
if (uid == ULONG_MAX)
{
printf("Can not get UID from username '%ls'!", value);
return false;
}
}
else
{
printf("Invaild option '%ls'!", option);
return false;
}
bool enable = false;
if (flag)
{
if (wcscmp(value, L"true") == 0)
enable = true;
else if (wcscmp(value, L"false") == 0)
{
// pass
}
else
{
printf("Invaild value '%ls' for option '%ls'!", value, option);
return false;
}
}
ULONG default_uid;
WSL_DISTRIBUTION_FLAGS distor_flags;
if (!get_wsl_distro_config(DISTOR_NAME, &default_uid, &distor_flags))
return false;
if (flag)
{
if (enable)
distor_flags |= flag;
else
distor_flags &= ~flag;
}
else
default_uid = uid;
HRESULT hr = _WslConfigureDistribution(DISTOR_NAME, default_uid, distor_flags);
return SUCCEEDED(hr);
}
return true;
}
int main(void)
{
if (!load_wsl_api())
{
printf("Failed to load wsl api! (%lu)\n", GetLastError());
return 1;
}
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argv == NULL)
{
printf("CommandLineToArgvW failed! (%lu)\n", GetLastError());
return 1;
}
int retval = 0;
do {
if (!_WslIsDistributionRegistered(DISTOR_NAME))
{
if (!install_distor())
{
retval = 1;
break;
}
}
if (argc < 2)
{
retval = run_wsl(0, NULL);
break;
}
wchar_t *command = argv[1];
if (wcscmp(command, L"run") == 0 || wcscmp(command, L"exec") == 0)
{
retval = run_wsl(argc - 2, argv + 2);
}
else if (wcscmp(command, L"config") == 0)
{
if (!config(argc - 2, argv + 2))
retval = 1;
}
else if (wcscmp(command, L"uninstall") == 0 || wcscmp(command, L"clean") == 0)
{
if (!uninstall_distor())
retval = 1;
}
else
{
print_help();
retval = 1;
}
} while (0);
LocalFree(argv);
return retval;
}