From 88b0d668394c22a1097b0cb80347d839abbf804d Mon Sep 17 00:00:00 2001 From: Riccardo Murri Date: Tue, 30 May 2017 16:20:48 +0200 Subject: [PATCH 1/7] Add Makefile target for debug compilation --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index bda7689..1e128e8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -CFLAGS ?= -Wall -Werror -g +CFLAGS ?= -Wall -Werror LDFLAGS ?= PROG := su-exec @@ -13,5 +13,8 @@ $(PROG): $(SRCS) $(PROG)-static: $(SRCS) $(CC) $(CFLAGS) -o $@ $^ -static $(LDFLAGS) +$(PROG)-debug: $(SRCS) + $(CC) -g $(CFLAGS) -o $@ $^ $(LDFLAGS) + clean: - rm -f $(PROG) $(PROG)-static + rm -f $(PROG) $(PROG)-static $(PROG)-debug From f4d37b2f1cf50b9aa1cc2f2650bfae758dfe85b0 Mon Sep 17 00:00:00 2001 From: Riccardo Murri Date: Tue, 30 May 2017 16:22:35 +0200 Subject: [PATCH 2/7] Strip non-debug binaries. This brings down `su-exec` to 11kB and `su-exec-static` to 853kB on my Ubuntu 16.04 x86_64. --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 1e128e8..0eead54 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,11 @@ all: $(PROG) $(PROG): $(SRCS) $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + strip $@ $(PROG)-static: $(SRCS) $(CC) $(CFLAGS) -o $@ $^ -static $(LDFLAGS) + strip $@ $(PROG)-debug: $(SRCS) $(CC) -g $(CFLAGS) -o $@ $^ $(LDFLAGS) From cc18e89d7338c798f5447a1119355bf67ba14bed Mon Sep 17 00:00:00 2001 From: Andrew Ball Date: Tue, 30 May 2017 13:48:36 -0500 Subject: [PATCH 3/7] Add functionality for "make install" with dynamic linking to libc. Static linking to GNU libc is not really allowed, and I am using GNU libc for my purposes (running Java on CentOS and Fedora). Being able to do "make install" makes packaging su-exec as an RPM easier. --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index bda7689..75919ea 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,9 @@ LDFLAGS ?= PROG := su-exec SRCS := $(PROG).c +PREFIX := /usr/local +INSTALL_DIR := $(PREFIX)/bin + all: $(PROG) $(PROG): $(SRCS) @@ -13,5 +16,8 @@ $(PROG): $(SRCS) $(PROG)-static: $(SRCS) $(CC) $(CFLAGS) -o $@ $^ -static $(LDFLAGS) +install: + install -m 0755 $(PROG) $(INSTALL_DIR) + clean: rm -f $(PROG) $(PROG)-static From 5a4e10b56fe8d1f9820f3aab5636ed1f94d9f4e6 Mon Sep 17 00:00:00 2001 From: Riccardo Murri Date: Wed, 31 May 2017 13:38:06 +0200 Subject: [PATCH 4/7] git ignore also the `-static` and `-debug` variants of exe file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ac47793..94e08f0 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ *.dSYM/ su-exec +su-exec-static +su-exec-debug From 41cda4e5a30adaf5b56d6143d53ead11439980d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20L=C3=B8vdal?= Date: Sat, 14 Oct 2017 13:19:16 +0200 Subject: [PATCH 5/7] Add support for DESTDIR install prefix --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5698707..2532c7f 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ $(PROG)-debug: $(SRCS) $(CC) -g $(CFLAGS) -o $@ $^ $(LDFLAGS) install: - install -m 0755 $(PROG) $(INSTALL_DIR) + install -m 0755 $(PROG) $(DESTDIR)$(INSTALL_DIR) clean: rm -f $(PROG) $(PROG)-static $(PROG)-debug From 4912a85c95b0d471256947117d52cca28846d827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20L=C3=B8vdal?= Date: Sat, 14 Oct 2017 13:47:21 +0200 Subject: [PATCH 6/7] Make sure install dir exists --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 2532c7f..bf4f3d7 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ $(PROG)-debug: $(SRCS) $(CC) -g $(CFLAGS) -o $@ $^ $(LDFLAGS) install: + install -d 0755 $(DESTDIR)$(INSTALL_DIR) install -m 0755 $(PROG) $(DESTDIR)$(INSTALL_DIR) clean: From 4176c2cf71b7313ba947f0434f91d776caeaee14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20L=C3=B8vdal?= Date: Sat, 14 Oct 2017 21:22:16 +0200 Subject: [PATCH 7/7] Add man page http://www.linuxjournal.com/article/1158 used as reference. --- Makefile | 3 +++ su-exec.1 | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 su-exec.1 diff --git a/Makefile b/Makefile index bf4f3d7..b70490a 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ SRCS := $(PROG).c PREFIX := /usr/local INSTALL_DIR := $(PREFIX)/bin +MAN_DIR := $(PREFIX)/share/man/man8 all: $(PROG) @@ -24,6 +25,8 @@ $(PROG)-debug: $(SRCS) install: install -d 0755 $(DESTDIR)$(INSTALL_DIR) install -m 0755 $(PROG) $(DESTDIR)$(INSTALL_DIR) + install -d 0755 $(DESTDIR)$(MAN_DIR) + install -m 0644 su-exec.1 $(DESTDIR)$(MAN_DIR) clean: rm -f $(PROG) $(PROG)-static $(PROG)-debug diff --git a/su-exec.1 b/su-exec.1 new file mode 100644 index 0000000..47e809b --- /dev/null +++ b/su-exec.1 @@ -0,0 +1,59 @@ +.TH SU-EXEC 8 "14 Oct 2017" + +.SH NAME +su-exec \- change user id and group id before executing a program + +.SH SYNOPSIS +\fBsu-exec\fP \fIuser-spec\fP \fIcommand\fP [ \fIarguments...\fP ] + +.SH DESCRIPTION +\fBsu-exec\fP executes a program with modified privileges. The program +will be exceuted directly and not run as a child, like su and sudo does, +which avoids TTY and signal issues. + +Notice that su-exec depends on being run by the root user, non-root +users do not have permission to change uid/gid. + +.SH OPTIONS +.TP +\fIuser-spec\fP +is either a user name (e.g. \fBnobody\fP) or user name and group name +separated with colon (e.g. \fBnobody:ftp\fP). Numeric uid/gid values +can be used instead of names. + +.TP +\fIcommand\fP +is the program to execute. Can be either absolute or relative path. + +.SH EXAMPLES + +.TP +Execute httpd as user \fIapache\fP and gid value 1000 with the two specified arguments: + +$ \fBsu-exec apache:1000 /usr/sbin/httpd -f /opt/www/httpd.conf\fP + +.SH ENVIRONMENT VARIABLES + +.TP +\fBHOME\fP +Is updated to the value matching the user entry in \fC/etc/passwd\fP. + +.TP +\fBPATH\fP +Is used for searching for the program to execute. + +Since su-exec is not running as a suid binary, the dynamic linker or +libc will not strip or ignore variables like LD_LIBRARY_PATH etc. + +.SH EXIT STATUS +.TP +\fB1\fP +If \fbsu-exec\fR fails to change priveledges or execute the program it +will return \fB1\fP. In the successfull case the exit value will be +whatever the executed program returns. + +.SH "SEE ALSO" +su(1), runuser(8), sudo(8), gosu(1) + +.SH BUGS +\fBUSER\fP and \fBLOGNAME\fP environmental variables are not updated.