1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef EXTENSIONS_CONFIG_LDAP_LDAPACCESS_HXX_
29 #define EXTENSIONS_CONFIG_LDAP_LDAPACCESS_HXX_
30 
31 #include "sal/config.h"
32 
33 #include <map>
34 
35 #include "wrapldapinclude.hxx"
36 #include <com/sun/star/ldap/LdapGenericException.hpp>
37 
38 #include <com/sun/star/ldap/LdapConnectionException.hpp>
39 #include <com/sun/star/lang/IllegalArgumentException.hpp>
40 #include <osl/module.h>
41 
42 namespace extensions { namespace config { namespace ldap {
43 
44 namespace css = com::sun::star ;
45 namespace uno = css::uno ;
46 namespace lang = css::lang ;
47 namespace ldap = css::ldap ;
48 //------------------------------------------------------------------------------
49 // LdapUserProfile classes
50 struct LdapUserProfile;
51 class LdapUserProfileMap;
52 
53 typedef LDAP_API(int) (LDAP_CALL *t_ldap_unbind_s)( LDAP *ld );
54 typedef LDAP_API(int) (LDAP_CALL *t_ldap_simple_bind_s)( LDAP *ld, const char *who,	const char *passwd );
55 typedef LDAP_API(int) (LDAP_CALL *t_ldap_set_option)( LDAP *ld, int option,	const void *optdata );
56 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_err2string)( int err );
57 typedef LDAP_API(LDAP *) (LDAP_CALL *t_ldap_init)( const char *defhost, int defport );
58 typedef LDAP_API(int) (LDAP_CALL *t_ldap_msgfree)( LDAPMessage *lm );
59 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_get_dn)( LDAP *ld, LDAPMessage *entry );
60 typedef LDAP_API(LDAPMessage *) (LDAP_CALL *t_ldap_first_entry)( LDAP *ld, 	LDAPMessage *chain );
61 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_first_attribute)( LDAP *ld, LDAPMessage *entry, void **ptr );
62 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_next_attribute)( LDAP *ld, LDAPMessage *entry, void *ptr );
63 typedef LDAP_API(int) (LDAP_CALL *t_ldap_search_s)( LDAP *ld, const char *base, int scope,	const char *filter, char **attrs, int attrsonly, LDAPMessage **res );
64 typedef LDAP_API(void) (LDAP_CALL *t_ldap_value_free)( char **vals );
65 typedef LDAP_API(char **) (LDAP_CALL *t_ldap_get_values)( LDAP *ld, LDAPMessage *entry,	const char *target );
66 typedef LDAP_API(void) (LDAP_CALL *t_ldap_memfree)( void *p );
67 //------------------------------------------------------------------------------
68 /** Struct containing the information on LDAP connection */
69 struct LdapDefinition
70 {
71 	/** LDAP server name */
72     rtl::OString mServer ;
73     /** LDAP server port number */
74     sal_Int32 mPort ;
75     /** Repository base DN */
76     rtl::OString mBaseDN ;
77     /** DN to use for "anonymous" connection */
78     rtl::OString mAnonUser ;
79     /** Credentials to use for "anonymous" connection */
80     rtl::OString mAnonCredentials ;
81 	/** User Entity Object Class */
82 	rtl::OString mUserObjectClass;
83 	/** User Entity Unique Attribute */
84 	rtl::OString mUserUniqueAttr;
85  } ;
86 
87 typedef std::map< rtl::OUString, rtl::OUString > LdapData; // key/value pairs
88 
89 /** Class encapulating all LDAP functionality */
90 class LdapConnection
91 {
92     friend struct LdapMessageHolder;
93 public:
94 
95     /** Default constructor */
96     LdapConnection(void) : mConnection(NULL),mLdapDefinition() {}
97     /** Destructor, releases the connection */
98     ~LdapConnection(void) ;
99 	/** Make connection to LDAP server */
100 	void  connectSimple(const LdapDefinition& aDefinition)
101         throw (ldap::LdapConnectionException,
102                 ldap::LdapGenericException);
103 
104 	/**
105         Gets LdapUserProfile from LDAP repository for specified user
106         @param aUser    name of logged on user
107         @param aUserProfileMap  Map containing LDAP->00o mapping
108        @param aUserProfile     struct for holding OOo values
109 
110          @throws com::sun::star::ldap::LdapGenericException
111                   if an LDAP error occurs.
112     */
113     void getUserProfile(const rtl::OUString& aUser, LdapData * data)
114          throw (lang::IllegalArgumentException,
115                  ldap::LdapConnectionException,
116                  ldap::LdapGenericException);
117 
118     /** finds DN of user
119 		@return  DN of User
120 	*/
121 	rtl::OString findUserDn(const rtl::OString& aUser)
122         throw (lang::IllegalArgumentException,
123                 ldap::LdapConnectionException,
124                 ldap::LdapGenericException);
125 
126     void loadModule();
127 
128     static t_ldap_err2string        s_p_err2string;
129 private:
130 
131 	void initConnection()
132          throw (ldap::LdapConnectionException);
133     void disconnect();
134 	/**
135       Indicates whether the connection is in a valid state.
136 	  @return   sal_True if connection is valid, sal_False otherwise
137       */
138     bool isValid(void) const { return mConnection != NULL ; }
139 
140     void  connectSimple()
141         throw (ldap::LdapConnectionException,
142                 ldap::LdapGenericException);
143 
144     /** LDAP connection object */
145     LDAP* mConnection ;
146 	LdapDefinition mLdapDefinition;
147 
148     static oslModule		        s_Ldap_Module;
149     static t_ldap_value_free        s_p_value_free;
150     static t_ldap_get_values        s_p_get_values;
151     static t_ldap_unbind_s          s_p_unbind_s;
152     static t_ldap_simple_bind_s	    s_p_simple_bind_s;
153     static t_ldap_set_option        s_p_set_option;
154     static t_ldap_init              s_p_init;
155     static t_ldap_msgfree           s_p_msgfree;
156     static t_ldap_get_dn            s_p_get_dn;
157     static t_ldap_first_entry       s_p_first_entry;
158     static t_ldap_first_attribute   s_p_first_attribute;
159     static t_ldap_next_attribute    s_p_next_attribute;
160     static t_ldap_search_s          s_p_search_s;
161 
162     static t_ldap_memfree           s_p_memfree;
163 
164 } ;
165 //------------------------------------------------------------------------------
166 }} }
167 
168 #endif // EXTENSIONS_CONFIG_LDAP_LDAPUSERPROFILE_HXX_
169