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