Fix PCRE checks on FreeBSD
[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 built and linked versions of OpenSSL match
39  *
40  * OpenSSL version number consists of:
41  * MMNNFFPPS: major minor fix patch status
42  *
43  * Where status >= 0 && < 10 means beta, and status 10 means release.
44  *
45  * Startup check for whether the linked version of OpenSSL matches the
46  * version the server was built against.
47  *
48  * @return 0 if ok, else -1
49  */
50 int ssl_check_consistency(void)
51 {
52         long ssl_linked;
53
54         ssl_linked = SSLeay();
55
56         /*
57          *      Status mismatch always triggers error.
58          */
59         if ((ssl_linked & 0x00000000f) != (ssl_built & 0x00000000f)) {
60         mismatch:
61                 ERROR("libssl version mismatch.  built: %lx linked: %lx",
62                        (unsigned long) ssl_built,
63                        (unsigned long) ssl_linked);
64
65                 return -1;
66         }
67
68         /*
69          *      Use the OpenSSH approach and relax fix checks after version
70          *      1.0.0 and only allow moving backwards within a patch
71          *      series.
72          */
73         if (ssl_built & 0xff) {
74                 if ((ssl_built & 0xffff) != (ssl_linked & 0xffff) ||
75                     (ssl_built & 0x0000ff) > (ssl_linked & 0x0000ff)) goto mismatch;
76         /*
77          *      Before 1.0.0 we require the same major minor and fix version
78          *      and ignore the patch number.
79          */
80         } else if ((ssl_built & 0xffffff) != (ssl_linked & 0xffffff)) goto mismatch;
81
82         return 0;
83 }
84
85 /** Convert a version number to a text string
86  *
87  * @note Not thread safe.
88  *
89  * @param v version to convert.
90  * @return pointer to a static buffer containing the version string.
91  */
92 char const *ssl_version_by_num(uint64_t v)
93 {
94         /* 2 (%s) + 1 (.) + 2 (%i) + 1 (.) + 2 (%i) + 1 (c) + 1 (-) + 2 (%i) + \0 */
95         static char buffer[13];
96         char *p = buffer;
97
98         p += sprintf(p, "%i.%i.%i",
99                      (int) ((0xff0000000 & v) >> 28),
100                      (int) ((0x00ff00000 & v) >> 20),
101                      (int) ((0x0000ff000 & v) >> 12));
102
103         if ((0x000000ff0 & v) >> 4) {
104                 *p++ =  (char) (0x60 + ((0x000000ff0 & v) >> 4));
105         }
106
107         sprintf(p, "-%i", (int) (0x00000000f & v));
108
109         return buffer;
110 }
111
112 /** Convert two openssl version numbers into a range string
113  *
114  * @note Not thread safe.
115  *
116  * @param low version to convert.
117  * @param high version to convert.
118  * @return pointer to a static buffer containing the version range string.
119  */
120 char const *ssl_version_range(uint64_t low, uint64_t high)
121 {
122         /* 12 (version) + 3 ( - ) + 12 (version) */
123         static char buffer[28];
124         char *p = buffer;
125
126         p += strlcpy(p, ssl_version_by_num(low), sizeof(buffer));
127         p += strlcpy(p, " - ", sizeof(buffer) - (p - buffer));
128         strlcpy(p, ssl_version_by_num(high), sizeof(buffer) - (p - buffer));
129
130         return buffer;
131 }
132
133 /** Print the current linked version of Openssl
134  *
135  * Print the currently linked version of the OpenSSL library.
136  *
137  * @note Not thread safe.
138  * @return pointer to a static buffer containing libssl version information.
139  */
140 char const *ssl_version(void)
141 {
142         static char buffer[256];
143
144         uint64_t v = (uint64_t) SSLeay();
145
146         snprintf(buffer, sizeof(buffer), "%s 0x%.9" PRIx64 " (%s)",
147                  SSLeay_version(SSLEAY_VERSION),                /* Not all builds include a useful version number */
148                  v,
149                  ssl_version_by_num((uint64_t) v));
150
151         return buffer;
152 }
153 #  else
154 int ssl_check_consistency(void) {
155         return 0;
156 }
157
158 char const *ssl_version()
159 {
160         return "not linked";
161 }
162 #endif /* ifdef HAVE_OPENSSL_CRYPTO_H */
163
164
165 /** Check if the application linking to the library has the correct magic number
166  *
167  * @param magic number as defined by RADIUSD_MAGIC_NUMBER
168  * @returns 0 on success, -1 on prefix mismatch, -2 on version mismatch -3 on commit mismatch.
169  */
170 int rad_check_lib_magic(uint64_t magic)
171 {
172         if (MAGIC_PREFIX(magic) != MAGIC_PREFIX(libmagic)) {
173                 ERROR("Application and libfreeradius-server magic number (prefix) mismatch."
174                       "  application: %x library: %x",
175                       MAGIC_PREFIX(magic), MAGIC_PREFIX(libmagic));
176                 return -1;
177         }
178
179         if (MAGIC_VERSION(magic) != MAGIC_VERSION(libmagic)) {
180                 ERROR("Application and libfreeradius-server magic number (version) mismatch."
181                       "  application: %lx library: %lx",
182                       (unsigned long) MAGIC_VERSION(magic), (unsigned long) MAGIC_VERSION(libmagic));
183                 return -2;
184         }
185
186         if (MAGIC_COMMIT(magic) != MAGIC_COMMIT(libmagic)) {
187                 ERROR("Application and libfreeradius-server magic number (commit) mismatch."
188                       "  application: %lx library: %lx",
189                       (unsigned long) MAGIC_COMMIT(magic), (unsigned long) MAGIC_COMMIT(libmagic));
190                 return -3;
191         }
192
193         return 0;
194 }
195
196 /*
197  *      Display the revision number for this program
198  */
199 void version(void)
200 {
201         INFO("%s: %s", progname, radiusd_version);
202
203         DEBUG3("Server was built with: ");
204
205 #ifdef WITH_ACCOUNTING
206         DEBUG3("  accounting");
207 #endif
208         DEBUG3("  authentication"); /* always enabled */
209
210 #ifdef WITH_ASCEND_BINARY
211         DEBUG3("  ascend binary attributes");
212 #endif
213 #ifdef WITH_COA
214         DEBUG3("  coa");
215 #endif
216 #ifdef WITH_COMMAND_SOCKET
217         DEBUG3("  control-socket");
218 #endif
219 #ifdef WITH_DETAIL
220         DEBUG3("  detail");
221 #endif
222 #ifdef WITH_DHCP
223         DEBUG3("  dhcp");
224 #endif
225 #ifdef WITH_DYNAMIC_CLIENTS
226         DEBUG3("  dynamic clients");
227 #endif
228 #ifdef OSFC2
229         DEBUG3("  OSFC2");
230 #endif
231 #ifdef WITH_PROXY
232         DEBUG3("  proxy");
233 #endif
234 #ifdef HAVE_PCRE
235         DEBUG3("  regex-pcre");
236 #else
237 #ifdef HAVE_REGEX
238         DEBUG3("  regex-posix");
239 #endif
240 #endif
241
242 #ifdef WITH_SESSION_MGMT
243         DEBUG3("  session-management");
244 #endif
245 #ifdef WITH_STATS
246         DEBUG3("  stats");
247 #endif
248 #ifdef WITH_TCP
249         DEBUG3("  tcp");
250 #endif
251 #ifdef WITH_THREADS
252         DEBUG3("  threads");
253 #endif
254 #ifdef WITH_TLS
255         DEBUG3("  tls");
256 #endif
257 #ifdef WITH_UNLANG
258         DEBUG3("  unlang");
259 #endif
260 #ifdef WITH_VMPS
261         DEBUG3("  vmps");
262 #endif
263 #ifndef NDEBUG
264         DEBUG3("  developer");
265 #endif
266
267         DEBUG3("Server core libs:");
268         DEBUG3("  talloc : %i.%i.*", talloc_version_major(), talloc_version_minor());
269         DEBUG3("  ssl    : %s", ssl_version());
270
271         DEBUG3("Library magic number:");
272         DEBUG3("  0x%llx", (unsigned long long) libmagic);
273
274         DEBUG3("Endianess:");
275 #if defined(LITTLE_ENDIAN)
276         DEBUG3("  little");
277 #elif defined(BIG_ENDIAN)
278         DEBUG3("  big");
279 #else
280         DEBUG3("  unknown");
281 #endif
282
283         INFO("Copyright (C) 1999-2014 The FreeRADIUS server project and contributors");
284         INFO("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A");
285         INFO("PARTICULAR PURPOSE");
286         INFO("You may redistribute copies of FreeRADIUS under the terms of the");
287         INFO("GNU General Public License");
288         INFO("For more information about these matters, see the file named COPYRIGHT");
289
290         fflush(NULL);
291 }
292