mirror of
https://github.com/sheumann/hush.git
synced 2025-01-13 21:31:51 +00:00
lineedit: nuke two unused variables and code which sets them
applets: do not even try to read config if run by real root msh: use named constants (O_RDONLY etc) in open() instead of magic numbers, other minor code size reduction.
This commit is contained in:
parent
8a28e620ce
commit
5f9468e996
@ -48,14 +48,15 @@ static const char usage_messages[] = ""
|
|||||||
/* The -1 arises because of the {0,NULL,0,-1} entry. */
|
/* The -1 arises because of the {0,NULL,0,-1} entry. */
|
||||||
const unsigned short NUM_APPLETS = sizeof(applets) / sizeof(applets[0]) - 1;
|
const unsigned short NUM_APPLETS = sizeof(applets) / sizeof(applets[0]) - 1;
|
||||||
|
|
||||||
|
|
||||||
const struct bb_applet *current_applet;
|
const struct bb_applet *current_applet;
|
||||||
const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
|
const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
|
||||||
#if !BB_MMU
|
#if !BB_MMU
|
||||||
bool re_execed;
|
bool re_execed;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if ENABLE_FEATURE_SUID
|
||||||
|
static uid_t ruid; /* real uid */
|
||||||
|
#endif
|
||||||
|
|
||||||
#if ENABLE_FEATURE_SUID_CONFIG
|
#if ENABLE_FEATURE_SUID_CONFIG
|
||||||
|
|
||||||
@ -143,6 +144,10 @@ static void parse_config_file(void)
|
|||||||
|
|
||||||
assert(!suid_config); /* Should be set to NULL by bss init. */
|
assert(!suid_config); /* Should be set to NULL by bss init. */
|
||||||
|
|
||||||
|
ruid = getuid();
|
||||||
|
if (ruid == 0) /* run by root - don't need to even read config file */
|
||||||
|
return;
|
||||||
|
|
||||||
if ((stat(config_file, &st) != 0) /* No config file? */
|
if ((stat(config_file, &st) != 0) /* No config file? */
|
||||||
|| !S_ISREG(st.st_mode) /* Not a regular file? */
|
|| !S_ISREG(st.st_mode) /* Not a regular file? */
|
||||||
|| (st.st_uid != 0) /* Not owned by root? */
|
|| (st.st_uid != 0) /* Not owned by root? */
|
||||||
@ -324,15 +329,21 @@ static void parse_config_file(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#define parse_config_file() ((void)0)
|
static inline void parse_config_file(void)
|
||||||
|
{
|
||||||
|
ruid = getuid();
|
||||||
|
}
|
||||||
#endif /* FEATURE_SUID_CONFIG */
|
#endif /* FEATURE_SUID_CONFIG */
|
||||||
|
|
||||||
|
|
||||||
#if ENABLE_FEATURE_SUID
|
#if ENABLE_FEATURE_SUID
|
||||||
static void check_suid(const struct bb_applet *applet)
|
static void check_suid(const struct bb_applet *applet)
|
||||||
{
|
{
|
||||||
uid_t ruid = getuid(); /* real [ug]id */
|
uid_t rgid; /* real gid */
|
||||||
uid_t rgid = getgid();
|
|
||||||
|
if (ruid == 0) /* set by parse_config_file() */
|
||||||
|
return; /* run by root - no need to check more */
|
||||||
|
rgid = getgid();
|
||||||
|
|
||||||
#if ENABLE_FEATURE_SUID_CONFIG
|
#if ENABLE_FEATURE_SUID_CONFIG
|
||||||
if (suid_cfg_readable) {
|
if (suid_cfg_readable) {
|
||||||
@ -387,7 +398,7 @@ static void check_suid(const struct bb_applet *applet)
|
|||||||
if (geteuid())
|
if (geteuid())
|
||||||
bb_error_msg_and_die("applet requires root privileges!");
|
bb_error_msg_and_die("applet requires root privileges!");
|
||||||
} else if (applet->need_suid == _BB_SUID_NEVER) {
|
} else if (applet->need_suid == _BB_SUID_NEVER) {
|
||||||
xsetgid(rgid); /* drop all privileges */
|
xsetgid(rgid); /* drop all privileges */
|
||||||
xsetuid(ruid);
|
xsetuid(ruid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -636,8 +647,7 @@ int main(int argc, char **argv)
|
|||||||
if (s)
|
if (s)
|
||||||
applet_name = s + 1;
|
applet_name = s + 1;
|
||||||
|
|
||||||
if (ENABLE_FEATURE_SUID_CONFIG)
|
parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
|
||||||
parse_config_file();
|
|
||||||
|
|
||||||
/* Set locale for everybody except 'init' */
|
/* Set locale for everybody except 'init' */
|
||||||
if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
|
if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
|
||||||
|
@ -85,11 +85,6 @@ static char *user_buf = (char*)"";
|
|||||||
static char *home_pwd_buf = (char*)"";
|
static char *home_pwd_buf = (char*)"";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ENABLE_FEATURE_TAB_COMPLETION
|
|
||||||
static int my_uid;
|
|
||||||
static int my_gid;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Put 'command_ps[cursor]', cursor++.
|
/* Put 'command_ps[cursor]', cursor++.
|
||||||
* Advance cursor on screen. If we reached right margin, scroll text up
|
* Advance cursor on screen. If we reached right margin, scroll text up
|
||||||
* and remove terminal margin effect by printing 'next_char' */
|
* and remove terminal margin effect by printing 'next_char' */
|
||||||
@ -1311,10 +1306,6 @@ int read_line_input(const char* prompt, char* command, int maxsize, line_input_t
|
|||||||
home_pwd_buf = xstrdup(entry->pw_dir);
|
home_pwd_buf = xstrdup(entry->pw_dir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
#if ENABLE_FEATURE_TAB_COMPLETION
|
|
||||||
my_uid = getuid();
|
|
||||||
my_gid = getgid();
|
|
||||||
#endif
|
#endif
|
||||||
/* Print out the command prompt */
|
/* Print out the command prompt */
|
||||||
parse_prompt(prompt);
|
parse_prompt(prompt);
|
||||||
|
67
shell/msh.c
67
shell/msh.c
@ -152,16 +152,15 @@ int mshdbg_rc = 0;
|
|||||||
/*
|
/*
|
||||||
* values returned by wait
|
* values returned by wait
|
||||||
*/
|
*/
|
||||||
#define WAITSIG(s) ((s)&0177)
|
#define WAITSIG(s) ((s) & 0177)
|
||||||
#define WAITVAL(s) (((s)>>8)&0377)
|
#define WAITVAL(s) (((s) >> 8) & 0377)
|
||||||
#define WAITCORE(s) (((s)&0200)!=0)
|
#define WAITCORE(s) (((s) & 0200) != 0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* library and system definitions
|
* library and system definitions
|
||||||
*/
|
*/
|
||||||
typedef void xint; /* base type of jmp_buf, for not broken compilers */
|
typedef void xint; /* base type of jmp_buf, for not broken compilers */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* shell components
|
* shell components
|
||||||
*/
|
*/
|
||||||
@ -170,7 +169,6 @@ typedef void xint; /* base type of jmp_buf, for not broken compilers */
|
|||||||
#define NOWORDS ((char **)NULL)
|
#define NOWORDS ((char **)NULL)
|
||||||
#define NOPIPE ((int *)NULL)
|
#define NOPIPE ((int *)NULL)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* redirection
|
* redirection
|
||||||
*/
|
*/
|
||||||
@ -250,21 +248,20 @@ static const char *const T_CMD_NAMES[] = {
|
|||||||
/*
|
/*
|
||||||
* actions determining the environment of a process
|
* actions determining the environment of a process
|
||||||
*/
|
*/
|
||||||
#define BIT(i) (1<<(i))
|
#define FEXEC 1 /* execute without forking */
|
||||||
#define FEXEC BIT(0) /* execute without forking */
|
|
||||||
|
|
||||||
#define AREASIZE (90000)
|
#define AREASIZE (90000)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* flags to control evaluation of words
|
* flags to control evaluation of words
|
||||||
*/
|
*/
|
||||||
#define DOSUB 1 /* interpret $, `, and quotes */
|
#define DOSUB 1 /* interpret $, `, and quotes */
|
||||||
#define DOBLANK 2 /* perform blank interpretation */
|
#define DOBLANK 2 /* perform blank interpretation */
|
||||||
#define DOGLOB 4 /* interpret [?* */
|
#define DOGLOB 4 /* interpret [?* */
|
||||||
#define DOKEY 8 /* move words with `=' to 2nd arg. list */
|
#define DOKEY 8 /* move words with `=' to 2nd arg. list */
|
||||||
#define DOTRIM 16 /* trim resulting string */
|
#define DOTRIM 16 /* trim resulting string */
|
||||||
|
|
||||||
#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
|
#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
|
||||||
|
|
||||||
|
|
||||||
/* PROTOTYPES */
|
/* PROTOTYPES */
|
||||||
@ -333,13 +330,13 @@ static void runtrap(int i);
|
|||||||
|
|
||||||
/* -------- area stuff -------- */
|
/* -------- area stuff -------- */
|
||||||
|
|
||||||
#define REGSIZE sizeof(struct region)
|
#define REGSIZE sizeof(struct region)
|
||||||
#define GROWBY (256)
|
#define GROWBY (256)
|
||||||
/* #define SHRINKBY (64) */
|
/* #define SHRINKBY (64) */
|
||||||
#undef SHRINKBY
|
#undef SHRINKBY
|
||||||
#define FREE (32767)
|
#define FREE (32767)
|
||||||
#define BUSY (0)
|
#define BUSY (0)
|
||||||
#define ALIGN (sizeof(int)-1)
|
#define ALIGN (sizeof(int)-1)
|
||||||
|
|
||||||
|
|
||||||
struct region {
|
struct region {
|
||||||
@ -1313,7 +1310,7 @@ static int newfile(char *s)
|
|||||||
f = 0;
|
f = 0;
|
||||||
if (NOT_LONE_DASH(s)) {
|
if (NOT_LONE_DASH(s)) {
|
||||||
DBGPRINTF(("NEWFILE: s is %s\n", s));
|
DBGPRINTF(("NEWFILE: s is %s\n", s));
|
||||||
f = open(s, 0);
|
f = open(s, O_RDONLY);
|
||||||
if (f < 0) {
|
if (f < 0) {
|
||||||
prs(s);
|
prs(s);
|
||||||
err(": cannot open");
|
err(": cannot open");
|
||||||
@ -2554,7 +2551,7 @@ static int execute(struct op *t, int *pin, int *pout, int act)
|
|||||||
interactive = 0;
|
interactive = 0;
|
||||||
if (pin == NULL) {
|
if (pin == NULL) {
|
||||||
close(0);
|
close(0);
|
||||||
open(bb_dev_null, 0);
|
open(bb_dev_null, O_RDONLY);
|
||||||
}
|
}
|
||||||
_exit(execute(t->left, pin, pout, FEXEC));
|
_exit(execute(t->left, pin, pout, FEXEC));
|
||||||
}
|
}
|
||||||
@ -2734,7 +2731,8 @@ static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp)
|
|||||||
resetsig = 0;
|
resetsig = 0;
|
||||||
rv = -1; /* system-detected error */
|
rv = -1; /* system-detected error */
|
||||||
if (t->type == TCOM) {
|
if (t->type == TCOM) {
|
||||||
while (*wp++ != NULL);
|
while (*wp++ != NULL)
|
||||||
|
continue;
|
||||||
cp = *wp;
|
cp = *wp;
|
||||||
|
|
||||||
/* strip all initial assignments */
|
/* strip all initial assignments */
|
||||||
@ -2747,7 +2745,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp)
|
|||||||
|
|
||||||
if (cp == NULL && t->ioact == NULL) {
|
if (cp == NULL && t->ioact == NULL) {
|
||||||
while ((cp = *owp++) != NULL && assign(cp, COPYV))
|
while ((cp = *owp++) != NULL && assign(cp, COPYV))
|
||||||
/**/;
|
continue;
|
||||||
DBGPRINTF(("FORKEXEC: returning setstatus()\n"));
|
DBGPRINTF(("FORKEXEC: returning setstatus()\n"));
|
||||||
return setstatus(0);
|
return setstatus(0);
|
||||||
}
|
}
|
||||||
@ -2932,7 +2930,7 @@ static int iosetup(struct ioword *iop, int pipein, int pipeout)
|
|||||||
}
|
}
|
||||||
switch (iop->io_flag) {
|
switch (iop->io_flag) {
|
||||||
case IOREAD:
|
case IOREAD:
|
||||||
u = open(cp, 0);
|
u = open(cp, O_RDONLY);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IOHERE:
|
case IOHERE:
|
||||||
@ -2942,7 +2940,7 @@ static int iosetup(struct ioword *iop, int pipein, int pipeout)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case IOWRITE | IOCAT:
|
case IOWRITE | IOCAT:
|
||||||
u = open(cp, 1);
|
u = open(cp, O_WRONLY);
|
||||||
if (u >= 0) {
|
if (u >= 0) {
|
||||||
lseek(u, (long) 0, SEEK_END);
|
lseek(u, (long) 0, SEEK_END);
|
||||||
break;
|
break;
|
||||||
@ -3346,7 +3344,7 @@ static int dodot(struct op *t)
|
|||||||
for (i = 0; (*tp++ = cp[i++]) != '\0';);
|
for (i = 0; (*tp++ = cp[i++]) != '\0';);
|
||||||
|
|
||||||
/* Original code */
|
/* Original code */
|
||||||
i = open(e.linep, 0);
|
i = open(e.linep, O_RDONLY);
|
||||||
if (i >= 0) {
|
if (i >= 0) {
|
||||||
exstat = 0;
|
exstat = 0;
|
||||||
maltmp = remap(i);
|
maltmp = remap(i);
|
||||||
@ -5098,7 +5096,7 @@ static int herein(char *hname, int xdoll)
|
|||||||
|
|
||||||
DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll));
|
DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll));
|
||||||
|
|
||||||
hf = open(hname, 0);
|
hf = open(hname, O_RDONLY);
|
||||||
if (hf < 0)
|
if (hf < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -5122,7 +5120,7 @@ static int herein(char *hname, int xdoll)
|
|||||||
} else
|
} else
|
||||||
unlink(tname);
|
unlink(tname);
|
||||||
close(tf);
|
close(tf);
|
||||||
tf = open(tname, 0);
|
tf = open(tname, O_RDONLY);
|
||||||
unlink(tname);
|
unlink(tname);
|
||||||
return tf;
|
return tf;
|
||||||
}
|
}
|
||||||
@ -5214,10 +5212,11 @@ int msh_main(int argc, char **argv)
|
|||||||
|
|
||||||
path = lookup("PATH");
|
path = lookup("PATH");
|
||||||
if (path->value == null) {
|
if (path->value == null) {
|
||||||
|
/* Can be merged with same string elsewhere in bbox */
|
||||||
if (geteuid() == 0)
|
if (geteuid() == 0)
|
||||||
setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
|
setval(path, "/sbin:/usr/sbin:/bin:/usr/bin");
|
||||||
else
|
else
|
||||||
setval(path, "/bin:/usr/bin");
|
setval(path, "/sbin:/usr/sbin:/bin:/usr/bin" + sizeof("/sbin:/usr/sbin"));
|
||||||
}
|
}
|
||||||
export(path);
|
export(path);
|
||||||
|
|
||||||
@ -5329,10 +5328,10 @@ int msh_main(int argc, char **argv)
|
|||||||
signal(SIGQUIT, qflag);
|
signal(SIGQUIT, qflag);
|
||||||
if (name && name[0] == '-') {
|
if (name && name[0] == '-') {
|
||||||
interactive++;
|
interactive++;
|
||||||
f = open(".profile", 0);
|
f = open(".profile", O_RDONLY);
|
||||||
if (f >= 0)
|
if (f >= 0)
|
||||||
next(remap(f));
|
next(remap(f));
|
||||||
f = open("/etc/profile", 0);
|
f = open("/etc/profile", O_RDONLY);
|
||||||
if (f >= 0)
|
if (f >= 0)
|
||||||
next(remap(f));
|
next(remap(f));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user