sinstall.sh revision d522f475
1#!/bin/sh
2# $XTermId: sinstall.sh,v 1.16 2008/03/02 23:35:02 tom Exp $
3#
4# Install program setuid if the installer is running as root, and if xterm is
5# already installed on the system with setuid privilege.  This is a safeguard
6# for ordinary users installing xterm for themselves on systems where the
7# setuid is not needed to access a PTY, but only for things like utmp.
8#
9# Options:
10#	u+s, g+s as in chmod
11#	-u, -g and -m as in install.  If any options are given, $3 is ignored.
12#
13# Parameters:
14#	$1 = program to invoke as "install"
15#	$2 = program to install
16#	$3 = previously-installed program, for reference
17#	$4 = final installed-path, if different from reference
18
19trace=:
20trace=echo
21
22# override locale...
23# (otherwise GNU ls displays date column in a locale-dependent manner).
24LANG=C;		export LANG
25LANGUAGE=C;	export LANGUAGE
26LC_ALL=C;	export LC_ALL
27LC_CTYPE=C;	export LC_CTYPE
28
29OPTS_SUID=
30OPTS_SGID=
31OPTS_MODE=
32OPTS_USR=
33OPTS_GRP=
34
35while test $# != 0
36do
37	case $1 in
38	-*)
39		OPT="$1"
40		shift
41		if test $# != 0
42		then
43			case $OPT in
44			-u)	OPTS_USR="$1"; shift;;
45			-g)	OPTS_GRP="$1"; shift;;
46			-m)	OPTS_MODE="$1"; shift;;
47			esac
48		else
49			break
50		fi
51		;;
52	u+s)	shift;	OPTS_SUID=4000;;
53	g+s)	shift;	OPTS_SGID=2000;;
54	*)	break
55		;;
56	esac
57done
58
59SINSTALL="$1"
60SRC_PROG="$2"
61REF_PROG="$3"
62DST_PROG="$4"
63
64test -z "$SINSTALL" && SINSTALL=install
65test -z "$SRC_PROG" && SRC_PROG=xterm
66test -z "$REF_PROG" && REF_PROG=/usr/bin/X11/xterm
67test -z "$DST_PROG" && DST_PROG="$REF_PROG"
68
69test -n "$OPTS_SUID" && test -n "$OPTS_USR" && REF_PROG=
70test -n "$OPTS_SGID" && test -n "$OPTS_GRP" && REF_PROG=
71
72echo checking for presumed installation-mode
73
74PROG_SUID=
75PROG_SGID=
76PROG_MODE=
77PROG_USR=
78PROG_GRP=
79
80if test -z "$REF_PROG" ; then
81	$trace "... reference program not used"
82elif test -f "$REF_PROG" ; then
83	cf_option="-l -L"
84	MYTEMP=${TMPDIR-/tmp}/sinstall$$
85
86	# Expect listing to have fields like this:
87	#-r--r--r--   1 user      group       34293 Jul 18 16:29 pathname
88	ls $cf_option $REF_PROG
89	ls $cf_option $REF_PROG >$MYTEMP
90	read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <$MYTEMP
91	$trace "... if \"$cf_rest\" is null, try the ls -g option"
92	if test -z "$cf_rest" ; then
93		cf_option="$cf_option -g"
94		ls $cf_option $REF_PROG
95		ls $cf_option $REF_PROG >$MYTEMP
96		read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <$MYTEMP
97	fi
98	rm -f $MYTEMP
99
100	# If we have a pathname, and the date fields look right, assume we've
101	# captured the group as well.
102	$trace "... if \"$cf_rest\" is null, we do not look for group"
103	if test -n "$cf_rest" ; then
104		cf_test=`echo "${cf_date2}${cf_date3}" | sed -e 's/[0-9:]//g'`
105		$trace "... if we have date in proper columns ($cf_date1 $cf_date2 $cf_date3), \"$cf_test\" is null"
106		if test -z "$cf_test" ; then
107			PROG_USR=$cf_usr;
108			PROG_GRP=$cf_grp;
109		fi
110	fi
111	$trace "... derived user \"$PROG_USR\", group \"$PROG_GRP\" of previously-installed $SRC_PROG"
112
113	$trace "... see if mode \"$cf_mode\" has s-bit set"
114	case ".$cf_mode" in #(vi
115	.???s??s*) #(vi
116		$trace "... both setuid/setgid"
117		PROG_SUID=4000
118		PROG_SGID=2000
119		;;
120	.???s*) #(vi
121		$trace "... setuid"
122		PROG_SUID=4000
123		PROG_GRP=
124		;;
125	.??????s*)
126		$trace "... setgid"
127		PROG_SGID=2000
128		PROG_USR=
129		;;
130	esac
131	PROG_MODE=`echo ".$cf_mode" | sed -e 's/^..//' -e 's/rw./7/g' -e 's/r-./5/g' -e 's/---/0/g' -e 's/--[sxt]/1/g' -e 's/+//g'`
132fi
133
134# passed-in options override the reference
135test -n "$OPTS_SUID" && PROG_SUID="$OPTS_SUID"
136test -n "$OPTS_SGID" && PROG_SGID="$OPTS_SGID"
137test -n "$OPTS_MODE" && PROG_MODE="$OPTS_MODE"
138test -n "$OPTS_USR"  && PROG_USR="$OPTS_USR"
139test -n "$OPTS_GRP"  && PROG_GRP="$OPTS_GRP"
140
141# we always need a mode
142test -z "$PROG_MODE" && PROG_MODE=755
143
144if test -n "${PROG_USR}${PROG_GRP}" ; then
145	cf_uid=`id | sed -e 's/^[^=]*=//' -e 's/(.*$//'`
146	cf_usr=`id | sed -e 's/^[^(]*(//' -e 's/).*$//'`
147	cf_grp=`id | sed -e 's/^.* gid=[^(]*(//' -e 's/).*$//'`
148	$trace "... installing $SRC_PROG as user \"$cf_usr\", group \"$cf_grp\""
149	if test "$cf_uid" != 0 ; then
150		PROG_SUID=
151		PROG_SGID=
152		PROG_USR=""
153		PROG_GRP=""
154	fi
155	test "$PROG_USR" = "$cf_usr" && PROG_USR=""
156	test "$PROG_GRP" = "$cf_grp" && PROG_GRP=""
157fi
158
159test -n "${PROG_SUID}${PROG_SGID}" && PROG_MODE=`expr $PROG_MODE % 1000`
160test -n "$PROG_SUID" && PROG_MODE=`expr $PROG_SUID + $PROG_MODE`
161test -n "$PROG_SGID" && PROG_MODE=`expr $PROG_SGID + $PROG_MODE`
162
163test -n "$PROG_USR" && PROG_USR="-o $PROG_USR"
164test -n "$PROG_GRP" && PROG_GRP="-g $PROG_GRP"
165
166echo "$SINSTALL -m $PROG_MODE $PROG_USR $PROG_GRP $SRC_PROG $DST_PROG"
167eval "$SINSTALL -m $PROG_MODE $PROG_USR $PROG_GRP $SRC_PROG $DST_PROG"
168