2009-01-14 17:48:30 +00:00
|
|
|
/* audit.c - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2009-01-14 17:48:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2009-06-25 12:14:07 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
2009-01-14 17:48:30 +00:00
|
|
|
* under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation. See http://www.gnu.org/ for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2009-07-31 13:08:09 +00:00
|
|
|
#include <fcntl.h>
|
2009-01-14 17:48:30 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2012-05-01 12:40:31 +00:00
|
|
|
#include <fnmatch.h>
|
2013-06-12 15:43:56 +00:00
|
|
|
#include <limits.h>
|
2009-01-14 17:48:30 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "apk_applet.h"
|
|
|
|
#include "apk_database.h"
|
2012-02-23 13:05:06 +00:00
|
|
|
#include "apk_print.h"
|
|
|
|
|
2014-03-12 12:59:49 +00:00
|
|
|
/* Use (unused) highest bit of mode_t as seen flag of our internal
|
|
|
|
* database file entries */
|
|
|
|
#define S_SEENFLAG 0x80000000
|
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
enum {
|
|
|
|
MODE_BACKUP = 0,
|
|
|
|
MODE_SYSTEM
|
|
|
|
};
|
2009-01-14 17:48:30 +00:00
|
|
|
|
|
|
|
struct audit_ctx {
|
2012-02-23 13:05:06 +00:00
|
|
|
unsigned mode : 1;
|
|
|
|
unsigned recursive : 1;
|
|
|
|
unsigned check_permissions : 1;
|
|
|
|
unsigned packages_only : 1;
|
|
|
|
};
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static int option_parse_applet(void *ctx, struct apk_db_options *dbopts, int optch, const char *optarg)
|
2012-02-23 13:05:06 +00:00
|
|
|
{
|
|
|
|
struct audit_ctx *actx = (struct audit_ctx *) ctx;
|
|
|
|
|
|
|
|
switch (optch) {
|
|
|
|
case 0x10000:
|
|
|
|
actx->mode = MODE_BACKUP;
|
|
|
|
break;
|
|
|
|
case 0x10001:
|
|
|
|
actx->mode = MODE_SYSTEM;
|
|
|
|
break;
|
|
|
|
case 0x10002:
|
|
|
|
actx->check_permissions = 1;
|
|
|
|
break;
|
|
|
|
case 0x10003:
|
|
|
|
actx->packages_only = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
actx->recursive = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2014-10-08 12:29:27 +00:00
|
|
|
return -ENOTSUP;
|
2012-02-23 13:05:06 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static const struct apk_option options_applet[] = {
|
|
|
|
{ 0x10000, "backup", "List all modified configuration files (in "
|
|
|
|
"protected_paths.d) that need to be backed up" },
|
|
|
|
{ 0x10001, "system", "Verify checksums of all installed non-configuration files " },
|
|
|
|
{ 0x10002, "check-permissions", "Check file and directory uid/gid/mode too" },
|
|
|
|
{ 'r', "recursive", "List individually all entries in new directories" },
|
|
|
|
{ 0x10003, "packages", "List only the changed packages (or names only with -q)" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct apk_option_group optgroup_applet = {
|
|
|
|
.name = "Audit",
|
|
|
|
.options = options_applet,
|
|
|
|
.num_options = ARRAY_SIZE(options_applet),
|
|
|
|
.parse = option_parse_applet,
|
|
|
|
};
|
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
struct audit_tree_ctx {
|
|
|
|
struct audit_ctx *actx;
|
|
|
|
struct apk_database *db;
|
2012-07-16 11:44:15 +00:00
|
|
|
struct apk_db_dir *dir;
|
2012-02-23 13:05:06 +00:00
|
|
|
size_t pathlen;
|
|
|
|
char path[PATH_MAX];
|
2009-01-14 17:48:30 +00:00
|
|
|
};
|
|
|
|
|
2012-02-14 14:31:40 +00:00
|
|
|
static int audit_file(struct audit_ctx *actx,
|
|
|
|
struct apk_database *db,
|
|
|
|
struct apk_db_file *dbf,
|
2012-02-23 13:05:06 +00:00
|
|
|
int dirfd, const char *name)
|
2009-07-31 16:35:45 +00:00
|
|
|
{
|
|
|
|
struct apk_file_info fi;
|
2015-04-08 07:25:44 +00:00
|
|
|
int rv = 0;
|
2009-07-31 16:35:45 +00:00
|
|
|
|
2012-02-14 14:31:40 +00:00
|
|
|
if (dbf == NULL)
|
|
|
|
return 'A';
|
|
|
|
|
2014-11-01 18:18:57 +00:00
|
|
|
dbf->audited = 1;
|
2014-03-12 12:59:49 +00:00
|
|
|
|
2015-04-08 07:25:44 +00:00
|
|
|
if (apk_fileinfo_get(dirfd, name,
|
|
|
|
APK_FI_NOFOLLOW |
|
|
|
|
APK_FI_XATTR_CSUM(dbf->acl->xattr_csum.type ?: APK_CHECKSUM_DEFAULT) |
|
|
|
|
APK_FI_CSUM(dbf->csum.type),
|
|
|
|
&fi) != 0)
|
2012-02-23 13:05:06 +00:00
|
|
|
return -EPERM;
|
2009-07-31 16:35:45 +00:00
|
|
|
|
|
|
|
if (dbf->csum.type != APK_CHECKSUM_NONE &&
|
2012-02-23 13:05:06 +00:00
|
|
|
apk_checksum_compare(&fi.csum, &dbf->csum) != 0)
|
2015-04-08 07:25:44 +00:00
|
|
|
rv = 'U';
|
2015-04-17 07:07:49 +00:00
|
|
|
else if (!S_ISLNK(fi.mode) && !dbf->diri->pkg->ipkg->broken_xattr &&
|
2015-04-17 06:44:52 +00:00
|
|
|
apk_checksum_compare(&fi.xattr_csum, &dbf->acl->xattr_csum) != 0)
|
2015-04-08 08:08:16 +00:00
|
|
|
rv = 'x';
|
2015-04-08 07:25:44 +00:00
|
|
|
else if (S_ISLNK(fi.mode) && dbf->csum.type == APK_CHECKSUM_NONE)
|
|
|
|
rv = 'U';
|
|
|
|
else if (actx->check_permissions) {
|
2014-11-01 18:18:57 +00:00
|
|
|
if ((fi.mode & 07777) != (dbf->acl->mode & 07777))
|
2015-04-08 07:25:44 +00:00
|
|
|
rv = 'M';
|
|
|
|
else if (fi.uid != dbf->acl->uid || fi.gid != dbf->acl->gid)
|
|
|
|
rv = 'M';
|
2012-02-14 14:31:40 +00:00
|
|
|
}
|
2015-04-08 07:25:44 +00:00
|
|
|
apk_fileinfo_free(&fi);
|
2009-07-31 16:35:45 +00:00
|
|
|
|
2015-04-08 07:25:44 +00:00
|
|
|
return rv;
|
2012-02-14 14:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int audit_directory(struct audit_ctx *actx,
|
|
|
|
struct apk_database *db,
|
|
|
|
struct apk_db_dir *dbd,
|
|
|
|
struct apk_file_info *fi)
|
|
|
|
{
|
2014-03-12 12:59:49 +00:00
|
|
|
if (dbd != NULL) dbd->mode |= S_SEENFLAG;
|
|
|
|
|
2012-07-16 13:34:35 +00:00
|
|
|
if (dbd == NULL || dbd->refs == 1)
|
|
|
|
return actx->recursive ? 'd' : 'D';
|
2012-02-14 14:31:40 +00:00
|
|
|
|
|
|
|
if (actx->check_permissions &&
|
2015-06-01 08:21:46 +00:00
|
|
|
((dbd->mode & ~S_SEENFLAG) || dbd->uid || dbd->gid)) {
|
2012-02-14 14:31:40 +00:00
|
|
|
if ((fi->mode & 07777) != (dbd->mode & 07777))
|
|
|
|
return 'm';
|
|
|
|
if (fi->uid != dbd->uid || fi->gid != dbd->gid)
|
|
|
|
return 'm';
|
|
|
|
}
|
2012-02-10 14:40:01 +00:00
|
|
|
|
2012-02-14 14:31:40 +00:00
|
|
|
return 0;
|
2009-07-31 16:35:45 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
static void report_audit(struct audit_ctx *actx,
|
|
|
|
char reason, apk_blob_t bfull, struct apk_package *pkg)
|
|
|
|
{
|
|
|
|
if (!reason)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (actx->packages_only) {
|
|
|
|
if (pkg == NULL || pkg->state_int != 0)
|
|
|
|
return;
|
|
|
|
pkg->state_int = 1;
|
|
|
|
if (apk_verbosity < 1)
|
|
|
|
printf("%s\n", pkg->name->name);
|
|
|
|
else
|
|
|
|
printf(PKG_VER_FMT "\n", PKG_VER_PRINTF(pkg));
|
|
|
|
} else if (apk_verbosity < 1) {
|
|
|
|
printf(BLOB_FMT "\n", BLOB_PRINTF(bfull));
|
|
|
|
} else
|
|
|
|
printf("%c " BLOB_FMT "\n", reason, BLOB_PRINTF(bfull));
|
|
|
|
}
|
2012-02-14 14:31:40 +00:00
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
static int audit_directory_tree_item(void *ctx, int dirfd, const char *name)
|
2009-01-14 17:48:30 +00:00
|
|
|
{
|
2012-02-14 14:31:40 +00:00
|
|
|
struct audit_tree_ctx *atctx = (struct audit_tree_ctx *) ctx;
|
2012-02-23 13:05:06 +00:00
|
|
|
apk_blob_t bdir = APK_BLOB_PTR_LEN(atctx->path, atctx->pathlen);
|
|
|
|
apk_blob_t bent = APK_BLOB_STR(name);
|
|
|
|
apk_blob_t bfull;
|
2012-02-14 14:31:40 +00:00
|
|
|
struct audit_ctx *actx = atctx->actx;
|
|
|
|
struct apk_database *db = atctx->db;
|
2012-07-16 11:44:15 +00:00
|
|
|
struct apk_db_dir *dir = atctx->dir, *child = NULL;
|
2009-07-13 17:37:03 +00:00
|
|
|
struct apk_file_info fi;
|
2012-02-23 13:05:06 +00:00
|
|
|
int reason = 0;
|
2009-08-06 05:57:50 +00:00
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
if (bdir.len + bent.len + 1 >= sizeof(atctx->path))
|
|
|
|
return -ENOMEM;
|
2009-01-14 17:48:30 +00:00
|
|
|
|
2015-03-10 13:46:05 +00:00
|
|
|
if (apk_fileinfo_get(dirfd, name, APK_FI_NOFOLLOW, &fi) < 0)
|
2012-02-23 13:05:06 +00:00
|
|
|
return -EPERM;
|
2009-01-14 17:48:30 +00:00
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
memcpy(&atctx->path[atctx->pathlen], bent.ptr, bent.len);
|
|
|
|
atctx->pathlen += bent.len;
|
|
|
|
bfull = APK_BLOB_PTR_LEN(atctx->path, atctx->pathlen);
|
2009-01-14 17:48:30 +00:00
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
if (S_ISDIR(fi.mode)) {
|
|
|
|
int recurse = TRUE;
|
2009-07-30 07:42:20 +00:00
|
|
|
|
2012-07-16 11:44:15 +00:00
|
|
|
if (actx->mode == MODE_BACKUP) {
|
|
|
|
child = apk_db_dir_get(db, bfull);
|
|
|
|
if (!child->has_protected_children)
|
|
|
|
recurse = FALSE;
|
2014-03-12 07:08:26 +00:00
|
|
|
if (child->protect_mode == APK_PROTECT_NONE)
|
2012-07-16 11:44:15 +00:00
|
|
|
goto recurse_check;
|
2009-01-14 17:48:30 +00:00
|
|
|
} else {
|
2012-07-16 11:44:15 +00:00
|
|
|
child = apk_db_dir_query(db, bfull);
|
|
|
|
if (child == NULL)
|
2012-07-16 13:34:35 +00:00
|
|
|
goto done;
|
|
|
|
child = apk_db_dir_ref(child);
|
2009-01-14 17:48:30 +00:00
|
|
|
}
|
2009-07-31 16:35:45 +00:00
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
reason = audit_directory(actx, db, child, &fi);
|
|
|
|
if (reason < 0)
|
|
|
|
goto done;
|
2009-07-31 16:35:45 +00:00
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
recurse_check:
|
|
|
|
atctx->path[atctx->pathlen++] = '/';
|
|
|
|
bfull.len++;
|
|
|
|
report_audit(actx, reason, bfull, NULL);
|
2012-07-16 13:34:35 +00:00
|
|
|
if (reason != 'D' && recurse) {
|
2012-07-16 11:44:15 +00:00
|
|
|
atctx->dir = child;
|
2012-02-23 13:05:06 +00:00
|
|
|
reason = apk_dir_foreach_file(
|
|
|
|
openat(dirfd, name, O_RDONLY|O_CLOEXEC),
|
|
|
|
audit_directory_tree_item, atctx);
|
2012-07-16 11:44:15 +00:00
|
|
|
atctx->dir = dir;
|
2012-02-23 13:05:06 +00:00
|
|
|
}
|
|
|
|
bfull.len--;
|
|
|
|
atctx->pathlen--;
|
|
|
|
} else {
|
|
|
|
struct apk_db_file *dbf;
|
2013-06-14 17:26:48 +00:00
|
|
|
struct apk_protected_path *ppath;
|
2014-03-12 07:08:26 +00:00
|
|
|
int protect_mode = dir->protect_mode;
|
2012-05-01 12:40:31 +00:00
|
|
|
|
|
|
|
/* inherit file's protection mask */
|
2013-06-14 17:26:48 +00:00
|
|
|
foreach_array_item(ppath, dir->protected_paths) {
|
2012-05-01 12:40:31 +00:00
|
|
|
char *slash = strchr(ppath->relative_pattern, '/');
|
|
|
|
if (slash == NULL) {
|
|
|
|
if (fnmatch(ppath->relative_pattern, name, FNM_PATHNAME) != 0)
|
|
|
|
continue;
|
2014-03-12 07:08:26 +00:00
|
|
|
protect_mode = ppath->protect_mode;
|
2012-05-01 12:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-23 13:05:06 +00:00
|
|
|
|
|
|
|
if (actx->mode == MODE_BACKUP) {
|
2014-03-12 07:08:26 +00:00
|
|
|
switch (protect_mode) {
|
|
|
|
case APK_PROTECT_NONE:
|
2012-02-23 13:05:06 +00:00
|
|
|
goto done;
|
2014-03-12 07:08:26 +00:00
|
|
|
case APK_PROTECT_CHANGED:
|
|
|
|
break;
|
|
|
|
case APK_PROTECT_SYMLINKS_ONLY:
|
|
|
|
if (!S_ISLNK(fi.mode))
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
case APK_PROTECT_ALL:
|
|
|
|
reason = 'A';
|
|
|
|
break;
|
|
|
|
}
|
2012-02-23 13:05:06 +00:00
|
|
|
}
|
2009-07-31 16:35:45 +00:00
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
dbf = apk_db_file_query(db, bdir, bent);
|
2014-03-12 07:08:26 +00:00
|
|
|
if (reason == 0)
|
|
|
|
reason = audit_file(actx, db, dbf, dirfd, name);
|
2012-02-23 13:05:06 +00:00
|
|
|
if (reason < 0)
|
|
|
|
goto done;
|
2015-01-30 14:12:45 +00:00
|
|
|
if (actx->mode == MODE_SYSTEM &&
|
|
|
|
(reason == 'A' || protect_mode != APK_PROTECT_NONE))
|
2012-02-23 13:05:06 +00:00
|
|
|
goto done;
|
2015-04-08 08:08:16 +00:00
|
|
|
if (actx->mode == MODE_BACKUP &&
|
|
|
|
reason == 'A' &&
|
|
|
|
apk_blob_ends_with(bent, APK_BLOB_STR(".apk-new")))
|
|
|
|
goto done;
|
2012-02-23 13:05:06 +00:00
|
|
|
report_audit(actx, reason, bfull, dbf ? dbf->diri->pkg : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2012-07-16 11:44:15 +00:00
|
|
|
if (child)
|
|
|
|
apk_db_dir_unref(db, child, FALSE);
|
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
atctx->pathlen -= bent.len;
|
|
|
|
return reason < 0 ? reason : 0;
|
2009-07-31 16:35:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 12:59:49 +00:00
|
|
|
static int audit_directory_tree(struct audit_tree_ctx *atctx, int dirfd)
|
|
|
|
{
|
|
|
|
apk_blob_t path;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
path = APK_BLOB_PTR_LEN(atctx->path, atctx->pathlen);
|
|
|
|
if (path.len && path.ptr[path.len-1] == '/')
|
|
|
|
path.len--;
|
|
|
|
|
|
|
|
atctx->dir = apk_db_dir_get(atctx->db, path);
|
|
|
|
atctx->dir->mode |= S_SEENFLAG;
|
|
|
|
r = apk_dir_foreach_file(dirfd, audit_directory_tree_item, atctx);
|
|
|
|
apk_db_dir_unref(atctx->db, atctx->dir, FALSE);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audit_missing_files(apk_hash_item item, void *pctx)
|
|
|
|
{
|
|
|
|
struct audit_ctx *actx = pctx;
|
|
|
|
struct apk_db_file *file = item;
|
|
|
|
struct apk_db_dir *dir;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
int len;
|
|
|
|
|
2014-11-01 18:18:57 +00:00
|
|
|
if (file->audited) return 0;
|
2014-03-12 12:59:49 +00:00
|
|
|
|
|
|
|
dir = file->diri->dir;
|
|
|
|
if (dir->mode & S_SEENFLAG) {
|
|
|
|
len = snprintf(path, sizeof(path), DIR_FILE_FMT, DIR_FILE_PRINTF(dir, file));
|
|
|
|
report_audit(actx, 'X', APK_BLOB_PTR_LEN(path, len), file->diri->pkg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
static int audit_main(void *ctx, struct apk_database *db, struct apk_string_array *args)
|
2009-07-31 16:35:45 +00:00
|
|
|
{
|
2012-02-23 13:05:06 +00:00
|
|
|
struct audit_tree_ctx atctx;
|
2014-03-12 12:59:49 +00:00
|
|
|
struct audit_ctx *actx = (struct audit_ctx *) ctx;
|
2013-06-18 10:01:51 +00:00
|
|
|
char **parg, *arg;
|
|
|
|
int r = 0;
|
2012-02-23 13:05:06 +00:00
|
|
|
|
|
|
|
atctx.db = db;
|
2014-03-12 12:59:49 +00:00
|
|
|
atctx.actx = actx;
|
2012-02-23 13:05:06 +00:00
|
|
|
atctx.pathlen = 0;
|
|
|
|
atctx.path[0] = 0;
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
if (args->num == 0) {
|
2014-03-12 12:59:49 +00:00
|
|
|
r |= audit_directory_tree(&atctx, dup(db->root_fd));
|
|
|
|
} else {
|
|
|
|
foreach_array_item(parg, args) {
|
|
|
|
arg = *parg;
|
|
|
|
if (arg[0] != '/') {
|
|
|
|
apk_warning("%s: relative path skipped.\n", arg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
arg++;
|
|
|
|
atctx.pathlen = strlen(arg);
|
|
|
|
memcpy(atctx.path, arg, atctx.pathlen);
|
|
|
|
if (atctx.path[atctx.pathlen-1] != '/')
|
|
|
|
atctx.path[atctx.pathlen++] = '/';
|
2013-06-18 10:01:51 +00:00
|
|
|
|
2014-03-12 12:59:49 +00:00
|
|
|
r |= audit_directory_tree(&atctx, openat(db->root_fd, arg, O_RDONLY|O_CLOEXEC));
|
2009-07-31 16:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-12 12:59:49 +00:00
|
|
|
if (actx->mode == MODE_SYSTEM)
|
|
|
|
apk_hash_foreach(&db->installed.files, audit_missing_files, ctx);
|
|
|
|
|
2012-02-23 13:05:06 +00:00
|
|
|
return r;
|
2009-01-14 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct apk_applet apk_audit = {
|
|
|
|
.name = "audit",
|
2013-06-19 13:43:51 +00:00
|
|
|
.help = "Audit the directories for changes",
|
2012-02-23 13:05:06 +00:00
|
|
|
.arguments = "[directory to audit]...",
|
2009-08-06 11:25:03 +00:00
|
|
|
.open_flags = APK_OPENF_READ|APK_OPENF_NO_SCRIPTS|APK_OPENF_NO_REPOS,
|
2009-01-14 17:48:30 +00:00
|
|
|
.context_size = sizeof(struct audit_ctx),
|
2014-10-08 12:29:27 +00:00
|
|
|
.optgroups = { &optgroup_global, &optgroup_applet },
|
2009-01-14 17:48:30 +00:00
|
|
|
.main = audit_main,
|
|
|
|
};
|
|
|
|
|
|
|
|
APK_DEFINE_APPLET(apk_audit);
|
|
|
|
|