]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache-template.c
Fix prev commit, return generated passwd
[openldap] / servers / slapd / back-meta / cache-template.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2003 The OpenLDAP Foundation.
5  * Portions Copyright 2003 IBM Corporation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by the Apurva Kumar for inclusion
18  * in OpenLDAP Software based, in part, on existing OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28
29 #include "slap.h"
30 #include "../back-ldap/back-ldap.h"
31 #include "back-meta.h"
32 #undef ldap_debug       /* silence a warning in ldap-int.h */
33 #include "../../../libraries/libldap/ldap-int.h"
34
35 void 
36 filter2template(
37         Filter                  *f,
38         struct                  berval *fstr,
39         AttributeName**         filter_attrs, 
40         int*                    filter_cnt,
41         struct exception*       result  )
42 {
43         int     i;
44         Filter  *p;
45         struct berval tmp;
46         ber_len_t len;
47         const char* text; 
48
49         /*
50          * FIXME: should we use an assert here?
51          */
52         if ( f == NULL ) {
53                 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
54                 result->type = FILTER_ERR; 
55                 return; 
56         }
57
58         switch ( f->f_choice ) {
59         case LDAP_FILTER_EQUALITY:
60                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
61                                 ( sizeof("(=)") - 1 );
62                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
63
64                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
65                                 f->f_av_desc->ad_cname.bv_val );
66
67                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
68                                 (*filter_cnt + 2)*sizeof(AttributeName)); 
69                 
70 #if 0   /* ? */
71                 ber_dupbv(&(*filter_attrs)[*filter_cnt].an_name,
72                                 &f->f_av_desc->ad_cname); 
73 #endif
74                 
75                 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname; 
76                 (*filter_attrs)[*filter_cnt].an_desc = NULL; 
77                 slap_bv2ad(&f->f_av_desc->ad_cname,
78                                 &(*filter_attrs)[*filter_cnt].an_desc, &text); 
79                 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL; 
80                 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0; 
81                 (*filter_cnt)++; 
82                 break;
83         case LDAP_FILTER_GE:
84                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
85                                 + ( sizeof("(>=)") - 1 );
86                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
87
88                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=)",
89                         f->f_av_desc->ad_cname.bv_val);
90
91                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs, 
92                                 (*filter_cnt + 2)*sizeof(AttributeName)); 
93 #if 0   /* ? */
94                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
95                                 &f->f_av_desc->ad_cname);
96 #endif
97
98                 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname; 
99                 (*filter_attrs)[*filter_cnt].an_desc = NULL; 
100                 slap_bv2ad(&f->f_av_desc->ad_cname,
101                                 &(*filter_attrs)[*filter_cnt].an_desc, &text); 
102                 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL; 
103                 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0; 
104                 (*filter_cnt)++; 
105
106                 break;
107
108         case LDAP_FILTER_LE:
109                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
110                         + ( sizeof("(<=)") - 1 );
111                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
112
113                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=)",
114                         f->f_av_desc->ad_cname.bv_val);
115
116                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs, 
117                                 (*filter_cnt + 2)*sizeof(AttributeName)); 
118 #if 0   /* ? */
119                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
120                                 &f->f_av_desc->ad_cname);
121 #endif
122                 
123                 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname; 
124                 (*filter_attrs)[*filter_cnt].an_desc = NULL; 
125                 slap_bv2ad(&f->f_av_desc->ad_cname,
126                                 &(*filter_attrs)[*filter_cnt].an_desc, &text); 
127                 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL; 
128                 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0; 
129                 (*filter_cnt++); 
130
131                 break;
132
133         case LDAP_FILTER_APPROX:
134                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
135                         + ( sizeof("(~=)") - 1 );
136                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
137
138                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=)",
139                         f->f_av_desc->ad_cname.bv_val);
140                 
141                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
142                                 (*filter_cnt + 2)*sizeof(AttributeName));
143
144 #if 0   /* ? */
145                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
146                                 &f->f_av_desc->ad_cname);
147 #endif
148                 
149                 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname; 
150                 (*filter_attrs)[*filter_cnt].an_desc = NULL; 
151                 slap_bv2ad(&f->f_av_desc->ad_cname,
152                                 &(*filter_attrs)[*filter_cnt].an_desc, &text); 
153                 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL; 
154                 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0; 
155                 (*filter_cnt)++; 
156
157                 break;
158
159         case LDAP_FILTER_SUBSTRINGS:
160                 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
161                         ( sizeof("(=)") - 1 );
162                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
163
164                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
165                         f->f_sub_desc->ad_cname.bv_val );
166
167                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
168                                 (*filter_cnt + 2)*sizeof(AttributeName));
169
170 #if 0   /* ? */
171                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
172                                 &f->f_av_desc->ad_cname);
173 #endif
174                 
175                 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname; 
176                 (*filter_attrs)[*filter_cnt].an_desc = NULL; 
177                 slap_bv2ad(&f->f_av_desc->ad_cname,
178                                 &(*filter_attrs)[*filter_cnt].an_desc, &text);
179                 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL; 
180                 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0; 
181                 (*filter_cnt)++; 
182
183                 break;
184
185         case LDAP_FILTER_PRESENT:
186                 fstr->bv_len = f->f_desc->ad_cname.bv_len +
187                         ( sizeof("(=*)") - 1 );
188                 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
189
190                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
191                         f->f_desc->ad_cname.bv_val );
192
193                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
194                                 (*filter_cnt+2)*sizeof(AttributeName)); 
195
196 #if 0   /* ? */
197                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
198                                 &f->f_av_desc->ad_cname);
199 #endif
200                 
201                 (*filter_attrs)[*filter_cnt].an_name = f->f_desc->ad_cname; 
202                 (*filter_attrs)[*filter_cnt].an_desc = NULL; 
203                 slap_bv2ad(&f->f_desc->ad_cname,
204                                 &(*filter_attrs)[*filter_cnt].an_desc, &text);
205                 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL; 
206                 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0; 
207                 (*filter_cnt)++; 
208
209                 break;
210
211         case LDAP_FILTER_AND:
212         case LDAP_FILTER_OR:
213         case LDAP_FILTER_NOT:
214                 fstr->bv_len = sizeof("(%)") - 1;
215                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
216
217                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
218                         f->f_choice == LDAP_FILTER_AND ? '&' :
219                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
220
221                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
222                         len = fstr->bv_len;
223
224                         filter2template( p, &tmp, filter_attrs, filter_cnt,
225                                         result); 
226                         if (result->type != SUCCESS) {
227                                 return; 
228                         }
229                         
230                         fstr->bv_len += tmp.bv_len;
231                         fstr->bv_val = ch_realloc( fstr->bv_val,
232                                         fstr->bv_len + 1 );
233
234                         snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
235                                 /*"("*/ "%s)", tmp.bv_val );
236
237                         ch_free( tmp.bv_val );
238                 }
239
240                 break;
241
242         default:
243                 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
244                 result->type = FILTER_ERR; 
245                 return;
246         }
247 }