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