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
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
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