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.
19 #include <sys/types.h>
27 #include "shellutil.h"
33 static struct inputparams ips[] = {
34 IP_TYPE_SUFFIX, "suffix",
36 IP_TYPE_SCOPE, "scope",
37 IP_TYPE_ALIASDEREF, "deref",
38 IP_TYPE_SIZELIMIT, "sizelimit",
39 IP_TYPE_TIMELIMIT, "timelimit",
40 IP_TYPE_FILTER, "filter",
41 IP_TYPE_ATTRS, "attrs",
42 IP_TYPE_ATTRSONLY, "attrsonly",
48 write_result( FILE *fp, int code, char *matched, char *info )
50 fprintf( fp, "RESULT\ncode: %d\n", code );
51 debug_printf( ">> RESULT\n" );
52 debug_printf( ">> code: %d\n", code );
54 if ( matched != NULL ) {
55 fprintf( fp, "matched: %s\n", matched );
56 debug_printf( ">> matched: %s\n", matched );
60 fprintf( fp, "info: %s\n", info );
61 debug_printf( ">> info: %s\n", info );
67 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
72 fprintf( ofp, "dn: %s\n", entry->lde_dn );
73 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
74 if ( attr_requested( (*app)->lda_name, op )) {
75 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
76 fprintf( ofp, "%s: %s\n", (*app)->lda_name, *valp );
85 test_filter( struct ldop *op, struct ldentry *entry )
87 return (( random() & 0x07 ) == 0x07 ); /* XXX random for now */
92 attr_requested( char *name, struct ldop *op )
96 if ( op->ldop_srch.ldsp_attrs == NULL ) { /* special case */
100 for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
101 if ( strcasecmp( name, *ap ) == 0 ) {
111 free_entry( struct ldentry *entry )
116 free( entry->lde_dn );
118 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
119 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
122 free( (*app)->lda_values );
123 free( (*app)->lda_name );
126 free( entry->lde_attrs );
132 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
134 char *p, *args, line[ MAXLINELEN + 1 ];
135 struct inputparams *ip;
137 if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
138 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
140 line[ strlen( line ) - 1 ] = '\0';
141 if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
143 write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
144 "Operation Not Supported" );
148 op->ldop_op = LDOP_SEARCH;
150 while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
151 line[ strlen( line ) - 1 ] = '\0';
152 debug_printf( "<< %s\n", line );
155 if (( ip = find_input_tag( &args )) == NULL ) {
156 debug_printf( "ignoring %s\n", line );
160 switch( ip->ip_type ) {
162 add_strval( &op->ldop_suffixes, args );
165 op->ldop_dn = estrdup( args );
168 if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
169 op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
170 op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
171 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Bad scope" );
175 case IP_TYPE_ALIASDEREF:
176 op->ldop_srch.ldsp_aliasderef = atoi( args );
178 case IP_TYPE_SIZELIMIT:
179 op->ldop_srch.ldsp_sizelimit = atoi( args );
181 case IP_TYPE_TIMELIMIT:
182 op->ldop_srch.ldsp_timelimit = atoi( args );
185 op->ldop_srch.ldsp_filter = estrdup( args );
187 case IP_TYPE_ATTRSONLY:
188 op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
191 if ( strcmp( args, "all" ) == 0 ) {
192 op->ldop_srch.ldsp_attrs = NULL;
194 while ( args != NULL ) {
195 if (( p = strchr( args, ' ' )) != NULL ) {
197 while ( isspace( *p )) {
201 add_strval( &op->ldop_srch.ldsp_attrs, args );
209 if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
210 op->ldop_srch.ldsp_filter == NULL ) {
211 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL,
212 "Required suffix:, base:, or filter: missing" );
221 find_input_tag( char **linep ) /* linep is set to start of args */
226 if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
230 for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
231 if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
232 while ( isspace( *(++p) )) {
245 add_strval( char ***sp, char *val )
252 if ( vallist == NULL ) {
255 for ( i = 0; vallist[ i ] != NULL; ++i ) {
260 vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
261 vallist[ i ] = estrdup( val );
262 vallist[ ++i ] = NULL;
272 if (( p = strdup( s )) == NULL ) {
273 debug_printf( "strdup failed\n" );
282 erealloc( void *s, unsigned size )
289 p = realloc( s, size );
293 debug_printf( "realloc( p, %d ) failed\n", size );
302 ecalloc( unsigned nelem, unsigned elsize )
306 if (( p = calloc( nelem, elsize )) == NULL ) {
307 debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
319 debug_printf( va_alist /* char *fmt, args... */ )
327 fmt = va_arg( ap, char * );
328 fprintf( stderr, "%s: ", progname );
329 vfprintf( stderr, fmt, ap );
336 dump_ldop( struct ldop *op )
342 debug_printf( "SEARCH operation\n" );
343 if ( op->ldop_suffixes == NULL ) {
344 debug_printf( " suffix: NONE\n" );
347 for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
348 debug_printf( " suffix: <%s>\n", op->ldop_suffixes[ i ] );
351 debug_printf( " dn: <%s>\n", op->ldop_dn );
352 debug_printf( " scope: <%d>\n", op->ldop_srch.ldsp_scope );
353 debug_printf( " filter: <%s>\n", op->ldop_srch.ldsp_filter );
354 debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
355 debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
356 debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
357 debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
358 if ( op->ldop_srch.ldsp_attrs == NULL ) {
359 debug_printf( " attrs: ALL\n" );
363 for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
364 debug_printf( " attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
368 #endif /* LDAP_DEBUG */