Debian Codegraph @

src/getpeername.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2011 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_GETPEERNAME
24
25 #define _GNU_SOURCE
26 #include <stddef.h>
27 #include <sys/socket.h>
28
29 #ifdef AF_UNIX
30
31 #include <sys/un.h>
32
33 #include "libfakechroot.h"
34 #include "strlcpy.h"
35
36 #ifdef HAVE_GETPEERNAME_TYPE_ARG2___SOCKADDR_ARG__
37 # define SOCKADDR(addr) ((addr).__sockaddr__)
38 # define SOCKADDR_UN(addr) ((addr).__sockaddr_un__)
39 #else
40 # define SOCKADDR(addr) (addr)
41 # define SOCKADDR_UN(addr) (addr)
42 #endif
43
44
45 wrapper(getpeername, int, (int s, GETPEERNAME_TYPE_ARG2(addr), socklen_t * addrlen))
46 {
47 int status;
48 socklen_t origlen = *addrlen;
49
50 debug("getpeername(%d, &addr, &addrlen)", s);
51
52 status = nextcall(getpeername)(s, addr, addrlen);
53 if (status == 0 && SOCKADDR(addr)->sa_family == AF_UNIX) {
54 struct sockaddr_un *addr_un = SOCKADDR_UN(addr);
55 size_t path_max = origlen - offsetof(struct sockaddr_un, sun_path);
56 if (path_max > origlen) {
57 /* underflow, addr does not have space for the path */
58 return status;
59 } else if (path_max > sizeof(addr_un->sun_path)) {
60 path_max = sizeof(addr_un->sun_path);
61 }
62 if (addr_un->sun_path && *(addr_un->sun_path)) {
63 char tmp[FAKECHROOT_PATH_MAX];
64 strlcpy(tmp, addr_un->sun_path, FAKECHROOT_PATH_MAX);
65 narrow_chroot_path(tmp);
66 strlcpy(addr_un->sun_path, tmp, path_max);
67 *addrlen = SUN_LEN(addr_un);
68 }
69 }
70
71 return status;
72 }
73
74 #else
75 typedef int empty_translation_unit;
76 #endif
77
78 #else
79 typedef int empty_translation_unit;
80 #endif