2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 09:57:41 +00:00

Add SUDO_TTY environment variable if the user has a tty

This can be used to find the user's original tty device when sudo
runs the command in its own pty.  GitHub issue #447.
This commit is contained in:
Todd C. Miller 2025-06-07 16:20:37 -06:00
parent a925829e60
commit afd01d856b
3 changed files with 12 additions and 5 deletions

View File

@ -2,7 +2,7 @@
.\" .\"
.\" SPDX-License-Identifier: ISC .\" SPDX-License-Identifier: ISC
.\" .\"
.\" Copyright (c) 1994-1996, 1998-2005, 2007-2024 .\" Copyright (c) 1994-1996, 1998-2005, 2007-2025
.\" Todd C. Miller <Todd.Miller@sudo.ws> .\" Todd C. Miller <Todd.Miller@sudo.ws>
.\" .\"
.\" Permission to use, copy, modify, and distribute this software for any .\" Permission to use, copy, modify, and distribute this software for any
@ -25,7 +25,7 @@
.nr BA @BAMAN@ .nr BA @BAMAN@
.nr LC @LCMAN@ .nr LC @LCMAN@
.nr PS @PSMAN@ .nr PS @PSMAN@
.TH "SUDO" "@mansectsu@" "December 20, 2024" "Sudo @PACKAGE_VERSION@" "System Manager's Manual" .TH "SUDO" "@mansectsu@" "June 7, 2025" "Sudo @PACKAGE_VERSION@" "System Manager's Manual"
.nh .nh
.if n .ad l .if n .ad l
.SH "NAME" .SH "NAME"
@ -1436,6 +1436,9 @@ If set,
\fRPS1\fR \fRPS1\fR
will be set to its value for the program being run. will be set to its value for the program being run.
.TP 17n .TP 17n
\fRSUDO_TTY\fR
Set to the terminal device of the user who invoked sudo, if one is present.
.TP 17n
\fRSUDO_UID\fR \fRSUDO_UID\fR
Set to the user-ID of the user who invoked sudo. Set to the user-ID of the user who invoked sudo.
.TP 17n .TP 17n

View File

@ -1,7 +1,7 @@
.\" .\"
.\" SPDX-License-Identifier: ISC .\" SPDX-License-Identifier: ISC
.\" .\"
.\" Copyright (c) 1994-1996, 1998-2005, 2007-2024 .\" Copyright (c) 1994-1996, 1998-2005, 2007-2025
.\" Todd C. Miller <Todd.Miller@sudo.ws> .\" Todd C. Miller <Todd.Miller@sudo.ws>
.\" .\"
.\" Permission to use, copy, modify, and distribute this software for any .\" Permission to use, copy, modify, and distribute this software for any
@ -24,7 +24,7 @@
.nr BA @BAMAN@ .nr BA @BAMAN@
.nr LC @LCMAN@ .nr LC @LCMAN@
.nr PS @PSMAN@ .nr PS @PSMAN@
.Dd December 20, 2024 .Dd June 7, 2025
.Dt SUDO @mansectsu@ .Dt SUDO @mansectsu@
.Os Sudo @PACKAGE_VERSION@ .Os Sudo @PACKAGE_VERSION@
.Sh NAME .Sh NAME
@ -1355,6 +1355,8 @@ option was specified.
If set, If set,
.Ev PS1 .Ev PS1
will be set to its value for the program being run. will be set to its value for the program being run.
.It Ev SUDO_TTY
Set to the terminal device of the user who invoked sudo, if one is present.
.It Ev SUDO_UID .It Ev SUDO_UID
Set to the user-ID of the user who invoked sudo. Set to the user-ID of the user who invoked sudo.
.It Ev SUDO_USER .It Ev SUDO_USER

View File

@ -1123,13 +1123,15 @@ rebuild_env(const struct sudoers_context *ctx)
CHECK_SETENV2("SUDO_COMMAND", ctx->user.cmnd, true, true); CHECK_SETENV2("SUDO_COMMAND", ctx->user.cmnd, true, true);
} }
/* Add the SUDO_{USER,UID,GID,HOME} environment variables. */ /* Add the SUDO_{USER,UID,GID,HOME,TTY,TTY} environment variables. */
CHECK_SETENV2("SUDO_USER", ctx->user.name, true, true); CHECK_SETENV2("SUDO_USER", ctx->user.name, true, true);
(void)snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) ctx->user.uid); (void)snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) ctx->user.uid);
CHECK_SETENV2("SUDO_UID", idbuf, true, true); CHECK_SETENV2("SUDO_UID", idbuf, true, true);
(void)snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) ctx->user.gid); (void)snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) ctx->user.gid);
CHECK_SETENV2("SUDO_GID", idbuf, true, true); CHECK_SETENV2("SUDO_GID", idbuf, true, true);
CHECK_SETENV2("SUDO_HOME", ctx->user.pw->pw_dir, true, true); CHECK_SETENV2("SUDO_HOME", ctx->user.pw->pw_dir, true, true);
if (ctx->user.ttypath != NULL)
CHECK_SETENV2("SUDO_TTY", ctx->user.ttypath, true, true);
debug_return_bool(true); debug_return_bool(true);