Add isadmin feature 36/28036/1
authorStephen Clymans <stephen.clymans@open.eurogiciel.org>
Wed, 24 Sep 2014 13:52:13 +0000 (15:52 +0200)
committerStephen Clymans <stephen.clymans@open.eurogiciel.org>
Wed, 24 Sep 2014 15:03:38 +0000 (17:03 +0200)
Add a function to know if a given uid is in the "system" group or not.
Is based on the value TZ_SYS_ADMIN_GROUP of tzplatform_config

Change-Id: If3a6647dbc2315ca1c12baf51ff7e876e75f08d2
Signed-off-by: Stephen Clymans <stephen.clymans@open.eurogiciel.org>
src/Makefile.am
src/isadmin.c [new file with mode: 0644]
src/isadmin.h [new file with mode: 0644]
src/static-api.c
src/tzplatform_config.h

index 987247aecfcda08a0ed75213d7254dfefea9e16d..932b3230e3b51794d4af040a682e407d6d47a4db 100644 (file)
@@ -30,4 +30,6 @@ dist_pkgdata_DATA = buffer.c \
                     tzplatform_config.h \
                     tzplatform_get.c \
                     passwd.h \
-                    passwd.c
+                    passwd.c \
+                    isadmin.h \
+                    isadmin.c
diff --git a/src/isadmin.c b/src/isadmin.c
new file mode 100644 (file)
index 0000000..c17a4c9
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2013 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Stephen Clymans <stephen.clymans@open.eurogiciel.org>
+ */
+ /*
+ * DISCLAIMER :
+ *  This source file and its associated header are present to maintain a 
+ * temporary solution. We need to know if an user have the privilege for
+ * a particular action.
+ * 
+ * At the end, this feature will be managed by Cynara
+ * 
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <grp.h>
+
+#include "isadmin.h"
+#include "tzplatform_variables.h"
+char is_admin(int uid) {
+       
+       struct passwd *userinfo = NULL;
+       struct group *systemgroupinfo = NULL;
+       uid_t myuid = 0;
+       gid_t system_gid = 0;
+       gid_t *groups = NULL;
+       int i, nbgroups = 0;
+       
+       
+       if(uid == -1)
+               /* Get current uid */
+               myuid = getuid();
+       else
+               myuid = uid;
+       
+       /* Get the gid of the group named "system" */
+       systemgroupinfo = getgrnam(TZ_SYS_ADMIN_GROUP);
+       if(systemgroupinfo == NULL) {
+               fprintf( stderr, "isadmin ERROR: cannot find group named \"sudo\" \n");
+               return -1;
+       }
+       
+       system_gid = admingroupinfo->gr_gid;
+       
+       /* Get all the gid of the given uid */
+       
+       userinfo = getpwuid(myuid);
+       
+       /* Need to call this function now to get the number of group to make the
+          malloc correctly sized */
+       if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) != -1) {
+               fprintf( stderr, "isadmin ERROR: cannot get number of groups\n");
+               return -1;
+       }
+       
+       groups = malloc(nbgroups * sizeof (gid_t));
+       if (groups == NULL) {
+               fprintf( stderr, "isadmin ERROR: malloc cannot allocate memory\n");
+               return -1;
+       }
+       
+       if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) == -1) {
+               fprintf( stderr, "isadmin ERROR: cannot get groups\n");
+               return -1;
+       }
+       
+       /* Check if the given uid is in the system group */
+       
+       for(i = 0 ; i < nbgroups ; i++) {
+               if(groups[i] == system_gid)
+                       return 1;
+       }
+       
+       return 0;
+}
+
diff --git a/src/isadmin.h b/src/isadmin.h
new file mode 100644 (file)
index 0000000..3e5fe0f
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2013 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Stephen Clymans <stephen.clymans@open.eurogiciel.org>
+ */
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifndef TIZEN_PLATFORM_WRAPPER_ISADMIN_H
+#define TIZEN_PLATFORM_WRAPPER_ISADMIN_H
+
+/*
+ * DISCLAIMER :
+ *  This header and its associated source file are present to maintain a 
+ * temporary solution. We need to know if an user have the privilege for
+ * a particular action.
+ * 
+ * At the end, this feature will be managed by Cynara
+ * 
+ */
+
+#include <pwd.h>
+/*
+ * Return 0 if the given uid is not in the admin group.
+ * Return 1 if the given uid is in the admin group.
+ * 
+ * If you pass the -1 value to this function it will take the current uid given
+ * by the POSIX function getuid();
+*/
+char is_admin(int uid);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* TIZEN_PLATFORM_WRAPPER_ISADMIN_H  */
+
index 054d09387f37b9cec844464861d86dd4715da234..ed5007d346c633cd887fc1a05a64732639d9707f 100644 (file)
@@ -32,6 +32,7 @@
 #include "tzplatform_config.h"
 
 #include "shared-api.h"
+#include "isadmin.h"
 
 #include "signup.inc"
 
@@ -130,6 +131,11 @@ gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatf
     return _context_getgid_tzplatform_(id, tizen_platform_config_signup, context);
 }
 
+char tzplatform_isadmin(uid_t uid) 
+{
+       return is_admin(uid);
+}
+
 #ifdef TEST
 #include <stdlib.h>
 #include <stdio.h>
index 5a982265dc4e5c4b2ec44a5615b4416f3fff99ec..6055beef8d820f23cd7ff4e4e46ab252a17d169c 100644 (file)
@@ -381,6 +381,29 @@ uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatf
 extern
 gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id);
 
+
+/*
+ Return 1 if given uid is in the system admin group (named "system")
+ Return 0 if not
+ Return -1 in case of error.
+
+ Example:
+        tzplatform_isadmin(1000)
+
+    will return 0 or 1 depends on right of given uid.
+    
+   NOTE :
+   * If you pass the -1 value to this function it will take the current uid given
+   * by the POSIX function getuid(); 
+   
+   *** WARNING : ***
+   * This is a temporary feature
+   * This will be managed by Cynara
+    
+*/
+extern
+char tzplatform_isadmin(uid_t uid);
+
 #ifdef __cplusplus
 }
 #endif