Back to index

lightning-sunbird  0.9+nobinonly
tmpltest.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* tmpltest.c - implements a test/config templates. */
00039 #include <stdio.h>
00040 #include <sys/types.h>
00041 
00042 #ifdef _WINDOWS
00043 #include <windows.h>
00044 #endif
00045 
00046 #include "ldap-int.h"
00047 #include "disptmpl.h"
00048 #include "srchpref.h"
00049 
00050 #ifdef MACOS
00051 #include <stdlib.h>
00052 #include <console.h>
00053 #endif /* MACOS */
00054 
00055 #ifdef NEEDPROTOS
00056 void dump_tmpl( struct ldap_disptmpl *tmpl );
00057 void dump_srchpref( struct ldap_searchobj *sp );
00058 #else /* NEEDPROTOS */
00059 void dump_tmpl();
00060 void dump_srchpref();
00061 #endif /* NEEDPROTOS */
00062 
00063 
00064 #define NULLSTRINGIFNULL( s )      ( s == NULL ? "(null)" : s )
00065 
00066 
00067 int
00068 main( int argc, char **argv )
00069 {
00070     struct ldap_disptmpl    *templates, *dtp;
00071     struct ldap_searchobj   *so, *sop;
00072     int                            err;
00073 
00074 #ifdef MACOS
00075        ccommand( &argv );
00076        for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
00077            ;
00078        }
00079        cshow( stdout );
00080 #endif /* MACOS */
00081 
00082     if (( err = ldap_init_templates( "ldaptemplates.conf", &templates ))
00083            != 0 ) {
00084        fprintf( stderr, "ldap_init_templates failed (%d)\n", err );
00085        exit( 1 );
00086     }
00087 
00088     if (( err = ldap_init_searchprefs( "ldapsearchprefs.conf", &so ))
00089            != 0 ) {
00090        fprintf( stderr, "ldap_init_searchprefs failed (%d)\n", err );
00091        exit( 1 );
00092     }
00093 
00094     if ( argc == 1 ) {
00095        printf( "*** Display Templates:\n" );
00096        for ( dtp = ldap_first_disptmpl( templates ); dtp != NULLDISPTMPL;
00097               dtp = ldap_next_disptmpl( templates, dtp )) {
00098            dump_tmpl( dtp );
00099            printf( "\n\n" );
00100        }
00101 
00102        printf( "\n\n*** Search Objects:\n" );
00103        for ( sop = ldap_first_searchobj( so ); sop != NULLSEARCHOBJ;
00104                   sop = ldap_next_searchobj( so, sop )) {
00105            dump_srchpref( sop );
00106            printf( "\n\n" );
00107        }
00108 
00109     } else {
00110        if (( dtp = ldap_oc2template( ++argv, templates )) == NULL ) {
00111            fprintf( stderr, "no matching template found\n" );
00112        } else {
00113            dump_tmpl( dtp );
00114        }
00115     }
00116 
00117 
00118     ldap_free_templates( templates );
00119     ldap_free_searchprefs( so );
00120 
00121     exit( 0 );
00122 }
00123 
00124 
00125 static char *syn_name[] = {
00126     "?", "CIS", "MLS", "DN", "BOOL", "JPEG", "JPEGBTN", "FAX", "FAXBTN",
00127     "AUDIOBTN", "TIME", "DATE", "URL", "SEARCHACT", "LINKACT", "ADDDNACT",
00128     "VERIFYACT",
00129 };
00130 
00131 static char *syn_type[] = {
00132     "?", "txt", "img", "?", "bool", "?", "?", "?", "btn",
00133     "?", "?", "?", "?", "?", "?", "?",
00134     "action", "?"
00135 };
00136 
00137 static char *includeattrs[] = { "objectClass", "sn", NULL };
00138 
00139 static char *item_opts[] = {
00140     "ro", "sort", "1val", "hide", "required", "hideiffalse", NULL
00141 };
00142 
00143 static unsigned long item_opt_vals[] = {
00144     LDAP_DITEM_OPT_READONLY,              LDAP_DITEM_OPT_SORTVALUES,
00145     LDAP_DITEM_OPT_SINGLEVALUED,   LDAP_DITEM_OPT_HIDEIFEMPTY,
00146     LDAP_DITEM_OPT_VALUEREQUIRED,  LDAP_DITEM_OPT_HIDEIFFALSE,
00147 };
00148 
00149 
00150 void
00151 dump_tmpl( struct ldap_disptmpl *tmpl )
00152 {
00153     struct ldap_tmplitem    *rowp, *colp;
00154     int                            i, rowcnt, colcnt;
00155     char                    **fetchattrs;
00156     struct ldap_oclist             *ocp;
00157     struct ldap_adddeflist  *adp;
00158 
00159     printf( "** Template \"%s\" (plural \"%s\", icon \"%s\")\n",
00160            NULLSTRINGIFNULL( tmpl->dt_name ),
00161            NULLSTRINGIFNULL( tmpl->dt_pluralname ),
00162            NULLSTRINGIFNULL( tmpl->dt_iconname ));
00163 
00164     printf( "object class list:\n" );
00165     for ( ocp = tmpl->dt_oclist; ocp != NULL; ocp = ocp->oc_next ) {
00166        for ( i = 0; ocp->oc_objclasses[ i ] != NULL; ++i ) {
00167            printf( "%s%s", i == 0 ? "  " : " & ",
00168                   NULLSTRINGIFNULL( ocp->oc_objclasses[ i ] ));
00169        }
00170        putchar( '\n' );
00171     }
00172     putchar( '\n' );
00173 
00174     printf( "template options:          " );
00175     if ( tmpl->dt_options == 0L ) {
00176        printf( "NONE\n" );
00177     } else {
00178        printf( "%s %s %s\n", LDAP_IS_DISPTMPL_OPTION_SET( tmpl,
00179               LDAP_DTMPL_OPT_ADDABLE ) ? "addable" : "",
00180               LDAP_IS_DISPTMPL_OPTION_SET( tmpl, LDAP_DTMPL_OPT_ALLOWMODRDN )
00181               ? "modrdn" : "",
00182               LDAP_IS_DISPTMPL_OPTION_SET( tmpl, LDAP_DTMPL_OPT_ALTVIEW )
00183               ? "altview" : "" );
00184     }
00185 
00186     printf( "authenticate as attribute: %s\n", tmpl->dt_authattrname != NULL ?
00187            tmpl->dt_authattrname : "<default>" );
00188 
00189     printf( "default RDN attribute:     %s\n", tmpl->dt_defrdnattrname != NULL ?
00190            tmpl->dt_defrdnattrname : "NONE" );
00191 
00192     printf( "default add location:      %s\n", tmpl->dt_defaddlocation != NULL ?
00193            tmpl->dt_defaddlocation : "NONE" );
00194 
00195     printf( "\nnew entry value default rules:\n" );
00196     for ( adp = tmpl->dt_adddeflist; adp != NULL; adp = adp->ad_next ) {
00197        if ( adp->ad_source == LDAP_ADSRC_CONSTANTVALUE ) {
00198            printf( "  attribute %s <-- constant value \"%s\"\n",
00199               NULLSTRINGIFNULL( adp->ad_attrname),
00200               NULLSTRINGIFNULL( adp->ad_value ));
00201        } else {
00202            printf( "  attribute %s <-- adder's DN\n",
00203                   NULLSTRINGIFNULL( adp->ad_attrname ));
00204        }
00205     }
00206     putchar( '\n' );
00207 
00208     printf( "\nfetch attributes & values:\n" );
00209     if (( fetchattrs = ldap_tmplattrs( tmpl, includeattrs, 1,
00210               LDAP_SYN_OPT_DEFER )) == NULL ) {
00211        printf( "  <none>\n" );
00212     } else {
00213        for ( i = 0; fetchattrs[ i ] != NULL; ++i ) {
00214            printf( "  %s\n", fetchattrs[ i ] );
00215            free( fetchattrs[ i ] );
00216        }
00217        free( (char *)fetchattrs );
00218     }
00219 
00220     printf( "\nfetch attributes only:\n" );
00221     if (( fetchattrs = ldap_tmplattrs( tmpl, NULL, 0,
00222               LDAP_SYN_OPT_DEFER )) == NULL ) {
00223        printf( "  <none>\n" );
00224     } else {
00225        for ( i = 0; fetchattrs[ i ] != NULL; ++i ) {
00226            printf( "  %s\n", fetchattrs[ i ] );
00227            free( fetchattrs[ i ] );
00228        }
00229        free( (char *)fetchattrs );
00230     }
00231 
00232     printf( "\ntemplate items:\n" );
00233     rowcnt = 0;
00234     for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
00235            rowp = ldap_next_tmplrow( tmpl, rowp )) {
00236        ++rowcnt;
00237        colcnt = 0;
00238        for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
00239               colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
00240            ++colcnt;
00241            printf( "  %2d-%d: %s (%s%s", rowcnt, colcnt,
00242               syn_name[ colp->ti_syntaxid & 0x0000FFFF ],
00243               syn_type[ LDAP_GET_SYN_TYPE( colp->ti_syntaxid ) >> 24 ],
00244               (( LDAP_GET_SYN_OPTIONS( colp->ti_syntaxid ) &
00245               LDAP_SYN_OPT_DEFER ) != 0 ) ? ",defer" : "" );
00246 
00247            for ( i = 0; item_opts[ i ] != NULL; ++i ) {
00248               if ( LDAP_IS_TMPLITEM_OPTION_SET( colp, item_opt_vals[ i ] )) {
00249                   printf( ",%s", NULLSTRINGIFNULL( item_opts[ i ] ));
00250               }
00251            }
00252 
00253            printf( "), %s, %s", NULLSTRINGIFNULL( colp->ti_attrname ),
00254                   NULLSTRINGIFNULL( colp->ti_label ));
00255            if ( colp->ti_args != NULL ) {
00256               printf( ",args=" );
00257               for ( i = 0; colp->ti_args[ i ] != NULL; ++i ) {
00258                   printf( "<%s>", NULLSTRINGIFNULL( colp->ti_args[ i ] ));
00259               }
00260            }
00261 
00262            putchar( '\n' );
00263        }
00264     }
00265 }
00266 
00267 
00268 void
00269 dump_srchpref( struct ldap_searchobj *so )
00270 {
00271     int i;
00272     struct ldap_searchattr *sa;
00273     struct ldap_searchmatch *sm;
00274 
00275     printf( "Object type prompt:  %s\n",
00276            NULLSTRINGIFNULL( so->so_objtypeprompt ));
00277     printf( "Options:             %s\n",
00278            LDAP_IS_SEARCHOBJ_OPTION_SET( so, LDAP_SEARCHOBJ_OPT_INTERNAL ) ?
00279            "internal" : "NONE" );
00280     printf( "Prompt:              %s\n", NULLSTRINGIFNULL( so->so_prompt ));
00281     printf( "Scope:               " );
00282     switch ( so->so_defaultscope ) {
00283     case LDAP_SCOPE_BASE:
00284        printf( "LDAP_SCOPE_BASE" );
00285        break;
00286     case LDAP_SCOPE_ONELEVEL:
00287        printf( "LDAP_SCOPE_ONELEVEL" );
00288        break;
00289     case LDAP_SCOPE_SUBTREE:
00290        printf( "LDAP_SCOPE_SUBTREE" );
00291        break;
00292     default:
00293        printf("*** unknown!" );
00294     }
00295     puts( "\n" );
00296     printf( "Filter prefix:       %s\n",
00297            NULLSTRINGIFNULL( so->so_filterprefix ));
00298     printf( "Filter tag:          %s\n",
00299            NULLSTRINGIFNULL( so->so_filtertag ));
00300     printf( "Default select attr: %s\n",
00301            NULLSTRINGIFNULL( so->so_defaultselectattr ));
00302     printf( "Default select text: %s\n",
00303            NULLSTRINGIFNULL( so->so_defaultselecttext ));
00304     printf( "Searchable attributes ---- \n" );
00305     for ( sa = so->so_salist; sa != NULL; sa = sa->sa_next ) {
00306        printf( "  Label: %s\n", NULLSTRINGIFNULL( sa->sa_attrlabel ));
00307        printf( "  Attribute: %s\n", NULLSTRINGIFNULL( sa->sa_attr ));
00308        printf( "  Select attr: %s\n", NULLSTRINGIFNULL( sa->sa_selectattr ));
00309        printf( "  Select text: %s\n", NULLSTRINGIFNULL( sa->sa_selecttext ));
00310        printf( "  Match types ---- \n" );
00311        for ( i = 0, sm = so->so_smlist; sm != NULL; i++, sm = sm->sm_next ) {
00312            if (( sa->sa_matchtypebitmap >> i ) & 1 ) {
00313               printf( "    %s (%s)\n",
00314                      NULLSTRINGIFNULL( sm->sm_matchprompt ),
00315                      NULLSTRINGIFNULL( sm->sm_filter ));
00316            }
00317        }
00318     }
00319 }