Skip to content

Commit 174d22c

Browse files
committed
tscat: timestamp stdin to stdout/stder
0 parents  commit 174d22c

16 files changed

+1098
-0
lines changed

.gitignore

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Object files
2+
*.o
3+
*.ko
4+
*.obj
5+
*.elf
6+
7+
# Precompiled Headers
8+
*.gch
9+
*.pch
10+
11+
# Libraries
12+
*.lib
13+
*.a
14+
*.la
15+
*.lo
16+
17+
# Shared objects (inc. Windows DLLs)
18+
*.dll
19+
*.so
20+
*.so.*
21+
*.dylib
22+
23+
# Executables
24+
*.exe
25+
*.out
26+
*.app
27+
*.i*86
28+
*.x86_64
29+
*.hex
30+
31+
# Debug files
32+
*.dSYM/
33+
34+
# vim
35+
[._]*.s[a-w][a-z]
36+
[._]s[a-w][a-z]
37+
*.un~
38+
Session.vim
39+
.netrwhist
40+
*~
41+
42+
tscat

LICENSE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2020, Michael Santos <michael.santos@gmail.com>
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Makefile

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
.PHONY: all clean test
2+
3+
PROG= tscat
4+
SRCS= tscat.c \
5+
getnline.c \
6+
strtonum.c \
7+
restrict_process_null.c \
8+
restrict_process_rlimit.c \
9+
restrict_process_seccomp.c \
10+
restrict_process_pledge.c \
11+
restrict_process_capsicum.c
12+
13+
UNAME_SYS := $(shell uname -s)
14+
ifeq ($(UNAME_SYS), Linux)
15+
CFLAGS ?= -D_FORTIFY_SOURCE=2 -O2 -fstack-protector-strong \
16+
-Wformat -Werror=format-security \
17+
-fno-strict-aliasing
18+
LDFLAGS ?= -Wl,-z,relro,-z,now -Wl,-z,noexecstack
19+
RESTRICT_PROCESS ?= seccomp
20+
else ifeq ($(UNAME_SYS), OpenBSD)
21+
CFLAGS ?= -DHAVE_STRTONUM \
22+
-D_FORTIFY_SOURCE=2 -O2 -fstack-protector-strong \
23+
-Wformat -Werror=format-security \
24+
-fno-strict-aliasing
25+
LDFLAGS ?= -Wl,-z,relro,-z,now -Wl,-z,noexecstack
26+
RESTRICT_PROCESS ?= pledge
27+
else ifeq ($(UNAME_SYS), FreeBSD)
28+
CFLAGS ?= -DHAVE_STRTONUM \
29+
-D_FORTIFY_SOURCE=2 -O2 -fstack-protector-strong \
30+
-Wformat -Werror=format-security \
31+
-fno-strict-aliasing
32+
LDFLAGS ?= -Wl,-z,relro,-z,now -Wl,-z,noexecstack
33+
RESTRICT_PROCESS ?= capsicum
34+
endif
35+
36+
RM ?= rm
37+
38+
RESTRICT_PROCESS ?= rlimit
39+
TSCAT_CFLAGS ?= -g -Wall -fwrapv -pedantic -pie -fPIE
40+
41+
CFLAGS += $(TSCAT_CFLAGS) \
42+
-DRESTRICT_PROCESS=\"$(RESTRICT_PROCESS)\" -DRESTRICT_PROCESS_$(RESTRICT_PROCESS)
43+
44+
LDFLAGS += $(TSCAT_LDFLAGS)
45+
46+
all: $(PROG)
47+
48+
$(PROG):
49+
$(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LDFLAGS)
50+
51+
clean:
52+
-@$(RM) $(PROG)
53+
54+
test: $(PROG)
55+
@PATH=.:$(PATH) bats test

README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SYNOPSIS
2+
3+
tscat *option* [*label*]
4+
5+
# DESCRIPTION
6+
7+
tscat: timestamp stdin to stdout/stderr
8+
9+
tscat timestamps standard input and writes the output to standard output
10+
and standard error.
11+
12+
# EXAMPLES
13+
14+
cat /etc/passwd | tscat
15+
16+
cat /etc/passwd | tscat foo
17+
18+
# Build
19+
20+
make
21+
22+
# selecting process restrictions
23+
RESTRICT_PROCESS=seccomp make
24+
25+
#### using musl
26+
RESTRICT_PROCESS=rlimit ./musl-make
27+
28+
## linux seccomp sandbox: requires kernel headers
29+
30+
# clone the kernel headers somewhere
31+
cd /path/to/dir
32+
git clone https://github.com/sabotage-linux/kernel-headers.git
33+
34+
# then compile
35+
MUSL_INCLUDE=/path/to/dir ./musl-make clean all
36+
37+
# OPTIONS
38+
39+
-o, --output *1|2|3*
40+
: stdout=1, stderr=2, both=3 (default: 1)
41+
42+
-f, --format *fmt*
43+
: timestamp format (see strftime(3)) (default: `%F%T%z`)
44+
45+
-W, --write-error *exit|drop|block*
46+
: behaviour if write buffer is full (default: block)
47+
48+
-h, --help
49+
: usage summary
50+
51+
# ALTERNATIVES
52+
53+
~~~
54+
#!/bin/sh
55+
56+
LABEL="${1-""}"
57+
exec awk -v service="$LABEL" '{
58+
printf("%s %s %s\n", strftime("%FT%T%z"), service, $0) > "/dev/stderr"
59+
printf("%s %s %s\n", strftime("%FT%T%z"), service, $0)
60+
fflush()
61+
}'
62+
~~~

getnline.c

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* $NetBSD: getline.c,v 1.1.1.6 2015/01/02 20:34:27 christos Exp $ */
2+
3+
/* NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp */
4+
5+
/*-
6+
* Copyright (c) 2011 The NetBSD Foundation, Inc.
7+
* All rights reserved.
8+
*
9+
* This code is derived from software contributed to The NetBSD Foundation
10+
* by Christos Zoulas.
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions
14+
* are met:
15+
* 1. Redistributions of source code must retain the above copyright
16+
* notice, this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in the
19+
* documentation and/or other materials provided with the distribution.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*/
33+
34+
/* openssh-portable:
35+
* https://raw.githubusercontent.com/openssh/openssh-portable/872517ddbb72deaff31d4760f28f2b0a1c16358f/openbsd-compat/bsd-getline.c
36+
*/
37+
/* NETBSD ORIGINAL: external/bsd/file/dist/src/getline.c */
38+
39+
#include <errno.h>
40+
#include <stdio.h>
41+
#include <stdlib.h>
42+
#include <string.h>
43+
#include <unistd.h>
44+
45+
ssize_t getndelim(char **buf, size_t *bufsiz, size_t nmax, int delimiter,
46+
FILE *fp) {
47+
char *ptr, *eptr;
48+
49+
if (*buf == NULL || *bufsiz == 0) {
50+
if ((*buf = malloc(BUFSIZ)) == NULL)
51+
return -1;
52+
*bufsiz = BUFSIZ;
53+
}
54+
55+
for (ptr = *buf, eptr = *buf + *bufsiz;;) {
56+
int c = fgetc(fp);
57+
if (c == -1) {
58+
if (feof(fp)) {
59+
ssize_t diff = (ssize_t)(ptr - *buf);
60+
if (diff != 0) {
61+
*ptr = '\0';
62+
return diff;
63+
}
64+
}
65+
return -1;
66+
}
67+
*ptr++ = c;
68+
if (c == delimiter || ptr - *buf >= nmax) {
69+
*ptr = '\0';
70+
return ptr - *buf;
71+
}
72+
if (ptr + 2 >= eptr) {
73+
char *nbuf;
74+
size_t nbufsiz = *bufsiz * 2;
75+
ssize_t d = ptr - *buf;
76+
if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
77+
return -1;
78+
*buf = nbuf;
79+
*bufsiz = nbufsiz;
80+
eptr = nbuf + nbufsiz;
81+
ptr = nbuf + d;
82+
}
83+
}
84+
}
85+
86+
ssize_t getnline(char **buf, size_t *bufsiz, size_t nmax, FILE *fp) {
87+
return getndelim(buf, bufsiz, nmax, '\n', fp);
88+
}

getnline.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ssize_t getndelim(char **buf, size_t *bufsiz, size_t nmax, int delimiter,
2+
FILE *fp);
3+
ssize_t getnline(char **buf, size_t *bufsiz, size_t nmax, FILE *fp);

musl-make

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
export MUSL_INCLUDE="${MUSL_INCLUDE-/usr/local/lib}"
8+
9+
MACHTYPE="$(uname -m)"
10+
case "${MACHTYPE}" in
11+
armv6l) ;&
12+
armv7l) MACHTYPE=arm ;;
13+
*) ;;
14+
esac
15+
16+
export TSCAT_CFLAGS="-g -Wall -fwrapv -pedantic"
17+
export TSCAT_LDFLAGS="-I$MUSL_INCLUDE/kernel-headers/generic/include -I$MUSL_INCLUDE/kernel-headers/${MACHTYPE}/include"
18+
export CC="musl-gcc -static -Os"
19+
make $@

restrict_process.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2020, Michael Santos <michael.santos@gmail.com>
3+
*
4+
* Permission to use, copy, modify, and/or distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
int restrict_process_init(void);
18+
int restrict_process_stdin(void);

restrict_process_capsicum.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Copyright (c) 2020, Michael Santos <michael.santos@gmail.com>
2+
*
3+
* Permission to use, copy, modify, and/or distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
#include "restrict_process.h"
16+
#ifdef RESTRICT_PROCESS_capsicum
17+
#include <sys/capsicum.h>
18+
#include <sys/param.h>
19+
#include <sys/resource.h>
20+
#include <sys/time.h>
21+
#include <sys/types.h>
22+
#include <unistd.h>
23+
24+
#include <errno.h>
25+
26+
int restrict_process_init() {
27+
struct rlimit rl = {0};
28+
29+
return setrlimit(RLIMIT_NPROC, &rl);
30+
}
31+
32+
int restrict_process_stdin() {
33+
struct rlimit rl = {0};
34+
cap_rights_t policy_read;
35+
cap_rights_t policy_write;
36+
37+
int fd = -1;
38+
39+
(void)cap_rights_init(&policy_read, CAP_READ, CAP_EVENT, CAP_FCNTL);
40+
(void)cap_rights_init(&policy_write, CAP_WRITE, CAP_READ);
41+
42+
if (cap_rights_limit(STDIN_FILENO, &policy_read) < 0)
43+
return -1;
44+
45+
if (cap_rights_limit(STDOUT_FILENO, &policy_write) < 0)
46+
return -1;
47+
48+
if (cap_rights_limit(STDERR_FILENO, &policy_write) < 0)
49+
return -1;
50+
51+
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
52+
return -1;
53+
54+
for (fd = STDERR_FILENO + 1; fd < rl.rlim_cur; fd++) {
55+
if (fcntl(fd, F_GETFD, 0) < 0)
56+
continue;
57+
58+
if (cap_rights_limit(fd, &policy_read) < 0)
59+
return -1;
60+
}
61+
62+
return cap_enter();
63+
}
64+
#endif

restrict_process_null.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Copyright (c) 2020, Michael Santos <michael.santos@gmail.com>
2+
*
3+
* Permission to use, copy, modify, and/or distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
#include "restrict_process.h"
16+
#ifdef RESTRICT_PROCESS_null
17+
int restrict_process_init() { return 0; }
18+
19+
int restrict_process_stdin() { return 0; }
20+
#endif

0 commit comments

Comments
 (0)