1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "ldapaccess.hxx"
22 #include "ldapuserprofilebe.hxx"
23 #include <sal/log.hxx>
24 #include <comphelper/diagnose_ex.hxx>
26 #include <rtl/instance.hxx>
27 #include <com/sun/star/beans/NamedValue.hpp>
28 #include <com/sun/star/beans/Optional.hpp>
29 #include <com/sun/star/configuration/theDefaultProvider.hpp>
30 #include <comphelper/scopeguard.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <osl/security.hxx>
35 namespace extensions::config::ldap
{
37 LdapUserProfileBe::LdapUserProfileBe( const uno::Reference
<uno::XComponentContext
>& xContext
)
38 : BackendBase(m_aMutex
)
40 LdapDefinition aDefinition
;
41 OUString loggedOnUser
;
42 // true initially to handle reentrant call; will become false if readLdapConfiguration fails
43 bool bHaveLdapConfiguration
= true;
45 // This whole rigmarole is to prevent an infinite recursion where reading
46 // the configuration for the backend would create another instance of the
47 // backend, which would try and read the configuration which would...
49 static osl::Mutex aInitMutex
;
50 osl::MutexGuard
aInitGuard(aInitMutex
);
52 static bool bReentrantCall
; // = false
53 OSL_ENSURE(!bReentrantCall
, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
57 bReentrantCall
= true ;
58 comphelper::ScopeGuard
aReentrantCallGuard([]() { bReentrantCall
= false; });
59 // Don't throw on fail: this will crash if LDAP is misconfigured, and user opens
60 // Expert Configuration dialog. Instead, just don't fill data_, which will make the
61 // backend return empty values. This happens in SvtUserOptions::Impl::GetValue_Impl
62 // anyway even in throwing scenario, but doing it here also improves performance
63 // because of avoiding repeated attempts to create the backend.
64 bHaveLdapConfiguration
= readLdapConfiguration(
65 xContext
, &aDefinition
, &loggedOnUser
);
66 if (!bHaveLdapConfiguration
)
67 SAL_WARN("extensions.config", "LdapUserProfileBackend: LDAP not configured");
71 if (bHaveLdapConfiguration
)
73 LdapConnection connection
;
74 connection
.connectSimple(aDefinition
);
75 connection
.getUserProfile(loggedOnUser
, &data_
);
79 LdapUserProfileBe::~LdapUserProfileBe()
84 bool LdapUserProfileBe::readLdapConfiguration(
85 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
86 LdapDefinition
* definition
, OUString
* loggedOnUser
)
88 OSL_ASSERT(context
.is() && definition
!= nullptr && loggedOnUser
!= nullptr);
90 uno::Reference
< XInterface
> xIface
;
93 uno::Reference
< lang::XMultiServiceFactory
> xCfgProvider(
94 css::configuration::theDefaultProvider::get(context
));
96 css::beans::NamedValue
aPath("nodepath", uno::Any(OUString("org.openoffice.LDAP/UserDirectory")) );
98 uno::Sequence
< uno::Any
> aArgs
{ uno::Any(aPath
) };
100 xIface
= xCfgProvider
->createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aArgs
);
102 uno::Reference
<container::XNameAccess
> xAccess(xIface
, uno::UNO_QUERY_THROW
);
103 xAccess
->getByName("ServerDefinition") >>= xIface
;
105 uno::Reference
<container::XNameAccess
> xChildAccess(xIface
, uno::UNO_QUERY_THROW
);
107 if (!getLdapStringParam(xChildAccess
, "Server", definition
->mServer
))
109 if (!getLdapStringParam(xChildAccess
, "BaseDN", definition
->mBaseDN
))
113 xChildAccess
->getByName("Port") >>= definition
->mPort
;
114 if (definition
->mPort
== 0)
117 if (!getLdapStringParam(xAccess
, "UserObjectClass", definition
->mUserObjectClass
))
119 if (!getLdapStringParam(xAccess
, "UserUniqueAttribute", definition
->mUserUniqueAttr
))
122 getLdapStringParam(xAccess
, "SearchUser", definition
->mAnonUser
);
123 getLdapStringParam(xAccess
, "SearchPassword", definition
->mAnonCredentials
);
125 catch (const uno::Exception
&)
127 TOOLS_WARN_EXCEPTION("extensions.config", "LdapUserProfileBackend: access to configuration data failed");
131 osl::Security aSecurityContext
;
132 if (!aSecurityContext
.getUserName(*loggedOnUser
))
133 SAL_WARN("extensions.config", "LdapUserProfileBackend - could not get Logged on user from system");
135 sal_Int32 nIndex
= loggedOnUser
->indexOf('/');
137 *loggedOnUser
= loggedOnUser
->copy(nIndex
+1);
143 bool LdapUserProfileBe::getLdapStringParam(
144 uno::Reference
<container::XNameAccess
> const & xAccess
,
145 const OUString
& aLdapSetting
,
146 OUString
& aServerParameter
)
148 xAccess
->getByName(aLdapSetting
) >>= aServerParameter
;
150 return !aServerParameter
.isEmpty();
153 void LdapUserProfileBe::setPropertyValue(
154 OUString
const &, css::uno::Any
const &)
156 throw css::lang::IllegalArgumentException(
157 "setPropertyValue not supported",
158 static_cast< cppu::OWeakObject
* >(this), -1);
161 css::uno::Any
LdapUserProfileBe::getPropertyValue(
162 OUString
const & PropertyName
)
164 for (sal_Int32 i
= 0;;) {
165 sal_Int32 j
= PropertyName
.indexOf(',', i
);
167 j
= PropertyName
.getLength();
170 throw css::beans::UnknownPropertyException(
171 PropertyName
, static_cast< cppu::OWeakObject
* >(this));
173 LdapData::iterator
k(data_
.find(PropertyName
.copy(i
, j
- i
)));
174 if (k
!= data_
.end()) {
175 return css::uno::Any(
176 css::beans::Optional
< css::uno::Any
>(
177 true, css::uno::Any(k
->second
)));
179 if (j
== PropertyName
.getLength()) {
184 return css::uno::Any(css::beans::Optional
< css::uno::Any
>());
188 OUString SAL_CALL
LdapUserProfileBe::getImplementationName()
190 return "com.sun.star.comp.configuration.backend.LdapUserProfileBe";
193 sal_Bool SAL_CALL
LdapUserProfileBe::supportsService(const OUString
& aServiceName
)
195 return cppu::supportsService(this, aServiceName
);
198 uno::Sequence
<OUString
>
199 SAL_CALL
LdapUserProfileBe::getSupportedServiceNames()
201 return { "com.sun.star.configuration.backend.LdapUserProfileBe" };
206 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
207 extensions_ldp_LdapUserProfileBe_get_implementation(
208 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
210 return cppu::acquire(new extensions::config::ldap::LdapUserProfileBe(context
));
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */