Skip to content

Commit 9da28f9

Browse files
authored
Merge pull request #94 from bjornaxis/master
Use privport with ipv6
2 parents b4eedf2 + 6cbbacf commit 9da28f9

File tree

4 files changed

+114
-35
lines changed

4 files changed

+114
-35
lines changed

libdiod/diod_conf.c

+28-24
Original file line numberDiff line numberDiff line change
@@ -351,30 +351,6 @@ void diod_conf_clr_exports (void)
351351
config.exports = _xlist_create ((ListDelF)_destroy_export);
352352
config.ro_mask |= RO_EXPORTS;
353353
}
354-
void diod_conf_add_exports (char *path)
355-
{
356-
Export *x = _xcreate_export (path);
357-
_xlist_append (config.exports, x);
358-
config.ro_mask |= RO_EXPORTS;
359-
}
360-
void diod_conf_validate_exports (void)
361-
{
362-
ListIterator itr;
363-
Export *x;
364-
365-
if (config.exportall == 0 && list_count (config.exports) == 0)
366-
msg_exit ("no exports defined");
367-
if ((itr = list_iterator_create (config.exports)) == NULL)
368-
msg_exit ("out of memory");
369-
while ((x = list_next (itr))) {
370-
if (*x->path != '/' && strcmp (x->path, "ctl") != 0)
371-
msg_exit ("exports should begin with '/'");
372-
if (strstr (x->path, "/..") != 0)
373-
msg_exit ("exports should not contain '/..'"); /* FIXME */
374-
}
375-
list_iterator_destroy (itr);
376-
}
377-
378354
static void
379355
_parse_expopt (char *s, int *fp)
380356
{
@@ -403,6 +379,34 @@ _parse_expopt (char *s, int *fp)
403379
free (cpy);
404380
*fp = flags;
405381
}
382+
void diod_conf_add_exports (char *path)
383+
{
384+
Export *x = _xcreate_export (path);
385+
if (config.exportopts)
386+
x->opts = _xstrdup (config.exportopts);
387+
if (x->opts)
388+
_parse_expopt (x->opts, &x->oflags);
389+
_xlist_append (config.exports, x);
390+
config.ro_mask |= RO_EXPORTS;
391+
392+
}
393+
void diod_conf_validate_exports (void)
394+
{
395+
ListIterator itr;
396+
Export *x;
397+
398+
if (config.exportall == 0 && list_count (config.exports) == 0)
399+
msg_exit ("no exports defined");
400+
if ((itr = list_iterator_create (config.exports)) == NULL)
401+
msg_exit ("out of memory");
402+
while ((x = list_next (itr))) {
403+
if (*x->path != '/' && strcmp (x->path, "ctl") != 0)
404+
msg_exit ("exports should begin with '/'");
405+
if (strstr (x->path, "/..") != 0)
406+
msg_exit ("exports should not contain '/..'"); /* FIXME */
407+
}
408+
list_iterator_destroy (itr);
409+
}
406410

407411
/* exportall - export everything in /proc/mounts
408412
*/

libdiod/diod_sock.c

+70-9
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ _bind_priv_inet4 (int sockfd)
396396
return rc;
397397
}
398398

399+
/* Bind socket to a local IPv6 port < 1024.
400+
*/
401+
static int
402+
_bind_priv_inet6 (int sockfd)
403+
{
404+
struct sockaddr_in6 in;
405+
int port;
406+
int rc = -1;
407+
408+
memset (&in, 0, sizeof(in));
409+
in.sin6_family = AF_INET6;
410+
in.sin6_addr = in6addr_any;
411+
412+
for (port = IPPORT_RESERVED - 1; port >= IPPORT_RESERVED / 2; port--) {
413+
in.sin6_port = htons ((ushort)port);
414+
rc = bind(sockfd, (struct sockaddr *) &in, sizeof(in));
415+
if (rc == 0 || (rc < 0 && errno != EADDRINUSE))
416+
break;
417+
}
418+
if (rc < 0 && errno == EADDRINUSE)
419+
errno = EAGAIN;
420+
return rc;
421+
}
422+
399423
/* Connect to host:port.
400424
* Return fd on success, -1 on failure.
401425
*/
@@ -428,16 +452,25 @@ diod_sock_connect_inet (char *host, char *port, int flags)
428452
continue;
429453
}
430454
if (flags & DIOD_SOCK_PRIVPORT) {
431-
if (r->ai_family != AF_INET) {
455+
if (r->ai_family == AF_INET) {
456+
if (_bind_priv_inet4 (fd) < 0) {
457+
errnum = errno;
458+
errmsg = "_bind_resv_inet4";
459+
(void)close (fd);
460+
fd = -1;
461+
continue;
462+
}
463+
} else if (r->ai_family == AF_INET6) {
464+
if (_bind_priv_inet6 (fd) < 0) {
465+
errnum = errno;
466+
errmsg = "_bind_resv_inet6";
467+
(void)close (fd);
468+
fd = -1;
469+
continue;
470+
}
471+
} else {
432472
errnum = EINVAL;
433-
errmsg = "_bind_resv_inet4";
434-
(void)close (fd);
435-
fd = -1;
436-
continue;
437-
}
438-
if (_bind_priv_inet4 (fd) < 0) {
439-
errnum = errno;
440-
errmsg = "_bind_resv_inet4";
473+
errmsg = "protocol";
441474
(void)close (fd);
442475
fd = -1;
443476
continue;
@@ -485,6 +518,30 @@ diod_sock_connect_unix (char *path, int flags)
485518
return -1;
486519
}
487520

521+
static int _diod_sock_connect_inet6(char *name, int flags)
522+
{
523+
char *hoststart;
524+
char *hostend;
525+
char *port;
526+
527+
hoststart = name + 1;
528+
if ((hostend = strchr (hoststart, ']'))) {
529+
port = strchr (hostend, ':');
530+
*hostend = '\0';
531+
} else {
532+
errno = EINVAL;
533+
if (!(flags & DIOD_SOCK_QUIET))
534+
err ("diod_sock_connect invalid address %s", name);
535+
return -1;
536+
}
537+
if (port) {
538+
port++;
539+
return diod_sock_connect_inet (hoststart, port, flags);
540+
} else {
541+
return diod_sock_connect_inet (hoststart, "564", flags);
542+
}
543+
}
544+
488545
int
489546
diod_sock_connect (char *name, int flags)
490547
{
@@ -505,6 +562,10 @@ diod_sock_connect (char *name, int flags)
505562
err ("diod_sock_connect %s", name);
506563
goto done;
507564
}
565+
if (host[0] == '[') {
566+
fd = _diod_sock_connect_inet6(host, flags);
567+
goto done;
568+
}
508569
if (!(port = strchr (host, ':'))) {
509570
errno = EINVAL;
510571
if (!(flags & DIOD_SOCK_QUIET))

utils/diodmount.8.in

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Set verbose mode.
4545
.TP
4646
.I "-o, --options opt[,opt,...]"
4747
Set mount options (see below).
48+
.TP
49+
.I "-p, --privport"
50+
Connect from a socket bound to a port in the range of 512-1023,
51+
available to root only. This can be used in conjunction with the
52+
\fIprivport\fR export option.
4853
.SH MOUNT OPTIONS
4954
The following file system specific mount options are handled by
5055
\fBmount.diod\fR.

utils/diodmount.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#include "diod_auth.h"
5858
#include "opt.h"
5959

60-
#define OPTIONS "fnvo:ad"
60+
#define OPTIONS "fnvo:adp"
6161
#if HAVE_GETOPT_LONG
6262
#define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL)
6363
static const struct option longopts[] = {
@@ -67,6 +67,7 @@ static const struct option longopts[] = {
6767
{"options", required_argument, 0, 'o'},
6868
{"9nbd-attach", no_argument, 0, 'a'},
6969
{"9nbd-detach", no_argument, 0, 'd'},
70+
{"privport", no_argument, 0, 'p'},
7071
{0, 0, 0, 0},
7172
};
7273
#else
@@ -106,6 +107,7 @@ usage (void)
106107
" -n,--no-mtab do not update /etc/mtab\n"
107108
" -v,--verbose verbose mode\n"
108109
" -o,--options opt[,opt,...] specify mount options\n"
110+
" -p,--privport bind localy to a privileged port\n"
109111
//"Usage: mount.diod --9nbd-attach host[:aname] 9nbd-device\n"
110112
//" --9nbd-detach 9nbd-device\n"
111113
);
@@ -124,6 +126,7 @@ main (int argc, char *argv[])
124126
int fopt = 0;
125127
int aopt = 0;
126128
int dopt = 0;
129+
int privport = 0;
127130
int rfd = -1, wfd = -1;
128131
Opt o;
129132

@@ -152,6 +155,9 @@ main (int argc, char *argv[])
152155
case 'd': /* --9nbd-detach */
153156
dopt++;
154157
break;
158+
case 'p': /* --privport */
159+
privport++;
160+
break;
155161
default:
156162
usage ();
157163
}
@@ -263,9 +269,12 @@ main (int argc, char *argv[])
263269
if (!(hi = hostlist_iterator_create (hl)))
264270
msg_exit ("out of memory");
265271
while ((h = hostlist_next (hi))) {
272+
int flags = DIOD_SOCK_QUIET;
273+
if (privport)
274+
flags |= DIOD_SOCK_PRIVPORT;
266275
if (vopt)
267276
msg ("trying to connect to %s:%s", h, port);
268-
if ((rfd = diod_sock_connect_inet (h, port, DIOD_SOCK_QUIET)) >= 0)
277+
if ((rfd = diod_sock_connect_inet (h, port, flags )) >= 0)
269278
break;
270279
}
271280
if (h) { /* create new 'spec' string identifying successful host */

0 commit comments

Comments
 (0)