xref: /aoo41x/main/sal/osl/os2/security.c (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 
29 #include "system.h"
30 
31 #include <osl/security.h>
32 #include <osl/diagnose.h>
33 #include <osl/module.h>
34 
35 #include "osl/thread.h"
36 #include "osl/file.h"
37 
38 #ifdef SOLARIS
39 #include <crypt.h>
40 #endif
41 
42 #include "secimpl.h"
43 
44 #ifndef PAM_BINARY_MSG
45 #define PAM_BINARY_MSG 6
46 #endif
47 
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);
57 
58 
59 
60 oslSecurity SAL_CALL osl_getCurrentSecurity()
61 {
62 
63 	oslSecurityImpl *pSecImpl = (oslSecurityImpl*) malloc(sizeof(oslSecurityImpl));
64 	struct passwd	*pPasswd  = getpwuid(getuid());
65 
66 	if (pPasswd)
67 	{
68 		memcpy(&pSecImpl->m_pPasswd, pPasswd, sizeof(pSecImpl->m_pPasswd));
69 		pSecImpl->m_isValid = sal_True;
70 	}
71 	else
72 	{
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
80 		{
81 			pSecImpl->m_pPasswd.pw_name		= "unknown";
82 			pSecImpl->m_pPasswd.pw_dir		= "/tmp";
83 			pSecImpl->m_isValid				= sal_False;
84 		}
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";
90 	}
91 
92 
93 	return ((oslSecurity)pSecImpl);
94 }
95 
96 
97 oslSecurityError SAL_CALL osl_loginUser(
98     rtl_uString *ustrUserName,
99 	rtl_uString *ustrPassword,
100 	oslSecurity *pSecurity
101 	)
102 {
103 	oslSecurityError ret;
104 
105 	*pSecurity = osl_getCurrentSecurity();
106 	ret = osl_Security_E_None;
107 
108     return ret;
109 }
110 
111 
112 
113 oslSecurityError SAL_CALL osl_loginUserOnFileServer(
114     rtl_uString *strUserName,
115 	rtl_uString *strPasswd,
116 	rtl_uString *strFileServer,
117 	oslSecurity *pSecurity
118 	)
119 {
120 	oslSecurityError erg;
121 	return erg = osl_Security_E_UserUnknown;
122 }
123 
124 
125 oslSecurityError SAL_CALL osl_psz_loginUserOnFileServer( const sal_Char*  pszUserName,
126                                                      const sal_Char*  pszPasswd,
127                                                      const sal_Char*  pszFileServer,
128                                                      oslSecurity*     pSecurity )
129 {
130 	oslSecurityError erg;
131 	return erg = osl_Security_E_UserUnknown;
132 }
133 
134 sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent)
135 {
136     sal_Bool bRet=sal_False;
137     sal_Char pszIdent[1024];
138 
139     pszIdent[0] = '\0';
140 
141     bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent));
142 
143     rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
144     OSL_ASSERT(*ustrIdent != NULL);
145 
146     return bRet;
147 }
148 
149 
150 sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax)
151 {
152 	sal_Char  buffer[32];
153     sal_Int32 nChr;
154 
155 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
156 
157     if (pSecImpl == NULL)
158 	    return sal_False;
159 
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 */
163 
164 	memcpy(pszIdent, buffer, nChr+1);
165     return sal_True;
166 }
167 
168 sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName)
169 {
170     sal_Bool bRet=sal_False;
171     sal_Char pszName[1024];
172 
173     pszName[0] = '\0';
174 
175     bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName));
176 
177     rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
178     OSL_ASSERT(*ustrName != NULL);
179 
180     return bRet;
181 }
182 
183 
184 
185 sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax)
186 {
187 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
188 
189     if ((pSecImpl == NULL) || (! pSecImpl->m_isValid))
190 	    return sal_False;
191 
192 	strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax);
193 
194     return sal_True;
195 }
196 
197 sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
198 {
199     sal_Bool bRet=sal_False;
200     sal_Char pszDirectory[PATH_MAX];
201 
202     pszDirectory[0] = '\0';
203 
204     bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory));
205 
206     if ( bRet == sal_True )
207     {
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 );
211     }
212 
213     return bRet;
214 }
215 
216 
217 sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
218 {
219 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
220 
221     if (pSecImpl == NULL)
222 	    return sal_False;
223 
224 	/* if current user, check also environment for HOME */
225 	if (getuid() == pSecImpl->m_pPasswd.pw_uid)
226 	{
227 		sal_Char *pStr = NULL;
228 #ifdef SOLARIS
229 		char	buffer[8192];
230 
231 		struct passwd pwd;
232 		struct passwd *ppwd;
233 
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
240 
241 		if ( ppwd )
242 			pStr = ppwd->pw_dir;
243 #else
244 		pStr = getenv("HOME");
245 #endif
246 
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;
255 	}
256 	else
257 		strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
258 
259 	return sal_True;
260 }
261 
262 sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory)
263 {
264     sal_Bool bRet = sal_False;
265     sal_Char pszDirectory[PATH_MAX];
266 
267     pszDirectory[0] = '\0';
268 
269     bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory));
270 
271     if ( bRet == sal_True )
272     {
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 );
276     }
277 
278     return bRet;
279 }
280 
281 
282 sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
283 {
284 	return (osl_psz_getHomeDir(Security, pszDirectory, nMax));
285 }
286 
287 sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security)
288 {
289 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
290 
291     if (pSecImpl == NULL)
292 	    return sal_False;
293 
294     if (pSecImpl->m_pPasswd.pw_uid != 0)
295 		return (sal_False);
296 
297 	return (sal_True);
298 }
299 
300 void SAL_CALL osl_freeSecurityHandle(oslSecurity Security)
301 {
302 	if (Security)
303 		free ((oslSecurityImpl*)Security);
304 }
305 
306 
307 sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security)
308 {
309     return sal_False;
310 }
311 
312 void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
313 {
314     return;
315 }
316 
317 
318