LCOV - code coverage report
Current view: top level - libreoffice/sal/osl/unx - security.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 105 153 68.6 %
Date: 2012-12-17 Functions: 17 19 89.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <stddef.h>
      21             : 
      22             : /* Solaris 8 has no C99 stdint.h, and Solaris generally seems not to miss it for
      23             :    SIZE_MAX: */
      24             : #if !defined __SUNPRO_C
      25             : #include <stdint.h>
      26             : #endif
      27             : 
      28             : #include "system.h"
      29             : 
      30             : #include <osl/security.h>
      31             : #include <osl/diagnose.h>
      32             : #include <rtl/bootstrap.h>
      33             : 
      34             : #include "osl/thread.h"
      35             : #include "osl/file.h"
      36             : 
      37             : #if defined LINUX || defined SOLARIS
      38             : #include <crypt.h>
      39             : #endif
      40             : 
      41             : #include "secimpl.h"
      42             : 
      43             : #ifdef ANDROID
      44             : #define getpwuid_r(uid, pwd, buf, buflen, result) (*(result) = getpwuid(uid), (*(result) ? (memcpy (buf, *(result), sizeof (struct passwd)), 0) : errno))
      45             : #endif
      46             : 
      47             : 
      48             : static oslSecurityError SAL_CALL
      49             : osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd,
      50             :                   oslSecurity* pSecurity);
      51             : sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax);
      52             : static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax);
      53             : static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
      54             : static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
      55             : 
      56       21520 : static sal_Bool sysconf_SC_GETPW_R_SIZE_MAX(size_t * value) {
      57             : #if defined _SC_GETPW_R_SIZE_MAX
      58             :     long m;
      59       21520 :     errno = 0;
      60       21520 :     m = sysconf(_SC_GETPW_R_SIZE_MAX);
      61       21520 :     if (m == -1) {
      62             :         /* _SC_GETPW_R_SIZE_MAX has no limit; some platforms like certain
      63             :            FreeBSD versions support sysconf(_SC_GETPW_R_SIZE_MAX) in a broken
      64             :            way and always set EINVAL, so be resilient here: */
      65           0 :         return sal_False;
      66             :     } else {
      67             :         OSL_ASSERT(m >= 0 && (unsigned long) m < SIZE_MAX);
      68       21520 :         *value = (size_t) m;
      69       21520 :         return sal_True;
      70             :     }
      71             : #else
      72             :     /* some platforms like Mac OS X 1.3 do not define _SC_GETPW_R_SIZE_MAX: */
      73             :     return sal_False;
      74             : #endif
      75             : }
      76             : 
      77       21520 : static oslSecurityImpl * growSecurityImpl(
      78             :     oslSecurityImpl * impl, size_t * bufSize)
      79             : {
      80       21520 :     size_t n = 0;
      81       21520 :     oslSecurityImpl * p = NULL;
      82       21520 :     if (impl == NULL) {
      83       21520 :         if (!sysconf_SC_GETPW_R_SIZE_MAX(&n)) {
      84             :             /* choose something sensible (the callers of growSecurityImpl will
      85             :                detect it if the allocated buffer is too small: */
      86           0 :             n = 1024;
      87             :         }
      88           0 :     } else if (*bufSize <= SIZE_MAX / 2) {
      89           0 :         n = 2 * *bufSize;
      90             :     }
      91       21520 :     if (n != 0) {
      92       21520 :         if (n <= SIZE_MAX - offsetof(oslSecurityImpl, m_buffer)) {
      93       21520 :             *bufSize = n;
      94       21520 :             n += offsetof(oslSecurityImpl, m_buffer);
      95             :         } else {
      96           0 :             *bufSize = SIZE_MAX - offsetof(oslSecurityImpl, m_buffer);
      97           0 :             n = SIZE_MAX;
      98             :         }
      99       21520 :         p = realloc(impl, n);
     100       21520 :         memset (p, 0, n);
     101             :     }
     102       21520 :     if (p == NULL) {
     103           0 :         free(impl);
     104             :     }
     105       21520 :     return p;
     106             : }
     107             : 
     108       21514 : static void deleteSecurityImpl(oslSecurityImpl * impl) {
     109       21514 :     free(impl);
     110       21514 : }
     111             : 
     112       21520 : oslSecurity SAL_CALL osl_getCurrentSecurity()
     113             : {
     114       21520 :     size_t n = 0;
     115       21520 :     oslSecurityImpl * p = NULL;
     116             :     for (;;) {
     117             :         struct passwd * found;
     118       21520 :         p = growSecurityImpl(p, &n);
     119       21520 :         if (p == NULL) {
     120           0 :             return NULL;
     121             :         }
     122       21520 :         switch (getpwuid_r(getuid(), &p->m_pPasswd, p->m_buffer, n, &found)) {
     123             :         case ERANGE:
     124           0 :             break;
     125             :         case 0:
     126       21520 :             if (found != NULL) {
     127       21520 :                 return p;
     128             :             }
     129             :             /* fall through */
     130             :         default:
     131           0 :             deleteSecurityImpl(p);
     132           0 :             return NULL;
     133             :         }
     134           0 :     }
     135             : }
     136             : 
     137           0 : oslSecurityError SAL_CALL osl_loginUser(
     138             :     rtl_uString *ustrUserName,
     139             :     rtl_uString *ustrPassword,
     140             :     oslSecurity *pSecurity
     141             :     )
     142             : {
     143             :     oslSecurityError Error;
     144           0 :     rtl_String* strUserName=0;
     145           0 :     rtl_String* strPassword=0;
     146           0 :     sal_Char* pszUserName=0;
     147           0 :     sal_Char* pszPassword=0;
     148             : 
     149           0 :     if ( ustrUserName != 0 )
     150             :     {
     151           0 :         rtl_uString2String( &strUserName,
     152           0 :                             rtl_uString_getStr(ustrUserName),
     153             :                             rtl_uString_getLength(ustrUserName),
     154             :                             RTL_TEXTENCODING_UTF8,
     155             :                             OUSTRING_TO_OSTRING_CVTFLAGS );
     156           0 :         pszUserName = rtl_string_getStr(strUserName);
     157             :     }
     158             : 
     159             : 
     160           0 :     if ( ustrPassword != 0 )
     161             :     {
     162           0 :         rtl_uString2String( &strPassword,
     163           0 :                             rtl_uString_getStr(ustrPassword),
     164             :                             rtl_uString_getLength(ustrPassword),
     165             :                             RTL_TEXTENCODING_UTF8,
     166             :                             OUSTRING_TO_OSTRING_CVTFLAGS );
     167           0 :         pszPassword = rtl_string_getStr(strPassword);
     168             :     }
     169             : 
     170             : 
     171           0 :     Error=osl_psz_loginUser(pszUserName,pszPassword,pSecurity);
     172             : 
     173           0 :     if ( strUserName != 0 )
     174             :     {
     175           0 :         rtl_string_release(strUserName);
     176             :     }
     177             : 
     178           0 :     if ( strPassword)
     179             :     {
     180           0 :         rtl_string_release(strPassword);
     181             :     }
     182             : 
     183             : 
     184           0 :     return Error;
     185             : }
     186             : 
     187             : 
     188             : static oslSecurityError SAL_CALL
     189           0 : osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd,
     190             :                oslSecurity* pSecurity)
     191             : {
     192             :     (void)pszUserName;
     193             :     (void)pszPasswd;
     194             :     (void)pSecurity;
     195             : 
     196           0 :     return osl_Security_E_None;
     197             : }
     198             : 
     199           2 : oslSecurityError SAL_CALL osl_loginUserOnFileServer(
     200             :     rtl_uString *strUserName,
     201             :     rtl_uString *strPasswd,
     202             :     rtl_uString *strFileServer,
     203             :     oslSecurity *pSecurity
     204             :     )
     205             : {
     206             :     (void) strUserName; /* unused */
     207             :     (void) strPasswd; /* unused */
     208             :     (void) strFileServer; /* unused */
     209             :     (void) pSecurity; /* unused */
     210           2 :     return osl_Security_E_UserUnknown;
     211             : }
     212             : 
     213             : 
     214           2 : sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent)
     215             : {
     216           2 :     sal_Bool bRet=sal_False;
     217             :     sal_Char pszIdent[1024];
     218             : 
     219           2 :     pszIdent[0] = '\0';
     220             : 
     221           2 :     bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent));
     222             : 
     223           2 :     rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     224             :     OSL_ASSERT(*ustrIdent != NULL);
     225             : 
     226           2 :     return bRet;
     227             : }
     228             : 
     229             : 
     230       21042 : sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax)
     231             : {
     232             :     sal_Char  buffer[32];
     233             :     sal_Int32 nChr;
     234             : 
     235       21042 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     236             : 
     237       21042 :     if (pSecImpl == NULL)
     238           0 :         return sal_False;
     239             : 
     240       21042 :     nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid);
     241       21042 :     if ( nChr < 0 || SAL_INT_CAST(sal_uInt32, nChr) >= sizeof(buffer)
     242       21042 :          || SAL_INT_CAST(sal_uInt32, nChr) >= nMax )
     243           0 :         return sal_False; /* leave *pszIdent unmodified in case of failure */
     244             : 
     245       21042 :     memcpy(pszIdent, buffer, nChr+1);
     246       21042 :     return sal_True;
     247             : }
     248             : 
     249          29 : sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName)
     250             : {
     251          29 :     sal_Bool bRet=sal_False;
     252             :     sal_Char pszName[1024];
     253             : 
     254          29 :     pszName[0] = '\0';
     255             : 
     256          29 :     bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName));
     257             : 
     258          29 :     rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     259             :     OSL_ASSERT(*ustrName != NULL);
     260             : 
     261          29 :     return bRet;
     262             : }
     263             : 
     264             : 
     265             : 
     266          29 : static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax)
     267             : {
     268          29 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     269             : 
     270          29 :     if (pSecImpl == NULL || pSecImpl->m_pPasswd.pw_name == NULL)
     271           0 :         return sal_False;
     272             : 
     273          29 :     strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax);
     274             : 
     275          29 :     return sal_True;
     276             : }
     277             : 
     278         429 : sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
     279             : {
     280         429 :     sal_Bool bRet=sal_False;
     281             :     sal_Char pszDirectory[PATH_MAX];
     282             : 
     283         429 :     pszDirectory[0] = '\0';
     284             : 
     285         429 :     bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory));
     286             : 
     287         429 :     if ( bRet == sal_True )
     288             :     {
     289         429 :         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     290             :         OSL_ASSERT(*pustrDirectory != NULL);
     291         429 :         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
     292             :     }
     293             : 
     294         429 :     return bRet;
     295             : }
     296             : 
     297         431 : static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
     298             : {
     299         431 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     300             : 
     301         431 :     if (pSecImpl == NULL)
     302           0 :         return sal_False;
     303             : 
     304             : #ifdef ANDROID
     305             : {
     306             :     sal_Bool bRet = sal_False;
     307             :     rtl_uString *pName = 0, *pValue = 0;
     308             : 
     309             :     rtl_uString_newFromAscii(&pName, "HOME");
     310             : 
     311             :     if (rtl_bootstrap_get(pName, &pValue, NULL))
     312             :     {
     313             :         rtl_String *pStrValue = 0;
     314             :         if (pValue && pValue->length > 0)
     315             :         {
     316             :             rtl_uString2String(&pStrValue, pValue->buffer,
     317             :                                pValue->length, RTL_TEXTENCODING_UTF8,
     318             :                                OUSTRING_TO_OSTRING_CVTFLAGS);
     319             :             if (pStrValue && pStrValue->length > 0)
     320             :             {
     321             :                 sal_Int32 nCopy = (sal_Int32)(nMax-1) < pStrValue->length ? (sal_Int32)(nMax-1) : pStrValue->length ;
     322             :                 strncpy (pszDirectory, pStrValue->buffer, nCopy);
     323             :                 pszDirectory[nCopy] = '\0';
     324             :                 bRet = (size_t)pStrValue->length < nMax;
     325             :             }
     326             :             rtl_string_release(pStrValue);
     327             :         }
     328             :         rtl_uString_release(pName);
     329             :     }
     330             :     if (bRet)
     331             :         return bRet;
     332             : }
     333             : #endif
     334             : 
     335             :     /* if current user, check also environment for HOME */
     336         431 :     if (getuid() == pSecImpl->m_pPasswd.pw_uid)
     337             :     {
     338         431 :         sal_Char *pStr = NULL;
     339             : #ifdef SOLARIS
     340             :         char    buffer[8192];
     341             : 
     342             :         struct passwd pwd;
     343             :         struct passwd *ppwd;
     344             : 
     345             : #ifdef _POSIX_PTHREAD_SEMANTICS
     346             :         if ( 0 != getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &ppwd ) )
     347             :             ppwd = NULL;
     348             : #else
     349             :         ppwd = getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer) );
     350             : #endif
     351             : 
     352             :         if ( ppwd )
     353             :             pStr = ppwd->pw_dir;
     354             : #else
     355         431 :         pStr = getenv("HOME");
     356             : #endif
     357             : 
     358         431 :         if (pStr != NULL && strlen(pStr) > 0 && access(pStr, 0) == 0)
     359         431 :             strncpy(pszDirectory, pStr, nMax);
     360           0 :         else if (pSecImpl->m_pPasswd.pw_dir != NULL)
     361           0 :             strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
     362             :         else
     363           0 :             return sal_False;
     364             :     }
     365           0 :     else if (pSecImpl->m_pPasswd.pw_dir != NULL)
     366           0 :         strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
     367             :     else
     368           0 :         return sal_False;
     369             : 
     370         431 :     return sal_True;
     371             : }
     372             : 
     373           2 : sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory)
     374             : {
     375           2 :     sal_Bool bRet = sal_False;
     376             :     sal_Char pszDirectory[PATH_MAX];
     377             : 
     378           2 :     pszDirectory[0] = '\0';
     379             : 
     380           2 :     bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory));
     381             : 
     382           2 :     if ( bRet == sal_True )
     383             :     {
     384           2 :         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     385             :         OSL_ASSERT(*pustrDirectory != NULL);
     386           2 :         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
     387             :     }
     388             : 
     389           2 :     return bRet;
     390             : }
     391             : 
     392             : #ifndef MACOSX
     393             : 
     394             : #define DOT_CONFIG "/.config"
     395             : 
     396           2 : static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
     397             : {
     398           2 :     sal_Char *pStr = getenv("XDG_CONFIG_HOME");
     399             : 
     400           2 :     if (pStr == NULL || strlen(pStr) == 0 || access(pStr, 0) != 0)
     401           2 :     {
     402           2 :         size_t n = 0;
     403             :         // a default equal to $HOME/.config should be used.
     404           2 :         if (!osl_psz_getHomeDir(Security, pszDirectory, nMax))
     405           0 :             return sal_False;
     406           2 :         n = strlen(pszDirectory);
     407           2 :         if (n + sizeof(DOT_CONFIG) < nMax)
     408             :         {
     409           2 :             strncpy(pszDirectory+n, DOT_CONFIG, sizeof(DOT_CONFIG));
     410           2 :             if (access(pszDirectory, 0) != 0)
     411             :             {
     412             :                 // resort to HOME
     413           0 :                 pszDirectory[n] = '\0';
     414             :             }
     415             :         }
     416             :     }
     417             :     else
     418           0 :         strncpy(pszDirectory, pStr, nMax);
     419             : 
     420           2 :     return sal_True;
     421             : }
     422             : 
     423             : #undef DOT_CONFIG
     424             : 
     425             : #else
     426             : 
     427             : /*
     428             :  * FIXME: rewrite to use more flexible
     429             :  * NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)
     430             :  * as soon as we can bumb the baseline to Tiger (for NSApplicationSupportDirectory) and have
     431             :  * support for Objective-C in the build environment
     432             :  */
     433             : 
     434             : #define MACOSX_CONFIG_DIR "/Library/Application Support"
     435             : static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
     436             : {
     437             :     if( osl_psz_getHomeDir(Security, pszDirectory, nMax - sizeof(MACOSX_CONFIG_DIR) + 1) )
     438             :     {
     439             :         strcat( pszDirectory, MACOSX_CONFIG_DIR );
     440             :         return sal_True;
     441             :     }
     442             : 
     443             :     return sal_False;
     444             : }
     445             : 
     446             : #endif
     447             : 
     448           6 : sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security)
     449             : {
     450           6 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     451             : 
     452           6 :     if (pSecImpl == NULL)
     453           0 :         return sal_False;
     454             : 
     455           6 :     if (pSecImpl->m_pPasswd.pw_uid != 0)
     456           0 :         return sal_False;
     457             : 
     458           6 :     return sal_True;
     459             : }
     460             : 
     461       21514 : void SAL_CALL osl_freeSecurityHandle(oslSecurity Security)
     462             : {
     463       21514 :     deleteSecurityImpl(Security);
     464       21514 : }
     465             : 
     466             : 
     467           2 : sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security)
     468             : {
     469             :     (void) Security; /* unused */
     470           2 :     return sal_False;
     471             : }
     472             : 
     473           2 : void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
     474             : {
     475             :     (void) Security; /* unused */
     476           2 : }
     477             : 
     478             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10