nitc: fix calling extern constructors from extern code in separate compiler
[nit.git] / lib / privileges.nit
index 20a2179..f17a28e 100644 (file)
@@ -22,6 +22,22 @@ 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
 
@@ -31,16 +47,20 @@ class UserGroup
        # Group name
        var group: nullable String
 
-       # Drop privileges of a user and set his privileges back to default (program privileges)
+       # 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
 
@@ -50,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
@@ -75,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