[NET] Implement the undocumented /RANDOM option to generate random passwords
authorEric Kohl <eric.kohl@reactos.org>
Sat, 7 Jul 2018 20:06:08 +0000 (22:06 +0200)
committerEric Kohl <eric.kohl@reactos.org>
Mon, 9 Jul 2018 03:56:40 +0000 (05:56 +0200)
base/applications/network/net/cmdUser.c

index 3ee392b..7750d24 100644 (file)
@@ -10,6 +10,8 @@
 
 #include "net.h"
 
+static WCHAR szPasswordChars[] = L"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%_-+:";
+
 static
 int
 CompareUserInfo(const void *a, const void *b)
@@ -356,6 +358,35 @@ ReadPassword(
 }
 
 
+static
+VOID
+GenerateRandomPassword(
+    LPWSTR *lpPassword,
+    LPBOOL lpAllocated)
+{
+    LPWSTR pPassword = NULL;
+    INT nCharsLen, i, nLength = 8;
+
+    srand(GetTickCount());
+
+    pPassword = HeapAlloc(GetProcessHeap(),
+                          HEAP_ZERO_MEMORY,
+                          (nLength + 1) * sizeof(WCHAR));
+    if (pPassword == NULL)
+        return;
+
+    nCharsLen = wcslen(szPasswordChars);
+
+    for (i = 0; i < nLength; i++)
+    {
+        pPassword[i] = szPasswordChars[rand() % nCharsLen];
+    }
+
+    *lpPassword = pPassword;
+    *lpAllocated = TRUE;
+}
+
+
 INT
 cmdUser(
     INT argc,
@@ -368,6 +399,7 @@ cmdUser(
 #if 0
     BOOL bDomain = FALSE;
 #endif
+    BOOL bRandomPassword = FALSE;
     LPWSTR lpUserName = NULL;
     LPWSTR lpPassword = NULL;
     PUSER_INFO_4 pUserInfo = NULL;
@@ -428,6 +460,12 @@ cmdUser(
             bDomain = TRUE;
 #endif
         }
+        else if (_wcsicmp(argv[j], L"/random") == 0)
+        {
+            bRandomPassword = TRUE;
+            GenerateRandomPassword(&lpPassword,
+                                   &bPasswordAllocated);
+        }
     }
 
     if (bAdd && bDelete)
@@ -616,6 +654,13 @@ cmdUser(
         ConPrintf(StdOut, L"Status: %lu\n", Status);
     }
 
+    if (Status == NERR_Success &&
+        lpPassword != NULL &&
+        bRandomPassword == TRUE)
+    {
+        ConPrintf(StdOut, L"The password for %s is: %s\n", lpUserName, lpPassword);
+    }
+
 done:
     if ((bPasswordAllocated != FALSE) && (lpPassword != NULL))
         HeapFree(GetProcessHeap(), 0, lpPassword);