xref: /AOO41X/main/extensions/source/config/ldap/ldapuserprofilebe.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "ldapaccess.hxx"
32*cdf0e10cSrcweir #include "ldapuserprofilebe.hxx"
33*cdf0e10cSrcweir #include <osl/file.hxx>
34*cdf0e10cSrcweir #include <osl/module.hxx>
35*cdf0e10cSrcweir #include <osl/process.h>
36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37*cdf0e10cSrcweir #include <rtl/byteseq.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #ifndef INCLUDED_RTL_INSTANCE_HXX_
40*cdf0e10cSrcweir #include <rtl/instance.hxx>
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/beans/Optional.hpp>
44*cdf0e10cSrcweir #include <osl/security.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir //==============================================================================
47*cdf0e10cSrcweir namespace extensions { namespace config { namespace ldap {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
50*cdf0e10cSrcweir : LdapProfileMutexHolder(),
51*cdf0e10cSrcweir   BackendBase(mMutex)
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     LdapDefinition aDefinition;
54*cdf0e10cSrcweir     rtl::OUString loggedOnUser;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     // This whole rigmarole is to prevent an infinite recursion where reading
57*cdf0e10cSrcweir     // the configuration for the backend would create another instance of the
58*cdf0e10cSrcweir     // backend, which would try and read the configuration which would...
59*cdf0e10cSrcweir     {
60*cdf0e10cSrcweir         osl::Mutex & aInitMutex = rtl::Static< osl::Mutex, LdapUserProfileBe >::get();
61*cdf0e10cSrcweir         osl::MutexGuard aInitGuard(aInitMutex);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir         static bool bReentrantCall; // = false
64*cdf0e10cSrcweir         OSL_ENSURE(!bReentrantCall, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir         if (!bReentrantCall)
67*cdf0e10cSrcweir         {
68*cdf0e10cSrcweir             try
69*cdf0e10cSrcweir             {
70*cdf0e10cSrcweir                 bReentrantCall = true ;
71*cdf0e10cSrcweir                 if (!readLdapConfiguration(
72*cdf0e10cSrcweir                         css::uno::Reference< css::lang::XMultiServiceFactory >(
73*cdf0e10cSrcweir                             xContext->getServiceManager(),
74*cdf0e10cSrcweir                             css::uno::UNO_QUERY_THROW),
75*cdf0e10cSrcweir                         &aDefinition, &loggedOnUser))
76*cdf0e10cSrcweir                 {
77*cdf0e10cSrcweir                     throw css::uno::RuntimeException(
78*cdf0e10cSrcweir                         rtl::OUString::createFromAscii("LdapUserProfileBe- LDAP not configured"),
79*cdf0e10cSrcweir                         NULL);
80*cdf0e10cSrcweir                 }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir                 bReentrantCall = false ;
83*cdf0e10cSrcweir             }
84*cdf0e10cSrcweir             catch (...)
85*cdf0e10cSrcweir             {
86*cdf0e10cSrcweir                 bReentrantCall = false;
87*cdf0e10cSrcweir                 throw;
88*cdf0e10cSrcweir             }
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     LdapConnection connection;
93*cdf0e10cSrcweir     connection.loadModule();
94*cdf0e10cSrcweir     connection.connectSimple(aDefinition);
95*cdf0e10cSrcweir     connection.getUserProfile(loggedOnUser, &data_);
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir //------------------------------------------------------------------------------
98*cdf0e10cSrcweir LdapUserProfileBe::~LdapUserProfileBe()
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir //------------------------------------------------------------------------------
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir bool LdapUserProfileBe::readLdapConfiguration(
104*cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > const & factory,
105*cdf0e10cSrcweir     LdapDefinition * definition, rtl::OUString * loggedOnUser)
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     OSL_ASSERT(factory.is() && definition != 0 && loggedOnUser != 0);
108*cdf0e10cSrcweir     const rtl::OUString kConfigurationProviderService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationProvider")) ;
109*cdf0e10cSrcweir     const rtl::OUString kReadOnlyViewService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")) ;
110*cdf0e10cSrcweir     const rtl::OUString kComponent( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.LDAP/UserDirectory"));
111*cdf0e10cSrcweir     const rtl::OUString kServerDefiniton(RTL_CONSTASCII_USTRINGPARAM ("ServerDefinition"));
112*cdf0e10cSrcweir     const rtl::OUString kServer(RTL_CONSTASCII_USTRINGPARAM ("Server"));
113*cdf0e10cSrcweir     const rtl::OUString kPort(RTL_CONSTASCII_USTRINGPARAM("Port"));
114*cdf0e10cSrcweir     const rtl::OUString kBaseDN(RTL_CONSTASCII_USTRINGPARAM("BaseDN"));
115*cdf0e10cSrcweir     const rtl::OUString kUser(RTL_CONSTASCII_USTRINGPARAM("SearchUser"));
116*cdf0e10cSrcweir     const rtl::OUString kPassword(RTL_CONSTASCII_USTRINGPARAM("SearchPassword"));
117*cdf0e10cSrcweir     const rtl::OUString kUserObjectClass(RTL_CONSTASCII_USTRINGPARAM("UserObjectClass"));
118*cdf0e10cSrcweir     const rtl::OUString kUserUniqueAttr(RTL_CONSTASCII_USTRINGPARAM("UserUniqueAttribute"));
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	uno::Reference< XInterface > xIface;
121*cdf0e10cSrcweir     try
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
124*cdf0e10cSrcweir                                                         factory->createInstance(kConfigurationProviderService),
125*cdf0e10cSrcweir                                                         uno::UNO_QUERY);
126*cdf0e10cSrcweir 	    OSL_ENSURE(xCfgProvider.is(),"LdapUserProfileBe: could not create the configuration provider");
127*cdf0e10cSrcweir 	    if (!xCfgProvider.is())
128*cdf0e10cSrcweir             return false;
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         css::beans::NamedValue aPath(rtl::OUString::createFromAscii("nodepath"), uno::makeAny(kComponent) );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         uno::Sequence< uno::Any > aArgs(1);
133*cdf0e10cSrcweir         aArgs[0] <<=  aPath;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         xIface = xCfgProvider->createInstanceWithArguments(kReadOnlyViewService, aArgs);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
138*cdf0e10cSrcweir         xAccess->getByName(kServerDefiniton) >>= xIface;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         if (!getLdapStringParam(xChildAccess, kServer, definition->mServer))
143*cdf0e10cSrcweir             return false;
144*cdf0e10cSrcweir         if (!getLdapStringParam(xChildAccess, kBaseDN, definition->mBaseDN))
145*cdf0e10cSrcweir             return false;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         definition->mPort=0;
148*cdf0e10cSrcweir         xChildAccess->getByName(kPort) >>= definition->mPort ;
149*cdf0e10cSrcweir 	    if (definition->mPort == 0)
150*cdf0e10cSrcweir 		    return false;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         if (!getLdapStringParam(xAccess, kUserObjectClass, definition->mUserObjectClass))
153*cdf0e10cSrcweir             return false;
154*cdf0e10cSrcweir         if (!getLdapStringParam(xAccess, kUserUniqueAttr, definition->mUserUniqueAttr))
155*cdf0e10cSrcweir             return false;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         getLdapStringParam(xAccess, kUser, definition->mAnonUser);
158*cdf0e10cSrcweir         getLdapStringParam(xAccess, kPassword, definition->mAnonCredentials);
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir     catch (uno::Exception & e)
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir         OSL_TRACE("LdapUserProfileBackend: access to configuration data failed: %s",
163*cdf0e10cSrcweir                 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
164*cdf0e10cSrcweir         return false;
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     osl::Security aSecurityContext;
168*cdf0e10cSrcweir 	if (!aSecurityContext.getUserName(*loggedOnUser))
169*cdf0e10cSrcweir 		OSL_TRACE("LdapUserProfileBackend - could not get Logged on user from system");
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     sal_Int32 nIndex = loggedOnUser->indexOf('/');
172*cdf0e10cSrcweir 	if (nIndex > 0)
173*cdf0e10cSrcweir 		*loggedOnUser = loggedOnUser->copy(nIndex+1);
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     //Remember to remove
176*cdf0e10cSrcweir     OSL_TRACE("Logged on user is %s", rtl::OUStringToOString(*loggedOnUser,RTL_TEXTENCODING_ASCII_US).getStr());
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     return true;
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir //------------------------------------------------------------------------------
182*cdf0e10cSrcweir bool LdapUserProfileBe::getLdapStringParam(
183*cdf0e10cSrcweir 	uno::Reference<container::XNameAccess>& xAccess,
184*cdf0e10cSrcweir 	const rtl::OUString& aLdapSetting,
185*cdf0e10cSrcweir 	rtl::OString& aServerParameter)
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir     rtl::OUString sParam;
188*cdf0e10cSrcweir     xAccess->getByName(aLdapSetting) >>= sParam;
189*cdf0e10cSrcweir     aServerParameter = rtl::OUStringToOString(sParam, RTL_TEXTENCODING_ASCII_US);
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     return aServerParameter.getLength() != 0;
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir //------------------------------------------------------------------------------
194*cdf0e10cSrcweir void LdapUserProfileBe::setPropertyValue(
195*cdf0e10cSrcweir     rtl::OUString const &, css::uno::Any const &)
196*cdf0e10cSrcweir     throw (
197*cdf0e10cSrcweir         css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
198*cdf0e10cSrcweir         css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
199*cdf0e10cSrcweir         css::uno::RuntimeException)
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir     throw css::lang::IllegalArgumentException(
202*cdf0e10cSrcweir         rtl::OUString(
203*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("setPropertyValue not supported")),
204*cdf0e10cSrcweir         static_cast< cppu::OWeakObject * >(this), -1);
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir css::uno::Any LdapUserProfileBe::getPropertyValue(
208*cdf0e10cSrcweir     rtl::OUString const & PropertyName)
209*cdf0e10cSrcweir     throw (
210*cdf0e10cSrcweir         css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
211*cdf0e10cSrcweir         css::uno::RuntimeException)
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     for (sal_Int32 i = 0;;) {
214*cdf0e10cSrcweir         sal_Int32 j = PropertyName.indexOf(',', i);
215*cdf0e10cSrcweir         if (j == -1) {
216*cdf0e10cSrcweir             j = PropertyName.getLength();
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir         if (j == i) {
219*cdf0e10cSrcweir             throw css::beans::UnknownPropertyException(
220*cdf0e10cSrcweir                 PropertyName, static_cast< cppu::OWeakObject * >(this));
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir         LdapData::iterator k(data_.find(PropertyName.copy(i, j - i)));
223*cdf0e10cSrcweir         if (k != data_.end()) {
224*cdf0e10cSrcweir             return css::uno::makeAny(
225*cdf0e10cSrcweir                 css::beans::Optional< css::uno::Any >(
226*cdf0e10cSrcweir                     true, css::uno::makeAny(k->second)));
227*cdf0e10cSrcweir         }
228*cdf0e10cSrcweir         if (j == PropertyName.getLength()) {
229*cdf0e10cSrcweir             break;
230*cdf0e10cSrcweir         }
231*cdf0e10cSrcweir         i = j + 1;
232*cdf0e10cSrcweir     }
233*cdf0e10cSrcweir     return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir //------------------------------------------------------------------------------
237*cdf0e10cSrcweir rtl::OUString SAL_CALL LdapUserProfileBe::getLdapUserProfileBeName(void) {
238*cdf0e10cSrcweir 	return rtl::OUString::createFromAscii("com.sun.star.comp.configuration.backend.LdapUserProfileBe") ;
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir //------------------------------------------------------------------------------
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir rtl::OUString SAL_CALL LdapUserProfileBe::getImplementationName(void)
243*cdf0e10cSrcweir     throw (uno::RuntimeException)
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir     return getLdapUserProfileBeName() ;
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir //------------------------------------------------------------------------------
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL LdapUserProfileBe::getLdapUserProfileBeServiceNames(void)
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir     uno::Sequence<rtl::OUString> aServices(1) ;
252*cdf0e10cSrcweir     aServices[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.backend.LdapUserProfileBe")) ;
253*cdf0e10cSrcweir     return aServices ;
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir //------------------------------------------------------------------------------
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const rtl::OUString& aServiceName)
258*cdf0e10cSrcweir     throw (uno::RuntimeException)
259*cdf0e10cSrcweir {
260*cdf0e10cSrcweir     uno::Sequence< rtl::OUString > const svc = getLdapUserProfileBeServiceNames();
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	for(sal_Int32 i = 0; i < svc.getLength(); ++i )
263*cdf0e10cSrcweir 		if(svc[i] == aServiceName)
264*cdf0e10cSrcweir 			return true;
265*cdf0e10cSrcweir 	return false;
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir //------------------------------------------------------------------------------
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir uno::Sequence<rtl::OUString>
271*cdf0e10cSrcweir SAL_CALL LdapUserProfileBe::getSupportedServiceNames(void)
272*cdf0e10cSrcweir     throw (uno::RuntimeException)
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir     return getLdapUserProfileBeServiceNames() ;
275*cdf0e10cSrcweir }
276*cdf0e10cSrcweir // ---------------------------------------------------------------------------------------
277*cdf0e10cSrcweir }}}
278*cdf0e10cSrcweir // ---------------------------------------------------------------------------------------
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir 
281