X-Git-Url: http://nitlanguage.org diff --git a/lib/privileges.nit b/lib/privileges.nit index 05f8fc7..f17a28e 100644 --- a/lib/privileges.nit +++ b/lib/privileges.nit @@ -22,19 +22,45 @@ module privileges import opts +redef class Text + # Does the operating system know the user named `self`? + fun user_exists: Bool + do + var passwd = new Passwd.from_name(to_s) + return not passwd.address_is_null + end + + # Does the operating system know the group named `self`? + fun group_exists: Bool + do + var passwd = new Group.from_name(to_s) + return not passwd.address_is_null + end +end + +# Class to manage user groups class UserGroup + + # User name var user: String + + # Group name var group: nullable String + # Drop privileges of the running program to those of `self` + # + # require: `user.user_exists and (group == null or group.group_exists)` fun drop_privileges do var passwd = new Passwd.from_name(user) + assert not passwd.address_is_null var uid = passwd.uid var group = group var gid if group != null then var gpasswd = new Group.from_name(group) + assert not gpasswd.address_is_null gid = gpasswd.gid else gid = passwd.gid @@ -44,21 +70,16 @@ class UserGroup end # Option to ask for a username and group -class OptionDropPrivileges - super OptionUserAndGroup - - init do init_user_and_group("Drop privileges to user:group or simply user", "-u", "--usergroup") -end - -# Option to ask for a username and group class OptionUserAndGroup super OptionParameter redef type VALUE: nullable UserGroup - #init for_droping_privileges() do init("Drop privileges to user:group or simply user", "-u", "--usergroup") - init(help: String, names: String...) do init_opt(help, null, names) - private init init_user_and_group(help: String, names: String...) do init_opt(help, null, names) + # Create an `OptionUserAndGroup` for dropping privileges + init for_dropping_privileges + do + init("Drop privileges to user:group or simply user", null, ["-u", "--usergroup"]) + end redef fun convert(str) do @@ -69,8 +90,7 @@ class OptionUserAndGroup return new UserGroup(words[0], words[1]) else errors.add("Option {names.join(", ")} expected parameter in the format \"user:group\" or simply \"user\".\n") - abort # FIXME only for nitc, remove and replace with next line when FFI is working in nitg - #return null + return null end end end