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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/* $Id: innbind.c 7724 2008-04-06 06:50:54Z iulius $
**
** Helper program to bind a socket to a low-numbered port.
**
** Written by Russ Allbery <rra@stanford.edu>
*/
#include "config.h"
#include "clibrary.h"
#include "portable/socket.h"
#include <errno.h>
#ifdef HAVE_STREAMS_SENDFD
# include <stropts.h>
#endif
#include <syslog.h>
#include "inn/libinn.h"
#include "inn/messages.h"
#include "inn/newsuser.h"
#include "inn/vector.h"
/* Macros to set the len attribute of sockaddrs. */
#if HAVE_STRUCT_SOCKADDR_SA_LEN
# define sin_set_length(s) ((s)->sin_len = sizeof(struct sockaddr_in))
# define sin6_set_length(s) ((s)->sin6_len = sizeof(struct sockaddr_in6))
#else
# define sin_set_length(s) /* empty */
# define sin6_set_length(s) /* empty */
#endif
/* INND_PORT is the additional port specified at configure time to which the
news user should be allowed to bind. If it's not set, set it to 119 (which
will cause it to have no effect). I hate #ifdef in code, can you tell? */
#ifndef INND_PORT
# define INND_PORT 119
#endif
/* Holds the information about a network socket to bind. */
struct binding {
int fd;
int family;
char *address;
unsigned short port;
};
/*
** Convert a string to a number with error checking, returning true if the
** number was parsed correctly and false otherwise. Stores the converted
** number in the second argument. Equivalent to calling strtol, but with the
** base always fixed at 10, with checking of errno, ensuring that all of the
** string is consumed, and checking that the resulting number is positive.
*/
static bool
convert_string(const char *string, long *result)
{
char *end;
if (*string == '\0')
return false;
errno = 0;
*result = strtol(string, &end, 10);
if (errno != 0 || *end != '\0' || *result < 0)
return false;
return true;
}
/*
** Parse a command-line argument into a struct binding. The command line
** argument is four comma-separated values: the file descriptor, the family,
** the listening address, and the port number. The caller is responsible for
** freeing the address attribute of the supplied binding struct, although if
** a binding struct is passed in for use and has a non-NULL address, it will
** be freed first.
*/
static void
parse_argument(const char *string, struct binding *binding)
{
struct vector *spec;
long value;
/* Do the initial parse and allocate our data structures. */
spec = vector_split(string, ',', NULL);
if (spec->count != 4)
die("invalid command-line argument %s", string);
/* Get the file descriptor, address family, and port. */
if (!convert_string(spec->strings[0], &value))
die("invalid file descriptor %s in %s", spec->strings[0], string);
binding->fd = value;
if (!convert_string(spec->strings[1], &value))
die("invalid protocol family %s in %s", spec->strings[1], string);
binding->family = value;
if (binding->address != NULL)
free(binding->address);
binding->address = xstrdup(spec->strings[2]);
if (!convert_string(spec->strings[3], &value))
die("invalid port number %s in %s", spec->strings[3], string);
if (value == 0)
die("port may not be zero in %s", string);
binding->port = value;
/* Done. Clean up. */
vector_free(spec);
}
/*
** Bind an IPv4 address, given the file descriptor, string giving the
** address, and the port. The fourth argument is the full binding
** specification for error reporting. Returns true on success, false if
** binding failed due to permission denied. Die on any other failure.
*/
static bool
bind_ipv4(int fd, const char *address, unsigned short port, const char *spec)
{
struct sockaddr_in server;
struct in_addr addr;
memset(&server, '\0', sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
if (!inet_aton(address, &addr))
die("invalid IPv4 address %s in %s", address, spec);
server.sin_addr = addr;
sin_set_length(&server);
if (bind(fd, (struct sockaddr *) &server, sizeof(server)) < 0) {
if (errno == EACCES)
return false;
else
sysdie("cannot bind socket for %s", spec);
}
return true;
}
/*
** Bind an IPv6 address, given the file descriptor, string giving the
** address, and the port. The fourth argument is the full binding
** specification for error reporting. Returns true on success, false if
** binding failed due to permission denied. Die on any other failure.
*/
#ifdef HAVE_INET6
static bool
bind_ipv6(int fd, const char *address, unsigned short port, const char *spec)
{
struct sockaddr_in6 server;
struct in6_addr addr;
memset(&server, '\0', sizeof(server));
server.sin6_family = AF_INET6;
server.sin6_port = htons(port);
if (inet_pton(AF_INET6, address, &addr) < 1)
die("invalid IPv6 address %s in %s", address, spec);
server.sin6_addr = addr;
sin6_set_length(&server);
if (bind(fd, (struct sockaddr *) &server, sizeof(server)) < 0) {
if (errno == EACCES)
return false;
else
sysdie("cannot bind socket for %s", spec);
}
return true;
}
#endif /* HAVE_INET6 */
/*
** Given a struct binding, bind that file descriptor. Also takes the
** command-line argument for error reporting. Returns true on success, false
** if binding failed due to permission denied. Die on any other failure.
*/
static bool
bind_address(struct binding *binding, const char *spec)
{
int fd = binding->fd;
unsigned short port = binding->port;
int type;
socklen_t length;
/* Make sure that we're allowed to bind to that port. */
if (port < 1024 && port != 119 && port != 433 && port != 563
&& port != INND_PORT)
die("cannot bind to restricted port %hu in %s", port, spec);
/* Sanity check on the socket. */
length = sizeof(type);
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &length) < 0)
sysdie("cannot get socket options for file descriptor %d", fd);
if (type != SOCK_STREAM)
die("invalid file descriptor %d: not SOCK_STREAM", fd);
/* Based on the address family, parse either an IPv4 or IPv6 address. */
if (binding->family == AF_INET)
return bind_ipv4(fd, binding->address, port, spec);
#ifdef HAVE_INET6
else if (binding->family == AF_INET6)
return bind_ipv6(fd, binding->address, port, spec);
#endif
else
die("unknown protocol family %d in %s", binding->family, spec);
}
/*
** Given a struct binding, create a socket for it and fill in the file
** descriptor. Also takes the command-line argument for error reporting.
** Dies on any failure.
*/
static void
create_socket(struct binding *binding, const char *spec)
{
int fd;
#ifdef SO_REUSEADDR
int flag;
#endif
/* Create the socket. */
if (binding->family == AF_INET)
fd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
#ifdef HAVE_INET6
else if (binding->family == AF_INET6)
fd = socket(PF_INET6, SOCK_STREAM, IPPROTO_IP);
#endif
else
die("unknown protocol family %d in %s", binding->family, spec);
if (fd < -1)
sysdie("cannot create socket for %s", spec);
/* Mark it reusable if possible. */
#ifdef SO_REUSEADDR
flag = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0)
sysdie("cannot mark socket reusable for %s", spec);
#endif
/* Mark it IPv6 only if possible. */
#ifdef IPV6_V6ONLY
flag = 1;
if (binding->family == AF_INET6 &&
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
sysdie("cannot mark socket IPv6 only for %s", spec);
#endif
/* Fill in the struct. */
binding->fd = fd;
}
/*
** Given a file descriptor, attempt to pass it via stdout. Die on any error.
** Currently, file descriptor passing is only implemented for systems that
** use STREAMS.
*/
#ifdef HAVE_STREAMS_SENDFD
static void
send_fd(int fd)
{
if (isastream(STDOUT_FILENO) != 1)
die("cannot pass file descriptor: stdout is not a stream");
if (ioctl(STDOUT_FILENO, I_SENDFD, fd) < 0)
sysdie("cannot pass file descriptor");
}
#else /* !HAVE_STREAMS_SENDFD */
static void
send_fd(int fd UNUSED)
{
die("cannot pass file descriptor: STREAMS not supported");
}
#endif /* !HAVE_STREAMS_SENDFD */
int
main(int argc, char *argv[])
{
uid_t real_uid, uid;
int i;
bool done;
struct binding binding = { 0, 0, NULL, 0 };
bool force_sendfd = false;
/* Set up the error handlers. Errors go to stderr and to syslog with a
priority of LOG_CRIT. This priority level is too high, but it's chosen
to match innd. */
openlog("innbind", LOG_CONS, LOG_INN_PROG);
message_handlers_die(2, message_log_stderr, message_log_syslog_crit);
message_program_name = "innbind";
/* If we're running privileged (effective and real UIDs are different),
convert runasuser to a UID and exit if run by another user. Don't do
this if we're not running privileged to make installations that don't
need privileged ports easier and to make testing easier. */
real_uid = getuid();
if (real_uid != geteuid()) {
get_news_uid_gid(&uid, false, true);
if (real_uid != uid) {
die("must be run by runasuser (%lu), not %lu",
(unsigned long) uid, (unsigned long) real_uid);
}
}
/* If the first argument is -p, force creation of the socket and file
descriptor passing rather than even attempting to bind the socket
first. */
if (argc > 1 && strcmp(argv[1], "-p") == 0) {
force_sendfd = true;
argc--;
argv++;
}
if (argc < 2)
die("no addresses specified");
/* Walk the argument list and try to bind each argument. For each
successful bind, print "ok\n" to stdout. For each bind that fails with
permission denied, print "no\n" to stdout and then immediately attempt
to create a new file descriptor and pass it back over stdout. */
for (i = 1; i < argc; i++) {
parse_argument(argv[i], &binding);
done = false;
if (!force_sendfd)
done = bind_address(&binding, argv[i]);
if (done)
write(STDOUT_FILENO, "ok\n", 3);
else {
write(STDOUT_FILENO, "no\n", 3);
create_socket(&binding, argv[i]);
if (!bind_address(&binding, argv[i]))
sysdie("cannot bind socket for %s", argv[i]);
send_fd(binding.fd);
}
}
exit(0);
}
|