2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include <sys/types.h>
20 #include "schema-map.h"
24 int backsql_attrlist_add(backsql_srch_info *bsi,char *at_name)
34 Debug(LDAP_DEBUG_TRACE,"==>backsql_attrlist_add(): attribute '%s' is in list\n",*p,0,0);
35 if (!strcasecmp(*p,at_name))
40 Debug(LDAP_DEBUG_TRACE,"==>backsql_attrlist_add(): adding '%s' to list\n",at_name,0,0);
41 bsi->attrs=(char**)ch_realloc(bsi->attrs,(n_attrs+2)*sizeof(char*));
42 bsi->attrs[n_attrs]=ch_strdup(at_name);
43 bsi->attrs[n_attrs+1]=NULL;
47 void backsql_init_search(backsql_srch_info *bsi,backsql_info *bi,char *nbase,int scope,
48 int slimit,int tlimit,time_t stoptime,Filter *filter,
49 SQLHDBC dbh,BackendDB *be,Connection *conn,Operation *op,char **attrs)
63 bsi->attrs=(char**)ch_calloc(1,sizeof(char*));
65 for(p=attrs;*p!=NULL;p++)
66 backsql_attrlist_add(bsi,*p);
72 bsi->stoptime=stoptime;
74 bsi->sel=NULL; bsi->from=NULL; bsi->join_where=NULL; bsi->flt_where=NULL;
75 bsi->sel_len=0; bsi->from_len=0; bsi->jwhere_len=0; bsi->fwhere_len=0;
78 int backsql_process_filter_list(backsql_srch_info *bsi,Filter *f,int op)
80 char *sub_clause=NULL;
85 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"(",NULL);
88 res=backsql_process_filter(bsi,f);
90 return -1; /* TimesTen : If the query has no answers,
91 don't bother to run the query. */
100 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len," AND ",NULL);
103 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len," OR ",NULL);
109 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,")",NULL);
113 int backsql_process_sub_filter(backsql_srch_info *bsi,Filter *f)
116 backsql_at_map_rec *at=backsql_at_with_name(bsi->oc,f->f_sub_desc->ad_cname.bv_val);
121 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"(",NULL);
123 Debug(LDAP_DEBUG_TRACE,"expr: '%s' '%s'\n",at->sel_expr,
124 at->sel_expr_u?at->sel_expr_u:"<NULL>",0);
125 if (bsi->bi->upper_func)
127 /* If a pre-upper-cased version of the column exists, use it. */
128 if (at->sel_expr_u) {
129 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,
130 at->sel_expr_u," LIKE '",NULL);
133 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,
134 bsi->bi->upper_func,"(",at->sel_expr,")",
140 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,at->sel_expr,
143 if (f->f_sub_initial!=NULL)
145 if (bsi->bi->upper_func)
147 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,ldap_pvt_str2upper(f->f_sub_initial->bv_val),NULL);
150 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,f->f_sub_initial->bv_val,NULL);
153 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"%",NULL);
155 if (f->f_sub_any!=NULL)
156 for(i=0;f->f_sub_any[i]!=NULL;i++)
158 /*Debug(LDAP_DEBUG_TRACE,"==>backsql_process_sub_filter(): sub_any='%s'\n",f->f_sub_any[i]->bv_val,0,0);*/
159 if (bsi->bi->upper_func)
161 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,ldap_pvt_str2upper(f->f_sub_any[i]->bv_val),"%",NULL);
164 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,f->f_sub_any[i]->bv_val,"%",NULL);
167 if (f->f_sub_final!=NULL)
168 if (bsi->bi->upper_func)
170 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,ldap_pvt_str2upper(f->f_sub_final->bv_val),NULL);
173 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,f->f_sub_final->bv_val,NULL);
175 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"')",NULL);
180 int backsql_process_filter(backsql_srch_info *bsi,Filter *f)
182 backsql_at_map_rec *at;
183 backsql_at_map_rec oc_attr={"objectClass","","",NULL,NULL,NULL,NULL};
186 int rc=0; /* TimesTen */
188 Debug(LDAP_DEBUG_TRACE,"==>backsql_process_filter()\n",0,0,0);
189 if (f==NULL || f->f_choice==SLAPD_FILTER_COMPUTED)
197 rc = backsql_process_filter_list(bsi,f->f_or,LDAP_FILTER_OR);
200 case LDAP_FILTER_AND:
201 rc = backsql_process_filter_list(bsi,f->f_and,LDAP_FILTER_AND);
204 case LDAP_FILTER_NOT:
205 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"NOT (",NULL);
206 rc = backsql_process_filter(bsi,f->f_not);
207 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,")",NULL);
210 case LDAP_FILTER_PRESENT:
211 at_name=f->f_desc->ad_cname.bv_val;
214 at_name=f->f_av_desc->ad_cname.bv_val;
219 goto impossible; /* TimesTen : Don't run the query */
224 if (strcasecmp(at_name,"objectclass"))
225 at=backsql_at_with_name(bsi->oc,at_name);
229 at->sel_expr=backsql_strcat(at->sel_expr,&len,"'",bsi->oc->name,"'",NULL);
233 Debug(LDAP_DEBUG_TRACE,"backsql_process_filter(): attribute '%s' is not defined for objectclass '%s'\n",
234 at_name,bsi->oc->name,0);
235 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len," 1=0 ",NULL);
239 backsql_merge_from_clause(&bsi->from,&bsi->from_len,at->from_tbls);
240 /*need to add this attribute to list of attrs to load, so that we could do test_filter() later*/
241 backsql_attrlist_add(bsi,at_name);
243 if (at->join_where != NULL && strstr(bsi->join_where,at->join_where)==NULL)
244 bsi->join_where=backsql_strcat(bsi->join_where,&bsi->jwhere_len," AND ",at->join_where,NULL);
247 bsi->sel=backsql_strcat(bsi->sel,&bsi->sel_len,",",at->sel_expr," AS ",at->name,NULL);
252 case LDAP_FILTER_EQUALITY:
253 /*maybe we should check type of at->sel_expr here somehow,
254 * to know whether upper_func is applicable, but for now
255 * upper_func stuff is made for Oracle, where UPPER is
256 * safely applicable to NUMBER etc.
258 if (bsi->bi->upper_func) {
260 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"(",
262 ldap_pvt_str2upper(f->f_av_value->bv_val),"')",
265 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"(",
266 bsi->bi->upper_func,"(",at->sel_expr,")='",
267 ldap_pvt_str2upper(f->f_av_value->bv_val),"')",NULL);
270 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"(",at->sel_expr,"='",
271 f->f_av_value->bv_val,"')",NULL);
275 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"(",at->sel_expr,">=",
276 f->f_av_value->bv_val,")",NULL);
280 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"(",at->sel_expr,"<=",
281 f->f_av_value->bv_val,")",NULL);
283 case LDAP_FILTER_PRESENT:
284 bsi->flt_where=backsql_strcat(bsi->flt_where,&bsi->fwhere_len,"NOT (",at->sel_expr,
287 case LDAP_FILTER_SUBSTRINGS:
288 backsql_process_sub_filter(bsi,f);
293 if (oc_attr.sel_expr!=NULL)
294 free(oc_attr.sel_expr);
295 Debug(LDAP_DEBUG_TRACE,"<==backsql_process_filter()\n",0,0,0);
299 if (oc_attr.sel_expr!=NULL)
300 free(oc_attr.sel_expr);
301 Debug(LDAP_DEBUG_TRACE,"<==backsql_process_filter() returns -1\n",0,0,0);
306 char* backsql_srch_query(backsql_srch_info *bsi)
312 Debug(LDAP_DEBUG_TRACE,"==>backsql_srch_query()\n",0,0,0);
315 bsi->join_where=NULL;
317 bsi->sel_len=bsi->from_len=bsi->jwhere_len=bsi->fwhere_len=0;
319 bsi->sel=backsql_strcat(bsi->sel,&bsi->sel_len,
320 "SELECT DISTINCT ldap_entries.id,",bsi->oc->keytbl,".",bsi->oc->keycol,
321 ", '",bsi->oc->name,"' AS objectClass",
322 ", ldap_entries.dn AS dn",
324 bsi->from=backsql_strcat(bsi->from,&bsi->from_len," FROM ldap_entries,",bsi->oc->keytbl,NULL);
325 bsi->join_where=backsql_strcat(bsi->join_where,&bsi->jwhere_len," WHERE ",
326 bsi->oc->keytbl,".",bsi->oc->keycol,"=ldap_entries.keyval AND ",
327 "ldap_entries.oc_map_id=? AND ",NULL);
331 case LDAP_SCOPE_BASE:
332 if (bsi->bi->upper_func)
334 bsi->join_where=backsql_strcat(bsi->join_where,&bsi->jwhere_len,
335 bsi->bi->upper_func,"(","ldap_entries.dn)=(?)",NULL);
339 bsi->join_where=backsql_strcat(bsi->join_where,&bsi->jwhere_len,
340 "ldap_entries.dn=?",NULL);
343 case LDAP_SCOPE_ONELEVEL:
344 bsi->join_where=backsql_strcat(bsi->join_where,&bsi->jwhere_len,
345 "ldap_entries.parent=?",NULL);
347 case LDAP_SCOPE_SUBTREE:
348 bsi->join_where=backsql_strcat(bsi->join_where,&bsi->jwhere_len,
349 bsi->bi->subtree_cond,NULL);
353 rc = backsql_process_filter(bsi, bsi->filter);
355 query=backsql_strcat(query,&q_len,bsi->sel,bsi->from,bsi->join_where," AND ",bsi->flt_where,NULL);
358 /* Indicates that there's no possible way the filter matches
359 anything. No need to issue the query. */
361 Debug(LDAP_DEBUG_TRACE,"<==backsql_srch_query() returns NULL\n",0,0,0);
368 free(bsi->join_where);
369 free(bsi->flt_where);
370 bsi->sel_len=bsi->from_len=bsi->jwhere_len=bsi->fwhere_len=0;
371 Debug(LDAP_DEBUG_TRACE,"<==backsql_srch_query()\n",0,0,0);
375 int backsql_oc_get_candidates(backsql_oc_map_rec *oc,backsql_srch_info *bsi)
380 backsql_entryID base_id,*res,*c_id;
385 char temp_base_dn[BACKSQL_MAX_DN_LEN+1]; /* TimesTen*/
387 Debug(LDAP_DEBUG_TRACE,"==>backsql_oc_get_candidates(): oc='%s'\n",oc->name,0,0);
389 query=backsql_srch_query(bsi);
392 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): could not construct query for objectclass\n",0,0,0);
396 Debug(LDAP_DEBUG_TRACE,"Constructed query: %s\n",query,0,0);
398 if ((rc=backsql_Prepare(bsi->dbh,&sth,query,0)) != SQL_SUCCESS)
400 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): error preparing query\n",0,0,0);
401 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
407 if (backsql_BindParamID(sth,1,&bsi->oc->id) != SQL_SUCCESS)
409 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): error binding objectclass id parameter\n",0,0,0);
414 case LDAP_SCOPE_BASE:
415 if ((rc=backsql_BindParamStr(sth,2,bsi->base_dn,BACKSQL_MAX_DN_LEN)) != SQL_SUCCESS)
417 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): error binding base_dn parameter\n",0,0,0);
418 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
423 case LDAP_SCOPE_SUBTREE:
424 /* Sets the parameters for the SQL built earlier */
425 /* NOTE that all the databases could actually use the TimesTen version,
426 which would be cleaner and would also eliminate the need for the
427 subtree_cond line in the configuration file. For now, I'm leaving
428 it the way it is, so non-TimesTen databases use the original code.
429 But at some point this should get cleaned up. */
430 /* If "dn" is being used, do a suffix search.
431 If "dn_ru" is being used, do a prefix search. */
433 if (bsi->bi->has_ldapinfo_dn_ru) {
434 temp_base_dn[0] = '\0';
435 for ((i=0, j=strlen(bsi->base_dn)-1); j >= 0; (i++, j--)) {
436 *(temp_base_dn+i) = toupper(*(bsi->base_dn+j));
438 *(temp_base_dn+i) = '%';
439 *(temp_base_dn+i+1) = '\0';
442 strcpy(temp_base_dn, "%");
443 for (i = 0; *(bsi->base_dn+i); i++) {
444 *(temp_base_dn+i+1) = toupper(*(bsi->base_dn+i));
446 *(temp_base_dn+i+1) = '\0';
448 Debug(LDAP_DEBUG_TRACE, "dn '%s'\n", temp_base_dn, 0, 0);
450 if ((rc=backsql_BindParamStr(sth,2,temp_base_dn,BACKSQL_MAX_DN_LEN)) !=
453 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): error binding base_dn parameter (2)\n",0,0,0);
454 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
459 case LDAP_SCOPE_ONELEVEL:
460 res=backsql_dn2id(bsi->bi,&base_id,bsi->dbh,bsi->base_dn);
463 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): could not retrieve base_dn id - no such entry\n",0,0,0);
464 bsi->status=LDAP_NO_SUCH_OBJECT;
467 if (backsql_BindParamID(sth,2,&base_id.id) != SQL_SUCCESS)
469 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): error binding base id parameter\n",0,0,0);
477 if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
479 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): error executing query\n",0,0,0);
480 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
481 SQLFreeStmt(sth,SQL_DROP);
485 backsql_BindRowAsStrings(sth,&row);
486 while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
489 e=(Entry*)ch_calloc(1,sizeof(Entry));
490 for (i=1;i<row.ncols;i++)
492 if (row.is_null[i]>0)
494 backsql_entry_addattr(e,row.col_names[i],row.cols[i],row.col_prec[i]);
495 Debug(LDAP_DEBUG_TRACE,"prec=%d\n",(int)row.col_prec[i],0,0);
498 Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
502 c_id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
503 c_id->id=atoi(row.cols[0]);
504 c_id->keyval=atoi(row.cols[1]);
505 c_id->oc_id=bsi->oc->id;
506 c_id->dn=ch_strdup(row.cols[3]);
507 c_id->next=bsi->id_list;
509 Debug(LDAP_DEBUG_TRACE,"backsql_oc_get_candidates(): added entry id=%d, keyval=%d dn='%s'\n",
510 c_id->id,c_id->keyval,row.cols[3]);
512 backsql_FreeRow(&row);
513 SQLFreeStmt(sth,SQL_DROP);
515 Debug(LDAP_DEBUG_TRACE,"<==backsql_oc_get_candidates()\n",0,0,0);
519 int backsql_search(BackendDB *be,Connection *conn,Operation *op,
520 const char *base, const char *nbase, int scope,int deref,int slimit,int tlimit,
521 Filter *filter, const char *filterstr,char **attrs,int attrsonly)
523 backsql_info *bi=(backsql_info*)be->be_private;
528 int manageDSAit = get_manageDSAit( op );
529 struct berval **v2refs = NULL;
531 backsql_srch_info srch_info;
532 backsql_entryID *eid=NULL;
534 Debug(LDAP_DEBUG_TRACE,"==>backsql_search(): base='%s', filter='%s', scope=%d,",
535 nbase,filterstr,scope);
536 Debug(LDAP_DEBUG_TRACE," deref=%d, attrsonly=%d, attributes to load: %s\n",
537 deref,attrsonly,attrs==NULL?"all":"custom list");
538 dbh=backsql_get_db_conn(be,conn);
542 Debug(LDAP_DEBUG_TRACE,"backsql_search(): could not get connection handle - exiting\n",0,0,0);
543 send_ldap_result(conn,op,LDAP_OTHER,"","SQL-backend error",NULL,NULL);
547 /* TimesTen : Pass it along to the lower level routines */
548 srch_info.isTimesTen = bi->isTimesTen;
550 if (tlimit == 0 && be_isroot(be,op->o_dn))
552 tlimit = -1; /* allow root to set no limit */
556 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
557 be->be_timelimit : tlimit;
558 stoptime = op->o_time + tlimit;
561 if (slimit == 0 && be_isroot(be,op->o_dn))
563 slimit = -1; /* allow root to set no limit */
567 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
568 be->be_sizelimit : slimit;
571 backsql_init_search(&srch_info,bi,(char*)nbase,scope,slimit,tlimit,stoptime,filter,dbh,
574 /*for each objectclass we try to construct query which gets IDs
575 *of entries matching LDAP query filter and scope (or at least candidates),
578 avl_apply(bi->oc_by_name,(AVL_APPLY)backsql_oc_get_candidates,&srch_info,0,AVL_INORDER);
581 /*now we load candidate entries (only those attrubutes mentioned in attrs and filter),
582 *test it against full filter and then send to client
584 for(eid=srch_info.id_list;eid!=NULL;eid=eid->next)
586 /* check for abandon */
587 ldap_pvt_thread_mutex_lock(&op->o_abandonmutex);
590 ldap_pvt_thread_mutex_unlock(&op->o_abandonmutex);
593 ldap_pvt_thread_mutex_unlock(&op->o_abandonmutex);
595 /* check time limit */
596 if ( tlimit != -1 && slap_get_time() > stoptime)
598 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
599 NULL, NULL, v2refs, NULL, nentries );
604 Debug(LDAP_DEBUG_TRACE,"backsql_search(): loading data for entry id=%d, oc_id=%d, keyval=%d\n",
605 eid->id,eid->oc_id,eid->keyval);
607 entry=(Entry *)ch_calloc(sizeof(Entry),1);
608 res=backsql_id2entry(&srch_info,entry,eid);
611 Debug(LDAP_DEBUG_TRACE,"backsql_search(): error in backsql_id2entry() - skipping entry\n",0,0,0);
615 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
616 is_entry_referral( entry ) )
618 struct berval **refs = get_entry_referrals(be,conn,op,entry);
620 send_search_reference( be, conn, op, entry, refs, scope, NULL, &v2refs );
621 ber_bvecfree( refs );
625 if (test_filter(be,conn,op,entry,filter)==LDAP_COMPARE_TRUE)
627 if ((sres=send_search_entry(be,conn,op,entry,attrs,attrsonly,NULL))==-1)
629 Debug(LDAP_DEBUG_TRACE,"backsql_search(): connection lost\n",0,0,0);
637 for(eid=srch_info.id_list;eid!=NULL;eid=backsql_free_entryID(eid));
639 charray_free(srch_info.attrs);
642 send_search_result( conn, op,
643 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
644 NULL, NULL, v2refs, NULL, nentries );
646 send_ldap_result(conn,op,LDAP_NO_SUCH_OBJECT,NULL,NULL,NULL,0);
648 Debug(LDAP_DEBUG_TRACE,"<==backsql_search()\n",0,0,0);
652 #endif /* SLAPD_SQL */