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