forked from bonsai/harakit
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
6d3f084d9c
|
|||
|
c295f531cb
|
|||
|
c7739612a7
|
|||
|
46b607384a
|
|||
|
4e5fc680e9
|
17
Makefile
17
Makefile
@@ -28,7 +28,7 @@ RUSTLIBS = --extern getopt=build/o/libgetopt.rlib \
|
|||||||
CFLAGS += -I$(SYSEXITS)
|
CFLAGS += -I$(SYSEXITS)
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: dj false fop hru intcmp mm npc rpn scrut sleep str strcmp swab true
|
all: dj false fop hru intcmp mm npc rpn scrut str strcmp swab true
|
||||||
|
|
||||||
build:
|
build:
|
||||||
# keep build/include until bindgen(1) has stdin support
|
# keep build/include until bindgen(1) has stdin support
|
||||||
@@ -102,11 +102,15 @@ mm: build/bin/mm
|
|||||||
build/bin/mm: src/mm.c build
|
build/bin/mm: src/mm.c build
|
||||||
$(CC) $(CFLAGS) -o $@ src/mm.c
|
$(CC) $(CFLAGS) -o $@ src/mm.c
|
||||||
|
|
||||||
|
|
||||||
.PHONY: npc
|
.PHONY: npc
|
||||||
npc: build/bin/npc
|
npc: build/bin/npc
|
||||||
build/bin/npc: src/npc.c build
|
build/bin/npc: src/npc.c build
|
||||||
$(CC) $(CFLAGAS) -o $@ src/npc.c
|
$(CC) $(CFLAGS) -o $@ src/npc.c
|
||||||
|
|
||||||
|
.PHONY: peek
|
||||||
|
peek: build/bin/peek
|
||||||
|
build/bin/peek: src/peek.c build
|
||||||
|
$(CC) $(CFLAGS) -o $@ src/peek.c
|
||||||
|
|
||||||
.PHONY: rpn
|
.PHONY: rpn
|
||||||
rpn: build/bin/rpn
|
rpn: build/bin/rpn
|
||||||
@@ -118,13 +122,6 @@ scrut: build/bin/scrut
|
|||||||
build/bin/scrut: src/scrut.c build
|
build/bin/scrut: src/scrut.c build
|
||||||
$(CC) $(CFLAGS) -o $@ src/scrut.c
|
$(CC) $(CFLAGS) -o $@ src/scrut.c
|
||||||
|
|
||||||
.PHONY: sleep
|
|
||||||
sleep: build/bin/sleep
|
|
||||||
build/bin/sleep: src/sleep.rs build rustlibs
|
|
||||||
$(RUSTC) $(RUSTFLAGS) \
|
|
||||||
--extern sysexits=build/o/libsysexits.rlib \
|
|
||||||
-o $@ src/sleep.rs
|
|
||||||
|
|
||||||
.PHONY: str
|
.PHONY: str
|
||||||
str: build/bin/str
|
str: build/bin/str
|
||||||
build/bin/str: src/str.c build
|
build/bin/str: src/str.c build
|
||||||
|
|||||||
90
docs/peek.1
Normal file
90
docs/peek.1
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
.\" Copyright (c) 2023-2024 DTB <trinity@trinity.moe>
|
||||||
|
.\"
|
||||||
|
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||||
|
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
|
||||||
|
|
||||||
|
.TH PEEK 1
|
||||||
|
|
||||||
|
.SH NAME
|
||||||
|
|
||||||
|
peek \(en read from the standard input, furtively
|
||||||
|
|
||||||
|
.SH SYNOPSIS
|
||||||
|
|
||||||
|
peek
|
||||||
|
.RB ( -i )
|
||||||
|
|
||||||
|
.SH DESCRIPTION
|
||||||
|
|
||||||
|
Peek reads input from the standard input with terminal echo disabled.
|
||||||
|
|
||||||
|
.SH OPTIONS
|
||||||
|
|
||||||
|
.B -i
|
||||||
|
.RS
|
||||||
|
Allows input to come from sources other than terminals (i.e. a pipe).
|
||||||
|
.RE
|
||||||
|
|
||||||
|
.SH DIAGNOSTICS
|
||||||
|
|
||||||
|
Peek prints an error message and exits with the appropriate status from
|
||||||
|
sysexits.h(3) if it encounters an unrecoverable error.
|
||||||
|
|
||||||
|
Peek tries to handle the signal SIGINT (^C) to ensure the user's terminal is
|
||||||
|
still usable after premature termination; if the signal can't be handled, it
|
||||||
|
prints an error message and continues. If peek is interrupted, it exits
|
||||||
|
unsuccessfully, without an error message.
|
||||||
|
|
||||||
|
.SH RATIONALE
|
||||||
|
|
||||||
|
This tool was originally written to accept passwords in shell scripts, as an
|
||||||
|
extremely simple alternative to the GNU Privacy Guard project's pinentry(1).
|
||||||
|
Accepting input without showing what is being typed is useful when keying in
|
||||||
|
secrets in public settings or places with installed surveillance cameras.
|
||||||
|
|
||||||
|
.SH BUGS
|
||||||
|
|
||||||
|
This does nothing to prevent others seeing the keyboard being used to input
|
||||||
|
secrets or mask the sound of typing. Audio or video recordings of typing can be
|
||||||
|
used to determine what was input without needing to see the characters appear
|
||||||
|
on the screen.
|
||||||
|
|
||||||
|
Accepting secrets in shell scripts is probably not adviseable.
|
||||||
|
|
||||||
|
On systems that support it, the ioctl(2) command TIOCSTI can be used to insert
|
||||||
|
characters into the standard input going to peek. This doesn't allow snooping
|
||||||
|
but can be used for general mischief.
|
||||||
|
|
||||||
|
.SH EXAMPLES
|
||||||
|
|
||||||
|
This is an sh(1p) command line that hashes a given password. It uses head(1p)
|
||||||
|
to only accept one line of input, xargs(1p) and printf(1p) to strip the
|
||||||
|
trailing newline, htpasswd(1) from Apache's utilities to hash the input with
|
||||||
|
the bcrypt algorithm, and cut(1p) to print only the resulting hash:
|
||||||
|
|
||||||
|
.RS
|
||||||
|
.R $ peek | head -n 1 | xargs printf '%s' | htpasswd -nBi _ | cut -d : -f 2
|
||||||
|
.RE
|
||||||
|
|
||||||
|
This is an sh(1p) command line that lets a user blindly write into a text file,
|
||||||
|
only able to see written lines. Some writers have the habit of prematurely
|
||||||
|
revising their work and use tools like this to prevent it. It uses mm(1) to
|
||||||
|
pipe the output of peek to both the standard error and the regular file
|
||||||
|
writing.txt.
|
||||||
|
|
||||||
|
.RS
|
||||||
|
.R $ echo Input ^D to quit. && peek | mm -eo - >writing.txt
|
||||||
|
.RE
|
||||||
|
|
||||||
|
.SH AUTHOR
|
||||||
|
|
||||||
|
Written by DTB <trinity@trinity.moe>.
|
||||||
|
|
||||||
|
.SH COPYRIGHT
|
||||||
|
|
||||||
|
Copyright (c) 2023-2024 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
||||||
|
<https://gnu.org/licenses/gpl.html>.
|
||||||
|
|
||||||
|
.SH SEE ALSO
|
||||||
|
|
||||||
|
ioctl(2), ioctl_tty(2), read(1), sh(1)
|
||||||
53
docs/sleep.1
53
docs/sleep.1
@@ -1,53 +0,0 @@
|
|||||||
.\" Copyright (c) 2024 DTB <trinity@trinity.moe>
|
|
||||||
.\"
|
|
||||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
|
||||||
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
|
|
||||||
|
|
||||||
.TH SLEEP 1
|
|
||||||
|
|
||||||
.SH NAME
|
|
||||||
|
|
||||||
sleep \(en wait a moment
|
|
||||||
|
|
||||||
.SH SYNOPSIS
|
|
||||||
|
|
||||||
sleep
|
|
||||||
.RB [ seconds ]
|
|
||||||
|
|
||||||
.SH DESCRIPTION
|
|
||||||
|
|
||||||
Sleep waits the given amount of seconds before exiting.
|
|
||||||
|
|
||||||
.SH DIAGNOSTICS
|
|
||||||
|
|
||||||
Sleep exits successfully when the time specified has elapsed, or unsuccessfully
|
|
||||||
if the time fails to elapse or if invoked incorrectly. In the latter scenario
|
|
||||||
sleep will also print a usage synopsis.
|
|
||||||
|
|
||||||
.SH BUGS
|
|
||||||
|
|
||||||
User may still be tired after invoking sleep.
|
|
||||||
|
|
||||||
.SH EXAMPLES
|
|
||||||
|
|
||||||
The following invocation sleeps for five seconds.
|
|
||||||
|
|
||||||
.RS
|
|
||||||
.R sleep 5
|
|
||||||
.RE
|
|
||||||
|
|
||||||
The following sh(1p) command line sleeps for five minutes, or for sixty
|
|
||||||
seconds, five times, using rpn(1) and xargs(1p).
|
|
||||||
|
|
||||||
.RS
|
|
||||||
.R $ rpn 60 5 '*' | xargs sleep
|
|
||||||
.RE
|
|
||||||
|
|
||||||
.SH COPYRIGHT
|
|
||||||
|
|
||||||
Copyright (c) 2024 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
|
||||||
<https://gnu.org/licenses/gpl.html>.
|
|
||||||
|
|
||||||
.SH SEE ALSO
|
|
||||||
|
|
||||||
sleep(3)
|
|
||||||
18
src/mm.c
18
src/mm.c
@@ -106,15 +106,6 @@ oserr(char *s, char *r){
|
|||||||
} \
|
} \
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
/* Prints a usage text, in which s is the program being run (i.e. argv[0]), and
|
|
||||||
* returns an exit status appropriate for a usage error. */
|
|
||||||
int usage(char *s){
|
|
||||||
|
|
||||||
fprintf(stderr, "Usage: %s (-aenu) (-i [input])... (-o [output])...\n", s);
|
|
||||||
|
|
||||||
return EX_USAGE;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
int c;
|
int c;
|
||||||
struct Files files[2]; /* {read, write} */
|
struct Files files[2]; /* {read, write} */
|
||||||
@@ -187,15 +178,12 @@ int main(int argc, char *argv[]){
|
|||||||
k = 1;
|
k = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
retval = usage(argv[0]);
|
fprintf(stderr, "Usage: %s (-aenu) (-i [input])..."
|
||||||
|
" (-o [output])...\n", argv[0]);
|
||||||
|
retval = EX_USAGE;
|
||||||
terminate;
|
terminate;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(optind != argc){
|
|
||||||
retval = usage(argv[0]);
|
|
||||||
terminate;
|
|
||||||
}
|
|
||||||
|
|
||||||
files[0].s += files[0].s == 0;
|
files[0].s += files[0].s == 0;
|
||||||
files[1].s += files[1].s == 0;
|
files[1].s += files[1].s == 0;
|
||||||
|
|
||||||
|
|||||||
118
src/peek.c
Normal file
118
src/peek.c
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU Affero General Public License as published by the Free
|
||||||
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
||||||
|
* later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <signal.h> /* sigaction(2), signal(2), struct sigaction, SIGINT */
|
||||||
|
#include <stdio.h> /* fprintf(3), fgetc(3), perror(3), fputc(3), stderr, stdin,
|
||||||
|
* stdout, EOF, NULL */
|
||||||
|
#include <stdlib.h> /* exit(3), EXIT_FAILURE */
|
||||||
|
#if !defined EX_IOERR || !defined EX_OK || !defined EX_USAGE
|
||||||
|
# include <sysexits.h>
|
||||||
|
#endif
|
||||||
|
#include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */
|
||||||
|
#include <unistd.h> /* getopt(3), isatty(3), STDIN_FILENO */
|
||||||
|
|
||||||
|
static char *program_name = "peek";
|
||||||
|
|
||||||
|
/* Restores terminal echo; otherwise when a user ^Cs the terminal would
|
||||||
|
* continue to not display typed text. If sig isn't zero, this will terminate
|
||||||
|
* the program. */
|
||||||
|
static void restore_echo(int sig){
|
||||||
|
static struct termios t;
|
||||||
|
|
||||||
|
tcgetattr(STDIN_FILENO, &t);
|
||||||
|
t.c_lflag |= ECHO;
|
||||||
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
||||||
|
|
||||||
|
/* If, for whatever ungodly reason, exit(3) returns, the user will notice
|
||||||
|
* their typed characters on the screen. */
|
||||||
|
if(sig != 0)
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ioerr(char *s){ perror(s); restore_echo(0); return EX_IOERR; }
|
||||||
|
static int usage(char *s){
|
||||||
|
fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n", s);
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
|
||||||
|
if(argc < 1)
|
||||||
|
return usage(program_name);
|
||||||
|
|
||||||
|
{ /* option parsing */
|
||||||
|
char allow_nonterminals;
|
||||||
|
int c;
|
||||||
|
|
||||||
|
allow_nonterminals = 0;
|
||||||
|
while((c = getopt(argc, argv, "i")) != -1)
|
||||||
|
switch(c){
|
||||||
|
case 'i': allow_nonterminals = 1; break;
|
||||||
|
default: return usage(argv[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc > optind)
|
||||||
|
return usage(argv[0]);
|
||||||
|
|
||||||
|
if(!allow_nonterminals && isatty(STDIN_FILENO) != 1){
|
||||||
|
fprintf(stderr, "%s: Must be run in a terminal"
|
||||||
|
" (option -i skips this check)\n", argv[0]);
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{ /* install signal handler */
|
||||||
|
/* There isn't a difference in functionality between the signal(2) and
|
||||||
|
* sigaction(2) methods. sigaction(2) is vastly preferred for
|
||||||
|
* portability but some configurations can only use signal(2). */
|
||||||
|
/* Errors aren't terminating because the worst that happens is some
|
||||||
|
* terminal phooeyness if things go awry. */
|
||||||
|
#if defined _POSIX_C_SOURCE
|
||||||
|
struct sigaction act = { 0 };
|
||||||
|
|
||||||
|
act.sa_handler = restore_echo;
|
||||||
|
if(sigaction(SIGINT, &act, NULL) != 0)
|
||||||
|
perror(argv[0]);
|
||||||
|
#else
|
||||||
|
if(signal(SIGINT, restore_echo) == SIG_ERR)
|
||||||
|
perror(argv[0]);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
{ /* banish terminal echo */
|
||||||
|
struct termios t;
|
||||||
|
|
||||||
|
tcgetattr(STDIN_FILENO, &t);
|
||||||
|
t.c_lflag ^= ECHO;
|
||||||
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ /* actual input loop */
|
||||||
|
int c;
|
||||||
|
|
||||||
|
while((c = fgetc(stdin)) != EOF)
|
||||||
|
if(fputc(c, stdout) == EOF)
|
||||||
|
return ioerr(argv[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
restore_echo(0);
|
||||||
|
|
||||||
|
return EX_OK;
|
||||||
|
}
|
||||||
15
src/scrut.c
15
src/scrut.c
@@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
* Copyright (c) 2023 DTB <trinity@trinity.moe>
|
||||||
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify it under
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
@@ -18,15 +17,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
||||||
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
#include <stdlib.h> /* EXIT_FAILURE */
|
||||||
#include <string.h> /* memset(3), strchr(3) */
|
#include <string.h> /* memset(3), strchr(3) */
|
||||||
#ifndef EX_USAGE
|
|
||||||
# include <sysexits.h>
|
|
||||||
#endif
|
|
||||||
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
|
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
|
||||||
#include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR,
|
#include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR,
|
||||||
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
||||||
* S_ISUID, S_ISVTX */
|
* S_ISUID, S_ISVTX */
|
||||||
|
#include <sysexits.h>
|
||||||
|
|
||||||
static char args[] = "bcdefghkprsuwxLS";
|
static char args[] = "bcdefghkprsuwxLS";
|
||||||
static char ops[(sizeof args) / (sizeof *args)];
|
static char ops[(sizeof args) / (sizeof *args)];
|
||||||
@@ -60,7 +57,7 @@ int main(int argc, char *argv[]){
|
|||||||
|
|
||||||
argv += optind;
|
argv += optind;
|
||||||
do{ if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1)
|
do{ if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1)
|
||||||
return EXIT_FAILURE; /* doesn't exist or isn't stattable */
|
return 1; /* doesn't exist or isn't stattable */
|
||||||
|
|
||||||
for(i = 0; ops[i] != '\0'; ++i)
|
for(i = 0; ops[i] != '\0'; ++i)
|
||||||
if(ops[i] == 'e')
|
if(ops[i] == 'e')
|
||||||
@@ -100,8 +97,8 @@ usage: fprintf(stderr, "Usage: %s (-%s) [file...]\n",
|
|||||||
&& !S_ISLNK(buf.st_mode))
|
&& !S_ISLNK(buf.st_mode))
|
||||||
|| (ops[i] == 'S'
|
|| (ops[i] == 'S'
|
||||||
&& !S_ISSOCK(buf.st_mode)))
|
&& !S_ISSOCK(buf.st_mode)))
|
||||||
return EXIT_FAILURE;
|
return 1;
|
||||||
}while(*++argv != NULL);
|
}while(*++argv != NULL);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
47
src/sleep.rs
47
src/sleep.rs
@@ -1,47 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify it under
|
|
||||||
* the terms of the GNU Affero General Public License as published by the Free
|
|
||||||
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
||||||
* later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
||||||
* details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use std::{
|
|
||||||
env::args,
|
|
||||||
process::ExitCode,
|
|
||||||
thread::sleep,
|
|
||||||
time::Duration
|
|
||||||
};
|
|
||||||
|
|
||||||
extern crate sysexits;
|
|
||||||
use sysexits::EX_USAGE;
|
|
||||||
|
|
||||||
fn usage(s: &str) -> ExitCode {
|
|
||||||
eprintln!("Usage: {} [seconds]", s);
|
|
||||||
ExitCode::from(EX_USAGE as u8)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> ExitCode {
|
|
||||||
let argv = args().collect::<Vec<String>>();
|
|
||||||
|
|
||||||
if argv.len() == 2 {
|
|
||||||
if let Ok(s) = argv[1].parse::<u64>() {
|
|
||||||
sleep(Duration::from_secs(s));
|
|
||||||
ExitCode::SUCCESS
|
|
||||||
} else {
|
|
||||||
usage(&argv[0])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
usage(&argv[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user