2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE: base/applications/network/net/cmdUser.c
7 * PROGRAMMERS: Eric Kohl
16 CompareUserInfo(const void *a
,
19 return _wcsicmp(((PUSER_INFO_0
)a
)->usri0_name
,
20 ((PUSER_INFO_0
)b
)->usri0_name
);
28 PUSER_INFO_0 pBuffer
= NULL
;
29 PSERVER_INFO_100 pServer
= NULL
;
30 DWORD dwRead
= 0, dwTotal
= 0;
32 DWORD_PTR ResumeHandle
= 0;
33 NET_API_STATUS Status
;
35 Status
= NetServerGetInfo(NULL
,
38 if (Status
!= NERR_Success
)
41 ConPuts(StdOut
, L
"\n");
42 ConResPrintf(StdOut
, IDS_USER_ACCOUNTS
, pServer
->sv100_name
);
43 ConPuts(StdOut
, L
"\n\n");
44 PrintPadding(L
'-', 79);
45 ConPuts(StdOut
, L
"\n");
47 NetApiBufferFree(pServer
);
51 Status
= NetUserEnum(NULL
,
59 if ((Status
!= NERR_Success
) && (Status
!= ERROR_MORE_DATA
))
67 for (i
= 0; i
< dwRead
; i
++)
69 if (pBuffer
[i
].usri0_name
)
70 ConPrintf(StdOut
, L
"%s\n", pBuffer
[i
].usri0_name
);
73 NetApiBufferFree(pBuffer
);
76 while (Status
== ERROR_MORE_DATA
);
84 PrintDateTime(DWORD dwSeconds
)
88 SYSTEMTIME SystemTime
;
92 RtlSecondsSince1970ToTime(dwSeconds
, &Time
);
93 FileTime
.dwLowDateTime
= Time
.u
.LowPart
;
94 FileTime
.dwHighDateTime
= Time
.u
.HighPart
;
95 FileTimeToLocalFileTime(&FileTime
, &FileTime
);
96 FileTimeToSystemTime(&FileTime
, &SystemTime
);
98 GetDateFormatW(LOCALE_USER_DEFAULT
,
105 GetTimeFormatW(LOCALE_USER_DEFAULT
,
112 ConPrintf(StdOut
, L
"%s %s", DateBuffer
, TimeBuffer
);
118 GetTimeInSeconds(VOID
)
124 GetSystemTimeAsFileTime(&FileTime
);
125 Time
.u
.LowPart
= FileTime
.dwLowDateTime
;
126 Time
.u
.HighPart
= FileTime
.dwHighDateTime
;
127 RtlTimeToSecondsSince1970(&Time
, &dwSeconds
);
135 DisplayUser(LPWSTR lpUserName
)
137 PUSER_MODALS_INFO_0 pUserModals
= NULL
;
138 PUSER_INFO_4 pUserInfo
= NULL
;
139 PLOCALGROUP_USERS_INFO_0 pLocalGroupInfo
= NULL
;
140 PGROUP_USERS_INFO_0 pGroupInfo
= NULL
;
141 DWORD dwLocalGroupRead
, dwLocalGroupTotal
;
142 DWORD dwGroupRead
, dwGroupTotal
;
145 INT nPaddedLength
= 29;
146 NET_API_STATUS Status
;
148 /* Modify the user */
149 Status
= NetUserGetInfo(NULL
,
152 (LPBYTE
*)&pUserInfo
);
153 if (Status
!= NERR_Success
)
156 Status
= NetUserModalsGet(NULL
,
158 (LPBYTE
*)&pUserModals
);
159 if (Status
!= NERR_Success
)
162 Status
= NetUserGetLocalGroups(NULL
,
166 (LPBYTE
*)&pLocalGroupInfo
,
167 MAX_PREFERRED_LENGTH
,
170 if (Status
!= NERR_Success
)
173 Status
= NetUserGetGroups(NULL
,
176 (LPBYTE
*)&pGroupInfo
,
177 MAX_PREFERRED_LENGTH
,
180 if (Status
!= NERR_Success
)
183 PrintPaddedResourceString(IDS_USER_NAME
, nPaddedLength
);
184 ConPrintf(StdOut
, L
"%s\n", pUserInfo
->usri4_name
);
186 PrintPaddedResourceString(IDS_USER_FULL_NAME
, nPaddedLength
);
187 ConPrintf(StdOut
, L
"%s\n", pUserInfo
->usri4_full_name
);
189 PrintPaddedResourceString(IDS_USER_COMMENT
, nPaddedLength
);
190 ConPrintf(StdOut
, L
"%s\n", pUserInfo
->usri4_comment
);
192 PrintPaddedResourceString(IDS_USER_USER_COMMENT
, nPaddedLength
);
193 ConPrintf(StdOut
, L
"%s\n", pUserInfo
->usri4_usr_comment
);
195 PrintPaddedResourceString(IDS_USER_COUNTRY_CODE
, nPaddedLength
);
196 ConPrintf(StdOut
, L
"%03ld ()\n", pUserInfo
->usri4_country_code
);
198 PrintPaddedResourceString(IDS_USER_ACCOUNT_ACTIVE
, nPaddedLength
);
199 if (pUserInfo
->usri4_flags
& UF_ACCOUNTDISABLE
)
200 ConResPuts(StdOut
, IDS_GENERIC_NO
);
201 else if (pUserInfo
->usri4_flags
& UF_LOCKOUT
)
202 ConResPuts(StdOut
, IDS_GENERIC_LOCKED
);
204 ConResPuts(StdOut
, IDS_GENERIC_YES
);
205 ConPuts(StdOut
, L
"\n");
207 PrintPaddedResourceString(IDS_USER_ACCOUNT_EXPIRES
, nPaddedLength
);
208 if (pUserInfo
->usri4_acct_expires
== TIMEQ_FOREVER
)
209 ConResPuts(StdOut
, IDS_GENERIC_NEVER
);
211 PrintDateTime(pUserInfo
->usri4_acct_expires
);
212 ConPuts(StdOut
, L
"\n\n");
214 PrintPaddedResourceString(IDS_USER_PW_LAST_SET
, nPaddedLength
);
215 dwLastSet
= GetTimeInSeconds() - pUserInfo
->usri4_password_age
;
216 PrintDateTime(dwLastSet
);
217 ConPuts(StdOut
, L
"\n");
219 PrintPaddedResourceString(IDS_USER_PW_EXPIRES
, nPaddedLength
);
220 if ((pUserInfo
->usri4_flags
& UF_DONT_EXPIRE_PASSWD
) || pUserModals
->usrmod0_max_passwd_age
== TIMEQ_FOREVER
)
221 ConResPuts(StdOut
, IDS_GENERIC_NEVER
);
223 PrintDateTime(dwLastSet
+ pUserModals
->usrmod0_max_passwd_age
);
224 ConPuts(StdOut
, L
"\n");
226 PrintPaddedResourceString(IDS_USER_PW_CHANGEABLE
, nPaddedLength
);
227 PrintDateTime(dwLastSet
+ pUserModals
->usrmod0_min_passwd_age
);
228 ConPuts(StdOut
, L
"\n");
230 PrintPaddedResourceString(IDS_USER_PW_REQUIRED
, nPaddedLength
);
231 ConResPuts(StdOut
, (pUserInfo
->usri4_flags
& UF_PASSWD_NOTREQD
) ? IDS_GENERIC_NO
: IDS_GENERIC_YES
);
232 ConPuts(StdOut
, L
"\n");
234 PrintPaddedResourceString(IDS_USER_CHANGE_PW
, nPaddedLength
);
235 ConResPuts(StdOut
, (pUserInfo
->usri4_flags
& UF_PASSWD_CANT_CHANGE
) ? IDS_GENERIC_NO
: IDS_GENERIC_YES
);
236 ConPuts(StdOut
, L
"\n\n");
238 PrintPaddedResourceString(IDS_USER_WORKSTATIONS
, nPaddedLength
);
239 if (pUserInfo
->usri4_workstations
== NULL
|| wcslen(pUserInfo
->usri4_workstations
) == 0)
240 ConResPuts(StdOut
, IDS_GENERIC_ALL
);
242 ConPrintf(StdOut
, L
"%s", pUserInfo
->usri4_workstations
);
243 ConPuts(StdOut
, L
"\n");
245 PrintPaddedResourceString(IDS_USER_LOGON_SCRIPT
, nPaddedLength
);
246 ConPrintf(StdOut
, L
"%s\n", pUserInfo
->usri4_script_path
);
248 PrintPaddedResourceString(IDS_USER_PROFILE
, nPaddedLength
);
249 ConPrintf(StdOut
, L
"%s\n", pUserInfo
->usri4_profile
);
251 PrintPaddedResourceString(IDS_USER_HOME_DIR
, nPaddedLength
);
252 ConPrintf(StdOut
, L
"%s\n", pUserInfo
->usri4_home_dir
);
254 PrintPaddedResourceString(IDS_USER_LAST_LOGON
, nPaddedLength
);
255 if (pUserInfo
->usri4_last_logon
== 0)
256 ConResPuts(StdOut
, IDS_GENERIC_NEVER
);
258 PrintDateTime(pUserInfo
->usri4_last_logon
);
259 ConPuts(StdOut
, L
"\n\n");
261 PrintPaddedResourceString(IDS_USER_LOGON_HOURS
, nPaddedLength
);
262 if (pUserInfo
->usri4_logon_hours
== NULL
)
263 ConResPuts(StdOut
, IDS_GENERIC_ALL
);
264 ConPuts(StdOut
, L
"\n\n");
266 ConPuts(StdOut
, L
"\n");
267 PrintPaddedResourceString(IDS_USER_LOCAL_GROUPS
, nPaddedLength
);
268 if (dwLocalGroupTotal
!= 0 && pLocalGroupInfo
!= NULL
)
270 for (i
= 0; i
< dwLocalGroupTotal
; i
++)
273 PrintPadding(L
' ', nPaddedLength
);
274 ConPrintf(StdOut
, L
"*%s\n", pLocalGroupInfo
[i
].lgrui0_name
);
279 ConPuts(StdOut
, L
"\n");
282 PrintPaddedResourceString(IDS_USER_GLOBAL_GROUPS
, nPaddedLength
);
283 if (dwGroupTotal
!= 0 && pGroupInfo
!= NULL
)
285 for (i
= 0; i
< dwGroupTotal
; i
++)
288 PrintPadding(L
' ', nPaddedLength
);
289 ConPrintf(StdOut
, L
"*%s\n", pGroupInfo
[i
].grui0_name
);
294 ConPuts(StdOut
, L
"\n");
298 if (pGroupInfo
!= NULL
)
299 NetApiBufferFree(pGroupInfo
);
301 if (pLocalGroupInfo
!= NULL
)
302 NetApiBufferFree(pLocalGroupInfo
);
304 if (pUserModals
!= NULL
)
305 NetApiBufferFree(pUserModals
);
307 if (pUserInfo
!= NULL
)
308 NetApiBufferFree(pUserInfo
);
320 WCHAR szPassword1
[PWLEN
+ 1];
321 WCHAR szPassword2
[PWLEN
+ 1];
324 *lpAllocated
= FALSE
;
328 ConResPuts(StdOut
, IDS_USER_ENTER_PASSWORD1
);
329 ReadFromConsole(szPassword1
, PWLEN
+ 1, FALSE
);
330 ConPuts(StdOut
, L
"\n");
332 ConResPuts(StdOut
, IDS_USER_ENTER_PASSWORD2
);
333 ReadFromConsole(szPassword2
, PWLEN
+ 1, FALSE
);
334 ConPuts(StdOut
, L
"\n");
336 if (wcslen(szPassword1
) == wcslen(szPassword2
) &&
337 wcscmp(szPassword1
, szPassword2
) == 0)
339 ptr
= HeapAlloc(GetProcessHeap(),
341 (wcslen(szPassword1
) + 1) * sizeof(WCHAR
));
344 wcscpy(ptr
, szPassword1
);
352 ConPuts(StdOut
, L
"\n");
353 ConResPuts(StdOut
, IDS_USER_NO_PASSWORD_MATCH
);
354 ConPuts(StdOut
, L
"\n");
369 BOOL bDelete
= FALSE
;
371 BOOL bDomain
= FALSE
;
373 LPWSTR lpUserName
= NULL
;
374 LPWSTR lpPassword
= NULL
;
375 PUSER_INFO_4 pUserInfo
= NULL
;
376 USER_INFO_4 UserInfo
;
380 BOOL bPasswordAllocated
= FALSE
;
381 NET_API_STATUS Status
;
385 Status
= EnumerateUsers();
386 ConPrintf(StdOut
, L
"Status: %lu\n", Status
);
391 Status
= DisplayUser(argv
[2]);
392 ConPrintf(StdOut
, L
"Status: %lu\n", Status
);
397 if (argv
[i
][0] != L
'/')
399 lpUserName
= argv
[i
];
400 // ConPrintf(StdOut, L"User: %s\n", lpUserName);
404 if (argv
[i
][0] != L
'/')
406 lpPassword
= argv
[i
];
407 // ConPrintf(StdOut, L"Password: %s\n", lpPassword);
411 for (j
= i
; j
< argc
; j
++)
413 if (_wcsicmp(argv
[j
], L
"/help") == 0)
415 ConResPuts(StdOut
, IDS_USER_HELP
);
418 else if (_wcsicmp(argv
[j
], L
"/add") == 0)
422 else if (_wcsicmp(argv
[j
], L
"/delete") == 0)
426 else if (_wcsicmp(argv
[j
], L
"/domain") == 0)
428 ConResPrintf(StdErr
, IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/DOMAIN");
441 /* Interactive password input */
442 if (lpPassword
!= NULL
&& wcscmp(lpPassword
, L
"*") == 0)
444 ReadPassword(&lpPassword
,
445 &bPasswordAllocated
);
448 if (!bAdd
&& !bDelete
)
450 /* Modify the user */
451 Status
= NetUserGetInfo(NULL
,
454 (LPBYTE
*)&pUserInfo
);
455 if (Status
!= NERR_Success
)
457 ConPrintf(StdOut
, L
"Status: %lu\n", Status
);
462 else if (bAdd
&& !bDelete
)
465 ZeroMemory(&UserInfo
, sizeof(USER_INFO_4
));
467 UserInfo
.usri4_name
= lpUserName
;
468 UserInfo
.usri4_password
= lpPassword
;
469 UserInfo
.usri4_flags
= UF_SCRIPT
| UF_NORMAL_ACCOUNT
;
471 pUserInfo
= &UserInfo
;
474 for (j
= i
; j
< argc
; j
++)
476 if (_wcsnicmp(argv
[j
], L
"/active:", 8) == 0)
479 if (_wcsicmp(p
, L
"yes") == 0)
481 pUserInfo
->usri4_flags
&= ~UF_ACCOUNTDISABLE
;
483 else if (_wcsicmp(p
, L
"no") == 0)
485 pUserInfo
->usri4_flags
|= UF_ACCOUNTDISABLE
;
489 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"/ACTIVE");
494 else if (_wcsnicmp(argv
[j
], L
"/comment:", 9) == 0)
496 pUserInfo
->usri4_comment
= &argv
[j
][9];
498 else if (_wcsnicmp(argv
[j
], L
"/countrycode:", 13) == 0)
501 value
= wcstoul(p
, &endptr
, 10);
504 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"/COUNTRYCODE");
509 /* FIXME: verify the country code */
511 pUserInfo
->usri4_country_code
= value
;
513 else if (_wcsnicmp(argv
[j
], L
"/expires:", 9) == 0)
516 if (_wcsicmp(p
, L
"never") == 0)
518 pUserInfo
->usri4_acct_expires
= TIMEQ_FOREVER
;
522 /* FIXME: Parse the date */
523 ConResPrintf(StdErr
, IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/EXPIRES");
526 else if (_wcsnicmp(argv
[j
], L
"/fullname:", 10) == 0)
528 pUserInfo
->usri4_full_name
= &argv
[j
][10];
530 else if (_wcsnicmp(argv
[j
], L
"/homedir:", 9) == 0)
532 pUserInfo
->usri4_home_dir
= &argv
[j
][9];
534 else if (_wcsnicmp(argv
[j
], L
"/passwordchg:", 13) == 0)
537 if (_wcsicmp(p
, L
"yes") == 0)
539 pUserInfo
->usri4_flags
&= ~UF_PASSWD_CANT_CHANGE
;
541 else if (_wcsicmp(p
, L
"no") == 0)
543 pUserInfo
->usri4_flags
|= UF_PASSWD_CANT_CHANGE
;
547 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"/PASSWORDCHG");
552 else if (_wcsnicmp(argv
[j
], L
"/passwordreq:", 13) == 0)
555 if (_wcsicmp(p
, L
"yes") == 0)
557 pUserInfo
->usri4_flags
&= ~UF_PASSWD_NOTREQD
;
559 else if (_wcsicmp(p
, L
"no") == 0)
561 pUserInfo
->usri4_flags
|= UF_PASSWD_NOTREQD
;
565 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"/PASSWORDREQ");
570 else if (_wcsnicmp(argv
[j
], L
"/profilepath:", 13) == 0)
572 pUserInfo
->usri4_profile
= &argv
[j
][13];
574 else if (_wcsnicmp(argv
[j
], L
"/scriptpath:", 12) == 0)
576 pUserInfo
->usri4_script_path
= &argv
[j
][12];
578 else if (_wcsnicmp(argv
[j
], L
"/times:", 7) == 0)
581 ConResPrintf(StdErr
, IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/TIMES");
583 else if (_wcsnicmp(argv
[j
], L
"/usercomment:", 13) == 0)
585 pUserInfo
->usri4_usr_comment
= &argv
[j
][13];
587 else if (_wcsnicmp(argv
[j
], L
"/workstations:", 14) == 0)
590 ConResPrintf(StdErr
, IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/WORKSTATIONS");
594 if (!bAdd
&& !bDelete
)
596 /* Modify the user */
597 Status
= NetUserSetInfo(NULL
,
602 ConPrintf(StdOut
, L
"Status: %lu\n", Status
);
604 else if (bAdd
&& !bDelete
)
607 Status
= NetUserAdd(NULL
,
611 ConPrintf(StdOut
, L
"Status: %lu\n", Status
);
613 else if (!bAdd
&& bDelete
)
615 /* Delete the user */
616 Status
= NetUserDel(NULL
,
618 ConPrintf(StdOut
, L
"Status: %lu\n", Status
);
622 if ((bPasswordAllocated
!= FALSE
) && (lpPassword
!= NULL
))
623 HeapFree(GetProcessHeap(), 0, lpPassword
);
625 if (!bAdd
&& !bDelete
&& pUserInfo
!= NULL
)
626 NetApiBufferFree(pUserInfo
);
629 ConResPuts(StdOut
, IDS_USER_SYNTAX
);