Define and use an OS enumeration type
[strongswan.git] / src / libimcv / os_info / os_info.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "os_info.h"
17
18 #include <sys/utsname.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21
22 #include <collections/linked_list.h>
23 #include <utils/debug.h>
24
25 typedef struct private_os_info_t private_os_info_t;
26
27 ENUM(os_type_names, OS_TYPE_UNKNOWN, OS_TYPE_ANDROID,
28 "Unknown",
29 "Debian",
30 "Ubuntu",
31 "Fedora",
32 "Red Hat",
33 "CentOS",
34 "SUSE",
35 "Gentoo",
36 "Android"
37 );
38
39 ENUM(os_fwd_status_names, OS_FWD_DISABLED, OS_FWD_UNKNOWN,
40 "disabled",
41 "enabled",
42 "unknown"
43 );
44
45 /**
46 * Private data of an os_info_t object.
47 *
48 */
49 struct private_os_info_t {
50
51 /**
52 * Public os_info_t interface.
53 */
54 os_info_t public;
55
56 /**
57 * OS type
58 */
59 os_type_t type;
60
61 /**
62 * OS name
63 */
64 chunk_t name;
65
66 /**
67 * OS version
68 */
69 chunk_t version;
70
71 };
72
73 METHOD(os_info_t, get_type, os_type_t,
74 private_os_info_t *this)
75 {
76 return this->type;
77 }
78
79 METHOD(os_info_t, get_name, chunk_t,
80 private_os_info_t *this)
81 {
82 return this->name;
83 }
84
85 METHOD(os_info_t, get_numeric_version, void,
86 private_os_info_t *this, u_int32_t *major, u_int32_t *minor)
87 {
88 u_char *pos;
89
90 if (major)
91 {
92 *major = atol(this->version.ptr);
93 }
94 pos = memchr(this->version.ptr, '.', this->version.len);
95 if (minor)
96 {
97 *minor = pos ? atol(pos + 1) : 0;
98 }
99 }
100
101 METHOD(os_info_t, get_version, chunk_t,
102 private_os_info_t *this)
103 {
104 return this->version;
105 }
106
107 METHOD(os_info_t, get_fwd_status, os_fwd_status_t,
108 private_os_info_t *this)
109 {
110 const char ip_forward[] = "/proc/sys/net/ipv4/ip_forward";
111 char buf[2];
112 FILE *file;
113
114 os_fwd_status_t fwd_status = OS_FWD_UNKNOWN;
115
116 file = fopen(ip_forward, "r");
117 if (file)
118 {
119 if (fread(buf, 1, 1, file) == 1)
120 {
121 switch (buf[0])
122 {
123 case '0':
124 fwd_status = OS_FWD_DISABLED;
125 break;
126 case '1':
127 fwd_status = OS_FWD_ENABLED;
128 break;
129 default:
130 DBG1(DBG_IMC, "\"%s\" returns invalid value ", ip_forward);
131 break;
132 }
133 }
134 else
135 {
136 DBG1(DBG_IMC, "could not read from \"%s\"", ip_forward);
137 }
138 fclose(file);
139 }
140 else
141 {
142 DBG1(DBG_IMC, "failed to open \"%s\"", ip_forward);
143 }
144
145 return fwd_status;
146 }
147
148 METHOD(os_info_t, get_uptime, time_t,
149 private_os_info_t *this)
150 {
151 const char proc_uptime[] = "/proc/uptime";
152 FILE *file;
153 time_t uptime;
154
155 file = fopen(proc_uptime, "r");
156 if (!file)
157 {
158 DBG1(DBG_IMC, "failed to open \"%s\"", proc_uptime);
159 return 0;
160 }
161 if (fscanf(file, "%u", &uptime) != 1)
162 {
163 DBG1(DBG_IMC, "failed to read file \"%s\"", proc_uptime);
164 uptime = 0;
165 }
166 fclose(file);
167
168 return uptime;
169 }
170
171 METHOD(os_info_t, get_setting, chunk_t,
172 private_os_info_t *this, char *name)
173 {
174 FILE *file;
175 u_char buf[2048];
176 size_t i = 0;
177 chunk_t value;
178
179 if (!strneq(name, "/etc/", 5) && !strneq(name, "/proc/", 6) &&
180 !strneq(name, "/sys/", 5))
181 {
182 /**
183 * In order to guarantee privacy, only settings from the
184 * /etc/, /proc/ and /sys/ directories can be retrieved
185 */
186 DBG1(DBG_IMC, "not allowed to access \"%s\"", name);
187
188 return chunk_empty;
189 }
190
191 file = fopen(name, "r");
192 if (!file)
193 {
194 DBG1(DBG_IMC, "failed to open \"%s\"", name);
195
196 return chunk_empty;
197 }
198 while (i < sizeof(buf) && fread(buf + i, 1, 1, file) == 1)
199 {
200 i++;
201 }
202 fclose(file);
203
204 value = chunk_create(buf, i);
205
206 return chunk_clone(value);
207 }
208
209 typedef struct {
210 /**
211 * implements enumerator_t
212 */
213 enumerator_t public;
214
215 /**
216 * package info pipe stream
217 */
218 FILE* file;
219
220 /**
221 * line buffer
222 */
223 char line[512];
224
225 } package_enumerator_t;
226
227 /**
228 * Implementation of package_enumerator.destroy.
229 */
230 static void package_enumerator_destroy(package_enumerator_t *this)
231 {
232 pclose(this->file);
233 free(this);
234 }
235
236 /**
237 * Implementation of package_enumerator.enumerate
238 */
239 static bool package_enumerator_enumerate(package_enumerator_t *this, ...)
240 {
241 chunk_t *name, *version;
242 char *pos;
243 va_list args;
244
245 if (!fgets(this->line, sizeof(this->line), this->file))
246 {
247 return FALSE;
248 }
249 va_start(args, this);
250
251 name = va_arg(args, chunk_t*);
252 name->ptr = this->line;
253 pos = strchr(this->line, '\t');
254 if (!pos)
255 {
256 return FALSE;
257 }
258 name->len = pos++ - this->line;
259
260 version = va_arg(args, chunk_t*);
261 version->ptr = pos;
262 version->len = strlen(pos) - 1;
263
264 va_end(args);
265 return TRUE;
266 }
267
268 METHOD(os_info_t, create_package_enumerator, enumerator_t*,
269 private_os_info_t *this)
270 {
271 FILE *file;
272 package_enumerator_t *enumerator;
273
274 /* Only Debian and Ubuntu package enumeration is currently supported */
275 if (this->type != OS_TYPE_DEBIAN && this->type != OS_TYPE_UBUNTU)
276 {
277 return NULL;
278 }
279
280 /* Open a pipe stream for reading the output of the dpkg-query commmand */
281 file = popen("/usr/bin/dpkg-query --show", "r");
282 if (!file)
283 {
284 DBG1(DBG_IMC, "failed to run dpkg command");
285 return NULL;
286 }
287
288 /* Create a package enumerator instance */
289 enumerator = malloc_thing(package_enumerator_t);
290 enumerator->public.enumerate = (void*)package_enumerator_enumerate;
291 enumerator->public.destroy = (void*)package_enumerator_destroy;
292 enumerator->file = file;
293
294 return (enumerator_t*)enumerator;
295 }
296
297
298 METHOD(os_info_t, destroy, void,
299 private_os_info_t *this)
300 {
301 free(this->name.ptr);
302 free(this->version.ptr);
303 free(this);
304 }
305
306 #define RELEASE_LSB 0
307 #define RELEASE_DEBIAN 1
308
309 /**
310 * Determine Linux distribution version and hardware platform
311 */
312 static bool extract_platform_info(os_type_t *type, chunk_t *name,
313 chunk_t *version)
314 {
315 FILE *file;
316 u_char buf[BUF_LEN], *pos = buf;
317 int len = BUF_LEN - 1;
318 os_type_t os_type = OS_TYPE_UNKNOWN;
319 chunk_t os_name = chunk_empty;
320 chunk_t os_version = chunk_empty;
321 char *os_str;
322 struct utsname uninfo;
323 int i, t;
324
325 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
326 const char* releases[] = {
327 "/etc/lsb-release", "/etc/debian_version",
328 "/etc/SuSE-release", "/etc/novell-release",
329 "/etc/sles-release", "/etc/redhat-release",
330 "/etc/fedora-release", "/etc/gentoo-release",
331 "/etc/slackware-version", "/etc/annvix-release",
332 "/etc/arch-release", "/etc/arklinux-release",
333 "/etc/aurox-release", "/etc/blackcat-release",
334 "/etc/cobalt-release", "/etc/conectiva-release",
335 "/etc/debian_release", "/etc/immunix-release",
336 "/etc/lfs-release", "/etc/linuxppc-release",
337 "/etc/mandrake-release", "/etc/mandriva-release",
338 "/etc/mandrakelinux-release", "/etc/mklinux-release",
339 "/etc/pld-release", "/etc/redhat_version",
340 "/etc/slackware-release", "/etc/e-smith-release",
341 "/etc/release", "/etc/sun-release",
342 "/etc/tinysofa-release", "/etc/turbolinux-release",
343 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
344 "/etc/va-release", "/etc/yellowdog-release"
345 };
346
347 const char lsb_distrib_id[] = "DISTRIB_ID=";
348 const char lsb_distrib_release[] = "DISTRIB_RELEASE=";
349
350 for (i = 0; i < countof(releases); i++)
351 {
352 file = fopen(releases[i], "r");
353 if (!file)
354 {
355 continue;
356 }
357
358 /* read release file into buffer */
359 fseek(file, 0, SEEK_END);
360 len = min(ftell(file), len);
361 rewind(file);
362 buf[len] = '\0';
363 if (fread(buf, 1, len, file) != len)
364 {
365 DBG1(DBG_IMC, "failed to read file \"%s\"", releases[i]);
366 fclose(file);
367 return FALSE;
368 }
369 fclose(file);
370
371 DBG1(DBG_IMC, "processing \"%s\" file", releases[i]);
372
373 switch (i)
374 {
375 case RELEASE_LSB:
376 {
377 /* Determine Distribution ID */
378 pos = strstr(buf, lsb_distrib_id);
379 if (!pos)
380 {
381 DBG1(DBG_IMC, "failed to find begin of DISTRIB_ID field");
382 return FALSE;
383 }
384 pos += strlen(lsb_distrib_id);
385
386 os_name.ptr = pos;
387
388 pos = strchr(pos, '\n');
389 if (!pos)
390 {
391 DBG1(DBG_IMC, "failed to find end of DISTRIB_ID field");
392 return FALSE;
393 }
394 os_name.len = pos - os_name.ptr;
395
396 /* Determine Distribution Release */
397 pos = strstr(buf, lsb_distrib_release);
398 if (!pos)
399 {
400 DBG1(DBG_IMC, "failed to find begin of DISTRIB_RELEASE field");
401 return FALSE;
402 }
403 pos += strlen(lsb_distrib_release);
404
405 os_version.ptr = pos;
406
407 pos = strchr(pos, '\n');
408 if (!pos)
409 {
410 DBG1(DBG_IMC, "failed to find end of DISTRIB_RELEASE field");
411 return FALSE;
412 }
413 os_version.len = pos - os_version.ptr;
414
415 break;
416 }
417 case RELEASE_DEBIAN:
418 {
419 os_type = OS_TYPE_DEBIAN;
420
421 os_version.ptr = buf;
422 pos = strchr(buf, '\n');
423 if (!pos)
424 {
425 DBG1(DBG_PTS, "failed to find end of release string");
426 return FALSE;
427 }
428
429 os_version.len = pos - os_version.ptr;
430
431 break;
432 }
433 default:
434 {
435 const char str_release[] = " release ";
436
437 os_name.ptr = buf;
438
439 pos = strstr(buf, str_release);
440 if (!pos)
441 {
442 DBG1(DBG_IMC, "failed to find release keyword");
443 return FALSE;
444 }
445
446 os_name.len = pos - os_name.ptr;
447
448 pos += strlen(str_release);
449 os_version.ptr = pos;
450
451 pos = strchr(pos, '\n');
452 if (!pos)
453 {
454 DBG1(DBG_IMC, "failed to find end of release string");
455 return FALSE;
456 }
457
458 os_version.len = pos - os_version.ptr;
459
460 break;
461 }
462 }
463 break;
464 }
465
466 if (!os_name.ptr)
467 {
468 DBG1(DBG_IMC, "no distribution release file found");
469 return FALSE;
470 }
471
472 if (uname(&uninfo) < 0)
473 {
474 DBG1(DBG_IMC, "could not retrieve machine architecture");
475 return FALSE;
476 }
477
478 /* Try to find a matching OS type */
479 if (os_type == OS_TYPE_UNKNOWN)
480 {
481 for (t = OS_TYPE_DEBIAN; t <= OS_TYPE_GENTOO; t++)
482 {
483 os_str = enum_to_name(os_type_names, t);
484 if (memeq(os_name.ptr, os_str, min(os_name.len, strlen(os_str))))
485 {
486 os_type = t;
487 os_name = chunk_create(os_str, strlen(os_str));
488 break;
489 }
490 }
491 }
492 else
493 {
494 os_str = enum_to_name(os_type_names, os_type);
495 os_name = chunk_create(os_str, strlen(os_str));
496 }
497
498 /* copy OS type */
499 *type = os_type;
500
501 /* copy OS name */
502 *name = chunk_clone(os_name);
503
504 /* copy OS version and machine architecture */
505 *version = chunk_alloc(os_version.len + 1 + strlen(uninfo.machine));
506 pos = version->ptr;
507 memcpy(pos, os_version.ptr, os_version.len);
508 pos += os_version.len;
509 *pos++ = ' ';
510 memcpy(pos, uninfo.machine, strlen(uninfo.machine));
511
512 return TRUE;
513 }
514
515 /**
516 * See header
517 */
518 os_info_t *os_info_create(void)
519 {
520 private_os_info_t *this;
521 chunk_t name, version;
522 os_type_t type;
523
524 /* As an option OS name and OS version can be configured manually */
525 name.ptr = lib->settings->get_str(lib->settings,
526 "libimcv.os_info.name", NULL);
527 version.ptr = lib->settings->get_str(lib->settings,
528 "libimcv.os_info.version", NULL);
529 if (name.ptr && version.ptr)
530 {
531 name.len = strlen(name.ptr);
532 name = chunk_clone(name);
533
534 version.len = strlen(version.ptr);
535 version = chunk_clone(version);
536 }
537 else
538 {
539 if (!extract_platform_info(&type, &name, &version))
540 {
541 return NULL;
542 }
543 }
544 DBG1(DBG_IMC, "operating system name is '%.*s'",
545 name.len, name.ptr);
546 DBG1(DBG_IMC, "operating system version is '%.*s'",
547 version.len, version.ptr);
548
549 INIT(this,
550 .public = {
551 .get_type = _get_type,
552 .get_name = _get_name,
553 .get_numeric_version = _get_numeric_version,
554 .get_version = _get_version,
555 .get_fwd_status = _get_fwd_status,
556 .get_uptime = _get_uptime,
557 .get_setting = _get_setting,
558 .create_package_enumerator = _create_package_enumerator,
559 .destroy = _destroy,
560 },
561 .type = type,
562 .name = name,
563 .version = version,
564 );
565
566 return &this->public;
567 }