summaryrefslogtreecommitdiffstats
path: root/util.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'util.cxx')
-rw-r--r--util.cxx32
1 files changed, 32 insertions, 0 deletions
diff --git a/util.cxx b/util.cxx
index b81d37cb..319af098 100644
--- a/util.cxx
+++ b/util.cxx
@@ -22,6 +22,7 @@
extern "C" {
#include <fcntl.h>
+#include <grp.h>
#include <pwd.h>
#include <spawn.h>
#include <stdio.h>
@@ -188,6 +189,37 @@ remove_file_or_dir (const char *name)
return 0;
}
+// Determine whether the current user is in the given group
+// by gid.
+bool
+in_group_id (gid_t target_gid)
+{
+ gid_t gid, gidlist[NGROUPS_MAX];
+ int i, ngids;
+
+ // According to the getgroups() man page, getgroups() may not
+ // return the effective gid, so try to match it first. */
+ gid = getegid();
+ if (gid == target_gid)
+ return true;
+
+ // Get the list of the user's groups.
+ ngids = getgroups(NGROUPS_MAX, gidlist);
+ if (ngids < 0) {
+ cerr << "Unable to retrieve group list" << endl;
+ return false;
+ }
+
+ for (i = 0; i < ngids; i++) {
+ // If the user is a member of the target group, then we're done.
+ if (gidlist[i] == target_gid)
+ return true;
+ }
+
+ // The user is not a member of the target group
+ return false;
+}
+
void
tokenize(const string& str, vector<string>& tokens,
const string& delimiters = " ")