Skip to content

Commit 72d01e6

Browse files
Jamie GrittonJamie Gritton
authored andcommitted
netgraph: teach ngctl to attach and run itself in a jail
Add -j <jail> flag to ngctl to allow ngctl to attach and run inside a jail. This allow parent to manipulate netgraph nodes in the jail even if ngctl is not available. Submitted by: David Marker <dave_freedave.net> Reviewed by: kevans, zlei, jamie Relnotes: yes Differential Revision: https://reviews.freebsd.org/D50241
1 parent c4fed7d commit 72d01e6

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

usr.sbin/ngctl/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ LIBADD= netgraph
1313
CFLAGS+= -DEDITLINE
1414
LIBADD+= edit pthread
1515

16+
.if ${MK_JAIL} != "no"
17+
CFLAGS+= -DJAIL
18+
LIBADD+= jail
19+
.endif
20+
1621
.include <bsd.prog.mk>

usr.sbin/ngctl/main.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
#include <histedit.h>
5656
#include <pthread.h>
5757
#endif
58+
#ifdef JAIL
59+
#include <sys/jail.h>
60+
#include <jail.h>
61+
#endif
5862

5963
#include <netgraph.h>
6064

@@ -137,16 +141,17 @@ int csock, dsock;
137141
int
138142
main(int ac, char *av[])
139143
{
140-
char name[NG_NODESIZ];
141-
int interactive = isatty(0) && isatty(1);
142-
FILE *fp = NULL;
143-
int ch, rtn = 0;
144+
char name[NG_NODESIZ];
145+
int interactive = isatty(0) && isatty(1);
146+
FILE *fp = NULL;
147+
const char *jail_name = NULL;
148+
int ch, rtn = 0;
144149

145150
/* Set default node name */
146151
snprintf(name, sizeof(name), "ngctl%d", getpid());
147152

148153
/* Parse command line */
149-
while ((ch = getopt(ac, av, "df:n:")) != -1) {
154+
while ((ch = getopt(ac, av, "df:j:n:")) != -1) {
150155
switch (ch) {
151156
case 'd':
152157
NgSetDebug(NgSetDebug(-1) + 1);
@@ -157,6 +162,13 @@ main(int ac, char *av[])
157162
else if ((fp = fopen(optarg, "r")) == NULL)
158163
err(EX_NOINPUT, "%s", optarg);
159164
break;
165+
case 'j':
166+
#ifdef JAIL
167+
jail_name = optarg;
168+
#else
169+
errx(EX_UNAVAILABLE, "not built with jail support");
170+
#endif
171+
break;
160172
case 'n':
161173
snprintf(name, sizeof(name), "%s", optarg);
162174
break;
@@ -169,6 +181,22 @@ main(int ac, char *av[])
169181
ac -= optind;
170182
av += optind;
171183

184+
if (jail_name != NULL) {
185+
int jid;
186+
187+
if (jail_name[0] == '\0')
188+
Usage("invalid jail name");
189+
190+
jid = jail_getid(jail_name);
191+
192+
if (jid == -1)
193+
errx((errno == EPERM) ? EX_NOPERM : EX_NOHOST,
194+
"%s", jail_errmsg);
195+
if (jail_attach(jid) != 0)
196+
errx((errno == EPERM) ? EX_NOPERM : EX_OSERR,
197+
"cannot attach to jail");
198+
}
199+
172200
/* Create a new socket node */
173201
if (NgMkSockNode(name, &csock, &dsock) < 0)
174202
err(EX_OSERR, "can't create node");
@@ -657,6 +685,7 @@ Usage(const char *msg)
657685
if (msg)
658686
warnx("%s", msg);
659687
fprintf(stderr,
660-
"usage: ngctl [-d] [-f file] [-n name] [command ...]\n");
688+
"usage: ngctl [-j jail] [-d] [-f filename] [-n nodename] "
689+
"[command [argument ...]]\n");
661690
exit(EX_USAGE);
662691
}

usr.sbin/ngctl/ngctl.8

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@
3131
.\" OF SUCH DAMAGE.
3232
.\" $Whistle: ngctl.8,v 1.6 1999/01/20 03:19:44 archie Exp $
3333
.\"
34-
.Dd January 19, 1999
34+
.Dd August 29, 2025
3535
.Dt NGCTL 8
3636
.Os
3737
.Sh NAME
3838
.Nm ngctl
3939
.Nd netgraph control utility
4040
.Sh SYNOPSIS
4141
.Nm
42+
.Op Fl j Ar jail
4243
.Op Fl d
4344
.Op Fl f Ar filename
4445
.Op Fl n Ar nodename
46+
.Op Ar command Op Ns Ar argument ...
4547
.Op Ar command ...
4648
.Sh DESCRIPTION
4749
The
@@ -73,12 +75,31 @@ form if the originating node supports conversion.
7375
.Pp
7476
The options are as follows:
7577
.Bl -tag -width indent
76-
.It Fl f Ar nodeinfo
78+
.It Fl f Ar filename
7779
Read commands from the named file.
7880
A single dash represents the standard input.
7981
Blank lines and lines starting with a
8082
.Dq #
8183
are ignored.
84+
Note that when the
85+
.Fl j Ar jail
86+
option is specified, the file will be opened before attaching to the jail and
87+
then be processed inside the jail.
88+
.It Fl j Ar jail
89+
Perform the actions inside the
90+
.Ar jail .
91+
.Pp
92+
.Nm
93+
will first attach to the
94+
.Ar jail
95+
(by jail id or jail name) before performing the effects.
96+
.Pp
97+
This allows netgraph nodes of
98+
.Ar jail
99+
to be created, modified, and destroyed even if the
100+
.Nm
101+
binary is not available in
102+
.Ar jail .
82103
.It Fl n Ar nodename
83104
Assign
84105
.Em nodename

0 commit comments

Comments
 (0)