| 1 | /** |
| 2 | * @file runlevel_probe.c |
| 3 | * @brief runlevel probe |
| 4 | * @author "Daniel Kopecek" <[email protected]> |
| 5 | * @author "Tomas Heinrich" <[email protected]> |
| 6 | * |
| 7 | * 2010/06/13 [email protected] |
| 8 | * This probe is able to process a runlevel_object as defined in OVAL 5.4 and 5.5. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 14 | * All Rights Reserved. |
| 15 | * |
| 16 | * This library is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU Lesser General Public |
| 18 | * License as published by the Free Software Foundation; either |
| 19 | * version 2.1 of the License, or (at your option) any later version. |
| 20 | * |
| 21 | * This library is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | * Lesser General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU Lesser General Public |
| 27 | * License along with this library; if not, write to the Free Software |
| 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 29 | * |
| 30 | * Authors: |
| 31 | * "Daniel Kopecek" <[email protected]> |
| 32 | * "Tomas Heinrich" <[email protected]> |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * runlevel probe: |
| 37 | * |
| 38 | * runlevel_object(string service_name, |
| 39 | * string runlevel) |
| 40 | * |
| 41 | * runlevel_state(string service_name, |
| 42 | * string runlevel, |
| 43 | * bool start, |
| 44 | * bool kill) |
| 45 | */ |
| 46 | |
| 47 | #ifdef HAVE_CONFIG_H |
| 48 | #include <config.h> |
| 49 | #endif |
| 50 | |
| 51 | #undef _BSD_SOURCE |
| 52 | #define _BSD_SOURCE |
| 53 | #undef _DEFAULT_SOURCE |
| 54 | #define _DEFAULT_SOURCE |
| 55 | #include <stdio.h> |
| 56 | #include <stdbool.h> |
| 57 | #include <string.h> |
| 58 | #include <errno.h> |
| 59 | #include <assert.h> |
| 60 | #include <unistd.h> |
| 61 | #include <sys/types.h> |
| 62 | #include <sys/stat.h> |
| 63 | #include <dirent.h> |
| 64 | #include <limits.h> |
| 65 | |
| 66 | #include "_seap.h" |
| 67 | #include <probe-api.h> |
| 68 | #include <probe/probe.h> |
| 69 | #include <probe/entcmp.h> |
| 70 | #include <probe/option.h> |
| 71 | #include "common/debug_priv.h" |
| 72 | #include "runlevel_probe.h" |
| 73 | |
| 74 | #define RELEASENAME_MAX_SIZE 256 |
| 75 | #define RELEASENAME_PATTERN "CPE_NAME=\"%255s\"" |
| 76 | |
| 77 | struct runlevel_req { |
| 78 | SEXP_t *service_name_ent; |
| 79 | SEXP_t *runlevel_ent; |
| 80 | }; |
| 81 | |
| 82 | struct runlevel_rep { |
| 83 | char *service_name; |
| 84 | char *runlevel; |
| 85 | bool start; |
| 86 | bool kill; |
| 87 | struct runlevel_rep *next; |
| 88 | }; |
| 89 | |
| 90 | static int get_runlevel (struct runlevel_req *req, struct runlevel_rep **rep); |
| 91 | |
| 92 | #if defined(OS_LINUX) || defined(OS_SOLARIS) |
| 93 | static int get_runlevel_sysv (struct runlevel_req *req, struct runlevel_rep **rep, bool suse, const char *init_path, const char *rc_path) |
| 94 | { |
| 95 | const char runlevel_list[] = {'0', '1', '2', '3', '4', '5', '6'}; |
| 96 | |
| 97 | char pathbuf[PATH_MAX]; |
| 98 | DIR *init_dir, *rc_dir, *orig_dir; |
| 99 | struct dirent *init_dp, *rc_dp; |
| 100 | struct stat init_st, rc_st; |
| 101 | struct runlevel_rep *rep_lst = NULL; |
| 102 | |
| 103 | _A(req != NULL); |
| 104 | _A(rep != NULL); |
| 105 | |
| 106 | orig_dir = opendir("."); |
| 107 | if (orig_dir == NULL) { |
| 108 | dD("Can't open directory \".\": errno=%d, %s.", errno, strerror(errno)); |
| 109 | return -1; |
| 110 | } |
| 111 | init_dir = opendir(init_path); |
| 112 | if (init_dir == NULL) { |
| 113 | dD("Can't open directory \"%s\": errno=%d, %s.", |
| 114 | init_path, errno, strerror (errno)); |
| 115 | closedir(orig_dir); |
| 116 | return (-1); |
| 117 | } |
| 118 | |
| 119 | while ((init_dp = readdir(init_dir)) != NULL) { |
| 120 | char *service_name; |
| 121 | unsigned int i; |
| 122 | SEXP_t *r0; |
| 123 | |
| 124 | // Ensure that we are in the expected directory before |
| 125 | // touching relative paths |
| 126 | if (fchdir(dirfd(init_dir)) != 0) { |
| 127 | dD("Can't fchdir to \"%s\": errno=%d, %s.", |
| 128 | init_path, errno, strerror (errno)); |
| 129 | closedir(init_dir); |
| 130 | closedir(orig_dir); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | if (stat(init_dp->d_name, &init_st) != 0) { |
| 135 | dD("Can't stat file %s/%s: errno=%d, %s.", |
| 136 | init_path, init_dp->d_name, errno, strerror(errno)); |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | r0 = SEXP_string_newf("%s", init_dp->d_name); |
| 141 | if (probe_entobj_cmp(req->service_name_ent, r0) != OVAL_RESULT_TRUE) { |
| 142 | SEXP_free(r0); |
| 143 | continue; |
| 144 | } |
| 145 | SEXP_free(r0); |
| 146 | service_name = init_dp->d_name; |
| 147 | |
| 148 | for (i = 0; i < (sizeof (runlevel_list) / sizeof (runlevel_list[0])); ++i) { |
| 149 | char runlevel[2] = {'\0', '\0'}; |
| 150 | bool start, kill; |
| 151 | |
| 152 | r0 = SEXP_string_newf("%c", runlevel_list[i]); |
| 153 | if (probe_entobj_cmp(req->runlevel_ent, r0) != OVAL_RESULT_TRUE) { |
| 154 | SEXP_free(r0); |
| 155 | continue; |
| 156 | } |
| 157 | SEXP_free(r0); |
| 158 | runlevel[0] = runlevel_list[i]; |
| 159 | |
| 160 | snprintf(pathbuf, sizeof (pathbuf), rc_path, runlevel_list[i]); |
| 161 | rc_dir = opendir(pathbuf); |
| 162 | if (rc_dir == NULL) { |
| 163 | dD("Can't open directory \"%s\": errno=%d, %s.", |
| 164 | rc_path, errno, strerror (errno)); |
| 165 | continue; |
| 166 | } |
| 167 | if (chdir(pathbuf) != 0) { |
| 168 | dD("Can't fchdir to \"%s\": errno=%d, %s.", |
| 169 | rc_path, errno, strerror (errno)); |
| 170 | closedir(rc_dir); |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | // On SUSE, the presence of a symbolic link to the init.d/<service> in |
| 175 | // a runlevel directory rcx.d implies that the sevice is started on x. |
| 176 | |
| 177 | if (suse) { |
| 178 | start = false; |
| 179 | kill = true; |
| 180 | } |
| 181 | else |
| 182 | start = kill = false; |
| 183 | |
| 184 | while ((rc_dp = readdir(rc_dir)) != NULL) { |
| 185 | if (stat(rc_dp->d_name, &rc_st) != 0) { |
| 186 | dD("Can't stat file %s/%s: errno=%d, %s.", |
| 187 | rc_path, rc_dp->d_name, errno, strerror(errno)); |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | if (init_st.st_ino == rc_st.st_ino) { |
| 192 | |
| 193 | if (suse) { |
| 194 | if (rc_dp->d_name[0] == 'S') { |
| 195 | |
| 196 | start = true; |
| 197 | kill = false; |
| 198 | |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | else { |
| 203 | if (rc_dp->d_name[0] == 'S') { |
| 204 | start = true; |
| 205 | break; |
| 206 | } else if (rc_dp->d_name[0] == 'K') { |
| 207 | kill = true; |
| 208 | break; |
| 209 | } else { |
| 210 | dD("Unexpected character in filename: %c, %s/%s.", |
| 211 | rc_dp->d_name[0], pathbuf, rc_dp->d_name); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | closedir(rc_dir); |
| 217 | |
| 218 | if (rep_lst == NULL) { |
| 219 | rep_lst = *rep = malloc(sizeof (struct runlevel_rep)); |
| 220 | } else { |
| 221 | rep_lst->next = malloc(sizeof (struct runlevel_rep)); |
| 222 | rep_lst = rep_lst->next; |
| 223 | } |
| 224 | |
| 225 | rep_lst->service_name = strdup(service_name); |
| 226 | rep_lst->runlevel = strdup(runlevel); |
| 227 | rep_lst->start = start; |
| 228 | rep_lst->kill = kill; |
| 229 | rep_lst->next = NULL; |
| 230 | } |
| 231 | } |
| 232 | closedir(init_dir); |
| 233 | |
| 234 | if (fchdir(dirfd(orig_dir)) == -1) { |
| 235 | dE("Can't fchdir back to original working directory: errno=%d, %s.", errno, strerror(errno)); |
| 236 | return -1; |
| 237 | } |
| 238 | closedir(orig_dir); |
| 239 | |
| 240 | return (1); |
| 241 | } |
| 242 | |
| 243 | static int get_runlevel_redhat (struct runlevel_req *req, struct runlevel_rep **rep) |
| 244 | { |
| 245 | #if defined(OS_LINUX) |
| 246 | const char *init_path = "/etc/rc.d/init.d"; |
| 247 | #elif defined(OS_SOLARIS) |
| 248 | const char *init_path = "/etc/init.d"; |
| 249 | #endif |
| 250 | const char *rc_path = "/etc/rc%c.d"; |
| 251 | |
| 252 | bool suse = false; |
| 253 | return (get_runlevel_sysv (req, rep, suse, init_path, rc_path)); |
| 254 | } |
| 255 | |
| 256 | static int get_runlevel_debian (struct runlevel_req *req, struct runlevel_rep **rep) |
| 257 | { |
| 258 | return (-1); |
| 259 | } |
| 260 | |
| 261 | static int get_runlevel_slack (struct runlevel_req *req, struct runlevel_rep **rep) |
| 262 | { |
| 263 | return (-1); |
| 264 | } |
| 265 | |
| 266 | static int get_runlevel_gentoo (struct runlevel_req *req, struct runlevel_rep **rep) |
| 267 | { |
| 268 | return (-1); |
| 269 | } |
| 270 | |
| 271 | static int get_runlevel_arch (struct runlevel_req *req, struct runlevel_rep **rep) |
| 272 | { |
| 273 | return (-1); |
| 274 | } |
| 275 | |
| 276 | static int get_runlevel_mandriva (struct runlevel_req *req, struct runlevel_rep **rep) |
| 277 | { |
| 278 | return (-1); |
| 279 | } |
| 280 | |
| 281 | static int get_runlevel_suse (struct runlevel_req *req, struct runlevel_rep **rep) |
| 282 | { |
| 283 | const char *init_path = "/etc/init.d"; |
| 284 | const char *rc_path = "/etc/init.d/rc%c.d"; |
| 285 | |
| 286 | bool suse = true; |
| 287 | return (get_runlevel_sysv (req, rep, suse, init_path, rc_path)); |
| 288 | } |
| 289 | |
| 290 | static int get_runlevel_wrlinux (struct runlevel_req *req, struct runlevel_rep **rep) |
| 291 | { |
| 292 | return (-1); |
| 293 | } |
| 294 | |
| 295 | static int get_runlevel_common (struct runlevel_req *req, struct runlevel_rep **rep) |
| 296 | { |
| 297 | return (-1); |
| 298 | } |
| 299 | |
| 300 | #if !defined(LINUX_DISTRO) |
| 301 | # define LINUX_DISTRO generic |
| 302 | |
| 303 | /** |
| 304 | * Parse /etc/os-release and return 1 if CPE_NAME inside starts with given |
| 305 | * value in "cpe". Otherwise returns 0. |
| 306 | * |
| 307 | * Examples (on Fedora 25): |
| 308 | * - parse_os_release("cpe:/o:fedoraproject:fedora:25") returns 1 |
| 309 | * - parse_os_release("cpe:/o:fedoraproject:fedora:24") returns 0 |
| 310 | * - parse_os_release("aasdfasdfasdf") returns 0 |
| 311 | * - parse_os_release("cpe") returns 1 (!!!) |
| 312 | * - parse_os_release("cpe:/o:fedoraproject:fedora:*") returns 0 (!!!) |
| 313 | */ |
| 314 | static int parse_os_release(const char *cpe) |
| 315 | { |
| 316 | FILE *osrelease = fopen("/etc/os-release", "r"); |
| 317 | if (osrelease == NULL) |
| 318 | // we cound't match the CPE because we couldn't open the file |
| 319 | return 0; |
| 320 | |
| 321 | char releasename[RELEASENAME_MAX_SIZE]; |
| 322 | memset(releasename, 0, RELEASENAME_MAX_SIZE); |
| 323 | |
| 324 | int got = -1; |
| 325 | int c; |
| 326 | do { |
| 327 | got = fscanf(osrelease, RELEASENAME_PATTERN, releasename); |
| 328 | c = fgetc(osrelease); |
| 329 | } while (got == 0 && c != EOF); |
| 330 | |
| 331 | int ret; |
| 332 | if (got < 0) { |
| 333 | ret = 0; // 0 means we couldn't find a match |
| 334 | goto done; |
| 335 | } |
| 336 | |
| 337 | ret = strncmp(releasename, cpe, strlen(cpe)) == 0; |
| 338 | |
| 339 | done: |
| 340 | fclose(osrelease); |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | static int is_redhat (void) |
| 345 | { |
| 346 | struct stat st; |
| 347 | return (stat ("/etc/redhat-release", &st) == 0); |
| 348 | } |
| 349 | |
| 350 | static int is_debian (void) |
| 351 | { |
| 352 | struct stat st; |
| 353 | return (stat ("/etc/debian_version", &st) == 0 || |
| 354 | stat ("/etc/debian_release", &st) == 0); |
| 355 | } |
| 356 | |
| 357 | static int is_slack (void) |
| 358 | { |
| 359 | struct stat st; |
| 360 | return (stat ("/etc/slackware-release", &st) == 0); |
| 361 | } |
| 362 | |
| 363 | static int is_gentoo (void) |
| 364 | { |
| 365 | struct stat st; |
| 366 | return (stat ("/etc/gentoo-release", &st) == 0); |
| 367 | } |
| 368 | |
| 369 | static int is_arch (void) |
| 370 | { |
| 371 | struct stat st; |
| 372 | return (stat ("/etc/arch-release", &st) == 0); |
| 373 | } |
| 374 | |
| 375 | static int is_mandriva (void) |
| 376 | { |
| 377 | struct stat st; |
| 378 | return (stat ("/etc/mandriva-release", &st) == 0); |
| 379 | } |
| 380 | |
| 381 | static int is_suse (void) |
| 382 | { |
| 383 | struct stat st; |
| 384 | return (stat ("/etc/SuSE-release", &st) == 0 || |
| 385 | stat ("/etc/sles-release", &st) == 0 || |
| 386 | stat ("/etc/novell-release", &st) == 0); |
| 387 | } |
| 388 | |
| 389 | static int is_solaris (void) |
| 390 | { |
| 391 | struct stat st; |
| 392 | return (stat ("/etc/release", &st) == 0); |
| 393 | } |
| 394 | |
| 395 | static int is_oracle (void) |
| 396 | { |
| 397 | struct stat st; |
| 398 | return (stat ("/etc/oracle-release", &st) == 0); |
| 399 | } |
| 400 | |
| 401 | static int is_wrlinux(void) |
| 402 | { |
| 403 | return parse_os_release("cpe:/o:windriver:wrlinux"); |
| 404 | } |
| 405 | |
| 406 | static int is_openembedded(void) |
| 407 | { |
| 408 | return parse_os_release("cpe:/o:openembedded:nodistro"); |
| 409 | } |
| 410 | |
| 411 | static int is_poky(void) |
| 412 | { |
| 413 | return parse_os_release("cpe:/o:openembedded:poky"); |
| 414 | } |
| 415 | |
| 416 | static int is_common (void) |
| 417 | { |
| 418 | return (1); |
| 419 | } |
| 420 | |
| 421 | typedef struct { |
| 422 | int (*distrop)(void); |
| 423 | int (*get_runlevel)(struct runlevel_req *, struct runlevel_rep **); |
| 424 | } distro_tbl_t; |
| 425 | |
| 426 | const distro_tbl_t distro_tbl[] = { |
| 427 | { &is_debian, &get_runlevel_debian }, |
| 428 | { &is_oracle, &get_runlevel_redhat }, |
| 429 | { &is_redhat, &get_runlevel_redhat }, |
| 430 | { &is_slack, &get_runlevel_slack }, |
| 431 | { &is_gentoo, &get_runlevel_gentoo }, |
| 432 | { &is_arch, &get_runlevel_arch }, |
| 433 | { &is_mandriva, &get_runlevel_mandriva }, |
| 434 | { &is_suse, &get_runlevel_suse }, |
| 435 | { &is_solaris, &get_runlevel_redhat }, |
| 436 | { &is_wrlinux, &get_runlevel_wrlinux }, |
| 437 | { &is_common, &get_runlevel_common }, |
| 438 | { &is_openembedded, &get_runlevel_common }, |
| 439 | { &is_poky, &get_runlevel_common } |
| 440 | }; |
| 441 | |
| 442 | #define DISTRO_TBL_SIZE ((sizeof distro_tbl)/sizeof (distro_tbl_t)) |
| 443 | |
| 444 | static int get_runlevel_generic (struct runlevel_req *req, struct runlevel_rep **rep) |
| 445 | { |
| 446 | uint16_t i; |
| 447 | |
| 448 | _A(req != NULL); |
| 449 | _A(rep != NULL); |
| 450 | |
| 451 | for (i = 0; i < DISTRO_TBL_SIZE; ++i) |
| 452 | if (distro_tbl[i].distrop ()) |
| 453 | return distro_tbl[i].get_runlevel (req, rep); |
| 454 | |
| 455 | abort (); |
| 456 | |
| 457 | /* NOTREACHED */ |
| 458 | return (-1); |
| 459 | } |
| 460 | #endif |
| 461 | |
| 462 | #define CONCAT(a, b) a ## b |
| 463 | #define GET_RUNLEVEL(d, q, p) CONCAT(get_runlevel_, d) (q, p) |
| 464 | |
| 465 | static int get_runlevel (struct runlevel_req *req, struct runlevel_rep **rep) |
| 466 | { |
| 467 | _A(req != NULL); |
| 468 | _A(rep != NULL); |
| 469 | return GET_RUNLEVEL(LINUX_DISTRO, req, rep); |
| 470 | } |
| 471 | #elif defined(OS_FREEBSD) |
| 472 | static int get_runlevel (struct runlevel_req *req, struct runlevel_rep **rep) |
| 473 | { |
| 474 | _A(req != NULL); |
| 475 | _A(rep != NULL); |
| 476 | return (-1); |
| 477 | } |
| 478 | #else |
| 479 | # error "Sorry, your OS isn't supported." |
| 480 | #endif |
| 481 | |
| 482 | int runlevel_probe_offline_mode_supported() |
| 483 | { |
| 484 | return PROBE_OFFLINE_CHROOT; |
| 485 | } |
| 486 | |
| 487 | int runlevel_probe_main(probe_ctx *ctx, void *arg) |
| 488 | { |
| 489 | SEXP_t *object; |
| 490 | struct runlevel_req request_st; |
| 491 | struct runlevel_rep *reply_st = NULL; |
| 492 | |
| 493 | object = probe_ctx_getobject(ctx); |
| 494 | |
| 495 | request_st.service_name_ent = probe_obj_getent(object, "service_name", 1); |
| 496 | if (request_st.service_name_ent == NULL) { |
| 497 | dD("%s: element not found", "service_name"); |
| 498 | |
| 499 | return PROBE_ENOELM; |
| 500 | } |
| 501 | |
| 502 | request_st.runlevel_ent = probe_obj_getent(object, "runlevel", 1); |
| 503 | if (request_st.runlevel_ent == NULL) { |
| 504 | SEXP_free(request_st.service_name_ent); |
| 505 | dD("%s: element not found", "runlevel"); |
| 506 | |
| 507 | return PROBE_ENOELM; |
| 508 | } |
| 509 | |
| 510 | if (get_runlevel(&request_st, &reply_st) == -1) { |
| 511 | SEXP_t *msg; |
| 512 | |
| 513 | msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, "get_runlevel failed."); |
| 514 | probe_cobj_add_msg(probe_ctx_getresult(ctx), msg); |
| 515 | SEXP_free(msg); |
| 516 | probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR); |
| 517 | } else { |
| 518 | struct runlevel_rep *next_rep; |
| 519 | SEXP_t *item; |
| 520 | |
| 521 | while (reply_st != NULL) { |
| 522 | dD("get_runlevel: [0]=\"%s\", [1]=\"%s\", [2]=\"%d\", [3]=\"%d\"", |
| 523 | reply_st->service_name, reply_st->runlevel, reply_st->start, reply_st->kill); |
| 524 | |
| 525 | item = probe_item_create(OVAL_UNIX_RUNLEVEL, NULL, |
| 526 | "service_name", OVAL_DATATYPE_STRING, reply_st->service_name, |
| 527 | "runlevel", OVAL_DATATYPE_STRING, reply_st->runlevel, |
| 528 | "start", OVAL_DATATYPE_BOOLEAN, reply_st->start, |
| 529 | "kill", OVAL_DATATYPE_BOOLEAN, reply_st->kill, |
| 530 | NULL); |
| 531 | |
| 532 | probe_item_collect(ctx, item); |
| 533 | |
| 534 | next_rep = reply_st->next; |
| 535 | free(reply_st->service_name); |
| 536 | free(reply_st->runlevel); |
| 537 | free(reply_st); |
| 538 | reply_st = next_rep; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | SEXP_free(request_st.runlevel_ent); |
| 543 | SEXP_free(request_st.service_name_ent); |
| 544 | |
| 545 | return 0; |
| 546 | } |