[go: up one dir, main page]

File: popen_as_ugid.h

package info (click to toggle)
smartmontools 7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,740 kB
  • sloc: cpp: 49,345; ansic: 9,924; sh: 6,043; makefile: 962
file content (39 lines) | stat: -rw-r--r-- 1,480 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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