bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / config / ldap / ldapuserprofilebe.cxx
blobcbf04fd6b027b57624ca31b992c777201aa68a98
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
30 #include <rtl/instance.hxx>
31 #include <com/sun/star/beans/NamedValue.hpp>
32 #include <com/sun/star/beans/Optional.hpp>
33 #include <com/sun/star/configuration/theDefaultProvider.hpp>
34 #include <comphelper/scopeguard.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <osl/security.hxx>
39 namespace extensions { namespace config { namespace ldap {
41 LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
42 : LdapProfileMutexHolder(),
43 BackendBase(mMutex)
45 LdapDefinition aDefinition;
46 OUString loggedOnUser;
47 // true initially to handle reentrant call; will become false if readLdapConfiguration fails
48 bool bHaveLdapConfiguration = true;
50 // This whole rigmarole is to prevent an infinite recursion where reading
51 // the configuration for the backend would create another instance of the
52 // backend, which would try and read the configuration which would...
54 osl::Mutex & aInitMutex = rtl::Static< osl::Mutex, LdapUserProfileBe >::get();
55 osl::MutexGuard aInitGuard(aInitMutex);
57 static bool bReentrantCall; // = false
58 OSL_ENSURE(!bReentrantCall, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
60 if (!bReentrantCall)
62 bReentrantCall = true ;
63 comphelper::ScopeGuard aReentrantCallGuard([]() { bReentrantCall = false; });
64 // Don't throw on fail: this will crash if LDAP is misconfigured, and user opens
65 // Expert Configuration dialog. Instead, just don't fill data_, which will make the
66 // backend return empty values. This happens in SvtUserOptions::Impl::GetValue_Impl
67 // anyway even in throwing scenario, but doing it here also improves performance
68 // because of avoiding repeated attempts to create the backend.
69 bHaveLdapConfiguration = readLdapConfiguration(
70 xContext, &aDefinition, &loggedOnUser);
71 if (!bHaveLdapConfiguration)
72 SAL_WARN("extensions.config", "LdapUserProfileBackend: LDAP not configured");
76 if (bHaveLdapConfiguration)
78 LdapConnection connection;
79 connection.connectSimple(aDefinition);
80 connection.getUserProfile(loggedOnUser, &data_);
84 LdapUserProfileBe::~LdapUserProfileBe()
89 bool LdapUserProfileBe::readLdapConfiguration(
90 css::uno::Reference< css::uno::XComponentContext > const & context,
91 LdapDefinition * definition, OUString * loggedOnUser)
93 OSL_ASSERT(context.is() && definition != nullptr && loggedOnUser != nullptr);
94 const OUString kReadOnlyViewService("com.sun.star.configuration.ConfigurationAccess") ;
95 const OUString kComponent("org.openoffice.LDAP/UserDirectory");
96 const OUString kServerDefiniton("ServerDefinition");
97 const OUString kServer("Server");
98 const OUString kPort("Port");
99 const OUString kBaseDN("BaseDN");
100 const OUString kUser("SearchUser");
101 const OUString kPassword("SearchPassword");
102 const OUString kUserObjectClass("UserObjectClass");
103 const OUString kUserUniqueAttr("UserUniqueAttribute");
105 uno::Reference< XInterface > xIface;
108 uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
109 css::configuration::theDefaultProvider::get(context));
111 css::beans::NamedValue aPath("nodepath", uno::makeAny(kComponent) );
113 uno::Sequence< uno::Any > aArgs(1);
114 aArgs[0] <<= aPath;
116 xIface = xCfgProvider->createInstanceWithArguments(kReadOnlyViewService, aArgs);
118 uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
119 xAccess->getByName(kServerDefiniton) >>= xIface;
121 uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
123 if (!getLdapStringParam(xChildAccess, kServer, definition->mServer))
124 return false;
125 if (!getLdapStringParam(xChildAccess, kBaseDN, definition->mBaseDN))
126 return false;
128 definition->mPort=0;
129 xChildAccess->getByName(kPort) >>= definition->mPort ;
130 if (definition->mPort == 0)
131 return false;
133 if (!getLdapStringParam(xAccess, kUserObjectClass, definition->mUserObjectClass))
134 return false;
135 if (!getLdapStringParam(xAccess, kUserUniqueAttr, definition->mUserUniqueAttr))
136 return false;
138 getLdapStringParam(xAccess, kUser, definition->mAnonUser);
139 getLdapStringParam(xAccess, kPassword, definition->mAnonCredentials);
141 catch (const uno::Exception & e)
143 SAL_WARN("extensions.config", "LdapUserProfileBackend: access to configuration data failed: " << e);
144 return false;
147 osl::Security aSecurityContext;
148 if (!aSecurityContext.getUserName(*loggedOnUser))
149 SAL_WARN("extensions.config", "LdapUserProfileBackend - could not get Logged on user from system");
151 sal_Int32 nIndex = loggedOnUser->indexOf('/');
152 if (nIndex > 0)
153 *loggedOnUser = loggedOnUser->copy(nIndex+1);
155 return true;
159 bool LdapUserProfileBe::getLdapStringParam(
160 uno::Reference<container::XNameAccess> const & xAccess,
161 const OUString& aLdapSetting,
162 OUString& aServerParameter)
164 xAccess->getByName(aLdapSetting) >>= aServerParameter;
166 return !aServerParameter.isEmpty();
169 void LdapUserProfileBe::setPropertyValue(
170 OUString const &, css::uno::Any const &)
172 throw css::lang::IllegalArgumentException(
173 "setPropertyValue not supported",
174 static_cast< cppu::OWeakObject * >(this), -1);
177 css::uno::Any LdapUserProfileBe::getPropertyValue(
178 OUString const & PropertyName)
180 for (sal_Int32 i = 0;;) {
181 sal_Int32 j = PropertyName.indexOf(',', i);
182 if (j == -1) {
183 j = PropertyName.getLength();
185 if (j == i) {
186 throw css::beans::UnknownPropertyException(
187 PropertyName, static_cast< cppu::OWeakObject * >(this));
189 LdapData::iterator k(data_.find(PropertyName.copy(i, j - i)));
190 if (k != data_.end()) {
191 return css::uno::makeAny(
192 css::beans::Optional< css::uno::Any >(
193 true, css::uno::makeAny(k->second)));
195 if (j == PropertyName.getLength()) {
196 break;
198 i = j + 1;
200 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
204 OUString LdapUserProfileBe::getLdapUserProfileBeName() {
205 return OUString("com.sun.star.comp.configuration.backend.LdapUserProfileBe");
209 OUString SAL_CALL LdapUserProfileBe::getImplementationName()
211 return getLdapUserProfileBeName() ;
215 uno::Sequence<OUString> LdapUserProfileBe::getLdapUserProfileBeServiceNames()
217 uno::Sequence<OUString> aServices { "com.sun.star.configuration.backend.LdapUserProfileBe" };
218 return aServices ;
221 sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const OUString& aServiceName)
223 return cppu::supportsService(this, aServiceName);
226 uno::Sequence<OUString>
227 SAL_CALL LdapUserProfileBe::getSupportedServiceNames()
229 return getLdapUserProfileBeServiceNames() ;
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */