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 | * | |
de6df72d MW |
17 | * Preload-hacks are distributed in the hope that it will be useful, but |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
19 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
1d1ccf4f MW |
21 | * |
22 | * You should have received a copy of the GNU General Public License along | |
de6df72d MW |
23 | * with preload-hacks; if not, write to the Free Software Foundation, Inc., |
24 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
1d1ccf4f MW |
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> | |
9f1396d9 | 54 | #include <ifaddrs.h> |
e4976bb0 | 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, \ | |
7d769c69 | 101 | struct sockaddr *from, socklen_t *fromlen)) \ |
e4976bb0 | 102 | _(sendmsg, ssize_t, (int, const struct msghdr *, int)) \ |
9fb8e5c9 | 103 | _(recvmsg, ssize_t, (int, struct msghdr *, int)) |
e4976bb0 | 104 | |
1d1ccf4f | 105 | /* Function pointers to set up. */ |
e4976bb0 | 106 | #define DECL(imp, ret, args) static ret (*real_##imp) args; |
107 | IMPORTS(DECL) | |
108 | #undef DECL | |
109 | ||
1d1ccf4f | 110 | /* Import the system calls. */ |
e4976bb0 | 111 | static void import(void) |
112 | { | |
113 | #define IMPORT(imp, ret, args) \ | |
114 | real_##imp = (ret (*)args)dlsym(RTLD_NEXT, #imp); | |
115 | IMPORTS(IMPORT) | |
116 | #undef IMPORT | |
117 | } | |
118 | ||
1d1ccf4f | 119 | /*----- Utilities ---------------------------------------------------------*/ |
e4976bb0 | 120 | |
1d1ccf4f | 121 | /* Socket address casts */ |
e4976bb0 | 122 | #define SA(sa) ((struct sockaddr *)(sa)) |
123 | #define SIN(sa) ((struct sockaddr_in *)(sa)) | |
124 | #define SUN(sa) ((struct sockaddr_un *)(sa)) | |
125 | ||
1d1ccf4f | 126 | /* Raw bytes */ |
e4976bb0 | 127 | #define UC(ch) ((unsigned char)(ch)) |
128 | ||
1d1ccf4f | 129 | /* Memory allocation */ |
e4976bb0 | 130 | #define NEW(x) ((x) = xmalloc(sizeof(*x))) |
131 | #define NEWV(x, n) ((x) = xmalloc(sizeof(*x) * (n))) | |
132 | ||
1d1ccf4f | 133 | /* Debugging */ |
e4976bb0 | 134 | #ifdef DEBUG |
135 | # define D(body) { if (debug) { body } } | |
136 | #else | |
137 | # define D(body) ; | |
138 | #endif | |
139 | ||
1d1ccf4f | 140 | /* Preservation of error status */ |
e4976bb0 | 141 | #define PRESERVING_ERRNO(body) do { \ |
142 | int _err = errno; { body } errno = _err; \ | |
143 | } while (0) | |
144 | ||
1d1ccf4f | 145 | /* Allocate N bytes of memory; abort on failure. */ |
e4976bb0 | 146 | static void *xmalloc(size_t n) |
147 | { | |
148 | void *p; | |
149 | if (!n) return (0); | |
3ef1fec9 | 150 | if ((p = malloc(n)) == 0) { perror("malloc"); exit(127); } |
e4976bb0 | 151 | return (p); |
152 | } | |
153 | ||
1d1ccf4f | 154 | /* Allocate a copy of the null-terminated string P; abort on failure. */ |
e4976bb0 | 155 | static char *xstrdup(const char *p) |
156 | { | |
157 | size_t n = strlen(p) + 1; | |
158 | char *q = xmalloc(n); | |
159 | memcpy(q, p, n); | |
160 | return (q); | |
161 | } | |
1d1ccf4f | 162 | /*----- Access control lists ----------------------------------------------*/ |
e4976bb0 | 163 | |
164 | #ifdef DEBUG | |
165 | ||
1d1ccf4f | 166 | /* Write to standard error a description of the ACL node A. */ |
e4976bb0 | 167 | static void dump_aclnode(aclnode *a) |
168 | { | |
169 | char minbuf[16], maxbuf[16]; | |
170 | struct in_addr amin, amax; | |
171 | ||
172 | amin.s_addr = htonl(a->minaddr); | |
173 | amax.s_addr = htonl(a->maxaddr); | |
174 | fprintf(stderr, "noip: %c ", a->act ? '+' : '-'); | |
175 | if (a->minaddr == 0 && a->maxaddr == 0xffffffff) | |
176 | fprintf(stderr, "any"); | |
177 | else { | |
178 | fprintf(stderr, "%s", | |
179 | inet_ntop(AF_INET, &amin, minbuf, sizeof(minbuf))); | |
180 | if (a->maxaddr != a->minaddr) { | |
181 | fprintf(stderr, "-%s", | |
182 | inet_ntop(AF_INET, &amax, maxbuf, sizeof(maxbuf))); | |
183 | } | |
184 | } | |
185 | if (a->minport != 0 || a->maxport != 0xffff) { | |
186 | fprintf(stderr, ":%u", (unsigned)a->minport); | |
187 | if (a->minport != a->maxport) | |
188 | fprintf(stderr, "-%u", (unsigned)a->maxport); | |
189 | } | |
190 | fputc('\n', stderr); | |
191 | } | |
192 | ||
1d1ccf4f MW |
193 | static void dump_acl(aclnode *a) |
194 | { | |
195 | int act = ALLOW; | |
196 | ||
197 | for (; a; a = a->next) { | |
198 | dump_aclnode(a); | |
199 | act = a->act; | |
200 | } | |
201 | fprintf(stderr, "noip: [default policy: %s]\n", | |
202 | act == ALLOW ? "DENY" : "ALLOW"); | |
203 | } | |
204 | ||
e4976bb0 | 205 | #endif |
206 | ||
1d1ccf4f | 207 | /* Returns nonzero if the ACL A allows the IP socket SIN. */ |
e4976bb0 | 208 | static int acl_allows_p(aclnode *a, const struct sockaddr_in *sin) |
209 | { | |
210 | unsigned long addr = ntohl(sin->sin_addr.s_addr); | |
211 | unsigned short port = ntohs(sin->sin_port); | |
212 | int act = ALLOW; | |
213 | ||
214 | D( char buf[16]; | |
215 | fprintf(stderr, "noip: check %s:%u\n", | |
216 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
217 | ntohs((unsigned)sin->sin_port)); ) | |
218 | for (; a; a = a->next) { | |
219 | D( dump_aclnode(a); ) | |
220 | if (a->minaddr <= addr && addr <= a->maxaddr && | |
221 | a->minport <= port && port <= a->maxport) { | |
222 | D( fprintf(stderr, "noip: aha! %s\n", a->act ? "ALLOW" : "DENY"); ) | |
223 | return (a->act); | |
224 | } | |
225 | act = a->act; | |
226 | } | |
227 | D( fprintf(stderr, "noip: nothing found: %s\n", act ? "DENY" : "ALLOW"); ) | |
228 | return (!act); | |
229 | } | |
230 | ||
1d1ccf4f | 231 | /*----- Socket address conversion -----------------------------------------*/ |
e4976bb0 | 232 | |
1d1ccf4f | 233 | /* Return a uniformly distributed integer between MIN and MAX inclusive. */ |
f6049fdd | 234 | static unsigned randrange(unsigned min, unsigned max) |
235 | { | |
236 | unsigned mask, i; | |
237 | ||
238 | /* It's so nice not to have to care about the quality of the generator | |
239 | much! */ | |
240 | max -= min; | |
241 | for (mask = 1; mask < max; mask = (mask << 1) | 1) | |
242 | ; | |
243 | do i = rand() & mask; while (i > max); | |
244 | return (i + min); | |
245 | } | |
246 | ||
1d1ccf4f MW |
247 | /* Return the status of Unix-domain socket address SUN. Returns: UNUSED if |
248 | * the socket doesn't exist; USED if the path refers to an active socket, or | |
249 | * isn't really a socket at all, or we can't tell without a careful search | |
250 | * and QUICKP is set; or STALE if the file refers to a socket which isn't | |
251 | * being used any more. | |
252 | */ | |
253 | static int unix_socket_status(struct sockaddr_un *sun, int quickp) | |
254 | { | |
255 | struct stat st; | |
256 | FILE *fp = 0; | |
257 | size_t len, n; | |
258 | int rc; | |
259 | char buf[256]; | |
260 | ||
261 | if (stat(sun->sun_path, &st)) | |
262 | return (errno == ENOENT ? UNUSED : USED); | |
263 | if (!S_ISSOCK(st.st_mode) || quickp) | |
264 | return (USED); | |
265 | rc = USED; | |
266 | if ((fp = fopen("/proc/net/unix", "r")) == 0) | |
267 | goto done; | |
72d85cdb | 268 | if (!fgets(buf, sizeof(buf), fp)) goto done; /* skip header */ |
1d1ccf4f MW |
269 | len = strlen(sun->sun_path); |
270 | while (fgets(buf, sizeof(buf), fp)) { | |
271 | n = strlen(buf); | |
272 | if (n >= len + 2 && buf[n - len - 2] == ' ' && buf[n - 1] == '\n' && | |
273 | memcmp(buf + n - len - 1, sun->sun_path, len) == 0) | |
274 | goto done; | |
275 | } | |
276 | if (ferror(fp)) | |
277 | goto done; | |
278 | rc = STALE; | |
279 | done: | |
280 | if (fp) fclose(fp); | |
281 | return (rc); | |
282 | } | |
283 | ||
284 | /* Encode the Internet address SIN as a Unix-domain address SUN. If WANT is | |
285 | * WANT_FRESH, and SIN->sin_port is zero, then we pick an arbitrary local | |
286 | * port. Otherwise we pick the port given. There's an unpleasant hack to | |
287 | * find servers bound to INADDR_ANY. Returns zero on success; -1 on failure. | |
288 | */ | |
e4976bb0 | 289 | static int encode_inet_addr(struct sockaddr_un *sun, |
290 | const struct sockaddr_in *sin, | |
291 | int want) | |
292 | { | |
293 | int i; | |
3ef1fec9 | 294 | int desperatep = 0; |
e4976bb0 | 295 | char buf[INET_ADDRSTRLEN]; |
296 | int rc; | |
297 | ||
298 | D( fprintf(stderr, "noip: encode %s:%u (%s)", | |
299 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
300 | (unsigned)ntohs(sin->sin_port), | |
301 | want == WANT_EXISTING ? "EXISTING" : "FRESH"); ) | |
302 | sun->sun_family = AF_UNIX; | |
303 | if (sin->sin_port || want == WANT_EXISTING) { | |
304 | snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir, | |
305 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
306 | (unsigned)ntohs(sin->sin_port)); | |
307 | rc = unix_socket_status(sun, 0); | |
308 | if (rc == STALE) unlink(sun->sun_path); | |
309 | if (rc != USED && want == WANT_EXISTING) { | |
310 | snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/0.0.0.0:%u", | |
311 | sockdir, (unsigned)ntohs(sin->sin_port)); | |
312 | if (unix_socket_status(sun, 0) == STALE) unlink(sun->sun_path); | |
313 | } | |
314 | } else { | |
f6049fdd | 315 | for (i = 0; i < 10; i++) { |
316 | snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir, | |
317 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
318 | randrange(minautoport, maxautoport)); | |
319 | if (unix_socket_status(sun, 1) == UNUSED) goto found; | |
320 | } | |
3ef1fec9 | 321 | for (desperatep = 0; desperatep < 2; desperatep++) { |
f6049fdd | 322 | for (i = minautoport; i <= maxautoport; i++) { |
e4976bb0 | 323 | snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir, |
324 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
325 | (unsigned)i); | |
3ef1fec9 | 326 | rc = unix_socket_status(sun, !desperatep); |
e4976bb0 | 327 | switch (rc) { |
328 | case STALE: unlink(sun->sun_path); | |
329 | case UNUSED: goto found; | |
330 | } | |
331 | } | |
332 | } | |
333 | errno = EADDRINUSE; | |
334 | D( fprintf(stderr, " -- can't resolve\n"); ) | |
335 | return (-1); | |
336 | found:; | |
337 | } | |
338 | D( fprintf(stderr, " -> `%s'\n", sun->sun_path); ) | |
339 | return (0); | |
340 | } | |
341 | ||
00a98a8a MW |
342 | /* Decode the Unix address SUN to an Internet address SIN. If |
343 | * DECODE_UNBOUND_P is nonzero, an empty address (indicative of an unbound | |
344 | * Unix-domain socket) is translated to a wildcard Internet address. Returns | |
345 | * zero on success; -1 on failure (e.g., it wasn't one of our addresses). | |
346 | */ | |
e4976bb0 | 347 | static int decode_inet_addr(struct sockaddr_in *sin, |
348 | const struct sockaddr_un *sun, | |
00a98a8a MW |
349 | socklen_t len, |
350 | int decode_unbound_p) | |
e4976bb0 | 351 | { |
352 | char buf[INET_ADDRSTRLEN + 16]; | |
353 | char *p; | |
a2114371 | 354 | size_t n = strlen(sockdir), nn; |
e4976bb0 | 355 | struct sockaddr_in sin_mine; |
356 | unsigned long port; | |
357 | ||
358 | if (!sin) | |
359 | sin = &sin_mine; | |
360 | if (sun->sun_family != AF_UNIX) | |
361 | return (-1); | |
a2114371 | 362 | nn = strlen(sun->sun_path); |
e4976bb0 | 363 | if (len < sizeof(sun)) ((char *)sun)[len] = 0; |
364 | D( fprintf(stderr, "noip: decode (%d) `%s'", | |
365 | *sun->sun_path, sun->sun_path); ) | |
00a98a8a | 366 | if (decode_unbound_p && !sun->sun_path[0]) { |
e4976bb0 | 367 | sin->sin_family = AF_INET; |
368 | sin->sin_addr.s_addr = INADDR_ANY; | |
369 | sin->sin_port = 0; | |
370 | D( fprintf(stderr, " -- unbound socket\n"); ) | |
371 | return (0); | |
372 | } | |
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"); ) | |
376 | return (-1); | |
377 | } | |
378 | memcpy(buf, sun->sun_path + n + 1, nn - n); | |
379 | if ((p = strchr(buf, ':')) == 0) { | |
380 | D( fprintf(stderr, " -- malformed (no port)\n"); ) | |
381 | return (-1); | |
382 | } | |
383 | *p++ = 0; | |
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); ) | |
387 | return (-1); | |
388 | } | |
389 | port = strtoul(p, &p, 10); | |
390 | if (*p || port >= 65536) { | |
391 | D( fprintf(stderr, " -- malformed (port out of range)"); ) | |
392 | return (-1); | |
393 | } | |
394 | sin->sin_port = htons(port); | |
395 | D( fprintf(stderr, " -> %s:%u\n", | |
396 | inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)), | |
397 | (unsigned)port); ) | |
398 | return (0); | |
399 | } | |
400 | ||
1d1ccf4f MW |
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. | |
404 | */ | |
e4976bb0 | 405 | static int fixup_real_ip_socket(int sk) |
406 | { | |
407 | int nsk; | |
408 | int type; | |
409 | int f, fd; | |
410 | struct sockaddr_un sun; | |
411 | struct sockaddr_in sin; | |
412 | socklen_t len; | |
413 | ||
414 | #define OPTS(_) \ | |
415 | _(DEBUG, int) \ | |
416 | _(REUSEADDR, int) \ | |
417 | _(DONTROUTE, int) \ | |
418 | _(BROADCAST, int) \ | |
419 | _(SNDBUF, int) \ | |
420 | _(RCVBUF, int) \ | |
421 | _(OOBINLINE, int) \ | |
422 | _(NO_CHECK, int) \ | |
423 | _(LINGER, struct linger) \ | |
424 | _(BSDCOMPAT, int) \ | |
425 | _(RCVLOWAT, int) \ | |
426 | _(RCVTIMEO, struct timeval) \ | |
427 | _(SNDTIMEO, struct timeval) | |
428 | ||
429 | len = sizeof(sun); | |
430 | if (real_getsockname(sk, SA(&sun), &len)) | |
431 | return (-1); | |
00a98a8a | 432 | if (decode_inet_addr(&sin, &sun, len, 1)) |
e4976bb0 | 433 | return (0); /* Not one of ours */ |
434 | len = sizeof(type); | |
435 | if (real_getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0 || | |
436 | (nsk = real_socket(PF_INET, type, 0)) < 0) | |
437 | return (-1); | |
438 | #define FIX(opt, ty) do { \ | |
439 | ty ov_; \ | |
440 | len = sizeof(ov_); \ | |
441 | if (real_getsockopt(sk, SOL_SOCKET, SO_##opt, &ov_, &len) < 0 || \ | |
442 | real_setsockopt(nsk, SOL_SOCKET, SO_##opt, &ov_, len)) { \ | |
9fb8e5c9 | 443 | close(nsk); \ |
e4976bb0 | 444 | return (-1); \ |
445 | } \ | |
446 | } while (0); | |
447 | OPTS(FIX) | |
448 | #undef FIX | |
449 | if ((f = fcntl(sk, F_GETFL)) < 0 || | |
450 | (fd = fcntl(sk, F_GETFD)) < 0 || | |
451 | fcntl(nsk, F_SETFL, f) < 0 || | |
452 | dup2(nsk, sk) < 0) { | |
9fb8e5c9 | 453 | close(nsk); |
e4976bb0 | 454 | return (-1); |
455 | } | |
456 | unlink(sun.sun_path); | |
9fb8e5c9 | 457 | close(nsk); |
e4976bb0 | 458 | if (fcntl(sk, F_SETFD, fd) < 0) { |
459 | perror("noip: fixup_real_ip_socket F_SETFD"); | |
460 | abort(); | |
461 | } | |
462 | return (0); | |
463 | } | |
464 | ||
1d1ccf4f MW |
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. | |
467 | */ | |
e4976bb0 | 468 | static int do_implicit_bind(int sk, const struct sockaddr **sa, |
469 | socklen_t *len, struct sockaddr_un *sun) | |
470 | { | |
471 | struct sockaddr_in sin; | |
472 | socklen_t mylen = sizeof(*sun); | |
473 | ||
474 | if (acl_allows_p(connect_real, SIN(*sa))) { | |
475 | if (fixup_real_ip_socket(sk)) | |
476 | return (-1); | |
477 | } else { | |
478 | if (real_getsockname(sk, SA(sun), &mylen) < 0) | |
479 | return (-1); | |
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; | |
4ab301de | 484 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
e4976bb0 | 485 | sin.sin_port = 0; |
486 | encode_inet_addr(sun, &sin, WANT_FRESH); | |
487 | if (real_bind(sk, SA(sun), SUN_LEN(sun))) | |
488 | return (-1); | |
489 | } | |
490 | encode_inet_addr(sun, SIN(*sa), WANT_EXISTING); | |
491 | *sa = SA(sun); | |
492 | *len = SUN_LEN(sun); | |
493 | } | |
494 | } | |
495 | return (0); | |
496 | } | |
497 | ||
1d1ccf4f MW |
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 | |
501 | * at FAKELEN. | |
502 | */ | |
e4976bb0 | 503 | static void return_fake_name(struct sockaddr *sa, socklen_t len, |
504 | struct sockaddr *fake, socklen_t *fakelen) | |
505 | { | |
506 | struct sockaddr_in sin; | |
507 | socklen_t alen; | |
508 | ||
00a98a8a MW |
509 | if (sa->sa_family == AF_UNIX && |
510 | !decode_inet_addr(&sin, SUN(sa), len, 0)) { | |
e4976bb0 | 511 | sa = SA(&sin); |
512 | len = sizeof(sin); | |
513 | } | |
514 | alen = len; | |
515 | if (len > *fakelen) | |
516 | len = *fakelen; | |
517 | if (len > 0) | |
518 | memcpy(fake, sa, len); | |
519 | *fakelen = alen; | |
520 | } | |
521 | ||
1d1ccf4f | 522 | /*----- Configuration -----------------------------------------------------*/ |
e4976bb0 | 523 | |
1d1ccf4f | 524 | /* Return the process owner's home directory. */ |
e4976bb0 | 525 | static char *home(void) |
526 | { | |
527 | char *p; | |
528 | struct passwd *pw; | |
529 | ||
3ef1fec9 | 530 | if (getuid() == uid && |
531 | (p = getenv("HOME")) != 0) | |
532 | return (p); | |
533 | else if ((pw = getpwuid(uid)) != 0) | |
534 | return (pw->pw_dir); | |
535 | else | |
536 | return "/notexist"; | |
e4976bb0 | 537 | } |
538 | ||
1d1ccf4f | 539 | /* Return a good temporary directory to use. */ |
e4976bb0 | 540 | static char *tmpdir(void) |
541 | { | |
542 | char *p; | |
543 | ||
544 | if ((p = getenv("TMPDIR")) != 0) return (p); | |
545 | else if ((p = getenv("TMP")) != 0) return (p); | |
546 | else return ("/tmp"); | |
547 | } | |
548 | ||
1d1ccf4f | 549 | /* Return the user's name, or at least something distinctive. */ |
e4976bb0 | 550 | static char *user(void) |
551 | { | |
552 | static char buf[16]; | |
553 | char *p; | |
554 | struct passwd *pw; | |
555 | ||
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); | |
559 | else { | |
560 | snprintf(buf, sizeof(buf), "uid-%lu", (unsigned long)uid); | |
561 | return (buf); | |
562 | } | |
563 | } | |
564 | ||
1d1ccf4f | 565 | /* Skip P over space characters. */ |
e4976bb0 | 566 | #define SKIPSPC do { while (*p && isspace(UC(*p))) p++; } while (0) |
1d1ccf4f MW |
567 | |
568 | /* Set Q to point to the next word following P, null-terminate it, and step P | |
569 | * past it. */ | |
e4976bb0 | 570 | #define NEXTWORD(q) do { \ |
571 | SKIPSPC; \ | |
572 | q = p; \ | |
573 | while (*p && !isspace(UC(*p))) p++; \ | |
574 | if (*p) *p++ = 0; \ | |
575 | } while (0) | |
1d1ccf4f MW |
576 | |
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. */ | |
e4976bb0 | 579 | #define NEXTADDR(q, del) do { \ |
580 | SKIPSPC; \ | |
581 | q = p; \ | |
582 | while (*p && (*p == '.' || isdigit(UC(*p)))) p++; \ | |
583 | del = *p; \ | |
584 | if (*p) *p++ = 0; \ | |
585 | } while (0) | |
1d1ccf4f MW |
586 | |
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. */ | |
e4976bb0 | 589 | #define NEXTNUMBER(q, del) do { \ |
590 | SKIPSPC; \ | |
591 | q = p; \ | |
592 | while (*p && isdigit(UC(*p))) p++; \ | |
593 | del = *p; \ | |
594 | if (*p) *p++ = 0; \ | |
595 | } while (0) | |
1d1ccf4f MW |
596 | |
597 | /* Push the character DEL back so we scan it again, unless it's zero | |
598 | * (end-of-file). */ | |
e4976bb0 | 599 | #define RESCAN(del) do { if (del) *--p = del; } while (0) |
1d1ccf4f MW |
600 | |
601 | /* Evaluate true if P is pointing to the word KW (and not some longer string | |
602 | * of which KW is a prefix). */ | |
603 | ||
e4976bb0 | 604 | #define KWMATCHP(kw) (strncmp(p, kw, sizeof(kw) - 1) == 0 && \ |
605 | !isalnum(UC(p[sizeof(kw) - 1])) && \ | |
606 | (p += sizeof(kw) - 1)) | |
9f82ba1f | 607 | |
1d1ccf4f MW |
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. | |
612 | */ | |
e4976bb0 | 613 | static void parse_ports(char **pp, unsigned short *min, unsigned short *max) |
614 | { | |
615 | char *p = *pp, *q; | |
616 | int del; | |
617 | ||
618 | SKIPSPC; | |
f6049fdd | 619 | if (*p != ':') |
620 | { *min = 0; *max = 0xffff; } | |
621 | else { | |
e4976bb0 | 622 | p++; |
f6049fdd | 623 | NEXTNUMBER(q, del); *min = strtoul(q, 0, 0); RESCAN(del); |
e4976bb0 | 624 | SKIPSPC; |
f6049fdd | 625 | if (*p == '-') |
d83beb5c | 626 | { p++; NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); } |
f6049fdd | 627 | else |
e4976bb0 | 628 | *max = *min; |
629 | } | |
630 | *pp = p; | |
631 | } | |
632 | ||
1d1ccf4f MW |
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. | |
636 | */ | |
e4976bb0 | 637 | #define ACLNODE(tail_, act_, \ |
638 | minaddr_, maxaddr_, minport_, maxport_) do { \ | |
639 | aclnode *a_; \ | |
640 | NEW(a_); \ | |
641 | a_->act = act_; \ | |
642 | a_->minaddr = minaddr_; a_->maxaddr = maxaddr_; \ | |
643 | a_->minport = minport_; a_->maxport = maxport_; \ | |
644 | *tail_ = a_; tail_ = &a_->next; \ | |
645 | } while (0) | |
646 | ||
1d1ccf4f MW |
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.. | |
652 | */ | |
e4976bb0 | 653 | static void parse_acl_line(char **pp, aclnode ***tail) |
654 | { | |
655 | struct in_addr addr; | |
656 | unsigned long minaddr, maxaddr, mask; | |
657 | unsigned short minport, maxport; | |
658 | int i, n; | |
659 | int act; | |
660 | int del; | |
661 | char *p = *pp; | |
662 | char *q; | |
663 | ||
a6d9626b | 664 | for (;;) { |
665 | SKIPSPC; | |
666 | if (*p == '+') act = ALLOW; | |
667 | else if (*p == '-') act = DENY; | |
668 | else goto bad; | |
e4976bb0 | 669 | |
a6d9626b | 670 | p++; |
671 | SKIPSPC; | |
672 | if (KWMATCHP("any")) { | |
e4976bb0 | 673 | minaddr = 0; |
674 | maxaddr = 0xffffffff; | |
a6d9626b | 675 | goto justone; |
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); | |
683 | } | |
e4976bb0 | 684 | } else { |
a6d9626b | 685 | if (*p == ':') { |
686 | minaddr = 0; | |
687 | maxaddr = 0xffffffff; | |
688 | } else { | |
e4976bb0 | 689 | NEXTADDR(q, del); |
690 | if (inet_pton(AF_INET, q, &addr) <= 0) goto bad; | |
a6d9626b | 691 | minaddr = ntohl(addr.s_addr); |
e4976bb0 | 692 | RESCAN(del); |
a6d9626b | 693 | SKIPSPC; |
694 | if (*p == '-') { | |
695 | p++; | |
696 | NEXTADDR(q, del); | |
e4976bb0 | 697 | if (inet_pton(AF_INET, q, &addr) <= 0) goto bad; |
a6d9626b | 698 | RESCAN(del); |
699 | maxaddr = ntohl(addr.s_addr); | |
700 | } else if (*p == '/') { | |
701 | p++; | |
702 | NEXTADDR(q, del); | |
703 | if (strchr(q, '.')) { | |
704 | if (inet_pton(AF_INET, q, &addr) <= 0) goto bad; | |
705 | mask = ntohl(addr.s_addr); | |
706 | } else { | |
f6049fdd | 707 | n = strtoul(q, 0, 0); |
a6d9626b | 708 | mask = (~0ul << (32 - n)) & 0xffffffff; |
709 | } | |
710 | RESCAN(del); | |
711 | minaddr &= mask; | |
712 | maxaddr = minaddr | (mask ^ 0xffffffff); | |
713 | } else | |
714 | maxaddr = minaddr; | |
715 | } | |
716 | justone: | |
717 | parse_ports(&p, &minport, &maxport); | |
718 | ACLNODE(*tail, act, minaddr, maxaddr, minport, maxport); | |
e4976bb0 | 719 | } |
a6d9626b | 720 | SKIPSPC; |
721 | if (*p != ',') break; | |
722 | p++; | |
e4976bb0 | 723 | } |
724 | return; | |
725 | ||
726 | bad: | |
727 | D( fprintf(stderr, "noip: bad acl spec (ignored)\n"); ) | |
728 | return; | |
729 | } | |
730 | ||
1d1ccf4f | 731 | /* Parse the autoports configuration directive. Syntax is MIN - MAX. */ |
f6049fdd | 732 | static void parse_autoports(char **pp) |
733 | { | |
734 | char *p = *pp, *q; | |
735 | unsigned x, y; | |
736 | int del; | |
737 | ||
738 | SKIPSPC; | |
739 | NEXTNUMBER(q, del); x = strtoul(q, 0, 0); RESCAN(del); | |
740 | SKIPSPC; | |
741 | if (*p != '-') goto bad; p++; | |
742 | NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del); | |
743 | minautoport = x; maxautoport = y; | |
744 | return; | |
745 | ||
746 | bad: | |
747 | D( fprintf(stderr, "bad port range (ignored)\n"); ) | |
748 | return; | |
749 | } | |
750 | ||
1d1ccf4f MW |
751 | /* Parse an ACL from an environment variable VAR, attaching it to the list |
752 | * TAIL. */ | |
a6d9626b | 753 | static void parse_acl_env(const char *var, aclnode ***tail) |
754 | { | |
f6049fdd | 755 | char *p, *q; |
a6d9626b | 756 | |
757 | if ((p = getenv(var)) != 0) { | |
f6049fdd | 758 | p = q = xstrdup(p); |
759 | parse_acl_line(&q, tail); | |
a6d9626b | 760 | free(p); |
761 | } | |
762 | } | |
763 | ||
1d1ccf4f | 764 | /* Read the configuration from the config file and environment. */ |
e4976bb0 | 765 | static void readconfig(void) |
766 | { | |
767 | FILE *fp; | |
768 | char buf[1024]; | |
769 | size_t n; | |
f6049fdd | 770 | char *p, *q, *cmd; |
e4976bb0 | 771 | |
a6d9626b | 772 | parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail); |
773 | parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail); | |
f6049fdd | 774 | if ((p = getenv("NOIP_AUTOPORTS")) != 0) { |
775 | p = q = xstrdup(p); | |
776 | parse_autoports(&q); | |
777 | free(p); | |
778 | } | |
4ab301de | 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); ) | |
782 | ||
783 | if ((fp = fopen(p, "r")) == 0) { | |
784 | D( fprintf(stderr, "noip: couldn't read config: %s\n", | |
785 | strerror(errno)); ) | |
f6049fdd | 786 | goto done; |
4ab301de | 787 | } |
e4976bb0 | 788 | while (fgets(buf, sizeof(buf), fp)) { |
789 | n = strlen(buf); | |
790 | p = buf; | |
791 | ||
792 | SKIPSPC; | |
793 | if (!*p || *p == '#') continue; | |
794 | while (n && isspace(UC(buf[n - 1]))) n--; | |
795 | buf[n] = 0; | |
796 | NEXTWORD(cmd); | |
797 | SKIPSPC; | |
798 | ||
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); | |
f6049fdd | 805 | else if (strcmp(cmd, "autoports") == 0) |
806 | parse_autoports(&p); | |
e4976bb0 | 807 | else if (strcmp(cmd, "debug") == 0) |
808 | debug = *p ? atoi(p) : 1; | |
809 | else | |
810 | D( fprintf(stderr, "noip: bad config command %s\n", cmd); ) | |
811 | } | |
812 | fclose(fp); | |
813 | ||
814 | done: | |
a6d9626b | 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); | |
e4976bb0 | 819 | *bind_tail = 0; |
820 | *connect_tail = 0; | |
a6d9626b | 821 | if (!sockdir) sockdir = getenv("NOIP_SOCKETDIR"); |
e4976bb0 | 822 | if (!sockdir) { |
823 | snprintf(buf, sizeof(buf), "%s/noip-%s", tmpdir(), user()); | |
824 | sockdir = xstrdup(buf); | |
825 | } | |
3ef1fec9 | 826 | D( fprintf(stderr, "noip: socketdir: %s\n", sockdir); |
f6049fdd | 827 | fprintf(stderr, "noip: autoports: %u-%u\n", |
828 | minautoport, maxautoport); | |
e4976bb0 | 829 | fprintf(stderr, "noip: realbind acl:\n"); |
830 | dump_acl(bind_real); | |
831 | fprintf(stderr, "noip: realconnect acl:\n"); | |
832 | dump_acl(connect_real); ) | |
833 | } | |
834 | ||
1d1ccf4f | 835 | /*----- Overridden system calls -------------------------------------------*/ |
e4976bb0 | 836 | |
837 | int socket(int pf, int ty, int proto) | |
838 | { | |
8ce11853 MW |
839 | switch (pf) { |
840 | case PF_INET: | |
841 | pf = PF_UNIX; | |
842 | proto = 0; | |
843 | case PF_UNIX: | |
41c50995 MW |
844 | #ifdef PF_NETLINK |
845 | case PF_NETLINK: | |
846 | #endif | |
8ce11853 MW |
847 | return real_socket(pf, ty, proto); |
848 | default: | |
849 | errno = EAFNOSUPPORT; | |
850 | return -1; | |
e4976bb0 | 851 | } |
e4976bb0 | 852 | } |
853 | ||
854 | int socketpair(int pf, int ty, int proto, int *sk) | |
855 | { | |
856 | if (pf == PF_INET) { | |
857 | pf = PF_UNIX; | |
858 | proto = 0; | |
859 | } | |
860 | return (real_socketpair(pf, ty, proto, sk)); | |
861 | } | |
862 | ||
863 | int bind(int sk, const struct sockaddr *sa, socklen_t len) | |
864 | { | |
865 | struct sockaddr_un sun; | |
866 | ||
9f82ba1f | 867 | if (sa->sa_family == AF_INET) { |
e4976bb0 | 868 | PRESERVING_ERRNO({ |
869 | if (acl_allows_p(bind_real, SIN(sa))) { | |
870 | if (fixup_real_ip_socket(sk)) | |
871 | return (-1); | |
872 | } else { | |
873 | encode_inet_addr(&sun, SIN(sa), WANT_FRESH); | |
874 | sa = SA(&sun); | |
875 | len = SUN_LEN(&sun); | |
876 | } | |
877 | }); | |
878 | } | |
879 | return real_bind(sk, sa, len); | |
880 | } | |
881 | ||
882 | int connect(int sk, const struct sockaddr *sa, socklen_t len) | |
883 | { | |
884 | struct sockaddr_un sun; | |
53390eff | 885 | int rc; |
e4976bb0 | 886 | |
6df6f816 MW |
887 | switch (sa->sa_family) { |
888 | case AF_INET: | |
889 | PRESERVING_ERRNO({ | |
890 | do_implicit_bind(sk, &sa, &len, &sun); | |
6df6f816 MW |
891 | }); |
892 | rc = real_connect(sk, sa, len); | |
893 | if (rc < 0) { | |
894 | switch (errno) { | |
895 | case ENOENT: errno = ECONNREFUSED; break; | |
896 | } | |
897 | } | |
898 | break; | |
899 | default: | |
900 | rc = real_connect(sk, sa, len); | |
901 | break; | |
53390eff MW |
902 | } |
903 | return rc; | |
e4976bb0 | 904 | } |
905 | ||
906 | ssize_t sendto(int sk, const void *buf, size_t len, int flags, | |
907 | const struct sockaddr *to, socklen_t tolen) | |
908 | { | |
909 | struct sockaddr_un sun; | |
910 | ||
911 | if (to && to->sa_family == AF_INET) { | |
912 | PRESERVING_ERRNO({ | |
913 | do_implicit_bind(sk, &to, &tolen, &sun); | |
914 | }); | |
915 | } | |
916 | return real_sendto(sk, buf, len, flags, to, tolen); | |
917 | } | |
918 | ||
919 | ssize_t recvfrom(int sk, void *buf, size_t len, int flags, | |
920 | struct sockaddr *from, socklen_t *fromlen) | |
921 | { | |
922 | char sabuf[1024]; | |
923 | socklen_t mylen = sizeof(sabuf); | |
924 | ssize_t n; | |
925 | ||
926 | if (!from) | |
927 | return real_recvfrom(sk, buf, len, flags, 0, 0); | |
928 | PRESERVING_ERRNO({ | |
929 | n = real_recvfrom(sk, buf, len, flags, SA(sabuf), &mylen); | |
930 | if (n < 0) | |
931 | return (-1); | |
932 | return_fake_name(SA(sabuf), mylen, from, fromlen); | |
933 | }); | |
934 | return (n); | |
935 | } | |
936 | ||
937 | ssize_t sendmsg(int sk, const struct msghdr *msg, int flags) | |
938 | { | |
939 | struct sockaddr_un sun; | |
940 | const struct sockaddr *sa; | |
941 | struct msghdr mymsg; | |
942 | ||
943 | if (msg->msg_name && SA(msg->msg_name)->sa_family == AF_INET) { | |
944 | PRESERVING_ERRNO({ | |
945 | sa = SA(msg->msg_name); | |
946 | mymsg = *msg; | |
947 | do_implicit_bind(sk, &sa, &mymsg.msg_namelen, &sun); | |
948 | mymsg.msg_name = SA(sa); | |
949 | msg = &mymsg; | |
950 | }); | |
951 | } | |
952 | return real_sendmsg(sk, msg, flags); | |
953 | } | |
954 | ||
955 | ssize_t recvmsg(int sk, struct msghdr *msg, int flags) | |
956 | { | |
957 | char sabuf[1024]; | |
958 | struct sockaddr *sa; | |
959 | socklen_t len; | |
960 | ssize_t n; | |
961 | ||
962 | if (!msg->msg_name) | |
963 | return real_recvmsg(sk, msg, flags); | |
964 | PRESERVING_ERRNO({ | |
965 | sa = SA(msg->msg_name); | |
966 | len = msg->msg_namelen; | |
967 | msg->msg_name = sabuf; | |
968 | msg->msg_namelen = sizeof(sabuf); | |
969 | n = real_recvmsg(sk, msg, flags); | |
970 | if (n < 0) | |
971 | return (-1); | |
972 | return_fake_name(SA(sabuf), msg->msg_namelen, sa, &len); | |
973 | msg->msg_name = sa; | |
974 | msg->msg_namelen = len; | |
975 | }); | |
976 | return (n); | |
977 | } | |
978 | ||
979 | int accept(int sk, struct sockaddr *sa, socklen_t *len) | |
980 | { | |
981 | char sabuf[1024]; | |
982 | socklen_t mylen = sizeof(sabuf); | |
983 | int nsk = real_accept(sk, SA(sabuf), &mylen); | |
984 | ||
985 | if (nsk < 0) | |
986 | return (-1); | |
987 | return_fake_name(SA(sabuf), mylen, sa, len); | |
988 | return (nsk); | |
989 | } | |
990 | ||
991 | int getsockname(int sk, struct sockaddr *sa, socklen_t *len) | |
992 | { | |
993 | PRESERVING_ERRNO({ | |
994 | char sabuf[1024]; | |
995 | socklen_t mylen = sizeof(sabuf); | |
996 | if (real_getsockname(sk, SA(sabuf), &mylen)) | |
997 | return (-1); | |
998 | return_fake_name(SA(sabuf), mylen, sa, len); | |
999 | }); | |
1000 | return (0); | |
1001 | } | |
1002 | ||
1003 | int getpeername(int sk, struct sockaddr *sa, socklen_t *len) | |
1004 | { | |
1005 | PRESERVING_ERRNO({ | |
1006 | char sabuf[1024]; | |
1007 | socklen_t mylen = sizeof(sabuf); | |
1008 | if (real_getpeername(sk, SA(sabuf), &mylen)) | |
1009 | return (-1); | |
1010 | return_fake_name(SA(sabuf), mylen, sa, len); | |
1011 | }); | |
1012 | return (0); | |
1013 | } | |
1014 | ||
1015 | int getsockopt(int sk, int lev, int opt, void *p, socklen_t *len) | |
1016 | { | |
1017 | switch (lev) { | |
1018 | case SOL_IP: | |
1019 | case SOL_TCP: | |
1020 | case SOL_UDP: | |
1021 | if (*len > 0) | |
1022 | memset(p, 0, *len); | |
1023 | return (0); | |
1024 | } | |
1025 | return real_getsockopt(sk, lev, opt, p, len); | |
1026 | } | |
1027 | ||
1028 | int setsockopt(int sk, int lev, int opt, const void *p, socklen_t len) | |
1029 | { | |
1030 | switch (lev) { | |
1031 | case SOL_IP: | |
1032 | case SOL_TCP: | |
1033 | case SOL_UDP: | |
1034 | return (0); | |
1035 | } | |
1036 | switch (opt) { | |
1037 | case SO_BINDTODEVICE: | |
1038 | case SO_ATTACH_FILTER: | |
1039 | case SO_DETACH_FILTER: | |
1040 | return (0); | |
1041 | } | |
1042 | return real_setsockopt(sk, lev, opt, p, len); | |
1043 | } | |
1044 | ||
1d1ccf4f | 1045 | /*----- Initialization ----------------------------------------------------*/ |
e4976bb0 | 1046 | |
1d1ccf4f | 1047 | /* Clean up the socket directory, deleting stale sockets. */ |
e4976bb0 | 1048 | static void cleanup_sockdir(void) |
1049 | { | |
1050 | DIR *dir; | |
1051 | struct dirent *d; | |
3ef1fec9 | 1052 | struct sockaddr_in sin; |
e4976bb0 | 1053 | struct sockaddr_un sun; |
3ef1fec9 | 1054 | struct stat st; |
e4976bb0 | 1055 | |
1056 | if ((dir = opendir(sockdir)) == 0) | |
1057 | return; | |
4ab301de | 1058 | sun.sun_family = AF_UNIX; |
e4976bb0 | 1059 | while ((d = readdir(dir)) != 0) { |
1060 | if (d->d_name[0] == '.') continue; | |
1061 | snprintf(sun.sun_path, sizeof(sun.sun_path), | |
1062 | "%s/%s", sockdir, d->d_name); | |
00a98a8a | 1063 | if (decode_inet_addr(&sin, &sun, SUN_LEN(&sun), 0) || |
3ef1fec9 | 1064 | stat(sun.sun_path, &st) || |
4ab301de | 1065 | !S_ISSOCK(st.st_mode)) { |
1066 | D( fprintf(stderr, "noip: ignoring unknown socketdir entry `%s'\n", | |
1067 | sun.sun_path); ) | |
3ef1fec9 | 1068 | continue; |
4ab301de | 1069 | } |
e4976bb0 | 1070 | if (unix_socket_status(&sun, 0) == STALE) { |
1071 | D( fprintf(stderr, "noip: clearing away stale socket %s\n", | |
1072 | d->d_name); ) | |
1073 | unlink(sun.sun_path); | |
1074 | } | |
1075 | } | |
1076 | closedir(dir); | |
1077 | } | |
1078 | ||
1d1ccf4f MW |
1079 | /* Find the addresses attached to local network interfaces, and remember them |
1080 | * in a table. | |
1081 | */ | |
e4976bb0 | 1082 | static void get_local_ipaddrs(void) |
1083 | { | |
9f1396d9 MW |
1084 | struct ifaddrs *ifa_head, *ifa; |
1085 | const struct in_addr *a; | |
e4976bb0 | 1086 | int i; |
1087 | ||
9f1396d9 MW |
1088 | if (getifaddrs(&ifa_head)) { perror("getifaddrs"); return; } |
1089 | for (n_local_ipaddrs = 0, ifa = ifa_head; | |
1090 | n_local_ipaddrs < MAX_LOCAL_IPADDRS && ifa; | |
1091 | ifa = ifa->ifa_next) { | |
1092 | if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) | |
e4976bb0 | 1093 | continue; |
9f1396d9 MW |
1094 | a = &SIN(ifa->ifa_addr)->sin_addr; |
1095 | D( fprintf(stderr, "noip: local addr %s = %s", | |
1096 | ifa->ifa_name, inet_ntoa(*a)); ) | |
1097 | for (i = 0; i < n_local_ipaddrs; i++) { | |
1098 | if (local_ipaddrs[i].s_addr == a->s_addr) { | |
1099 | D( fprintf(stderr, " (duplicate)\n"); ) | |
1100 | goto skip; | |
1101 | } | |
1102 | } | |
1103 | D( fprintf(stderr, "\n"); ) | |
1104 | local_ipaddrs[n_local_ipaddrs] = *a; | |
1105 | n_local_ipaddrs++; | |
1106 | skip:; | |
e4976bb0 | 1107 | } |
9f1396d9 | 1108 | freeifaddrs(ifa_head); |
e4976bb0 | 1109 | } |
1110 | ||
1d1ccf4f | 1111 | /* Print the given message to standard error. Avoids stdio. */ |
72d85cdb | 1112 | static void printerr(const char *p) |
65f3786c | 1113 | { if (write(STDERR_FILENO, p, strlen(p))) ; } |
3ef1fec9 | 1114 | |
1d1ccf4f | 1115 | /* Create the socket directory, being careful about permissions. */ |
3ef1fec9 | 1116 | static void create_sockdir(void) |
1117 | { | |
1118 | struct stat st; | |
1119 | ||
1120 | if (stat(sockdir, &st)) { | |
1121 | if (errno == ENOENT) { | |
1122 | if (mkdir(sockdir, 0700)) { | |
1123 | perror("noip: creating socketdir"); | |
1124 | exit(127); | |
1125 | } | |
1126 | if (!stat(sockdir, &st)) | |
1127 | goto check; | |
1128 | } | |
1129 | perror("noip: checking socketdir"); | |
1130 | exit(127); | |
1131 | } | |
1132 | check: | |
1133 | if (!S_ISDIR(st.st_mode)) { | |
1134 | printerr("noip: bad socketdir: not a directory\n"); | |
1135 | exit(127); | |
1136 | } | |
1137 | if (st.st_uid != uid) { | |
1138 | printerr("noip: bad socketdir: not owner\n"); | |
1139 | exit(127); | |
1140 | } | |
1141 | if (st.st_mode & 077) { | |
1142 | printerr("noip: bad socketdir: not private\n"); | |
1143 | exit(127); | |
1144 | } | |
1145 | } | |
1146 | ||
1d1ccf4f MW |
1147 | /* Initialization function. */ |
1148 | static void setup(void) __attribute__((constructor)); | |
e4976bb0 | 1149 | static void setup(void) |
1150 | { | |
1151 | PRESERVING_ERRNO({ | |
1152 | char *p; | |
1153 | ||
1154 | import(); | |
3ef1fec9 | 1155 | uid = geteuid(); |
e4976bb0 | 1156 | if ((p = getenv("NOIP_DEBUG")) && atoi(p)) |
1157 | debug = 1; | |
1158 | get_local_ipaddrs(); | |
1159 | readconfig(); | |
3ef1fec9 | 1160 | create_sockdir(); |
e4976bb0 | 1161 | cleanup_sockdir(); |
1162 | }); | |
1163 | } | |
1d1ccf4f MW |
1164 | |
1165 | /*----- That's all, folks -------------------------------------------------*/ |