xref: /aoo41x/main/sal/osl/unx/security.c (revision 7994814d)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19647f063dSAndrew Rist  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stddef.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir /* Solaris 8 has no C99 stdint.h, and Solaris generally seems not to miss it for
27cdf0e10cSrcweir    SIZE_MAX: */
28cdf0e10cSrcweir #if !defined __SUNPRO_C
29cdf0e10cSrcweir #include <stdint.h>
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "system.h"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <osl/security.h>
35cdf0e10cSrcweir #include <osl/diagnose.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "osl/thread.h"
38cdf0e10cSrcweir #include "osl/file.h"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #if defined LINUX || defined SOLARIS
41cdf0e10cSrcweir #include <crypt.h>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include "secimpl.h"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #ifndef NOPAM
47cdf0e10cSrcweir #ifndef PAM_BINARY_MSG
48cdf0e10cSrcweir #define PAM_BINARY_MSG 6
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir 
52cdf0e10cSrcweir static oslSecurityError SAL_CALL
53cdf0e10cSrcweir osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd,
54cdf0e10cSrcweir                   oslSecurity* pSecurity);
55cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax);
56cdf0e10cSrcweir static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax);
57cdf0e10cSrcweir static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
58cdf0e10cSrcweir static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
59cdf0e10cSrcweir 
sysconf_SC_GETPW_R_SIZE_MAX(size_t * value)60cdf0e10cSrcweir static sal_Bool sysconf_SC_GETPW_R_SIZE_MAX(size_t * value) {
61cdf0e10cSrcweir #if defined _SC_GETPW_R_SIZE_MAX
62cdf0e10cSrcweir     long m;
63cdf0e10cSrcweir     errno = 0;
64cdf0e10cSrcweir     m = sysconf(_SC_GETPW_R_SIZE_MAX);
65cdf0e10cSrcweir     if (m == -1) {
66cdf0e10cSrcweir         /* _SC_GETPW_R_SIZE_MAX has no limit; some platforms like certain
67cdf0e10cSrcweir            FreeBSD versions support sysconf(_SC_GETPW_R_SIZE_MAX) in a broken
68cdf0e10cSrcweir            way and always set EINVAL, so be resilient here: */
69cdf0e10cSrcweir         return sal_False;
70cdf0e10cSrcweir     } else {
71cdf0e10cSrcweir         OSL_ASSERT(m >= 0 && (unsigned long) m < SIZE_MAX);
72cdf0e10cSrcweir         *value = (size_t) m;
73cdf0e10cSrcweir         return sal_True;
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir #else
76cdf0e10cSrcweir     /* some platforms like Mac OS X 1.3 do not define _SC_GETPW_R_SIZE_MAX: */
77cdf0e10cSrcweir     return sal_False;
78cdf0e10cSrcweir #endif
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
growSecurityImpl(oslSecurityImpl * impl,size_t * bufSize)81cdf0e10cSrcweir static oslSecurityImpl * growSecurityImpl(
82cdf0e10cSrcweir     oslSecurityImpl * impl, size_t * bufSize)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     size_t n = 0;
85cdf0e10cSrcweir     oslSecurityImpl * p = NULL;
86cdf0e10cSrcweir     if (impl == NULL) {
87cdf0e10cSrcweir         if (!sysconf_SC_GETPW_R_SIZE_MAX(&n)) {
88cdf0e10cSrcweir             /* choose something sensible (the callers of growSecurityImpl will
89cdf0e10cSrcweir                detect it if the allocated buffer is too small: */
90cdf0e10cSrcweir             n = 1024;
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir     } else if (*bufSize <= SIZE_MAX / 2) {
93cdf0e10cSrcweir         n = 2 * *bufSize;
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir     if (n != 0) {
96cdf0e10cSrcweir         if (n <= SIZE_MAX - offsetof(oslSecurityImpl, m_buffer)) {
97cdf0e10cSrcweir             *bufSize = n;
98cdf0e10cSrcweir             n += offsetof(oslSecurityImpl, m_buffer);
99cdf0e10cSrcweir         } else {
100cdf0e10cSrcweir             *bufSize = SIZE_MAX - offsetof(oslSecurityImpl, m_buffer);
101cdf0e10cSrcweir             n = SIZE_MAX;
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir         p = realloc(impl, n);
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir     if (p == NULL) {
106cdf0e10cSrcweir         free(impl);
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir     return p;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
deleteSecurityImpl(oslSecurityImpl * impl)111cdf0e10cSrcweir static void deleteSecurityImpl(oslSecurityImpl * impl) {
112cdf0e10cSrcweir     free(impl);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
osl_getCurrentSecurity()115cdf0e10cSrcweir oslSecurity SAL_CALL osl_getCurrentSecurity()
116cdf0e10cSrcweir {
117cdf0e10cSrcweir     size_t n = 0;
118cdf0e10cSrcweir 	oslSecurityImpl * p = NULL;
119cdf0e10cSrcweir     for (;;) {
120cdf0e10cSrcweir         struct passwd * found;
121cdf0e10cSrcweir         p = growSecurityImpl(p, &n);
122cdf0e10cSrcweir         if (p == NULL) {
123cdf0e10cSrcweir             return NULL;
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir         switch (getpwuid_r(getuid(), &p->m_pPasswd, p->m_buffer, n, &found)) {
126cdf0e10cSrcweir         case ERANGE:
127cdf0e10cSrcweir             break;
128cdf0e10cSrcweir         case 0:
129cdf0e10cSrcweir             if (found != NULL) {
130cdf0e10cSrcweir                 return p;
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir             /* fall through */
133cdf0e10cSrcweir         default:
134cdf0e10cSrcweir             deleteSecurityImpl(p);
135cdf0e10cSrcweir             return NULL;
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141cdf0e10cSrcweir #if defined LINUX && !defined NOPAM
142cdf0e10cSrcweir 
143cdf0e10cSrcweir /*
144cdf0e10cSrcweir  *
145cdf0e10cSrcweir  * osl Routines for Pluggable Authentication Modules (PAM)
146cdf0e10cSrcweir  * tested with Linux-PAM 0.66 on Redhat-6.0 and
147cdf0e10cSrcweir  * Linux-PAM 0.64 on RedHat-5.2,
148cdf0e10cSrcweir  * XXX Will probably not run on PAM 0.59 or prior, since
149cdf0e10cSrcweir  *     number of pam_response* responses has changed
150cdf0e10cSrcweir  *
151cdf0e10cSrcweir  */
152cdf0e10cSrcweir 
153cdf0e10cSrcweir #include <security/pam_appl.h>
154cdf0e10cSrcweir 
155cdf0e10cSrcweir typedef struct {
156cdf0e10cSrcweir 	char* name;
157cdf0e10cSrcweir 	char* password;
158cdf0e10cSrcweir } sal_PamData;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir typedef struct {
161cdf0e10cSrcweir 	int (*pam_start)(const char *service_name, const char *user,
162cdf0e10cSrcweir 		     	     const struct pam_conv *pam_conversation,
163cdf0e10cSrcweir 		     		 pam_handle_t **pamh);
164cdf0e10cSrcweir 	int (*pam_end)          (pam_handle_t *pamh, int pam_status);
165cdf0e10cSrcweir 	int (*pam_authenticate) (pam_handle_t *pamh, int flags);
166cdf0e10cSrcweir 	int (*pam_acct_mgmt)    (pam_handle_t *pamh, int flags);
167cdf0e10cSrcweir } sal_PamModule;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir /*
170cdf0e10cSrcweir  * Implement a pam-conversation callback-routine,
171cdf0e10cSrcweir  * it just supply name and password instead of prompting the user.
172cdf0e10cSrcweir  * I guess that echo-off means 'ask for password' and echo-on means
173cdf0e10cSrcweir  * 'ask for user-name'. In fact I've never been asked anything else
174cdf0e10cSrcweir  * than the password
175cdf0e10cSrcweir  * XXX Please notice that if a pam-module does ask anything else, we
176cdf0e10cSrcweir  *     are completely lost, and a pam-module is free to do so
177cdf0e10cSrcweir  * XXX
178cdf0e10cSrcweir  */
179cdf0e10cSrcweir 
180cdf0e10cSrcweir static int
osl_PamConversation(int num_msg,const struct pam_message ** msgm,struct pam_response ** response,void * appdata_ptr)181cdf0e10cSrcweir osl_PamConversation (int num_msg, const struct pam_message **msgm,
182cdf0e10cSrcweir                      struct pam_response **response, void *appdata_ptr)
183cdf0e10cSrcweir {
184cdf0e10cSrcweir 	int	  		i;
185cdf0e10cSrcweir 	sal_Bool	error;
186cdf0e10cSrcweir 	sal_PamData         *pam_data;
187cdf0e10cSrcweir 	struct pam_response *p_reply;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 	/* resource initialization */
190cdf0e10cSrcweir 	pam_data = (sal_PamData*) appdata_ptr;
191cdf0e10cSrcweir 	p_reply  = (struct pam_response *) calloc( num_msg,
192cdf0e10cSrcweir 											   sizeof(struct pam_response));
193cdf0e10cSrcweir 	if ( p_reply == NULL || pam_data == NULL )
194cdf0e10cSrcweir 	{
195cdf0e10cSrcweir 		if ( p_reply != NULL )
196cdf0e10cSrcweir 			free ( p_reply );
197cdf0e10cSrcweir 		*response = NULL;
198cdf0e10cSrcweir 		return PAM_CONV_ERR;
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	/* pseudo dialog */
202cdf0e10cSrcweir 	error = sal_False;
203cdf0e10cSrcweir 	for ( i = 0; i < num_msg ; i++ )
204cdf0e10cSrcweir 	{
205cdf0e10cSrcweir 		switch ( msgm[ i ]->msg_style )
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir 			case PAM_PROMPT_ECHO_OFF:
208cdf0e10cSrcweir 				p_reply[ i ].resp_retcode = 0;
209cdf0e10cSrcweir 				p_reply[ i ].resp 		  = strdup( pam_data->password );
210cdf0e10cSrcweir 			 	break;
211cdf0e10cSrcweir 			case PAM_PROMPT_ECHO_ON:
212cdf0e10cSrcweir 				p_reply[ i ].resp_retcode = 0;
213cdf0e10cSrcweir 				p_reply[ i ].resp 		= strdup( pam_data->name );
214cdf0e10cSrcweir 				break;
215cdf0e10cSrcweir 			case PAM_ERROR_MSG:
216cdf0e10cSrcweir 			case PAM_TEXT_INFO:
217cdf0e10cSrcweir  			case PAM_BINARY_PROMPT:
218cdf0e10cSrcweir 			case PAM_BINARY_MSG:
219cdf0e10cSrcweir 				p_reply[ i ].resp_retcode 	= 0;
220cdf0e10cSrcweir 				p_reply[ i ].resp 			= NULL;
221cdf0e10cSrcweir 				break;
222cdf0e10cSrcweir 			default:
223cdf0e10cSrcweir 				error = sal_True;
224cdf0e10cSrcweir 				break;
225cdf0e10cSrcweir 		}
226cdf0e10cSrcweir 	}
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	/* free resources on error */
229cdf0e10cSrcweir 	if ( error )
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 		for ( i = 0; i < num_msg ; i++ )
232cdf0e10cSrcweir 			if ( p_reply[ i ].resp )
233cdf0e10cSrcweir 			{
234cdf0e10cSrcweir 				memset ( p_reply[ i ].resp, 0,
235cdf0e10cSrcweir 						 strlen( p_reply[ i ].resp ) );
236cdf0e10cSrcweir 				free   ( p_reply[ i ].resp );
237cdf0e10cSrcweir 			}
238cdf0e10cSrcweir 		free ( p_reply );
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 		*response = NULL;
241cdf0e10cSrcweir 		return PAM_CONV_ERR;
242cdf0e10cSrcweir 	}
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	/* well done */
245cdf0e10cSrcweir 	*response = p_reply;
246cdf0e10cSrcweir 	return PAM_SUCCESS;
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir #ifndef PAM_LINK
250cdf0e10cSrcweir /*
251cdf0e10cSrcweir  * avoid linking against libpam.so, since it is not available on all systems,
252cdf0e10cSrcweir  * instead load-on-call, returns structure which holds pointer to
253cdf0e10cSrcweir  * pam-functions,
254cdf0e10cSrcweir  * library is never closed in case of success
255cdf0e10cSrcweir  */
256cdf0e10cSrcweir 
osl_getPAM()257cdf0e10cSrcweir static sal_PamModule* osl_getPAM()
258cdf0e10cSrcweir {
259cdf0e10cSrcweir 	static sal_PamModule *pam_module = NULL;
260cdf0e10cSrcweir 	static sal_Bool load_once = sal_False;
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	if ( !load_once )
263cdf0e10cSrcweir 	{
264cdf0e10cSrcweir 		/* get library-handle. cannot use osl-module, since
265cdf0e10cSrcweir  		   RTLD_GLOBAL is required for PAM-0.64 RH 5.2
266cdf0e10cSrcweir 		   (but not for PAM-0.66 RH 6.0) */
267cdf0e10cSrcweir 		void *pam_hdl;
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 		pam_hdl = dlopen( "libpam.so.0", RTLD_GLOBAL | RTLD_LAZY );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 		if ( pam_hdl != NULL )
272cdf0e10cSrcweir 			pam_module = (sal_PamModule*)calloc( 1, sizeof(sal_PamModule) );
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 		/* load functions */
275cdf0e10cSrcweir 		if ( pam_module  != NULL )
276cdf0e10cSrcweir 		{
277cdf0e10cSrcweir 			pam_module->pam_acct_mgmt = (int (*)(pam_handle_t *, int)) dlsym ( pam_hdl, "pam_acct_mgmt" );
278cdf0e10cSrcweir 			pam_module->pam_authenticate
279cdf0e10cSrcweir 									  = (int (*)(pam_handle_t *, int)) dlsym ( pam_hdl, "pam_authenticate" );
280cdf0e10cSrcweir 			pam_module->pam_end       = (int (*)(pam_handle_t *, int)) dlsym ( pam_hdl, "pam_end" );
281cdf0e10cSrcweir 			pam_module->pam_start     = (int (*)(const char *, const char *, const struct pam_conv *, pam_handle_t **)) dlsym ( pam_hdl, "pam_start" );
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 			/* free resources, if not completely successful */
284cdf0e10cSrcweir 			if (   (pam_module->pam_start 		 == NULL)
285cdf0e10cSrcweir 				|| (pam_module->pam_end 		 == NULL)
286cdf0e10cSrcweir 				|| (pam_module->pam_authenticate == NULL)
287cdf0e10cSrcweir 				|| (pam_module->pam_acct_mgmt 	 == NULL) )
288cdf0e10cSrcweir 			{
289cdf0e10cSrcweir 				free( pam_module );
290cdf0e10cSrcweir 				pam_module = NULL;
291cdf0e10cSrcweir 				dlclose( pam_hdl );
292cdf0e10cSrcweir 			}
293cdf0e10cSrcweir 		}
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 		/* never try again */
296cdf0e10cSrcweir 		load_once = sal_True;
297cdf0e10cSrcweir 	}
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 	return pam_module;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir #endif
302cdf0e10cSrcweir 
303cdf0e10cSrcweir /*
304cdf0e10cSrcweir  * User Identification using PAM
305cdf0e10cSrcweir  */
306cdf0e10cSrcweir 
307cdf0e10cSrcweir static sal_Bool
osl_PamAuthentification(const sal_Char * name,const sal_Char * password)308cdf0e10cSrcweir osl_PamAuthentification( const sal_Char* name, const sal_Char* password )
309cdf0e10cSrcweir {
310cdf0e10cSrcweir 	sal_Bool success = sal_False;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir #ifndef PAM_LINK
313cdf0e10cSrcweir 	sal_PamModule* pam_module;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	pam_module = osl_getPAM();
316cdf0e10cSrcweir 	if ( pam_module != NULL )
317cdf0e10cSrcweir 	{
318cdf0e10cSrcweir #endif
319cdf0e10cSrcweir 		pam_handle_t   *pam_handle = NULL;
320cdf0e10cSrcweir 		struct pam_conv pam_conversation;
321cdf0e10cSrcweir 		sal_PamData     pam_data;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 		int				return_value;
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 		pam_data.name     = (char*) name;
326cdf0e10cSrcweir 		pam_data.password = (char*) password;
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 		pam_conversation.conv 		 = osl_PamConversation;
329cdf0e10cSrcweir 		pam_conversation.appdata_ptr = (void*)(&pam_data);
330cdf0e10cSrcweir 
331cdf0e10cSrcweir #ifndef PAM_LINK
332cdf0e10cSrcweir   		return_value = pam_module->pam_start( "su", name,
333cdf0e10cSrcweir 			&pam_conversation, &pam_handle);
334cdf0e10cSrcweir #else
335cdf0e10cSrcweir   		return_value = pam_start( "su", name,
336cdf0e10cSrcweir 			&pam_conversation, &pam_handle);
337cdf0e10cSrcweir #endif
338cdf0e10cSrcweir 		if (return_value == PAM_SUCCESS )
339cdf0e10cSrcweir #ifndef PAM_LINK
340cdf0e10cSrcweir 			return_value = pam_module->pam_authenticate(pam_handle, 0);
341cdf0e10cSrcweir #else
342cdf0e10cSrcweir 			return_value = pam_authenticate(pam_handle, 0);
343cdf0e10cSrcweir #endif
344cdf0e10cSrcweir   		if (return_value == PAM_SUCCESS )
345cdf0e10cSrcweir #ifndef PAM_LINK
346cdf0e10cSrcweir 			return_value = pam_module->pam_acct_mgmt(pam_handle, 0);
347cdf0e10cSrcweir 		pam_module->pam_end( pam_handle, return_value );
348cdf0e10cSrcweir #else
349cdf0e10cSrcweir 			return_value = pam_acct_mgmt(pam_handle, 0);
350cdf0e10cSrcweir 		pam_end( pam_handle, return_value );
351cdf0e10cSrcweir #endif
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 		success = (sal_Bool)(return_value == PAM_SUCCESS);
354cdf0e10cSrcweir #ifndef PAM_LINK
355cdf0e10cSrcweir 	}
356cdf0e10cSrcweir #endif
357cdf0e10cSrcweir 
358cdf0e10cSrcweir   	return success;
359cdf0e10cSrcweir }
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 
362cdf0e10cSrcweir #ifndef CRYPT_LINK
363cdf0e10cSrcweir /* dummy crypt, matches the interface of
364cdf0e10cSrcweir    crypt() but does not encrypt at all */
365cdf0e10cSrcweir static const sal_Char* SAL_CALL
osl_noCrypt(const sal_Char * key,const sal_Char * salt)366cdf0e10cSrcweir osl_noCrypt ( const sal_Char *key, const sal_Char *salt )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir     (void) salt; /* unused */
369cdf0e10cSrcweir 	return key;
370cdf0e10cSrcweir }
371cdf0e10cSrcweir 
372cdf0e10cSrcweir /* load-on-call crypt library and crypt symbol */
373cdf0e10cSrcweir static void*  SAL_CALL
osl_getCrypt()374cdf0e10cSrcweir osl_getCrypt()
375cdf0e10cSrcweir {
376cdf0e10cSrcweir 	static char* (*crypt_sym)(const char*, const char*) = NULL;
377cdf0e10cSrcweir 	static sal_Bool load_once  = sal_False;
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	if ( !load_once )
380cdf0e10cSrcweir 	{
381cdf0e10cSrcweir 		void * crypt_library;
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 		crypt_library = dlopen( "libcrypt.so.1", RTLD_GLOBAL | RTLD_LAZY ); /* never closed */
384cdf0e10cSrcweir 		if ( crypt_library != NULL )
385cdf0e10cSrcweir 			crypt_sym = (char* (*)(const char *, const char *)) dlsym(crypt_library, "crypt" );
386cdf0e10cSrcweir 		if ( crypt_sym == NULL ) /* no libcrypt or libcrypt without crypt */
387cdf0e10cSrcweir 			crypt_sym = (char* (*)(const char *, const char *)) &osl_noCrypt;
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 		load_once = sal_True;
390cdf0e10cSrcweir 	}
391cdf0e10cSrcweir 
392cdf0e10cSrcweir 	return (void*)crypt_sym;
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
395cdf0e10cSrcweir /* replacement for crypt function for password encryption, uses either
396cdf0e10cSrcweir    strong encryption of dlopen'ed libcrypt.so.1 or dummy implementation
397cdf0e10cSrcweir    with no encryption. Objective target is to avoid linking against
398cdf0e10cSrcweir    libcrypt (not available on caldera open linux 2.2 #63822#) */
399cdf0e10cSrcweir static sal_Char* SAL_CALL
osl_dynamicCrypt(const sal_Char * key,const sal_Char * salt)400cdf0e10cSrcweir osl_dynamicCrypt ( const sal_Char *key, const sal_Char *salt )
401cdf0e10cSrcweir {
402cdf0e10cSrcweir 	char* (*dynamic_crypt)(char *, char *);
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 	dynamic_crypt = (char * (*)(char *, char *)) osl_getCrypt();
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 	return dynamic_crypt( (sal_Char*)key, (sal_Char*)salt );
407cdf0e10cSrcweir }
408cdf0e10cSrcweir #endif
409cdf0e10cSrcweir 
410cdf0e10cSrcweir /*
411cdf0e10cSrcweir  * compare an encrypted and an unencrypted password for equality
412cdf0e10cSrcweir  * returns true if passwords are equal, false otherwise
413cdf0e10cSrcweir  * Note: uses crypt() and a mutex instead of crypt_r() since crypt_r needs
414cdf0e10cSrcweir  * more than 128KByte of external buffer for struct crypt_data
415cdf0e10cSrcweir  */
416cdf0e10cSrcweir 
417cdf0e10cSrcweir static sal_Bool SAL_CALL
osl_equalPasswords(const sal_Char * pEncryptedPassword,const sal_Char * pPlainPassword)418cdf0e10cSrcweir osl_equalPasswords ( const sal_Char *pEncryptedPassword, const sal_Char *pPlainPassword )
419cdf0e10cSrcweir {
420cdf0e10cSrcweir 	static pthread_mutex_t crypt_mutex = PTHREAD_MUTEX_INITIALIZER;
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 	sal_Bool  success;
423cdf0e10cSrcweir 	sal_Char  salt[3];
424cdf0e10cSrcweir 	sal_Char *encrypted_plain;
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 	salt[0] = pEncryptedPassword[0];
427cdf0e10cSrcweir 	salt[1] = pEncryptedPassword[1];
428cdf0e10cSrcweir 	salt[2] = '\0';
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 	pthread_mutex_lock(&crypt_mutex);
431cdf0e10cSrcweir 
432cdf0e10cSrcweir #ifndef CRYPT_LINK
433cdf0e10cSrcweir 	encrypted_plain = (sal_Char *)osl_dynamicCrypt( pPlainPassword, salt );
434cdf0e10cSrcweir #else
435cdf0e10cSrcweir 	encrypted_plain = (sal_Char *)crypt( pPlainPassword, salt );
436cdf0e10cSrcweir #endif
437cdf0e10cSrcweir 	success = (sal_Bool) (strcmp(pEncryptedPassword, encrypted_plain) == 0);
438cdf0e10cSrcweir 
439cdf0e10cSrcweir 	pthread_mutex_unlock(&crypt_mutex);
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 	return success;
442cdf0e10cSrcweir }
443cdf0e10cSrcweir 
444cdf0e10cSrcweir #endif /* defined LINUX && !defined NOPAM */
osl_loginUser(rtl_uString * ustrUserName,rtl_uString * ustrPassword,oslSecurity * pSecurity)445cdf0e10cSrcweir oslSecurityError SAL_CALL osl_loginUser(
446cdf0e10cSrcweir     rtl_uString *ustrUserName,
447cdf0e10cSrcweir 	rtl_uString *ustrPassword,
448cdf0e10cSrcweir 	oslSecurity *pSecurity
449cdf0e10cSrcweir 	)
450cdf0e10cSrcweir {
451cdf0e10cSrcweir     oslSecurityError Error;
452cdf0e10cSrcweir     rtl_String* strUserName=0;
453cdf0e10cSrcweir     rtl_String* strPassword=0;
454cdf0e10cSrcweir     sal_Char* pszUserName=0;
455cdf0e10cSrcweir     sal_Char* pszPassword=0;
456cdf0e10cSrcweir 
457cdf0e10cSrcweir     if ( ustrUserName != 0 )
458cdf0e10cSrcweir     {
459cdf0e10cSrcweir 
460cdf0e10cSrcweir         rtl_uString2String( &strUserName,
461cdf0e10cSrcweir                             rtl_uString_getStr(ustrUserName),
462cdf0e10cSrcweir                             rtl_uString_getLength(ustrUserName),
463cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
464cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
465cdf0e10cSrcweir         pszUserName = rtl_string_getStr(strUserName);
466cdf0e10cSrcweir     }
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     if ( ustrPassword != 0 )
470cdf0e10cSrcweir     {
471cdf0e10cSrcweir         rtl_uString2String( &strPassword,
472cdf0e10cSrcweir                             rtl_uString_getStr(ustrPassword),
473cdf0e10cSrcweir                             rtl_uString_getLength(ustrPassword),
474cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8,
475cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
476cdf0e10cSrcweir         pszPassword = rtl_string_getStr(strPassword);
477cdf0e10cSrcweir     }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 
480cdf0e10cSrcweir     Error=osl_psz_loginUser(pszUserName,pszPassword,pSecurity);
481cdf0e10cSrcweir 
482cdf0e10cSrcweir     if ( strUserName != 0 )
483cdf0e10cSrcweir     {
484cdf0e10cSrcweir         rtl_string_release(strUserName);
485cdf0e10cSrcweir     }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir     if ( strPassword)
488cdf0e10cSrcweir     {
489cdf0e10cSrcweir         rtl_string_release(strPassword);
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir 
493cdf0e10cSrcweir     return Error;
494cdf0e10cSrcweir }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir 
497cdf0e10cSrcweir static oslSecurityError SAL_CALL
osl_psz_loginUser(const sal_Char * pszUserName,const sal_Char * pszPasswd,oslSecurity * pSecurity)498cdf0e10cSrcweir osl_psz_loginUser(const sal_Char* pszUserName, const sal_Char* pszPasswd,
499cdf0e10cSrcweir 			   oslSecurity* pSecurity)
500cdf0e10cSrcweir {
501cdf0e10cSrcweir #if defined NETBSD || defined SCO || defined AIX || defined FREEBSD || \
502cdf0e10cSrcweir 	defined MACOSX
503cdf0e10cSrcweir 
504cdf0e10cSrcweir 	return osl_Security_E_None;
505cdf0e10cSrcweir 
506cdf0e10cSrcweir #else
507cdf0e10cSrcweir 
508cdf0e10cSrcweir     oslSecurityError nError = osl_Security_E_Unknown;
509cdf0e10cSrcweir     oslSecurityImpl * p = NULL;
510cdf0e10cSrcweir     if (pszUserName != NULL && pszPasswd != NULL && pSecurity != NULL) {
511cdf0e10cSrcweir         /* get nis or normal password, should succeed for any known user, but
512cdf0e10cSrcweir            perhaps the password is wrong (i.e. 'x') if shadow passwords are in
513cdf0e10cSrcweir            use or authentication must be done by PAM */
514cdf0e10cSrcweir         size_t n = 0;
515cdf0e10cSrcweir         int err = 0;
516cdf0e10cSrcweir         struct passwd * found = NULL;
517cdf0e10cSrcweir         for (;;) {
518cdf0e10cSrcweir             p = growSecurityImpl(p, &n);
519cdf0e10cSrcweir             if (p == NULL) {
520cdf0e10cSrcweir                 break;
521cdf0e10cSrcweir             }
522cdf0e10cSrcweir             err = getpwnam_r(
523cdf0e10cSrcweir                 pszUserName, &p->m_pPasswd, p->m_buffer, n, &found);
524cdf0e10cSrcweir             if (err != ERANGE) {
525cdf0e10cSrcweir                 break;
526cdf0e10cSrcweir             }
527cdf0e10cSrcweir         }
528cdf0e10cSrcweir         if (p != NULL && err == 0) {
529cdf0e10cSrcweir             if (found == NULL) {
530cdf0e10cSrcweir                 nError = osl_Security_E_UserUnknown;
531cdf0e10cSrcweir             } else {
532cdf0e10cSrcweir #if defined LINUX && !defined NOPAM
533cdf0e10cSrcweir                 /* only root is able to read the /etc/shadow passwd, a normal
534cdf0e10cSrcweir                    user even can't read his own encrypted passwd */
535cdf0e10cSrcweir                 if (osl_equalPasswords(p->m_pPasswd.pw_passwd, pszPasswd) ||
536cdf0e10cSrcweir                     osl_PamAuthentification(pszUserName, pszPasswd))
537cdf0e10cSrcweir                 {
538cdf0e10cSrcweir                     nError = osl_Security_E_None;
539cdf0e10cSrcweir                 } else {
540cdf0e10cSrcweir                     char buffer[1024];
541cdf0e10cSrcweir                     struct spwd result_buf;
542cdf0e10cSrcweir                     struct spwd * pShadowPasswd;
543cdf0e10cSrcweir                     buffer[0] = '\0';
544cdf0e10cSrcweir                     if (getspnam_r(
545cdf0e10cSrcweir                             pszUserName, &result_buf, buffer, sizeof buffer,
546cdf0e10cSrcweir                             &pShadowPasswd) == 0 &&
547cdf0e10cSrcweir                         pShadowPasswd != NULL)
548cdf0e10cSrcweir                     {
549cdf0e10cSrcweir                         nError =
550cdf0e10cSrcweir                             osl_equalPasswords(
551cdf0e10cSrcweir                                 pShadowPasswd->sp_pwdp, pszPasswd)
552cdf0e10cSrcweir                             ? osl_Security_E_None
553cdf0e10cSrcweir                             : osl_Security_E_WrongPassword;
554cdf0e10cSrcweir                     } else if (getuid() == 0) {
555cdf0e10cSrcweir                         /* mfe: Try to verify the root-password via nis */
556cdf0e10cSrcweir                         if (getspnam_r(
557cdf0e10cSrcweir                                 "root", &result_buf, buffer, sizeof buffer,
558cdf0e10cSrcweir                                 &pShadowPasswd) == 0 &&
559cdf0e10cSrcweir                             pShadowPasswd != NULL &&
560cdf0e10cSrcweir                             osl_equalPasswords(
561cdf0e10cSrcweir                                 pShadowPasswd->sp_pwdp, pszPasswd))
562cdf0e10cSrcweir                         {
563cdf0e10cSrcweir                             nError = osl_Security_E_None;
564cdf0e10cSrcweir                         } else {
565cdf0e10cSrcweir                             /* mfe: we can't get via nis (glibc2.0.x has bug in
566cdf0e10cSrcweir                                getspnam_r) we try it with the normal getspnam */
567cdf0e10cSrcweir                             static pthread_mutex_t pwmutex =
568cdf0e10cSrcweir                                 PTHREAD_MUTEX_INITIALIZER;
569cdf0e10cSrcweir                             pthread_mutex_lock(&pwmutex);
570cdf0e10cSrcweir                             pShadowPasswd = getspnam("root");
571cdf0e10cSrcweir                             pthread_mutex_unlock(&pwmutex);
572cdf0e10cSrcweir                             nError =
573cdf0e10cSrcweir                                 ((pShadowPasswd != NULL &&
574cdf0e10cSrcweir                                   osl_equalPasswords(
575cdf0e10cSrcweir                                       pShadowPasswd->sp_pwdp, pszPasswd)) ||
576cdf0e10cSrcweir                                  osl_PamAuthentification("root", pszPasswd))
577cdf0e10cSrcweir                                 ? osl_Security_E_None
578cdf0e10cSrcweir                                 : osl_Security_E_WrongPassword;
579cdf0e10cSrcweir                         }
580cdf0e10cSrcweir                     }
581cdf0e10cSrcweir                 }
582cdf0e10cSrcweir #else
583cdf0e10cSrcweir                 char buffer[1024];
584cdf0e10cSrcweir                 struct spwd spwdStruct;
585cdf0e10cSrcweir                 buffer[0] = '\0';
586*7994814dSHerbert Dürr #ifdef OLD_SHADOW_API
587cdf0e10cSrcweir                 if (getspnam_r(pszUserName, &spwdStruct, buffer, sizeof buffer) != NULL)
588cdf0e10cSrcweir #else
589cdf0e10cSrcweir                 if (getspnam_r(pszUserName, &spwdStruct, buffer, sizeof buffer, NULL) == 0)
590cdf0e10cSrcweir #endif
591cdf0e10cSrcweir                 {
592cdf0e10cSrcweir                     char salt[3];
593cdf0e10cSrcweir                     char * cryptPasswd;
594cdf0e10cSrcweir                     strncpy(salt, spwdStruct.sp_pwdp, 2);
595cdf0e10cSrcweir                     salt[2] = '\0';
596cdf0e10cSrcweir                     cryptPasswd = (char *) crypt(pszPasswd, salt);
597cdf0e10cSrcweir                     if (strcmp(spwdStruct.sp_pwdp, cryptPasswd) == 0) {
598cdf0e10cSrcweir                         nError = osl_Security_E_None;
599cdf0e10cSrcweir                     } else if (getuid() == 0 &&
600*7994814dSHerbert Dürr #ifdef OLD_SHADOW_API
601cdf0e10cSrcweir                                (getspnam_r("root", &spwdStruct, buffer, sizeof buffer) != NULL))
602cdf0e10cSrcweir #else
603cdf0e10cSrcweir                                (getspnam_r("root", &spwdStruct, buffer, sizeof buffer, NULL) == 0))
604cdf0e10cSrcweir #endif
605cdf0e10cSrcweir                     {
606cdf0e10cSrcweir                         /* if current process is running as root, allow to logon
607cdf0e10cSrcweir                            as any other user */
608cdf0e10cSrcweir                         strncpy(salt, spwdStruct.sp_pwdp, 2);
609cdf0e10cSrcweir                         salt[2] = '\0';
610cdf0e10cSrcweir                         cryptPasswd = (char *) crypt(pszPasswd, salt);
611cdf0e10cSrcweir                         if (strcmp(spwdStruct.sp_pwdp, cryptPasswd) == 0) {
612cdf0e10cSrcweir                             nError = osl_Security_E_None;
613cdf0e10cSrcweir                         }
614cdf0e10cSrcweir                     } else {
615cdf0e10cSrcweir                         nError = osl_Security_E_WrongPassword;
616cdf0e10cSrcweir                     }
617cdf0e10cSrcweir                 }
618cdf0e10cSrcweir #endif
619cdf0e10cSrcweir             }
620cdf0e10cSrcweir         }
621cdf0e10cSrcweir     }
622cdf0e10cSrcweir     if (nError == osl_Security_E_None) {
623cdf0e10cSrcweir         *pSecurity = p;
624cdf0e10cSrcweir     } else {
625cdf0e10cSrcweir         deleteSecurityImpl(p);
626cdf0e10cSrcweir         *pSecurity = NULL;
627cdf0e10cSrcweir     }
628cdf0e10cSrcweir     return nError;
629cdf0e10cSrcweir 
630cdf0e10cSrcweir #endif
631cdf0e10cSrcweir }
632cdf0e10cSrcweir 
osl_loginUserOnFileServer(rtl_uString * strUserName,rtl_uString * strPasswd,rtl_uString * strFileServer,oslSecurity * pSecurity)633cdf0e10cSrcweir oslSecurityError SAL_CALL osl_loginUserOnFileServer(
634cdf0e10cSrcweir     rtl_uString *strUserName,
635cdf0e10cSrcweir 	rtl_uString *strPasswd,
636cdf0e10cSrcweir 	rtl_uString *strFileServer,
637cdf0e10cSrcweir 	oslSecurity *pSecurity
638cdf0e10cSrcweir 	)
639cdf0e10cSrcweir {
640cdf0e10cSrcweir     (void) strUserName; /* unused */
641cdf0e10cSrcweir     (void) strPasswd; /* unused */
642cdf0e10cSrcweir     (void) strFileServer; /* unused */
643cdf0e10cSrcweir     (void) pSecurity; /* unused */
644cdf0e10cSrcweir 	return osl_Security_E_UserUnknown;
645cdf0e10cSrcweir }
646cdf0e10cSrcweir 
647cdf0e10cSrcweir 
osl_getUserIdent(oslSecurity Security,rtl_uString ** ustrIdent)648cdf0e10cSrcweir sal_Bool SAL_CALL osl_getUserIdent(oslSecurity Security, rtl_uString **ustrIdent)
649cdf0e10cSrcweir {
650cdf0e10cSrcweir     sal_Bool bRet=sal_False;
651cdf0e10cSrcweir     sal_Char pszIdent[1024];
652cdf0e10cSrcweir 
653cdf0e10cSrcweir     pszIdent[0] = '\0';
654cdf0e10cSrcweir 
655cdf0e10cSrcweir     bRet = osl_psz_getUserIdent(Security,pszIdent,sizeof(pszIdent));
656cdf0e10cSrcweir 
657cdf0e10cSrcweir     rtl_string2UString( ustrIdent, pszIdent, rtl_str_getLength( pszIdent ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
658cdf0e10cSrcweir     OSL_ASSERT(*ustrIdent != NULL);
659cdf0e10cSrcweir 
660cdf0e10cSrcweir     return bRet;
661cdf0e10cSrcweir }
662cdf0e10cSrcweir 
663cdf0e10cSrcweir 
osl_psz_getUserIdent(oslSecurity Security,sal_Char * pszIdent,sal_uInt32 nMax)664cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax)
665cdf0e10cSrcweir {
666cdf0e10cSrcweir 	sal_Char  buffer[32];
667cdf0e10cSrcweir     sal_Int32 nChr;
668cdf0e10cSrcweir 
669cdf0e10cSrcweir 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
670cdf0e10cSrcweir 
671cdf0e10cSrcweir     if (pSecImpl == NULL)
672cdf0e10cSrcweir 	    return sal_False;
673cdf0e10cSrcweir 
674cdf0e10cSrcweir 	nChr = snprintf(buffer, sizeof(buffer), "%u", pSecImpl->m_pPasswd.pw_uid);
675cdf0e10cSrcweir     if ( nChr < 0 || SAL_INT_CAST(sal_uInt32, nChr) >= sizeof(buffer)
676cdf0e10cSrcweir          || SAL_INT_CAST(sal_uInt32, nChr) >= nMax )
677cdf0e10cSrcweir         return sal_False; /* leave *pszIdent unmodified in case of failure */
678cdf0e10cSrcweir 
679cdf0e10cSrcweir 	memcpy(pszIdent, buffer, nChr+1);
680cdf0e10cSrcweir     return sal_True;
681cdf0e10cSrcweir }
682cdf0e10cSrcweir 
osl_getUserName(oslSecurity Security,rtl_uString ** ustrName)683cdf0e10cSrcweir sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName)
684cdf0e10cSrcweir {
685cdf0e10cSrcweir     sal_Bool bRet=sal_False;
686cdf0e10cSrcweir     sal_Char pszName[1024];
687cdf0e10cSrcweir 
688cdf0e10cSrcweir     pszName[0] = '\0';
689cdf0e10cSrcweir 
690cdf0e10cSrcweir     bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName));
691cdf0e10cSrcweir 
692cdf0e10cSrcweir     rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
693cdf0e10cSrcweir     OSL_ASSERT(*ustrName != NULL);
694cdf0e10cSrcweir 
695cdf0e10cSrcweir     return bRet;
696cdf0e10cSrcweir }
697cdf0e10cSrcweir 
698cdf0e10cSrcweir 
699cdf0e10cSrcweir 
osl_psz_getUserName(oslSecurity Security,sal_Char * pszName,sal_uInt32 nMax)700cdf0e10cSrcweir static sal_Bool SAL_CALL osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32  nMax)
701cdf0e10cSrcweir {
702cdf0e10cSrcweir 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
703cdf0e10cSrcweir 
704cdf0e10cSrcweir     if (pSecImpl == NULL)
705cdf0e10cSrcweir 	    return sal_False;
706cdf0e10cSrcweir 
707cdf0e10cSrcweir 	strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax);
708cdf0e10cSrcweir 
709cdf0e10cSrcweir     return sal_True;
710cdf0e10cSrcweir }
711cdf0e10cSrcweir 
osl_getHomeDir(oslSecurity Security,rtl_uString ** pustrDirectory)712cdf0e10cSrcweir sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
713cdf0e10cSrcweir {
714cdf0e10cSrcweir     sal_Bool bRet=sal_False;
715cdf0e10cSrcweir     sal_Char pszDirectory[PATH_MAX];
716cdf0e10cSrcweir 
717cdf0e10cSrcweir     pszDirectory[0] = '\0';
718cdf0e10cSrcweir 
719cdf0e10cSrcweir     bRet = osl_psz_getHomeDir(Security,pszDirectory,sizeof(pszDirectory));
720cdf0e10cSrcweir 
721cdf0e10cSrcweir     if ( bRet == sal_True )
722cdf0e10cSrcweir     {
723cdf0e10cSrcweir         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
724cdf0e10cSrcweir         OSL_ASSERT(*pustrDirectory != NULL);
725cdf0e10cSrcweir         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
726cdf0e10cSrcweir     }
727cdf0e10cSrcweir 
728cdf0e10cSrcweir     return bRet;
729cdf0e10cSrcweir }
730cdf0e10cSrcweir 
731cdf0e10cSrcweir 
osl_psz_getHomeDir(oslSecurity Security,sal_Char * pszDirectory,sal_uInt32 nMax)732cdf0e10cSrcweir static sal_Bool SAL_CALL osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
733cdf0e10cSrcweir {
734cdf0e10cSrcweir 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
735cdf0e10cSrcweir 
736cdf0e10cSrcweir     if (pSecImpl == NULL)
737cdf0e10cSrcweir 	    return sal_False;
738cdf0e10cSrcweir 
739cdf0e10cSrcweir 	/* if current user, check also environment for HOME */
740cdf0e10cSrcweir 	if (getuid() == pSecImpl->m_pPasswd.pw_uid)
741cdf0e10cSrcweir 	{
742cdf0e10cSrcweir 		sal_Char *pStr = NULL;
743cdf0e10cSrcweir #ifdef SOLARIS
744cdf0e10cSrcweir 		char	buffer[8192];
745cdf0e10cSrcweir 
746cdf0e10cSrcweir 		struct passwd pwd;
747cdf0e10cSrcweir 		struct passwd *ppwd;
748cdf0e10cSrcweir 
749cdf0e10cSrcweir #ifdef _POSIX_PTHREAD_SEMANTICS
750cdf0e10cSrcweir 		if ( 0 != getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &ppwd ) )
751cdf0e10cSrcweir 			ppwd = NULL;
752cdf0e10cSrcweir #else
753cdf0e10cSrcweir 		ppwd = getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer) );
754cdf0e10cSrcweir #endif
755cdf0e10cSrcweir 
756cdf0e10cSrcweir 		if ( ppwd )
757cdf0e10cSrcweir 			pStr = ppwd->pw_dir;
758cdf0e10cSrcweir #else
759cdf0e10cSrcweir 		pStr = getenv("HOME");
760cdf0e10cSrcweir #endif
761cdf0e10cSrcweir 
762cdf0e10cSrcweir 		if ((pStr != NULL) && (strlen(pStr) > 0) &&
763cdf0e10cSrcweir 		    (access(pStr, 0) == 0))
764cdf0e10cSrcweir 			strncpy(pszDirectory, pStr, nMax);
765cdf0e10cSrcweir 		else
766cdf0e10cSrcweir             strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
767cdf0e10cSrcweir 	}
768cdf0e10cSrcweir 	else
769cdf0e10cSrcweir 		strncpy(pszDirectory, pSecImpl->m_pPasswd.pw_dir, nMax);
770cdf0e10cSrcweir 
771cdf0e10cSrcweir 	return sal_True;
772cdf0e10cSrcweir }
773cdf0e10cSrcweir 
osl_getConfigDir(oslSecurity Security,rtl_uString ** pustrDirectory)774cdf0e10cSrcweir sal_Bool SAL_CALL osl_getConfigDir(oslSecurity Security, rtl_uString **pustrDirectory)
775cdf0e10cSrcweir {
776cdf0e10cSrcweir     sal_Bool bRet = sal_False;
777cdf0e10cSrcweir     sal_Char pszDirectory[PATH_MAX];
778cdf0e10cSrcweir 
779cdf0e10cSrcweir     pszDirectory[0] = '\0';
780cdf0e10cSrcweir 
781cdf0e10cSrcweir     bRet = osl_psz_getConfigDir(Security,pszDirectory,sizeof(pszDirectory));
782cdf0e10cSrcweir 
783cdf0e10cSrcweir     if ( bRet == sal_True )
784cdf0e10cSrcweir     {
785cdf0e10cSrcweir         rtl_string2UString( pustrDirectory, pszDirectory, rtl_str_getLength( pszDirectory ), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS );
786cdf0e10cSrcweir         OSL_ASSERT(*pustrDirectory != NULL);
787cdf0e10cSrcweir         osl_getFileURLFromSystemPath( *pustrDirectory, pustrDirectory );
788cdf0e10cSrcweir     }
789cdf0e10cSrcweir 
790cdf0e10cSrcweir     return bRet;
791cdf0e10cSrcweir }
792cdf0e10cSrcweir 
793cdf0e10cSrcweir #ifndef MACOSX
794cdf0e10cSrcweir 
osl_psz_getConfigDir(oslSecurity Security,sal_Char * pszDirectory,sal_uInt32 nMax)795cdf0e10cSrcweir static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
796cdf0e10cSrcweir {
797cdf0e10cSrcweir     sal_Char *pStr = getenv("XDG_CONFIG_HOME");
798cdf0e10cSrcweir 
799cdf0e10cSrcweir     if ((pStr == NULL) || (strlen(pStr) == 0) ||
800cdf0e10cSrcweir         (access(pStr, 0) != 0))
801cdf0e10cSrcweir         return (osl_psz_getHomeDir(Security, pszDirectory, nMax));
802cdf0e10cSrcweir 
803cdf0e10cSrcweir     strncpy(pszDirectory, pStr, nMax);
804cdf0e10cSrcweir     return sal_True;
805cdf0e10cSrcweir }
806cdf0e10cSrcweir 
807cdf0e10cSrcweir #else
808cdf0e10cSrcweir 
809cdf0e10cSrcweir /*
810cdf0e10cSrcweir  * FIXME: rewrite to use more flexible
811cdf0e10cSrcweir  * NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)
812cdf0e10cSrcweir  * as soon as we can bumb the baseline to Tiger (for NSApplicationSupportDirectory) and have
813cdf0e10cSrcweir  * support for Objective-C in the build environment
814cdf0e10cSrcweir  */
815cdf0e10cSrcweir 
816cdf0e10cSrcweir #define MACOSX_CONFIG_DIR "/Library/Application Support"
osl_psz_getConfigDir(oslSecurity Security,sal_Char * pszDirectory,sal_uInt32 nMax)817cdf0e10cSrcweir static sal_Bool SAL_CALL osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax)
818cdf0e10cSrcweir {
819cdf0e10cSrcweir     if( osl_psz_getHomeDir(Security, pszDirectory, nMax - sizeof(MACOSX_CONFIG_DIR) + 1) )
820cdf0e10cSrcweir     {
821cdf0e10cSrcweir         strcat( pszDirectory, MACOSX_CONFIG_DIR );
822cdf0e10cSrcweir         return sal_True;
823cdf0e10cSrcweir     }
824cdf0e10cSrcweir 
825cdf0e10cSrcweir     return sal_False;
826cdf0e10cSrcweir }
827cdf0e10cSrcweir 
828cdf0e10cSrcweir #endif
829cdf0e10cSrcweir 
osl_isAdministrator(oslSecurity Security)830cdf0e10cSrcweir sal_Bool SAL_CALL osl_isAdministrator(oslSecurity Security)
831cdf0e10cSrcweir {
832cdf0e10cSrcweir 	oslSecurityImpl *pSecImpl = (oslSecurityImpl *)Security;
833cdf0e10cSrcweir 
834cdf0e10cSrcweir     if (pSecImpl == NULL)
835cdf0e10cSrcweir 	    return sal_False;
836cdf0e10cSrcweir 
837cdf0e10cSrcweir     if (pSecImpl->m_pPasswd.pw_uid != 0)
838cdf0e10cSrcweir 		return (sal_False);
839cdf0e10cSrcweir 
840cdf0e10cSrcweir 	return (sal_True);
841cdf0e10cSrcweir }
842cdf0e10cSrcweir 
osl_freeSecurityHandle(oslSecurity Security)843cdf0e10cSrcweir void SAL_CALL osl_freeSecurityHandle(oslSecurity Security)
844cdf0e10cSrcweir {
845cdf0e10cSrcweir     deleteSecurityImpl(Security);
846cdf0e10cSrcweir }
847cdf0e10cSrcweir 
848cdf0e10cSrcweir 
osl_loadUserProfile(oslSecurity Security)849cdf0e10cSrcweir sal_Bool SAL_CALL osl_loadUserProfile(oslSecurity Security)
850cdf0e10cSrcweir {
851cdf0e10cSrcweir     (void) Security; /* unused */
852cdf0e10cSrcweir     return sal_False;
853cdf0e10cSrcweir }
854cdf0e10cSrcweir 
osl_unloadUserProfile(oslSecurity Security)855cdf0e10cSrcweir void SAL_CALL osl_unloadUserProfile(oslSecurity Security)
856cdf0e10cSrcweir {
857cdf0e10cSrcweir     (void) Security; /* unused */
858cdf0e10cSrcweir }
859