… | |
… | |
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 | |
… | |
… | |
295 | #endif |
296 | #endif |
296 | return new ptytty_unix; |
297 | return new ptytty_unix; |
297 | } |
298 | } |
298 | |
299 | |
299 | 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 |
300 | ptytty::init () |
318 | ptytty::init () |
301 | { |
319 | { |
|
|
320 | sanitise_stdfd (); |
|
|
321 | |
302 | uid_t uid = getuid (); |
322 | uid_t uid = getuid (); |
303 | gid_t gid = getgid (); |
323 | gid_t gid = getgid (); |
304 | |
324 | |
305 | // before doing anything else, check for setuid/setgid operation, |
325 | // before doing anything else, check for setuid/setgid operation, |
306 | // start the helper process and drop privileges |
326 | // start the helper process and drop privileges |