Add [-J jid_file] option to write out a JidFile, similar to a PidFile,

containing the jailid, path, hostname, ip and the command used to start
the jail.

PR:		misc/89883
Submitted by:	L. Jason Godsey <lannygodsey -at- yahoo.com>
Reviewed by:	phk
MFC after:	1 week
This commit is contained in:
Philip Paeps 2005-12-03 17:32:39 +00:00
parent 3a721a33cf
commit cdafc85119
2 changed files with 30 additions and 6 deletions

View File

@ -42,6 +42,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl i
.Op Fl J Ar jid_file
.Op Fl l u Ar username | Fl U Ar username
.Ar path hostname ip-number command ...
.Sh DESCRIPTION
@ -53,6 +54,9 @@ The options are as follows:
.Bl -tag -width ".Fl u Ar username"
.It Fl i
Output the jail identifier of the newly created jail.
.It Fl J Ar jid_file
Write a JidFile, like a PidFile, containing jailid, path, hostname, ip and
command used to start the jail.
.It Fl l
Run program in the clean environment.
The environment is discarded except for

View File

@ -54,19 +54,25 @@ main(int argc, char **argv)
struct passwd *pwd = NULL;
struct in_addr in;
gid_t groups[NGROUPS];
int ch, i, iflag, lflag, ngroups, uflag, Uflag;
char path[PATH_MAX], *username;
int ch, i, iflag, Jflag, lflag, ngroups, uflag, Uflag;
char path[PATH_MAX], *username, *JidFile;
static char *cleanenv;
const char *shell, *p = NULL;
FILE *fp;
iflag = lflag = uflag = Uflag = 0;
username = cleanenv = NULL;
iflag = Jflag = lflag = uflag = Uflag = 0;
username = JidFile = cleanenv = NULL;
fp = NULL;
while ((ch = getopt(argc, argv, "ilu:U:")) != -1) {
while ((ch = getopt(argc, argv, "ilu:U:J:")) != -1) {
switch (ch) {
case 'i':
iflag = 1;
break;
case 'J':
JidFile = optarg;
Jflag = 1;
break;
case 'u':
username = optarg;
uflag = 1;
@ -103,6 +109,11 @@ main(int argc, char **argv)
if (inet_aton(argv[2], &in) == 0)
errx(1, "Could not make sense of ip-number: %s", argv[2]);
j.ip_number = ntohl(in.s_addr);
if (Jflag) {
fp = fopen(JidFile, "w");
if (fp == NULL)
errx(1, "Could not create JidFile: %s", JidFile);
}
i = jail(&j);
if (i == -1)
err(1, "jail");
@ -110,6 +121,15 @@ main(int argc, char **argv)
printf("%d\n", i);
fflush(stdout);
}
if (Jflag) {
if (fp != NULL) {
fprintf(fp, "%d\t%s\t%s\t%s\t%s\n",
i, j.path, j.hostname, argv[2], argv[3]);
(void)fclose(fp);
} else {
errx(1, "Could not write JidFile: %s", JidFile);
}
}
if (username != NULL) {
if (Uflag)
GET_USER_INFO;
@ -149,7 +169,7 @@ usage(void)
{
(void)fprintf(stderr, "%s%s\n",
"usage: jail [-i] [-l -u username | -U username]",
"usage: jail [-i] [-J jid_file] [-l -u username | -U username]",
" path hostname ip-number command ...");
exit(1);
}