xref: /onnv-gate/usr/src/lib/libldap4/common/friendly.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 
80Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
90Sstevel@tonic-gate 
100Sstevel@tonic-gate /*
110Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
120Sstevel@tonic-gate  *  All rights reserved.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  *  friendly.c
150Sstevel@tonic-gate  */
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #ifndef lint
180Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
190Sstevel@tonic-gate #endif
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #include <stdio.h>
220Sstevel@tonic-gate #include <ctype.h>
230Sstevel@tonic-gate #include <string.h>
240Sstevel@tonic-gate #include <stdlib.h> /* malloc(),  free() for Solaris */
250Sstevel@tonic-gate #ifdef MACOS
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include "macos.h"
280Sstevel@tonic-gate #endif /* MACOS */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
310Sstevel@tonic-gate #include <malloc.h>
320Sstevel@tonic-gate #include "msdos.h"
330Sstevel@tonic-gate #endif /* DOS */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/socket.h>
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "lber.h"
420Sstevel@tonic-gate #include "ldap.h"
430Sstevel@tonic-gate #include "ldap-private.h"
440Sstevel@tonic-gate #include "ldap-int.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate char *
ldap_friendly_name(char * filename,char * uname,FriendlyMap ** map)470Sstevel@tonic-gate ldap_friendly_name( char *filename, char *uname, FriendlyMap **map )
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	int	i, entries;
500Sstevel@tonic-gate 	FILE	*fp;
510Sstevel@tonic-gate 	char	*s;
520Sstevel@tonic-gate 	char	buf[BUFSIZ];
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if ( map == NULL ) {
550Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
560Sstevel@tonic-gate 		errno = EINVAL;
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate 		return( uname );
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if ( *map == NULL ) {
620Sstevel@tonic-gate 		if ( (fp = fopen( filename, "r" )) == NULL )
630Sstevel@tonic-gate 			return( uname );
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 		entries = 0;
660Sstevel@tonic-gate 		while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
670Sstevel@tonic-gate 			if ( buf[0] != '#' )
680Sstevel@tonic-gate 				entries++;
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 		rewind( fp );
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 		if ( (*map = (FriendlyMap *) malloc( (entries + 1) *
730Sstevel@tonic-gate 		    sizeof(FriendlyMap) )) == NULL ) {
740Sstevel@tonic-gate 			(void) fclose( fp );
750Sstevel@tonic-gate 			return( uname );
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 		i = 0;
790Sstevel@tonic-gate 		while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
800Sstevel@tonic-gate 			if ( buf[0] == '#' )
810Sstevel@tonic-gate 				continue;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 			if ( (s = strchr( buf, '\n' )) != NULL )
840Sstevel@tonic-gate 				*s = '\0';
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 			if ( (s = strchr( buf, '\t' )) == NULL )
870Sstevel@tonic-gate 				continue;
880Sstevel@tonic-gate 			*s++ = '\0';
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 			if ( *s == '"' ) {
910Sstevel@tonic-gate 				int	esc = 0, found = 0;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 				for ( ++s; *s && !found; s++ ) {
940Sstevel@tonic-gate 					switch ( *s ) {
950Sstevel@tonic-gate 					case '\\':
960Sstevel@tonic-gate 						esc = 1;
970Sstevel@tonic-gate 						break;
980Sstevel@tonic-gate 					case '"':
990Sstevel@tonic-gate 						if ( !esc )
1000Sstevel@tonic-gate 							found = 1;
1010Sstevel@tonic-gate 						/* FALL */
1020Sstevel@tonic-gate 					default:
1030Sstevel@tonic-gate 						esc = 0;
1040Sstevel@tonic-gate 						break;
1050Sstevel@tonic-gate 					}
1060Sstevel@tonic-gate 				}
1070Sstevel@tonic-gate 			}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 			(*map)[i].f_unfriendly = strdup( buf );
1100Sstevel@tonic-gate 			(*map)[i].f_friendly = strdup( s );
1110Sstevel@tonic-gate 			i++;
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		(void) fclose( fp );
1150Sstevel@tonic-gate 		(*map)[i].f_unfriendly = NULL;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
1190Sstevel@tonic-gate 		if ( strcasecmp( uname, (*map)[i].f_unfriendly ) == 0 )
1200Sstevel@tonic-gate 			return( (*map)[i].f_friendly );
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	return( uname );
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate void
ldap_free_friendlymap(FriendlyMap ** map)1270Sstevel@tonic-gate ldap_free_friendlymap( FriendlyMap **map )
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	struct friendly* pF = *map;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if ( pF == NULL )
1320Sstevel@tonic-gate 		return;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	while ( pF->f_unfriendly )
1350Sstevel@tonic-gate 	{
1360Sstevel@tonic-gate 		free( pF->f_unfriendly );
1370Sstevel@tonic-gate 		free( pF->f_friendly );
1380Sstevel@tonic-gate 		pF++;
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	free( *map );
1410Sstevel@tonic-gate 	*map = NULL;
1420Sstevel@tonic-gate }
143