7b70e2fd61fcbf62b29fde1f6d5e0dbff06f442a
[reactos.git] / reactos / dll / win32 / 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 "netapi32.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
24
25 /************************************************************
26 * ACCESS_QueryAdminDisplayInformation
27 *
28 * Creates a buffer with information for the Admin User
29 */
30 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
31 {
32 static const WCHAR sAdminUserName[] = {
33 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
34
35 /* sizes of the field buffers in WCHARS */
36 int name_sz, comment_sz, full_name_sz;
37 PNET_DISPLAY_USER usr;
38
39 /* set up buffer */
40 name_sz = lstrlenW(sAdminUserName) + 1;
41 comment_sz = 1;
42 full_name_sz = 1;
43
44 *pdwSize = sizeof(NET_DISPLAY_USER);
45 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
46 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
47
48 usr = *buf;
49 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
50 usr->usri1_comment = (LPWSTR) (
51 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
52 usr->usri1_full_name = (LPWSTR) (
53 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
54
55 /* set data */
56 lstrcpyW(usr->usri1_name, sAdminUserName);
57 usr->usri1_comment[0] = 0;
58 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
59 usr->usri1_full_name[0] = 0;
60 usr->usri1_user_id = DOMAIN_USER_RID_ADMIN;
61 usr->usri1_next_index = 0;
62 }
63
64 /************************************************************
65 * ACCESS_QueryGuestDisplayInformation
66 *
67 * Creates a buffer with information for the Guest User
68 */
69 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
70 {
71 static const WCHAR sGuestUserName[] = {
72 'G','u','e','s','t',0 };
73
74 /* sizes of the field buffers in WCHARS */
75 int name_sz, comment_sz, full_name_sz;
76 PNET_DISPLAY_USER usr;
77
78 /* set up buffer */
79 name_sz = lstrlenW(sGuestUserName) + 1;
80 comment_sz = 1;
81 full_name_sz = 1;
82
83 *pdwSize = sizeof(NET_DISPLAY_USER);
84 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
85 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
86
87 usr = *buf;
88 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
89 usr->usri1_comment = (LPWSTR) (
90 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
91 usr->usri1_full_name = (LPWSTR) (
92 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
93
94 /* set data */
95 lstrcpyW(usr->usri1_name, sGuestUserName);
96 usr->usri1_comment[0] = 0;
97 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
98 UF_DONT_EXPIRE_PASSWD;
99 usr->usri1_full_name[0] = 0;
100 usr->usri1_user_id = DOMAIN_USER_RID_GUEST;
101 usr->usri1_next_index = 0;
102 }
103
104 /************************************************************
105 * Copies NET_DISPLAY_USER record.
106 */
107 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER *dest, LPWSTR *dest_buf,
108 PNET_DISPLAY_USER src)
109 {
110 LPWSTR str = *dest_buf;
111
112 src->usri1_name = str;
113 lstrcpyW(src->usri1_name, dest->usri1_name);
114 str = (LPWSTR) (
115 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
116
117 src->usri1_comment = str;
118 lstrcpyW(src->usri1_comment, dest->usri1_comment);
119 str = (LPWSTR) (
120 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
121
122 src->usri1_flags = dest->usri1_flags;
123
124 src->usri1_full_name = str;
125 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
126 str = (LPWSTR) (
127 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
128
129 src->usri1_user_id = dest->usri1_user_id;
130 src->usri1_next_index = dest->usri1_next_index;
131 *dest_buf = str;
132 }
133
134 /************************************************************
135 * NetQueryDisplayInformation (NETAPI32.@)
136 *
137 * The buffer structure:
138 * - array of fixed size record of the level type
139 * - strings, referenced by the record of the level type
140 */
141 NET_API_STATUS WINAPI
142 NetQueryDisplayInformation(
143 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
144 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
145 PVOID *SortedBuffer)
146 {
147 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
148 Level, Index, EntriesRequested, PreferredMaximumLength,
149 ReturnedEntryCount, SortedBuffer);
150
151 if(!NETAPI_IsLocalComputer(ServerName))
152 {
153 FIXME("Only implemented on local computer, but requested for "
154 "remote server %s\n", debugstr_w(ServerName));
155 return ERROR_ACCESS_DENIED;
156 }
157
158 switch (Level)
159 {
160 case 1:
161 {
162 /* current record */
163 PNET_DISPLAY_USER inf;
164 /* current available strings buffer */
165 LPWSTR str;
166 PNET_DISPLAY_USER admin, guest;
167 DWORD admin_size, guest_size;
168 LPWSTR name = NULL;
169 DWORD dwSize;
170
171 /* sizes of the field buffers in WCHARS */
172 int name_sz, comment_sz, full_name_sz;
173
174 /* number of the records, returned in SortedBuffer
175 3 - for current user, Administrator and Guest users
176 */
177 int records = 3;
178
179 FIXME("Level %d partially implemented\n", Level);
180 *ReturnedEntryCount = records;
181 comment_sz = 1;
182 full_name_sz = 1;
183
184 /* get data */
185 dwSize = UNLEN + 1;
186 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &name);
187 if (!GetUserNameW(name, &dwSize))
188 {
189 NetApiBufferFree(name);
190 return ERROR_ACCESS_DENIED;
191 }
192 name_sz = dwSize;
193 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
194 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
195
196 /* set up buffer */
197 dwSize = sizeof(NET_DISPLAY_USER) * records;
198 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
199
200 NetApiBufferAllocate(dwSize +
201 admin_size - sizeof(NET_DISPLAY_USER) +
202 guest_size - sizeof(NET_DISPLAY_USER),
203 SortedBuffer);
204 inf = *SortedBuffer;
205 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
206 inf->usri1_name = str;
207 str = (LPWSTR) (
208 ((PBYTE) str) + name_sz * sizeof(WCHAR));
209 inf->usri1_comment = str;
210 str = (LPWSTR) (
211 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
212 inf->usri1_full_name = str;
213 str = (LPWSTR) (
214 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
215
216 /* set data */
217 lstrcpyW(inf->usri1_name, name);
218 NetApiBufferFree(name);
219 inf->usri1_comment[0] = 0;
220 inf->usri1_flags =
221 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
222 inf->usri1_full_name[0] = 0;
223 inf->usri1_user_id = 0;
224 inf->usri1_next_index = 0;
225
226 inf++;
227 ACCESS_CopyDisplayUser(admin, &str, inf);
228 NetApiBufferFree(admin);
229
230 inf++;
231 ACCESS_CopyDisplayUser(guest, &str, inf);
232 NetApiBufferFree(guest);
233 break;
234 }
235
236 case 2:
237 case 3:
238 {
239 FIXME("Level %d is not implemented\n", Level);
240 break;
241 }
242
243 default:
244 TRACE("Invalid level %d is specified\n", Level);
245 return ERROR_INVALID_LEVEL;
246 }
247 return NERR_Success;
248 }