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