1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: security.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 #include <osl/security.h>
35 #include <osl/diagnose.h>
36 #include <osl/module.h>
38 #include "osl/thread.h"
47 #ifndef PAM_BINARY_MSG
48 #define PAM_BINARY_MSG 6
51 extern oslModule SAL_CALL
osl_psz_loadModule(const sal_Char
*pszModuleName
, sal_Int32 nRtldMode
);
52 extern void* SAL_CALL
osl_psz_getSymbol(oslModule hModule
, const sal_Char
* pszSymbolName
);
53 extern oslSecurityError SAL_CALL
54 osl_psz_loginUser(const sal_Char
* pszUserName
, const sal_Char
* pszPasswd
,
55 oslSecurity
* pSecurity
);
56 sal_Bool SAL_CALL
osl_psz_getUserIdent(oslSecurity Security
, sal_Char
*pszIdent
, sal_uInt32 nMax
);
57 sal_Bool SAL_CALL
osl_psz_getUserName(oslSecurity Security
, sal_Char
* pszName
, sal_uInt32 nMax
);
58 sal_Bool SAL_CALL
osl_psz_getHomeDir(oslSecurity Security
, sal_Char
* pszDirectory
, sal_uInt32 nMax
);
59 sal_Bool SAL_CALL
osl_psz_getConfigDir(oslSecurity Security
, sal_Char
* pszDirectory
, sal_uInt32 nMax
);
63 oslSecurity SAL_CALL
osl_getCurrentSecurity()
66 oslSecurityImpl
*pSecImpl
= (oslSecurityImpl
*) malloc(sizeof(oslSecurityImpl
));
67 struct passwd
*pPasswd
= getpwuid(getuid());
71 memcpy(&pSecImpl
->m_pPasswd
, pPasswd
, sizeof(pSecImpl
->m_pPasswd
));
72 pSecImpl
->m_isValid
= sal_True
;
76 /* Some UNIX-OS don't implement getpwuid, e.g. NC OS (special NetBSD) 1.2.1 */
77 /* so we have to catch this in this else branch */
78 pSecImpl
->m_pPasswd
.pw_name
= getenv("USER");
79 pSecImpl
->m_pPasswd
.pw_dir
= getenv("HOME");
80 if (pSecImpl
->m_pPasswd
.pw_name
&& pSecImpl
->m_pPasswd
.pw_dir
)
81 pSecImpl
->m_isValid
= sal_True
;
84 pSecImpl
->m_pPasswd
.pw_name
= "unknown";
85 pSecImpl
->m_pPasswd
.pw_dir
= "/tmp";
86 pSecImpl
->m_isValid
= sal_False
;
88 pSecImpl
->m_pPasswd
.pw_passwd
= NULL
;
89 pSecImpl
->m_pPasswd
.pw_uid
= getuid();
90 pSecImpl
->m_pPasswd
.pw_gid
= getgid();
91 pSecImpl
->m_pPasswd
.pw_gecos
= "unknown";
92 pSecImpl
->m_pPasswd
.pw_shell
= "unknown";
96 return ((oslSecurity
)pSecImpl
);
100 oslSecurityError SAL_CALL
osl_loginUser(
101 rtl_uString
*ustrUserName
,
102 rtl_uString
*ustrPassword
,
103 oslSecurity
*pSecurity
106 oslSecurityError ret
;
108 *pSecurity
= osl_getCurrentSecurity();
109 ret
= osl_Security_E_None
;
116 oslSecurityError SAL_CALL
osl_loginUserOnFileServer(
117 rtl_uString
*strUserName
,
118 rtl_uString
*strPasswd
,
119 rtl_uString
*strFileServer
,
120 oslSecurity
*pSecurity
123 oslSecurityError erg
;
124 return erg
= osl_Security_E_UserUnknown
;
128 oslSecurityError SAL_CALL
osl_psz_loginUserOnFileServer( const sal_Char
* pszUserName
,
129 const sal_Char
* pszPasswd
,
130 const sal_Char
* pszFileServer
,
131 oslSecurity
* pSecurity
)
133 oslSecurityError erg
;
134 return erg
= osl_Security_E_UserUnknown
;
137 sal_Bool SAL_CALL
osl_getUserIdent(oslSecurity Security
, rtl_uString
**ustrIdent
)
139 sal_Bool bRet
=sal_False
;
140 sal_Char pszIdent
[1024];
144 bRet
= osl_psz_getUserIdent(Security
,pszIdent
,sizeof(pszIdent
));
146 rtl_string2UString( ustrIdent
, pszIdent
, rtl_str_getLength( pszIdent
), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS
);
147 OSL_ASSERT(*ustrIdent
!= NULL
);
153 sal_Bool SAL_CALL
osl_psz_getUserIdent(oslSecurity Security
, sal_Char
*pszIdent
, sal_uInt32 nMax
)
158 oslSecurityImpl
*pSecImpl
= (oslSecurityImpl
*)Security
;
160 if (pSecImpl
== NULL
)
163 nChr
= snprintf(buffer
, sizeof(buffer
), "%u", pSecImpl
->m_pPasswd
.pw_uid
);
164 if ( nChr
< 0 || nChr
>= sizeof(buffer
) || nChr
>= nMax
)
165 return sal_False
; /* leave *pszIdent unmodified in case of failure */
167 memcpy(pszIdent
, buffer
, nChr
+1);
171 sal_Bool SAL_CALL
osl_getUserName(oslSecurity Security
, rtl_uString
**ustrName
)
173 sal_Bool bRet
=sal_False
;
174 sal_Char pszName
[1024];
178 bRet
= osl_psz_getUserName(Security
,pszName
,sizeof(pszName
));
180 rtl_string2UString( ustrName
, pszName
, rtl_str_getLength( pszName
), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS
);
181 OSL_ASSERT(*ustrName
!= NULL
);
188 sal_Bool SAL_CALL
osl_psz_getUserName(oslSecurity Security
, sal_Char
* pszName
, sal_uInt32 nMax
)
190 oslSecurityImpl
*pSecImpl
= (oslSecurityImpl
*)Security
;
192 if ((pSecImpl
== NULL
) || (! pSecImpl
->m_isValid
))
195 strncpy(pszName
, pSecImpl
->m_pPasswd
.pw_name
, nMax
);
200 sal_Bool SAL_CALL
osl_getHomeDir(oslSecurity Security
, rtl_uString
**pustrDirectory
)
202 sal_Bool bRet
=sal_False
;
203 sal_Char pszDirectory
[PATH_MAX
];
205 pszDirectory
[0] = '\0';
207 bRet
= osl_psz_getHomeDir(Security
,pszDirectory
,sizeof(pszDirectory
));
209 if ( bRet
== sal_True
)
211 rtl_string2UString( pustrDirectory
, pszDirectory
, rtl_str_getLength( pszDirectory
), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS
);
212 OSL_ASSERT(*pustrDirectory
!= NULL
);
213 osl_getFileURLFromSystemPath( *pustrDirectory
, pustrDirectory
);
220 sal_Bool SAL_CALL
osl_psz_getHomeDir(oslSecurity Security
, sal_Char
* pszDirectory
, sal_uInt32 nMax
)
222 oslSecurityImpl
*pSecImpl
= (oslSecurityImpl
*)Security
;
224 if (pSecImpl
== NULL
)
227 /* if current user, check also environment for HOME */
228 if (getuid() == pSecImpl
->m_pPasswd
.pw_uid
)
230 sal_Char
*pStr
= NULL
;
237 #ifdef _POSIX_PTHREAD_SEMANTICS
238 if ( 0 != getpwuid_r(getuid(), &pwd
, buffer
, sizeof(buffer
), &ppwd
) )
241 ppwd
= getpwuid_r(getuid(), &pwd
, buffer
, sizeof(buffer
) );
247 pStr
= getenv("HOME");
250 if ((pStr
!= NULL
) && (strlen(pStr
) > 0) &&
251 (access(pStr
, 0) == 0))
252 strncpy(pszDirectory
, pStr
, nMax
);
254 if (pSecImpl
->m_isValid
)
255 strncpy(pszDirectory
, pSecImpl
->m_pPasswd
.pw_dir
, nMax
);
260 strncpy(pszDirectory
, pSecImpl
->m_pPasswd
.pw_dir
, nMax
);
265 sal_Bool SAL_CALL
osl_getConfigDir(oslSecurity Security
, rtl_uString
**pustrDirectory
)
267 sal_Bool bRet
= sal_False
;
268 sal_Char pszDirectory
[PATH_MAX
];
270 pszDirectory
[0] = '\0';
272 bRet
= osl_psz_getConfigDir(Security
,pszDirectory
,sizeof(pszDirectory
));
274 if ( bRet
== sal_True
)
276 rtl_string2UString( pustrDirectory
, pszDirectory
, rtl_str_getLength( pszDirectory
), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS
);
277 OSL_ASSERT(*pustrDirectory
!= NULL
);
278 osl_getFileURLFromSystemPath( *pustrDirectory
, pustrDirectory
);
285 sal_Bool SAL_CALL
osl_psz_getConfigDir(oslSecurity Security
, sal_Char
* pszDirectory
, sal_uInt32 nMax
)
287 return (osl_psz_getHomeDir(Security
, pszDirectory
, nMax
));
290 sal_Bool SAL_CALL
osl_isAdministrator(oslSecurity Security
)
292 oslSecurityImpl
*pSecImpl
= (oslSecurityImpl
*)Security
;
294 if (pSecImpl
== NULL
)
297 if (pSecImpl
->m_pPasswd
.pw_uid
!= 0)
303 void SAL_CALL
osl_freeSecurityHandle(oslSecurity Security
)
306 free ((oslSecurityImpl
*)Security
);
310 sal_Bool SAL_CALL
osl_loadUserProfile(oslSecurity Security
)
315 void SAL_CALL
osl_unloadUserProfile(oslSecurity Security
)