merge the formfield patch from ooo-build
[ooovba.git] / framework / source / helper / networkdomain.cxx
blobb31d67173b6eba2fc8ac0b6a88820edcce7577a8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: networkdomain.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include <helper/networkdomain.hxx>
35 namespace framework
38 #ifdef WNT
39 //_________________________________________________________________________________________________________________
40 // Windows
41 //_________________________________________________________________________________________________________________
43 #define UNICODE
44 #if defined _MSC_VER
45 #pragma warning(push, 1)
46 #endif
47 #include <windows.h>
48 #if defined _MSC_VER
49 #pragma warning(pop)
50 #endif
52 //_________________________________________________________________________________________________________________
53 // Win NT, Win 2000, Win XP
54 //_________________________________________________________________________________________________________________
56 static DWORD WINAPI GetUserDomainW_NT( LPWSTR lpBuffer, DWORD nSize )
58 return GetEnvironmentVariable( TEXT("USERDOMAIN"), lpBuffer, nSize );
61 //_________________________________________________________________________________________________________________
62 // Win 9x,Win ME
63 //_________________________________________________________________________________________________________________
65 static DWORD WINAPI GetUserDomainW_WINDOWS( LPWSTR lpBuffer, DWORD nSize )
67 HKEY hkeyLogon;
68 HKEY hkeyWorkgroup;
69 DWORD dwResult = 0;
72 if ( ERROR_SUCCESS == RegOpenKeyEx(
73 HKEY_LOCAL_MACHINE,
74 TEXT("Network\\Logon"),
75 0, KEY_READ, &hkeyLogon ) )
77 DWORD dwLogon = 0;
78 DWORD dwLogonSize = sizeof(dwLogon);
79 RegQueryValueEx( hkeyLogon, TEXT("LMLogon"), 0, NULL, (LPBYTE)&dwLogon, &dwLogonSize );
80 RegCloseKey( hkeyLogon );
82 if ( dwLogon )
84 HKEY hkeyNetworkProvider;
86 if ( ERROR_SUCCESS == RegOpenKeyEx(
87 HKEY_LOCAL_MACHINE,
88 TEXT("SYSTEM\\CurrentControlSet\\Services\\MSNP32\\NetworkProvider"),
89 0, KEY_READ, &hkeyNetworkProvider ) )
91 DWORD dwBufferSize = nSize;
92 LONG lResult = RegQueryValueEx( hkeyNetworkProvider, TEXT("AuthenticatingAgent"), 0, NULL, (LPBYTE)lpBuffer, &dwBufferSize );
94 if ( ERROR_SUCCESS == lResult || ERROR_MORE_DATA == lResult )
95 dwResult = dwBufferSize / sizeof(TCHAR);
97 RegCloseKey( hkeyNetworkProvider );
101 else if ( ERROR_SUCCESS == RegOpenKeyEx(
102 HKEY_LOCAL_MACHINE,
103 TEXT("SYSTEM\\CurrentControlSet\\Services\\VxD\\VNETSUP"),
104 0, KEY_READ, &hkeyWorkgroup ) )
106 DWORD dwBufferSize = nSize;
107 LONG lResult = RegQueryValueEx( hkeyWorkgroup, TEXT("Workgroup"), 0, NULL, (LPBYTE)lpBuffer, &dwBufferSize );
109 if ( ERROR_SUCCESS == lResult || ERROR_MORE_DATA == lResult )
110 dwResult = dwBufferSize / sizeof(TCHAR);
112 RegCloseKey( hkeyWorkgroup );
116 return dwResult;
119 static rtl::OUString GetUserDomain()
121 sal_Unicode aBuffer[256];
123 long nVersion = GetVersion();
124 DWORD nResult;
126 if ( nVersion < 0 )
127 nResult = GetUserDomainW_WINDOWS( reinterpret_cast<LPWSTR>(aBuffer), sizeof( aBuffer ) );
128 else
129 nResult = GetUserDomainW_NT( reinterpret_cast<LPWSTR>(aBuffer), sizeof( aBuffer ) );
131 if ( nResult > 0 )
132 return rtl::OUString( aBuffer );
133 else
134 return rtl::OUString();
137 //_________________________________________________________________________________________________________________
138 // Windows
139 //_________________________________________________________________________________________________________________
141 rtl::OUString NetworkDomain::GetYPDomainName()
143 return ::rtl::OUString();
146 rtl::OUString NetworkDomain::GetNTDomainName()
148 return GetUserDomain();
151 #elif defined( UNIX )
153 #include <rtl/ustring.h>
154 #include <stdlib.h>
155 #include <errno.h>
156 #include <osl/thread.h>
158 //_________________________________________________________________________________________________________________
159 // Unix
160 //_________________________________________________________________________________________________________________
162 #if defined( SOLARIS )
164 //_________________________________________________________________________________________________________________
165 // Solaris
166 //_________________________________________________________________________________________________________________
168 #include <sys/systeminfo.h>
169 #include <sal/alloca.h>
171 static rtl_uString *getDomainName()
173 /* Initialize and assume failure */
174 rtl_uString *ustrDomainName = NULL;
176 char szBuffer[256];
178 long nCopied = sizeof(szBuffer);
179 char *pBuffer = szBuffer;
180 long nBufSize;
184 nBufSize = nCopied;
185 nCopied = sysinfo( SI_SRPC_DOMAIN, pBuffer, nBufSize );
187 /* If nCopied is greater than buffersize we need to allocate
188 a buffer with suitable size */
190 if ( nCopied > nBufSize )
191 pBuffer = (char *)alloca( nCopied );
193 } while ( nCopied > nBufSize );
195 if ( -1 != nCopied )
197 rtl_string2UString(
198 &ustrDomainName,
199 pBuffer,
200 nCopied - 1,
201 osl_getThreadTextEncoding(),
202 OSTRING_TO_OUSTRING_CVTFLAGS );
205 return ustrDomainName;
208 #elif defined( LINUX ) /* endif SOLARIS */
210 //_________________________________________________________________________________________________________________
211 // Linux
212 //_________________________________________________________________________________________________________________
214 #include <unistd.h>
215 #include <string.h>
217 static rtl_uString *getDomainName()
219 /* Initialize and assume failure */
220 rtl_uString *ustrDomainName = NULL;
222 char *pBuffer;
223 int result;
224 size_t nBufSize = 0;
228 nBufSize += 256; /* Increase buffer size by steps of 256 bytes */
229 pBuffer = (char *)alloca( nBufSize );
230 result = getdomainname( pBuffer, nBufSize );
231 /* If buffersize in not large enough -1 is returned and errno
232 is set to EINVAL. This only applies to libc. With glibc the name
233 is truncated. */
234 } while ( -1 == result && EINVAL == errno );
236 if ( 0 == result )
238 rtl_string2UString(
239 &ustrDomainName,
240 pBuffer,
241 strlen( pBuffer ),
242 osl_getThreadTextEncoding(),
243 OSTRING_TO_OUSTRING_CVTFLAGS );
246 return ustrDomainName;
249 #else /* LINUX */
251 //_________________________________________________________________________________________________________________
252 // Other Unix
253 //_________________________________________________________________________________________________________________
255 static rtl_uString *getDomainName()
257 return NULL;
260 #endif
262 //_________________________________________________________________________________________________________________
263 // Unix
264 //_________________________________________________________________________________________________________________
266 rtl::OUString NetworkDomain::GetYPDomainName()
268 rtl_uString* pResult = getDomainName();
269 if ( pResult )
270 return rtl::OUString( pResult );
271 else
272 return rtl::OUString();
275 rtl::OUString NetworkDomain::GetNTDomainName()
277 return ::rtl::OUString();
280 #else /* UNIX */
282 //_________________________________________________________________________________________________________________
283 // Other operating systems (non-Windows and non-Unix)
284 //_________________________________________________________________________________________________________________
286 rtl::OUString NetworkDomain::GetYPDomainName()
288 return rtl::OUString();
291 rtl::OUString NetworkDomain::GetNTDomainName()
293 return rtl::OUString();
296 #endif
298 } // namespace framework