merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / os2 / security.c
blobf03be57acc85be9adbe419af44b3e2a433028891
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 #include "system.h"
31 #include <osl/security.h>
32 #include <osl/diagnose.h>
33 #include <osl/module.h>
35 #include "osl/thread.h"
36 #include "osl/file.h"
38 #ifdef SOLARIS
39 #include <crypt.h>
40 #endif
42 #include "secimpl.h"
44 #ifndef PAM_BINARY_MSG
45 #define PAM_BINARY_MSG 6
46 #endif
48 extern oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode);
49 extern void* SAL_CALL osl_psz_getSymbol(oslModule hModule, const sal_Char* pszSymbolName);
50 extern oslSecurityError SAL_CALL
51 osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd,
52 oslSecurity* pSecurity);
53 sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax);
54 sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax);
55 sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
56 sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
60 oslSecurity SAL_CALL osl_getCurrentSecurity()
63 oslSecurityImpl *pSecImpl = (oslSecurityImpl*) malloc(sizeof(oslSecurityImpl));
64 struct passwd *pPasswd = getpwuid(getuid());
66 if (pPasswd)
68 memcpy(&pSecImpl->m_pPasswd, pPasswd, sizeof(pSecImpl->m_pPasswd));
69 pSecImpl->m_isValid = sal_True;
71 else
73 /* Some UNIX-OS don't implement getpwuid, e.g. NC OS (special NetBSD) 1.2.1 */
74 /* so we have to catch this in this else branch */
75 pSecImpl->m_pPasswd.pw_name = getenv("USER");
76 pSecImpl->m_pPasswd.pw_dir = getenv("HOME");
77 if (pSecImpl->m_pPasswd.pw_name && pSecImpl->m_pPasswd.pw_dir)
78 pSecImpl->m_isValid = sal_True;
79 else
81 pSecImpl->m_pPasswd.pw_name = "unknown";
82 pSecImpl->m_pPasswd.pw_dir = "/tmp";
83 pSecImpl->m_isValid = sal_False;
85 pSecImpl->m_pPasswd.pw_passwd = NULL;
86 pSecImpl->m_pPasswd.pw_uid = getuid();
87 pSecImpl->m_pPasswd.pw_gid = getgid();
88 pSecImpl->m_pPasswd.pw_gecos = "unknown";
89 pSecImpl->m_pPasswd.pw_shell = "unknown";
93 return ((oslSecurity)pSecImpl);
97 oslSecurityError SAL_CALL osl_loginUser(
98 rtl_uString *ustrUserName,
99 rtl_uString *ustrPassword,
100 oslSecurity *pSecurity
103 oslSecurityError ret;
105 *pSecurity = osl_getCurrentSecurity();
106 ret = osl_Security_E_None;
108 return ret;
113 oslSecurityError SAL_CALL osl_loginUserOnFileServer(
114 rtl_uString *strUserName,
115 rtl_uString *strPasswd,
116 rtl_uString *strFileServer,
117 oslSecurity *pSecurity
120 oslSecurityError erg;
121 return erg = osl_Security_E_UserUnknown;
125 oslSecurityError SAL_CALL osl_psz_loginUserOnFileServer( const sal_Char* pszUserName,
126 const sal_Char* pszPasswd,
127 const sal_Char* pszFileServer,
128 oslSecurity* pSecurity )
130 oslSecurityError erg;
131 return erg = osl_Security_E_UserUnknown;
134 sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent)
136 sal_Bool bRet=sal_False;
137 sal_Char pszIdent[1024];
139 pszIdent[0] = '\0';
141 bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent));
143 rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
144 OSL_ASSERT(*ustrIdent != NULL);
146 return bRet;
150 sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax)
152 sal_Char buffer[32];
153 sal_Int32 nChr;
155 oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
157 if (pSecImpl == NULL)
158 return sal_False;
160 nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid);
161 if ( nChr < 0 || nChr >= sizeof(buffer) || nChr >= nMax )
162 return sal_False; /* leave *pszIdent unmodified in case of failure */
164 memcpy(pszIdent, buffer, nChr+1);
165 return sal_True;
168 sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName)
170 sal_Bool bRet=sal_False;
171 sal_Char pszName[1024];
173 pszName[0] = '\0';
175 bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName));
177 rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
178 OSL_ASSERT(*ustrName != NULL);
180 return bRet;
185 sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax)
187 oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
189 if ((pSecImpl == NULL) || (! pSecImpl->m_isValid))
190 return sal_False;
192 strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax);
194 return sal_True;
197 sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
199 sal_Bool bRet=sal_False;
200 sal_Char pszDirectory[PATH_MAX];
202 pszDirectory[0] = '\0';
204 bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory));
206 if ( bRet == sal_True )
208 rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
209 OSL_ASSERT(*pustrDirectory != NULL);
210 osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
213 return bRet;
217 sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
219 oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
221 if (pSecImpl == NULL)
222 return sal_False;
224 /* if current user, check also environment for HOME */
225 if (getuid() == pSecImpl->m_pPasswd.pw_uid)
227 sal_Char *pStr = NULL;
228 #ifdef SOLARIS
229 char buffer[8192];
231 struct passwd pwd;
232 struct passwd *ppwd;
234 #ifdef _POSIX_PTHREAD_SEMANTICS
235 if ( 0 != getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &ppwd ) )
236 ppwd = NULL;
237 #else
238 ppwd = getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer) );
239 #endif
241 if ( ppwd )
242 pStr = ppwd->pw_dir;
243 #else
244 pStr = getenv("HOME");
245 #endif
247 if ((pStr != NULL) && (strlen(pStr) > 0) &&
248 (access(pStr, 0) == 0))
249 strncpy(pszDirectory, pStr, nMax);
250 else
251 if (pSecImpl->m_isValid)
252 strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
253 else
254 return sal_False;
256 else
257 strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
259 return sal_True;
262 sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory)
264 sal_Bool bRet = sal_False;
265 sal_Char pszDirectory[PATH_MAX];
267 pszDirectory[0] = '\0';
269 bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory));
271 if ( bRet == sal_True )
273 rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
274 OSL_ASSERT(*pustrDirectory != NULL);
275 osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
278 return bRet;
282 sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
284 return (osl_psz_getHomeDir(Security, pszDirectory, nMax));
287 sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security)
289 oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
291 if (pSecImpl == NULL)
292 return sal_False;
294 if (pSecImpl->m_pPasswd.pw_uid != 0)
295 return (sal_False);
297 return (sal_True);
300 void SAL_CALL osl_freeSecurityHandle(oslSecurity Security)
302 if (Security)
303 free ((oslSecurityImpl*)Security);
307 sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security)
309 return sal_False;
312 void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
314 return;