1#!/bin/sh
2# $XTermId: sinstall.sh,v 1.18 2010/03/06 16:19:13 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 1999-2008,2010 by Thomas E. Dickey
7# 
8#                         All Rights Reserved
9# 
10# Permission is hereby granted, free of charge, to any person obtaining a
11# copy of this software and associated documentation files (the
12# "Software"), to deal in the Software without restriction, including
13# without limitation the rights to use, copy, modify, merge, publish,
14# distribute, sublicense, and/or sell copies of the Software, and to
15# permit persons to whom the Software is furnished to do so, subject to
16# the following conditions:
17# 
18# The above copyright notice and this permission notice shall be included
19# in all copies or substantial portions of the Software.
20# 
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28# 
29# Except as contained in this notice, the name(s) of the above copyright
30# holders shall not be used in advertising or otherwise to promote the
31# sale, use or other dealings in this Software without prior written
32# authorization.
33# -----------------------------------------------------------------------------
34#
35# Install program setuid if the installer is running as root, and if xterm is
36# already installed on the system with setuid privilege.  This is a safeguard
37# for ordinary users installing xterm for themselves on systems where the
38# setuid is not needed to access a PTY, but only for things like utmp.
39#
40# Options:
41#	u+s, g+s as in chmod
42#	-u, -g and -m as in install.  If any options are given, $3 is ignored.
43#
44# Parameters:
45#	$1 = program to invoke as "install"
46#	$2 = program to install
47#	$3 = previously-installed program, for reference
48#	$4 = final installed-path, if different from reference
49
50trace=:
51trace=echo
52
53# override locale...
54# (otherwise GNU ls displays date column in a locale-dependent manner).
55LANG=C;		export LANG
56LANGUAGE=C;	export LANGUAGE
57LC_ALL=C;	export LC_ALL
58LC_CTYPE=C;	export LC_CTYPE
59
60OPTS_SUID=
61OPTS_SGID=
62OPTS_MODE=
63OPTS_USR=
64OPTS_GRP=
65
66while test $# != 0
67do
68	case $1 in
69	-*)
70		OPT="$1"
71		shift
72		if test $# != 0
73		then
74			case $OPT in
75			-u)	OPTS_USR="$1"; shift;;
76			-g)	OPTS_GRP="$1"; shift;;
77			-m)	OPTS_MODE="$1"; shift;;
78			esac
79		else
80			break
81		fi
82		;;
83	u+s)	shift;	OPTS_SUID=4000;;
84	g+s)	shift;	OPTS_SGID=2000;;
85	*)	break
86		;;
87	esac
88done
89
90SINSTALL="$1"
91SRC_PROG="$2"
92REF_PROG="$3"
93DST_PROG="$4"
94
95test -z "$SINSTALL" && SINSTALL=install
96test -z "$SRC_PROG" && SRC_PROG=xterm
97test -z "$REF_PROG" && REF_PROG=/usr/bin/X11/xterm
98test -z "$DST_PROG" && DST_PROG="$REF_PROG"
99
100test -n "$OPTS_SUID" && test -n "$OPTS_USR" && REF_PROG=
101test -n "$OPTS_SGID" && test -n "$OPTS_GRP" && REF_PROG=
102
103echo checking for presumed installation-mode
104
105PROG_SUID=
106PROG_SGID=
107PROG_MODE=
108PROG_USR=
109PROG_GRP=
110
111if test -z "$REF_PROG" ; then
112	$trace "... reference program not used"
113elif test -f "$REF_PROG" ; then
114	cf_option="-l -L"
115	MYTEMP=${TMPDIR-/tmp}/sinstall$$
116
117	# Expect listing to have fields like this:
118	#-r--r--r--   1 user      group       34293 Jul 18 16:29 pathname
119	ls $cf_option $REF_PROG
120	ls $cf_option $REF_PROG >$MYTEMP
121	read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <$MYTEMP
122	$trace "... if \"$cf_rest\" is null, try the ls -g option"
123	if test -z "$cf_rest" ; then
124		cf_option="$cf_option -g"
125		ls $cf_option $REF_PROG
126		ls $cf_option $REF_PROG >$MYTEMP
127		read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <$MYTEMP
128	fi
129	rm -f $MYTEMP
130
131	# If we have a pathname, and the date fields look right, assume we've
132	# captured the group as well.
133	$trace "... if \"$cf_rest\" is null, we do not look for group"
134	if test -n "$cf_rest" ; then
135		cf_test=`echo "${cf_date2}${cf_date3}" | sed -e 's/[0-9:]//g'`
136		$trace "... if we have date in proper columns ($cf_date1 $cf_date2 $cf_date3), \"$cf_test\" is null"
137		if test -z "$cf_test" ; then
138			PROG_USR=$cf_usr;
139			PROG_GRP=$cf_grp;
140		fi
141	fi
142	$trace "... derived user \"$PROG_USR\", group \"$PROG_GRP\" of previously-installed $SRC_PROG"
143
144	$trace "... see if mode \"$cf_mode\" has s-bit set"
145	case ".$cf_mode" in #(vi
146	.???s??s*) #(vi
147		$trace "... both setuid/setgid"
148		PROG_SUID=4000
149		PROG_SGID=2000
150		;;
151	.???s*) #(vi
152		$trace "... setuid"
153		PROG_SUID=4000
154		PROG_GRP=
155		;;
156	.??????s*)
157		$trace "... setgid"
158		PROG_SGID=2000
159		PROG_USR=
160		;;
161	esac
162	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/[^0-7]//g'`
163fi
164
165# passed-in options override the reference
166test -n "$OPTS_SUID" && PROG_SUID="$OPTS_SUID"
167test -n "$OPTS_SGID" && PROG_SGID="$OPTS_SGID"
168test -n "$OPTS_MODE" && PROG_MODE="$OPTS_MODE"
169test -n "$OPTS_USR"  && PROG_USR="$OPTS_USR"
170test -n "$OPTS_GRP"  && PROG_GRP="$OPTS_GRP"
171
172# we always need a mode
173test -z "$PROG_MODE" && PROG_MODE=755
174
175if test -n "${PROG_USR}${PROG_GRP}" ; then
176	cf_uid=`id | sed -e 's/^[^=]*=//' -e 's/(.*$//'`
177	cf_usr=`id | sed -e 's/^[^(]*(//' -e 's/).*$//'`
178	cf_grp=`id | sed -e 's/^.* gid=[^(]*(//' -e 's/).*$//'`
179	$trace "... installing $SRC_PROG as user \"$cf_usr\", group \"$cf_grp\""
180	if test "$cf_uid" != 0 ; then
181		PROG_SUID=
182		PROG_SGID=
183		PROG_USR=""
184		PROG_GRP=""
185	fi
186	test "$PROG_USR" = "$cf_usr" && PROG_USR=""
187	test "$PROG_GRP" = "$cf_grp" && PROG_GRP=""
188fi
189
190test -n "${PROG_SUID}${PROG_SGID}" && PROG_MODE=`expr $PROG_MODE % 1000`
191test -n "$PROG_SUID" && PROG_MODE=`expr $PROG_SUID + $PROG_MODE`
192test -n "$PROG_SGID" && PROG_MODE=`expr $PROG_SGID + $PROG_MODE`
193
194test -n "$PROG_USR" && PROG_USR="-o $PROG_USR"
195test -n "$PROG_GRP" && PROG_GRP="-g $PROG_GRP"
196
197echo "$SINSTALL -m $PROG_MODE $PROG_USR $PROG_GRP $SRC_PROG $DST_PROG"
198eval "$SINSTALL -m $PROG_MODE $PROG_USR $PROG_GRP $SRC_PROG $DST_PROG"
199