[enh] Catch unknown uid/gid in utils.filesystem.chown

This commit is contained in:
Jérôme Lebleu 2016-05-21 18:40:52 +02:00
parent 79e46a5160
commit a092370710
2 changed files with 16 additions and 3 deletions

View file

@ -38,5 +38,8 @@
"logged_out" : "Logged out",
"not_logged_in" : "You are not logged in",
"server_already_running" : "A server is already running on that port",
"websocket_request_expected" : "Expected a WebSocket request"
"websocket_request_expected" : "Expected a WebSocket request",
"unknown_user" : "Unknown '{user}' user",
"unknown_group" : "Unknown '{group}' group"
}

View file

@ -4,6 +4,8 @@ import shutil
from pwd import getpwnam
from grp import getgrnam
from moulinette.core import MoulinetteError
# Files & directories --------------------------------------------------
@ -60,11 +62,19 @@ def chown(path, uid=None, gid=None, recursive=False):
# Retrieve uid/gid
if isinstance(uid, basestring):
uid = getpwnam(uid).pw_uid
try:
uid = getpwnam(uid).pw_uid
except KeyError:
raise MoulinetteError(errno.EINVAL,
m18n.g('unknown_user', user=uid))
elif uid is None:
uid = -1
if isinstance(gid, basestring):
gid = getpwnam(gid).gr_gid
try:
gid = getpwnam(gid).gr_gid
except KeyError:
raise MoulinetteError(errno.EINVAL,
m18n.g('unknown_group', group=gid))
elif gid is None:
gid = -1