1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 #include <helper/networkdomain.hxx>
27 
28 namespace framework
29 {
30 
31 #ifdef WNT
32 //_________________________________________________________________________________________________________________
33 //	Windows
34 //_________________________________________________________________________________________________________________
35 
36 #define UNICODE
37 #if defined _MSC_VER
38 #pragma warning(push, 1)
39 #endif
40 #include <windows.h>
41 #if defined _MSC_VER
42 #pragma warning(pop)
43 #endif
44 
45 //_________________________________________________________________________________________________________________
46 //	Win NT, Win 2000, Win XP
47 //_________________________________________________________________________________________________________________
48 
GetUserDomainW_NT(LPWSTR lpBuffer,DWORD nSize)49 static DWORD WINAPI GetUserDomainW_NT( LPWSTR lpBuffer, DWORD nSize )
50 {
51 	return GetEnvironmentVariable( TEXT("USERDOMAIN"), lpBuffer, nSize );
52 }
53 
54 //_________________________________________________________________________________________________________________
55 //	Win 9x,Win ME
56 //_________________________________________________________________________________________________________________
57 
GetUserDomainW_WINDOWS(LPWSTR lpBuffer,DWORD nSize)58 static DWORD WINAPI GetUserDomainW_WINDOWS( LPWSTR lpBuffer, DWORD nSize )
59 {
60 	HKEY	hkeyLogon;
61 	HKEY	hkeyWorkgroup;
62 	DWORD	dwResult = 0;
63 
64 
65 	if ( ERROR_SUCCESS  == RegOpenKeyEx(
66 		HKEY_LOCAL_MACHINE,
67 		TEXT("Network\\Logon"),
68 		0, KEY_READ, &hkeyLogon ) )
69 	{
70 		DWORD	dwLogon = 0;
71 		DWORD	dwLogonSize = sizeof(dwLogon);
72 		RegQueryValueEx( hkeyLogon, TEXT("LMLogon"), 0, NULL, (LPBYTE)&dwLogon, &dwLogonSize );
73 		RegCloseKey( hkeyLogon );
74 
75 		if ( dwLogon )
76 		{
77 			HKEY	hkeyNetworkProvider;
78 
79 			if ( ERROR_SUCCESS  == RegOpenKeyEx(
80 				HKEY_LOCAL_MACHINE,
81 				TEXT("SYSTEM\\CurrentControlSet\\Services\\MSNP32\\NetworkProvider"),
82 				0, KEY_READ, &hkeyNetworkProvider ) )
83 			{
84 				DWORD	dwBufferSize = nSize;
85 				LONG	lResult = RegQueryValueEx( hkeyNetworkProvider, TEXT("AuthenticatingAgent"), 0, NULL, (LPBYTE)lpBuffer, &dwBufferSize );
86 
87 				if ( ERROR_SUCCESS == lResult || ERROR_MORE_DATA == lResult )
88 					dwResult = dwBufferSize / sizeof(TCHAR);
89 
90 				RegCloseKey( hkeyNetworkProvider );
91 			}
92 		}
93 	}
94 	else if ( ERROR_SUCCESS == RegOpenKeyEx(
95 		HKEY_LOCAL_MACHINE,
96 		TEXT("SYSTEM\\CurrentControlSet\\Services\\VxD\\VNETSUP"),
97 		0, KEY_READ, &hkeyWorkgroup ) )
98 	{
99 		DWORD	dwBufferSize = nSize;
100 		LONG	lResult = RegQueryValueEx( hkeyWorkgroup, TEXT("Workgroup"), 0, NULL, (LPBYTE)lpBuffer, &dwBufferSize );
101 
102 		if ( ERROR_SUCCESS == lResult || ERROR_MORE_DATA == lResult )
103 			dwResult = dwBufferSize / sizeof(TCHAR);
104 
105 		RegCloseKey( hkeyWorkgroup );
106 	}
107 
108 
109 	return dwResult;
110 }
111 
GetUserDomain()112 static rtl::OUString GetUserDomain()
113 {
114 	sal_Unicode	aBuffer[256];
115 
116 	long	nVersion = GetVersion();
117 	DWORD	nResult;
118 
119 	if ( nVersion < 0 )
120 		nResult = GetUserDomainW_WINDOWS( reinterpret_cast<LPWSTR>(aBuffer), sizeof( aBuffer ) );
121 	else
122 		nResult = GetUserDomainW_NT( reinterpret_cast<LPWSTR>(aBuffer), sizeof( aBuffer ) );
123 
124 	if ( nResult > 0 )
125 		return rtl::OUString( aBuffer );
126 	else
127 		return rtl::OUString();
128 }
129 
130 //_________________________________________________________________________________________________________________
131 //	Windows
132 //_________________________________________________________________________________________________________________
133 
GetYPDomainName()134 rtl::OUString NetworkDomain::GetYPDomainName()
135 {
136 	return ::rtl::OUString();
137 }
138 
GetNTDomainName()139 rtl::OUString NetworkDomain::GetNTDomainName()
140 {
141 	return GetUserDomain();
142 }
143 
144 #elif defined( UNIX )
145 
146 #include <rtl/ustring.h>
147 #include <stdlib.h>
148 #include <errno.h>
149 #include <osl/thread.h>
150 
151 //_________________________________________________________________________________________________________________
152 //	Unix
153 //_________________________________________________________________________________________________________________
154 
155 #if defined( SOLARIS )
156 
157 //_________________________________________________________________________________________________________________
158 //	Solaris
159 //_________________________________________________________________________________________________________________
160 
161 #include <sys/systeminfo.h>
162 #include <sal/alloca.h>
163 
164 static rtl_uString *getDomainName()
165 {
166 	/* Initialize and assume failure */
167 	rtl_uString	*ustrDomainName = NULL;
168 
169 	char	szBuffer[256];
170 
171 	long	nCopied = sizeof(szBuffer);
172 	char	*pBuffer = szBuffer;
173 	long	nBufSize;
174 
175 	do
176 	{
177 		nBufSize = nCopied;
178 		nCopied = sysinfo( SI_SRPC_DOMAIN, pBuffer, nBufSize );
179 
180 		/*	If nCopied is greater than buffersize we need to allocate
181 			a buffer with suitable size */
182 
183 		if ( nCopied > nBufSize )
184 			pBuffer = (char *)alloca( nCopied );
185 
186 	} while ( nCopied > nBufSize );
187 
188 	if ( -1 != nCopied 	)
189 	{
190 		rtl_string2UString(
191 			&ustrDomainName,
192 			pBuffer,
193 			nCopied - 1,
194 			osl_getThreadTextEncoding(),
195 			OSTRING_TO_OUSTRING_CVTFLAGS );
196 	}
197 
198 	return ustrDomainName;
199 }
200 
201 #elif defined( LINUX ) /* endif SOLARIS */
202 
203 //_________________________________________________________________________________________________________________
204 //	Linux
205 //_________________________________________________________________________________________________________________
206 
207 #include <unistd.h>
208 #include <string.h>
209 
210 static rtl_uString *getDomainName()
211 {
212 	/* Initialize and assume failure */
213 	rtl_uString	*ustrDomainName = NULL;
214 
215 	char	*pBuffer;
216 	int		result;
217 	size_t	nBufSize = 0;
218 
219 	do
220 	{
221 		nBufSize += 256; /* Increase buffer size by steps of 256 bytes */
222 		pBuffer = (char *)alloca( nBufSize );
223 		result = getdomainname( pBuffer, nBufSize );
224 		/* If buffersize in not large enough -1 is returned and errno
225 		is set to EINVAL. This only applies to libc. With glibc the name
226 		is truncated. */
227 	} while ( -1 == result && EINVAL == errno );
228 
229 	if ( 0 == result )
230 	{
231 		rtl_string2UString(
232 			&ustrDomainName,
233 			pBuffer,
234 			strlen( pBuffer ),
235 			osl_getThreadTextEncoding(),
236 			OSTRING_TO_OUSTRING_CVTFLAGS );
237 	}
238 
239 	return ustrDomainName;
240 }
241 
242 #else /* LINUX */
243 
244 //_________________________________________________________________________________________________________________
245 //	Other Unix
246 //_________________________________________________________________________________________________________________
247 
248 static rtl_uString *getDomainName()
249 {
250 	return NULL;
251 }
252 
253 #endif
254 
255 //_________________________________________________________________________________________________________________
256 //	Unix
257 //_________________________________________________________________________________________________________________
258 
259 rtl::OUString NetworkDomain::GetYPDomainName()
260 {
261 	rtl_uString* pResult = getDomainName();
262 	if ( pResult )
263 		return rtl::OUString( pResult );
264 	else
265 		return rtl::OUString();
266 }
267 
268 rtl::OUString NetworkDomain::GetNTDomainName()
269 {
270 	return ::rtl::OUString();
271 }
272 
273 #else /* UNIX */
274 
275 //_________________________________________________________________________________________________________________
276 //	Other operating systems (non-Windows and non-Unix)
277 //_________________________________________________________________________________________________________________
278 
279 rtl::OUString NetworkDomain::GetYPDomainName()
280 {
281 	return rtl::OUString();
282 }
283 
284 rtl::OUString NetworkDomain::GetNTDomainName()
285 {
286 	return rtl::OUString();
287 }
288 
289 #endif
290 
291 } // namespace framework
292