]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
Added bdb_attribute and bdb_group ACL support routines
[openldap] / servers / slapd / back-sql / entry-id.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
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.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <string.h>
17 #include "slap.h"
18 #include "back-sql.h"
19 #include "sql-wrap.h"
20 #include "schema-map.h"
21 #include "entry-id.h"
22 #include "util.h"
23
24 backsql_entryID* backsql_free_entryID(backsql_entryID* id)
25 {
26  backsql_entryID* next=id->next;
27  if (id->dn!=NULL)
28   free(id->dn);
29  free(id);
30  return next;
31 }
32
33 backsql_entryID* backsql_dn2id(backsql_info *bi,backsql_entryID *id,SQLHDBC dbh,char *dn)
34 {
35  SQLHSTMT sth; 
36  BACKSQL_ROW_NTS row;
37  /*SQLINTEGER nrows=0;*/
38  RETCODE rc;
39
40  /* TimesTen */
41  char upperdn[BACKSQL_MAX_DN_LEN+1];
42  char* toBind;
43  int i, j, k;
44
45  Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): dn='%s'\n",dn,0,0);
46  /* begin TimesTen */
47  Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
48  rc = backsql_Prepare(dbh,&sth,bi->id_query,0);
49  if (rc != SQL_SUCCESS) {
50    Debug(LDAP_DEBUG_TRACE, "backsql_dn2id(): error preparing SQL:\n", 0, 0, 0);
51    Debug(LDAP_DEBUG_TRACE, "%s\n", bi->id_query, 0, 0);
52    backsql_PrintErrors(SQL_NULL_HENV, dbh, sth, rc);
53    SQLFreeStmt(sth, SQL_DROP);
54    return NULL;
55  }
56
57  if (bi->has_ldapinfo_dn_ru) {
58    /* Prepare an upper cased, byte reversed version that can be
59       searched using indexes */
60
61    for ((i=0, j=strlen(dn)-1); *(dn+i); (i++, j--)) {
62      *(upperdn+i) = toupper(*(dn+j));
63    }   
64    *(upperdn+i) = '\0';
65    Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): upperdn='%s'\n",upperdn,0,0);
66    toBind = upperdn;
67  }
68  else {
69    if (bi->isTimesTen) {
70      for (i = 0; *(dn+i); i++) {
71        *(upperdn+i) = toupper(*(dn+i)); /* Copy while upper casing */
72      }
73      *(upperdn+i) = '\0';
74      Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): upperdn='%s'\n",upperdn,0,0);
75      toBind = upperdn;
76    }
77    else
78      toBind = dn;
79  }
80
81  if ((rc=backsql_BindParamStr(sth,1,toBind,
82                   BACKSQL_MAX_DN_LEN)) != SQL_SUCCESS)
83  /* end TimesTen*/
84  {
85    Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error binding dn parameter:\n",0,0,0);
86    backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
87    SQLFreeStmt(sth,SQL_DROP);
88    return NULL;
89  }
90  
91  if ((rc=SQLExecute(sth)) != SQL_SUCCESS)
92   {
93    Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error executing query:\n",0,0,0);
94    backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
95    SQLFreeStmt(sth,SQL_DROP);
96    return NULL;
97   }
98  
99  backsql_BindRowAsStrings(sth,&row);
100  if ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
101   {
102    if (id==NULL)
103     {
104      id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
105     }
106    id->id=atoi(row.cols[0]);
107    id->keyval=atoi(row.cols[1]);
108    id->oc_id=atoi(row.cols[2]);
109    id->dn=ch_strdup(dn);
110    id->next=NULL;
111   }
112  else
113   id=NULL;
114  backsql_FreeRow(&row);
115  
116  SQLFreeStmt(sth, SQL_DROP);
117  if (id!=NULL)
118   Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): id=%d\n",(int)id->id,0,0);
119  else
120   Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): no match\n",0,0,0);
121  return id;
122 }
123
124
125 int backsql_get_attr_vals(backsql_at_map_rec *at,backsql_srch_info *bsi)
126 {
127  RETCODE rc;
128  SQLHSTMT sth;
129  BACKSQL_ROW_NTS row;
130  int i;
131  
132  Debug(LDAP_DEBUG_TRACE,"==>backsql_get_attr_vals(): oc='%s' attr='%s' keyval=%d\n",
133                         bsi->oc->name,at->name,bsi->c_eid->keyval);
134
135  if ((rc=backsql_Prepare(bsi->dbh,&sth,at->query,0)) != SQL_SUCCESS)
136   {
137    Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error preparing query: %s\n",at->query,0,0);
138    backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
139    return 1;
140   }
141
142  if (backsql_BindParamID(sth,1,&(bsi->c_eid->keyval)) != SQL_SUCCESS)
143  {
144   Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error binding key value parameter\n",0,0,0);
145   return 1;
146  }
147
148  if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
149   {
150    Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error executing attribute query '%s'\n",at->query,0,0);
151    backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
152    SQLFreeStmt(sth,SQL_DROP);
153    return 1;
154   }
155
156  backsql_BindRowAsStrings(sth,&row);
157  while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
158   {
159    for (i=0;i<row.ncols;i++)
160     {
161      if (row.is_null[i]>0)
162       {
163        backsql_entry_addattr(bsi->e,row.col_names[i],row.cols[i],/*row.col_prec[i]*/
164                                         strlen(row.cols[i]));
165 /*       Debug(LDAP_DEBUG_TRACE,"prec=%d\n",(int)row.col_prec[i],0,0);*/
166       }
167     /* else
168       Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
169 */
170     }
171   }
172  backsql_FreeRow(&row);
173  SQLFreeStmt(sth,SQL_DROP);
174  Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
175  return 1;
176 }
177
178
179 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
180 {
181  char **c_at_name;
182  backsql_at_map_rec *at;
183
184  Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
185
186  bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
187  bsi->e=e;
188  bsi->c_eid=eid;
189  e->e_attrs=NULL;
190  e->e_private=NULL;
191  
192 /* if (bsi->base_dn != NULL)???*/
193
194  e->e_id=eid->id;
195  e->e_dn=ch_strdup(bsi->c_eid->dn);
196  e->e_ndn=dn_normalize(ch_strdup(bsi->c_eid->dn));
197  
198  if (bsi->attrs!=NULL)
199  {
200   Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): custom attribute list\n",0,0,0);
201   for(c_at_name=bsi->attrs;*c_at_name!=NULL;c_at_name++)
202   {
203    if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
204    {
205         /*backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));*/
206     continue;
207    }
208    at=backsql_at_with_name(bsi->oc,*c_at_name);
209    if (at!=NULL)
210     backsql_get_attr_vals(at,bsi);
211    else
212         Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
213                         *c_at_name,bsi->oc->name,0);
214
215   }
216  }
217  else
218  {
219   Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): retrieving all attributes\n",0,0,0);
220   avl_apply(bsi->oc->attrs,(AVL_APPLY)backsql_get_attr_vals,bsi,0,AVL_INORDER);
221  }
222  backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
223
224  Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);
225  return e;
226 }
227
228 #endif /* SLAPD_SQL */