]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/tmplout.c
Braced ldap_connect_to_path() in #ifdef LDAP_PF_LOCAL so as to compile
[openldap] / libraries / libldap / tmplout.c
index 84ae94c90e78f8c22e25ff7a822175e179def668..e804fe0be74e32349653fd5b8fd69289caeea443 100644 (file)
@@ -1,3 +1,8 @@
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
 /*
  * tmplout.c:  display template library output routines for LDAP clients
  * 12 April 1994 by Mark C Smith
@@ -6,7 +11,9 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/socket.h>
+#include <ac/stdlib.h>
 
 #include <ac/ctype.h>
 #include <ac/string.h>
 #include <sys/file.h>
 #endif
 
-#include "lber.h"
-#include "ldap-int.h"
+#include <ldap.h>
+#include <disptmpl.h>
+
 #include "ldap_log.h"
-#include "disptmpl.h"
 
-#include "ldapconfig.h"
+#include "ldap-int.h"
+#include "ldap_defaults.h"
+
+#define writeptype ldap_writeptype
 
+/* local functions */
 static int do_entry2text LDAP_P((
        LDAP *ld, char *buf, char *base, LDAPMessage *entry,
        struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
@@ -57,8 +68,8 @@ static int searchaction LDAP_P((
 #define OCATTRNAME             "objectClass"
 
 
-#define NONFATAL_LDAP_ERR( err )       ( err == LDAP_SUCCESS || \
-       err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
+#define NONFATAL_LDAP_ERR( err )       ( (err) == LDAP_SUCCESS || \
+       (err) == LDAP_TIMELIMIT_EXCEEDED || (err) == LDAP_SIZELIMIT_EXCEEDED )
 
 #define DEF_LDAP_URL_PREFIX    "ldap:///"
 
@@ -142,9 +153,9 @@ do_entry2text(
     }
 
     if ( buf == NULL ) {
-       if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
+       if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
            ld->ld_errno = LDAP_NO_MEMORY;
-           free( dn );
+           LDAP_FREE( dn );
            return( ld->ld_errno );
        }
        freebuf = 1;
@@ -193,7 +204,7 @@ do_entry2text(
            sprintf( buf + strlen( buf ),
                    "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
                    ( vals[ 1 ] == NULL ) ? "??one" : "",
-                   ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol, eol );
+                   ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
            (*writeproc)( writeparm, buf, strlen( buf ));
 
            /*
@@ -249,9 +260,7 @@ do_entry2text(
                freevals = 1;
            }
 
-           if ( islower( *attr )) {    /* cosmetic -- upcase attr. name */
-               *attr = toupper( *attr );
-           }
+           *attr = TOUPPER( (unsigned char) *attr );
 
            err = do_vals2text( ld, buf, vals, attr, labelwidth,
                    LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol, 
@@ -260,11 +269,15 @@ do_entry2text(
                ldap_value_free( vals );
            }
        }
+
+       if( ber != NULL) {
+               ber_free( ber, 0 );
+       }
     } else {
        for ( rowp = ldap_first_tmplrow( tmpl );
-               NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
+               NONFATAL_LDAP_ERR( err ) && rowp != NULL;
                rowp = ldap_next_tmplrow( tmpl, rowp )) {
-           for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
+           for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
                    colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
                vals = NULL;
                if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
@@ -300,14 +313,14 @@ do_entry2text(
                if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
                        == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
                        colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
-                       toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
+                       TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) != 'T' ) {
                    show = 0;
                }
 
                if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
                    if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
                        if ( colp->ti_attrname == NULL || ( show &&
-                               toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
+                               TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) == 'T' )) {
                            err = searchaction( ld, buf, base, entry, dn, colp,
                                    labelwidth, rdncount, writeproc,
                                    writeparm, eol, urlprefix );
@@ -334,9 +347,9 @@ do_entry2text(
        (*writeproc)( writeparm, buf, strlen( buf ));
     }
 
-    free( dn );
+    LDAP_FREE( dn );
     if ( freebuf ) {
-       free( buf );
+       LDAP_FREE( buf );
     }
 
     return( err );
@@ -414,7 +427,7 @@ do_entry2text_search(
     struct ldap_disptmpl       *tmpl;
     struct timeval             timeout;
 
-    if ( dn == NULL && entry == NULLMSG ) {
+    if ( dn == NULL && entry == NULL ) {
        ld->ld_errno = LDAP_PARAM_ERROR;
        return( ld->ld_errno );
     }
@@ -424,7 +437,7 @@ do_entry2text_search(
     timeout.tv_sec = SEARCH_TIMEOUT_SECS;
     timeout.tv_usec = 0;
 
-    if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
+    if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
        ld->ld_errno = LDAP_NO_MEMORY;
        return( ld->ld_errno );
     }
@@ -444,7 +457,7 @@ do_entry2text_search(
 
     if ( dn == NULL ) {
        if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
-           free( buf );
+           LDAP_FREE( buf );
            if ( freetmpls ) {
                ldap_free_templates( tmpllist );
            }
@@ -455,7 +468,7 @@ do_entry2text_search(
 
 
     if ( tmpllist != NULL ) {
-       ldmp = NULLMSG;
+       ldmp = NULL;
 
        if ( entry == NULL ) {
            char        *ocattrs[2];
@@ -506,7 +519,7 @@ do_entry2text_search(
                fetchattrs, 0, &timeout, &ldmp );
 
     if ( freedn ) {
-       free( dn );
+       LDAP_FREE( dn );
     }
     if ( fetchattrs != NULL ) {
        ldap_value_free( fetchattrs );
@@ -517,14 +530,14 @@ do_entry2text_search(
        if ( freetmpls ) {
             ldap_free_templates( tmpllist );
         }
-       free( buf );
+       LDAP_FREE( buf );
        return( ld->ld_errno );
     }
 
     err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
            writeproc, writeparm, eol, rdncount, opts, urlprefix );
 
-    free( buf );
+    LDAP_FREE( buf );
     if ( freetmpls ) {
        ldap_free_templates( tmpllist );
     }
@@ -618,7 +631,7 @@ do_vals2text(
     }
 
     if ( buf == NULL ) {
-       if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
+       if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
            ld->ld_errno = LDAP_NO_MEMORY;
            return( ld->ld_errno );
        }
@@ -671,7 +684,7 @@ do_vals2text(
            p = s = outval;
            while (( s = strchr( s, '$' )) != NULL ) {
                *s++ = '\0';
-               while ( isspace( *s )) {
+               while ( isspace( (unsigned char) *s )) {
                    ++s;
                }
                if ( html ) {
@@ -687,7 +700,7 @@ do_vals2text(
            break;
 
        case LDAP_SYN_BOOLEAN:
-           outval = toupper( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
+           outval = TOUPPER((unsigned char) outval[0]) == 'T' ? "TRUE" : "FALSE";
            ++writeoutval;
            break;
 
@@ -700,13 +713,13 @@ do_vals2text(
        case LDAP_SYN_LABELEDURL:
            if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
                *p++ = '\0';
-               while ( isspace( *p )) {
+               while ( isspace( (unsigned char) *p )) {
                    ++p;
                }
                s = outval;
            } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
                *s++ = '\0';
-               while ( isspace( *s )) {
+               while ( isspace( (unsigned char) *s )) {
                    ++s;
                }
                p = outval;
@@ -744,7 +757,7 @@ do_vals2text(
     }
 
     if ( freebuf ) {
-       free( buf );
+       LDAP_FREE( buf );
     }
 
     return( LDAP_SUCCESS );
@@ -759,9 +772,9 @@ max_label_len( struct ldap_disptmpl *tmpl )
 
     maxlen = 0;
 
-    for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
+    for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULL;
            rowp = ldap_next_tmplrow( tmpl, rowp )) {
-       for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
+       for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
                colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
            if (( len = strlen( colp->ti_label )) > maxlen ) {
                maxlen = len;
@@ -839,10 +852,10 @@ output_dn( char *buf, char *dn, int width, int rdncount,
 
 
 
-#define HREF_CHAR_ACCEPTABLE( c )      (( c >= '-' && c <= '9' ) ||    \
-                                        ( c >= '@' && c <= 'Z' ) ||    \
-                                        ( c == '_' ) ||                \
-                                        ( c >= 'a' && c <= 'z' ))
+#define HREF_CHAR_ACCEPTABLE( c )      (( (c) >= '-' && (c) <= '9' ) || \
+                                        ( (c) >= '@' && (c) <= 'Z' ) || \
+                                        ( (c) == '_' )               || \
+                                        ( (c) >= 'a' && (c) <= 'z' ))
 
 static void
 strcat_escaped( char *s1, char *s2 )
@@ -865,7 +878,7 @@ strcat_escaped( char *s1, char *s2 )
 }
 
 
-#define GET2BYTENUM( p )       (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
+#define GET2BYTENUM( p )       (( *(p) - '0' ) * 10 + ( *((p)+1) - '0' ))
 
 static char *
 time2text( char *ldtimestr, int dateonly )
@@ -873,20 +886,42 @@ time2text( char *ldtimestr, int dateonly )
     struct tm          t;
     char               *p, *timestr, zone, *fmterr = "badly formatted time";
     time_t             gmttime;
+    char               timebuf[32];
+       int ndigits;
 
-    memset( (char *)&t, 0, sizeof( struct tm ));
-    if ( (int) strlen( ldtimestr ) < 13 ) {
-       return( fmterr );
+       if (strlen( ldtimestr ) < 12 ) {
+               return( fmterr );
+       }
+
+    for ( ndigits=0; isdigit((unsigned char) ldtimestr[ndigits]); ndigits++) {
+               ; /* EMPTY */
     }
 
-    for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
-       if ( !isdigit( *p )) {
+       if ( ndigits != 12 && ndigits != 14) {
            return( fmterr );
        }
-    }
+       
+    memset( (char *)&t, 0, sizeof( struct tm ));
 
     p = ldtimestr;
-    t.tm_year = GET2BYTENUM( p ); p += 2;
+
+       if( ndigits == 14) {
+               /* came with a century */
+               /* POSIX says tm_year should be year - 1900 */
+       t.tm_year = 100 * GET2BYTENUM( p ) - 1900;
+               p += 2;
+               t.tm_year += GET2BYTENUM( p ); p += 2;
+
+       } else {
+               /* came without a century */
+               t.tm_year = GET2BYTENUM( p ); p += 2;
+
+               /* Y2K hack - 2 digit years < 70 are 21st century */
+               if( t.tm_year < 70 ) {
+                       t.tm_year += 100;
+               }
+       }
+
     t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
     t.tm_mday = GET2BYTENUM( p ); p += 2;
     t.tm_hour = GET2BYTENUM( p ); p += 2;
@@ -898,11 +933,12 @@ time2text( char *ldtimestr, int dateonly )
     }
 
     gmttime = gtime( &t );
-    timestr = ctime( &gmttime );
 
+    timestr = ldap_pvt_ctime( &gmttime, timebuf );
+   
     timestr[ strlen( timestr ) - 1 ] = zone;   /* replace trailing newline */
     if ( dateonly ) {
-       strcpy( timestr + 11, timestr + 20 );
+       SAFEMEMCPY( timestr + 11, timestr + 20, strlen( timestr + 20 ) + 1 );
     }
 
     return( timestr );
@@ -915,14 +951,25 @@ time2text( char *ldtimestr, int dateonly )
        This routine was supplied by Mike Accetta at CMU many years ago.
  */
 
-static int     dmsize[] = {
+static const int dmsize[] = {
     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
 };
 
 #define        dysize(y)       \
        (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
 
-#define        YEAR(y)         ((y) >= 100 ? (y) : (y) + 1900)
+/*
+ * Y2K YEAR
+ */
+       /* per STDC & POSIX tm_year should be offset by 1900 */
+#define YEAR_POSIX(y)          ((y) + 1900)
+
+       /*
+        * year is < 1900, year is offset by 1900
+        */
+#define YEAR_CAREFUL(y)                ((y) < 1900 ? (y) + 1900 : (y))
+
+#define YEAR(y) YEAR_CAREFUL(y)
 
 /* \f */
 
@@ -997,6 +1044,8 @@ searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
        return( LDAP_PARAM_ERROR );
     } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
        value = dn;
+    } else if ( strcasecmp( attr, "-dn" ) == 0 ) {
+       value = dn;
     } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
        value = vals[ 0 ];
     } else {
@@ -1040,7 +1089,7 @@ searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
 
     if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
        if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
-           if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
+           if (( members = (char **)LDAP_MALLOC( (count + 1) * sizeof(char *)))
                    == NULL ) {
                err = LDAP_NO_MEMORY;
            } else {