CID #1198853 *sigh*
[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         /* 2 (%s) + 1 (.) + 2 (%i) + 1 (.) + 2 (%i) + 1 (c) + 1 (-) + 2 (%i) + \0 */
72         static char buffer[13];
73         char *p = buffer;
74
75         p += sprintf(p, "%i.%i.%i",
76                      (int) ((0xff0000000 & v) >> 28),
77                      (int) ((0x00ff00000 & v) >> 20),
78                      (int) ((0x0000ff000 & v) >> 12));
79
80         if ((0x000000ff0 & v) >> 4) {
81                 *p++ =  (char) (0x60 + ((0x000000ff0 & v) >> 4));
82         }
83
84         sprintf(p, "-%i", (int) (0x00000000f & v));
85
86         return buffer;
87 }
88
89 /** Convert two openssl version numbers into a range string
90  *
91  * @note Not thread safe.
92  *
93  * @param low version to convert.
94  * @param high version to convert.
95  * @return pointer to a static buffer containing the version range string.
96  */
97 char const *ssl_version_range(uint64_t low, uint64_t high)
98 {
99         /* 12 (version) + 3 ( - ) + 12 (version) */
100         static char buffer[28];
101         char *p = buffer;
102
103         p += strlcpy(p, ssl_version_by_num(low), sizeof(buffer));
104         p += strlcpy(p, " - ", sizeof(buffer) - (p - buffer));
105         strlcpy(p, ssl_version_by_num(high), sizeof(buffer) - (p - buffer));
106
107         return buffer;
108 }
109
110 /** Print the current linked version of Openssl
111  *
112  * Print the currently linked version of the OpenSSL library.
113  *
114  * @note Not thread safe.
115  * @return pointer to a static buffer containing libssl version information.
116  */
117 char const *ssl_version(void)
118 {
119         static char buffer[256];
120
121         uint64_t v = (uint64_t) SSLeay();
122
123         snprintf(buffer, sizeof(buffer), "%s 0x%.9" PRIx64 " (%s)",
124                  SSLeay_version(SSLEAY_VERSION),                /* Not all builds include a useful version number */
125                  v,
126                  ssl_version_by_num((uint64_t) v));
127
128         return buffer;
129 }
130 #  else
131 int ssl_check_consistency(void) {
132         return 0;
133 }
134
135 char const *ssl_version()
136 {
137         return "not linked";
138 }
139 #endif /* ifdef HAVE_OPENSSL_CRYPTO_H */
140
141
142 /** Check if the application linking to the library has the correct magic number
143  *
144  * @param magic number as defined by RADIUSD_MAGIC_NUMBER
145  * @returns 0 on success, -1 on prefix mismatch, -2 on version mismatch -3 on commit mismatch.
146  */
147 int rad_check_lib_magic(uint64_t magic)
148 {
149         if (MAGIC_PREFIX(magic) != MAGIC_PREFIX(libmagic)) {
150                 ERROR("Application and libfreeradius-server magic number (prefix) mismatch."
151                       "  application: %x library: %x",
152                       MAGIC_PREFIX(magic), MAGIC_PREFIX(libmagic));
153                 return -1;
154         }
155
156         if (MAGIC_VERSION(magic) != MAGIC_VERSION(libmagic)) {
157                 ERROR("Application and libfreeradius-server magic number (version) mismatch."
158                       "  application: %lx library: %lx",
159                       (unsigned long) MAGIC_VERSION(magic), (unsigned long) MAGIC_VERSION(libmagic));
160                 return -2;
161         }
162
163         if (MAGIC_COMMIT(magic) != MAGIC_COMMIT(libmagic)) {
164                 ERROR("Application and libfreeradius-server magic number (commit) mismatch."
165                       "  application: %lx library: %lx",
166                       (unsigned long) MAGIC_COMMIT(magic), (unsigned long) MAGIC_COMMIT(libmagic));
167                 return -3;
168         }
169
170         return 0;
171 }
172
173 /*
174  *      Display the revision number for this program
175  */
176 void version(void)
177 {
178         INFO("%s: %s", progname, radiusd_version);
179
180         DEBUG3("Server was built with: ");
181
182 #ifdef WITH_ACCOUNTING
183         DEBUG3("  accounting");
184 #endif
185         DEBUG3("  authentication"); /* always enabled */
186
187 #ifdef WITH_ASCEND_BINARY
188         DEBUG3("  ascend binary attributes");
189 #endif
190 #ifdef WITH_COA
191         DEBUG3("  coa");
192 #endif
193 #ifdef WITH_COMMAND_SOCKET
194         DEBUG3("  control-socket");
195 #endif
196 #ifdef WITH_DETAIL
197         DEBUG3("  detail");
198 #endif
199 #ifdef WITH_DHCP
200         DEBUG3("  dhcp");
201 #endif
202 #ifdef WITH_DYNAMIC_CLIENTS
203         DEBUG3("  dynamic clients");
204 #endif
205 #ifdef OSFC2
206         DEBUG3("  OSFC2");
207 #endif
208 #ifdef WITH_PROXY
209         DEBUG3("  proxy");
210 #endif
211 #ifdef HAVE_PCREPOSIX_H
212         DEBUG3("  regex-pcre");
213 #else
214 #ifdef HAVE_REGEX_H
215         DEBUG3("  regex-posix");
216 #endif
217 #endif
218
219 #ifdef WITH_SESSION_MGMT
220         DEBUG3("  session-management");
221 #endif
222 #ifdef WITH_STATS
223         DEBUG3("  stats");
224 #endif
225 #ifdef WITH_TCP
226         DEBUG3("  tcp");
227 #endif
228 #ifdef WITH_THREADS
229         DEBUG3("  threads");
230 #endif
231 #ifdef WITH_TLS
232         DEBUG3("  tls");
233 #endif
234 #ifdef WITH_UNLANG
235         DEBUG3("  unlang");
236 #endif
237 #ifdef WITH_VMPS
238         DEBUG3("  vmps");
239 #endif
240
241         DEBUG3("Server core libs:");
242         DEBUG3("  talloc : %i.%i.*", talloc_version_major(), talloc_version_minor());
243         DEBUG3("  ssl    : %s", ssl_version());
244
245         DEBUG3("Library magic number:");
246         DEBUG3("  0x%llx", (unsigned long long) libmagic);
247
248         DEBUG3("Endianess:");
249 #if defined(LITTLE_ENDIAN)
250         DEBUG3("  little");
251 #elif defined(BIG_ENDIAN)
252         DEBUG3("  big");
253 #else
254         DEBUG3("  unknown");
255 #endif
256
257         INFO("Copyright (C) 1999-2014 The FreeRADIUS server project and contributors");
258         INFO("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A");
259         INFO("PARTICULAR PURPOSE");
260         INFO("You may redistribute copies of FreeRADIUS under the terms of the");
261         INFO("GNU General Public License");
262         INFO("For more information about these matters, see the file named COPYRIGHT");
263
264         fflush(NULL);
265 }
266