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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
* Copyright (c) 2003, 2004, 2005 Nokia
* Author: Timo Savola <tsavola@movial.fi>
*
* This program is licensed under GPL (see COPYING for details)
*/
#include "fakeroot.h"
#include "types.h"
#include "common.h"
#include "daemon.h"
#include "mount.h"
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/stat.h>
extern char **environ;
typedef enum {
DAEMON_TO_CLIENT,
CLIENT_TO_DAEMON
} direction_t;
struct node_s;
typedef struct node_s {
struct node_s *next;
int client;
int daemon;
fake_dev_t stored_dev;
} node_t;
static volatile bool_t parent_alive = TRUE;
static volatile int listen_sd = -1;
static uint32_t relay_id;
static void append_node(handler_t *data, node_t **head, int client_sd, int daemon_sd)
{
node_t *node, *n;
node = calloc(1, sizeof (node_t));
if (!node) {
errno = 0;
send_error(data, oom);
exit(1);
}
node->client = client_sd;
node->daemon = daemon_sd;
if (*head) {
for (n = *head; n->next; n = n->next);
n->next = node;
} else {
*head = node;
}
}
static void remove_node(node_t **head, node_t *node)
{
node_t *n;
if (*head == node) {
*head = node->next;
} else {
for (n = *head; n->next != node; n = n->next);
n->next = node->next;
}
debug("Removing node: client=%d, daemon=%d", node->client, node->daemon);
close(node->client);
close(node->daemon);
free(node);
}
static void get_device_numbers(handler_t *data)
{
mount_t **ptr;
if (!data->mounts) {
debug("No mounts");
return;
}
for (ptr = data->mounts; *ptr; ptr++) {
mount_t *m = *ptr;
if (m->info.type != MTYPE_BIND && m->point_dev == 0LL) {
struct stat buf;
if (stat(m->info.point, &buf) < 0) {
send_error(data, "Can't stat %s", m->info.point);
exit(1);
}
m->point_dev = buf.st_dev;
debug("Device number of %s is %lld", m->info.device, m->info.device_dev);
debug("Device number of %s is %lld", m->info.point, m->point_dev);
}
}
}
static bool_t translate_dev(handler_t *data, node_t *node, struct fakestat *st)
{
mount_t **ptr;
const fake_dev_t old = ntohll(st->dev);
node->stored_dev = st->dev;
if (!data->mounts) {
debug("No mounts");
return FALSE;
}
for (ptr = data->mounts; *ptr; ptr++) {
const mount_t *m = *ptr;
if (old == m->point_dev) {
debug("Translating device number: %lld -> %lld", old, m->info.device_dev);
st->dev = htonll(m->info.device_dev);
return TRUE;
}
}
debug("No match for device number %lld", old);
return FALSE;
}
static void restore_dev(node_t *node, struct fakestat *st)
{
debug("Restoring device number");
st->dev = node->stored_dev;
}
static int copy_msg(handler_t *data, node_t *node, direction_t direction)
{
int source, destination;
struct fake_msg buf;
ssize_t len;
if (direction == CLIENT_TO_DAEMON) {
source = node->client;
destination = node->daemon;
debug("Processing message from client=%d", source);
} else {
source = node->daemon;
destination = node->client;
debug("Processing message from daemon=%d", source);
}
while (1) {
len = read(source, &buf, sizeof (buf));
if (len >= 0)
break;
if (errno != EINTR) {
send_error(data, "Can't read from socket");
exit(1);
}
}
if (len == 0) {
debug("No message");
return -1;
}
if (direction == CLIENT_TO_DAEMON) {
if (translate_dev(data, node, &buf.st)) {
buf.remote = htonl(0);
} else if (ntohl(buf.remote) == 0) {
debug("Setting remote ID");
buf.remote = htonl(relay_id);
}
} else {
restore_dev(node, &buf.st);
}
if (direction == CLIENT_TO_DAEMON) {
debug("Forwarding message to daemon=%d", destination);
} else {
debug("Forwarding message to client=%d", destination);
}
while (1) {
if (write(destination, &buf, sizeof (buf)) >= 0)
break;
if (errno != EINTR) {
send_error(data, "Can't write to socket");
exit(1);
}
}
return 0;
}
static int get_daemon(handler_t *data, struct sockaddr_in *addr)
{
int sd;
sd = socket(PF_INET, SOCK_STREAM, 0);
if (sd < 0) {
send_error(data, "Can't create socket");
exit(1);
}
if (connect(sd, (struct sockaddr *) addr, sizeof (struct sockaddr_in)) < 0) {
if (errno == EINTR) {
debug("Connect interrupted");
exit(0);
}
send_error(data, "Connect failed");
exit(1);
}
debug("Connected to daemon=%d", sd);
return sd;
}
static int get_client(handler_t *data)
{
struct sockaddr_in addr;
socklen_t len = sizeof (addr);
int sd;
sd = accept(listen_sd, (struct sockaddr *) &addr, &len);
if (sd < 0) {
if (errno == EINTR) {
debug("Accept interrupted");
exit(0);
}
send_error(data, "Accept failed");
exit(1);
}
debug("Connection from client=%d", sd);
return sd;
}
static void do_relay(handler_t *data, int port)
{
struct hostent *host;
struct sockaddr_in addr = { 0 };
node_t *sd_head = NULL;
fd_set fds;
host = gethostbyname(data->host);
if (!host) {
send_error(data, "Can't resolve host: %s", data->host);
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = *(unsigned int *) host->h_addr;
addr.sin_port = htons(port);
debug("Fakeroot daemon assumed to be at %s:%d", data->host, port);
while (parent_alive) {
int maxfd, count;
node_t *n, *next;
FD_ZERO(&fds);
FD_SET(listen_sd, &fds);
maxfd = listen_sd;
for (n = sd_head, count = 0; n; n = n->next, count++) {
FD_SET(n->daemon, &fds);
FD_SET(n->client, &fds);
maxfd = MAX(maxfd, n->daemon);
maxfd = MAX(maxfd, n->client);
}
debug("Selecting (%d nodes)", count);
if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 0) {
if (!parent_alive) {
debug("SIGEXIT caught during select call");
break;
}
if (errno == EINTR) {
debug("Select interrupted");
continue;
}
send_error(data, "Select failed");
exit(1);
}
for (n = sd_head; n; n = next) {
next = n->next;
if (FD_ISSET(n->daemon, &fds) && copy_msg(data, n, DAEMON_TO_CLIENT) < 0) {
remove_node(&sd_head, n);
continue;
}
if (FD_ISSET(n->client, &fds) && copy_msg(data, n, CLIENT_TO_DAEMON) < 0) {
remove_node(&sd_head, n);
}
}
if (FD_ISSET(listen_sd, &fds)) {
int cli, dae;
cli = get_client(data);
dae = get_daemon(data, &addr);
append_node(data, &sd_head, cli, dae);
}
}
debug("Exiting");
}
static void sig_exit(int sig)
{
int stored_errno = errno;
parent_alive = FALSE;
if (listen_sd >= 0)
close(listen_sd);
errno = stored_errno;
}
static pid_t fork_relay(handler_t *data, uint16_t *portp)
{
int faked_port, sd;
struct sockaddr_in addr;
socklen_t len;
uint32_t id;
pid_t pid;
struct sigaction act_exit;
faked_port = *portp;
sd = socket(PF_INET, SOCK_STREAM, 0);
if (sd < 0) {
send_error(data, "Can't create socket");
return -1;
}
if (setsockopt_bool(sd, SOL_SOCKET, SO_REUSEADDR, TRUE)) {
send_error(data, "Can't set socket option: SO_REUSEADDR");
goto _error;
}
if (listen(sd, SOMAXCONN) < 0) {
send_error(data, "Can't listen to socket");
goto _error;
}
len = sizeof (addr);
if (getsockname(sd, (struct sockaddr *) &addr, &len) < 0) {
send_error(data, "Can't get name of relay listening socket");
goto _error;
}
*portp = ntohs(addr.sin_port);
len = sizeof (addr);
if (getsockname(data->sd, (struct sockaddr *) &addr, &len) < 0) {
send_error(data, "Can't get name of client connection socket");
goto _error;
}
id = ntohl(addr.sin_addr.s_addr);
pid = fork();
if (pid < 0) {
send_error(data, "Can't fork");
goto _error;
}
if (pid > 0) {
/* Parent: */
close(sd);
return pid;
}
/* Child: */
set_debug_name("RELAY");
debug("Relaying fakeroot messages at port %d", *portp);
debug("Remote ID of this relay is 0x%lx", id);
daemonize(sd);
act_exit.sa_handler = sig_exit;
sigemptyset(&act_exit.sa_mask);
act_exit.sa_flags = 0;
sigaction(SIGHUP, &act_exit, NULL);
sigaction(SIGTERM, &act_exit, NULL);
get_device_numbers(data);
listen_sd = sd;
relay_id = id;
do_relay(data, faked_port);
exit(0);
_error:
close(sd);
return -1;
}
pid_t fakeroot_relay(handler_t *data)
{
char **old_environ, *str;
uint16_t port;
pid_t pid;
/* TODO: better. */
old_environ = environ;
environ = data->param.environ;
str = getenv(FAKEROOTKEY_ENV);
environ = old_environ;
if (!str || !*str)
return 0;
debug("Creating relay process");
port = atoi(str);
if (port == 0) {
send_error(data, "Invalid " FAKEROOTKEY_ENV ": %s", str);
return -1;
}
pid = fork_relay(data, &port);
if (pid < 0)
return -1;
data->fakerootkey = malloc(strlen(FAKEROOTKEY_ENV) + 1 + 5 + 1);
if (!data->fakerootkey) {
errno = 0;
send_error(data, oom);
goto _kill;
}
sprintf(data->fakerootkey, FAKEROOTKEY_ENV "=%d", port);
return pid;
_kill:
kill(pid, SIGTERM);
return -1;
}
|