-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuexec.go
215 lines (185 loc) · 5.03 KB
/
suexec.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package suexec
import (
"os"
"os/user"
"strconv"
"strings"
"syscall"
)
type Suexec struct {
cmd string
args []string
param *Param
}
type Param struct {
Args []string
Log *Log
Uid int
Cwd string
}
func NewSuexec(p Param) *Suexec {
return &Suexec{param: &p}
}
func (self *Suexec) Exec(environ []string) *SuexecError {
cmd := self.param.Args[3]
/*
* (I can't help myself...sorry.)
*
* Uh oh. Still here. Where's the kaboom? There was supposed to be an
* EARTH-shattering kaboom!
*
* Oh well, log the failure and error out.
*/
if err := syscall.Exec(cmd, []string{cmd}, environ); err != nil {
return NewSuexecError(255, "(%d) %s: failed(%s)", err, err, cmd)
}
return nil
}
func (self *Suexec) VerifyToSuexec() *SuexecError {
var userdir bool = false
args := self.param.Args
cwd := self.param.Cwd
log := self.param.Log
original_uid := self.param.Uid
/*
* Check existence/validity of the UID of the user
* running this program. Error out if invalid.
*/
pw, err := user.LookupId(strconv.Itoa(original_uid))
if err != nil {
return NewSuexecError(102, "crit: invalid uid: (%d) %s", original_uid, err)
}
/*
* See if this is a 'how were you compiled' request, and
* comply if so.
*/
if len(args) > 1 && args[1] == "-V" && pw.Uid == "0" {
PrintConstants()
return NewSuexecError(0, "")
}
/*
* If there are a proper number of arguments, set
* all of them to variables. Otherwise, error out.
*/
if len(args) < 4 {
return NewSuexecError(101, "too few arguments")
}
target_uname := args[1]
target_gname := args[2]
cmd := args[3]
/*
* Check to see if the user running this program
* is the user allowed to do so as defined in
* suexec.h. If not the allowed user, error out.
*/
if AP_HTTPD_USER != pw.Username {
return NewSuexecError(103, "user mismatch (%s instead os %s)", pw.Username, AP_HTTPD_USER)
}
/*
* Check to see if this is a ~userdir request. If
* so, set the flag, and remove the '~' from the
* target username.
*/
userdir = IsUserdirEnabled(target_uname)
/*
* Error out if the target username is invalid.
*/
pw, err = Lookup(target_uname)
if err != nil {
return NewSuexecError(121, "invalid target user: (%s)", target_uname)
}
/*
* Error out if the target group name is invalid.
*/
gr, err := LookupGroup(target_gname)
if err != nil {
return NewSuexecError(106, "invalid target group name: (%s)", target_gname)
}
gid, err := strconv.Atoi(gr.Gid)
if err != nil {
log.LogErr("failed to strconv.Atoi: (%v)", err)
}
actual_gname := gr.Name
/*
* Save these for later since initgroups will hose the struct
*/
uid, err := strconv.Atoi(pw.Uid)
if err != nil {
return NewSuexecError(255, "failed to strconv.Atoi: (%v)", err)
}
actual_uname := pw.Username
// target_homedir := pw.HomeDir
/*
* Log the transaction here to be sure we have an open log
* before we setuid().
*/
log.LogNoErr("uid: (%s/%s) gid: (%s/%s) cmd: %s\n",
target_uname, actual_uname,
target_gname, actual_gname,
cmd)
/*
* Error out if attempt is made to execute as root or as
* a UID less than AP_UID_MIN. Tsk tsk.
*/
if uid == 0 || uid < AP_UID_MIN {
return NewSuexecError(107, "cannot run as forbidden uid (%d/%s)", uid, cmd)
}
/*
* Error out if attempt is made to execute as root group
* or as a GID less than AP_GID_MIN. Tsk tsk.
*/
if gid == 0 || (gid < AP_GID_MIN) {
return NewSuexecError(108, "cannot run as forbidden gid (%d/%s)", gid, cmd)
}
/*
* Change UID/GID here so that the following tests work over NFS.
*
* Initialize the group access list for the target user,
* and setgid() to the target group. If unsuccessful, error out.
*/
if err := syscall.Setgid(gid); err != nil {
return NewSuexecError(109, "failed to setgid (%d: %s)", gid, cmd)
}
/*
* setuid() to the target user. Error out on fail.
*/
if err := syscall.Setuid(uid); err != nil {
return NewSuexecError(110, "failed to setuid (%d: %s)", uid, cmd)
}
/*
* Get the current working directory, as well as the proper
* document root (dependant upon whether or not it is a
* ~userdir request). Error out if we cannot get either one,
* or if the current working directory is not in the docroot.
* Use chdir()s and getcwd()s to avoid problems with symlinked
* directories. Yuck.
*/
var dwd string
if userdir {
/* todo */
} else {
/* oops */
if err = os.Chdir(AP_DOC_ROOT); err != nil {
return NewSuexecError(113, "cannot get docroot information (%s)", AP_DOC_ROOT)
}
dwd, err = os.Getwd()
if err != nil {
return NewSuexecError(113, "cannot get docroot information (%s)", AP_DOC_ROOT)
}
if err = os.Chdir(cwd); err != nil {
return NewSuexecError(113, "cannot get docroot information (%s)", AP_DOC_ROOT)
}
}
if !strings.HasPrefix(cwd, dwd) {
return NewSuexecError(114, "command not in docroot (%s/%s)", cwd, cmd)
}
script, err := NewScript(cmd, cwd)
if err != nil {
return NewSuexecError(1, "%v", err)
}
if suexec_err := script.VerifyToSuexec(uid, gid); err != nil {
return suexec_err
}
log.SetCloseOnExec()
return nil
}