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 <osl/file.hxx>
24 #include <osl/module.hxx>
25 #include <osl/process.h>
26 #include <rtl/ustrbuf.hxx>
27 #include <rtl/byteseq.h>
28 #include <sal/log.hxx>
29 #include <tools/diagnose_ex.h>
31 #include <rtl/instance.hxx>
32 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <com/sun/star/beans/Optional.hpp>
34 #include <com/sun/star/configuration/theDefaultProvider.hpp>
35 #include <comphelper/scopeguard.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <osl/security.hxx>
40 namespace extensions
{ namespace config
{ namespace ldap
{
42 LdapUserProfileBe::LdapUserProfileBe( const uno::Reference
<uno::XComponentContext
>& xContext
)
43 : LdapProfileMutexHolder(),
46 LdapDefinition aDefinition
;
47 OUString loggedOnUser
;
48 // true initially to handle reentrant call; will become false if readLdapConfiguration fails
49 bool bHaveLdapConfiguration
= true;
51 // This whole rigmarole is to prevent an infinite recursion where reading
52 // the configuration for the backend would create another instance of the
53 // backend, which would try and read the configuration which would...
55 osl::Mutex
& aInitMutex
= rtl::Static
< osl::Mutex
, LdapUserProfileBe
>::get();
56 osl::MutexGuard
aInitGuard(aInitMutex
);
58 static bool bReentrantCall
; // = false
59 OSL_ENSURE(!bReentrantCall
, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
63 bReentrantCall
= true ;
64 comphelper::ScopeGuard
aReentrantCallGuard([]() { bReentrantCall
= false; });
65 // Don't throw on fail: this will crash if LDAP is misconfigured, and user opens
66 // Expert Configuration dialog. Instead, just don't fill data_, which will make the
67 // backend return empty values. This happens in SvtUserOptions::Impl::GetValue_Impl
68 // anyway even in throwing scenario, but doing it here also improves performance
69 // because of avoiding repeated attempts to create the backend.
70 bHaveLdapConfiguration
= readLdapConfiguration(
71 xContext
, &aDefinition
, &loggedOnUser
);
72 if (!bHaveLdapConfiguration
)
73 SAL_WARN("extensions.config", "LdapUserProfileBackend: LDAP not configured");
77 if (bHaveLdapConfiguration
)
79 LdapConnection connection
;
80 connection
.connectSimple(aDefinition
);
81 connection
.getUserProfile(loggedOnUser
, &data_
);
85 LdapUserProfileBe::~LdapUserProfileBe()
90 bool LdapUserProfileBe::readLdapConfiguration(
91 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
92 LdapDefinition
* definition
, OUString
* loggedOnUser
)
94 OSL_ASSERT(context
.is() && definition
!= nullptr && loggedOnUser
!= nullptr);
95 const OUString
kReadOnlyViewService("com.sun.star.configuration.ConfigurationAccess") ;
96 const OUString
kComponent("org.openoffice.LDAP/UserDirectory");
97 const OUString
kServerDefiniton("ServerDefinition");
98 const OUString
kServer("Server");
99 const OUString
kPort("Port");
100 const OUString
kBaseDN("BaseDN");
101 const OUString
kUser("SearchUser");
102 const OUString
kPassword("SearchPassword");
103 const OUString
kUserObjectClass("UserObjectClass");
104 const OUString
kUserUniqueAttr("UserUniqueAttribute");
106 uno::Reference
< XInterface
> xIface
;
109 uno::Reference
< lang::XMultiServiceFactory
> xCfgProvider(
110 css::configuration::theDefaultProvider::get(context
));
112 css::beans::NamedValue
aPath("nodepath", uno::makeAny(kComponent
) );
114 uno::Sequence
< uno::Any
> aArgs(1);
117 xIface
= xCfgProvider
->createInstanceWithArguments(kReadOnlyViewService
, aArgs
);
119 uno::Reference
<container::XNameAccess
> xAccess(xIface
, uno::UNO_QUERY_THROW
);
120 xAccess
->getByName(kServerDefiniton
) >>= xIface
;
122 uno::Reference
<container::XNameAccess
> xChildAccess(xIface
, uno::UNO_QUERY_THROW
);
124 if (!getLdapStringParam(xChildAccess
, kServer
, definition
->mServer
))
126 if (!getLdapStringParam(xChildAccess
, kBaseDN
, definition
->mBaseDN
))
130 xChildAccess
->getByName(kPort
) >>= definition
->mPort
;
131 if (definition
->mPort
== 0)
134 if (!getLdapStringParam(xAccess
, kUserObjectClass
, definition
->mUserObjectClass
))
136 if (!getLdapStringParam(xAccess
, kUserUniqueAttr
, definition
->mUserUniqueAttr
))
139 getLdapStringParam(xAccess
, kUser
, definition
->mAnonUser
);
140 getLdapStringParam(xAccess
, kPassword
, definition
->mAnonCredentials
);
142 catch (const uno::Exception
&)
144 TOOLS_WARN_EXCEPTION("extensions.config", "LdapUserProfileBackend: access to configuration data failed");
148 osl::Security aSecurityContext
;
149 if (!aSecurityContext
.getUserName(*loggedOnUser
))
150 SAL_WARN("extensions.config", "LdapUserProfileBackend - could not get Logged on user from system");
152 sal_Int32 nIndex
= loggedOnUser
->indexOf('/');
154 *loggedOnUser
= loggedOnUser
->copy(nIndex
+1);
160 bool LdapUserProfileBe::getLdapStringParam(
161 uno::Reference
<container::XNameAccess
> const & xAccess
,
162 const OUString
& aLdapSetting
,
163 OUString
& aServerParameter
)
165 xAccess
->getByName(aLdapSetting
) >>= aServerParameter
;
167 return !aServerParameter
.isEmpty();
170 void LdapUserProfileBe::setPropertyValue(
171 OUString
const &, css::uno::Any
const &)
173 throw css::lang::IllegalArgumentException(
174 "setPropertyValue not supported",
175 static_cast< cppu::OWeakObject
* >(this), -1);
178 css::uno::Any
LdapUserProfileBe::getPropertyValue(
179 OUString
const & PropertyName
)
181 for (sal_Int32 i
= 0;;) {
182 sal_Int32 j
= PropertyName
.indexOf(',', i
);
184 j
= PropertyName
.getLength();
187 throw css::beans::UnknownPropertyException(
188 PropertyName
, static_cast< cppu::OWeakObject
* >(this));
190 LdapData::iterator
k(data_
.find(PropertyName
.copy(i
, j
- i
)));
191 if (k
!= data_
.end()) {
192 return css::uno::makeAny(
193 css::beans::Optional
< css::uno::Any
>(
194 true, css::uno::makeAny(k
->second
)));
196 if (j
== PropertyName
.getLength()) {
201 return css::uno::makeAny(css::beans::Optional
< css::uno::Any
>());
205 OUString
LdapUserProfileBe::getLdapUserProfileBeName() {
206 return "com.sun.star.comp.configuration.backend.LdapUserProfileBe";
210 OUString SAL_CALL
LdapUserProfileBe::getImplementationName()
212 return getLdapUserProfileBeName() ;
216 uno::Sequence
<OUString
> LdapUserProfileBe::getLdapUserProfileBeServiceNames()
218 uno::Sequence
<OUString
> aServices
{ "com.sun.star.configuration.backend.LdapUserProfileBe" };
222 sal_Bool SAL_CALL
LdapUserProfileBe::supportsService(const OUString
& aServiceName
)
224 return cppu::supportsService(this, aServiceName
);
227 uno::Sequence
<OUString
>
228 SAL_CALL
LdapUserProfileBe::getSupportedServiceNames()
230 return getLdapUserProfileBeServiceNames() ;
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */