diff options
author | Rob Landley <rob@landley.net> | 2012-08-25 14:25:22 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-08-25 14:25:22 -0500 |
commit | 3a9241add947cb6d24b5de7a8927517426a78795 (patch) | |
tree | d122ab6570439cd6b17c7d73ed8d4e085e0b8a95 /toys/lsb | |
parent | 689f095bc976417bf50810fe59a3b3ac32b21105 (diff) | |
download | toybox-3a9241add947cb6d24b5de7a8927517426a78795.tar.gz |
Move commands into "posix", "lsb", and "other" menus/directories.
Diffstat (limited to 'toys/lsb')
-rw-r--r-- | toys/lsb/dmesg.c | 59 | ||||
-rw-r--r-- | toys/lsb/hostname.c | 33 | ||||
-rw-r--r-- | toys/lsb/killall.c | 79 | ||||
-rw-r--r-- | toys/lsb/mknod.c | 51 | ||||
-rw-r--r-- | toys/lsb/mktemp.c | 54 | ||||
-rw-r--r-- | toys/lsb/passwd.c | 274 | ||||
-rw-r--r-- | toys/lsb/pidof.c | 32 | ||||
-rw-r--r-- | toys/lsb/seq.c | 48 | ||||
-rw-r--r-- | toys/lsb/sync.c | 25 |
9 files changed, 655 insertions, 0 deletions
diff --git a/toys/lsb/dmesg.c b/toys/lsb/dmesg.c new file mode 100644 index 00000000..95b023d6 --- /dev/null +++ b/toys/lsb/dmesg.c @@ -0,0 +1,59 @@ +/* vi: set sw=4 ts=4: + * + * dmesg.c - display/control kernel ring buffer. + * + * Copyright 2006, 2007 Rob Landley <rob@landley.net> + * + * Not in SUSv3. + +USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN)) + +config DMESG + bool "dmesg" + default y + help + usage: dmesg [-n level] [-s bufsize] | -c + + Print or control the kernel ring buffer. + + -n Set kernel logging level (1-9). + -s Size of buffer to read (in bytes), default 16384. + -c Clear the ring buffer after printing. +*/ + +#include "toys.h" +#include <sys/klog.h> + +DEFINE_GLOBALS( + long level; + long size; +) + +#define TT this.dmesg + +void dmesg_main(void) +{ + // For -n just tell kernel to which messages to keep. + if (toys.optflags & 2) { + if (klogctl(8, NULL, TT.level)) + error_exit("klogctl"); + } else { + int size, i, last = '\n'; + char *data; + + // Figure out how much data we need, and fetch it. + size = TT.size; + if (size<2) size = 16384; + data = xmalloc(size); + size = klogctl(3 + (toys.optflags&1), data, size); + if (size < 0) error_exit("klogctl"); + + // Display data, filtering out level markers. + for (i=0; i<size; ) { + if (last=='\n' && data[i]=='<') i += 3; + else xputc(last = data[i++]); + } + if (last!='\n') xputc('\n'); + if (CFG_TOYBOX_FREE) free(data); + } +} diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c new file mode 100644 index 00000000..5f1c20e7 --- /dev/null +++ b/toys/lsb/hostname.c @@ -0,0 +1,33 @@ +/* vi: set sw=4 ts=4: + * + * hostname.c - Get/Set the hostname + * + * Copyright 2012 Andre Renaud <andre@bluewatersys.com> + * + * Not in SUSv4. + +USE_HOSTNAME(NEWTOY(hostname, NULL, TOYFLAG_BIN)) + +config HOSTNAME + bool "hostname" + default n + help + usage: hostname [newname] + + Get/Set the current hostname +*/ + +#include "toys.h" + +void hostname_main(void) +{ + const char *hostname = toys.optargs[0]; + if (hostname) { + if (sethostname(hostname, strlen(hostname))) + perror_exit("cannot set hostname to '%s'", hostname); + } else { + if (gethostname(toybuf, sizeof(toybuf))) + perror_exit("cannot get hostname"); + xputs(toybuf); + } +} diff --git a/toys/lsb/killall.c b/toys/lsb/killall.c new file mode 100644 index 00000000..b4e06944 --- /dev/null +++ b/toys/lsb/killall.c @@ -0,0 +1,79 @@ +/* vi: set sw=4 ts=4: + * + * killall.c - Send signal (default: TERM) to all processes with given names. + * + * Copyright 2012 Andreas Heck <aheck@gmx.de> + * + * Not in SUSv4. + +USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN)) + +config KILLALL + bool "killall" + default y + help + usage: killall [-l] [-q] [-SIG] PROCESS_NAME... + + Send a signal (default: TERM) to all processes with the given names. + + -l print list of all available signals + -q don't print any warnings or error messages +*/ + +#include "toys.h" + +#define FLAG_q 1 +#define FLAG_l 2 + +DEFINE_GLOBALS( + int signum; +) +#define TT this.killall + +static void kill_process(pid_t pid) +{ + int ret; + + toys.exitval = 0; + ret = kill(pid, TT.signum); + + if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill"); +} + +void killall_main(void) +{ + char **names; + + if (toys.optflags & FLAG_l) { + sig_to_num(NULL); + return; + } + + TT.signum = SIGTERM; + toys.exitval++; + + if (!*toys.optargs) { + toys.exithelp = 1; + error_exit("Process name missing!"); + } + + names = toys.optargs; + + if (**names == '-') { + if (0 > (TT.signum = sig_to_num((*names)+1))) { + if (toys.optflags & FLAG_q) exit(1); + error_exit("Invalid signal"); + } + names++; + + if (!*names) { + toys.exithelp++; + error_exit("Process name missing!"); + } + } + + for_each_pid_with_name_in(names, kill_process); + + if (toys.exitval && !(toys.optflags & FLAG_q)) + error_exit("No such process"); +} diff --git a/toys/lsb/mknod.c b/toys/lsb/mknod.c new file mode 100644 index 00000000..fb727bc3 --- /dev/null +++ b/toys/lsb/mknod.c @@ -0,0 +1,51 @@ +/* vi: set sw=4 ts=4: + * + * mknod.c - make block or character special file + * + * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> + * + * Not in SUSv3. + +USE_MKNOD(NEWTOY(mknod, "<2>4", TOYFLAG_BIN)) + +config MKNOD + bool "mknod" + default y + help + usage: mknod NAME TYPE [MAJOR MINOR] + + Create a special file NAME with a given type, possible types are + b create a block device with the given MAJOR/MINOR + c or u create a character device with the given MAJOR/MINOR + p create a named pipe ignoring MAJOR/MINOR +*/ + +#include "toys.h" + +static const char modes_char[] = {'p', 'c', 'u', 'b'}; +static const mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK}; + +void mknod_main(void) +{ + int major=0, minor=0, type; + char * tmp; + int mode = 0660; + + tmp = strchr(modes_char, toys.optargs[1][0]); + if (!tmp) + perror_exit("unknown special device type %c", toys.optargs[1][0]); + + type = modes[tmp-modes_char]; + + if (type == S_IFCHR || type == S_IFBLK) { + if (toys.optc != 4) + perror_exit("creating a block/char device requires major/minor"); + + major = atoi(toys.optargs[2]); + minor = atoi(toys.optargs[3]); + } + + if (mknod(toys.optargs[0], mode | type, makedev(major, minor))) + perror_exit("mknod %s failed", toys.optargs[0]); + +} diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c new file mode 100644 index 00000000..e82256de --- /dev/null +++ b/toys/lsb/mktemp.c @@ -0,0 +1,54 @@ +/* vi: set sw=4 ts=4: + * + * mktemp.c - Create a temporary file or directory. + * + * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> + * + * Not in SUSv4. + +USE_MKTEMP(NEWTOY(mktemp, ">1(directory)d(tmpdir)p:", TOYFLAG_BIN)) + +config MKTEMP + bool "mktemp" + default y + help + usage: mktemp [OPTION] [TEMPLATE] + + Safely create a temporary file or directory and print its name. + TEMPLATE should end in 6 consecutive X's, the default + template is tmp.XXXXXX and the default directory is /tmp/. + + -d, --directory Create a directory, instead of a file + -p DIR, --tmpdir=DIR Use DIR as a base path + +*/ + +#include "toys.h" + +DEFINE_GLOBALS( + char * tmpdir; +) +#define TT this.mktemp + +void mktemp_main(void) +{ + int d_flag = toys.optflags & 2; + char *tmp, *path; + + tmp = *toys.optargs; + if (!tmp) tmp = "tmp.XXXXXX"; + if (!TT.tmpdir) TT.tmpdir = "/tmp/"; + + tmp = xmsprintf("%s/%s", TT.tmpdir, tmp); + + if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1) + perror_exit("Failed to create temporary %s", + d_flag ? "directory" : "file"); + + xputs(path = xrealpath(tmp)); + + if (CFG_TOYBOX_FREE) { + free(path); + free(tmp); + } +} diff --git a/toys/lsb/passwd.c b/toys/lsb/passwd.c new file mode 100644 index 00000000..122cd942 --- /dev/null +++ b/toys/lsb/passwd.c @@ -0,0 +1,274 @@ +/* vi: set sw=4 ts=4: + * + * passwd.c - Program to upadte user password. + * + * Copyright 2012 Ashwini Kumar <ak.ashwini@gmail.com> + * Modified 2012 Jason Kyungwan Han <asura321@gmail.com> + * + * Not in SUSv4. + +USE_PASSWD(NEWTOY(passwd, ">1a:dlu", TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN)) + +config PASSWD + bool "passwd" + default y + help + usage: passwd [-a ALGO] [-d] [-l] [-u] <account name> + + update user’s authentication tokens. Default : current user + + -a ALGO Encryption method (des, md5, sha256, sha512) default: des + -d Set password to '' + -l Lock (disable) account + -u Unlock (enable) account + +*/ + +#include "toys.h" +#include <time.h> + + +DEFINE_GLOBALS( + char *algo; +) + +#define TT this.passwd + +#define FLAG_u (1 << 0) +#define FLAG_l (1 << 1) +#define FLAG_d (1 << 2) +#define FLAG_a (1 << 3) + +#define MAX_SALT_LEN 20 //3 for id, 16 for key, 1 for '\0' +#define URANDOM_PATH "/dev/urandom" + +#ifndef _GNU_SOURCE +char *strcasestr(const char *haystack, const char *needle); +#endif + +unsigned int random_number_generator(int fd) +{ + unsigned int randnum; + xreadall(fd, &randnum, sizeof(randnum)); + return randnum; +} + + + +char inttoc(int i) +{ + // salt value uses 64 chracters in "./0-9a-zA-Z" + const char character_set[]="./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + i &= 0x3f; // masking for using 10 bits only + return character_set[i]; +} + +int get_salt(char *salt) +{ + int i, salt_length = 0; + int randfd; + if(!strncmp(TT.algo,"des",3)){ + // 2 bytes salt value is used in des + salt_length = 2; + } else { + *salt++ = '$'; + if(!strncmp(TT.algo,"md5",3)){ + *salt++ = '1'; + // 8 bytes salt value is used in md5 + salt_length = 8; + } else if(!strncmp(TT.algo,"sha256",6)){ + *salt++ = '5'; + // 16 bytes salt value is used in sha256 + salt_length = 16; + } else if(!strncmp(TT.algo,"sha512",6)){ + *salt++ = '6'; + // 16 bytes salt value is used in sha512 + salt_length = 16; + } else return 1; + + *salt++ = '$'; + } + + randfd = xopen(URANDOM_PATH, O_RDONLY); + for(i=0; i<salt_length; i++) + salt[i] = inttoc(random_number_generator(randfd)); + salt[salt_length+1] = '\0'; + xclose(randfd); + + return 0; +} + +static int str_check(char *s, char *p) +{ + if((strcasestr(s, p) != NULL) || (strcasestr(p, s) != NULL)) + return 1; + return 0; +} + +static void strength_check(char *newp, char *oldp, char *user) +{ + char *msg = NULL; + if(strlen(newp) < 6) { //Min passwd len + msg = "too short"; + xprintf("BAD PASSWORD: %s\n",msg); + } + if(!newp[0]) + return; //passwd is empty + + if(str_check(newp, user)) { + msg = "user based password"; + xprintf("BAD PASSWORD: %s\n",msg); + } + + if(oldp[0] && str_check(newp, oldp)) { + msg = "based on old passwd"; + xprintf("BAD PASSWORD: %s\n",msg); + } +} + +static int verify_passwd(char * pwd) +{ + char * pass; + + if (!pwd) return 1; + if (pwd[0] == '!' || pwd[0] == '*') return 1; + + pass = crypt(toybuf, pwd); + if (pass != NULL && strcmp(pass, pwd)==0) + return 0; + + return 1; +} + +static char *new_password(char *oldp, char *user) +{ + char *newp = NULL; + + if(read_password(toybuf, sizeof(toybuf), "New password:")) + return NULL; //may be due to Ctrl-C + + newp = xstrdup(toybuf); + strength_check(newp, oldp, user); + if(read_password(toybuf, sizeof(toybuf), "Retype password:")) { + free(newp); + return NULL; //may be due to Ctrl-C + } + + if(strcmp(newp, toybuf) == 0) + return newp; + else error_msg("Passwords do not match.\n"); + /*Failure Case */ + free(newp); + return NULL; +} + + +void passwd_main(void) +{ + uid_t myuid; + struct passwd *pw; + struct spwd *sp; + char *name = NULL; + char *pass = NULL, *encrypted = NULL, *newp = NULL; + char *orig = (char *)""; + char salt[MAX_SALT_LEN]; + int ret = -1; + + myuid = getuid(); + if((myuid != 0) && (toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) + error_exit("You need to be root to do these actions\n"); + + pw = getpwuid(myuid); + + if(!pw) + error_exit("Unknown uid '%u'",myuid); + + if(toys.optargs[0]) + name = toys.optargs[0]; + else + name = xstrdup(pw->pw_name); + + pw = getpwnam(name); + if(!pw) error_exit("Unknown user '%s'",name); + + if(myuid != 0 && (myuid != pw->pw_uid)) + error_exit("You need to be root to change '%s' password\n", name); + + pass = pw->pw_passwd; + if(pw->pw_passwd[0] == 'x') { + /*get shadow passwd */ + sp = getspnam(name); + if(sp) + pass = sp->sp_pwdp; + } + + + if(!(toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) { + printf("Changing password for %s\n",name); + if(pass[0] == '!') + error_exit("Can't change, password is locked for %s",name); + if(myuid != 0) { + /*Validate user */ + + if(read_password(toybuf, sizeof(toybuf), "Origial password:")) { + if(!toys.optargs[0]) free(name); + return; + } + orig = toybuf; + if(verify_passwd(pass)) + error_exit("Authentication failed\n"); + } + + orig = xstrdup(orig); + + /*Get new password */ + newp = new_password(orig, name); + if(!newp) { + free(orig); + if(!toys.optargs[0]) free(name); + return; //new password is not set well. + } + + /*Encrypt the passwd */ + if(!(toys.optflags & FLAG_a)) TT.algo = "des"; + + if(get_salt(salt)) + error_exit("Error: Unkown encryption algorithm\n"); + + encrypted = crypt(newp, salt); + free(newp); + free(orig); + } + else if(toys.optflags & FLAG_l) { + if(pass[0] == '!') + error_exit("password is already locked for %s",name); + printf("Locking password for %s\n",name); + encrypted = xmsprintf("!%s",pass); + } + else if(toys.optflags & FLAG_u) { + if(pass[0] != '!') + error_exit("password is already unlocked for %s",name); + + printf("Unlocking password for %s\n",name); + encrypted = xstrdup(&pass[1]); + } + else if(toys.optflags & FLAG_d) { + printf("Deleting password for %s\n",name); + encrypted = (char*)xzalloc(sizeof(char)*2); //1 = "", 2 = '\0' + } + + /*Update the passwd */ + if(pw->pw_passwd[0] == 'x') + ret = update_password("/etc/shadow", name, encrypted); + else + ret = update_password("/etc/passwd", name, encrypted); + + if((toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) + free(encrypted); + + if(!toys.optargs[0]) free(name); + if(!ret) + error_msg("Success"); + else + error_msg("Failure"); +} diff --git a/toys/lsb/pidof.c b/toys/lsb/pidof.c new file mode 100644 index 00000000..d8f24be8 --- /dev/null +++ b/toys/lsb/pidof.c @@ -0,0 +1,32 @@ +/* vi: set sw=4 ts=4: + * + * pidof.c - Print the PIDs of all processes with the given names. + * + * Copyright 2012 Andreas Heck <aheck@gmx.de> + * + * Not in SUSv4. + +USE_PIDOF(NEWTOY(pidof, "<1", TOYFLAG_USR|TOYFLAG_BIN)) + +config PIDOF + bool "pidof" + default y + help + usage: pidof [NAME]... + + Print the PIDs of all processes with the given names. +*/ + +#include "toys.h" + +static void print_pid(pid_t pid) { + xprintf("%s%ld", toys.exitval ? "" : " ", (long)pid); + toys.exitval = 0; +} + +void pidof_main(void) +{ + toys.exitval = 1; + for_each_pid_with_name_in(toys.optargs, print_pid); + if (!toys.exitval) xputc('\n'); +} diff --git a/toys/lsb/seq.c b/toys/lsb/seq.c new file mode 100644 index 00000000..a41537c0 --- /dev/null +++ b/toys/lsb/seq.c @@ -0,0 +1,48 @@ +/* vi: set sw=4 ts=4: + * + * seq.c - Count from first to last, by increment. + * + * Copyright 2006 Rob Landley <rob@landley.net> + * + * Not in SUSv3. (Don't ask me why not.) + +USE_SEQ(NEWTOY(seq, "<1>3?", TOYFLAG_USR|TOYFLAG_BIN)) + +config SEQ + bool "seq" + depends on TOYBOX_FLOAT + default y + help + usage: seq [first] [increment] last + + Count from first to last, by increment. Omitted arguments default + to 1. Two arguments are used as first and last. Arguments can be + negative or floating point. +*/ + +#include "toys.h" + +void seq_main(void) +{ + double first, increment, last, dd; + + // Parse command line arguments, with appropriate defaults. + // Note that any non-numeric arguments are treated as zero. + first = increment = 1; + switch (toys.optc) { + case 3: + increment = atof(toys.optargs[1]); + case 2: + first = atof(*toys.optargs); + default: + last = atof(toys.optargs[toys.optc-1]); + } + + // Yes, we're looping on a double. Yes rounding errors can accumulate if + // you use a non-integer increment. Deal with it. + for (dd=first; (increment>0 && dd<=last) || (increment <0 && dd>=last); + dd+=increment) + { + printf("%g\n", dd); + } +} diff --git a/toys/lsb/sync.c b/toys/lsb/sync.c new file mode 100644 index 00000000..e6990fd8 --- /dev/null +++ b/toys/lsb/sync.c @@ -0,0 +1,25 @@ +/* vi: set sw=4 ts=4: + * + * sync.c - Write all pending data to disk. + * + * Copyright 2007 Rob Landley <rob@landley.net> + * + * Not in SUSv3. + +USE_SYNC(NEWTOY(sync, NULL, TOYFLAG_BIN)) + +config SYNC + bool "sync" + default y + help + usage: sync + + Write pending cached data to disk (synchronize), blocking until done. +*/ + +#include "toys.h" + +void sync_main(void) +{ + sync(); +} |