Skip to content

Commit

Permalink
Don't check that numeric UIDs and GIDs exist in user database.
Browse files Browse the repository at this point in the history
A user database is not always available.

For #79.
  • Loading branch information
mpartel committed Dec 21, 2019
1 parent 28f2488 commit e07c09d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 50 deletions.
64 changes: 16 additions & 48 deletions src/userinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,37 +243,21 @@ static int gid_cache_gid_searchcmp(const void *key, const void *entry)

int user_uid(const char *username, uid_t *ret)
{
struct passwd pwbuf, *pwbufp = NULL;
char *buf;
size_t buflen;
int res;

uid_t uid;
char *endptr;

/* Check whether the string is a numerical UID */
uid = strtol(username, &endptr, 10);
char *endptr;
uid_t uid = strtol(username, &endptr, 10);
if (*endptr == '\0') {
buflen = 1024;
buf = malloc(buflen);
res = getpwuid_r(uid, &pwbuf, buf, buflen, &pwbufp);
if (res != 0) {
if (res != ERANGE) { /* don't care if buffer was too small */
free(buf);
return 0;
}
}
free(buf);
*ret = uid;
return 1;
}

/* Process user name */
buflen = 1024;
buf = malloc(buflen);
/* Handle as textual user name */
size_t buflen = 1024;
char *buf = malloc(buflen);

res = getpwnam_r(username, &pwbuf, buf, buflen, &pwbufp);
while(res == ERANGE) {
struct passwd pwbuf, *pwbufp = NULL;
int res = getpwnam_r(username, &pwbuf, buf, buflen, &pwbufp);
while (res == ERANGE) {
buflen *= 2;
buf = realloc(buf, buflen);
res = getpwnam_r(username, &pwbuf, buf, buflen, &pwbufp);
Expand All @@ -292,37 +276,21 @@ int user_uid(const char *username, uid_t *ret)

int group_gid(const char *groupname, gid_t *ret)
{
struct group gbuf, *gbufp = NULL;
char *buf;
size_t buflen;
int res;

gid_t gid;
char *endptr;

/* Check whether the string is a numerical GID */
gid = strtol(groupname, &endptr, 10);
char *endptr;
gid_t gid = strtol(groupname, &endptr, 10);
if (*endptr == '\0') {
buflen = 1024;
buf = malloc(buflen);
res = getgrgid_r(gid, &gbuf, buf, buflen, &gbufp);
if (res != 0) {
if (res != ERANGE) { /* don't care if buffer was too small */
free(buf);
return 0;
}
}
free(buf);
*ret = gid;
return 1;
}

/* Process group name */
buflen = 1024;
buf = malloc(buflen);
/* Handle as textual group name */
size_t buflen = 1024;
char *buf = malloc(buflen);

res = getgrnam_r(groupname, &gbuf, buf, buflen, &gbufp);
while(res == ERANGE) {
struct group gbuf, *gbufp = NULL;
int res = getgrnam_r(groupname, &gbuf, buf, buflen, &gbufp);
while (res == ERANGE) {
buflen *= 2;
buf = realloc(buf, buflen);
res = getgrnam_r(groupname, &gbuf, buf, buflen, &gbufp);
Expand Down
16 changes: 15 additions & 1 deletion tests/test_bindfs.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
#
# Copyright 2006,2007,2008,2009,2010,2012 Martin Pärtel <[email protected]>
# Copyright 2006,2007,2008,2009,2010,2012,2019 Martin Pärtel <[email protected]>
#
# This file is part of bindfs.
#
Expand Down Expand Up @@ -66,6 +66,20 @@ def chown(user, group, list)
assert { File.stat('mnt/file').gid == nobody_gid }
end

testenv("-u #{nobody_uid} -g #{nobody_gid}", :title => "numeric UID and GID") do
touch('src/file')

assert { File.stat('mnt/file').uid == nobody_uid }
assert { File.stat('mnt/file').gid == nobody_gid }
end

testenv("-u 55544 -g 55566", :title => "nonexistent UID and GID") do
touch('src/file')

assert { File.stat('mnt/file').uid == 55544 }
assert { File.stat('mnt/file').gid == 55566 }
end

testenv("-p 0600:u+D") do
touch('src/file')
chmod(0777, 'src/file')
Expand Down
3 changes: 2 additions & 1 deletion vagrant/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def with_retries(n, options = {}, &block)
threads.each(&:join)

if errors.empty?
puts "OK"
puts "All OK"
else
unless errors.empty?
puts
Expand All @@ -119,3 +119,4 @@ def with_retries(n, options = {}, &block)
puts
end
end

0 comments on commit e07c09d

Please sign in to comment.