Line data Source code
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 <osl/file.hxx>
24 : #include <osl/module.hxx>
25 : #include <osl/process.h>
26 : #include <rtl/ustrbuf.hxx>
27 : #include <rtl/byteseq.h>
28 :
29 : #include <rtl/instance.hxx>
30 : #include <com/sun/star/beans/NamedValue.hpp>
31 : #include <com/sun/star/beans/Optional.hpp>
32 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
33 : #include <osl/security.hxx>
34 :
35 : //==============================================================================
36 : namespace extensions { namespace config { namespace ldap {
37 :
38 0 : LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
39 : : LdapProfileMutexHolder(),
40 0 : BackendBase(mMutex)
41 : {
42 0 : LdapDefinition aDefinition;
43 0 : rtl::OUString loggedOnUser;
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 0 : osl::Mutex & aInitMutex = rtl::Static< osl::Mutex, LdapUserProfileBe >::get();
50 0 : 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 0 : if (!bReentrantCall)
56 : {
57 : try
58 : {
59 0 : bReentrantCall = true ;
60 0 : if (!readLdapConfiguration(
61 0 : xContext, &aDefinition, &loggedOnUser))
62 : {
63 : throw css::uno::RuntimeException(
64 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("LdapUserProfileBe- LDAP not configured")),
65 0 : NULL);
66 : }
67 :
68 0 : bReentrantCall = false ;
69 : }
70 0 : catch (...)
71 : {
72 0 : bReentrantCall = false;
73 0 : throw;
74 : }
75 0 : }
76 : }
77 :
78 0 : LdapConnection connection;
79 0 : connection.connectSimple(aDefinition);
80 0 : connection.getUserProfile(loggedOnUser, &data_);
81 0 : }
82 : //------------------------------------------------------------------------------
83 0 : LdapUserProfileBe::~LdapUserProfileBe()
84 : {
85 0 : }
86 : //------------------------------------------------------------------------------
87 :
88 0 : bool LdapUserProfileBe::readLdapConfiguration(
89 : css::uno::Reference< css::uno::XComponentContext > const & context,
90 : LdapDefinition * definition, rtl::OUString * loggedOnUser)
91 : {
92 : OSL_ASSERT(context.is() && definition != 0 && loggedOnUser != 0);
93 0 : const rtl::OUString kReadOnlyViewService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")) ;
94 0 : const rtl::OUString kComponent( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.LDAP/UserDirectory"));
95 0 : const rtl::OUString kServerDefiniton(RTL_CONSTASCII_USTRINGPARAM ("ServerDefinition"));
96 0 : const rtl::OUString kServer(RTL_CONSTASCII_USTRINGPARAM ("Server"));
97 0 : const rtl::OUString kPort(RTL_CONSTASCII_USTRINGPARAM("Port"));
98 0 : const rtl::OUString kBaseDN(RTL_CONSTASCII_USTRINGPARAM("BaseDN"));
99 0 : const rtl::OUString kUser(RTL_CONSTASCII_USTRINGPARAM("SearchUser"));
100 0 : const rtl::OUString kPassword(RTL_CONSTASCII_USTRINGPARAM("SearchPassword"));
101 0 : const rtl::OUString kUserObjectClass(RTL_CONSTASCII_USTRINGPARAM("UserObjectClass"));
102 0 : const rtl::OUString kUserUniqueAttr(RTL_CONSTASCII_USTRINGPARAM("UserUniqueAttribute"));
103 :
104 0 : uno::Reference< XInterface > xIface;
105 : try
106 : {
107 : uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
108 0 : css::configuration::theDefaultProvider::get(context));
109 :
110 0 : css::beans::NamedValue aPath(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), uno::makeAny(kComponent) );
111 :
112 0 : uno::Sequence< uno::Any > aArgs(1);
113 0 : aArgs[0] <<= aPath;
114 :
115 0 : xIface = xCfgProvider->createInstanceWithArguments(kReadOnlyViewService, aArgs);
116 :
117 0 : uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
118 0 : xAccess->getByName(kServerDefiniton) >>= xIface;
119 :
120 0 : uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
121 :
122 0 : if (!getLdapStringParam(xChildAccess, kServer, definition->mServer))
123 0 : return false;
124 0 : if (!getLdapStringParam(xChildAccess, kBaseDN, definition->mBaseDN))
125 0 : return false;
126 :
127 0 : definition->mPort=0;
128 0 : xChildAccess->getByName(kPort) >>= definition->mPort ;
129 0 : if (definition->mPort == 0)
130 0 : return false;
131 :
132 0 : if (!getLdapStringParam(xAccess, kUserObjectClass, definition->mUserObjectClass))
133 0 : return false;
134 0 : if (!getLdapStringParam(xAccess, kUserUniqueAttr, definition->mUserUniqueAttr))
135 0 : return false;
136 :
137 0 : getLdapStringParam(xAccess, kUser, definition->mAnonUser);
138 0 : getLdapStringParam(xAccess, kPassword, definition->mAnonCredentials);
139 : }
140 0 : catch (const uno::Exception & e)
141 : {
142 : OSL_TRACE("LdapUserProfileBackend: access to configuration data failed: %s",
143 : rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
144 0 : return false;
145 : }
146 :
147 0 : osl::Security aSecurityContext;
148 0 : if (!aSecurityContext.getUserName(*loggedOnUser))
149 : OSL_TRACE("LdapUserProfileBackend - could not get Logged on user from system");
150 :
151 0 : sal_Int32 nIndex = loggedOnUser->indexOf('/');
152 0 : if (nIndex > 0)
153 0 : *loggedOnUser = loggedOnUser->copy(nIndex+1);
154 :
155 : //Remember to remove
156 : OSL_TRACE("Logged on user is %s", rtl::OUStringToOString(*loggedOnUser,RTL_TEXTENCODING_ASCII_US).getStr());
157 :
158 0 : return true;
159 : }
160 :
161 : //------------------------------------------------------------------------------
162 0 : bool LdapUserProfileBe::getLdapStringParam(
163 : uno::Reference<container::XNameAccess>& xAccess,
164 : const rtl::OUString& aLdapSetting,
165 : rtl::OUString& aServerParameter)
166 : {
167 0 : xAccess->getByName(aLdapSetting) >>= aServerParameter;
168 :
169 0 : return !aServerParameter.isEmpty();
170 : }
171 : //------------------------------------------------------------------------------
172 0 : void LdapUserProfileBe::setPropertyValue(
173 : rtl::OUString const &, css::uno::Any const &)
174 : throw (
175 : css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
176 : css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
177 : css::uno::RuntimeException)
178 : {
179 : throw css::lang::IllegalArgumentException(
180 : rtl::OUString(
181 : RTL_CONSTASCII_USTRINGPARAM("setPropertyValue not supported")),
182 0 : static_cast< cppu::OWeakObject * >(this), -1);
183 : }
184 :
185 0 : css::uno::Any LdapUserProfileBe::getPropertyValue(
186 : rtl::OUString const & PropertyName)
187 : throw (
188 : css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
189 : css::uno::RuntimeException)
190 : {
191 0 : for (sal_Int32 i = 0;;) {
192 0 : sal_Int32 j = PropertyName.indexOf(',', i);
193 0 : if (j == -1) {
194 0 : j = PropertyName.getLength();
195 : }
196 0 : if (j == i) {
197 : throw css::beans::UnknownPropertyException(
198 0 : PropertyName, static_cast< cppu::OWeakObject * >(this));
199 : }
200 0 : LdapData::iterator k(data_.find(PropertyName.copy(i, j - i)));
201 0 : if (k != data_.end()) {
202 : return css::uno::makeAny(
203 : css::beans::Optional< css::uno::Any >(
204 0 : true, css::uno::makeAny(k->second)));
205 : }
206 0 : if (j == PropertyName.getLength()) {
207 : break;
208 : }
209 0 : i = j + 1;
210 : }
211 0 : return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
212 : }
213 :
214 : //------------------------------------------------------------------------------
215 0 : rtl::OUString SAL_CALL LdapUserProfileBe::getLdapUserProfileBeName(void) {
216 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.configuration.backend.LdapUserProfileBe"));
217 : }
218 : //------------------------------------------------------------------------------
219 :
220 0 : rtl::OUString SAL_CALL LdapUserProfileBe::getImplementationName(void)
221 : throw (uno::RuntimeException)
222 : {
223 0 : return getLdapUserProfileBeName() ;
224 : }
225 : //------------------------------------------------------------------------------
226 :
227 0 : uno::Sequence<rtl::OUString> SAL_CALL LdapUserProfileBe::getLdapUserProfileBeServiceNames(void)
228 : {
229 0 : uno::Sequence<rtl::OUString> aServices(1) ;
230 0 : aServices[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.backend.LdapUserProfileBe")) ;
231 0 : return aServices ;
232 : }
233 : //------------------------------------------------------------------------------
234 :
235 0 : sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const rtl::OUString& aServiceName)
236 : throw (uno::RuntimeException)
237 : {
238 0 : uno::Sequence< rtl::OUString > const svc = getLdapUserProfileBeServiceNames();
239 :
240 0 : for(sal_Int32 i = 0; i < svc.getLength(); ++i )
241 0 : if(svc[i] == aServiceName)
242 0 : return true;
243 0 : return false;
244 : }
245 :
246 : //------------------------------------------------------------------------------
247 :
248 : uno::Sequence<rtl::OUString>
249 0 : SAL_CALL LdapUserProfileBe::getSupportedServiceNames(void)
250 : throw (uno::RuntimeException)
251 : {
252 0 : return getLdapUserProfileBeServiceNames() ;
253 : }
254 : // ---------------------------------------------------------------------------------------
255 : }}}
256 : // ---------------------------------------------------------------------------------------
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|