summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2005-02-19 22:15:42 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2005-02-19 22:15:42 +0000
commitfdc8e2faecab97e8254c499e8796f45862249f36 (patch)
treecb40f32b8bfba6794c08e519257a69624a76601e /usr.bin
parent625676310955739103d1c1250674305997787015 (diff)
Add getcap(1), a simple tool to query getcap(3)-style databases.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/Makefile10
-rw-r--r--usr.bin/getcap/Makefile5
-rw-r--r--usr.bin/getcap/getcap.1184
-rw-r--r--usr.bin/getcap/getcap.c184
4 files changed, 378 insertions, 5 deletions
diff --git a/usr.bin/Makefile b/usr.bin/Makefile
index 845a35ee6e4..5d5794dbfb8 100644
--- a/usr.bin/Makefile
+++ b/usr.bin/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.90 2004/05/02 17:55:09 millert Exp $
+# $OpenBSD: Makefile,v 1.91 2005/02/19 22:15:41 millert Exp $
.include <bsd.own.mk>
@@ -7,10 +7,10 @@ SUBDIR= apply apropos arch asa asn1_compile at aucat audioctl awk banner \
biff cal calendar cap_mkdb cdio checknr chpass cmp col \
colcrt colrm column comm compile_et compress cpp crontab ctags cut \
dc deroff diff diff3 dirname du elf2olf encrypt env expand false file \
- file2c find fgen finger fmt fold from fsplit fstat ftp gencat getconf \
- getopt gprof grep head hexdump id indent infocmp ipcrm ipcs join jot \
- kdump keynote ktrace lam last lastcomm leave less lex lndir locate \
- lock logger login logname look lorder m4 mail make man mesg mg \
+ file2c find fgen finger fmt fold from fsplit fstat ftp gencat getcap \
+ getconf getopt gprof grep head hexdump id indent infocmp ipcrm ipcs \
+ join jot kdump keynote ktrace lam last lastcomm leave less lex lndir \
+ locate lock logger login logname look lorder m4 mail make man mesg mg \
midiplay mixerctl mkdep mkstr mktemp modstat msgs nc netstat newsyslog \
nfsstat nice nm nohup oldrdist pagesize passwd paste patch pctr pkill \
pmdb pr printenv printf quota radioctl rdist rdistd readlink renice \
diff --git a/usr.bin/getcap/Makefile b/usr.bin/getcap/Makefile
new file mode 100644
index 00000000000..84b47bf8e6d
--- /dev/null
+++ b/usr.bin/getcap/Makefile
@@ -0,0 +1,5 @@
+# $OpenBSD: Makefile,v 1.1 2005/02/19 22:15:41 millert Exp $
+
+PROG= getcap
+
+.include <bsd.prog.mk>
diff --git a/usr.bin/getcap/getcap.1 b/usr.bin/getcap/getcap.1
new file mode 100644
index 00000000000..f01ea06162d
--- /dev/null
+++ b/usr.bin/getcap/getcap.1
@@ -0,0 +1,184 @@
+.\" $OpenBSD: getcap.1,v 1.1 2005/02/19 22:15:41 millert Exp $
+.\"
+.\" Copyright (c) 2005 Todd C. Miller <Todd.Miller@courtesan.com>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd February 18, 2005
+.Dt GETCAP 1
+.Os
+.Sh NAME
+.Nm getcap
+.Nd capability database access utility
+.Sh SYNOPSIS
+.Nm getcap
+.Oo
+.Fl b Ar boolean | Fl c Ar capability |
+.Fl n Ar number | Fl s Ar string
+.Oc
+.Fl a
+.Fl f Ar path
+.Nm getcap
+.Oo
+.Fl b Ar boolean | Fl c Ar capability |
+.Fl n Ar number | Fl s Ar string
+.Oc
+.Fl f Ar path
+record ...
+.Sh DESCRIPTION
+The
+.Nm
+utility provides a simple method of querying a capability database such as
+.Pa /etc/login.conf
+or
+.Pa /etc/printcap
+and as described by
+.Xr getcap 3 .
+.Pp
+The following query types are supported:
+.Bl -tag -width "capability"
+.It boolean
+A boolean flag.
+For each matching record, the name of the boolean capability is printed
+(since there is no value per se).
+.It number
+A numeric capability (key#number).
+For each matching record, the value of the numeric capability is printed
+as a decimal number.
+To print a number in the same format as it is listed in the
+capabilities database (i.e. literally), use a
+.Dq raw
+query instead.
+.It string
+A string capability (key=value).
+Note that the value printed has escape sequences expanded, see
+.Xr getcap 3
+for a list of valid escape sequences.
+To print the string exactly as it is listed in the
+capabilities database, use a
+.Dq raw
+query instead.
+.It raw
+A generic query where no interpretation of the data is done.
+Use this for querying capabilities with non-standard types or
+for getting the raw, unformatted entry from the database.
+.El
+.Pp
+.Nm
+prints the value associated with the give key.
+If more than one record is being searched, the value is prefixed
+with the record name (for records with multiple names, only the
+first is printed).
+If a key has no associated value and a boolean query is not being
+performed, a blank line will be printed.
+.Pp
+The options are as follows:
+.Bl -tag -width "-c capability"
+.It Fl a
+Query all records in the database.
+.It Fl b Ar boolean
+Return occurrences of
+.Ar boolean .
+.It Fl c Ar capability
+Return the raw value for the key
+.Ar capability ,
+which must include the type as the last character, e.g.
+.Dq foo=
+for the string
+.Sq foo .
+See
+.Xr getcap 3
+for more information on capability types.
+.It Fl f Ar path
+A colon-separated list of database filenames to be searched for records.
+.It Fl n Ar number
+Return occurrences of
+.Ar number .
+.It Fl s Ar string
+Return occurrences of the string
+.Ar string .
+.El
+.Pp
+Only one of the
+.Fl b ,
+.Fl c ,
+.Fl n
+and
+.Fl s
+options may be specified.
+If none are specified, the record will be dumped from the database as-is.
+.Sh FILES
+.Bl -tag -width /etc/login.conf -compact
+.It Pa /etc/login.conf
+login configuration
+.It Pa /etc/printcap
+printer configuration
+.It Pa /etc/termcap
+terminal configuration
+.El
+.Sh EXAMPLES
+Find all
+.Xr login.conf 5
+entries with the
+.Dq ignorenologin
+capability:
+.Pp
+.Dl "$ getcap -f /etc/login.conf -a -b ignorenologin"
+.Pp
+Dump the
+.Dq default
+record in
+.Xr login.conf 5 :
+.Pp
+.Dl "$ getcap -f /etc/login.conf default"
+.Pp
+Print all
+.Xr login.conf 5 :
+records with the
+.Dq localcipher
+capability:
+.Pp
+.Dl "$ getcap -f /etc/login.conf -a -s localcipher"
+.Pp
+Print the
+.Dq datasize-max
+capability in the
+.Dq staff
+record in
+.Xr login.conf 5
+if it exists:
+.Pp
+.Dl "$ getcap -f /etc/login.conf -s datasize-max staff"
+.Pp
+Print all
+.Xr printcap 5
+entries that reference
+.Pa /dev/lp .
+Note that we are treating the entire string
+.Dq lp=/dev/lp
+as a boolean flag instead of a key/value pair.
+.Pp
+.Dl "$ getcap -f /etc/printcap -a -b lp=/dev/lp"
+.Sh SEE ALSO
+.Xr cap_mkdb 1 ,
+.Xr getcap 3 ,
+.Xr login.conf 5 ,
+.Xr printcap 5 ,
+.Xr termcap 5
+.Sh HISTORY
+The
+.Nm
+command appeared in
+.Ox 3.7 .
+.Sh AUTHORS
+Todd C. Miller
diff --git a/usr.bin/getcap/getcap.c b/usr.bin/getcap/getcap.c
new file mode 100644
index 00000000000..4e90e0ec9ad
--- /dev/null
+++ b/usr.bin/getcap/getcap.c
@@ -0,0 +1,184 @@
+/* $OpenBSD: getcap.c,v 1.1 2005/02/19 22:15:41 millert Exp $ */
+
+/*
+ * Copyright (c) 2005 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef lint
+static const char rcsid[] = "$OpenBSD: getcap.c,v 1.1 2005/02/19 22:15:41 millert Exp $";
+#endif /* not lint */
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+enum captype {
+ boolean,
+ number,
+ string,
+ raw
+};
+
+void lookup_cap(char *, char *, enum captype, int);
+__dead void usage(void);
+
+int
+main(int argc, char *argv[])
+{
+ int ch, aflag;
+ enum captype type;
+ char *cp, *buf, *cap = NULL, **pathvec = NULL;
+ size_t n;
+
+ aflag = type = 0;
+ while ((ch = getopt(argc, argv, "ab:c:f:n:s:")) != -1) {
+ switch (ch) {
+ case 'a':
+ aflag = 1;
+ break;
+ case 'b':
+ if (*optarg == '\0')
+ usage();
+ cap = optarg;
+ type = boolean;
+ break;
+ case 'n':
+ if (*optarg == '\0')
+ usage();
+ cap = optarg;
+ type = number;
+ break;
+ case 's':
+ if (*optarg == '\0')
+ usage();
+ cap = optarg;
+ type = string;
+ break;
+ case 'c':
+ if (*optarg == '\0')
+ usage();
+ cap = optarg;
+ type = raw;
+ break;
+ case 'f':
+ if (pathvec != NULL)
+ errx(1, "only one -f option may be specified");
+ for (n = 1, cp = optarg; (cp = strchr(cp, ':')); n++)
+ continue;
+ pathvec = calloc(n + 1, sizeof(char *));
+ for (n = 0; (pathvec[n] = strsep(&optarg, ":"));) {
+ if (*pathvec[n] != '\0')
+ n++;
+ }
+ break;
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (pathvec == NULL) {
+ warnx("no path specified");
+ usage();
+ }
+ if (!aflag && !argc) {
+ warnx("must specify -a or a record name");
+ usage();
+ }
+
+ if (aflag) {
+ while (cgetnext(&buf, pathvec) > 0) {
+ lookup_cap(buf, cap, type, 1);
+ free(buf);
+ }
+ } else {
+ while (*argv != NULL) {
+ if (cgetent(&buf, pathvec, *argv) != 0)
+ errx(1, "unable to lookup %s", *argv); /* XXX */
+ lookup_cap(buf, cap, type, argc > 1);
+ free(buf);
+ argv++;
+ }
+ }
+ exit(0);
+}
+
+void
+lookup_cap(char *buf, char *cap, enum captype type, int useprefix)
+{
+ char *cp, *endp;
+ long l;
+ int ch, n, prefixlen;
+
+ if (cap == NULL) {
+ puts(buf);
+ return;
+ }
+
+ prefixlen = useprefix ? strcspn(buf, "|:") : 0;
+
+ switch (type) {
+ case boolean:
+ if (cgetcap(buf, cap, ':') == NULL)
+ return;
+ printf("%.*s%s%s\n", prefixlen, buf,
+ useprefix ? ": " : "", cap);
+ break;
+ case number:
+ if (cgetnum(buf, cap, &l) == -1)
+ return;
+ printf("%.*s%s%ld\n", prefixlen, buf,
+ useprefix ? ": " : "", l);
+ break;
+ case string:
+ if ((n = cgetstr(buf, cap, &cp)) == -1)
+ return;
+ else if (n == -2)
+ err(1, NULL); /* ENOMEM */
+ if (cgetstr(buf, cap, &cp) < 0)
+ return;
+ printf("%.*s%s%s\n", prefixlen, buf,
+ useprefix ? ": " : "", cp);
+ break;
+ case raw:
+ n = strlen(cap) - 1;
+ ch = cap[n];
+ cap[n] = '\0';
+ cp = cgetcap(buf, cap, ch);
+ cap[n] = ch;
+ if (cp != NULL) {
+ if ((endp = strchr(cp, ':')) != NULL)
+ printf("%.*s%s%.*s\n", prefixlen, buf,
+ useprefix ? ": " : "", endp - cp, cp);
+ else
+ printf("%.*s%s%s\n", prefixlen, buf,
+ useprefix ? ": " : "", cp);
+ }
+ break;
+ }
+}
+
+__dead void
+usage(void)
+{
+ extern char *__progname;
+
+ fprintf(stderr, "usage: %s [-b boolean | -c capability | -n number | -s string] -a -f path\n"
+ " %s [-b boolean | -c capability | -n number | -s string] -f path record ...\n",
+ __progname, __progname);
+ exit(1);
+}