Debian Codegraph @

src/OVAL/probes/unix/shadow_probe.c

raw symbols SCIP
1 /**
2 * @file shadow_probe.c
3 * @brief shadow probe
4 * @author "Steve Grubb" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process a shadow_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2009-2010 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 * shadow probe:
35 *
36 * username
37 * password
38 * chg_lst
39 * chg_allow
40 * chg_req
41 * exp_warn
42 * exp_inact
43 * exp_date
44 * flag
45 */
46
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif
50
51 #include <stdlib.h>
52 #include <string.h>
53 #include <stdio.h>
54 #include <errno.h>
55
56 #include "_seap.h"
57 #include "probe-api.h"
58 #include "probe/entcmp.h"
59 #include "common/debug_priv.h"
60 #include <probe/probe.h>
61 #include <probe/option.h>
62 #include "shadow_probe.h"
63
64 #ifndef HAVE_SHADOW_H
65 int shadow_probe_main(probe_ctx *ctx, void *arg)
66 {
67 SEXP_t *item_sexp;
68
69 (void)arg;
70
71 item_sexp = probe_item_create(OVAL_UNIX_SHADOW, NULL, NULL);
72 probe_item_setstatus (item_sexp, SYSCHAR_STATUS_NOT_COLLECTED);
73 probe_item_collect(ctx, item_sexp);
74
75 return 0;
76 }
77 #else
78 /* shadow.h is present */
79 #include <shadow.h>
80
81 static oval_schema_version_t over;
82
83 /* Convenience structure for the results being reported */
84 struct result_info {
85 const char *username;
86 const char *password;
87 long chg_lst;
88 long chg_allow;
89 long chg_req;
90 long exp_warn;
91 long exp_inact;
92 long exp_date;
93 unsigned long flag;
94 };
95
96 static SEXP_t *parse_enc_mth(const char *pwd)
97 {
98 char *mth_str;
99
100 switch (*pwd) {
101 case '_':
102 return SEXP_string_newf("BSDi");
103 case '$':
104 pwd++;
105 switch (*pwd) {
106 case '1':
107 mth_str = "MD5";
108 pwd++;
109 break;
110 case '2':
111 mth_str = "Blowfish";
112 pwd++;
113 if (*pwd == 'a')
114 pwd++;
115 break;
116 case '5':
117 mth_str = "SHA-256";
118 pwd++;
119 break;
120 case '6':
121 mth_str = "SHA-512";
122 pwd++;
123 break;
124 default:
125 if (strncmp(pwd, "md5", 3))
126 goto fail;
127 mth_str = "Sun MD5";
128 pwd += 3;
129 }
130
131 if (*pwd != '$')
132 goto fail;
133
134 return SEXP_string_newf("%s", mth_str);
135 default:
136 return SEXP_string_newf("DES");
137 }
138
139 fail:
140 return NULL;
141 }
142
143 static void report_finding(struct result_info *res, probe_ctx *ctx)
144 {
145 SEXP_t *item, *enc_mth;
146 SEXP_t se_chl_mem, se_cha_mem, se_chr_mem;
147 SEXP_t se_exw_mem, se_exi_mem, se_exd_mem;
148 SEXP_t se_flg_mem;
149
150 item = probe_item_create(OVAL_UNIX_SHADOW, NULL,
151 "username", OVAL_DATATYPE_STRING, res->username,
152 "password", OVAL_DATATYPE_STRING, res->password,
153 "chg_lst", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_chl_mem, res->chg_lst),
154 "chg_allow", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_cha_mem, res->chg_allow),
155 "chg_req", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_chr_mem, res->chg_req),
156 "exp_warn", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_exw_mem, res->exp_warn),
157 "exp_inact", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_exi_mem, res->exp_inact),
158 "exp_date", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_exd_mem, res->exp_date),
159 "flag", OVAL_DATATYPE_SEXP, SEXP_string_newf_r(&se_flg_mem, "%lu", res->flag),
160 NULL);
161 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) >= 0) {
162 enc_mth = parse_enc_mth(res->password);
163 if (enc_mth) {
164 probe_item_ent_add(item, "encrypt_method", NULL, enc_mth);
165 SEXP_free(enc_mth);
166 }
167 }
168
169 probe_item_collect(ctx, item);
170
171 SEXP_free_r(&se_chl_mem);
172 SEXP_free_r(&se_cha_mem);
173 SEXP_free_r(&se_chr_mem);
174 SEXP_free_r(&se_exw_mem);
175 SEXP_free_r(&se_exi_mem);
176 SEXP_free_r(&se_exd_mem);
177 SEXP_free_r(&se_flg_mem);
178 }
179
180 static void _process_struct_shadow(struct spwd *sp, SEXP_t *un_ent, probe_ctx *ctx)
181 {
182 SEXP_t *un;
183 struct result_info r;
184
185 dI("Have user: %s", sp->sp_namp);
186 un = SEXP_string_newf("%s", sp->sp_namp);
187 if (probe_entobj_cmp(un_ent, un) != OVAL_RESULT_TRUE) {
188 SEXP_free(un);
189 return;
190 }
191
192 r.username = sp->sp_namp;
193 r.password = sp->sp_pwdp;
194 r.chg_lst = sp->sp_lstchg;
195 r.chg_allow = sp->sp_min;
196 r.chg_req = sp->sp_max;
197 r.exp_warn = sp->sp_warn;
198 r.exp_inact = sp->sp_inact;
199 r.exp_date = sp->sp_expire;
200 r.flag = sp->sp_flag;
201
202 report_finding(&r, ctx);
203 SEXP_free(un);
204 }
205
206 static int read_shadow(SEXP_t *un_ent, probe_ctx *ctx)
207 {
208 struct spwd *sp;
209
210 if (ctx->offline_mode & PROBE_OFFLINE_OWN) {
211 const char *root = getenv("OSCAP_PROBE_ROOT");
212 char *shadow_file_path = oscap_path_join(root, "/etc/shadow");
213 FILE *fp = fopen(shadow_file_path, "r");
214 if (fp == NULL) {
215 free(shadow_file_path);
216 return 1;
217 }
218 while ((sp = fgetspent(fp))) {
219 _process_struct_shadow(sp, un_ent, ctx);
220 }
221 fclose(fp);
222 free(shadow_file_path);
223 } else {
224 while ((sp = getspent())) {
225 _process_struct_shadow(sp, un_ent, ctx);
226 }
227 endspent();
228 }
229
230 return 0;
231 }
232
233 int shadow_probe_offline_mode_supported()
234 {
235 return PROBE_OFFLINE_OWN;
236 }
237
238 int shadow_probe_main(probe_ctx *ctx, void *arg)
239 {
240 SEXP_t *ent, *obj;
241
242 obj = probe_ctx_getobject(ctx);
243 over = probe_obj_get_platform_schema_version(obj);
244 ent = probe_obj_getent(obj, "username", 1);
245 if (ent == NULL) {
246 return PROBE_ENOVAL;
247 }
248
249 // Now we check the file...
250 read_shadow(ent, ctx);
251 SEXP_free(ent);
252
253 return 0;
254 }
255 #endif /* HAVE_SHADOW_H */