summaryrefslogtreecommitdiff
path: root/sinstall.sh
blob: fa85ec71d11c32b4c3514ba5748ae417f3467b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/sh
# $XTermId: sinstall.sh,v 1.18 2010/03/06 16:19:13 tom Exp $
# -----------------------------------------------------------------------------
# this file is part of xterm
#
# Copyright 1999-2008,2010 by Thomas E. Dickey
# 
#                         All Rights Reserved
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
# -----------------------------------------------------------------------------
#
# Install program setuid if the installer is running as root, and if xterm is
# already installed on the system with setuid privilege.  This is a safeguard
# for ordinary users installing xterm for themselves on systems where the
# setuid is not needed to access a PTY, but only for things like utmp.
#
# Options:
#	u+s, g+s as in chmod
#	-u, -g and -m as in install.  If any options are given, $3 is ignored.
#
# Parameters:
#	$1 = program to invoke as "install"
#	$2 = program to install
#	$3 = previously-installed program, for reference
#	$4 = final installed-path, if different from reference

trace=:
trace=echo

# override locale...
# (otherwise GNU ls displays date column in a locale-dependent manner).
LANG=C;		export LANG
LANGUAGE=C;	export LANGUAGE
LC_ALL=C;	export LC_ALL
LC_CTYPE=C;	export LC_CTYPE

OPTS_SUID=
OPTS_SGID=
OPTS_MODE=
OPTS_USR=
OPTS_GRP=

while test $# != 0
do
	case $1 in
	-*)
		OPT="$1"
		shift
		if test $# != 0
		then
			case $OPT in
			-u)	OPTS_USR="$1"; shift;;
			-g)	OPTS_GRP="$1"; shift;;
			-m)	OPTS_MODE="$1"; shift;;
			esac
		else
			break
		fi
		;;
	u+s)	shift;	OPTS_SUID=4000;;
	g+s)	shift;	OPTS_SGID=2000;;
	*)	break
		;;
	esac
done

SINSTALL="$1"
SRC_PROG="$2"
REF_PROG="$3"
DST_PROG="$4"

test -z "$SINSTALL" && SINSTALL=install
test -z "$SRC_PROG" && SRC_PROG=xterm
test -z "$REF_PROG" && REF_PROG=/usr/bin/X11/xterm
test -z "$DST_PROG" && DST_PROG="$REF_PROG"

test -n "$OPTS_SUID" && test -n "$OPTS_USR" && REF_PROG=
test -n "$OPTS_SGID" && test -n "$OPTS_GRP" && REF_PROG=

echo checking for presumed installation-mode

PROG_SUID=
PROG_SGID=
PROG_MODE=
PROG_USR=
PROG_GRP=

if test -z "$REF_PROG" ; then
	$trace "... reference program not used"
elif test -f "$REF_PROG" ; then
	cf_option="-l -L"
	MYTEMP=${TMPDIR-/tmp}/sinstall$$

	# Expect listing to have fields like this:
	#-r--r--r--   1 user      group       34293 Jul 18 16:29 pathname
	ls $cf_option $REF_PROG
	ls $cf_option $REF_PROG >$MYTEMP
	read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <$MYTEMP
	$trace "... if \"$cf_rest\" is null, try the ls -g option"
	if test -z "$cf_rest" ; then
		cf_option="$cf_option -g"
		ls $cf_option $REF_PROG
		ls $cf_option $REF_PROG >$MYTEMP
		read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <$MYTEMP
	fi
	rm -f $MYTEMP

	# If we have a pathname, and the date fields look right, assume we've
	# captured the group as well.
	$trace "... if \"$cf_rest\" is null, we do not look for group"
	if test -n "$cf_rest" ; then
		cf_test=`echo "${cf_date2}${cf_date3}" | sed -e 's/[0-9:]//g'`
		$trace "... if we have date in proper columns ($cf_date1 $cf_date2 $cf_date3), \"$cf_test\" is null"
		if test -z "$cf_test" ; then
			PROG_USR=$cf_usr;
			PROG_GRP=$cf_grp;
		fi
	fi
	$trace "... derived user \"$PROG_USR\", group \"$PROG_GRP\" of previously-installed $SRC_PROG"

	$trace "... see if mode \"$cf_mode\" has s-bit set"
	case ".$cf_mode" in #(vi
	.???s??s*) #(vi
		$trace "... both setuid/setgid"
		PROG_SUID=4000
		PROG_SGID=2000
		;;
	.???s*) #(vi
		$trace "... setuid"
		PROG_SUID=4000
		PROG_GRP=
		;;
	.??????s*)
		$trace "... setgid"
		PROG_SGID=2000
		PROG_USR=
		;;
	esac
	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'`
fi

# passed-in options override the reference
test -n "$OPTS_SUID" && PROG_SUID="$OPTS_SUID"
test -n "$OPTS_SGID" && PROG_SGID="$OPTS_SGID"
test -n "$OPTS_MODE" && PROG_MODE="$OPTS_MODE"
test -n "$OPTS_USR"  && PROG_USR="$OPTS_USR"
test -n "$OPTS_GRP"  && PROG_GRP="$OPTS_GRP"

# we always need a mode
test -z "$PROG_MODE" && PROG_MODE=755

if test -n "${PROG_USR}${PROG_GRP}" ; then
	cf_uid=`id | sed -e 's/^[^=]*=//' -e 's/(.*$//'`
	cf_usr=`id | sed -e 's/^[^(]*(//' -e 's/).*$//'`
	cf_grp=`id | sed -e 's/^.* gid=[^(]*(//' -e 's/).*$//'`
	$trace "... installing $SRC_PROG as user \"$cf_usr\", group \"$cf_grp\""
	if test "$cf_uid" != 0 ; then
		PROG_SUID=
		PROG_SGID=
		PROG_USR=""
		PROG_GRP=""
	fi
	test "$PROG_USR" = "$cf_usr" && PROG_USR=""
	test "$PROG_GRP" = "$cf_grp" && PROG_GRP=""
fi

test -n "${PROG_SUID}${PROG_SGID}" && PROG_MODE=`expr $PROG_MODE % 1000`
test -n "$PROG_SUID" && PROG_MODE=`expr $PROG_SUID + $PROG_MODE`
test -n "$PROG_SGID" && PROG_MODE=`expr $PROG_SGID + $PROG_MODE`

test -n "$PROG_USR" && PROG_USR="-o $PROG_USR"
test -n "$PROG_GRP" && PROG_GRP="-g $PROG_GRP"

echo "$SINSTALL -m $PROG_MODE $PROG_USR $PROG_GRP $SRC_PROG $DST_PROG"
eval "$SINSTALL -m $PROG_MODE $PROG_USR $PROG_GRP $SRC_PROG $DST_PROG"