2 * Copyright (C) 2004 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <lsass/lsass.h>
29 /***********************************************************************
30 * GetComputerObjectNameA (SECUR32.@) Wine 1.1.14
32 * Get the local computer's name using the format specified.
35 * NameFormat [I] The format for the name.
36 * lpNameBuffer [O] Pointer to buffer to receive the name.
37 * nSize [I/O] Size in characters of buffer.
40 * TRUE If the name was written to lpNameBuffer.
41 * FALSE If the name couldn't be written.
44 * If lpNameBuffer is NULL, then the size of the buffer needed to hold the
45 * name will be returned in *nSize.
47 * nSize returns the number of characters written when lpNameBuffer is not
48 * NULL or the size of the buffer needed to hold the name when the buffer
49 * is too short or lpNameBuffer is NULL.
51 * It the buffer is too short, ERROR_INSUFFICIENT_BUFFER error will be set.
53 BOOLEAN WINAPI
GetComputerObjectNameA(
54 EXTENDED_NAME_FORMAT NameFormat
, LPSTR lpNameBuffer
, PULONG nSize
)
57 LPWSTR bufferW
= NULL
;
59 DPRINT("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
61 bufferW
= HeapAlloc(GetProcessHeap(), 0, sizeW
* sizeof(WCHAR
));
62 if (bufferW
== NULL
) {
63 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
67 rc
= GetComputerObjectNameW(NameFormat
, bufferW
, &sizeW
);
69 ULONG len
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
70 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, lpNameBuffer
, *nSize
, NULL
, NULL
);
75 HeapFree(GetProcessHeap(), 0, bufferW
);
79 /***********************************************************************
80 * GetComputerObjectNameW (SECUR32.@) Wine 1.1.14
82 BOOLEAN WINAPI
GetComputerObjectNameW(
83 EXTENDED_NAME_FORMAT NameFormat
, LPWSTR lpNameBuffer
, PULONG nSize
)
85 LSA_HANDLE policyHandle
;
86 LSA_OBJECT_ATTRIBUTES objectAttributes
;
87 PPOLICY_DNS_DOMAIN_INFO domainInfo
;
90 DPRINT("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
92 if (NameFormat
== NameUnknown
)
94 SetLastError(ERROR_INVALID_PARAMETER
);
98 ZeroMemory(&objectAttributes
, sizeof(objectAttributes
));
99 objectAttributes
.Length
= sizeof(objectAttributes
);
101 ntStatus
= LsaOpenPolicy(NULL
, &objectAttributes
,
102 POLICY_VIEW_LOCAL_INFORMATION
,
104 if (ntStatus
!= STATUS_SUCCESS
)
106 SetLastError(LsaNtStatusToWinError(ntStatus
));
107 DPRINT1("LsaOpenPolicy failed with NT status %u\n", GetLastError());
111 ntStatus
= LsaQueryInformationPolicy(policyHandle
,
112 PolicyDnsDomainInformation
,
113 (PVOID
*)&domainInfo
);
114 if (ntStatus
!= STATUS_SUCCESS
)
116 SetLastError(LsaNtStatusToWinError(ntStatus
));
117 DPRINT1("LsaQueryInformationPolicy failed with NT status %u\n",
119 LsaClose(policyHandle
);
127 case NameSamCompatible
:
129 WCHAR name
[MAX_COMPUTERNAME_LENGTH
+ 1];
130 DWORD size
= sizeof(name
)/sizeof(name
[0]);
131 if (GetComputerNameW(name
, &size
))
133 DWORD len
= domainInfo
->Name
.Length
+ size
+ 3;
139 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
144 WCHAR bs
[] = { '\\', 0 };
145 WCHAR ds
[] = { '$', 0 };
146 lstrcpyW(lpNameBuffer
, domainInfo
->Name
.Buffer
);
147 lstrcatW(lpNameBuffer
, bs
);
148 lstrcatW(lpNameBuffer
, name
);
149 lstrcatW(lpNameBuffer
, ds
);
153 else /* just requesting length required */
161 SetLastError(ERROR_INTERNAL_ERROR
);
166 case NameFullyQualifiedDN
:
170 case NameUserPrincipal
:
171 case NameCanonicalEx
:
172 case NameServicePrincipal
:
174 DPRINT1("NameFormat %d not implemented\n", NameFormat
);
175 SetLastError(ERROR_CANT_ACCESS_DOMAIN_INFO
);
179 SetLastError(ERROR_INVALID_PARAMETER
);
185 SetLastError(ERROR_CANT_ACCESS_DOMAIN_INFO
);
189 LsaFreeMemory(domainInfo
);
190 LsaClose(policyHandle
);
196 BOOLEAN WINAPI
GetUserNameExA(
197 EXTENDED_NAME_FORMAT NameFormat
, LPSTR lpNameBuffer
, PULONG nSize
)
200 LPWSTR bufferW
= NULL
;
201 ULONG sizeW
= *nSize
;
202 DPRINT("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
204 bufferW
= HeapAlloc(GetProcessHeap(), 0, sizeW
* sizeof(WCHAR
));
205 if (bufferW
== NULL
) {
206 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
210 rc
= GetUserNameExW(NameFormat
, bufferW
, &sizeW
);
212 ULONG len
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
215 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, lpNameBuffer
, *nSize
, NULL
, NULL
);
222 SetLastError(ERROR_MORE_DATA
);
227 HeapFree(GetProcessHeap(), 0, bufferW
);
232 BOOLEAN WINAPI
GetUserNameExW(
233 EXTENDED_NAME_FORMAT NameFormat
, LPWSTR lpNameBuffer
, PULONG nSize
)
236 WCHAR samname
[UNLEN
+ 1 + MAX_COMPUTERNAME_LENGTH
+ 1];
239 DPRINT("(%d %p %p)\n", NameFormat
, lpNameBuffer
, nSize
);
243 case NameSamCompatible
:
245 /* This assumes the current user is always a local account */
246 len
= MAX_COMPUTERNAME_LENGTH
+ 1;
247 if (GetComputerNameW(samname
, &len
))
249 out
= samname
+ lstrlenW(samname
);
252 if (GetUserNameW(out
, &len
))
254 status
= (lstrlenW(samname
) < *nSize
);
257 lstrcpyW(lpNameBuffer
, samname
);
258 *nSize
= lstrlenW(samname
);
262 SetLastError(ERROR_MORE_DATA
);
263 *nSize
= lstrlenW(samname
) + 1;
274 case NameFullyQualifiedDN
:
278 case NameUserPrincipal
:
279 case NameCanonicalEx
:
280 case NameServicePrincipal
:
282 SetLastError(ERROR_NONE_MAPPED
);
286 SetLastError(ERROR_INVALID_PARAMETER
);