]> git.sur5r.net Git - openldap/blob - servers/slapd/shell-backends/shellutil.c
Fix pkiUser
[openldap] / servers / slapd / shell-backends / shellutil.c
1 /*
2  shellutil.c - common routines useful when building shell-based backends
3                  for the standalone ldap server
4
5  Copyright (c) 1995 Regents of the University of Michigan.
6  All rights reserved.
7
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.
14 */
15
16
17 #include <sys/types.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <pwd.h>
22 #include <varargs.h>
23 #include <lber.h>
24 #include <ldap.h>
25 #include "shellutil.h"
26
27
28 int     debugflg;
29 char    *progname;
30
31 static struct inputparams       ips[] = {
32     IP_TYPE_SUFFIX,     "suffix",
33     IP_TYPE_BASE,       "base",
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",
41     0,                  NULL
42 };
43
44
45 void
46 write_result( FILE *fp, int code, char *matched, char *info )
47 {
48     fprintf( fp, "RESULT\ncode: %d\n", code );
49     debug_printf( ">> RESULT\n" );
50     debug_printf( ">> code: %d\n", code );
51
52     if ( matched != NULL ) {
53         fprintf( fp, "matched: %s\n", matched );
54         debug_printf( ">> matched: %s\n", matched );
55     }
56
57     if ( info != NULL ) {
58         fprintf( fp, "info: %s\n", info );
59         debug_printf( ">> info: %s\n", info );
60     }
61 }
62
63
64 void
65 write_entry( struct ldop *op, struct ldentry *entry, FILE *ofp )
66 {
67     struct ldattr       **app;
68     char                **valp;
69
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 );
75             }
76         }
77     }
78     fputc( '\n', ofp );
79 }
80
81
82 int
83 test_filter( struct ldop *op, struct ldentry *entry )
84 {
85     return (( random() & 0x07 ) == 0x07 );      /* XXX random for now */
86 }
87
88
89 int
90 attr_requested( char *name, struct ldop *op )
91 {
92     char        **ap;
93
94     if ( op->ldop_srch.ldsp_attrs == NULL ) {   /* special case */
95         return( 1 );
96     }
97
98     for ( ap = op->ldop_srch.ldsp_attrs; *ap != NULL; ++ap ) {
99         if ( strcasecmp( name, *ap ) == 0 ) {
100             return( 1 );
101         }
102     }
103
104     return( 0 );
105 }
106
107
108 void
109 free_entry( struct ldentry *entry )
110 {
111     struct ldattr       **app;
112     char                **valp;
113
114     free( entry->lde_dn );
115
116     for ( app = entry->lde_attrs; *app != NULL; ++app ) {
117         for ( valp = (*app)->lda_values; *valp != NULL; ++valp ) {
118             free( *valp );
119         }
120         free( (*app)->lda_values );
121         free( (*app)->lda_name );
122     }
123
124     free( entry->lde_attrs );
125     free( entry );
126 }
127
128
129 int
130 parse_input( FILE *ifp, FILE *ofp, struct ldop *op )
131 {
132     char                *p, *args, line[ MAXLINELEN + 1 ];
133     struct inputparams  *ip;
134
135     if ( fgets( line, MAXLINELEN, ifp ) == NULL ) {
136         write_result( ofp, LDAP_OPERATIONS_ERROR, NULL, "Empty Input" );
137     }
138     line[ strlen( line ) - 1 ] = '\0';
139     if ( strncasecmp( line, STR_OP_SEARCH, sizeof( STR_OP_SEARCH ) - 1 )
140             != 0 ) {
141         write_result( ofp, LDAP_UNWILLING_TO_PERFORM, NULL,
142                 "Operation Not Supported" );
143         return( -1 );
144     }
145
146     op->ldop_op = LDOP_SEARCH;
147
148     while ( fgets( line, MAXLINELEN, ifp ) != NULL ) {
149         line[ strlen( line ) - 1 ] = '\0';
150         debug_printf( "<< %s\n", line );
151
152         args = line;
153         if (( ip = find_input_tag( &args )) == NULL ) {
154             debug_printf( "ignoring %s\n", line );
155             continue;
156         }
157
158         switch( ip->ip_type ) {
159         case IP_TYPE_SUFFIX:
160             add_strval( &op->ldop_suffixes, args );
161             break;
162         case IP_TYPE_BASE:
163             op->ldop_dn = estrdup( args );
164             break;
165         case IP_TYPE_SCOPE:
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" );
170                 return( -1 );
171             }
172             break;
173         case IP_TYPE_ALIASDEREF:
174             op->ldop_srch.ldsp_aliasderef = atoi( args );
175             break;
176         case IP_TYPE_SIZELIMIT:
177             op->ldop_srch.ldsp_sizelimit = atoi( args );
178             break;
179         case IP_TYPE_TIMELIMIT:
180             op->ldop_srch.ldsp_timelimit = atoi( args );
181             break;
182         case IP_TYPE_FILTER:
183             op->ldop_srch.ldsp_filter = estrdup( args );
184             break;
185         case IP_TYPE_ATTRSONLY:
186             op->ldop_srch.ldsp_attrsonly = ( *args != '0' );
187             break;
188         case IP_TYPE_ATTRS:
189             if ( strcmp( args, "all" ) == 0 ) {
190                 op->ldop_srch.ldsp_attrs = NULL;
191             } else {
192                 while ( args != NULL ) {
193                     if (( p = strchr( args, ' ' )) != NULL ) {
194                         *p++ = '\0';
195                         while ( isspace( *p )) {
196                             ++p;
197                         }
198                     }
199                     add_strval( &op->ldop_srch.ldsp_attrs, args );
200                     args = p;
201                 }
202             }
203             break;
204         }
205     }
206
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" );
211         return( -1 );
212     }
213
214     return( 0 );
215 }
216
217
218 struct inputparams *
219 find_input_tag( char **linep )  /* linep is set to start of args */
220 {
221     int         i;
222     char        *p;
223
224     if (( p = strchr( *linep, ':' )) == NULL || p == *linep ) {
225         return( NULL );
226     }
227
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) )) {
231                 ;
232             }
233             *linep = p;
234             return( &ips[ i ] );
235         }
236     }
237
238     return( NULL );
239 }
240
241
242 void
243 add_strval( char ***sp, char *val )
244 {
245     int         i;
246     char        **vallist;
247
248     vallist = *sp;
249
250     if ( vallist == NULL ) {
251         i = 0;
252     } else {
253         for ( i = 0; vallist[ i ] != NULL; ++i ) {
254             ;
255         }
256     }
257
258     vallist = (char **)erealloc( vallist, ( i + 2 ) * sizeof( char * ));
259     vallist[ i ] = estrdup( val );
260     vallist[ ++i ] = NULL;
261     *sp = vallist;
262 }
263
264
265 char *
266 estrdup( char *s )
267 {
268     char        *p;
269
270     if (( p = strdup( s )) == NULL ) {
271         debug_printf( "strdup failed\n" );
272         exit( 1 );
273     }
274
275     return( p );
276 }
277
278
279 void *
280 erealloc( void *s, unsigned size )
281 {
282     char        *p;
283
284     if ( s == NULL ) {
285         p = malloc( size );
286     } else {
287         p = realloc( s, size );
288     }
289
290     if ( p == NULL ) {
291         debug_printf( "realloc( p, %d ) failed\n", size );
292         exit( 1 );
293     }
294
295     return( p );
296 }
297
298
299 char *
300 ecalloc( unsigned nelem, unsigned elsize )
301 {
302     char        *p;
303
304     if (( p = calloc( nelem, elsize )) == NULL ) {
305         debug_printf( "calloc( %d, %d ) failed\n", nelem, elsize );
306         exit( 1 );
307     }
308
309     return( p );
310 }
311
312
313 #ifdef LDAP_DEBUG
314
315 /* VARARGS */
316 void
317 debug_printf( va_alist /* char *fmt, args... */ )
318     va_dcl
319 {
320     char        *fmt;
321     va_list     ap;
322
323     if ( debugflg ) {
324         va_start( ap );
325         fmt = va_arg( ap, char * );
326         fprintf( stderr, "%s: ", progname );
327         vfprintf( stderr, fmt, ap );
328         va_end( ap );
329     }
330 }
331
332
333 void
334 dump_ldop( struct ldop *op )
335 {
336     if ( !debugflg ) {
337         return;
338     }
339
340     debug_printf( "SEARCH operation\n" );
341     if ( op->ldop_suffixes == NULL ) {
342         debug_printf( "    suffix: NONE\n" );
343     } else {
344         int     i;
345         for ( i = 0; op->ldop_suffixes[ i ] != NULL; ++i ) {
346             debug_printf( "    suffix: <%s>\n", op->ldop_suffixes[ i ] );
347         }
348     }
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" );
358     } else {
359         int     i;
360
361         for ( i = 0; op->ldop_srch.ldsp_attrs[ i ] != NULL; ++i ) {
362             debug_printf( "  attrs: <%s>\n", op->ldop_srch.ldsp_attrs[ i ] );
363         }
364     }
365 }
366 #endif /* LDAP_DEBUG */