From ec12841456217d619c7b7d145274962e2ef576b1 Mon Sep 17 00:00:00 2001 From: eugen Date: Mon, 10 Dec 2018 13:47:05 +0000 Subject: [PATCH] MFC r340319: jail(8): introduce new command option -e to exhibit a list of configured non-wildcard jails with their parameters, no matter running or not. The option -e takes separator argument that is used to separate printed parameters. It will be used with following additions to system periodic scripts to differentiate parts of directory tree belonging jails as opposed to host's. --- usr.sbin/jail/jail.8 | 14 ++++++++++- usr.sbin/jail/jail.c | 57 ++++++++++++++++++++++++++++++++++--------- usr.sbin/jail/jailp.h | 1 + 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/usr.sbin/jail/jail.8 b/usr.sbin/jail/jail.8 index b4d7bac9c6a..4ba1bc6b7bc 100644 --- a/usr.sbin/jail/jail.8 +++ b/usr.sbin/jail/jail.8 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd October 20, 2018 +.Dd November 10, 2018 .Dt JAIL 8 .Os .Sh NAME @@ -49,6 +49,7 @@ .Nm .Op Fl qv .Op Fl f Ar conf_file +.Op Fl e Ar separator .Op Fl rR .Op Cm * | Ar jail ... .Nm @@ -63,6 +64,7 @@ The .Nm utility creates new jails, or modifies or removes existing jails. +It can also print a list of configured jails and their parameters. A jail .Pq or Dq prison is specified via parameters on the command line, or in the @@ -71,6 +73,7 @@ file. .Pp At least one of the options .Fl c , +.Fl e , .Fl m or .Fl r @@ -86,6 +89,15 @@ and .Va name parameters (if specified on the command line) must not refer to an existing jail. +.It Fl e Ar separator +Exhibit a list of all configured non-wildcard jails and their parameters. +No jail creation, modification or removal performed if this option is used. +The +.Ar separator +string is used to separate parameters. +Use +.Xr jls 8 +utility to list running jails. .It Fl m Modify an existing jail. One of the diff --git a/usr.sbin/jail/jail.c b/usr.sbin/jail/jail.c index c0d6794357f..fa4dbc757e0 100644 --- a/usr.sbin/jail/jail.c +++ b/usr.sbin/jail/jail.c @@ -60,6 +60,7 @@ const char *cfname; int iflag; int note_remove; int verbose; +const char *separator = "\t"; static void clear_persist(struct cfjail *j); static int update_jail(struct cfjail *j); @@ -69,8 +70,9 @@ static void jail_quoted_warnx(const struct cfjail *j, const char *name_msg, const char *noname_msg); static int jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp, int flags); -static void print_jail(FILE *fp, struct cfjail *j, int oldcl); +static void print_jail(FILE *fp, struct cfjail *j, int oldcl, int running); static void print_param(FILE *fp, const struct cfparam *p, int sep, int doname); +static void show_jails(void); static void quoted_print(FILE *fp, char *str); static void usage(void); @@ -151,7 +153,7 @@ main(int argc, char **argv) cfname = CONF_FILE; JidFile = NULL; - while ((ch = getopt(argc, argv, "cdf:hiJ:lmn:p:qrRs:u:U:v")) != -1) { + while ((ch = getopt(argc, argv, "cde:f:hiJ:lmn:p:qrRs:u:U:v")) != -1) { switch (ch) { case 'c': op |= JF_START; @@ -159,6 +161,10 @@ main(int argc, char **argv) case 'd': dflag = 1; break; + case 'e': + op |= JF_SHOW; + separator = optarg; + break; case 'f': cfname = optarg; break; @@ -284,7 +290,13 @@ main(int argc, char **argv) enforce_statfs); } } - } else if (op == JF_STOP) { + } else if (op == JF_STOP || op == JF_SHOW) { + /* Just print list of all configured non-wildcard jails */ + if (op == JF_SHOW) { + load_config(); + show_jails(); + exit(0); + } /* Jail remove, perhaps using the config file */ if (!docf || argc == 0) usage(); @@ -447,7 +459,7 @@ main(int argc, char **argv) jail_create_done: clear_persist(j); if (jfp != NULL) - print_jail(jfp, j, oldcl); + print_jail(jfp, j, oldcl, 1); dep_done(j, 0); break; @@ -926,16 +938,18 @@ jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp, * Print a jail record. */ static void -print_jail(FILE *fp, struct cfjail *j, int oldcl) +print_jail(FILE *fp, struct cfjail *j, int oldcl, int running) { struct cfparam *p; + int printsep; if (oldcl) { - fprintf(fp, "%d\t", j->jid); + if (running) + fprintf(fp, "%d%s", j->jid, separator); print_param(fp, j->intparams[KP_PATH], ',', 0); - putc('\t', fp); + fputs(separator, fp); print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0); - putc('\t', fp); + fputs(separator, fp); #ifdef INET print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0); #ifdef INET6 @@ -949,19 +963,38 @@ print_jail(FILE *fp, struct cfjail *j, int oldcl) #ifdef INET6 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0); #endif - putc('\t', fp); + fputs(separator, fp); print_param(fp, j->intparams[IP_COMMAND], ' ', 0); } else { - fprintf(fp, "jid=%d", j->jid); + printsep = 0; + if (running) { + fprintf(fp, "jid=%d", j->jid); + printsep = 1; + } TAILQ_FOREACH(p, &j->params, tq) if (strcmp(p->name, "jid")) { - putc(' ', fp); + if (printsep) + fputs(separator, fp); + else + printsep = 1; print_param(fp, p, ',', 1); } } putc('\n', fp); } +/* + * Exhibit list of all configured non-wildcard jails + */ +static void +show_jails(void) +{ + struct cfjail *j; + + TAILQ_FOREACH(j, &cfjails, tq) + print_jail(stdout, j, 0, 0); +} + /* * Print a parameter value, or a name=value pair. */ @@ -1015,7 +1048,7 @@ usage(void) (void)fprintf(stderr, "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n" " -[cmr] param=value ... [command=command ...]\n" - " jail [-dqv] [-f file] -[cmr] [jail]\n" + " jail [-dqv] [-f file] [-e separator] -[cmr] [jail]\n" " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n" " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n" " [-n jailname] [-s securelevel]\n" diff --git a/usr.sbin/jail/jailp.h b/usr.sbin/jail/jailp.h index 399d0b88695..ba717df198b 100644 --- a/usr.sbin/jail/jailp.h +++ b/usr.sbin/jail/jailp.h @@ -67,6 +67,7 @@ #define JF_TIMEOUT 0x0200 /* A command (or process kill) timed out */ #define JF_SLEEPQ 0x0400 /* Waiting on a command and/or timeout */ #define JF_FROM_RUNQ 0x0800 /* Has already been on the run queue */ +#define JF_SHOW 0x1000 /* -e Exhibit list of configured jails */ #define JF_OP_MASK (JF_START | JF_SET | JF_STOP) #define JF_RESTART (JF_START | JF_STOP) -- 2.45.0