xref: /illumos-gate/usr/src/lib/libldap5/sources/ldap/ssldap/ldapsinit.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
8*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
9*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
10*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
13*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
15*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
18*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
21*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
22*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
23*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * Contributor(s):
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * ldapsinit.c
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #if defined(NET_SSL)
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #if defined( _WINDOWS )
35*7c478bd9Sstevel@tonic-gate #include <windows.h>
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /* XXX:mhein The following is a workaround for the redefinition of */
39*7c478bd9Sstevel@tonic-gate /*           const problem on OSF.  Fix to be provided by NSS */
40*7c478bd9Sstevel@tonic-gate /*           This is a pretty benign workaround for us which */
41*7c478bd9Sstevel@tonic-gate /*           should not cause problems in the future even if */
42*7c478bd9Sstevel@tonic-gate /*           we forget to take it out :-) */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #ifdef OSF1V4D
45*7c478bd9Sstevel@tonic-gate #ifndef __STDC__
46*7c478bd9Sstevel@tonic-gate #  define __STDC__
47*7c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
48*7c478bd9Sstevel@tonic-gate #endif /* OSF1V4D */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include <errno.h>
51*7c478bd9Sstevel@tonic-gate #include <nspr.h>
52*7c478bd9Sstevel@tonic-gate #include <cert.h>
53*7c478bd9Sstevel@tonic-gate #include <key.h>
54*7c478bd9Sstevel@tonic-gate #include <ssl.h>
55*7c478bd9Sstevel@tonic-gate #include <sslproto.h>
56*7c478bd9Sstevel@tonic-gate #include <sslerr.h>
57*7c478bd9Sstevel@tonic-gate #include <prnetdb.h>
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #include <ldap.h>
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #include <ldappr.h>
62*7c478bd9Sstevel@tonic-gate #include <pk11func.h>
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
65*7c478bd9Sstevel@tonic-gate #include "solaris-int.h"
66*7c478bd9Sstevel@tonic-gate #include <libintl.h>
67*7c478bd9Sstevel@tonic-gate #include <syslog.h>
68*7c478bd9Sstevel@tonic-gate #include <nsswitch.h>
69*7c478bd9Sstevel@tonic-gate #include <synch.h>
70*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
71*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #define	HOST_BUF_SIZE	2048
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate #ifndef INADDR_NONE
76*7c478bd9Sstevel@tonic-gate #define	INADDR_NONE (-1)
77*7c478bd9Sstevel@tonic-gate #endif
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate extern int
80*7c478bd9Sstevel@tonic-gate str2hostent(const char *instr, int lenstr, void *ent, char *buffer,
81*7c478bd9Sstevel@tonic-gate 		int buflen);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate extern int
84*7c478bd9Sstevel@tonic-gate str2hostent6(const char *instr, int lenstr, void *ent, char *buffer,
85*7c478bd9Sstevel@tonic-gate 		int buflen);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate extern LDAPHostEnt *
88*7c478bd9Sstevel@tonic-gate _ns_gethostbyaddr(LDAP *ld, const char *addr, int length, int type,
89*7c478bd9Sstevel@tonic-gate 	LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
90*7c478bd9Sstevel@tonic-gate 	void *extradata);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate static char *host_service = NULL;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root_hosts);
95*7c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root_ipnodes);
96*7c478bd9Sstevel@tonic-gate #endif
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * Data structure to hold the standard NSPR I/O function pointers set by
100*7c478bd9Sstevel@tonic-gate  * libprldap.   We save them in our session data structure so we can call
101*7c478bd9Sstevel@tonic-gate  * them from our own I/O functions (we add functionality to support SSL
102*7c478bd9Sstevel@tonic-gate  * while using libprldap's functions as much as possible).
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate typedef struct ldapssl_std_functions {
105*7c478bd9Sstevel@tonic-gate     LDAP_X_EXTIOF_CLOSE_CALLBACK		*lssf_close_fn;
106*7c478bd9Sstevel@tonic-gate     LDAP_X_EXTIOF_CONNECT_CALLBACK		*lssf_connect_fn;
107*7c478bd9Sstevel@tonic-gate     LDAP_X_EXTIOF_DISPOSEHANDLE_CALLBACK	*lssf_disposehdl_fn;
108*7c478bd9Sstevel@tonic-gate } LDAPSSLStdFunctions;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * LDAP session data structure.
114*7c478bd9Sstevel@tonic-gate  */
115*7c478bd9Sstevel@tonic-gate typedef struct ldapssl_session_info {
116*7c478bd9Sstevel@tonic-gate     int                 lssei_using_pcks_fns;
117*7c478bd9Sstevel@tonic-gate     int                 lssei_ssl_strength;
118*7c478bd9Sstevel@tonic-gate     char                *lssei_certnickname;
119*7c478bd9Sstevel@tonic-gate     char                *lssei_keypasswd;
120*7c478bd9Sstevel@tonic-gate     LDAPSSLStdFunctions lssei_std_functions;
121*7c478bd9Sstevel@tonic-gate     CERTCertDBHandle    *lssei_certdbh;
122*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
123*7c478bd9Sstevel@tonic-gate 	/*
124*7c478bd9Sstevel@tonic-gate 	 * This is a hack.
125*7c478bd9Sstevel@tonic-gate 	 * ld is used so that we can use libldap's gethostbyaddr
126*7c478bd9Sstevel@tonic-gate 	 * resolver. This is needed to prevent recursion with libsldap.
127*7c478bd9Sstevel@tonic-gate 	 */
128*7c478bd9Sstevel@tonic-gate     LDAP		*ld;
129*7c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
130*7c478bd9Sstevel@tonic-gate } LDAPSSLSessionInfo;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /*
134*7c478bd9Sstevel@tonic-gate  * LDAP socket data structure.
135*7c478bd9Sstevel@tonic-gate  */
136*7c478bd9Sstevel@tonic-gate typedef struct ldapssl_socket_info {
137*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo	*soi_sessioninfo;	/* session info */
138*7c478bd9Sstevel@tonic-gate } LDAPSSLSocketInfo;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate /*
142*7c478bd9Sstevel@tonic-gate  * XXXceb  This is a hack until the new IO functions are done.
143*7c478bd9Sstevel@tonic-gate  * this function MUST be called before ldap_enable_clienauth.
144*7c478bd9Sstevel@tonic-gate  * right now, this function is called in ldapssl_pkcs_init();
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate static int using_pkcs_functions = 0;
148*7c478bd9Sstevel@tonic-gate 
set_using_pkcs_functions(int val)149*7c478bd9Sstevel@tonic-gate void set_using_pkcs_functions( int val )
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate     using_pkcs_functions = val;
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate  * Utility functions:
157*7c478bd9Sstevel@tonic-gate  */
158*7c478bd9Sstevel@tonic-gate static void ldapssl_free_session_info( LDAPSSLSessionInfo **ssipp );
159*7c478bd9Sstevel@tonic-gate static void ldapssl_free_socket_info( LDAPSSLSocketInfo **soipp );
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate  *  SSL Stuff
164*7c478bd9Sstevel@tonic-gate  */
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate static int ldapssl_AuthCertificate(void *sessionarg, PRFileDesc *fd,
167*7c478bd9Sstevel@tonic-gate         PRBool checkSig, PRBool isServer);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  * client auth stuff
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate static int get_clientauth_data( void *sessionarg, PRFileDesc *prfd,
173*7c478bd9Sstevel@tonic-gate 	CERTDistNames *caNames,  CERTCertificate **pRetCert,
174*7c478bd9Sstevel@tonic-gate 	SECKEYPrivateKey **pRetKey );
175*7c478bd9Sstevel@tonic-gate static int get_keyandcert( LDAPSSLSessionInfo *ssip,
176*7c478bd9Sstevel@tonic-gate 	CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey,
177*7c478bd9Sstevel@tonic-gate 	char **errmsgp );
178*7c478bd9Sstevel@tonic-gate static int check_clientauth_nicknames_and_passwd( LDAP *ld,
179*7c478bd9Sstevel@tonic-gate 	LDAPSSLSessionInfo *ssip );
180*7c478bd9Sstevel@tonic-gate static char *get_keypassword( PK11SlotInfo *slot, PRBool retry,
181*7c478bd9Sstevel@tonic-gate 	void *sessionarg );
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate  * Static variables.
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
187*7c478bd9Sstevel@tonic-gate static int default_ssl_strength = LDAPSSL_AUTH_CNCHECK;
188*7c478bd9Sstevel@tonic-gate #else
189*7c478bd9Sstevel@tonic-gate static int default_ssl_strength = LDAPSSL_AUTH_CERT;
190*7c478bd9Sstevel@tonic-gate #endif
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate /*
193*7c478bd9Sstevel@tonic-gate  * Like ldap_init(), except also install I/O routines from libsec so we
194*7c478bd9Sstevel@tonic-gate  * can support SSL.  If defsecure is non-zero, SSL is enabled for the
195*7c478bd9Sstevel@tonic-gate  * default connection as well.
196*7c478bd9Sstevel@tonic-gate  */
197*7c478bd9Sstevel@tonic-gate LDAP *
198*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldapssl_init(const char * defhost,int defport,int defsecure)199*7c478bd9Sstevel@tonic-gate ldapssl_init( const char *defhost, int defport, int defsecure )
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate     LDAP	*ld;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate #ifndef LDAP_SSLIO_HOOKS
205*7c478bd9Sstevel@tonic-gate     return( NULL );
206*7c478bd9Sstevel@tonic-gate #else
207*7c478bd9Sstevel@tonic-gate     if (0 ==defport)
208*7c478bd9Sstevel@tonic-gate 	defport = LDAPS_PORT;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate     if (( ld = ldap_init( defhost, defport )) == NULL ) {
211*7c478bd9Sstevel@tonic-gate 	return( NULL );
212*7c478bd9Sstevel@tonic-gate     }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate     if ( ldapssl_install_routines( ld ) < 0 || ldap_set_option( ld,
215*7c478bd9Sstevel@tonic-gate 		LDAP_OPT_SSL, defsecure ? LDAP_OPT_ON : LDAP_OPT_OFF ) != 0 ) {
216*7c478bd9Sstevel@tonic-gate 	PR_SetError( PR_UNKNOWN_ERROR, EINVAL );  /* XXXmcs: just a guess! */
217*7c478bd9Sstevel@tonic-gate 	ldap_unbind( ld );
218*7c478bd9Sstevel@tonic-gate 	return( NULL );
219*7c478bd9Sstevel@tonic-gate     }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate     return( ld );
222*7c478bd9Sstevel@tonic-gate #endif
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate static int
ldapssl_close(int s,struct lextiof_socket_private * socketarg)227*7c478bd9Sstevel@tonic-gate ldapssl_close(int s, struct lextiof_socket_private *socketarg)
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate     PRLDAPSocketInfo	soi;
230*7c478bd9Sstevel@tonic-gate     LDAPSSLSocketInfo	*ssoip;
231*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo	*sseip;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate     memset( &soi, 0, sizeof(soi));
234*7c478bd9Sstevel@tonic-gate     soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
235*7c478bd9Sstevel@tonic-gate     if ( prldap_get_socket_info( s, socketarg, &soi ) != LDAP_SUCCESS ) {
236*7c478bd9Sstevel@tonic-gate 	return( -1 );
237*7c478bd9Sstevel@tonic-gate     }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate     ssoip = (LDAPSSLSocketInfo *)soi.soinfo_appdata;
240*7c478bd9Sstevel@tonic-gate     sseip = ssoip->soi_sessioninfo;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate     ldapssl_free_socket_info( (LDAPSSLSocketInfo **)&soi.soinfo_appdata );
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate     return( (*(sseip->lssei_std_functions.lssf_close_fn))( s, socketarg ));
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate static int
do_ldapssl_connect(const char * hostlist,int defport,int timeout,unsigned long options,struct lextiof_session_private * sessionarg,struct lextiof_socket_private ** socketargp,int clientauth)248*7c478bd9Sstevel@tonic-gate do_ldapssl_connect(const char *hostlist, int defport, int timeout,
249*7c478bd9Sstevel@tonic-gate 	unsigned long options, struct lextiof_session_private *sessionarg,
250*7c478bd9Sstevel@tonic-gate 	struct lextiof_socket_private **socketargp, int clientauth )
251*7c478bd9Sstevel@tonic-gate {
252*7c478bd9Sstevel@tonic-gate     int			intfd = -1;
253*7c478bd9Sstevel@tonic-gate     PRBool		secure;
254*7c478bd9Sstevel@tonic-gate     PRLDAPSessionInfo	sei;
255*7c478bd9Sstevel@tonic-gate     PRLDAPSocketInfo	soi;
256*7c478bd9Sstevel@tonic-gate     LDAPSSLSocketInfo	*ssoip = NULL;
257*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo	*sseip;
258*7c478bd9Sstevel@tonic-gate     PRFileDesc		*sslfd = NULL;
259*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
260*7c478bd9Sstevel@tonic-gate     int			port;
261*7c478bd9Sstevel@tonic-gate     int			parse_err;
262*7c478bd9Sstevel@tonic-gate     char		*host = NULL;
263*7c478bd9Sstevel@tonic-gate     char		*name;
264*7c478bd9Sstevel@tonic-gate     struct ldap_x_hostlist_status
265*7c478bd9Sstevel@tonic-gate 			*status = NULL;
266*7c478bd9Sstevel@tonic-gate     in_addr_t		addr_ipv4;
267*7c478bd9Sstevel@tonic-gate     in6_addr_t		addr_ipv6;
268*7c478bd9Sstevel@tonic-gate     char		*host_buf;
269*7c478bd9Sstevel@tonic-gate     LDAPHostEnt		*hent;
270*7c478bd9Sstevel@tonic-gate     LDAPHostEnt		host_ent;
271*7c478bd9Sstevel@tonic-gate     int			stat;
272*7c478bd9Sstevel@tonic-gate     int			type;
273*7c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate     /*
276*7c478bd9Sstevel@tonic-gate      * Determine if secure option is set.  Also, clear secure bit in options
277*7c478bd9Sstevel@tonic-gate      * the we pass to the standard connect() function (since it doesn't know
278*7c478bd9Sstevel@tonic-gate      * how to handle the secure option).
279*7c478bd9Sstevel@tonic-gate      */
280*7c478bd9Sstevel@tonic-gate     if ( 0 != ( options & LDAP_X_EXTIOF_OPT_SECURE )) {
281*7c478bd9Sstevel@tonic-gate 	secure = PR_TRUE;
282*7c478bd9Sstevel@tonic-gate 	options &= ~LDAP_X_EXTIOF_OPT_SECURE;
283*7c478bd9Sstevel@tonic-gate     } else {
284*7c478bd9Sstevel@tonic-gate 	secure = PR_FALSE;
285*7c478bd9Sstevel@tonic-gate     }
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate     /*
288*7c478bd9Sstevel@tonic-gate      * Retrieve session info. so we can store a pointer to our session info.
289*7c478bd9Sstevel@tonic-gate      * in our socket info. later.
290*7c478bd9Sstevel@tonic-gate      */
291*7c478bd9Sstevel@tonic-gate     memset( &sei, 0, sizeof(sei));
292*7c478bd9Sstevel@tonic-gate     sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
293*7c478bd9Sstevel@tonic-gate     if ( prldap_get_session_info( NULL, sessionarg, &sei ) != LDAP_SUCCESS ) {
294*7c478bd9Sstevel@tonic-gate 	return( -1 );
295*7c478bd9Sstevel@tonic-gate     }
296*7c478bd9Sstevel@tonic-gate     sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate     /*
299*7c478bd9Sstevel@tonic-gate      * Call the standard connect() callback to make the TCP connection.
300*7c478bd9Sstevel@tonic-gate      * If it succeeds, *socketargp is set.
301*7c478bd9Sstevel@tonic-gate      */
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate     intfd = (*(sseip->lssei_std_functions.lssf_connect_fn))( hostlist, defport,
304*7c478bd9Sstevel@tonic-gate 		timeout, options, sessionarg, socketargp
305*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
306*7c478bd9Sstevel@tonic-gate 		, &host );
307*7c478bd9Sstevel@tonic-gate #else
308*7c478bd9Sstevel@tonic-gate 		);
309*7c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate     if ( intfd < 0 ) {
312*7c478bd9Sstevel@tonic-gate 	return( intfd );
313*7c478bd9Sstevel@tonic-gate     }
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
316*7c478bd9Sstevel@tonic-gate         /*
317*7c478bd9Sstevel@tonic-gate          * Determine if the "host name" is an ip address. If so,
318*7c478bd9Sstevel@tonic-gate          * we must look up the actual host name corresponding to
319*7c478bd9Sstevel@tonic-gate          * it.
320*7c478bd9Sstevel@tonic-gate          */
321*7c478bd9Sstevel@tonic-gate 	if ( NULL == host ) {
322*7c478bd9Sstevel@tonic-gate 		goto close_socket_and_exit_with_error;
323*7c478bd9Sstevel@tonic-gate 	}
324*7c478bd9Sstevel@tonic-gate         type = AF_UNSPEC;
325*7c478bd9Sstevel@tonic-gate         if (strlen(host) < INET6_ADDRSTRLEN &&
326*7c478bd9Sstevel@tonic-gate                         inet_pton(AF_INET6, host, &addr_ipv6) == 1) {
327*7c478bd9Sstevel@tonic-gate                 type = AF_INET6;
328*7c478bd9Sstevel@tonic-gate         } else if (strlen(host) < INET_ADDRSTRLEN &&
329*7c478bd9Sstevel@tonic-gate                         inet_pton(AF_INET, host, &addr_ipv4) == 1) {
330*7c478bd9Sstevel@tonic-gate                 type = AF_INET;
331*7c478bd9Sstevel@tonic-gate         }
332*7c478bd9Sstevel@tonic-gate         if (type == AF_INET || type == AF_INET6) {
333*7c478bd9Sstevel@tonic-gate                 host_buf = malloc(HOST_BUF_SIZE);
334*7c478bd9Sstevel@tonic-gate                 if (host_buf == NULL) {
335*7c478bd9Sstevel@tonic-gate 			/* will free host in close_socket_and_exit_with_error */
336*7c478bd9Sstevel@tonic-gate                         goto close_socket_and_exit_with_error;
337*7c478bd9Sstevel@tonic-gate 		}
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate                 /* Call ldap layer's gethostbyaddr resolver */
340*7c478bd9Sstevel@tonic-gate                 hent = _ns_gethostbyaddr(sseip->ld, host, strlen(host), type,
341*7c478bd9Sstevel@tonic-gate                         &host_ent, host_buf, HOST_BUF_SIZE, &stat, NULL);
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate                 /* If we are unable to lookup the host addr, we fail! */
344*7c478bd9Sstevel@tonic-gate                 if (hent == NULL) {
345*7c478bd9Sstevel@tonic-gate                         syslog(LOG_WARNING,
346*7c478bd9Sstevel@tonic-gate                                 "libldap: do_ldapssl_connect: "
347*7c478bd9Sstevel@tonic-gate                                 "Unable to resolve '%s'", host);
348*7c478bd9Sstevel@tonic-gate                         free(host_buf);
349*7c478bd9Sstevel@tonic-gate 			/* will free host in close_socket_and_exit_with_error */
350*7c478bd9Sstevel@tonic-gate 			goto close_socket_and_exit_with_error;
351*7c478bd9Sstevel@tonic-gate                 }
352*7c478bd9Sstevel@tonic-gate                 /* We support only the primary host name */
353*7c478bd9Sstevel@tonic-gate                 else {
354*7c478bd9Sstevel@tonic-gate 			if (hent->ldaphe_name != NULL)
355*7c478bd9Sstevel@tonic-gate                         	name = strdup(hent->ldaphe_name);
356*7c478bd9Sstevel@tonic-gate                 	free(host_buf);
357*7c478bd9Sstevel@tonic-gate                 	if (name == NULL)
358*7c478bd9Sstevel@tonic-gate                         	goto close_socket_and_exit_with_error;
359*7c478bd9Sstevel@tonic-gate 			else
360*7c478bd9Sstevel@tonic-gate                 		ldap_memfree(host); host = NULL;
361*7c478bd9Sstevel@tonic-gate                 	host = name;
362*7c478bd9Sstevel@tonic-gate 		}
363*7c478bd9Sstevel@tonic-gate         }
364*7c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate     /*
367*7c478bd9Sstevel@tonic-gate      * Retrieve socket info. so we have the PRFileDesc.
368*7c478bd9Sstevel@tonic-gate      */
369*7c478bd9Sstevel@tonic-gate     memset( &soi, 0, sizeof(soi));
370*7c478bd9Sstevel@tonic-gate     soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
371*7c478bd9Sstevel@tonic-gate     if ( prldap_get_socket_info( intfd, *socketargp, &soi ) != LDAP_SUCCESS ) {
372*7c478bd9Sstevel@tonic-gate 	goto close_socket_and_exit_with_error;
373*7c478bd9Sstevel@tonic-gate     }
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate     /*
376*7c478bd9Sstevel@tonic-gate      * Allocate a structure to hold our socket-specific data.
377*7c478bd9Sstevel@tonic-gate      */
378*7c478bd9Sstevel@tonic-gate     if ( NULL == ( ssoip = PR_Calloc( 1, sizeof( LDAPSSLSocketInfo )))) {
379*7c478bd9Sstevel@tonic-gate 	goto close_socket_and_exit_with_error;
380*7c478bd9Sstevel@tonic-gate     }
381*7c478bd9Sstevel@tonic-gate     ssoip->soi_sessioninfo = sseip;
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate     /*
384*7c478bd9Sstevel@tonic-gate      * Add SSL layer and let the standard NSPR to LDAP layer and enable SSL.
385*7c478bd9Sstevel@tonic-gate      */
386*7c478bd9Sstevel@tonic-gate     if (( sslfd = SSL_ImportFD( NULL, soi.soinfo_prfd )) == NULL ) {
387*7c478bd9Sstevel@tonic-gate 	goto close_socket_and_exit_with_error;
388*7c478bd9Sstevel@tonic-gate     }
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate     if ( SSL_OptionSet( sslfd, SSL_SECURITY, secure ) != SECSuccess ||
391*7c478bd9Sstevel@tonic-gate 		SSL_OptionSet( sslfd, SSL_HANDSHAKE_AS_CLIENT, secure )
392*7c478bd9Sstevel@tonic-gate 		!= SECSuccess || ( secure && SSL_ResetHandshake( sslfd,
393*7c478bd9Sstevel@tonic-gate 		PR_FALSE ) != SECSuccess )) {
394*7c478bd9Sstevel@tonic-gate 	goto close_socket_and_exit_with_error;
395*7c478bd9Sstevel@tonic-gate     }
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate     /*
398*7c478bd9Sstevel@tonic-gate      * Let the standard NSPR to LDAP layer know about the new socket and
399*7c478bd9Sstevel@tonic-gate      * our own socket-specific data.
400*7c478bd9Sstevel@tonic-gate      */
401*7c478bd9Sstevel@tonic-gate     soi.soinfo_prfd = sslfd;
402*7c478bd9Sstevel@tonic-gate     soi.soinfo_appdata = (void *)ssoip;
403*7c478bd9Sstevel@tonic-gate     if ( prldap_set_socket_info( intfd, *socketargp, &soi ) != LDAP_SUCCESS ) {
404*7c478bd9Sstevel@tonic-gate 	goto close_socket_and_exit_with_error;
405*7c478bd9Sstevel@tonic-gate     }
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
408*7c478bd9Sstevel@tonic-gate     /*
409*7c478bd9Sstevel@tonic-gate      * Set hostname which will be retrieved (depending on ssl strength) when
410*7c478bd9Sstevel@tonic-gate      * using client or server auth.
411*7c478bd9Sstevel@tonic-gate      */
412*7c478bd9Sstevel@tonic-gate     if (SSL_SetURL(sslfd, host) != SECSuccess)
413*7c478bd9Sstevel@tonic-gate 	goto close_socket_and_exit_with_error;
414*7c478bd9Sstevel@tonic-gate     ldap_memfree(host);
415*7c478bd9Sstevel@tonic-gate     host = NULL;
416*7c478bd9Sstevel@tonic-gate #endif /* _SOLARIS_SDK */
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate     sslfd = NULL;	/* so we don't close the socket twice upon error */
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate     /*
421*7c478bd9Sstevel@tonic-gate      * Install certificate hook function.
422*7c478bd9Sstevel@tonic-gate      */
423*7c478bd9Sstevel@tonic-gate     SSL_AuthCertificateHook( soi.soinfo_prfd,
424*7c478bd9Sstevel@tonic-gate 			     (SSLAuthCertificate)ldapssl_AuthCertificate,
425*7c478bd9Sstevel@tonic-gate                              (void *)sseip);
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate     if ( SSL_GetClientAuthDataHook( soi.soinfo_prfd,
428*7c478bd9Sstevel@tonic-gate 		get_clientauth_data, clientauth ? sseip : NULL ) != 0 ) {
429*7c478bd9Sstevel@tonic-gate 	goto close_socket_and_exit_with_error;
430*7c478bd9Sstevel@tonic-gate     }
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate     return( intfd );	/* success */
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate close_socket_and_exit_with_error:
435*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
436*7c478bd9Sstevel@tonic-gate     if ( NULL != host ) ldap_memfree(host);
437*7c478bd9Sstevel@tonic-gate #endif /* _SOLARIS_SDK */
438*7c478bd9Sstevel@tonic-gate     if ( NULL != sslfd ) {
439*7c478bd9Sstevel@tonic-gate 	PR_Close( sslfd );
440*7c478bd9Sstevel@tonic-gate     }
441*7c478bd9Sstevel@tonic-gate     if ( NULL != ssoip ) {
442*7c478bd9Sstevel@tonic-gate 	ldapssl_free_socket_info( &ssoip );
443*7c478bd9Sstevel@tonic-gate     }
444*7c478bd9Sstevel@tonic-gate     if ( intfd >= 0 && NULL != *socketargp ) {
445*7c478bd9Sstevel@tonic-gate 	(*(sseip->lssei_std_functions.lssf_close_fn))( intfd, *socketargp );
446*7c478bd9Sstevel@tonic-gate     }
447*7c478bd9Sstevel@tonic-gate     return( -1 );
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate static int
ldapssl_connect(const char * hostlist,int defport,int timeout,unsigned long options,struct lextiof_session_private * sessionarg,struct lextiof_socket_private ** socketargp)452*7c478bd9Sstevel@tonic-gate ldapssl_connect(const char *hostlist, int defport, int timeout,
453*7c478bd9Sstevel@tonic-gate 	unsigned long options, struct lextiof_session_private *sessionarg,
454*7c478bd9Sstevel@tonic-gate 	struct lextiof_socket_private **socketargp )
455*7c478bd9Sstevel@tonic-gate {
456*7c478bd9Sstevel@tonic-gate     return( do_ldapssl_connect( hostlist, defport, timeout, options,
457*7c478bd9Sstevel@tonic-gate 		sessionarg, socketargp, 0 ));
458*7c478bd9Sstevel@tonic-gate }
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate static int
ldapssl_clientauth_connect(const char * hostlist,int defport,int timeout,unsigned long options,struct lextiof_session_private * sessionarg,struct lextiof_socket_private ** socketargp)462*7c478bd9Sstevel@tonic-gate ldapssl_clientauth_connect(const char *hostlist, int defport, int timeout,
463*7c478bd9Sstevel@tonic-gate 	unsigned long options, struct lextiof_session_private *sessionarg,
464*7c478bd9Sstevel@tonic-gate 	struct lextiof_socket_private **socketargp )
465*7c478bd9Sstevel@tonic-gate {
466*7c478bd9Sstevel@tonic-gate     return( do_ldapssl_connect( hostlist, defport, timeout, options,
467*7c478bd9Sstevel@tonic-gate 		sessionarg, socketargp, 1 ));
468*7c478bd9Sstevel@tonic-gate }
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate static void
ldapssl_disposehandle(LDAP * ld,struct lextiof_session_private * sessionarg)472*7c478bd9Sstevel@tonic-gate ldapssl_disposehandle(LDAP *ld, struct lextiof_session_private *sessionarg)
473*7c478bd9Sstevel@tonic-gate {
474*7c478bd9Sstevel@tonic-gate     PRLDAPSessionInfo				sei;
475*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo				*sseip;
476*7c478bd9Sstevel@tonic-gate     LDAP_X_EXTIOF_DISPOSEHANDLE_CALLBACK	*disposehdl_fn;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate     memset( &sei, 0, sizeof( sei ));
479*7c478bd9Sstevel@tonic-gate     sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
480*7c478bd9Sstevel@tonic-gate     if ( prldap_get_session_info( ld, NULL, &sei ) == LDAP_SUCCESS ) {
481*7c478bd9Sstevel@tonic-gate 	sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
482*7c478bd9Sstevel@tonic-gate 	disposehdl_fn = sseip->lssei_std_functions.lssf_disposehdl_fn;
483*7c478bd9Sstevel@tonic-gate 	ldapssl_free_session_info( &sseip );
484*7c478bd9Sstevel@tonic-gate 	(*disposehdl_fn)( ld, sessionarg );
485*7c478bd9Sstevel@tonic-gate     }
486*7c478bd9Sstevel@tonic-gate }
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate /*
490*7c478bd9Sstevel@tonic-gate  * Install I/O routines from libsec and NSPR into libldap to allow libldap
491*7c478bd9Sstevel@tonic-gate  * to do SSL.
492*7c478bd9Sstevel@tonic-gate  *
493*7c478bd9Sstevel@tonic-gate  * We rely on libprldap to provide most of the functions, and then we override
494*7c478bd9Sstevel@tonic-gate  * a few of them to support SSL.
495*7c478bd9Sstevel@tonic-gate  */
496*7c478bd9Sstevel@tonic-gate int
497*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldapssl_install_routines(LDAP * ld)498*7c478bd9Sstevel@tonic-gate ldapssl_install_routines( LDAP *ld )
499*7c478bd9Sstevel@tonic-gate {
500*7c478bd9Sstevel@tonic-gate #ifndef LDAP_SSLIO_HOOKS
501*7c478bd9Sstevel@tonic-gate     ldap_set_lderrno( ld, LDAP_LOCAL_ERROR, NULL, NULL );
502*7c478bd9Sstevel@tonic-gate     return( -1 );
503*7c478bd9Sstevel@tonic-gate #else
504*7c478bd9Sstevel@tonic-gate     struct ldap_x_ext_io_fns	iofns;
505*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo		*ssip;
506*7c478bd9Sstevel@tonic-gate     PRLDAPSessionInfo		sei;
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate /*
509*7c478bd9Sstevel@tonic-gate  * This is done within ldap_init() and
510*7c478bd9Sstevel@tonic-gate  * ldap_init() is called from ldapssl_init()
511*7c478bd9Sstevel@tonic-gate  */
512*7c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
513*7c478bd9Sstevel@tonic-gate     if ( prldap_install_routines(
514*7c478bd9Sstevel@tonic-gate 		ld,
515*7c478bd9Sstevel@tonic-gate 		1 /* shared -- we have to assume it is */ )
516*7c478bd9Sstevel@tonic-gate 		!= LDAP_SUCCESS ) {
517*7c478bd9Sstevel@tonic-gate 	return( -1 );
518*7c478bd9Sstevel@tonic-gate     }
519*7c478bd9Sstevel@tonic-gate #endif /*_SOLARIS_SDK*/
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate     /*
522*7c478bd9Sstevel@tonic-gate      * Allocate our own session information.
523*7c478bd9Sstevel@tonic-gate      */
524*7c478bd9Sstevel@tonic-gate     if ( NULL == ( ssip = (LDAPSSLSessionInfo *)PR_Calloc( 1,
525*7c478bd9Sstevel@tonic-gate 		sizeof( LDAPSSLSessionInfo )))) {
526*7c478bd9Sstevel@tonic-gate 	ldap_set_lderrno( ld, LDAP_NO_MEMORY, NULL, NULL );
527*7c478bd9Sstevel@tonic-gate 	return( -1 );
528*7c478bd9Sstevel@tonic-gate     }
529*7c478bd9Sstevel@tonic-gate     /*
530*7c478bd9Sstevel@tonic-gate      * Initialize session info.
531*7c478bd9Sstevel@tonic-gate      * XXX: it would be nice to be able to set these on a per-session basis:
532*7c478bd9Sstevel@tonic-gate      *          lssei_using_pcks_fns
533*7c478bd9Sstevel@tonic-gate      *          lssei_certdbh
534*7c478bd9Sstevel@tonic-gate      */
535*7c478bd9Sstevel@tonic-gate     ssip->lssei_ssl_strength = default_ssl_strength;
536*7c478bd9Sstevel@tonic-gate     ssip->lssei_using_pcks_fns = using_pkcs_functions;
537*7c478bd9Sstevel@tonic-gate     ssip->lssei_certdbh = CERT_GetDefaultCertDB();
538*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
539*7c478bd9Sstevel@tonic-gate     /*
540*7c478bd9Sstevel@tonic-gate      * This is part of a hack to allow the ssl portion of the
541*7c478bd9Sstevel@tonic-gate      * library to call the ldap library gethostbyaddr resolver.
542*7c478bd9Sstevel@tonic-gate      */
543*7c478bd9Sstevel@tonic-gate     ssip->ld = ld;
544*7c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate     /*
547*7c478bd9Sstevel@tonic-gate      * override a few functions, saving a pointer to the standard function
548*7c478bd9Sstevel@tonic-gate      * in each case so we can call it from our SSL savvy functions.
549*7c478bd9Sstevel@tonic-gate      */
550*7c478bd9Sstevel@tonic-gate     memset( &iofns, 0, sizeof(iofns));
551*7c478bd9Sstevel@tonic-gate     iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
552*7c478bd9Sstevel@tonic-gate     if ( ldap_get_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns ) < 0 ) {
553*7c478bd9Sstevel@tonic-gate 	ldapssl_free_session_info( &ssip );
554*7c478bd9Sstevel@tonic-gate 	return( -1 );
555*7c478bd9Sstevel@tonic-gate     }
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate     /* override socket, connect, and ioctl */
558*7c478bd9Sstevel@tonic-gate     ssip->lssei_std_functions.lssf_connect_fn = iofns.lextiof_connect;
559*7c478bd9Sstevel@tonic-gate     iofns.lextiof_connect = ldapssl_connect;
560*7c478bd9Sstevel@tonic-gate     ssip->lssei_std_functions.lssf_close_fn = iofns.lextiof_close;
561*7c478bd9Sstevel@tonic-gate     iofns.lextiof_close = ldapssl_close;
562*7c478bd9Sstevel@tonic-gate     ssip->lssei_std_functions.lssf_disposehdl_fn = iofns.lextiof_disposehandle;
563*7c478bd9Sstevel@tonic-gate     iofns.lextiof_disposehandle = ldapssl_disposehandle;
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate     if ( ldap_set_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns ) < 0 ) {
566*7c478bd9Sstevel@tonic-gate 	ldapssl_free_session_info( &ssip );
567*7c478bd9Sstevel@tonic-gate 	return( -1 );
568*7c478bd9Sstevel@tonic-gate     }
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate     /*
571*7c478bd9Sstevel@tonic-gate      * Store session info. for later retrieval.
572*7c478bd9Sstevel@tonic-gate      */
573*7c478bd9Sstevel@tonic-gate     sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
574*7c478bd9Sstevel@tonic-gate     sei.seinfo_appdata = (void *)ssip;
575*7c478bd9Sstevel@tonic-gate     if ( prldap_set_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
576*7c478bd9Sstevel@tonic-gate 	return( -1 );
577*7c478bd9Sstevel@tonic-gate     }
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate     return( 0 );
580*7c478bd9Sstevel@tonic-gate #endif
581*7c478bd9Sstevel@tonic-gate }
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate /*
585*7c478bd9Sstevel@tonic-gate  * Set the SSL strength for an existing SSL-enabled LDAP session handle.
586*7c478bd9Sstevel@tonic-gate  *
587*7c478bd9Sstevel@tonic-gate  * See the description of ldapssl_serverauth_init() above for valid
588*7c478bd9Sstevel@tonic-gate  * sslstrength values. If ld is NULL, the default for new LDAP session
589*7c478bd9Sstevel@tonic-gate  * handles is set.
590*7c478bd9Sstevel@tonic-gate  *
591*7c478bd9Sstevel@tonic-gate  * Returns 0 if all goes well and -1 if an error occurs.
592*7c478bd9Sstevel@tonic-gate  */
593*7c478bd9Sstevel@tonic-gate int
594*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldapssl_set_strength(LDAP * ld,int sslstrength)595*7c478bd9Sstevel@tonic-gate ldapssl_set_strength( LDAP *ld, int sslstrength )
596*7c478bd9Sstevel@tonic-gate {
597*7c478bd9Sstevel@tonic-gate     int                 rc = 0; /* assume success */
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate     if ( sslstrength != LDAPSSL_AUTH_WEAK &&
600*7c478bd9Sstevel@tonic-gate                 sslstrength != LDAPSSL_AUTH_CERT &&
601*7c478bd9Sstevel@tonic-gate                 sslstrength != LDAPSSL_AUTH_CNCHECK ) {
602*7c478bd9Sstevel@tonic-gate         rc = -1;
603*7c478bd9Sstevel@tonic-gate     } else {
604*7c478bd9Sstevel@tonic-gate         if ( NULL == ld ) {     /* set default strength */
605*7c478bd9Sstevel@tonic-gate             default_ssl_strength = sslstrength;
606*7c478bd9Sstevel@tonic-gate         } else {                /* set session-specific strength */
607*7c478bd9Sstevel@tonic-gate             PRLDAPSessionInfo   sei;
608*7c478bd9Sstevel@tonic-gate             LDAPSSLSessionInfo  *sseip;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate             memset( &sei, 0, sizeof( sei ));
611*7c478bd9Sstevel@tonic-gate             sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
612*7c478bd9Sstevel@tonic-gate             if ( prldap_get_session_info( ld, NULL, &sei ) == LDAP_SUCCESS )
613*7c478bd9Sstevel@tonic-gate {
614*7c478bd9Sstevel@tonic-gate                 sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
615*7c478bd9Sstevel@tonic-gate                 sseip->lssei_ssl_strength = sslstrength;
616*7c478bd9Sstevel@tonic-gate             } else {
617*7c478bd9Sstevel@tonic-gate                 rc = -1;
618*7c478bd9Sstevel@tonic-gate             }
619*7c478bd9Sstevel@tonic-gate         }
620*7c478bd9Sstevel@tonic-gate     }
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate     return( rc );
623*7c478bd9Sstevel@tonic-gate }
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate int
626*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldapssl_enable_clientauth(LDAP * ld,char * keynickname,char * keypasswd,char * certnickname)627*7c478bd9Sstevel@tonic-gate ldapssl_enable_clientauth( LDAP *ld, char *keynickname,
628*7c478bd9Sstevel@tonic-gate         char *keypasswd, char *certnickname )
629*7c478bd9Sstevel@tonic-gate {
630*7c478bd9Sstevel@tonic-gate #ifndef LDAP_SSLIO_HOOKS
631*7c478bd9Sstevel@tonic-gate     ldap_set_lderrno( ld, LDAP_LOCAL_ERROR, NULL, NULL );
632*7c478bd9Sstevel@tonic-gate     return( -1 );
633*7c478bd9Sstevel@tonic-gate #else
634*7c478bd9Sstevel@tonic-gate     struct ldap_x_ext_io_fns	iofns;
635*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo		*ssip;
636*7c478bd9Sstevel@tonic-gate     PRLDAPSessionInfo		sei;
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate     /*
639*7c478bd9Sstevel@tonic-gate      * Check parameters
640*7c478bd9Sstevel@tonic-gate      */
641*7c478bd9Sstevel@tonic-gate     if ( certnickname == NULL || keypasswd == NULL ) {
642*7c478bd9Sstevel@tonic-gate 	ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
643*7c478bd9Sstevel@tonic-gate 	return( -1 );
644*7c478bd9Sstevel@tonic-gate     }
645*7c478bd9Sstevel@tonic-gate 
646*7c478bd9Sstevel@tonic-gate     /*
647*7c478bd9Sstevel@tonic-gate      * Update session info. data structure.
648*7c478bd9Sstevel@tonic-gate      */
649*7c478bd9Sstevel@tonic-gate     sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
650*7c478bd9Sstevel@tonic-gate     if ( prldap_get_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
651*7c478bd9Sstevel@tonic-gate 	return( -1 );
652*7c478bd9Sstevel@tonic-gate     }
653*7c478bd9Sstevel@tonic-gate     ssip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
654*7c478bd9Sstevel@tonic-gate     if ( NULL == ssip ) {
655*7c478bd9Sstevel@tonic-gate 	ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
656*7c478bd9Sstevel@tonic-gate 	return( -1 );
657*7c478bd9Sstevel@tonic-gate     }
658*7c478bd9Sstevel@tonic-gate     ssip->lssei_certnickname = PL_strdup( certnickname );
659*7c478bd9Sstevel@tonic-gate     ssip->lssei_keypasswd = PL_strdup( keypasswd );
660*7c478bd9Sstevel@tonic-gate 
661*7c478bd9Sstevel@tonic-gate     if ( NULL == ssip->lssei_certnickname || NULL == ssip->lssei_keypasswd ) {
662*7c478bd9Sstevel@tonic-gate 	ldap_set_lderrno( ld, LDAP_NO_MEMORY, NULL, NULL );
663*7c478bd9Sstevel@tonic-gate 	return( -1 );
664*7c478bd9Sstevel@tonic-gate     }
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate     if ( check_clientauth_nicknames_and_passwd( ld, ssip ) != 0 ) {
667*7c478bd9Sstevel@tonic-gate 	return( -1 );
668*7c478bd9Sstevel@tonic-gate     }
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate     /*
671*7c478bd9Sstevel@tonic-gate      * replace standard SSL CONNECT function with client auth aware one
672*7c478bd9Sstevel@tonic-gate      */
673*7c478bd9Sstevel@tonic-gate     memset( &iofns, 0, sizeof(iofns));
674*7c478bd9Sstevel@tonic-gate     iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
675*7c478bd9Sstevel@tonic-gate     if ( ldap_get_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns )
676*7c478bd9Sstevel@tonic-gate 		!= 0 ) {
677*7c478bd9Sstevel@tonic-gate 	return( -1 );
678*7c478bd9Sstevel@tonic-gate     }
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate     if ( iofns.lextiof_connect != ldapssl_connect ) {
681*7c478bd9Sstevel@tonic-gate 	/* standard SSL setup has not done */
682*7c478bd9Sstevel@tonic-gate 	ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
683*7c478bd9Sstevel@tonic-gate 	return( -1 );
684*7c478bd9Sstevel@tonic-gate     }
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate     iofns.lextiof_connect = ldapssl_clientauth_connect;
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate     if ( ldap_set_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns )
689*7c478bd9Sstevel@tonic-gate 		!= 0 ) {
690*7c478bd9Sstevel@tonic-gate 	return( -1 );
691*7c478bd9Sstevel@tonic-gate     }
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate     return( 0 );
694*7c478bd9Sstevel@tonic-gate #endif
695*7c478bd9Sstevel@tonic-gate }
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate static void
ldapssl_free_session_info(LDAPSSLSessionInfo ** ssipp)699*7c478bd9Sstevel@tonic-gate ldapssl_free_session_info( LDAPSSLSessionInfo **ssipp )
700*7c478bd9Sstevel@tonic-gate {
701*7c478bd9Sstevel@tonic-gate     if ( NULL != ssipp && NULL != *ssipp ) {
702*7c478bd9Sstevel@tonic-gate 	if ( NULL != (*ssipp)->lssei_certnickname ) {
703*7c478bd9Sstevel@tonic-gate 	    PL_strfree( (*ssipp)->lssei_certnickname );
704*7c478bd9Sstevel@tonic-gate 	    (*ssipp)->lssei_certnickname = NULL;
705*7c478bd9Sstevel@tonic-gate 	}
706*7c478bd9Sstevel@tonic-gate 	if ( NULL != (*ssipp)->lssei_keypasswd ) {
707*7c478bd9Sstevel@tonic-gate 	    PL_strfree( (*ssipp)->lssei_keypasswd );
708*7c478bd9Sstevel@tonic-gate 	    (*ssipp)->lssei_keypasswd = NULL;
709*7c478bd9Sstevel@tonic-gate 	}
710*7c478bd9Sstevel@tonic-gate 	PR_Free( *ssipp );
711*7c478bd9Sstevel@tonic-gate 	*ssipp = NULL;
712*7c478bd9Sstevel@tonic-gate     }
713*7c478bd9Sstevel@tonic-gate }
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate static void
ldapssl_free_socket_info(LDAPSSLSocketInfo ** soipp)717*7c478bd9Sstevel@tonic-gate ldapssl_free_socket_info( LDAPSSLSocketInfo **soipp )
718*7c478bd9Sstevel@tonic-gate {
719*7c478bd9Sstevel@tonic-gate     if ( NULL != soipp && NULL != *soipp ) {
720*7c478bd9Sstevel@tonic-gate 	PR_Free( *soipp );
721*7c478bd9Sstevel@tonic-gate 	*soipp = NULL;
722*7c478bd9Sstevel@tonic-gate     }
723*7c478bd9Sstevel@tonic-gate }
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate /* this function provides cert authentication.  This is called during
727*7c478bd9Sstevel@tonic-gate  * the SSL_Handshake process.  Once the cert has been retrieved from
728*7c478bd9Sstevel@tonic-gate  * the server, the it is checked, using VerifyCertNow(), then
729*7c478bd9Sstevel@tonic-gate  * the cn is checked against the host name, set with SSL_SetURL()
730*7c478bd9Sstevel@tonic-gate  */
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate static int
ldapssl_AuthCertificate(void * sessionarg,PRFileDesc * fd,PRBool checkSig,PRBool isServer)733*7c478bd9Sstevel@tonic-gate ldapssl_AuthCertificate(void *sessionarg, PRFileDesc *fd, PRBool checkSig,
734*7c478bd9Sstevel@tonic-gate 	PRBool isServer)
735*7c478bd9Sstevel@tonic-gate {
736*7c478bd9Sstevel@tonic-gate     SECStatus		rv = SECFailure;
737*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo	*sseip;
738*7c478bd9Sstevel@tonic-gate     CERTCertificate	*cert;
739*7c478bd9Sstevel@tonic-gate     SECCertUsage	certUsage;
740*7c478bd9Sstevel@tonic-gate     char		*hostname = (char *)0;
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate     if (!sessionarg || !socket)
743*7c478bd9Sstevel@tonic-gate 	return rv;
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate     sseip = (LDAPSSLSessionInfo *)sessionarg;
746*7c478bd9Sstevel@tonic-gate 
747*7c478bd9Sstevel@tonic-gate     if (LDAPSSL_AUTH_WEAK == sseip->lssei_ssl_strength ) { /* no check */
748*7c478bd9Sstevel@tonic-gate         return SECSuccess;
749*7c478bd9Sstevel@tonic-gate     }
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate     if ( isServer ) {
752*7c478bd9Sstevel@tonic-gate 	certUsage = certUsageSSLClient;
753*7c478bd9Sstevel@tonic-gate     } else {
754*7c478bd9Sstevel@tonic-gate 	certUsage = certUsageSSLServer;
755*7c478bd9Sstevel@tonic-gate     }
756*7c478bd9Sstevel@tonic-gate     cert = SSL_PeerCertificate( fd );
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate     rv = CERT_VerifyCertNow(sseip->lssei_certdbh, cert, checkSig,
759*7c478bd9Sstevel@tonic-gate 			certUsage, NULL);
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate     if ( rv != SECSuccess || isServer )
762*7c478bd9Sstevel@tonic-gate 	return rv;
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate     if ( LDAPSSL_AUTH_CNCHECK == sseip->lssei_ssl_strength )
765*7c478bd9Sstevel@tonic-gate       {
766*7c478bd9Sstevel@tonic-gate 	/* cert is OK.  This is the client side of an SSL connection.
767*7c478bd9Sstevel@tonic-gate 	 * Now check the name field in the cert against the desired hostname.
768*7c478bd9Sstevel@tonic-gate 	 * NB: This is our only defense against Man-In-The-Middle (MITM)
769*7c478bd9Sstevel@tonic-gate 	 * attacks!
770*7c478bd9Sstevel@tonic-gate 	 */
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate 	hostname = SSL_RevealURL( fd );
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 	if (hostname && hostname[0]) {
775*7c478bd9Sstevel@tonic-gate 	  rv = CERT_VerifyCertName(cert, hostname);
776*7c478bd9Sstevel@tonic-gate 	} else  {
777*7c478bd9Sstevel@tonic-gate 	  rv = SECFailure;
778*7c478bd9Sstevel@tonic-gate      	}
779*7c478bd9Sstevel@tonic-gate 	if (hostname)
780*7c478bd9Sstevel@tonic-gate 		PORT_Free(hostname);
781*7c478bd9Sstevel@tonic-gate 	if (rv != SECSuccess)
782*7c478bd9Sstevel@tonic-gate 	  PORT_SetError(SSL_ERROR_BAD_CERT_DOMAIN);
783*7c478bd9Sstevel@tonic-gate       }
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate     return((int)rv);
786*7c478bd9Sstevel@tonic-gate }
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate /*
790*7c478bd9Sstevel@tonic-gate  * called during SSL client auth. when server wants our cert and key.
791*7c478bd9Sstevel@tonic-gate  * return 0 if we succeeded and set *pRetCert and *pRetKey, -1 otherwise.
792*7c478bd9Sstevel@tonic-gate  * if -1 is returned SSL will proceed without sending a cert.
793*7c478bd9Sstevel@tonic-gate  */
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate static int
get_clientauth_data(void * sessionarg,PRFileDesc * prfd,CERTDistNames * caNames,CERTCertificate ** pRetCert,SECKEYPrivateKey ** pRetKey)796*7c478bd9Sstevel@tonic-gate get_clientauth_data( void *sessionarg, PRFileDesc *prfd,
797*7c478bd9Sstevel@tonic-gate         CERTDistNames *caNames,  CERTCertificate **pRetCert,
798*7c478bd9Sstevel@tonic-gate         SECKEYPrivateKey **pRetKey )
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate {
801*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo	*ssip;
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate     if (( ssip = (LDAPSSLSessionInfo *)sessionarg ) == NULL ) {
804*7c478bd9Sstevel@tonic-gate 	return( -1 );       /* client auth. not enabled */
805*7c478bd9Sstevel@tonic-gate     }
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate     return( get_keyandcert( ssip, pRetCert, pRetKey, NULL ));
808*7c478bd9Sstevel@tonic-gate }
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate static int
get_keyandcert(LDAPSSLSessionInfo * ssip,CERTCertificate ** pRetCert,SECKEYPrivateKey ** pRetKey,char ** errmsgp)811*7c478bd9Sstevel@tonic-gate get_keyandcert( LDAPSSLSessionInfo *ssip,
812*7c478bd9Sstevel@tonic-gate 	CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey,
813*7c478bd9Sstevel@tonic-gate 	char **errmsgp )
814*7c478bd9Sstevel@tonic-gate {
815*7c478bd9Sstevel@tonic-gate     CERTCertificate	*cert;
816*7c478bd9Sstevel@tonic-gate     SECKEYPrivateKey	*key;
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate     if (( cert = PK11_FindCertFromNickname( ssip->lssei_certnickname, NULL ))
819*7c478bd9Sstevel@tonic-gate 		== NULL ) {
820*7c478bd9Sstevel@tonic-gate 	if ( errmsgp != NULL ) {
821*7c478bd9Sstevel@tonic-gate 	    *errmsgp = dgettext(TEXT_DOMAIN, "unable to find certificate");
822*7c478bd9Sstevel@tonic-gate 	}
823*7c478bd9Sstevel@tonic-gate 	return( -1 );
824*7c478bd9Sstevel@tonic-gate     }
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate     {
827*7c478bd9Sstevel@tonic-gate 	PK11_SetPasswordFunc( get_keypassword );
828*7c478bd9Sstevel@tonic-gate     }
829*7c478bd9Sstevel@tonic-gate 
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate 
832*7c478bd9Sstevel@tonic-gate     if (( key = PK11_FindKeyByAnyCert( cert, (void *)ssip )) == NULL ) {
833*7c478bd9Sstevel@tonic-gate 	CERT_DestroyCertificate( cert );
834*7c478bd9Sstevel@tonic-gate 	if ( errmsgp != NULL ) {
835*7c478bd9Sstevel@tonic-gate 	    *errmsgp = dgettext(TEXT_DOMAIN, "bad key or key password");
836*7c478bd9Sstevel@tonic-gate 	}
837*7c478bd9Sstevel@tonic-gate 	return( -1 );
838*7c478bd9Sstevel@tonic-gate     }
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate     *pRetCert = cert;
841*7c478bd9Sstevel@tonic-gate     *pRetKey = key;
842*7c478bd9Sstevel@tonic-gate     return( 0 );
843*7c478bd9Sstevel@tonic-gate }
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 
846*7c478bd9Sstevel@tonic-gate /*
847*7c478bd9Sstevel@tonic-gate  * This function returns the password to NSS.
848*7c478bd9Sstevel@tonic-gate  * This function is enable through PK11_SetPasswordFunc
849*7c478bd9Sstevel@tonic-gate  * only if pkcs functions are not being used.
850*7c478bd9Sstevel@tonic-gate  */
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate static char *
get_keypassword(PK11SlotInfo * slot,PRBool retry,void * sessionarg)853*7c478bd9Sstevel@tonic-gate get_keypassword( PK11SlotInfo *slot, PRBool retry, void *sessionarg )
854*7c478bd9Sstevel@tonic-gate {
855*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo	*ssip;
856*7c478bd9Sstevel@tonic-gate 
857*7c478bd9Sstevel@tonic-gate     if ( retry)
858*7c478bd9Sstevel@tonic-gate       return (NULL);
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate     ssip = (LDAPSSLSessionInfo *)sessionarg;
861*7c478bd9Sstevel@tonic-gate     if ( NULL == ssip ) {
862*7c478bd9Sstevel@tonic-gate 	return( NULL );
863*7c478bd9Sstevel@tonic-gate     }
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate     return( ssip->lssei_keypasswd );
866*7c478bd9Sstevel@tonic-gate }
867*7c478bd9Sstevel@tonic-gate 
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate /*
870*7c478bd9Sstevel@tonic-gate  * performs some basic checks on clientauth cert and key/password
871*7c478bd9Sstevel@tonic-gate  *
872*7c478bd9Sstevel@tonic-gate  * XXXmcs: could perform additional checks... see servers/slapd/ssl.c
873*7c478bd9Sstevel@tonic-gate  *	1) check expiration
874*7c478bd9Sstevel@tonic-gate  *	2) check that public key in cert matches private key
875*7c478bd9Sstevel@tonic-gate  * see ns/netsite/ldap/servers/slapd/ssl.c:slapd_ssl_init() for example code.
876*7c478bd9Sstevel@tonic-gate  */
877*7c478bd9Sstevel@tonic-gate static int
check_clientauth_nicknames_and_passwd(LDAP * ld,LDAPSSLSessionInfo * ssip)878*7c478bd9Sstevel@tonic-gate check_clientauth_nicknames_and_passwd( LDAP *ld, LDAPSSLSessionInfo *ssip )
879*7c478bd9Sstevel@tonic-gate {
880*7c478bd9Sstevel@tonic-gate     char		*errmsg = NULL;
881*7c478bd9Sstevel@tonic-gate     CERTCertificate	*cert = NULL;
882*7c478bd9Sstevel@tonic-gate     SECKEYPrivateKey	*key = NULL;
883*7c478bd9Sstevel@tonic-gate     int rv;
884*7c478bd9Sstevel@tonic-gate 
885*7c478bd9Sstevel@tonic-gate     rv = get_keyandcert( ssip, &cert, &key, &errmsg );
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate     if ( rv != 0 ) {
888*7c478bd9Sstevel@tonic-gate     	if ( errmsg != NULL ) {
889*7c478bd9Sstevel@tonic-gate 	    errmsg = strdup( errmsg );
890*7c478bd9Sstevel@tonic-gate 	}
891*7c478bd9Sstevel@tonic-gate 	ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, errmsg );
892*7c478bd9Sstevel@tonic-gate 	return( -1 );
893*7c478bd9Sstevel@tonic-gate     }
894*7c478bd9Sstevel@tonic-gate 
895*7c478bd9Sstevel@tonic-gate     if ( cert != NULL ) {
896*7c478bd9Sstevel@tonic-gate 	CERT_DestroyCertificate( cert );
897*7c478bd9Sstevel@tonic-gate     }
898*7c478bd9Sstevel@tonic-gate     if ( key != NULL ) {
899*7c478bd9Sstevel@tonic-gate 	SECKEY_DestroyPrivateKey( key );
900*7c478bd9Sstevel@tonic-gate     }
901*7c478bd9Sstevel@tonic-gate     return( 0 );
902*7c478bd9Sstevel@tonic-gate }
903*7c478bd9Sstevel@tonic-gate 
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate #if 0	/* NOT_NEEDED_IN_LIBLDAP */
906*7c478bd9Sstevel@tonic-gate /* there are patches and kludges.  this is both.  force some linkers to
907*7c478bd9Sstevel@tonic-gate  * link this stuff in
908*7c478bd9Sstevel@tonic-gate  */
909*7c478bd9Sstevel@tonic-gate int stubs_o_stuff( void )
910*7c478bd9Sstevel@tonic-gate {
911*7c478bd9Sstevel@tonic-gate     PRExplodedTime exploded;
912*7c478bd9Sstevel@tonic-gate     PLArenaPool pool;
913*7c478bd9Sstevel@tonic-gate 
914*7c478bd9Sstevel@tonic-gate     const char *name ="t";
915*7c478bd9Sstevel@tonic-gate     PRUint32 size = 0, align = 0;
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate     PR_ImplodeTime( &exploded );
918*7c478bd9Sstevel@tonic-gate     PL_InitArenaPool( &pool, name, size, align);
919*7c478bd9Sstevel@tonic-gate     PR_Cleanup();
920*7c478bd9Sstevel@tonic-gate     PR_fprintf((PRFileDesc*)stderr, "Bad IDEA!!");
921*7c478bd9Sstevel@tonic-gate 
922*7c478bd9Sstevel@tonic-gate     return 0;
923*7c478bd9Sstevel@tonic-gate 
924*7c478bd9Sstevel@tonic-gate }
925*7c478bd9Sstevel@tonic-gate #endif	/* NOT_NEEDED_IN_LIBLDAP */
926*7c478bd9Sstevel@tonic-gate 
927*7c478bd9Sstevel@tonic-gate 
928*7c478bd9Sstevel@tonic-gate /*
929*7c478bd9Sstevel@tonic-gate  * Import the file descriptor corresponding to the socket of an already
930*7c478bd9Sstevel@tonic-gate  * open LDAP connection into SSL, and update the socket and session
931*7c478bd9Sstevel@tonic-gate  * information accordingly.
932*7c478bd9Sstevel@tonic-gate  */
ldapssl_import_fd(LDAP * ld,int secure)933*7c478bd9Sstevel@tonic-gate int ldapssl_import_fd ( LDAP *ld, int secure )
934*7c478bd9Sstevel@tonic-gate {
935*7c478bd9Sstevel@tonic-gate     PRLDAPSessionInfo   sei;
936*7c478bd9Sstevel@tonic-gate     PRLDAPSocketInfo    soi;
937*7c478bd9Sstevel@tonic-gate     LDAPSSLSocketInfo   *ssoip = NULL;
938*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo  *sseip;
939*7c478bd9Sstevel@tonic-gate     PRFileDesc          *sslfd = NULL;
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 
942*7c478bd9Sstevel@tonic-gate     /*
943*7c478bd9Sstevel@tonic-gate      * Retrieve session info. so we can store a pointer to our session info.
944*7c478bd9Sstevel@tonic-gate      * in our socket info. later.
945*7c478bd9Sstevel@tonic-gate      */
946*7c478bd9Sstevel@tonic-gate     memset( &sei, 0, sizeof(sei));
947*7c478bd9Sstevel@tonic-gate     sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
948*7c478bd9Sstevel@tonic-gate     if ( prldap_get_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
949*7c478bd9Sstevel@tonic-gate         return( -1 );
950*7c478bd9Sstevel@tonic-gate     }
951*7c478bd9Sstevel@tonic-gate     sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
952*7c478bd9Sstevel@tonic-gate 
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate     /*
955*7c478bd9Sstevel@tonic-gate      * Retrieve socket info. so we have the PRFileDesc.
956*7c478bd9Sstevel@tonic-gate      */
957*7c478bd9Sstevel@tonic-gate     memset( &soi, 0, sizeof(soi));
958*7c478bd9Sstevel@tonic-gate     soi.soinfo_size = PRLDAP_SOCKETINFO_SIZE;
959*7c478bd9Sstevel@tonic-gate     if ( prldap_get_default_socket_info( ld, &soi ) != LDAP_SUCCESS ) {
960*7c478bd9Sstevel@tonic-gate         return( -1 );
961*7c478bd9Sstevel@tonic-gate     }
962*7c478bd9Sstevel@tonic-gate 
963*7c478bd9Sstevel@tonic-gate     /*
964*7c478bd9Sstevel@tonic-gate      * Allocate a structure to hold our socket-specific data.
965*7c478bd9Sstevel@tonic-gate      */
966*7c478bd9Sstevel@tonic-gate     if ( NULL == ( ssoip = PR_Calloc( 1, sizeof( LDAPSSLSocketInfo )))) {
967*7c478bd9Sstevel@tonic-gate         goto reset_socket_and_exit_with_error;
968*7c478bd9Sstevel@tonic-gate     }
969*7c478bd9Sstevel@tonic-gate     ssoip->soi_sessioninfo = sseip;
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate     /*
972*7c478bd9Sstevel@tonic-gate      * Add SSL layer and let the standard NSPR to LDAP layer and enable SSL.
973*7c478bd9Sstevel@tonic-gate      */
974*7c478bd9Sstevel@tonic-gate     if (( sslfd = SSL_ImportFD( NULL, soi.soinfo_prfd )) == NULL ) {
975*7c478bd9Sstevel@tonic-gate         goto reset_socket_and_exit_with_error;
976*7c478bd9Sstevel@tonic-gate     }
977*7c478bd9Sstevel@tonic-gate 
978*7c478bd9Sstevel@tonic-gate     if ( SSL_OptionSet( sslfd, SSL_SECURITY, secure ) != SECSuccess ||
979*7c478bd9Sstevel@tonic-gate                 SSL_OptionSet( sslfd, SSL_HANDSHAKE_AS_CLIENT, secure )
980*7c478bd9Sstevel@tonic-gate                 != SECSuccess || ( secure && SSL_ResetHandshake( sslfd,
981*7c478bd9Sstevel@tonic-gate                 PR_FALSE ) != SECSuccess )) {
982*7c478bd9Sstevel@tonic-gate         goto reset_socket_and_exit_with_error;
983*7c478bd9Sstevel@tonic-gate     }
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate     /*
986*7c478bd9Sstevel@tonic-gate      * Let the standard NSPR to LDAP layer know about the new socket and
987*7c478bd9Sstevel@tonic-gate      * our own socket-specific data.
988*7c478bd9Sstevel@tonic-gate      */
989*7c478bd9Sstevel@tonic-gate     soi.soinfo_prfd = sslfd;
990*7c478bd9Sstevel@tonic-gate     soi.soinfo_appdata = (void *)ssoip;
991*7c478bd9Sstevel@tonic-gate     if ( prldap_set_default_socket_info( ld, &soi ) != LDAP_SUCCESS ) {
992*7c478bd9Sstevel@tonic-gate         goto reset_socket_and_exit_with_error;
993*7c478bd9Sstevel@tonic-gate     }
994*7c478bd9Sstevel@tonic-gate 
995*7c478bd9Sstevel@tonic-gate     /*
996*7c478bd9Sstevel@tonic-gate      * Install certificate hook function.
997*7c478bd9Sstevel@tonic-gate      */
998*7c478bd9Sstevel@tonic-gate     if ( SSL_AuthCertificateHook( soi.soinfo_prfd,
999*7c478bd9Sstevel@tonic-gate                                   (SSLAuthCertificate)ldapssl_AuthCertificate,
1000*7c478bd9Sstevel@tonic-gate                                   (void *)CERT_GetDefaultCertDB()) != 0 ) {
1001*7c478bd9Sstevel@tonic-gate         goto reset_socket_and_exit_with_error;
1002*7c478bd9Sstevel@tonic-gate     }
1003*7c478bd9Sstevel@tonic-gate 
1004*7c478bd9Sstevel@tonic-gate     if ( SSL_GetClientAuthDataHook( soi.soinfo_prfd,
1005*7c478bd9Sstevel@tonic-gate                 get_clientauth_data, sseip->lssei_certnickname ? sseip : NULL )
1006*7c478bd9Sstevel@tonic-gate 		!= 0 ) {
1007*7c478bd9Sstevel@tonic-gate         goto reset_socket_and_exit_with_error;
1008*7c478bd9Sstevel@tonic-gate     }
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate     return 0;
1011*7c478bd9Sstevel@tonic-gate 
1012*7c478bd9Sstevel@tonic-gate  reset_socket_and_exit_with_error:
1013*7c478bd9Sstevel@tonic-gate     if ( NULL != sslfd ) {
1014*7c478bd9Sstevel@tonic-gate         /*
1015*7c478bd9Sstevel@tonic-gate          * "Unimport" the socket from SSL, i.e. get rid of the upper layer of
1016*7c478bd9Sstevel@tonic-gate          * the file descriptor stack, which represents SSL.
1017*7c478bd9Sstevel@tonic-gate          */
1018*7c478bd9Sstevel@tonic-gate         soi.soinfo_prfd = sslfd;
1019*7c478bd9Sstevel@tonic-gate         sslfd = PR_PopIOLayer( soi.soinfo_prfd, PR_TOP_IO_LAYER );
1020*7c478bd9Sstevel@tonic-gate         sslfd->dtor( sslfd );
1021*7c478bd9Sstevel@tonic-gate     }
1022*7c478bd9Sstevel@tonic-gate     if ( NULL != ssoip ) {
1023*7c478bd9Sstevel@tonic-gate         ldapssl_free_socket_info( &ssoip );
1024*7c478bd9Sstevel@tonic-gate         soi.soinfo_appdata = NULL;
1025*7c478bd9Sstevel@tonic-gate     }
1026*7c478bd9Sstevel@tonic-gate     prldap_set_default_socket_info( ld, &soi );
1027*7c478bd9Sstevel@tonic-gate 
1028*7c478bd9Sstevel@tonic-gate     return( -1 );
1029*7c478bd9Sstevel@tonic-gate }
1030*7c478bd9Sstevel@tonic-gate 
1031*7c478bd9Sstevel@tonic-gate 
1032*7c478bd9Sstevel@tonic-gate /*
1033*7c478bd9Sstevel@tonic-gate  * Reset an LDAP session from SSL to a non-secure status.
1034*7c478bd9Sstevel@tonic-gate  * Basically, this function undoes the work done by ldapssl_install_routines.
1035*7c478bd9Sstevel@tonic-gate  */
ldapssl_reset_to_nonsecure(LDAP * ld)1036*7c478bd9Sstevel@tonic-gate int ldapssl_reset_to_nonsecure ( LDAP *ld )
1037*7c478bd9Sstevel@tonic-gate {
1038*7c478bd9Sstevel@tonic-gate     PRLDAPSessionInfo   sei;
1039*7c478bd9Sstevel@tonic-gate     LDAPSSLSessionInfo  *sseip;
1040*7c478bd9Sstevel@tonic-gate 
1041*7c478bd9Sstevel@tonic-gate     struct ldap_x_ext_io_fns    iofns;
1042*7c478bd9Sstevel@tonic-gate     int rc = 0;
1043*7c478bd9Sstevel@tonic-gate 
1044*7c478bd9Sstevel@tonic-gate     /*
1045*7c478bd9Sstevel@tonic-gate      * Retrieve session info.
1046*7c478bd9Sstevel@tonic-gate      */
1047*7c478bd9Sstevel@tonic-gate     memset( &sei, 0, sizeof(sei));
1048*7c478bd9Sstevel@tonic-gate     sei.seinfo_size = PRLDAP_SESSIONINFO_SIZE;
1049*7c478bd9Sstevel@tonic-gate     if ( prldap_get_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
1050*7c478bd9Sstevel@tonic-gate         return( -1 );
1051*7c478bd9Sstevel@tonic-gate     }
1052*7c478bd9Sstevel@tonic-gate     sseip = (LDAPSSLSessionInfo *)sei.seinfo_appdata;
1053*7c478bd9Sstevel@tonic-gate 
1054*7c478bd9Sstevel@tonic-gate     if ( sseip != NULL ) {
1055*7c478bd9Sstevel@tonic-gate         /*
1056*7c478bd9Sstevel@tonic-gate          * Reset the standard extended io functions.
1057*7c478bd9Sstevel@tonic-gate          */
1058*7c478bd9Sstevel@tonic-gate         memset( &iofns, 0, sizeof(iofns));
1059*7c478bd9Sstevel@tonic-gate         iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
1060*7c478bd9Sstevel@tonic-gate         if ( ldap_get_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns )
1061*7c478bd9Sstevel@tonic-gate 		< 0) {
1062*7c478bd9Sstevel@tonic-gate             rc = -1;
1063*7c478bd9Sstevel@tonic-gate             goto free_session_info;
1064*7c478bd9Sstevel@tonic-gate         }
1065*7c478bd9Sstevel@tonic-gate 
1066*7c478bd9Sstevel@tonic-gate         /* reset socket, connect, and ioctl */
1067*7c478bd9Sstevel@tonic-gate         iofns.lextiof_connect = sseip->lssei_std_functions.lssf_connect_fn;
1068*7c478bd9Sstevel@tonic-gate         iofns.lextiof_close = sseip->lssei_std_functions.lssf_close_fn;
1069*7c478bd9Sstevel@tonic-gate         iofns.lextiof_disposehandle =
1070*7c478bd9Sstevel@tonic-gate 			sseip->lssei_std_functions.lssf_disposehdl_fn;
1071*7c478bd9Sstevel@tonic-gate 
1072*7c478bd9Sstevel@tonic-gate         if ( ldap_set_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns )
1073*7c478bd9Sstevel@tonic-gate 		< 0) {
1074*7c478bd9Sstevel@tonic-gate             rc = -1;
1075*7c478bd9Sstevel@tonic-gate             goto free_session_info;
1076*7c478bd9Sstevel@tonic-gate         }
1077*7c478bd9Sstevel@tonic-gate 
1078*7c478bd9Sstevel@tonic-gate free_session_info:
1079*7c478bd9Sstevel@tonic-gate         ldapssl_free_session_info( &sseip );
1080*7c478bd9Sstevel@tonic-gate         sei.seinfo_appdata = NULL;
1081*7c478bd9Sstevel@tonic-gate         if ( prldap_set_session_info( ld, NULL, &sei ) != LDAP_SUCCESS ) {
1082*7c478bd9Sstevel@tonic-gate             rc = -1;
1083*7c478bd9Sstevel@tonic-gate         }
1084*7c478bd9Sstevel@tonic-gate     } /* if ( sseip && *sseip ) */
1085*7c478bd9Sstevel@tonic-gate 
1086*7c478bd9Sstevel@tonic-gate     if ( ldap_set_option( ld, LDAP_OPT_SSL, LDAP_OPT_OFF ) < 0 ) {
1087*7c478bd9Sstevel@tonic-gate         return (-1);
1088*7c478bd9Sstevel@tonic-gate     }
1089*7c478bd9Sstevel@tonic-gate 
1090*7c478bd9Sstevel@tonic-gate     return rc;
1091*7c478bd9Sstevel@tonic-gate }
1092*7c478bd9Sstevel@tonic-gate 
1093*7c478bd9Sstevel@tonic-gate 
1094*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
1095*7c478bd9Sstevel@tonic-gate static void
_nss_initf_ipnodes(nss_db_params_t * p)1096*7c478bd9Sstevel@tonic-gate _nss_initf_ipnodes(nss_db_params_t *p)
1097*7c478bd9Sstevel@tonic-gate {
1098*7c478bd9Sstevel@tonic-gate 	static char *no_service = "";
1099*7c478bd9Sstevel@tonic-gate 
1100*7c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_IPNODES;
1101*7c478bd9Sstevel@tonic-gate         p->flags |= NSS_USE_DEFAULT_CONFIG;
1102*7c478bd9Sstevel@tonic-gate         p->default_config = host_service == NULL ? no_service : host_service;
1103*7c478bd9Sstevel@tonic-gate }
1104*7c478bd9Sstevel@tonic-gate 
1105*7c478bd9Sstevel@tonic-gate static void
_nss_initf_hosts(nss_db_params_t * p)1106*7c478bd9Sstevel@tonic-gate _nss_initf_hosts(nss_db_params_t *p)
1107*7c478bd9Sstevel@tonic-gate {
1108*7c478bd9Sstevel@tonic-gate 	static char *no_service = "";
1109*7c478bd9Sstevel@tonic-gate 
1110*7c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_HOSTS;
1111*7c478bd9Sstevel@tonic-gate         p->flags |= NSS_USE_DEFAULT_CONFIG;
1112*7c478bd9Sstevel@tonic-gate         p->default_config = host_service == NULL ? no_service : host_service;
1113*7c478bd9Sstevel@tonic-gate }
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate static struct hostent *
_switch_gethostbyaddr_r(const char * addr,int len,int type,struct hostent * result,char * buffer,int buflen,int * h_errnop)1116*7c478bd9Sstevel@tonic-gate _switch_gethostbyaddr_r(const char *addr, int len, int type,
1117*7c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen,
1118*7c478bd9Sstevel@tonic-gate 	int *h_errnop)
1119*7c478bd9Sstevel@tonic-gate {
1120*7c478bd9Sstevel@tonic-gate         nss_XbyY_args_t arg;
1121*7c478bd9Sstevel@tonic-gate         nss_status_t    res;
1122*7c478bd9Sstevel@tonic-gate 	int		(*str2ent)();
1123*7c478bd9Sstevel@tonic-gate 	void		(*nss_initf)();
1124*7c478bd9Sstevel@tonic-gate 	nss_db_root_t	*nss_db_root;
1125*7c478bd9Sstevel@tonic-gate 
1126*7c478bd9Sstevel@tonic-gate 	if (AF_INET == type) {
1127*7c478bd9Sstevel@tonic-gate 		str2ent		= str2hostent;
1128*7c478bd9Sstevel@tonic-gate 		nss_initf	= _nss_initf_hosts;
1129*7c478bd9Sstevel@tonic-gate 		nss_db_root	= &db_root_hosts;
1130*7c478bd9Sstevel@tonic-gate 	} else if (AF_INET6 == type) {
1131*7c478bd9Sstevel@tonic-gate 		str2ent		= str2hostent6;
1132*7c478bd9Sstevel@tonic-gate 		nss_initf	= _nss_initf_ipnodes;
1133*7c478bd9Sstevel@tonic-gate 		nss_db_root	= &db_root_ipnodes;
1134*7c478bd9Sstevel@tonic-gate 	} else {
1135*7c478bd9Sstevel@tonic-gate 		return NULL;
1136*7c478bd9Sstevel@tonic-gate 	}
1137*7c478bd9Sstevel@tonic-gate 
1138*7c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2ent);
1139*7c478bd9Sstevel@tonic-gate 
1140*7c478bd9Sstevel@tonic-gate         arg.key.hostaddr.addr	= addr;
1141*7c478bd9Sstevel@tonic-gate         arg.key.hostaddr.len	= len;
1142*7c478bd9Sstevel@tonic-gate         arg.key.hostaddr.type	= type;
1143*7c478bd9Sstevel@tonic-gate         arg.stayopen		= 0;
1144*7c478bd9Sstevel@tonic-gate 
1145*7c478bd9Sstevel@tonic-gate         res = nss_search(nss_db_root, nss_initf,
1146*7c478bd9Sstevel@tonic-gate         	NSS_DBOP_HOSTS_BYADDR, &arg);
1147*7c478bd9Sstevel@tonic-gate         arg.status = res;
1148*7c478bd9Sstevel@tonic-gate         *h_errnop = arg.h_errno;
1149*7c478bd9Sstevel@tonic-gate         return (struct hostent *)NSS_XbyY_FINI(&arg);
1150*7c478bd9Sstevel@tonic-gate }
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate /*
1153*7c478bd9Sstevel@tonic-gate  * ns_gethostbyaddr is used to be a substitute gethostbyaddr for
1154*7c478bd9Sstevel@tonic-gate  * libldap when ssl will need to determine the fully qualified
1155*7c478bd9Sstevel@tonic-gate  * host name from an address when it is unsafe to use the normal
1156*7c478bd9Sstevel@tonic-gate  * nameservice functions.
1157*7c478bd9Sstevel@tonic-gate  *
1158*7c478bd9Sstevel@tonic-gate  * Note that the ldap name service resolver calls this with the address as
1159*7c478bd9Sstevel@tonic-gate  * a character string - which we must convert into address form.
1160*7c478bd9Sstevel@tonic-gate  */
1161*7c478bd9Sstevel@tonic-gate 
1162*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1163*7c478bd9Sstevel@tonic-gate static LDAPHostEnt *
ns_gethostbyaddr(const char * addr,int len,int type,LDAPHostEnt * result,char * buffer,int buflen,int * statusp,void * extradata)1164*7c478bd9Sstevel@tonic-gate ns_gethostbyaddr(const char *addr, int len, int type,
1165*7c478bd9Sstevel@tonic-gate 	LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
1166*7c478bd9Sstevel@tonic-gate 	void *extradata)
1167*7c478bd9Sstevel@tonic-gate {
1168*7c478bd9Sstevel@tonic-gate 	LDAPHostEnt	*ldap_hent;
1169*7c478bd9Sstevel@tonic-gate 	int		h_errno;
1170*7c478bd9Sstevel@tonic-gate 	struct hostent	h_ent;
1171*7c478bd9Sstevel@tonic-gate 	struct hostent	*h_e = NULL;
1172*7c478bd9Sstevel@tonic-gate 	struct in_addr	a;
1173*7c478bd9Sstevel@tonic-gate 	struct in6_addr	a6;
1174*7c478bd9Sstevel@tonic-gate 	int		inet_error;	/* error returned by inet_pton */
1175*7c478bd9Sstevel@tonic-gate 
1176*7c478bd9Sstevel@tonic-gate 
1177*7c478bd9Sstevel@tonic-gate 	if (addr == NULL || result == NULL || buffer == NULL ||
1178*7c478bd9Sstevel@tonic-gate 			(type != AF_INET && type != AF_INET6))
1179*7c478bd9Sstevel@tonic-gate 		return (NULL);
1180*7c478bd9Sstevel@tonic-gate 
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate 	(void) memset(&h_ent, 0, sizeof (h_ent));
1183*7c478bd9Sstevel@tonic-gate 
1184*7c478bd9Sstevel@tonic-gate 	if (AF_INET == type) {
1185*7c478bd9Sstevel@tonic-gate 		if (inet_pton(type, addr, &a.s_addr) == 1) {
1186*7c478bd9Sstevel@tonic-gate 			h_e = _switch_gethostbyaddr_r((char *)&a,
1187*7c478bd9Sstevel@tonic-gate 				sizeof (a.s_addr), type, &h_ent,
1188*7c478bd9Sstevel@tonic-gate 				buffer, buflen, &h_errno);
1189*7c478bd9Sstevel@tonic-gate 		}
1190*7c478bd9Sstevel@tonic-gate 	} else if (AF_INET6 == type) {
1191*7c478bd9Sstevel@tonic-gate 		if (inet_pton(type, addr, &a6.s6_addr) == 1) {
1192*7c478bd9Sstevel@tonic-gate 			h_e = _switch_gethostbyaddr_r((char *)&a6,
1193*7c478bd9Sstevel@tonic-gate 				sizeof (a6.s6_addr), type, &h_ent,
1194*7c478bd9Sstevel@tonic-gate 				buffer, buflen, &h_errno);
1195*7c478bd9Sstevel@tonic-gate 		}
1196*7c478bd9Sstevel@tonic-gate 	}
1197*7c478bd9Sstevel@tonic-gate 
1198*7c478bd9Sstevel@tonic-gate 	if (h_e == NULL) {
1199*7c478bd9Sstevel@tonic-gate 		ldap_hent = NULL;
1200*7c478bd9Sstevel@tonic-gate 	} else {
1201*7c478bd9Sstevel@tonic-gate 		(void) memset(result, 0, sizeof (LDAPHostEnt));
1202*7c478bd9Sstevel@tonic-gate 		ldap_hent = result;
1203*7c478bd9Sstevel@tonic-gate 		result->ldaphe_name = h_e->h_name;
1204*7c478bd9Sstevel@tonic-gate  		result->ldaphe_aliases = h_e->h_aliases;
1205*7c478bd9Sstevel@tonic-gate  		result->ldaphe_addrtype = h_e->h_addrtype;
1206*7c478bd9Sstevel@tonic-gate  		result->ldaphe_length = h_e->h_length;
1207*7c478bd9Sstevel@tonic-gate  		result->ldaphe_addr_list = h_e->h_addr_list;
1208*7c478bd9Sstevel@tonic-gate 	}
1209*7c478bd9Sstevel@tonic-gate 	return (ldap_hent);
1210*7c478bd9Sstevel@tonic-gate }
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate /*
1213*7c478bd9Sstevel@tonic-gate  * ldapssl_install_gethostbyaddr attempts to prevent recursion in
1214*7c478bd9Sstevel@tonic-gate  * gethostbyaddr calls when an ip address is given to ssl. This ip address
1215*7c478bd9Sstevel@tonic-gate  * must be resolved to a host name.
1216*7c478bd9Sstevel@tonic-gate  *
1217*7c478bd9Sstevel@tonic-gate  * For example, libsldap cannot use LDAP to resolve this address to a
1218*7c478bd9Sstevel@tonic-gate  * name because of recursion. The caller is instructing libldap to skip
1219*7c478bd9Sstevel@tonic-gate  * the specified name service when resolving addresses for the specified
1220*7c478bd9Sstevel@tonic-gate  * ldap connection.
1221*7c478bd9Sstevel@tonic-gate  *
1222*7c478bd9Sstevel@tonic-gate  * Currently only ldap and dns name services always return fully qualified
1223*7c478bd9Sstevel@tonic-gate  * names. The other name services (files, nis, and nisplus) will returned
1224*7c478bd9Sstevel@tonic-gate  * fully qualified names if the host names are stored as fully qualified names
1225*7c478bd9Sstevel@tonic-gate  * in these name services.
1226*7c478bd9Sstevel@tonic-gate  *
1227*7c478bd9Sstevel@tonic-gate  * Note:
1228*7c478bd9Sstevel@tonic-gate  *
1229*7c478bd9Sstevel@tonic-gate  *	Since host_service applies to all connections, calling
1230*7c478bd9Sstevel@tonic-gate  *	ldapssl_install_gethostbyaddr with different name services to
1231*7c478bd9Sstevel@tonic-gate  *	skip will lead to unpredictable results.
1232*7c478bd9Sstevel@tonic-gate  *
1233*7c478bd9Sstevel@tonic-gate  * Returns:
1234*7c478bd9Sstevel@tonic-gate  *	0	if success
1235*7c478bd9Sstevel@tonic-gate  *	-1	if failure
1236*7c478bd9Sstevel@tonic-gate  */
1237*7c478bd9Sstevel@tonic-gate 
1238*7c478bd9Sstevel@tonic-gate int
ldapssl_install_gethostbyaddr(LDAP * ld,const char * skip)1239*7c478bd9Sstevel@tonic-gate ldapssl_install_gethostbyaddr(LDAP *ld, const char *skip)
1240*7c478bd9Sstevel@tonic-gate {
1241*7c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err		pserr;
1242*7c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig	*conf;
1243*7c478bd9Sstevel@tonic-gate 	struct __nsw_lookup		*lkp;
1244*7c478bd9Sstevel@tonic-gate 	struct ldap_dns_fns		dns_fns;
1245*7c478bd9Sstevel@tonic-gate 	char				*name_list = NULL;
1246*7c478bd9Sstevel@tonic-gate 	char				*tmp;
1247*7c478bd9Sstevel@tonic-gate 	const char			*name;
1248*7c478bd9Sstevel@tonic-gate 	int				len;
1249*7c478bd9Sstevel@tonic-gate 	boolean_t			got_skip = B_FALSE;
1250*7c478bd9Sstevel@tonic-gate 
1251*7c478bd9Sstevel@tonic-gate 	/*
1252*7c478bd9Sstevel@tonic-gate 	 * db_root_hosts.lock mutex is used to ensure that the name list
1253*7c478bd9Sstevel@tonic-gate 	 * is not in use by the name service switch while we are updating
1254*7c478bd9Sstevel@tonic-gate 	 * the host_service
1255*7c478bd9Sstevel@tonic-gate 	 */
1256*7c478bd9Sstevel@tonic-gate 
1257*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&db_root_hosts.lock);
1258*7c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig("hosts", &pserr);
1259*7c478bd9Sstevel@tonic-gate 	if (conf == NULL) {
1260*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&db_root_hosts.lock);
1261*7c478bd9Sstevel@tonic-gate 		return (0);
1262*7c478bd9Sstevel@tonic-gate 	}
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate 	/* check for ldap and count other backends */
1265*7c478bd9Sstevel@tonic-gate 	for (lkp = conf->lookups; lkp != NULL; lkp = lkp->next) {
1266*7c478bd9Sstevel@tonic-gate 		name = lkp->service_name;
1267*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, skip) == 0) {
1268*7c478bd9Sstevel@tonic-gate 			got_skip = B_TRUE;
1269*7c478bd9Sstevel@tonic-gate 			continue;
1270*7c478bd9Sstevel@tonic-gate 		}
1271*7c478bd9Sstevel@tonic-gate 		if (name_list == NULL)
1272*7c478bd9Sstevel@tonic-gate 			name_list = strdup(name);
1273*7c478bd9Sstevel@tonic-gate 		else {
1274*7c478bd9Sstevel@tonic-gate 			len = strlen(name_list);
1275*7c478bd9Sstevel@tonic-gate 			tmp = realloc(name_list, len + strlen(name) + 2);
1276*7c478bd9Sstevel@tonic-gate 			if (tmp == NULL) {
1277*7c478bd9Sstevel@tonic-gate 				free(name_list);
1278*7c478bd9Sstevel@tonic-gate 				name_list = NULL;
1279*7c478bd9Sstevel@tonic-gate 			} else {
1280*7c478bd9Sstevel@tonic-gate 				name_list = tmp;
1281*7c478bd9Sstevel@tonic-gate 				name_list[len++] = ' ';
1282*7c478bd9Sstevel@tonic-gate 				(void) strcpy(name_list+len, name);
1283*7c478bd9Sstevel@tonic-gate 			}
1284*7c478bd9Sstevel@tonic-gate 		}
1285*7c478bd9Sstevel@tonic-gate 		if (name_list == NULL) {	/* alloc error */
1286*7c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&db_root_hosts.lock);
1287*7c478bd9Sstevel@tonic-gate 			__nsw_freeconfig(conf);
1288*7c478bd9Sstevel@tonic-gate 			return (-1);
1289*7c478bd9Sstevel@tonic-gate 		}
1290*7c478bd9Sstevel@tonic-gate 	}
1291*7c478bd9Sstevel@tonic-gate 	__nsw_freeconfig(conf);
1292*7c478bd9Sstevel@tonic-gate 	if (!got_skip) {
1293*7c478bd9Sstevel@tonic-gate 		/*
1294*7c478bd9Sstevel@tonic-gate 		 * Since skip name service not used for hosts, we do not need
1295*7c478bd9Sstevel@tonic-gate 		 * to install our private address resolution function
1296*7c478bd9Sstevel@tonic-gate 		 */
1297*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&db_root_hosts.lock);
1298*7c478bd9Sstevel@tonic-gate 		if (name_list != NULL)
1299*7c478bd9Sstevel@tonic-gate 			free(name_list);
1300*7c478bd9Sstevel@tonic-gate 		return (0);
1301*7c478bd9Sstevel@tonic-gate 	}
1302*7c478bd9Sstevel@tonic-gate 	if (host_service != NULL)
1303*7c478bd9Sstevel@tonic-gate 		free(host_service);
1304*7c478bd9Sstevel@tonic-gate 	host_service = name_list;
1305*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&db_root_hosts.lock);
1306*7c478bd9Sstevel@tonic-gate 
1307*7c478bd9Sstevel@tonic-gate 	if (ldap_get_option(ld, LDAP_OPT_DNS_FN_PTRS, &dns_fns) != 0)
1308*7c478bd9Sstevel@tonic-gate 		return (-1);
1309*7c478bd9Sstevel@tonic-gate 	dns_fns.lddnsfn_gethostbyaddr = ns_gethostbyaddr;
1310*7c478bd9Sstevel@tonic-gate 	if (ldap_set_option(ld, LDAP_OPT_DNS_FN_PTRS, &dns_fns) != 0)
1311*7c478bd9Sstevel@tonic-gate 		return (-1);
1312*7c478bd9Sstevel@tonic-gate 	return (0);
1313*7c478bd9Sstevel@tonic-gate }
1314*7c478bd9Sstevel@tonic-gate #endif	/* _SOLARIS_SDK */
1315*7c478bd9Sstevel@tonic-gate #endif /* NET_SSL */
1316