]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nss/getent.c
nss_files: Add generic code for set*ent, end*ent and file open
[thirdparty/glibc.git] / nss / getent.c
index 02f2034efbbe333e3c10734576242c1cf40ac71b..ec48ba4bf1f5f788ad6bca0071150b4a736b3a1b 100644 (file)
@@ -1,41 +1,46 @@
-/* Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc.
+/* Copyright (c) 1998-2021 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA. */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
-/* getent: get entries from administrative database
-   supported databases: passwd, group, hosts, services, protocols
-   and networks */
+/* getent: get entries from administrative database.  */
 
+#include <aliases.h>
 #include <argp.h>
-#include <grp.h>
-#include <pwd.h>
 #include <ctype.h>
 #include <error.h>
+#include <grp.h>
+#include <gshadow.h>
 #include <libintl.h>
 #include <locale.h>
+#include <mcheck.h>
 #include <netdb.h>
+#include <pwd.h>
+#include <shadow.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
+#include <netinet/ether.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <scratch_buffer.h>
+#include <inttypes.h>
 
 /* Get libc version number.  */
 #include <version.h>
@@ -49,60 +54,167 @@ void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
 /* Short description of parameters.  */
 static const char args_doc[] = N_("database [key ...]");
 
+/* Supported options. */
+static const struct argp_option args_options[] =
+  {
+    { "service", 's', N_("CONFIG"), 0, N_("Service configuration to be used") },
+    { "no-idn", 'i', NULL, 0, N_("disable IDN encoding") },
+    { NULL, 0, NULL, 0, NULL },
+  };
+
 /* Short description of program.  */
-static const char doc[] =
-                N_("getent - get entries from administrative database.");
+static const char doc[] = N_("Get entries from administrative database.");
+
+/* Prototype for option handler.  */
+static error_t parse_option (int key, char *arg, struct argp_state *state);
+
+/* Function to print some extra text in the help message.  */
+static char *more_help (int key, const char *text, void *input);
 
 /* Data structure to communicate with argp functions.  */
-static struct argp argp = {
-  NULL, NULL, args_doc, doc,
-};
+static struct argp argp =
+  {
+    args_options, parse_option, args_doc, doc, NULL, more_help
+  };
+
+/* Additional getaddrinfo flags for IDN encoding.  */
+static int idn_flags = AI_IDN | AI_CANONIDN;
 
 /* Print the version information.  */
 static void
 print_version (FILE *stream, struct argp_state *state)
 {
-  fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
+  fprintf (stream, "getent %s%s\n", PKGVERSION, VERSION);
   fprintf (stream, gettext ("\
 Copyright (C) %s Free Software Foundation, Inc.\n\
 This is free software; see the source for copying conditions.  There is NO\n\
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
-"), "1999");
+"), "2021");
   fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
 }
 
-/* This is for group */
-static inline void
-print_group (struct group *grp)
+/* This is for aliases */
+static void
+print_aliases (struct aliasent *alias)
 {
   unsigned int i = 0;
 
-  printf ("%s:%s:%ld:", grp->gr_name ? grp->gr_name : "",
-         grp->gr_passwd ? grp->gr_passwd : "",
-         (unsigned long)grp->gr_gid);
+  printf ("%s: ", alias->alias_name);
+  for  (i = strlen (alias->alias_name); i < 14; ++i)
+    fputs_unlocked (" ", stdout);
 
-  while (grp->gr_mem[i] != NULL)
+  for (i = 0; i < alias->alias_members_len; ++i)
+    printf ("%s%s",
+           alias->alias_members [i],
+           i + 1 == alias->alias_members_len ? "\n" : ", ");
+}
+
+static int
+aliases_keys (int number, char *key[])
+{
+  int result = 0;
+  int i;
+  struct aliasent *alias;
+
+  if (number == 0)
     {
-      fputs (grp->gr_mem[i], stdout);
-      ++i;
-      if (grp->gr_mem[i] != NULL)
-       fputs (",", stdout);
+      setaliasent ();
+      while ((alias = getaliasent ()) != NULL)
+       print_aliases (alias);
+      endaliasent ();
+      return result;
+    }
+
+  for (i = 0; i < number; ++i)
+    {
+      alias = getaliasbyname (key[i]);
+
+      if (alias == NULL)
+       result = 2;
+      else
+       print_aliases (alias);
     }
-  fputs ("\n", stdout);
+
+  return result;
 }
 
-static inline int
+/* This is for ethers */
+static int
+ethers_keys (int number, char *key[])
+{
+  int result = 0;
+  int i;
+
+  if (number == 0)
+    {
+      fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
+      return 3;
+    }
+
+  for (i = 0; i < number; ++i)
+    {
+      struct ether_addr *ethp, eth;
+      char buffer [1024], *p;
+
+      ethp = ether_aton (key[i]);
+      if (ethp != NULL)
+       {
+         if (ether_ntohost (buffer, ethp))
+           {
+             result = 2;
+             continue;
+           }
+         p = buffer;
+       }
+      else
+       {
+         if (ether_hostton (key[i], &eth))
+           {
+             result = 2;
+             continue;
+           }
+         p = key[i];
+         ethp = &eth;
+       }
+      printf ("%s %s\n", ether_ntoa (ethp), p);
+    }
+
+  return result;
+}
+
+/* This is for group */
+static void
+print_group (struct group *grp)
+{
+  if (putgrent (grp, stdout) != 0)
+    fprintf (stderr, "error writing group entry: %m\n");
+}
+
+static int
 group_keys (int number, char *key[])
 {
   int result = 0;
   int i;
+  struct group *grp;
+
+  if (number == 0)
+    {
+      setgrent ();
+      while ((grp = getgrent ()) != NULL)
+       print_group (grp);
+      endgrent ();
+      return result;
+    }
 
   for (i = 0; i < number; ++i)
     {
-      struct group *grp;
+      errno = 0;
+      char *ep;
+      gid_t arg_gid = strtoul(key[i], &ep, 10);
 
-      if (isdigit (key[i][0]))
-       grp = getgrgid (atol (key[i]));
+      if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
+       /* Valid numeric gid.  */
+       grp = getgrgid (arg_gid);
       else
        grp = getgrnam (key[i]);
 
@@ -115,43 +227,370 @@ group_keys (int number, char *key[])
   return result;
 }
 
+/* This is for gshadow */
+static void
+print_gshadow (struct sgrp *sg)
+{
+  if (putsgent (sg, stdout) != 0)
+    fprintf (stderr, "error writing gshadow entry: %m\n");
+}
+
+static int
+gshadow_keys (int number, char *key[])
+{
+  int result = 0;
+  int i;
+
+  if (number == 0)
+    {
+      struct sgrp *sg;
+
+      setsgent ();
+      while ((sg = getsgent ()) != NULL)
+       print_gshadow (sg);
+      endsgent ();
+      return result;
+    }
+
+  for (i = 0; i < number; ++i)
+    {
+      struct sgrp *sg;
+
+      sg = getsgnam (key[i]);
+
+      if (sg == NULL)
+       result = 2;
+      else
+       print_gshadow (sg);
+    }
+
+  return result;
+}
+
+/* This is for hosts */
+static void
+print_hosts (struct hostent *host)
+{
+  unsigned int cnt;
+
+  for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
+    {
+      char buf[INET6_ADDRSTRLEN];
+      const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
+                                 buf, sizeof (buf));
+
+      printf ("%-15s %s", ip, host->h_name);
+
+      unsigned int i;
+      for (i = 0; host->h_aliases[i] != NULL; ++i)
+       {
+         putchar_unlocked (' ');
+         fputs_unlocked (host->h_aliases[i], stdout);
+       }
+      putchar_unlocked ('\n');
+    }
+}
+
+static int
+hosts_keys (int number, char *key[])
+{
+  int result = 0;
+  int i;
+  struct hostent *host;
+
+  if (number == 0)
+    {
+      sethostent (0);
+      while ((host = gethostent ()) != NULL)
+       print_hosts (host);
+      endhostent ();
+      return result;
+    }
+
+  for (i = 0; i < number; ++i)
+    {
+      struct hostent *host = NULL;
+      char addr[IN6ADDRSZ];
+
+      if (inet_pton (AF_INET6, key[i], &addr) > 0)
+       host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
+      else if (inet_pton (AF_INET, key[i], &addr) > 0)
+       host = gethostbyaddr (addr, INADDRSZ, AF_INET);
+      else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
+       host = gethostbyname2 (key[i], AF_INET);
+
+      if (host == NULL)
+       result = 2;
+      else
+       print_hosts (host);
+    }
+
+  return result;
+}
+
+/* This is for hosts, but using getaddrinfo */
+static int
+ahosts_keys_int (int af, int xflags, int number, char *key[])
+{
+  int result = 0;
+  int i;
+  struct hostent *host;
+
+  if (number == 0)
+    {
+      sethostent (0);
+      while ((host = gethostent ()) != NULL)
+       print_hosts (host);
+      endhostent ();
+      return result;
+    }
+
+  struct addrinfo hint;
+  memset (&hint, '\0', sizeof (hint));
+  hint.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME
+                  | idn_flags | xflags);
+  hint.ai_family = af;
+
+  for (i = 0; i < number; ++i)
+    {
+      struct addrinfo *res;
+
+      if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
+       result = 2;
+      else
+       {
+         struct addrinfo *runp = res;
+
+         while (runp != NULL)
+           {
+             char sockbuf[20];
+             const char *sockstr;
+             if (runp->ai_socktype == SOCK_STREAM)
+               sockstr = "STREAM";
+             else if (runp->ai_socktype == SOCK_DGRAM)
+               sockstr = "DGRAM";
+             else if (runp->ai_socktype == SOCK_RAW)
+               sockstr = "RAW";
+#ifdef SOCK_SEQPACKET
+             else if (runp->ai_socktype == SOCK_SEQPACKET)
+               sockstr = "SEQPACKET";
+#endif
+#ifdef SOCK_RDM
+             else if (runp->ai_socktype == SOCK_RDM)
+               sockstr = "RDM";
+#endif
+#ifdef SOCK_DCCP
+             else if (runp->ai_socktype == SOCK_DCCP)
+               sockstr = "DCCP";
+#endif
+#ifdef SOCK_PACKET
+             else if (runp->ai_socktype == SOCK_PACKET)
+               sockstr = "PACKET";
+#endif
+             else
+               {
+                 snprintf (sockbuf, sizeof (sockbuf), "%d",
+                           runp->ai_socktype);
+                 sockstr = sockbuf;
+               }
+
+             /* Three digits per byte, plus '%' and null terminator.  */
+             char scope[3 * sizeof (uint32_t) + 2];
+             struct sockaddr_in6 *addr6
+               = (struct sockaddr_in6 *) runp->ai_addr;
+             if (runp->ai_family != AF_INET6 || addr6->sin6_scope_id == 0)
+               /* No scope ID present.  */
+               scope[0] = '\0';
+             else
+               snprintf (scope, sizeof (scope), "%%%" PRIu32,
+                         addr6->sin6_scope_id);
+
+             char buf[INET6_ADDRSTRLEN];
+             if (inet_ntop (runp->ai_family,
+                            runp->ai_family == AF_INET
+                            ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
+                            : &addr6->sin6_addr,
+                            buf, sizeof (buf)) == NULL)
+               {
+                 strcpy (buf, "<invalid>");
+                 scope[0] = '\0';
+               }
+
+             int pad = 15 - strlen (buf) - strlen (scope);
+             if (pad < 0)
+               pad = 0;
+
+             printf ("%s%-*s %-6s %s\n",
+                     buf, pad, scope, sockstr, runp->ai_canonname ?: "");
+
+             runp = runp->ai_next;
+           }
+
+         freeaddrinfo (res);
+       }
+    }
+
+  return result;
+}
+
+static int
+ahosts_keys (int number, char *key[])
+{
+  return ahosts_keys_int (AF_UNSPEC, 0, number, key);
+}
+
+static int
+ahostsv4_keys (int number, char *key[])
+{
+  return ahosts_keys_int (AF_INET, 0, number, key);
+}
+
+static int
+ahostsv6_keys (int number, char *key[])
+{
+  return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
+}
+
+/* This is for netgroup */
+static int
+netgroup_keys (int number, char *key[])
+{
+  int result = 0;
+
+  if (number == 0)
+    {
+      fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
+      return 3;
+    }
+
+  if (number == 4)
+    {
+      char *host = strcmp (key[1], "*") == 0 ? NULL : key[1];
+      char *user = strcmp (key[2], "*") == 0 ? NULL : key[2];
+      char *domain = strcmp (key[3], "*") == 0 ? NULL : key[3];
+
+      printf ("%-21s (%s,%s,%s) = %d\n",
+             key[0], host ?: "", user ?: "", domain ?: "",
+             innetgr (key[0], host, user, domain));
+    }
+  else if (number == 1)
+    {
+      if (!setnetgrent (key[0]))
+       result = 2;
+      else
+       {
+         char *p[3];
+
+         printf ("%-21s", key[0]);
+
+         while (getnetgrent (p, p + 1, p + 2))
+           printf (" (%s,%s,%s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
+         putchar_unlocked ('\n');
+       }
+    }
+
+  endnetgrent ();
+
+  return result;
+}
+
+#define DYNARRAY_STRUCT gid_list
+#define DYNARRAY_ELEMENT gid_t
+#define DYNARRAY_PREFIX gid_list_
+#define DYNARRAY_INITIAL_SIZE 10
+#include <malloc/dynarray-skeleton.c>
+
+/* This is for initgroups */
+static int
+initgroups_keys (int number, char *key[])
+{
+  if (number == 0)
+    {
+      fprintf (stderr, _("Enumeration not supported on %s\n"), "initgroups");
+      return 3;
+    }
+
+  struct gid_list list;
+  gid_list_init (&list);
+  if (!gid_list_resize (&list, 10))
+    {
+      fprintf (stderr, _("Could not allocate group list: %m\n"));
+      return 3;
+    }
+
+  for (int i = 0; i < number; ++i)
+    {
+      int no = gid_list_size (&list);
+      int n;
+      while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1
+            && no > gid_list_size (&list))
+       {
+         if (!gid_list_resize (&list, no))
+           {
+             fprintf (stderr, _("Could not allocate group list: %m\n"));
+             return 3;
+           }
+       }
+
+      if (n == -1)
+       {
+         gid_list_free (&list);
+         return 1;
+       }
+
+      const gid_t *grps = gid_list_begin (&list);
+      printf ("%-21s", key[i]);
+      for (int j = 0; j < n; ++j)
+       if (grps[j] != -1)
+         printf (" %ld", (long int) grps[j]);
+      putchar_unlocked ('\n');
+    }
+
+  gid_list_free (&list);
+
+  return 0;
+}
+
 /* This is for networks */
-static inline void
+static void
 print_networks (struct netent *net)
 {
   unsigned int i;
   struct in_addr ip;
   ip.s_addr = htonl (net->n_net);
 
-  fputs (net->n_name, stdout);
-  for  (i = strlen (net->n_name); i < 22; ++i)
-    fputs (" ", stdout);
-  fputs (inet_ntoa (ip), stdout);
+  printf ("%-21s %s", net->n_name, inet_ntoa (ip));
 
   i = 0;
   while (net->n_aliases[i] != NULL)
     {
-      fputs (" ", stdout);
-      fputs (net->n_aliases[i], stdout);
+      putchar_unlocked (' ');
+      fputs_unlocked (net->n_aliases[i], stdout);
       ++i;
-      if (net->n_aliases[i] != NULL)
-       fputs (",", stdout);
     }
-  fputs ("\n", stdout);
+  putchar_unlocked ('\n');
 }
 
-static inline int
+static int
 networks_keys (int number, char *key[])
 {
   int result = 0;
   int i;
+  struct netent *net;
 
-  for (i = 0; i < number; ++i)
+  if (number == 0)
     {
-      struct netent *net;
+      setnetent (0);
+      while ((net = getnetent ()) != NULL)
+       print_networks (net);
+      endnetent ();
+      return result;
+    }
 
+  for (i = 0; i < number; ++i)
+    {
       if (isdigit (key[i][0]))
-       net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
+       net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC);
       else
        net = getnetbyname (key[i]);
 
@@ -165,31 +604,38 @@ networks_keys (int number, char *key[])
 }
 
 /* Now is all for passwd */
-static inline void
+static void
 print_passwd (struct passwd *pwd)
 {
-  printf ("%s:%s:%ld:%ld:%s:%s:%s\n",
-         pwd->pw_name ? pwd->pw_name : "",
-         pwd->pw_passwd ? pwd->pw_passwd : "",
-         (unsigned long)pwd->pw_uid,
-         (unsigned long)pwd->pw_gid,
-         pwd->pw_gecos ? pwd->pw_gecos : "",
-         pwd->pw_dir ? pwd->pw_dir : "",
-         pwd->pw_shell ? pwd->pw_shell : "");
+  if (putpwent (pwd, stdout) != 0)
+    fprintf (stderr, "error writing passwd entry: %m\n");
 }
 
-static inline int
+static int
 passwd_keys (int number, char *key[])
 {
   int result = 0;
   int i;
+  struct passwd *pwd;
+
+  if (number == 0)
+    {
+      setpwent ();
+      while ((pwd = getpwent ()) != NULL)
+       print_passwd (pwd);
+      endpwent ();
+      return result;
+    }
 
   for (i = 0; i < number; ++i)
     {
-      struct passwd *pwd;
+      errno = 0;
+      char *ep;
+      uid_t arg_uid = strtoul(key[i], &ep, 10);
 
-      if (isdigit (key[i][0]))
-       pwd = getpwuid (atol (key[i]));
+      if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
+       /* Valid numeric uid.  */
+       pwd = getpwuid (arg_uid);
       else
        pwd = getpwnam (key[i]);
 
@@ -203,36 +649,41 @@ passwd_keys (int number, char *key[])
 }
 
 /* This is for protocols */
-static inline void
+static void
 print_protocols (struct protoent *proto)
 {
   unsigned int i;
 
-  fputs (proto->p_name, stdout);
-  for (i = strlen (proto->p_name); i < 22; ++i)
-    fputs (" ", stdout);
-  printf ("%d", proto->p_proto);
+  printf ("%-21s %d", proto->p_name, proto->p_proto);
 
   i = 0;
   while (proto->p_aliases[i] != NULL)
     {
-      fputs (" ", stdout);
-      fputs (proto->p_aliases[i], stdout);
+      putchar_unlocked (' ');
+      fputs_unlocked (proto->p_aliases[i], stdout);
       ++i;
     }
-  fputs ("\n", stdout);
+  putchar_unlocked ('\n');
 }
 
-static inline int
+static int
 protocols_keys (int number, char *key[])
 {
   int result = 0;
   int i;
+  struct protoent *proto;
 
-  for (i = 0; i < number; ++i)
+  if (number == 0)
     {
-      struct protoent *proto;
+      setprotoent (0);
+      while ((proto = getprotoent ()) != NULL)
+       print_protocols (proto);
+      endprotoent ();
+      return result;
+    }
 
+  for (i = 0; i < number; ++i)
+    {
       if (isdigit (key[i][0]))
        proto = getprotobynumber (atol (key[i]));
       else
@@ -247,272 +698,303 @@ protocols_keys (int number, char *key[])
   return result;
 }
 
-/* This is for hosts */
-static inline void
-print_hosts (struct hostent *host)
+#if HAVE_SUNRPC
+/* Now is all for rpc */
+static void
+print_rpc (struct rpcent *rpc)
 {
-  unsigned int i;
-  char buf[INET6_ADDRSTRLEN];
-  const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[0],
-                             buf, sizeof (buf));
+  int i;
 
-  fputs (ip, stdout);
-  for (i = strlen (ip); i < 16; ++i)
-    fputs (" ", stdout);
-  fputs (host->h_name, stdout);
+  printf ("%-15s %d%s",
+         rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
 
-  i = 0;
-  while (host->h_aliases[i] != NULL)
-    {
-      fputs (" ", stdout);
-      fputs (host->h_aliases[i], stdout);
-      ++i;
-    }
-  fputs ("\n", stdout);
+  for (i = 0; rpc->r_aliases[i]; ++i)
+    printf (" %s", rpc->r_aliases[i]);
+  putchar_unlocked ('\n');
 }
 
-static inline int
-hosts_keys (int number, char *key[])
+static int
+rpc_keys (int number, char *key[])
 {
   int result = 0;
   int i;
+  struct rpcent *rpc;
 
-  for (i = 0; i < number; ++i)
+  if (number == 0)
     {
-      struct hostent *host = NULL;
+      setrpcent (0);
+      while ((rpc = getrpcent ()) != NULL)
+       print_rpc (rpc);
+      endrpcent ();
+      return result;
+    }
 
-      if (strchr (key[i], ':') != NULL)
-       {
-         char addr[IN6ADDRSZ];
-         if (inet_pton (AF_INET6, key[i], &addr))
-           host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
-       }
-      else if (isdigit (key[i][0]))
-       {
-         char addr[INADDRSZ];
-         if (inet_pton (AF_INET, key[i], &addr))
-           host = gethostbyaddr (addr, sizeof (addr), AF_INET);
-       }
-      else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
-       host = gethostbyname2 (key[i], AF_INET);
+  for (i = 0; i < number; ++i)
+    {
+      if (isdigit (key[i][0]))
+       rpc = getrpcbynumber (atol (key[i]));
+      else
+       rpc = getrpcbyname (key[i]);
 
-      if (host == NULL)
+      if (rpc == NULL)
        result = 2;
       else
-       print_hosts (host);
+       print_rpc (rpc);
     }
 
   return result;
 }
+#endif
 
 /* for services */
-static inline void
+static void
 print_services (struct servent *serv)
 {
   unsigned int i;
 
-  fputs (serv->s_name, stdout);
-  for (i = strlen (serv->s_name); i < 22; ++i)
-    fputs (" ", stdout);
-  printf ("%d/%s", ntohs (serv->s_port), serv->s_proto);
+  printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
 
   i = 0;
   while (serv->s_aliases[i] != NULL)
     {
-      fputs (" ", stdout);
-      fputs (serv->s_aliases[i], stdout);
+      putchar_unlocked (' ');
+      fputs_unlocked (serv->s_aliases[i], stdout);
       ++i;
     }
-  fputs ("\n", stdout);
+  putchar_unlocked ('\n');
 }
 
-static inline int
+static int
 services_keys (int number, char *key[])
 {
   int result = 0;
   int i;
+  struct servent *serv;
+
+  if (!number)
+    {
+      setservent (0);
+      while ((serv = getservent ()) != NULL)
+       print_services (serv);
+      endservent ();
+      return result;
+    }
 
   for (i = 0; i < number; ++i)
     {
       struct servent *serv;
       char *proto = strchr (key[i], '/');
 
-      if (proto == NULL)
-       {
-         setservent (0);
-         if (isdigit (key[i][0]))
-           {
-             int port = htons (atol (key[i]));
-             while ((serv = getservent ()) != NULL)
-               if (serv->s_port == port)
-                 {
-                   print_services (serv);
-                   break;
-                 }
-           }
-         else
-           {
-             while ((serv = getservent ()) != NULL)
-               if (strcmp (serv->s_name, key[i]) == 0)
-                 {
-                   print_services (serv);
-                   break;
-                 }
-           }
-         endservent ();
-       }
-      else
-       {
-         *proto++ = '\0';
+      if (proto != NULL)
+       *proto++ = '\0';
 
-         if (isdigit (key[i][0]))
-           serv = getservbyport (atol (key[i]), proto);
-         else
-           serv = getservbyname (key[i], proto);
+      char *endptr;
+      long port = strtol (key[i], &endptr, 10);
 
-         if (serv == NULL)
-           result = 2;
-         else
-           print_services (serv);
-       }
+      if (isdigit (key[i][0]) && *endptr == '\0'
+         && 0 <= port && port <= 65535)
+       serv = getservbyport (htons (port), proto);
+      else
+       serv = getservbyname (key[i], proto);
+
+      if (serv == NULL)
+       result = 2;
+      else
+       print_services (serv);
     }
 
   return result;
 }
 
-/* the main function */
-int
-main (int argc, char *argv[])
+/* This is for shadow */
+static void
+print_shadow (struct spwd *sp)
 {
-  int remaining;
-
-  /* Set locale via LC_ALL.  */
-  setlocale (LC_ALL, "");
-  /* Set the text message domain.  */
-  textdomain (PACKAGE);
+  if (putspent (sp, stdout) != 0)
+    fprintf (stderr, "error writing shadow entry: %m\n");
+}
 
-  /* Parse and process arguments.  */
-  argp_parse (&argp, argc, argv, 0, &remaining, NULL);
+static int
+shadow_keys (int number, char *key[])
+{
+  int result = 0;
+  int i;
 
-  if ((argc - remaining) < 1)
+  if (number == 0)
     {
-      error (0, 0, gettext ("wrong number of arguments"));
-      argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
-      return 1;
+      struct spwd *sp;
+
+      setspent ();
+      while ((sp = getspent ()) != NULL)
+       print_shadow (sp);
+      endspent ();
+      return result;
     }
 
-  switch(argv[1][0])
+  for (i = 0; i < number; ++i)
     {
-    case 'g': /* group */
-      if (strcmp (argv[1], "group") == 0)
-       {
-         if (argc == 2)
-           {
-             struct group *grp;
+      struct spwd *sp;
 
-             setgrent ();
-             while ((grp = getgrent()) != NULL)
-               print_group (grp);
-             endgrent ();
-           }
-         else
-           return group_keys (argc - 2, &argv[2]);
-       }
-      else
-       goto error;
-      break;
-    case 'h': /* hosts */
-      if (strcmp (argv[1], "hosts") == 0)
-       {
-         if (argc == 2)
-           {
-             struct hostent *host;
+      sp = getspnam (key[i]);
 
-             sethostent (0);
-             while ((host = gethostent()) != NULL)
-               print_hosts (host);
-             endhostent ();
-           }
-         else
-           return hosts_keys (argc - 2, &argv[2]);
-       }
+      if (sp == NULL)
+       result = 2;
       else
-       goto error;
-      break;
-    case 'n': /* networks */
-      if (strcmp (argv[1], "networks") == 0)
-       {
-         if (argc == 2)
-           {
-             struct netent *net;
+       print_shadow (sp);
+    }
 
-             setnetent (0);
-             while ((net = getnetent()) != NULL)
-               print_networks (net);
-             endnetent ();
-           }
-         else
-           return networks_keys (argc - 2, &argv[2]);
-       }
+  return result;
+}
+
+struct
+  {
+    const char *name;
+    int (*func) (int number, char *key[]);
+  } databases[] =
+  {
+#define D(name) { #name, name ## _keys },
+D(ahosts)
+D(ahostsv4)
+D(ahostsv6)
+D(aliases)
+D(ethers)
+D(group)
+D(gshadow)
+D(hosts)
+D(initgroups)
+D(netgroup)
+D(networks)
+D(passwd)
+D(protocols)
+#if HAVE_SUNRPC
+D(rpc)
+#endif
+D(services)
+D(shadow)
+#undef D
+    { NULL, NULL }
+  };
+
+/* Handle arguments found by argp. */
+static error_t
+parse_option (int key, char *arg, struct argp_state *state)
+{
+  char *endp;
+  switch (key)
+    {
+    case 's':
+      endp = strchr (arg, ':');
+      if (endp == NULL)
+       /* No specific database, change them all.  */
+       for (int i = 0; databases[i].name != NULL; ++i)
+         __nss_configure_lookup (databases[i].name, arg);
       else
-       goto error;
-      break;
-    case 'p': /* passwd, protocols */
-      if (strcmp (argv[1], "passwd") == 0)
        {
-         if (argc == 2)
-           {
-             struct passwd *pwd;
-
-             setpwent ();
-             while ((pwd = getpwent()) != NULL)
-               print_passwd (pwd);
-             endpwent ();
-           }
-         else
-           return passwd_keys (argc - 2, &argv[2]);
+         int i;
+         for (i = 0; databases[i].name != NULL; ++i)
+           if (strncmp (databases[i].name, arg, endp - arg) == 0)
+             {
+               __nss_configure_lookup (databases[i].name, endp + 1);
+               break;
+             }
+         if (databases[i].name == NULL)
+           error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
        }
-      else if (strcmp (argv[1], "protocols") == 0)
-       {
-         if (argc == 2)
-           {
-             struct protoent *proto;
+      break;
 
-             setprotoent (0);
-             while ((proto = getprotoent()) != NULL)
-               print_protocols (proto);
-             endprotoent ();
-           }
-         else
-           return protocols_keys (argc - 2, &argv[2]);
-       }
-      else
-       goto error;
+    case 'i':
+      idn_flags = 0;
       break;
-    case 's': /* services */
-      if (strcmp (argv[1], "services") == 0)
+
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+
+  return 0;
+}
+
+
+static char *
+more_help (int key, const char *text, void *input)
+{
+  switch (key)
+    {
+      size_t len;
+      char *doc;
+      FILE *fp;
+
+    case ARGP_KEY_HELP_EXTRA:
+      /* We print some extra information.  */
+      fp = open_memstream (&doc, &len);
+      if (fp != NULL)
        {
-         if (argc == 2)
-           {
-             struct servent *serv;
+         fputs_unlocked (_("Supported databases:\n"), fp);
 
-             setservent (0);
-             while ((serv = getservent()) != NULL)
-               print_services (serv);
-             endservent ();
+         for (int i = 0, col = 0; databases[i].name != NULL; ++i)
+           {
+             len = strlen (databases[i].name);
+             if (i != 0)
+               {
+                 if (col + len > 72)
+                   {
+                     col = 0;
+                     fputc_unlocked ('\n', fp);
+                   }
+                 else
+                   fputc_unlocked (' ', fp);
+               }
+
+             fputs_unlocked (databases[i].name, fp);
+             col += len + 1;
            }
-         else
-           return services_keys (argc - 2, &argv[2]);
+
+         fputs ("\n\n", fp);
+
+         fprintf (fp, gettext ("\
+For bug reporting instructions, please see:\n\
+%s.\n"), REPORT_BUGS_TO);
+
+         if (fclose (fp) == 0)
+           return doc;
        }
-      else
-       goto error;
       break;
+
     default:
-    error:
-      fprintf (stderr, _("Unknown database: %s\n"), argv[1]);
+      break;
+    }
+  return (char *) text;
+}
+
+
+/* the main function */
+int
+main (int argc, char *argv[])
+{
+  /* Debugging support.  */
+  mtrace ();
+
+  /* Set locale via LC_ALL.  */
+  setlocale (LC_ALL, "");
+  /* Set the text message domain.  */
+  textdomain (PACKAGE);
+
+  /* Parse and process arguments.  */
+  int remaining;
+  argp_parse (&argp, argc, argv, 0, &remaining, NULL);
+
+  if ((argc - remaining) < 1)
+    {
+      error (0, 0, gettext ("wrong number of arguments"));
       argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
       return 1;
     }
 
-  return 0;
+  for (int i = 0; databases[i].name; ++i)
+    if (argv[remaining][0] == databases[i].name[0]
+       && !strcmp (argv[remaining], databases[i].name))
+      return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
+
+  fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
+  argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
+  return 1;
 }