2 shellutil.c - common routines useful when building shell-based backends
3 for the standalone ldap server
5 Copyright (c) 1995 Regents of the University of Michigan.
8 Redistribution and use in source and binary forms are permitted
9 provided that this notice is preserved and that due credit is given
10 to the University of Michigan at Ann Arbor. The name of the University
11 may not be used to endorse or promote products derived from this
12 software without specific prior written permission. This software
13 is provided ``as is'' without express or implied warranty.
21 #include <ac/stdlib.h>
22 #include <ac/stdarg.h>
27 #include <ac/string.h>
31 #include "shellutil.h"
37 static struct inputparams ips[] = {
38 IP_TYPE_SUFFIX, "suffix",
40 IP_TYPE_SCOPE, "scope",
41 IP_TYPE_ALIASDEREF, "deref",
42 IP_TYPE_SIZELIMIT, "sizelimit",
43 IP_TYPE_TIMELIMIT, "timelimit",
44 IP_TYPE_FILTER, "filter",
45 IP_TYPE_ATTRS, "attrs",
46 IP_TYPE_ATTRSONLY, "attrsonly",
52 write_result( FILE *fp, int code, char *matched, char *info )
54 fprintf( fp, "RESULT\ncode: %d\n", code );
55 debug_printf( ">> RESULT\n" );
56 debug_printf( ">> code: %d\n", code );
58 if ( matched != NULL ) {
59 fprintf( fp, "matched: %s\n", matched );
60 debug_printf( ">> matched: %s\n", matched );
64 fprintf( fp, "info: %s\n", info );
65 debug_printf( ">> info: %s\n", info );
71 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
76 fprintf( ofp, "dn: %s\n", entry->lde_dn );
77 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
78 if ( attr_requested( (*app)->lda_name, op )) {
79 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
80 fprintf( ofp, "%s: %s\n", (*app)->lda_name, *valp );
89 test_filter( struct ldop *op, struct ldentry *entry )
91 return (( random() & 0x07 ) == 0x07 ); /* XXX random for now */
96 attr_requested( char *name, struct ldop *op )
100 if ( op->ldop_srch.ldsp_attrs == NULL ) { /* special case */
104 for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
105 if ( strcasecmp( name, *ap ) == 0 ) {
115 free_entry( struct ldentry *entry )
120 free( entry->lde_dn );
122 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
123 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
126 free( (*app)->lda_values );
127 free( (*app)->lda_name );
130 free( entry->lde_attrs );
136 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
138 char *p, *args, line[ MAXLINELEN + 1 ];
139 struct inputparams *ip;
141 if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
142 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
144 line[ strlen( line ) - 1 ] = '\0';
145 if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
147 write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
148 "Operation Not Supported" );
152 op->ldop_op = LDOP_SEARCH;
154 while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
155 line[ strlen( line ) - 1 ] = '\0';
156 debug_printf( "<< %s\n", line );
159 if (( ip = find_input_tag( &args )) == NULL ) {
160 debug_printf( "ignoring %s\n", line );
164 switch( ip->ip_type ) {
166 add_strval( &op->ldop_suffixes, args );
169 op->ldop_dn = estrdup( args );
172 if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
173 op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
174 op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
175 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Bad scope" );
179 case IP_TYPE_ALIASDEREF:
180 op->ldop_srch.ldsp_aliasderef = atoi( args );
182 case IP_TYPE_SIZELIMIT:
183 op->ldop_srch.ldsp_sizelimit = atoi( args );
185 case IP_TYPE_TIMELIMIT:
186 op->ldop_srch.ldsp_timelimit = atoi( args );
189 op->ldop_srch.ldsp_filter = estrdup( args );
191 case IP_TYPE_ATTRSONLY:
192 op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
195 if ( strcmp( args, "all" ) == 0 ) {
196 op->ldop_srch.ldsp_attrs = NULL;
198 while ( args != NULL ) {
199 if (( p = strchr( args, ' ' )) != NULL ) {
201 while ( isspace( (unsigned char) *p )) {
205 add_strval( &op->ldop_srch.ldsp_attrs, args );
213 if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
214 op->ldop_srch.ldsp_filter == NULL ) {
215 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL,
216 "Required suffix:, base:, or filter: missing" );
225 find_input_tag( char **linep ) /* linep is set to start of args */
230 if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
234 for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
235 if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
236 while ( isspace( (unsigned char) *(++p) )) {
249 add_strval( char ***sp, char *val )
256 if ( vallist == NULL ) {
259 for ( i = 0; vallist[ i ] != NULL; ++i ) {
264 vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
265 vallist[ i ] = estrdup( val );
266 vallist[ ++i ] = NULL;
276 if (( p = strdup( s )) == NULL ) {
277 debug_printf( "strdup failed\n" );
286 erealloc( void *s, unsigned size )
293 p = realloc( s, size );
297 debug_printf( "realloc( p, %d ) failed\n", size );
306 ecalloc( unsigned nelem, unsigned elsize )
310 if (( p = calloc( nelem, elsize )) == NULL ) {
311 debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
341 fmt = va_arg( ap, char * );
343 fprintf( stderr, "%s: ", progname );
344 vfprintf( stderr, fmt, ap );
351 dump_ldop( struct ldop *op )
357 debug_printf( "SEARCH operation\n" );
358 if ( op->ldop_suffixes == NULL ) {
359 debug_printf( " suffix: NONE\n" );
362 for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
363 debug_printf( " suffix: <%s>\n", op->ldop_suffixes[ i ] );
366 debug_printf( " dn: <%s>\n", op->ldop_dn );
367 debug_printf( " scope: <%d>\n", op->ldop_srch.ldsp_scope );
368 debug_printf( " filter: <%s>\n", op->ldop_srch.ldsp_filter );
369 debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
370 debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
371 debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
372 debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
373 if ( op->ldop_srch.ldsp_attrs == NULL ) {
374 debug_printf( " attrs: ALL\n" );
378 for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
379 debug_printf( " attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
383 #endif /* LDAP_DEBUG */