commit: c1708108ad428dc66d95e2abac07ca3848ee21fb
parent cd224ae0f86b3ae35e2b79787e431667f59fccd6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 9 Oct 2022 03:24:19 +0200
su: Add -c option
Diffstat:
M | su.c | 22 | ++++++++++++++++++---- |
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/su.c b/su.c
@@ -33,6 +33,7 @@ main(int argc, char *argv[])
char *username = NULL;
struct passwd *pwent = NULL;
char *shell = NULL;
+ char *command = NULL;
if(geteuid() != 0)
{
@@ -41,10 +42,13 @@ main(int argc, char *argv[])
}
/* flawfinder: ignore CWE-120, CWE-20 */
- while((c = getopt(argc, argv, ":l:s:p")) != EOF)
+ while((c = getopt(argc, argv, ":c:l:s:p")) != EOF)
{
switch(c)
{
+ case 'c': // command
+ command = optarg;
+ break;
case 'l': // login-mode
opt_l = true;
username = optarg;
@@ -184,9 +188,19 @@ main(int argc, char *argv[])
setenv("SHELL", shell, 1);
- errno = 0;
- /* flawfinder: ignore CWE-78 */
- if(execlp(shell, shell, NULL) < 0)
+ errno = 0;
+ int ret = 0;
+ if(command != NULL)
+ {
+ /* flawfinder: ignore CWE-78 */
+ ret = execlp(shell, shell, "-c", command, NULL);
+ }
+ else
+ {
+ /* flawfinder: ignore CWE-78 */
+ ret = execlp(shell, shell, NULL);
+ }
+ if(ret < 0)
{
if(errno == ENOENT)
{