Skip to content

Commit 8afac1d

Browse files
author
git-core
committed
Setup environment for a new process image, implement -p/-m options
1 parent c97414b commit 8afac1d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

su.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ static int from_init(struct su_initiator *from)
116116
return 0;
117117
}
118118

119+
static void populate_environment(const struct su_context *ctx)
120+
{
121+
struct passwd *pw;
122+
123+
if (ctx->to.keepenv)
124+
return;
125+
126+
pw = getpwuid(ctx->to.uid);
127+
if (pw) {
128+
setenv("HOME", pw->pw_dir, 1);
129+
setenv("SHELL", ctx->to.shell, 1);
130+
if (ctx->to.login || ctx->to.uid) {
131+
setenv("USER", pw->pw_name, 1);
132+
setenv("LOGNAME", pw->pw_name, 1);
133+
}
134+
}
135+
}
136+
119137
static void socket_cleanup(void)
120138
{
121139
unlink(socket_path);
@@ -259,8 +277,9 @@ static void usage(int status)
259277
"Options:\n"
260278
" -c, --command COMMAND pass COMMAND to the invoked shell\n"
261279
" -h, --help display this help message and exit\n"
262-
" -, -l, --login, -m, -p,\n"
263-
" --preserve-environment do nothing, kept for compatibility\n"
280+
" -, -l, --login pretend the shell to be a login shell\n"
281+
" -m, -p,\n"
282+
" --preserve-environment do not change environment variables\n"
264283
" -s, --shell SHELL use SHELL instead of the default " DEFAULT_SHELL "\n"
265284
" -v, --version display version number and exit\n"
266285
" -V display version code and exit,\n"
@@ -299,6 +318,9 @@ static void allow(const struct su_context *ctx)
299318
strcpy(p + 1, arg0);
300319
arg0 = p;
301320
}
321+
322+
populate_environment(ctx);
323+
302324
if (setresgid(ctx->to.uid, ctx->to.uid, ctx->to.uid)) {
303325
PLOGE("setresgid (%u)", ctx->to.uid);
304326
exit(EXIT_FAILURE);
@@ -343,6 +365,7 @@ int main(int argc, char *argv[])
343365
.to = {
344366
.uid = AID_ROOT,
345367
.login = 0,
368+
.keepenv = 0,
346369
.shell = DEFAULT_SHELL,
347370
.command = NULL,
348371
.argv = argv,
@@ -375,8 +398,9 @@ int main(int argc, char *argv[])
375398
case 'l':
376399
ctx.to.login = 1;
377400
break;
378-
case 'm': /* for compatibility */
401+
case 'm':
379402
case 'p':
403+
ctx.to.keepenv = 1;
380404
break;
381405
case 's':
382406
ctx.to.shell = optarg;

su.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct su_initiator {
5858
struct su_request {
5959
unsigned uid;
6060
int login;
61+
int keepenv;
6162
char *shell;
6263
char *command;
6364
char **argv;

0 commit comments

Comments
 (0)