From b03aca8204af897b0b35365f84f03bb934dda94d Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 29 May 2008 17:00:01 +0000 Subject: Fix some bugs/complaints: - make addr2jid static - add -h Flag for hostname/ip-number search - s,strncmp,strcmp, in addr2jid - return jid only if found once Requested by: some --- usr.sbin/jexec/jexec.8 | 11 +++++------ usr.sbin/jexec/jexec.c | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 22 deletions(-) (limited to 'usr.sbin/jexec') diff --git a/usr.sbin/jexec/jexec.8 b/usr.sbin/jexec/jexec.8 index 2df0f60..40c4979 100644 --- a/usr.sbin/jexec/jexec.8 +++ b/usr.sbin/jexec/jexec.8 @@ -34,7 +34,7 @@ .Sh SYNOPSIS .Nm .Op Fl u Ar username | Fl U Ar username -.Op Ar jid | Ar hostname | Ar ip-number +.Op Fl h Ar hostname | Fl h Ar ip-number | Ar jid .Ar command ... .Sh DESCRIPTION The @@ -42,11 +42,11 @@ The utility executes .Ar command inside the jail identified by -.Ar jid -or .Ar hostname or -.Ar ip-number . +.Ar ip-number +or +.Ar jid . .Pp The following options are available: .Bl -tag -width indent @@ -61,8 +61,7 @@ should run. .El .Sh "CAUTIONS" Only jid is guaranteed to uniquely identify a jail. -For hostname or ip-number to work here unambiguousness of one or both -of them have to be assured beforehand. +Hostname or ip-number only work here if matched to one unique jail. .Sh SEE ALSO .Xr jail_attach 2 , .Xr jail 8 , diff --git a/usr.sbin/jexec/jexec.c b/usr.sbin/jexec/jexec.c index 5b67da3..137eec3 100644 --- a/usr.sbin/jexec/jexec.c +++ b/usr.sbin/jexec/jexec.c @@ -43,7 +43,7 @@ #include static void usage(void); -int addr2jid(const char *addr); +static int addr2jid(const char *addr); #define GET_USER_INFO do { \ pwd = getpwnam(username); \ @@ -68,12 +68,12 @@ main(int argc, char *argv[]) login_cap_t *lcap = NULL; struct passwd *pwd = NULL; gid_t groups[NGROUPS]; - int ch, ngroups, uflag, Uflag; + int ch, ngroups, uflag, Uflag, hflag; char *username; - ch = uflag = Uflag = 0; + ch = uflag = Uflag = hflag = 0; username = NULL; - while ((ch = getopt(argc, argv, "u:U:")) != -1) { + while ((ch = getopt(argc, argv, "u:U:h")) != -1) { switch (ch) { case 'u': username = optarg; @@ -83,6 +83,9 @@ main(int argc, char *argv[]) username = optarg; Uflag = 1; break; + case 'h': + hflag = 1; + break; default: usage(); } @@ -95,10 +98,13 @@ main(int argc, char *argv[]) usage(); if (uflag) GET_USER_INFO; - jid = (int)strtol(argv[0], NULL, 10); - if (jail_attach(jid) == -1) - if (jail_attach(addr2jid(argv[0])) == -1) + if (hflag) { + if ((jid = addr2jid(argv[0])) == 0) errx(1, "jail_attach(): Cannot convert %s to jid", argv[0]); + } else + jid = (int)strtol(argv[0], NULL, 10); + if (jail_attach(jid) == -1) + err(1, "jail_attach(): %d", jid); if (chdir("/") == -1) err(1, "chdir(): /"); if (username != NULL) { @@ -124,16 +130,18 @@ usage(void) fprintf(stderr, "%s%s\n", "usage: jexec [-u username | -U username]", - " [jid | hostname | ip-number] command ..."); + " [-h hostname | -h ip-number | jid] command ..."); exit(1); } -int +static int addr2jid(const char *addr) { struct xprison *sxp, *xp; struct in_addr in; - size_t i, len, slen; + size_t i, len; + int jid, cnt; + jid = cnt = 0; if (sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1) err(1, "sysctlbyname(): security.jail.list"); @@ -158,16 +166,18 @@ addr2jid(const char *addr) if (len < sizeof(*xp) || len % sizeof(*xp) || xp->pr_version != XPRISON_VERSION) errx(1, "Kernel and userland out of sync"); - slen = strlen(addr); for (i = 0; i < len / sizeof(*xp); i++) { in.s_addr = ntohl(xp->pr_ip); - if ((strncmp(inet_ntoa(in), addr, slen) == 0) || - (strncmp(xp->pr_host, addr, slen) == 0)) { - free(sxp); - return (xp->pr_id); + if ((strcmp(inet_ntoa(in), addr) == 0) || + (strcmp(xp->pr_host, addr) == 0)) { + jid = xp->pr_id; + cnt++; } xp++; } free(sxp); - return 0; + if (cnt == 1) + return (jid); + else + return(0); } -- cgit v1.1