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