10Sstevel@tonic-gate /*
2*3857Sstevel * Portions Copyright 1998 Sun Microsystems, Inc. All rights reserved.
3*3857Sstevel * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
70Sstevel@tonic-gate
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate * Copyright (c) 1994 Regents of the University of Michigan.
100Sstevel@tonic-gate * All rights reserved.
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * getdn.c
130Sstevel@tonic-gate */
140Sstevel@tonic-gate
150Sstevel@tonic-gate #ifndef lint
160Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
170Sstevel@tonic-gate #endif
180Sstevel@tonic-gate
190Sstevel@tonic-gate #include <stdio.h>
200Sstevel@tonic-gate #include <ctype.h>
210Sstevel@tonic-gate #include <string.h>
220Sstevel@tonic-gate #include <stdlib.h> /* malloc(), realloc(), calloc() for Solaris */
230Sstevel@tonic-gate #ifdef MACOS
240Sstevel@tonic-gate #include <stdlib.h>
250Sstevel@tonic-gate #include "macos.h"
260Sstevel@tonic-gate #else /* MACOS */
270Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
280Sstevel@tonic-gate #include <malloc.h>
290Sstevel@tonic-gate #include "msdos.h"
300Sstevel@tonic-gate #else /* DOS */
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/socket.h>
330Sstevel@tonic-gate #endif /* DOS */
340Sstevel@tonic-gate #endif /* MACOS */
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include "lber.h"
370Sstevel@tonic-gate #include "ldap.h"
380Sstevel@tonic-gate #include "ldap-private.h"
390Sstevel@tonic-gate #include "ldap-int.h"
400Sstevel@tonic-gate
410Sstevel@tonic-gate char *
ldap_get_dn(LDAP * ld,LDAPMessage * entry)420Sstevel@tonic-gate ldap_get_dn( LDAP *ld, LDAPMessage *entry )
430Sstevel@tonic-gate {
440Sstevel@tonic-gate char *dn;
450Sstevel@tonic-gate BerElement tmp;
460Sstevel@tonic-gate
470Sstevel@tonic-gate Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 181, "ldap_get_dn\n"), 0, 0, 0 );
480Sstevel@tonic-gate
490Sstevel@tonic-gate if ( entry == NULL ) {
500Sstevel@tonic-gate ld->ld_errno = LDAP_PARAM_ERROR;
510Sstevel@tonic-gate return( NULL );
520Sstevel@tonic-gate }
530Sstevel@tonic-gate
540Sstevel@tonic-gate tmp = *entry->lm_ber; /* struct copy */
550Sstevel@tonic-gate if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
560Sstevel@tonic-gate ld->ld_errno = LDAP_DECODING_ERROR;
570Sstevel@tonic-gate return( NULL );
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate return( dn );
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate char *
ldap_dn2ufn(char * dn)640Sstevel@tonic-gate ldap_dn2ufn( char *dn )
650Sstevel@tonic-gate {
660Sstevel@tonic-gate char *p, *ufn, *r;
670Sstevel@tonic-gate int state;
680Sstevel@tonic-gate
690Sstevel@tonic-gate Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 182, "ldap_dn2ufn\n"), 0, 0, 0 );
700Sstevel@tonic-gate
710Sstevel@tonic-gate if ( ldap_is_dns_dn( dn ) || ( p = strchr( dn, '=' )) == NULL )
720Sstevel@tonic-gate return( strdup( dn ));
730Sstevel@tonic-gate
740Sstevel@tonic-gate ufn = strdup( ++p );
750Sstevel@tonic-gate
760Sstevel@tonic-gate #define INQUOTE 1
770Sstevel@tonic-gate #define OUTQUOTE 2
780Sstevel@tonic-gate state = OUTQUOTE;
790Sstevel@tonic-gate for ( p = ufn, r = ufn; *p; p++ ) {
800Sstevel@tonic-gate switch ( *p ) {
810Sstevel@tonic-gate case '\\':
820Sstevel@tonic-gate if ( *++p == '\0' )
830Sstevel@tonic-gate p--;
840Sstevel@tonic-gate else {
850Sstevel@tonic-gate *r++ = '\\';
860Sstevel@tonic-gate *r++ = *p;
870Sstevel@tonic-gate }
880Sstevel@tonic-gate break;
890Sstevel@tonic-gate case '"':
900Sstevel@tonic-gate if ( state == INQUOTE )
910Sstevel@tonic-gate state = OUTQUOTE;
920Sstevel@tonic-gate else
930Sstevel@tonic-gate state = INQUOTE;
940Sstevel@tonic-gate *r++ = *p;
950Sstevel@tonic-gate break;
960Sstevel@tonic-gate case ';':
970Sstevel@tonic-gate case ',':
980Sstevel@tonic-gate if ( state == OUTQUOTE )
990Sstevel@tonic-gate *r++ = ',';
1000Sstevel@tonic-gate else
1010Sstevel@tonic-gate *r++ = *p;
1020Sstevel@tonic-gate break;
1030Sstevel@tonic-gate case '=':
1040Sstevel@tonic-gate if ( state == INQUOTE )
1050Sstevel@tonic-gate *r++ = *p;
1060Sstevel@tonic-gate else {
1070Sstevel@tonic-gate char *rsave = r;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate *r-- = '\0';
1100Sstevel@tonic-gate while ( !isspace( *r ) && *r != ';'
1110Sstevel@tonic-gate && *r != ',' && r > ufn )
1120Sstevel@tonic-gate r--;
1130Sstevel@tonic-gate r++;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate if ( strcasecmp( r, "c" )
1160Sstevel@tonic-gate && strcasecmp( r, "o" )
1170Sstevel@tonic-gate && strcasecmp( r, "ou" )
1180Sstevel@tonic-gate && strcasecmp( r, "st" )
1190Sstevel@tonic-gate && strcasecmp( r, "l" )
1200Sstevel@tonic-gate && strcasecmp( r, "cn" ) ) {
1210Sstevel@tonic-gate r = rsave;
1220Sstevel@tonic-gate *r++ = '=';
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate break;
1260Sstevel@tonic-gate default:
1270Sstevel@tonic-gate *r++ = *p;
1280Sstevel@tonic-gate break;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate *r = '\0';
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate return( ufn );
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate char **
ldap_explode_dns(char * dn)1370Sstevel@tonic-gate ldap_explode_dns( char *dn )
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate int ncomps, maxcomps;
1400Sstevel@tonic-gate char *s;
1410Sstevel@tonic-gate char **rdns;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate if ( (rdns = (char **) malloc( 8 * sizeof(char *) )) == NULL ) {
1440Sstevel@tonic-gate return( NULL );
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate maxcomps = 8;
1480Sstevel@tonic-gate ncomps = 0;
1490Sstevel@tonic-gate for ( s = strtok( dn, "@." ); s != NULL; s = strtok( NULL, "@." ) ) {
1500Sstevel@tonic-gate if ( ncomps == maxcomps ) {
1510Sstevel@tonic-gate maxcomps *= 2;
1520Sstevel@tonic-gate if ( (rdns = (char **) realloc( rdns, maxcomps *
1530Sstevel@tonic-gate sizeof(char *) )) == NULL ) {
1540Sstevel@tonic-gate return( NULL );
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate rdns[ncomps++] = strdup( s );
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate rdns[ncomps] = NULL;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate return( rdns );
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate char **
ldap_explode_dn(char * dn,int notypes)1650Sstevel@tonic-gate ldap_explode_dn( char *dn, int notypes )
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate char *p, *q, *rdnstart, **rdns = NULL;
1680Sstevel@tonic-gate int state, count = 0, endquote;
1690Sstevel@tonic-gate ssize_t len;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 183, "ldap_explode_dn\n"), 0, 0, 0 );
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if ( ldap_is_dns_dn( dn ) ) {
1740Sstevel@tonic-gate return( ldap_explode_dns( dn ) );
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate rdnstart = dn;
1780Sstevel@tonic-gate p = dn-1;
1790Sstevel@tonic-gate state = OUTQUOTE;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate do {
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate ++p;
1840Sstevel@tonic-gate switch ( *p ) {
1850Sstevel@tonic-gate case '\\':
1860Sstevel@tonic-gate if ( *++p == '\0' )
1870Sstevel@tonic-gate p--;
1880Sstevel@tonic-gate break;
1890Sstevel@tonic-gate case '"':
1900Sstevel@tonic-gate if ( state == INQUOTE )
1910Sstevel@tonic-gate state = OUTQUOTE;
1920Sstevel@tonic-gate else
1930Sstevel@tonic-gate state = INQUOTE;
1940Sstevel@tonic-gate break;
1950Sstevel@tonic-gate case ';':
1960Sstevel@tonic-gate case ',':
1970Sstevel@tonic-gate case '\0':
1980Sstevel@tonic-gate if ( state == OUTQUOTE ) {
1990Sstevel@tonic-gate ++count;
2000Sstevel@tonic-gate if ( rdns == NULL ) {
2010Sstevel@tonic-gate if (( rdns = (char **)malloc( 8
2020Sstevel@tonic-gate * sizeof( char *))) == NULL )
2030Sstevel@tonic-gate return( NULL );
2040Sstevel@tonic-gate } else if ( count >= 8 ) {
2050Sstevel@tonic-gate if (( rdns = (char **)realloc( rdns,
2060Sstevel@tonic-gate (count+1) * sizeof( char *)))
2070Sstevel@tonic-gate == NULL )
2080Sstevel@tonic-gate return( NULL );
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate rdns[ count ] = NULL;
2110Sstevel@tonic-gate endquote = 0;
2120Sstevel@tonic-gate if ( notypes ) {
2130Sstevel@tonic-gate for ( q = rdnstart;
2140Sstevel@tonic-gate q < p && *q != '='; ++q ) {
2150Sstevel@tonic-gate ;
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate if ( q < p ) {
2180Sstevel@tonic-gate rdnstart = ++q;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate if ( *rdnstart == '"' ) {
2210Sstevel@tonic-gate ++rdnstart;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate if ( *(p-1) == '"' ) {
2250Sstevel@tonic-gate endquote = 1;
2260Sstevel@tonic-gate --p;
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate len = p - rdnstart;
2310Sstevel@tonic-gate if (( rdns[ count-1 ] = (char *)calloc( 1,
2320Sstevel@tonic-gate len + 1 )) != NULL ) {
2330Sstevel@tonic-gate (void) SAFEMEMCPY( rdns[ count-1 ], rdnstart,
2340Sstevel@tonic-gate len );
2350Sstevel@tonic-gate rdns[ count-1 ][ len ] = '\0';
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate * Don't forget to increment 'p' back to where
2400Sstevel@tonic-gate * it should be. If we don't, then we will
2410Sstevel@tonic-gate * never get past an "end quote."
2420Sstevel@tonic-gate */
2430Sstevel@tonic-gate if ( endquote == 1 )
2440Sstevel@tonic-gate p++;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate rdnstart = *p ? p + 1 : p;
2470Sstevel@tonic-gate while ( isspace( *rdnstart ))
2480Sstevel@tonic-gate ++rdnstart;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate break;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate } while ( *p );
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate return( rdns );
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate int
ldap_is_dns_dn(char * dn)2590Sstevel@tonic-gate ldap_is_dns_dn( char *dn )
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate return( dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
2620Sstevel@tonic-gate strchr( dn, ',' ) == NULL );
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate #if defined( ultrix ) || defined( NeXT )
2670Sstevel@tonic-gate
strdup(char * s)2680Sstevel@tonic-gate char *strdup( char *s )
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate char *p;
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if ( (p = (char *) malloc( strlen( s ) + 1 )) == NULL )
2730Sstevel@tonic-gate return( NULL );
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate strcpy( p, s );
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate return( p );
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate #endif /* ultrix */
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate * Convert a DNS domain name into an X.500 distinguished name.
2850Sstevel@tonic-gate * For example, "sales.wiz.com" -> "dc=sales,dc=wiz,dc=com"
2860Sstevel@tonic-gate *
2870Sstevel@tonic-gate * If an error is encountered zero is returned, otherwise a string
2880Sstevel@tonic-gate * distinguished name and the number of nameparts is returned.
2890Sstevel@tonic-gate * The caller should free the returned string if it is non-zero.
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate char *
ldap_dns_to_dn(char * dns_name,int * nameparts)2930Sstevel@tonic-gate ldap_dns_to_dn(
2940Sstevel@tonic-gate char *dns_name,
2950Sstevel@tonic-gate int *nameparts
2960Sstevel@tonic-gate )
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate size_t dns_len;
2990Sstevel@tonic-gate char *dn = 0;
3000Sstevel@tonic-gate char *cp;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate /* check for NULL string, empty name and name ending in '.' */
3030Sstevel@tonic-gate if (dns_name && (dns_len = strlen(dns_name)) &&
3040Sstevel@tonic-gate (dns_name[dns_len - 1] != '.')) {
3050Sstevel@tonic-gate if (dn = (char *)malloc(dns_len * 3 + 1)) {
3060Sstevel@tonic-gate *nameparts = 0;
3070Sstevel@tonic-gate cp = dn;
3080Sstevel@tonic-gate while (*dns_name) {
3090Sstevel@tonic-gate *cp++ = 'd';
3100Sstevel@tonic-gate *cp++ = 'c';
3110Sstevel@tonic-gate *cp++ = '=';
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate while (*dns_name && (*dns_name != '.')) {
3140Sstevel@tonic-gate *cp++ = *dns_name++;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate if (*dns_name == '.') {
3170Sstevel@tonic-gate dns_name++;
3180Sstevel@tonic-gate *cp++ = ',';
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate (*nameparts)++;
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate *cp = '\0';
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate return (dn);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate char **
ldap_explode_rdn(char * rdn,int notypes)3290Sstevel@tonic-gate ldap_explode_rdn( char *rdn, int notypes )
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate char *p, *q, *rdnstart, **rdncomps = NULL;
3320Sstevel@tonic-gate int state, count = 0, endquote;
3330Sstevel@tonic-gate size_t len;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 183, "ldap_explode_rdn\n"), 0, 0, 0 );
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate rdnstart = rdn;
3380Sstevel@tonic-gate p = rdn-1;
3390Sstevel@tonic-gate state = OUTQUOTE;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate do {
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate ++p;
3440Sstevel@tonic-gate switch ( *p ) {
3450Sstevel@tonic-gate case '\\':
3460Sstevel@tonic-gate if ( *++p == '\0' )
3470Sstevel@tonic-gate p--;
3480Sstevel@tonic-gate break;
3490Sstevel@tonic-gate case '"':
3500Sstevel@tonic-gate if ( state == INQUOTE )
3510Sstevel@tonic-gate state = OUTQUOTE;
3520Sstevel@tonic-gate else
3530Sstevel@tonic-gate state = INQUOTE;
3540Sstevel@tonic-gate break;
3550Sstevel@tonic-gate case '+':
3560Sstevel@tonic-gate case '\0':
3570Sstevel@tonic-gate if ( state == OUTQUOTE ) {
3580Sstevel@tonic-gate ++count;
3590Sstevel@tonic-gate if ( rdncomps == NULL ) {
3600Sstevel@tonic-gate if (( rdncomps = (char **)malloc( 8 * sizeof( char *))) == NULL )
3610Sstevel@tonic-gate return( NULL );
3620Sstevel@tonic-gate } else if ( count >= 8 ) {
3630Sstevel@tonic-gate if (( rdncomps = (char **)realloc( rdncomps,
3640Sstevel@tonic-gate (count+1) * sizeof( char *)))
3650Sstevel@tonic-gate == NULL )
3660Sstevel@tonic-gate return( NULL );
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate rdncomps[ count ] = NULL;
3690Sstevel@tonic-gate endquote = 0;
3700Sstevel@tonic-gate if ( notypes ) {
3710Sstevel@tonic-gate for ( q = rdnstart;
3720Sstevel@tonic-gate q < p && *q != '='; ++q ) {
3730Sstevel@tonic-gate ;
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate if ( q < p ) {
3760Sstevel@tonic-gate rdnstart = ++q;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate if ( *rdnstart == '"' ) {
3790Sstevel@tonic-gate ++rdnstart;
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate if ( *(p-1) == '"' ) {
3830Sstevel@tonic-gate endquote = 1;
3840Sstevel@tonic-gate --p;
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate len = p - rdnstart;
3890Sstevel@tonic-gate if (( rdncomps[ count-1 ] = (char *)calloc( 1, len + 1 )) != NULL ) {
3900Sstevel@tonic-gate SAFEMEMCPY( rdncomps[ count-1 ], rdnstart,
3910Sstevel@tonic-gate len );
3920Sstevel@tonic-gate rdncomps[ count-1 ][ len ] = '\0';
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * Don't forget to increment 'p' back to where
3970Sstevel@tonic-gate * it should be. If we don't, then we will
3980Sstevel@tonic-gate * never get past an "end quote."
3990Sstevel@tonic-gate */
4000Sstevel@tonic-gate if ( endquote == 1 )
4010Sstevel@tonic-gate p++;
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate rdnstart = *p ? p + 1 : p;
4040Sstevel@tonic-gate while ( isspace( *rdnstart ))
4050Sstevel@tonic-gate ++rdnstart;
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate break;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate } while ( *p );
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate return( rdncomps );
4120Sstevel@tonic-gate }
413