forked from lispmeister/rpxp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runasuser.sh
executable file
·183 lines (157 loc) · 3.9 KB
/
runasuser.sh
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
#!/bin/bash
this_program="$0"
function usage_message()
{
cat <<EOF
${this_program}: usage:
$0 [-h | --help] [options] [program [args]]
Create a user/group and run program as that user.
If no user is specified, run as the calling user (assumed to be root).
If no program is specified, run /bin/bash.
options:
-h --help help
-U USER --user username to create (default: root)
-u UID --uid uid for user
-G GROUP --group group name for user
-g GID --gid gid for user
-s G1,G2 --supplemental supplemental groups (comma separated)
program program to start (default: /bin/bash)
args arguments of program to start
EOF
}
function usage()
{
usage_message >&2
exit 1
}
user=""
uid=""
group=""
gid=""
supplemental=""
default_program="/bin/bash"
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
usage
;;
-U|--user)
if [ "$#" -gt 1 ]; then
user="$2"
shift
else
echo "$this_program: missing user argument" >&2
usage
fi
;;
-u|--uid)
if [ "$#" -gt 1 ]; then
uid="$2"
shift
else
echo "$this_program: missing uid argument" >&2
usage
fi
;;
-G|--group)
if [ "$#" -gt 1 ]; then
group="$2"
shift
else
echo "$this_program: missing group argument" >&2
usage
fi
;;
-g|--gid)
if [ "$#" -gt 1 ]; then
gid="$2"
shift
else
echo "$this_program: missing gid argument" >&2
usage
fi
;;
-S|--supplemental)
if [ "$#" -gt 1 ]; then
supplemental="$2"
shift
else
echo "$this_program: missing supplemental argument" >&2
usage
fi
;;
--)
shift
break
;;
-?*)
echo "$this_program: unknown option $1" >&2
usage
;;
*)
break
;;
esac
shift
done
#echo "user $user"
#echo "uid $uid"
#echo "group $group"
#echo "gid $gid"
#echo "supplemental $supplemental"
#echo "program -$@-"
if [ -n "$user" ]; then
if [ -z "$uid" ]; then
echo "$this_program: missing uid" >&2
usage
elif [ -z "$group" ]; then
echo "$this_program: missing group" >&2
usage
elif [ -z "$gid" ]; then
echo "$this_program: missing gid" >&2
usage
fi
sup_g=""
if [ -n "$supplemental" ]; then
sup_g="-G $supplemental"
fi
groupadd -f -g "$gid" "$group"
if [ $? -ne 0 ]; then
echo "$this_program: error adding group: $?" >&2
exit 1
fi
useradd -M -g "$group" $sup_g -u "$uid" "$user"
if [ $? -ne 0 ]; then
echo "$this_program: error adding user: $?" >&2
exit 1
fi
fi
# cases
# no user, no program
# exec /bin/bash
# no user, program
# exec program
# user, no program
# exec su -c /bin/bash $user
# user, program
# exec su -c "$@" $user
#
if [ -n "$user" ]; then
# specified user
if [ -n "$1" ]; then
exec su -c "$(printf "%q " "$@")" -s /bin/bash "$user"
else
exec su -s /bin/bash "$user"
fi
else
# use default user (assumed root)
eff_uid="$(id -u)"
if [ "$eff_uid" != "0" ]; then
echo "$this_program: warning: command running as uid $eff_uid instead of 0" >&2
fi
if [ -n "$1" ]; then
exec "$@"
else
exec /bin/bash
fi
fi