summaryrefslogtreecommitdiffstats
path: root/popen_as_ugid.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 17:14:45 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 17:14:45 +0000
commit43e8530e93493bb978c446a2023134bdd4277e50 (patch)
treee8c0d3c0c394b17381f48fb2d288f166b4f22440 /popen_as_ugid.h
parentInitial commit. (diff)
downloadsmartmontools-43e8530e93493bb978c446a2023134bdd4277e50.tar.xz
smartmontools-43e8530e93493bb978c446a2023134bdd4277e50.zip
Adding upstream version 7.4.upstream/7.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'popen_as_ugid.h')
-rw-r--r--popen_as_ugid.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/popen_as_ugid.h b/popen_as_ugid.h
new file mode 100644
index 0000000..d697274
--- /dev/null
+++ b/popen_as_ugid.h
@@ -0,0 +1,39 @@
+/*
+ * popen_as_ugid.h
+ *
+ * Home page of code is: https://www.smartmontools.org
+ *
+ * Copyright (C) 2021 Christian Franke
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef POPEN_AS_UGID_H_CVSID
+#define POPEN_AS_UGID_H_CVSID "$Id: popen_as_ugid.h 5268 2021-12-13 18:55:13Z chrfranke $"
+
+#include <grp.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <string>
+
+// Wrapper for popen(3) which prevents that unneeded file descriptors
+// are inherited to the command run by popen() and optionally drops
+// privileges of root user:
+// If uid != 0, popen() is run as this user.
+// If gid != 0, popen() is run as this group and no supplemental groups.
+// Only mode "r" is supported. Only one open stream at a time is supported.
+FILE * popen_as_ugid(const char * cmd, const char * mode, uid_t uid, gid_t gid);
+
+// Call corresponding pclose(3) and return its result.
+int pclose_as_ugid(FILE * f);
+
+// Parse "USER[:GROUP]" string and set uid, gid, uname and gname accordingly.
+// USER and GROUP may be specified as numeric ids or names.
+// If a numeric id is used and the corresponding user (or group) does not
+// exist, the function succeeds but leaves uname (or gname) unchanged.
+// If no GROUP is specified, the default group of USER is used instead.
+// Returns nullptr on success or a message string on error.
+const char * parse_ugid(const char * s, uid_t & uid, gid_t & gid,
+ std::string & uname, std::string & gname );
+
+#endif // POPEN_AS_UGID_H_CVSID