187d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 387d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 487d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 587d2adbcSAndrew Rist * distributed with this work for additional information 687d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 787d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 887d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 987d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 1087d2adbcSAndrew Rist * 1187d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 1287d2adbcSAndrew Rist * 1387d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 1487d2adbcSAndrew Rist * software distributed under the License is distributed on an 1587d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1687d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 1787d2adbcSAndrew Rist * specific language governing permissions and limitations 1887d2adbcSAndrew Rist * under the License. 1987d2adbcSAndrew Rist * 2087d2adbcSAndrew Rist *************************************************************/ 2187d2adbcSAndrew Rist 2287d2adbcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sal.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir //------------------------------------------------------------------------ 28cdf0e10cSrcweir // header file 29cdf0e10cSrcweir //------------------------------------------------------------------------ 30cdf0e10cSrcweir #include <osl_Security_Const.h> 31cdf0e10cSrcweir 32cdf0e10cSrcweir using namespace osl; 33cdf0e10cSrcweir using namespace rtl; 34cdf0e10cSrcweir 35cdf0e10cSrcweir 36cdf0e10cSrcweir //------------------------------------------------------------------------ 37cdf0e10cSrcweir // helper functions and classes 38cdf0e10cSrcweir //------------------------------------------------------------------------ 39cdf0e10cSrcweir 40cdf0e10cSrcweir /** print Boolean value. 41cdf0e10cSrcweir */ 42cdf0e10cSrcweir inline void printBool( sal_Bool bOk ) 43cdf0e10cSrcweir { 44*63045d55SDamjan Jovanovic //printf("#printBool# " ); 45*63045d55SDamjan Jovanovic ( sal_True == bOk ) ? printf("TRUE!\n" ): printf("FALSE!\n" ); 46cdf0e10cSrcweir } 47cdf0e10cSrcweir 48cdf0e10cSrcweir /** print a UNI_CODE String. 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str ) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir rtl::OString aString; 53cdf0e10cSrcweir 54*63045d55SDamjan Jovanovic //printf("#printUString_u# " ); 55cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 56*63045d55SDamjan Jovanovic printf("%s\n", aString.getStr( ) ); 57cdf0e10cSrcweir } 58cdf0e10cSrcweir 59cdf0e10cSrcweir 60cdf0e10cSrcweir //------------------------------------------------------------------------ 61cdf0e10cSrcweir // test code start here 62cdf0e10cSrcweir //------------------------------------------------------------------------ 63cdf0e10cSrcweir 64cdf0e10cSrcweir namespace osl_Security 65cdf0e10cSrcweir { 66cdf0e10cSrcweir 67cdf0e10cSrcweir /** testing the method: 68cdf0e10cSrcweir Security() 69cdf0e10cSrcweir */ 70*63045d55SDamjan Jovanovic class ctors : public ::testing::Test 71cdf0e10cSrcweir { 72cdf0e10cSrcweir public: 73cdf0e10cSrcweir sal_Bool bRes, bRes1; 74cdf0e10cSrcweir 75cdf0e10cSrcweir }; // class ctors 76*63045d55SDamjan Jovanovic 77*63045d55SDamjan Jovanovic TEST_F(ctors, ctors_001) 78*63045d55SDamjan Jovanovic { 79*63045d55SDamjan Jovanovic ::osl::Security aSec; 80*63045d55SDamjan Jovanovic 81*63045d55SDamjan Jovanovic ASSERT_TRUE(aSec.getHandle( ) != NULL) << "#test comment#: create a security its handle should not be NULL."; 82*63045d55SDamjan Jovanovic } 83cdf0e10cSrcweir 84cdf0e10cSrcweir /** testing the methods: 85cdf0e10cSrcweir inline sal_Bool SAL_CALL logonUser(const ::rtl::OUString& strName, 86cdf0e10cSrcweir const ::rtl::OUString& strPasswd); 87cdf0e10cSrcweir inline sal_Bool SAL_CALL logonUser(const ::rtl::OUString & strName, 88cdf0e10cSrcweir const ::rtl::OUString & strPasswd, 89cdf0e10cSrcweir const ::rtl::OUString & strFileServer); 90cdf0e10cSrcweir */ 91*63045d55SDamjan Jovanovic class logonUser : public ::testing::Test 92cdf0e10cSrcweir { 93cdf0e10cSrcweir public: 94cdf0e10cSrcweir sal_Bool bRes; 95cdf0e10cSrcweir 96cdf0e10cSrcweir void logonUser_user_pwd( ) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir ::osl::Security aSec; 99cdf0e10cSrcweir bRes = aSec.logonUser( aLogonUser, aLogonPasswd ); 100cdf0e10cSrcweir 101*63045d55SDamjan Jovanovic ASSERT_TRUE(( sal_True == bRes )) << "#test comment#: check logon user through forwarded user name, pwd, passed in (UNX), failed in (W32)."; 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir void logonUser_user_pwd_server( ) 105cdf0e10cSrcweir { 106cdf0e10cSrcweir ::osl::Security aSec; 107cdf0e10cSrcweir bRes = aSec.logonUser( aLogonUser, aLogonPasswd, aFileServer ); 108cdf0e10cSrcweir 109*63045d55SDamjan Jovanovic ASSERT_TRUE(( sal_True == bRes )) << "#test comment#: check logon user through forwarded user name, pwd and server name, failed in (UNX)(W32)."; 110cdf0e10cSrcweir } 111*63045d55SDamjan Jovanovic }; // class logonUser 112cdf0e10cSrcweir 113*63045d55SDamjan Jovanovic TEST_F(logonUser, logonUser_001) 114*63045d55SDamjan Jovanovic { 115cdf0e10cSrcweir if ( !aStringForward.equals( aNullURL ) && aStringForward.indexOf( (sal_Unicode)' ' ) != -1 && ( aStringForward.indexOf( ( sal_Unicode ) ' ' ) == aStringForward.lastIndexOf( ( sal_Unicode ) ' ' ) ) ) 116cdf0e10cSrcweir /// if user name and passwd are forwarded 117cdf0e10cSrcweir { 118*63045d55SDamjan Jovanovic logonUser_user_pwd(); 119cdf0e10cSrcweir } 120*63045d55SDamjan Jovanovic } 121*63045d55SDamjan Jovanovic 122*63045d55SDamjan Jovanovic TEST_F(logonUser, logonUser_002) 123*63045d55SDamjan Jovanovic { 124cdf0e10cSrcweir if ( !aStringForward.equals( aNullURL ) && aStringForward.indexOf( (sal_Unicode)' ' ) != -1 && ( aStringForward.indexOf( ( sal_Unicode ) ' ' ) != aStringForward.lastIndexOf( ( sal_Unicode ) ' ' ) ) ) 125cdf0e10cSrcweir /// if user name and passwd and file server are forwarded 126cdf0e10cSrcweir { 127*63045d55SDamjan Jovanovic logonUser_user_pwd_server(); 128cdf0e10cSrcweir } 129*63045d55SDamjan Jovanovic } 130cdf0e10cSrcweir 131cdf0e10cSrcweir /** testing the method: 132cdf0e10cSrcweir inline sal_Bool Security::getUserIdent( rtl::OUString& strIdent) const 133cdf0e10cSrcweir */ 134*63045d55SDamjan Jovanovic class getUserIdent : public ::testing::Test 135cdf0e10cSrcweir { 136cdf0e10cSrcweir public: 137cdf0e10cSrcweir sal_Bool bRes, bRes1; 138cdf0e10cSrcweir }; // class getUserIdent 139cdf0e10cSrcweir 140*63045d55SDamjan Jovanovic TEST_F(getUserIdent, getUserIdent_001) 141*63045d55SDamjan Jovanovic { 142*63045d55SDamjan Jovanovic ::osl::Security aSec; 143*63045d55SDamjan Jovanovic ::rtl::OUString strID; 144*63045d55SDamjan Jovanovic bRes = aSec.getUserIdent( strID ); 145*63045d55SDamjan Jovanovic 146*63045d55SDamjan Jovanovic ASSERT_TRUE(( sal_True == strUserID.equals( strID ) ) && ( sal_True == bRes )) << "#test comment#: get UserID and compare it with names got at the beginning of the test."; 147*63045d55SDamjan Jovanovic } 148*63045d55SDamjan Jovanovic 149cdf0e10cSrcweir 150cdf0e10cSrcweir /** testing the method: 151cdf0e10cSrcweir inline sal_Bool SAL_CALL getUserName( ::rtl::OUString& strName) const; 152cdf0e10cSrcweir */ 153*63045d55SDamjan Jovanovic class getUserName : public ::testing::Test 154cdf0e10cSrcweir { 155cdf0e10cSrcweir public: 156cdf0e10cSrcweir sal_Bool bRes, bRes1; 157*63045d55SDamjan Jovanovic }; // class getUserName 158cdf0e10cSrcweir 159*63045d55SDamjan Jovanovic TEST_F(getUserName, getUserName_001) 160*63045d55SDamjan Jovanovic { 161*63045d55SDamjan Jovanovic ::osl::Security aSec; 162cdf0e10cSrcweir #ifdef WNT 163*63045d55SDamjan Jovanovic ::rtl::OUString strName( strUserName ), strGetName; 164cdf0e10cSrcweir #else 165*63045d55SDamjan Jovanovic ::rtl::OUString strName( strUserName ), strGetName; 166cdf0e10cSrcweir #endif 167*63045d55SDamjan Jovanovic bRes = aSec.getUserName( strGetName ); 168cdf0e10cSrcweir 169*63045d55SDamjan Jovanovic sal_Int32 nPos = -1; 170*63045d55SDamjan Jovanovic if (strName.getLength() > 0) 171*63045d55SDamjan Jovanovic { 172*63045d55SDamjan Jovanovic nPos = strGetName.indexOf(strName); 173*63045d55SDamjan Jovanovic } 174*63045d55SDamjan Jovanovic ASSERT_TRUE(( nPos >= 0 ) && ( sal_True == bRes )) << "#test comment#: get UserName and compare it with names got at the beginning of the test."; 175*63045d55SDamjan Jovanovic } 176cdf0e10cSrcweir 177cdf0e10cSrcweir /** testing the method: 178cdf0e10cSrcweir inline sal_Bool SAL_CALL getHomeDir( ::rtl::OUString& strDirectory) const; 179cdf0e10cSrcweir */ 180*63045d55SDamjan Jovanovic class getHomeDir : public ::testing::Test 181cdf0e10cSrcweir { 182cdf0e10cSrcweir public: 183cdf0e10cSrcweir sal_Bool bRes, bRes1; 184cdf0e10cSrcweir }; // class getHomeDir 185cdf0e10cSrcweir 186*63045d55SDamjan Jovanovic TEST_F(getHomeDir, getHomeDir_001) 187*63045d55SDamjan Jovanovic { 188*63045d55SDamjan Jovanovic ::osl::Security aSec; 189*63045d55SDamjan Jovanovic ::rtl::OUString strHome; 190*63045d55SDamjan Jovanovic bRes = aSec.getHomeDir( strHome ); 191*63045d55SDamjan Jovanovic 192*63045d55SDamjan Jovanovic ASSERT_TRUE(( sal_True == strHomeDirectory.equals( strHome ) ) && ( sal_True == bRes )) << "#test comment#: getHomeDir and compare it with the info we get at the beginning."; 193*63045d55SDamjan Jovanovic } 194*63045d55SDamjan Jovanovic 195cdf0e10cSrcweir /** testing the method: 196cdf0e10cSrcweir inline sal_Bool Security::getConfigDir( rtl::OUString& strDirectory ) const 197cdf0e10cSrcweir */ 198*63045d55SDamjan Jovanovic class getConfigDir : public ::testing::Test 199cdf0e10cSrcweir { 200cdf0e10cSrcweir public: 201cdf0e10cSrcweir sal_Bool bRes, bRes1; 202cdf0e10cSrcweir }; // class getConfigDir 203cdf0e10cSrcweir 204*63045d55SDamjan Jovanovic TEST_F(getConfigDir, getConfigDir_001) 205*63045d55SDamjan Jovanovic { 206*63045d55SDamjan Jovanovic ::osl::Security aSec; 207*63045d55SDamjan Jovanovic ::rtl::OUString strConfig; 208*63045d55SDamjan Jovanovic bRes = aSec.getConfigDir( strConfig ); 209*63045d55SDamjan Jovanovic 210*63045d55SDamjan Jovanovic ASSERT_TRUE(( sal_True == strConfigDirectory.equals( strConfig ) ) && ( sal_True == bRes )) << "#test comment#: getHomeDir and compare it with the info we get at the beginning."; 211*63045d55SDamjan Jovanovic } 212*63045d55SDamjan Jovanovic 213cdf0e10cSrcweir /** testing the method: 214cdf0e10cSrcweir inline sal_Bool SAL_CALL isAdministrator() const; 215cdf0e10cSrcweir */ 216*63045d55SDamjan Jovanovic class isAdministrator : public ::testing::Test 217cdf0e10cSrcweir { 218cdf0e10cSrcweir public: 219cdf0e10cSrcweir sal_Bool bRes; 220cdf0e10cSrcweir }; // class isAdministrator 221cdf0e10cSrcweir 222*63045d55SDamjan Jovanovic TEST_F(isAdministrator, isAdministrator_001) 223*63045d55SDamjan Jovanovic { 224*63045d55SDamjan Jovanovic ::osl::Security aSec; 225*63045d55SDamjan Jovanovic bRes = aSec.isAdministrator( ); 226*63045d55SDamjan Jovanovic 227*63045d55SDamjan Jovanovic ASSERT_TRUE(bRes == isAdmin) << "#test comment#: check if the user is administrator at beginning, compare here."; 228*63045d55SDamjan Jovanovic } 229*63045d55SDamjan Jovanovic 230cdf0e10cSrcweir /** testing the method: 231cdf0e10cSrcweir inline oslSecurity getHandle() const; 232cdf0e10cSrcweir */ 233*63045d55SDamjan Jovanovic class getHandle : public ::testing::Test 234cdf0e10cSrcweir { 235cdf0e10cSrcweir public: 236cdf0e10cSrcweir sal_Bool bRes; 237cdf0e10cSrcweir }; // class getHandle 238cdf0e10cSrcweir 239*63045d55SDamjan Jovanovic TEST_F(getHandle, getHandle_001) 240*63045d55SDamjan Jovanovic { 241*63045d55SDamjan Jovanovic ::osl::Security aSec; 242*63045d55SDamjan Jovanovic bRes = aSec.isAdministrator( ) == osl_isAdministrator( aSec.getHandle( ) ); 243*63045d55SDamjan Jovanovic 244*63045d55SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "#test comment#: use getHandle function to call C API."; 245*63045d55SDamjan Jovanovic } 246cdf0e10cSrcweir 247*63045d55SDamjan Jovanovic class UserProfile : public ::testing::Test 248cdf0e10cSrcweir { 249cdf0e10cSrcweir public: 250cdf0e10cSrcweir }; // class UserProfile 251cdf0e10cSrcweir 252*63045d55SDamjan Jovanovic TEST_F(UserProfile, loadUserProfile) 253cdf0e10cSrcweir { 254*63045d55SDamjan Jovanovic ::osl::Security aSec; 255*63045d55SDamjan Jovanovic sal_Bool bValue = osl_loadUserProfile(aSec.getHandle()); 256*63045d55SDamjan Jovanovic 257*63045d55SDamjan Jovanovic ASSERT_TRUE(bValue == sal_False) << "empty function."; 258*63045d55SDamjan Jovanovic } 259cdf0e10cSrcweir 260*63045d55SDamjan Jovanovic TEST_F(UserProfile, unloadUserProfile) 261*63045d55SDamjan Jovanovic { 262*63045d55SDamjan Jovanovic ::osl::Security aSec; 263*63045d55SDamjan Jovanovic osl_unloadUserProfile(aSec.getHandle()); 264*63045d55SDamjan Jovanovic ASSERT_TRUE(sal_True) << "empty function."; 265*63045d55SDamjan Jovanovic } 266*63045d55SDamjan Jovanovic 267*63045d55SDamjan Jovanovic class loginUserOnFileServer : public ::testing::Test 268*63045d55SDamjan Jovanovic { 269*63045d55SDamjan Jovanovic public: 270cdf0e10cSrcweir }; // class loginUserOnFileServer 271cdf0e10cSrcweir 272*63045d55SDamjan Jovanovic TEST_F(loginUserOnFileServer, loginUserOnFileServer_001) 273*63045d55SDamjan Jovanovic { 274*63045d55SDamjan Jovanovic rtl::OUString suUserName; 275*63045d55SDamjan Jovanovic rtl::OUString suPassword; 276*63045d55SDamjan Jovanovic rtl::OUString suFileServer; 277*63045d55SDamjan Jovanovic ::osl::Security aSec; 278*63045d55SDamjan Jovanovic oslSecurity pSec = aSec.getHandle(); 279*63045d55SDamjan Jovanovic 280*63045d55SDamjan Jovanovic oslSecurityError erg = osl_loginUserOnFileServer(suUserName.pData, suPassword.pData, suFileServer.pData, &pSec); 281*63045d55SDamjan Jovanovic 282*63045d55SDamjan Jovanovic ASSERT_TRUE(erg == osl_Security_E_UserUnknown) << "empty function."; 283*63045d55SDamjan Jovanovic } 284cdf0e10cSrcweir 285cdf0e10cSrcweir } // namespace osl_Security 286cdf0e10cSrcweir 287cdf0e10cSrcweir 288cdf0e10cSrcweir // ----------------------------------------------------------------------------- 289cdf0e10cSrcweir 290cdf0e10cSrcweir /** to do some initialized work, we replace the NOADDITIONAL macro with the initialize work which 291cdf0e10cSrcweir get current user name, . 292cdf0e10cSrcweir */ 293cdf0e10cSrcweir 294*63045d55SDamjan Jovanovic int main(int argc, char **argv) 295cdf0e10cSrcweir { 296cdf0e10cSrcweir /// start message 297*63045d55SDamjan Jovanovic printf("#Initializing ...\n" ); 298*63045d55SDamjan Jovanovic printf("#\n#logonUser function need root/Administrator account to test.\n" ); 299*63045d55SDamjan Jovanovic printf("#You can test by login with root/Administrator, and excute:\n" ); 300*63045d55SDamjan Jovanovic printf("#testshl2 -forward \"username password\" ../../../wntmsci9/bin/Security.dll\n" ); 301*63045d55SDamjan Jovanovic printf("# where username and password are forwarded account info.\n" ); 302*63045d55SDamjan Jovanovic printf("#if no text forwarded, this function will be skipped.\n" ); 303cdf0e10cSrcweir 304cdf0e10cSrcweir /// get system information 305cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 ) 306cdf0e10cSrcweir /// some initialization work for UNIX OS 307cdf0e10cSrcweir 308cdf0e10cSrcweir 309cdf0e10cSrcweir struct passwd* pw; 310*63045d55SDamjan Jovanovic EXPECT_TRUE(( pw = getpwuid( getuid() ) ) != NULL) << "getpwuid: no password entry\n"; 311cdf0e10cSrcweir 312cdf0e10cSrcweir /// get user ID; 313cdf0e10cSrcweir strUserID = ::rtl::OUString::valueOf( ( sal_Int32 )getuid( ) ); 314cdf0e10cSrcweir 315cdf0e10cSrcweir /// get user Name; 316cdf0e10cSrcweir strUserName = ::rtl::OUString::createFromAscii( pw->pw_name ); 317cdf0e10cSrcweir 318cdf0e10cSrcweir /// get home directory; 319*63045d55SDamjan Jovanovic EXPECT_TRUE(::osl::File::E_None == ::osl::File::getFileURLFromSystemPath( ::rtl::OUString::createFromAscii( pw->pw_dir ), strHomeDirectory )) << "#Convert from system path to URL failed."; 320cdf0e10cSrcweir 321cdf0e10cSrcweir /// get config directory; 322cdf0e10cSrcweir strConfigDirectory = strHomeDirectory.copy(0); 323cdf0e10cSrcweir 324cdf0e10cSrcweir /// is administrator; 325cdf0e10cSrcweir if( !getuid( ) ) 326cdf0e10cSrcweir isAdmin = sal_True; 327cdf0e10cSrcweir 328cdf0e10cSrcweir #endif 329cdf0e10cSrcweir #if defined ( WNT ) 330cdf0e10cSrcweir /// some initialization work for Windows OS 331cdf0e10cSrcweir 332cdf0e10cSrcweir 333cdf0e10cSrcweir /// Get the user name, computer name, user home directory. 334cdf0e10cSrcweir LPTSTR lpszSystemInfo; // pointer to system information string 335cdf0e10cSrcweir DWORD cchBuff = BUFSIZE; // size of computer or user name 336cdf0e10cSrcweir TCHAR tchBuffer[BUFSIZE]; // buffer for string 337cdf0e10cSrcweir 338cdf0e10cSrcweir lpszSystemInfo = tchBuffer; 339cdf0e10cSrcweir cchBuff = BUFSIZE; 340cdf0e10cSrcweir if( GetUserNameA(lpszSystemInfo, &cchBuff) ) 341cdf0e10cSrcweir strUserName = ::rtl::OUString::createFromAscii( lpszSystemInfo ); 342cdf0e10cSrcweir 343cdf0e10cSrcweir if( GetComputerName(lpszSystemInfo, &cchBuff) ) 344cdf0e10cSrcweir strComputerName = ::rtl::OUString::createFromAscii( lpszSystemInfo ); 345cdf0e10cSrcweir 346cdf0e10cSrcweir /// Get user home directory. 347cdf0e10cSrcweir HKEY hRegKey; 348cdf0e10cSrcweir sal_Char PathA[_MAX_PATH]; 349cdf0e10cSrcweir ::rtl::OUString strHome; 350cdf0e10cSrcweir if (RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", &hRegKey) == ERROR_SUCCESS) 351cdf0e10cSrcweir { 352cdf0e10cSrcweir LONG lRet, lSize = sizeof(PathA); 353cdf0e10cSrcweir DWORD Type; 354cdf0e10cSrcweir 355cdf0e10cSrcweir lRet = RegQueryValueEx(hRegKey, "AppData", NULL, &Type, ( unsigned char * )PathA, ( unsigned long * )&lSize); 356cdf0e10cSrcweir if ( ( lRet == ERROR_SUCCESS ) && ( Type == REG_SZ ) && ( _access( PathA, 0 ) == 0 ) ) 357cdf0e10cSrcweir { 358*63045d55SDamjan Jovanovic EXPECT_TRUE(::osl::File::E_None == ::osl::File::getFileURLFromSystemPath( ::rtl::OUString::createFromAscii( PathA ), strConfigDirectory )) << "#Convert from system path to URL failed."; 359cdf0e10cSrcweir } 360cdf0e10cSrcweir 361cdf0e10cSrcweir lRet = RegQueryValueEx(hRegKey, "Personal", NULL, &Type, ( unsigned char * )PathA, ( unsigned long * )&lSize); 362cdf0e10cSrcweir if ( ( lRet == ERROR_SUCCESS ) && ( Type == REG_SZ ) && ( _access( PathA, 0 ) == 0 ) ) 363cdf0e10cSrcweir { 364*63045d55SDamjan Jovanovic EXPECT_TRUE(::osl::File::E_None == ::osl::File::getFileURLFromSystemPath( ::rtl::OUString::createFromAscii( PathA ), strHomeDirectory )) << "#Convert from system path to URL failed."; 365cdf0e10cSrcweir } 366cdf0e10cSrcweir 367cdf0e10cSrcweir RegCloseKey(hRegKey); 368cdf0e10cSrcweir } 369cdf0e10cSrcweir 370cdf0e10cSrcweir 371cdf0e10cSrcweir /// Get user Security ID: 372cdf0e10cSrcweir 373cdf0e10cSrcweir // Create buffers that may be large enough. If a buffer is too small, the count parameter will be set to the size needed. 374cdf0e10cSrcweir const DWORD INITIAL_SIZE = 32; 375cdf0e10cSrcweir DWORD cbSid = 0; 376cdf0e10cSrcweir DWORD dwSidBufferSize = INITIAL_SIZE; 377cdf0e10cSrcweir DWORD cchDomainName = 0; 378cdf0e10cSrcweir DWORD dwDomainBufferSize = INITIAL_SIZE; 379cdf0e10cSrcweir WCHAR * wszDomainName = NULL; 380cdf0e10cSrcweir SID_NAME_USE eSidType; 381cdf0e10cSrcweir DWORD dwErrorCode = 0; 382cdf0e10cSrcweir 383cdf0e10cSrcweir LPCWSTR wszAccName = ( LPWSTR ) strUserName.getStr( ); 384cdf0e10cSrcweir 385cdf0e10cSrcweir // Create buffers for the SID and the domain name. 386cdf0e10cSrcweir PSID pSid = (PSID) new WIN_BYTE[dwSidBufferSize]; 387*63045d55SDamjan Jovanovic EXPECT_TRUE(pSid!= NULL) << "# creating SID buffer failed.\n"; 388cdf0e10cSrcweir memset( pSid, 0, dwSidBufferSize); 389cdf0e10cSrcweir 390cdf0e10cSrcweir wszDomainName = new WCHAR[dwDomainBufferSize]; 391*63045d55SDamjan Jovanovic EXPECT_TRUE(wszDomainName != NULL) << "# creating Domain name buffer failed.\n"; 392cdf0e10cSrcweir memset(wszDomainName, 0, dwDomainBufferSize*sizeof(WCHAR)); 393cdf0e10cSrcweir 394cdf0e10cSrcweir // Obtain the SID for the account name passed. 395cdf0e10cSrcweir for ( ; ; ) 396cdf0e10cSrcweir { 397cdf0e10cSrcweir // Set the count variables to the buffer sizes and retrieve the SID. 398cdf0e10cSrcweir cbSid = dwSidBufferSize; 399cdf0e10cSrcweir cchDomainName = dwDomainBufferSize; 400cdf0e10cSrcweir if (LookupAccountNameW( 401cdf0e10cSrcweir NULL, // Computer name. NULL for the local computer 402cdf0e10cSrcweir wszAccName, 403cdf0e10cSrcweir pSid, // Pointer to the SID buffer. Use NULL to get the size needed, 404cdf0e10cSrcweir &cbSid, // Size of the SID buffer needed. 405cdf0e10cSrcweir wszDomainName, // wszDomainName, 406cdf0e10cSrcweir &cchDomainName, 407cdf0e10cSrcweir &eSidType 408cdf0e10cSrcweir )) 409cdf0e10cSrcweir { 410cdf0e10cSrcweir if (IsValidSid( pSid) == FALSE) 411cdf0e10cSrcweir wprintf(L"# The SID for %s is invalid.\n", wszAccName); 412cdf0e10cSrcweir break; 413cdf0e10cSrcweir } 414cdf0e10cSrcweir dwErrorCode = GetLastError(); 415cdf0e10cSrcweir 416cdf0e10cSrcweir // Check if one of the buffers was too small. 417cdf0e10cSrcweir if (dwErrorCode == ERROR_INSUFFICIENT_BUFFER) 418cdf0e10cSrcweir { 419cdf0e10cSrcweir if (cbSid > dwSidBufferSize) 420cdf0e10cSrcweir { 421cdf0e10cSrcweir // Reallocate memory for the SID buffer. 422cdf0e10cSrcweir wprintf(L"# The SID buffer was too small. It will be reallocated.\n"); 423cdf0e10cSrcweir FreeSid( pSid); 424cdf0e10cSrcweir pSid = (PSID) new WIN_BYTE[cbSid]; 425*63045d55SDamjan Jovanovic EXPECT_TRUE(pSid!= NULL) << "# re-creating SID buffer failed.\n"; 426cdf0e10cSrcweir memset( pSid, 0, cbSid); 427cdf0e10cSrcweir dwSidBufferSize = cbSid; 428cdf0e10cSrcweir } 429cdf0e10cSrcweir if (cchDomainName > dwDomainBufferSize) 430cdf0e10cSrcweir { 431cdf0e10cSrcweir // Reallocate memory for the domain name buffer. 432cdf0e10cSrcweir wprintf(L"# The domain name buffer was too small. It will be reallocated.\n"); 433cdf0e10cSrcweir delete [] wszDomainName; 434cdf0e10cSrcweir wszDomainName = new WCHAR[cchDomainName]; 435*63045d55SDamjan Jovanovic EXPECT_TRUE(wszDomainName!= NULL) << "# re-creating domain name buffer failed.\n"; 436cdf0e10cSrcweir memset(wszDomainName, 0, cchDomainName*sizeof(WCHAR)); 437cdf0e10cSrcweir dwDomainBufferSize = cchDomainName; 438cdf0e10cSrcweir } 439cdf0e10cSrcweir } 440cdf0e10cSrcweir else 441cdf0e10cSrcweir { 442cdf0e10cSrcweir wprintf(L"# LookupAccountNameW failed. GetLastError returned: %d\n", dwErrorCode); 443cdf0e10cSrcweir break; 444cdf0e10cSrcweir } 445cdf0e10cSrcweir } 446cdf0e10cSrcweir 447cdf0e10cSrcweir // now got SID successfully, only need to compare SID, so I copied the rest lines from source to convert SID to OUString. 448cdf0e10cSrcweir PSID_IDENTIFIER_AUTHORITY psia; 449cdf0e10cSrcweir DWORD dwSubAuthorities; 450cdf0e10cSrcweir DWORD dwSidRev=SID_REVISION; 451cdf0e10cSrcweir DWORD dwCounter; 452cdf0e10cSrcweir DWORD dwSidSize; 453cdf0e10cSrcweir sal_Char *Ident; 454cdf0e10cSrcweir 455cdf0e10cSrcweir /* obtain SidIdentifierAuthority */ 456cdf0e10cSrcweir psia=GetSidIdentifierAuthority(pSid); 457cdf0e10cSrcweir 458cdf0e10cSrcweir /* obtain sidsubauthority count */ 459cdf0e10cSrcweir dwSubAuthorities=*GetSidSubAuthorityCount(pSid)<=5?*GetSidSubAuthorityCount(pSid):5; 460cdf0e10cSrcweir 461cdf0e10cSrcweir /* buffer length: S-SID_REVISION- + identifierauthority- + subauthorities- + NULL */ 462cdf0e10cSrcweir Ident=(sal_Char * )malloc(88*sizeof(sal_Char)); 463cdf0e10cSrcweir 464cdf0e10cSrcweir /* prepare S-SID_REVISION- */ 465cdf0e10cSrcweir dwSidSize=wsprintf(Ident, TEXT("S-%lu-"), dwSidRev); 466cdf0e10cSrcweir 467cdf0e10cSrcweir /* prepare SidIdentifierAuthority */ 468cdf0e10cSrcweir if ((psia->Value[0] != 0) || (psia->Value[1] != 0)) 469cdf0e10cSrcweir { 470cdf0e10cSrcweir dwSidSize+=wsprintf(Ident + strlen(Ident), 471cdf0e10cSrcweir TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"), 472cdf0e10cSrcweir (USHORT)psia->Value[0], 473cdf0e10cSrcweir (USHORT)psia->Value[1], 474cdf0e10cSrcweir (USHORT)psia->Value[2], 475cdf0e10cSrcweir (USHORT)psia->Value[3], 476cdf0e10cSrcweir (USHORT)psia->Value[4], 477cdf0e10cSrcweir (USHORT)psia->Value[5]); 478cdf0e10cSrcweir } 479cdf0e10cSrcweir else 480cdf0e10cSrcweir { 481cdf0e10cSrcweir dwSidSize+=wsprintf(Ident + strlen(Ident), 482cdf0e10cSrcweir TEXT("%lu"), 483cdf0e10cSrcweir (ULONG)(psia->Value[5] ) + 484cdf0e10cSrcweir (ULONG)(psia->Value[4] << 8) + 485cdf0e10cSrcweir (ULONG)(psia->Value[3] << 16) + 486cdf0e10cSrcweir (ULONG)(psia->Value[2] << 24) ); 487cdf0e10cSrcweir } 488cdf0e10cSrcweir 489cdf0e10cSrcweir /* loop through SidSubAuthorities */ 490cdf0e10cSrcweir for (dwCounter=0; dwCounter < dwSubAuthorities; dwCounter++) 491cdf0e10cSrcweir { 492cdf0e10cSrcweir dwSidSize+=wsprintf(Ident + dwSidSize, TEXT("-%lu"), 493cdf0e10cSrcweir *GetSidSubAuthority(pSid, dwCounter) ); 494cdf0e10cSrcweir } 495cdf0e10cSrcweir 496cdf0e10cSrcweir strUserID = ::rtl::OUString::createFromAscii( Ident ); 497cdf0e10cSrcweir 498cdf0e10cSrcweir free(Ident); 499cdf0e10cSrcweir delete pSid; 500cdf0e10cSrcweir delete [] wszDomainName; 501cdf0e10cSrcweir 502cdf0e10cSrcweir 503cdf0e10cSrcweir /// check if logged in user is administrator: 504cdf0e10cSrcweir 505cdf0e10cSrcweir WIN_BOOL b; 506cdf0e10cSrcweir SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; 507cdf0e10cSrcweir PSID AdministratorsGroup; 508cdf0e10cSrcweir b = AllocateAndInitializeSid( 509cdf0e10cSrcweir &NtAuthority, 510cdf0e10cSrcweir 2, 511cdf0e10cSrcweir SECURITY_BUILTIN_DOMAIN_RID, 512cdf0e10cSrcweir DOMAIN_ALIAS_RID_ADMINS, 513cdf0e10cSrcweir 0, 0, 0, 0, 0, 0, 514cdf0e10cSrcweir &AdministratorsGroup); 515cdf0e10cSrcweir if(b) 516cdf0e10cSrcweir { 517cdf0e10cSrcweir if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 518cdf0e10cSrcweir { 519cdf0e10cSrcweir b = FALSE; 520cdf0e10cSrcweir } 521cdf0e10cSrcweir FreeSid(AdministratorsGroup); 522cdf0e10cSrcweir } 523cdf0e10cSrcweir 524cdf0e10cSrcweir isAdmin = ( sal_Bool )b; 525cdf0e10cSrcweir 526cdf0e10cSrcweir #endif 527cdf0e10cSrcweir 528cdf0e10cSrcweir /// print the information. 529*63045d55SDamjan Jovanovic printf("#\n#Retrived system information is below:\n"); 530cdf0e10cSrcweir 531*63045d55SDamjan Jovanovic printf("Computer Name: "); 532cdf0e10cSrcweir if ( strComputerName == aNullURL ) 533*63045d55SDamjan Jovanovic printf(" Not retrived\n" ); 534cdf0e10cSrcweir else 535cdf0e10cSrcweir printUString( strComputerName ); 536cdf0e10cSrcweir 537*63045d55SDamjan Jovanovic printf("Current User Name: "); 538cdf0e10cSrcweir if ( strUserName == aNullURL ) 539*63045d55SDamjan Jovanovic printf(" Not retrived\n" ); 540cdf0e10cSrcweir else 541cdf0e10cSrcweir printUString( strUserName ); 542cdf0e10cSrcweir 543*63045d55SDamjan Jovanovic printf("Current User Home Directory:"); 544cdf0e10cSrcweir if ( strHomeDirectory == aNullURL ) 545*63045d55SDamjan Jovanovic printf(" Not retrived\n" ); 546cdf0e10cSrcweir else 547cdf0e10cSrcweir printUString( strHomeDirectory ); 548cdf0e10cSrcweir 549*63045d55SDamjan Jovanovic printf("Current Config Directory: "); 550cdf0e10cSrcweir if ( strConfigDirectory == aNullURL ) 551*63045d55SDamjan Jovanovic printf(" Not retrived\n" ); 552cdf0e10cSrcweir else 553cdf0e10cSrcweir printUString( strConfigDirectory ); 554cdf0e10cSrcweir 555*63045d55SDamjan Jovanovic printf("Current UserID: "); 556cdf0e10cSrcweir if ( strUserID == aNullURL ) 557*63045d55SDamjan Jovanovic printf(" Not retrived\n" ); 558cdf0e10cSrcweir else 559cdf0e10cSrcweir printUString( strUserID ); 560cdf0e10cSrcweir 561*63045d55SDamjan Jovanovic printf("Current User is"); 562cdf0e10cSrcweir if ( isAdmin == sal_False ) 563*63045d55SDamjan Jovanovic printf(" NOT Administrator.\n" ); 564cdf0e10cSrcweir else 565*63045d55SDamjan Jovanovic printf(" Administrator.\n" ); 566cdf0e10cSrcweir 567cdf0e10cSrcweir 568cdf0e10cSrcweir /// get and display forwarded text if available. 569cdf0e10cSrcweir aStringForward = ::rtl::OUString::createFromAscii( getForwardString() ); 570cdf0e10cSrcweir if ( !aStringForward.equals( aNullURL ) && aStringForward.indexOf( (sal_Unicode)' ' ) != -1 ) 571cdf0e10cSrcweir { 572cdf0e10cSrcweir sal_Int32 nFirstSpacePoint = aStringForward.indexOf( (sal_Unicode)' ' );; 573cdf0e10cSrcweir sal_Int32 nLastSpacePoint = aStringForward.lastIndexOf( (sal_Unicode)' ' );; 574cdf0e10cSrcweir if ( nFirstSpacePoint == nLastSpacePoint ) 575cdf0e10cSrcweir /// only forwarded two parameters, username and password. 576cdf0e10cSrcweir { 577cdf0e10cSrcweir aLogonUser = aStringForward.copy( 0, nFirstSpacePoint ); 578*63045d55SDamjan Jovanovic printf("\n#Forwarded username: "); 579cdf0e10cSrcweir printUString( aLogonUser); 580cdf0e10cSrcweir 581cdf0e10cSrcweir aLogonPasswd = aStringForward.copy( nFirstSpacePoint +1, aStringForward.getLength( ) - 1 ); 582*63045d55SDamjan Jovanovic printf("#Forwarded password: "); 583*63045d55SDamjan Jovanovic for ( int i = nFirstSpacePoint +1; i <= aStringForward.getLength( ) - 1; i++, printf("*") ); 584*63045d55SDamjan Jovanovic printf("\n" ); 585cdf0e10cSrcweir } 586cdf0e10cSrcweir else 587cdf0e10cSrcweir /// forwarded three parameters, username, password and fileserver. 588cdf0e10cSrcweir { 589cdf0e10cSrcweir aLogonUser = aStringForward.copy( 0, nFirstSpacePoint ); 590*63045d55SDamjan Jovanovic printf("#Forwarded username: "); 591cdf0e10cSrcweir printUString( aLogonUser); 592cdf0e10cSrcweir 593cdf0e10cSrcweir aLogonPasswd = aStringForward.copy( nFirstSpacePoint +1, nLastSpacePoint ); 594*63045d55SDamjan Jovanovic printf("#Forwarded password: "); 595*63045d55SDamjan Jovanovic for ( int i = nFirstSpacePoint +1; i <= nLastSpacePoint; i++, printf("*") ); 596*63045d55SDamjan Jovanovic printf("\n" ); 597cdf0e10cSrcweir 598cdf0e10cSrcweir aFileServer = aStringForward.copy( nLastSpacePoint +1, aStringForward.getLength( ) - 1 ); 599*63045d55SDamjan Jovanovic printf("#Forwarded FileServer: "); 600cdf0e10cSrcweir printUString( aFileServer ); 601cdf0e10cSrcweir 602cdf0e10cSrcweir } 603cdf0e10cSrcweir } 604cdf0e10cSrcweir 605*63045d55SDamjan Jovanovic printf("#\n#Initialization Done.\n" ); 606cdf0e10cSrcweir 607*63045d55SDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv); 608*63045d55SDamjan Jovanovic return RUN_ALL_TESTS(); 609cdf0e10cSrcweir } 610