[NET]
[reactos.git] / reactos / base / applications / network / net / cmdUser.c
index 9f9dcb2..3525ccb 100644 (file)
@@ -1,10 +1,11 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS net command
- * FILE:
+ * FILE:            base/applications/network/net/cmdUser.c
  * PURPOSE:
  *
  * PROGRAMMERS:     Eric Kohl
+ *                  Curtis Wilson
  */
 
 #include "net.h"
@@ -37,37 +38,42 @@ EnumerateUsers(VOID)
     if (Status != NERR_Success)
         return Status;
 
-    PrintToConsole(L"\nUser accounts for \\\\%s\n\n", pServer->sv100_name);
+    PrintToConsole(L"\n");
+    PrintResourceString(IDS_USER_ACCOUNTS, pServer->sv100_name);
+    PrintToConsole(L"\n\n");
+    PrintPadding(L'-', 79);
+    PrintToConsole(L"\n");
 
     NetApiBufferFree(pServer);
 
-    PrintToConsole(L"------------------------------------------\n");
-
-    Status = NetUserEnum(NULL,
-                         0,
-                         0,
-                         (LPBYTE*)&pBuffer,
-                         MAX_PREFERRED_LENGTH,
-                         &dwRead,
-                         &dwTotal,
-                         &ResumeHandle);
-    if (Status != NERR_Success)
-        return Status;
-
-    qsort(pBuffer,
-          dwRead,
-          sizeof(PUSER_INFO_0),
-          CompareInfo);
-
-//    printf("dwRead: %lu  dwTotal: %lu\n", dwRead, dwTotal);
-    for (i = 0; i < dwRead; i++)
+    do
     {
-//        printf("%p\n", pBuffer[i].lgrpi0_name);
-         if (pBuffer[i].usri0_name)
-            PrintToConsole(L"%s\n", pBuffer[i].usri0_name);
-    }
+        Status = NetUserEnum(NULL,
+                             0,
+                             0,
+                             (LPBYTE*)&pBuffer,
+                             MAX_PREFERRED_LENGTH,
+                             &dwRead,
+                             &dwTotal,
+                             &ResumeHandle);
+        if ((Status != NERR_Success) && (Status != ERROR_MORE_DATA))
+            return Status;
+
+        qsort(pBuffer,
+              dwRead,
+              sizeof(PUSER_INFO_0),
+              CompareInfo);
+
+        for (i = 0; i < dwRead; i++)
+        {
+            if (pBuffer[i].usri0_name)
+                PrintToConsole(L"%s\n", pBuffer[i].usri0_name);
+        }
 
-    NetApiBufferFree(pBuffer);
+        NetApiBufferFree(pBuffer);
+        pBuffer = NULL;
+    }
+    while (Status == ERROR_MORE_DATA);
 
     return NERR_Success;
 }
@@ -103,7 +109,7 @@ PrintDateTime(DWORD dwSeconds)
                    TimeBuffer,
                    80);
 
-    PrintToConsole(L"%s %s\n", DateBuffer, TimeBuffer);
+    PrintToConsole(L"%s %s", DateBuffer, TimeBuffer);
 }
 
 
@@ -130,7 +136,13 @@ DisplayUser(LPWSTR lpUserName)
 {
     PUSER_MODALS_INFO_0 pUserModals = NULL;
     PUSER_INFO_4 pUserInfo = NULL;
+    PLOCALGROUP_USERS_INFO_0 pLocalGroupInfo = NULL;
+    PGROUP_USERS_INFO_0 pGroupInfo = NULL;
+    DWORD dwLocalGroupRead, dwLocalGroupTotal;
+    DWORD dwGroupRead, dwGroupTotal;
     DWORD dwLastSet;
+    DWORD i;
+    INT nPaddedLength = 29;
     NET_API_STATUS Status;
 
     /* Modify the user */
@@ -147,53 +159,146 @@ DisplayUser(LPWSTR lpUserName)
     if (Status != NERR_Success)
         goto done;
 
-    PrintToConsole(L"User name                    %s\n", pUserInfo->usri4_name);
-    PrintToConsole(L"Full name                    %s\n", pUserInfo->usri4_full_name);
-    PrintToConsole(L"Comment                      %s\n", pUserInfo->usri4_comment);
-    PrintToConsole(L"User comment                 %s\n", pUserInfo->usri4_usr_comment);
-    PrintToConsole(L"Country code                 %03ld ()\n", pUserInfo->usri4_country_code);
-    PrintToConsole(L"Account active               %s\n", (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)? L"No" : ((pUserInfo->usri4_flags & UF_LOCKOUT) ? L"Locked" : L"Yes"));
-    PrintToConsole(L"Account expires              ");
+    Status = NetUserGetLocalGroups(NULL,
+                                   lpUserName,
+                                   0,
+                                   0,
+                                   (LPBYTE*)&pLocalGroupInfo,
+                                   MAX_PREFERRED_LENGTH,
+                                   &dwLocalGroupRead,
+                                   &dwLocalGroupTotal);
+    if (Status != NERR_Success)
+        goto done;
+
+    Status = NetUserGetGroups(NULL,
+                              lpUserName,
+                              0,
+                              (LPBYTE*)&pGroupInfo,
+                              MAX_PREFERRED_LENGTH,
+                              &dwGroupRead,
+                              &dwGroupTotal);
+    if (Status != NERR_Success)
+        goto done;
+
+    PrintPaddedResourceString(IDS_USER_NAME, nPaddedLength);
+    PrintToConsole(L"%s\n", pUserInfo->usri4_name);
+
+    PrintPaddedResourceString(IDS_USER_FULL_NAME, nPaddedLength);
+    PrintToConsole(L"%s\n", pUserInfo->usri4_full_name);
+
+    PrintPaddedResourceString(IDS_USER_COMMENT, nPaddedLength);
+    PrintToConsole(L"%s\n", pUserInfo->usri4_comment);
+
+    PrintPaddedResourceString(IDS_USER_USER_COMMENT, nPaddedLength);
+    PrintToConsole(L"%s\n", pUserInfo->usri4_usr_comment);
+
+    PrintPaddedResourceString(IDS_USER_COUNTRY_CODE, nPaddedLength);
+    PrintToConsole(L"%03ld ()\n", pUserInfo->usri4_country_code);
+
+    PrintPaddedResourceString(IDS_USER_ACCOUNT_ACTIVE, nPaddedLength);
+    if (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)
+        PrintResourceString(IDS_GENERIC_NO);
+    else if (pUserInfo->usri4_flags & UF_LOCKOUT)
+        PrintResourceString(IDS_GENERIC_LOCKED);
+    else
+        PrintResourceString(IDS_GENERIC_YES);
+    PrintToConsole(L"\n");
+
+    PrintPaddedResourceString(IDS_USER_ACCOUNT_EXPIRES, nPaddedLength);
     if (pUserInfo->usri4_acct_expires == TIMEQ_FOREVER)
-        PrintToConsole(L"Never\n");
+        PrintResourceString(IDS_GENERIC_NEVER);
     else
         PrintDateTime(pUserInfo->usri4_acct_expires);
+    PrintToConsole(L"\n\n");
 
-    PrintToConsole(L"\n");
-
-    PrintToConsole(L"Password expires             ");
+    PrintPaddedResourceString(IDS_USER_PW_LAST_SET, nPaddedLength);
     dwLastSet = GetTimeInSeconds() - pUserInfo->usri4_password_age;
     PrintDateTime(dwLastSet);
 
-    PrintToConsole(L"Password expires             ");
+    PrintPaddedResourceString(IDS_USER_PW_EXPIRES, nPaddedLength);
     if ((pUserInfo->usri4_flags & UF_DONT_EXPIRE_PASSWD) || pUserModals->usrmod0_max_passwd_age == TIMEQ_FOREVER)
-        PrintToConsole(L"Never\n");
+        PrintResourceString(IDS_GENERIC_NEVER);
     else
         PrintDateTime(dwLastSet + pUserModals->usrmod0_max_passwd_age);
+    PrintToConsole(L"\n");
 
-    PrintToConsole(L"Password changeable          ");
+    PrintPaddedResourceString(IDS_USER_PW_CHANGEABLE, nPaddedLength);
     PrintDateTime(dwLastSet + pUserModals->usrmod0_min_passwd_age);
 
-    PrintToConsole(L"Password required            %s\n", (pUserInfo->usri4_flags & UF_PASSWD_NOTREQD) ? L"No" : L"Yes");
-    PrintToConsole(L"User may change password     %s\n", (pUserInfo->usri4_flags & UF_PASSWD_CANT_CHANGE) ? L"No" : L"Yes");
+    PrintPaddedResourceString(IDS_USER_PW_REQUIRED, nPaddedLength);
+    PrintResourceString((pUserInfo->usri4_flags & UF_PASSWD_NOTREQD) ? IDS_GENERIC_NO : IDS_GENERIC_YES);
+    PrintToConsole(L"\n");
+
+    PrintPaddedResourceString(IDS_USER_CHANGE_PW, nPaddedLength);
+    PrintResourceString((pUserInfo->usri4_flags & UF_PASSWD_CANT_CHANGE) ? IDS_GENERIC_NO : IDS_GENERIC_YES);
+    PrintToConsole(L"\n\n");
 
+    PrintPaddedResourceString(IDS_USER_WORKSTATIONS, nPaddedLength);
+    if (pUserInfo->usri4_workstations == NULL || wcslen(pUserInfo->usri4_workstations) == 0)
+        PrintResourceString(IDS_GENERIC_ALL);
+    else
+        PrintToConsole(L"%s", pUserInfo->usri4_workstations);
     PrintToConsole(L"\n");
-    PrintToConsole(L"Workstation allowed          %s\n", pUserInfo->usri4_workstations);
-    PrintToConsole(L"Logon script                 %s\n", pUserInfo->usri4_script_path);
-    PrintToConsole(L"User profile                 %s\n", pUserInfo->usri4_profile);
-    PrintToConsole(L"Home directory               %s\n", pUserInfo->usri4_home_dir);
-    PrintToConsole(L"Last logon                   ");
+
+    PrintPaddedResourceString(IDS_USER_LOGON_SCRIPT, nPaddedLength);
+    PrintToConsole(L"%s\n", pUserInfo->usri4_script_path);
+
+    PrintPaddedResourceString(IDS_USER_PROFILE, nPaddedLength);
+    PrintToConsole(L"%s\n", pUserInfo->usri4_profile);
+
+    PrintPaddedResourceString(IDS_USER_HOME_DIR, nPaddedLength);
+    PrintToConsole(L"%s\n", pUserInfo->usri4_home_dir);
+
+    PrintPaddedResourceString(IDS_USER_LAST_LOGON, nPaddedLength);
     if (pUserInfo->usri4_last_logon == 0)
-        PrintToConsole(L"Never\n");
+        PrintResourceString(IDS_GENERIC_NEVER);
     else
         PrintDateTime(pUserInfo->usri4_last_logon);
+    PrintToConsole(L"\n\n");
+
+    PrintPaddedResourceString(IDS_USER_LOGON_HOURS, nPaddedLength);
+    if (pUserInfo->usri4_logon_hours == NULL)
+        PrintResourceString(IDS_GENERIC_ALL);
+    PrintToConsole(L"\n\n");
+
     PrintToConsole(L"\n");
-    PrintToConsole(L"Logon hours allowed          \n");
-    PrintToConsole(L"\n");
-    PrintToConsole(L"Local group memberships      \n");
-    PrintToConsole(L"Global group memberships     \n");
+    PrintPaddedResourceString(IDS_USER_LOCAL_GROUPS, nPaddedLength);
+    if (dwLocalGroupTotal != 0 && pLocalGroupInfo != NULL)
+    {
+        for (i = 0; i < dwLocalGroupTotal; i++)
+        {
+            if (i != 0)
+                PrintPadding(L' ', nPaddedLength);
+            PrintToConsole(L"*%s\n", pLocalGroupInfo[i].lgrui0_name);
+        }
+    }
+    else
+    {
+        PrintToConsole(L"\n");
+    }
+
+    PrintPaddedResourceString(IDS_USER_GLOBAL_GROUPS, nPaddedLength);
+    if (dwGroupTotal != 0 && pGroupInfo != NULL)
+    {
+        for (i = 0; i < dwGroupTotal; i++)
+        {
+            if (i != 0)
+                PrintPadding(L' ', nPaddedLength);
+            PrintToConsole(L"*%s\n", pGroupInfo[i].grui0_name);
+        }
+    }
+    else
+    {
+        PrintToConsole(L"\n");
+    }
 
 done:
+    if (pGroupInfo != NULL)
+        NetApiBufferFree(pGroupInfo);
+
+    if (pLocalGroupInfo != NULL)
+        NetApiBufferFree(pLocalGroupInfo);
+
     if (pUserModals != NULL)
         NetApiBufferFree(pUserModals);
 
@@ -204,6 +309,53 @@ done:
 }
 
 
+static
+VOID
+ReadPassword(
+    LPWSTR *lpPassword,
+    LPBOOL lpAllocated)
+{
+    WCHAR szPassword1[PWLEN + 1];
+    WCHAR szPassword2[PWLEN + 1];
+    LPWSTR ptr;
+
+    *lpAllocated = FALSE;
+
+    while (TRUE)
+    {
+        PrintResourceString(IDS_USER_ENTER_PASSWORD1);
+        ReadFromConsole(szPassword1, PWLEN + 1, FALSE);
+        PrintToConsole(L"\n");
+
+        PrintResourceString(IDS_USER_ENTER_PASSWORD2);
+        ReadFromConsole(szPassword2, PWLEN + 1, FALSE);
+        PrintToConsole(L"\n");
+
+        if (wcslen(szPassword1) == wcslen(szPassword2) &&
+            wcscmp(szPassword1, szPassword2) == 0)
+        {
+            ptr = HeapAlloc(GetProcessHeap(),
+                            0,
+                            (wcslen(szPassword1) + 1) * sizeof(WCHAR));
+            if (ptr != NULL)
+            {
+                wcscpy(ptr, szPassword1);
+                *lpPassword = ptr;
+                *lpAllocated = TRUE;
+                return;
+            }
+        }
+        else
+        {
+            PrintToConsole(L"\n");
+            PrintResourceString(IDS_USER_NO_PASSWORD_MATCH);
+            PrintToConsole(L"\n");
+            *lpPassword = NULL;
+        }
+    }
+}
+
+
 INT
 cmdUser(
     INT argc,
@@ -221,6 +373,9 @@ cmdUser(
     PUSER_INFO_4 pUserInfo = NULL;
     USER_INFO_4 UserInfo;
     LPWSTR p;
+    LPWSTR endptr;
+    DWORD value;
+    BOOL bPasswordAllocated = FALSE;
     NET_API_STATUS Status;
 
     if (argc == 2)
@@ -240,14 +395,14 @@ cmdUser(
     if (argv[i][0] != L'/')
     {
         lpUserName = argv[i];
-        printf("User: %S\n", lpUserName);
+//        printf("User: %S\n", lpUserName);
         i++;
     }
 
     if (argv[i][0] != L'/')
     {
         lpPassword = argv[i];
-        printf("Password: %S\n", lpPassword);
+//        printf("Password: %S\n", lpPassword);
         i++;
     }
 
@@ -268,7 +423,7 @@ cmdUser(
         }
         else if (_wcsicmp(argv[j], L"/domain") == 0)
         {
-            printf("The /DOMAIN option is not supported yet!\n");
+            PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED, L"/DOMAIN");
 #if 0
             bDomain = TRUE;
 #endif
@@ -281,6 +436,13 @@ cmdUser(
         goto done;
     }
 
+    /* Interactive password input */
+    if (lpPassword != NULL && wcscmp(lpPassword, L"*") == 0)
+    {
+        ReadPassword(&lpPassword,
+                     &bPasswordAllocated);
+    }
+
     if (!bAdd && !bDelete)
     {
         /* Modify the user */
@@ -288,7 +450,12 @@ cmdUser(
                                 lpUserName,
                                 4,
                                 (LPBYTE*)&pUserInfo);
-        printf("Status: %lu\n", Status);
+        if (Status != NERR_Success)
+        {
+            printf("Status: %lu\n", Status);
+            result = 1;
+            goto done;
+        }
     }
     else if (bAdd && !bDelete)
     {
@@ -309,15 +476,15 @@ cmdUser(
             p = &argv[i][8];
             if (_wcsicmp(p, L"yes") == 0)
             {
-
+                pUserInfo->usri4_flags &= ~UF_ACCOUNTDISABLE;
             }
             else if (_wcsicmp(p, L"no") == 0)
             {
-
+                pUserInfo->usri4_flags |= UF_ACCOUNTDISABLE;
             }
             else
             {
-                PrintToConsole(L"You entered an invalid value for the /ACTIVE option.\n");
+                PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"/ACTIVE");
                 result = 1;
                 goto done;
             }
@@ -328,9 +495,31 @@ cmdUser(
         }
         else if (_wcsnicmp(argv[j], L"/countrycode:", 13) == 0)
         {
+            p = &argv[i][13];
+            value = wcstoul(p, &endptr, 10);
+            if (*endptr != 0)
+            {
+                PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"/COUNTRYCODE");
+                result = 1;
+                goto done;
+            }
+
+            /* FIXME: verify the country code */
+
+            pUserInfo->usri4_country_code = value;
         }
         else if (_wcsnicmp(argv[j], L"/expires:", 9) == 0)
         {
+            p = &argv[i][9];
+            if (_wcsicmp(p, L"never") == 0)
+            {
+                pUserInfo->usri4_acct_expires = TIMEQ_FOREVER;
+            }
+            else
+            {
+                /* FIXME: Parse the date */
+                PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED, L"/EXPIRES");
+            }
         }
         else if (_wcsnicmp(argv[j], L"/fullname:", 10) == 0)
         {
@@ -342,9 +531,39 @@ cmdUser(
         }
         else if (_wcsnicmp(argv[j], L"/passwordchg:", 13) == 0)
         {
+            p = &argv[i][13];
+            if (_wcsicmp(p, L"yes") == 0)
+            {
+                pUserInfo->usri4_flags &= ~UF_PASSWD_CANT_CHANGE;
+            }
+            else if (_wcsicmp(p, L"no") == 0)
+            {
+                pUserInfo->usri4_flags |= UF_PASSWD_CANT_CHANGE;
+            }
+            else
+            {
+                PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"/PASSWORDCHG");
+                result = 1;
+                goto done;
+            }
         }
         else if (_wcsnicmp(argv[j], L"/passwordreq:", 13) == 0)
         {
+            p = &argv[i][13];
+            if (_wcsicmp(p, L"yes") == 0)
+            {
+                pUserInfo->usri4_flags &= ~UF_PASSWD_NOTREQD;
+            }
+            else if (_wcsicmp(p, L"no") == 0)
+            {
+                pUserInfo->usri4_flags |= UF_PASSWD_NOTREQD;
+            }
+            else
+            {
+                PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"/PASSWORDREQ");
+                result = 1;
+                goto done;
+            }
         }
         else if (_wcsnicmp(argv[j], L"/profilepath:", 13) == 0)
         {
@@ -356,6 +575,8 @@ cmdUser(
         }
         else if (_wcsnicmp(argv[j], L"/times:", 7) == 0)
         {
+            /* FIXME */
+            PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED, L"/TIMES");
         }
         else if (_wcsnicmp(argv[j], L"/usercomment:", 13) == 0)
         {
@@ -363,6 +584,8 @@ cmdUser(
         }
         else if (_wcsnicmp(argv[j], L"/workstations:", 14) == 0)
         {
+            /* FIXME */
+            PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED, L"/WORKSTATIONS");
         }
     }
 
@@ -394,6 +617,9 @@ cmdUser(
     }
 
 done:
+    if (bPasswordAllocated == TRUE && lpPassword != NULL)
+        HeapFree(GetProcessHeap(), 0, lpPassword);
+
     if (!bAdd && !bDelete && pUserInfo != NULL)
         NetApiBufferFree(pUserInfo);