1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate /*
11*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
12*0Sstevel@tonic-gate  *  All rights reserved.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  *  friendly.c
15*0Sstevel@tonic-gate  */
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #ifndef lint
18*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
19*0Sstevel@tonic-gate #endif
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include <stdio.h>
22*0Sstevel@tonic-gate #include <ctype.h>
23*0Sstevel@tonic-gate #include <string.h>
24*0Sstevel@tonic-gate #include <stdlib.h> /* malloc(),  free() for Solaris */
25*0Sstevel@tonic-gate #ifdef MACOS
26*0Sstevel@tonic-gate #include <stdlib.h>
27*0Sstevel@tonic-gate #include "macos.h"
28*0Sstevel@tonic-gate #endif /* MACOS */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
31*0Sstevel@tonic-gate #include <malloc.h>
32*0Sstevel@tonic-gate #include "msdos.h"
33*0Sstevel@tonic-gate #endif /* DOS */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/socket.h>
39*0Sstevel@tonic-gate #endif
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "lber.h"
42*0Sstevel@tonic-gate #include "ldap.h"
43*0Sstevel@tonic-gate #include "ldap-private.h"
44*0Sstevel@tonic-gate #include "ldap-int.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate char *
47*0Sstevel@tonic-gate ldap_friendly_name( char *filename, char *uname, FriendlyMap **map )
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	int	i, entries;
50*0Sstevel@tonic-gate 	FILE	*fp;
51*0Sstevel@tonic-gate 	char	*s;
52*0Sstevel@tonic-gate 	char	buf[BUFSIZ];
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	if ( map == NULL ) {
55*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
56*0Sstevel@tonic-gate 		errno = EINVAL;
57*0Sstevel@tonic-gate #endif
58*0Sstevel@tonic-gate 		return( uname );
59*0Sstevel@tonic-gate 	}
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	if ( *map == NULL ) {
62*0Sstevel@tonic-gate 		if ( (fp = fopen( filename, "r" )) == NULL )
63*0Sstevel@tonic-gate 			return( uname );
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 		entries = 0;
66*0Sstevel@tonic-gate 		while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
67*0Sstevel@tonic-gate 			if ( buf[0] != '#' )
68*0Sstevel@tonic-gate 				entries++;
69*0Sstevel@tonic-gate 		}
70*0Sstevel@tonic-gate 		rewind( fp );
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 		if ( (*map = (FriendlyMap *) malloc( (entries + 1) *
73*0Sstevel@tonic-gate 		    sizeof(FriendlyMap) )) == NULL ) {
74*0Sstevel@tonic-gate 			(void) fclose( fp );
75*0Sstevel@tonic-gate 			return( uname );
76*0Sstevel@tonic-gate 		}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 		i = 0;
79*0Sstevel@tonic-gate 		while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
80*0Sstevel@tonic-gate 			if ( buf[0] == '#' )
81*0Sstevel@tonic-gate 				continue;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 			if ( (s = strchr( buf, '\n' )) != NULL )
84*0Sstevel@tonic-gate 				*s = '\0';
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 			if ( (s = strchr( buf, '\t' )) == NULL )
87*0Sstevel@tonic-gate 				continue;
88*0Sstevel@tonic-gate 			*s++ = '\0';
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 			if ( *s == '"' ) {
91*0Sstevel@tonic-gate 				int	esc = 0, found = 0;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 				for ( ++s; *s && !found; s++ ) {
94*0Sstevel@tonic-gate 					switch ( *s ) {
95*0Sstevel@tonic-gate 					case '\\':
96*0Sstevel@tonic-gate 						esc = 1;
97*0Sstevel@tonic-gate 						break;
98*0Sstevel@tonic-gate 					case '"':
99*0Sstevel@tonic-gate 						if ( !esc )
100*0Sstevel@tonic-gate 							found = 1;
101*0Sstevel@tonic-gate 						/* FALL */
102*0Sstevel@tonic-gate 					default:
103*0Sstevel@tonic-gate 						esc = 0;
104*0Sstevel@tonic-gate 						break;
105*0Sstevel@tonic-gate 					}
106*0Sstevel@tonic-gate 				}
107*0Sstevel@tonic-gate 			}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 			(*map)[i].f_unfriendly = strdup( buf );
110*0Sstevel@tonic-gate 			(*map)[i].f_friendly = strdup( s );
111*0Sstevel@tonic-gate 			i++;
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		(void) fclose( fp );
115*0Sstevel@tonic-gate 		(*map)[i].f_unfriendly = NULL;
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
119*0Sstevel@tonic-gate 		if ( strcasecmp( uname, (*map)[i].f_unfriendly ) == 0 )
120*0Sstevel@tonic-gate 			return( (*map)[i].f_friendly );
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	return( uname );
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate void
127*0Sstevel@tonic-gate ldap_free_friendlymap( FriendlyMap **map )
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	struct friendly* pF = *map;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if ( pF == NULL )
132*0Sstevel@tonic-gate 		return;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	while ( pF->f_unfriendly )
135*0Sstevel@tonic-gate 	{
136*0Sstevel@tonic-gate 		free( pF->f_unfriendly );
137*0Sstevel@tonic-gate 		free( pF->f_friendly );
138*0Sstevel@tonic-gate 		pF++;
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 	free( *map );
141*0Sstevel@tonic-gate 	*map = NULL;
142*0Sstevel@tonic-gate }
143