xref: /onnv-gate/usr/src/cmd/ldap/common/tmpltest.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  *
3*3857Sstevel  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4*3857Sstevel  * Use is subject to license terms.
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  */
70Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
80Sstevel@tonic-gate #include <stdio.h>
90Sstevel@tonic-gate #include <sys/types.h>
100Sstevel@tonic-gate #include "lber.h"
110Sstevel@tonic-gate #include "ldap.h"
120Sstevel@tonic-gate #include "disptmpl.h"
130Sstevel@tonic-gate #include "srchpref.h"
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #ifdef MACOS
160Sstevel@tonic-gate #include <stdlib.h>
170Sstevel@tonic-gate #include <console.h>
180Sstevel@tonic-gate #endif /* MACOS */
190Sstevel@tonic-gate 
200Sstevel@tonic-gate #ifdef NEEDPROTOS
210Sstevel@tonic-gate void dump_tmpl( struct ldap_disptmpl *tmpl );
220Sstevel@tonic-gate void dump_srchpref( struct ldap_searchobj *sp );
230Sstevel@tonic-gate #else /* NEEDPROTOS */
240Sstevel@tonic-gate void dump_tmpl();
250Sstevel@tonic-gate void dump_srchpref();
260Sstevel@tonic-gate #endif /* NEEDPROTOS */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #define NULLSTRINGIFNULL( s )	( s == NULL ? "(null)" : s )
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate int
main(int argc,char ** argv)330Sstevel@tonic-gate main( int argc, char **argv )
340Sstevel@tonic-gate {
350Sstevel@tonic-gate     struct ldap_disptmpl	*templates, *dtp;
360Sstevel@tonic-gate     struct ldap_searchobj	*so, *sop;
370Sstevel@tonic-gate     int				err;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #ifdef MACOS
400Sstevel@tonic-gate 	ccommand( &argv );
410Sstevel@tonic-gate 	for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
420Sstevel@tonic-gate 	    ;
430Sstevel@tonic-gate 	}
440Sstevel@tonic-gate 	cshow( stdout );
450Sstevel@tonic-gate #endif /* MACOS */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate     if (( err = ldap_init_templates( "ldaptemplates.conf", &templates ))
480Sstevel@tonic-gate 	    != 0 ) {
490Sstevel@tonic-gate 	fprintf( stderr, "ldap_init_templates failed (%d)\n", err );
500Sstevel@tonic-gate 	exit( 1 );
510Sstevel@tonic-gate     }
520Sstevel@tonic-gate 
530Sstevel@tonic-gate     if (( err = ldap_init_searchprefs( "ldapsearchprefs.conf", &so ))
540Sstevel@tonic-gate 	    != 0 ) {
550Sstevel@tonic-gate 	fprintf( stderr, "ldap_init_searchprefs failed (%d)\n", err );
560Sstevel@tonic-gate 	exit( 1 );
570Sstevel@tonic-gate     }
580Sstevel@tonic-gate 
590Sstevel@tonic-gate     if ( argc == 1 ) {
600Sstevel@tonic-gate 	printf( "*** Display Templates:\n" );
610Sstevel@tonic-gate 	for ( dtp = ldap_first_disptmpl( templates ); dtp != NULLDISPTMPL;
620Sstevel@tonic-gate 		dtp = ldap_next_disptmpl( templates, dtp )) {
630Sstevel@tonic-gate 	    dump_tmpl( dtp );
640Sstevel@tonic-gate 	    printf( "\n\n" );
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	printf( "\n\n*** Search Objects:\n" );
680Sstevel@tonic-gate 	for ( sop = ldap_first_searchobj( so ); sop != NULLSEARCHOBJ;
690Sstevel@tonic-gate 		    sop = ldap_next_searchobj( so, sop )) {
700Sstevel@tonic-gate 	    dump_srchpref( sop );
710Sstevel@tonic-gate 	    printf( "\n\n" );
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
740Sstevel@tonic-gate     } else {
750Sstevel@tonic-gate 	if (( dtp = ldap_oc2template( ++argv, templates )) == NULL ) {
760Sstevel@tonic-gate 	    fprintf( stderr, "no matching template found\n" );
770Sstevel@tonic-gate 	} else {
780Sstevel@tonic-gate 	    dump_tmpl( dtp );
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate     }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 
830Sstevel@tonic-gate     ldap_free_templates( templates );
840Sstevel@tonic-gate     ldap_free_searchprefs( so );
850Sstevel@tonic-gate 
860Sstevel@tonic-gate     exit( 0 );
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static char *syn_name[] = {
910Sstevel@tonic-gate     "?", "CIS", "MLS", "DN", "BOOL", "JPEG", "JPEGBTN", "FAX", "FAXBTN",
920Sstevel@tonic-gate     "AUDIOBTN", "TIME", "DATE", "URL", "SEARCHACT", "LINKACT", "ADDDNACT",
930Sstevel@tonic-gate     "VERIFYACT",
940Sstevel@tonic-gate };
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static char *syn_type[] = {
970Sstevel@tonic-gate     "?", "txt", "img", "?", "bool", "?", "?", "?", "btn",
980Sstevel@tonic-gate     "?", "?", "?", "?", "?", "?", "?",
990Sstevel@tonic-gate     "action", "?"
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static char *includeattrs[] = { "objectClass", "sn", NULL };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static char *item_opts[] = {
1050Sstevel@tonic-gate     "ro", "sort", "1val", "hide", "required", "hideiffalse", NULL
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static unsigned long item_opt_vals[] = {
1090Sstevel@tonic-gate     LDAP_DITEM_OPT_READONLY,		LDAP_DITEM_OPT_SORTVALUES,
1100Sstevel@tonic-gate     LDAP_DITEM_OPT_SINGLEVALUED,	LDAP_DITEM_OPT_HIDEIFEMPTY,
1110Sstevel@tonic-gate     LDAP_DITEM_OPT_VALUEREQUIRED,	LDAP_DITEM_OPT_HIDEIFFALSE,
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate void
dump_tmpl(struct ldap_disptmpl * tmpl)1160Sstevel@tonic-gate dump_tmpl( struct ldap_disptmpl *tmpl )
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate     struct ldap_tmplitem	*rowp, *colp;
1190Sstevel@tonic-gate     int				i, rowcnt, colcnt;
1200Sstevel@tonic-gate     char			**fetchattrs;
1210Sstevel@tonic-gate     struct ldap_oclist		*ocp;
1220Sstevel@tonic-gate     struct ldap_adddeflist	*adp;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate     printf( "** Template \"%s\" (plural \"%s\", icon \"%s\")\n",
1250Sstevel@tonic-gate 	    NULLSTRINGIFNULL( tmpl->dt_name ),
1260Sstevel@tonic-gate 	    NULLSTRINGIFNULL( tmpl->dt_pluralname ),
1270Sstevel@tonic-gate 	    NULLSTRINGIFNULL( tmpl->dt_iconname ));
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate     printf( "object class list:\n" );
1300Sstevel@tonic-gate     for ( ocp = tmpl->dt_oclist; ocp != NULL; ocp = ocp->oc_next ) {
1310Sstevel@tonic-gate 	for ( i = 0; ocp->oc_objclasses[ i ] != NULL; ++i ) {
1320Sstevel@tonic-gate 	    printf( "%s%s", i == 0 ? "  " : " & ",
1330Sstevel@tonic-gate 		    NULLSTRINGIFNULL( ocp->oc_objclasses[ i ] ));
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	putchar( '\n' );
1360Sstevel@tonic-gate     }
1370Sstevel@tonic-gate     putchar( '\n' );
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate     printf( "template options:          " );
1400Sstevel@tonic-gate     if ( tmpl->dt_options == 0L ) {
1410Sstevel@tonic-gate 	printf( "NONE\n" );
1420Sstevel@tonic-gate     } else {
1430Sstevel@tonic-gate 	printf( "%s %s %s\n", LDAP_IS_DISPTMPL_OPTION_SET( tmpl,
1440Sstevel@tonic-gate 		LDAP_DTMPL_OPT_ADDABLE ) ? "addable" : "",
1450Sstevel@tonic-gate 		LDAP_IS_DISPTMPL_OPTION_SET( tmpl, LDAP_DTMPL_OPT_ALLOWMODRDN )
1460Sstevel@tonic-gate 		? "modrdn" : "",
1470Sstevel@tonic-gate 		LDAP_IS_DISPTMPL_OPTION_SET( tmpl, LDAP_DTMPL_OPT_ALTVIEW )
1480Sstevel@tonic-gate 		? "altview" : "" );
1490Sstevel@tonic-gate     }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate     printf( "authenticate as attribute: %s\n", tmpl->dt_authattrname != NULL ?
1520Sstevel@tonic-gate 	    tmpl->dt_authattrname : "<default>" );
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate     printf( "default RDN attribute:     %s\n", tmpl->dt_defrdnattrname != NULL ?
1550Sstevel@tonic-gate 	    tmpl->dt_defrdnattrname : "NONE" );
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate     printf( "default add location:      %s\n", tmpl->dt_defaddlocation != NULL ?
1580Sstevel@tonic-gate 	    tmpl->dt_defaddlocation : "NONE" );
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate     printf( "\nnew entry value default rules:\n" );
1610Sstevel@tonic-gate     for ( adp = tmpl->dt_adddeflist; adp != NULL; adp = adp->ad_next ) {
1620Sstevel@tonic-gate 	if ( adp->ad_source == LDAP_ADSRC_CONSTANTVALUE ) {
1630Sstevel@tonic-gate 	    printf( "  attribute %s <-- constant value \"%s\"\n",
1640Sstevel@tonic-gate 		NULLSTRINGIFNULL( adp->ad_attrname),
1650Sstevel@tonic-gate 		NULLSTRINGIFNULL( adp->ad_value ));
1660Sstevel@tonic-gate 	} else {
1670Sstevel@tonic-gate 	    printf( "  attribute %s <-- adder's DN\n",
1680Sstevel@tonic-gate 		    NULLSTRINGIFNULL( adp->ad_attrname ));
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate     }
1710Sstevel@tonic-gate     putchar( '\n' );
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate     printf( "\nfetch attributes & values:\n" );
1740Sstevel@tonic-gate     if (( fetchattrs = ldap_tmplattrs( tmpl, includeattrs, 1,
1750Sstevel@tonic-gate 		LDAP_SYN_OPT_DEFER )) == NULL ) {
1760Sstevel@tonic-gate 	printf( "  <none>\n" );
1770Sstevel@tonic-gate     } else {
1780Sstevel@tonic-gate 	for ( i = 0; fetchattrs[ i ] != NULL; ++i ) {
1790Sstevel@tonic-gate 	    printf( "  %s\n", fetchattrs[ i ] );
1800Sstevel@tonic-gate 	    free( fetchattrs[ i ] );
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	free( (char *)fetchattrs );
1830Sstevel@tonic-gate     }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate     printf( "\nfetch attributes only:\n" );
1860Sstevel@tonic-gate     if (( fetchattrs = ldap_tmplattrs( tmpl, NULL, 0,
1870Sstevel@tonic-gate 		LDAP_SYN_OPT_DEFER )) == NULL ) {
1880Sstevel@tonic-gate 	printf( "  <none>\n" );
1890Sstevel@tonic-gate     } else {
1900Sstevel@tonic-gate 	for ( i = 0; fetchattrs[ i ] != NULL; ++i ) {
1910Sstevel@tonic-gate 	    printf( "  %s\n", fetchattrs[ i ] );
1920Sstevel@tonic-gate 	    free( fetchattrs[ i ] );
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	free( (char *)fetchattrs );
1950Sstevel@tonic-gate     }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate     printf( "\ntemplate items:\n" );
1980Sstevel@tonic-gate     rowcnt = 0;
1990Sstevel@tonic-gate     for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
2000Sstevel@tonic-gate 	    rowp = ldap_next_tmplrow( tmpl, rowp )) {
2010Sstevel@tonic-gate 	++rowcnt;
2020Sstevel@tonic-gate 	colcnt = 0;
2030Sstevel@tonic-gate 	for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
2040Sstevel@tonic-gate 		colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
2050Sstevel@tonic-gate 	    ++colcnt;
2060Sstevel@tonic-gate 	    printf( "  %2d-%d: %s (%s%s", rowcnt, colcnt,
2070Sstevel@tonic-gate 		syn_name[ colp->ti_syntaxid & 0x0000FFFF ],
2080Sstevel@tonic-gate 		syn_type[ LDAP_GET_SYN_TYPE( colp->ti_syntaxid ) >> 24 ],
2090Sstevel@tonic-gate 		(( LDAP_GET_SYN_OPTIONS( colp->ti_syntaxid ) &
2100Sstevel@tonic-gate 		LDAP_SYN_OPT_DEFER ) != 0 ) ? ",defer" : "" );
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	    for ( i = 0; item_opts[ i ] != NULL; ++i ) {
2130Sstevel@tonic-gate 		if ( LDAP_IS_TMPLITEM_OPTION_SET( colp, item_opt_vals[ i ] )) {
2140Sstevel@tonic-gate 		    printf( ",%s", NULLSTRINGIFNULL( item_opts[ i ] ));
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 	    }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	    printf( "), %s, %s", NULLSTRINGIFNULL( colp->ti_attrname ),
2190Sstevel@tonic-gate 		    NULLSTRINGIFNULL( colp->ti_label ));
2200Sstevel@tonic-gate 	    if ( colp->ti_args != NULL ) {
2210Sstevel@tonic-gate 		printf( ",args=" );
2220Sstevel@tonic-gate 		for ( i = 0; colp->ti_args[ i ] != NULL; ++i ) {
2230Sstevel@tonic-gate 		    printf( "<%s>", NULLSTRINGIFNULL( colp->ti_args[ i ] ));
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	    }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	    putchar( '\n' );
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate     }
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate void
dump_srchpref(struct ldap_searchobj * so)2340Sstevel@tonic-gate dump_srchpref( struct ldap_searchobj *so )
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate     int i;
2370Sstevel@tonic-gate     struct ldap_searchattr *sa;
2380Sstevel@tonic-gate     struct ldap_searchmatch *sm;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate     printf( "Object type prompt:  %s\n",
2410Sstevel@tonic-gate 	    NULLSTRINGIFNULL( so->so_objtypeprompt ));
2420Sstevel@tonic-gate     printf( "Options:             %s\n",
2430Sstevel@tonic-gate 	    LDAP_IS_SEARCHOBJ_OPTION_SET( so, LDAP_SEARCHOBJ_OPT_INTERNAL ) ?
2440Sstevel@tonic-gate 	    "internal" : "NONE" );
2450Sstevel@tonic-gate     printf( "Prompt:              %s\n", NULLSTRINGIFNULL( so->so_prompt ));
2460Sstevel@tonic-gate     printf( "Scope:               " );
2470Sstevel@tonic-gate     switch ( so->so_defaultscope ) {
2480Sstevel@tonic-gate     case LDAP_SCOPE_BASE:
2490Sstevel@tonic-gate 	printf( "LDAP_SCOPE_BASE" );
2500Sstevel@tonic-gate 	break;
2510Sstevel@tonic-gate     case LDAP_SCOPE_ONELEVEL:
2520Sstevel@tonic-gate 	printf( "LDAP_SCOPE_ONELEVEL" );
2530Sstevel@tonic-gate 	break;
2540Sstevel@tonic-gate     case LDAP_SCOPE_SUBTREE:
2550Sstevel@tonic-gate 	printf( "LDAP_SCOPE_SUBTREE" );
2560Sstevel@tonic-gate 	break;
2570Sstevel@tonic-gate     default:
2580Sstevel@tonic-gate 	printf("*** unknown!" );
2590Sstevel@tonic-gate     }
2600Sstevel@tonic-gate     puts( "\n" );
2610Sstevel@tonic-gate     printf( "Filter prefix:       %s\n",
2620Sstevel@tonic-gate 	    NULLSTRINGIFNULL( so->so_filterprefix ));
2630Sstevel@tonic-gate     printf( "Filter tag:          %s\n",
2640Sstevel@tonic-gate 	    NULLSTRINGIFNULL( so->so_filtertag ));
2650Sstevel@tonic-gate     printf( "Default select attr: %s\n",
2660Sstevel@tonic-gate 	    NULLSTRINGIFNULL( so->so_defaultselectattr ));
2670Sstevel@tonic-gate     printf( "Default select text: %s\n",
2680Sstevel@tonic-gate 	    NULLSTRINGIFNULL( so->so_defaultselecttext ));
2690Sstevel@tonic-gate     printf( "Searchable attributes ---- \n" );
2700Sstevel@tonic-gate     for ( sa = so->so_salist; sa != NULL; sa = sa->sa_next ) {
2710Sstevel@tonic-gate 	printf( "  Label: %s\n", NULLSTRINGIFNULL( sa->sa_attrlabel ));
2720Sstevel@tonic-gate 	printf( "  Attribute: %s\n", NULLSTRINGIFNULL( sa->sa_attr ));
2730Sstevel@tonic-gate 	printf( "  Select attr: %s\n", NULLSTRINGIFNULL( sa->sa_selectattr ));
2740Sstevel@tonic-gate 	printf( "  Select text: %s\n", NULLSTRINGIFNULL( sa->sa_selecttext ));
2750Sstevel@tonic-gate 	printf( "  Match types ---- \n" );
2760Sstevel@tonic-gate 	for ( i = 0, sm = so->so_smlist; sm != NULL; i++, sm = sm->sm_next ) {
2770Sstevel@tonic-gate 	    if (( sa->sa_matchtypebitmap >> i ) & 1 ) {
2780Sstevel@tonic-gate 		printf( "    %s (%s)\n",
2790Sstevel@tonic-gate 			NULLSTRINGIFNULL( sm->sm_matchprompt ),
2800Sstevel@tonic-gate 			NULLSTRINGIFNULL( sm->sm_filter ));
2810Sstevel@tonic-gate 	    }
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate     }
2840Sstevel@tonic-gate }
285