Skip to content

Commit

Permalink
setuidgid.c: added -e option for setting USER, LOGNAME and HOME env v…
Browse files Browse the repository at this point in the history
…ariables
  • Loading branch information
mbhangui committed Dec 29, 2024
1 parent ef244c9 commit 4f62d5e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
3 changes: 3 additions & 0 deletions daemontools-x/doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
- 27/12/2024
01. envdir.c, envuidgid.c: Ignore return value of pathexec()
02. setuidgid.c: Set HOME, USER, LOGNAME env variable
- 29/12/2024
03. setuidgid.c: added -e option for setting USER, LOGNAME and HOME env
variables

* Tue Dec 24 2024 20:28:14 +0000 Manvendra Bhangui <[email protected] 1.1.6-1.1%{?dist}
Release 1.1.6-1.1 Start 13/08/2024 End 24/12/2024
Expand Down
32 changes: 20 additions & 12 deletions daemontools-x/setuidgid.8
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,45 @@
.SH NAME
setuidgid \- run a program under a specified account's uid and gid.
.SH SYNOPSIS
\fBsetuidgid\fR [ \fB\-s\fR ] [ \fB\-g\fR \fIgroups\fR] \fIaccount\fR \fIchild\fR
\fBsetuidgid\fR [ \fB\-e\fR ] [ \fB\-s\fR ] [ \fB\-g\fR \fIgroups\fR]
\fIaccount\fR \fIchild\fR

.SH DESCRIPTION
\fIaccount\fR is a single argument. \fIchild\fR consists of one or more
arguments.

.TP 3
\fB-s\fR
\fB\-e\fR
Set the environment variables \fBHOME\fR, \fBUSER\fR and \fBLOGNAME\fR.

.TP
\fB\-s\fR
Set supplementary group ids

.TP
\fB-g\fR \fIgroups\fR
\fB\-g\fR \fIgroups\fR
Set additional supplementary groups from a comma separated list of groups.
\fIgroups\fR can be numeric group IDs or group IDs from /etc/groups.
\fIgroups\fR can be numeric group IDs or group IDs from \fI/etc/groups\fR.

.PP
\fBsetuidgid\fR(8) sets its uid and gid to \fIaccount\fR's uid and gid,
removing all supplementary groups. It then runs \fIchild\fR. If \-s option
is specified, \fBsetuidgid\fR adds all supplementary groups for
\fIaccount\fR in group(5). Additional groups can be supplied using \-g
option. \fBsetuidgid\fR(8) sets the environment variables \fBUSER\fR and
\fBLOGNAME\fR to \fIaccount\fR and the environment variable \fBHOME\fR to
the home directory of \fIaccount\fR in the \fBpasswd\fB(5) database.
removing all supplementary groups. It then runs \fIchild\fR. If \fB\-s\fR
option is specified, \fBsetuidgid\fR adds all supplementary groups for
\fIaccount\fR in \fBgroup\fR(5) database. Additional groups can be supplied
using \fB\-g\fR option.

If \fB\-e\fR option is passed, \fBsetuidgid\fR(8) sets the environment
variables \fBUSER\fR and \fBLOGNAME\fR to \fIaccount\fR and the environment
variable \fBHOME\fR to the home directory of \fIaccount\fR in the
\fBpasswd\fR(5) database.

\fBsetuidgid\fR(8) cannot be run by anyone other than root.

.SH EXIT CODES
\fBsetuidgid\fR(8) exits 111 if it cannot find a UNIX account named
\fIaccount\fR, if it cannot setgid, if it cannot setgroups, if it cannot
setuid, or if it cannot run \fIchild\fR. Otherwise its exit code is the
same as that of \fIchild\fR.
setuid, if it cannot allocate memory or if it cannot run \fIchild\fR.
Otherwise its exit code is the same as that of \fIchild\fR.

.SH SEE ALSO
supervise(8),
Expand Down
20 changes: 13 additions & 7 deletions daemontools-x/setuidgid.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* $Id: setuidgid.c,v 1.10 2024-12-27 01:02:09+05:30 Cprogrammer Exp mbhangui $
* $Id: setuidgid.c,v 1.11 2024-12-29 09:07:32+05:30 Cprogrammer Exp mbhangui $
*/
#include <sys/types.h>
#include <unistd.h>
Expand All @@ -25,14 +25,17 @@ main(int argc, char **argv)
struct passwd *pw;
struct group *gr;
gid_t *gidset = (gid_t *) NULL, g;
char *ptr, *cptr, *account, *groups = 0;
char *ptr, *cptr, *account, *groups = NULL;
const char *usage = "usage: setuidgid [-s] [-g gid_list] account child";
char **child;
int i, ngroups = 0, opt, old;
int i, ngroups = 0, opt, old, do_env = 0;

while ((opt = getopt(argc, argv, "sg:")) != opteof) {
while ((opt = getopt(argc, argv, "esg:")) != opteof) {
switch (opt)
{
case 'e':
do_env = 1;
break;
case 's':
ngroups = 1;
break;
Expand Down Expand Up @@ -93,8 +96,8 @@ main(int argc, char **argv)
}
if (prot_uid(pw->pw_uid) == -1)
strerr_die2sys(111, FATAL, "unable to set user id: ");
if (!pathexec_env("HOME", pw->pw_dir) || !pathexec_env("USER", account) ||
!pathexec_env("LOGNAME", account))
if (do_env && (!pathexec_env("HOME", pw->pw_dir) || !pathexec_env("USER", account) ||
!pathexec_env("LOGNAME", account)))
strerr_die2x(111, FATAL, "out of memory");
pathexec(child);
strerr_die4sys(111, FATAL, "unable to run ", *child, ": ");
Expand All @@ -103,13 +106,16 @@ main(int argc, char **argv)
void
getversion_setuidgid_c()
{
const char *x = "$Id: setuidgid.c,v 1.10 2024-12-27 01:02:09+05:30 Cprogrammer Exp mbhangui $";
const char *x = "$Id: setuidgid.c,v 1.11 2024-12-29 09:07:32+05:30 Cprogrammer Exp mbhangui $";

x++;
}

/*
* $Log: setuidgid.c,v $
* Revision 1.11 2024-12-29 09:07:32+05:30 Cprogrammer
* added -e option for setting USER, LOGNAME and HOME env variables
*
* Revision 1.10 2024-12-27 01:02:09+05:30 Cprogrammer
* Set HOME, USER, LOGNAME env variable
*
Expand Down

0 comments on commit 4f62d5e

Please sign in to comment.