aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Pärtel <martin.partel@gmail.com>2020-05-10 23:09:01 +0300
committerMartin Pärtel <martin.partel@gmail.com>2020-05-10 23:09:01 +0300
commite7beb6b9f8c41d760e2df997c5dbc857401a0e82 (patch)
tree29e54324e19d0210b0694d2e938f3daf292cbd3b
parent8e3a87fd0240c1aac39b5fae6f43f63259619c3b (diff)
downloadbindfs-e7beb6b9f8c41d760e2df997c5dbc857401a0e82.tar.gz
Added test case for #85 and fixed a minor UX issue with it.
-rw-r--r--src/bindfs.c18
-rwxr-xr-xtests/test_bindfs.rb21
2 files changed, 33 insertions, 6 deletions
diff --git a/src/bindfs.c b/src/bindfs.c
index 7f69fb0..6668ab8 100644
--- a/src/bindfs.c
+++ b/src/bindfs.c
@@ -1845,11 +1845,11 @@ static int parse_mirrored_users(char* mirror)
return 1;
}
-/**
+/*
* Reads a passwd or group file (like /etc/passwd and /etc/group) and
* adds all entries to the map. Useful for restoring backups
* where UIDs or GIDs differ.
- **/
+ */
static int parse_map_file(UserMap *map, UserMap *reverse_map, char *file, int as_gid)
{
int result = 0;
@@ -1887,19 +1887,25 @@ static int parse_map_file(UserMap *map, UserMap *reverse_map, char *file, int as
}
while ((read = getline(&line, &len, fp)) != -1) {
+ // Remove newline in case someone builds a file with lines like 'a:b:123' by hand.
+ // If we left the newline, strtok would return "123\n" as the last token.
+ if (read > 0) {
+ line[read - 1] = '\0';
+ }
+
lineno++;
- /**
+ /*
* NAME::[GU]ID(:....)
* NAME = TO
* [GU]ID = FROM
- **/
+ */
column = strtok(line, ":");
if (column == NULL) {
fprintf(stderr, "Unexpected end of entry in %s on line %d\n", file, lineno);
goto exit;
}
if (!value_to_id(column, &uid_to)) {
- fprintf(stderr, "Warning: Ignoring invalid %s in %s on line %d: %s\n\n", label_name, file, lineno, column);
+ fprintf(stderr, "Warning: Ignoring invalid %s in %s on line %d: '%s'\n", label_name, file, lineno, column);
continue;
}
@@ -1913,7 +1919,7 @@ static int parse_map_file(UserMap *map, UserMap *reverse_map, char *file, int as
goto exit;
}
if (!value_to_id(column, &uid_from)) {
- fprintf(stderr, "Warning: Ignoring invalid %s in %s on line %d: %s\n\n", label_id, file, lineno, column);
+ fprintf(stderr, "Warning: Ignoring invalid %s in %s on line %d: '%s'\n", label_id, file, lineno, column);
continue;
}
diff --git a/tests/test_bindfs.rb b/tests/test_bindfs.rb
index f8eb70e..8e4ee13 100755
--- a/tests/test_bindfs.rb
+++ b/tests/test_bindfs.rb
@@ -23,6 +23,7 @@ $LOAD_PATH << (ENV['srcdir'] || '.')
require 'common.rb'
require 'etc'
+require 'tempfile'
include Errno
@@ -395,6 +396,26 @@ root_testenv("--map=0/1:@0/@1", :title => "--map and chown/chgrp") do
assert { File.stat('mnt/file1').gid == 1 }
end
+Tempfile.create('passwdfile') do |passwd_file|
+ Tempfile.create('groupfile') do |group_file|
+ passwd_file.puts("nobody:x:123:456:,,,:/tmp:/bin/false")
+ passwd_file.flush
+ group_file.puts("#{nobody_group}:x:789")
+ group_file.flush
+ root_testenv("--map-passwd=#{Shellwords.escape(passwd_file.path)} --map-group=#{Shellwords.escape(group_file.path)}") do
+ touch('src/file1')
+ chown(123, 789, 'src/file1')
+ assert { File.stat('mnt/file1').uid == nobody_uid }
+ assert { File.stat('mnt/file1').gid == nobody_gid }
+
+ touch('src/file2')
+ chown(nobody_uid, nobody_gid, 'mnt/file2')
+ assert { File.stat('src/file2').uid == 123 }
+ assert { File.stat('src/file2').gid == 789 }
+ end
+ end
+end
+
root_testenv("--uid-offset=2") do
touch('src/file')
chown(1, nil, 'src/file')