Debian Codegraph @

src/OVAL/probes/unix/symlink_probe.c

raw symbols SCIP
1 /**
2 * @file symlink_probe.c
3 * @brief implementation of the symlink_object
4 * @author "Jan Černý" <[email protected]>
5 */
6
7 /*
8 * Copyright 2015 Red Hat Inc., Durham, North Carolina.
9 * All Rights Reserved.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Authors:
26 * Jan Černý <[email protected]>
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <probe-api.h>
34 #include "common/debug_priv.h"
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdlib.h>
42 #include "oscap_helpers.h"
43
44 #include <probe/probe.h>
45 #include <probe/option.h>
46 #include "symlink_probe.h"
47
48 static int collect_symlink(SEXP_t *ent, probe_ctx *ctx)
49 {
50 SEXP_t *ent_val, *item_sexp, *msg;
51 struct stat sb;
52 char *linkname;
53 char resolved_name[PATH_MAX];
54
55 ent_val = probe_ent_getval(ent);
56 char *pathname = SEXP_string_cstr(ent_val);
57 SEXP_free(ent_val);
58 if (pathname == NULL) {
59 return PROBE_EINVAL;
60 }
61 if (probe_path_is_blocked(pathname, ctx->blocked_paths)) {
62 free(pathname);
63 return 0;
64 }
65
66 if (lstat(pathname, &sb) == -1) {
67 if (errno == ENOENT) {
68 /* File does not exist.
69 * Resulting item should have a status of "does not exist". */
70 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_INFO,
71 "File '%s' does not exist.", pathname);
72 } else {
73 /* error */
74 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, strerror(errno));
75 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
76 }
77 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
78 SEXP_free(msg);
79 free(pathname);
80 return 0;
81 }
82
83 if (!S_ISLNK(sb.st_mode)) {
84 /* File is not a symlink.
85 * Resulting item should have a status "does not exist". */
86 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_INFO,
87 "File '%s' is not a symlink.", pathname);
88 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
89 SEXP_free(msg);
90 free(pathname);
91 return 0;
92 }
93
94 linkname = oscap_realpath(pathname, resolved_name);
95 if (linkname == NULL) {
96 if (errno == ENOENT) {
97 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
98 "Link target of symlink '%s' does not exist.", pathname);
99 } else if (errno == ELOOP) {
100 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
101 "Link '%s' is a circular link.", pathname);
102 } else {
103 msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_ERROR, strerror(errno));
104 }
105 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
106 SEXP_free(msg);
107 probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
108 free(pathname);
109 return 0;
110 }
111
112
113 item_sexp = probe_item_create(OVAL_UNIX_SYMLINK, NULL,
114 "filepath", OVAL_DATATYPE_STRING, pathname,
115 "canonical_path", OVAL_DATATYPE_STRING, linkname,
116 NULL);
117 probe_item_collect(ctx, item_sexp);
118
119 free(pathname);
120 return 0;
121 }
122
123 int symlink_probe_offline_mode_supported()
124 {
125 return PROBE_OFFLINE_CHROOT;
126 }
127
128 int symlink_probe_main(probe_ctx *ctx, void *probe_arg)
129 {
130 int err;
131 SEXP_t *entity, *probe_in;
132 oval_schema_version_t oval_version;
133
134 probe_in = probe_ctx_getobject(ctx);
135
136 oval_version = probe_obj_get_platform_schema_version(probe_in);
137 if (oval_schema_version_cmp(oval_version, OVAL_SCHEMA_VERSION(5.11)) < 0) {
138 // OVAL 5.10 and less
139 return PROBE_EOPNOTSUPP;
140 }
141
142 entity = probe_obj_getent(probe_in, "filepath", 1);
143
144 err = collect_symlink(entity, ctx);
145
146 SEXP_free(entity);
147 return err;
148 }