17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <netinet/tcp.h>
25 #include <netinet/udp.h>
28 enum { UNUSED
, STALE
, USED
};
29 enum { WANT_FRESH
, WANT_EXISTING
};
31 typedef struct aclnode
{
34 unsigned long minaddr
, maxaddr
;
35 unsigned short minport
, maxport
;
38 #define MAX_LOCAL_IPADDRS 16
39 static struct in_addr local_ipaddrs
[MAX_LOCAL_IPADDRS
];
40 static int n_local_ipaddrs
;
43 static char *sockdir
= 0;
45 static unsigned minautoport
= 16384, maxautoport
= 65536;
47 static aclnode
*bind_real
, **bind_tail
= &bind_real
;
48 static aclnode
*connect_real
, **connect_tail
= &connect_real
;
50 /* --- Import magic --- */
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)) \
70 #define DECL(imp, ret, args) static ret (*real_##imp) args;
74 static void setup(void) __attribute__((constructor
));
75 static void import(void)
77 #define IMPORT(imp, ret, args) \
78 real_##imp = (ret (*)args)dlsym(RTLD_NEXT, #imp);
85 #define SA(sa) ((struct sockaddr *)(sa))
86 #define SIN(sa) ((struct sockaddr_in *)(sa))
87 #define SUN(sa) ((struct sockaddr_un *)(sa))
89 #define UC(ch) ((unsigned char)(ch))
91 #define NEW(x) ((x) = xmalloc(sizeof(*x)))
92 #define NEWV(x, n) ((x) = xmalloc(sizeof(*x) * (n)))
95 # define D(body) { if (debug) { body } }
100 #define PRESERVING_ERRNO(body) do { \
101 int _err = errno; { body } errno = _err; \
104 static void *xmalloc(size_t n
)
108 if ((p
= malloc(n
)) == 0) { perror("malloc"); exit(127); }
112 static char *xstrdup(const char *p
)
114 size_t n
= strlen(p
) + 1;
115 char *q
= xmalloc(n
);
120 static int unix_socket_status(struct sockaddr_un
*sun
, int quickp
)
128 if (stat(sun
->sun_path
, &st
))
129 return (errno
== ENOENT ? UNUSED
: USED
);
130 if (!S_ISSOCK(st
.st_mode
) || quickp
)
133 if ((fp
= fopen("/proc/net/unix", "r")) == 0)
135 fgets(buf
, sizeof(buf
), fp
); /* skip header */
136 len
= strlen(sun
->sun_path
);
137 while (fgets(buf
, sizeof(buf
), fp
)) {
139 if (n
>= len
+ 2 && buf
[n
- len
- 2] == ' ' && buf
[n
- 1] == '\n' &&
140 memcmp(buf
+ n
- len
- 1, sun
->sun_path
, len
) == 0)
153 static void dump_aclnode(aclnode
*a
)
155 char minbuf
[16], maxbuf
[16];
156 struct in_addr amin
, amax
;
158 amin
.s_addr
= htonl(a
->minaddr
);
159 amax
.s_addr
= htonl(a
->maxaddr
);
160 fprintf(stderr
, "noip: %c ", a
->act ?
'+' : '-');
161 if (a
->minaddr
== 0 && a
->maxaddr
== 0xffffffff)
162 fprintf(stderr
, "any");
164 fprintf(stderr
, "%s",
165 inet_ntop(AF_INET
, &amin
, minbuf
, sizeof(minbuf
)));
166 if (a
->maxaddr
!= a
->minaddr
) {
167 fprintf(stderr
, "-%s",
168 inet_ntop(AF_INET
, &amax
, maxbuf
, sizeof(maxbuf
)));
171 if (a
->minport
!= 0 || a
->maxport
!= 0xffff) {
172 fprintf(stderr
, ":%u", (unsigned)a
->minport
);
173 if (a
->minport
!= a
->maxport
)
174 fprintf(stderr
, "-%u", (unsigned)a
->maxport
);
181 static int acl_allows_p(aclnode
*a
, const struct sockaddr_in
*sin
)
183 unsigned long addr
= ntohl(sin
->sin_addr
.s_addr
);
184 unsigned short port
= ntohs(sin
->sin_port
);
188 fprintf(stderr
, "noip: check %s:%u\n",
189 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
190 ntohs((unsigned)sin
->sin_port
)); )
191 for (; a
; a
= a
->next
) {
192 D( dump_aclnode(a
); )
193 if (a
->minaddr
<= addr
&& addr
<= a
->maxaddr
&&
194 a
->minport
<= port
&& port
<= a
->maxport
) {
195 D( fprintf(stderr
, "noip: aha! %s\n", a
->act ?
"ALLOW" : "DENY"); )
200 D( fprintf(stderr
, "noip: nothing found: %s\n", act ?
"DENY" : "ALLOW"); )
206 static void dump_acl(aclnode
*a
)
210 for (; a
; a
= a
->next
) {
214 fprintf(stderr
, "noip: [default policy: %s]\n",
215 act
== ALLOW ?
"DENY" : "ALLOW");
220 static unsigned randrange(unsigned min
, unsigned max
)
224 /* It's so nice not to have to care about the quality of the generator
227 for (mask
= 1; mask
< max
; mask
= (mask
<< 1) | 1)
229 do i
= rand() & mask
; while (i
> max
);
233 static int encode_inet_addr(struct sockaddr_un
*sun
,
234 const struct sockaddr_in
*sin
,
239 char buf
[INET_ADDRSTRLEN
];
242 D( fprintf(stderr
, "noip: encode %s:%u (%s)",
243 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
244 (unsigned)ntohs(sin
->sin_port
),
245 want
== WANT_EXISTING ?
"EXISTING" : "FRESH"); )
246 sun
->sun_family
= AF_UNIX
;
247 if (sin
->sin_port
|| want
== WANT_EXISTING
) {
248 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/%s:%u", sockdir
,
249 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
250 (unsigned)ntohs(sin
->sin_port
));
251 rc
= unix_socket_status(sun
, 0);
252 if (rc
== STALE
) unlink(sun
->sun_path
);
253 if (rc
!= USED
&& want
== WANT_EXISTING
) {
254 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/0.0.0.0:%u",
255 sockdir
, (unsigned)ntohs(sin
->sin_port
));
256 if (unix_socket_status(sun
, 0) == STALE
) unlink(sun
->sun_path
);
259 for (i
= 0; i
< 10; i
++) {
260 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/%s:%u", sockdir
,
261 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
262 randrange(minautoport
, maxautoport
));
263 if (unix_socket_status(sun
, 1) == UNUSED
) goto found
;
265 for (desperatep
= 0; desperatep
< 2; desperatep
++) {
266 for (i
= minautoport
; i
<= maxautoport
; i
++) {
267 snprintf(sun
->sun_path
, sizeof(sun
->sun_path
), "%s/%s:%u", sockdir
,
268 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
270 rc
= unix_socket_status(sun
, !desperatep
);
272 case STALE
: unlink(sun
->sun_path
);
273 case UNUSED
: goto found
;
278 D( fprintf(stderr
, " -- can't resolve\n"); )
282 D( fprintf(stderr
, " -> `%s'\n", sun
->sun_path
); )
286 static int decode_inet_addr(struct sockaddr_in
*sin
,
287 const struct sockaddr_un
*sun
,
290 char buf
[INET_ADDRSTRLEN
+ 16];
292 size_t n
= strlen(sockdir
), nn
= strlen(sun
->sun_path
);
293 struct sockaddr_in sin_mine
;
298 if (sun
->sun_family
!= AF_UNIX
)
300 if (len
< sizeof(sun
)) ((char *)sun
)[len
] = 0;
301 D( fprintf(stderr
, "noip: decode (%d) `%s'",
302 *sun
->sun_path
, sun
->sun_path
); )
303 if (!sun
->sun_path
[0]) {
304 sin
->sin_family
= AF_INET
;
305 sin
->sin_addr
.s_addr
= INADDR_ANY
;
307 D( fprintf(stderr
, " -- unbound socket\n"); )
310 if (nn
< n
+ 1 || nn
- n
>= sizeof(buf
) || sun
->sun_path
[n
] != '/' ||
311 memcmp(sun
->sun_path
, sockdir
, n
) != 0) {
312 D( fprintf(stderr
, " -- not one of ours\n"); )
315 memcpy(buf
, sun
->sun_path
+ n
+ 1, nn
- n
);
316 if ((p
= strchr(buf
, ':')) == 0) {
317 D( fprintf(stderr
, " -- malformed (no port)\n"); )
321 sin
->sin_family
= AF_INET
;
322 if (inet_pton(AF_INET
, buf
, &sin
->sin_addr
) <= 0) {
323 D( fprintf(stderr
, " -- malformed (bad address `%s')\n", buf
); )
326 port
= strtoul(p
, &p
, 10);
327 if (*p
|| port
>= 65536) {
328 D( fprintf(stderr
, " -- malformed (port out of range)"); )
331 sin
->sin_port
= htons(port
);
332 D( fprintf(stderr
, " -> %s:%u\n",
333 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
, sizeof(buf
)),
338 static int fixup_real_ip_socket(int sk
)
343 struct sockaddr_un sun
;
344 struct sockaddr_in sin
;
356 _(LINGER, struct linger) \
359 _(RCVTIMEO, struct timeval) \
360 _(SNDTIMEO, struct timeval)
363 if (real_getsockname(sk
, SA(&sun
), &len
))
365 if (decode_inet_addr(&sin
, &sun
, len
))
366 return (0); /* Not one of ours */
368 if (real_getsockopt(sk
, SOL_SOCKET
, SO_TYPE
, &type
, &len
) < 0 ||
369 (nsk
= real_socket(PF_INET
, type
, 0)) < 0)
371 #define FIX(opt, ty) do { \
374 if (real_getsockopt(sk, SOL_SOCKET, SO_##opt, &ov_, &len) < 0 || \
375 real_setsockopt(nsk, SOL_SOCKET, SO_##opt, &ov_, len)) { \
382 if ((f
= fcntl(sk
, F_GETFL
)) < 0 ||
383 (fd
= fcntl(sk
, F_GETFD
)) < 0 ||
384 fcntl(nsk
, F_SETFL
, f
) < 0 ||
389 unlink(sun
.sun_path
);
391 if (fcntl(sk
, F_SETFD
, fd
) < 0) {
392 perror("noip: fixup_real_ip_socket F_SETFD");
398 static int do_implicit_bind(int sk
, const struct sockaddr
**sa
,
399 socklen_t
*len
, struct sockaddr_un
*sun
)
401 struct sockaddr_in sin
;
402 socklen_t mylen
= sizeof(*sun
);
404 if (acl_allows_p(connect_real
, SIN(*sa
))) {
405 if (fixup_real_ip_socket(sk
))
408 if (real_getsockname(sk
, SA(sun
), &mylen
) < 0)
410 if (sun
->sun_family
== AF_UNIX
) {
411 if (mylen
< sizeof(*sun
)) ((char *)sun
)[mylen
] = 0;
412 if (!sun
->sun_path
[0]) {
413 sin
.sin_family
= AF_INET
;
414 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
416 encode_inet_addr(sun
, &sin
, WANT_FRESH
);
417 if (real_bind(sk
, SA(sun
), SUN_LEN(sun
)))
420 encode_inet_addr(sun
, SIN(*sa
), WANT_EXISTING
);
428 static void return_fake_name(struct sockaddr
*sa
, socklen_t len
,
429 struct sockaddr
*fake
, socklen_t
*fakelen
)
431 struct sockaddr_in sin
;
434 if (sa
->sa_family
== AF_UNIX
&& !decode_inet_addr(&sin
, SUN(sa
), len
)) {
442 memcpy(fake
, sa
, len
);
446 /* --- Configuration --- */
448 static char *home(void)
453 if (getuid() == uid
&&
454 (p
= getenv("HOME")) != 0)
456 else if ((pw
= getpwuid(uid
)) != 0)
462 static char *tmpdir(void)
466 if ((p
= getenv("TMPDIR")) != 0) return (p
);
467 else if ((p
= getenv("TMP")) != 0) return (p
);
468 else return ("/tmp");
471 static char *user(void)
477 if ((p
= getenv("USER")) != 0) return (p
);
478 else if ((p
= getenv("LOGNAME")) != 0) return (p
);
479 else if ((pw
= getpwuid(uid
)) != 0) return (pw
->pw_name
);
481 snprintf(buf
, sizeof(buf
), "uid-%lu", (unsigned long)uid
);
486 #define SKIPSPC do { while (*p && isspace(UC(*p))) p++; } while (0)
487 #define NEXTWORD(q) do { \
490 while (*p && !isspace(UC(*p))) p++; \
493 #define NEXTADDR(q, del) do { \
496 while (*p && (*p == '.' || isdigit(UC(*p)))) p++; \
500 #define NEXTNUMBER(q, del) do { \
503 while (*p && isdigit(UC(*p))) p++; \
507 #define RESCAN(del) do { if (del) *--p = del; } while (0)
508 #define KWMATCHP(kw) (strncmp(p, kw, sizeof(kw) - 1) == 0 && \
509 !isalnum(UC(p[sizeof(kw) - 1])) && \
510 (p += sizeof(kw) - 1))
512 static void parse_ports(char **pp
, unsigned short *min
, unsigned short *max
)
519 { *min
= 0; *max
= 0xffff; }
522 NEXTNUMBER(q
, del
); *min
= strtoul(q
, 0, 0); RESCAN(del
);
525 { NEXTNUMBER(q
, del
); *max
= strtoul(q
, 0, 0); RESCAN(del
); }
532 #define ACLNODE(tail_, act_, \
533 minaddr_, maxaddr_, minport_, maxport_) do { \
537 a_->minaddr = minaddr_; a_->maxaddr = maxaddr_; \
538 a_->minport = minport_; a_->maxport = maxport_; \
539 *tail_ = a_; tail_ = &a_->next; \
542 static void parse_acl_line(char **pp
, aclnode
***tail
)
545 unsigned long minaddr
, maxaddr
, mask
;
546 unsigned short minport
, maxport
;
555 if (*p
== '+') act
= ALLOW
;
556 else if (*p
== '-') act
= DENY
;
561 if (KWMATCHP("any")) {
563 maxaddr
= 0xffffffff;
565 } else if (KWMATCHP("local")) {
566 parse_ports(&p
, &minport
, &maxport
);
567 ACLNODE(*tail
, act
, 0, 0, minport
, maxport
);
568 ACLNODE(*tail
, act
, 0xffffffff, 0xffffffff, minport
, maxport
);
569 for (i
= 0; i
< n_local_ipaddrs
; i
++) {
570 minaddr
= ntohl(local_ipaddrs
[i
].s_addr
);
571 ACLNODE(*tail
, act
, minaddr
, minaddr
, minport
, maxport
);
576 maxaddr
= 0xffffffff;
579 if (inet_pton(AF_INET
, q
, &addr
) <= 0) goto bad
;
580 minaddr
= ntohl(addr
.s_addr
);
586 if (inet_pton(AF_INET
, q
, &addr
) <= 0) goto bad
;
588 maxaddr
= ntohl(addr
.s_addr
);
589 } else if (*p
== '/') {
592 if (strchr(q
, '.')) {
593 if (inet_pton(AF_INET
, q
, &addr
) <= 0) goto bad
;
594 mask
= ntohl(addr
.s_addr
);
596 n
= strtoul(q
, 0, 0);
597 mask
= (~0ul << (32 - n
)) & 0xffffffff;
601 maxaddr
= minaddr
| (mask
^ 0xffffffff);
606 parse_ports(&p
, &minport
, &maxport
);
607 ACLNODE(*tail
, act
, minaddr
, maxaddr
, minport
, maxport
);
610 if (*p
!= ',') break;
616 D( fprintf(stderr
, "noip: bad acl spec (ignored)\n"); )
620 static void parse_autoports(char **pp
)
627 NEXTNUMBER(q
, del
); x
= strtoul(q
, 0, 0); RESCAN(del
);
629 if (*p
!= '-') goto bad
; p
++;
630 NEXTNUMBER(q
, del
); y
= strtoul(q
, 0, 0); RESCAN(del
);
631 minautoport
= x
; maxautoport
= y
;
635 D( fprintf(stderr
, "bad port range (ignored)\n"); )
639 static void parse_acl_env(const char *var
, aclnode
***tail
)
643 if ((p
= getenv(var
)) != 0) {
645 parse_acl_line(&q
, tail
);
650 static void readconfig(void)
657 parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail
);
658 parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail
);
659 if ((p
= getenv("NOIP_AUTOPORTS")) != 0) {
664 if ((p
= getenv("NOIP_CONFIG")) == 0)
665 snprintf(p
= buf
, sizeof(buf
), "%s/.noip", home());
666 D( fprintf(stderr
, "noip: config file: %s\n", p
); )
668 if ((fp
= fopen(p
, "r")) == 0) {
669 D( fprintf(stderr
, "noip: couldn't read config: %s\n",
673 while (fgets(buf
, sizeof(buf
), fp
)) {
678 if (!*p
|| *p
== '#') continue;
679 while (n
&& isspace(UC(buf
[n
- 1]))) n
--;
684 if (strcmp(cmd
, "socketdir") == 0)
685 sockdir
= xstrdup(p
);
686 else if (strcmp(cmd
, "realbind") == 0)
687 parse_acl_line(&p
, &bind_tail
);
688 else if (strcmp(cmd
, "realconnect") == 0)
689 parse_acl_line(&p
, &connect_tail
);
690 else if (strcmp(cmd
, "autoports") == 0)
692 else if (strcmp(cmd
, "debug") == 0)
693 debug
= *p ?
atoi(p
) : 1;
695 D( fprintf(stderr
, "noip: bad config command %s\n", cmd
); )
700 parse_acl_env("NOIP_REALBIND", &bind_tail
);
701 parse_acl_env("NOIP_REALCONNECT", &connect_tail
);
702 parse_acl_env("NOIP_REALBIND_AFTER", &bind_tail
);
703 parse_acl_env("NOIP_REALCONNECT_AFTER", &connect_tail
);
706 if (!sockdir
) sockdir
= getenv("NOIP_SOCKETDIR");
708 snprintf(buf
, sizeof(buf
), "%s/noip-%s", tmpdir(), user());
709 sockdir
= xstrdup(buf
);
711 D( fprintf(stderr
, "noip: socketdir: %s\n", sockdir
);
712 fprintf(stderr
, "noip: autoports: %u-%u\n",
713 minautoport
, maxautoport
);
714 fprintf(stderr
, "noip: realbind acl:\n");
716 fprintf(stderr
, "noip: realconnect acl:\n");
717 dump_acl(connect_real
); )
722 int socket(int pf
, int ty
, int proto
)
728 return real_socket(pf
, ty
, proto
);
731 int socketpair(int pf
, int ty
, int proto
, int *sk
)
737 return (real_socketpair(pf
, ty
, proto
, sk
));
740 int bind(int sk
, const struct sockaddr
*sa
, socklen_t len
)
742 struct sockaddr_un sun
;
744 if (sa
->sa_family
== AF_INET
) {
746 if (acl_allows_p(bind_real
, SIN(sa
))) {
747 if (fixup_real_ip_socket(sk
))
750 encode_inet_addr(&sun
, SIN(sa
), WANT_FRESH
);
756 return real_bind(sk
, sa
, len
);
759 int connect(int sk
, const struct sockaddr
*sa
, socklen_t len
)
761 struct sockaddr_un sun
;
763 if (sa
->sa_family
== AF_INET
) {
765 do_implicit_bind(sk
, &sa
, &len
, &sun
);
768 return real_connect(sk
, sa
, len
);
771 ssize_t
sendto(int sk
, const void *buf
, size_t len
, int flags
,
772 const struct sockaddr
*to
, socklen_t tolen
)
774 struct sockaddr_un sun
;
776 if (to
&& to
->sa_family
== AF_INET
) {
778 do_implicit_bind(sk
, &to
, &tolen
, &sun
);
781 return real_sendto(sk
, buf
, len
, flags
, to
, tolen
);
784 ssize_t
recvfrom(int sk
, void *buf
, size_t len
, int flags
,
785 struct sockaddr
*from
, socklen_t
*fromlen
)
788 socklen_t mylen
= sizeof(sabuf
);
792 return real_recvfrom(sk
, buf
, len
, flags
, 0, 0);
794 n
= real_recvfrom(sk
, buf
, len
, flags
, SA(sabuf
), &mylen
);
797 return_fake_name(SA(sabuf
), mylen
, from
, fromlen
);
802 ssize_t
sendmsg(int sk
, const struct msghdr
*msg
, int flags
)
804 struct sockaddr_un sun
;
805 const struct sockaddr
*sa
;
808 if (msg
->msg_name
&& SA(msg
->msg_name
)->sa_family
== AF_INET
) {
810 sa
= SA(msg
->msg_name
);
812 do_implicit_bind(sk
, &sa
, &mymsg
.msg_namelen
, &sun
);
813 mymsg
.msg_name
= SA(sa
);
817 return real_sendmsg(sk
, msg
, flags
);
820 ssize_t
recvmsg(int sk
, struct msghdr
*msg
, int flags
)
828 return real_recvmsg(sk
, msg
, flags
);
830 sa
= SA(msg
->msg_name
);
831 len
= msg
->msg_namelen
;
832 msg
->msg_name
= sabuf
;
833 msg
->msg_namelen
= sizeof(sabuf
);
834 n
= real_recvmsg(sk
, msg
, flags
);
837 return_fake_name(SA(sabuf
), msg
->msg_namelen
, sa
, &len
);
839 msg
->msg_namelen
= len
;
844 int accept(int sk
, struct sockaddr
*sa
, socklen_t
*len
)
847 socklen_t mylen
= sizeof(sabuf
);
848 int nsk
= real_accept(sk
, SA(sabuf
), &mylen
);
852 return_fake_name(SA(sabuf
), mylen
, sa
, len
);
856 int getsockname(int sk
, struct sockaddr
*sa
, socklen_t
*len
)
860 socklen_t mylen
= sizeof(sabuf
);
861 if (real_getsockname(sk
, SA(sabuf
), &mylen
))
863 return_fake_name(SA(sabuf
), mylen
, sa
, len
);
868 int getpeername(int sk
, struct sockaddr
*sa
, socklen_t
*len
)
872 socklen_t mylen
= sizeof(sabuf
);
873 if (real_getpeername(sk
, SA(sabuf
), &mylen
))
875 return_fake_name(SA(sabuf
), mylen
, sa
, len
);
880 int getsockopt(int sk
, int lev
, int opt
, void *p
, socklen_t
*len
)
890 return real_getsockopt(sk
, lev
, opt
, p
, len
);
893 int setsockopt(int sk
, int lev
, int opt
, const void *p
, socklen_t len
)
902 case SO_BINDTODEVICE
:
903 case SO_ATTACH_FILTER
:
904 case SO_DETACH_FILTER
:
907 return real_setsockopt(sk
, lev
, opt
, p
, len
);
910 /* --- Initialization --- */
912 static void cleanup_sockdir(void)
916 struct sockaddr_in sin
;
917 struct sockaddr_un sun
;
920 if ((dir
= opendir(sockdir
)) == 0)
922 sun
.sun_family
= AF_UNIX
;
923 while ((d
= readdir(dir
)) != 0) {
924 if (d
->d_name
[0] == '.') continue;
925 snprintf(sun
.sun_path
, sizeof(sun
.sun_path
),
926 "%s/%s", sockdir
, d
->d_name
);
927 if (decode_inet_addr(&sin
, &sun
, SUN_LEN(&sun
)) ||
928 stat(sun
.sun_path
, &st
) ||
929 !S_ISSOCK(st
.st_mode
)) {
930 D( fprintf(stderr
, "noip: ignoring unknown socketdir entry `%s'\n",
934 if (unix_socket_status(&sun
, 0) == STALE
) {
935 D( fprintf(stderr
, "noip: clearing away stale socket %s\n",
937 unlink(sun
.sun_path
);
943 static void get_local_ipaddrs(void)
945 struct if_nameindex
*ifn
;
950 ifn
= if_nameindex();
951 if ((sk
= real_socket(PF_INET
, SOCK_STREAM
, 00)) < 0)
953 for (i
= n_local_ipaddrs
= 0;
954 n_local_ipaddrs
< MAX_LOCAL_IPADDRS
&&
955 ifn
[i
].if_name
&& *ifn
[i
].if_name
;
957 strcpy(ifr
.ifr_name
, ifn
[i
].if_name
);
958 if (ioctl(sk
, SIOCGIFADDR
, &ifr
) || ifr
.ifr_addr
.sa_family
!= AF_INET
)
960 local_ipaddrs
[n_local_ipaddrs
++] =
961 SIN(&ifr
.ifr_addr
)->sin_addr
;
962 D( fprintf(stderr
, "noip: local addr %s = %s\n", ifn
[i
].if_name
,
963 inet_ntoa(local_ipaddrs
[n_local_ipaddrs
- 1])); )
968 static void printerr(const char *p
) { write(STDERR_FILENO
, p
, strlen(p
)); }
970 static void create_sockdir(void)
974 if (stat(sockdir
, &st
)) {
975 if (errno
== ENOENT
) {
976 if (mkdir(sockdir
, 0700)) {
977 perror("noip: creating socketdir");
980 if (!stat(sockdir
, &st
))
983 perror("noip: checking socketdir");
987 if (!S_ISDIR(st
.st_mode
)) {
988 printerr("noip: bad socketdir: not a directory\n");
991 if (st
.st_uid
!= uid
) {
992 printerr("noip: bad socketdir: not owner\n");
995 if (st
.st_mode
& 077) {
996 printerr("noip: bad socketdir: not private\n");
1001 static void setup(void)
1008 if ((p
= getenv("NOIP_DEBUG")) && atoi(p
))
1010 get_local_ipaddrs();