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