… | |
… | |
31 | |
31 | |
32 | #include <sys/types.h> |
32 | #include <sys/types.h> |
33 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
34 | #include <unistd.h> |
34 | #include <unistd.h> |
35 | #include <fcntl.h> |
35 | #include <fcntl.h> |
|
|
36 | #include <errno.h> |
36 | |
37 | |
37 | // helper/proxy support |
38 | // helper/proxy support |
38 | |
39 | |
39 | #if PTYTTY_HELPER |
40 | #if PTYTTY_HELPER |
40 | |
41 | |
… | |
… | |
123 | |
124 | |
124 | ptytty_proxy::~ptytty_proxy () |
125 | ptytty_proxy::~ptytty_proxy () |
125 | { |
126 | { |
126 | if (id) |
127 | if (id) |
127 | { |
128 | { |
|
|
129 | close_tty (); |
|
|
130 | |
|
|
131 | if (pty >= 0) |
128 | close (pty); |
132 | close (pty); |
129 | |
133 | |
130 | NEED_TOKEN; |
134 | NEED_TOKEN; |
131 | |
135 | |
132 | command cmd; |
136 | command cmd; |
133 | |
137 | |
… | |
… | |
292 | #endif |
296 | #endif |
293 | return new ptytty_unix; |
297 | return new ptytty_unix; |
294 | } |
298 | } |
295 | |
299 | |
296 | void |
300 | void |
|
|
301 | ptytty::sanitise_stdfd () |
|
|
302 | { |
|
|
303 | // sanitise stdin/stdout/stderr to point to *something*. |
|
|
304 | for (int fd = 0; fd <= 2; ++fd) |
|
|
305 | if (fcntl (fd, F_GETFL) < 0 && errno == EBADF) |
|
|
306 | { |
|
|
307 | int fd2 = open ("/dev/tty", fd ? O_WRONLY : O_RDONLY); |
|
|
308 | |
|
|
309 | if (fd2 < 0) |
|
|
310 | fd2 = open ("/dev/null", fd ? O_WRONLY : O_RDONLY); |
|
|
311 | |
|
|
312 | if (fd2 != fd) |
|
|
313 | abort (); |
|
|
314 | } |
|
|
315 | } |
|
|
316 | |
|
|
317 | void |
297 | ptytty::init () |
318 | ptytty::init () |
298 | { |
319 | { |
|
|
320 | sanitise_stdfd (); |
|
|
321 | |
299 | uid_t uid = getuid (); |
322 | uid_t uid = getuid (); |
300 | gid_t gid = getgid (); |
323 | gid_t gid = getgid (); |
301 | |
324 | |
302 | // before doing anything else, check for setuid/setgid operation, |
325 | // before doing anything else, check for setuid/setgid operation, |
303 | // start the helper process and drop privileges |
326 | // start the helper process and drop privileges |
304 | if (uid != geteuid () |
327 | if (uid != geteuid () |
305 | || gid != getegid ()) |
328 | || gid != getegid ()) |
306 | { |
329 | { |