Add a function to traverse shadow files 42/66142/1
authorSungbae Yoo <sungbae.yoo@samsung.com>
Fri, 15 Apr 2016 06:42:08 +0000 (15:42 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Fri, 15 Apr 2016 07:18:06 +0000 (16:18 +0900)
Change-Id: Ic05b1f4070407e1a9563e1f5085d05de3170abeb
Signed-off-by: Sungbae Yoo <sungbae.yoo@samsung.com>
common/auth/group.cpp
common/auth/shadow.cpp
common/auth/shadow.h
common/auth/user.cpp

index 77c7c7b..c6e6014 100644 (file)
@@ -152,8 +152,14 @@ void Group::remove()
         throw runtime::Exception("Group is already removed");
     }
 
-    Shadow::removeGroup(GROUP_DIR_PATH GROUP_FILE_NAME, gid);
-    Shadow::removeGshadow(GROUP_DIR_PATH GSHADOW_FILE_NAME, name);
+    Shadow::foreachGroup(GROUP_DIR_PATH GROUP_FILE_NAME,
+    [this](const struct group & grp) -> bool {
+        return grp.gr_gid != gid;
+    });
+    Shadow::foreachGshadow(GROUP_DIR_PATH GSHADOW_FILE_NAME,
+    [this](const struct sgrp & sgrp) -> bool {
+        return sgrp.sg_namp != name;
+    });
 
     name = "";
     gid = INVALID_GID;
index 01cbdc4..4e98c0c 100644 (file)
@@ -86,11 +86,11 @@ void Shadow::put(const std::string& filename, const pwdStruct& pwd,
     fp_pwd.reset();
 }
 
-template<typename pwdStruct, typename element>
-void Shadow::remove(const std::string& filename, const element& value,
+template<typename pwdStruct>
+void Shadow::foreach(const std::string& filename,
                     std::function<int(const pwdStruct*, FILE*)> put,
                     std::function<pwdStruct *(FILE*)> get,
-                    std::function<bool(const pwdStruct&, const element&)> compare)
+                    std::function<bool(pwdStruct&)> check)
 {
     std::string tmpfilename = filename + ".tmp";
     pwdStruct* ppwd;
@@ -134,7 +134,7 @@ void Shadow::remove(const std::string& filename, const element& value,
     }
 
     for (ppwd = get(fp_pwd.get()); ppwd != NULL; ppwd = get(fp_pwd.get()))
-        if (!compare(*ppwd, value))
+        if (check(*ppwd))
             if (put(ppwd, fp_tmp_pwd.get()) != 0) {
                 throw runtime::Exception("Tmp file for shadow write error");
             }
@@ -156,6 +156,7 @@ void Shadow::remove(const std::string& filename, const element& value,
     }
 }
 
+
 void Shadow::putPasswd(const std::string& filename, const struct passwd& ent)
 {
     put<struct passwd>(filename, ent, putpwent);
@@ -177,36 +178,24 @@ void Shadow::putGshadow(const std::string& filename, const struct sgrp& ent)
 }
 
 
-void Shadow::removePasswd(const std::string& filename, const uid_t uid)
+void Shadow::foreachPasswd(const std::string& filename, std::function<bool(passwd&)> check)
 {
-    remove<struct passwd, uid_t>(filename, uid, putpwent, fgetpwent,
-    [](const struct passwd & pwd, const uid_t & uid) -> bool {
-        return pwd.pw_uid == uid;
-    });
+    foreach<struct passwd>(filename, putpwent, fgetpwent, check);
 }
 
-void Shadow::removeShadow(const std::string& filename, const std::string& user)
+void Shadow::foreachShadow(const std::string& filename, std::function<bool(spwd&)> check)
 {
-    remove<struct spwd, std::string>(filename, user, putspent, fgetspent,
-    [](const struct spwd & spwd, const std::string & user) -> bool {
-        return spwd.sp_namp == user;
-    });
+    foreach<struct spwd>(filename, putspent, fgetspent, check);
 }
 
-void Shadow::removeGroup(const std::string& filename, const gid_t gid)
+void Shadow::foreachGroup(const std::string& filename, std::function<bool(group&)> check)
 {
-    remove<struct group, gid_t>(filename, gid, putgrent, fgetgrent,
-    [](const struct group & grp, const gid_t & gid) -> bool {
-        return grp.gr_gid == gid;
-    });
+    foreach<struct group>(filename, putgrent, fgetgrent, check);
 }
 
-void Shadow::removeGshadow(const std::string& filename, const std::string& group)
+void Shadow::foreachGshadow(const std::string& filename, std::function<bool(sgrp&)> check)
 {
-    remove<struct sgrp, std::string>(filename, group, putsgent, fgetsgent,
-    [](const struct sgrp & sgrp, const std::string & group) -> bool {
-        return sgrp.sg_namp == group;
-    });
+    foreach<struct sgrp>(filename, putsgent, fgetsgent, check);
 }
 
 } // namespace runtime
index e17294c..d3c0863 100644 (file)
@@ -44,21 +44,21 @@ public:
     static void putGroup(const std::string& filename, const struct group& ent);
     static void putGshadow(const std::string& filename, const struct sgrp& ent);
 
-    static void removePasswd(const std::string& filename, const uid_t uid);
-    static void removeShadow(const std::string& filename, const std::string& user);
-    static void removeGroup(const std::string& filename, const gid_t gid);
-    static void removeGshadow(const std::string& filename, const std::string& group);
+    static void foreachPasswd(const std::string& filename, std::function<bool(passwd&)> check);
+    static void foreachShadow(const std::string& filename, std::function<bool(spwd&)> check);
+    static void foreachGroup(const std::string& filename, std::function<bool(group&)> check);
+    static void foreachGshadow(const std::string& filename, std::function<bool(sgrp&)> check);
 
 private:
     template<typename pwdStruct>
     static void put(const std::string& filename, const pwdStruct& pwd,
                     std::function<int(const pwdStruct*, FILE*)> put);
 
-    template<typename pwdStruct, typename element>
-    static void remove(const std::string& filename, const element& value,
-                       std::function<int(const pwdStruct*, FILE*)> put,
-                       std::function<pwdStruct *(FILE*)> get,
-                       std::function<bool(const pwdStruct&, const element&)> compare);
+    template<typename pwdStruct>
+    static void foreach(const std::string& filename,
+                        std::function<int(const pwdStruct*, FILE*)> put,
+                        std::function<pwdStruct *(FILE*)> get,
+                        std::function<bool(pwdStruct&)> check);
 };
 
 } // namespace runtime
index 84d2cbf..4568914 100644 (file)
@@ -179,8 +179,14 @@ void User::remove()
         throw runtime::Exception("User is already removed");
     }
 
-    Shadow::removePasswd(PASSWD_DIR_PATH PASSWD_FILE_NAME, uid);
-    Shadow::removeShadow(PASSWD_DIR_PATH SHADOW_FILE_NAME, name);
+    Shadow::foreachPasswd(PASSWD_DIR_PATH PASSWD_FILE_NAME,
+    [this](const struct passwd & pwd) -> bool {
+        return pwd.pw_uid != uid;
+    });
+    Shadow::foreachShadow(PASSWD_DIR_PATH SHADOW_FILE_NAME,
+    [this](const struct spwd & spwd) -> bool {
+        return spwd.sp_namp != name;
+    });
 
     try {
         home.remove(true);