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>
32 extern long int random (void);
36 #include "shellutil.h"
42 static struct inputparams ips[] = {
43 IP_TYPE_SUFFIX, "suffix",
45 IP_TYPE_SCOPE, "scope",
46 IP_TYPE_ALIASDEREF, "deref",
47 IP_TYPE_SIZELIMIT, "sizelimit",
48 IP_TYPE_TIMELIMIT, "timelimit",
49 IP_TYPE_FILTER, "filter",
50 IP_TYPE_ATTRS, "attrs",
51 IP_TYPE_ATTRSONLY, "attrsonly",
57 write_result( FILE *fp, int code, char *matched, char *info )
59 fprintf( fp, "RESULT\ncode: %d\n", code );
60 debug_printf( ">> RESULT\n" );
61 debug_printf( ">> code: %d\n", code );
63 if ( matched != NULL ) {
64 fprintf( fp, "matched: %s\n", matched );
65 debug_printf( ">> matched: %s\n", matched );
69 fprintf( fp, "info: %s\n", info );
70 debug_printf( ">> info: %s\n", info );
76 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
81 fprintf( ofp, "dn: %s\n", entry->lde_dn );
82 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
83 if ( attr_requested( (*app)->lda_name, op )) {
84 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
85 fprintf( ofp, "%s: %s\n", (*app)->lda_name, *valp );
94 test_filter( struct ldop *op, struct ldentry *entry )
96 return (( random() & 0x07 ) == 0x07 ); /* XXX random for now */
101 attr_requested( char *name, struct ldop *op )
105 if ( op->ldop_srch.ldsp_attrs == NULL ) { /* special case */
109 for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
110 if ( strcasecmp( name, *ap ) == 0 ) {
120 free_entry( struct ldentry *entry )
125 free( entry->lde_dn );
127 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
128 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
131 free( (*app)->lda_values );
132 free( (*app)->lda_name );
135 free( entry->lde_attrs );
141 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
143 char *p, *args, line[ MAXLINELEN + 1 ];
144 struct inputparams *ip;
146 if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
147 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
149 line[ strlen( line ) - 1 ] = '\0';
150 if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
152 write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
153 "Operation Not Supported" );
157 op->ldop_op = LDOP_SEARCH;
159 while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
160 line[ strlen( line ) - 1 ] = '\0';
161 debug_printf( "<< %s\n", line );
164 if (( ip = find_input_tag( &args )) == NULL ) {
165 debug_printf( "ignoring %s\n", line );
169 switch( ip->ip_type ) {
171 add_strval( &op->ldop_suffixes, args );
174 op->ldop_dn = estrdup( args );
177 if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
178 op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
179 op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
180 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Bad scope" );
184 case IP_TYPE_ALIASDEREF:
185 op->ldop_srch.ldsp_aliasderef = atoi( args );
187 case IP_TYPE_SIZELIMIT:
188 op->ldop_srch.ldsp_sizelimit = atoi( args );
190 case IP_TYPE_TIMELIMIT:
191 op->ldop_srch.ldsp_timelimit = atoi( args );
194 op->ldop_srch.ldsp_filter = estrdup( args );
196 case IP_TYPE_ATTRSONLY:
197 op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
200 if ( strcmp( args, "all" ) == 0 ) {
201 op->ldop_srch.ldsp_attrs = NULL;
203 while ( args != NULL ) {
204 if (( p = strchr( args, ' ' )) != NULL ) {
206 while ( isspace( *p )) {
210 add_strval( &op->ldop_srch.ldsp_attrs, args );
218 if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
219 op->ldop_srch.ldsp_filter == NULL ) {
220 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL,
221 "Required suffix:, base:, or filter: missing" );
230 find_input_tag( char **linep ) /* linep is set to start of args */
235 if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
239 for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
240 if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
241 while ( isspace( *(++p) )) {
254 add_strval( char ***sp, char *val )
261 if ( vallist == NULL ) {
264 for ( i = 0; vallist[ i ] != NULL; ++i ) {
269 vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
270 vallist[ i ] = estrdup( val );
271 vallist[ ++i ] = NULL;
281 if (( p = strdup( s )) == NULL ) {
282 debug_printf( "strdup failed\n" );
291 erealloc( void *s, unsigned size )
298 p = realloc( s, size );
302 debug_printf( "realloc( p, %d ) failed\n", size );
311 ecalloc( unsigned nelem, unsigned elsize )
315 if (( p = calloc( nelem, elsize )) == NULL ) {
316 debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
346 fmt = va_arg( ap, char * );
348 fprintf( stderr, "%s: ", progname );
349 vfprintf( stderr, fmt, ap );
356 dump_ldop( struct ldop *op )
362 debug_printf( "SEARCH operation\n" );
363 if ( op->ldop_suffixes == NULL ) {
364 debug_printf( " suffix: NONE\n" );
367 for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
368 debug_printf( " suffix: <%s>\n", op->ldop_suffixes[ i ] );
371 debug_printf( " dn: <%s>\n", op->ldop_dn );
372 debug_printf( " scope: <%d>\n", op->ldop_srch.ldsp_scope );
373 debug_printf( " filter: <%s>\n", op->ldop_srch.ldsp_filter );
374 debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
375 debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
376 debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
377 debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
378 if ( op->ldop_srch.ldsp_attrs == NULL ) {
379 debug_printf( " attrs: ALL\n" );
383 for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
384 debug_printf( " attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
388 #endif /* LDAP_DEBUG */