| 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 |
#ifdef HAVE_CONFIG_H |
| 34 |
#include <config.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "_seap.h" |
| 38 |
#include <probe-api.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <sys/types.h> |
| 42 |
#include <sys/stat.h> |
| 43 |
#include <fcntl.h> |
| 44 |
#include <limits.h> |
| 45 |
#include <pthread.h> |
| 46 |
#include <errno.h> |
| 47 |
#include <crapi/crapi.h> |
| 48 |
#include <probe/probe.h> |
| 49 |
#include <probe/option.h> |
| 50 |
|
| 51 |
#include "oval_fts.h" |
| 52 |
|
| 53 |
#define FILE_SEPARATOR '/' |
| 54 |
|
| 55 |
static pthread_mutex_t __filemd5_probe_mutex; |
| 56 |
|
| 57 |
static int mem2hex (uint8_t *mem, size_t mlen, char *str, size_t slen) |
| 58 |
{ |
| 59 |
const char ch[] = "0123456789abcdef"; |
| 60 |
register size_t i; |
| 61 |
|
| 62 |
if (slen < (mlen * 2) + 1) { |
| 63 |
errno = ENOBUFS; |
| 64 |
return (-1); |
| 65 |
} |
| 66 |
|
| 67 |
for (i = 0; i < mlen; ++i) { |
| 68 |
str[i*2 ] = ch[(mem[i] & 0xf0) >> 4]; |
| 69 |
str[i*2+1] = ch[(mem[i] & 0x0f)]; |
| 70 |
} |
| 71 |
|
| 72 |
str[i*2] = '\0'; |
| 73 |
|
| 74 |
return (0); |
| 75 |
} |
| 76 |
|
| 77 |
static int filehash_cb (const char *p, const char *f, void *ptr, const SEXP_t *filters) |
| 78 |
{ |
| 79 |
SEXP_t *itm, *r0, *r1; |
| 80 |
SEXP_t *cobj = (SEXP_t *) ptr; |
| 81 |
|
| 82 |
char pbuf[PATH_MAX+1]; |
| 83 |
size_t plen, flen; |
| 84 |
|
| 85 |
int fd; |
| 86 |
|
| 87 |
if (f == NULL) |
| 88 |
return (0); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
plen = strlen (p); |
| 94 |
flen = strlen (f); |
| 95 |
|
| 96 |
if (plen + flen + 1 > PATH_MAX) |
| 97 |
return (-1); |
| 98 |
|
| 99 |
memcpy (pbuf, p, sizeof (char) * plen); |
| 100 |
|
| 101 |
if (p[plen - 1] != FILE_SEPARATOR) { |
| 102 |
pbuf[plen] = FILE_SEPARATOR; |
| 103 |
++plen; |
| 104 |
} |
| 105 |
|
| 106 |
memcpy (pbuf + plen, f, sizeof (char) * flen); |
| 107 |
pbuf[plen+flen] = '\0'; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
fd = open (pbuf, O_RDONLY); |
| 113 |
|
| 114 |
if (fd < 0) { |
| 115 |
itm = probe_item_create(OVAL_INDEPENDENT_FILE_MD5, NULL, |
| 116 |
"filepath", OVAL_DATATYPE_STRING, pbuf, |
| 117 |
"path", OVAL_DATATYPE_STRING, p, |
| 118 |
"filename", OVAL_DATATYPE_STRING, f, |
| 119 |
NULL); |
| 120 |
probe_item_add_msg(itm, OVAL_MESSAGE_LEVEL_ERROR, |
| 121 |
"Can't get context for %s: %s\n", pbuf, strerror(errno)); |
| 122 |
probe_item_setstatus(itm, SYSCHAR_STATUS_ERROR); |
| 123 |
|
| 124 |
} else { |
| 125 |
uint8_t md5_dst[16]; |
| 126 |
size_t md5_dstlen = sizeof md5_dst; |
| 127 |
char md5_str[32+1]; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
if (crapi_digest_fd (fd, CRAPI_DIGEST_MD5, &md5_dst, &md5_dstlen) != 0) |
| 133 |
{ |
| 134 |
close (fd); |
| 135 |
return (-1); |
| 136 |
} |
| 137 |
|
| 138 |
close (fd); |
| 139 |
|
| 140 |
mem2hex (md5_dst, sizeof md5_dst, md5_str, sizeof md5_str); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
itm = probe_item_create(OVAL_INDEPENDENT_FILE_MD5, NULL, |
| 146 |
"filepath", OVAL_DATATYPE_STRING, pbuf, |
| 147 |
"path", OVAL_DATATYPE_STRING, p, |
| 148 |
"filename", OVAL_DATATYPE_STRING, f, |
| 149 |
"md5", OVAL_DATATYPE_STRING, md5_str, |
| 150 |
NULL); |
| 151 |
} |
| 152 |
|
| 153 |
probe_cobj_add_item(cobj, itm, filters); |
| 154 |
SEXP_free (itm); |
| 155 |
|
| 156 |
return (0); |
| 157 |
} |
| 158 |
|
| 159 |
int probe_offline_mode_supported() |
| 160 |
{ |
| 161 |
return PROBE_OFFLINE_CHROOT; |
| 162 |
} |
| 163 |
|
| 164 |
void *probe_init (void) |
| 165 |
{ |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
switch (pthread_mutex_init (&__filemd5_probe_mutex, NULL)) { |
| 170 |
case 0: |
| 171 |
return ((void *)&__filemd5_probe_mutex); |
| 172 |
default: |
| 173 |
dD("Can't initialize mutex: errno=%u, %s.", errno, strerror (errno)); |
| 174 |
} |
| 175 |
|
| 176 |
return (NULL); |
| 177 |
} |
| 178 |
|
| 179 |
void probe_fini (void *arg) |
| 180 |
{ |
| 181 |
_A((void *)arg == (void *)&__filemd5_probe_mutex); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
(void) pthread_mutex_destroy (&__filemd5_probe_mutex); |
| 187 |
|
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
int probe_main (SEXP_t *probe_in, SEXP_t *probe_out, void *mutex, SEXP_t *filters) |
| 192 |
{ |
| 193 |
SEXP_t *path, *filename, *behaviors, *filepath; |
| 194 |
OVAL_FTS *ofts; |
| 195 |
OVAL_FTSENT *ofts_ent; |
| 196 |
|
| 197 |
(void)filters; |
| 198 |
|
| 199 |
if (probe_in == NULL || probe_out == NULL) { |
| 200 |
return (PROBE_EINVAL); |
| 201 |
} |
| 202 |
|
| 203 |
if (mutex == NULL) { |
| 204 |
return (PROBE_EINIT); |
| 205 |
} |
| 206 |
|
| 207 |
_A(mutex == &__filemd5_probe_mutex); |
| 208 |
|
| 209 |
path = probe_obj_getent (probe_in, "path", 1); |
| 210 |
filename = probe_obj_getent (probe_in, "filename", 1); |
| 211 |
behaviors = probe_obj_getent (probe_in, "behaviors", 1); |
| 212 |
filepath = probe_obj_getent (probe_in, "filepath", 1); |
| 213 |
|
| 214 |
|
| 215 |
if ( (path == NULL || filename == NULL) && filepath==NULL ) { |
| 216 |
SEXP_free (behaviors); |
| 217 |
SEXP_free (path); |
| 218 |
SEXP_free (filename); |
| 219 |
SEXP_free (filepath); |
| 220 |
|
| 221 |
return (PROBE_ENOELM); |
| 222 |
} |
| 223 |
|
| 224 |
probe_filebehaviors_canonicalize(&behaviors); |
| 225 |
|
| 226 |
switch (pthread_mutex_lock (&__filemd5_probe_mutex)) { |
| 227 |
case 0: |
| 228 |
break; |
| 229 |
default: |
| 230 |
dD("Can't lock mutex(%p): %u, %s.", &__filemd5_probe_mutex, errno, strerror (errno)); |
| 231 |
|
| 232 |
SEXP_free (behaviors); |
| 233 |
SEXP_free (path); |
| 234 |
SEXP_free (filename); |
| 235 |
SEXP_free (filepath); |
| 236 |
|
| 237 |
return (PROBE_EFATAL); |
| 238 |
} |
| 239 |
|
| 240 |
if ((ofts = oval_fts_open_prefixed(NULL, path, filename, filepath, behaviors, probe_ctx_getresult(ctx))) != NULL) { |
| 241 |
while ((ofts_ent = oval_fts_read(ofts)) != NULL) { |
| 242 |
filehash_cb(ofts_ent->path, ofts_ent->file, probe_out, filters); |
| 243 |
oval_ftsent_free(ofts_ent); |
| 244 |
} |
| 245 |
|
| 246 |
oval_fts_close(ofts); |
| 247 |
} |
| 248 |
|
| 249 |
SEXP_free (behaviors); |
| 250 |
SEXP_free (path); |
| 251 |
SEXP_free (filename); |
| 252 |
SEXP_free (filepath); |
| 253 |
|
| 254 |
switch (pthread_mutex_unlock (&__filemd5_probe_mutex)) { |
| 255 |
case 0: |
| 256 |
break; |
| 257 |
default: |
| 258 |
dD("Can't unlock mutex(%p): %u, %s.", &__filemd5_probe_mutex, errno, strerror (errno)); |
| 259 |
|
| 260 |
return (PROBE_EFATAL); |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |