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