Back to index

openldap  2.4.31
proto-sql.h
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1999-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1999 Dmitry Kovalev.
00006  * Portions Copyright 2002 Pierangelo Mararati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Dmitry Kovalev for inclusion
00019  * by OpenLDAP Software.  Additional significant contributors include
00020  * Pierangelo Masarati
00021  */
00022 
00023 /*
00024  * The following changes have been addressed:
00025  *      
00026  * Enhancements:
00027  *   - re-styled code for better readability
00028  *   - upgraded backend API to reflect recent changes
00029  *   - LDAP schema is checked when loading SQL/LDAP mapping
00030  *   - AttributeDescription/ObjectClass pointers used for more efficient
00031  *     mapping lookup
00032  *   - bervals used where string length is required often
00033  *   - atomized write operations by committing at the end of each operation
00034  *     and defaulting connection closure to rollback
00035  *   - added LDAP access control to write operations
00036  *   - fully implemented modrdn (with rdn attrs change, deleteoldrdn,
00037  *     access check, parent/children check and more)
00038  *   - added parent access control, children control to delete operation
00039  *   - added structuralObjectClass operational attribute check and
00040  *     value return on search
00041  *   - added hasSubordinate operational attribute on demand
00042  *   - search limits are appropriately enforced
00043  *   - function backsql_strcat() has been made more efficient
00044  *   - concat function has been made configurable by means of a pattern
00045  *   - added config switches:
00046  *       - fail_if_no_mapping      write operations fail if there is no mapping
00047  *       - has_ldapinfo_dn_ru      overrides autodetect
00048  *       - concat_pattern   a string containing two '?' is used
00049  *                          (note that "?||?" should be more portable
00050  *                          than builtin function "CONCAT(?,?)")
00051  *       - strcast_func            cast of string constants in "SELECT DISTINCT
00052  *                          statements (needed by PostgreSQL)
00053  *       - upper_needs_cast cast the argument of upper when required
00054  *                          (basically when building dn substring queries)
00055  *   - added noop control
00056  *   - added values return filter control
00057  *   - hasSubordinate can be used in search filters (with limitations)
00058  *   - eliminated oc->name; use oc->oc->soc_cname instead
00059  * 
00060  * Todo:
00061  *   - add security checks for SQL statements that can be injected (?)
00062  *   - re-test with previously supported RDBMs
00063  *   - replace dn_ru and so with normalized dn (no need for upper() and so
00064  *     in dn match)
00065  *   - implement a backsql_normalize() function to replace the upper()
00066  *     conversion routines
00067  *   - note that subtree deletion, subtree renaming and so could be easily
00068  *     implemented (rollback and consistency checks are available :)
00069  *   - implement "lastmod" and other operational stuff (ldap_entries table ?)
00070  *   - check how to allow multiple operations with one statement, to remove
00071  *     BACKSQL_REALLOC_STMT from modify.c (a more recent unixODBC lib?)
00072  */
00073 
00074 #ifndef PROTO_SQL_H
00075 #define PROTO_SQL_H
00076 
00077 #include "back-sql.h"
00078 
00079 /*
00080  * add.c
00081  */
00082 int backsql_modify_delete_all_values(
00083        Operation            *op,
00084        SlapReply            *rs,
00085        SQLHDBC                     dbh, 
00086        backsql_entryID             *e_id,
00087        backsql_at_map_rec   *at );
00088 
00089 int backsql_modify_internal(
00090        Operation            *op,
00091        SlapReply            *rs,
00092        SQLHDBC                     dbh, 
00093        backsql_oc_map_rec   *oc,
00094        backsql_entryID             *e_id,
00095        Modifications        *modlist );
00096 
00097 /*
00098  * api.c
00099  */
00100 int backsql_api_config( backsql_info *bi, const char *name,
00101               int argc, char *argv[] );
00102 int backsql_api_destroy( backsql_info *bi );
00103 int backsql_api_register( backsql_api *ba );
00104 int backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn );
00105 int backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn );
00106 
00107 /*
00108  * entry-id.c
00109  */
00110 #ifdef BACKSQL_ARBITRARY_KEY
00111 extern struct berval backsql_baseObject_bv;
00112 #endif /* BACKSQL_ARBITRARY_KEY */
00113 
00114 /* stores in *id the ID in table ldap_entries corresponding to DN, if any */
00115 extern int
00116 backsql_dn2id( Operation *op, SlapReply *rs, SQLHDBC dbh,
00117               struct berval *ndn, backsql_entryID *id,
00118               int matched, int muck );
00119 
00120 /* stores in *nchildren the count of children for an entry */
00121 extern int
00122 backsql_count_children( Operation *op, SQLHDBC dbh,
00123               struct berval *dn, unsigned long *nchildren );
00124 
00125 /* returns LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE if the entry corresponding
00126  * to DN has/has not children */
00127 extern int
00128 backsql_has_children( Operation *op, SQLHDBC dbh, struct berval *dn );
00129 
00130 /* free *id and return next in list */
00131 extern backsql_entryID *
00132 backsql_free_entryID( backsql_entryID *id, int freeit, void *ctx );
00133 
00134 /* turn an ID into an entry */
00135 extern int
00136 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );
00137 
00138 /* duplicate an entryID */
00139 extern backsql_entryID *
00140 backsql_entryID_dup( backsql_entryID *eid, void *ctx );
00141 
00142 /*
00143  * operational.c
00144  */
00145 
00146 Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );
00147 
00148 Attribute *backsql_operational_entryCSN( Operation *op );
00149 
00150 /*
00151  * schema-map.c
00152  */
00153 
00154 int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );
00155 
00156 backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );
00157 
00158 backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );
00159 
00160 backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
00161               struct berval *oc_name );
00162 
00163 backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
00164               AttributeDescription *ad );
00165 
00166 int backsql_supad2at( backsql_oc_map_rec *objclass,
00167               AttributeDescription *supad, backsql_at_map_rec ***pret );
00168 
00169 int backsql_destroy_schema_map( backsql_info *si );
00170 
00171 /*
00172  * search.c
00173  */
00174 
00175 int backsql_init_search( backsql_srch_info *bsi, 
00176               struct berval *nbase, int scope,
00177               time_t stoptime, Filter *filter, SQLHDBC dbh,
00178               Operation *op, SlapReply *rs, AttributeName *attrs,
00179               unsigned flags );
00180 
00181 void backsql_entry_clean( Operation *op, Entry *e );
00182 
00183 /*
00184  * sql-wrap.h
00185  */
00186 
00187 RETCODE backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, const char* query, int timeout );
00188 
00189 #define backsql_BindParamStr( sth, par_ind, io, str, maxlen )         \
00190        SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),              \
00191                      (io), SQL_C_CHAR, SQL_VARCHAR,                   \
00192                      (SQLULEN)(maxlen), 0, (SQLPOINTER)(str),  \
00193                      (SQLLEN)(maxlen), NULL )
00194 
00195 #define backsql_BindParamBerVal( sth, par_ind, io, bv )               \
00196        SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),              \
00197                      (io), SQL_C_CHAR, SQL_VARCHAR,                   \
00198                      (SQLULEN)(bv)->bv_len, 0,                 \
00199                      (SQLPOINTER)(bv)->bv_val,                 \
00200                      (SQLLEN)(bv)->bv_len, NULL )
00201 
00202 #define backsql_BindParamInt( sth, par_ind, io, val )                 \
00203        SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),              \
00204                      (io), SQL_C_ULONG, SQL_INTEGER,                  \
00205                      0, 0, (SQLPOINTER)(val), 0, (SQLLEN*)NULL )
00206 
00207 #define backsql_BindParamNumID( sth, par_ind, io, val )               \
00208        SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),              \
00209                      (io), BACKSQL_C_NUMID, SQL_INTEGER,              \
00210                      0, 0, (SQLPOINTER)(val), 0, (SQLLEN*)NULL )
00211 
00212 #ifdef BACKSQL_ARBITRARY_KEY
00213 #define backsql_BindParamID( sth, par_ind, io, id )                   \
00214        backsql_BindParamBerVal( (sth), (par_ind), (io), (id) )
00215 #else /* ! BACKSQL_ARBITRARY_KEY */
00216 #define backsql_BindParamID( sth, par_ind, io, id )                   \
00217        backsql_BindParamNumID( (sth), (par_ind), (io), (id) )
00218 #endif /* ! BACKSQL_ARBITRARY_KEY */
00219 
00220 RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );
00221 
00222 RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
00223 
00224 RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );
00225 
00226 RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
00227 
00228 void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
00229 
00230 int backsql_conn_destroy( backsql_info *bi );
00231 
00232 int backsql_init_db_env( backsql_info *si );
00233 
00234 int backsql_free_db_env( backsql_info *si );
00235 
00236 int backsql_get_db_conn( Operation *op, SQLHDBC  *dbh );
00237 
00238 int backsql_free_db_conn( Operation *op, SQLHDBC dbh );
00239 
00240 /*
00241  * util.c
00242  */
00243 
00244 extern const char 
00245        backsql_def_oc_query[],
00246        backsql_def_needs_select_oc_query[],
00247        backsql_def_at_query[],
00248        backsql_def_delentry_stmt[],
00249        backsql_def_renentry_stmt[],
00250        backsql_def_insentry_stmt[],
00251        backsql_def_delobjclasses_stmt[],
00252        backsql_def_subtree_cond[],
00253        backsql_def_upper_subtree_cond[],
00254        backsql_id_query[],
00255        backsql_def_concat_func[],
00256        backsql_check_dn_ru_query[];
00257 
00258 struct berbuf * backsql_strcat_x( struct berbuf *dest, void *memctx, ... );
00259 struct berbuf * backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... );
00260 
00261 int backsql_entry_addattr( Entry *e, AttributeDescription *ad, 
00262               struct berval *at_val, void *memctx );
00263 
00264 int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from, 
00265               struct berval *src_from );
00266 
00267 int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
00268               int expected );
00269 
00270 int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
00271               struct berval *res );
00272 
00273 int backsql_entryUUID( backsql_info *bi, backsql_entryID *id,
00274               struct berval *entryUUID, void *memctx );
00275 int backsql_entryUUID_decode( struct berval *entryUUID, unsigned long *oc_id,
00276 #ifdef BACKSQL_ARBITRARY_KEY
00277        struct berval *keyval
00278 #else /* ! BACKSQL_ARBITRARY_KEY */
00279        unsigned long *keyval
00280 #endif /* ! BACKSQL_ARBITRARY_KEY */
00281        );
00282 
00283 /*
00284  * former external.h
00285  */
00286 
00287 extern BI_init                     sql_back_initialize;
00288 
00289 extern BI_destroy           backsql_destroy;
00290 
00291 extern BI_db_init           backsql_db_init;
00292 extern BI_db_open           backsql_db_open;
00293 extern BI_db_close          backsql_db_close;
00294 extern BI_db_destroy        backsql_db_destroy;
00295 extern BI_db_config         backsql_db_config;
00296 
00297 extern BI_op_bind           backsql_bind;
00298 extern BI_op_search         backsql_search;
00299 extern BI_op_compare        backsql_compare;
00300 extern BI_op_modify         backsql_modify;
00301 extern BI_op_modrdn         backsql_modrdn;
00302 extern BI_op_add            backsql_add;
00303 extern BI_op_delete         backsql_delete;
00304 
00305 extern BI_operational              backsql_operational;
00306 extern BI_entry_get_rw             backsql_entry_get;
00307 extern BI_entry_release_rw  backsql_entry_release;
00308 
00309 extern BI_connection_destroy       backsql_connection_destroy;
00310 
00311 int backsql_init_cf( BackendInfo * bi );
00312 
00313 #endif /* PROTO_SQL_H */