winecoreaudio: Fix a leak.
[wine/multimedia.git] / dlls / netapi32 / access.c
blob7cd05fcbbeaea3e57df335bdbaf03de596231276
1 /*
2 * Copyright 2002 Andriy Palamarchuk
4 * netapi32 access 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
21 #include "config.h"
22 #include <stdarg.h>
23 #include <fcntl.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "lmcons.h"
34 #include "lmaccess.h"
35 #include "lmapibuf.h"
36 #include "lmerr.h"
37 #include "lmuse.h"
38 #include "ntsecapi.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
45 /* NOTE: So far, this is implemented to support tests that require user logins,
46 * but not designed to handle real user databases. Those should probably
47 * be synced with either the host's user database or with Samba.
49 * FIXME: The user database should hold all the information the USER_INFO_4 struct
50 * needs, but for the first try, I will just implement the USER_INFO_1 fields.
53 struct sam_user
55 struct list entry;
56 WCHAR user_name[LM20_UNLEN+1];
57 WCHAR user_password[PWLEN + 1];
58 DWORD sec_since_passwd_change;
59 DWORD user_priv;
60 LPWSTR home_dir;
61 LPWSTR user_comment;
62 DWORD user_flags;
63 LPWSTR user_logon_script_path;
66 static struct list user_list = LIST_INIT( user_list );
68 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName);
70 /************************************************************
71 * NETAPI_ValidateServername
73 * Validates server name
75 static NET_API_STATUS NETAPI_ValidateServername(LPCWSTR ServerName)
77 if (ServerName)
79 if (ServerName[0] == 0)
80 return ERROR_BAD_NETPATH;
81 else if (
82 ((ServerName[0] == '\\') &&
83 (ServerName[1] != '\\'))
85 ((ServerName[0] == '\\') &&
86 (ServerName[1] == '\\') &&
87 (ServerName[2] == 0))
89 return ERROR_INVALID_NAME;
91 return NERR_Success;
94 /************************************************************
95 * NETAPI_FindUser
97 * Looks for a user in the user database.
98 * Returns a pointer to the entry in the user list when the user
99 * is found, NULL otherwise.
101 static struct sam_user* NETAPI_FindUser(LPCWSTR UserName)
103 struct sam_user *user;
105 LIST_FOR_EACH_ENTRY(user, &user_list, struct sam_user, entry)
107 if(lstrcmpW(user->user_name, UserName) == 0)
108 return user;
110 return NULL;
113 static BOOL NETAPI_IsCurrentUser(LPCWSTR username)
115 LPWSTR curr_user = NULL;
116 DWORD dwSize;
117 BOOL ret = FALSE;
119 dwSize = LM20_UNLEN+1;
120 curr_user = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
121 if(!curr_user)
123 ERR("Failed to allocate memory for user name.\n");
124 goto end;
126 if(!GetUserNameW(curr_user, &dwSize))
128 ERR("Failed to get current user's user name.\n");
129 goto end;
131 if (!lstrcmpW(curr_user, username))
133 ret = TRUE;
136 end:
137 HeapFree(GetProcessHeap(), 0, curr_user);
138 return ret;
141 /************************************************************
142 * NetUserAdd (NETAPI32.@)
144 NET_API_STATUS WINAPI NetUserAdd(LPCWSTR servername,
145 DWORD level, LPBYTE bufptr, LPDWORD parm_err)
147 NET_API_STATUS status;
148 struct sam_user * su = NULL;
150 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_w(servername), level, bufptr, parm_err);
152 if((status = NETAPI_ValidateServername(servername)) != NERR_Success)
153 return status;
155 switch(level)
157 /* Level 3 and 4 are identical for the purposes of NetUserAdd */
158 case 4:
159 case 3:
160 FIXME("Level 3 and 4 not implemented.\n");
161 /* Fall through */
162 case 2:
163 FIXME("Level 2 not implemented.\n");
164 /* Fall through */
165 case 1:
167 PUSER_INFO_1 ui = (PUSER_INFO_1) bufptr;
168 su = HeapAlloc(GetProcessHeap(), 0, sizeof(struct sam_user));
169 if(!su)
171 status = NERR_InternalError;
172 break;
175 if(lstrlenW(ui->usri1_name) > LM20_UNLEN)
177 status = NERR_BadUsername;
178 break;
181 /*FIXME: do other checks for a valid username */
182 lstrcpyW(su->user_name, ui->usri1_name);
184 if(lstrlenW(ui->usri1_password) > PWLEN)
186 /* Always return PasswordTooShort on invalid passwords. */
187 status = NERR_PasswordTooShort;
188 break;
190 lstrcpyW(su->user_password, ui->usri1_password);
192 su->sec_since_passwd_change = ui->usri1_password_age;
193 su->user_priv = ui->usri1_priv;
194 su->user_flags = ui->usri1_flags;
196 /*FIXME: set the other LPWSTRs to NULL for now */
197 su->home_dir = NULL;
198 su->user_comment = NULL;
199 su->user_logon_script_path = NULL;
201 list_add_head(&user_list, &su->entry);
202 return NERR_Success;
204 default:
205 TRACE("Invalid level %d specified.\n", level);
206 status = ERROR_INVALID_LEVEL;
207 break;
210 HeapFree(GetProcessHeap(), 0, su);
212 return status;
215 /************************************************************
216 * NetUserDel (NETAPI32.@)
218 NET_API_STATUS WINAPI NetUserDel(LPCWSTR servername, LPCWSTR username)
220 NET_API_STATUS status;
221 struct sam_user *user;
223 TRACE("(%s, %s)\n", debugstr_w(servername), debugstr_w(username));
225 if((status = NETAPI_ValidateServername(servername))!= NERR_Success)
226 return status;
228 if ((user = NETAPI_FindUser(username)) == NULL)
229 return NERR_UserNotFound;
231 list_remove(&user->entry);
233 HeapFree(GetProcessHeap(), 0, user->home_dir);
234 HeapFree(GetProcessHeap(), 0, user->user_comment);
235 HeapFree(GetProcessHeap(), 0, user->user_logon_script_path);
236 HeapFree(GetProcessHeap(), 0, user);
238 return NERR_Success;
241 /************************************************************
242 * NetUserGetInfo (NETAPI32.@)
244 NET_API_STATUS WINAPI
245 NetUserGetInfo(LPCWSTR servername, LPCWSTR username, DWORD level,
246 LPBYTE* bufptr)
248 NET_API_STATUS status;
249 TRACE("(%s, %s, %d, %p)\n", debugstr_w(servername), debugstr_w(username),
250 level, bufptr);
251 status = NETAPI_ValidateServername(servername);
252 if (status != NERR_Success)
253 return status;
255 if(!NETAPI_IsLocalComputer(servername))
257 FIXME("Only implemented for local computer, but remote server"
258 "%s was requested.\n", debugstr_w(servername));
259 return NERR_InvalidComputer;
262 if(!NETAPI_FindUser(username) && !NETAPI_IsCurrentUser(username))
264 TRACE("User %s is unknown.\n", debugstr_w(username));
265 return NERR_UserNotFound;
268 switch (level)
270 case 0:
272 PUSER_INFO_0 ui;
273 int name_sz;
275 name_sz = lstrlenW(username) + 1;
277 /* set up buffer */
278 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
279 (LPVOID *) bufptr);
281 ui = (PUSER_INFO_0) *bufptr;
282 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
284 /* get data */
285 lstrcpyW(ui->usri0_name, username);
286 break;
289 case 10:
291 PUSER_INFO_10 ui;
292 PUSER_INFO_0 ui0;
293 NET_API_STATUS status;
294 /* sizes of the field buffers in WCHARS */
295 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
297 comment_sz = 1;
298 usr_comment_sz = 1;
299 full_name_sz = 1;
301 /* get data */
302 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
303 if (status != NERR_Success)
305 NetApiBufferFree(ui0);
306 return status;
308 name_sz = lstrlenW(ui0->usri0_name) + 1;
310 /* set up buffer */
311 NetApiBufferAllocate(sizeof(USER_INFO_10) +
312 (name_sz + comment_sz + usr_comment_sz +
313 full_name_sz) * sizeof(WCHAR),
314 (LPVOID *) bufptr);
315 ui = (PUSER_INFO_10) *bufptr;
316 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
317 ui->usri10_comment = (LPWSTR) (
318 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
319 ui->usri10_usr_comment = (LPWSTR) (
320 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
321 ui->usri10_full_name = (LPWSTR) (
322 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
324 /* set data */
325 lstrcpyW(ui->usri10_name, ui0->usri0_name);
326 NetApiBufferFree(ui0);
327 ui->usri10_comment[0] = 0;
328 ui->usri10_usr_comment[0] = 0;
329 ui->usri10_full_name[0] = 0;
330 break;
333 case 1:
335 static const WCHAR homedirW[] = {'H','O','M','E',0};
336 PUSER_INFO_1 ui;
337 PUSER_INFO_0 ui0;
338 NET_API_STATUS status;
339 /* sizes of the field buffers in WCHARS */
340 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
342 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
343 comment_sz = 1;
344 script_path_sz = 1;
346 /* get data */
347 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
348 if (status != NERR_Success)
350 NetApiBufferFree(ui0);
351 return status;
353 name_sz = lstrlenW(ui0->usri0_name) + 1;
354 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
355 /* set up buffer */
356 NetApiBufferAllocate(sizeof(USER_INFO_1) +
357 (name_sz + password_sz + home_dir_sz +
358 comment_sz + script_path_sz) * sizeof(WCHAR),
359 (LPVOID *) bufptr);
361 ui = (PUSER_INFO_1) *bufptr;
362 ui->usri1_name = (LPWSTR) (ui + 1);
363 ui->usri1_password = ui->usri1_name + name_sz;
364 ui->usri1_home_dir = ui->usri1_password + password_sz;
365 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
366 ui->usri1_script_path = ui->usri1_comment + comment_sz;
367 /* set data */
368 lstrcpyW(ui->usri1_name, ui0->usri0_name);
369 NetApiBufferFree(ui0);
370 ui->usri1_password[0] = 0;
371 ui->usri1_password_age = 0;
372 ui->usri1_priv = 0;
373 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
374 ui->usri1_comment[0] = 0;
375 ui->usri1_flags = 0;
376 ui->usri1_script_path[0] = 0;
377 break;
379 case 2:
380 case 3:
381 case 4:
382 case 11:
383 case 20:
384 case 23:
385 case 1003:
386 case 1005:
387 case 1006:
388 case 1007:
389 case 1008:
390 case 1009:
391 case 1010:
392 case 1011:
393 case 1012:
394 case 1013:
395 case 1014:
396 case 1017:
397 case 1018:
398 case 1020:
399 case 1023:
400 case 1024:
401 case 1025:
402 case 1051:
403 case 1052:
404 case 1053:
406 FIXME("Level %d is not implemented\n", level);
407 return NERR_InternalError;
409 default:
410 TRACE("Invalid level %d is specified\n", level);
411 return ERROR_INVALID_LEVEL;
413 return NERR_Success;
416 /************************************************************
417 * NetUserGetLocalGroups (NETAPI32.@)
419 NET_API_STATUS WINAPI
420 NetUserGetLocalGroups(LPCWSTR servername, LPCWSTR username, DWORD level,
421 DWORD flags, LPBYTE* bufptr, DWORD prefmaxlen,
422 LPDWORD entriesread, LPDWORD totalentries)
424 NET_API_STATUS status;
425 const WCHAR admins[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r','s',0};
426 LPWSTR currentuser;
427 LOCALGROUP_USERS_INFO_0* info;
428 DWORD size;
430 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
431 debugstr_w(servername), debugstr_w(username), level, flags, bufptr,
432 prefmaxlen, entriesread, totalentries);
434 status = NETAPI_ValidateServername(servername);
435 if (status != NERR_Success)
436 return status;
438 size = UNLEN + 1;
439 NetApiBufferAllocate(size * sizeof(WCHAR), (LPVOID*)&currentuser);
440 if (!GetUserNameW(currentuser, &size)) {
441 NetApiBufferFree(currentuser);
442 return ERROR_NOT_ENOUGH_MEMORY;
445 if (lstrcmpiW(username, currentuser) && NETAPI_FindUser(username))
447 NetApiBufferFree(currentuser);
448 return NERR_UserNotFound;
451 NetApiBufferFree(currentuser);
452 *totalentries = 1;
453 size = sizeof(*info) + sizeof(admins);
455 if(prefmaxlen < size)
456 status = ERROR_MORE_DATA;
457 else
458 status = NetApiBufferAllocate(size, (LPVOID*)&info);
460 if(status != NERR_Success)
462 *bufptr = NULL;
463 *entriesread = 0;
464 return status;
467 info->lgrui0_name = (LPWSTR)((LPBYTE)info + sizeof(*info));
468 lstrcpyW(info->lgrui0_name, admins);
470 *bufptr = (LPBYTE)info;
471 *entriesread = 1;
473 return NERR_Success;
476 /************************************************************
477 * NetUserEnum (NETAPI32.@)
479 NET_API_STATUS WINAPI
480 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
481 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
482 LPDWORD resume_handle)
484 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername), level,
485 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
487 return ERROR_ACCESS_DENIED;
490 /************************************************************
491 * ACCESS_QueryAdminDisplayInformation
493 * Creates a buffer with information for the Admin User
495 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
497 static const WCHAR sAdminUserName[] = {
498 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
500 /* sizes of the field buffers in WCHARS */
501 int name_sz, comment_sz, full_name_sz;
502 PNET_DISPLAY_USER usr;
504 /* set up buffer */
505 name_sz = lstrlenW(sAdminUserName) + 1;
506 comment_sz = 1;
507 full_name_sz = 1;
509 *pdwSize = sizeof(NET_DISPLAY_USER);
510 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
511 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
513 usr = *buf;
514 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
515 usr->usri1_comment = (LPWSTR) (
516 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
517 usr->usri1_full_name = (LPWSTR) (
518 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
520 /* set data */
521 lstrcpyW(usr->usri1_name, sAdminUserName);
522 usr->usri1_comment[0] = 0;
523 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
524 usr->usri1_full_name[0] = 0;
525 usr->usri1_user_id = DOMAIN_USER_RID_ADMIN;
526 usr->usri1_next_index = 0;
529 /************************************************************
530 * ACCESS_QueryGuestDisplayInformation
532 * Creates a buffer with information for the Guest User
534 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
536 static const WCHAR sGuestUserName[] = {
537 'G','u','e','s','t',0 };
539 /* sizes of the field buffers in WCHARS */
540 int name_sz, comment_sz, full_name_sz;
541 PNET_DISPLAY_USER usr;
543 /* set up buffer */
544 name_sz = lstrlenW(sGuestUserName) + 1;
545 comment_sz = 1;
546 full_name_sz = 1;
548 *pdwSize = sizeof(NET_DISPLAY_USER);
549 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
550 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
552 usr = *buf;
553 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
554 usr->usri1_comment = (LPWSTR) (
555 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
556 usr->usri1_full_name = (LPWSTR) (
557 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
559 /* set data */
560 lstrcpyW(usr->usri1_name, sGuestUserName);
561 usr->usri1_comment[0] = 0;
562 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
563 UF_DONT_EXPIRE_PASSWD;
564 usr->usri1_full_name[0] = 0;
565 usr->usri1_user_id = DOMAIN_USER_RID_GUEST;
566 usr->usri1_next_index = 0;
569 /************************************************************
570 * Copies NET_DISPLAY_USER record.
572 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER *dest, LPWSTR *dest_buf,
573 PNET_DISPLAY_USER src)
575 LPWSTR str = *dest_buf;
577 src->usri1_name = str;
578 lstrcpyW(src->usri1_name, dest->usri1_name);
579 str = (LPWSTR) (
580 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
582 src->usri1_comment = str;
583 lstrcpyW(src->usri1_comment, dest->usri1_comment);
584 str = (LPWSTR) (
585 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
587 src->usri1_flags = dest->usri1_flags;
589 src->usri1_full_name = str;
590 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
591 str = (LPWSTR) (
592 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
594 src->usri1_user_id = dest->usri1_user_id;
595 src->usri1_next_index = dest->usri1_next_index;
596 *dest_buf = str;
599 /************************************************************
600 * NetQueryDisplayInformation (NETAPI32.@)
602 * The buffer structure:
603 * - array of fixed size record of the level type
604 * - strings, referenced by the record of the level type
606 NET_API_STATUS WINAPI
607 NetQueryDisplayInformation(
608 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
609 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
610 PVOID *SortedBuffer)
612 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
613 Level, Index, EntriesRequested, PreferredMaximumLength,
614 ReturnedEntryCount, SortedBuffer);
616 if(!NETAPI_IsLocalComputer(ServerName))
618 FIXME("Only implemented on local computer, but requested for "
619 "remote server %s\n", debugstr_w(ServerName));
620 return ERROR_ACCESS_DENIED;
623 switch (Level)
625 case 1:
627 /* current record */
628 PNET_DISPLAY_USER inf;
629 /* current available strings buffer */
630 LPWSTR str;
631 PNET_DISPLAY_USER admin, guest;
632 DWORD admin_size, guest_size;
633 LPWSTR name = NULL;
634 DWORD dwSize;
636 /* sizes of the field buffers in WCHARS */
637 int name_sz, comment_sz, full_name_sz;
639 /* number of the records, returned in SortedBuffer
640 3 - for current user, Administrator and Guest users
642 int records = 3;
644 FIXME("Level %d partially implemented\n", Level);
645 *ReturnedEntryCount = records;
646 comment_sz = 1;
647 full_name_sz = 1;
649 /* get data */
650 dwSize = UNLEN + 1;
651 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &name);
652 if (!GetUserNameW(name, &dwSize))
654 NetApiBufferFree(name);
655 return ERROR_ACCESS_DENIED;
657 name_sz = dwSize;
658 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
659 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
661 /* set up buffer */
662 dwSize = sizeof(NET_DISPLAY_USER) * records;
663 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
665 NetApiBufferAllocate(dwSize +
666 admin_size - sizeof(NET_DISPLAY_USER) +
667 guest_size - sizeof(NET_DISPLAY_USER),
668 SortedBuffer);
669 inf = *SortedBuffer;
670 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
671 inf->usri1_name = str;
672 str = (LPWSTR) (
673 ((PBYTE) str) + name_sz * sizeof(WCHAR));
674 inf->usri1_comment = str;
675 str = (LPWSTR) (
676 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
677 inf->usri1_full_name = str;
678 str = (LPWSTR) (
679 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
681 /* set data */
682 lstrcpyW(inf->usri1_name, name);
683 NetApiBufferFree(name);
684 inf->usri1_comment[0] = 0;
685 inf->usri1_flags =
686 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
687 inf->usri1_full_name[0] = 0;
688 inf->usri1_user_id = 0;
689 inf->usri1_next_index = 0;
691 inf++;
692 ACCESS_CopyDisplayUser(admin, &str, inf);
693 NetApiBufferFree(admin);
695 inf++;
696 ACCESS_CopyDisplayUser(guest, &str, inf);
697 NetApiBufferFree(guest);
698 break;
701 case 2:
702 case 3:
704 FIXME("Level %d is not implemented\n", Level);
705 break;
708 default:
709 TRACE("Invalid level %d is specified\n", Level);
710 return ERROR_INVALID_LEVEL;
712 return NERR_Success;
715 /************************************************************
716 * NetGetDCName (NETAPI32.@)
718 * Return the name of the primary domain controller (PDC)
721 NET_API_STATUS WINAPI
722 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
724 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
725 debugstr_w(domainname), bufptr);
726 return NERR_DCNotFound; /* say we can't find a domain controller */
729 /************************************************************
730 * NetGroupEnum (NETAPI32.@)
733 NET_API_STATUS WINAPI
734 NetGroupEnum(LPCWSTR servername, DWORD level, LPBYTE *bufptr, DWORD prefmaxlen,
735 LPDWORD entriesread, LPDWORD totalentries, LPDWORD resume_handle)
737 FIXME("(%s, %d, %p, %d, %p, %p, %p) stub!\n", debugstr_w(servername),
738 level, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
739 return ERROR_ACCESS_DENIED;
742 /************************************************************
743 * NetGroupGetInfo (NETAPI32.@)
746 NET_API_STATUS WINAPI NetGroupGetInfo(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE *bufptr)
748 FIXME("(%s, %s, %d, %p) stub!\n", debugstr_w(servername), debugstr_w(groupname), level, bufptr);
749 return ERROR_ACCESS_DENIED;
752 /******************************************************************************
753 * NetUserModalsGet (NETAPI32.@)
755 * Retrieves global information for all users and global groups in the security
756 * database.
758 * PARAMS
759 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
760 * on which the function is to execute.
761 * level [I] Information level of the data.
762 * 0 Return global passwords parameters. bufptr points to a
763 * USER_MODALS_INFO_0 struct.
764 * 1 Return logon server and domain controller information. bufptr
765 * points to a USER_MODALS_INFO_1 struct.
766 * 2 Return domain name and identifier. bufptr points to a
767 * USER_MODALS_INFO_2 struct.
768 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
769 * struct.
770 * pbuffer [I] Buffer that receives the data.
772 * RETURNS
773 * Success: NERR_Success.
774 * Failure:
775 * ERROR_ACCESS_DENIED - the user does not have access to the info.
776 * NERR_InvalidComputer - computer name is invalid.
778 NET_API_STATUS WINAPI NetUserModalsGet(
779 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
781 TRACE("(%s %d %p)\n", debugstr_w(szServer), level, pbuffer);
783 switch (level)
785 case 0:
786 /* return global passwords parameters */
787 FIXME("level 0 not implemented!\n");
788 *pbuffer = NULL;
789 return NERR_InternalError;
790 case 1:
791 /* return logon server and domain controller info */
792 FIXME("level 1 not implemented!\n");
793 *pbuffer = NULL;
794 return NERR_InternalError;
795 case 2:
797 /* return domain name and identifier */
798 PUSER_MODALS_INFO_2 umi;
799 LSA_HANDLE policyHandle;
800 LSA_OBJECT_ATTRIBUTES objectAttributes;
801 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
802 NTSTATUS ntStatus;
803 PSID domainIdentifier = NULL;
804 int domainNameLen;
806 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
807 objectAttributes.Length = sizeof(objectAttributes);
809 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
810 POLICY_VIEW_LOCAL_INFORMATION,
811 &policyHandle);
812 if (ntStatus != STATUS_SUCCESS)
814 WARN("LsaOpenPolicy failed with NT status %x\n",
815 LsaNtStatusToWinError(ntStatus));
816 return ntStatus;
819 ntStatus = LsaQueryInformationPolicy(policyHandle,
820 PolicyAccountDomainInformation,
821 (PVOID *)&domainInfo);
822 if (ntStatus != STATUS_SUCCESS)
824 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
825 LsaNtStatusToWinError(ntStatus));
826 LsaClose(policyHandle);
827 return ntStatus;
830 domainIdentifier = domainInfo->DomainSid;
831 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
832 LsaClose(policyHandle);
834 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
835 GetLengthSid(domainIdentifier) +
836 domainNameLen * sizeof(WCHAR),
837 (LPVOID *)pbuffer);
839 if (ntStatus != NERR_Success)
841 WARN("NetApiBufferAllocate() failed\n");
842 LsaFreeMemory(domainInfo);
843 return ntStatus;
846 umi = (USER_MODALS_INFO_2 *) *pbuffer;
847 umi->usrmod2_domain_id = *pbuffer + sizeof(USER_MODALS_INFO_2);
848 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
849 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
851 lstrcpynW(umi->usrmod2_domain_name,
852 domainInfo->DomainName.Buffer,
853 domainNameLen);
854 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
855 domainIdentifier);
857 LsaFreeMemory(domainInfo);
859 break;
861 case 3:
862 /* return lockout information */
863 FIXME("level 3 not implemented!\n");
864 *pbuffer = NULL;
865 return NERR_InternalError;
866 default:
867 TRACE("Invalid level %d is specified\n", level);
868 *pbuffer = NULL;
869 return ERROR_INVALID_LEVEL;
872 return NERR_Success;
875 static int fork_smbpasswd( char * const argv[] )
877 #ifdef HAVE_FORK
878 int pipe_out[2];
880 if (pipe( pipe_out ) == -1) return -1;
881 fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
882 fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
884 switch (fork())
886 case -1:
887 close( pipe_out[0] );
888 close( pipe_out[1] );
889 return -1;
890 case 0:
891 dup2( pipe_out[0], 0 );
892 close( pipe_out[0] );
893 close( pipe_out[1] );
894 execvp( "smbpasswd", argv );
895 ERR( "can't execute smbpasswd, is it installed?\n" );
896 return -1;
897 default:
898 close( pipe_out[0] );
899 break;
901 return pipe_out[1];
902 #else
903 ERR( "no fork support on this platform\n" );
904 return -1;
905 #endif
908 static char *strdup_unixcp( const WCHAR *str )
910 char *ret;
911 int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
912 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
913 WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
914 return ret;
917 static NET_API_STATUS change_password_smb( LPCWSTR domainname, LPCWSTR username,
918 LPCWSTR oldpassword, LPCWSTR newpassword )
920 static char option_silent[] = "-s";
921 static char option_user[] = "-U";
922 static char option_remote[] = "-r";
923 static char smbpasswd[] = "smbpasswd";
924 int pipe_out;
925 char *server = NULL, *user, *argv[7], *old, *new;
927 if (domainname && !(server = strdup_unixcp( domainname ))) return ERROR_OUTOFMEMORY;
928 if (!(user = strdup_unixcp( username )))
930 HeapFree( GetProcessHeap(), 0, server );
931 return ERROR_OUTOFMEMORY;
933 argv[0] = smbpasswd;
934 argv[1] = option_silent;
935 argv[2] = option_user;
936 argv[3] = user;
937 if (server)
939 argv[4] = option_remote;
940 argv[5] = server;
941 argv[6] = NULL;
943 else argv[4] = NULL;
945 pipe_out = fork_smbpasswd( argv );
946 HeapFree( GetProcessHeap(), 0, server );
947 HeapFree( GetProcessHeap(), 0, user );
948 if (pipe_out == -1) return NERR_InternalError;
950 if (!(old = strdup_unixcp( oldpassword )))
952 close( pipe_out );
953 return ERROR_OUTOFMEMORY;
955 if (!(new = strdup_unixcp( newpassword )))
957 close( pipe_out );
958 HeapFree( GetProcessHeap(), 0, old );
959 return ERROR_OUTOFMEMORY;
961 write( pipe_out, old, strlen( old ) );
962 write( pipe_out, "\n", 1 );
963 write( pipe_out, new, strlen( new ) );
964 write( pipe_out, "\n", 1 );
965 write( pipe_out, new, strlen( new ) );
966 write( pipe_out, "\n", 1 );
968 close( pipe_out );
969 HeapFree( GetProcessHeap(), 0, old );
970 HeapFree( GetProcessHeap(), 0, new );
971 return NERR_Success;
974 /******************************************************************************
975 * NetUserChangePassword (NETAPI32.@)
976 * PARAMS
977 * domainname [I] Optional. Domain on which the user resides or the logon
978 * domain of the current user if NULL.
979 * username [I] Optional. Username to change the password for or the name
980 * of the current user if NULL.
981 * oldpassword [I] The user's current password.
982 * newpassword [I] The password that the user will be changed to using.
984 * RETURNS
985 * Success: NERR_Success.
986 * Failure: NERR_* failure code or win error code.
989 NET_API_STATUS WINAPI NetUserChangePassword(LPCWSTR domainname, LPCWSTR username,
990 LPCWSTR oldpassword, LPCWSTR newpassword)
992 struct sam_user *user;
994 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname), debugstr_w(username));
996 if (!change_password_smb( domainname, username, oldpassword, newpassword ))
997 return NERR_Success;
999 if(domainname)
1000 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname));
1002 if((user = NETAPI_FindUser(username)) == NULL)
1003 return NERR_UserNotFound;
1005 if(lstrcmpW(user->user_password, oldpassword) != 0)
1006 return ERROR_INVALID_PASSWORD;
1008 if(lstrlenW(newpassword) > PWLEN)
1009 return ERROR_PASSWORD_RESTRICTION;
1011 lstrcpyW(user->user_password, newpassword);
1013 return NERR_Success;
1016 NET_API_STATUS WINAPI NetUseAdd(LMSTR servername, DWORD level, LPBYTE bufptr, LPDWORD parm_err)
1018 FIXME("%s %d %p %p stub\n", debugstr_w(servername), level, bufptr, parm_err);
1019 return NERR_Success;
1022 NET_API_STATUS WINAPI NetUseDel(LMSTR servername, LMSTR usename, DWORD forcecond)
1024 FIXME("%s %s %d stub\n", debugstr_w(servername), debugstr_w(usename), forcecond);
1025 return NERR_Success;