1 /* shellutil.c - common routines useful when building shell-based backends */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/shell-backends/shellutil.c,v 1.17.2.3 2008/02/11 23:26:49 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
36 #include <ac/stdlib.h>
37 #include <ac/stdarg.h>
42 #include <ac/string.h>
46 #include "shellutil.h"
52 static struct inputparams ips
[] = {
53 IP_TYPE_SUFFIX
, "suffix",
55 IP_TYPE_SCOPE
, "scope",
56 IP_TYPE_ALIASDEREF
, "deref",
57 IP_TYPE_SIZELIMIT
, "sizelimit",
58 IP_TYPE_TIMELIMIT
, "timelimit",
59 IP_TYPE_FILTER
, "filter",
60 IP_TYPE_ATTRS
, "attrs",
61 IP_TYPE_ATTRSONLY
, "attrsonly",
67 write_result( FILE *fp
, int code
, char *matched
, char *info
)
69 fprintf( fp
, "RESULT\ncode: %d\n", code
);
70 debug_printf( ">> RESULT\n" );
71 debug_printf( ">> code: %d\n", code
);
73 if ( matched
!= NULL
) {
74 fprintf( fp
, "matched: %s\n", matched
);
75 debug_printf( ">> matched: %s\n", matched
);
79 fprintf( fp
, "info: %s\n", info
);
80 debug_printf( ">> info: %s\n", info
);
86 write_entry( struct ldop
*op
, struct ldentry
*entry
, FILE *ofp
)
91 fprintf( ofp
, "dn: %s\n", entry
->lde_dn
);
92 for ( app
= entry
->lde_attrs
; *app
!= NULL
; ++app
) {
93 if ( attr_requested( (*app
)->lda_name
, op
)) {
94 for ( valp
= (*app
)->lda_values
; *valp
!= NULL
; ++valp
) {
95 fprintf( ofp
, "%s: %s\n", (*app
)->lda_name
, *valp
);
104 test_filter( struct ldop
*op
, struct ldentry
*entry
)
106 return ((random() & 0x07 ) == 0x07) /* XXX random for now */
107 ? LDAP_COMPARE_TRUE
: LDAP_COMPARE_FALSE
;
112 attr_requested( char *name
, struct ldop
*op
)
116 if ( op
->ldop_srch
.ldsp_attrs
== NULL
) { /* special case */
120 for ( ap
= op
->ldop_srch
.ldsp_attrs
; *ap
!= NULL
; ++ap
) {
121 if ( strcasecmp( name
, *ap
) == 0 ) {
131 free_entry( struct ldentry
*entry
)
136 free( entry
->lde_dn
);
138 for ( app
= entry
->lde_attrs
; *app
!= NULL
; ++app
) {
139 for ( valp
= (*app
)->lda_values
; *valp
!= NULL
; ++valp
) {
142 free( (*app
)->lda_values
);
143 free( (*app
)->lda_name
);
146 free( entry
->lde_attrs
);
152 parse_input( FILE *ifp
, FILE *ofp
, struct ldop
*op
)
154 char *p
, *args
, line
[ MAXLINELEN
+ 1 ];
155 struct inputparams
*ip
;
157 if ( fgets( line
, MAXLINELEN
, ifp
) == NULL
) {
158 write_result( ofp
, LDAP_OTHER
, NULL
, "Empty Input" );
160 line
[ strlen( line
) - 1 ] = '\0';
161 if ( strncasecmp( line
, STR_OP_SEARCH
, sizeof( STR_OP_SEARCH
) - 1 )
163 write_result( ofp
, LDAP_UNWILLING_TO_PERFORM
, NULL
,
164 "Operation Not Supported" );
168 op
->ldop_op
= LDOP_SEARCH
;
170 while ( fgets( line
, MAXLINELEN
, ifp
) != NULL
) {
171 line
[ strlen( line
) - 1 ] = '\0';
172 debug_printf( "<< %s\n", line
);
175 if (( ip
= find_input_tag( &args
)) == NULL
) {
176 debug_printf( "ignoring %s\n", line
);
180 switch( ip
->ip_type
) {
182 add_strval( &op
->ldop_suffixes
, args
);
185 op
->ldop_dn
= estrdup( args
);
188 if ( lutil_atoi( &op
->ldop_srch
.ldsp_scope
, args
) != 0 ||
189 ( op
->ldop_srch
.ldsp_scope
!= LDAP_SCOPE_BASE
&&
190 op
->ldop_srch
.ldsp_scope
!= LDAP_SCOPE_ONELEVEL
&&
191 op
->ldop_srch
.ldsp_scope
!= LDAP_SCOPE_SUBTREE
) )
193 write_result( ofp
, LDAP_OTHER
, NULL
, "Bad scope" );
197 case IP_TYPE_ALIASDEREF
:
198 if ( lutil_atoi( &op
->ldop_srch
.ldsp_aliasderef
, args
) != 0 ) {
199 write_result( ofp
, LDAP_OTHER
, NULL
, "Bad alias deref" );
203 case IP_TYPE_SIZELIMIT
:
204 if ( lutil_atoi( &op
->ldop_srch
.ldsp_sizelimit
, args
) != 0 ) {
205 write_result( ofp
, LDAP_OTHER
, NULL
, "Bad size limit" );
209 case IP_TYPE_TIMELIMIT
:
210 if ( lutil_atoi( &op
->ldop_srch
.ldsp_timelimit
, args
) != 0 ) {
211 write_result( ofp
, LDAP_OTHER
, NULL
, "Bad time limit" );
216 op
->ldop_srch
.ldsp_filter
= estrdup( args
);
218 case IP_TYPE_ATTRSONLY
:
219 op
->ldop_srch
.ldsp_attrsonly
= ( *args
!= '0' );
222 if ( strcmp( args
, "all" ) == 0 ) {
223 op
->ldop_srch
.ldsp_attrs
= NULL
;
225 while ( args
!= NULL
) {
226 if (( p
= strchr( args
, ' ' )) != NULL
) {
228 while ( isspace( (unsigned char) *p
)) {
232 add_strval( &op
->ldop_srch
.ldsp_attrs
, args
);
240 if ( op
->ldop_suffixes
== NULL
|| op
->ldop_dn
== NULL
||
241 op
->ldop_srch
.ldsp_filter
== NULL
) {
242 write_result( ofp
, LDAP_OTHER
, NULL
,
243 "Required suffix:, base:, or filter: missing" );
252 find_input_tag( char **linep
) /* linep is set to start of args */
257 if (( p
= strchr( *linep
, ':' )) == NULL
|| p
== *linep
) {
261 for ( i
= 0; ips
[ i
].ip_type
!= 0; ++i
) {
262 if ( strncasecmp( *linep
, ips
[ i
].ip_tag
, p
- *linep
) == 0 ) {
263 while ( isspace( (unsigned char) *(++p
) )) {
276 add_strval( char ***sp
, char *val
)
283 if ( vallist
== NULL
) {
286 for ( i
= 0; vallist
[ i
] != NULL
; ++i
) {
291 vallist
= (char **)erealloc( vallist
, ( i
+ 2 ) * sizeof( char * ));
292 vallist
[ i
] = estrdup( val
);
293 vallist
[ ++i
] = NULL
;
303 if (( p
= strdup( s
)) == NULL
) {
304 debug_printf( "strdup failed\n" );
305 exit( EXIT_FAILURE
);
313 erealloc( void *s
, unsigned size
)
320 p
= realloc( s
, size
);
324 debug_printf( "realloc( p, %d ) failed\n", size
);
325 exit( EXIT_FAILURE
);
333 ecalloc( unsigned nelem
, unsigned elsize
)
337 if (( p
= calloc( nelem
, elsize
)) == NULL
) {
338 debug_printf( "calloc( %d, %d ) failed\n", nelem
, elsize
);
339 exit( EXIT_FAILURE
);
350 debug_printf( const char *fmt
, ... )
356 fprintf( stderr
, "%s: ", progname
);
357 vfprintf( stderr
, fmt
, ap
);
364 dump_ldop( struct ldop
*op
)
370 debug_printf( "SEARCH operation\n" );
371 if ( op
->ldop_suffixes
== NULL
) {
372 debug_printf( " suffix: NONE\n" );
375 for ( i
= 0; op
->ldop_suffixes
[ i
] != NULL
; ++i
) {
376 debug_printf( " suffix: <%s>\n", op
->ldop_suffixes
[ i
] );
379 debug_printf( " dn: <%s>\n", op
->ldop_dn
);
380 debug_printf( " scope: <%d>\n", op
->ldop_srch
.ldsp_scope
);
381 debug_printf( " filter: <%s>\n", op
->ldop_srch
.ldsp_filter
);
382 debug_printf( "aliasderef: <%d>\n", op
->ldop_srch
.ldsp_aliasderef
);
383 debug_printf( " sizelimit: <%d>\n", op
->ldop_srch
.ldsp_sizelimit
);
384 debug_printf( " timelimit: <%d>\n", op
->ldop_srch
.ldsp_timelimit
);
385 debug_printf( " attrsonly: <%d>\n", op
->ldop_srch
.ldsp_attrsonly
);
386 if ( op
->ldop_srch
.ldsp_attrs
== NULL
) {
387 debug_printf( " attrs: ALL\n" );
391 for ( i
= 0; op
->ldop_srch
.ldsp_attrs
[ i
] != NULL
; ++i
) {
392 debug_printf( " attrs: <%s>\n", op
->ldop_srch
.ldsp_attrs
[ i
] );
396 #endif /* LDAP_DEBUG */