From 6d8ce32cbfa3afc88183af0233998dc77bb4b0ed Mon Sep 17 00:00:00 2001 From: lukem Date: Mon, 29 Nov 2004 04:13:15 +0000 Subject: [PATCH] Implement "services" --- usr.bin/getent/getent.1 | 8 ++++-- usr.bin/getent/getent.c | 61 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/usr.bin/getent/getent.1 b/usr.bin/getent/getent.1 index cc744f89294b..6a1c805e9cf2 100644 --- a/usr.bin/getent/getent.1 +++ b/usr.bin/getent/getent.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: getent.1,v 1.6 2004/11/26 10:15:37 wiz Exp $ +.\" $NetBSD: getent.1,v 1.7 2004/11/29 04:13:15 lukem Exp $ .\" .\" Copyright (c) 2004 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -34,7 +34,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd November 26, 2004 +.Dd November 29, 2004 .Dt GETENT 1 .Os .Sh NAME @@ -63,10 +63,11 @@ may be one of: .Bl -column "netgroup" -offset indent -compact .Sy Database Ta Sy Display format .It group Ta group:passwd:gid:[member[,member]...] -.It hosts Ta address hostname [alias ...] +.It hosts Ta address name [alias ...] .It networks Ta name network [alias ...] .It passwd Ta user:passwd:uid:gid:gecos:home_dir_shell .It protocols Ta name protocol [alias ...] +.It services Ta name port/protocol [alias ...] .It shells Ta /path/to/shell .El .Pp @@ -104,6 +105,7 @@ or 3 if there is no support for enumeration on .Xr nsswitch.conf 5 , .Xr passwd 5 , .Xr protocols 5 , +.Xr services 5 , .Xr shells 5 .Sh HISTORY A diff --git a/usr.bin/getent/getent.c b/usr.bin/getent/getent.c index e57a30890d47..34e820125b1b 100644 --- a/usr.bin/getent/getent.c +++ b/usr.bin/getent/getent.c @@ -1,4 +1,4 @@ -/* $NetBSD: getent.c,v 1.3 2004/11/26 05:07:12 lukem Exp $ */ +/* $NetBSD: getent.c,v 1.4 2004/11/29 04:13:15 lukem Exp $ */ /*- * Copyright (c) 2004 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ #include #ifndef lint -__RCSID("$NetBSD: getent.c,v 1.3 2004/11/26 05:07:12 lukem Exp $"); +__RCSID("$NetBSD: getent.c,v 1.4 2004/11/29 04:13:15 lukem Exp $"); #endif /* not lint */ #include @@ -67,6 +67,7 @@ static int hosts(int, char *[]); static int networks(int, char *[]); static int passwd(int, char *[]); static int protocols(int, char *[]); +static int services(int, char *[]); static int shells(int, char *[]); enum { @@ -88,6 +89,7 @@ main(int argc, char *argv[]) { "networks", networks, }, { "passwd", passwd, }, { "protocols", protocols, }, + { "services", services, }, { "shells", shells, }, { NULL, NULL, }, @@ -413,6 +415,61 @@ protocols(int argc, char *argv[]) } + /* + * services + */ + +static void +servicesprint(const struct servent *se) +{ + int i; + + assert(se != NULL); + printf("%s\t%d/%s", se->s_name, ntohs(se->s_port), se->s_proto); + for (i = 0; se->s_aliases[i] != NULL; i++) { + printf(" %s", se->s_aliases[i]); + } + printf("\n"); +} + +static int +services(int argc, char *argv[]) +{ + struct servent *se; + unsigned long id; + char *proto; + int i, rv; + + assert(argc > 1); + assert(argv != NULL); + + setservent(1); + rv = RV_OK; + if (argc == 2) { + while ((se = getservent()) != NULL) + servicesprint(se); + } else { + for (i = 2; i < argc; i++) { + proto = strchr(argv[i], '/'); + if (proto != NULL) + *proto++ = '\0'; + if (parsenum(argv[i], &id)) + se = getservbyport((int)id, proto); + else + se = getservbyname(argv[i], proto); + if (se != NULL) + servicesprint(se); + else { + rv = RV_NOTFOUND; + break; + } + } + } + endservent(); + return rv; +} + + /* * shells */