chiark / gitweb /
Allow user control over autobinding. Also try ports at random to start
[preload-hacks] / noip.c
CommitLineData
e4976bb0 1#define _GNU_SOURCE
2#undef sun
3#undef SUN
4#define DEBUG
5
6#include <ctype.h>
7#include <errno.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11#include <unistd.h>
12#include <dirent.h>
13#include <dlfcn.h>
14#include <fcntl.h>
15#include <pwd.h>
16
17#include <sys/ioctl.h>
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/un.h>
21
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <netinet/tcp.h>
25#include <netinet/udp.h>
26#include <net/if.h>
27
28enum { UNUSED, STALE, USED };
29enum { WANT_FRESH, WANT_EXISTING };
30enum { DENY, ALLOW };
31typedef struct aclnode {
32 struct aclnode *next;
33 int act;
34 unsigned long minaddr, maxaddr;
35 unsigned short minport, maxport;
36} aclnode;
37
38#define MAX_LOCAL_IPADDRS 16
39static struct in_addr local_ipaddrs[MAX_LOCAL_IPADDRS];
40static int n_local_ipaddrs;
41
42static uid_t uid;
43static char *sockdir = 0;
44static int debug = 0;
f6049fdd 45static unsigned minautoport = 16384, maxautoport = 65536;
e4976bb0 46
47static aclnode *bind_real, **bind_tail = &bind_real;
48static aclnode *connect_real, **connect_tail = &connect_real;
49
50/* --- Import magic --- */
51
52#define IMPORTS(_) \
53 _(socket, int, (int, int, int)) \
54 _(socketpair, int, (int, int, int, int *)) \
55 _(connect, int, (int, const struct sockaddr *, socklen_t)) \
56 _(bind, int, (int, const struct sockaddr *, socklen_t)) \
57 _(accept, int, (int, struct sockaddr *, socklen_t *)) \
58 _(getsockname, int, (int, struct sockaddr *, socklen_t *)) \
59 _(getpeername, int, (int, struct sockaddr *, socklen_t *)) \
60 _(getsockopt, int, (int, int, int, void *, socklen_t *)) \
61 _(setsockopt, int, (int, int, int, const void *, socklen_t)) \
62 _(sendto, ssize_t, (int, const void *buf, size_t, int, \
63 const struct sockaddr *to, socklen_t tolen)) \
64 _(recvfrom, ssize_t, (int, void *buf, size_t, int, \
65 struct sockaddr *from, socklen_t *fromlen)) \
66 _(sendmsg, ssize_t, (int, const struct msghdr *, int)) \
67 _(recvmsg, ssize_t, (int, struct msghdr *, int)) \
68 _(close, int, (int))
69
70#define DECL(imp, ret, args) static ret (*real_##imp) args;
71IMPORTS(DECL)
72#undef DECL
73
74static void setup(void) __attribute__((constructor));
75static void import(void)
76{
77#define IMPORT(imp, ret, args) \
78 real_##imp = (ret (*)args)dlsym(RTLD_NEXT, #imp);
79 IMPORTS(IMPORT)
80#undef IMPORT
81}
82
83/* --- Support --- */
84
85#define SA(sa) ((struct sockaddr *)(sa))
86#define SIN(sa) ((struct sockaddr_in *)(sa))
87#define SUN(sa) ((struct sockaddr_un *)(sa))
88
89#define UC(ch) ((unsigned char)(ch))
90
91#define NEW(x) ((x) = xmalloc(sizeof(*x)))
92#define NEWV(x, n) ((x) = xmalloc(sizeof(*x) * (n)))
93
94#ifdef DEBUG
95# define D(body) { if (debug) { body } }
96#else
97# define D(body) ;
98#endif
99
100#define PRESERVING_ERRNO(body) do { \
101 int _err = errno; { body } errno = _err; \
102} while (0)
103
104static void *xmalloc(size_t n)
105{
106 void *p;
107 if (!n) return (0);
108 if ((p = malloc(n)) == 0) { perror("malloc"); exit(1); }
109 return (p);
110}
111
112static char *xstrdup(const char *p)
113{
114 size_t n = strlen(p) + 1;
115 char *q = xmalloc(n);
116 memcpy(q, p, n);
117 return (q);
118}
119
120static int unix_socket_status(struct sockaddr_un *sun, int quick_p)
121{
122 struct stat st;
123 FILE *fp = 0;
124 size_t len, n;
125 int rc;
126 char buf[256];
127
128 rc = UNUSED;
129 if (stat(sun->sun_path, &st) && errno == ENOENT)
130 goto done;
131 rc = USED;
132 if (quick_p)
133 goto done;
134
135 if ((fp = fopen("/proc/net/unix", "r")) == 0)
136 goto done;
137 fgets(buf, sizeof(buf), fp); /* skip header */
138 len = strlen(sun->sun_path);
139 while (fgets(buf, sizeof(buf), fp)) {
140 n = strlen(buf);
141 if (n >= len + 2 && buf[n - len - 2] == ' ' && buf[n - 1] == '\n' &&
142 memcmp(buf + n - len - 1, sun->sun_path, len) == 0)
143 goto done;
144 }
145 if (ferror(fp))
146 goto done;
147 rc = STALE;
148done:
149 if (fp) fclose(fp);
150 return (rc);
151}
152
153#ifdef DEBUG
154
155static void dump_aclnode(aclnode *a)
156{
157 char minbuf[16], maxbuf[16];
158 struct in_addr amin, amax;
159
160 amin.s_addr = htonl(a->minaddr);
161 amax.s_addr = htonl(a->maxaddr);
162 fprintf(stderr, "noip: %c ", a->act ? '+' : '-');
163 if (a->minaddr == 0 && a->maxaddr == 0xffffffff)
164 fprintf(stderr, "any");
165 else {
166 fprintf(stderr, "%s",
167 inet_ntop(AF_INET, &amin, minbuf, sizeof(minbuf)));
168 if (a->maxaddr != a->minaddr) {
169 fprintf(stderr, "-%s",
170 inet_ntop(AF_INET, &amax, maxbuf, sizeof(maxbuf)));
171 }
172 }
173 if (a->minport != 0 || a->maxport != 0xffff) {
174 fprintf(stderr, ":%u", (unsigned)a->minport);
175 if (a->minport != a->maxport)
176 fprintf(stderr, "-%u", (unsigned)a->maxport);
177 }
178 fputc('\n', stderr);
179}
180
181#endif
182
183static int acl_allows_p(aclnode *a, const struct sockaddr_in *sin)
184{
185 unsigned long addr = ntohl(sin->sin_addr.s_addr);
186 unsigned short port = ntohs(sin->sin_port);
187 int act = ALLOW;
188
189 D( char buf[16];
190 fprintf(stderr, "noip: check %s:%u\n",
191 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
192 ntohs((unsigned)sin->sin_port)); )
193 for (; a; a = a->next) {
194 D( dump_aclnode(a); )
195 if (a->minaddr <= addr && addr <= a->maxaddr &&
196 a->minport <= port && port <= a->maxport) {
197 D( fprintf(stderr, "noip: aha! %s\n", a->act ? "ALLOW" : "DENY"); )
198 return (a->act);
199 }
200 act = a->act;
201 }
202 D( fprintf(stderr, "noip: nothing found: %s\n", act ? "DENY" : "ALLOW"); )
203 return (!act);
204}
205
206#ifdef DEBUG
207
208static void dump_acl(aclnode *a)
209{
210 int act = ALLOW;
211
212 for (; a; a = a->next) {
213 dump_aclnode(a);
214 act = a->act;
215 }
216 fprintf(stderr, "noip: [default policy: %s]\n",
217 act == ALLOW ? "DENY" : "ALLOW");
218}
219
220#endif
221
f6049fdd 222static unsigned randrange(unsigned min, unsigned max)
223{
224 unsigned mask, i;
225
226 /* It's so nice not to have to care about the quality of the generator
227 much! */
228 max -= min;
229 for (mask = 1; mask < max; mask = (mask << 1) | 1)
230 ;
231 do i = rand() & mask; while (i > max);
232 return (i + min);
233}
234
e4976bb0 235static int encode_inet_addr(struct sockaddr_un *sun,
236 const struct sockaddr_in *sin,
237 int want)
238{
239 int i;
240 int desperate_p = 0;
241 char buf[INET_ADDRSTRLEN];
242 int rc;
243
244 D( fprintf(stderr, "noip: encode %s:%u (%s)",
245 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
246 (unsigned)ntohs(sin->sin_port),
247 want == WANT_EXISTING ? "EXISTING" : "FRESH"); )
248 sun->sun_family = AF_UNIX;
249 if (sin->sin_port || want == WANT_EXISTING) {
250 snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
251 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
252 (unsigned)ntohs(sin->sin_port));
253 rc = unix_socket_status(sun, 0);
254 if (rc == STALE) unlink(sun->sun_path);
255 if (rc != USED && want == WANT_EXISTING) {
256 snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/0.0.0.0:%u",
257 sockdir, (unsigned)ntohs(sin->sin_port));
258 if (unix_socket_status(sun, 0) == STALE) unlink(sun->sun_path);
259 }
260 } else {
f6049fdd 261 for (i = 0; i < 10; i++) {
262 snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
263 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
264 randrange(minautoport, maxautoport));
265 if (unix_socket_status(sun, 1) == UNUSED) goto found;
266 }
e4976bb0 267 for (desperate_p = 0; desperate_p < 2; desperate_p++) {
f6049fdd 268 for (i = minautoport; i <= maxautoport; i++) {
e4976bb0 269 snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
270 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
271 (unsigned)i);
272 rc = unix_socket_status(sun, !desperate_p);
273 switch (rc) {
274 case STALE: unlink(sun->sun_path);
275 case UNUSED: goto found;
276 }
277 }
278 }
279 errno = EADDRINUSE;
280 D( fprintf(stderr, " -- can't resolve\n"); )
281 return (-1);
282 found:;
283 }
284 D( fprintf(stderr, " -> `%s'\n", sun->sun_path); )
285 return (0);
286}
287
288static int decode_inet_addr(struct sockaddr_in *sin,
289 const struct sockaddr_un *sun,
290 socklen_t len)
291{
292 char buf[INET_ADDRSTRLEN + 16];
293 char *p;
294 size_t n = strlen(sockdir), nn = strlen(sun->sun_path);
295 struct sockaddr_in sin_mine;
296 unsigned long port;
297
298 if (!sin)
299 sin = &sin_mine;
300 if (sun->sun_family != AF_UNIX)
301 return (-1);
302 if (len < sizeof(sun)) ((char *)sun)[len] = 0;
303 D( fprintf(stderr, "noip: decode (%d) `%s'",
304 *sun->sun_path, sun->sun_path); )
305 if (!sun->sun_path[0]) {
306 sin->sin_family = AF_INET;
307 sin->sin_addr.s_addr = INADDR_ANY;
308 sin->sin_port = 0;
309 D( fprintf(stderr, " -- unbound socket\n"); )
310 return (0);
311 }
312 if (nn < n + 1 || nn - n >= sizeof(buf) || sun->sun_path[n] != '/' ||
313 memcmp(sun->sun_path, sockdir, n) != 0) {
314 D( fprintf(stderr, " -- not one of ours\n"); )
315 return (-1);
316 }
317 memcpy(buf, sun->sun_path + n + 1, nn - n);
318 if ((p = strchr(buf, ':')) == 0) {
319 D( fprintf(stderr, " -- malformed (no port)\n"); )
320 return (-1);
321 }
322 *p++ = 0;
323 sin->sin_family = AF_INET;
324 if (inet_pton(AF_INET, buf, &sin->sin_addr) <= 0) {
325 D( fprintf(stderr, " -- malformed (bad address `%s')\n", buf); )
326 return (-1);
327 }
328 port = strtoul(p, &p, 10);
329 if (*p || port >= 65536) {
330 D( fprintf(stderr, " -- malformed (port out of range)"); )
331 return (-1);
332 }
333 sin->sin_port = htons(port);
334 D( fprintf(stderr, " -> %s:%u\n",
335 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
336 (unsigned)port); )
337 return (0);
338}
339
340static int fixup_real_ip_socket(int sk)
341{
342 int nsk;
343 int type;
344 int f, fd;
345 struct sockaddr_un sun;
346 struct sockaddr_in sin;
347 socklen_t len;
348
349#define OPTS(_) \
350 _(DEBUG, int) \
351 _(REUSEADDR, int) \
352 _(DONTROUTE, int) \
353 _(BROADCAST, int) \
354 _(SNDBUF, int) \
355 _(RCVBUF, int) \
356 _(OOBINLINE, int) \
357 _(NO_CHECK, int) \
358 _(LINGER, struct linger) \
359 _(BSDCOMPAT, int) \
360 _(RCVLOWAT, int) \
361 _(RCVTIMEO, struct timeval) \
362 _(SNDTIMEO, struct timeval)
363
364 len = sizeof(sun);
365 if (real_getsockname(sk, SA(&sun), &len))
366 return (-1);
367 if (decode_inet_addr(&sin, &sun, len))
368 return (0); /* Not one of ours */
369 len = sizeof(type);
370 if (real_getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0 ||
371 (nsk = real_socket(PF_INET, type, 0)) < 0)
372 return (-1);
373#define FIX(opt, ty) do { \
374 ty ov_; \
375 len = sizeof(ov_); \
376 if (real_getsockopt(sk, SOL_SOCKET, SO_##opt, &ov_, &len) < 0 || \
377 real_setsockopt(nsk, SOL_SOCKET, SO_##opt, &ov_, len)) { \
378 real_close(nsk); \
379 return (-1); \
380 } \
381} while (0);
382 OPTS(FIX)
383#undef FIX
384 if ((f = fcntl(sk, F_GETFL)) < 0 ||
385 (fd = fcntl(sk, F_GETFD)) < 0 ||
386 fcntl(nsk, F_SETFL, f) < 0 ||
387 dup2(nsk, sk) < 0) {
388 real_close(nsk);
389 return (-1);
390 }
391 unlink(sun.sun_path);
392 real_close(nsk);
393 if (fcntl(sk, F_SETFD, fd) < 0) {
394 perror("noip: fixup_real_ip_socket F_SETFD");
395 abort();
396 }
397 return (0);
398}
399
400static int do_implicit_bind(int sk, const struct sockaddr **sa,
401 socklen_t *len, struct sockaddr_un *sun)
402{
403 struct sockaddr_in sin;
404 socklen_t mylen = sizeof(*sun);
405
406 if (acl_allows_p(connect_real, SIN(*sa))) {
407 if (fixup_real_ip_socket(sk))
408 return (-1);
409 } else {
410 if (real_getsockname(sk, SA(sun), &mylen) < 0)
411 return (-1);
412 if (sun->sun_family == AF_UNIX) {
413 if (mylen < sizeof(*sun)) ((char *)sun)[mylen] = 0;
414 if (!sun->sun_path[0]) {
415 sin.sin_family = AF_INET;
416 sin.sin_addr.s_addr = INADDR_LOOPBACK;
417 sin.sin_port = 0;
418 encode_inet_addr(sun, &sin, WANT_FRESH);
419 if (real_bind(sk, SA(sun), SUN_LEN(sun)))
420 return (-1);
421 }
422 encode_inet_addr(sun, SIN(*sa), WANT_EXISTING);
423 *sa = SA(sun);
424 *len = SUN_LEN(sun);
425 }
426 }
427 return (0);
428}
429
430static void return_fake_name(struct sockaddr *sa, socklen_t len,
431 struct sockaddr *fake, socklen_t *fakelen)
432{
433 struct sockaddr_in sin;
434 socklen_t alen;
435
436 if (sa->sa_family == AF_UNIX && !decode_inet_addr(&sin, SUN(sa), len)) {
437 sa = SA(&sin);
438 len = sizeof(sin);
439 }
440 alen = len;
441 if (len > *fakelen)
442 len = *fakelen;
443 if (len > 0)
444 memcpy(fake, sa, len);
445 *fakelen = alen;
446}
447
448/* --- Configuration --- */
449
450static char *home(void)
451{
452 char *p;
453 struct passwd *pw;
454
455 if ((p = getenv("HOME")) != 0) return (p);
456 else if ((pw = getpwuid(uid)) != 0) return (pw->pw_dir);
457 else return "/notexist";
458}
459
460static char *tmpdir(void)
461{
462 char *p;
463
464 if ((p = getenv("TMPDIR")) != 0) return (p);
465 else if ((p = getenv("TMP")) != 0) return (p);
466 else return ("/tmp");
467}
468
469static char *user(void)
470{
471 static char buf[16];
472 char *p;
473 struct passwd *pw;
474
475 if ((p = getenv("USER")) != 0) return (p);
476 else if ((p = getenv("LOGNAME")) != 0) return (p);
477 else if ((pw = getpwuid(uid)) != 0) return (pw->pw_name);
478 else {
479 snprintf(buf, sizeof(buf), "uid-%lu", (unsigned long)uid);
480 return (buf);
481 }
482}
483
484#define SKIPSPC do { while (*p && isspace(UC(*p))) p++; } while (0)
485#define NEXTWORD(q) do { \
486 SKIPSPC; \
487 q = p; \
488 while (*p && !isspace(UC(*p))) p++; \
489 if (*p) *p++ = 0; \
490} while (0)
491#define NEXTADDR(q, del) do { \
492 SKIPSPC; \
493 q = p; \
494 while (*p && (*p == '.' || isdigit(UC(*p)))) p++; \
495 del = *p; \
496 if (*p) *p++ = 0; \
497} while (0)
498#define NEXTNUMBER(q, del) do { \
499 SKIPSPC; \
500 q = p; \
501 while (*p && isdigit(UC(*p))) p++; \
502 del = *p; \
503 if (*p) *p++ = 0; \
504} while (0)
505#define RESCAN(del) do { if (del) *--p = del; } while (0)
506#define KWMATCHP(kw) (strncmp(p, kw, sizeof(kw) - 1) == 0 && \
507 !isalnum(UC(p[sizeof(kw) - 1])) && \
508 (p += sizeof(kw) - 1))
509
510static void parse_ports(char **pp, unsigned short *min, unsigned short *max)
511{
512 char *p = *pp, *q;
513 int del;
514
515 SKIPSPC;
f6049fdd 516 if (*p != ':')
517 { *min = 0; *max = 0xffff; }
518 else {
e4976bb0 519 p++;
f6049fdd 520 NEXTNUMBER(q, del); *min = strtoul(q, 0, 0); RESCAN(del);
e4976bb0 521 SKIPSPC;
f6049fdd 522 if (*p == '-')
523 { NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); }
524 else
e4976bb0 525 *max = *min;
526 }
527 *pp = p;
528}
529
530#define ACLNODE(tail_, act_, \
531 minaddr_, maxaddr_, minport_, maxport_) do { \
532 aclnode *a_; \
533 NEW(a_); \
534 a_->act = act_; \
535 a_->minaddr = minaddr_; a_->maxaddr = maxaddr_; \
536 a_->minport = minport_; a_->maxport = maxport_; \
537 *tail_ = a_; tail_ = &a_->next; \
538} while (0)
539
540static void parse_acl_line(char **pp, aclnode ***tail)
541{
542 struct in_addr addr;
543 unsigned long minaddr, maxaddr, mask;
544 unsigned short minport, maxport;
545 int i, n;
546 int act;
547 int del;
548 char *p = *pp;
549 char *q;
550
a6d9626b 551 for (;;) {
552 SKIPSPC;
553 if (*p == '+') act = ALLOW;
554 else if (*p == '-') act = DENY;
555 else goto bad;
e4976bb0 556
a6d9626b 557 p++;
558 SKIPSPC;
559 if (KWMATCHP("any")) {
e4976bb0 560 minaddr = 0;
561 maxaddr = 0xffffffff;
a6d9626b 562 goto justone;
563 } else if (KWMATCHP("local")) {
564 parse_ports(&p, &minport, &maxport);
565 ACLNODE(*tail, act, 0, 0, minport, maxport);
566 ACLNODE(*tail, act, 0xffffffff, 0xffffffff, minport, maxport);
567 for (i = 0; i < n_local_ipaddrs; i++) {
568 minaddr = ntohl(local_ipaddrs[i].s_addr);
569 ACLNODE(*tail, act, minaddr, minaddr, minport, maxport);
570 }
e4976bb0 571 } else {
a6d9626b 572 if (*p == ':') {
573 minaddr = 0;
574 maxaddr = 0xffffffff;
575 } else {
e4976bb0 576 NEXTADDR(q, del);
577 if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
a6d9626b 578 minaddr = ntohl(addr.s_addr);
e4976bb0 579 RESCAN(del);
a6d9626b 580 SKIPSPC;
581 if (*p == '-') {
582 p++;
583 NEXTADDR(q, del);
e4976bb0 584 if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
a6d9626b 585 RESCAN(del);
586 maxaddr = ntohl(addr.s_addr);
587 } else if (*p == '/') {
588 p++;
589 NEXTADDR(q, del);
590 if (strchr(q, '.')) {
591 if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
592 mask = ntohl(addr.s_addr);
593 } else {
f6049fdd 594 n = strtoul(q, 0, 0);
a6d9626b 595 mask = (~0ul << (32 - n)) & 0xffffffff;
596 }
597 RESCAN(del);
598 minaddr &= mask;
599 maxaddr = minaddr | (mask ^ 0xffffffff);
600 } else
601 maxaddr = minaddr;
602 }
603 justone:
604 parse_ports(&p, &minport, &maxport);
605 ACLNODE(*tail, act, minaddr, maxaddr, minport, maxport);
e4976bb0 606 }
a6d9626b 607 SKIPSPC;
608 if (*p != ',') break;
609 p++;
e4976bb0 610 }
611 return;
612
613bad:
614 D( fprintf(stderr, "noip: bad acl spec (ignored)\n"); )
615 return;
616}
617
f6049fdd 618static void parse_autoports(char **pp)
619{
620 char *p = *pp, *q;
621 unsigned x, y;
622 int del;
623
624 SKIPSPC;
625 NEXTNUMBER(q, del); x = strtoul(q, 0, 0); RESCAN(del);
626 SKIPSPC;
627 if (*p != '-') goto bad; p++;
628 NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del);
629 minautoport = x; maxautoport = y;
630 return;
631
632bad:
633 D( fprintf(stderr, "bad port range (ignored)\n"); )
634 return;
635}
636
a6d9626b 637static void parse_acl_env(const char *var, aclnode ***tail)
638{
f6049fdd 639 char *p, *q;
a6d9626b 640
641 if ((p = getenv(var)) != 0) {
f6049fdd 642 p = q = xstrdup(p);
643 parse_acl_line(&q, tail);
a6d9626b 644 free(p);
645 }
646}
647
e4976bb0 648static void readconfig(void)
649{
650 FILE *fp;
651 char buf[1024];
652 size_t n;
f6049fdd 653 char *p, *q, *cmd;
e4976bb0 654
655 if ((p = getenv("NOIP_CONFIG")) == 0)
656 snprintf(p = buf, sizeof(buf), "%s/.noip", home());
657 D( fprintf(stderr, "noip: config file: %s\n", p); )
e4976bb0 658
a6d9626b 659 parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail);
660 parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail);
f6049fdd 661 if ((p = getenv("NOIP_AUTOPORTS")) != 0) {
662 p = q = xstrdup(p);
663 parse_autoports(&q);
664 free(p);
665 }
666 if ((fp = fopen(p, "r")) == 0)
667 goto done;
e4976bb0 668 while (fgets(buf, sizeof(buf), fp)) {
669 n = strlen(buf);
670 p = buf;
671
672 SKIPSPC;
673 if (!*p || *p == '#') continue;
674 while (n && isspace(UC(buf[n - 1]))) n--;
675 buf[n] = 0;
676 NEXTWORD(cmd);
677 SKIPSPC;
678
679 if (strcmp(cmd, "socketdir") == 0)
680 sockdir = xstrdup(p);
681 else if (strcmp(cmd, "realbind") == 0)
682 parse_acl_line(&p, &bind_tail);
683 else if (strcmp(cmd, "realconnect") == 0)
684 parse_acl_line(&p, &connect_tail);
f6049fdd 685 else if (strcmp(cmd, "autoports") == 0)
686 parse_autoports(&p);
e4976bb0 687 else if (strcmp(cmd, "debug") == 0)
688 debug = *p ? atoi(p) : 1;
689 else
690 D( fprintf(stderr, "noip: bad config command %s\n", cmd); )
691 }
692 fclose(fp);
693
694done:
a6d9626b 695 parse_acl_env("NOIP_REALBIND", &bind_tail);
696 parse_acl_env("NOIP_REALCONNECT", &connect_tail);
697 parse_acl_env("NOIP_REALBIND_AFTER", &bind_tail);
698 parse_acl_env("NOIP_REALCONNECT_AFTER", &connect_tail);
e4976bb0 699 *bind_tail = 0;
700 *connect_tail = 0;
a6d9626b 701 if (!sockdir) sockdir = getenv("NOIP_SOCKETDIR");
e4976bb0 702 if (!sockdir) {
703 snprintf(buf, sizeof(buf), "%s/noip-%s", tmpdir(), user());
704 sockdir = xstrdup(buf);
705 }
706 D( fprintf(stderr, "noip: sockdir: %s\n", sockdir);
f6049fdd 707 fprintf(stderr, "noip: autoports: %u-%u\n",
708 minautoport, maxautoport);
e4976bb0 709 fprintf(stderr, "noip: realbind acl:\n");
710 dump_acl(bind_real);
711 fprintf(stderr, "noip: realconnect acl:\n");
712 dump_acl(connect_real); )
713}
714
715/* --- Hooks --- */
716
717int socket(int pf, int ty, int proto)
718{
719 if (pf == PF_INET) {
720 pf = PF_UNIX;
721 proto = 0;
722 }
723 return real_socket(pf, ty, proto);
724}
725
726int socketpair(int pf, int ty, int proto, int *sk)
727{
728 if (pf == PF_INET) {
729 pf = PF_UNIX;
730 proto = 0;
731 }
732 return (real_socketpair(pf, ty, proto, sk));
733}
734
735int bind(int sk, const struct sockaddr *sa, socklen_t len)
736{
737 struct sockaddr_un sun;
738
739 if (sa->sa_family == AF_INET) {
740 PRESERVING_ERRNO({
741 if (acl_allows_p(bind_real, SIN(sa))) {
742 if (fixup_real_ip_socket(sk))
743 return (-1);
744 } else {
745 encode_inet_addr(&sun, SIN(sa), WANT_FRESH);
746 sa = SA(&sun);
747 len = SUN_LEN(&sun);
748 }
749 });
750 }
751 return real_bind(sk, sa, len);
752}
753
754int connect(int sk, const struct sockaddr *sa, socklen_t len)
755{
756 struct sockaddr_un sun;
757
758 if (sa->sa_family == AF_INET) {
759 PRESERVING_ERRNO({
760 do_implicit_bind(sk, &sa, &len, &sun);
761 });
762 }
763 return real_connect(sk, sa, len);
764}
765
766ssize_t sendto(int sk, const void *buf, size_t len, int flags,
767 const struct sockaddr *to, socklen_t tolen)
768{
769 struct sockaddr_un sun;
770
771 if (to && to->sa_family == AF_INET) {
772 PRESERVING_ERRNO({
773 do_implicit_bind(sk, &to, &tolen, &sun);
774 });
775 }
776 return real_sendto(sk, buf, len, flags, to, tolen);
777}
778
779ssize_t recvfrom(int sk, void *buf, size_t len, int flags,
780 struct sockaddr *from, socklen_t *fromlen)
781{
782 char sabuf[1024];
783 socklen_t mylen = sizeof(sabuf);
784 ssize_t n;
785
786 if (!from)
787 return real_recvfrom(sk, buf, len, flags, 0, 0);
788 PRESERVING_ERRNO({
789 n = real_recvfrom(sk, buf, len, flags, SA(sabuf), &mylen);
790 if (n < 0)
791 return (-1);
792 return_fake_name(SA(sabuf), mylen, from, fromlen);
793 });
794 return (n);
795}
796
797ssize_t sendmsg(int sk, const struct msghdr *msg, int flags)
798{
799 struct sockaddr_un sun;
800 const struct sockaddr *sa;
801 struct msghdr mymsg;
802
803 if (msg->msg_name && SA(msg->msg_name)->sa_family == AF_INET) {
804 PRESERVING_ERRNO({
805 sa = SA(msg->msg_name);
806 mymsg = *msg;
807 do_implicit_bind(sk, &sa, &mymsg.msg_namelen, &sun);
808 mymsg.msg_name = SA(sa);
809 msg = &mymsg;
810 });
811 }
812 return real_sendmsg(sk, msg, flags);
813}
814
815ssize_t recvmsg(int sk, struct msghdr *msg, int flags)
816{
817 char sabuf[1024];
818 struct sockaddr *sa;
819 socklen_t len;
820 ssize_t n;
821
822 if (!msg->msg_name)
823 return real_recvmsg(sk, msg, flags);
824 PRESERVING_ERRNO({
825 sa = SA(msg->msg_name);
826 len = msg->msg_namelen;
827 msg->msg_name = sabuf;
828 msg->msg_namelen = sizeof(sabuf);
829 n = real_recvmsg(sk, msg, flags);
830 if (n < 0)
831 return (-1);
832 return_fake_name(SA(sabuf), msg->msg_namelen, sa, &len);
833 msg->msg_name = sa;
834 msg->msg_namelen = len;
835 });
836 return (n);
837}
838
839int accept(int sk, struct sockaddr *sa, socklen_t *len)
840{
841 char sabuf[1024];
842 socklen_t mylen = sizeof(sabuf);
843 int nsk = real_accept(sk, SA(sabuf), &mylen);
844
845 if (nsk < 0)
846 return (-1);
847 return_fake_name(SA(sabuf), mylen, sa, len);
848 return (nsk);
849}
850
851int getsockname(int sk, struct sockaddr *sa, socklen_t *len)
852{
853 PRESERVING_ERRNO({
854 char sabuf[1024];
855 socklen_t mylen = sizeof(sabuf);
856 if (real_getsockname(sk, SA(sabuf), &mylen))
857 return (-1);
858 return_fake_name(SA(sabuf), mylen, sa, len);
859 });
860 return (0);
861}
862
863int getpeername(int sk, struct sockaddr *sa, socklen_t *len)
864{
865 PRESERVING_ERRNO({
866 char sabuf[1024];
867 socklen_t mylen = sizeof(sabuf);
868 if (real_getpeername(sk, SA(sabuf), &mylen))
869 return (-1);
870 return_fake_name(SA(sabuf), mylen, sa, len);
871 });
872 return (0);
873}
874
875int getsockopt(int sk, int lev, int opt, void *p, socklen_t *len)
876{
877 switch (lev) {
878 case SOL_IP:
879 case SOL_TCP:
880 case SOL_UDP:
881 if (*len > 0)
882 memset(p, 0, *len);
883 return (0);
884 }
885 return real_getsockopt(sk, lev, opt, p, len);
886}
887
888int setsockopt(int sk, int lev, int opt, const void *p, socklen_t len)
889{
890 switch (lev) {
891 case SOL_IP:
892 case SOL_TCP:
893 case SOL_UDP:
894 return (0);
895 }
896 switch (opt) {
897 case SO_BINDTODEVICE:
898 case SO_ATTACH_FILTER:
899 case SO_DETACH_FILTER:
900 return (0);
901 }
902 return real_setsockopt(sk, lev, opt, p, len);
903}
904
905/* --- Initialization --- */
906
907static void cleanup_sockdir(void)
908{
909 DIR *dir;
910 struct dirent *d;
911 struct sockaddr_un sun;
912
913 if ((dir = opendir(sockdir)) == 0)
914 return;
915 while ((d = readdir(dir)) != 0) {
916 if (d->d_name[0] == '.') continue;
917 snprintf(sun.sun_path, sizeof(sun.sun_path),
918 "%s/%s", sockdir, d->d_name);
919 if (unix_socket_status(&sun, 0) == STALE) {
920 D( fprintf(stderr, "noip: clearing away stale socket %s\n",
921 d->d_name); )
922 unlink(sun.sun_path);
923 }
924 }
925 closedir(dir);
926}
927
928static void get_local_ipaddrs(void)
929{
930 struct if_nameindex *ifn;
931 struct ifreq ifr;
932 int sk;
933 int i;
934
935 ifn = if_nameindex();
936 if ((sk = real_socket(PF_INET, SOCK_STREAM, 00)) < 0)
937 return;
938 for (i = n_local_ipaddrs = 0;
939 n_local_ipaddrs < MAX_LOCAL_IPADDRS &&
940 ifn[i].if_name && *ifn[i].if_name;
941 i++) {
942 strcpy(ifr.ifr_name, ifn[i].if_name);
943 if (ioctl(sk, SIOCGIFADDR, &ifr) || ifr.ifr_addr.sa_family != AF_INET)
944 continue;
945 local_ipaddrs[n_local_ipaddrs++] =
946 SIN(&ifr.ifr_addr)->sin_addr;
947 D( fprintf(stderr, "noip: local addr %s = %s\n", ifn[i].if_name,
948 inet_ntoa(local_ipaddrs[n_local_ipaddrs - 1])); )
949 }
950 close(sk);
951}
952
953static void setup(void)
954{
955 PRESERVING_ERRNO({
956 char *p;
957
958 import();
959 uid = getuid();
960 if ((p = getenv("NOIP_DEBUG")) && atoi(p))
961 debug = 1;
962 get_local_ipaddrs();
963 readconfig();
964 mkdir(sockdir, 0700);
965 cleanup_sockdir();
966 });
967}