1
0
Fork 0

- invert io.load_nasal() return value

- let load_rules() return nil or the path (rather than 0 or 1)
- better log output
- cosmetics
This commit is contained in:
mfranz 2008-07-03 16:23:13 +00:00
parent 758a2304b9
commit 6e24e077ba
2 changed files with 15 additions and 14 deletions

View file

@ -17,7 +17,8 @@
# Entries are considered from top down. If no entry matches, then file # Entries are considered from top down. If no entry matches, then file
# access is denied. A local rules file $FG_HOME/Nasal/IOrules using the # access is denied. A local rules file $FG_HOME/Nasal/IOrules using the
# same syntax takes precedence over this file. The default rules that # same syntax takes precedence over this file. The default rules that
# apply when there's no Nasal/IOrules file at all are equivalent to this: # apply if there's no Nasal/IOrules file at all, or there are no rules
# defined in it are equivalent to this:
# #
# READ DENY * # READ DENY *
# WRITE DENY * # WRITE DENY *

View file

@ -48,11 +48,11 @@ var load_nasal = func(file, module = nil) {
printlog("info", "loading ", file, " into namespace ", module); printlog("info", "loading ", file, " into namespace ", module);
var code = call(func compile(readfile(file), file), nil, err); var code = call(func compile(readfile(file), file), nil, err);
if(size(err)) if(size(err))
return !!print(file ~ ": " ~ err[0]); return !print(file ~ ": " ~ err[0]);
call(bind(code, globals), nil, nil, globals[module], err); call(bind(code, globals), nil, nil, globals[module], err);
debug.printerror(err); debug.printerror(err);
return !!size(err); return !size(err);
} }
# The following two functions are for reading generic XML files into # The following two functions are for reading generic XML files into
@ -95,9 +95,7 @@ var readxml = func(path, prefix = "___") {
node.setValue(buf[1]); node.setValue(buf[1]);
node = node.getParent(); node = node.getParent();
} }
var data = func(d) { var data = func(d) stack[-1][1] ~= d;
stack[-1][1] ~= d;
}
return parsexml(path, start, end, data) == nil ? nil : tree; return parsexml(path, start, end, data) == nil ? nil : tree;
} }
@ -145,17 +143,18 @@ var writexml = func(path, node, indent = "\t", prefix = "___") {
# Redefine io.open() such that files can only be opened under authorized directories. # Redefine io.open() such that files can only be opened under authorized directories.
# #
_setlistener("/sim/signals/nasal-dir-initialized", func { _setlistener("/sim/signals/nasal-dir-initialized", func {
var _open = open; var _open = io.open;
var root = string.fixpath(getprop("/sim/fg-root")); var root = string.fixpath(getprop("/sim/fg-root"));
var home = string.fixpath(getprop("/sim/fg-home")); var home = string.fixpath(getprop("/sim/fg-home"));
var config = "Nasal/IOrules"; var config = "Nasal/IOrules";
var rules_file = nil;
var read_rules = []; var read_rules = [];
var write_rules = []; var write_rules = [];
var load_rules = func(path) { var load_rules = func(path) {
if(stat(path) == nil) if(stat(path) == nil)
return 0; return nil;
printlog("info", "using io.open() rules from ", path); printlog("info", "using io.open() rules from ", path);
read_rules = []; read_rules = [];
write_rules = []; write_rules = [];
@ -180,11 +179,12 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
append(f[0] == "READ" ? read_rules : write_rules, [pattern, f[1] == "ALLOW"]); append(f[0] == "READ" ? read_rules : write_rules, [pattern, f[1] == "ALLOW"]);
} }
close(file); close(file);
return 1; return path;
} }
# catch exceptions so that a die() doesn't ruin everything # catch exceptions so that a die() doesn't ruin everything
call(func load_rules(home ~ "/" ~ config) or load_rules(root ~ "/" ~ config), nil, var err = []); var rules_file = call(func load_rules(home ~ "/" ~ config)
or load_rules(root ~ "/" ~ config), nil, var err = []);
if(size(err)) { if(size(err)) {
debug.printerror(err); debug.printerror(err);
read_rules = write_rules = []; read_rules = write_rules = [];
@ -192,9 +192,9 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
read_rules = [["*/" ~ config, 0]] ~ read_rules; read_rules = [["*/" ~ config, 0]] ~ read_rules;
write_rules = [["*/" ~ config, 0]] ~ write_rules; write_rules = [["*/" ~ config, 0]] ~ write_rules;
if(getprop("/sim/logging/priority") == "info") { if(__.log_level <= 3) {
print("READ: ", debug.string(read_rules)); print("io.open()/READ: ", debug.string(read_rules));
print("WRITE: ", debug.string(write_rules)); print("io.open()/WRITE: ", debug.string(write_rules));
} }
io.open = func(path, mode = "rb") { io.open = func(path, mode = "rb") {
@ -211,7 +211,7 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
} }
} }
die("io.open(): opening file '" ~ path ~ "' denied (unauthorized directory)\n "); die("io.open(): opening file '" ~ path ~ "' denied (unauthorized access)\n ");
} }
}); });