Commit | Line | Data |
---|---|---|
1d1ccf4f MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Make programs use Unix-domain sockets instead of IP | |
4 | * | |
5 | * (c) 2008 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of the preload-hacks package. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | * more details. | |
21 | * | |
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. | |
25 | */ | |
26 | ||
e4976bb0 | 27 | #define _GNU_SOURCE |
28 | #undef sun | |
29 | #undef SUN | |
30 | #define DEBUG | |
31 | ||
1d1ccf4f MW |
32 | /*----- Header files ------------------------------------------------------*/ |
33 | ||
e4976bb0 | 34 | #include <ctype.h> |
35 | #include <errno.h> | |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | ||
39 | #include <unistd.h> | |
40 | #include <dirent.h> | |
41 | #include <dlfcn.h> | |
42 | #include <fcntl.h> | |
43 | #include <pwd.h> | |
44 | ||
45 | #include <sys/ioctl.h> | |
46 | #include <sys/socket.h> | |
47 | #include <sys/stat.h> | |
48 | #include <sys/un.h> | |
49 | ||
50 | #include <netinet/in.h> | |
51 | #include <arpa/inet.h> | |
52 | #include <netinet/tcp.h> | |
53 | #include <netinet/udp.h> | |
54 | #include <net/if.h> | |
55 | ||
1d1ccf4f MW |
56 | /*----- Data structures ---------------------------------------------------*/ |
57 | ||
58 | enum { UNUSED, STALE, USED }; /* Unix socket status values */ | |
59 | enum { WANT_FRESH, WANT_EXISTING }; /* Socket address dispositions */ | |
60 | enum { DENY, ALLOW }; /* ACL verdicts */ | |
61 | ||
62 | /* Access control list nodes */ | |
e4976bb0 | 63 | typedef struct aclnode { |
64 | struct aclnode *next; | |
65 | int act; | |
66 | unsigned long minaddr, maxaddr; | |
67 | unsigned short minport, maxport; | |
68 | } aclnode; | |
69 | ||
1d1ccf4f | 70 | /* Local address records */ |
e4976bb0 | 71 | #define MAX_LOCAL_IPADDRS 16 |
72 | static struct in_addr local_ipaddrs[MAX_LOCAL_IPADDRS]; | |
73 | static int n_local_ipaddrs; | |
74 | ||
1d1ccf4f | 75 | /* General configuration */ |
e4976bb0 | 76 | static uid_t uid; |
77 | static char *sockdir = 0; | |
78 | static int debug = 0; | |
f6049fdd | 79 | static unsigned minautoport = 16384, maxautoport = 65536; |
e4976bb0 | 80 | |
1d1ccf4f | 81 | /* Access control lists */ |
e4976bb0 | 82 | static aclnode *bind_real, **bind_tail = &bind_real; |
83 | static aclnode *connect_real, **connect_tail = &connect_real; | |
84 | ||
1d1ccf4f | 85 | /*----- Import the real versions of functions -----------------------------*/ |
e4976bb0 | 86 | |
1d1ccf4f | 87 | /* The list of functions to immport. */ |
e4976bb0 | 88 | #define IMPORTS(_) \ |
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)) \ | |
104 | _(close, int, (int)) | |
105 | ||
1d1ccf4f | 106 | /* Function pointers to set up. */ |
e4976bb0 | 107 | #define DECL(imp, ret, args) static ret (*real_##imp) args; |
108 | IMPORTS(DECL) | |
109 | #undef DECL | |
110 | ||
1d1ccf4f | 111 | /* Import the system calls. */ |
e4976bb0 | 112 | static void import(void) |
113 | { | |
114 | #define IMPORT(imp, ret, args) \ | |
115 | real_##imp = (ret (*)args)dlsym(RTLD_NEXT, #imp); | |
116 | IMPORTS(IMPORT) | |
117 | #undef IMPORT | |
118 | } | |
119 | ||
1d1ccf4f | 120 | /*----- Utilities ---------------------------------------------------------*/ |
e4976bb0 | 121 | |
1d1ccf4f | 122 | /* Socket address casts */ |
e4976bb0 | 123 | #define SA(sa) ((struct sockaddr *)(sa)) |
124 | #define SIN(sa) ((struct sockaddr_in *)(sa)) | |
125 | #define SUN(sa) ((struct sockaddr_un *)(sa)) | |
126 | ||
1d1ccf4f | 127 | /* Raw bytes */ |
e4976bb0 | 128 | #define UC(ch) ((unsigned char)(ch)) |
129 | ||
1d1ccf4f | 130 | /* Memory allocation */ |
e4976bb0 | 131 | #define NEW(x) ((x) = xmalloc(sizeof(*x))) |
132 | #define NEWV(x, n) ((x) = xmalloc(sizeof(*x) * (n))) | |
133 | ||
1d1ccf4f | 134 | /* Debugging */ |
e4976bb0 | 135 | #ifdef DEBUG |
136 | # define D(body) { if (debug) { body } } | |
137 | #else | |
138 | # define D(body) ; | |
139 | #endif | |
140 | ||
1d1ccf4f | 141 | /* Preservation of error status */ |
e4976bb0 | 142 | #define PRESERVING_ERRNO(body) do { \ |
143 | int _err = errno; { body } errno = _err; \ | |
144 | } while (0) | |
145 | ||
1d1ccf4f | 146 | /* Allocate N bytes of memory; abort on failure. */ |
e4976bb0 | 147 | static void *xmalloc(size_t n) |
148 | { | |
149 | void *p; | |
150 | if (!n) return (0); | |
3ef1fec9 | 151 | if ((p = malloc(n)) == 0) { perror("malloc"); exit(127); } |
e4976bb0 | 152 | return (p); |
153 | } | |
154 | ||
1d1ccf4f | 155 | /* Allocate a copy of the null-terminated string P; abort on failure. */ |
e4976bb0 | 156 | static char *xstrdup(const char *p) |
157 | { | |
158 | size_t n = strlen(p) + 1; | |
159 | char *q = xmalloc(n); | |
160 | memcpy(q, p, n); | |
161 | return (q); | |
162 | } | |
1d1ccf4f | 163 | /*----- Access control lists ----------------------------------------------*/ |
e4976bb0 | 164 | |
165 | #ifdef DEBUG | |
166 | ||
1d1ccf4f | 167 | /* Write to standard error a description of the ACL node A. */ |
e4976bb0 | 168 | static void dump_aclnode(aclnode *a) |
169 | { | |
170 | char minbuf[16], maxbuf[16]; | |
171 | struct in_addr amin, amax; | |
172 | ||
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"); | |
178 | else { | |
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))); | |
184 | } | |
185 | } | |
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); | |
190 | } | |
191 | fputc('\n', stderr); | |
192 | } | |
193 | ||
1d1ccf4f MW |
194 | static void dump_acl(aclnode *a) |
195 | { | |
196 | int act = ALLOW; | |
197 | ||
198 | for (; a; a = a->next) { | |
199 | dump_aclnode(a); | |
200 | act = a->act; | |
201 | } | |
202 | fprintf(stderr, "noip: [default policy: %s]\n", | |
203 | act == ALLOW ? "DENY" : "ALLOW"); | |
204 | } | |
205 | ||
e4976bb0 | 206 | #endif |
207 | ||
1d1ccf4f | 208 | /* Returns nonzero if the ACL A allows the IP socket SIN. */ |
e4976bb0 | 209 | static int acl_allows_p(aclnode *a, const struct sockaddr_in *sin) |
210 | { | |
211 | unsigned long addr = ntohl(sin->sin_addr.s_addr); | |
212 | unsigned short port = ntohs(sin->sin_port); | |
213 | int act = ALLOW; | |
214 | ||
215 | D( char buf[16]; | |
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"); ) | |
224 | return (a->act); | |
225 | } | |
226 | act = a->act; | |
227 | } | |
228 | D( fprintf(stderr, "noip: nothing found: %s\n", act ? "DENY" : "ALLOW"); ) | |
229 | return (!act); | |
230 | } | |
231 | ||
1d1ccf4f | 232 | /*----- Socket address conversion -----------------------------------------*/ |
e4976bb0 | 233 | |
1d1ccf4f | 234 | /* Return a uniformly distributed integer between MIN and MAX inclusive. */ |
f6049fdd | 235 | static unsigned randrange(unsigned min, unsigned max) |
236 | { | |
237 | unsigned mask, i; | |
238 | ||
239 | /* It's so nice not to have to care about the quality of the generator | |
240 | much! */ | |
241 | max -= min; | |
242 | for (mask = 1; mask < max; mask = (mask << 1) | 1) | |
243 | ; | |
244 | do i = rand() & mask; while (i > max); | |
245 | return (i + min); | |
246 | } | |
247 | ||
1d1ccf4f MW |
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. | |
253 | */ | |
254 | static int unix_socket_status(struct sockaddr_un *sun, int quickp) | |
255 | { | |
256 | struct stat st; | |
257 | FILE *fp = 0; | |
258 | size_t len, n; | |
259 | int rc; | |
260 | char buf[256]; | |
261 | ||
262 | if (stat(sun->sun_path, &st)) | |
263 | return (errno == ENOENT ? UNUSED : USED); | |
264 | if (!S_ISSOCK(st.st_mode) || quickp) | |
265 | return (USED); | |
266 | rc = USED; | |
267 | if ((fp = fopen("/proc/net/unix", "r")) == 0) | |
268 | goto done; | |
269 | fgets(buf, sizeof(buf), fp); /* skip header */ | |
270 | len = strlen(sun->sun_path); | |
271 | while (fgets(buf, sizeof(buf), fp)) { | |
272 | n = strlen(buf); | |
273 | if (n >= len + 2 && buf[n - len - 2] == ' ' && buf[n - 1] == '\n' && | |
274 | memcmp(buf + n - len - 1, sun->sun_path, len) == 0) | |
275 | goto done; | |
276 | } | |
277 | if (ferror(fp)) | |
278 | goto done; | |
279 | rc = STALE; | |
280 | done: | |
281 | if (fp) fclose(fp); | |
282 | return (rc); | |
283 | } | |
284 | ||
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. | |
289 | */ | |
e4976bb0 | 290 | static int encode_inet_addr(struct sockaddr_un *sun, |
291 | const struct sockaddr_in *sin, | |
292 | int want) | |
293 | { | |
294 | int i; | |
3ef1fec9 | 295 | int desperatep = 0; |
e4976bb0 | 296 | char buf[INET_ADDRSTRLEN]; |
297 | int rc; | |
298 | ||
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); | |
314 | } | |
315 | } else { | |
f6049fdd | 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; | |
321 | } | |
3ef1fec9 | 322 | for (desperatep = 0; desperatep < 2; desperatep++) { |
f6049fdd | 323 | for (i = minautoport; i <= maxautoport; i++) { |
e4976bb0 | 324 | snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir, |
325 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
326 | (unsigned)i); | |
3ef1fec9 | 327 | rc = unix_socket_status(sun, !desperatep); |
e4976bb0 | 328 | switch (rc) { |
329 | case STALE: unlink(sun->sun_path); | |
330 | case UNUSED: goto found; | |
331 | } | |
332 | } | |
333 | } | |
334 | errno = EADDRINUSE; | |
335 | D( fprintf(stderr, " -- can't resolve\n"); ) | |
336 | return (-1); | |
337 | found:; | |
338 | } | |
339 | D( fprintf(stderr, " -> `%s'\n", sun->sun_path); ) | |
340 | return (0); | |
341 | } | |
342 | ||
1d1ccf4f MW |
343 | /* Decode the Unix address SUN to an Internet address SIN. Returns zero on |
344 | * success; -1 on failure (e.g., it wasn't one of our addresses). */ | |
e4976bb0 | 345 | static int decode_inet_addr(struct sockaddr_in *sin, |
346 | const struct sockaddr_un *sun, | |
347 | socklen_t len) | |
348 | { | |
349 | char buf[INET_ADDRSTRLEN + 16]; | |
350 | char *p; | |
351 | size_t n = strlen(sockdir), nn = strlen(sun->sun_path); | |
352 | struct sockaddr_in sin_mine; | |
353 | unsigned long port; | |
354 | ||
355 | if (!sin) | |
356 | sin = &sin_mine; | |
357 | if (sun->sun_family != AF_UNIX) | |
358 | return (-1); | |
359 | if (len < sizeof(sun)) ((char *)sun)[len] = 0; | |
360 | D( fprintf(stderr, "noip: decode (%d) `%s'", | |
361 | *sun->sun_path, sun->sun_path); ) | |
362 | if (!sun->sun_path[0]) { | |
363 | sin->sin_family = AF_INET; | |
364 | sin->sin_addr.s_addr = INADDR_ANY; | |
365 | sin->sin_port = 0; | |
366 | D( fprintf(stderr, " -- unbound socket\n"); ) | |
367 | return (0); | |
368 | } | |
369 | if (nn < n + 1 || nn - n >= sizeof(buf) || sun->sun_path[n] != '/' || | |
370 | memcmp(sun->sun_path, sockdir, n) != 0) { | |
371 | D( fprintf(stderr, " -- not one of ours\n"); ) | |
372 | return (-1); | |
373 | } | |
374 | memcpy(buf, sun->sun_path + n + 1, nn - n); | |
375 | if ((p = strchr(buf, ':')) == 0) { | |
376 | D( fprintf(stderr, " -- malformed (no port)\n"); ) | |
377 | return (-1); | |
378 | } | |
379 | *p++ = 0; | |
380 | sin->sin_family = AF_INET; | |
381 | if (inet_pton(AF_INET, buf, &sin->sin_addr) <= 0) { | |
382 | D( fprintf(stderr, " -- malformed (bad address `%s')\n", buf); ) | |
383 | return (-1); | |
384 | } | |
385 | port = strtoul(p, &p, 10); | |
386 | if (*p || port >= 65536) { | |
387 | D( fprintf(stderr, " -- malformed (port out of range)"); ) | |
388 | return (-1); | |
389 | } | |
390 | sin->sin_port = htons(port); | |
391 | D( fprintf(stderr, " -> %s:%u\n", | |
392 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
393 | (unsigned)port); ) | |
394 | return (0); | |
395 | } | |
396 | ||
1d1ccf4f MW |
397 | /* SK is (or at least might be) a Unix-domain socket we created when an |
398 | * Internet socket was asked for. We've decided it should be an Internet | |
399 | * socket after all, so convert it. | |
400 | */ | |
e4976bb0 | 401 | static int fixup_real_ip_socket(int sk) |
402 | { | |
403 | int nsk; | |
404 | int type; | |
405 | int f, fd; | |
406 | struct sockaddr_un sun; | |
407 | struct sockaddr_in sin; | |
408 | socklen_t len; | |
409 | ||
410 | #define OPTS(_) \ | |
411 | _(DEBUG, int) \ | |
412 | _(REUSEADDR, int) \ | |
413 | _(DONTROUTE, int) \ | |
414 | _(BROADCAST, int) \ | |
415 | _(SNDBUF, int) \ | |
416 | _(RCVBUF, int) \ | |
417 | _(OOBINLINE, int) \ | |
418 | _(NO_CHECK, int) \ | |
419 | _(LINGER, struct linger) \ | |
420 | _(BSDCOMPAT, int) \ | |
421 | _(RCVLOWAT, int) \ | |
422 | _(RCVTIMEO, struct timeval) \ | |
423 | _(SNDTIMEO, struct timeval) | |
424 | ||
425 | len = sizeof(sun); | |
426 | if (real_getsockname(sk, SA(&sun), &len)) | |
427 | return (-1); | |
428 | if (decode_inet_addr(&sin, &sun, len)) | |
429 | return (0); /* Not one of ours */ | |
430 | len = sizeof(type); | |
431 | if (real_getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0 || | |
432 | (nsk = real_socket(PF_INET, type, 0)) < 0) | |
433 | return (-1); | |
434 | #define FIX(opt, ty) do { \ | |
435 | ty ov_; \ | |
436 | len = sizeof(ov_); \ | |
437 | if (real_getsockopt(sk, SOL_SOCKET, SO_##opt, &ov_, &len) < 0 || \ | |
438 | real_setsockopt(nsk, SOL_SOCKET, SO_##opt, &ov_, len)) { \ | |
439 | real_close(nsk); \ | |
440 | return (-1); \ | |
441 | } \ | |
442 | } while (0); | |
443 | OPTS(FIX) | |
444 | #undef FIX | |
445 | if ((f = fcntl(sk, F_GETFL)) < 0 || | |
446 | (fd = fcntl(sk, F_GETFD)) < 0 || | |
447 | fcntl(nsk, F_SETFL, f) < 0 || | |
448 | dup2(nsk, sk) < 0) { | |
449 | real_close(nsk); | |
450 | return (-1); | |
451 | } | |
452 | unlink(sun.sun_path); | |
453 | real_close(nsk); | |
454 | if (fcntl(sk, F_SETFD, fd) < 0) { | |
455 | perror("noip: fixup_real_ip_socket F_SETFD"); | |
456 | abort(); | |
457 | } | |
458 | return (0); | |
459 | } | |
460 | ||
1d1ccf4f MW |
461 | /* The socket SK is about to be used to communicate with the remote address |
462 | * SA. Assign it a local address so that getpeername does something useful. | |
463 | */ | |
e4976bb0 | 464 | static int do_implicit_bind(int sk, const struct sockaddr **sa, |
465 | socklen_t *len, struct sockaddr_un *sun) | |
466 | { | |
467 | struct sockaddr_in sin; | |
468 | socklen_t mylen = sizeof(*sun); | |
469 | ||
470 | if (acl_allows_p(connect_real, SIN(*sa))) { | |
471 | if (fixup_real_ip_socket(sk)) | |
472 | return (-1); | |
473 | } else { | |
474 | if (real_getsockname(sk, SA(sun), &mylen) < 0) | |
475 | return (-1); | |
476 | if (sun->sun_family == AF_UNIX) { | |
477 | if (mylen < sizeof(*sun)) ((char *)sun)[mylen] = 0; | |
478 | if (!sun->sun_path[0]) { | |
479 | sin.sin_family = AF_INET; | |
4ab301de | 480 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
e4976bb0 | 481 | sin.sin_port = 0; |
482 | encode_inet_addr(sun, &sin, WANT_FRESH); | |
483 | if (real_bind(sk, SA(sun), SUN_LEN(sun))) | |
484 | return (-1); | |
485 | } | |
486 | encode_inet_addr(sun, SIN(*sa), WANT_EXISTING); | |
487 | *sa = SA(sun); | |
488 | *len = SUN_LEN(sun); | |
489 | } | |
490 | } | |
491 | return (0); | |
492 | } | |
493 | ||
1d1ccf4f MW |
494 | /* We found the real address SA, with length LEN; if it's a Unix-domain |
495 | * address corresponding to a fake socket, convert it to cover up the | |
496 | * deception. Whatever happens, put the result at FAKE and store its length | |
497 | * at FAKELEN. | |
498 | */ | |
e4976bb0 | 499 | static void return_fake_name(struct sockaddr *sa, socklen_t len, |
500 | struct sockaddr *fake, socklen_t *fakelen) | |
501 | { | |
502 | struct sockaddr_in sin; | |
503 | socklen_t alen; | |
504 | ||
505 | if (sa->sa_family == AF_UNIX && !decode_inet_addr(&sin, SUN(sa), len)) { | |
506 | sa = SA(&sin); | |
507 | len = sizeof(sin); | |
508 | } | |
509 | alen = len; | |
510 | if (len > *fakelen) | |
511 | len = *fakelen; | |
512 | if (len > 0) | |
513 | memcpy(fake, sa, len); | |
514 | *fakelen = alen; | |
515 | } | |
516 | ||
1d1ccf4f | 517 | /*----- Configuration -----------------------------------------------------*/ |
e4976bb0 | 518 | |
1d1ccf4f | 519 | /* Return the process owner's home directory. */ |
e4976bb0 | 520 | static char *home(void) |
521 | { | |
522 | char *p; | |
523 | struct passwd *pw; | |
524 | ||
3ef1fec9 | 525 | if (getuid() == uid && |
526 | (p = getenv("HOME")) != 0) | |
527 | return (p); | |
528 | else if ((pw = getpwuid(uid)) != 0) | |
529 | return (pw->pw_dir); | |
530 | else | |
531 | return "/notexist"; | |
e4976bb0 | 532 | } |
533 | ||
1d1ccf4f | 534 | /* Return a good temporary directory to use. */ |
e4976bb0 | 535 | static char *tmpdir(void) |
536 | { | |
537 | char *p; | |
538 | ||
539 | if ((p = getenv("TMPDIR")) != 0) return (p); | |
540 | else if ((p = getenv("TMP")) != 0) return (p); | |
541 | else return ("/tmp"); | |
542 | } | |
543 | ||
1d1ccf4f | 544 | /* Return the user's name, or at least something distinctive. */ |
e4976bb0 | 545 | static char *user(void) |
546 | { | |
547 | static char buf[16]; | |
548 | char *p; | |
549 | struct passwd *pw; | |
550 | ||
551 | if ((p = getenv("USER")) != 0) return (p); | |
552 | else if ((p = getenv("LOGNAME")) != 0) return (p); | |
553 | else if ((pw = getpwuid(uid)) != 0) return (pw->pw_name); | |
554 | else { | |
555 | snprintf(buf, sizeof(buf), "uid-%lu", (unsigned long)uid); | |
556 | return (buf); | |
557 | } | |
558 | } | |
559 | ||
1d1ccf4f | 560 | /* Skip P over space characters. */ |
e4976bb0 | 561 | #define SKIPSPC do { while (*p && isspace(UC(*p))) p++; } while (0) |
1d1ccf4f MW |
562 | |
563 | /* Set Q to point to the next word following P, null-terminate it, and step P | |
564 | * past it. */ | |
e4976bb0 | 565 | #define NEXTWORD(q) do { \ |
566 | SKIPSPC; \ | |
567 | q = p; \ | |
568 | while (*p && !isspace(UC(*p))) p++; \ | |
569 | if (*p) *p++ = 0; \ | |
570 | } while (0) | |
1d1ccf4f MW |
571 | |
572 | /* Set Q to point to the next dotted-quad address, store the ending delimiter | |
573 | * in DEL, null-terminate it, and step P past it. */ | |
e4976bb0 | 574 | #define NEXTADDR(q, del) do { \ |
575 | SKIPSPC; \ | |
576 | q = p; \ | |
577 | while (*p && (*p == '.' || isdigit(UC(*p)))) p++; \ | |
578 | del = *p; \ | |
579 | if (*p) *p++ = 0; \ | |
580 | } while (0) | |
1d1ccf4f MW |
581 | |
582 | /* Set Q to point to the next decimal number, store the ending delimiter in | |
583 | * DEL, null-terminate it, and step P past it. */ | |
e4976bb0 | 584 | #define NEXTNUMBER(q, del) do { \ |
585 | SKIPSPC; \ | |
586 | q = p; \ | |
587 | while (*p && isdigit(UC(*p))) p++; \ | |
588 | del = *p; \ | |
589 | if (*p) *p++ = 0; \ | |
590 | } while (0) | |
1d1ccf4f MW |
591 | |
592 | /* Push the character DEL back so we scan it again, unless it's zero | |
593 | * (end-of-file). */ | |
e4976bb0 | 594 | #define RESCAN(del) do { if (del) *--p = del; } while (0) |
1d1ccf4f MW |
595 | |
596 | /* Evaluate true if P is pointing to the word KW (and not some longer string | |
597 | * of which KW is a prefix). */ | |
598 | ||
e4976bb0 | 599 | #define KWMATCHP(kw) (strncmp(p, kw, sizeof(kw) - 1) == 0 && \ |
600 | !isalnum(UC(p[sizeof(kw) - 1])) && \ | |
601 | (p += sizeof(kw) - 1)) | |
9f82ba1f | 602 | |
1d1ccf4f MW |
603 | /* Parse a port list, starting at *PP. Port lists have the form |
604 | * [:LOW[-HIGH]]: if omitted, all ports are included; if HIGH is omitted, | |
605 | * it's as if HIGH = LOW. Store LOW in *MIN, HIGH in *MAX and set *PP to the | |
606 | * rest of the string. | |
607 | */ | |
e4976bb0 | 608 | static void parse_ports(char **pp, unsigned short *min, unsigned short *max) |
609 | { | |
610 | char *p = *pp, *q; | |
611 | int del; | |
612 | ||
613 | SKIPSPC; | |
f6049fdd | 614 | if (*p != ':') |
615 | { *min = 0; *max = 0xffff; } | |
616 | else { | |
e4976bb0 | 617 | p++; |
f6049fdd | 618 | NEXTNUMBER(q, del); *min = strtoul(q, 0, 0); RESCAN(del); |
e4976bb0 | 619 | SKIPSPC; |
f6049fdd | 620 | if (*p == '-') |
d83beb5c | 621 | { p++; NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); } |
f6049fdd | 622 | else |
e4976bb0 | 623 | *max = *min; |
624 | } | |
625 | *pp = p; | |
626 | } | |
627 | ||
1d1ccf4f MW |
628 | /* Make a new ACL node. ACT is the verdict; MINADDR and MAXADDR are the |
629 | * ranges on IP addresses; MINPORT and MAXPORT are the ranges on port | |
630 | * numbers; TAIL is the list tail to attach the new node to. | |
631 | */ | |
e4976bb0 | 632 | #define ACLNODE(tail_, act_, \ |
633 | minaddr_, maxaddr_, minport_, maxport_) do { \ | |
634 | aclnode *a_; \ | |
635 | NEW(a_); \ | |
636 | a_->act = act_; \ | |
637 | a_->minaddr = minaddr_; a_->maxaddr = maxaddr_; \ | |
638 | a_->minport = minport_; a_->maxport = maxport_; \ | |
639 | *tail_ = a_; tail_ = &a_->next; \ | |
640 | } while (0) | |
641 | ||
1d1ccf4f MW |
642 | /* Parse an ACL line. *PP points to the end of the line; *TAIL points to |
643 | * the list tail (i.e., the final link in the list). An ACL entry has the | |
644 | * form +|- [any | local | ADDR | ADDR - ADDR | ADDR/ADDR | ADDR/INT] PORTS | |
645 | * where PORTS is parsed by parse_ports above; an ACL line consists of a | |
646 | * comma-separated sequence of entries.. | |
647 | */ | |
e4976bb0 | 648 | static void parse_acl_line(char **pp, aclnode ***tail) |
649 | { | |
650 | struct in_addr addr; | |
651 | unsigned long minaddr, maxaddr, mask; | |
652 | unsigned short minport, maxport; | |
653 | int i, n; | |
654 | int act; | |
655 | int del; | |
656 | char *p = *pp; | |
657 | char *q; | |
658 | ||
a6d9626b | 659 | for (;;) { |
660 | SKIPSPC; | |
661 | if (*p == '+') act = ALLOW; | |
662 | else if (*p == '-') act = DENY; | |
663 | else goto bad; | |
e4976bb0 | 664 | |
a6d9626b | 665 | p++; |
666 | SKIPSPC; | |
667 | if (KWMATCHP("any")) { | |
e4976bb0 | 668 | minaddr = 0; |
669 | maxaddr = 0xffffffff; | |
a6d9626b | 670 | goto justone; |
671 | } else if (KWMATCHP("local")) { | |
672 | parse_ports(&p, &minport, &maxport); | |
673 | ACLNODE(*tail, act, 0, 0, minport, maxport); | |
674 | ACLNODE(*tail, act, 0xffffffff, 0xffffffff, minport, maxport); | |
675 | for (i = 0; i < n_local_ipaddrs; i++) { | |
676 | minaddr = ntohl(local_ipaddrs[i].s_addr); | |
677 | ACLNODE(*tail, act, minaddr, minaddr, minport, maxport); | |
678 | } | |
e4976bb0 | 679 | } else { |
a6d9626b | 680 | if (*p == ':') { |
681 | minaddr = 0; | |
682 | maxaddr = 0xffffffff; | |
683 | } else { | |
e4976bb0 | 684 | NEXTADDR(q, del); |
685 | if (inet_pton(AF_INET, q, &addr) <= 0) goto bad; | |
a6d9626b | 686 | minaddr = ntohl(addr.s_addr); |
e4976bb0 | 687 | RESCAN(del); |
a6d9626b | 688 | SKIPSPC; |
689 | if (*p == '-') { | |
690 | p++; | |
691 | NEXTADDR(q, del); | |
e4976bb0 | 692 | if (inet_pton(AF_INET, q, &addr) <= 0) goto bad; |
a6d9626b | 693 | RESCAN(del); |
694 | maxaddr = ntohl(addr.s_addr); | |
695 | } else if (*p == '/') { | |
696 | p++; | |
697 | NEXTADDR(q, del); | |
698 | if (strchr(q, '.')) { | |
699 | if (inet_pton(AF_INET, q, &addr) <= 0) goto bad; | |
700 | mask = ntohl(addr.s_addr); | |
701 | } else { | |
f6049fdd | 702 | n = strtoul(q, 0, 0); |
a6d9626b | 703 | mask = (~0ul << (32 - n)) & 0xffffffff; |
704 | } | |
705 | RESCAN(del); | |
706 | minaddr &= mask; | |
707 | maxaddr = minaddr | (mask ^ 0xffffffff); | |
708 | } else | |
709 | maxaddr = minaddr; | |
710 | } | |
711 | justone: | |
712 | parse_ports(&p, &minport, &maxport); | |
713 | ACLNODE(*tail, act, minaddr, maxaddr, minport, maxport); | |
e4976bb0 | 714 | } |
a6d9626b | 715 | SKIPSPC; |
716 | if (*p != ',') break; | |
717 | p++; | |
e4976bb0 | 718 | } |
719 | return; | |
720 | ||
721 | bad: | |
722 | D( fprintf(stderr, "noip: bad acl spec (ignored)\n"); ) | |
723 | return; | |
724 | } | |
725 | ||
1d1ccf4f | 726 | /* Parse the autoports configuration directive. Syntax is MIN - MAX. */ |
f6049fdd | 727 | static void parse_autoports(char **pp) |
728 | { | |
729 | char *p = *pp, *q; | |
730 | unsigned x, y; | |
731 | int del; | |
732 | ||
733 | SKIPSPC; | |
734 | NEXTNUMBER(q, del); x = strtoul(q, 0, 0); RESCAN(del); | |
735 | SKIPSPC; | |
736 | if (*p != '-') goto bad; p++; | |
737 | NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del); | |
738 | minautoport = x; maxautoport = y; | |
739 | return; | |
740 | ||
741 | bad: | |
742 | D( fprintf(stderr, "bad port range (ignored)\n"); ) | |
743 | return; | |
744 | } | |
745 | ||
1d1ccf4f MW |
746 | /* Parse an ACL from an environment variable VAR, attaching it to the list |
747 | * TAIL. */ | |
a6d9626b | 748 | static void parse_acl_env(const char *var, aclnode ***tail) |
749 | { | |
f6049fdd | 750 | char *p, *q; |
a6d9626b | 751 | |
752 | if ((p = getenv(var)) != 0) { | |
f6049fdd | 753 | p = q = xstrdup(p); |
754 | parse_acl_line(&q, tail); | |
a6d9626b | 755 | free(p); |
756 | } | |
757 | } | |
758 | ||
1d1ccf4f | 759 | /* Read the configuration from the config file and environment. */ |
e4976bb0 | 760 | static void readconfig(void) |
761 | { | |
762 | FILE *fp; | |
763 | char buf[1024]; | |
764 | size_t n; | |
f6049fdd | 765 | char *p, *q, *cmd; |
e4976bb0 | 766 | |
a6d9626b | 767 | parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail); |
768 | parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail); | |
f6049fdd | 769 | if ((p = getenv("NOIP_AUTOPORTS")) != 0) { |
770 | p = q = xstrdup(p); | |
771 | parse_autoports(&q); | |
772 | free(p); | |
773 | } | |
4ab301de | 774 | if ((p = getenv("NOIP_CONFIG")) == 0) |
775 | snprintf(p = buf, sizeof(buf), "%s/.noip", home()); | |
776 | D( fprintf(stderr, "noip: config file: %s\n", p); ) | |
777 | ||
778 | if ((fp = fopen(p, "r")) == 0) { | |
779 | D( fprintf(stderr, "noip: couldn't read config: %s\n", | |
780 | strerror(errno)); ) | |
f6049fdd | 781 | goto done; |
4ab301de | 782 | } |
e4976bb0 | 783 | while (fgets(buf, sizeof(buf), fp)) { |
784 | n = strlen(buf); | |
785 | p = buf; | |
786 | ||
787 | SKIPSPC; | |
788 | if (!*p || *p == '#') continue; | |
789 | while (n && isspace(UC(buf[n - 1]))) n--; | |
790 | buf[n] = 0; | |
791 | NEXTWORD(cmd); | |
792 | SKIPSPC; | |
793 | ||
794 | if (strcmp(cmd, "socketdir") == 0) | |
795 | sockdir = xstrdup(p); | |
796 | else if (strcmp(cmd, "realbind") == 0) | |
797 | parse_acl_line(&p, &bind_tail); | |
798 | else if (strcmp(cmd, "realconnect") == 0) | |
799 | parse_acl_line(&p, &connect_tail); | |
f6049fdd | 800 | else if (strcmp(cmd, "autoports") == 0) |
801 | parse_autoports(&p); | |
e4976bb0 | 802 | else if (strcmp(cmd, "debug") == 0) |
803 | debug = *p ? atoi(p) : 1; | |
804 | else | |
805 | D( fprintf(stderr, "noip: bad config command %s\n", cmd); ) | |
806 | } | |
807 | fclose(fp); | |
808 | ||
809 | done: | |
a6d9626b | 810 | parse_acl_env("NOIP_REALBIND", &bind_tail); |
811 | parse_acl_env("NOIP_REALCONNECT", &connect_tail); | |
812 | parse_acl_env("NOIP_REALBIND_AFTER", &bind_tail); | |
813 | parse_acl_env("NOIP_REALCONNECT_AFTER", &connect_tail); | |
e4976bb0 | 814 | *bind_tail = 0; |
815 | *connect_tail = 0; | |
a6d9626b | 816 | if (!sockdir) sockdir = getenv("NOIP_SOCKETDIR"); |
e4976bb0 | 817 | if (!sockdir) { |
818 | snprintf(buf, sizeof(buf), "%s/noip-%s", tmpdir(), user()); | |
819 | sockdir = xstrdup(buf); | |
820 | } | |
3ef1fec9 | 821 | D( fprintf(stderr, "noip: socketdir: %s\n", sockdir); |
f6049fdd | 822 | fprintf(stderr, "noip: autoports: %u-%u\n", |
823 | minautoport, maxautoport); | |
e4976bb0 | 824 | fprintf(stderr, "noip: realbind acl:\n"); |
825 | dump_acl(bind_real); | |
826 | fprintf(stderr, "noip: realconnect acl:\n"); | |
827 | dump_acl(connect_real); ) | |
828 | } | |
829 | ||
1d1ccf4f | 830 | /*----- Overridden system calls -------------------------------------------*/ |
e4976bb0 | 831 | |
832 | int socket(int pf, int ty, int proto) | |
833 | { | |
834 | if (pf == PF_INET) { | |
835 | pf = PF_UNIX; | |
836 | proto = 0; | |
837 | } | |
838 | return real_socket(pf, ty, proto); | |
839 | } | |
840 | ||
841 | int socketpair(int pf, int ty, int proto, int *sk) | |
842 | { | |
843 | if (pf == PF_INET) { | |
844 | pf = PF_UNIX; | |
845 | proto = 0; | |
846 | } | |
847 | return (real_socketpair(pf, ty, proto, sk)); | |
848 | } | |
849 | ||
850 | int bind(int sk, const struct sockaddr *sa, socklen_t len) | |
851 | { | |
852 | struct sockaddr_un sun; | |
853 | ||
9f82ba1f | 854 | if (sa->sa_family == AF_INET) { |
e4976bb0 | 855 | PRESERVING_ERRNO({ |
856 | if (acl_allows_p(bind_real, SIN(sa))) { | |
857 | if (fixup_real_ip_socket(sk)) | |
858 | return (-1); | |
859 | } else { | |
860 | encode_inet_addr(&sun, SIN(sa), WANT_FRESH); | |
861 | sa = SA(&sun); | |
862 | len = SUN_LEN(&sun); | |
863 | } | |
864 | }); | |
865 | } | |
866 | return real_bind(sk, sa, len); | |
867 | } | |
868 | ||
869 | int connect(int sk, const struct sockaddr *sa, socklen_t len) | |
870 | { | |
871 | struct sockaddr_un sun; | |
53390eff MW |
872 | int fixup_p = 0; |
873 | int rc; | |
e4976bb0 | 874 | |
6df6f816 MW |
875 | switch (sa->sa_family) { |
876 | case AF_INET: | |
877 | PRESERVING_ERRNO({ | |
878 | do_implicit_bind(sk, &sa, &len, &sun); | |
879 | fixup_p = 1; | |
880 | }); | |
881 | rc = real_connect(sk, sa, len); | |
882 | if (rc < 0) { | |
883 | switch (errno) { | |
884 | case ENOENT: errno = ECONNREFUSED; break; | |
885 | } | |
886 | } | |
887 | break; | |
888 | default: | |
889 | rc = real_connect(sk, sa, len); | |
890 | break; | |
53390eff MW |
891 | } |
892 | return rc; | |
e4976bb0 | 893 | } |
894 | ||
895 | ssize_t sendto(int sk, const void *buf, size_t len, int flags, | |
896 | const struct sockaddr *to, socklen_t tolen) | |
897 | { | |
898 | struct sockaddr_un sun; | |
899 | ||
900 | if (to && to->sa_family == AF_INET) { | |
901 | PRESERVING_ERRNO({ | |
902 | do_implicit_bind(sk, &to, &tolen, &sun); | |
903 | }); | |
904 | } | |
905 | return real_sendto(sk, buf, len, flags, to, tolen); | |
906 | } | |
907 | ||
908 | ssize_t recvfrom(int sk, void *buf, size_t len, int flags, | |
909 | struct sockaddr *from, socklen_t *fromlen) | |
910 | { | |
911 | char sabuf[1024]; | |
912 | socklen_t mylen = sizeof(sabuf); | |
913 | ssize_t n; | |
914 | ||
915 | if (!from) | |
916 | return real_recvfrom(sk, buf, len, flags, 0, 0); | |
917 | PRESERVING_ERRNO({ | |
918 | n = real_recvfrom(sk, buf, len, flags, SA(sabuf), &mylen); | |
919 | if (n < 0) | |
920 | return (-1); | |
921 | return_fake_name(SA(sabuf), mylen, from, fromlen); | |
922 | }); | |
923 | return (n); | |
924 | } | |
925 | ||
926 | ssize_t sendmsg(int sk, const struct msghdr *msg, int flags) | |
927 | { | |
928 | struct sockaddr_un sun; | |
929 | const struct sockaddr *sa; | |
930 | struct msghdr mymsg; | |
931 | ||
932 | if (msg->msg_name && SA(msg->msg_name)->sa_family == AF_INET) { | |
933 | PRESERVING_ERRNO({ | |
934 | sa = SA(msg->msg_name); | |
935 | mymsg = *msg; | |
936 | do_implicit_bind(sk, &sa, &mymsg.msg_namelen, &sun); | |
937 | mymsg.msg_name = SA(sa); | |
938 | msg = &mymsg; | |
939 | }); | |
940 | } | |
941 | return real_sendmsg(sk, msg, flags); | |
942 | } | |
943 | ||
944 | ssize_t recvmsg(int sk, struct msghdr *msg, int flags) | |
945 | { | |
946 | char sabuf[1024]; | |
947 | struct sockaddr *sa; | |
948 | socklen_t len; | |
949 | ssize_t n; | |
950 | ||
951 | if (!msg->msg_name) | |
952 | return real_recvmsg(sk, msg, flags); | |
953 | PRESERVING_ERRNO({ | |
954 | sa = SA(msg->msg_name); | |
955 | len = msg->msg_namelen; | |
956 | msg->msg_name = sabuf; | |
957 | msg->msg_namelen = sizeof(sabuf); | |
958 | n = real_recvmsg(sk, msg, flags); | |
959 | if (n < 0) | |
960 | return (-1); | |
961 | return_fake_name(SA(sabuf), msg->msg_namelen, sa, &len); | |
962 | msg->msg_name = sa; | |
963 | msg->msg_namelen = len; | |
964 | }); | |
965 | return (n); | |
966 | } | |
967 | ||
968 | int accept(int sk, struct sockaddr *sa, socklen_t *len) | |
969 | { | |
970 | char sabuf[1024]; | |
971 | socklen_t mylen = sizeof(sabuf); | |
972 | int nsk = real_accept(sk, SA(sabuf), &mylen); | |
973 | ||
974 | if (nsk < 0) | |
975 | return (-1); | |
976 | return_fake_name(SA(sabuf), mylen, sa, len); | |
977 | return (nsk); | |
978 | } | |
979 | ||
980 | int getsockname(int sk, struct sockaddr *sa, socklen_t *len) | |
981 | { | |
982 | PRESERVING_ERRNO({ | |
983 | char sabuf[1024]; | |
984 | socklen_t mylen = sizeof(sabuf); | |
985 | if (real_getsockname(sk, SA(sabuf), &mylen)) | |
986 | return (-1); | |
987 | return_fake_name(SA(sabuf), mylen, sa, len); | |
988 | }); | |
989 | return (0); | |
990 | } | |
991 | ||
992 | int getpeername(int sk, struct sockaddr *sa, socklen_t *len) | |
993 | { | |
994 | PRESERVING_ERRNO({ | |
995 | char sabuf[1024]; | |
996 | socklen_t mylen = sizeof(sabuf); | |
997 | if (real_getpeername(sk, SA(sabuf), &mylen)) | |
998 | return (-1); | |
999 | return_fake_name(SA(sabuf), mylen, sa, len); | |
1000 | }); | |
1001 | return (0); | |
1002 | } | |
1003 | ||
1004 | int getsockopt(int sk, int lev, int opt, void *p, socklen_t *len) | |
1005 | { | |
1006 | switch (lev) { | |
1007 | case SOL_IP: | |
1008 | case SOL_TCP: | |
1009 | case SOL_UDP: | |
1010 | if (*len > 0) | |
1011 | memset(p, 0, *len); | |
1012 | return (0); | |
1013 | } | |
1014 | return real_getsockopt(sk, lev, opt, p, len); | |
1015 | } | |
1016 | ||
1017 | int setsockopt(int sk, int lev, int opt, const void *p, socklen_t len) | |
1018 | { | |
1019 | switch (lev) { | |
1020 | case SOL_IP: | |
1021 | case SOL_TCP: | |
1022 | case SOL_UDP: | |
1023 | return (0); | |
1024 | } | |
1025 | switch (opt) { | |
1026 | case SO_BINDTODEVICE: | |
1027 | case SO_ATTACH_FILTER: | |
1028 | case SO_DETACH_FILTER: | |
1029 | return (0); | |
1030 | } | |
1031 | return real_setsockopt(sk, lev, opt, p, len); | |
1032 | } | |
1033 | ||
1d1ccf4f | 1034 | /*----- Initialization ----------------------------------------------------*/ |
e4976bb0 | 1035 | |
1d1ccf4f | 1036 | /* Clean up the socket directory, deleting stale sockets. */ |
e4976bb0 | 1037 | static void cleanup_sockdir(void) |
1038 | { | |
1039 | DIR *dir; | |
1040 | struct dirent *d; | |
3ef1fec9 | 1041 | struct sockaddr_in sin; |
e4976bb0 | 1042 | struct sockaddr_un sun; |
3ef1fec9 | 1043 | struct stat st; |
e4976bb0 | 1044 | |
1045 | if ((dir = opendir(sockdir)) == 0) | |
1046 | return; | |
4ab301de | 1047 | sun.sun_family = AF_UNIX; |
e4976bb0 | 1048 | while ((d = readdir(dir)) != 0) { |
1049 | if (d->d_name[0] == '.') continue; | |
1050 | snprintf(sun.sun_path, sizeof(sun.sun_path), | |
1051 | "%s/%s", sockdir, d->d_name); | |
3ef1fec9 | 1052 | if (decode_inet_addr(&sin, &sun, SUN_LEN(&sun)) || |
1053 | stat(sun.sun_path, &st) || | |
4ab301de | 1054 | !S_ISSOCK(st.st_mode)) { |
1055 | D( fprintf(stderr, "noip: ignoring unknown socketdir entry `%s'\n", | |
1056 | sun.sun_path); ) | |
3ef1fec9 | 1057 | continue; |
4ab301de | 1058 | } |
e4976bb0 | 1059 | if (unix_socket_status(&sun, 0) == STALE) { |
1060 | D( fprintf(stderr, "noip: clearing away stale socket %s\n", | |
1061 | d->d_name); ) | |
1062 | unlink(sun.sun_path); | |
1063 | } | |
1064 | } | |
1065 | closedir(dir); | |
1066 | } | |
1067 | ||
1d1ccf4f MW |
1068 | /* Find the addresses attached to local network interfaces, and remember them |
1069 | * in a table. | |
1070 | */ | |
e4976bb0 | 1071 | static void get_local_ipaddrs(void) |
1072 | { | |
1073 | struct if_nameindex *ifn; | |
1074 | struct ifreq ifr; | |
1075 | int sk; | |
1076 | int i; | |
1077 | ||
1078 | ifn = if_nameindex(); | |
1079 | if ((sk = real_socket(PF_INET, SOCK_STREAM, 00)) < 0) | |
1080 | return; | |
1081 | for (i = n_local_ipaddrs = 0; | |
1082 | n_local_ipaddrs < MAX_LOCAL_IPADDRS && | |
1083 | ifn[i].if_name && *ifn[i].if_name; | |
1084 | i++) { | |
1085 | strcpy(ifr.ifr_name, ifn[i].if_name); | |
1086 | if (ioctl(sk, SIOCGIFADDR, &ifr) || ifr.ifr_addr.sa_family != AF_INET) | |
1087 | continue; | |
1088 | local_ipaddrs[n_local_ipaddrs++] = | |
1089 | SIN(&ifr.ifr_addr)->sin_addr; | |
1090 | D( fprintf(stderr, "noip: local addr %s = %s\n", ifn[i].if_name, | |
1091 | inet_ntoa(local_ipaddrs[n_local_ipaddrs - 1])); ) | |
1092 | } | |
1093 | close(sk); | |
1094 | } | |
1095 | ||
1d1ccf4f | 1096 | /* Print the given message to standard error. Avoids stdio. */ |
3ef1fec9 | 1097 | static void printerr(const char *p) { write(STDERR_FILENO, p, strlen(p)); } |
1098 | ||
1d1ccf4f | 1099 | /* Create the socket directory, being careful about permissions. */ |
3ef1fec9 | 1100 | static void create_sockdir(void) |
1101 | { | |
1102 | struct stat st; | |
1103 | ||
1104 | if (stat(sockdir, &st)) { | |
1105 | if (errno == ENOENT) { | |
1106 | if (mkdir(sockdir, 0700)) { | |
1107 | perror("noip: creating socketdir"); | |
1108 | exit(127); | |
1109 | } | |
1110 | if (!stat(sockdir, &st)) | |
1111 | goto check; | |
1112 | } | |
1113 | perror("noip: checking socketdir"); | |
1114 | exit(127); | |
1115 | } | |
1116 | check: | |
1117 | if (!S_ISDIR(st.st_mode)) { | |
1118 | printerr("noip: bad socketdir: not a directory\n"); | |
1119 | exit(127); | |
1120 | } | |
1121 | if (st.st_uid != uid) { | |
1122 | printerr("noip: bad socketdir: not owner\n"); | |
1123 | exit(127); | |
1124 | } | |
1125 | if (st.st_mode & 077) { | |
1126 | printerr("noip: bad socketdir: not private\n"); | |
1127 | exit(127); | |
1128 | } | |
1129 | } | |
1130 | ||
1d1ccf4f MW |
1131 | /* Initialization function. */ |
1132 | static void setup(void) __attribute__((constructor)); | |
e4976bb0 | 1133 | static void setup(void) |
1134 | { | |
1135 | PRESERVING_ERRNO({ | |
1136 | char *p; | |
1137 | ||
1138 | import(); | |
3ef1fec9 | 1139 | uid = geteuid(); |
e4976bb0 | 1140 | if ((p = getenv("NOIP_DEBUG")) && atoi(p)) |
1141 | debug = 1; | |
1142 | get_local_ipaddrs(); | |
1143 | readconfig(); | |
3ef1fec9 | 1144 | create_sockdir(); |
e4976bb0 | 1145 | cleanup_sockdir(); |
1146 | }); | |
1147 | } | |
1d1ccf4f MW |
1148 | |
1149 | /*----- That's all, folks -------------------------------------------------*/ |