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 */
93 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
98 attr_requested( char *name, struct ldop *op )
102 if ( op->ldop_srch.ldsp_attrs == NULL ) { /* special case */
106 for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
107 if ( strcasecmp( name, *ap ) == 0 ) {
117 free_entry( struct ldentry *entry )
122 free( entry->lde_dn );
124 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
125 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
128 free( (*app)->lda_values );
129 free( (*app)->lda_name );
132 free( entry->lde_attrs );
138 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
140 char *p, *args, line[ MAXLINELEN + 1 ];
141 struct inputparams *ip;
143 if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
144 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
146 line[ strlen( line ) - 1 ] = '\0';
147 if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
149 write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
150 "Operation Not Supported" );
154 op->ldop_op = LDOP_SEARCH;
156 while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
157 line[ strlen( line ) - 1 ] = '\0';
158 debug_printf( "<< %s\n", line );
161 if (( ip = find_input_tag( &args )) == NULL ) {
162 debug_printf( "ignoring %s\n", line );
166 switch( ip->ip_type ) {
168 add_strval( &op->ldop_suffixes, args );
171 op->ldop_dn = estrdup( args );
174 if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
175 op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
176 op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
177 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Bad scope" );
181 case IP_TYPE_ALIASDEREF:
182 op->ldop_srch.ldsp_aliasderef = atoi( args );
184 case IP_TYPE_SIZELIMIT:
185 op->ldop_srch.ldsp_sizelimit = atoi( args );
187 case IP_TYPE_TIMELIMIT:
188 op->ldop_srch.ldsp_timelimit = atoi( args );
191 op->ldop_srch.ldsp_filter = estrdup( args );
193 case IP_TYPE_ATTRSONLY:
194 op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
197 if ( strcmp( args, "all" ) == 0 ) {
198 op->ldop_srch.ldsp_attrs = NULL;
200 while ( args != NULL ) {
201 if (( p = strchr( args, ' ' )) != NULL ) {
203 while ( isspace( (unsigned char) *p )) {
207 add_strval( &op->ldop_srch.ldsp_attrs, args );
215 if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
216 op->ldop_srch.ldsp_filter == NULL ) {
217 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL,
218 "Required suffix:, base:, or filter: missing" );
227 find_input_tag( char **linep ) /* linep is set to start of args */
232 if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
236 for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
237 if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
238 while ( isspace( (unsigned char) *(++p) )) {
251 add_strval( char ***sp, char *val )
258 if ( vallist == NULL ) {
261 for ( i = 0; vallist[ i ] != NULL; ++i ) {
266 vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
267 vallist[ i ] = estrdup( val );
268 vallist[ ++i ] = NULL;
278 if (( p = strdup( s )) == NULL ) {
279 debug_printf( "strdup failed\n" );
280 exit( EXIT_FAILURE );
288 erealloc( void *s, unsigned size )
295 p = realloc( s, size );
299 debug_printf( "realloc( p, %d ) failed\n", size );
300 exit( EXIT_FAILURE );
308 ecalloc( unsigned nelem, unsigned elsize )
312 if (( p = calloc( nelem, elsize )) == NULL ) {
313 debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
314 exit( EXIT_FAILURE );
325 debug_printf( const char *fmt, ... )
331 fprintf( stderr, "%s: ", progname );
332 vfprintf( stderr, fmt, ap );
339 dump_ldop( struct ldop *op )
345 debug_printf( "SEARCH operation\n" );
346 if ( op->ldop_suffixes == NULL ) {
347 debug_printf( " suffix: NONE\n" );
350 for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
351 debug_printf( " suffix: <%s>\n", op->ldop_suffixes[ i ] );
354 debug_printf( " dn: <%s>\n", op->ldop_dn );
355 debug_printf( " scope: <%d>\n", op->ldop_srch.ldsp_scope );
356 debug_printf( " filter: <%s>\n", op->ldop_srch.ldsp_filter );
357 debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
358 debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
359 debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
360 debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
361 if ( op->ldop_srch.ldsp_attrs == NULL ) {
362 debug_printf( " attrs: ALL\n" );
366 for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
367 debug_printf( " attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
371 #endif /* LDAP_DEBUG */