2 * Copyright 2002 Andriy Palamarchuk
4 * netapi32 user functions
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_NO_STATUS
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(netapi32
);
40 /* NOTE: So far, this is implemented to support tests that require user logins,
41 * but not designed to handle real user databases. Those should probably
42 * be synced with either the host's user database or with Samba.
44 * FIXME: The user database should hold all the information the USER_INFO_4 struct
45 * needs, but for the first try, I will just implement the USER_INFO_1 fields.
51 WCHAR user_name
[LM20_UNLEN
+1];
52 WCHAR user_password
[PWLEN
+ 1];
53 DWORD sec_since_passwd_change
;
58 LPWSTR user_logon_script_path
;
61 static struct list user_list
= LIST_INIT( user_list
);
63 BOOL
NETAPI_IsLocalComputer(LPCWSTR ServerName
);
65 /************************************************************
66 * NETAPI_ValidateServername
68 * Validates server name
70 static NET_API_STATUS
NETAPI_ValidateServername(LPCWSTR ServerName
)
74 if (ServerName
[0] == 0)
75 return ERROR_BAD_NETPATH
;
77 ((ServerName
[0] == '\\') &&
78 (ServerName
[1] != '\\'))
80 ((ServerName
[0] == '\\') &&
81 (ServerName
[1] == '\\') &&
84 return ERROR_INVALID_NAME
;
89 /************************************************************
92 * Looks for a user in the user database.
93 * Returns a pointer to the entry in the user list when the user
94 * is found, NULL otherwise.
96 static struct sam_user
* NETAPI_FindUser(LPCWSTR UserName
)
98 struct sam_user
*user
;
100 LIST_FOR_EACH_ENTRY(user
, &user_list
, struct sam_user
, entry
)
102 if(lstrcmpW(user
->user_name
, UserName
) == 0)
108 static BOOL
NETAPI_IsCurrentUser(LPCWSTR username
)
110 LPWSTR curr_user
= NULL
;
114 dwSize
= LM20_UNLEN
+1;
115 curr_user
= HeapAlloc(GetProcessHeap(), 0, dwSize
* sizeof(WCHAR
));
118 ERR("Failed to allocate memory for user name.\n");
121 if(!GetUserNameW(curr_user
, &dwSize
))
123 ERR("Failed to get current user's user name.\n");
126 if (!lstrcmpW(curr_user
, username
))
132 HeapFree(GetProcessHeap(), 0, curr_user
);
136 /************************************************************
137 * NetUserAdd (NETAPI32.@)
141 NetUserAdd(LPCWSTR servername
,
146 NET_API_STATUS status
;
147 struct sam_user
* su
= NULL
;
149 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_w(servername
), level
, bufptr
, parm_err
);
151 if((status
= NETAPI_ValidateServername(servername
)) != NERR_Success
)
156 /* Level 3 and 4 are identical for the purposes of NetUserAdd */
159 FIXME("Level 3 and 4 not implemented.\n");
162 FIXME("Level 2 not implemented.\n");
166 PUSER_INFO_1 ui
= (PUSER_INFO_1
) bufptr
;
167 su
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct sam_user
));
170 status
= NERR_InternalError
;
174 if(lstrlenW(ui
->usri1_name
) > LM20_UNLEN
)
176 status
= NERR_BadUsername
;
180 /*FIXME: do other checks for a valid username */
181 lstrcpyW(su
->user_name
, ui
->usri1_name
);
183 if(lstrlenW(ui
->usri1_password
) > PWLEN
)
185 /* Always return PasswordTooShort on invalid passwords. */
186 status
= NERR_PasswordTooShort
;
189 lstrcpyW(su
->user_password
, ui
->usri1_password
);
191 su
->sec_since_passwd_change
= ui
->usri1_password_age
;
192 su
->user_priv
= ui
->usri1_priv
;
193 su
->user_flags
= ui
->usri1_flags
;
195 /*FIXME: set the other LPWSTRs to NULL for now */
197 su
->user_comment
= NULL
;
198 su
->user_logon_script_path
= NULL
;
200 list_add_head(&user_list
, &su
->entry
);
204 TRACE("Invalid level %d specified.\n", level
);
205 status
= ERROR_INVALID_LEVEL
;
209 HeapFree(GetProcessHeap(), 0, su
);
215 /******************************************************************************
216 * NetUserChangePassword (NETAPI32.@)
218 * domainname [I] Optional. Domain on which the user resides or the logon
219 * domain of the current user if NULL.
220 * username [I] Optional. Username to change the password for or the name
221 * of the current user if NULL.
222 * oldpassword [I] The user's current password.
223 * newpassword [I] The password that the user will be changed to using.
226 * Success: NERR_Success.
227 * Failure: NERR_* failure code or win error code.
232 NetUserChangePassword(LPCWSTR domainname
,
237 struct sam_user
*user
;
239 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname
), debugstr_w(username
));
242 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname
));
244 if((user
= NETAPI_FindUser(username
)) == NULL
)
245 return NERR_UserNotFound
;
247 if(lstrcmpW(user
->user_password
, oldpassword
) != 0)
248 return ERROR_INVALID_PASSWORD
;
250 if(lstrlenW(newpassword
) > PWLEN
)
251 return ERROR_PASSWORD_RESTRICTION
;
253 lstrcpyW(user
->user_password
, newpassword
);
259 /************************************************************
260 * NetUserDel (NETAPI32.@)
264 NetUserDel(LPCWSTR servername
,
267 NET_API_STATUS status
;
268 struct sam_user
*user
;
270 TRACE("(%s, %s)\n", debugstr_w(servername
), debugstr_w(username
));
272 if((status
= NETAPI_ValidateServername(servername
))!= NERR_Success
)
275 if ((user
= NETAPI_FindUser(username
)) == NULL
)
276 return NERR_UserNotFound
;
278 list_remove(&user
->entry
);
280 HeapFree(GetProcessHeap(), 0, user
->home_dir
);
281 HeapFree(GetProcessHeap(), 0, user
->user_comment
);
282 HeapFree(GetProcessHeap(), 0, user
->user_logon_script_path
);
283 HeapFree(GetProcessHeap(), 0, user
);
289 /************************************************************
290 * NetUserEnum (NETAPI32.@)
294 NetUserEnum(LPCWSTR servername
,
300 LPDWORD totalentries
,
301 LPDWORD resume_handle
)
303 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername
), level
,
304 filter
, bufptr
, prefmaxlen
, entriesread
, totalentries
, resume_handle
);
306 return ERROR_ACCESS_DENIED
;
310 /************************************************************
311 * NetUserGetGroups (NETAPI32.@)
315 NetUserGetGroups(LPCWSTR servername
,
321 LPDWORD totalentries
)
323 FIXME("%s %s %d %p %d %p %p stub\n", debugstr_w(servername
),
324 debugstr_w(username
), level
, bufptr
, prefixmaxlen
, entriesread
,
331 return ERROR_INVALID_LEVEL
;
335 /************************************************************
336 * NetUserGetInfo (NETAPI32.@)
340 NetUserGetInfo(LPCWSTR servername
,
345 NET_API_STATUS status
;
346 TRACE("(%s, %s, %d, %p)\n", debugstr_w(servername
), debugstr_w(username
),
348 status
= NETAPI_ValidateServername(servername
);
349 if (status
!= NERR_Success
)
352 if(!NETAPI_IsLocalComputer(servername
))
354 FIXME("Only implemented for local computer, but remote server"
355 "%s was requested.\n", debugstr_w(servername
));
356 return NERR_InvalidComputer
;
359 if(!NETAPI_FindUser(username
) && !NETAPI_IsCurrentUser(username
))
361 TRACE("User %s is unknown.\n", debugstr_w(username
));
362 return NERR_UserNotFound
;
372 name_sz
= lstrlenW(username
) + 1;
375 NetApiBufferAllocate(sizeof(USER_INFO_0
) + name_sz
* sizeof(WCHAR
),
378 ui
= (PUSER_INFO_0
) *bufptr
;
379 ui
->usri0_name
= (LPWSTR
) (*bufptr
+ sizeof(USER_INFO_0
));
382 lstrcpyW(ui
->usri0_name
, username
);
390 NET_API_STATUS status
;
391 /* sizes of the field buffers in WCHARS */
392 int name_sz
, comment_sz
, usr_comment_sz
, full_name_sz
;
399 status
= NetUserGetInfo(servername
, username
, 0, (LPBYTE
*) &ui0
);
400 if (status
!= NERR_Success
)
402 NetApiBufferFree(ui0
);
405 name_sz
= lstrlenW(ui0
->usri0_name
) + 1;
408 NetApiBufferAllocate(sizeof(USER_INFO_10
) +
409 (name_sz
+ comment_sz
+ usr_comment_sz
+
410 full_name_sz
) * sizeof(WCHAR
),
412 ui
= (PUSER_INFO_10
) *bufptr
;
413 ui
->usri10_name
= (LPWSTR
) (*bufptr
+ sizeof(USER_INFO_10
));
414 ui
->usri10_comment
= (LPWSTR
) (
415 ((PBYTE
) ui
->usri10_name
) + name_sz
* sizeof(WCHAR
));
416 ui
->usri10_usr_comment
= (LPWSTR
) (
417 ((PBYTE
) ui
->usri10_comment
) + comment_sz
* sizeof(WCHAR
));
418 ui
->usri10_full_name
= (LPWSTR
) (
419 ((PBYTE
) ui
->usri10_usr_comment
) + usr_comment_sz
* sizeof(WCHAR
));
422 lstrcpyW(ui
->usri10_name
, ui0
->usri0_name
);
423 NetApiBufferFree(ui0
);
424 ui
->usri10_comment
[0] = 0;
425 ui
->usri10_usr_comment
[0] = 0;
426 ui
->usri10_full_name
[0] = 0;
432 static const WCHAR homedirW
[] = {'H','O','M','E',0};
435 NET_API_STATUS status
;
436 /* sizes of the field buffers in WCHARS */
437 int name_sz
, password_sz
, home_dir_sz
, comment_sz
, script_path_sz
;
439 password_sz
= 1; /* not filled out for security reasons for NetUserGetInfo*/
444 status
= NetUserGetInfo(servername
, username
, 0, (LPBYTE
*) &ui0
);
445 if (status
!= NERR_Success
)
447 NetApiBufferFree(ui0
);
450 name_sz
= lstrlenW(ui0
->usri0_name
) + 1;
451 home_dir_sz
= GetEnvironmentVariableW(homedirW
, NULL
,0);
453 NetApiBufferAllocate(sizeof(USER_INFO_1
) +
454 (name_sz
+ password_sz
+ home_dir_sz
+
455 comment_sz
+ script_path_sz
) * sizeof(WCHAR
),
458 ui
= (PUSER_INFO_1
) *bufptr
;
459 ui
->usri1_name
= (LPWSTR
) (ui
+ 1);
460 ui
->usri1_password
= ui
->usri1_name
+ name_sz
;
461 ui
->usri1_home_dir
= ui
->usri1_password
+ password_sz
;
462 ui
->usri1_comment
= ui
->usri1_home_dir
+ home_dir_sz
;
463 ui
->usri1_script_path
= ui
->usri1_comment
+ comment_sz
;
465 lstrcpyW(ui
->usri1_name
, ui0
->usri0_name
);
466 NetApiBufferFree(ui0
);
467 ui
->usri1_password
[0] = 0;
468 ui
->usri1_password_age
= 0;
470 GetEnvironmentVariableW(homedirW
, ui
->usri1_home_dir
,home_dir_sz
);
471 ui
->usri1_comment
[0] = 0;
473 ui
->usri1_script_path
[0] = 0;
503 FIXME("Level %d is not implemented\n", level
);
504 return NERR_InternalError
;
507 TRACE("Invalid level %d is specified\n", level
);
508 return ERROR_INVALID_LEVEL
;
514 /************************************************************
515 * NetUserGetLocalGroups (NETAPI32.@)
519 NetUserGetLocalGroups(LPCWSTR servername
,
526 LPDWORD totalentries
)
528 NET_API_STATUS status
;
529 const WCHAR admins
[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r','s',0};
531 LOCALGROUP_USERS_INFO_0
* info
;
534 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
535 debugstr_w(servername
), debugstr_w(username
), level
, flags
, bufptr
,
536 prefmaxlen
, entriesread
, totalentries
);
538 status
= NETAPI_ValidateServername(servername
);
539 if (status
!= NERR_Success
)
543 NetApiBufferAllocate(size
* sizeof(WCHAR
), (LPVOID
*)¤tuser
);
544 GetUserNameW(currentuser
, &size
);
546 if (lstrcmpiW(username
, currentuser
) && NETAPI_FindUser(username
))
548 NetApiBufferFree(currentuser
);
549 return NERR_UserNotFound
;
552 NetApiBufferFree(currentuser
);
554 size
= sizeof(*info
) + sizeof(admins
);
556 if(prefmaxlen
< size
)
557 status
= ERROR_MORE_DATA
;
559 status
= NetApiBufferAllocate(size
, (LPVOID
*)&info
);
561 if(status
!= NERR_Success
)
568 info
->lgrui0_name
= (LPWSTR
)((LPBYTE
)info
+ sizeof(*info
));
569 lstrcpyW(info
->lgrui0_name
, admins
);
571 *bufptr
= (LPBYTE
)info
;
578 /******************************************************************************
579 * NetUserSetGroups (NETAPI32.@)
583 NetUserSetGroups(LPCWSTR servername
,
589 FIXME("(%s %s %lu %p %lu)\n",
590 debugstr_w(servername
), debugstr_w(username
), level
, buf
, num_entries
);
591 return ERROR_ACCESS_DENIED
;
596 /******************************************************************************
597 * NetUserSetInfo (NETAPI32.@)
601 NetUserSetInfo(LPCWSTR servername
,
607 FIXME("(%s %s %lu %p %p)\n",
608 debugstr_w(servername
), debugstr_w(username
), level
, buf
, parm_err
);
609 return ERROR_ACCESS_DENIED
;