]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/util.c
Added bdb_attribute and bdb_group ACL support routines
[openldap] / servers / slapd / back-sql / util.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 <stdarg.h>
18 #include "slap.h"
19 #include "back-sql.h"
20 #include "schema-map.h"
21 #include "util.h"
22
23
24 char backsql_def_oc_query[]="SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return FROM ldap_oc_mappings";
25 char backsql_def_at_query[]="SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,param_order,expect_return,sel_expr_u FROM ldap_attr_mappings WHERE oc_map_id=?";
26 char backsql_def_delentry_query[]="DELETE FROM ldap_entries WHERE id=?";
27 char backsql_def_insentry_query[]="INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) VALUES (?,?,?,?)";
28 char backsql_def_subtree_cond[]="ldap_entries.dn LIKE CONCAT('%',?)";
29 char backsql_id_query[]="SELECT id,keyval,oc_map_id FROM ldap_entries WHERE ";
30
31 /* TimesTen*/
32 char backsql_check_dn_ru_query[] = "SELECT dn_ru from ldap_entries";
33
34 char* backsql_strcat(char* dest,int *buflen, ...)
35 {
36  va_list strs;
37  int cdlen,cslen,grow;
38  char *cstr;
39  
40  /*Debug(LDAP_DEBUG_TRACE,"==>my_strcat()\n");*/
41  va_start(strs,buflen);
42  if (dest==NULL || *buflen<=0)
43   {
44    dest=(char*)ch_calloc(BACKSQL_STR_GROW,sizeof(char));
45    *buflen=BACKSQL_STR_GROW;
46   }
47  cdlen=strlen(dest)+1;
48  while ((cstr=va_arg(strs,char*)) != NULL)
49   {
50    cslen=strlen(cstr);
51    grow=BACKSQL_MAX(BACKSQL_STR_GROW,cslen);
52    if (*buflen-cdlen < cslen)
53     {
54      /*Debug(LDAP_DEBUG_TRACE,"my_strcat(): buflen=%d, cdlen=%d, cslen=%d -- reallocating dest\n",
55                            *buflen,cdlen,cslen); */
56      dest=(char*)ch_realloc(dest,(*buflen)+grow*sizeof(char));
57      if (dest == NULL)
58       {
59        Debug(LDAP_DEBUG_ANY,"my_strcat(): could not reallocate string buffer.\n",0,0,0);
60       }
61      *buflen+=grow;
62      /*Debug(LDAP_DEBUG_TRACE,"my_strcat(): new buflen=%d, dest=%p\n",*buflen,dest,0);*/
63     }
64    strcat(dest,cstr);
65    cdlen+=cslen;
66   }
67  va_end(strs);
68  /*Debug(LDAP_DEBUG_TRACE,"<==my_strcat() (dest='%s')\n",dest,0,0);*/
69  return dest;
70
71
72 int backsql_entry_addattr(Entry *e,char *at_name,char *at_val,unsigned int at_val_len)
73 {
74  Attribute *c_at=e->e_attrs;
75  struct berval* add_val[2];
76  struct berval cval;
77  AttributeDescription *ad;
78  int rc;
79  const char *text;
80  
81  Debug(LDAP_DEBUG_TRACE,"backsql_entry_addattr(): at_name='%s', at_val='%s'\n",at_name,at_val,0);
82  cval.bv_val=at_val;
83  cval.bv_len=at_val_len;
84  add_val[0]=&cval;
85  add_val[1]=NULL;
86  
87  ad=NULL;
88  rc = slap_str2ad( at_name, &ad, &text );
89  if( rc != LDAP_SUCCESS ) 
90   {
91    Debug(LDAP_DEBUG_TRACE,"backsql_entry_addattr(): failed to find AttributeDescription for '%s'\n",at_name,0,0);
92    return 0;
93   }
94   
95  rc = attr_merge(e,ad,add_val);
96
97  if( rc != 0 )
98   {
99    Debug(LDAP_DEBUG_TRACE,"backsql_entry_addattr(): failed to merge value '%s' for attribute '%s'\n",at_val,at_name,0);
100    return 0;
101   }
102  
103  Debug(LDAP_DEBUG_TRACE,"<==backsql_query_addattr()\n",0,0,0);
104  return 1;
105 }
106
107 char* backsql_get_table_spec(char **p)
108 {
109  char *s,*q;
110  char *res=NULL;
111  int res_len=0;
112
113  s=*p;
114  while(**p && **p!=',') (*p)++;
115  if (**p)
116   *(*p)++='\0';
117
118 #define BACKSQL_NEXT_WORD  {while (*s && isspace(*s)) s++; if (!*s) return res; q=s; while (*q && !isspace(*q)) q++; if (*q) *q++='\0';}
119  BACKSQL_NEXT_WORD;
120  res=backsql_strcat(res,&res_len,s,NULL);/*table name*/
121  s=q;
122
123  BACKSQL_NEXT_WORD;
124  if (!strcasecmp(s,"as"))
125  {
126   s=q;
127   BACKSQL_NEXT_WORD;
128  }
129  /*res=backsql_strcat(res,&res_len," AS ",s,NULL);
130   *oracle doesn't understand AS :(
131   */
132  res=backsql_strcat(res,&res_len," ",s,NULL);/*table alias*/
133  return res;
134 }
135
136 int backsql_merge_from_clause(char **dest_from,int *dest_len,char *src_from)
137 {
138  char *s,*p,*srcc,*pos,e;
139
140  /*Debug(LDAP_DEBUG_TRACE,"==>backsql_merge_from_clause(): dest_from='%s',src_from='%s'\n",
141                                 dest_from,src_from,0); */
142  srcc=ch_strdup(src_from);
143  p=srcc;
144  while(*p)
145  {
146   s=backsql_get_table_spec(&p);
147  /* Debug(LDAP_DEBUG_TRACE,"backsql_merge_from_clause(): p='%s' s='%s'\n",p,s,0);  */
148   if (*dest_from==NULL)
149    *dest_from=backsql_strcat(*dest_from,dest_len,s,NULL);
150   else
151         if((pos=strstr(*dest_from,s))==NULL)
152      *dest_from=backsql_strcat(*dest_from,dest_len,",",s,NULL);
153     else if((e=pos[strlen(s)])!='\0' && e!=',')
154       *dest_from=backsql_strcat(*dest_from,dest_len,",",s,NULL);
155   if (s)
156         ch_free(s);
157  }
158 /* Debug(LDAP_DEBUG_TRACE,"<==backsql_merge_from_clause()\n",0,0,0);*/
159  free(srcc);
160  return 1;
161 }
162
163 #endif /* SLAPD_SQL */