]> git.sur5r.net Git - openldap/blob - servers/slapd/shell-backends/shellutil.c
7926aacf67ce794651d7feb67e382797e446660e
[openldap] / servers / slapd / shell-backends / shellutil.c
1 /* shellutil.c - common routines useful when building shell-based backends */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
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 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26 /* ACKNOWLEDGEMENTS:
27  * This work was originally developed by the University of Michigan
28  * (as part of U-MICH LDAP).
29  */
30
31
32 #include "portable.h"
33
34 #include <stdio.h>
35
36 #include <ac/stdlib.h>
37 #include <ac/stdarg.h>
38
39 #include <pwd.h>
40
41 #include <ac/ctype.h>
42 #include <ac/string.h>
43
44 #include <lber.h>
45 #include <ldap.h>
46 #include "shellutil.h"
47
48
49 int     debugflg;
50 char    *progname;
51
52 static struct inputparams       ips[] = {
53     IP_TYPE_SUFFIX,     "suffix",
54     IP_TYPE_BASE,       "base",
55     IP_TYPE_SCOPE,      "scope",
56     IP_TYPE_ALIASDEREF, "deref",
57     IP_TYPE_SIZELIMIT,  "sizelimit",
58     IP_TYPE_TIMELIMIT,  "timelimit",
59     IP_TYPE_FILTER,     "filter",
60     IP_TYPE_ATTRS,      "attrs",
61     IP_TYPE_ATTRSONLY,  "attrsonly",
62     0,                  NULL
63 };
64
65
66 void
67 write_result( FILE *fp, int code, char *matched, char *info )
68 {
69     fprintf( fp, "RESULT\ncode: %d\n", code );
70     debug_printf( ">> RESULT\n" );
71     debug_printf( ">> code: %d\n", code );
72
73     if ( matched != NULL ) {
74         fprintf( fp, "matched: %s\n", matched );
75         debug_printf( ">> matched: %s\n", matched );
76     }
77
78     if ( info != NULL ) {
79         fprintf( fp, "info: %s\n", info );
80         debug_printf( ">> info: %s\n", info );
81     }
82 }
83
84
85 void
86 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
87 {
88     struct ldattr       **app;
89     char                **valp;
90
91     fprintf( ofp, "dn: %s\n", entry->lde_dn );
92     for ( app = entry->lde_attrs; *app != NULL; ++app ) {
93         if ( attr_requested( (*app)->lda_name, op )) {
94             for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
95                 fprintf( ofp, "%s: %s\n", (*app)->lda_name, *valp );
96             }
97         }
98     }
99     fputc( '\n', ofp );
100 }
101
102
103 int
104 test_filter( struct ldop *op, struct ldentry *entry )
105 {
106     return ((random() & 0x07 ) == 0x07) /* XXX random for now */
107                 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
108 }
109
110
111 int
112 attr_requested( char *name, struct ldop *op )
113 {
114     char        **ap;
115
116     if ( op->ldop_srch.ldsp_attrs == NULL ) {   /* special case */
117         return( 1 );
118     }
119
120     for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
121         if ( strcasecmp( name, *ap ) == 0 ) {
122             return( 1 );
123         }
124     }
125
126     return( 0 );
127 }
128
129
130 void
131 free_entry( struct ldentry *entry )
132 {
133     struct ldattr       **app;
134     char                **valp;
135
136     free( entry->lde_dn );
137
138     for ( app = entry->lde_attrs; *app != NULL; ++app ) {
139         for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
140             free( *valp );
141         }
142         free( (*app)->lda_values );
143         free( (*app)->lda_name );
144     }
145
146     free( entry->lde_attrs );
147     free( entry );
148 }
149
150
151 int
152 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
153 {
154     char                *p, *args, line[ MAXLINELEN + 1 ];
155     struct inputparams  *ip;
156
157     if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
158         write_result( ofp, LDAP_OTHER, NULL, "Empty Input" );
159     }
160     line[ strlen( line ) - 1 ] = '\0';
161     if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
162             != 0 ) {
163         write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
164                 "Operation Not Supported" );
165         return( -1 );
166     }
167
168     op->ldop_op = LDOP_SEARCH;
169
170     while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
171         line[ strlen( line ) - 1 ] = '\0';
172         debug_printf( "<< %s\n", line );
173
174         args = line;
175         if (( ip = find_input_tag( &args )) == NULL ) {
176             debug_printf( "ignoring %s\n", line );
177             continue;
178         }
179
180         switch( ip->ip_type ) {
181         case IP_TYPE_SUFFIX:
182             add_strval( &op->ldop_suffixes, args );
183             break;
184         case IP_TYPE_BASE:
185             op->ldop_dn = estrdup( args );
186             break;
187         case IP_TYPE_SCOPE:
188             if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
189                     op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
190                     op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
191                 write_result( ofp, LDAP_OTHER, NULL, "Bad scope" );
192                 return( -1 );
193             }
194             break;
195         case IP_TYPE_ALIASDEREF:
196             op->ldop_srch.ldsp_aliasderef = atoi( args );
197             break;
198         case IP_TYPE_SIZELIMIT:
199             op->ldop_srch.ldsp_sizelimit = atoi( args );
200             break;
201         case IP_TYPE_TIMELIMIT:
202             op->ldop_srch.ldsp_timelimit = atoi( args );
203             break;
204         case IP_TYPE_FILTER:
205             op->ldop_srch.ldsp_filter = estrdup( args );
206             break;
207         case IP_TYPE_ATTRSONLY:
208             op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
209             break;
210         case IP_TYPE_ATTRS:
211             if ( strcmp( args, "all" ) == 0 ) {
212                 op->ldop_srch.ldsp_attrs = NULL;
213             } else {
214                 while ( args != NULL ) {
215                     if (( p = strchr( args, ' ' )) != NULL ) {
216                         *p++ = '\0';
217                         while ( isspace( (unsigned char) *p )) {
218                             ++p;
219                         }
220                     }
221                     add_strval( &op->ldop_srch.ldsp_attrs, args );
222                     args = p;
223                 }
224             }
225             break;
226         }
227     }
228
229     if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
230                 op->ldop_srch.ldsp_filter == NULL ) {
231         write_result( ofp, LDAP_OTHER, NULL,
232                 "Required suffix:, base:, or filter: missing" );
233         return( -1 );
234     }
235
236     return( 0 );
237 }
238
239
240 struct inputparams *
241 find_input_tag( char **linep )  /* linep is set to start of args */
242 {
243     int         i;
244     char        *p;
245
246     if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
247         return( NULL );
248     }
249
250     for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
251         if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
252             while ( isspace( (unsigned char) *(++p) )) {
253                 ;
254             }
255             *linep = p;
256             return( &ips[ i ] );
257         }
258     }
259
260     return( NULL );
261 }
262
263
264 void
265 add_strval( char ***sp, char *val )
266 {
267     int         i;
268     char        **vallist;
269
270     vallist = *sp;
271
272     if ( vallist == NULL ) {
273         i = 0;
274     } else {
275         for ( i = 0; vallist[ i ] != NULL; ++i ) {
276             ;
277         }
278     }
279
280     vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
281     vallist[ i ] = estrdup( val );
282     vallist[ ++i ] = NULL;
283     *sp = vallist;
284 }
285
286
287 char *
288 estrdup( char *s )
289 {
290     char        *p;
291
292     if (( p = strdup( s )) == NULL ) {
293         debug_printf( "strdup failed\n" );
294         exit( EXIT_FAILURE );
295     }
296
297     return( p );
298 }
299
300
301 void *
302 erealloc( void *s, unsigned size )
303 {
304     char        *p;
305
306     if ( s == NULL ) {
307         p = malloc( size );
308     } else {
309         p = realloc( s, size );
310     }
311
312     if ( p == NULL ) {
313         debug_printf( "realloc( p, %d ) failed\n", size );
314         exit( EXIT_FAILURE );
315     }
316
317     return( p );
318 }
319
320
321 char *
322 ecalloc( unsigned nelem, unsigned elsize )
323 {
324     char        *p;
325
326     if (( p = calloc( nelem, elsize )) == NULL ) {
327         debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
328         exit( EXIT_FAILURE );
329     }
330
331     return( p );
332 }
333
334
335 #ifdef LDAP_DEBUG
336
337 /* VARARGS */
338 void
339 debug_printf( const char *fmt, ... )
340 {
341     va_list     ap;
342
343         if ( debugflg ) {
344                 va_start( ap, fmt );
345                 fprintf( stderr, "%s: ", progname );
346                 vfprintf( stderr, fmt, ap );
347                 va_end( ap );
348         }
349 }
350
351
352 void
353 dump_ldop( struct ldop *op )
354 {
355     if ( !debugflg ) {
356         return;
357     }
358
359     debug_printf( "SEARCH operation\n" );
360     if ( op->ldop_suffixes == NULL ) {
361         debug_printf( "    suffix: NONE\n" );
362     } else {
363         int     i;
364         for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
365             debug_printf( "    suffix: <%s>\n", op->ldop_suffixes[ i ] );
366         }
367     }
368     debug_printf( "        dn: <%s>\n", op->ldop_dn );
369     debug_printf( "     scope: <%d>\n", op->ldop_srch.ldsp_scope );
370     debug_printf( "    filter: <%s>\n", op->ldop_srch.ldsp_filter );
371     debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
372     debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
373     debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
374     debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
375     if ( op->ldop_srch.ldsp_attrs == NULL ) {
376         debug_printf( "     attrs: ALL\n" );
377     } else {
378         int     i;
379
380         for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
381             debug_printf( "  attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
382         }
383     }
384 }
385 #endif /* LDAP_DEBUG */