3 * Make programs use Unix-domain sockets instead of IP
5 * (c) 2008 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the preload-hacks package.
12 * Preload-hacks are free software; you can redistribute it and/or modify
13 * them under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * Preload-hacks distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * You should have received a copy of the GNU General Public License along
23 * with mLib; if not, write to the Free Software Foundation, Inc., 59 Temple
24 * Place - Suite 330, Boston, MA 02111-1307, USA.
32 /*----- Header files ------------------------------------------------------*/
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <netinet/tcp.h>
53 #include <netinet/udp.h>
56 /*----- Data structures ---------------------------------------------------*/
58 enum { UNUSED
, STALE
, USED
}; /* Unix socket status values */
59 enum { WANT_FRESH
, WANT_EXISTING
}; /* Socket address dispositions */
60 enum { DENY
, ALLOW
}; /* ACL verdicts */
62 /* Access control list nodes */
63 typedef struct aclnode
{
66 unsigned long minaddr
, maxaddr
;
67 unsigned short minport
, maxport
;
70 /* Local address records */
71 #define MAX_LOCAL_IPADDRS 16
72 static struct in_addr local_ipaddrs
[MAX_LOCAL_IPADDRS
];
73 static int n_local_ipaddrs
;
75 /* General configuration */
77 static char *sockdir
= 0;
79 static unsigned minautoport
= 16384, maxautoport
= 65536;
81 /* Access control lists */
82 static aclnode
*bind_real
, **bind_tail
= &bind_real
;
83 static aclnode
*connect_real
, **connect_tail
= &connect_real
;
85 /*----- Import the real versions of functions -----------------------------*/
87 /* The list of functions to immport. */
89 _(socket, int, (int, int, int)) \
90 _(socketpair, int, (int, int, int, int *)) \
91 _(connect, int, (int, const struct sockaddr *, socklen_t)) \
92 _(bind, int, (int, const struct sockaddr *, socklen_t)) \
93 _(accept, int, (int, struct sockaddr *, socklen_t *)) \
94 _(getsockname, int, (int, struct sockaddr *, socklen_t *)) \
95 _(getpeername, int, (int, struct sockaddr *, socklen_t *)) \
96 _(getsockopt, int, (int, int, int, void *, socklen_t *)) \
97 _(setsockopt, int, (int, int, int, const void *, socklen_t)) \
98 _(sendto, ssize_t, (int, const void *buf, size_t, int, \
99 const struct sockaddr *to, socklen_t tolen)) \
100 _(recvfrom, ssize_t, (int, void *buf, size_t, int, \
101 struct sockaddr *from, socklen_t *fromlen)) \
102 _(sendmsg, ssize_t, (int, const struct msghdr *, int)) \
103 _(recvmsg, ssize_t, (int, struct msghdr *, int)) \
106 /* Function pointers to set up. */
107 #define DECL(imp, ret, args) static ret (*real_##imp) args;
111 /* Import the system calls. */
112 static void import(void)
114 #define IMPORT(imp, ret, args) \
115 real_##imp = (ret (*)args)dlsym(RTLD_NEXT, #imp);
120 /*----- Utilities ---------------------------------------------------------*/
122 /* Socket address casts */
123 #define SA(sa) ((struct sockaddr *)(sa))
124 #define SIN(sa) ((struct sockaddr_in *)(sa))
125 #define SUN(sa) ((struct sockaddr_un *)(sa))
128 #define UC(ch) ((unsigned char)(ch))
130 /* Memory allocation */
131 #define NEW(x) ((x) = xmalloc(sizeof(*x)))
132 #define NEWV(x, n) ((x) = xmalloc(sizeof(*x) * (n)))
136 # define D(body) { if (debug) { body } }
141 /* Preservation of error status */
142 #define PRESERVING_ERRNO(body) do { \
143 int _err = errno; { body } errno = _err; \
146 /* Allocate N bytes of memory; abort on failure. */
147 static void *xmalloc(size_t n
)
151 if ((p
= malloc(n
)) == 0) { perror("malloc"); exit(127); }
155 /* Allocate a copy of the null-terminated string P; abort on failure. */
156 static char *xstrdup(const char *p
)
158 size_t n
= strlen(p
) + 1;
159 char *q
= xmalloc(n
);
163 /*----- Access control lists ----------------------------------------------*/
167 /* Write to standard error a description of the ACL node A. */
168 static void dump_aclnode(aclnode
*a
)
170 char minbuf
[16], maxbuf
[16];
171 struct in_addr amin
, amax
;
173 amin
.s_addr
= htonl(a
->minaddr
);
174 amax
.s_addr
= htonl(a
->maxaddr
);
175 fprintf(stderr
, "noip: %c ", a
->act ?
'+' : '-');
176 if (a
->minaddr
== 0 && a
->maxaddr
== 0xffffffff)
177 fprintf(stderr
, "any");
179 fprintf(stderr
, "%s",
180 inet_ntop(AF_INET
, &amin
, minbuf
, sizeof(minbuf
)));
181 if (a
->maxaddr
!= a
->minaddr
) {
182 fprintf(stderr
, "-%s",
183 inet_ntop(AF_INET
, &amax
, maxbuf
, sizeof(maxbuf
)));
186 if (a
->minport
!= 0 || a
->maxport
!= 0xffff) {
187 fprintf(stderr
, ":%u", (unsigned)a
->minport
);
188 if (a
->minport
!= a
->maxport
)
189 fprintf(stderr
, "-%u", (unsigned)a
->maxport
);
194 static void dump_acl(aclnode
*a
)
198 for (; a
; a
= a
->next
) {
202 fprintf(stderr
, "noip: [default policy: %s]\n",
203 act
== ALLOW ?
"DENY" : "ALLOW");
208 /* Returns nonzero if the ACL A allows the IP socket SIN. */
209 static int acl_allows_p(aclnode
*a
, const struct sockaddr_in
*sin
)
211 unsigned long addr
= ntohl(sin
->sin_addr
.s_addr
);
212 unsigned short port
= ntohs(sin
->sin_port
);
216 fprintf(stderr
, "noip: check %s:%u\n",
217 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
218 ntohs((unsigned)sin
->sin_port
)); )
219 for (; a
; a
= a
->next
) {
220 D( dump_aclnode(a
); )
221 if (a
->minaddr
<= addr
&& addr
<= a
->maxaddr
&&
222 a
->minport
<= port
&& port
<= a
->maxport
) {
223 D( fprintf(stderr
, "noip: aha! %s\n", a
->act ?
"ALLOW" : "DENY"); )
228 D( fprintf(stderr
, "noip: nothing found: %s\n", act ?
"DENY" : "ALLOW"); )
232 /*----- Socket address conversion -----------------------------------------*/
234 /* Return a uniformly distributed integer between MIN and MAX inclusive. */
235 static unsigned randrange(unsigned min
, unsigned max
)
239 /* It's so nice not to have to care about the quality of the generator
242 for (mask
= 1; mask
< max
; mask
= (mask
<< 1) | 1)
244 do i
= rand() & mask
; while (i
> max
);
248 /* Return the status of Unix-domain socket address SUN. Returns: UNUSED if
249 * the socket doesn't exist; USED if the path refers to an active socket, or
250 * isn't really a socket at all, or we can't tell without a careful search
251 * and QUICKP is set; or STALE if the file refers to a socket which isn't
252 * being used any more.
254 static int unix_socket_status(struct sockaddr_un
*sun
, int quickp
)
262 if (stat(sun
->sun_path
, &st
))
263 return (errno
== ENOENT ? UNUSED
: USED
);
264 if (!S_ISSOCK(st
.st_mode
) || quickp
)
267 if ((fp
= fopen("/proc/net/unix", "r")) == 0)
269 fgets(buf
, sizeof(buf
), fp
); /* skip header */
270 len
= strlen(sun
->sun_path
);
271 while (fgets(buf
, sizeof(buf
), fp
)) {
273 if (n
>= len
+ 2 && buf
[n
- len
- 2] == ' ' && buf
[n
- 1] == '\n' &&
274 memcmp(buf
+ n
- len
- 1, sun
->sun_path
, len
) == 0)
285 /* Encode the Internet address SIN as a Unix-domain address SUN. If WANT is
286 * WANT_FRESH, and SIN->sin_port is zero, then we pick an arbitrary local
287 * port. Otherwise we pick the port given. There's an unpleasant hack to
288 * find servers bound to INADDR_ANY. Returns zero on success; -1 on failure.
290 static int encode_inet_addr(struct sockaddr_un
*sun
,
291 const struct sockaddr_in
*sin
,
296 char buf
[INET_ADDRSTRLEN
];
299 D( fprintf(stderr
, "noip: encode %s:%u (%s)",
300 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
301 (unsigned)ntohs(sin
->sin_port
),
302 want
== WANT_EXISTING ?
"EXISTING" : "FRESH"); )
303 sun
->sun_family
= AF_UNIX
;
304 if (sin
->sin_port
|| want
== WANT_EXISTING
) {
305 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/%s:%u", sockdir
,
306 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
307 (unsigned)ntohs(sin
->sin_port
));
308 rc
= unix_socket_status(sun
, 0);
309 if (rc
== STALE
) unlink(sun
->sun_path
);
310 if (rc
!= USED
&& want
== WANT_EXISTING
) {
311 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/0.0.0.0:%u",
312 sockdir
, (unsigned)ntohs(sin
->sin_port
));
313 if (unix_socket_status(sun
, 0) == STALE
) unlink(sun
->sun_path
);
316 for (i
= 0; i
< 10; i
++) {
317 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/%s:%u", sockdir
,
318 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
319 randrange(minautoport
, maxautoport
));
320 if (unix_socket_status(sun
, 1) == UNUSED
) goto found
;
322 for (desperatep
= 0; desperatep
< 2; desperatep
++) {
323 for (i
= minautoport
; i
<= maxautoport
; i
++) {
324 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/%s:%u", sockdir
,
325 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
327 rc
= unix_socket_status(sun
, !desperatep
);
329 case STALE
: unlink(sun
->sun_path
);
330 case UNUSED
: goto found
;
335 D( fprintf(stderr
, " -- can't resolve\n"); )
339 D( fprintf(stderr
, " -> `%s'\n", sun
->sun_path
); )
343 /* Decode the Unix address SUN to an Internet address SIN. If
344 * DECODE_UNBOUND_P is nonzero, an empty address (indicative of an unbound
345 * Unix-domain socket) is translated to a wildcard Internet address. Returns
346 * zero on success; -1 on failure (e.g., it wasn't one of our addresses).
348 static int decode_inet_addr(struct sockaddr_in
*sin
,
349 const struct sockaddr_un
*sun
,
351 int decode_unbound_p
)
353 char buf
[INET_ADDRSTRLEN
+ 16];
355 size_t n
= strlen(sockdir
), nn
= strlen(sun
->sun_path
);
356 struct sockaddr_in sin_mine
;
361 if (sun
->sun_family
!= AF_UNIX
)
363 if (len
< sizeof(sun
)) ((char *)sun
)[len
] = 0;
364 D( fprintf(stderr
, "noip: decode (%d) `%s'",
365 *sun
->sun_path
, sun
->sun_path
); )
366 if (decode_unbound_p
&& !sun
->sun_path
[0]) {
367 sin
->sin_family
= AF_INET
;
368 sin
->sin_addr
.s_addr
= INADDR_ANY
;
370 D( fprintf(stderr
, " -- unbound socket\n"); )
373 if (nn
< n
+ 1 || nn
- n
>= sizeof(buf
) || sun
->sun_path
[n
] != '/' ||
374 memcmp(sun
->sun_path
, sockdir
, n
) != 0) {
375 D( fprintf(stderr
, " -- not one of ours\n"); )
378 memcpy(buf
, sun
->sun_path
+ n
+ 1, nn
- n
);
379 if ((p
= strchr(buf
, ':')) == 0) {
380 D( fprintf(stderr
, " -- malformed (no port)\n"); )
384 sin
->sin_family
= AF_INET
;
385 if (inet_pton(AF_INET
, buf
, &sin
->sin_addr
) <= 0) {
386 D( fprintf(stderr
, " -- malformed (bad address `%s')\n", buf
); )
389 port
= strtoul(p
, &p
, 10);
390 if (*p
|| port
>= 65536) {
391 D( fprintf(stderr
, " -- malformed (port out of range)"); )
394 sin
->sin_port
= htons(port
);
395 D( fprintf(stderr
, " -> %s:%u\n",
396 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
401 /* SK is (or at least might be) a Unix-domain socket we created when an
402 * Internet socket was asked for. We've decided it should be an Internet
403 * socket after all, so convert it.
405 static int fixup_real_ip_socket(int sk
)
410 struct sockaddr_un sun
;
411 struct sockaddr_in sin
;
423 _(LINGER, struct linger) \
426 _(RCVTIMEO, struct timeval) \
427 _(SNDTIMEO, struct timeval)
430 if (real_getsockname(sk
, SA(&sun
), &len
))
432 if (decode_inet_addr(&sin
, &sun
, len
, 1))
433 return (0); /* Not one of ours */
435 if (real_getsockopt(sk
, SOL_SOCKET
, SO_TYPE
, &type
, &len
) < 0 ||
436 (nsk
= real_socket(PF_INET
, type
, 0)) < 0)
438 #define FIX(opt, ty) do { \
441 if (real_getsockopt(sk, SOL_SOCKET, SO_##opt, &ov_, &len) < 0 || \
442 real_setsockopt(nsk, SOL_SOCKET, SO_##opt, &ov_, len)) { \
449 if ((f
= fcntl(sk
, F_GETFL
)) < 0 ||
450 (fd
= fcntl(sk
, F_GETFD
)) < 0 ||
451 fcntl(nsk
, F_SETFL
, f
) < 0 ||
456 unlink(sun
.sun_path
);
458 if (fcntl(sk
, F_SETFD
, fd
) < 0) {
459 perror("noip: fixup_real_ip_socket F_SETFD");
465 /* The socket SK is about to be used to communicate with the remote address
466 * SA. Assign it a local address so that getpeername does something useful.
468 static int do_implicit_bind(int sk
, const struct sockaddr
**sa
,
469 socklen_t
*len
, struct sockaddr_un
*sun
)
471 struct sockaddr_in sin
;
472 socklen_t mylen
= sizeof(*sun
);
474 if (acl_allows_p(connect_real
, SIN(*sa
))) {
475 if (fixup_real_ip_socket(sk
))
478 if (real_getsockname(sk
, SA(sun
), &mylen
) < 0)
480 if (sun
->sun_family
== AF_UNIX
) {
481 if (mylen
< sizeof(*sun
)) ((char *)sun
)[mylen
] = 0;
482 if (!sun
->sun_path
[0]) {
483 sin
.sin_family
= AF_INET
;
484 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
486 encode_inet_addr(sun
, &sin
, WANT_FRESH
);
487 if (real_bind(sk
, SA(sun
), SUN_LEN(sun
)))
490 encode_inet_addr(sun
, SIN(*sa
), WANT_EXISTING
);
498 /* We found the real address SA, with length LEN; if it's a Unix-domain
499 * address corresponding to a fake socket, convert it to cover up the
500 * deception. Whatever happens, put the result at FAKE and store its length
503 static void return_fake_name(struct sockaddr
*sa
, socklen_t len
,
504 struct sockaddr
*fake
, socklen_t
*fakelen
)
506 struct sockaddr_in sin
;
509 if (sa
->sa_family
== AF_UNIX
&&
510 !decode_inet_addr(&sin
, SUN(sa
), len
, 0)) {
518 memcpy(fake
, sa
, len
);
522 /*----- Configuration -----------------------------------------------------*/
524 /* Return the process owner's home directory. */
525 static char *home(void)
530 if (getuid() == uid
&&
531 (p
= getenv("HOME")) != 0)
533 else if ((pw
= getpwuid(uid
)) != 0)
539 /* Return a good temporary directory to use. */
540 static char *tmpdir(void)
544 if ((p
= getenv("TMPDIR")) != 0) return (p
);
545 else if ((p
= getenv("TMP")) != 0) return (p
);
546 else return ("/tmp");
549 /* Return the user's name, or at least something distinctive. */
550 static char *user(void)
556 if ((p
= getenv("USER")) != 0) return (p
);
557 else if ((p
= getenv("LOGNAME")) != 0) return (p
);
558 else if ((pw
= getpwuid(uid
)) != 0) return (pw
->pw_name
);
560 snprintf(buf
, sizeof(buf
), "uid-%lu", (unsigned long)uid
);
565 /* Skip P over space characters. */
566 #define SKIPSPC do { while (*p && isspace(UC(*p))) p++; } while (0)
568 /* Set Q to point to the next word following P, null-terminate it, and step P
570 #define NEXTWORD(q) do { \
573 while (*p && !isspace(UC(*p))) p++; \
577 /* Set Q to point to the next dotted-quad address, store the ending delimiter
578 * in DEL, null-terminate it, and step P past it. */
579 #define NEXTADDR(q, del) do { \
582 while (*p && (*p == '.' || isdigit(UC(*p)))) p++; \
587 /* Set Q to point to the next decimal number, store the ending delimiter in
588 * DEL, null-terminate it, and step P past it. */
589 #define NEXTNUMBER(q, del) do { \
592 while (*p && isdigit(UC(*p))) p++; \
597 /* Push the character DEL back so we scan it again, unless it's zero
599 #define RESCAN(del) do { if (del) *--p = del; } while (0)
601 /* Evaluate true if P is pointing to the word KW (and not some longer string
602 * of which KW is a prefix). */
604 #define KWMATCHP(kw) (strncmp(p, kw, sizeof(kw) - 1) == 0 && \
605 !isalnum(UC(p[sizeof(kw) - 1])) && \
606 (p += sizeof(kw) - 1))
608 /* Parse a port list, starting at *PP. Port lists have the form
609 * [:LOW[-HIGH]]: if omitted, all ports are included; if HIGH is omitted,
610 * it's as if HIGH = LOW. Store LOW in *MIN, HIGH in *MAX and set *PP to the
611 * rest of the string.
613 static void parse_ports(char **pp
, unsigned short *min
, unsigned short *max
)
620 { *min
= 0; *max
= 0xffff; }
623 NEXTNUMBER(q
, del
); *min
= strtoul(q
, 0, 0); RESCAN(del
);
626 { p
++; NEXTNUMBER(q
, del
); *max
= strtoul(q
, 0, 0); RESCAN(del
); }
633 /* Make a new ACL node. ACT is the verdict; MINADDR and MAXADDR are the
634 * ranges on IP addresses; MINPORT and MAXPORT are the ranges on port
635 * numbers; TAIL is the list tail to attach the new node to.
637 #define ACLNODE(tail_, act_, \
638 minaddr_, maxaddr_, minport_, maxport_) do { \
642 a_->minaddr = minaddr_; a_->maxaddr = maxaddr_; \
643 a_->minport = minport_; a_->maxport = maxport_; \
644 *tail_ = a_; tail_ = &a_->next; \
647 /* Parse an ACL line. *PP points to the end of the line; *TAIL points to
648 * the list tail (i.e., the final link in the list). An ACL entry has the
649 * form +|- [any | local | ADDR | ADDR - ADDR | ADDR/ADDR | ADDR/INT] PORTS
650 * where PORTS is parsed by parse_ports above; an ACL line consists of a
651 * comma-separated sequence of entries..
653 static void parse_acl_line(char **pp
, aclnode
***tail
)
656 unsigned long minaddr
, maxaddr
, mask
;
657 unsigned short minport
, maxport
;
666 if (*p
== '+') act
= ALLOW
;
667 else if (*p
== '-') act
= DENY
;
672 if (KWMATCHP("any")) {
674 maxaddr
= 0xffffffff;
676 } else if (KWMATCHP("local")) {
677 parse_ports(&p
, &minport
, &maxport
);
678 ACLNODE(*tail
, act
, 0, 0, minport
, maxport
);
679 ACLNODE(*tail
, act
, 0xffffffff, 0xffffffff, minport
, maxport
);
680 for (i
= 0; i
< n_local_ipaddrs
; i
++) {
681 minaddr
= ntohl(local_ipaddrs
[i
].s_addr
);
682 ACLNODE(*tail
, act
, minaddr
, minaddr
, minport
, maxport
);
687 maxaddr
= 0xffffffff;
690 if (inet_pton(AF_INET
, q
, &addr
) <= 0) goto bad
;
691 minaddr
= ntohl(addr
.s_addr
);
697 if (inet_pton(AF_INET
, q
, &addr
) <= 0) goto bad
;
699 maxaddr
= ntohl(addr
.s_addr
);
700 } else if (*p
== '/') {
703 if (strchr(q
, '.')) {
704 if (inet_pton(AF_INET
, q
, &addr
) <= 0) goto bad
;
705 mask
= ntohl(addr
.s_addr
);
707 n
= strtoul(q
, 0, 0);
708 mask
= (~0ul << (32 - n
)) & 0xffffffff;
712 maxaddr
= minaddr
| (mask
^ 0xffffffff);
717 parse_ports(&p
, &minport
, &maxport
);
718 ACLNODE(*tail
, act
, minaddr
, maxaddr
, minport
, maxport
);
721 if (*p
!= ',') break;
727 D( fprintf(stderr
, "noip: bad acl spec (ignored)\n"); )
731 /* Parse the autoports configuration directive. Syntax is MIN - MAX. */
732 static void parse_autoports(char **pp
)
739 NEXTNUMBER(q
, del
); x
= strtoul(q
, 0, 0); RESCAN(del
);
741 if (*p
!= '-') goto bad
; p
++;
742 NEXTNUMBER(q
, del
); y
= strtoul(q
, 0, 0); RESCAN(del
);
743 minautoport
= x
; maxautoport
= y
;
747 D( fprintf(stderr
, "bad port range (ignored)\n"); )
751 /* Parse an ACL from an environment variable VAR, attaching it to the list
753 static void parse_acl_env(const char *var
, aclnode
***tail
)
757 if ((p
= getenv(var
)) != 0) {
759 parse_acl_line(&q
, tail
);
764 /* Read the configuration from the config file and environment. */
765 static void readconfig(void)
772 parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail
);
773 parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail
);
774 if ((p
= getenv("NOIP_AUTOPORTS")) != 0) {
779 if ((p
= getenv("NOIP_CONFIG")) == 0)
780 snprintf(p
= buf
, sizeof(buf
), "%s/.noip", home());
781 D( fprintf(stderr
, "noip: config file: %s\n", p
); )
783 if ((fp
= fopen(p
, "r")) == 0) {
784 D( fprintf(stderr
, "noip: couldn't read config: %s\n",
788 while (fgets(buf
, sizeof(buf
), fp
)) {
793 if (!*p
|| *p
== '#') continue;
794 while (n
&& isspace(UC(buf
[n
- 1]))) n
--;
799 if (strcmp(cmd
, "socketdir") == 0)
800 sockdir
= xstrdup(p
);
801 else if (strcmp(cmd
, "realbind") == 0)
802 parse_acl_line(&p
, &bind_tail
);
803 else if (strcmp(cmd
, "realconnect") == 0)
804 parse_acl_line(&p
, &connect_tail
);
805 else if (strcmp(cmd
, "autoports") == 0)
807 else if (strcmp(cmd
, "debug") == 0)
808 debug
= *p ?
atoi(p
) : 1;
810 D( fprintf(stderr
, "noip: bad config command %s\n", cmd
); )
815 parse_acl_env("NOIP_REALBIND", &bind_tail
);
816 parse_acl_env("NOIP_REALCONNECT", &connect_tail
);
817 parse_acl_env("NOIP_REALBIND_AFTER", &bind_tail
);
818 parse_acl_env("NOIP_REALCONNECT_AFTER", &connect_tail
);
821 if (!sockdir
) sockdir
= getenv("NOIP_SOCKETDIR");
823 snprintf(buf
, sizeof(buf
), "%s/noip-%s", tmpdir(), user());
824 sockdir
= xstrdup(buf
);
826 D( fprintf(stderr
, "noip: socketdir: %s\n", sockdir
);
827 fprintf(stderr
, "noip: autoports: %u-%u\n",
828 minautoport
, maxautoport
);
829 fprintf(stderr
, "noip: realbind acl:\n");
831 fprintf(stderr
, "noip: realconnect acl:\n");
832 dump_acl(connect_real
); )
835 /*----- Overridden system calls -------------------------------------------*/
837 int socket(int pf
, int ty
, int proto
)
844 return real_socket(pf
, ty
, proto
);
846 errno
= EAFNOSUPPORT
;
851 int socketpair(int pf
, int ty
, int proto
, int *sk
)
857 return (real_socketpair(pf
, ty
, proto
, sk
));
860 int bind(int sk
, const struct sockaddr
*sa
, socklen_t len
)
862 struct sockaddr_un sun
;
864 if (sa
->sa_family
== AF_INET
) {
866 if (acl_allows_p(bind_real
, SIN(sa
))) {
867 if (fixup_real_ip_socket(sk
))
870 encode_inet_addr(&sun
, SIN(sa
), WANT_FRESH
);
876 return real_bind(sk
, sa
, len
);
879 int connect(int sk
, const struct sockaddr
*sa
, socklen_t len
)
881 struct sockaddr_un sun
;
885 if (sa
->sa_family
== AF_INET
) {
887 do_implicit_bind(sk
, &sa
, &len
, &sun
);
891 rc
= real_connect(sk
, sa
, len
);
894 case ENOENT
: errno
= ECONNREFUSED
; break;
900 ssize_t
sendto(int sk
, const void *buf
, size_t len
, int flags
,
901 const struct sockaddr
*to
, socklen_t tolen
)
903 struct sockaddr_un sun
;
905 if (to
&& to
->sa_family
== AF_INET
) {
907 do_implicit_bind(sk
, &to
, &tolen
, &sun
);
910 return real_sendto(sk
, buf
, len
, flags
, to
, tolen
);
913 ssize_t
recvfrom(int sk
, void *buf
, size_t len
, int flags
,
914 struct sockaddr
*from
, socklen_t
*fromlen
)
917 socklen_t mylen
= sizeof(sabuf
);
921 return real_recvfrom(sk
, buf
, len
, flags
, 0, 0);
923 n
= real_recvfrom(sk
, buf
, len
, flags
, SA(sabuf
), &mylen
);
926 return_fake_name(SA(sabuf
), mylen
, from
, fromlen
);
931 ssize_t
sendmsg(int sk
, const struct msghdr
*msg
, int flags
)
933 struct sockaddr_un sun
;
934 const struct sockaddr
*sa
;
937 if (msg
->msg_name
&& SA(msg
->msg_name
)->sa_family
== AF_INET
) {
939 sa
= SA(msg
->msg_name
);
941 do_implicit_bind(sk
, &sa
, &mymsg
.msg_namelen
, &sun
);
942 mymsg
.msg_name
= SA(sa
);
946 return real_sendmsg(sk
, msg
, flags
);
949 ssize_t
recvmsg(int sk
, struct msghdr
*msg
, int flags
)
957 return real_recvmsg(sk
, msg
, flags
);
959 sa
= SA(msg
->msg_name
);
960 len
= msg
->msg_namelen
;
961 msg
->msg_name
= sabuf
;
962 msg
->msg_namelen
= sizeof(sabuf
);
963 n
= real_recvmsg(sk
, msg
, flags
);
966 return_fake_name(SA(sabuf
), msg
->msg_namelen
, sa
, &len
);
968 msg
->msg_namelen
= len
;
973 int accept(int sk
, struct sockaddr
*sa
, socklen_t
*len
)
976 socklen_t mylen
= sizeof(sabuf
);
977 int nsk
= real_accept(sk
, SA(sabuf
), &mylen
);
981 return_fake_name(SA(sabuf
), mylen
, sa
, len
);
985 int getsockname(int sk
, struct sockaddr
*sa
, socklen_t
*len
)
989 socklen_t mylen
= sizeof(sabuf
);
990 if (real_getsockname(sk
, SA(sabuf
), &mylen
))
992 return_fake_name(SA(sabuf
), mylen
, sa
, len
);
997 int getpeername(int sk
, struct sockaddr
*sa
, socklen_t
*len
)
1001 socklen_t mylen
= sizeof(sabuf
);
1002 if (real_getpeername(sk
, SA(sabuf
), &mylen
))
1004 return_fake_name(SA(sabuf
), mylen
, sa
, len
);
1009 int getsockopt(int sk
, int lev
, int opt
, void *p
, socklen_t
*len
)
1019 return real_getsockopt(sk
, lev
, opt
, p
, len
);
1022 int setsockopt(int sk
, int lev
, int opt
, const void *p
, socklen_t len
)
1031 case SO_BINDTODEVICE
:
1032 case SO_ATTACH_FILTER
:
1033 case SO_DETACH_FILTER
:
1036 return real_setsockopt(sk
, lev
, opt
, p
, len
);
1039 /*----- Initialization ----------------------------------------------------*/
1041 /* Clean up the socket directory, deleting stale sockets. */
1042 static void cleanup_sockdir(void)
1046 struct sockaddr_in sin
;
1047 struct sockaddr_un sun
;
1050 if ((dir
= opendir(sockdir
)) == 0)
1052 sun
.sun_family
= AF_UNIX
;
1053 while ((d
= readdir(dir
)) != 0) {
1054 if (d
->d_name
[0] == '.') continue;
1055 snprintf(sun
.sun_path
, sizeof(sun
.sun_path
),
1056 "%s/%s", sockdir
, d
->d_name
);
1057 if (decode_inet_addr(&sin
, &sun
, SUN_LEN(&sun
), 0) ||
1058 stat(sun
.sun_path
, &st
) ||
1059 !S_ISSOCK(st
.st_mode
)) {
1060 D( fprintf(stderr
, "noip: ignoring unknown socketdir entry `%s'\n",
1064 if (unix_socket_status(&sun
, 0) == STALE
) {
1065 D( fprintf(stderr
, "noip: clearing away stale socket %s\n",
1067 unlink(sun
.sun_path
);
1073 /* Find the addresses attached to local network interfaces, and remember them
1076 static void get_local_ipaddrs(void)
1078 struct if_nameindex
*ifn
;
1083 ifn
= if_nameindex();
1084 if ((sk
= real_socket(PF_INET
, SOCK_STREAM
, 00)) < 0)
1086 for (i
= n_local_ipaddrs
= 0;
1087 n_local_ipaddrs
< MAX_LOCAL_IPADDRS
&&
1088 ifn
[i
].if_name
&& *ifn
[i
].if_name
;
1090 strcpy(ifr
.ifr_name
, ifn
[i
].if_name
);
1091 if (ioctl(sk
, SIOCGIFADDR
, &ifr
) || ifr
.ifr_addr
.sa_family
!= AF_INET
)
1093 local_ipaddrs
[n_local_ipaddrs
++] =
1094 SIN(&ifr
.ifr_addr
)->sin_addr
;
1095 D( fprintf(stderr
, "noip: local addr %s = %s\n", ifn
[i
].if_name
,
1096 inet_ntoa(local_ipaddrs
[n_local_ipaddrs
- 1])); )
1101 /* Print the given message to standard error. Avoids stdio. */
1102 static void printerr(const char *p
) { write(STDERR_FILENO
, p
, strlen(p
)); }
1104 /* Create the socket directory, being careful about permissions. */
1105 static void create_sockdir(void)
1109 if (stat(sockdir
, &st
)) {
1110 if (errno
== ENOENT
) {
1111 if (mkdir(sockdir
, 0700)) {
1112 perror("noip: creating socketdir");
1115 if (!stat(sockdir
, &st
))
1118 perror("noip: checking socketdir");
1122 if (!S_ISDIR(st
.st_mode
)) {
1123 printerr("noip: bad socketdir: not a directory\n");
1126 if (st
.st_uid
!= uid
) {
1127 printerr("noip: bad socketdir: not owner\n");
1130 if (st
.st_mode
& 077) {
1131 printerr("noip: bad socketdir: not private\n");
1136 /* Initialization function. */
1137 static void setup(void) __attribute__((constructor
));
1138 static void setup(void)
1145 if ((p
= getenv("NOIP_DEBUG")) && atoi(p
))
1147 get_local_ipaddrs();
1154 /*----- That's all, folks -------------------------------------------------*/