Debian Codegraph @

src/OVAL/probes/unix/password_probe.c

raw symbols SCIP
1 /**
2 * @file password_probe.c
3 * @brief password probe
4 * @author "Steve Grubb" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process a password_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
13 * All Rights Reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * Authors:
30 * Steve Grubb <[email protected]>
31 */
32
33 /*
34 * password probe:
35 *
36 * username
37 * password
38 * user_id
39 * group_id
40 * gcos
41 * home_dir
42 * login_shell
43 */
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <pwd.h>
53 #include <paths.h>
54 #if defined(OS_APPLE)
55 #include <utmp.h>
56 #elif defined(OS_FREEBSD)
57 #include <utmpx.h>
58 #else
59 #include <lastlog.h>
60 #endif
61
62 #include "_seap.h"
63 #include "probe-api.h"
64 #include "probe/entcmp.h"
65 #include "common/debug_priv.h"
66 #include <probe/probe.h>
67 #include <probe/option.h>
68 #include "password_probe.h"
69
70 /* Convenience structure for the results being reported */
71 struct result_info {
72 const char *username;
73 const char *password;
74 uid_t user_id;
75 gid_t group_id;
76 const char *gcos;
77 const char *home_dir;
78 const char *login_shell;
79 int64_t last_login;
80 };
81
82 static void report_finding(struct result_info *res, probe_ctx *ctx, oval_schema_version_t over)
83 {
84 SEXP_t *item;
85
86 item = probe_item_create(OVAL_UNIX_PASSWORD, NULL,
87 "username", OVAL_DATATYPE_STRING, res->username,
88 "password", OVAL_DATATYPE_STRING, res->password,
89 "user_id", OVAL_DATATYPE_INTEGER, (int64_t)res->user_id,
90 "group_id", OVAL_DATATYPE_INTEGER, (int64_t)res->group_id,
91 "gcos", OVAL_DATATYPE_STRING, res->gcos,
92 "home_dir", OVAL_DATATYPE_STRING, res->home_dir,
93 "login_shell", OVAL_DATATYPE_STRING, res->login_shell,
94 NULL);
95
96 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.10)) >= 0) {
97 SEXP_t last_login;
98 SEXP_number_newi_64_r(&last_login, res->last_login);
99 probe_item_ent_add(item, "last_login", NULL, &last_login);
100 SEXP_free_r(&last_login);
101 }
102
103 probe_item_collect(ctx, item);
104 }
105
106 #if defined(OS_FREEBSD)
107 static time_t get_last_login(char *username) {
108 struct utmpx *ut;
109 time_t t = 0;
110
111 /* Iterate over the entries of the utx.log file */
112 while ((ut = getutxent()) != NULL) {
113 if (strcmp(username,ut->ut_user) == 0) {
114 t = ut->ut_tv.tv_sec;
115 break;
116 }
117 }
118
119 endutxent();
120
121 return t;
122 }
123
124 static void _freebsd_process_struct_passwd(struct passwd *pw, SEXP_t *un_ent, probe_ctx *ctx, oval_schema_version_t over)
125 {
126 SEXP_t *un;
127 struct result_info r;
128
129 dI("Have user: %s", pw->pw_name);
130 un = SEXP_string_newf("%s", pw->pw_name);
131 if (probe_entobj_cmp(un_ent, un) != OVAL_RESULT_TRUE) {
132 SEXP_free(un);
133 return;
134 }
135
136 r.username = pw->pw_name;
137 r.password = pw->pw_passwd;
138 r.user_id = pw->pw_uid;
139 r.group_id = pw->pw_gid;
140 r.gcos = pw->pw_gecos;
141 r.home_dir = pw->pw_dir;
142 r.login_shell = pw->pw_shell;
143 r.last_login = -1;
144
145 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.10)) >= 0) {
146 r.last_login = get_last_login(pw->pw_name);
147 }
148
149 report_finding(&r, ctx, over);
150 SEXP_free(un);
151 }
152
153 static int read_password(SEXP_t *un_ent, probe_ctx *ctx, oval_schema_version_t over)
154 {
155 struct passwd *pw;
156
157 while ((pw = getpwent())) {
158 _freebsd_process_struct_passwd(pw, un_ent, ctx, over);
159 }
160
161 endpwent();
162 return 0;
163 }
164
165 int password_probe_offline_mode_supported()
166 {
167 return PROBE_OFFLINE_NONE;
168 }
169
170 #else
171 static void _process_struct_passwd(struct passwd *pw, const char *lastlog, SEXP_t *un_ent, probe_ctx *ctx, oval_schema_version_t over)
172 {
173 SEXP_t *un;
174 struct result_info r;
175
176 dI("Have user: %s", pw->pw_name);
177 un = SEXP_string_newf("%s", pw->pw_name);
178 if (probe_entobj_cmp(un_ent, un) != OVAL_RESULT_TRUE) {
179 SEXP_free(un);
180 return;
181 }
182
183 r.username = pw->pw_name;
184 r.password = pw->pw_passwd;
185 r.user_id = pw->pw_uid;
186 r.group_id = pw->pw_gid;
187 r.gcos = pw->pw_gecos;
188 r.home_dir = pw->pw_dir;
189 r.login_shell = pw->pw_shell;
190 r.last_login = -1;
191
192 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.10)) >= 0) {
193 FILE *ll_fp = fopen(lastlog, "r");
194
195 if (ll_fp != NULL) {
196 struct lastlog ll;
197
198 if (fseeko(ll_fp, (off_t)pw->pw_uid * sizeof(ll), SEEK_SET) == 0)
199 if (fread((char *)&ll, sizeof(ll), 1, ll_fp) == 1)
200 r.last_login = (int64_t)ll.ll_time;
201 fclose(ll_fp);
202 }
203 }
204
205 report_finding(&r, ctx, over);
206 SEXP_free(un);
207 }
208
209 static int read_password(SEXP_t *un_ent, probe_ctx *ctx, oval_schema_version_t over)
210 {
211 struct passwd *pw;
212
213 if (ctx->offline_mode & PROBE_OFFLINE_OWN) {
214 const char *root = getenv("OSCAP_PROBE_ROOT");
215 if (root == NULL)
216 return 1;
217 char *passwd_file_path = oscap_path_join(root, "/etc/passwd");
218 char *lastlog_file_path = oscap_path_join(root, _PATH_LASTLOG);
219 FILE *fp = fopen(passwd_file_path, "r");
220 if (fp == NULL) {
221 free(passwd_file_path);
222 free(lastlog_file_path);
223 return 1;
224 }
225 while ((pw = fgetpwent(fp))) {
226 _process_struct_passwd(pw, lastlog_file_path, un_ent, ctx, over);
227 }
228 fclose(fp);
229 free(passwd_file_path);
230 free(lastlog_file_path);
231 } else {
232 while ((pw = getpwent())) {
233 _process_struct_passwd(pw, _PATH_LASTLOG, un_ent, ctx, over);
234 }
235 endpwent();
236 }
237
238 return 0;
239 }
240
241 int password_probe_offline_mode_supported()
242 {
243 return PROBE_OFFLINE_OWN;
244 }
245 #endif
246
247 int password_probe_main(probe_ctx *ctx, void *arg)
248 {
249 SEXP_t *ent, *obj;
250 oval_schema_version_t over;
251
252 obj = probe_ctx_getobject(ctx);
253
254 if (obj == NULL)
255 return PROBE_ENOOBJ;
256
257 over = probe_obj_get_platform_schema_version(obj);
258 ent = probe_obj_getent(obj, "username", 1);
259
260 if (ent == NULL) {
261 return PROBE_ENOVAL;
262 }
263
264 // Now we check the file...
265 if (read_password(ent, ctx, over) != 0) {
266 SEXP_free(ent);
267 return PROBE_EINVAL;
268 }
269 SEXP_free(ent);
270
271 return 0;
272 }