[NET] Load the names of the days of week for the 'times' parser routine from the...
[reactos.git] / base / applications / network / net / cmdUser.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE: base/applications/network/net/cmdUser.c
5 * PURPOSE:
6 *
7 * PROGRAMMERS: Eric Kohl
8 * Curtis Wilson
9 */
10
11 #include "net.h"
12
13 #define SECONDS_PER_DAY (60 * 60 * 24)
14 #define SECONDS_PER_HOUR (60 * 60)
15 #define HOURS_PER_DAY 24
16 #define DAYS_PER_WEEK 7
17
18 typedef struct _COUNTY_TABLE
19 {
20 DWORD dwCountryCode;
21 DWORD dwMessageId;
22 } COUNTRY_TABLE, *PCOUNTRY_TABLE;
23
24
25 static WCHAR szPasswordChars[] = L"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%_-+:";
26 static COUNTRY_TABLE CountryTable[] =
27 { { 0, 5080}, // System Default
28 { 1, 5081}, // United States
29 { 2, 5082}, // Canada (French)
30 { 3, 5083}, // Latin America
31 { 31, 5084}, // Netherlands
32 { 32, 5085}, // Belgium
33 { 33, 5086}, // France
34 { 34, 5090}, // Spain
35 { 39, 5087}, // Italy
36 { 41, 5088}, // Switzerland
37 { 44, 5089}, // United Kingdom
38 { 45, 5091}, // Denmark
39 { 46, 5092}, // Sweden
40 { 47, 5093}, // Norway
41 { 49, 5094}, // Germany
42 { 61, 5095}, // Australia
43 { 81, 5096}, // Japan
44 { 82, 5097}, // Korea
45 { 86, 5098}, // China (PRC)
46 { 88, 5099}, // Taiwan
47 { 99, 5100}, // Asia
48 {351, 5101}, // Portugal
49 {358, 5102}, // Finland
50 {785, 5103}, // Arabic
51 {972, 5104} }; // Hebrew
52
53
54 static
55 int
56 CompareInfo(const void *a, const void *b)
57 {
58 return _wcsicmp(((PUSER_INFO_0)a)->usri0_name,
59 ((PUSER_INFO_0)b)->usri0_name);
60 }
61
62
63 static
64 NET_API_STATUS
65 EnumerateUsers(VOID)
66 {
67 PUSER_INFO_0 pBuffer = NULL;
68 PSERVER_INFO_100 pServer = NULL;
69 DWORD dwRead = 0, dwTotal = 0;
70 DWORD i;
71 DWORD ResumeHandle = 0;
72 NET_API_STATUS Status;
73
74 Status = NetServerGetInfo(NULL,
75 100,
76 (LPBYTE*)&pServer);
77 if (Status != NERR_Success)
78 return Status;
79
80 ConPuts(StdOut, L"\n");
81 PrintMessageStringV(4410, pServer->sv100_name);
82 ConPuts(StdOut, L"\n");
83 PrintPadding(L'-', 79);
84 ConPuts(StdOut, L"\n");
85
86 NetApiBufferFree(pServer);
87
88 do
89 {
90 Status = NetUserEnum(NULL,
91 0,
92 0,
93 (LPBYTE*)&pBuffer,
94 MAX_PREFERRED_LENGTH,
95 &dwRead,
96 &dwTotal,
97 &ResumeHandle);
98 if ((Status != NERR_Success) && (Status != ERROR_MORE_DATA))
99 return Status;
100
101 qsort(pBuffer,
102 dwRead,
103 sizeof(PUSER_INFO_0),
104 CompareInfo);
105
106 for (i = 0; i < dwRead; i++)
107 {
108 if (pBuffer[i].usri0_name)
109 ConPrintf(StdOut, L"%s\n", pBuffer[i].usri0_name);
110 }
111
112 NetApiBufferFree(pBuffer);
113 pBuffer = NULL;
114 }
115 while (Status == ERROR_MORE_DATA);
116
117 return NERR_Success;
118 }
119
120
121 static
122 VOID
123 PrintDateTime(DWORD dwSeconds)
124 {
125 LARGE_INTEGER Time;
126 FILETIME FileTime;
127 SYSTEMTIME SystemTime;
128 WCHAR DateBuffer[80];
129 WCHAR TimeBuffer[80];
130
131 RtlSecondsSince1970ToTime(dwSeconds, &Time);
132 FileTime.dwLowDateTime = Time.u.LowPart;
133 FileTime.dwHighDateTime = Time.u.HighPart;
134 FileTimeToLocalFileTime(&FileTime, &FileTime);
135 FileTimeToSystemTime(&FileTime, &SystemTime);
136
137 GetDateFormatW(LOCALE_USER_DEFAULT,
138 DATE_SHORTDATE,
139 &SystemTime,
140 NULL,
141 DateBuffer,
142 ARRAYSIZE(DateBuffer));
143
144 GetTimeFormatW(LOCALE_USER_DEFAULT,
145 TIME_NOSECONDS,
146 &SystemTime,
147 NULL,
148 TimeBuffer,
149 ARRAYSIZE(TimeBuffer));
150
151 ConPrintf(StdOut, L"%s %s", DateBuffer, TimeBuffer);
152 }
153
154
155 static
156 VOID
157 PrintLocalTime(DWORD dwSeconds)
158 {
159 LARGE_INTEGER Time;
160 FILETIME FileTime;
161 SYSTEMTIME SystemTime;
162 WCHAR TimeBuffer[80];
163
164 RtlSecondsSince1970ToTime(dwSeconds, &Time);
165 FileTime.dwLowDateTime = Time.u.LowPart;
166 FileTime.dwHighDateTime = Time.u.HighPart;
167 FileTimeToSystemTime(&FileTime, &SystemTime);
168
169 GetTimeFormatW(LOCALE_USER_DEFAULT,
170 TIME_NOSECONDS,
171 &SystemTime,
172 NULL,
173 TimeBuffer,
174 ARRAYSIZE(TimeBuffer));
175
176 ConPrintf(StdOut, L"%s", TimeBuffer);
177 }
178
179
180 static
181 DWORD
182 GetTimeInSeconds(VOID)
183 {
184 LARGE_INTEGER Time;
185 FILETIME FileTime;
186 DWORD dwSeconds;
187
188 GetSystemTimeAsFileTime(&FileTime);
189 Time.u.LowPart = FileTime.dwLowDateTime;
190 Time.u.HighPart = FileTime.dwHighDateTime;
191 RtlTimeToSecondsSince1970(&Time, &dwSeconds);
192
193 return dwSeconds;
194 }
195
196
197 static
198 BOOL
199 GetCountryFromCountryCode(
200 _In_ DWORD dwCountryCode,
201 _In_ DWORD dwCountryLength,
202 _Out_ PWSTR szCountryBuffer)
203 {
204 DWORD i;
205
206 for (i = 0; i < ARRAYSIZE(CountryTable); i++)
207 {
208 if (CountryTable[i].dwCountryCode == dwCountryCode)
209 {
210 if (szCountryBuffer != NULL && dwCountryLength > 0)
211 {
212 FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
213 hModuleNetMsg,
214 CountryTable[i].dwMessageId,
215 LANG_USER_DEFAULT,
216 szCountryBuffer,
217 dwCountryLength,
218 NULL);
219 }
220
221 return TRUE;
222 }
223 }
224
225 return FALSE;
226 }
227
228
229 static
230 BOOL
231 GetBitValue(
232 PBYTE pBitmap,
233 DWORD dwBitNumber)
234 {
235 DWORD dwIndex = dwBitNumber / 8;
236 BYTE Mask = 1 << (dwBitNumber & 7);
237
238 return ((pBitmap[dwIndex] & Mask) != 0);
239 }
240
241
242 static
243 VOID
244 SetBitValue(
245 PBYTE pBitmap,
246 DWORD dwBitNumber)
247 {
248 DWORD dwIndex = dwBitNumber / 8;
249 BYTE Mask = 1 << (dwBitNumber & 7);
250
251 pBitmap[dwIndex] |= Mask;
252 }
253
254
255 static
256 VOID
257 PrintLogonHours(
258 DWORD dwUnitsPerWeek,
259 PBYTE pLogonHours,
260 INT nPaddedLength)
261 {
262 DWORD dwUnitsPerDay, dwBitNumber, dwSecondsPerUnit;
263 DWORD dwStartTime, dwEndTime, dwStartDay, dwEndDay, dwBias;
264 BOOL bBitValue, bFirst = TRUE;
265 TIME_ZONE_INFORMATION TimeZoneInformation;
266
267 GetTimeZoneInformation(&TimeZoneInformation);
268 dwBias = (TimeZoneInformation.Bias / 60) * SECONDS_PER_HOUR;
269
270 if ((dwUnitsPerWeek == 0) ||
271 ((dwUnitsPerWeek %7) != 0))
272 return;
273
274 dwUnitsPerDay = dwUnitsPerWeek / 7;
275
276 if (((dwUnitsPerDay % 24) != 0) ||
277 ((dwUnitsPerDay / 24) > 6))
278 return;
279
280 dwSecondsPerUnit = (SECONDS_PER_DAY) / dwUnitsPerDay;
281
282 for (dwBitNumber = 0; dwBitNumber < dwUnitsPerWeek; dwBitNumber++)
283 {
284 bBitValue = GetBitValue(pLogonHours, dwBitNumber);
285 if (bBitValue)
286 {
287 dwStartTime = dwSecondsPerUnit * dwBitNumber;
288
289 while (bBitValue != 0 && dwBitNumber < dwUnitsPerWeek)
290 {
291 dwBitNumber++;
292 if (dwBitNumber < dwUnitsPerWeek)
293 bBitValue = GetBitValue(pLogonHours, dwBitNumber);
294 }
295
296 dwEndTime = dwSecondsPerUnit * dwBitNumber;
297
298 if (!bFirst)
299 PrintPadding(L' ', nPaddedLength);
300
301 if (dwStartTime == 0 && dwEndTime == (SECONDS_PER_DAY * 7))
302 {
303 PrintMessageString(4302);
304 ConPuts(StdOut, L"\n");
305 }
306 else
307 {
308 dwStartDay = dwStartTime / SECONDS_PER_DAY;
309 dwEndDay = (dwEndTime / SECONDS_PER_DAY) % 7;
310
311 PrintMessageString(4307 + dwStartDay);
312 ConPuts(StdOut, L" ");
313
314 /* Convert from GMT to local timezone */
315 PrintLocalTime((dwStartTime % SECONDS_PER_DAY) - dwBias);
316
317 ConPrintf(StdOut, L" - ");
318 if (dwStartDay != dwEndDay)
319 {
320 PrintMessageString(4307 + dwEndDay);
321 ConPuts(StdOut, L" ");
322 }
323
324 /* Convert from GMT to local timezone */
325 PrintLocalTime((dwEndTime % SECONDS_PER_DAY) - dwBias);
326 ConPuts(StdOut, L"\n");
327 }
328
329 bFirst = FALSE;
330 }
331 }
332
333 if (bFirst)
334 {
335 /* No logon hours */
336 PrintMessageString(4434);
337 ConPuts(StdOut, L"\n");
338 }
339 }
340
341
342 static
343 NET_API_STATUS
344 DisplayUser(LPWSTR lpUserName)
345 {
346 PUSER_MODALS_INFO_0 pUserModals = NULL;
347 PUSER_INFO_4 pUserInfo = NULL;
348 PLOCALGROUP_USERS_INFO_0 pLocalGroupInfo = NULL;
349 PGROUP_USERS_INFO_0 pGroupInfo = NULL;
350 DWORD dwLocalGroupRead, dwLocalGroupTotal;
351 DWORD dwGroupRead, dwGroupTotal;
352 DWORD dwLastSet;
353 DWORD i;
354 WCHAR szCountry[40];
355 INT nPaddedLength = 36;
356 NET_API_STATUS Status;
357
358 /* Modify the user */
359 Status = NetUserGetInfo(NULL,
360 lpUserName,
361 4,
362 (LPBYTE*)&pUserInfo);
363 if (Status != NERR_Success)
364 return Status;
365
366 Status = NetUserModalsGet(NULL,
367 0,
368 (LPBYTE*)&pUserModals);
369 if (Status != NERR_Success)
370 goto done;
371
372 Status = NetUserGetLocalGroups(NULL,
373 lpUserName,
374 0,
375 0,
376 (LPBYTE*)&pLocalGroupInfo,
377 MAX_PREFERRED_LENGTH,
378 &dwLocalGroupRead,
379 &dwLocalGroupTotal);
380 if (Status != NERR_Success)
381 goto done;
382
383 Status = NetUserGetGroups(NULL,
384 lpUserName,
385 0,
386 (LPBYTE*)&pGroupInfo,
387 MAX_PREFERRED_LENGTH,
388 &dwGroupRead,
389 &dwGroupTotal);
390 if (Status != NERR_Success)
391 goto done;
392
393 PrintPaddedMessageString(4411, nPaddedLength);
394 ConPrintf(StdOut, L"%s\n", pUserInfo->usri4_name);
395
396 PrintPaddedMessageString(4412, nPaddedLength);
397 ConPrintf(StdOut, L"%s\n", pUserInfo->usri4_full_name);
398
399 PrintPaddedMessageString(4413, nPaddedLength);
400 ConPrintf(StdOut, L"%s\n", pUserInfo->usri4_comment);
401
402 PrintPaddedMessageString(4414, nPaddedLength);
403 ConPrintf(StdOut, L"%s\n", pUserInfo->usri4_usr_comment);
404
405 PrintPaddedMessageString(4416, nPaddedLength);
406 GetCountryFromCountryCode(pUserInfo->usri4_country_code,
407 ARRAYSIZE(szCountry), szCountry);
408 ConPrintf(StdOut, L"%03ld (%s)\n", pUserInfo->usri4_country_code, szCountry);
409
410 PrintPaddedMessageString(4419, nPaddedLength);
411 if (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)
412 PrintMessageString(4301);
413 else if (pUserInfo->usri4_flags & UF_LOCKOUT)
414 PrintMessageString(4440);
415 else
416 PrintMessageString(4300);
417 ConPuts(StdOut, L"\n");
418
419 PrintPaddedMessageString(4420, nPaddedLength);
420 if (pUserInfo->usri4_acct_expires == TIMEQ_FOREVER)
421 PrintMessageString(4305);
422 else
423 PrintDateTime(pUserInfo->usri4_acct_expires);
424 ConPuts(StdOut, L"\n\n");
425
426 PrintPaddedMessageString(4421, nPaddedLength);
427 dwLastSet = GetTimeInSeconds() - pUserInfo->usri4_password_age;
428 PrintDateTime(dwLastSet);
429 ConPuts(StdOut, L"\n");
430
431 PrintPaddedMessageString(4422, nPaddedLength);
432 if ((pUserInfo->usri4_flags & UF_DONT_EXPIRE_PASSWD) || pUserModals->usrmod0_max_passwd_age == TIMEQ_FOREVER)
433 PrintMessageString(4305);
434 else
435 PrintDateTime(dwLastSet + pUserModals->usrmod0_max_passwd_age);
436 ConPuts(StdOut, L"\n");
437
438 PrintPaddedMessageString(4423, nPaddedLength);
439 PrintDateTime(dwLastSet + pUserModals->usrmod0_min_passwd_age);
440 ConPuts(StdOut, L"\n");
441
442 PrintPaddedMessageString(4437, nPaddedLength);
443 PrintMessageString((pUserInfo->usri4_flags & UF_PASSWD_NOTREQD) ? 4301 : 4300);
444 ConPuts(StdOut, L"\n");
445
446 PrintPaddedMessageString(4438, nPaddedLength);
447 PrintMessageString((pUserInfo->usri4_flags & UF_PASSWD_CANT_CHANGE) ? 4301 : 4300);
448 ConPuts(StdOut, L"\n\n");
449
450 PrintPaddedMessageString(4424, nPaddedLength);
451 if (pUserInfo->usri4_workstations == NULL || wcslen(pUserInfo->usri4_workstations) == 0)
452 PrintMessageString(4302);
453 else
454 ConPrintf(StdOut, L"%s", pUserInfo->usri4_workstations);
455 ConPuts(StdOut, L"\n");
456
457 PrintPaddedMessageString(4429, nPaddedLength);
458 ConPrintf(StdOut, L"%s\n", pUserInfo->usri4_script_path);
459
460 PrintPaddedMessageString(4439, nPaddedLength);
461 ConPrintf(StdOut, L"%s\n", pUserInfo->usri4_profile);
462
463 PrintPaddedMessageString(4436, nPaddedLength);
464 ConPrintf(StdOut, L"%s\n", pUserInfo->usri4_home_dir);
465
466 PrintPaddedMessageString(4430, nPaddedLength);
467 if (pUserInfo->usri4_last_logon == 0)
468 PrintMessageString(4305);
469 else
470 PrintDateTime(pUserInfo->usri4_last_logon);
471 ConPuts(StdOut, L"\n\n");
472
473 PrintPaddedMessageString(4432, nPaddedLength);
474 if (pUserInfo->usri4_logon_hours == NULL)
475 {
476 PrintMessageString(4302);
477 ConPuts(StdOut, L"\n");
478 }
479 else
480 {
481 PrintLogonHours(pUserInfo->usri4_units_per_week,
482 pUserInfo->usri4_logon_hours,
483 nPaddedLength);
484 }
485
486 ConPuts(StdOut, L"\n");
487 PrintPaddedMessageString(4427, nPaddedLength);
488 if (dwLocalGroupTotal != 0 && pLocalGroupInfo != NULL)
489 {
490 for (i = 0; i < dwLocalGroupTotal; i++)
491 {
492 if (i != 0)
493 PrintPadding(L' ', nPaddedLength);
494 ConPrintf(StdOut, L"*%s\n", pLocalGroupInfo[i].lgrui0_name);
495 }
496 }
497 else
498 {
499 ConPuts(StdOut, L"\n");
500 }
501
502 PrintPaddedMessageString(4431, nPaddedLength);
503 if (dwGroupTotal != 0 && pGroupInfo != NULL)
504 {
505 for (i = 0; i < dwGroupTotal; i++)
506 {
507 if (i != 0)
508 PrintPadding(L' ', nPaddedLength);
509 ConPrintf(StdOut, L"*%s\n", pGroupInfo[i].grui0_name);
510 }
511 }
512 else
513 {
514 ConPuts(StdOut, L"\n");
515 }
516
517 done:
518 if (pGroupInfo != NULL)
519 NetApiBufferFree(pGroupInfo);
520
521 if (pLocalGroupInfo != NULL)
522 NetApiBufferFree(pLocalGroupInfo);
523
524 if (pUserModals != NULL)
525 NetApiBufferFree(pUserModals);
526
527 if (pUserInfo != NULL)
528 NetApiBufferFree(pUserInfo);
529
530 return NERR_Success;
531 }
532
533
534 static
535 VOID
536 ReadPassword(
537 LPWSTR *lpPassword,
538 LPBOOL lpAllocated)
539 {
540 WCHAR szPassword1[PWLEN + 1];
541 WCHAR szPassword2[PWLEN + 1];
542 LPWSTR ptr;
543
544 *lpAllocated = FALSE;
545
546 while (TRUE)
547 {
548 PrintMessageString(4358);
549 ReadFromConsole(szPassword1, PWLEN + 1, FALSE);
550 ConPuts(StdOut, L"\n");
551
552 PrintMessageString(4361);
553 ReadFromConsole(szPassword2, PWLEN + 1, FALSE);
554 ConPuts(StdOut, L"\n");
555
556 if (wcslen(szPassword1) == wcslen(szPassword2) &&
557 wcscmp(szPassword1, szPassword2) == 0)
558 {
559 ptr = HeapAlloc(GetProcessHeap(),
560 0,
561 (wcslen(szPassword1) + 1) * sizeof(WCHAR));
562 if (ptr != NULL)
563 {
564 wcscpy(ptr, szPassword1);
565 *lpPassword = ptr;
566 *lpAllocated = TRUE;
567 return;
568 }
569 }
570 else
571 {
572 ConPuts(StdOut, L"\n");
573 PrintMessageString(3728);
574 *lpPassword = NULL;
575 }
576 }
577 }
578
579
580 static
581 VOID
582 GenerateRandomPassword(
583 LPWSTR *lpPassword,
584 LPBOOL lpAllocated)
585 {
586 LPWSTR pPassword = NULL;
587 INT nCharsLen, i, nLength = 8;
588
589 srand(GetTickCount());
590
591 pPassword = HeapAlloc(GetProcessHeap(),
592 HEAP_ZERO_MEMORY,
593 (nLength + 1) * sizeof(WCHAR));
594 if (pPassword == NULL)
595 return;
596
597 nCharsLen = wcslen(szPasswordChars);
598
599 for (i = 0; i < nLength; i++)
600 {
601 pPassword[i] = szPasswordChars[rand() % nCharsLen];
602 }
603
604 *lpPassword = pPassword;
605 *lpAllocated = TRUE;
606 }
607
608
609 static
610 NET_API_STATUS
611 BuildWorkstationsList(
612 _Out_ PWSTR *pWorkstationsList,
613 _In_ PWSTR pRaw)
614 {
615 BOOL isLastSep, isSep;
616 INT i, j;
617 WCHAR c;
618 INT nLength = 0;
619 INT nArgs = 0;
620 INT nRawLength;
621 PWSTR pList;
622
623 /* Check for invalid characters in the raw string */
624 if (wcspbrk(pRaw, L"/[]=?\\+:.") != NULL)
625 return 3952;
626
627 /* Count the number of workstations in the list and
628 * the required buffer size */
629 isLastSep = FALSE;
630 isSep = FALSE;
631 nRawLength = wcslen(pRaw);
632 for (i = 0; i < nRawLength; i++)
633 {
634 c = pRaw[i];
635 if (c == L',' || c == L';')
636 isSep = TRUE;
637
638 if (isSep == TRUE)
639 {
640 if ((isLastSep == FALSE) && (i != 0) && (i != nRawLength - 1))
641 nLength++;
642 }
643 else
644 {
645 nLength++;
646
647 if (isLastSep == TRUE || (isLastSep == FALSE && i == 0))
648 nArgs++;
649 }
650
651 isLastSep = isSep;
652 isSep = FALSE;
653 }
654
655 nLength++;
656
657 /* Leave, if there are no workstations in the list */
658 if (nArgs == 0)
659 {
660 pWorkstationsList = NULL;
661 return NERR_Success;
662 }
663
664 /* Fail if there are more than eight workstations in the list */
665 if (nArgs > 8)
666 return 3951;
667
668 /* Allocate the buffer for the clean workstation list */
669 pList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength * sizeof(WCHAR));
670 if (pList == NULL)
671 return ERROR_NOT_ENOUGH_MEMORY;
672
673 /* Build the clean workstation list */
674 isLastSep = FALSE;
675 isSep = FALSE;
676 nRawLength = wcslen(pRaw);
677 for (i = 0, j = 0; i < nRawLength; i++)
678 {
679 c = pRaw[i];
680 if (c == L',' || c == L';')
681 isSep = TRUE;
682
683 if (isSep == TRUE)
684 {
685 if ((isLastSep == FALSE) && (i != 0) && (i != nRawLength - 1))
686 {
687 pList[j] = L',';
688 j++;
689 }
690 }
691 else
692 {
693 pList[j] = c;
694 j++;
695
696 if (isLastSep == TRUE || (isLastSep == FALSE && i == 0))
697 nArgs++;
698 }
699
700 isLastSep = isSep;
701 isSep = FALSE;
702 }
703
704 *pWorkstationsList = pList;
705
706 return NERR_Success;
707 }
708
709
710 static
711 BOOL
712 ReadNumber(
713 PWSTR *s,
714 PWORD pwValue)
715 {
716 if (!iswdigit(**s))
717 return FALSE;
718
719 while (iswdigit(**s))
720 {
721 *pwValue = *pwValue * 10 + **s - L'0';
722 (*s)++;
723 }
724
725 return TRUE;
726 }
727
728
729 static
730 BOOL
731 ReadSeparator(
732 PWSTR *s)
733 {
734 if (**s == L'/' || **s == L'.')
735 {
736 (*s)++;
737 return TRUE;
738 }
739
740 return FALSE;
741 }
742
743
744 static
745 BOOL
746 ParseDate(
747 PWSTR s,
748 PULONG pSeconds)
749 {
750 SYSTEMTIME SystemTime = {0};
751 FILETIME LocalFileTime, FileTime;
752 LARGE_INTEGER Time;
753 INT nDateFormat = 0;
754 PWSTR p = s;
755
756 if (!*s)
757 return FALSE;
758
759 GetLocaleInfoW(LOCALE_USER_DEFAULT,
760 LOCALE_IDATE,
761 (PWSTR)&nDateFormat,
762 sizeof(INT));
763
764 switch (nDateFormat)
765 {
766 case 0: /* mmddyy */
767 default:
768 if (!ReadNumber(&p, &SystemTime.wMonth))
769 return FALSE;
770 if (!ReadSeparator(&p))
771 return FALSE;
772 if (!ReadNumber(&p, &SystemTime.wDay))
773 return FALSE;
774 if (!ReadSeparator(&p))
775 return FALSE;
776 if (!ReadNumber(&p, &SystemTime.wYear))
777 return FALSE;
778 break;
779
780 case 1: /* ddmmyy */
781 if (!ReadNumber(&p, &SystemTime.wDay))
782 return FALSE;
783 if (!ReadSeparator(&p))
784 return FALSE;
785 if (!ReadNumber(&p, &SystemTime.wMonth))
786 return FALSE;
787 if (!ReadSeparator(&p))
788 return FALSE;
789 if (!ReadNumber(&p, &SystemTime.wYear))
790 return FALSE;
791 break;
792
793 case 2: /* yymmdd */
794 if (!ReadNumber(&p, &SystemTime.wYear))
795 return FALSE;
796 if (!ReadSeparator(&p))
797 return FALSE;
798 if (!ReadNumber(&p, &SystemTime.wMonth))
799 return FALSE;
800 if (!ReadSeparator(&p))
801 return FALSE;
802 if (!ReadNumber(&p, &SystemTime.wDay))
803 return FALSE;
804 break;
805 }
806
807 /* if only entered two digits: */
808 /* assume 2000's if value less than 80 */
809 /* assume 1900's if value greater or equal 80 */
810 if (SystemTime.wYear <= 99)
811 {
812 if (SystemTime.wYear >= 80)
813 SystemTime.wYear += 1900;
814 else
815 SystemTime.wYear += 2000;
816 }
817
818 if (!SystemTimeToFileTime(&SystemTime, &LocalFileTime))
819 return FALSE;
820
821 if (!LocalFileTimeToFileTime(&LocalFileTime, &FileTime))
822 return FALSE;
823
824 Time.u.LowPart = FileTime.dwLowDateTime;
825 Time.u.HighPart = FileTime.dwHighDateTime;
826
827 if (!RtlTimeToSecondsSince1970(&Time, pSeconds))
828 return FALSE;
829
830 return TRUE;
831 }
832
833
834 static
835 BOOL
836 ParseHour(
837 PWSTR pszString,
838 PWSTR *AmPmArray,
839 PLONG plHour)
840 {
841 PWCHAR pChar;
842 LONG lHour = 0;
843
844 if (!iswdigit(pszString[0]))
845 return FALSE;
846
847 pChar = pszString;
848 while (iswdigit(*pChar))
849 {
850 lHour = lHour * 10 + *pChar - L'0';
851 pChar++;
852 }
853
854 if (lHour > 24)
855 return FALSE;
856
857 if (lHour == 24)
858 lHour = 0;
859
860 if ((*pChar != UNICODE_NULL) &&
861 (lHour >= 1) &&
862 (lHour <= 12))
863 {
864 if ((_wcsicmp(pChar, AmPmArray[0]) == 0) ||
865 (_wcsicmp(pChar, AmPmArray[1]) == 0))
866 {
867 if (lHour == 12)
868 lHour = 0;
869 }
870 else if ((_wcsicmp(pChar, AmPmArray[2]) == 0) ||
871 (_wcsicmp(pChar, AmPmArray[3]) == 0))
872 {
873 if (lHour != 12)
874 lHour += 12;
875 }
876 else
877 {
878 return FALSE;
879 }
880 }
881
882 *plHour = lHour;
883
884 return TRUE;
885 }
886
887
888 static
889 BOOL
890 ParseDay(
891 PWSTR pszString,
892 PWSTR *ShortDays,
893 PWSTR *LongDays,
894 PDWORD pdwDay)
895 {
896 DWORD i;
897
898 for (i = 0; i < 7; i++)
899 {
900 if (_wcsicmp(pszString, ShortDays[i]) == 0 ||
901 _wcsicmp(pszString, LongDays[i]) == 0)
902 {
903 *pdwDay = i;
904 return TRUE;
905 }
906 }
907
908 return FALSE;
909 }
910
911
912 static
913 DWORD
914 ParseLogonHours(
915 PWSTR pszParams,
916 PBYTE *ppLogonBitmap,
917 PDWORD pdwUnitsPerWeek)
918 {
919 TIME_ZONE_INFORMATION TimeZoneInformation;
920 PBYTE pLogonBitmap = NULL;
921 DWORD dwError = ERROR_SUCCESS;
922 WCHAR szBuffer[32];
923 PWSTR ptr1, ptr2;
924 WCHAR prevSep, nextSep;
925 DWORD dwStartDay, dwEndDay, i, j;
926 LONG lStartHour, lEndHour, lBias;
927 BYTE DayBitmap;
928 BYTE HourBitmap[6];
929 LPWSTR ShortDays[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
930 LPWSTR LongDays[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
931 LPWSTR AmPmArray[4] = {NULL, NULL, NULL, NULL};
932
933 GetTimeZoneInformation(&TimeZoneInformation);
934 lBias = TimeZoneInformation.Bias / 60;
935
936 pLogonBitmap = HeapAlloc(GetProcessHeap(),
937 HEAP_ZERO_MEMORY,
938 UNITS_PER_WEEK / 8);
939 if (pLogonBitmap == NULL)
940 return ERROR_OUTOFMEMORY;
941
942 if (*pszParams == UNICODE_NULL)
943 {
944 goto done;
945 }
946
947 if (wcsicmp(pszParams, L"all") == 0)
948 {
949 FillMemory(pLogonBitmap, UNITS_PER_WEEK / 8, 0xFF);
950 goto done;
951 }
952
953 for (i = 0; i < 7; i++)
954 {
955 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
956 FORMAT_MESSAGE_FROM_HMODULE |
957 FORMAT_MESSAGE_IGNORE_INSERTS,
958 hModuleNetMsg,
959 4314 + i,
960 LANG_USER_DEFAULT,
961 (LPWSTR)&ShortDays[i],
962 0,
963 NULL);
964
965 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
966 FORMAT_MESSAGE_FROM_HMODULE |
967 FORMAT_MESSAGE_IGNORE_INSERTS,
968 hModuleNetMsg,
969 4307 + i,
970 LANG_USER_DEFAULT,
971 (LPWSTR)&LongDays[i],
972 0,
973 NULL);
974 }
975
976 for (i = 0; i < 4; i++)
977 {
978 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
979 FORMAT_MESSAGE_FROM_HMODULE |
980 FORMAT_MESSAGE_IGNORE_INSERTS,
981 hModuleNetMsg,
982 4322 + i,
983 LANG_USER_DEFAULT,
984 (LPWSTR)&AmPmArray[i],
985 0,
986 NULL);
987 }
988
989 ZeroMemory(&DayBitmap, sizeof(DayBitmap));
990 ZeroMemory(HourBitmap, sizeof(HourBitmap));
991
992 ZeroMemory(szBuffer, sizeof(szBuffer));
993 ptr1 = pszParams;
994 ptr2 = szBuffer;
995 prevSep = UNICODE_NULL;
996 nextSep = UNICODE_NULL;
997 for (;;)
998 {
999 if (*ptr1 != L'-' && *ptr1 != L',' && *ptr1 != L';' && *ptr1 != UNICODE_NULL)
1000 {
1001 *ptr2 = *ptr1;
1002 ptr2++;
1003 }
1004 else
1005 {
1006 prevSep = nextSep;
1007 nextSep = *ptr1;
1008
1009 if (prevSep != L'-')
1010 {
1011 /* Set first value */
1012 if (iswdigit(szBuffer[0]))
1013 {
1014 /* Parse hour */
1015 if (!ParseHour(szBuffer, AmPmArray, &lStartHour))
1016 {
1017 dwError = 3769;
1018 break;
1019 }
1020
1021 /* Convert from local timezone to GMT */
1022 lStartHour += lBias;
1023 if (lStartHour < 0)
1024 lStartHour += UNITS_PER_WEEK;
1025 else if (lStartHour > UNITS_PER_WEEK)
1026 lStartHour -= UNITS_PER_WEEK;
1027
1028 SetBitValue(HourBitmap, (DWORD)lStartHour);
1029 }
1030 else
1031 {
1032 /* Parse day */
1033 if (!ParseDay(szBuffer, ShortDays, LongDays, &dwStartDay))
1034 {
1035 dwError = 3768;
1036 break;
1037 }
1038
1039 SetBitValue(&DayBitmap, dwStartDay);
1040 }
1041 }
1042 else
1043 {
1044 /* Set second value */
1045 if (iswdigit(szBuffer[0]))
1046 {
1047 /* Parse hour */
1048 if (!ParseHour(szBuffer, AmPmArray, &lEndHour))
1049 {
1050 dwError = 3769;
1051 break;
1052 }
1053
1054 if (lEndHour < lStartHour)
1055 lEndHour += HOURS_PER_DAY;
1056 else if (lEndHour == lStartHour)
1057 lEndHour = lStartHour + HOURS_PER_DAY;
1058
1059 /* Convert from local timezone to GMT */
1060 lEndHour += lBias;
1061 if (lEndHour < 0)
1062 lEndHour += UNITS_PER_WEEK;
1063 else if (lEndHour > UNITS_PER_WEEK)
1064 lEndHour -= UNITS_PER_WEEK;
1065
1066 for (i = (DWORD)lStartHour; i < (DWORD)lEndHour; i++)
1067 SetBitValue(HourBitmap, i);
1068 }
1069 else
1070 {
1071 /* Parse day */
1072 if (!ParseDay(szBuffer, ShortDays, LongDays, &dwEndDay))
1073 {
1074 dwError = 3768;
1075 break;
1076 }
1077
1078 if (dwEndDay <= dwStartDay)
1079 dwEndDay += DAYS_PER_WEEK;
1080
1081 for (i = dwStartDay; i <= dwEndDay; i++)
1082 SetBitValue(&DayBitmap, i % DAYS_PER_WEEK);
1083 }
1084 }
1085
1086 if (*ptr1 == L';' || *ptr1 == UNICODE_NULL)
1087 {
1088 /* Fill the logon hour bitmap */
1089 for (i = 0; i < DAYS_PER_WEEK; i++)
1090 {
1091 if (GetBitValue(&DayBitmap, i))
1092 {
1093 for (j = 0; j < 48; j++)
1094 {
1095 if (GetBitValue(HourBitmap, j))
1096 SetBitValue(pLogonBitmap, ((i * HOURS_PER_DAY) + j) % UNITS_PER_WEEK);
1097 }
1098 }
1099 }
1100
1101 /* Reset the Bitmaps */
1102 ZeroMemory(&DayBitmap, sizeof(DayBitmap));
1103 ZeroMemory(HourBitmap, sizeof(HourBitmap));
1104 }
1105
1106 if (*ptr1 == UNICODE_NULL)
1107 break;
1108
1109 ZeroMemory(szBuffer, sizeof(szBuffer));
1110 ptr2 = szBuffer;
1111 }
1112
1113 ptr1++;
1114 }
1115
1116 done:
1117 for (i = 0; i < 7; i++)
1118 {
1119 LocalFree(ShortDays[i]);
1120 LocalFree(LongDays[i]);
1121 }
1122
1123 for (i = 0; i < 4; i++)
1124 {
1125 LocalFree(AmPmArray[i]);
1126 }
1127
1128 if (dwError == ERROR_SUCCESS)
1129 {
1130 *ppLogonBitmap = pLogonBitmap;
1131 *pdwUnitsPerWeek = UNITS_PER_WEEK;
1132 }
1133 else
1134 {
1135 if (pLogonBitmap != NULL)
1136 HeapFree(GetProcessHeap(), 0, pLogonBitmap);
1137 *ppLogonBitmap = NULL;
1138 *pdwUnitsPerWeek = 0;
1139 }
1140
1141 return dwError;
1142 }
1143
1144
1145 INT
1146 cmdUser(
1147 INT argc,
1148 WCHAR **argv)
1149 {
1150 INT i, j;
1151 INT result = 0;
1152 BOOL bAdd = FALSE;
1153 BOOL bDelete = FALSE;
1154 #if 0
1155 BOOL bDomain = FALSE;
1156 #endif
1157 BOOL bRandomPassword = FALSE;
1158 LPWSTR lpUserName = NULL;
1159 LPWSTR lpPassword = NULL;
1160 PUSER_INFO_4 pUserInfo = NULL;
1161 USER_INFO_4 UserInfo;
1162 LPWSTR pWorkstations = NULL;
1163 LPWSTR p;
1164 LPWSTR endptr;
1165 DWORD value;
1166 BOOL bPasswordAllocated = FALSE;
1167 PBYTE pLogonHours = NULL;
1168 DWORD dwUnitsPerWeek;
1169 NET_API_STATUS Status;
1170
1171 i = 2;
1172 if ((i < argc) && (argv[i][0] != L'/'))
1173 {
1174 lpUserName = argv[i];
1175 // ConPrintf(StdOut, L"User: %s\n", lpUserName);
1176 i++;
1177 }
1178
1179 if ((i < argc) && (argv[i][0] != L'/'))
1180 {
1181 lpPassword = argv[i];
1182 // ConPrintf(StdOut, L"Password: %s\n", lpPassword);
1183 i++;
1184 }
1185
1186 for (j = i; j < argc; j++)
1187 {
1188 if (_wcsicmp(argv[j], L"/help") == 0)
1189 {
1190 PrintNetMessage(MSG_USER_HELP);
1191 return 0;
1192 }
1193 else if (_wcsicmp(argv[j], L"/add") == 0)
1194 {
1195 bAdd = TRUE;
1196 }
1197 else if (_wcsicmp(argv[j], L"/delete") == 0)
1198 {
1199 bDelete = TRUE;
1200 }
1201 else if (_wcsicmp(argv[j], L"/domain") == 0)
1202 {
1203 ConPuts(StdErr, L"The /DOMAIN option is not supported yet.\n");
1204 #if 0
1205 bDomain = TRUE;
1206 #endif
1207 }
1208 else if (_wcsicmp(argv[j], L"/random") == 0)
1209 {
1210 bRandomPassword = TRUE;
1211 GenerateRandomPassword(&lpPassword,
1212 &bPasswordAllocated);
1213 }
1214 }
1215
1216 if (lpUserName == NULL && lpPassword == NULL)
1217 {
1218 Status = EnumerateUsers();
1219 ConPrintf(StdOut, L"Status: %lu\n", Status);
1220 return 0;
1221 }
1222 else if (lpUserName != NULL && lpPassword == NULL && argc == 3)
1223 {
1224 Status = DisplayUser(lpUserName);
1225 ConPrintf(StdOut, L"Status: %lu\n", Status);
1226 return 0;
1227 }
1228
1229 if (bAdd && bDelete)
1230 {
1231 result = 1;
1232 goto done;
1233 }
1234
1235 /* Interactive password input */
1236 if (lpPassword != NULL && wcscmp(lpPassword, L"*") == 0)
1237 {
1238 ReadPassword(&lpPassword,
1239 &bPasswordAllocated);
1240 }
1241
1242 if (!bAdd && !bDelete)
1243 {
1244 /* Modify the user */
1245 Status = NetUserGetInfo(NULL,
1246 lpUserName,
1247 4,
1248 (LPBYTE*)&pUserInfo);
1249 if (Status != NERR_Success)
1250 {
1251 ConPrintf(StdOut, L"Status: %lu\n", Status);
1252 result = 1;
1253 goto done;
1254 }
1255 }
1256 else if (bAdd && !bDelete)
1257 {
1258 /* Add the user */
1259 ZeroMemory(&UserInfo, sizeof(USER_INFO_4));
1260
1261 UserInfo.usri4_name = lpUserName;
1262 UserInfo.usri4_password = lpPassword;
1263 UserInfo.usri4_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT;
1264 UserInfo.usri4_acct_expires = TIMEQ_FOREVER;
1265 UserInfo.usri4_primary_group_id = DOMAIN_GROUP_RID_USERS;
1266
1267 pUserInfo = &UserInfo;
1268 }
1269
1270 for (j = i; j < argc; j++)
1271 {
1272 if (_wcsnicmp(argv[j], L"/active:", 8) == 0)
1273 {
1274 p = &argv[i][8];
1275 if (_wcsicmp(p, L"yes") == 0)
1276 {
1277 pUserInfo->usri4_flags &= ~UF_ACCOUNTDISABLE;
1278 }
1279 else if (_wcsicmp(p, L"no") == 0)
1280 {
1281 pUserInfo->usri4_flags |= UF_ACCOUNTDISABLE;
1282 }
1283 else
1284 {
1285 PrintMessageStringV(3952, L"/ACTIVE");
1286 result = 1;
1287 goto done;
1288 }
1289 }
1290 else if (_wcsnicmp(argv[j], L"/comment:", 9) == 0)
1291 {
1292 pUserInfo->usri4_comment = &argv[j][9];
1293 }
1294 else if (_wcsnicmp(argv[j], L"/countrycode:", 13) == 0)
1295 {
1296 p = &argv[i][13];
1297 value = wcstoul(p, &endptr, 10);
1298 if (*endptr != 0)
1299 {
1300 PrintMessageStringV(3952, L"/COUNTRYCODE");
1301 result = 1;
1302 goto done;
1303 }
1304
1305 /* Verify the country code */
1306 if (GetCountryFromCountryCode(value, 0, NULL))
1307 pUserInfo->usri4_country_code = value;
1308 }
1309 else if (_wcsnicmp(argv[j], L"/expires:", 9) == 0)
1310 {
1311 p = &argv[i][9];
1312 if (_wcsicmp(p, L"never") == 0)
1313 {
1314 pUserInfo->usri4_acct_expires = TIMEQ_FOREVER;
1315 }
1316 else if (!ParseDate(p, &pUserInfo->usri4_acct_expires))
1317 {
1318 PrintMessageStringV(3952, L"/EXPIRES");
1319 result = 1;
1320 goto done;
1321 }
1322 }
1323 else if (_wcsnicmp(argv[j], L"/fullname:", 10) == 0)
1324 {
1325 pUserInfo->usri4_full_name = &argv[j][10];
1326 }
1327 else if (_wcsnicmp(argv[j], L"/homedir:", 9) == 0)
1328 {
1329 pUserInfo->usri4_home_dir = &argv[j][9];
1330 }
1331 else if (_wcsnicmp(argv[j], L"/passwordchg:", 13) == 0)
1332 {
1333 p = &argv[i][13];
1334 if (_wcsicmp(p, L"yes") == 0)
1335 {
1336 pUserInfo->usri4_flags &= ~UF_PASSWD_CANT_CHANGE;
1337 }
1338 else if (_wcsicmp(p, L"no") == 0)
1339 {
1340 pUserInfo->usri4_flags |= UF_PASSWD_CANT_CHANGE;
1341 }
1342 else
1343 {
1344 PrintMessageStringV(3952, L"/PASSWORDCHG");
1345 result = 1;
1346 goto done;
1347 }
1348 }
1349 else if (_wcsnicmp(argv[j], L"/passwordreq:", 13) == 0)
1350 {
1351 p = &argv[i][13];
1352 if (_wcsicmp(p, L"yes") == 0)
1353 {
1354 pUserInfo->usri4_flags &= ~UF_PASSWD_NOTREQD;
1355 }
1356 else if (_wcsicmp(p, L"no") == 0)
1357 {
1358 pUserInfo->usri4_flags |= UF_PASSWD_NOTREQD;
1359 }
1360 else
1361 {
1362 PrintMessageStringV(3952, L"/PASSWORDREQ");
1363 result = 1;
1364 goto done;
1365 }
1366 }
1367 else if (_wcsnicmp(argv[j], L"/profilepath:", 13) == 0)
1368 {
1369 pUserInfo->usri4_profile = &argv[j][13];
1370 }
1371 else if (_wcsnicmp(argv[j], L"/scriptpath:", 12) == 0)
1372 {
1373 pUserInfo->usri4_script_path = &argv[j][12];
1374 }
1375 else if (_wcsnicmp(argv[j], L"/times:", 7) == 0)
1376 {
1377 Status = ParseLogonHours(&argv[j][7],
1378 &pLogonHours,
1379 &dwUnitsPerWeek);
1380 if (Status == ERROR_SUCCESS)
1381 {
1382 pUserInfo->usri4_logon_hours = pLogonHours;
1383 pUserInfo->usri4_units_per_week = dwUnitsPerWeek;
1384 }
1385 else
1386 {
1387 PrintMessageString(Status);
1388 goto done;
1389 }
1390 }
1391 else if (_wcsnicmp(argv[j], L"/usercomment:", 13) == 0)
1392 {
1393 pUserInfo->usri4_usr_comment = &argv[j][13];
1394 }
1395 else if (_wcsnicmp(argv[j], L"/workstations:", 14) == 0)
1396 {
1397 p = &argv[i][14];
1398 if (wcscmp(p, L"*") == 0 || wcscmp(p, L"") == 0)
1399 {
1400 pUserInfo->usri4_workstations = NULL;
1401 }
1402 else
1403 {
1404 Status = BuildWorkstationsList(&pWorkstations, p);
1405 if (Status == NERR_Success)
1406 {
1407 pUserInfo->usri4_workstations = pWorkstations;
1408 }
1409 else
1410 {
1411 ConPrintf(StdOut, L"Status %lu\n\n", Status);
1412 result = 1;
1413 goto done;
1414 }
1415 }
1416 }
1417 }
1418
1419 if (!bAdd && !bDelete)
1420 {
1421 /* Modify the user */
1422 Status = NetUserSetInfo(NULL,
1423 lpUserName,
1424 4,
1425 (LPBYTE)pUserInfo,
1426 NULL);
1427 ConPrintf(StdOut, L"Status: %lu\n", Status);
1428 }
1429 else if (bAdd && !bDelete)
1430 {
1431 /* Add the user */
1432 Status = NetUserAdd(NULL,
1433 4,
1434 (LPBYTE)pUserInfo,
1435 NULL);
1436 ConPrintf(StdOut, L"Status: %lu\n", Status);
1437 }
1438 else if (!bAdd && bDelete)
1439 {
1440 /* Delete the user */
1441 Status = NetUserDel(NULL,
1442 lpUserName);
1443 ConPrintf(StdOut, L"Status: %lu\n", Status);
1444 }
1445
1446 if (Status == NERR_Success &&
1447 lpPassword != NULL &&
1448 bRandomPassword == TRUE)
1449 {
1450 PrintMessageStringV(3968, lpUserName, lpPassword);
1451 }
1452
1453 done:
1454 if (pLogonHours != NULL)
1455 HeapFree(GetProcessHeap(), 0, pLogonHours);
1456
1457 if (pWorkstations != NULL)
1458 HeapFree(GetProcessHeap(), 0, pWorkstations);
1459
1460 if ((bPasswordAllocated == TRUE) && (lpPassword != NULL))
1461 HeapFree(GetProcessHeap(), 0, lpPassword);
1462
1463 if (!bAdd && !bDelete && pUserInfo != NULL)
1464 NetApiBufferFree(pUserInfo);
1465
1466 if (result != 0)
1467 {
1468 PrintMessageString(4381);
1469 ConPuts(StdOut, L"\n");
1470 PrintNetMessage(MSG_USER_SYNTAX);
1471 }
1472
1473 return result;
1474 }
1475
1476 /* EOF */