1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: networkdomain.cxx,v $
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>
39 //_________________________________________________________________________________________________________________
41 //_________________________________________________________________________________________________________________
45 #pragma warning(push, 1)
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 //_________________________________________________________________________________________________________________
63 //_________________________________________________________________________________________________________________
65 static DWORD WINAPI
GetUserDomainW_WINDOWS( LPWSTR lpBuffer
, DWORD nSize
)
72 if ( ERROR_SUCCESS
== RegOpenKeyEx(
74 TEXT("Network\\Logon"),
75 0, KEY_READ
, &hkeyLogon
) )
78 DWORD dwLogonSize
= sizeof(dwLogon
);
79 RegQueryValueEx( hkeyLogon
, TEXT("LMLogon"), 0, NULL
, (LPBYTE
)&dwLogon
, &dwLogonSize
);
80 RegCloseKey( hkeyLogon
);
84 HKEY hkeyNetworkProvider
;
86 if ( ERROR_SUCCESS
== RegOpenKeyEx(
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(
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
);
119 static rtl::OUString
GetUserDomain()
121 sal_Unicode aBuffer
[256];
123 long nVersion
= GetVersion();
127 nResult
= GetUserDomainW_WINDOWS( reinterpret_cast<LPWSTR
>(aBuffer
), sizeof( aBuffer
) );
129 nResult
= GetUserDomainW_NT( reinterpret_cast<LPWSTR
>(aBuffer
), sizeof( aBuffer
) );
132 return rtl::OUString( aBuffer
);
134 return rtl::OUString();
137 //_________________________________________________________________________________________________________________
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>
156 #include <osl/thread.h>
158 //_________________________________________________________________________________________________________________
160 //_________________________________________________________________________________________________________________
162 #if defined( SOLARIS )
164 //_________________________________________________________________________________________________________________
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
;
178 long nCopied
= sizeof(szBuffer
);
179 char *pBuffer
= szBuffer
;
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
);
201 osl_getThreadTextEncoding(),
202 OSTRING_TO_OUSTRING_CVTFLAGS
);
205 return ustrDomainName
;
208 #elif defined( LINUX ) /* endif SOLARIS */
210 //_________________________________________________________________________________________________________________
212 //_________________________________________________________________________________________________________________
217 static rtl_uString
*getDomainName()
219 /* Initialize and assume failure */
220 rtl_uString
*ustrDomainName
= NULL
;
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
234 } while ( -1 == result
&& EINVAL
== errno
);
242 osl_getThreadTextEncoding(),
243 OSTRING_TO_OUSTRING_CVTFLAGS
);
246 return ustrDomainName
;
251 //_________________________________________________________________________________________________________________
253 //_________________________________________________________________________________________________________________
255 static rtl_uString
*getDomainName()
262 //_________________________________________________________________________________________________________________
264 //_________________________________________________________________________________________________________________
266 rtl::OUString
NetworkDomain::GetYPDomainName()
268 rtl_uString
* pResult
= getDomainName();
270 return rtl::OUString( pResult
);
272 return rtl::OUString();
275 rtl::OUString
NetworkDomain::GetNTDomainName()
277 return ::rtl::OUString();
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();
298 } // namespace framework