-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathshadow_probe.c
More file actions
260 lines (227 loc) · 6.28 KB
/
shadow_probe.c
File metadata and controls
260 lines (227 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* @file shadow_probe.c
* @brief shadow probe
* @author "Steve Grubb" <sgrubb@redhat.com>
*
* 2010/06/13 dkopecek@redhat.com
* This probe is able to process a shadow_object as defined in OVAL 5.4 and 5.5.
*
*/
/*
* Copyright 2009-2010 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Steve Grubb <sgrubb@redhat.com>
*/
/*
* shadow probe:
*
* username
* password
* chg_lst
* chg_allow
* chg_req
* exp_warn
* exp_inact
* exp_date
* flag
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "_seap.h"
#include "probe-api.h"
#include "probe/entcmp.h"
#include "common/debug_priv.h"
#include <probe/probe.h>
#include <probe/option.h>
#include "shadow_probe.h"
#ifndef HAVE_SHADOW_H
int shadow_probe_offline_mode_supported()
{
return PROBE_OFFLINE_NONE;
}
int shadow_probe_main(probe_ctx *ctx, void *arg)
{
SEXP_t *item_sexp;
(void)arg;
item_sexp = probe_item_create(OVAL_UNIX_SHADOW, NULL, NULL);
probe_item_setstatus (item_sexp, SYSCHAR_STATUS_NOT_COLLECTED);
probe_item_collect(ctx, item_sexp);
return 0;
}
#else
/* shadow.h is present */
#include <shadow.h>
static oval_schema_version_t over;
/* Convenience structure for the results being reported */
struct result_info {
const char *username;
const char *password;
long chg_lst;
long chg_allow;
long chg_req;
long exp_warn;
long exp_inact;
long exp_date;
unsigned long flag;
};
static SEXP_t *parse_enc_mth(const char *pwd)
{
char *mth_str;
switch (*pwd) {
case '_':
return SEXP_string_newf("BSDi");
case '$':
pwd++;
switch (*pwd) {
case '1':
mth_str = "MD5";
pwd++;
break;
case '2':
mth_str = "Blowfish";
pwd++;
if (*pwd == 'a')
pwd++;
break;
case '5':
mth_str = "SHA-256";
pwd++;
break;
case '6':
mth_str = "SHA-512";
pwd++;
break;
default:
if (strncmp(pwd, "md5", 3))
goto fail;
mth_str = "Sun MD5";
pwd += 3;
}
if (*pwd != '$')
goto fail;
return SEXP_string_newf("%s", mth_str);
default:
return SEXP_string_newf("DES");
}
fail:
return NULL;
}
static void report_finding(struct result_info *res, probe_ctx *ctx)
{
SEXP_t *item, *enc_mth;
SEXP_t se_chl_mem, se_cha_mem, se_chr_mem;
SEXP_t se_exw_mem, se_exi_mem, se_exd_mem;
SEXP_t se_flg_mem;
item = probe_item_create(OVAL_UNIX_SHADOW, NULL,
"username", OVAL_DATATYPE_STRING, res->username,
"password", OVAL_DATATYPE_STRING, res->password,
"chg_lst", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_chl_mem, res->chg_lst),
"chg_allow", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_cha_mem, res->chg_allow),
"chg_req", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_chr_mem, res->chg_req),
"exp_warn", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_exw_mem, res->exp_warn),
"exp_inact", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_exi_mem, res->exp_inact),
"exp_date", OVAL_DATATYPE_SEXP, SEXP_number_newi_64_r(&se_exd_mem, res->exp_date),
"flag", OVAL_DATATYPE_SEXP, SEXP_string_newf_r(&se_flg_mem, "%lu", res->flag),
NULL);
if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) >= 0) {
enc_mth = parse_enc_mth(res->password);
if (enc_mth) {
probe_item_ent_add(item, "encrypt_method", NULL, enc_mth);
SEXP_free(enc_mth);
}
}
probe_item_collect(ctx, item);
SEXP_free_r(&se_chl_mem);
SEXP_free_r(&se_cha_mem);
SEXP_free_r(&se_chr_mem);
SEXP_free_r(&se_exw_mem);
SEXP_free_r(&se_exi_mem);
SEXP_free_r(&se_exd_mem);
SEXP_free_r(&se_flg_mem);
}
static void _process_struct_shadow(struct spwd *sp, SEXP_t *un_ent, probe_ctx *ctx)
{
SEXP_t *un;
struct result_info r;
dI("Have user: %s", sp->sp_namp);
un = SEXP_string_newf("%s", sp->sp_namp);
if (probe_entobj_cmp(un_ent, un) != OVAL_RESULT_TRUE) {
SEXP_free(un);
return;
}
r.username = sp->sp_namp;
r.password = sp->sp_pwdp;
r.chg_lst = sp->sp_lstchg;
r.chg_allow = sp->sp_min;
r.chg_req = sp->sp_max;
r.exp_warn = sp->sp_warn;
r.exp_inact = sp->sp_inact;
r.exp_date = sp->sp_expire;
r.flag = sp->sp_flag;
report_finding(&r, ctx);
SEXP_free(un);
}
static int read_shadow(SEXP_t *un_ent, probe_ctx *ctx)
{
struct spwd *sp;
if (ctx->offline_mode & PROBE_OFFLINE_OWN) {
const char *root = getenv("OSCAP_PROBE_ROOT");
char *shadow_file_path = oscap_path_join(root, "/etc/shadow");
FILE *fp = fopen(shadow_file_path, "r");
if (fp == NULL) {
free(shadow_file_path);
return 1;
}
while ((sp = fgetspent(fp))) {
_process_struct_shadow(sp, un_ent, ctx);
}
fclose(fp);
free(shadow_file_path);
} else {
while ((sp = getspent())) {
_process_struct_shadow(sp, un_ent, ctx);
}
endspent();
}
return 0;
}
int shadow_probe_offline_mode_supported()
{
return PROBE_OFFLINE_OWN;
}
int shadow_probe_main(probe_ctx *ctx, void *arg)
{
SEXP_t *ent, *obj;
obj = probe_ctx_getobject(ctx);
over = probe_obj_get_platform_schema_version(obj);
ent = probe_obj_getent(obj, "username", 1);
if (ent == NULL) {
return PROBE_ENOVAL;
}
// Now we check the file...
read_shadow(ent, ctx);
SEXP_free(ent);
return 0;
}
#endif /* HAVE_SHADOW_H */