allow specifiying access mode when creating twifs file node
[lunaix-os.git] / lunaix-os / kernel / usrscope.c
index 334cad7c18a7ea404395b876c75b537d10caf79b..8a6bc99096a1fa29a1f439e985ebd5ea7919c98c 100644 (file)
@@ -30,6 +30,10 @@ __alloc_groups_obj(unsigned int len)
 static inline void
 __ref_groups_obj(struct ugroup_obj* ugo)
 {
+    if (unlikely(!ugo)) {
+        return;
+    }
+
     ugo->refs++;
 }
 
@@ -111,19 +115,36 @@ uscope_copy(struct user_scope* to, struct user_scope* from)
     memcpy(to, from, sizeof(*to));
 }
 
+enum acl_match
+check_acl_between(uid_t u1, gid_t g1, uid_t u2, gid_t g2)
+{
+    struct user_scope* uscope;
+
+    if (!u1 || u1 == u2)
+        return ACL_MATCH_U;
+
+    if (g1 == g2)
+        return ACL_MATCH_G;
+
+    return ACL_NO_MATCH;
+}
+
 
 enum acl_match
 check_current_acl(uid_t desired_u, gid_t desired_g)
 {
+    enum acl_match match;
     struct user_scope* uscope;
 
-    if (!__current->euid || __current->euid == desired_u) 
-    {
-        return ACL_MATCH_U;
+    if (unlikely(!__current)) {
+        return ACL_NO_MATCH;
     }
 
-    if (__current->egid == desired_g) {
-        return ACL_MATCH_G;
+    match = check_acl_between(__current->euid, __current->egid,
+                              desired_u, desired_g);
+    
+    if (match != ACL_NO_MATCH) {
+        return match;
     }
 
     uscope = current_user_scope();