[NET]
[reactos.git] / reactos / base / applications / network / net / cmdUser.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE:
5 * PURPOSE:
6 *
7 * PROGRAMMERS: Eric Kohl
8 */
9
10 #include "net.h"
11
12
13 static
14 int
15 CompareInfo(const void *a,
16 const void *b)
17 {
18 return _wcsicmp(((PUSER_INFO_0)a)->usri0_name,
19 ((PUSER_INFO_0)b)->usri0_name);
20 }
21
22
23 static
24 NET_API_STATUS
25 EnumerateUsers(VOID)
26 {
27 PUSER_INFO_0 pBuffer = NULL;
28 PSERVER_INFO_100 pServer = NULL;
29 DWORD dwRead = 0, dwTotal = 0;
30 DWORD i;
31 DWORD_PTR ResumeHandle = 0;
32 NET_API_STATUS Status;
33
34 Status = NetServerGetInfo(NULL,
35 100,
36 (LPBYTE*)&pServer);
37 if (Status != NERR_Success)
38 return Status;
39
40 PrintToConsole(L"\nUser accounts for \\\\%s\n\n", pServer->sv100_name);
41
42 NetApiBufferFree(pServer);
43
44 PrintToConsole(L"------------------------------------------\n");
45
46 Status = NetUserEnum(NULL,
47 0,
48 0,
49 (LPBYTE*)&pBuffer,
50 MAX_PREFERRED_LENGTH,
51 &dwRead,
52 &dwTotal,
53 &ResumeHandle);
54 if (Status != NERR_Success)
55 return Status;
56
57 qsort(pBuffer,
58 dwRead,
59 sizeof(PUSER_INFO_0),
60 CompareInfo);
61
62 // printf("dwRead: %lu dwTotal: %lu\n", dwRead, dwTotal);
63 for (i = 0; i < dwRead; i++)
64 {
65 // printf("%p\n", pBuffer[i].lgrpi0_name);
66 if (pBuffer[i].usri0_name)
67 PrintToConsole(L"%s\n", pBuffer[i].usri0_name);
68 }
69
70 NetApiBufferFree(pBuffer);
71
72 return NERR_Success;
73 }
74
75
76 static
77 VOID
78 PrintDateTime(DWORD dwSeconds)
79 {
80 LARGE_INTEGER Time;
81 FILETIME FileTime;
82 SYSTEMTIME SystemTime;
83 WCHAR DateBuffer[80];
84 WCHAR TimeBuffer[80];
85
86 RtlSecondsSince1970ToTime(dwSeconds, &Time);
87 FileTime.dwLowDateTime = Time.u.LowPart;
88 FileTime.dwHighDateTime = Time.u.HighPart;
89 FileTimeToSystemTime(&FileTime, &SystemTime);
90
91 GetDateFormatW(LOCALE_USER_DEFAULT,
92 DATE_SHORTDATE,
93 &SystemTime,
94 NULL,
95 DateBuffer,
96 80);
97
98 GetTimeFormatW(LOCALE_USER_DEFAULT,
99 TIME_NOSECONDS,
100 &SystemTime,
101 NULL,
102 TimeBuffer,
103 80);
104
105 PrintToConsole(L"%s %s\n", DateBuffer, TimeBuffer);
106 }
107
108
109 static
110 NET_API_STATUS
111 DisplayUser(LPWSTR lpUserName)
112 {
113 PUSER_MODALS_INFO_0 pUserModals = NULL;
114 PUSER_INFO_4 pUserInfo = NULL;
115 NET_API_STATUS Status;
116
117 /* Modify the user */
118 Status = NetUserGetInfo(NULL,
119 lpUserName,
120 4,
121 (LPBYTE*)&pUserInfo);
122 if (Status != NERR_Success)
123 return Status;
124
125 Status = NetUserModalsGet(NULL,
126 0,
127 (LPBYTE*)&pUserModals);
128 if (Status != NERR_Success)
129 goto done;
130
131 PrintToConsole(L"User name %s\n", pUserInfo->usri4_name);
132 PrintToConsole(L"Full name %s\n", pUserInfo->usri4_full_name);
133 PrintToConsole(L"Comment %s\n", pUserInfo->usri4_comment);
134 PrintToConsole(L"User comment %s\n", pUserInfo->usri4_usr_comment);
135 PrintToConsole(L"Country code %03ld ()\n", pUserInfo->usri4_country_code);
136 PrintToConsole(L"Account active %s\n", (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)? L"No" : ((pUserInfo->usri4_flags & UF_LOCKOUT) ? L"Locked" : L"Yes"));
137 PrintToConsole(L"Account expires ");
138 if (pUserInfo->usri4_acct_expires == TIMEQ_FOREVER)
139 PrintToConsole(L"Never\n");
140 else
141 PrintDateTime(pUserInfo->usri4_acct_expires);
142
143 PrintToConsole(L"\n");
144 PrintToConsole(L"Password last set \n");
145
146 PrintToConsole(L"Password expires ");
147 if (pUserModals->usrmod0_max_passwd_age == TIMEQ_FOREVER)
148 PrintToConsole(L"Never\n");
149 else
150 PrintDateTime(pUserInfo->usri4_acct_expires);
151
152 PrintToConsole(L"Password changeable \n");
153 PrintToConsole(L"Password required \n");
154 PrintToConsole(L"User may change password \n");
155
156 PrintToConsole(L"\n");
157 PrintToConsole(L"Workstation allowed %s\n", pUserInfo->usri4_workstations);
158 PrintToConsole(L"Logon script %s\n", pUserInfo->usri4_script_path);
159 PrintToConsole(L"User profile %s\n", pUserInfo->usri4_profile);
160 PrintToConsole(L"Home directory %s\n", pUserInfo->usri4_home_dir);
161 PrintToConsole(L"Last logon ");
162 if (pUserInfo->usri4_last_logon == 0)
163 PrintToConsole(L"Never\n");
164 else
165 PrintDateTime(pUserInfo->usri4_last_logon);
166 PrintToConsole(L"\n");
167 PrintToConsole(L"Logon hours allowed \n");
168 PrintToConsole(L"\n");
169 PrintToConsole(L"Local group memberships \n");
170 PrintToConsole(L"Global group memberships \n");
171
172 done:
173 if (pUserModals != NULL)
174 NetApiBufferFree(pUserModals);
175
176 if (pUserInfo != NULL)
177 NetApiBufferFree(pUserInfo);
178
179 return NERR_Success;
180 }
181
182
183 INT
184 cmdUser(
185 INT argc,
186 WCHAR **argv)
187 {
188 INT i, j;
189 INT result = 0;
190 BOOL bAdd = FALSE;
191 BOOL bDelete = FALSE;
192 #if 0
193 BOOL bDomain = FALSE;
194 #endif
195 LPWSTR lpUserName = NULL;
196 LPWSTR lpPassword = NULL;
197 PUSER_INFO_4 pUserInfo = NULL;
198 USER_INFO_4 UserInfo;
199 NET_API_STATUS Status;
200
201 if (argc == 2)
202 {
203 Status = EnumerateUsers();
204 printf("Status: %lu\n", Status);
205 return 0;
206 }
207 else if (argc == 3)
208 {
209 Status = DisplayUser(argv[2]);
210 printf("Status: %lu\n", Status);
211 return 0;
212 }
213
214 i = 2;
215 if (argv[i][0] != L'/')
216 {
217 lpUserName = argv[i];
218 printf("User: %S\n", lpUserName);
219 i++;
220 }
221
222 if (argv[i][0] != L'/')
223 {
224 lpPassword = argv[i];
225 printf("Password: %S\n", lpPassword);
226 i++;
227 }
228
229 for (j = i; j < argc; j++)
230 {
231 if (_wcsicmp(argv[j], L"/help") == 0)
232 {
233 PrintResourceString(IDS_USER_HELP);
234 return 0;
235 }
236 else if (_wcsicmp(argv[j], L"/add") == 0)
237 {
238 bAdd = TRUE;
239 }
240 else if (_wcsicmp(argv[j], L"/delete") == 0)
241 {
242 bDelete = TRUE;
243 }
244 else if (_wcsicmp(argv[j], L"/domain") == 0)
245 {
246 printf("The /DOMAIN option is not supported yet!\n");
247 #if 0
248 bDomain = TRUE;
249 #endif
250 }
251 }
252
253 if (bAdd && bDelete)
254 {
255 result = 1;
256 goto done;
257 }
258
259 if (!bAdd && !bDelete)
260 {
261 /* Modify the user */
262 Status = NetUserGetInfo(NULL,
263 lpUserName,
264 4,
265 (LPBYTE*)&pUserInfo);
266 printf("Status: %lu\n", Status);
267 }
268 else if (bAdd && !bDelete)
269 {
270 /* Add the user */
271 ZeroMemory(&UserInfo, sizeof(USER_INFO_4));
272
273 UserInfo.usri4_name = lpUserName;
274 UserInfo.usri4_password = lpPassword;
275 UserInfo.usri4_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT;
276
277 pUserInfo = &UserInfo;
278 }
279
280 for (j = i; j < argc; j++)
281 {
282 if (_wcsnicmp(argv[j], L"/active:", 8) == 0)
283 {
284 }
285 else if (_wcsnicmp(argv[j], L"/comment:", 9) == 0)
286 {
287 pUserInfo->usri4_comment = &argv[j][9];
288 }
289 else if (_wcsnicmp(argv[j], L"/countrycode:", 13) == 0)
290 {
291 }
292 else if (_wcsnicmp(argv[j], L"/expires:", 9) == 0)
293 {
294 }
295 else if (_wcsnicmp(argv[j], L"/fullname:", 10) == 0)
296 {
297 pUserInfo->usri4_full_name = &argv[j][10];
298 }
299 else if (_wcsnicmp(argv[j], L"/homedir:", 9) == 0)
300 {
301 pUserInfo->usri4_home_dir = &argv[j][9];
302 }
303 else if (_wcsnicmp(argv[j], L"/passwordchg:", 13) == 0)
304 {
305 }
306 else if (_wcsnicmp(argv[j], L"/passwordreq:", 13) == 0)
307 {
308 }
309 else if (_wcsnicmp(argv[j], L"/profilepath:", 13) == 0)
310 {
311 pUserInfo->usri4_profile = &argv[j][13];
312 }
313 else if (_wcsnicmp(argv[j], L"/scriptpath:", 12) == 0)
314 {
315 pUserInfo->usri4_script_path = &argv[j][12];
316 }
317 else if (_wcsnicmp(argv[j], L"/times:", 7) == 0)
318 {
319 }
320 else if (_wcsnicmp(argv[j], L"/usercomment:", 13) == 0)
321 {
322 pUserInfo->usri4_usr_comment = &argv[j][13];
323 }
324 else if (_wcsnicmp(argv[j], L"/workstations:", 14) == 0)
325 {
326 }
327 }
328
329 if (!bAdd && !bDelete)
330 {
331 /* Modify the user */
332 Status = NetUserSetInfo(NULL,
333 lpUserName,
334 4,
335 (LPBYTE)pUserInfo,
336 NULL);
337 printf("Status: %lu\n", Status);
338 }
339 else if (bAdd && !bDelete)
340 {
341 /* Add the user */
342 Status = NetUserAdd(NULL,
343 4,
344 (LPBYTE)pUserInfo,
345 NULL);
346 printf("Status: %lu\n", Status);
347 }
348 else if (!bAdd && bDelete)
349 {
350 /* Delete the user */
351 Status = NetUserDel(NULL,
352 lpUserName);
353 printf("Status: %lu\n", Status);
354 }
355
356 done:
357 if (!bAdd && !bDelete && pUserInfo != NULL)
358 NetApiBufferFree(pUserInfo);
359
360 if (result != 0)
361 PrintResourceString(IDS_USER_SYNTAX);
362
363 return result;
364 }
365
366 /* EOF */