]> git.sur5r.net Git - openldap/blob - libraries/libldap/tmpltest.c
s/<stdlib.h>/<ac/stdlib.h>/
[openldap] / libraries / libldap / tmpltest.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <stdio.h>
8
9 #include <ac/stdlib.h>
10
11 #include <ac/socket.h>
12 #include <ac/time.h>
13
14 #ifdef HAVE_CONSOLE_H
15 #include <console.h>
16 #endif /* MACOS */
17
18 #include "lber.h"
19 #include "ldap.h"
20 #include "disptmpl.h"
21 #include "srchpref.h"
22
23 static void dump_tmpl    ( struct ldap_disptmpl *tmpl );
24 static void dump_srchpref( struct ldap_searchobj *sp );
25
26 #define NULLSTRINGIFNULL( s )   ( (s) == NULL ? "(null)" : (s) )
27
28 int
29 main( int argc, char **argv )
30 {
31     struct ldap_disptmpl        *templates, *dtp;
32     struct ldap_searchobj       *so, *sop;
33     int                         err;
34
35 #ifdef HAVE_CONSOLE_H
36         ccommand( &argv );
37         for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
38             ;
39         }
40         cshow( stdout );
41 #endif /* MACOS */
42
43     if (( err = ldap_init_templates( "ldaptemplates.conf", &templates ))
44             != 0 ) {
45         fprintf( stderr, "ldap_init_templates failed (%d)\n", err );
46         exit( 1 );
47     }
48
49     if (( err = ldap_init_searchprefs( "ldapsearchprefs.conf", &so ))
50             != 0 ) {
51         fprintf( stderr, "ldap_init_searchprefs failed (%d)\n", err );
52         exit( 1 );
53     }
54
55     if ( argc == 1 ) {
56         printf( "*** Display Templates:\n" );
57         for ( dtp = ldap_first_disptmpl( templates ); dtp != NULL;
58                 dtp = ldap_next_disptmpl( templates, dtp )) {
59             dump_tmpl( dtp );
60             printf( "\n\n" );
61         }
62
63         printf( "\n\n*** Search Objects:\n" );
64         for ( sop = ldap_first_searchobj( so ); sop != NULL;
65                     sop = ldap_next_searchobj( so, sop )) {
66             dump_srchpref( sop );
67             printf( "\n\n" );
68         }
69
70     } else {
71         if (( dtp = ldap_oc2template( ++argv, templates )) == NULL ) {
72             fprintf( stderr, "no matching template found\n" );
73         } else {
74             dump_tmpl( dtp );
75         }
76     }
77
78
79     ldap_free_templates( templates );
80     ldap_free_searchprefs( so );
81
82     exit( 0 );
83 }
84
85
86 static const char *const syn_name[] = {
87     "?", "CIS", "MLS", "DN", "BOOL", "JPEG", "JPEGBTN", "FAX", "FAXBTN",
88     "AUDIOBTN", "TIME", "DATE", "URL", "SEARCHACT", "LINKACT", "ADDDNACT",
89     "VERIFYACT",
90 };
91
92 static const char *const syn_type[] = {
93     "?", "txt", "img", "?", "bool", "?", "?", "?", "btn",
94     "?", "?", "?", "?", "?", "?", "?",
95     "action", "?"
96 };
97
98 static char *includeattrs[] = { "objectClass", "sn", NULL };
99
100 static const char *const item_opts[] = {
101     "ro", "sort", "1val", "hide", "required", "hideiffalse", NULL
102 };
103
104 static const unsigned long item_opt_vals[] = {
105     LDAP_DITEM_OPT_READONLY,            LDAP_DITEM_OPT_SORTVALUES,
106     LDAP_DITEM_OPT_SINGLEVALUED,        LDAP_DITEM_OPT_HIDEIFEMPTY,
107     LDAP_DITEM_OPT_VALUEREQUIRED,       LDAP_DITEM_OPT_HIDEIFFALSE,
108 };
109
110
111 void
112 dump_tmpl( struct ldap_disptmpl *tmpl )
113 {
114     struct ldap_tmplitem        *rowp, *colp;
115     int                         i, rowcnt, colcnt;
116     char                        **fetchattrs;
117     struct ldap_oclist          *ocp;
118     struct ldap_adddeflist      *adp;
119
120     printf( "** Template \"%s\" (plural \"%s\", icon \"%s\")\n",
121             NULLSTRINGIFNULL( tmpl->dt_name ),
122             NULLSTRINGIFNULL( tmpl->dt_pluralname ),
123             NULLSTRINGIFNULL( tmpl->dt_iconname ));
124
125     printf( "object class list:\n" );
126     for ( ocp = tmpl->dt_oclist; ocp != NULL; ocp = ocp->oc_next ) {
127         for ( i = 0; ocp->oc_objclasses[ i ] != NULL; ++i ) {
128             printf( "%s%s", i == 0 ? "  " : " & ",
129                     NULLSTRINGIFNULL( ocp->oc_objclasses[ i ] ));
130         }
131         putchar( '\n' );
132     }
133     putchar( '\n' );
134
135     printf( "template options:          " );
136     if ( tmpl->dt_options == 0L ) {
137         printf( "NONE\n" );
138     } else {
139         printf( "%s %s %s\n", LDAP_IS_DISPTMPL_OPTION_SET( tmpl,
140                 LDAP_DTMPL_OPT_ADDABLE ) ? "addable" : "",
141                 LDAP_IS_DISPTMPL_OPTION_SET( tmpl, LDAP_DTMPL_OPT_ALLOWMODRDN )
142                 ? "modrdn" : "",
143                 LDAP_IS_DISPTMPL_OPTION_SET( tmpl, LDAP_DTMPL_OPT_ALTVIEW )
144                 ? "altview" : "" );
145     }
146
147     printf( "authenticate as attribute: %s\n", tmpl->dt_authattrname != NULL ?
148             tmpl->dt_authattrname : "<default>" );
149
150     printf( "default RDN attribute:     %s\n", tmpl->dt_defrdnattrname != NULL ?
151             tmpl->dt_defrdnattrname : "NONE" );
152
153     printf( "default add location:      %s\n", tmpl->dt_defaddlocation != NULL ?
154             tmpl->dt_defaddlocation : "NONE" );
155
156     printf( "\nnew entry value default rules:\n" );
157     for ( adp = tmpl->dt_adddeflist; adp != NULL; adp = adp->ad_next ) {
158         if ( adp->ad_source == LDAP_ADSRC_CONSTANTVALUE ) {
159             printf( "  attribute %s <-- constant value \"%s\"\n",
160                 NULLSTRINGIFNULL( adp->ad_attrname),
161                 NULLSTRINGIFNULL( adp->ad_value ));
162         } else {
163             printf( "  attribute %s <-- adder's DN\n",
164                     NULLSTRINGIFNULL( adp->ad_attrname ));
165         }
166     }
167     putchar( '\n' );
168
169     printf( "\nfetch attributes & values:\n" );
170     if (( fetchattrs = ldap_tmplattrs( tmpl, includeattrs, 1,
171                 LDAP_SYN_OPT_DEFER )) == NULL ) {
172         printf( "  <none>\n" );
173     } else {
174         for ( i = 0; fetchattrs[ i ] != NULL; ++i ) {
175             printf( "  %s\n", fetchattrs[ i ] );
176             free( fetchattrs[ i ] );
177         }
178         free( (char *)fetchattrs );
179     }
180
181     printf( "\nfetch attributes only:\n" );
182     if (( fetchattrs = ldap_tmplattrs( tmpl, NULL, 0,
183                 LDAP_SYN_OPT_DEFER )) == NULL ) {
184         printf( "  <none>\n" );
185     } else {
186         for ( i = 0; fetchattrs[ i ] != NULL; ++i ) {
187             printf( "  %s\n", fetchattrs[ i ] );
188             free( fetchattrs[ i ] );
189         }
190         free( (char *)fetchattrs );
191     }
192
193     printf( "\ntemplate items:\n" );
194     rowcnt = 0;
195     for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULL;
196             rowp = ldap_next_tmplrow( tmpl, rowp )) {
197         ++rowcnt;
198         colcnt = 0;
199         for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
200                 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
201             ++colcnt;
202             printf( "  %2d-%d: %s (%s%s", rowcnt, colcnt,
203                 syn_name[ colp->ti_syntaxid & 0x0000FFFF ],
204                 syn_type[ LDAP_GET_SYN_TYPE( colp->ti_syntaxid ) >> 24 ],
205                 (( LDAP_GET_SYN_OPTIONS( colp->ti_syntaxid ) &
206                 LDAP_SYN_OPT_DEFER ) != 0 ) ? ",defer" : "" );
207
208             for ( i = 0; item_opts[ i ] != NULL; ++i ) {
209                 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp, item_opt_vals[ i ] )) {
210                     printf( ",%s", NULLSTRINGIFNULL( item_opts[ i ] ));
211                 }
212             }
213
214             printf( "), %s, %s", NULLSTRINGIFNULL( colp->ti_attrname ),
215                     NULLSTRINGIFNULL( colp->ti_label ));
216             if ( colp->ti_args != NULL ) {
217                 printf( ",args=" );
218                 for ( i = 0; colp->ti_args[ i ] != NULL; ++i ) {
219                     printf( "<%s>", NULLSTRINGIFNULL( colp->ti_args[ i ] ));
220                 }
221             }
222
223             putchar( '\n' );
224         }
225     }
226 }
227
228
229 void
230 dump_srchpref( struct ldap_searchobj *so )
231 {
232     int i;
233     struct ldap_searchattr *sa;
234     struct ldap_searchmatch *sm;
235
236     printf( "Object type prompt:  %s\n",
237             NULLSTRINGIFNULL( so->so_objtypeprompt ));
238     printf( "Options:             %s\n",
239             LDAP_IS_SEARCHOBJ_OPTION_SET( so, LDAP_SEARCHOBJ_OPT_INTERNAL ) ?
240             "internal" : "NONE" );
241     printf( "Prompt:              %s\n", NULLSTRINGIFNULL( so->so_prompt ));
242     printf( "Scope:               " );
243     switch ( so->so_defaultscope ) {
244     case LDAP_SCOPE_BASE:
245         printf( "LDAP_SCOPE_BASE" );
246         break;
247     case LDAP_SCOPE_ONELEVEL:
248         printf( "LDAP_SCOPE_ONELEVEL" );
249         break;
250     case LDAP_SCOPE_SUBTREE:
251         printf( "LDAP_SCOPE_SUBTREE" );
252         break;
253     default:
254         printf("*** unknown!" );
255     }
256     puts( "\n" );
257     printf( "Filter prefix:       %s\n",
258             NULLSTRINGIFNULL( so->so_filterprefix ));
259     printf( "Filter tag:          %s\n",
260             NULLSTRINGIFNULL( so->so_filtertag ));
261     printf( "Default select attr: %s\n",
262             NULLSTRINGIFNULL( so->so_defaultselectattr ));
263     printf( "Default select text: %s\n",
264             NULLSTRINGIFNULL( so->so_defaultselecttext ));
265     printf( "Searchable attributes ---- \n" );
266     for ( sa = so->so_salist; sa != NULL; sa = sa->sa_next ) {
267         printf( "  Label: %s\n", NULLSTRINGIFNULL( sa->sa_attrlabel ));
268         printf( "  Attribute: %s\n", NULLSTRINGIFNULL( sa->sa_attr ));
269         printf( "  Select attr: %s\n", NULLSTRINGIFNULL( sa->sa_selectattr ));
270         printf( "  Select text: %s\n", NULLSTRINGIFNULL( sa->sa_selecttext ));
271         printf( "  Match types ---- \n" );
272         for ( i = 0, sm = so->so_smlist; sm != NULL; i++, sm = sm->sm_next ) {
273             if (( sa->sa_matchtypebitmap >> i ) & 1 ) {
274                 printf( "    %s (%s)\n",
275                         NULLSTRINGIFNULL( sm->sm_matchprompt ),
276                         NULLSTRINGIFNULL( sm->sm_filter ));
277             }
278         }
279     }
280 }