3 shellutil.c - common routines useful when building shell-based backends
4 for the standalone ldap server
6 Copyright (c) 1995 Regents of the University of Michigan.
9 Redistribution and use in source and binary forms are permitted
10 provided that this notice is preserved and that due credit is given
11 to the University of Michigan at Ann Arbor. The name of the University
12 may not be used to endorse or promote products derived from this
13 software without specific prior written permission. This software
14 is provided ``as is'' without express or implied warranty.
22 #include <ac/stdlib.h>
23 #include <ac/stdarg.h>
28 #include <ac/string.h>
32 #include "shellutil.h"
38 static struct inputparams ips[] = {
39 IP_TYPE_SUFFIX, "suffix",
41 IP_TYPE_SCOPE, "scope",
42 IP_TYPE_ALIASDEREF, "deref",
43 IP_TYPE_SIZELIMIT, "sizelimit",
44 IP_TYPE_TIMELIMIT, "timelimit",
45 IP_TYPE_FILTER, "filter",
46 IP_TYPE_ATTRS, "attrs",
47 IP_TYPE_ATTRSONLY, "attrsonly",
53 write_result( FILE *fp, int code, char *matched, char *info )
55 fprintf( fp, "RESULT\ncode: %d\n", code );
56 debug_printf( ">> RESULT\n" );
57 debug_printf( ">> code: %d\n", code );
59 if ( matched != NULL ) {
60 fprintf( fp, "matched: %s\n", matched );
61 debug_printf( ">> matched: %s\n", matched );
65 fprintf( fp, "info: %s\n", info );
66 debug_printf( ">> info: %s\n", info );
72 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
77 fprintf( ofp, "dn: %s\n", entry->lde_dn );
78 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
79 if ( attr_requested( (*app)->lda_name, op )) {
80 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
81 fprintf( ofp, "%s: %s\n", (*app)->lda_name, *valp );
90 test_filter( struct ldop *op, struct ldentry *entry )
92 return (( random() & 0x07 ) == 0x07 ); /* XXX random for now */
97 attr_requested( char *name, struct ldop *op )
101 if ( op->ldop_srch.ldsp_attrs == NULL ) { /* special case */
105 for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
106 if ( strcasecmp( name, *ap ) == 0 ) {
116 free_entry( struct ldentry *entry )
121 free( entry->lde_dn );
123 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
124 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
127 free( (*app)->lda_values );
128 free( (*app)->lda_name );
131 free( entry->lde_attrs );
137 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
139 char *p, *args, line[ MAXLINELEN + 1 ];
140 struct inputparams *ip;
142 if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
143 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
145 line[ strlen( line ) - 1 ] = '\0';
146 if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
148 write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
149 "Operation Not Supported" );
153 op->ldop_op = LDOP_SEARCH;
155 while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
156 line[ strlen( line ) - 1 ] = '\0';
157 debug_printf( "<< %s\n", line );
160 if (( ip = find_input_tag( &args )) == NULL ) {
161 debug_printf( "ignoring %s\n", line );
165 switch( ip->ip_type ) {
167 add_strval( &op->ldop_suffixes, args );
170 op->ldop_dn = estrdup( args );
173 if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
174 op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
175 op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
176 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Bad scope" );
180 case IP_TYPE_ALIASDEREF:
181 op->ldop_srch.ldsp_aliasderef = atoi( args );
183 case IP_TYPE_SIZELIMIT:
184 op->ldop_srch.ldsp_sizelimit = atoi( args );
186 case IP_TYPE_TIMELIMIT:
187 op->ldop_srch.ldsp_timelimit = atoi( args );
190 op->ldop_srch.ldsp_filter = estrdup( args );
192 case IP_TYPE_ATTRSONLY:
193 op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
196 if ( strcmp( args, "all" ) == 0 ) {
197 op->ldop_srch.ldsp_attrs = NULL;
199 while ( args != NULL ) {
200 if (( p = strchr( args, ' ' )) != NULL ) {
202 while ( isspace( (unsigned char) *p )) {
206 add_strval( &op->ldop_srch.ldsp_attrs, args );
214 if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
215 op->ldop_srch.ldsp_filter == NULL ) {
216 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL,
217 "Required suffix:, base:, or filter: missing" );
226 find_input_tag( char **linep ) /* linep is set to start of args */
231 if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
235 for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
236 if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
237 while ( isspace( (unsigned char) *(++p) )) {
250 add_strval( char ***sp, char *val )
257 if ( vallist == NULL ) {
260 for ( i = 0; vallist[ i ] != NULL; ++i ) {
265 vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
266 vallist[ i ] = estrdup( val );
267 vallist[ ++i ] = NULL;
277 if (( p = strdup( s )) == NULL ) {
278 debug_printf( "strdup failed\n" );
279 exit( EXIT_FAILURE );
287 erealloc( void *s, unsigned size )
294 p = realloc( s, size );
298 debug_printf( "realloc( p, %d ) failed\n", size );
299 exit( EXIT_FAILURE );
307 ecalloc( unsigned nelem, unsigned elsize )
311 if (( p = calloc( nelem, elsize )) == NULL ) {
312 debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
313 exit( EXIT_FAILURE );
324 debug_printf( const char *fmt, ... )
330 fprintf( stderr, "%s: ", progname );
331 vfprintf( stderr, fmt, ap );
338 dump_ldop( struct ldop *op )
344 debug_printf( "SEARCH operation\n" );
345 if ( op->ldop_suffixes == NULL ) {
346 debug_printf( " suffix: NONE\n" );
349 for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
350 debug_printf( " suffix: <%s>\n", op->ldop_suffixes[ i ] );
353 debug_printf( " dn: <%s>\n", op->ldop_dn );
354 debug_printf( " scope: <%d>\n", op->ldop_srch.ldsp_scope );
355 debug_printf( " filter: <%s>\n", op->ldop_srch.ldsp_filter );
356 debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
357 debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
358 debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
359 debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
360 if ( op->ldop_srch.ldsp_attrs == NULL ) {
361 debug_printf( " attrs: ALL\n" );
365 for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
366 debug_printf( " attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
370 #endif /* LDAP_DEBUG */