1180740Sdes#!/bin/sh
2180740Sdes#
3180744Sdes# $Id: findssl.sh,v 1.4 2007/02/19 11:44:25 dtucker Exp $
4180740Sdes#
5180740Sdes# findssl.sh
6180740Sdes#	Search for all instances of OpenSSL headers and libraries
7180740Sdes#	and print their versions.
8180740Sdes#	Intended to help diagnose OpenSSH's "OpenSSL headers do not
9180740Sdes#	match your library" errors.
10180740Sdes#
11180740Sdes#	Written by Darren Tucker (dtucker at zip dot com dot au)
12180740Sdes#	This file is placed in the public domain.
13180740Sdes#
14180740Sdes#	Release history:
15180740Sdes#	2002-07-27: Initial release.
16180740Sdes#	2002-08-04: Added public domain notice.
17180740Sdes#	2003-06-24: Incorporated readme, set library paths. First cvs version.
18180740Sdes#	2004-12-13: Add traps to cleanup temp files, from Amarendra Godbole.
19180740Sdes#
20180740Sdes# "OpenSSL headers do not match your library" are usually caused by
21180740Sdes# OpenSSH's configure picking up an older version of OpenSSL headers
22180740Sdes# or libraries.  You can use the following # procedure to help identify
23180740Sdes# the cause.
24180740Sdes#
25180740Sdes# The  output  of  configure  will  tell you the versions of the OpenSSL
26180740Sdes# headers and libraries that were picked up, for example:
27180740Sdes#
28180740Sdes# checking OpenSSL header version... 90604f (OpenSSL 0.9.6d 9 May 2002)
29180740Sdes# checking OpenSSL library version... 90602f (OpenSSL 0.9.6b [engine] 9 Jul 2001)
30180740Sdes# checking whether OpenSSL's headers match the library... no
31180740Sdes# configure: error: Your OpenSSL headers do not match your library
32180740Sdes#
33180740Sdes# Now run findssl.sh. This should identify the headers and libraries
34180740Sdes# present  and  their  versions.  You  should  be  able  to identify the
35180740Sdes# libraries  and headers used and adjust your CFLAGS or remove incorrect
36180740Sdes# versions.  The  output will show OpenSSL's internal version identifier
37180740Sdes# and should look something like:
38180740Sdes
39180740Sdes# $ ./findssl.sh
40180740Sdes# Searching for OpenSSL header files.
41180740Sdes# 0x0090604fL /usr/include/openssl/opensslv.h
42180740Sdes# 0x0090604fL /usr/local/ssl/include/openssl/opensslv.h
43180740Sdes#
44180740Sdes# Searching for OpenSSL shared library files.
45180740Sdes# 0x0090602fL /lib/libcrypto.so.0.9.6b
46180740Sdes# 0x0090602fL /lib/libcrypto.so.2
47180740Sdes# 0x0090581fL /usr/lib/libcrypto.so.0
48180740Sdes# 0x0090602fL /usr/lib/libcrypto.so
49180740Sdes# 0x0090581fL /usr/lib/libcrypto.so.0.9.5a
50180740Sdes# 0x0090600fL /usr/lib/libcrypto.so.0.9.6
51180740Sdes# 0x0090600fL /usr/lib/libcrypto.so.1
52180740Sdes#
53180740Sdes# Searching for OpenSSL static library files.
54180740Sdes# 0x0090602fL /usr/lib/libcrypto.a
55180740Sdes# 0x0090604fL /usr/local/ssl/lib/libcrypto.a
56180740Sdes#
57180740Sdes# In  this  example, I gave configure no extra flags, so it's picking up
58180740Sdes# the  OpenSSL header from /usr/include/openssl (90604f) and the library
59180740Sdes# from /usr/lib/ (90602f).
60180740Sdes
61180740Sdes#
62180740Sdes# Adjust these to suit your compiler.
63180740Sdes# You may also need to set the *LIB*PATH environment variables if
64180740Sdes# DEFAULT_LIBPATH is not correct for your system.
65180740Sdes#
66180740SdesCC=gcc
67180740SdesSTATIC=-static
68180740Sdes
69180740Sdes#
70180740Sdes# Cleanup on interrupt
71180740Sdes#
72180740Sdestrap 'rm -f conftest.c' INT HUP TERM
73180740Sdes
74180740Sdes#
75180740Sdes# Set up conftest C source
76180740Sdes#
77180740Sdesrm -f findssl.log
78180740Sdescat >conftest.c <<EOD
79180740Sdes#include <stdio.h>
80180740Sdesint main(){printf("0x%08xL\n", SSLeay());}
81180740SdesEOD
82180740Sdes
83180740Sdes#
84180740Sdes# Set default library paths if not already set
85180740Sdes#
86180740SdesDEFAULT_LIBPATH=/usr/lib:/usr/local/lib
87180740SdesLIBPATH=${LIBPATH:=$DEFAULT_LIBPATH}
88180740SdesLD_LIBRARY_PATH=${LD_LIBRARY_PATH:=$DEFAULT_LIBPATH}
89180740SdesLIBRARY_PATH=${LIBRARY_PATH:=$DEFAULT_LIBPATH}
90180740Sdesexport LIBPATH LD_LIBRARY_PATH LIBRARY_PATH
91180740Sdes
92180740Sdes# not all platforms have a 'which' command
93180740Sdesif which ls >/dev/null 2>/dev/null; then
94180740Sdes    : which is defined
95180740Sdeselse
96180740Sdes    which () {
97180740Sdes	saveIFS="$IFS"
98180740Sdes	IFS=:
99180740Sdes	for p in $PATH; do
100180740Sdes	    if test -x "$p/$1" -a -f "$p/$1"; then
101180740Sdes		IFS="$saveIFS"
102180740Sdes		echo "$p/$1"
103180740Sdes		return 0
104180740Sdes	    fi
105180740Sdes	done
106180740Sdes	IFS="$saveIFS"
107180740Sdes	return 1
108180740Sdes    }
109180740Sdesfi
110180740Sdes
111180740Sdes#
112180740Sdes# Search for OpenSSL headers and print versions
113180740Sdes#
114180740Sdesecho Searching for OpenSSL header files.
115180740Sdesif [ -x "`which locate`" ]
116180740Sdesthen
117180740Sdes	headers=`locate opensslv.h`
118180740Sdeselse
119180740Sdes	headers=`find / -name opensslv.h -print 2>/dev/null`
120180740Sdesfi
121180740Sdes
122180740Sdesfor header in $headers
123180740Sdesdo
124180740Sdes	ver=`awk '/OPENSSL_VERSION_NUMBER/{printf \$3}' $header`
125180740Sdes	echo "$ver $header"
126180740Sdesdone
127180740Sdesecho
128180740Sdes
129180740Sdes#
130180740Sdes# Search for shared libraries.
131180740Sdes# Relies on shared libraries looking like "libcrypto.s*"
132180740Sdes#
133180740Sdesecho Searching for OpenSSL shared library files.
134180740Sdesif [ -x "`which locate`" ]
135180740Sdesthen
136180740Sdes	libraries=`locate libcrypto.s`
137180740Sdeselse
138180740Sdes	libraries=`find / -name 'libcrypto.s*' -print 2>/dev/null`
139180740Sdesfi
140180740Sdes
141180740Sdesfor lib in $libraries
142180740Sdesdo
143180740Sdes	(echo "Trying libcrypto $lib" >>findssl.log
144180740Sdes	dir=`dirname $lib`
145180740Sdes	LIBPATH="$dir:$LIBPATH"
146180740Sdes	LD_LIBRARY_PATH="$dir:$LIBPATH"
147180740Sdes	LIBRARY_PATH="$dir:$LIBPATH"
148180740Sdes	export LIBPATH LD_LIBRARY_PATH LIBRARY_PATH
149180740Sdes	${CC} -o conftest conftest.c $lib 2>>findssl.log
150180740Sdes	if [ -x ./conftest ]
151180740Sdes	then
152180740Sdes		ver=`./conftest 2>/dev/null`
153180740Sdes		rm -f ./conftest
154180740Sdes		echo "$ver $lib"
155180740Sdes	fi)
156180740Sdesdone
157180740Sdesecho
158180740Sdes
159180740Sdes#
160180740Sdes# Search for static OpenSSL libraries and print versions
161180740Sdes#
162180740Sdesecho Searching for OpenSSL static library files.
163180740Sdesif [ -x "`which locate`" ]
164180740Sdesthen
165180740Sdes	libraries=`locate libcrypto.a`
166180740Sdeselse
167180740Sdes	libraries=`find / -name libcrypto.a -print 2>/dev/null`
168180740Sdesfi
169180740Sdes
170180740Sdesfor lib in $libraries
171180740Sdesdo
172180740Sdes	libdir=`dirname $lib`
173180740Sdes	echo "Trying libcrypto $lib" >>findssl.log
174180740Sdes	${CC} ${STATIC} -o conftest conftest.c -L${libdir} -lcrypto 2>>findssl.log
175180740Sdes	if [ -x ./conftest ]
176180740Sdes	then
177180740Sdes		ver=`./conftest 2>/dev/null`
178180740Sdes		rm -f ./conftest
179180740Sdes		echo "$ver $lib"
180180740Sdes	fi
181180740Sdesdone
182180740Sdes
183180740Sdes#
184180740Sdes# Clean up
185180740Sdes#
186180740Sdesrm -f conftest.c
187