2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
7 * PROGRAMMERS: Eric Kohl
15 CompareInfo(const void *a
,
18 return _wcsicmp(((PUSER_INFO_0
)a
)->usri0_name
,
19 ((PUSER_INFO_0
)b
)->usri0_name
);
27 PUSER_INFO_0 pBuffer
= NULL
;
28 PSERVER_INFO_100 pServer
= NULL
;
29 DWORD dwRead
= 0, dwTotal
= 0;
31 DWORD_PTR ResumeHandle
= 0;
32 NET_API_STATUS Status
;
34 Status
= NetServerGetInfo(NULL
,
37 if (Status
!= NERR_Success
)
40 PrintToConsole(L
"\nUser accounts for \\\\%s\n\n", pServer
->sv100_name
);
42 NetApiBufferFree(pServer
);
44 PrintToConsole(L
"------------------------------------------\n");
46 Status
= NetUserEnum(NULL
,
54 if (Status
!= NERR_Success
)
62 // printf("dwRead: %lu dwTotal: %lu\n", dwRead, dwTotal);
63 for (i
= 0; i
< dwRead
; i
++)
65 // printf("%p\n", pBuffer[i].lgrpi0_name);
66 if (pBuffer
[i
].usri0_name
)
67 PrintToConsole(L
"%s\n", pBuffer
[i
].usri0_name
);
70 NetApiBufferFree(pBuffer
);
78 PrintDateTime(DWORD dwSeconds
)
82 SYSTEMTIME SystemTime
;
86 RtlSecondsSince1970ToTime(dwSeconds
, &Time
);
87 FileTime
.dwLowDateTime
= Time
.u
.LowPart
;
88 FileTime
.dwHighDateTime
= Time
.u
.HighPart
;
89 FileTimeToLocalFileTime(&FileTime
, &FileTime
);
90 FileTimeToSystemTime(&FileTime
, &SystemTime
);
92 GetDateFormatW(LOCALE_USER_DEFAULT
,
99 GetTimeFormatW(LOCALE_USER_DEFAULT
,
106 PrintToConsole(L
"%s %s\n", DateBuffer
, TimeBuffer
);
112 GetTimeInSeconds(VOID
)
118 GetSystemTimeAsFileTime(&FileTime
);
119 Time
.u
.LowPart
= FileTime
.dwLowDateTime
;
120 Time
.u
.HighPart
= FileTime
.dwHighDateTime
;
121 RtlTimeToSecondsSince1970(&Time
, &dwSeconds
);
129 DisplayUser(LPWSTR lpUserName
)
131 PUSER_MODALS_INFO_0 pUserModals
= NULL
;
132 PUSER_INFO_4 pUserInfo
= NULL
;
133 PLOCALGROUP_USERS_INFO_0 pLocalGroupInfo
= NULL
;
134 PGROUP_USERS_INFO_0 pGroupInfo
= NULL
;
135 DWORD dwLocalGroupRead
, dwLocalGroupTotal
;
136 DWORD dwGroupRead
, dwGroupTotal
;
139 NET_API_STATUS Status
;
141 /* Modify the user */
142 Status
= NetUserGetInfo(NULL
,
145 (LPBYTE
*)&pUserInfo
);
146 if (Status
!= NERR_Success
)
149 Status
= NetUserModalsGet(NULL
,
151 (LPBYTE
*)&pUserModals
);
152 if (Status
!= NERR_Success
)
155 Status
= NetUserGetLocalGroups(NULL
,
159 (LPBYTE
*)&pLocalGroupInfo
,
160 MAX_PREFERRED_LENGTH
,
163 if (Status
!= NERR_Success
)
166 Status
= NetUserGetGroups(NULL
,
169 (LPBYTE
*)&pGroupInfo
,
170 MAX_PREFERRED_LENGTH
,
173 if (Status
!= NERR_Success
)
176 PrintToConsole(L
"User name %s\n", pUserInfo
->usri4_name
);
177 PrintToConsole(L
"Full name %s\n", pUserInfo
->usri4_full_name
);
178 PrintToConsole(L
"Comment %s\n", pUserInfo
->usri4_comment
);
179 PrintToConsole(L
"User comment %s\n", pUserInfo
->usri4_usr_comment
);
180 PrintToConsole(L
"Country code %03ld ()\n", pUserInfo
->usri4_country_code
);
181 PrintToConsole(L
"Account active %s\n", (pUserInfo
->usri4_flags
& UF_ACCOUNTDISABLE
)? L
"No" : ((pUserInfo
->usri4_flags
& UF_LOCKOUT
) ? L
"Locked" : L
"Yes"));
182 PrintToConsole(L
"Account expires ");
183 if (pUserInfo
->usri4_acct_expires
== TIMEQ_FOREVER
)
184 PrintToConsole(L
"Never\n");
186 PrintDateTime(pUserInfo
->usri4_acct_expires
);
188 PrintToConsole(L
"\n");
190 PrintToConsole(L
"Password last set ");
191 dwLastSet
= GetTimeInSeconds() - pUserInfo
->usri4_password_age
;
192 PrintDateTime(dwLastSet
);
194 PrintToConsole(L
"Password expires ");
195 if ((pUserInfo
->usri4_flags
& UF_DONT_EXPIRE_PASSWD
) || pUserModals
->usrmod0_max_passwd_age
== TIMEQ_FOREVER
)
196 PrintToConsole(L
"Never\n");
198 PrintDateTime(dwLastSet
+ pUserModals
->usrmod0_max_passwd_age
);
200 PrintToConsole(L
"Password changeable ");
201 PrintDateTime(dwLastSet
+ pUserModals
->usrmod0_min_passwd_age
);
203 PrintToConsole(L
"Password required %s\n", (pUserInfo
->usri4_flags
& UF_PASSWD_NOTREQD
) ? L
"No" : L
"Yes");
204 PrintToConsole(L
"User may change password %s\n", (pUserInfo
->usri4_flags
& UF_PASSWD_CANT_CHANGE
) ? L
"No" : L
"Yes");
206 PrintToConsole(L
"\n");
207 PrintToConsole(L
"Workstations allowed %s\n", (pUserInfo
->usri4_workstations
== NULL
|| wcslen(pUserInfo
->usri4_workstations
) == 0) ? L
"All" : pUserInfo
->usri4_workstations
);
208 PrintToConsole(L
"Logon script %s\n", pUserInfo
->usri4_script_path
);
209 PrintToConsole(L
"User profile %s\n", pUserInfo
->usri4_profile
);
210 PrintToConsole(L
"Home directory %s\n", pUserInfo
->usri4_home_dir
);
211 PrintToConsole(L
"Last logon ");
212 if (pUserInfo
->usri4_last_logon
== 0)
213 PrintToConsole(L
"Never\n");
215 PrintDateTime(pUserInfo
->usri4_last_logon
);
216 PrintToConsole(L
"\n");
217 PrintToConsole(L
"Logon hours allowed ");
218 if (pUserInfo
->usri4_logon_hours
== NULL
)
219 PrintToConsole(L
"All\n");
220 PrintToConsole(L
"\n");
222 PrintToConsole(L
"\n");
223 PrintToConsole(L
"Local group memberships ");
224 if (dwLocalGroupTotal
!= 0 && pLocalGroupInfo
!= NULL
)
226 for (i
= 0; i
< dwLocalGroupTotal
; i
++)
229 PrintToConsole(L
" ");
230 PrintToConsole(L
"*%s\n", pLocalGroupInfo
[i
].lgrui0_name
);
235 PrintToConsole(L
"\n");
238 PrintToConsole(L
"Global group memberships ");
239 if (dwGroupTotal
!= 0 && pGroupInfo
!= NULL
)
241 for (i
= 0; i
< dwGroupTotal
; i
++)
244 PrintToConsole(L
" ");
245 PrintToConsole(L
"*%s\n", pGroupInfo
[i
].grui0_name
);
250 PrintToConsole(L
"\n");
254 if (pGroupInfo
!= NULL
)
255 NetApiBufferFree(pGroupInfo
);
257 if (pLocalGroupInfo
!= NULL
)
258 NetApiBufferFree(pLocalGroupInfo
);
260 if (pUserModals
!= NULL
)
261 NetApiBufferFree(pUserModals
);
263 if (pUserInfo
!= NULL
)
264 NetApiBufferFree(pUserInfo
);
276 WCHAR szPassword1
[PWLEN
+ 1];
277 WCHAR szPassword2
[PWLEN
+ 1];
280 *lpAllocated
= FALSE
;
282 printf("Enter the password for user xxx: ");
283 ReadFromConsole(szPassword1
, PWLEN
+ 1, FALSE
);
286 printf("Enter the password again: ");
287 ReadFromConsole(szPassword2
, PWLEN
+ 1, FALSE
);
290 if (wcslen(szPassword1
) == wcslen(szPassword2
) &&
291 wcscmp(szPassword1
, szPassword2
) == 0)
293 ptr
= HeapAlloc(GetProcessHeap(),
295 (wcslen(szPassword1
) + 1) * sizeof(WCHAR
));
298 wcscpy(ptr
, szPassword1
);
305 printf("The passwords do not match!");
319 BOOL bDelete
= FALSE
;
321 BOOL bDomain
= FALSE
;
323 LPWSTR lpUserName
= NULL
;
324 LPWSTR lpPassword
= NULL
;
325 PUSER_INFO_4 pUserInfo
= NULL
;
326 USER_INFO_4 UserInfo
;
330 BOOL bPasswordAllocated
= FALSE
;
331 NET_API_STATUS Status
;
335 Status
= EnumerateUsers();
336 printf("Status: %lu\n", Status
);
341 Status
= DisplayUser(argv
[2]);
342 printf("Status: %lu\n", Status
);
347 if (argv
[i
][0] != L
'/')
349 lpUserName
= argv
[i
];
350 // printf("User: %S\n", lpUserName);
354 if (argv
[i
][0] != L
'/')
356 lpPassword
= argv
[i
];
357 // printf("Password: %S\n", lpPassword);
361 for (j
= i
; j
< argc
; j
++)
363 if (_wcsicmp(argv
[j
], L
"/help") == 0)
365 PrintResourceString(IDS_USER_HELP
);
368 else if (_wcsicmp(argv
[j
], L
"/add") == 0)
372 else if (_wcsicmp(argv
[j
], L
"/delete") == 0)
376 else if (_wcsicmp(argv
[j
], L
"/domain") == 0)
378 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/DOMAIN");
391 /* Interactive password input */
392 if (lpPassword
!= NULL
&& wcscmp(lpPassword
, L
"*") == 0)
394 ReadPassword(&lpPassword
,
395 &bPasswordAllocated
);
398 if (!bAdd
&& !bDelete
)
400 /* Modify the user */
401 Status
= NetUserGetInfo(NULL
,
404 (LPBYTE
*)&pUserInfo
);
405 if (Status
!= NERR_Success
)
407 printf("Status: %lu\n", Status
);
412 else if (bAdd
&& !bDelete
)
415 ZeroMemory(&UserInfo
, sizeof(USER_INFO_4
));
417 UserInfo
.usri4_name
= lpUserName
;
418 UserInfo
.usri4_password
= lpPassword
;
419 UserInfo
.usri4_flags
= UF_SCRIPT
| UF_NORMAL_ACCOUNT
;
421 pUserInfo
= &UserInfo
;
424 for (j
= i
; j
< argc
; j
++)
426 if (_wcsnicmp(argv
[j
], L
"/active:", 8) == 0)
429 if (_wcsicmp(p
, L
"yes") == 0)
431 pUserInfo
->usri4_flags
&= ~UF_ACCOUNTDISABLE
;
433 else if (_wcsicmp(p
, L
"no") == 0)
435 pUserInfo
->usri4_flags
|= UF_ACCOUNTDISABLE
;
439 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/ACTIVE");
444 else if (_wcsnicmp(argv
[j
], L
"/comment:", 9) == 0)
446 pUserInfo
->usri4_comment
= &argv
[j
][9];
448 else if (_wcsnicmp(argv
[j
], L
"/countrycode:", 13) == 0)
451 value
= wcstoul(p
, &endptr
, 10);
454 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/COUNTRYCODE");
459 /* FIXME: verify the country code */
461 pUserInfo
->usri4_country_code
= value
;
463 else if (_wcsnicmp(argv
[j
], L
"/expires:", 9) == 0)
466 if (_wcsicmp(p
, L
"never") == 0)
468 pUserInfo
->usri4_acct_expires
= TIMEQ_FOREVER
;
472 /* FIXME: Parse the date */
473 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/EXPIRES");
476 else if (_wcsnicmp(argv
[j
], L
"/fullname:", 10) == 0)
478 pUserInfo
->usri4_full_name
= &argv
[j
][10];
480 else if (_wcsnicmp(argv
[j
], L
"/homedir:", 9) == 0)
482 pUserInfo
->usri4_home_dir
= &argv
[j
][9];
484 else if (_wcsnicmp(argv
[j
], L
"/passwordchg:", 13) == 0)
487 if (_wcsicmp(p
, L
"yes") == 0)
489 pUserInfo
->usri4_flags
&= ~UF_PASSWD_CANT_CHANGE
;
491 else if (_wcsicmp(p
, L
"no") == 0)
493 pUserInfo
->usri4_flags
|= UF_PASSWD_CANT_CHANGE
;
497 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/PASSWORDCHG");
502 else if (_wcsnicmp(argv
[j
], L
"/passwordreq:", 13) == 0)
505 if (_wcsicmp(p
, L
"yes") == 0)
507 pUserInfo
->usri4_flags
&= ~UF_PASSWD_NOTREQD
;
509 else if (_wcsicmp(p
, L
"no") == 0)
511 pUserInfo
->usri4_flags
|= UF_PASSWD_NOTREQD
;
515 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"/PASSWORDREQ");
520 else if (_wcsnicmp(argv
[j
], L
"/profilepath:", 13) == 0)
522 pUserInfo
->usri4_profile
= &argv
[j
][13];
524 else if (_wcsnicmp(argv
[j
], L
"/scriptpath:", 12) == 0)
526 pUserInfo
->usri4_script_path
= &argv
[j
][12];
528 else if (_wcsnicmp(argv
[j
], L
"/times:", 7) == 0)
531 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/TIMES");
533 else if (_wcsnicmp(argv
[j
], L
"/usercomment:", 13) == 0)
535 pUserInfo
->usri4_usr_comment
= &argv
[j
][13];
537 else if (_wcsnicmp(argv
[j
], L
"/workstations:", 14) == 0)
540 PrintResourceString(IDS_ERROR_OPTION_NOT_SUPPORTED
, L
"/WORKSTATIONS");
544 if (!bAdd
&& !bDelete
)
546 /* Modify the user */
547 Status
= NetUserSetInfo(NULL
,
552 printf("Status: %lu\n", Status
);
554 else if (bAdd
&& !bDelete
)
557 Status
= NetUserAdd(NULL
,
561 printf("Status: %lu\n", Status
);
563 else if (!bAdd
&& bDelete
)
565 /* Delete the user */
566 Status
= NetUserDel(NULL
,
568 printf("Status: %lu\n", Status
);
572 if (bPasswordAllocated
== TRUE
&& lpPassword
!= NULL
)
573 HeapFree(GetProcessHeap(), 0, lpPassword
);
575 if (!bAdd
&& !bDelete
&& pUserInfo
!= NULL
)
576 NetApiBufferFree(pUserInfo
);
579 PrintResourceString(IDS_USER_SYNTAX
);