| 1 |
/*----------------------------------------------------------------------* |
| 2 |
* File: proxy.C |
| 3 |
*----------------------------------------------------------------------* |
| 4 |
* |
| 5 |
* All portions of code are copyright by their respective author/s. |
| 6 |
* Copyright (c) 2006 Marc Lehmann <schmorp@schmorp.de> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*---------------------------------------------------------------------*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "ptytty.h" |
| 26 |
|
| 27 |
#include "estl.h" |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <string.h> |
| 31 |
#include <signal.h> |
| 32 |
|
| 33 |
#include <sys/types.h> |
| 34 |
#include <sys/socket.h> |
| 35 |
#include <unistd.h> |
| 36 |
#include <fcntl.h> |
| 37 |
#include <errno.h> |
| 38 |
|
| 39 |
// helper/proxy support |
| 40 |
|
| 41 |
#if PTYTTY_HELPER |
| 42 |
|
| 43 |
static int sock_fd = -1, lock_fd = -1; |
| 44 |
static int helper_pid; |
| 45 |
|
| 46 |
struct command |
| 47 |
{ |
| 48 |
enum { get, login, destroy } type; |
| 49 |
|
| 50 |
ptytty *id; |
| 51 |
|
| 52 |
bool login_shell; |
| 53 |
int cmd_pid; |
| 54 |
char hostname[512]; // arbitrary, but should be plenty |
| 55 |
}; |
| 56 |
|
| 57 |
struct ptytty_proxy : ptytty |
| 58 |
{ |
| 59 |
ptytty *id; |
| 60 |
|
| 61 |
ptytty_proxy () |
| 62 |
: id(0) |
| 63 |
{ |
| 64 |
} |
| 65 |
|
| 66 |
~ptytty_proxy (); |
| 67 |
|
| 68 |
bool get (); |
| 69 |
void login (int cmd_pid, bool login_shell, const char *hostname); |
| 70 |
}; |
| 71 |
|
| 72 |
#if PTYTTY_REENTRANT |
| 73 |
# define NEED_TOKEN do { char ch; read (lock_fd, &ch , 1); } while (0) |
| 74 |
# define GIVE_TOKEN write (lock_fd, &lock_fd, 1) |
| 75 |
#else |
| 76 |
# define NEED_TOKEN (void)0 |
| 77 |
# define GIVE_TOKEN (void)0 |
| 78 |
#endif |
| 79 |
|
| 80 |
bool |
| 81 |
ptytty_proxy::get () |
| 82 |
{ |
| 83 |
NEED_TOKEN; |
| 84 |
|
| 85 |
command cmd; |
| 86 |
|
| 87 |
cmd.type = command::get; |
| 88 |
|
| 89 |
write (sock_fd, &cmd, sizeof (cmd)); |
| 90 |
|
| 91 |
if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
| 92 |
PTYTTY_FATAL ("protocol error while creating pty using helper process, aborting.\n"); |
| 93 |
|
| 94 |
if (!id) |
| 95 |
{ |
| 96 |
GIVE_TOKEN; |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if ((pty = recv_fd (sock_fd)) < 0 |
| 101 |
|| (tty = recv_fd (sock_fd)) < 0) |
| 102 |
PTYTTY_FATAL ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
| 103 |
|
| 104 |
GIVE_TOKEN; |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
| 110 |
{ |
| 111 |
NEED_TOKEN; |
| 112 |
|
| 113 |
command cmd; |
| 114 |
|
| 115 |
cmd.type = command::login; |
| 116 |
cmd.id = id; |
| 117 |
cmd.cmd_pid = cmd_pid; |
| 118 |
cmd.login_shell = login_shell; |
| 119 |
strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
| 120 |
|
| 121 |
write (sock_fd, &cmd, sizeof (cmd)); |
| 122 |
|
| 123 |
GIVE_TOKEN; |
| 124 |
} |
| 125 |
|
| 126 |
ptytty_proxy::~ptytty_proxy () |
| 127 |
{ |
| 128 |
if (id) |
| 129 |
{ |
| 130 |
close_tty (); |
| 131 |
|
| 132 |
if (pty >= 0) |
| 133 |
close (pty); |
| 134 |
|
| 135 |
NEED_TOKEN; |
| 136 |
|
| 137 |
command cmd; |
| 138 |
|
| 139 |
cmd.type = command::destroy; |
| 140 |
cmd.id = id; |
| 141 |
|
| 142 |
write (sock_fd, &cmd, sizeof (cmd)); |
| 143 |
|
| 144 |
GIVE_TOKEN; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
static |
| 149 |
void serve () |
| 150 |
{ |
| 151 |
command cmd; |
| 152 |
vector<ptytty *> ptys; |
| 153 |
|
| 154 |
for (;;) |
| 155 |
{ |
| 156 |
GIVE_TOKEN; |
| 157 |
|
| 158 |
if (read (sock_fd, &cmd, sizeof (command)) != sizeof (command)) |
| 159 |
break; |
| 160 |
|
| 161 |
if (cmd.type == command::get) |
| 162 |
{ |
| 163 |
// -> id ptyfd ttyfd |
| 164 |
cmd.id = new ptytty_unix; |
| 165 |
|
| 166 |
if (cmd.id->get ()) |
| 167 |
{ |
| 168 |
write (sock_fd, &cmd.id, sizeof (cmd.id)); |
| 169 |
ptys.push_back (cmd.id); |
| 170 |
|
| 171 |
ptytty::send_fd (sock_fd, cmd.id->pty); |
| 172 |
ptytty::send_fd (sock_fd, cmd.id->tty); |
| 173 |
|
| 174 |
cmd.id->close_tty (); |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
delete cmd.id; |
| 179 |
cmd.id = 0; |
| 180 |
write (sock_fd, &cmd.id, sizeof (cmd.id)); |
| 181 |
} |
| 182 |
} |
| 183 |
else if (cmd.type == command::login) |
| 184 |
{ |
| 185 |
#if UTMP_SUPPORT |
| 186 |
if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ()) |
| 187 |
{ |
| 188 |
cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
| 189 |
cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
| 190 |
} |
| 191 |
#endif |
| 192 |
} |
| 193 |
else if (cmd.type == command::destroy) |
| 194 |
{ |
| 195 |
vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id); |
| 196 |
|
| 197 |
if (pty != ptys.end ()) |
| 198 |
{ |
| 199 |
delete *pty; |
| 200 |
ptys.erase (pty); |
| 201 |
} |
| 202 |
} |
| 203 |
else |
| 204 |
break; |
| 205 |
|
| 206 |
NEED_TOKEN; |
| 207 |
} |
| 208 |
|
| 209 |
// destroy all ptys |
| 210 |
for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); ) |
| 211 |
delete *i; |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
ptytty::use_helper () |
| 216 |
{ |
| 217 |
if (sock_fd >= 0) |
| 218 |
return; |
| 219 |
|
| 220 |
int sv[2]; |
| 221 |
|
| 222 |
if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
| 223 |
PTYTTY_FATAL ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
| 224 |
|
| 225 |
#if PTYTTY_REENTRANT |
| 226 |
int lv[2]; |
| 227 |
|
| 228 |
if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv)) |
| 229 |
PTYTTY_FATAL ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
| 230 |
#endif |
| 231 |
|
| 232 |
helper_pid = fork (); |
| 233 |
|
| 234 |
if (helper_pid < 0) |
| 235 |
PTYTTY_FATAL ("could not create pty/sessiondb helper process, aborting.\n"); |
| 236 |
|
| 237 |
if (helper_pid) |
| 238 |
{ |
| 239 |
// client, process |
| 240 |
sock_fd = sv[0]; |
| 241 |
close (sv[1]); |
| 242 |
fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
| 243 |
#if PTYTTY_REENTRANT |
| 244 |
lock_fd = lv[0]; |
| 245 |
close (lv[1]); |
| 246 |
fcntl (lock_fd, F_SETFD, FD_CLOEXEC); |
| 247 |
#endif |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
// server, pty-helper |
| 252 |
sock_fd = sv[1]; |
| 253 |
#if PTYTTY_REENTRANT |
| 254 |
lock_fd = lv[1]; |
| 255 |
#endif |
| 256 |
|
| 257 |
chdir ("/"); |
| 258 |
|
| 259 |
signal (SIGHUP, SIG_IGN); |
| 260 |
signal (SIGTERM, SIG_IGN); |
| 261 |
signal (SIGINT, SIG_IGN); |
| 262 |
signal (SIGPIPE, SIG_IGN); |
| 263 |
|
| 264 |
for (int fd = 0; fd < 1023; fd++) |
| 265 |
if (fd != sock_fd && fd != lock_fd) |
| 266 |
close (fd); |
| 267 |
|
| 268 |
serve (); |
| 269 |
_exit (EXIT_SUCCESS); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
#endif |
| 274 |
|
| 275 |
ptytty * |
| 276 |
ptytty::create () |
| 277 |
{ |
| 278 |
#if PTYTTY_HELPER |
| 279 |
if (helper_pid) |
| 280 |
// use helper process |
| 281 |
return new ptytty_proxy; |
| 282 |
else |
| 283 |
#endif |
| 284 |
return new ptytty_unix; |
| 285 |
} |
| 286 |
|
| 287 |
void |
| 288 |
ptytty::sanitise_stdfd () |
| 289 |
{ |
| 290 |
// sanitise stdin/stdout/stderr to point to *something*. |
| 291 |
for (int fd = 0; fd <= 2; ++fd) |
| 292 |
if (fcntl (fd, F_GETFL) < 0 && errno == EBADF) |
| 293 |
{ |
| 294 |
int fd2 = open ("/dev/tty", fd ? O_WRONLY : O_RDONLY); |
| 295 |
|
| 296 |
if (fd2 < 0) |
| 297 |
fd2 = open ("/dev/null", fd ? O_WRONLY : O_RDONLY); |
| 298 |
|
| 299 |
if (fd2 != fd) |
| 300 |
abort (); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
void |
| 305 |
ptytty::init () |
| 306 |
{ |
| 307 |
sanitise_stdfd (); |
| 308 |
|
| 309 |
uid_t uid = getuid (); |
| 310 |
gid_t gid = getgid (); |
| 311 |
|
| 312 |
// before doing anything else, check for setuid/setgid operation, |
| 313 |
// start the helper process and drop privileges |
| 314 |
if (uid != geteuid () |
| 315 |
|| gid != getegid ()) |
| 316 |
{ |
| 317 |
#if PTYTTY_HELPER |
| 318 |
use_helper (); |
| 319 |
#else |
| 320 |
PTYTTY_WARN ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n"); |
| 321 |
#endif |
| 322 |
|
| 323 |
drop_privileges (); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void |
| 328 |
ptytty::drop_privileges () |
| 329 |
{ |
| 330 |
uid_t uid = getuid (); |
| 331 |
gid_t gid = getgid (); |
| 332 |
|
| 333 |
// drop privileges |
| 334 |
#if HAVE_SETRESUID |
| 335 |
setresgid (gid, gid, gid); |
| 336 |
setresuid (uid, uid, uid); |
| 337 |
#elif HAVE_SETREUID |
| 338 |
setregid (gid, gid); |
| 339 |
setreuid (uid, uid); |
| 340 |
#elif HAVE_SETUID |
| 341 |
setgid (gid); |
| 342 |
setuid (uid); |
| 343 |
#else |
| 344 |
# error no way to drop privileges, configure failed? |
| 345 |
#endif |
| 346 |
|
| 347 |
if (uid != geteuid () |
| 348 |
|| gid != getegid ()) |
| 349 |
PTYTTY_FATAL ("unable to drop privileges, aborting.\n"); |
| 350 |
} |
| 351 |
|