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-27 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         138 : static sal_Bool sysconf_SC_GETPW_R_SIZE_MAX(size_t * value) {
      57             : #if defined _SC_GETPW_R_SIZE_MAX
      58             :     long m;
      59         138 :     errno = 0;
      60         138 :     m = sysconf(_SC_GETPW_R_SIZE_MAX);
      61         138 :     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         138 :         *value = (size_t) m;
      69         138 :         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         138 : static oslSecurityImpl * growSecurityImpl(
      78             :     oslSecurityImpl * impl, size_t * bufSize)
      79             : {
      80         138 :     size_t n = 0;
      81         138 :     oslSecurityImpl * p = NULL;
      82         138 :     if (impl == NULL) {
      83         138 :         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         138 :     if (n != 0) {
      92         138 :         if (n <= SIZE_MAX - offsetof(oslSecurityImpl, m_buffer)) {
      93         138 :             *bufSize = n;
      94         138 :             n += offsetof(oslSecurityImpl, m_buffer);
      95             :         } else {
      96           0 :             *bufSize = SIZE_MAX - offsetof(oslSecurityImpl, m_buffer);
      97           0 :             n = SIZE_MAX;
      98             :         }
      99         138 :         p = realloc(impl, n);
     100         138 :         memset (p, 0, n);
     101             :     }
     102         138 :     if (p == NULL) {
     103           0 :         free(impl);
     104             :     }
     105         138 :     return p;
     106             : }
     107             : 
     108         135 : static void deleteSecurityImpl(oslSecurityImpl * impl) {
     109         135 :     free(impl);
     110         135 : }
     111             : 
     112         138 : oslSecurity SAL_CALL osl_getCurrentSecurity()
     113             : {
     114         138 :     size_t n = 0;
     115         138 :     oslSecurityImpl * p = NULL;
     116             :     for (;;) {
     117             :         struct passwd * found;
     118         138 :         p = growSecurityImpl(p, &n);
     119         138 :         if (p == NULL) {
     120           0 :             return NULL;
     121             :         }
     122         138 :         switch (getpwuid_r(getuid(), &p->m_pPasswd, p->m_buffer, n, &found)) {
     123             :         case ERANGE:
     124           0 :             break;
     125             :         case 0:
     126         138 :             if (found != NULL) {
     127         138 :                 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           1 : 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           1 :     return osl_Security_E_UserUnknown;
     211             : }
     212             : 
     213             : 
     214           1 : sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent)
     215             : {
     216           1 :     sal_Bool bRet=sal_False;
     217             :     sal_Char pszIdent[1024];
     218             : 
     219           1 :     pszIdent[0] = '\0';
     220             : 
     221           1 :     bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent));
     222             : 
     223           1 :     rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     224             :     OSL_ASSERT(*ustrIdent != NULL);
     225             : 
     226           1 :     return bRet;
     227             : }
     228             : 
     229             : 
     230           1 : 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           1 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     236             : 
     237           1 :     if (pSecImpl == NULL)
     238           0 :         return sal_False;
     239             : 
     240           1 :     nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid);
     241           1 :     if ( nChr < 0 || SAL_INT_CAST(sal_uInt32, nChr) >= sizeof(buffer)
     242           1 :          || SAL_INT_CAST(sal_uInt32, nChr) >= nMax )
     243           0 :         return sal_False; /* leave *pszIdent unmodified in case of failure */
     244             : 
     245           1 :     memcpy(pszIdent, buffer, nChr+1);
     246           1 :     return sal_True;
     247             : }
     248             : 
     249          13 : sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName)
     250             : {
     251          13 :     sal_Bool bRet=sal_False;
     252             :     sal_Char pszName[1024];
     253             : 
     254          13 :     pszName[0] = '\0';
     255             : 
     256          13 :     bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName));
     257             : 
     258          13 :     rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     259             :     OSL_ASSERT(*ustrName != NULL);
     260             : 
     261          13 :     return bRet;
     262             : }
     263             : 
     264             : 
     265             : 
     266          13 : static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax)
     267             : {
     268          13 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     269             : 
     270          13 :     if (pSecImpl == NULL || pSecImpl->m_pPasswd.pw_name == NULL)
     271           0 :         return sal_False;
     272             : 
     273          13 :     strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax);
     274             : 
     275          13 :     return sal_True;
     276             : }
     277             : 
     278         114 : sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
     279             : {
     280         114 :     sal_Bool bRet=sal_False;
     281             :     sal_Char pszDirectory[PATH_MAX];
     282             : 
     283         114 :     pszDirectory[0] = '\0';
     284             : 
     285         114 :     bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory));
     286             : 
     287         114 :     if ( bRet == sal_True )
     288             :     {
     289         114 :         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     290             :         OSL_ASSERT(*pustrDirectory != NULL);
     291         114 :         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
     292             :     }
     293             : 
     294         114 :     return bRet;
     295             : }
     296             : 
     297         115 : static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
     298             : {
     299         115 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     300             : 
     301         115 :     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         115 :     if (getuid() == pSecImpl->m_pPasswd.pw_uid)
     337             :     {
     338         115 :         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         115 :         pStr = getenv("HOME");
     356             : #endif
     357             : 
     358         115 :         if (pStr != NULL && strlen(pStr) > 0 && access(pStr, 0) == 0)
     359         115 :             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         115 :     return sal_True;
     371             : }
     372             : 
     373           1 : sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory)
     374             : {
     375           1 :     sal_Bool bRet = sal_False;
     376             :     sal_Char pszDirectory[PATH_MAX];
     377             : 
     378           1 :     pszDirectory[0] = '\0';
     379             : 
     380           1 :     bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory));
     381             : 
     382           1 :     if ( bRet == sal_True )
     383             :     {
     384           1 :         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
     385             :         OSL_ASSERT(*pustrDirectory != NULL);
     386           1 :         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
     387             :     }
     388             : 
     389           1 :     return bRet;
     390             : }
     391             : 
     392             : #ifndef MACOSX
     393             : 
     394             : #define DOT_CONFIG "/.config"
     395             : 
     396           1 : static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
     397             : {
     398           1 :     sal_Char *pStr = getenv("XDG_CONFIG_HOME");
     399             : 
     400           1 :     if (pStr == NULL || strlen(pStr) == 0 || access(pStr, 0) != 0)
     401           1 :     {
     402           1 :         size_t n = 0;
     403             :         // a default equal to $HOME/.config should be used.
     404           1 :         if (!osl_psz_getHomeDir(Security, pszDirectory, nMax))
     405           0 :             return sal_False;
     406           1 :         n = strlen(pszDirectory);
     407           1 :         if (n + sizeof(DOT_CONFIG) < nMax)
     408             :         {
     409           1 :             strncpy(pszDirectory+n, DOT_CONFIG, sizeof(DOT_CONFIG));
     410           1 :             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           1 :     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           3 : sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security)
     449             : {
     450           3 :     oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
     451             : 
     452           3 :     if (pSecImpl == NULL)
     453           0 :         return sal_False;
     454             : 
     455           3 :     if (pSecImpl->m_pPasswd.pw_uid != 0)
     456           0 :         return sal_False;
     457             : 
     458           3 :     return sal_True;
     459             : }
     460             : 
     461         135 : void SAL_CALL osl_freeSecurityHandle(oslSecurity Security)
     462             : {
     463         135 :     deleteSecurityImpl(Security);
     464         135 : }
     465             : 
     466             : 
     467           1 : sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security)
     468             : {
     469             :     (void) Security; /* unused */
     470           1 :     return sal_False;
     471             : }
     472             : 
     473           1 : void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
     474             : {
     475             :     (void) Security; /* unused */
     476           1 : }
     477             : 
     478             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10