# This is a BitKeeper generated patch for the following project:
# Project Name: Daniel J. Bernsteins daemontools, with patches by Uwe Ohse
# This patch format is intended for GNU patch command version 2.5 or higher.
# This patch includes the following deltas:
#	           ChangeSet	1.7     -> 1.9    
#	            Makefile	1.5     -> 1.6    
#	             CHANGES	1.6     -> 1.7    
#	               FILES	1.5     -> 1.6    
#	             TARGETS	1.2     -> 1.3    
#	              README	1.6     -> 1.7    
#	              hier.c	1.2     -> 1.3    
#	               (new)	        -> 1.1     setuidgidfromenv.c
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 01/04/25	uwe@fjoras.ohse.de	1.8
# added setuidgidfromenv
# --------------------------------------------
# 01/04/25	uwe@fjoras.ohse.de	1.9
# added description
# --------------------------------------------
#
diff -Nru a/CHANGES b/CHANGES
--- a/CHANGES	Wed Apr 25 13:02:10 2001
+++ b/CHANGES	Wed Apr 25 13:02:10 2001
@@ -32,3 +32,5 @@
 	internal: sped up multilog.
 	ui: multilog sets current to mode 644 on restart.
 	version: daemontools 0.70, beta.
+20010425 uwe@ohse.de
+	ui: added new target setuidgidfromenv
diff -Nru a/FILES b/FILES
--- a/FILES	Wed Apr 25 13:02:10 2001
+++ b/FILES	Wed Apr 25 13:02:10 2001
@@ -175,3 +175,4 @@
 warn-auto.sh
 warn-shsgr
 x86cpuid.c
+setuidgidfromenv
diff -Nru a/Makefile b/Makefile
--- a/Makefile	Wed Apr 25 13:02:10 2001
+++ b/Makefile	Wed Apr 25 13:02:10 2001
@@ -336,7 +336,7 @@
 
 prog: \
 svscan supervise svc svok svstat fghack multilog tai64n tai64nlocal \
-softlimit setuidgid envuidgid envdir setlock rts matchtest
+softlimit setuidgid envuidgid setuidgidfromenv envdir setlock rts matchtest 
 
 prot.o: \
 compile prot.c hasshsgr.h prot.h
@@ -379,9 +379,17 @@
 load setuidgid.o unix.a byte.a
 	./load setuidgid unix.a byte.a 
 
+setuidgidfromenv: \
+load setuidgidfromenv.o unix.a byte.a
+	./load setuidgidfromenv unix.a byte.a 
+
 setuidgid.o: \
 compile setuidgid.c prot.h strerr.h pathexec.h
 	./compile setuidgid.c
+
+setuidgidfromenv.o: \
+compile setuidgidfromenv.c prot.h strerr.h pathexec.h env.h scan.h
+	./compile setuidgidfromenv.c
 
 setup: \
 it install
diff -Nru a/README b/README
--- a/README	Wed Apr 25 13:02:10 2001
+++ b/README	Wed Apr 25 13:02:10 2001
@@ -5,3 +5,7 @@
 
 daemontools home page: http://cr.yp.to/daemontools.html
 Installation instructions: http://cr.yp.to/daemontools/install.html
+--------------------------------------------------------------------------
+This version contains patches made by Uwe Ohse, <uwe@ohse.de>, adding:
+* setuidgidfromenv, a program to set group and user id according to
+  $GID and UID.
diff -Nru a/TARGETS b/TARGETS
--- a/TARGETS	Wed Apr 25 13:02:10 2001
+++ b/TARGETS	Wed Apr 25 13:02:10 2001
@@ -135,3 +135,4 @@
 it
 setup
 check
+setuidgidfromenv
diff -Nru a/hier.c b/hier.c
--- a/hier.c	Wed Apr 25 13:02:10 2001
+++ b/hier.c	Wed Apr 25 13:02:10 2001
@@ -17,6 +17,7 @@
   c(auto_home,"bin","tai64nlocal",-1,-1,0755);
   c(auto_home,"bin","softlimit",-1,-1,0755);
   c(auto_home,"bin","setuidgid",-1,-1,0700);
+  c(auto_home,"bin","setuidgidfromenv",-1,-1,0700);
   c(auto_home,"bin","envuidgid",-1,-1,0755);
   c(auto_home,"bin","envdir",-1,-1,0755);
   c(auto_home,"bin","setlock",-1,-1,0755);
diff -Nru a/setuidgidfromenv.c b/setuidgidfromenv.c
--- /dev/null	Wed Dec 31 16:00:00 1969
+++ b/setuidgidfromenv.c	Wed Apr 25 13:02:10 2001
@@ -0,0 +1,37 @@
+#include "prot.h"
+#include <unistd.h>
+#include "strerr.h"
+#include "pathexec.h"
+#include "env.h"
+#include "scan.h"
+
+#define FATAL "setuidgidfromenv: fatal: "
+
+char *account;
+struct passwd *pw;
+
+main(int argc,char **argv,char **envp)
+{
+  char *x;
+  unsigned long id;
+
+  x = env_get("GID");
+  if (!x)
+    strerr_die2x(111,FATAL,"$GID not set");
+  scan_ulong(x,&id);
+  if (prot_gid((int) id) == -1)
+    strerr_die2sys(111,FATAL,"unable to setgid: ");
+
+  x = env_get("UID");
+  if (!x)
+    strerr_die2x(111,FATAL,"$UID not set");
+  scan_ulong(x,&id);
+  if (prot_uid((int) id) == -1)
+    strerr_die2sys(111,FATAL,"unable to setuid: ");
+
+  if (!*++argv)
+    strerr_die1x(100,"setuidgidfromenv: usage: setuidgidfromenv child");
+
+  pathexec_run(*argv,argv,envp);
+  strerr_die4sys(111,FATAL,"unable to run ",*argv,": ");
+}
