Back to index

openldap  2.4.31
Classes | Defines | Typedefs | Functions
lutil.h File Reference
#include <ldap_cdefs.h>
#include <lber_types.h>

Go to the source code of this file.

Classes

struct  lutil_tm
struct  lutil_timet

Defines

#define LUTIL_BASE64_ENCODE_LEN(n)   (((n)+2)/3 * 4)
#define LUTIL_BASE64_DECODE_LEN(n)   ((n)/4*3)
#define LUTIL_PASSWD_OK   (0)
#define LUTIL_PASSWD_ERR   (-1)
#define LUTIL_SLASHPATH(p)
#define lutil_strbvcopy(a, bv)   lutil_memcopy((a),(bv)->bv_val,(bv)->bv_len)
#define LDAP_LUTIL_GENTIME_BUFSIZE   22
#define lutil_gentime(s, m, t)   lutil_localtime((s),(m),(t),0)
#define LDAP_LUTIL_UUIDSTR_BUFSIZE   40
#define lutil_atoi(v, s)   lutil_atoix((v), (s), 10)
#define lutil_atou(v, s)   lutil_atoux((v), (s), 10)
#define lutil_atol(v, s)   lutil_atolx((v), (s), 10)
#define lutil_atoul(v, s)   lutil_atoulx((v), (s), 10)
#define lutil_timerdiv(t, d, x)
#define lutil_timermul(t, m, x)

Typedefs

typedef intLUTIL_PASSWD_CHK_FUNC )(const struct berval *scheme, const struct berval *passwd, const struct berval *cred, const char **text)
typedef intLUTIL_PASSWD_HASH_FUNC )(const struct berval *scheme, const struct berval *passwd, struct berval *hash, const char **text)
typedef struct lutil_tm lutil_tm
typedef struct lutil_timet lutil_timet

Functions

lutil_b64_ntop LDAP_P ((unsigned char const *, size_t, char *, size_t))
lutil_b64_pton LDAP_P ((char const *, unsigned char *, size_t))
lutil_detach LDAP_P ((int debug, int do_close))
lutil_entropy LDAP_P ((unsigned char *buf, ber_len_t nbytes))
lutil_get_filed_password LDAP_P ((const char *filename, struct berval *))
lutil_passwd_add LDAP_P ((struct berval *scheme, LUTIL_PASSWD_CHK_FUNC *chk_fn, LUTIL_PASSWD_HASH_FUNC *hash_fn))
lutil_passwd_init LDAP_P ((void))
lutil_authpasswd LDAP_P ((const struct berval *passwd, const struct berval *cred, const char **methods))
lutil_authpasswd_hash LDAP_P ((const struct berval *cred, struct berval **passwd, struct berval **salt, const char *method))
lutil_passwd LDAP_P ((const struct berval *passwd, const struct berval *cred, const char **methods, const char **text))
lutil_passwd_generate LDAP_P ((struct berval *pw, ber_len_t))
lutil_passwd_hash LDAP_P ((const struct berval *passwd, const char *method, struct berval *hash, const char **text))
lutil_passwd_scheme LDAP_P ((const char *scheme))
lutil_salt_format LDAP_P ((const char *format))
lutil_passwd_string64 LDAP_P ((const struct berval *sc, const struct berval *hash, struct berval *b64, const struct berval *salt))
lutil_progname LDAP_P ((const char *name, int argc, char *argv[]))
lutil_parsetime LDAP_P ((char *atm, struct lutil_tm *))
lutil_tm2time LDAP_P ((struct lutil_tm *, struct lutil_timet *))
lutil_strcopy LDAP_P ((char *dst, const char *src))
lutil_strncopy LDAP_P ((char *dst, const char *src, size_t n))
lutil_localtime LDAP_P ((char *s, size_t smax, const struct tm *tm, long delta))
mkstemp LDAP_P ((char *template))
 lutil_pair (ber_socket_t sd[2])
 lutil_uuidstr (char *buf, size_t len)
 lutil_uuidstr_from_normalized (char *uuid, size_t uuidlen, char *buf, size_t buflen)
 lutil_atoix (int *v, const char *s, int x)
 lutil_atoux (unsigned *v, const char *s, int x)
 lutil_atolx (long *v, const char *s, int x)
 lutil_atoulx (unsigned long *v, const char *s, int x)
 lutil_str2bin (struct berval *in, struct berval *out, void *ctx)
 lutil_parse_time (const char *in, unsigned long *tp)
 lutil_unparse_time (char *buf, size_t buflen, unsigned long t)

Class Documentation

struct lutil_tm

Definition at line 160 of file lutil.h.

Class Members
int tm_hour
int tm_mday
int tm_min
int tm_mon
int tm_sec
int tm_usec
int tm_usub
int tm_year
struct lutil_timet

Definition at line 171 of file lutil.h.

Class Members
int tt_gsec
unsigned int tt_sec
unsigned int tt_usec

Define Documentation

#define LDAP_LUTIL_GENTIME_BUFSIZE   22

Definition at line 209 of file lutil.h.

#define LDAP_LUTIL_UUIDSTR_BUFSIZE   40

Definition at line 226 of file lutil.h.

#define lutil_atoi (   v,
  s 
)    lutil_atoix((v), (s), 10)

Definition at line 304 of file lutil.h.

#define lutil_atol (   v,
  s 
)    lutil_atolx((v), (s), 10)

Definition at line 306 of file lutil.h.

#define lutil_atou (   v,
  s 
)    lutil_atoux((v), (s), 10)

Definition at line 305 of file lutil.h.

#define lutil_atoul (   v,
  s 
)    lutil_atoulx((v), (s), 10)

Definition at line 307 of file lutil.h.

#define LUTIL_BASE64_DECODE_LEN (   n)    ((n)/4*3)

Definition at line 32 of file lutil.h.

#define LUTIL_BASE64_ENCODE_LEN (   n)    (((n)+2)/3 * 4)

Definition at line 31 of file lutil.h.

#define lutil_gentime (   s,
  m,
 
)    lutil_localtime((s),(m),(t),0)

Definition at line 210 of file lutil.h.

#define LUTIL_PASSWD_ERR   (-1)

Definition at line 74 of file lutil.h.

#define LUTIL_PASSWD_OK   (0)

Definition at line 73 of file lutil.h.

#define LUTIL_SLASHPATH (   p)

Definition at line 192 of file lutil.h.

#define lutil_strbvcopy (   a,
  bv 
)    lutil_memcopy((a),(bv)->bv_val,(bv)->bv_len)

Definition at line 204 of file lutil.h.

#define lutil_timerdiv (   t,
  d,
 
)
Value:
do { \
              time_t s = (t)->tv_sec; \
              assert( d > 0 ); \
              (x)->tv_sec = s / d; \
              (x)->tv_usec = ( (t)->tv_usec + 1000000 * ( s % d ) ) / d; \
       } while ( 0 )

Definition at line 337 of file lutil.h.

#define lutil_timermul (   t,
  m,
 
)
Value:
do { \
              time_t u = (t)->tv_usec * m; \
              assert( m > 0 ); \
              (x)->tv_sec = (t)->tv_sec * m + u / 1000000; \
              (x)->tv_usec = u % 1000000; \
       } while ( 0 );

Definition at line 350 of file lutil.h.


Typedef Documentation

typedef int( LUTIL_PASSWD_CHK_FUNC)(const struct berval *scheme, const struct berval *passwd, const struct berval *cred, const char **text)

Definition at line 76 of file lutil.h.

typedef int( LUTIL_PASSWD_HASH_FUNC)(const struct berval *scheme, const struct berval *passwd, struct berval *hash, const char **text)

Definition at line 82 of file lutil.h.

typedef struct lutil_timet lutil_timet
typedef struct lutil_tm lutil_tm

Function Documentation

lutil_b64_ntop LDAP_P ( (unsigned char const *, size_t, char *, size_t)  )
lutil_b64_pton LDAP_P ( (char const *, unsigned char *, size_t)  )
lutil_detach LDAP_P ( (int debug, int do_close)  )
lutil_entropy LDAP_P ( (unsigned char *buf, ber_len_t nbytes)  )
lutil_get_filed_password LDAP_P ( (const char *filename, struct berval *)  )
lutil_passwd_add LDAP_P ( (struct berval *scheme, LUTIL_PASSWD_CHK_FUNC *chk_fn, LUTIL_PASSWD_HASH_FUNC *hash_fn)  )
lutil_authpasswd LDAP_P ( (const struct berval *passwd, const struct berval *cred, const char **methods)  )
lutil_authpasswd_hash LDAP_P ( (const struct berval *cred, struct berval **passwd, struct berval **salt, const char *method)  )
lutil_passwd LDAP_P ( (const struct berval *passwd, const struct berval *cred, const char **methods, const char **text )
lutil_passwd_hash LDAP_P ( (const struct berval *passwd, const char *method, struct berval *hash, const char **text )
lutil_passwd_scheme LDAP_P ( (const char *scheme)  )
lutil_salt_format LDAP_P ( (const char *format)  )
lutil_passwd_string64 LDAP_P ( (const struct berval *sc, const struct berval *hash, struct berval *b64, const struct berval *salt)  )
lutil_progname LDAP_P ( (const char *name, int argc, char *argv[])  )
lutil_parsetime LDAP_P ( (char *atm, struct lutil_tm *)  )
lutil_tm2time LDAP_P ( (struct lutil_tm *, struct lutil_timet *)  )
lutil_strcopy LDAP_P ( (char *dst, const char *src )
lutil_strncopy LDAP_P ( (char *dst, const char *src, size_t n )
lutil_localtime LDAP_P ( (char *s, size_t smax, const struct tm *tm, long delta)  )
mkstemp LDAP_P ( (char *template)  )
lutil_atoix ( int v,
const char *  s,
int  x 
)

Definition at line 465 of file utils.c.

{
       char          *next;
       long          i;

       assert( s != NULL );
       assert( v != NULL );

       i = strtol( s, &next, x );
       if ( next == s || next[ 0 ] != '\0' ) {
              return -1;
       }

       if ( (long)(int)i != i ) {
              return 1;
       }

       *v = (int)i;

       return 0;
}

Here is the caller graph for this function:

lutil_atolx ( long *  v,
const char *  s,
int  x 
)

Definition at line 516 of file utils.c.

{
       char *next;
       long l;
       int save_errno;

       assert( s != NULL );
       assert( v != NULL );

       if ( isspace( s[ 0 ] ) ) {
              return -1;
       }

       errno = 0;
       l = strtol( s, &next, x );
       save_errno = errno;
       if ( next == s || next[ 0 ] != '\0' ) {
              return -1;
       }

       if ( ( l == LONG_MIN || l == LONG_MAX ) && save_errno != 0 ) {
              return -1;
       }

       *v = l;

       return 0;
}

Here is the caller graph for this function:

lutil_atoulx ( unsigned long *  v,
const char *  s,
int  x 
)

Definition at line 546 of file utils.c.

{
       char *next;
       unsigned long ul;
       int save_errno;

       assert( s != NULL );
       assert( v != NULL );

       /* strtoul() has an odd interface */
       if ( s[ 0 ] == '-' || isspace( s[ 0 ] ) ) {
              return -1;
       }

       errno = 0;
       ul = strtoul( s, &next, x );
       save_errno = errno;
       if ( next == s || next[ 0 ] != '\0' ) {
              return -1;
       }

       if ( ( ul == 0 || ul == ULONG_MAX ) && save_errno != 0 ) {
              return -1;
       }

       *v = ul;

       return 0;
}

Here is the caller graph for this function:

lutil_atoux ( unsigned v,
const char *  s,
int  x 
)

Definition at line 488 of file utils.c.

{
       char          *next;
       unsigned long u;

       assert( s != NULL );
       assert( v != NULL );

       /* strtoul() has an odd interface */
       if ( s[ 0 ] == '-' ) {
              return -1;
       }

       u = strtoul( s, &next, x );
       if ( next == s || next[ 0 ] != '\0' ) {
              return -1;
       }

       if ( (unsigned long)(unsigned)u != u ) {
              return 1;
       }

       *v = u;

       return 0;
}

Here is the caller graph for this function:

lutil_pair ( ber_socket_t  sd[2])

Definition at line 32 of file sockpair.c.

{
#ifdef USE_PIPE
       return pipe( sds );
#else
       struct sockaddr_in si;
       int rc;
       ber_socklen_t len = sizeof(si);
       ber_socket_t sd;

       sd = socket( AF_INET, SOCK_DGRAM, 0 );
       if ( sd == AC_SOCKET_INVALID ) {
              return sd;
       }
       
       (void) memset( (void*) &si, '\0', len );
       si.sin_family = AF_INET;
       si.sin_port = 0;
       si.sin_addr.s_addr = htonl( INADDR_LOOPBACK );

       rc = bind( sd, (struct sockaddr *)&si, len );
       if ( rc == AC_SOCKET_ERROR ) {
              tcp_close(sd);
              return rc;
       }

       rc = getsockname( sd, (struct sockaddr *)&si, &len );
       if ( rc == AC_SOCKET_ERROR ) {
              tcp_close(sd);
              return rc;
       }

       rc = connect( sd, (struct sockaddr *)&si, len );
       if ( rc == AC_SOCKET_ERROR ) {
              tcp_close(sd);
              return rc;
       }

       sds[0] = sd;
#if !HAVE_WINSOCK
       sds[1] = dup( sds[0] );
#else
       sds[1] = sds[0];
#endif
       return 0;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

lutil_parse_time ( const char *  in,
unsigned long *  tp 
)

Definition at line 857 of file utils.c.

{
       unsigned long t = 0;
       char          *s,
                     *next;
       int           sofar = -1,
                     scale[] = { 86400, 3600, 60, 1 };

       *tp = 0;

       for ( s = (char *)in; s[ 0 ] != '\0'; ) {
              unsigned long u;
              char          *what;

              /* strtoul() has an odd interface */
              if ( s[ 0 ] == '-' ) {
                     return -1;
              }

              u = strtoul( s, &next, 10 );
              if ( next == s ) {
                     return -1;
              }

              if ( next[ 0 ] == '\0' ) {
                     /* assume seconds */
                     t += u;
                     break;
              }

              what = strchr( time_unit, next[ 0 ] );
              if ( what == NULL ) {
                     return -1;
              }

              if ( what - time_unit <= sofar ) {
                     return -1;
              }

              sofar = what - time_unit;
              t += u * scale[ sofar ];

              s = &next[ 1 ];
       }

       *tp = t;
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

lutil_str2bin ( struct berval in,
struct berval out,
void ctx 
)

Definition at line 723 of file utils.c.

{
       char *pin, *pout;
       char *end;
       int i, chunk, len, rc = 0, hex = 0;
       if ( !out || !out->bv_val || out->bv_len < in->bv_len )
              return -1;

       pout = out->bv_val;
       /* Leading "0x" for hex input */
       if ( in->bv_len > 2 && in->bv_val[0] == '0' &&
              ( in->bv_val[1] == 'x' || in->bv_val[1] == 'X' ) )
       {
              len = in->bv_len - 2;
              pin = in->bv_val + 2;
              hex = 1;
       } else if ( in->bv_len > 3 && in->bv_val[0] == '\'' &&
              in->bv_val[in->bv_len-2] == '\'' &&
              in->bv_val[in->bv_len-1] == 'H' )
       {
              len = in->bv_len - 3;
              pin = in->bv_val + 1;
              hex = 1;
       }
       if ( hex ) {
#define HEXMAX       (2 * sizeof(long))
              unsigned long l;
              char tbuf[HEXMAX+1];

              /* Convert a longword at a time, but handle leading
               * odd bytes first
               */
              chunk = len % HEXMAX;
              if ( !chunk )
                     chunk = HEXMAX;

              while ( len ) {
                     int ochunk;
                     memcpy( tbuf, pin, chunk );
                     tbuf[chunk] = '\0';
                     errno = 0;
                     l = strtoul( tbuf, &end, 16 );
                     if ( errno )
                            return -1;
                     ochunk = (chunk + 1)/2;
                     for ( i = ochunk - 1; i >= 0; i-- ) {
                            pout[i] = l & 0xff;
                            l >>= 8;
                     }
                     pin += chunk;
                     pout += ochunk;
                     len -= chunk;
                     chunk = HEXMAX;
              }
              out->bv_len = pout - out->bv_val;
       } else {
       /* Decimal */
#define       DECMAX 8      /* 8 digits at a time */
              char tmpbuf[64], *tmp;
              lutil_int_decnum num;
              int neg = 0;
              long l;
              char tbuf[DECMAX+1];

              len = in->bv_len;
              pin = in->bv_val;
              num.buf = (unsigned char *)out->bv_val;
              num.bufsiz = out->bv_len;
              num.beg = num.bufsiz-1;
              num.len = 0;
              if ( pin[0] == '-' ) {
                     neg = 0xff;
                     len--;
                     pin++;
              }

              /* tmp must be at least as large as outbuf */
              if ( out->bv_len > sizeof(tmpbuf)) {
                     tmp = ber_memalloc_x( out->bv_len, ctx );
              } else {
                     tmp = tmpbuf;
              }
              chunk = len & (DECMAX-1);
              if ( !chunk )
                     chunk = DECMAX;

              while ( len ) {
                     memcpy( tbuf, pin, chunk );
                     tbuf[chunk] = '\0';
                     errno = 0;
                     l = strtol( tbuf, &end, 10 );
                     if ( errno ) {
                            rc = -1;
                            goto decfail;
                     }
                     scale( l, &num, (unsigned char *)tmp );
                     pin += chunk;
                     len -= chunk;
                     chunk = DECMAX;
              }
              /* Negate the result */
              if ( neg ) {
                     unsigned char *ptr;

                     ptr = num.buf+num.beg;

                     /* flip all bits */
                     for ( i=0; i<num.len; i++ )
                            ptr[i] ^= 0xff;

                     /* add 1, with carry - overflow handled below */
                     while ( i-- && ! (ptr[i] = (ptr[i] + 1) & 0xff )) ;
              }
              /* Prepend sign byte if wrong sign bit */
              if (( num.buf[num.beg] ^ neg ) & 0x80 ) {
                     num.beg--;
                     num.len++;
                     num.buf[num.beg] = neg;
              }
              if ( num.beg )
                     AC_MEMCPY( num.buf, num.buf+num.beg, num.len );
              out->bv_len = num.len;
decfail:
              if ( tmp != tmpbuf ) {
                     ber_memfree_x( tmp, ctx );
              }
       }
       return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

lutil_unparse_time ( char *  buf,
size_t  buflen,
unsigned long  t 
)

Definition at line 909 of file utils.c.

{
       int           len, i;
       unsigned long v[ 4 ];
       char          *ptr = buf;

       v[ 0 ] = t/86400;
       v[ 1 ] = (t%86400)/3600;
       v[ 2 ] = (t%3600)/60;
       v[ 3 ] = t%60;

       for ( i = 0; i < 4; i++ ) {
              if ( v[i] > 0 || ( i == 3 && ptr == buf ) ) {
                     len = snprintf( ptr, buflen, "%lu%c", v[ i ], time_unit[ i ] );
                     if ( len < 0 || (unsigned)len >= buflen ) {
                            return -1;
                     }
                     buflen -= len;
                     ptr += len;
              }
       }

       return 0;
}

Here is the caller graph for this function:

lutil_uuidstr ( char *  buf,
size_t  len 
)

Definition at line 274 of file uuid.c.

{
#ifdef HAVE_UUID_TO_STR
       uuid_t uu = {0};
       unsigned rc;
       char *s;
       size_t l;

       uuid_create( &uu, &rc );
       if ( rc != uuid_s_ok ) {
              return 0;
       }

       uuid_to_str( &uu, &s, &rc );
       if ( rc != uuid_s_ok ) {
              return 0;
       }

       l = strlen( s );
       if ( l >= len ) {
              free( s );
              return 0;
       }

       strncpy( buf, s, len );
       free( s );

       return l;

#elif defined( HAVE_UUID_GENERATE )
       uuid_t uu;

       uuid_generate( uu );
       uuid_unparse_lower( uu, buf );
       return strlen( buf );
       
#elif defined( _WIN32 )
       UUID uuid;
       unsigned char *uuidstr;
       size_t uuidlen;

       if( UuidCreate( &uuid ) != RPC_S_OK ) {
              return 0;
       }
 
       if( UuidToString( &uuid, &uuidstr ) !=  RPC_S_OK ) {
              return 0;
       }

       uuidlen = strlen( uuidstr );
       if( uuidlen >= len ) {
              return 0;
       }

       strncpy( buf, uuidstr, len );
       RpcStringFree( &uuidstr );

       return uuidlen;
 
#else
       struct timeval tv;
       UI64 tl;
       unsigned char *nl;
       unsigned short t2, t3, s1;
       unsigned long t1, tl_high;
       unsigned int rc;

       /*
        * Theoretically we should delay if seq wraps within 100usec but for now
        * systems are not fast enough to worry about it.
        */
       static int inited = 0;
       static unsigned short seq;
       
       if (!inited) {
              lutil_entropy( (unsigned char *) &seq, sizeof(seq) );
              inited++;
       }

#ifdef HAVE_GETTIMEOFDAY
       gettimeofday( &tv, 0 );
#else
       time( &tv.tv_sec );
       tv.tv_usec = 0;
#endif

       tl = mul64ll(tv.tv_sec, 10000000UL);
       set_add64l(tl, tv.tv_usec * 10UL);
       set_add64(tl, UUID_TPLUS);

       nl = lutil_eaddr();

       t1 = low32(tl);                           /* time_low */
       tl_high = high32(tl);
       t2 = tl_high & 0xffff;             /* time_mid */
       t3 = ((tl_high >> 16) & 0x0fff) | 0x1000; /* time_hi_and_version */
       s1 = ( ++seq & 0x1fff ) | 0x8000;         /* clock_seq_and_reserved */

       rc = snprintf( buf, len,
              "%08lx-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
              t1, (unsigned) t2, (unsigned) t3, (unsigned) s1,
              (unsigned) nl[0], (unsigned) nl[1],
              (unsigned) nl[2], (unsigned) nl[3],
              (unsigned) nl[4], (unsigned) nl[5] );

       return rc < len ? rc : 0;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

lutil_uuidstr_from_normalized ( char *  uuid,
size_t  uuidlen,
char *  buf,
size_t  buflen 
)

Definition at line 384 of file uuid.c.

{
       unsigned char nibble;
       int i, d = 0;

       assert( uuid != NULL );
       assert( buf != NULL );

       if ( uuidlen != 16 ) return -1;
       if ( buflen < 36 ) return -1;

       for ( i = 0; i < 16; i++ ) {
              if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
                     buf[(i<<1)+d] = '-';
                     d += 1;
              }

              nibble = (uuid[i] >> 4) & 0xF;
              if ( nibble < 10 ) {
                     buf[(i<<1)+d] = nibble + '0';
              } else {
                     buf[(i<<1)+d] = nibble - 10 + 'a';
              }

              nibble = (uuid[i]) & 0xF;
              if ( nibble < 10 ) {
                     buf[(i<<1)+d+1] = nibble + '0';
              } else {
                     buf[(i<<1)+d+1] = nibble - 10 + 'a';
              }
       }

       if ( buflen > 36 ) buf[36] = '\0';
       return 36;
}

Here is the caller graph for this function: