wined3d: Make the surface parameter to d3dfmt_get_conv() const.
[wine/multimedia.git] / dlls / netapi32 / access.c
blob255e51b31562383546a8fffd501cea77784a66c5
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 GetUserNameW(currentuser, &size);
442 if (lstrcmpiW(username, currentuser) && NETAPI_FindUser(username))
444 NetApiBufferFree(currentuser);
445 return NERR_UserNotFound;
448 NetApiBufferFree(currentuser);
449 *totalentries = 1;
450 size = sizeof(*info) + sizeof(admins);
452 if(prefmaxlen < size)
453 status = ERROR_MORE_DATA;
454 else
455 status = NetApiBufferAllocate(size, (LPVOID*)&info);
457 if(status != NERR_Success)
459 *bufptr = NULL;
460 *entriesread = 0;
461 return status;
464 info->lgrui0_name = (LPWSTR)((LPBYTE)info + sizeof(*info));
465 lstrcpyW(info->lgrui0_name, admins);
467 *bufptr = (LPBYTE)info;
468 *entriesread = 1;
470 return NERR_Success;
473 /************************************************************
474 * NetUserEnum (NETAPI32.@)
476 NET_API_STATUS WINAPI
477 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
478 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
479 LPDWORD resume_handle)
481 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername), level,
482 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
484 return ERROR_ACCESS_DENIED;
487 /************************************************************
488 * ACCESS_QueryAdminDisplayInformation
490 * Creates a buffer with information for the Admin User
492 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
494 static const WCHAR sAdminUserName[] = {
495 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
497 /* sizes of the field buffers in WCHARS */
498 int name_sz, comment_sz, full_name_sz;
499 PNET_DISPLAY_USER usr;
501 /* set up buffer */
502 name_sz = lstrlenW(sAdminUserName) + 1;
503 comment_sz = 1;
504 full_name_sz = 1;
506 *pdwSize = sizeof(NET_DISPLAY_USER);
507 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
508 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
510 usr = *buf;
511 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
512 usr->usri1_comment = (LPWSTR) (
513 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
514 usr->usri1_full_name = (LPWSTR) (
515 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
517 /* set data */
518 lstrcpyW(usr->usri1_name, sAdminUserName);
519 usr->usri1_comment[0] = 0;
520 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
521 usr->usri1_full_name[0] = 0;
522 usr->usri1_user_id = DOMAIN_USER_RID_ADMIN;
523 usr->usri1_next_index = 0;
526 /************************************************************
527 * ACCESS_QueryGuestDisplayInformation
529 * Creates a buffer with information for the Guest User
531 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
533 static const WCHAR sGuestUserName[] = {
534 'G','u','e','s','t',0 };
536 /* sizes of the field buffers in WCHARS */
537 int name_sz, comment_sz, full_name_sz;
538 PNET_DISPLAY_USER usr;
540 /* set up buffer */
541 name_sz = lstrlenW(sGuestUserName) + 1;
542 comment_sz = 1;
543 full_name_sz = 1;
545 *pdwSize = sizeof(NET_DISPLAY_USER);
546 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
547 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
549 usr = *buf;
550 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
551 usr->usri1_comment = (LPWSTR) (
552 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
553 usr->usri1_full_name = (LPWSTR) (
554 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
556 /* set data */
557 lstrcpyW(usr->usri1_name, sGuestUserName);
558 usr->usri1_comment[0] = 0;
559 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
560 UF_DONT_EXPIRE_PASSWD;
561 usr->usri1_full_name[0] = 0;
562 usr->usri1_user_id = DOMAIN_USER_RID_GUEST;
563 usr->usri1_next_index = 0;
566 /************************************************************
567 * Copies NET_DISPLAY_USER record.
569 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER *dest, LPWSTR *dest_buf,
570 PNET_DISPLAY_USER src)
572 LPWSTR str = *dest_buf;
574 src->usri1_name = str;
575 lstrcpyW(src->usri1_name, dest->usri1_name);
576 str = (LPWSTR) (
577 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
579 src->usri1_comment = str;
580 lstrcpyW(src->usri1_comment, dest->usri1_comment);
581 str = (LPWSTR) (
582 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
584 src->usri1_flags = dest->usri1_flags;
586 src->usri1_full_name = str;
587 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
588 str = (LPWSTR) (
589 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
591 src->usri1_user_id = dest->usri1_user_id;
592 src->usri1_next_index = dest->usri1_next_index;
593 *dest_buf = str;
596 /************************************************************
597 * NetQueryDisplayInformation (NETAPI32.@)
599 * The buffer structure:
600 * - array of fixed size record of the level type
601 * - strings, referenced by the record of the level type
603 NET_API_STATUS WINAPI
604 NetQueryDisplayInformation(
605 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
606 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
607 PVOID *SortedBuffer)
609 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
610 Level, Index, EntriesRequested, PreferredMaximumLength,
611 ReturnedEntryCount, SortedBuffer);
613 if(!NETAPI_IsLocalComputer(ServerName))
615 FIXME("Only implemented on local computer, but requested for "
616 "remote server %s\n", debugstr_w(ServerName));
617 return ERROR_ACCESS_DENIED;
620 switch (Level)
622 case 1:
624 /* current record */
625 PNET_DISPLAY_USER inf;
626 /* current available strings buffer */
627 LPWSTR str;
628 PNET_DISPLAY_USER admin, guest;
629 DWORD admin_size, guest_size;
630 LPWSTR name = NULL;
631 DWORD dwSize;
633 /* sizes of the field buffers in WCHARS */
634 int name_sz, comment_sz, full_name_sz;
636 /* number of the records, returned in SortedBuffer
637 3 - for current user, Administrator and Guest users
639 int records = 3;
641 FIXME("Level %d partially implemented\n", Level);
642 *ReturnedEntryCount = records;
643 comment_sz = 1;
644 full_name_sz = 1;
646 /* get data */
647 dwSize = UNLEN + 1;
648 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &name);
649 if (!GetUserNameW(name, &dwSize))
651 NetApiBufferFree(name);
652 return ERROR_ACCESS_DENIED;
654 name_sz = dwSize;
655 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
656 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
658 /* set up buffer */
659 dwSize = sizeof(NET_DISPLAY_USER) * records;
660 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
662 NetApiBufferAllocate(dwSize +
663 admin_size - sizeof(NET_DISPLAY_USER) +
664 guest_size - sizeof(NET_DISPLAY_USER),
665 SortedBuffer);
666 inf = *SortedBuffer;
667 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
668 inf->usri1_name = str;
669 str = (LPWSTR) (
670 ((PBYTE) str) + name_sz * sizeof(WCHAR));
671 inf->usri1_comment = str;
672 str = (LPWSTR) (
673 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
674 inf->usri1_full_name = str;
675 str = (LPWSTR) (
676 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
678 /* set data */
679 lstrcpyW(inf->usri1_name, name);
680 NetApiBufferFree(name);
681 inf->usri1_comment[0] = 0;
682 inf->usri1_flags =
683 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
684 inf->usri1_full_name[0] = 0;
685 inf->usri1_user_id = 0;
686 inf->usri1_next_index = 0;
688 inf++;
689 ACCESS_CopyDisplayUser(admin, &str, inf);
690 NetApiBufferFree(admin);
692 inf++;
693 ACCESS_CopyDisplayUser(guest, &str, inf);
694 NetApiBufferFree(guest);
695 break;
698 case 2:
699 case 3:
701 FIXME("Level %d is not implemented\n", Level);
702 break;
705 default:
706 TRACE("Invalid level %d is specified\n", Level);
707 return ERROR_INVALID_LEVEL;
709 return NERR_Success;
712 /************************************************************
713 * NetGetDCName (NETAPI32.@)
715 * Return the name of the primary domain controller (PDC)
718 NET_API_STATUS WINAPI
719 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
721 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
722 debugstr_w(domainname), bufptr);
723 return NERR_DCNotFound; /* say we can't find a domain controller */
726 /************************************************************
727 * NetGroupEnum (NETAPI32.@)
730 NET_API_STATUS WINAPI
731 NetGroupEnum(LPCWSTR servername, DWORD level, LPBYTE *bufptr, DWORD prefmaxlen,
732 LPDWORD entriesread, LPDWORD totalentries, LPDWORD resume_handle)
734 FIXME("(%s, %d, %p, %d, %p, %p, %p) stub!\n", debugstr_w(servername),
735 level, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
736 return ERROR_ACCESS_DENIED;
739 /************************************************************
740 * NetGroupGetInfo (NETAPI32.@)
743 NET_API_STATUS WINAPI NetGroupGetInfo(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE *bufptr)
745 FIXME("(%s, %s, %d, %p) stub!\n", debugstr_w(servername), debugstr_w(groupname), level, bufptr);
746 return ERROR_ACCESS_DENIED;
749 /******************************************************************************
750 * NetUserModalsGet (NETAPI32.@)
752 * Retrieves global information for all users and global groups in the security
753 * database.
755 * PARAMS
756 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
757 * on which the function is to execute.
758 * level [I] Information level of the data.
759 * 0 Return global passwords parameters. bufptr points to a
760 * USER_MODALS_INFO_0 struct.
761 * 1 Return logon server and domain controller information. bufptr
762 * points to a USER_MODALS_INFO_1 struct.
763 * 2 Return domain name and identifier. bufptr points to a
764 * USER_MODALS_INFO_2 struct.
765 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
766 * struct.
767 * pbuffer [I] Buffer that receives the data.
769 * RETURNS
770 * Success: NERR_Success.
771 * Failure:
772 * ERROR_ACCESS_DENIED - the user does not have access to the info.
773 * NERR_InvalidComputer - computer name is invalid.
775 NET_API_STATUS WINAPI NetUserModalsGet(
776 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
778 TRACE("(%s %d %p)\n", debugstr_w(szServer), level, pbuffer);
780 switch (level)
782 case 0:
783 /* return global passwords parameters */
784 FIXME("level 0 not implemented!\n");
785 *pbuffer = NULL;
786 return NERR_InternalError;
787 case 1:
788 /* return logon server and domain controller info */
789 FIXME("level 1 not implemented!\n");
790 *pbuffer = NULL;
791 return NERR_InternalError;
792 case 2:
794 /* return domain name and identifier */
795 PUSER_MODALS_INFO_2 umi;
796 LSA_HANDLE policyHandle;
797 LSA_OBJECT_ATTRIBUTES objectAttributes;
798 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
799 NTSTATUS ntStatus;
800 PSID domainIdentifier = NULL;
801 int domainNameLen;
803 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
804 objectAttributes.Length = sizeof(objectAttributes);
806 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
807 POLICY_VIEW_LOCAL_INFORMATION,
808 &policyHandle);
809 if (ntStatus != STATUS_SUCCESS)
811 WARN("LsaOpenPolicy failed with NT status %x\n",
812 LsaNtStatusToWinError(ntStatus));
813 return ntStatus;
816 ntStatus = LsaQueryInformationPolicy(policyHandle,
817 PolicyAccountDomainInformation,
818 (PVOID *)&domainInfo);
819 if (ntStatus != STATUS_SUCCESS)
821 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
822 LsaNtStatusToWinError(ntStatus));
823 LsaClose(policyHandle);
824 return ntStatus;
827 domainIdentifier = domainInfo->DomainSid;
828 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
829 LsaClose(policyHandle);
831 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
832 GetLengthSid(domainIdentifier) +
833 domainNameLen * sizeof(WCHAR),
834 (LPVOID *)pbuffer);
836 if (ntStatus != NERR_Success)
838 WARN("NetApiBufferAllocate() failed\n");
839 LsaFreeMemory(domainInfo);
840 return ntStatus;
843 umi = (USER_MODALS_INFO_2 *) *pbuffer;
844 umi->usrmod2_domain_id = *pbuffer + sizeof(USER_MODALS_INFO_2);
845 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
846 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
848 lstrcpynW(umi->usrmod2_domain_name,
849 domainInfo->DomainName.Buffer,
850 domainNameLen);
851 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
852 domainIdentifier);
854 LsaFreeMemory(domainInfo);
856 break;
858 case 3:
859 /* return lockout information */
860 FIXME("level 3 not implemented!\n");
861 *pbuffer = NULL;
862 return NERR_InternalError;
863 default:
864 TRACE("Invalid level %d is specified\n", level);
865 *pbuffer = NULL;
866 return ERROR_INVALID_LEVEL;
869 return NERR_Success;
872 static int fork_smbpasswd( char * const argv[] )
874 #ifdef HAVE_FORK
875 int pipe_out[2];
877 if (pipe( pipe_out ) == -1) return -1;
878 fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
879 fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
881 switch (fork())
883 case -1:
884 close( pipe_out[0] );
885 close( pipe_out[1] );
886 return -1;
887 case 0:
888 dup2( pipe_out[0], 0 );
889 close( pipe_out[0] );
890 close( pipe_out[1] );
891 execvp( "smbpasswd", argv );
892 ERR( "can't execute smbpasswd, is it installed?\n" );
893 return -1;
894 default:
895 close( pipe_out[0] );
896 break;
898 return pipe_out[1];
899 #else
900 ERR( "no fork support on this platform\n" );
901 return -1;
902 #endif
905 static char *strdup_unixcp( const WCHAR *str )
907 char *ret;
908 int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
909 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
910 WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
911 return ret;
914 static NET_API_STATUS change_password_smb( LPCWSTR domainname, LPCWSTR username,
915 LPCWSTR oldpassword, LPCWSTR newpassword )
917 static char option_silent[] = "-s";
918 static char option_user[] = "-U";
919 static char option_remote[] = "-r";
920 static char smbpasswd[] = "smbpasswd";
921 int pipe_out;
922 char *server = NULL, *user, *argv[7], *old, *new;
924 if (domainname && !(server = strdup_unixcp( domainname ))) return ERROR_OUTOFMEMORY;
925 if (!(user = strdup_unixcp( username )))
927 HeapFree( GetProcessHeap(), 0, server );
928 return ERROR_OUTOFMEMORY;
930 argv[0] = smbpasswd;
931 argv[1] = option_silent;
932 argv[2] = option_user;
933 argv[3] = user;
934 if (server)
936 argv[4] = option_remote;
937 argv[5] = server;
938 argv[6] = NULL;
940 else argv[4] = NULL;
942 pipe_out = fork_smbpasswd( argv );
943 HeapFree( GetProcessHeap(), 0, server );
944 HeapFree( GetProcessHeap(), 0, user );
945 if (pipe_out == -1) return NERR_InternalError;
947 if (!(old = strdup_unixcp( oldpassword )))
949 close( pipe_out );
950 return ERROR_OUTOFMEMORY;
952 if (!(new = strdup_unixcp( newpassword )))
954 close( pipe_out );
955 HeapFree( GetProcessHeap(), 0, old );
956 return ERROR_OUTOFMEMORY;
958 write( pipe_out, old, strlen( old ) );
959 write( pipe_out, "\n", 1 );
960 write( pipe_out, new, strlen( new ) );
961 write( pipe_out, "\n", 1 );
962 write( pipe_out, new, strlen( new ) );
963 write( pipe_out, "\n", 1 );
965 close( pipe_out );
966 HeapFree( GetProcessHeap(), 0, old );
967 HeapFree( GetProcessHeap(), 0, new );
968 return NERR_Success;
971 /******************************************************************************
972 * NetUserChangePassword (NETAPI32.@)
973 * PARAMS
974 * domainname [I] Optional. Domain on which the user resides or the logon
975 * domain of the current user if NULL.
976 * username [I] Optional. Username to change the password for or the name
977 * of the current user if NULL.
978 * oldpassword [I] The user's current password.
979 * newpassword [I] The password that the user will be changed to using.
981 * RETURNS
982 * Success: NERR_Success.
983 * Failure: NERR_* failure code or win error code.
986 NET_API_STATUS WINAPI NetUserChangePassword(LPCWSTR domainname, LPCWSTR username,
987 LPCWSTR oldpassword, LPCWSTR newpassword)
989 struct sam_user *user;
991 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname), debugstr_w(username));
993 if (!change_password_smb( domainname, username, oldpassword, newpassword ))
994 return NERR_Success;
996 if(domainname)
997 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname));
999 if((user = NETAPI_FindUser(username)) == NULL)
1000 return NERR_UserNotFound;
1002 if(lstrcmpW(user->user_password, oldpassword) != 0)
1003 return ERROR_INVALID_PASSWORD;
1005 if(lstrlenW(newpassword) > PWLEN)
1006 return ERROR_PASSWORD_RESTRICTION;
1008 lstrcpyW(user->user_password, newpassword);
1010 return NERR_Success;
1013 NET_API_STATUS WINAPI NetUseAdd(LMSTR servername, DWORD level, LPBYTE bufptr, LPDWORD parm_err)
1015 FIXME("%s %d %p %p stub\n", debugstr_w(servername), level, bufptr, parm_err);
1016 return NERR_Success;
1019 NET_API_STATUS WINAPI NetUseDel(LMSTR servername, LMSTR usename, DWORD forcecond)
1021 FIXME("%s %s %d stub\n", debugstr_w(servername), debugstr_w(usename), forcecond);
1022 return NERR_Success;