@@ -116,6 +116,24 @@ static int from_init(struct su_initiator *from)
116
116
return 0 ;
117
117
}
118
118
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
+
119
137
static void socket_cleanup (void )
120
138
{
121
139
unlink (socket_path );
@@ -259,8 +277,9 @@ static void usage(int status)
259
277
"Options:\n"
260
278
" -c, --command COMMAND pass COMMAND to the invoked shell\n"
261
279
" -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"
264
283
" -s, --shell SHELL use SHELL instead of the default " DEFAULT_SHELL "\n"
265
284
" -v, --version display version number and exit\n"
266
285
" -V display version code and exit,\n"
@@ -299,6 +318,9 @@ static void allow(const struct su_context *ctx)
299
318
strcpy (p + 1 , arg0 );
300
319
arg0 = p ;
301
320
}
321
+
322
+ populate_environment (ctx );
323
+
302
324
if (setresgid (ctx -> to .uid , ctx -> to .uid , ctx -> to .uid )) {
303
325
PLOGE ("setresgid (%u)" , ctx -> to .uid );
304
326
exit (EXIT_FAILURE );
@@ -343,6 +365,7 @@ int main(int argc, char *argv[])
343
365
.to = {
344
366
.uid = AID_ROOT ,
345
367
.login = 0 ,
368
+ .keepenv = 0 ,
346
369
.shell = DEFAULT_SHELL ,
347
370
.command = NULL ,
348
371
.argv = argv ,
@@ -375,8 +398,9 @@ int main(int argc, char *argv[])
375
398
case 'l' :
376
399
ctx .to .login = 1 ;
377
400
break ;
378
- case 'm' : /* for compatibility */
401
+ case 'm' :
379
402
case 'p' :
403
+ ctx .to .keepenv = 1 ;
380
404
break ;
381
405
case 's' :
382
406
ctx .to .shell = optarg ;
0 commit comments