Skip to content

Commit b4969dc

Browse files
committed
[NET] Implement the undocumented /RANDOM option to generate random passwords
1 parent 4e45a91 commit b4969dc

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

base/applications/network/net/cmdUser.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "net.h"
1212

13+
static WCHAR szPasswordChars[] = L"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%_-+:";
14+
1315
static
1416
int
1517
CompareUserInfo(const void *a, const void *b)
@@ -356,6 +358,35 @@ ReadPassword(
356358
}
357359

358360

361+
static
362+
VOID
363+
GenerateRandomPassword(
364+
LPWSTR *lpPassword,
365+
LPBOOL lpAllocated)
366+
{
367+
LPWSTR pPassword = NULL;
368+
INT nCharsLen, i, nLength = 8;
369+
370+
srand(GetTickCount());
371+
372+
pPassword = HeapAlloc(GetProcessHeap(),
373+
HEAP_ZERO_MEMORY,
374+
(nLength + 1) * sizeof(WCHAR));
375+
if (pPassword == NULL)
376+
return;
377+
378+
nCharsLen = wcslen(szPasswordChars);
379+
380+
for (i = 0; i < nLength; i++)
381+
{
382+
pPassword[i] = szPasswordChars[rand() % nCharsLen];
383+
}
384+
385+
*lpPassword = pPassword;
386+
*lpAllocated = TRUE;
387+
}
388+
389+
359390
INT
360391
cmdUser(
361392
INT argc,
@@ -368,6 +399,7 @@ cmdUser(
368399
#if 0
369400
BOOL bDomain = FALSE;
370401
#endif
402+
BOOL bRandomPassword = FALSE;
371403
LPWSTR lpUserName = NULL;
372404
LPWSTR lpPassword = NULL;
373405
PUSER_INFO_4 pUserInfo = NULL;
@@ -428,6 +460,12 @@ cmdUser(
428460
bDomain = TRUE;
429461
#endif
430462
}
463+
else if (_wcsicmp(argv[j], L"/random") == 0)
464+
{
465+
bRandomPassword = TRUE;
466+
GenerateRandomPassword(&lpPassword,
467+
&bPasswordAllocated);
468+
}
431469
}
432470

433471
if (bAdd && bDelete)
@@ -616,6 +654,13 @@ cmdUser(
616654
ConPrintf(StdOut, L"Status: %lu\n", Status);
617655
}
618656

657+
if (Status == NERR_Success &&
658+
lpPassword != NULL &&
659+
bRandomPassword == TRUE)
660+
{
661+
ConPrintf(StdOut, L"The password for %s is: %s\n", lpUserName, lpPassword);
662+
}
663+
619664
done:
620665
if ((bPasswordAllocated != FALSE) && (lpPassword != NULL))
621666
HeapFree(GetProcessHeap(), 0, lpPassword);

0 commit comments

Comments
 (0)