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