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 29*cdf0e10cSrcweir #include "system.h" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <osl/security.h> 32*cdf0e10cSrcweir #include <osl/diagnose.h> 33*cdf0e10cSrcweir #include <osl/module.h> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include "osl/thread.h" 36*cdf0e10cSrcweir #include "osl/file.h" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #ifdef SOLARIS 39*cdf0e10cSrcweir #include <crypt.h> 40*cdf0e10cSrcweir #endif 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir #include "secimpl.h" 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #ifndef PAM_BINARY_MSG 45*cdf0e10cSrcweir #define PAM_BINARY_MSG 6 46*cdf0e10cSrcweir #endif 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir extern oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode); 49*cdf0e10cSrcweir extern void* SAL_CALL osl_psz_getSymbol(oslModule hModule, const sal_Char* pszSymbolName); 50*cdf0e10cSrcweir extern oslSecurityError SAL_CALL 51*cdf0e10cSrcweir osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd, 52*cdf0e10cSrcweir oslSecurity* pSecurity); 53*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax); 54*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax); 55*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax); 56*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax); 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir oslSecurity SAL_CALL osl_getCurrentSecurity() 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir oslSecurityImpl *pSecImpl = (oslSecurityImpl*) malloc(sizeof(oslSecurityImpl)); 64*cdf0e10cSrcweir struct passwd *pPasswd = getpwuid(getuid()); 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir if (pPasswd) 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir memcpy(&pSecImpl->m_pPasswd, pPasswd, sizeof(pSecImpl->m_pPasswd)); 69*cdf0e10cSrcweir pSecImpl->m_isValid = sal_True; 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir else 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir /* Some UNIX-OS don't implement getpwuid, e.g. NC OS (special NetBSD) 1.2.1 */ 74*cdf0e10cSrcweir /* so we have to catch this in this else branch */ 75*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_name = getenv("USER"); 76*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_dir = getenv("HOME"); 77*cdf0e10cSrcweir if (pSecImpl->m_pPasswd.pw_name && pSecImpl->m_pPasswd.pw_dir) 78*cdf0e10cSrcweir pSecImpl->m_isValid = sal_True; 79*cdf0e10cSrcweir else 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_name = "unknown"; 82*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_dir = "/tmp"; 83*cdf0e10cSrcweir pSecImpl->m_isValid = sal_False; 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_passwd = NULL; 86*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_uid = getuid(); 87*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_gid = getgid(); 88*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_gecos = "unknown"; 89*cdf0e10cSrcweir pSecImpl->m_pPasswd.pw_shell = "unknown"; 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir return ((oslSecurity)pSecImpl); 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir oslSecurityError SAL_CALL osl_loginUser( 98*cdf0e10cSrcweir rtl_uString *ustrUserName, 99*cdf0e10cSrcweir rtl_uString *ustrPassword, 100*cdf0e10cSrcweir oslSecurity *pSecurity 101*cdf0e10cSrcweir ) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir oslSecurityError ret; 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir *pSecurity = osl_getCurrentSecurity(); 106*cdf0e10cSrcweir ret = osl_Security_E_None; 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir return ret; 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir oslSecurityError SAL_CALL osl_loginUserOnFileServer( 114*cdf0e10cSrcweir rtl_uString *strUserName, 115*cdf0e10cSrcweir rtl_uString *strPasswd, 116*cdf0e10cSrcweir rtl_uString *strFileServer, 117*cdf0e10cSrcweir oslSecurity *pSecurity 118*cdf0e10cSrcweir ) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir oslSecurityError erg; 121*cdf0e10cSrcweir return erg = osl_Security_E_UserUnknown; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir oslSecurityError SAL_CALL osl_psz_loginUserOnFileServer( const sal_Char* pszUserName, 126*cdf0e10cSrcweir const sal_Char* pszPasswd, 127*cdf0e10cSrcweir const sal_Char* pszFileServer, 128*cdf0e10cSrcweir oslSecurity* pSecurity ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir oslSecurityError erg; 131*cdf0e10cSrcweir return erg = osl_Security_E_UserUnknown; 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent) 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir sal_Bool bRet=sal_False; 137*cdf0e10cSrcweir sal_Char pszIdent[1024]; 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir pszIdent[0] = '\0'; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent)); 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); 144*cdf0e10cSrcweir OSL_ASSERT(*ustrIdent != NULL); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir return bRet; 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir sal_Char buffer[32]; 153*cdf0e10cSrcweir sal_Int32 nChr; 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir if (pSecImpl == NULL) 158*cdf0e10cSrcweir return sal_False; 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid); 161*cdf0e10cSrcweir if ( nChr < 0 || nChr >= sizeof(buffer) || nChr >= nMax ) 162*cdf0e10cSrcweir return sal_False; /* leave *pszIdent unmodified in case of failure */ 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir memcpy(pszIdent, buffer, nChr+1); 165*cdf0e10cSrcweir return sal_True; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir sal_Bool bRet=sal_False; 171*cdf0e10cSrcweir sal_Char pszName[1024]; 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir pszName[0] = '\0'; 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName)); 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); 178*cdf0e10cSrcweir OSL_ASSERT(*ustrName != NULL); 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir return bRet; 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir if ((pSecImpl == NULL) || (! pSecImpl->m_isValid)) 190*cdf0e10cSrcweir return sal_False; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax); 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir return sal_True; 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir sal_Bool bRet=sal_False; 200*cdf0e10cSrcweir sal_Char pszDirectory[PATH_MAX]; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir pszDirectory[0] = '\0'; 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory)); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir if ( bRet == sal_True ) 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); 209*cdf0e10cSrcweir OSL_ASSERT(*pustrDirectory != NULL); 210*cdf0e10cSrcweir osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory ); 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir return bRet; 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) 218*cdf0e10cSrcweir { 219*cdf0e10cSrcweir oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir if (pSecImpl == NULL) 222*cdf0e10cSrcweir return sal_False; 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir /* if current user, check also environment for HOME */ 225*cdf0e10cSrcweir if (getuid() == pSecImpl->m_pPasswd.pw_uid) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir sal_Char *pStr = NULL; 228*cdf0e10cSrcweir #ifdef SOLARIS 229*cdf0e10cSrcweir char buffer[8192]; 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir struct passwd pwd; 232*cdf0e10cSrcweir struct passwd *ppwd; 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir #ifdef _POSIX_PTHREAD_SEMANTICS 235*cdf0e10cSrcweir if ( 0 != getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &ppwd ) ) 236*cdf0e10cSrcweir ppwd = NULL; 237*cdf0e10cSrcweir #else 238*cdf0e10cSrcweir ppwd = getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer) ); 239*cdf0e10cSrcweir #endif 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir if ( ppwd ) 242*cdf0e10cSrcweir pStr = ppwd->pw_dir; 243*cdf0e10cSrcweir #else 244*cdf0e10cSrcweir pStr = getenv("HOME"); 245*cdf0e10cSrcweir #endif 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir if ((pStr != NULL) && (strlen(pStr) > 0) && 248*cdf0e10cSrcweir (access(pStr, 0) == 0)) 249*cdf0e10cSrcweir strncpy(pszDirectory, pStr, nMax); 250*cdf0e10cSrcweir else 251*cdf0e10cSrcweir if (pSecImpl->m_isValid) 252*cdf0e10cSrcweir strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax); 253*cdf0e10cSrcweir else 254*cdf0e10cSrcweir return sal_False; 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir else 257*cdf0e10cSrcweir strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax); 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir return sal_True; 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir sal_Bool bRet = sal_False; 265*cdf0e10cSrcweir sal_Char pszDirectory[PATH_MAX]; 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir pszDirectory[0] = '\0'; 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory)); 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir if ( bRet == sal_True ) 272*cdf0e10cSrcweir { 273*cdf0e10cSrcweir rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); 274*cdf0e10cSrcweir OSL_ASSERT(*pustrDirectory != NULL); 275*cdf0e10cSrcweir osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory ); 276*cdf0e10cSrcweir } 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir return bRet; 279*cdf0e10cSrcweir } 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) 283*cdf0e10cSrcweir { 284*cdf0e10cSrcweir return (osl_psz_getHomeDir(Security, pszDirectory, nMax)); 285*cdf0e10cSrcweir } 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security) 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir if (pSecImpl == NULL) 292*cdf0e10cSrcweir return sal_False; 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir if (pSecImpl->m_pPasswd.pw_uid != 0) 295*cdf0e10cSrcweir return (sal_False); 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir return (sal_True); 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir void SAL_CALL osl_freeSecurityHandle(oslSecurity Security) 301*cdf0e10cSrcweir { 302*cdf0e10cSrcweir if (Security) 303*cdf0e10cSrcweir free ((oslSecurityImpl*)Security); 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir return sal_False; 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir 312*cdf0e10cSrcweir void SAL_CALL osl_unloadUserProfile(oslSecurity Security) 313*cdf0e10cSrcweir { 314*cdf0e10cSrcweir return; 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir 318