1 /*- 2 * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org> 3 * Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org> 4 * Copyright (c) 2009 James Gritton <jamie@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/jail.h> 34 #include <sys/socket.h> 35 #include <sys/sysctl.h> 36 37 #include <arpa/inet.h> 38 #include <netinet/in.h> 39 40 #include <err.h> 41 #include <errno.h> 42 #include <jail.h> 43 #include <limits.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #define JP_USER 0x01000000 50 #define JP_OPT 0x02000000 51 52 #define PRINT_DEFAULT 0x01 53 #define PRINT_HEADER 0x02 54 #define PRINT_NAMEVAL 0x04 55 #define PRINT_QUOTED 0x08 56 #define PRINT_SKIP 0x10 57 #define PRINT_VERBOSE 0x20 58 #define PRINT_JAIL_NAME 0x40 59 60 static struct jailparam *params; 61 static int *param_parent; 62 static int nparams; 63 #ifdef INET6 64 static int ip6_ok; 65 #endif 66 #ifdef INET 67 static int ip4_ok; 68 #endif 69 70 static int add_param(const char *name, void *value, size_t valuelen, 71 struct jailparam *source, unsigned flags); 72 static int sort_param(const void *a, const void *b); 73 static char *noname(const char *name); 74 static char *nononame(const char *name); 75 static int print_jail(int pflags, int jflags); 76 static void quoted_print(char *str); 77 78 int 79 main(int argc, char **argv) 80 { 81 char *dot, *ep, *jname, *pname; 82 int c, i, jflags, jid, lastjid, pflags, spc; 83 84 jname = NULL; 85 pflags = jflags = jid = 0; 86 while ((c = getopt(argc, argv, "adj:hNnqsv")) >= 0) 87 switch (c) { 88 case 'a': 89 case 'd': 90 jflags |= JAIL_DYING; 91 break; 92 case 'j': 93 jid = strtoul(optarg, &ep, 10); 94 if (!jid || *ep) { 95 jid = 0; 96 jname = optarg; 97 } 98 break; 99 case 'h': 100 pflags = (pflags & ~(PRINT_SKIP | PRINT_VERBOSE)) | 101 PRINT_HEADER; 102 break; 103 case 'N': 104 pflags |= PRINT_JAIL_NAME; 105 break; 106 case 'n': 107 pflags = (pflags & ~PRINT_VERBOSE) | PRINT_NAMEVAL; 108 break; 109 case 'q': 110 pflags |= PRINT_QUOTED; 111 break; 112 case 's': 113 pflags = (pflags & ~(PRINT_HEADER | PRINT_VERBOSE)) | 114 PRINT_NAMEVAL | PRINT_QUOTED | PRINT_SKIP; 115 break; 116 case 'v': 117 pflags = (pflags & 118 ~(PRINT_HEADER | PRINT_NAMEVAL | PRINT_SKIP)) | 119 PRINT_VERBOSE; 120 break; 121 default: 122 errx(1, "usage: jls [-dhNnqv] [-j jail] [param ...]"); 123 } 124 125 #ifdef INET6 126 ip6_ok = feature_present("inet6"); 127 #endif 128 #ifdef INET 129 ip4_ok = feature_present("inet"); 130 #endif 131 132 /* Add the parameters to print. */ 133 if (optind == argc) { 134 if (pflags & (PRINT_HEADER | PRINT_NAMEVAL)) 135 add_param("all", NULL, (size_t)0, NULL, JP_USER); 136 else if (pflags & PRINT_VERBOSE) { 137 add_param("jid", NULL, (size_t)0, NULL, JP_USER); 138 add_param("host.hostname", NULL, (size_t)0, NULL, 139 JP_USER); 140 add_param("path", NULL, (size_t)0, NULL, JP_USER); 141 add_param("name", NULL, (size_t)0, NULL, JP_USER); 142 add_param("dying", NULL, (size_t)0, NULL, JP_USER); 143 add_param("cpuset.id", NULL, (size_t)0, NULL, JP_USER); 144 #ifdef INET 145 if (ip4_ok) 146 add_param("ip4.addr", NULL, (size_t)0, NULL, 147 JP_USER); 148 #endif 149 #ifdef INET6 150 if (ip6_ok) 151 add_param("ip6.addr", NULL, (size_t)0, NULL, 152 JP_USER | JP_OPT); 153 #endif 154 } else { 155 pflags |= PRINT_DEFAULT; 156 if (pflags & PRINT_JAIL_NAME) 157 add_param("name", NULL, (size_t)0, NULL, JP_USER); 158 else 159 add_param("jid", NULL, (size_t)0, NULL, JP_USER); 160 #ifdef INET 161 if (ip4_ok) 162 add_param("ip4.addr", NULL, (size_t)0, NULL, 163 JP_USER); 164 #endif 165 add_param("host.hostname", NULL, (size_t)0, NULL, 166 JP_USER); 167 add_param("path", NULL, (size_t)0, NULL, JP_USER); 168 } 169 } else { 170 pflags &= ~PRINT_VERBOSE; 171 while (optind < argc) 172 add_param(argv[optind++], NULL, (size_t)0, NULL, 173 JP_USER); 174 } 175 176 if (pflags & PRINT_SKIP) { 177 /* Check for parameters with jailsys parents. */ 178 for (i = 0; i < nparams; i++) { 179 if ((params[i].jp_flags & JP_USER) && 180 (dot = strchr(params[i].jp_name, '.'))) { 181 pname = alloca((dot - params[i].jp_name) + 1); 182 strlcpy(pname, params[i].jp_name, 183 (dot - params[i].jp_name) + 1); 184 param_parent[i] = add_param(pname, 185 NULL, (size_t)0, NULL, JP_OPT); 186 } 187 } 188 } 189 190 /* Add the index key parameters. */ 191 if (jid != 0) 192 add_param("jid", &jid, sizeof(jid), NULL, 0); 193 else if (jname != NULL) 194 add_param("name", jname, strlen(jname), NULL, 0); 195 else 196 add_param("lastjid", &lastjid, sizeof(lastjid), NULL, 0); 197 198 /* Print a header line if requested. */ 199 if (pflags & PRINT_VERBOSE) 200 printf(" JID Hostname Path\n" 201 " Name State\n" 202 " CPUSetID\n" 203 " IP Address(es)\n"); 204 else if (pflags & PRINT_DEFAULT) 205 if (pflags & PRINT_JAIL_NAME) 206 printf(" JID IP Address " 207 "Hostname Path\n"); 208 else 209 printf(" JID IP Address " 210 "Hostname Path\n"); 211 else if (pflags & PRINT_HEADER) { 212 for (i = spc = 0; i < nparams; i++) 213 if (params[i].jp_flags & JP_USER) { 214 if (spc) 215 putchar(' '); 216 else 217 spc = 1; 218 fputs(params[i].jp_name, stdout); 219 } 220 putchar('\n'); 221 } 222 223 /* Fetch the jail(s) and print the paramters. */ 224 if (jid != 0 || jname != NULL) { 225 if (print_jail(pflags, jflags) < 0) 226 errx(1, "%s", jail_errmsg); 227 } else { 228 for (lastjid = 0; 229 (lastjid = print_jail(pflags, jflags)) >= 0; ) 230 ; 231 if (errno != 0 && errno != ENOENT) 232 errx(1, "%s", jail_errmsg); 233 } 234 235 return (0); 236 } 237 238 static int 239 add_param(const char *name, void *value, size_t valuelen, 240 struct jailparam *source, unsigned flags) 241 { 242 struct jailparam *param, *tparams; 243 int i, tnparams; 244 245 static int paramlistsize; 246 247 /* The pseudo-parameter "all" scans the list of available parameters. */ 248 if (!strcmp(name, "all")) { 249 tnparams = jailparam_all(&tparams); 250 if (tnparams < 0) 251 errx(1, "%s", jail_errmsg); 252 qsort(tparams, (size_t)tnparams, sizeof(struct jailparam), 253 sort_param); 254 for (i = 0; i < tnparams; i++) 255 add_param(tparams[i].jp_name, NULL, (size_t)0, 256 tparams + i, flags); 257 free(tparams); 258 return -1; 259 } 260 261 /* Check for repeat parameters. */ 262 for (i = 0; i < nparams; i++) 263 if (!strcmp(name, params[i].jp_name)) { 264 if (value != NULL && jailparam_import_raw(params + i, 265 value, valuelen) < 0) 266 errx(1, "%s", jail_errmsg); 267 params[i].jp_flags |= flags; 268 if (source != NULL) 269 jailparam_free(source, 1); 270 return i; 271 } 272 273 /* Make sure there is room for the new param record. */ 274 if (!nparams) { 275 paramlistsize = 32; 276 params = malloc(paramlistsize * sizeof(*params)); 277 param_parent = malloc(paramlistsize * sizeof(*param_parent)); 278 if (params == NULL || param_parent == NULL) 279 err(1, "malloc"); 280 } else if (nparams >= paramlistsize) { 281 paramlistsize *= 2; 282 params = realloc(params, paramlistsize * sizeof(*params)); 283 param_parent = realloc(param_parent, 284 paramlistsize * sizeof(*param_parent)); 285 if (params == NULL || param_parent == NULL) 286 err(1, "realloc"); 287 } 288 289 /* Look up the parameter. */ 290 param_parent[nparams] = -1; 291 param = params + nparams++; 292 if (source != NULL) { 293 *param = *source; 294 param->jp_flags |= flags; 295 return param - params; 296 } 297 if (jailparam_init(param, name) < 0 || 298 (value != NULL ? jailparam_import_raw(param, value, valuelen) 299 : jailparam_import(param, value)) < 0) { 300 if (flags & JP_OPT) { 301 nparams--; 302 return (-1); 303 } 304 errx(1, "%s", jail_errmsg); 305 } 306 param->jp_flags = flags; 307 return param - params; 308 } 309 310 static int 311 sort_param(const void *a, const void *b) 312 { 313 const struct jailparam *parama, *paramb; 314 char *ap, *bp; 315 316 /* Put top-level parameters first. */ 317 parama = a; 318 paramb = b; 319 ap = strchr(parama->jp_name, '.'); 320 bp = strchr(paramb->jp_name, '.'); 321 if (ap && !bp) 322 return (1); 323 if (bp && !ap) 324 return (-1); 325 return (strcmp(parama->jp_name, paramb->jp_name)); 326 } 327 328 static char * 329 noname(const char *name) 330 { 331 char *nname, *p; 332 333 nname = malloc(strlen(name) + 3); 334 if (nname == NULL) 335 err(1, "malloc"); 336 p = strrchr(name, '.'); 337 if (p != NULL) 338 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1); 339 else 340 sprintf(nname, "no%s", name); 341 return nname; 342 } 343 344 static char * 345 nononame(const char *name) 346 { 347 char *nname, *p; 348 349 p = strrchr(name, '.'); 350 if (strncmp(p ? p + 1 : name, "no", 2)) 351 return NULL; 352 nname = malloc(strlen(name) - 1); 353 if (nname == NULL) 354 err(1, "malloc"); 355 if (p != NULL) 356 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3); 357 else 358 strcpy(nname, name + 2); 359 return nname; 360 } 361 362 static int 363 print_jail(int pflags, int jflags) 364 { 365 char *nname; 366 char **param_values; 367 int i, ai, jid, count, n, spc; 368 char ipbuf[INET6_ADDRSTRLEN]; 369 370 jid = jailparam_get(params, nparams, jflags); 371 if (jid < 0) 372 return jid; 373 if (pflags & PRINT_VERBOSE) { 374 printf("%6d %-29.29s %.74s\n" 375 "%6s %-29.29s %.74s\n" 376 "%6s %-6d\n", 377 *(int *)params[0].jp_value, 378 (char *)params[1].jp_value, 379 (char *)params[2].jp_value, 380 "", 381 (char *)params[3].jp_value, 382 *(int *)params[4].jp_value ? "DYING" : "ACTIVE", 383 "", 384 *(int *)params[5].jp_value); 385 n = 6; 386 #ifdef INET 387 if (ip4_ok && !strcmp(params[n].jp_name, "ip4.addr")) { 388 count = params[n].jp_valuelen / sizeof(struct in_addr); 389 for (ai = 0; ai < count; ai++) 390 if (inet_ntop(AF_INET, 391 &((struct in_addr *)params[n].jp_value)[ai], 392 ipbuf, sizeof(ipbuf)) == NULL) 393 err(1, "inet_ntop"); 394 else 395 printf("%6s %-15.15s\n", "", ipbuf); 396 n++; 397 } 398 #endif 399 #ifdef INET6 400 if (ip6_ok && !strcmp(params[n].jp_name, "ip6.addr")) { 401 count = params[n].jp_valuelen / sizeof(struct in6_addr); 402 for (ai = 0; ai < count; ai++) 403 if (inet_ntop(AF_INET6, 404 &((struct in6_addr *) 405 params[n].jp_value)[ai], 406 ipbuf, sizeof(ipbuf)) == NULL) 407 err(1, "inet_ntop"); 408 else 409 printf("%6s %s\n", "", ipbuf); 410 n++; 411 } 412 #endif 413 } else if (pflags & PRINT_DEFAULT) { 414 if (pflags & PRINT_JAIL_NAME) 415 printf(" %-15s ", (char *)params[0].jp_value); 416 else 417 printf("%6d ", *(int *)params[0].jp_value); 418 printf("%-15.15s %-29.29s %.74s\n", 419 #ifdef INET 420 (!ip4_ok || params[1].jp_valuelen == 0) ? "-" 421 : inet_ntoa(*(struct in_addr *)params[1].jp_value), 422 (char *)params[2-!ip4_ok].jp_value, 423 (char *)params[3-!ip4_ok].jp_value); 424 #else 425 "-", 426 (char *)params[1].jp_value, 427 (char *)params[2].jp_value); 428 #endif 429 } else { 430 param_values = alloca(nparams * sizeof(*param_values)); 431 for (i = 0; i < nparams; i++) { 432 if (!(params[i].jp_flags & JP_USER)) 433 continue; 434 param_values[i] = jailparam_export(params + i); 435 if (param_values[i] == NULL) 436 errx(1, "%s", jail_errmsg); 437 } 438 for (i = spc = 0; i < nparams; i++) { 439 if (!(params[i].jp_flags & JP_USER)) 440 continue; 441 if ((pflags & PRINT_SKIP) && 442 ((!(params[i].jp_ctltype & 443 (CTLFLAG_WR | CTLFLAG_TUN))) || 444 (param_parent[i] >= 0 && 445 *(int *)params[param_parent[i]].jp_value != 446 JAIL_SYS_NEW))) 447 continue; 448 if (spc) 449 putchar(' '); 450 else 451 spc = 1; 452 if (pflags & PRINT_NAMEVAL) { 453 /* 454 * Generally "name=value", but for booleans 455 * either "name" or "noname". 456 */ 457 if (params[i].jp_flags & 458 (JP_BOOL | JP_NOBOOL)) { 459 if (*(int *)params[i].jp_value) 460 printf("%s", params[i].jp_name); 461 else { 462 nname = (params[i].jp_flags & 463 JP_NOBOOL) ? 464 nononame(params[i].jp_name) 465 : noname(params[i].jp_name); 466 printf("%s", nname); 467 free(nname); 468 } 469 continue; 470 } 471 printf("%s=", params[i].jp_name); 472 } 473 if (params[i].jp_valuelen == 0) { 474 if (pflags & PRINT_QUOTED) 475 printf("\"\""); 476 else if (!(pflags & PRINT_NAMEVAL)) 477 putchar('-'); 478 } else 479 quoted_print(param_values[i]); 480 } 481 putchar('\n'); 482 for (i = 0; i < nparams; i++) 483 if (params[i].jp_flags & JP_USER) 484 free(param_values[i]); 485 } 486 return (jid); 487 } 488 489 static void 490 quoted_print(char *str) 491 { 492 int c, qc; 493 char *p = str; 494 495 /* An empty string needs quoting. */ 496 if (!*p) { 497 fputs("\"\"", stdout); 498 return; 499 } 500 501 /* 502 * The value will be surrounded by quotes if it contains spaces 503 * or quotes. 504 */ 505 qc = strchr(p, '\'') ? '"' 506 : strchr(p, '"') ? '\'' 507 : strchr(p, ' ') || strchr(p, '\t') ? '"' 508 : 0; 509 if (qc) 510 putchar(qc); 511 while ((c = *p++)) { 512 if (c == '\\' || c == qc) 513 putchar('\\'); 514 putchar(c); 515 } 516 if (qc) 517 putchar(qc); 518 } 519