From d37e9645edbdabaaf16ce0f1f904405f3477dc41 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Wed, 2 Apr 2014 10:36:04 +0200 Subject: sal/osl/unx/security.c -> .cxx Change-Id: If8dbfa3e26a02ac3d5ee0077f730eeca4b59c4d7 --- sal/Library_sal.mk | 2 +- sal/osl/unx/security.c | 501 ----------------------------------------------- sal/osl/unx/security.cxx | 501 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 502 insertions(+), 502 deletions(-) delete mode 100644 sal/osl/unx/security.c create mode 100644 sal/osl/unx/security.cxx diff --git a/sal/Library_sal.mk b/sal/Library_sal.mk index ec8d89b95119..2103fe830266 100644 --- a/sal/Library_sal.mk +++ b/sal/Library_sal.mk @@ -164,6 +164,7 @@ $(eval $(call gb_Library_add_exception_objects,sal,\ sal/osl/unx/process \ sal/osl/unx/process_impl \ sal/osl/unx/profile \ + sal/osl/unx/security \ $(if $(filter DESKTOP,$(BUILD_TYPE)), sal/osl/unx/salinit) \ )) $(eval $(call gb_Library_add_cobjects,sal,\ @@ -172,7 +173,6 @@ $(eval $(call gb_Library_add_cobjects,sal,\ sal/osl/unx/nlsupport \ sal/osl/unx/pipe \ sal/osl/unx/readwrite_helper \ - sal/osl/unx/security \ sal/osl/unx/socket \ sal/osl/unx/system \ sal/osl/unx/tempfile \ diff --git a/sal/osl/unx/security.c b/sal/osl/unx/security.c deleted file mode 100644 index 3806a0f96b2d..000000000000 --- a/sal/osl/unx/security.c +++ /dev/null @@ -1,501 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include -#include - -#include "system.h" - -#include -#include -#include - -#include "osl/thread.h" -#include "osl/file.h" - -#if defined LINUX || defined SOLARIS -#include -#endif - -#include "secimpl.h" - -#ifdef ANDROID -#define getpwuid_r(uid, pwd, buf, buflen, result) (*(result) = getpwuid(uid), (*(result) ? (memcpy (buf, *(result), sizeof (struct passwd)), 0) : errno)) -#endif - - -static oslSecurityError SAL_CALL -osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd, - oslSecurity* pSecurity); -sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax); -static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax); -static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax); -static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax); - -static sal_Bool sysconf_SC_GETPW_R_SIZE_MAX(size_t * value) { -#if defined _SC_GETPW_R_SIZE_MAX - long m; - errno = 0; - m = sysconf(_SC_GETPW_R_SIZE_MAX); - if (m == -1) { - /* _SC_GETPW_R_SIZE_MAX has no limit; some platforms like certain - FreeBSD versions support sysconf(_SC_GETPW_R_SIZE_MAX) in a broken - way and always set EINVAL, so be resilient here: */ - return sal_False; - } else { - OSL_ASSERT(m >= 0 && (unsigned long) m < SIZE_MAX); - *value = (size_t) m; - return sal_True; - } -#else - /* some platforms like Mac OS X 1.3 do not define _SC_GETPW_R_SIZE_MAX: */ - return sal_False; -#endif -} - -static oslSecurityImpl * growSecurityImpl( - oslSecurityImpl * impl, size_t * bufSize) -{ - size_t n = 0; - oslSecurityImpl * p = NULL; - if (impl == NULL) { - if (!sysconf_SC_GETPW_R_SIZE_MAX(&n)) { - /* choose something sensible (the callers of growSecurityImpl will - detect it if the allocated buffer is too small: */ - n = 1024; - } - } else if (*bufSize <= SIZE_MAX / 2) { - n = 2 * *bufSize; - } - if (n != 0) { - if (n <= SIZE_MAX - offsetof(oslSecurityImpl, m_buffer)) { - *bufSize = n; - n += offsetof(oslSecurityImpl, m_buffer); - } else { - *bufSize = SIZE_MAX - offsetof(oslSecurityImpl, m_buffer); - n = SIZE_MAX; - } - p = realloc(impl, n); - memset (p, 0, n); - } - if (p == NULL) { - free(impl); - } - return p; -} - -static void deleteSecurityImpl(oslSecurityImpl * impl) { - free(impl); -} - -oslSecurity SAL_CALL osl_getCurrentSecurity() -{ - size_t n = 0; - oslSecurityImpl * p = NULL; - for (;;) { - struct passwd * found; - p = growSecurityImpl(p, &n); - if (p == NULL) { - return NULL; - } - switch (getpwuid_r(getuid(), &p->m_pPasswd, p->m_buffer, n, &found)) { - case ERANGE: - break; - case 0: - if (found != NULL) { - return p; - } - /* fall through */ - default: - deleteSecurityImpl(p); - return NULL; - } - } -} - -oslSecurityError SAL_CALL osl_loginUser( - rtl_uString *ustrUserName, - rtl_uString *ustrPassword, - oslSecurity *pSecurity - ) -{ - oslSecurityError Error; - rtl_String* strUserName=0; - rtl_String* strPassword=0; - sal_Char* pszUserName=0; - sal_Char* pszPassword=0; - - if ( ustrUserName != 0 ) - { - rtl_uString2String( &strUserName, - rtl_uString_getStr(ustrUserName), - rtl_uString_getLength(ustrUserName), - RTL_TEXTENCODING_UTF8, - OUSTRING_TO_OSTRING_CVTFLAGS ); - pszUserName = rtl_string_getStr(strUserName); - } - - - if ( ustrPassword != 0 ) - { - rtl_uString2String( &strPassword, - rtl_uString_getStr(ustrPassword), - rtl_uString_getLength(ustrPassword), - RTL_TEXTENCODING_UTF8, - OUSTRING_TO_OSTRING_CVTFLAGS ); - pszPassword = rtl_string_getStr(strPassword); - } - - - Error=osl_psz_loginUser(pszUserName,pszPassword,pSecurity); - - if ( strUserName != 0 ) - { - rtl_string_release(strUserName); - } - - if ( strPassword) - { - rtl_string_release(strPassword); - } - - - return Error; -} - - -static oslSecurityError SAL_CALL -osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd, - oslSecurity* pSecurity) -{ - (void)pszUserName; - (void)pszPasswd; - (void)pSecurity; - - return osl_Security_E_None; -} - -oslSecurityError SAL_CALL osl_loginUserOnFileServer( - rtl_uString *strUserName, - rtl_uString *strPasswd, - rtl_uString *strFileServer, - oslSecurity *pSecurity - ) -{ - (void) strUserName; /* unused */ - (void) strPasswd; /* unused */ - (void) strFileServer; /* unused */ - (void) pSecurity; /* unused */ - return osl_Security_E_UserUnknown; -} - - -sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent) -{ - sal_Bool bRet=sal_False; - sal_Char pszIdent[1024]; - - pszIdent[0] = '\0'; - - bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent)); - - rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); - OSL_ASSERT(*ustrIdent != NULL); - - return bRet; -} - - -sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax) -{ - sal_Char buffer[32]; - sal_Int32 nChr; - - oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; - - if (pSecImpl == NULL) - return sal_False; - - nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid); - if ( nChr < 0 || SAL_INT_CAST(sal_uInt32, nChr) >= sizeof(buffer) - || SAL_INT_CAST(sal_uInt32, nChr) >= nMax ) - return sal_False; /* leave *pszIdent unmodified in case of failure */ - - memcpy(pszIdent, buffer, nChr+1); - return sal_True; -} - -sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName) -{ - sal_Bool bRet=sal_False; - sal_Char pszName[1024]; - - pszName[0] = '\0'; - - bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName)); - - rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); - OSL_ASSERT(*ustrName != NULL); - - return bRet; -} - - - -static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax) -{ - oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; - - if (pSecImpl == NULL || pSecImpl->m_pPasswd.pw_name == NULL) - return sal_False; - - strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax); - - return sal_True; -} - -sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory) -{ - sal_Bool bRet=sal_False; - sal_Char pszDirectory[PATH_MAX]; - - pszDirectory[0] = '\0'; - - bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory)); - - if ( bRet == sal_True ) - { - rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); - OSL_ASSERT(*pustrDirectory != NULL); - osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory ); - } - - return bRet; -} - -static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) -{ - oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; - - if (pSecImpl == NULL) - return sal_False; - -#ifdef ANDROID -{ - sal_Bool bRet = sal_False; - rtl_uString *pName = 0, *pValue = 0; - - rtl_uString_newFromAscii(&pName, "HOME"); - - if (rtl_bootstrap_get(pName, &pValue, NULL)) - { - rtl_String *pStrValue = 0; - if (pValue && pValue->length > 0) - { - rtl_uString2String(&pStrValue, pValue->buffer, - pValue->length, RTL_TEXTENCODING_UTF8, - OUSTRING_TO_OSTRING_CVTFLAGS); - if (pStrValue && pStrValue->length > 0) - { - sal_Int32 nCopy = (sal_Int32)(nMax-1) < pStrValue->length ? (sal_Int32)(nMax-1) : pStrValue->length ; - strncpy (pszDirectory, pStrValue->buffer, nCopy); - pszDirectory[nCopy] = '\0'; - bRet = (size_t)pStrValue->length < nMax; - } - rtl_string_release(pStrValue); - } - rtl_uString_release(pName); - } - if (bRet) - return bRet; -} -#endif - - /* if current user, check also environment for HOME */ - if (getuid() == pSecImpl->m_pPasswd.pw_uid) - { - sal_Char *pStr = NULL; -#ifdef SOLARIS - char buffer[8192]; - - struct passwd pwd; - struct passwd *ppwd; - -#ifdef _POSIX_PTHREAD_SEMANTICS - if ( 0 != getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &ppwd ) ) - ppwd = NULL; -#else - ppwd = getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer) ); -#endif - - if ( ppwd ) - pStr = ppwd->pw_dir; -#else - pStr = getenv("HOME"); -#endif - - if (pStr != NULL && strlen(pStr) > 0 && access(pStr, 0) == 0) - strncpy(pszDirectory, pStr, nMax); - else if (pSecImpl->m_pPasswd.pw_dir != NULL) - strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax); - else - return sal_False; - } - else if (pSecImpl->m_pPasswd.pw_dir != NULL) - strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax); - else - return sal_False; - - return sal_True; -} - -sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory) -{ - sal_Bool bRet = sal_False; - sal_Char pszDirectory[PATH_MAX]; - - pszDirectory[0] = '\0'; - - bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory)); - - if ( bRet == sal_True ) - { - rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); - OSL_ASSERT(*pustrDirectory != NULL); - osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory ); - } - - return bRet; -} - -#if !defined(MACOSX) && !defined(IOS) - -#define DOT_CONFIG "/.config" - -static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) -{ - sal_Char *pStr = getenv("XDG_CONFIG_HOME"); - - if (pStr == NULL || strlen(pStr) == 0 || access(pStr, 0) != 0) - { - size_t n = 0; - sal_Bool dirOK = sal_True; - - // a default equal to $HOME/.config should be used. - if (!osl_psz_getHomeDir(Security, pszDirectory, nMax)) - return sal_False; - n = strlen(pszDirectory); - if (n + sizeof(DOT_CONFIG) < nMax) - { - strncpy(pszDirectory+n, DOT_CONFIG, sizeof(DOT_CONFIG)); - - // try to create dir if not present - if (access(pszDirectory, F_OK) != 0 && mkdir(pszDirectory, S_IRWXU) != 0) - dirOK = sal_False; - else - { - // check file type and permissions - struct stat st; - if (stat(pszDirectory, &st) != 0) - { - OSL_TRACE("Could not stat $HOME/.config"); - dirOK = sal_False; - } - else - { - if (!S_ISDIR(st.st_mode)) - { - OSL_TRACE("$HOME/.config is not a directory"); - dirOK = sal_False; - } - if (!(st.st_mode & S_IRUSR && st.st_mode & S_IWUSR && st.st_mode & S_IXUSR)) - { - OSL_TRACE("$HOME/.config has bad permissions"); - dirOK = sal_False; - } - } - } - - // resort to HOME - if (dirOK == sal_False) - pszDirectory[n] = '\0'; - } - } - else - strncpy(pszDirectory, pStr, nMax); - - return sal_True; -} - -#undef DOT_CONFIG - -#else - -/* - * FIXME: rewrite to use more flexible - * NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) - * as soon as we can bumb the baseline to Tiger (for NSApplicationSupportDirectory) and have - * support for Objective-C in the build environment - */ - -#define MACOSX_CONFIG_DIR "/Library/Application Support" /* Used on iOS, too */ -static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) -{ - if( osl_psz_getHomeDir(Security, pszDirectory, nMax - sizeof(MACOSX_CONFIG_DIR) + 1) ) - { - strcat( pszDirectory, MACOSX_CONFIG_DIR ); - return sal_True; - } - - return sal_False; -} - -#endif - -sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security) -{ - oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; - - if (pSecImpl == NULL) - return sal_False; - - if (pSecImpl->m_pPasswd.pw_uid != 0) - return sal_False; - - return sal_True; -} - -void SAL_CALL osl_freeSecurityHandle(oslSecurity Security) -{ - deleteSecurityImpl(Security); -} - - -sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security) -{ - (void) Security; /* unused */ - return sal_False; -} - -void SAL_CALL osl_unloadUserProfile(oslSecurity Security) -{ - (void) Security; /* unused */ -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/osl/unx/security.cxx b/sal/osl/unx/security.cxx new file mode 100644 index 000000000000..77764b5caed5 --- /dev/null +++ b/sal/osl/unx/security.cxx @@ -0,0 +1,501 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include +#include + +#include "system.h" + +#include +#include +#include + +#include "osl/thread.h" +#include "osl/file.h" + +#if defined LINUX || defined SOLARIS +#include +#endif + +#include "secimpl.h" + +#ifdef ANDROID +#define getpwuid_r(uid, pwd, buf, buflen, result) (*(result) = getpwuid(uid), (*(result) ? (memcpy (buf, *(result), sizeof (struct passwd)), 0) : errno)) +#endif + + +static oslSecurityError SAL_CALL +osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd, + oslSecurity* pSecurity); +extern "C" sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax); +static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax); +static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax); +static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax); + +static sal_Bool sysconf_SC_GETPW_R_SIZE_MAX(size_t * value) { +#if defined _SC_GETPW_R_SIZE_MAX + long m; + errno = 0; + m = sysconf(_SC_GETPW_R_SIZE_MAX); + if (m == -1) { + /* _SC_GETPW_R_SIZE_MAX has no limit; some platforms like certain + FreeBSD versions support sysconf(_SC_GETPW_R_SIZE_MAX) in a broken + way and always set EINVAL, so be resilient here: */ + return sal_False; + } else { + OSL_ASSERT(m >= 0 && (unsigned long) m < SIZE_MAX); + *value = (size_t) m; + return sal_True; + } +#else + /* some platforms like Mac OS X 1.3 do not define _SC_GETPW_R_SIZE_MAX: */ + return sal_False; +#endif +} + +static oslSecurityImpl * growSecurityImpl( + oslSecurityImpl * impl, size_t * bufSize) +{ + size_t n = 0; + oslSecurityImpl * p = NULL; + if (impl == NULL) { + if (!sysconf_SC_GETPW_R_SIZE_MAX(&n)) { + /* choose something sensible (the callers of growSecurityImpl will + detect it if the allocated buffer is too small: */ + n = 1024; + } + } else if (*bufSize <= SIZE_MAX / 2) { + n = 2 * *bufSize; + } + if (n != 0) { + if (n <= SIZE_MAX - offsetof(oslSecurityImpl, m_buffer)) { + *bufSize = n; + n += offsetof(oslSecurityImpl, m_buffer); + } else { + *bufSize = SIZE_MAX - offsetof(oslSecurityImpl, m_buffer); + n = SIZE_MAX; + } + p = static_cast(realloc(impl, n)); + memset (p, 0, n); + } + if (p == NULL) { + free(impl); + } + return p; +} + +static void deleteSecurityImpl(oslSecurityImpl * impl) { + free(impl); +} + +oslSecurity SAL_CALL osl_getCurrentSecurity() +{ + size_t n = 0; + oslSecurityImpl * p = NULL; + for (;;) { + struct passwd * found; + p = growSecurityImpl(p, &n); + if (p == NULL) { + return NULL; + } + switch (getpwuid_r(getuid(), &p->m_pPasswd, p->m_buffer, n, &found)) { + case ERANGE: + break; + case 0: + if (found != NULL) { + return p; + } + /* fall through */ + default: + deleteSecurityImpl(p); + return NULL; + } + } +} + +oslSecurityError SAL_CALL osl_loginUser( + rtl_uString *ustrUserName, + rtl_uString *ustrPassword, + oslSecurity *pSecurity + ) +{ + oslSecurityError Error; + rtl_String* strUserName=0; + rtl_String* strPassword=0; + sal_Char* pszUserName=0; + sal_Char* pszPassword=0; + + if ( ustrUserName != 0 ) + { + rtl_uString2String( &strUserName, + rtl_uString_getStr(ustrUserName), + rtl_uString_getLength(ustrUserName), + RTL_TEXTENCODING_UTF8, + OUSTRING_TO_OSTRING_CVTFLAGS ); + pszUserName = rtl_string_getStr(strUserName); + } + + + if ( ustrPassword != 0 ) + { + rtl_uString2String( &strPassword, + rtl_uString_getStr(ustrPassword), + rtl_uString_getLength(ustrPassword), + RTL_TEXTENCODING_UTF8, + OUSTRING_TO_OSTRING_CVTFLAGS ); + pszPassword = rtl_string_getStr(strPassword); + } + + + Error=osl_psz_loginUser(pszUserName,pszPassword,pSecurity); + + if ( strUserName != 0 ) + { + rtl_string_release(strUserName); + } + + if ( strPassword) + { + rtl_string_release(strPassword); + } + + + return Error; +} + + +static oslSecurityError SAL_CALL +osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd, + oslSecurity* pSecurity) +{ + (void)pszUserName; + (void)pszPasswd; + (void)pSecurity; + + return osl_Security_E_None; +} + +oslSecurityError SAL_CALL osl_loginUserOnFileServer( + rtl_uString *strUserName, + rtl_uString *strPasswd, + rtl_uString *strFileServer, + oslSecurity *pSecurity + ) +{ + (void) strUserName; /* unused */ + (void) strPasswd; /* unused */ + (void) strFileServer; /* unused */ + (void) pSecurity; /* unused */ + return osl_Security_E_UserUnknown; +} + + +sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent) +{ + sal_Bool bRet=sal_False; + sal_Char pszIdent[1024]; + + pszIdent[0] = '\0'; + + bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent)); + + rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); + OSL_ASSERT(*ustrIdent != NULL); + + return bRet; +} + + +sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax) +{ + sal_Char buffer[32]; + sal_Int32 nChr; + + oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; + + if (pSecImpl == NULL) + return sal_False; + + nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid); + if ( nChr < 0 || sal::static_int_cast(nChr) >= sizeof(buffer) + || sal::static_int_cast(nChr) >= nMax ) + return sal_False; /* leave *pszIdent unmodified in case of failure */ + + memcpy(pszIdent, buffer, nChr+1); + return sal_True; +} + +sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName) +{ + sal_Bool bRet=sal_False; + sal_Char pszName[1024]; + + pszName[0] = '\0'; + + bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName)); + + rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); + OSL_ASSERT(*ustrName != NULL); + + return bRet; +} + + + +static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax) +{ + oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; + + if (pSecImpl == NULL || pSecImpl->m_pPasswd.pw_name == NULL) + return sal_False; + + strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax); + + return sal_True; +} + +sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory) +{ + sal_Bool bRet=sal_False; + sal_Char pszDirectory[PATH_MAX]; + + pszDirectory[0] = '\0'; + + bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory)); + + if ( bRet == sal_True ) + { + rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); + OSL_ASSERT(*pustrDirectory != NULL); + osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory ); + } + + return bRet; +} + +static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) +{ + oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; + + if (pSecImpl == NULL) + return sal_False; + +#ifdef ANDROID +{ + sal_Bool bRet = sal_False; + rtl_uString *pName = 0, *pValue = 0; + + rtl_uString_newFromAscii(&pName, "HOME"); + + if (rtl_bootstrap_get(pName, &pValue, NULL)) + { + rtl_String *pStrValue = 0; + if (pValue && pValue->length > 0) + { + rtl_uString2String(&pStrValue, pValue->buffer, + pValue->length, RTL_TEXTENCODING_UTF8, + OUSTRING_TO_OSTRING_CVTFLAGS); + if (pStrValue && pStrValue->length > 0) + { + sal_Int32 nCopy = (sal_Int32)(nMax-1) < pStrValue->length ? (sal_Int32)(nMax-1) : pStrValue->length ; + strncpy (pszDirectory, pStrValue->buffer, nCopy); + pszDirectory[nCopy] = '\0'; + bRet = (size_t)pStrValue->length < nMax; + } + rtl_string_release(pStrValue); + } + rtl_uString_release(pName); + } + if (bRet) + return bRet; +} +#endif + + /* if current user, check also environment for HOME */ + if (getuid() == pSecImpl->m_pPasswd.pw_uid) + { + sal_Char *pStr = NULL; +#ifdef SOLARIS + char buffer[8192]; + + struct passwd pwd; + struct passwd *ppwd; + +#ifdef _POSIX_PTHREAD_SEMANTICS + if ( 0 != getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &ppwd ) ) + ppwd = NULL; +#else + ppwd = getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer) ); +#endif + + if ( ppwd ) + pStr = ppwd->pw_dir; +#else + pStr = getenv("HOME"); +#endif + + if (pStr != NULL && strlen(pStr) > 0 && access(pStr, 0) == 0) + strncpy(pszDirectory, pStr, nMax); + else if (pSecImpl->m_pPasswd.pw_dir != NULL) + strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax); + else + return sal_False; + } + else if (pSecImpl->m_pPasswd.pw_dir != NULL) + strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax); + else + return sal_False; + + return sal_True; +} + +sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory) +{ + sal_Bool bRet = sal_False; + sal_Char pszDirectory[PATH_MAX]; + + pszDirectory[0] = '\0'; + + bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory)); + + if ( bRet == sal_True ) + { + rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS ); + OSL_ASSERT(*pustrDirectory != NULL); + osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory ); + } + + return bRet; +} + +#if !defined(MACOSX) && !defined(IOS) + +#define DOT_CONFIG "/.config" + +static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) +{ + sal_Char *pStr = getenv("XDG_CONFIG_HOME"); + + if (pStr == NULL || strlen(pStr) == 0 || access(pStr, 0) != 0) + { + size_t n = 0; + sal_Bool dirOK = sal_True; + + // a default equal to $HOME/.config should be used. + if (!osl_psz_getHomeDir(Security, pszDirectory, nMax)) + return sal_False; + n = strlen(pszDirectory); + if (n + sizeof(DOT_CONFIG) < nMax) + { + strncpy(pszDirectory+n, DOT_CONFIG, sizeof(DOT_CONFIG)); + + // try to create dir if not present + if (access(pszDirectory, F_OK) != 0 && mkdir(pszDirectory, S_IRWXU) != 0) + dirOK = sal_False; + else + { + // check file type and permissions + struct stat st; + if (stat(pszDirectory, &st) != 0) + { + OSL_TRACE("Could not stat $HOME/.config"); + dirOK = sal_False; + } + else + { + if (!S_ISDIR(st.st_mode)) + { + OSL_TRACE("$HOME/.config is not a directory"); + dirOK = sal_False; + } + if (!(st.st_mode & S_IRUSR && st.st_mode & S_IWUSR && st.st_mode & S_IXUSR)) + { + OSL_TRACE("$HOME/.config has bad permissions"); + dirOK = sal_False; + } + } + } + + // resort to HOME + if (dirOK == sal_False) + pszDirectory[n] = '\0'; + } + } + else + strncpy(pszDirectory, pStr, nMax); + + return sal_True; +} + +#undef DOT_CONFIG + +#else + +/* + * FIXME: rewrite to use more flexible + * NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) + * as soon as we can bumb the baseline to Tiger (for NSApplicationSupportDirectory) and have + * support for Objective-C in the build environment + */ + +#define MACOSX_CONFIG_DIR "/Library/Application Support" /* Used on iOS, too */ +static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax) +{ + if( osl_psz_getHomeDir(Security, pszDirectory, nMax - sizeof(MACOSX_CONFIG_DIR) + 1) ) + { + strcat( pszDirectory, MACOSX_CONFIG_DIR ); + return sal_True; + } + + return sal_False; +} + +#endif + +sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security) +{ + oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security; + + if (pSecImpl == NULL) + return sal_False; + + if (pSecImpl->m_pPasswd.pw_uid != 0) + return sal_False; + + return sal_True; +} + +void SAL_CALL osl_freeSecurityHandle(oslSecurity Security) +{ + deleteSecurityImpl(static_cast(Security)); +} + + +sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security) +{ + (void) Security; /* unused */ + return sal_False; +} + +void SAL_CALL osl_unloadUserProfile(oslSecurity Security) +{ + (void) Security; /* unused */ +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit