xref: /aoo42x/main/sal/osl/os2/security.c (revision 647f063d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #include "system.h"
26 
27 #include <osl/security.h>
28 #include <osl/diagnose.h>
29 #include <osl/module.h>
30 
31 #include "osl/thread.h"
32 #include "osl/file.h"
33 
34 #ifdef SOLARIS
35 #include <crypt.h>
36 #endif
37 
38 #include "secimpl.h"
39 
40 #ifndef PAM_BINARY_MSG
41 #define PAM_BINARY_MSG 6
42 #endif
43 
44 extern oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode);
45 extern void* SAL_CALL osl_psz_getSymbol(oslModule hModule, const sal_Char* pszSymbolName);
46 extern oslSecurityError SAL_CALL
47 osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd,
48                   oslSecurity* pSecurity);
49 sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax);
50 sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax);
51 sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
52 sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
53 
54 
55 
osl_getCurrentSecurity()56 oslSecurity SAL_CALL osl_getCurrentSecurity()
57 {
58 
59 	oslSecurityImpl *pSecImpl = (oslSecurityImpl*) malloc(sizeof(oslSecurityImpl));
60 	struct passwd	*pPasswd  = getpwuid(getuid());
61 
62 	if (pPasswd)
63 	{
64 		memcpy(&pSecImpl->m_pPasswd, pPasswd, sizeof(pSecImpl->m_pPasswd));
65 		pSecImpl->m_isValid = sal_True;
66 	}
67 	else
68 	{
69 		/* Some UNIX-OS don't implement getpwuid, e.g. NC OS (special NetBSD) 1.2.1 */
70 		/* so we have to catch this in this else branch */
71 		pSecImpl->m_pPasswd.pw_name		= getenv("USER");
72 		pSecImpl->m_pPasswd.pw_dir		= getenv("HOME");
73 		if (pSecImpl->m_pPasswd.pw_name && pSecImpl->m_pPasswd.pw_dir)
74 			pSecImpl->m_isValid				= sal_True;
75 		else
76 		{
77 			pSecImpl->m_pPasswd.pw_name		= "unknown";
78 			pSecImpl->m_pPasswd.pw_dir		= "/tmp";
79 			pSecImpl->m_isValid				= sal_False;
80 		}
81 		pSecImpl->m_pPasswd.pw_passwd	= NULL;
82 		pSecImpl->m_pPasswd.pw_uid		= getuid();
83 		pSecImpl->m_pPasswd.pw_gid		= getgid();
84 		pSecImpl->m_pPasswd.pw_gecos	= "unknown";
85 		pSecImpl->m_pPasswd.pw_shell	= "unknown";
86 	}
87 
88 
89 	return ((oslSecurity)pSecImpl);
90 }
91 
92 
osl_loginUser(rtl_uString * ustrUserName,rtl_uString * ustrPassword,oslSecurity * pSecurity)93 oslSecurityError SAL_CALL osl_loginUser(
94     rtl_uString *ustrUserName,
95 	rtl_uString *ustrPassword,
96 	oslSecurity *pSecurity
97 	)
98 {
99 	oslSecurityError ret;
100 
101 	*pSecurity = osl_getCurrentSecurity();
102 	ret = osl_Security_E_None;
103 
104     return ret;
105 }
106 
107 
108 
osl_loginUserOnFileServer(rtl_uString * strUserName,rtl_uString * strPasswd,rtl_uString * strFileServer,oslSecurity * pSecurity)109 oslSecurityError SAL_CALL osl_loginUserOnFileServer(
110     rtl_uString *strUserName,
111 	rtl_uString *strPasswd,
112 	rtl_uString *strFileServer,
113 	oslSecurity *pSecurity
114 	)
115 {
116 	oslSecurityError erg;
117 	return erg = osl_Security_E_UserUnknown;
118 }
119 
120 
osl_psz_loginUserOnFileServer(const sal_Char * pszUserName,const sal_Char * pszPasswd,const sal_Char * pszFileServer,oslSecurity * pSecurity)121 oslSecurityError SAL_CALL osl_psz_loginUserOnFileServer( const sal_Char*  pszUserName,
122                                                      const sal_Char*  pszPasswd,
123                                                      const sal_Char*  pszFileServer,
124                                                      oslSecurity*     pSecurity )
125 {
126 	oslSecurityError erg;
127 	return erg = osl_Security_E_UserUnknown;
128 }
129 
osl_getUserIdent(oslSecurity Security,rtl_uString ** ustrIdent)130 sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent)
131 {
132     sal_Bool bRet=sal_False;
133     sal_Char pszIdent[1024];
134 
135     pszIdent[0] = '\0';
136 
137     bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent));
138 
139     rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
140     OSL_ASSERT(*ustrIdent != NULL);
141 
142     return bRet;
143 }
144 
145 
osl_psz_getUserIdent(oslSecurity Security,sal_Char * pszIdent,sal_uInt32 nMax)146 sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax)
147 {
148 	sal_Char  buffer[32];
149     sal_Int32 nChr;
150 
151 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
152 
153     if (pSecImpl == NULL)
154 	    return sal_False;
155 
156 	nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid);
157     if ( nChr < 0 || nChr >= sizeof(buffer) || nChr >= nMax )
158         return sal_False; /* leave *pszIdent unmodified in case of failure */
159 
160 	memcpy(pszIdent, buffer, nChr+1);
161     return sal_True;
162 }
163 
osl_getUserName(oslSecurity Security,rtl_uString ** ustrName)164 sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName)
165 {
166     sal_Bool bRet=sal_False;
167     sal_Char pszName[1024];
168 
169     pszName[0] = '\0';
170 
171     bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName));
172 
173     rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
174     OSL_ASSERT(*ustrName != NULL);
175 
176     return bRet;
177 }
178 
179 
180 
osl_psz_getUserName(oslSecurity Security,sal_Char * pszName,sal_uInt32 nMax)181 sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax)
182 {
183 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
184 
185     if ((pSecImpl == NULL) || (! pSecImpl->m_isValid))
186 	    return sal_False;
187 
188 	strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax);
189 
190     return sal_True;
191 }
192 
osl_getHomeDir(oslSecurity Security,rtl_uString ** pustrDirectory)193 sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
194 {
195     sal_Bool bRet=sal_False;
196     sal_Char pszDirectory[PATH_MAX];
197 
198     pszDirectory[0] = '\0';
199 
200     bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory));
201 
202     if ( bRet == sal_True )
203     {
204         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
205         OSL_ASSERT(*pustrDirectory != NULL);
206         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
207     }
208 
209     return bRet;
210 }
211 
212 
osl_psz_getHomeDir(oslSecurity Security,sal_Char * pszDirectory,sal_uInt32 nMax)213 sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
214 {
215 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
216 
217     if (pSecImpl == NULL)
218 	    return sal_False;
219 
220 	/* if current user, check also environment for HOME */
221 	if (getuid() == pSecImpl->m_pPasswd.pw_uid)
222 	{
223 		sal_Char *pStr = NULL;
224 #ifdef SOLARIS
225 		char	buffer[8192];
226 
227 		struct passwd pwd;
228 		struct passwd *ppwd;
229 
230 #ifdef _POSIX_PTHREAD_SEMANTICS
231 		if ( 0 != getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &ppwd ) )
232 			ppwd = NULL;
233 #else
234 		ppwd = getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer) );
235 #endif
236 
237 		if ( ppwd )
238 			pStr = ppwd->pw_dir;
239 #else
240 		pStr = getenv("HOME");
241 #endif
242 
243 		if ((pStr != NULL) && (strlen(pStr) > 0) &&
244 		    (access(pStr, 0) == 0))
245 			strncpy(pszDirectory, pStr, nMax);
246 		else
247     		if (pSecImpl->m_isValid)
248 				strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
249 			else
250 				return sal_False;
251 	}
252 	else
253 		strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
254 
255 	return sal_True;
256 }
257 
osl_getConfigDir(oslSecurity Security,rtl_uString ** pustrDirectory)258 sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory)
259 {
260     sal_Bool bRet = sal_False;
261     sal_Char pszDirectory[PATH_MAX];
262 
263     pszDirectory[0] = '\0';
264 
265     bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory));
266 
267     if ( bRet == sal_True )
268     {
269         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
270         OSL_ASSERT(*pustrDirectory != NULL);
271         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
272     }
273 
274     return bRet;
275 }
276 
277 
osl_psz_getConfigDir(oslSecurity Security,sal_Char * pszDirectory,sal_uInt32 nMax)278 sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
279 {
280 	return (osl_psz_getHomeDir(Security, pszDirectory, nMax));
281 }
282 
osl_isAdministrator(oslSecurity Security)283 sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security)
284 {
285 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
286 
287     if (pSecImpl == NULL)
288 	    return sal_False;
289 
290     if (pSecImpl->m_pPasswd.pw_uid != 0)
291 		return (sal_False);
292 
293 	return (sal_True);
294 }
295 
osl_freeSecurityHandle(oslSecurity Security)296 void SAL_CALL osl_freeSecurityHandle(oslSecurity Security)
297 {
298 	if (Security)
299 		free ((oslSecurityImpl*)Security);
300 }
301 
302 
osl_loadUserProfile(oslSecurity Security)303 sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security)
304 {
305     return sal_False;
306 }
307 
osl_unloadUserProfile(oslSecurity Security)308 void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
309 {
310     return;
311 }
312 
313 
314