Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
2016-08-09 19:29:04 -04:00
|
|
|
* Copyright (c) 2013, 2015, 2016 Jonas 'Sortie' Termansen.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* rules.c
|
|
|
|
* Determines whether a given path is included in the filesystem.
|
|
|
|
*/
|
2013-01-29 14:01:46 -05:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2016-08-09 19:29:04 -04:00
|
|
|
#include <err.h>
|
2013-01-29 14:01:46 -05:00
|
|
|
#include <errno.h>
|
2016-02-07 16:20:12 -05:00
|
|
|
#include <libgen.h>
|
2013-01-29 14:01:46 -05:00
|
|
|
#include <stdarg.h>
|
2016-02-28 17:03:16 -05:00
|
|
|
#include <stdbool.h>
|
2013-01-29 14:01:46 -05:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "rules.h"
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
static struct InclusionRule** rules;
|
|
|
|
static size_t num_rules;
|
|
|
|
static size_t num_rules_allocated;
|
|
|
|
static bool default_inclusion = true;
|
|
|
|
static bool default_inclusion_determined;
|
|
|
|
static char** manifest;
|
|
|
|
static size_t manifest_used;
|
|
|
|
static size_t manifest_length;
|
2013-01-29 14:01:46 -05:00
|
|
|
|
|
|
|
static const char* SkipCharacters(const char* str, char c)
|
|
|
|
{
|
|
|
|
while ( *str == c)
|
|
|
|
str++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// /usr/bin/foobar match /usr = true
|
|
|
|
// /usr/bin/foobar match usr = false
|
|
|
|
// ///usr////bin//foobar match //usr// = true
|
|
|
|
// ///usr////bin//foobar match //usr//./evince = false
|
|
|
|
// TODO: Should this support . and .. too?
|
|
|
|
static bool PathMatchesPattern(const char* path, const char* pattern)
|
|
|
|
{
|
|
|
|
bool last_was_slash = false;
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
if ( !*pattern )
|
|
|
|
return !*path || last_was_slash;
|
|
|
|
if ( (last_was_slash = *pattern == '/') )
|
|
|
|
{
|
|
|
|
if ( *path == '/' )
|
|
|
|
{
|
|
|
|
path = SkipCharacters(path, '/');
|
|
|
|
pattern = SkipCharacters(pattern, '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( *pattern++ != *path++ )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 20:04:01 -05:00
|
|
|
static int search_path(const void* a_ptr, const void* b_ptr)
|
|
|
|
{
|
|
|
|
const char* key = (const char*) a_ptr;
|
|
|
|
char* path = *(char**) b_ptr;
|
|
|
|
return strcmp(key, path);
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool IncludesPath(const char* path)
|
2013-01-29 14:01:46 -05:00
|
|
|
{
|
|
|
|
bool determined = false;
|
|
|
|
bool included = false;
|
|
|
|
for ( size_t i = 0; i < num_rules; i++ )
|
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
struct InclusionRule* rule = rules[i];
|
|
|
|
if ( !PathMatchesPattern(path, rule->pattern) )
|
2013-01-29 14:01:46 -05:00
|
|
|
continue;
|
|
|
|
switch ( rules[i]->rule )
|
|
|
|
{
|
|
|
|
case RULE_INCLUDE:
|
|
|
|
included = true;
|
|
|
|
determined = true;
|
|
|
|
break;
|
|
|
|
case RULE_EXCLUDE:
|
|
|
|
included = false;
|
|
|
|
determined = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !determined )
|
|
|
|
included = default_inclusion;
|
2015-12-27 20:04:01 -05:00
|
|
|
if ( !included )
|
|
|
|
return false;
|
|
|
|
if ( manifest_used &&
|
|
|
|
!bsearch(path, manifest, manifest_used, sizeof(char*), search_path) )
|
|
|
|
return false;
|
|
|
|
return true;
|
2013-01-29 14:01:46 -05:00
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool ChangeRulesAmount(size_t new_length)
|
2013-01-29 14:01:46 -05:00
|
|
|
{
|
|
|
|
size_t new_num_rules = new_length < num_rules ? new_length : num_rules;
|
|
|
|
for ( size_t i = new_num_rules; i < num_rules; i++ )
|
2016-02-28 17:03:16 -05:00
|
|
|
{
|
|
|
|
free(rules[i]->pattern);
|
|
|
|
free(rules[i]);
|
|
|
|
}
|
2013-01-29 14:01:46 -05:00
|
|
|
num_rules = new_num_rules;
|
2016-02-28 17:03:16 -05:00
|
|
|
struct InclusionRule** new_rules = (struct InclusionRule**)
|
|
|
|
malloc(sizeof(struct InclusionRule*) * new_length);
|
2013-01-29 14:01:46 -05:00
|
|
|
for ( size_t i = 0; i < new_length && i < num_rules; i++ )
|
|
|
|
new_rules[i] = rules[i];
|
2016-02-28 17:03:16 -05:00
|
|
|
free(rules); rules = new_rules;
|
2013-01-29 14:01:46 -05:00
|
|
|
num_rules_allocated = new_length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool AddRule(struct InclusionRule* rule)
|
2013-01-29 14:01:46 -05:00
|
|
|
{
|
|
|
|
if ( num_rules == num_rules_allocated )
|
|
|
|
{
|
|
|
|
size_t new_length = num_rules_allocated ? 2 * num_rules_allocated : 32;
|
|
|
|
if ( !ChangeRulesAmount(new_length) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rules[num_rules++] = rule;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* SkipWhitespace(const char* line)
|
|
|
|
{
|
2015-08-11 09:37:31 -04:00
|
|
|
while ( *line && isspace((unsigned char) *line) )
|
|
|
|
line++;
|
2013-01-29 14:01:46 -05:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsLineComment(const char* line)
|
|
|
|
{
|
|
|
|
return !*line || *line == '#';
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* IsLineCommand(const char* line, const char* command)
|
|
|
|
{
|
2015-08-11 09:37:31 -04:00
|
|
|
while ( *line && isspace((unsigned char) *line) )
|
|
|
|
line++;
|
2013-01-29 14:01:46 -05:00
|
|
|
size_t cmdlen = strlen(command);
|
|
|
|
if ( strncmp(line, command, cmdlen) != 0 )
|
|
|
|
return NULL;
|
2015-08-11 09:37:31 -04:00
|
|
|
if ( line[cmdlen] && !isspace((unsigned char) line[cmdlen]) )
|
2013-01-29 14:01:46 -05:00
|
|
|
return NULL;
|
2015-08-11 09:37:31 -04:00
|
|
|
while ( line[cmdlen] && isspace((unsigned char) line[cmdlen]) )
|
2013-01-29 14:01:46 -05:00
|
|
|
cmdlen++;
|
|
|
|
return line + cmdlen;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool AddRulesFromFile(FILE* fp, const char* fpname)
|
2013-01-29 14:01:46 -05:00
|
|
|
{
|
|
|
|
size_t rules_at_start = num_rules;
|
|
|
|
size_t line_size;
|
|
|
|
size_t line_num = 0;
|
|
|
|
char* mem = NULL;
|
|
|
|
ssize_t line_len;
|
2016-05-15 10:32:38 -04:00
|
|
|
while ( 0 < (line_len = getline(&mem, &line_size, fp)) )
|
2013-01-29 14:01:46 -05:00
|
|
|
{
|
|
|
|
char* line = mem;
|
|
|
|
line_num++;
|
2016-05-15 10:32:38 -04:00
|
|
|
if ( line[line_len-1] == '\n' )
|
|
|
|
line[--line_len] = '\0';
|
2016-02-28 17:03:16 -05:00
|
|
|
line = (char*) SkipWhitespace((char*) line);
|
2013-01-29 14:01:46 -05:00
|
|
|
if ( IsLineComment(line) )
|
|
|
|
continue;
|
|
|
|
const char* parameter;
|
|
|
|
if ( (parameter = IsLineCommand(line, "default")) )
|
|
|
|
{
|
|
|
|
bool value;
|
|
|
|
if ( !strcmp(parameter, "true") )
|
|
|
|
value = true;
|
2015-10-07 15:30:12 -04:00
|
|
|
else if ( !strcmp(parameter, "false") )
|
2013-01-29 14:01:46 -05:00
|
|
|
value = false;
|
|
|
|
else
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warnx("%s:%zu: not a boolean '%s'", fpname, line_num, parameter);
|
2013-01-29 14:01:46 -05:00
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
if ( !default_inclusion_determined )
|
|
|
|
default_inclusion = value,
|
|
|
|
default_inclusion_determined = true;
|
|
|
|
else
|
|
|
|
default_inclusion = default_inclusion || value;
|
|
|
|
}
|
|
|
|
else if ( (parameter = IsLineCommand(line, "exclude")) ||
|
|
|
|
(parameter = IsLineCommand(line, "include")) )
|
|
|
|
{
|
|
|
|
if ( !*parameter )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warnx("%s:%zu: no parameter given", fpname, line_num);
|
2013-01-29 14:01:46 -05:00
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
const char* pattern = parameter;
|
2016-02-28 17:03:16 -05:00
|
|
|
enum InclusionRuleType type = line[0] == 'e' ? RULE_EXCLUDE : RULE_INCLUDE;
|
|
|
|
struct InclusionRule* rule =
|
|
|
|
(struct InclusionRule*) malloc(sizeof(struct InclusionRule));
|
|
|
|
rule->pattern = strdup(pattern);
|
|
|
|
rule->rule = type;
|
2013-01-29 14:01:46 -05:00
|
|
|
if ( !AddRule(rule) )
|
|
|
|
goto error_out_errno;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warnx("%s:%zu: line not understood: '%s'", fpname, line_num, line);
|
2013-01-29 14:01:46 -05:00
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ferror(fp) )
|
|
|
|
{
|
|
|
|
error_out_errno:
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("%s", fpname);
|
2013-01-29 14:01:46 -05:00
|
|
|
error_out:
|
2018-07-16 14:08:29 -04:00
|
|
|
free(mem);
|
2013-01-29 14:01:46 -05:00
|
|
|
ChangeRulesAmount(rules_at_start);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-16 14:08:29 -04:00
|
|
|
free(mem);
|
2013-01-29 14:01:46 -05:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-27 20:04:01 -05:00
|
|
|
|
|
|
|
int compare_path(const void* a_ptr, const void* b_ptr)
|
|
|
|
{
|
|
|
|
const char* a = *(const char* const*) a_ptr;
|
|
|
|
const char* b = *(const char* const*) b_ptr;
|
|
|
|
return strcmp(a, b);
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool AddManifestPath(const char* path)
|
2016-02-07 16:20:12 -05:00
|
|
|
{
|
|
|
|
if ( manifest_used == manifest_length )
|
|
|
|
{
|
|
|
|
size_t new_length = 2 * manifest_length;
|
|
|
|
if ( new_length == 0 )
|
|
|
|
new_length = 64;
|
|
|
|
size_t new_size = new_length * sizeof(char*);
|
|
|
|
char** new_manifest = (char**) realloc(manifest, new_size);
|
|
|
|
if ( !new_manifest )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("malloc");
|
2016-02-07 16:20:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
manifest = new_manifest;
|
|
|
|
manifest_length = new_length;
|
|
|
|
}
|
|
|
|
char* copy = strdup(path);
|
|
|
|
if ( !copy )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("malloc");
|
2016-02-07 16:20:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
manifest[manifest_used++] = copy;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool AddManifestFromFile(FILE* fp, const char* fpname)
|
2015-12-27 20:04:01 -05:00
|
|
|
{
|
|
|
|
char* line = NULL;
|
|
|
|
size_t line_size = 0;
|
|
|
|
ssize_t line_len;
|
2016-05-15 10:32:38 -04:00
|
|
|
while ( 0 < (line_len = getline(&line, &line_size, fp)) )
|
2015-12-27 20:04:01 -05:00
|
|
|
{
|
2016-05-15 10:32:38 -04:00
|
|
|
if ( line[line_len-1] == '\n' )
|
|
|
|
line[--line_len] = '\0';
|
2016-02-28 17:03:16 -05:00
|
|
|
if ( !AddManifestPath(line) )
|
2016-02-07 16:20:12 -05:00
|
|
|
return false;
|
2015-12-27 20:04:01 -05:00
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
if ( ferror(fp) )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("%s", fpname);
|
2015-12-27 20:04:01 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-28 17:03:16 -05:00
|
|
|
if ( !AddManifestPath("/") ||
|
|
|
|
!AddManifestPath("/tix") ||
|
|
|
|
!AddManifestPath("/tix/manifest") )
|
2016-02-07 16:20:12 -05:00
|
|
|
return false;
|
|
|
|
char* fpname_copy = strdup(fpname);
|
|
|
|
if ( !fpname_copy )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("malloc");
|
2016-02-07 16:20:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const char* fpname_basename = basename(fpname_copy);
|
|
|
|
char* manifest_path;
|
|
|
|
if ( asprintf(&manifest_path, "/tix/manifest/%s", fpname_basename) < 0 )
|
|
|
|
{
|
|
|
|
free(fpname_copy);
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("malloc");
|
2016-02-07 16:20:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
free(fpname_copy);
|
2016-02-28 17:03:16 -05:00
|
|
|
if ( !AddManifestPath(manifest_path) )
|
2016-02-07 16:20:12 -05:00
|
|
|
return free(manifest_path), false;
|
|
|
|
free(manifest_path);
|
2015-12-27 20:04:01 -05:00
|
|
|
qsort(manifest, manifest_used, sizeof(char*), compare_path);
|
|
|
|
return true;
|
|
|
|
}
|