A simple framework to deal with future security issues in libssl
[freeradius.git] / src / main / version.c
1 /*
2  * version.c    Print version number and exit.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 1999-2014  The FreeRADIUS server project
21  * Copyright 2012  Alan DeKok <aland@ox.org>
22  * Copyright 2000  Chris Parker <cparker@starnetusa.com>
23  */
24
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 USES_APPLE_DEPRECATED_API       /* OpenSSL API has been deprecated by Apple */
29
30 static uint64_t libmagic = RADIUSD_MAGIC_NUMBER;
31
32 #ifdef HAVE_OPENSSL_CRYPTO_H
33 #  include <openssl/crypto.h>
34 #  include <openssl/opensslv.h>
35
36 static long ssl_built = OPENSSL_VERSION_NUMBER;
37
38 /** Check build and linked versions of OpenSSL match
39  *
40  * Startup check for whether the linked version of OpenSSL matches the
41  * version the server was built against.
42  *
43  * @return 0 if ok, else -1
44  */
45 int ssl_check_consistency(void)
46 {
47         long ssl_linked;
48
49         ssl_linked = SSLeay();
50
51         if (ssl_linked != ssl_built) {
52                 ERROR("libssl version mismatch.  built: %lx linked: %lx",
53                        (unsigned long) ssl_built,
54                        (unsigned long) ssl_linked);
55
56                 return -1;
57         };
58
59         return 0;
60 }
61
62 /** Convert a version number to a text string
63  *
64  * @note Not thread safe.
65  *
66  * @param v version to convert.
67  * @return pointer to a static buffer containing the version string.
68  */
69 char const *ssl_version_by_num(uint64_t v)
70 {
71         static char buffer[12];
72
73         snprintf(buffer, sizeof(buffer), "%i.%i.%i%c %i",
74                  (int) ((0xff0000000 & v) >> 28),
75                  (int) ((0x00ff00000 & v) >> 20),
76                  (int) ((0x0000ff000 & v) >> 12),
77                  (char)((0x000000ff0 & v) >> 4 ? (0x60 + ((0x000000ff0 & v) >> 4)) : ' '),
78                  (int) ((0x00000000f & v)));
79
80         return buffer;
81 }
82
83 /** Convert two openssl version numbers into a range string
84  *
85  * @note Not thread safe.
86  *
87  * @param low version to convert.
88  * @param high version to convert.
89  * @return pointer to a static buffer containing the version range string.
90  */
91 char const *ssl_version_range(uint64_t low, uint64_t high)
92 {
93         static char buffer[26];
94
95         strcat(buffer, ssl_version_by_num(low));
96         strcat(buffer, "-");
97         strcat(buffer, ssl_version_by_num(high));
98
99         return buffer;
100 }
101
102 /** Print the current linked version of Openssl
103  *
104  * Print the currently linked version of the OpenSSL library.
105  *
106  * @note Not thread safe.
107  */
108 char const *ssl_version(void)
109 {
110         static char buffer[256];
111
112         uint64_t v = (uint64_t) SSLeay();
113
114         snprintf(buffer, sizeof(buffer), "%s 0x%.9" PRIx64 " (%s)",
115                  SSLeay_version(SSLEAY_VERSION),                /* Not all builds include a useful version number */
116                  v,
117                  ssl_version_by_num((uint64_t) v));
118
119         return buffer;
120 }
121 #  else
122 int ssl_check_consistency(void) {
123         return 0;
124 }
125
126 char const *ssl_version()
127 {
128         return "not linked";
129 }
130 #endif /* ifdef HAVE_OPENSSL_CRYPTO_H */
131
132
133 /** Check if the application linking to the library has the correct magic number
134  *
135  * @param magic number as defined by RADIUSD_MAGIC_NUMBER
136  * @returns 0 on success, -1 on prefix mismatch, -2 on version mismatch -3 on commit mismatch.
137  */
138 int rad_check_lib_magic(uint64_t magic)
139 {
140         if (MAGIC_PREFIX(magic) != MAGIC_PREFIX(libmagic)) {
141                 ERROR("Application and libfreeradius-server magic number (prefix) mismatch."
142                       "  application: %x library: %x",
143                       MAGIC_PREFIX(magic), MAGIC_PREFIX(libmagic));
144                 return -1;
145         }
146
147         if (MAGIC_VERSION(magic) != MAGIC_VERSION(libmagic)) {
148                 ERROR("Application and libfreeradius-server magic number (version) mismatch."
149                       "  application: %lx library: %lx",
150                       (unsigned long) MAGIC_VERSION(magic), (unsigned long) MAGIC_VERSION(libmagic));
151                 return -2;
152         }
153
154         if (MAGIC_COMMIT(magic) != MAGIC_COMMIT(libmagic)) {
155                 ERROR("Application and libfreeradius-server magic number (commit) mismatch."
156                       "  application: %lx library: %lx",
157                       (unsigned long) MAGIC_COMMIT(magic), (unsigned long) MAGIC_COMMIT(libmagic));
158                 return -3;
159         }
160
161         return 0;
162 }
163
164 /*
165  *      Display the revision number for this program
166  */
167 void version(void)
168 {
169         INFO("%s: %s", progname, radiusd_version);
170
171         DEBUG3("Server was built with: ");
172
173 #ifdef WITH_ACCOUNTING
174         DEBUG3("  accounting");
175 #endif
176         DEBUG3("  authentication"); /* always enabled */
177
178 #ifdef WITH_ASCEND_BINARY
179         DEBUG3("  ascend binary attributes");
180 #endif
181 #ifdef WITH_COA
182         DEBUG3("  coa");
183 #endif
184 #ifdef WITH_COMMAND_SOCKET
185         DEBUG3("  control-socket");
186 #endif
187 #ifdef WITH_DETAIL
188         DEBUG3("  detail");
189 #endif
190 #ifdef WITH_DHCP
191         DEBUG3("  dhcp");
192 #endif
193 #ifdef WITH_DYNAMIC_CLIENTS
194         DEBUG3("  dynamic clients");
195 #endif
196 #ifdef OSFC2
197         DEBUG3("  OSFC2");
198 #endif
199 #ifdef WITH_PROXY
200         DEBUG3("  proxy");
201 #endif
202 #ifdef HAVE_PCREPOSIX_H
203         DEBUG3("  regex-pcre");
204 #else
205 #ifdef HAVE_REGEX_H
206         DEBUG3("  regex-posix");
207 #endif
208 #endif
209
210 #ifdef WITH_SESSION_MGMT
211         DEBUG3("  session-management");
212 #endif
213 #ifdef WITH_STATS
214         DEBUG3("  stats");
215 #endif
216 #ifdef WITH_TCP
217         DEBUG3("  tcp");
218 #endif
219 #ifdef WITH_THREADS
220         DEBUG3("  threads");
221 #endif
222 #ifdef WITH_TLS
223         DEBUG3("  tls");
224 #endif
225 #ifdef WITH_UNLANG
226         DEBUG3("  unlang");
227 #endif
228 #ifdef WITH_VMPS
229         DEBUG3("  vmps");
230 #endif
231
232         DEBUG3("Server core libs:");
233         DEBUG3("  talloc : %i.%i.*", talloc_version_major(), talloc_version_minor());
234         DEBUG3("  ssl    : %s", ssl_version());
235
236         DEBUG3("Library magic number:");
237         DEBUG3("  0x%llx", (unsigned long long) libmagic);
238
239         DEBUG3("Endianess:");
240 #if defined(LITTLE_ENDIAN)
241         DEBUG3("  little");
242 #elif defined(BIG_ENDIAN)
243         DEBUG3("  big");
244 #else
245         DEBUG3("  unknown");
246 #endif
247
248         INFO("Copyright (C) 1999-2014 The FreeRADIUS server project and contributors");
249         INFO("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A");
250         INFO("PARTICULAR PURPOSE");
251         INFO("You may redistribute copies of FreeRADIUS under the terms of the");
252         INFO("GNU General Public License");
253         INFO("For more information about these matters, see the file named COPYRIGHT");
254
255         fflush(NULL);
256 }
257