| 1 |
/* |
| 2 |
libfakechroot -- fake chroot environment |
| 3 |
Copyright (c) 2010, 2013 Piotr Roszatycki <[email protected]> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public |
| 16 |
License along with this library; if not, write to the Free Software |
| 17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#ifdef HAVE_CONNECT |
| 24 |
|
| 25 |
#define _GNU_SOURCE |
| 26 |
#include <sys/types.h> |
| 27 |
#include <sys/socket.h> |
| 28 |
|
| 29 |
#ifdef AF_UNIX |
| 30 |
|
| 31 |
#include <sys/un.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <stdio.h> |
| 34 |
|
| 35 |
#include "libfakechroot.h" |
| 36 |
#include "strlcpy.h" |
| 37 |
|
| 38 |
#ifdef HAVE_CONNECT_TYPE_ARG2___CONST_SOCKADDR_ARG__ |
| 39 |
# define SOCKADDR_UN(addr) ((addr).__sockaddr_un__) |
| 40 |
#else |
| 41 |
# define SOCKADDR_UN(addr) (addr) |
| 42 |
#endif |
| 43 |
|
| 44 |
|
| 45 |
wrapper(connect, int, (int sockfd, CONNECT_TYPE_ARG2(addr), socklen_t addrlen)) |
| 46 |
{ |
| 47 |
char fakechroot_abspath[FAKECHROOT_PATH_MAX]; |
| 48 |
char fakechroot_buf[FAKECHROOT_PATH_MAX]; |
| 49 |
struct sockaddr_un *addr_un = (struct sockaddr_un *)SOCKADDR_UN(addr); |
| 50 |
char tmp[FAKECHROOT_PATH_MAX]; |
| 51 |
|
| 52 |
debug("connect(%d, &addr, %d)", sockfd, addrlen); |
| 53 |
|
| 54 |
if (addr_un->sun_family == AF_UNIX && addr_un->sun_path && *(addr_un->sun_path)) { |
| 55 |
socklen_t newaddrlen; |
| 56 |
struct sockaddr_un newaddr_un; |
| 57 |
|
| 58 |
const char *af_unix_path = getenv("FAKECHROOT_AF_UNIX_PATH"); |
| 59 |
const int af_unix_path_max = sizeof(addr_un->sun_path); |
| 60 |
const char *path = addr_un->sun_path; |
| 61 |
|
| 62 |
if (af_unix_path != NULL) { |
| 63 |
tmp[af_unix_path_max + 1] = '\0'; |
| 64 |
snprintf(tmp, af_unix_path_max + 1, "%s/%s", af_unix_path, path); |
| 65 |
path = tmp; |
| 66 |
} |
| 67 |
else { |
| 68 |
expand_chroot_path(path); |
| 69 |
} |
| 70 |
|
| 71 |
if (strlen(path) >= af_unix_path_max) { |
| 72 |
__set_errno(ENAMETOOLONG); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
memset(&newaddr_un, 0, sizeof(struct sockaddr_un)); |
| 76 |
newaddr_un.sun_family = addr_un->sun_family; |
| 77 |
strlcpy(newaddr_un.sun_path, path, sizeof(newaddr_un.sun_path)); |
| 78 |
newaddrlen = SUN_LEN(&newaddr_un); |
| 79 |
return nextcall(connect)(sockfd, (struct sockaddr *)&newaddr_un, newaddrlen); |
| 80 |
} |
| 81 |
return nextcall(connect)(sockfd, addr, addrlen); |
| 82 |
} |
| 83 |
|
| 84 |
#else |
| 85 |
typedef int empty_translation_unit; |
| 86 |
#endif |
| 87 |
|
| 88 |
#else |
| 89 |
typedef int empty_translation_unit; |
| 90 |
#endif |