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.
30 #include <ac/string.h>
34 #include "shellutil.h"
40 static struct inputparams ips[] = {
41 IP_TYPE_SUFFIX, "suffix",
43 IP_TYPE_SCOPE, "scope",
44 IP_TYPE_ALIASDEREF, "deref",
45 IP_TYPE_SIZELIMIT, "sizelimit",
46 IP_TYPE_TIMELIMIT, "timelimit",
47 IP_TYPE_FILTER, "filter",
48 IP_TYPE_ATTRS, "attrs",
49 IP_TYPE_ATTRSONLY, "attrsonly",
55 write_result( FILE *fp, int code, char *matched, char *info )
57 fprintf( fp, "RESULT\ncode: %d\n", code );
58 debug_printf( ">> RESULT\n" );
59 debug_printf( ">> code: %d\n", code );
61 if ( matched != NULL ) {
62 fprintf( fp, "matched: %s\n", matched );
63 debug_printf( ">> matched: %s\n", matched );
67 fprintf( fp, "info: %s\n", info );
68 debug_printf( ">> info: %s\n", info );
74 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
79 fprintf( ofp, "dn: %s\n", entry->lde_dn );
80 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
81 if ( attr_requested( (*app)->lda_name, op )) {
82 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
83 fprintf( ofp, "%s: %s\n", (*app)->lda_name, *valp );
92 test_filter( struct ldop *op, struct ldentry *entry )
94 return (( random() & 0x07 ) == 0x07 ); /* XXX random for now */
99 attr_requested( char *name, struct ldop *op )
103 if ( op->ldop_srch.ldsp_attrs == NULL ) { /* special case */
107 for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
108 if ( strcasecmp( name, *ap ) == 0 ) {
118 free_entry( struct ldentry *entry )
123 free( entry->lde_dn );
125 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
126 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
129 free( (*app)->lda_values );
130 free( (*app)->lda_name );
133 free( entry->lde_attrs );
139 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
141 char *p, *args, line[ MAXLINELEN + 1 ];
142 struct inputparams *ip;
144 if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
145 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
147 line[ strlen( line ) - 1 ] = '\0';
148 if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
150 write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
151 "Operation Not Supported" );
155 op->ldop_op = LDOP_SEARCH;
157 while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
158 line[ strlen( line ) - 1 ] = '\0';
159 debug_printf( "<< %s\n", line );
162 if (( ip = find_input_tag( &args )) == NULL ) {
163 debug_printf( "ignoring %s\n", line );
167 switch( ip->ip_type ) {
169 add_strval( &op->ldop_suffixes, args );
172 op->ldop_dn = estrdup( args );
175 if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
176 op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
177 op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
178 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Bad scope" );
182 case IP_TYPE_ALIASDEREF:
183 op->ldop_srch.ldsp_aliasderef = atoi( args );
185 case IP_TYPE_SIZELIMIT:
186 op->ldop_srch.ldsp_sizelimit = atoi( args );
188 case IP_TYPE_TIMELIMIT:
189 op->ldop_srch.ldsp_timelimit = atoi( args );
192 op->ldop_srch.ldsp_filter = estrdup( args );
194 case IP_TYPE_ATTRSONLY:
195 op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
198 if ( strcmp( args, "all" ) == 0 ) {
199 op->ldop_srch.ldsp_attrs = NULL;
201 while ( args != NULL ) {
202 if (( p = strchr( args, ' ' )) != NULL ) {
204 while ( isspace( *p )) {
208 add_strval( &op->ldop_srch.ldsp_attrs, args );
216 if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
217 op->ldop_srch.ldsp_filter == NULL ) {
218 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL,
219 "Required suffix:, base:, or filter: missing" );
228 find_input_tag( char **linep ) /* linep is set to start of args */
233 if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
237 for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
238 if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
239 while ( isspace( *(++p) )) {
252 add_strval( char ***sp, char *val )
259 if ( vallist == NULL ) {
262 for ( i = 0; vallist[ i ] != NULL; ++i ) {
267 vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
268 vallist[ i ] = estrdup( val );
269 vallist[ ++i ] = NULL;
279 if (( p = strdup( s )) == NULL ) {
280 debug_printf( "strdup failed\n" );
289 erealloc( void *s, unsigned size )
296 p = realloc( s, size );
300 debug_printf( "realloc( p, %d ) failed\n", size );
309 ecalloc( unsigned nelem, unsigned elsize )
313 if (( p = calloc( nelem, elsize )) == NULL ) {
314 debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
344 fmt = va_arg( ap, char * );
346 fprintf( stderr, "%s: ", progname );
347 vfprintf( stderr, fmt, ap );
354 dump_ldop( struct ldop *op )
360 debug_printf( "SEARCH operation\n" );
361 if ( op->ldop_suffixes == NULL ) {
362 debug_printf( " suffix: NONE\n" );
365 for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
366 debug_printf( " suffix: <%s>\n", op->ldop_suffixes[ i ] );
369 debug_printf( " dn: <%s>\n", op->ldop_dn );
370 debug_printf( " scope: <%d>\n", op->ldop_srch.ldsp_scope );
371 debug_printf( " filter: <%s>\n", op->ldop_srch.ldsp_filter );
372 debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
373 debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
374 debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
375 debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
376 if ( op->ldop_srch.ldsp_attrs == NULL ) {
377 debug_printf( " attrs: ALL\n" );
381 for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
382 debug_printf( " attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
386 #endif /* LDAP_DEBUG */