LibreOffice Module extensions (master) 1
ldapuserprofilebe.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include "ldapaccess.hxx"
22#include "ldapuserprofilebe.hxx"
23#include <sal/log.hxx>
25
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>
32#include <osl/security.hxx>
33
34
36
37LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
39{
40 LdapDefinition aDefinition;
41 OUString loggedOnUser;
42 // true initially to handle reentrant call; will become false if readLdapConfiguration fails
43 bool bHaveLdapConfiguration = true;
44
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...
48 {
49 static osl::Mutex aInitMutex;
50 osl::MutexGuard aInitGuard(aInitMutex);
51
52 static bool bReentrantCall; // = false
53 OSL_ENSURE(!bReentrantCall, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
54
55 if (!bReentrantCall)
56 {
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");
68 }
69 }
70
71 if (bHaveLdapConfiguration)
72 {
73 LdapConnection connection;
74 connection.connectSimple(aDefinition);
75 connection.getUserProfile(loggedOnUser, &data_);
76 }
77}
78
80{
81}
82
83
85 css::uno::Reference< css::uno::XComponentContext > const & context,
86 LdapDefinition * definition, OUString * loggedOnUser)
87{
88 OSL_ASSERT(context.is() && definition != nullptr && loggedOnUser != nullptr);
89
90 uno::Reference< XInterface > xIface;
91 try
92 {
93 uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
94 css::configuration::theDefaultProvider::get(context));
95
96 css::beans::NamedValue aPath("nodepath", uno::Any(OUString("org.openoffice.LDAP/UserDirectory")) );
97
98 uno::Sequence< uno::Any > aArgs{ uno::Any(aPath) };
99
100 xIface = xCfgProvider->createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aArgs);
101
102 uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
103 xAccess->getByName("ServerDefinition") >>= xIface;
104
105 uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
106
107 if (!getLdapStringParam(xChildAccess, "Server", definition->mServer))
108 return false;
109 if (!getLdapStringParam(xChildAccess, "BaseDN", definition->mBaseDN))
110 return false;
111
112 definition->mPort=0;
113 xChildAccess->getByName("Port") >>= definition->mPort ;
114 if (definition->mPort == 0)
115 return false;
116
117 if (!getLdapStringParam(xAccess, "UserObjectClass", definition->mUserObjectClass))
118 return false;
119 if (!getLdapStringParam(xAccess, "UserUniqueAttribute", definition->mUserUniqueAttr))
120 return false;
121
122 getLdapStringParam(xAccess, "SearchUser", definition->mAnonUser);
123 getLdapStringParam(xAccess, "SearchPassword", definition->mAnonCredentials);
124 }
125 catch (const uno::Exception&)
126 {
127 TOOLS_WARN_EXCEPTION("extensions.config", "LdapUserProfileBackend: access to configuration data failed");
128 return false;
129 }
130
131 osl::Security aSecurityContext;
132 if (!aSecurityContext.getUserName(*loggedOnUser))
133 SAL_WARN("extensions.config", "LdapUserProfileBackend - could not get Logged on user from system");
134
135 sal_Int32 nIndex = loggedOnUser->indexOf('/');
136 if (nIndex > 0)
137 *loggedOnUser = loggedOnUser->copy(nIndex+1);
138
139 return true;
140}
141
142
144 uno::Reference<container::XNameAccess> const & xAccess,
145 const OUString& aLdapSetting,
146 OUString& aServerParameter)
147{
148 xAccess->getByName(aLdapSetting) >>= aServerParameter;
149
150 return !aServerParameter.isEmpty();
151}
152
154 OUString const &, css::uno::Any const &)
155{
156 throw css::lang::IllegalArgumentException(
157 "setPropertyValue not supported",
158 static_cast< cppu::OWeakObject * >(this), -1);
159}
160
162 OUString const & PropertyName)
163{
164 for (sal_Int32 i = 0;;) {
165 sal_Int32 j = PropertyName.indexOf(',', i);
166 if (j == -1) {
167 j = PropertyName.getLength();
168 }
169 if (j == i) {
170 throw css::beans::UnknownPropertyException(
171 PropertyName, static_cast< cppu::OWeakObject * >(this));
172 }
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)));
178 }
179 if (j == PropertyName.getLength()) {
180 break;
181 }
182 i = j + 1;
183 }
184 return css::uno::Any(css::beans::Optional< css::uno::Any >());
185}
186
187
189{
190 return "com.sun.star.comp.configuration.backend.LdapUserProfileBe";
191}
192
193sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const OUString& aServiceName)
194{
195 return cppu::supportsService(this, aServiceName);
196}
197
198uno::Sequence<OUString>
200{
201 return { "com.sun.star.configuration.backend.LdapUserProfileBe" };
202}
203
204}
205
206extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
208 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
209{
210 return cppu::acquire(new extensions::config::ldap::LdapUserProfileBe(context));
211}
212
213
214/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Class encapsulating all LDAP functionality.
Definition: ldapaccess.hxx:76
void connectSimple(const LdapDefinition &aDefinition)
Make connection to LDAP server.
Definition: ldapaccess.cxx:98
void getUserProfile(const OUString &aUser, LdapData *data)
Gets LdapUserProfile from LDAP repository for specified user.
Definition: ldapaccess.cxx:172
Implements the PlatformBackend service, a specialization of the XPropertySet service for retrieving L...
LdapUserProfileBe(const uno::Reference< uno::XComponentContext > &xContext)
virtual OUString SAL_CALL getImplementationName() override
static bool getLdapStringParam(uno::Reference< container::XNameAccess > const &xAccess, const OUString &aLdapSetting, OUString &aServerParameter)
virtual sal_Bool SAL_CALL supportsService(const OUString &aServiceName) override
virtual void SAL_CALL setPropertyValue(OUString const &, css::uno::Any const &) override
virtual css::uno::Any SAL_CALL getPropertyValue(OUString const &PropertyName) override
virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static bool readLdapConfiguration(uno::Reference< uno::XComponentContext > const &context, LdapDefinition *definition, OUString *loggedOnUser)
Check if LDAP is configured.
#define TOOLS_WARN_EXCEPTION(area, stream)
sal_Int32 nIndex
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * extensions_ldp_LdapUserProfileBe_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
#define SAL_WARN(area, stream)
::osl::Mutex m_aMutex
Definition: logger.cxx:98
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
cppu::WeakComponentImplHelper< css::beans::XPropertySet, lang::XServiceInfo > BackendBase
int i
Struct containing the information on LDAP connection.
Definition: ldapaccess.hxx:50
OUString mAnonUser
DN to use for "anonymous" connection.
Definition: ldapaccess.hxx:58
OUString mServer
LDAP server name.
Definition: ldapaccess.hxx:52
OUString mUserObjectClass
User Entity Object Class.
Definition: ldapaccess.hxx:62
OUString mUserUniqueAttr
User Entity Unique Attribute.
Definition: ldapaccess.hxx:64
OUString mAnonCredentials
Credentials to use for "anonymous" connection.
Definition: ldapaccess.hxx:60
OUString mBaseDN
Repository base DN.
Definition: ldapaccess.hxx:56
sal_Int32 mPort
LDAP server port number.
Definition: ldapaccess.hxx:54
unsigned char sal_Bool