mirror of
https://github.com/sheumann/hush.git
synced 2024-10-31 19:04:47 +00:00
73c571a5ff
function old new delta get_sock_lsa - 72 +72 buffer_fill_and_print 179 196 +17 parse_expr 824 832 +8 read_base64 343 348 +5 nameval 202 206 +4 fbset_main 1694 1698 +4 expand 1849 1853 +4 udhcp_send_kernel_packet 249 252 +3 udhcp_get_option 223 222 -1 chat_main 1246 1245 -1 pack_gzip 1661 1659 -2 doset 299 297 -2 bb__parsespent 119 117 -2 test_main 260 257 -3 qgravechar 109 106 -3 tcpudpsvd_main 1834 1830 -4 sysctl_display_all 589 580 -9 xopen_xwrite_close 44 33 -11 prs 30 18 -12 find_main 418 406 -12 full_write2_str 25 12 -13 adduser_main 667 654 -13 evaltreenr 817 802 -15 evaltree 817 802 -15 tftpd_main 526 493 -33 ftpd_main 2050 1990 -60 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 7/18 up/down: 117/-211) Total: -94 bytes
261 lines
6.0 KiB
C
261 lines
6.0 KiB
C
/* vi: set sw=4 ts=4: */
|
|
/*
|
|
* Sysctl 1.01 - A utility to read and manipulate the sysctl parameters
|
|
*
|
|
* Copyright 1999 George Staikos
|
|
*
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
*
|
|
* Changelog:
|
|
* v1.01:
|
|
* - added -p <preload> to preload values from a file
|
|
* v1.01.1
|
|
* - busybox applet aware by <solar@gentoo.org>
|
|
*
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
static int sysctl_act_on_setting(char *setting);
|
|
static int sysctl_display_all(const char *path);
|
|
static int sysctl_handle_preload_file(const char *filename);
|
|
static void sysctl_dots_to_slashes(char *name);
|
|
|
|
enum {
|
|
FLAG_SHOW_KEYS = 1 << 0,
|
|
FLAG_SHOW_KEY_ERRORS = 1 << 1,
|
|
FLAG_TABLE_FORMAT = 1 << 2, /* not implemented */
|
|
FLAG_SHOW_ALL = 1 << 3,
|
|
FLAG_PRELOAD_FILE = 1 << 4,
|
|
FLAG_WRITE = 1 << 5,
|
|
};
|
|
|
|
int sysctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
int sysctl_main(int argc UNUSED_PARAM, char **argv)
|
|
{
|
|
int retval;
|
|
int opt;
|
|
|
|
opt = getopt32(argv, "+neAapw"); /* '+' - stop on first non-option */
|
|
argv += optind;
|
|
opt ^= (FLAG_SHOW_KEYS | FLAG_SHOW_KEY_ERRORS);
|
|
option_mask32 = opt;
|
|
|
|
if (opt & FLAG_PRELOAD_FILE) {
|
|
option_mask32 |= FLAG_WRITE;
|
|
/* xchdir("/proc/sys") is inside */
|
|
return sysctl_handle_preload_file(*argv ? *argv : "/etc/sysctl.conf");
|
|
}
|
|
xchdir("/proc/sys");
|
|
/* xchroot(".") - if you are paranoid */
|
|
if (opt & (FLAG_TABLE_FORMAT | FLAG_SHOW_ALL)) {
|
|
return sysctl_display_all(".");
|
|
}
|
|
|
|
retval = 0;
|
|
while (*argv) {
|
|
sysctl_dots_to_slashes(*argv);
|
|
retval |= sysctl_display_all(*argv);
|
|
argv++;
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
/* Set sysctl's from a conf file. Format example:
|
|
* # Controls IP packet forwarding
|
|
* net.ipv4.ip_forward = 0
|
|
*/
|
|
static int sysctl_handle_preload_file(const char *filename)
|
|
{
|
|
char *token[2];
|
|
parser_t *parser;
|
|
|
|
parser = config_open(filename);
|
|
/* Must do it _after_ config_open(): */
|
|
xchdir("/proc/sys");
|
|
/* xchroot(".") - if you are paranoid */
|
|
|
|
// TODO: ';' is comment char too
|
|
while (config_read(parser, token, 2, 2, "# \t=", PARSE_NORMAL)) {
|
|
/* Save ~4 bytes by using parser internals */
|
|
/* parser->line is big enough for sprintf */
|
|
sprintf(parser->line, "%s=%s", token[0], token[1]);
|
|
sysctl_dots_to_slashes(parser->line);
|
|
sysctl_display_all(parser->line);
|
|
}
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
config_close(parser);
|
|
return 0;
|
|
}
|
|
|
|
static int sysctl_act_on_setting(char *setting)
|
|
{
|
|
int fd, retval = EXIT_SUCCESS;
|
|
char *cptr, *outname;
|
|
char *value = value; /* for compiler */
|
|
|
|
outname = xstrdup(setting);
|
|
|
|
cptr = outname;
|
|
while (*cptr) {
|
|
if (*cptr == '/')
|
|
*cptr = '.';
|
|
cptr++;
|
|
}
|
|
|
|
if (option_mask32 & FLAG_WRITE) {
|
|
cptr = strchr(setting, '=');
|
|
if (cptr == NULL) {
|
|
bb_error_msg("error: '%s' must be of the form name=value",
|
|
outname);
|
|
retval = EXIT_FAILURE;
|
|
goto end;
|
|
}
|
|
value = cptr + 1; /* point to the value in name=value */
|
|
if (setting == cptr || !*value) {
|
|
bb_error_msg("error: malformed setting '%s'", outname);
|
|
retval = EXIT_FAILURE;
|
|
goto end;
|
|
}
|
|
*cptr = '\0';
|
|
fd = open(setting, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
|
} else {
|
|
fd = open(setting, O_RDONLY);
|
|
}
|
|
|
|
if (fd < 0) {
|
|
switch (errno) {
|
|
case ENOENT:
|
|
if (option_mask32 & FLAG_SHOW_KEY_ERRORS)
|
|
bb_error_msg("error: '%s' is an unknown key", outname);
|
|
break;
|
|
default:
|
|
bb_perror_msg("error %sing key '%s'",
|
|
option_mask32 & FLAG_WRITE ?
|
|
"sett" : "read",
|
|
outname);
|
|
break;
|
|
}
|
|
retval = EXIT_FAILURE;
|
|
goto end;
|
|
}
|
|
|
|
if (option_mask32 & FLAG_WRITE) {
|
|
xwrite_str(fd, value);
|
|
close(fd);
|
|
if (option_mask32 & FLAG_SHOW_KEYS)
|
|
printf("%s = ", outname);
|
|
puts(value);
|
|
} else {
|
|
char c;
|
|
|
|
value = cptr = xmalloc_read(fd, NULL);
|
|
close(fd);
|
|
if (value == NULL) {
|
|
bb_perror_msg("error reading key '%s'", outname);
|
|
goto end;
|
|
}
|
|
|
|
/* dev.cdrom.info and sunrpc.transports, for example,
|
|
* are multi-line. Try "sysctl sunrpc.transports"
|
|
*/
|
|
while ((c = *cptr) != '\0') {
|
|
if (option_mask32 & FLAG_SHOW_KEYS)
|
|
printf("%s = ", outname);
|
|
while (1) {
|
|
fputc(c, stdout);
|
|
cptr++;
|
|
if (c == '\n')
|
|
break;
|
|
c = *cptr;
|
|
if (c == '\0')
|
|
break;
|
|
}
|
|
}
|
|
free(value);
|
|
}
|
|
end:
|
|
free(outname);
|
|
return retval;
|
|
}
|
|
|
|
static int sysctl_display_all(const char *path)
|
|
{
|
|
DIR *dirp;
|
|
struct stat buf;
|
|
struct dirent *entry;
|
|
char *next;
|
|
int retval = 0;
|
|
|
|
stat(path, &buf);
|
|
if (S_ISDIR(buf.st_mode) && !(option_mask32 & FLAG_WRITE)) {
|
|
dirp = opendir(path);
|
|
if (dirp == NULL)
|
|
return -1;
|
|
while ((entry = readdir(dirp)) != NULL) {
|
|
next = concat_subpath_file(
|
|
path, entry->d_name);
|
|
if (next == NULL)
|
|
continue; /* d_name is "." or ".." */
|
|
/* if path was ".", drop "./" prefix: */
|
|
retval |= sysctl_display_all((next[0] == '.' && next[1] == '/') ?
|
|
next + 2 : next);
|
|
free(next);
|
|
}
|
|
closedir(dirp);
|
|
} else {
|
|
char *name = xstrdup(path);
|
|
retval |= sysctl_act_on_setting(name);
|
|
free(name);
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
static void sysctl_dots_to_slashes(char *name)
|
|
{
|
|
char *cptr, *last_good, *end;
|
|
|
|
/* Convert minimum number of '.' to '/' so that
|
|
* we end up with existing file's name.
|
|
*
|
|
* Example from bug 3894:
|
|
* net.ipv4.conf.eth0.100.mc_forwarding ->
|
|
* net/ipv4/conf/eth0.100/mc_forwarding
|
|
* NB: net/ipv4/conf/eth0/mc_forwarding *also exists*,
|
|
* therefore we must start from the end, and if
|
|
* we replaced even one . -> /, start over again,
|
|
* but never replace dots before the position
|
|
* where last replacement occurred.
|
|
*
|
|
* Another bug we later had is that
|
|
* net.ipv4.conf.eth0.100
|
|
* (without .mc_forwarding) was mishandled.
|
|
*
|
|
* To set up testing: modprobe 8021q; vconfig add eth0 100
|
|
*/
|
|
end = name + strlen(name);
|
|
last_good = name - 1;
|
|
*end = '.'; /* trick the loop into trying full name too */
|
|
|
|
again:
|
|
cptr = end;
|
|
while (cptr > last_good) {
|
|
if (*cptr == '.') {
|
|
*cptr = '\0';
|
|
//bb_error_msg("trying:'%s'", name);
|
|
if (access(name, F_OK) == 0) {
|
|
if (cptr != end) /* prevent trailing '/' */
|
|
*cptr = '/';
|
|
//bb_error_msg("replaced:'%s'", name);
|
|
last_good = cptr;
|
|
goto again;
|
|
}
|
|
*cptr = '.';
|
|
}
|
|
cptr--;
|
|
}
|
|
*end = '\0';
|
|
}
|