github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/data/apache_2_4_58/include/util_ldap.h (about)

     1  /* Licensed to the Apache Software Foundation (ASF) under one or more
     2   * contributor license agreements.  See the NOTICE file distributed with
     3   * this work for additional information regarding copyright ownership.
     4   * The ASF licenses this file to You under the Apache License, Version 2.0
     5   * (the "License"); you may not use this file except in compliance with
     6   * the License.  You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  /**
    18   * @file util_ldap.h
    19   * @brief Apache LDAP library
    20   */
    21  
    22  #ifndef UTIL_LDAP_H
    23  #define UTIL_LDAP_H
    24  
    25  /* APR header files */
    26  #include "apr.h"
    27  #include "apr_thread_mutex.h"
    28  #include "apr_thread_rwlock.h"
    29  #include "apr_tables.h"
    30  #include "apr_time.h"
    31  #include "apr_version.h"
    32  #if APR_MAJOR_VERSION < 2
    33  /* The LDAP API is currently only present in APR 1.x */
    34  #include "apr_ldap.h"
    35  #else
    36  #define APR_HAS_LDAP 0
    37  #endif
    38  
    39  #if APR_HAS_SHARED_MEMORY
    40  #include "apr_rmm.h"
    41  #include "apr_shm.h"
    42  #endif
    43  
    44  /* this whole thing disappears if LDAP is not enabled */
    45  #if APR_HAS_LDAP
    46  
    47  #if defined(LDAP_UNAVAILABLE) || APR_HAS_MICROSOFT_LDAPSDK
    48  #define AP_LDAP_IS_SERVER_DOWN(s)                ((s) == LDAP_SERVER_DOWN \
    49                  ||(s) == LDAP_UNAVAILABLE)
    50  #else
    51  #define AP_LDAP_IS_SERVER_DOWN(s)                ((s) == LDAP_SERVER_DOWN)
    52  #endif
    53  
    54  /* Apache header files */
    55  #include "ap_config.h"
    56  #include "httpd.h"
    57  #include "http_config.h"
    58  #include "http_core.h"
    59  #include "http_log.h"
    60  #include "http_protocol.h"
    61  #include "http_request.h"
    62  #include "apr_optional.h"
    63  
    64  /* Create a set of LDAP_DECLARE macros with appropriate export
    65   * and import tags for the platform
    66   */
    67  #if !defined(WIN32)
    68  #define LDAP_DECLARE(type)            type
    69  #define LDAP_DECLARE_NONSTD(type)     type
    70  #define LDAP_DECLARE_DATA
    71  #elif defined(LDAP_DECLARE_STATIC)
    72  #define LDAP_DECLARE(type)            type __stdcall
    73  #define LDAP_DECLARE_NONSTD(type)     type
    74  #define LDAP_DECLARE_DATA
    75  #elif defined(LDAP_DECLARE_EXPORT)
    76  #define LDAP_DECLARE(type)            __declspec(dllexport) type __stdcall
    77  #define LDAP_DECLARE_NONSTD(type)     __declspec(dllexport) type
    78  #define LDAP_DECLARE_DATA             __declspec(dllexport)
    79  #else
    80  #define LDAP_DECLARE(type)            __declspec(dllimport) type __stdcall
    81  #define LDAP_DECLARE_NONSTD(type)     __declspec(dllimport) type
    82  #define LDAP_DECLARE_DATA             __declspec(dllimport)
    83  #endif
    84  
    85  #if APR_HAS_MICROSOFT_LDAPSDK
    86  #define timeval l_timeval
    87  #endif
    88  
    89  #ifdef __cplusplus
    90  extern "C" {
    91  #endif
    92  
    93  /*
    94   * LDAP Connections
    95   */
    96  
    97  /* Values that the deref member can have */
    98  typedef enum {
    99      never=LDAP_DEREF_NEVER,
   100      searching=LDAP_DEREF_SEARCHING,
   101      finding=LDAP_DEREF_FINDING,
   102      always=LDAP_DEREF_ALWAYS
   103  } deref_options;
   104  
   105  /* Structure representing an LDAP connection */
   106  typedef struct util_ldap_connection_t {
   107      LDAP *ldap;
   108      apr_pool_t *pool;                   /* Pool from which this connection is created */
   109  #if APR_HAS_THREADS
   110      apr_thread_mutex_t *lock;           /* Lock to indicate this connection is in use */
   111  #endif
   112  
   113      const char *host;                   /* Name of the LDAP server (or space separated list) */
   114      int port;                           /* Port of the LDAP server */
   115      deref_options deref;                /* how to handle alias dereferening */
   116  
   117      const char *binddn;                 /* DN to bind to server (can be NULL) */
   118      const char *bindpw;                 /* Password to bind to server (can be NULL) */
   119  
   120      int bound;                          /* Flag to indicate whether this connection is bound yet */
   121  
   122      int secure;                         /* SSL/TLS mode of the connection */
   123      apr_array_header_t *client_certs;   /* Client certificates on this connection */
   124  
   125      const char *reason;                 /* Reason for an error failure */
   126  
   127      struct util_ldap_connection_t *next;
   128      struct util_ldap_state_t *st;        /* The LDAP vhost config this connection belongs to */
   129      int keep;                            /* Will this connection be kept when it's unlocked */
   130  
   131      int ChaseReferrals;                 /* [on|off] (default = AP_LDAP_CHASEREFERRALS_ON)*/
   132      int ReferralHopLimit;               /* # of referral hops to follow (default = AP_LDAP_DEFAULT_HOPLIMIT) */
   133      apr_time_t freed;                   /* the time this conn was placed back in the pool */
   134      apr_pool_t *rebind_pool;            /* frequently cleared pool for rebind data */
   135      int must_rebind;                    /* The connection was last bound with other then binddn/bindpw */
   136      request_rec *r;                     /* request_rec used to find this util_ldap_connection_t */
   137      apr_time_t last_backend_conn;       /* the approximate time of the last backend LDAP request */
   138  } util_ldap_connection_t;
   139  
   140  typedef struct util_ldap_config_t {
   141      int ChaseReferrals;
   142      int ReferralHopLimit;
   143      apr_array_header_t *client_certs;  /* Client certificates */
   144  } util_ldap_config_t;
   145  
   146  /* LDAP cache state information */
   147  typedef struct util_ldap_state_t {
   148      apr_pool_t *pool;           /* pool from which this state is allocated */
   149  #if APR_HAS_THREADS
   150      apr_thread_mutex_t *mutex;          /* mutex lock for the connection list */
   151  #endif
   152      apr_global_mutex_t *util_ldap_cache_lock;
   153  
   154      apr_size_t cache_bytes;     /* Size (in bytes) of shared memory cache */
   155      char *cache_file;           /* filename for shm */
   156      long search_cache_ttl;      /* TTL for search cache */
   157      long search_cache_size;     /* Size (in entries) of search cache */
   158      long compare_cache_ttl;     /* TTL for compare cache */
   159      long compare_cache_size;    /* Size (in entries) of compare cache */
   160  
   161      struct util_ldap_connection_t *connections;
   162      apr_array_header_t *global_certs;  /* Global CA certificates */
   163      int   ssl_supported;
   164      int   secure;
   165      int   secure_set;
   166      int   verify_svr_cert;
   167  
   168  #if APR_HAS_SHARED_MEMORY
   169      apr_shm_t *cache_shm;
   170      apr_rmm_t *cache_rmm;
   171  #endif
   172  
   173      /* cache ald */
   174      void *util_ldap_cache;
   175  
   176      long  connectionTimeout;
   177      struct timeval *opTimeout;
   178  
   179      int debug_level;                    /* SDK debug level */
   180      apr_interval_time_t connection_pool_ttl;
   181      int retries;                        /* number of retries for failed bind/search/compare */
   182      apr_interval_time_t retry_delay;    /* delay between retries of failed bind/search/compare */
   183  } util_ldap_state_t;
   184  
   185  /* Used to store arrays of attribute labels/values. */
   186  struct mod_auth_ldap_groupattr_entry_t {
   187      char *name;
   188  };
   189  
   190  /**
   191   * Open a connection to an LDAP server
   192   * @param ldc A structure containing the expanded details of the server
   193   *            to connect to. The handle to the LDAP connection is returned
   194   *            as ldc->ldap.
   195   * @tip This function connects to the LDAP server and binds. It does not
   196   *      connect if already connected (ldc->ldap != NULL). Does not bind
   197   *      if already bound.
   198   * @return If successful LDAP_SUCCESS is returned.
   199   * @fn int util_ldap_connection_open(request_rec *r,
   200   *                                        util_ldap_connection_t *ldc)
   201   */
   202  APR_DECLARE_OPTIONAL_FN(int,uldap_connection_open,(request_rec *r,
   203                                              util_ldap_connection_t *ldc));
   204  
   205  /**
   206   * Close a connection to an LDAP server
   207   * @param ldc A structure containing the expanded details of the server
   208   *            that was connected.
   209   * @tip This function unbinds from the LDAP server, and clears ldc->ldap.
   210   *      It is possible to rebind to this server again using the same ldc
   211   *      structure, using apr_ldap_open_connection().
   212   * @fn util_ldap_close_connection(util_ldap_connection_t *ldc)
   213   */
   214  APR_DECLARE_OPTIONAL_FN(void,uldap_connection_close,(util_ldap_connection_t *ldc));
   215  
   216  /**
   217   * Unbind a connection to an LDAP server
   218   * @param ldc A structure containing the expanded details of the server
   219   *            that was connected.
   220   * @tip This function unbinds the LDAP connection, and disconnects from
   221   *      the server. It is used during error conditions, to bring the LDAP
   222   *      connection back to a known state.
   223   * @fn apr_status_t util_ldap_connection_unbind(util_ldap_connection_t *ldc)
   224   */
   225  APR_DECLARE_OPTIONAL_FN(apr_status_t,uldap_connection_unbind,(void *param));
   226  
   227  /**
   228   * Find a connection in a list of connections
   229   * @param r The request record
   230   * @param host The hostname to connect to (multiple hosts space separated)
   231   * @param port The port to connect to
   232   * @param binddn The DN to bind with
   233   * @param bindpw The password to bind with
   234   * @param deref The dereferencing behavior
   235   * @param secure use SSL on the connection
   236   * @tip Once a connection is found and returned, a lock will be acquired to
   237   *      lock that particular connection, so that another thread does not try and
   238   *      use this connection while it is busy. Once you are finished with a connection,
   239   *      apr_ldap_connection_close() must be called to release this connection.
   240   * @fn util_ldap_connection_t *util_ldap_connection_find(request_rec *r, const char *host, int port,
   241   *                                                           const char *binddn, const char *bindpw, deref_options deref,
   242   *                                                           int netscapessl, int starttls)
   243   */
   244  APR_DECLARE_OPTIONAL_FN(util_ldap_connection_t *,uldap_connection_find,(request_rec *r, const char *host, int port,
   245                                                    const char *binddn, const char *bindpw, deref_options deref,
   246                                                    int secure));
   247  
   248  /**
   249   * Compare two DNs for sameness
   250   * @param r The request record
   251   * @param ldc The LDAP connection being used.
   252   * @param url The URL of the LDAP connection - used for deciding which cache to use.
   253   * @param dn The first DN to compare.
   254   * @param reqdn The DN to compare the first DN to.
   255   * @param compare_dn_on_server Flag to determine whether the DNs should be checked using
   256   *                             LDAP calls or with a direct string comparison. A direct
   257   *                             string comparison is faster, but not as accurate - false
   258   *                             negative comparisons are possible.
   259   * @tip Two DNs can be equal and still fail a string comparison. Eg "dc=example,dc=com"
   260   *      and "dc=example, dc=com". Use the compare_dn_on_server unless there are serious
   261   *      performance issues.
   262   * @fn int util_ldap_cache_comparedn(request_rec *r, util_ldap_connection_t *ldc,
   263   *                                        const char *url, const char *dn, const char *reqdn,
   264   *                                        int compare_dn_on_server)
   265   */
   266  APR_DECLARE_OPTIONAL_FN(int,uldap_cache_comparedn,(request_rec *r, util_ldap_connection_t *ldc,
   267                                const char *url, const char *dn, const char *reqdn,
   268                                int compare_dn_on_server));
   269  
   270  /**
   271   * A generic LDAP compare function
   272   * @param r The request record
   273   * @param ldc The LDAP connection being used.
   274   * @param url The URL of the LDAP connection - used for deciding which cache to use.
   275   * @param dn The DN of the object in which we do the compare.
   276   * @param attrib The attribute within the object we are comparing for.
   277   * @param value The value of the attribute we are trying to compare for.
   278   * @tip Use this function to determine whether an attribute/value pair exists within an
   279   *      object. Typically this would be used to determine LDAP top-level group
   280   *      membership.
   281   * @fn int util_ldap_cache_compare(request_rec *r, util_ldap_connection_t *ldc,
   282   *                                      const char *url, const char *dn, const char *attrib, const char *value)
   283   */
   284  APR_DECLARE_OPTIONAL_FN(int,uldap_cache_compare,(request_rec *r, util_ldap_connection_t *ldc,
   285                              const char *url, const char *dn, const char *attrib, const char *value));
   286  
   287  /**
   288   * An LDAP function that checks if the specified user is a member of a subgroup.
   289   * @param r The request record
   290   * @param ldc The LDAP connection being used.
   291   * @param url The URL of the LDAP connection - used for deciding which cache to use.
   292   * @param dn The DN of the object in which we find subgroups to search within.
   293   * @param attrib The attribute within group objects that identify users.
   294   * @param value The user attribute value we are trying to compare for.
   295   * @param subgroupAttrs The attributes within group objects that identify subgroups.
   296   *                      Array of strings.
   297   * @param subgroupclasses The objectClass values used to identify groups (and
   298   *                      subgroups). apr_array_header_t *.
   299   * @param cur_subgroup_depth Current recursive depth during subgroup processing.
   300   * @param max_subgroup_depth Maximum depth of recursion allowed during subgroup
   301   *                           processing.
   302   * @tip Use this function to determine whether an attribute/value pair exists within a
   303   *      starting group object or one of its nested subgroups. Typically this would be
   304   *      used to determine LDAP nested group membership.
   305   * @deffunc int util_ldap_cache_check_subgroups(request_rec *r, util_ldap_connection_t
   306   *                                      *ldc, const char *url, const char *dn,
   307   *                                      const char *attrib, const char value,
   308   *                                      char **subgroupAttrs, apr_array_header_t
   309   *                                      *subgroupclasses, int cur_subgroup_depth, int
   310   *                                      max_subgroup_depth )
   311   */
   312  APR_DECLARE_OPTIONAL_FN(int,uldap_cache_check_subgroups,(request_rec *r, util_ldap_connection_t *ldc,
   313                                         const char *url, const char *dn, const char *attrib, const char *value,
   314                                         char **subgroupAttrs, apr_array_header_t *subgroupclasses,
   315                                         int cur_subgroup_depth, int max_subgroup_depth));
   316  
   317  /**
   318   * Checks a username/password combination by binding to the LDAP server
   319   * @param r The request record
   320   * @param ldc The LDAP connection being used.
   321   * @param url The URL of the LDAP connection - used for deciding which cache to use.
   322   * @param basedn The Base DN to search for the user in.
   323   * @param scope LDAP scope of the search.
   324   * @param attrs LDAP attributes to return in search.
   325   * @param filter The user to search for in the form of an LDAP filter. This filter must return
   326   *               exactly one user for the check to be successful.
   327   * @param bindpw The user password to bind as.
   328   * @param binddn The DN of the user will be returned in this variable.
   329   * @param retvals The values corresponding to the attributes requested in the attrs array.
   330   * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
   331   *      is made to bind as that user. If this bind succeeds, the user is not validated.
   332   * @fn int util_ldap_cache_checkuserid(request_rec *r, util_ldap_connection_t *ldc,
   333   *                                          char *url, const char *basedn, int scope, char **attrs,
   334   *                                          char *filter, char *bindpw, char **binddn, char ***retvals)
   335   */
   336  APR_DECLARE_OPTIONAL_FN(int,uldap_cache_checkuserid,(request_rec *r, util_ldap_connection_t *ldc,
   337                                const char *url, const char *basedn, int scope, char **attrs,
   338                                const char *filter, const char *bindpw, const char **binddn, const char ***retvals));
   339  
   340  /**
   341   * Searches for a specified user object in an LDAP directory
   342   * @param r The request record
   343   * @param ldc The LDAP connection being used.
   344   * @param url The URL of the LDAP connection - used for deciding which cache to use.
   345   * @param basedn The Base DN to search for the user in.
   346   * @param scope LDAP scope of the search.
   347   * @param attrs LDAP attributes to return in search.
   348   * @param filter The user to search for in the form of an LDAP filter. This filter must return
   349   *               exactly one user for the check to be successful.
   350   * @param binddn The DN of the user will be returned in this variable.
   351   * @param retvals The values corresponding to the attributes requested in the attrs array.
   352   * @tip The filter supplied will be searched for. If a single entry is returned, an attempt
   353   *      is made to bind as that user. If this bind succeeds, the user is not validated.
   354   * @fn int util_ldap_cache_getuserdn(request_rec *r, util_ldap_connection_t *ldc,
   355   *                                          char *url, const char *basedn, int scope, char **attrs,
   356   *                                          char *filter, char **binddn, char ***retvals)
   357   */
   358  APR_DECLARE_OPTIONAL_FN(int,uldap_cache_getuserdn,(request_rec *r, util_ldap_connection_t *ldc,
   359                                const char *url, const char *basedn, int scope, char **attrs,
   360                                const char *filter, const char **binddn, const char ***retvals));
   361  
   362  /**
   363   * Checks if SSL support is available in mod_ldap
   364   * @fn int util_ldap_ssl_supported(request_rec *r)
   365   */
   366  APR_DECLARE_OPTIONAL_FN(int,uldap_ssl_supported,(request_rec *r));
   367  
   368  /* from apr_ldap_cache.c */
   369  
   370  /**
   371   * Init the LDAP cache
   372   * @param pool The pool to use to initialise the cache
   373   * @param reqsize The size of the shared memory segment to request. A size
   374   *                of zero requests the max size possible from
   375   *                apr_shmem_init()
   376   * @fn void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
   377   * @return The status code returned is the status code of the
   378   *         apr_smmem_init() call. Regardless of the status, the cache
   379   *         will be set up at least for in-process or in-thread operation.
   380   */
   381  apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
   382  
   383  /* from apr_ldap_cache_mgr.c */
   384  
   385  /**
   386   * Display formatted stats for cache
   387   * @param The pool to allocate the returned string from
   388   * @tip This function returns a string allocated from the provided pool that describes
   389   *      various stats about the cache.
   390   * @fn char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
   391   */
   392  char *util_ald_cache_display(request_rec *r, util_ldap_state_t *st);
   393  #ifdef __cplusplus
   394  }
   395  #endif
   396  #endif /* APR_HAS_LDAP */
   397  #endif /* UTIL_LDAP_H */