| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include <sys/types.h> |
| 37 |
#include <sys/socket.h> |
| 38 |
|
| 39 |
#include <netdb.h> |
| 40 |
#include <netinet/in.h> |
| 41 |
|
| 42 |
#include <stdio.h> |
| 43 |
#include <ctype.h> |
| 44 |
#include <errno.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
#include <unistd.h> |
| 48 |
|
| 49 |
#include <rpc/rpc.h> |
| 50 |
|
| 51 |
#include <string.h> |
| 52 |
#include <reentrant.h> |
| 53 |
|
| 54 |
extern pthread_mutex_t port_lock; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
int |
| 60 |
bindresvport(sd, sin) |
| 61 |
int sd; |
| 62 |
struct sockaddr_in *sin; |
| 63 |
{ |
| 64 |
return bindresvport_sa(sd, (struct sockaddr *)sin); |
| 65 |
} |
| 66 |
|
| 67 |
#ifdef __linux__ |
| 68 |
|
| 69 |
#define STARTPORT 600 |
| 70 |
#define LOWPORT 512 |
| 71 |
#define ENDPORT (IPPORT_RESERVED - 1) |
| 72 |
#define NPORTS (ENDPORT - STARTPORT + 1) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static int blacklist_read; |
| 80 |
static int *list; |
| 81 |
static int list_size = 0; |
| 82 |
|
| 83 |
static void |
| 84 |
load_blacklist (void) |
| 85 |
{ |
| 86 |
FILE *fp; |
| 87 |
char *buf = NULL; |
| 88 |
size_t buflen = 0; |
| 89 |
int size = 0, ptr = 0; |
| 90 |
|
| 91 |
blacklist_read = 1; |
| 92 |
|
| 93 |
fp = fopen ("/etc/bindresvport.blacklist", "r"); |
| 94 |
if (NULL == fp) |
| 95 |
return; |
| 96 |
|
| 97 |
while (!feof (fp)) |
| 98 |
{ |
| 99 |
unsigned long port; |
| 100 |
char *tmp, *cp; |
| 101 |
ssize_t n = getline (&buf, &buflen, fp); |
| 102 |
if (n < 1) |
| 103 |
break; |
| 104 |
|
| 105 |
cp = buf; |
| 106 |
tmp = strchr (cp, '#'); |
| 107 |
if (tmp) |
| 108 |
*tmp = '\0'; |
| 109 |
while (isspace ((int)*cp)) |
| 110 |
++cp; |
| 111 |
if (*cp == '\0') |
| 112 |
continue; |
| 113 |
if (cp[strlen (cp) - 1] == '\n') |
| 114 |
cp[strlen (cp) - 1] = '\0'; |
| 115 |
|
| 116 |
port = strtoul (cp, &tmp, 0); |
| 117 |
while (isspace(*tmp)) |
| 118 |
++tmp; |
| 119 |
if (*tmp != '\0' || (port == ULONG_MAX && errno == ERANGE)) |
| 120 |
continue; |
| 121 |
|
| 122 |
|
| 123 |
if (port < LOWPORT || port > ENDPORT) |
| 124 |
continue; |
| 125 |
|
| 126 |
if (ptr >= size) |
| 127 |
{ |
| 128 |
size += 10; |
| 129 |
list = realloc (list, size * sizeof (int)); |
| 130 |
if (list == NULL) |
| 131 |
{ |
| 132 |
free (buf); |
| 133 |
fclose (fp); |
| 134 |
return; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
list[ptr++] = port; |
| 139 |
} |
| 140 |
|
| 141 |
fclose (fp); |
| 142 |
|
| 143 |
if (buf) |
| 144 |
free (buf); |
| 145 |
|
| 146 |
list_size = ptr; |
| 147 |
} |
| 148 |
|
| 149 |
int |
| 150 |
bindresvport_sa(sd, sa) |
| 151 |
int sd; |
| 152 |
struct sockaddr *sa; |
| 153 |
{ |
| 154 |
int res, af; |
| 155 |
struct sockaddr_storage myaddr; |
| 156 |
struct sockaddr_in *sin; |
| 157 |
#ifdef INET6 |
| 158 |
struct sockaddr_in6 *sin6; |
| 159 |
#endif |
| 160 |
u_int16_t *portp; |
| 161 |
static u_int16_t port; |
| 162 |
static short startport = STARTPORT; |
| 163 |
socklen_t salen; |
| 164 |
int nports; |
| 165 |
int endport = ENDPORT; |
| 166 |
int i; |
| 167 |
|
| 168 |
mutex_lock(&port_lock); |
| 169 |
|
| 170 |
if (!blacklist_read) |
| 171 |
load_blacklist(); |
| 172 |
|
| 173 |
nports = ENDPORT - startport + 1; |
| 174 |
|
| 175 |
if (sa == NULL) { |
| 176 |
salen = sizeof(myaddr); |
| 177 |
sa = (struct sockaddr *)&myaddr; |
| 178 |
|
| 179 |
if (getsockname(sd, (struct sockaddr *)&myaddr, &salen) == -1) { |
| 180 |
mutex_unlock(&port_lock); |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
af = myaddr.ss_family; |
| 185 |
} else |
| 186 |
af = sa->sa_family; |
| 187 |
|
| 188 |
switch (af) { |
| 189 |
case AF_INET: |
| 190 |
sin = (struct sockaddr_in *)sa; |
| 191 |
salen = sizeof(struct sockaddr_in); |
| 192 |
port = ntohs(sin->sin_port); |
| 193 |
portp = &sin->sin_port; |
| 194 |
break; |
| 195 |
#ifdef INET6 |
| 196 |
case AF_INET6: |
| 197 |
sin6 = (struct sockaddr_in6 *)sa; |
| 198 |
salen = sizeof(struct sockaddr_in6); |
| 199 |
port = ntohs(sin6->sin6_port); |
| 200 |
portp = &sin6->sin6_port; |
| 201 |
break; |
| 202 |
#endif |
| 203 |
default: |
| 204 |
errno = EPFNOSUPPORT; |
| 205 |
mutex_unlock(&port_lock); |
| 206 |
return (-1); |
| 207 |
} |
| 208 |
sa->sa_family = af; |
| 209 |
|
| 210 |
if (port == 0) { |
| 211 |
port = (getpid() % NPORTS) + STARTPORT; |
| 212 |
} |
| 213 |
res = -1; |
| 214 |
errno = EADDRINUSE; |
| 215 |
again: |
| 216 |
for (i = 0; i < nports; ++i) { |
| 217 |
int j; |
| 218 |
|
| 219 |
|
| 220 |
for (j = 0; j < list_size; j++) |
| 221 |
if (port == list[j]) |
| 222 |
goto try_next_port; |
| 223 |
|
| 224 |
*portp = htons(port); |
| 225 |
res = bind(sd, sa, salen); |
| 226 |
if (res >= 0 || errno != EADDRINUSE) |
| 227 |
break; |
| 228 |
|
| 229 |
try_next_port: |
| 230 |
if (++port > endport) |
| 231 |
port = startport; |
| 232 |
} |
| 233 |
if (i == nports && startport != LOWPORT) { |
| 234 |
startport = LOWPORT; |
| 235 |
endport = STARTPORT - 1; |
| 236 |
nports = STARTPORT - LOWPORT; |
| 237 |
port = LOWPORT + port % (STARTPORT - LOWPORT); |
| 238 |
goto again; |
| 239 |
} |
| 240 |
mutex_unlock(&port_lock); |
| 241 |
|
| 242 |
return (res); |
| 243 |
} |
| 244 |
#else |
| 245 |
|
| 246 |
#define IP_PORTRANGE 19 |
| 247 |
#define IP_PORTRANGE_LOW 2 |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
int |
| 253 |
bindresvport_sa(sd, sa) |
| 254 |
int sd; |
| 255 |
struct sockaddr *sa; |
| 256 |
{ |
| 257 |
int old, error, af; |
| 258 |
struct sockaddr_storage myaddr; |
| 259 |
struct sockaddr_in *sin; |
| 260 |
#ifdef INET6 |
| 261 |
struct sockaddr_in6 *sin6; |
| 262 |
#endif |
| 263 |
int proto, portrange, portlow; |
| 264 |
u_int16_t *portp; |
| 265 |
socklen_t salen; |
| 266 |
|
| 267 |
if (sa == NULL) { |
| 268 |
salen = sizeof(myaddr); |
| 269 |
sa = (struct sockaddr *)&myaddr; |
| 270 |
|
| 271 |
if (getsockname(sd, sa, &salen) == -1) |
| 272 |
return -1; |
| 273 |
|
| 274 |
af = sa->sa_family; |
| 275 |
memset(sa, 0, salen); |
| 276 |
} else |
| 277 |
af = sa->sa_family; |
| 278 |
|
| 279 |
switch (af) { |
| 280 |
case AF_INET: |
| 281 |
proto = IPPROTO_IP; |
| 282 |
portrange = IP_PORTRANGE; |
| 283 |
portlow = IP_PORTRANGE_LOW; |
| 284 |
sin = (struct sockaddr_in *)sa; |
| 285 |
salen = sizeof(struct sockaddr_in); |
| 286 |
portp = &sin->sin_port; |
| 287 |
break; |
| 288 |
#ifdef INET6 |
| 289 |
case AF_INET6: |
| 290 |
proto = IPPROTO_IPV6; |
| 291 |
portrange = IPV6_PORTRANGE; |
| 292 |
portlow = IPV6_PORTRANGE_LOW; |
| 293 |
sin6 = (struct sockaddr_in6 *)sa; |
| 294 |
salen = sizeof(struct sockaddr_in6); |
| 295 |
portp = &sin6->sin6_port; |
| 296 |
break; |
| 297 |
#endif |
| 298 |
default: |
| 299 |
errno = EPFNOSUPPORT; |
| 300 |
return (-1); |
| 301 |
} |
| 302 |
sa->sa_family = af; |
| 303 |
|
| 304 |
if (*portp == 0) { |
| 305 |
socklen_t oldlen = sizeof(old); |
| 306 |
|
| 307 |
error = getsockopt(sd, proto, portrange, &old, &oldlen); |
| 308 |
if (error < 0) |
| 309 |
return (error); |
| 310 |
|
| 311 |
error = setsockopt(sd, proto, portrange, &portlow, |
| 312 |
sizeof(portlow)); |
| 313 |
if (error < 0) |
| 314 |
return (error); |
| 315 |
} |
| 316 |
|
| 317 |
error = bind(sd, sa, salen); |
| 318 |
|
| 319 |
if (*portp == 0) { |
| 320 |
int saved_errno = errno; |
| 321 |
|
| 322 |
if (error < 0) { |
| 323 |
if (setsockopt(sd, proto, portrange, &old, |
| 324 |
sizeof(old)) < 0) |
| 325 |
errno = saved_errno; |
| 326 |
return (error); |
| 327 |
} |
| 328 |
|
| 329 |
if (sa != (struct sockaddr *)&myaddr) { |
| 330 |
|
| 331 |
if (getsockname(sd, sa, &salen) < 0) |
| 332 |
errno = saved_errno; |
| 333 |
return (error); |
| 334 |
} |
| 335 |
} |
| 336 |
return (error); |
| 337 |
} |
| 338 |
#endif |