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 CompareInfo(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 PrintToConsole(L
"\n");
42 PrintResourceString(IDS_USER_ACCOUNTS
, pServer
->sv100_name
);
43 PrintToConsole(L
"\n\n");
44 PrintPadding(L
'-', 79);
45 PrintToConsole(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 PrintToConsole(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 PrintToConsole(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 PrintToConsole(L
"%s\n", pUserInfo
->usri4_name
);
186 PrintPaddedResourceString(IDS_USER_FULL_NAME
, nPaddedLength
);
187 PrintToConsole(L
"%s\n", pUserInfo
->usri4_full_name
);
189 PrintPaddedResourceString(IDS_USER_COMMENT
, nPaddedLength
);
190 PrintToConsole(L
"%s\n", pUserInfo
->usri4_comment
);
192 PrintPaddedResourceString(IDS_USER_USER_COMMENT
, nPaddedLength
);
193 PrintToConsole(L
"%s\n", pUserInfo
->usri4_usr_comment
);
195 PrintPaddedResourceString(IDS_USER_COUNTRY_CODE
, nPaddedLength
);
196 PrintToConsole(L
"%03ld ()\n", pUserInfo
->usri4_country_code
);
198 PrintPaddedResourceString(IDS_USER_ACCOUNT_ACTIVE
, nPaddedLength
);
199 if (pUserInfo
->usri4_flags
& UF_ACCOUNTDISABLE
)
200 PrintResourceString(IDS_GENERIC_NO
);
201 else if (pUserInfo
->usri4_flags
& UF_LOCKOUT
)
202 PrintResourceString(IDS_GENERIC_LOCKED
);
204 PrintResourceString(IDS_GENERIC_YES
);
205 PrintToConsole(L
"\n");
207 PrintPaddedResourceString(IDS_USER_ACCOUNT_EXPIRES
, nPaddedLength
);
208 if (pUserInfo
->usri4_acct_expires
== TIMEQ_FOREVER
)
209 PrintResourceString(IDS_GENERIC_NEVER
);
211 PrintDateTime(pUserInfo
->usri4_acct_expires
);
212 PrintToConsole(L
"\n\n");
214 PrintPaddedResourceString(IDS_USER_PW_LAST_SET
, nPaddedLength
);
215 dwLastSet
= GetTimeInSeconds() - pUserInfo
->usri4_password_age
;
216 PrintDateTime(dwLastSet
);
218 PrintPaddedResourceString(IDS_USER_PW_EXPIRES
, nPaddedLength
);
219 if ((pUserInfo
->usri4_flags
& UF_DONT_EXPIRE_PASSWD
) || pUserModals
->usrmod0_max_passwd_age
== TIMEQ_FOREVER
)
220 PrintResourceString(IDS_GENERIC_NEVER
);
222 PrintDateTime(dwLastSet
+ pUserModals
->usrmod0_max_passwd_age
);
223 PrintToConsole(L
"\n");
225 PrintPaddedResourceString(IDS_USER_PW_CHANGEABLE
, nPaddedLength
);
226 PrintDateTime(dwLastSet
+ pUserModals
->usrmod0_min_passwd_age
);
228 PrintPaddedResourceString(IDS_USER_PW_REQUIRED
, nPaddedLength
);
229 PrintResourceString((pUserInfo
->usri4_flags
& UF_PASSWD_NOTREQD
) ? IDS_GENERIC_NO
: IDS_GENERIC_YES
);
230 PrintToConsole(L
"\n");
232 PrintPaddedResourceString(IDS_USER_CHANGE_PW
, nPaddedLength
);
233 PrintResourceString((pUserInfo
->usri4_flags
& UF_PASSWD_CANT_CHANGE
) ? IDS_GENERIC_NO
: IDS_GENERIC_YES
);
234 PrintToConsole(L
"\n\n");
236 PrintPaddedResourceString(IDS_USER_WORKSTATIONS
, nPaddedLength
);
237 if (pUserInfo
->usri4_workstations
== NULL
|| wcslen(pUserInfo
->usri4_workstations
) == 0)
238 PrintResourceString(IDS_GENERIC_ALL
);
240 PrintToConsole(L
"%s", pUserInfo
->usri4_workstations
);
241 PrintToConsole(L
"\n");
243 PrintPaddedResourceString(IDS_USER_LOGON_SCRIPT
, nPaddedLength
);
244 PrintToConsole(L
"%s\n", pUserInfo
->usri4_script_path
);
246 PrintPaddedResourceString(IDS_USER_PROFILE
, nPaddedLength
);
247 PrintToConsole(L
"%s\n", pUserInfo
->usri4_profile
);
249 PrintPaddedResourceString(IDS_USER_HOME_DIR
, nPaddedLength
);
250 PrintToConsole(L
"%s\n", pUserInfo
->usri4_home_dir
);
252 PrintPaddedResourceString(IDS_USER_LAST_LOGON
, nPaddedLength
);
253 if (pUserInfo
->usri4_last_logon
== 0)
254 PrintResourceString(IDS_GENERIC_NEVER
);
256 PrintDateTime(pUserInfo
->usri4_last_logon
);
257 PrintToConsole(L
"\n\n");
259 PrintPaddedResourceString(IDS_USER_LOGON_HOURS
, nPaddedLength
);
260 if (pUserInfo
->usri4_logon_hours
== NULL
)
261 PrintResourceString(IDS_GENERIC_ALL
);
262 PrintToConsole(L
"\n\n");
264 PrintToConsole(L
"\n");
265 PrintPaddedResourceString(IDS_USER_LOCAL_GROUPS
, nPaddedLength
);
266 if (dwLocalGroupTotal
!= 0 && pLocalGroupInfo
!= NULL
)
268 for (i
= 0; i
< dwLocalGroupTotal
; i
++)
271 PrintPadding(L
' ', nPaddedLength
);
272 PrintToConsole(L
"*%s\n", pLocalGroupInfo
[i
].lgrui0_name
);
277 PrintToConsole(L
"\n");
280 PrintPaddedResourceString(IDS_USER_GLOBAL_GROUPS
, nPaddedLength
);
281 if (dwGroupTotal
!= 0 && pGroupInfo
!= NULL
)
283 for (i
= 0; i
< dwGroupTotal
; i
++)
286 PrintPadding(L
' ', nPaddedLength
);
287 PrintToConsole(L
"*%s\n", pGroupInfo
[i
].grui0_name
);
292 PrintToConsole(L
"\n");
296 if (pGroupInfo
!= NULL
)
297 NetApiBufferFree(pGroupInfo
);
299 if (pLocalGroupInfo
!= NULL
)
300 NetApiBufferFree(pLocalGroupInfo
);
302 if (pUserModals
!= NULL
)
303 NetApiBufferFree(pUserModals
);
305 if (pUserInfo
!= NULL
)
306 NetApiBufferFree(pUserInfo
);
318 WCHAR szPassword1
[PWLEN
+ 1];
319 WCHAR szPassword2
[PWLEN
+ 1];
322 *lpAllocated
= FALSE
;
326 PrintResourceString(IDS_USER_ENTER_PASSWORD1
);
327 ReadFromConsole(szPassword1
, PWLEN
+ 1, FALSE
);
328 PrintToConsole(L
"\n");
330 PrintResourceString(IDS_USER_ENTER_PASSWORD2
);
331 ReadFromConsole(szPassword2
, PWLEN
+ 1, FALSE
);
332 PrintToConsole(L
"\n");
334 if (wcslen(szPassword1
) == wcslen(szPassword2
) &&
335 wcscmp(szPassword1
, szPassword2
) == 0)
337 ptr
= HeapAlloc(GetProcessHeap(),
339 (wcslen(szPassword1
) + 1) * sizeof(WCHAR
));
342 wcscpy(ptr
, szPassword1
);
350 PrintToConsole(L
"\n");
351 PrintResourceString(IDS_USER_NO_PASSWORD_MATCH
);
352 PrintToConsole(L
"\n");
367 BOOL bDelete
= FALSE
;
369 BOOL bDomain
= FALSE
;
371 LPWSTR lpUserName
= NULL
;
372 LPWSTR lpPassword
= NULL
;
373 PUSER_INFO_4 pUserInfo
= NULL
;
374 USER_INFO_4 UserInfo
;
378 BOOL bPasswordAllocated
= FALSE
;
379 NET_API_STATUS Status
;
383 Status
= EnumerateUsers();
384 printf("Status: %lu\n", Status
);
389 Status
= DisplayUser(argv
[2]);
390 printf("Status: %lu\n", Status
);
395 if (argv
[i
][0] != L
'/')
397 lpUserName
= argv
[i
];
398 // printf("User: %S\n", lpUserName);
402 if (argv
[i
][0] != L
'/')
404 lpPassword
= argv
[i
];
405 // printf("Password: %S\n", lpPassword);
409 for (j
= i
; j
< argc
; j
++)
411 if (_wcsicmp(argv
[j
], L
"/help") == 0)
413 PrintResourceString(IDS_USER_HELP
);
416 else if (_wcsicmp(argv
[j
], L
"/add") == 0)
420 else if (_wcsicmp(argv
[j
], L
"/delete") == 0)
424 else if (_wcsicmp(argv
[j
], L
"/domain") == 0)
426 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/DOMAIN");
439 /* Interactive password input */
440 if (lpPassword
!= NULL
&& wcscmp(lpPassword
, L
"*") == 0)
442 ReadPassword(&lpPassword
,
443 &bPasswordAllocated
);
446 if (!bAdd
&& !bDelete
)
448 /* Modify the user */
449 Status
= NetUserGetInfo(NULL
,
452 (LPBYTE
*)&pUserInfo
);
453 if (Status
!= NERR_Success
)
455 printf("Status: %lu\n", Status
);
460 else if (bAdd
&& !bDelete
)
463 ZeroMemory(&UserInfo
, sizeof(USER_INFO_4
));
465 UserInfo
.usri4_name
= lpUserName
;
466 UserInfo
.usri4_password
= lpPassword
;
467 UserInfo
.usri4_flags
= UF_SCRIPT
| UF_NORMAL_ACCOUNT
;
469 pUserInfo
= &UserInfo
;
472 for (j
= i
; j
< argc
; j
++)
474 if (_wcsnicmp(argv
[j
], L
"/active:", 8) == 0)
477 if (_wcsicmp(p
, L
"yes") == 0)
479 pUserInfo
->usri4_flags
&= ~UF_ACCOUNTDISABLE
;
481 else if (_wcsicmp(p
, L
"no") == 0)
483 pUserInfo
->usri4_flags
|= UF_ACCOUNTDISABLE
;
487 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/ACTIVE");
492 else if (_wcsnicmp(argv
[j
], L
"/comment:", 9) == 0)
494 pUserInfo
->usri4_comment
= &argv
[j
][9];
496 else if (_wcsnicmp(argv
[j
], L
"/countrycode:", 13) == 0)
499 value
= wcstoul(p
, &endptr
, 10);
502 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/COUNTRYCODE");
507 /* FIXME: verify the country code */
509 pUserInfo
->usri4_country_code
= value
;
511 else if (_wcsnicmp(argv
[j
], L
"/expires:", 9) == 0)
514 if (_wcsicmp(p
, L
"never") == 0)
516 pUserInfo
->usri4_acct_expires
= TIMEQ_FOREVER
;
520 /* FIXME: Parse the date */
521 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/EXPIRES");
524 else if (_wcsnicmp(argv
[j
], L
"/fullname:", 10) == 0)
526 pUserInfo
->usri4_full_name
= &argv
[j
][10];
528 else if (_wcsnicmp(argv
[j
], L
"/homedir:", 9) == 0)
530 pUserInfo
->usri4_home_dir
= &argv
[j
][9];
532 else if (_wcsnicmp(argv
[j
], L
"/passwordchg:", 13) == 0)
535 if (_wcsicmp(p
, L
"yes") == 0)
537 pUserInfo
->usri4_flags
&= ~UF_PASSWD_CANT_CHANGE
;
539 else if (_wcsicmp(p
, L
"no") == 0)
541 pUserInfo
->usri4_flags
|= UF_PASSWD_CANT_CHANGE
;
545 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/PASSWORDCHG");
550 else if (_wcsnicmp(argv
[j
], L
"/passwordreq:", 13) == 0)
553 if (_wcsicmp(p
, L
"yes") == 0)
555 pUserInfo
->usri4_flags
&= ~UF_PASSWD_NOTREQD
;
557 else if (_wcsicmp(p
, L
"no") == 0)
559 pUserInfo
->usri4_flags
|= UF_PASSWD_NOTREQD
;
563 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/PASSWORDREQ");
568 else if (_wcsnicmp(argv
[j
], L
"/profilepath:", 13) == 0)
570 pUserInfo
->usri4_profile
= &argv
[j
][13];
572 else if (_wcsnicmp(argv
[j
], L
"/scriptpath:", 12) == 0)
574 pUserInfo
->usri4_script_path
= &argv
[j
][12];
576 else if (_wcsnicmp(argv
[j
], L
"/times:", 7) == 0)
579 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/TIMES");
581 else if (_wcsnicmp(argv
[j
], L
"/usercomment:", 13) == 0)
583 pUserInfo
->usri4_usr_comment
= &argv
[j
][13];
585 else if (_wcsnicmp(argv
[j
], L
"/workstations:", 14) == 0)
588 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/WORKSTATIONS");
592 if (!bAdd
&& !bDelete
)
594 /* Modify the user */
595 Status
= NetUserSetInfo(NULL
,
600 printf("Status: %lu\n", Status
);
602 else if (bAdd
&& !bDelete
)
605 Status
= NetUserAdd(NULL
,
609 printf("Status: %lu\n", Status
);
611 else if (!bAdd
&& bDelete
)
613 /* Delete the user */
614 Status
= NetUserDel(NULL
,
616 printf("Status: %lu\n", Status
);
620 if (bPasswordAllocated
== TRUE
&& lpPassword
!= NULL
)
621 HeapFree(GetProcessHeap(), 0, lpPassword
);
623 if (!bAdd
&& !bDelete
&& pUserInfo
!= NULL
)
624 NetApiBufferFree(pUserInfo
);
627 PrintResourceString(IDS_USER_SYNTAX
);