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