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.
17 #include <sys/types.h>
25 #include "shellutil.h"
31 static struct inputparams ips[] = {
32 IP_TYPE_SUFFIX, "suffix",
34 IP_TYPE_SCOPE, "scope",
35 IP_TYPE_ALIASDEREF, "deref",
36 IP_TYPE_SIZELIMIT, "sizelimit",
37 IP_TYPE_TIMELIMIT, "timelimit",
38 IP_TYPE_FILTER, "filter",
39 IP_TYPE_ATTRS, "attrs",
40 IP_TYPE_ATTRSONLY, "attrsonly",
46 write_result( FILE *fp, int code, char *matched, char *info )
48 fprintf( fp, "RESULT\ncode: %d\n", code );
49 debug_printf( ">> RESULT\n" );
50 debug_printf( ">> code: %d\n", code );
52 if ( matched != NULL ) {
53 fprintf( fp, "matched: %s\n", matched );
54 debug_printf( ">> matched: %s\n", matched );
58 fprintf( fp, "info: %s\n", info );
59 debug_printf( ">> info: %s\n", info );
65 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
70 fprintf( ofp, "dn: %s\n", entry->lde_dn );
71 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
72 if ( attr_requested( (*app)->lda_name, op )) {
73 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
74 fprintf( ofp, "%s: %s\n", (*app)->lda_name, *valp );
83 test_filter( struct ldop *op, struct ldentry *entry )
85 return (( random() & 0x07 ) == 0x07 ); /* XXX random for now */
90 attr_requested( char *name, struct ldop *op )
94 if ( op->ldop_srch.ldsp_attrs == NULL ) { /* special case */
98 for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
99 if ( strcasecmp( name, *ap ) == 0 ) {
109 free_entry( struct ldentry *entry )
114 free( entry->lde_dn );
116 for ( app = entry->lde_attrs; *app != NULL; ++app ) {
117 for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
120 free( (*app)->lda_values );
121 free( (*app)->lda_name );
124 free( entry->lde_attrs );
130 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
132 char *p, *args, line[ MAXLINELEN + 1 ];
133 struct inputparams *ip;
135 if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
136 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
138 line[ strlen( line ) - 1 ] = '\0';
139 if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
141 write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
142 "Operation Not Supported" );
146 op->ldop_op = LDOP_SEARCH;
148 while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
149 line[ strlen( line ) - 1 ] = '\0';
150 debug_printf( "<< %s\n", line );
153 if (( ip = find_input_tag( &args )) == NULL ) {
154 debug_printf( "ignoring %s\n", line );
158 switch( ip->ip_type ) {
160 add_strval( &op->ldop_suffixes, args );
163 op->ldop_dn = estrdup( args );
166 if (( op->ldop_srch.ldsp_scope = atoi( args )) != LDAP_SCOPE_BASE &&
167 op->ldop_srch.ldsp_scope != LDAP_SCOPE_ONELEVEL &&
168 op->ldop_srch.ldsp_scope != LDAP_SCOPE_SUBTREE ) {
169 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Bad scope" );
173 case IP_TYPE_ALIASDEREF:
174 op->ldop_srch.ldsp_aliasderef = atoi( args );
176 case IP_TYPE_SIZELIMIT:
177 op->ldop_srch.ldsp_sizelimit = atoi( args );
179 case IP_TYPE_TIMELIMIT:
180 op->ldop_srch.ldsp_timelimit = atoi( args );
183 op->ldop_srch.ldsp_filter = estrdup( args );
185 case IP_TYPE_ATTRSONLY:
186 op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
189 if ( strcmp( args, "all" ) == 0 ) {
190 op->ldop_srch.ldsp_attrs = NULL;
192 while ( args != NULL ) {
193 if (( p = strchr( args, ' ' )) != NULL ) {
195 while ( isspace( *p )) {
199 add_strval( &op->ldop_srch.ldsp_attrs, args );
207 if ( op->ldop_suffixes == NULL || op->ldop_dn == NULL ||
208 op->ldop_srch.ldsp_filter == NULL ) {
209 write_result( ofp, LDAP_OPERATIONS_ERROR, NULL,
210 "Required suffix:, base:, or filter: missing" );
219 find_input_tag( char **linep ) /* linep is set to start of args */
224 if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
228 for ( i = 0; ips[ i ].ip_type != 0; ++i ) {
229 if ( strncasecmp( *linep, ips[ i ].ip_tag, p - *linep ) == 0 ) {
230 while ( isspace( *(++p) )) {
243 add_strval( char ***sp, char *val )
250 if ( vallist == NULL ) {
253 for ( i = 0; vallist[ i ] != NULL; ++i ) {
258 vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
259 vallist[ i ] = estrdup( val );
260 vallist[ ++i ] = NULL;
270 if (( p = strdup( s )) == NULL ) {
271 debug_printf( "strdup failed\n" );
280 erealloc( void *s, unsigned size )
287 p = realloc( s, size );
291 debug_printf( "realloc( p, %d ) failed\n", size );
300 ecalloc( unsigned nelem, unsigned elsize )
304 if (( p = calloc( nelem, elsize )) == NULL ) {
305 debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
317 debug_printf( va_alist /* char *fmt, args... */ )
325 fmt = va_arg( ap, char * );
326 fprintf( stderr, "%s: ", progname );
327 vfprintf( stderr, fmt, ap );
334 dump_ldop( struct ldop *op )
340 debug_printf( "SEARCH operation\n" );
341 if ( op->ldop_suffixes == NULL ) {
342 debug_printf( " suffix: NONE\n" );
345 for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
346 debug_printf( " suffix: <%s>\n", op->ldop_suffixes[ i ] );
349 debug_printf( " dn: <%s>\n", op->ldop_dn );
350 debug_printf( " scope: <%d>\n", op->ldop_srch.ldsp_scope );
351 debug_printf( " filter: <%s>\n", op->ldop_srch.ldsp_filter );
352 debug_printf( "aliasderef: <%d>\n", op->ldop_srch.ldsp_aliasderef );
353 debug_printf( " sizelimit: <%d>\n", op->ldop_srch.ldsp_sizelimit );
354 debug_printf( " timelimit: <%d>\n", op->ldop_srch.ldsp_timelimit );
355 debug_printf( " attrsonly: <%d>\n", op->ldop_srch.ldsp_attrsonly );
356 if ( op->ldop_srch.ldsp_attrs == NULL ) {
357 debug_printf( " attrs: ALL\n" );
361 for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
362 debug_printf( " attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
366 #endif /* LDAP_DEBUG */