]> 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 b843433ed662c68cc35dc151c2f134f27e8f3841..ec48ba4bf1f5f788ad6bca0071150b4a736b3a1b 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 1998-2008, 2009, 2010, 2011 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.
 
@@ -13,9 +13,8 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 /* getent: get entries from administrative database.  */
 
@@ -40,6 +39,8 @@
 #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>
@@ -56,7 +57,7 @@ static const char args_doc[] = N_("database [key ...]");
 /* Supported options. */
 static const struct argp_option args_options[] =
   {
-    { "service", 's', "CONFIG", 0, N_("Service configuration to be used") },
+    { "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 },
   };
@@ -83,17 +84,17 @@ static int idn_flags = AI_IDN | AI_CANONIDN;
 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\
-"), "2011");
+"), "2021");
   fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
 }
 
 /* This is for aliases */
-static inline void
+static void
 print_aliases (struct aliasent *alias)
 {
   unsigned int i = 0;
@@ -182,23 +183,11 @@ ethers_keys (int number, char *key[])
 }
 
 /* This is for group */
-static inline void
+static void
 print_group (struct group *grp)
 {
-  unsigned int i = 0;
-
-  printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
-         grp->gr_passwd ? grp->gr_passwd : "",
-         (unsigned long int) grp->gr_gid);
-
-  while (grp->gr_mem[i] != NULL)
-    {
-      fputs_unlocked (grp->gr_mem[i], stdout);
-      ++i;
-      if (grp->gr_mem[i] != NULL)
-       putchar_unlocked (',');
-    }
-  putchar_unlocked ('\n');
+  if (putgrent (grp, stdout) != 0)
+    fprintf (stderr, "error writing group entry: %m\n");
 }
 
 static int
@@ -242,32 +231,8 @@ group_keys (int number, char *key[])
 static void
 print_gshadow (struct sgrp *sg)
 {
-  unsigned int i = 0;
-
-  printf ("%s:%s:",
-         sg->sg_namp ? sg->sg_namp : "",
-         sg->sg_passwd ? sg->sg_passwd : "");
-
-  while (sg->sg_adm[i] != NULL)
-    {
-      fputs_unlocked (sg->sg_adm[i], stdout);
-      ++i;
-      if (sg->sg_adm[i] != NULL)
-       putchar_unlocked (',');
-    }
-
-  putchar_unlocked (':');
-
-  i = 0;
-  while (sg->sg_mem[i] != NULL)
-    {
-      fputs_unlocked (sg->sg_mem[i], stdout);
-      ++i;
-      if (sg->sg_mem[i] != NULL)
-       putchar_unlocked (',');
-    }
-
-  putchar_unlocked ('\n');
+  if (putsgent (sg, stdout) != 0)
+    fprintf (stderr, "error writing gshadow entry: %m\n");
 }
 
 static int
@@ -429,15 +394,34 @@ ahosts_keys_int (int af, int xflags, int number, char *key[])
                  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];
-             printf ("%-15s %-6s %s\n",
-                     inet_ntop (runp->ai_family,
-                                runp->ai_family == AF_INET
-                                ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
-                                : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
-                                buf, sizeof (buf)),
-                     sockstr,
-                     runp->ai_canonname ?: "");
+             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;
            }
@@ -472,7 +456,6 @@ static int
 netgroup_keys (int number, char *key[])
 {
   int result = 0;
-  int i;
 
   if (number == 0)
     {
@@ -511,28 +494,51 @@ netgroup_keys (int number, char *key[])
   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[])
 {
-  int ngrps = 100;
-  size_t grpslen = ngrps * sizeof (gid_t);
-  gid_t *grps = alloca (grpslen);
+  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 = ngrps;
+      int no = gid_list_size (&list);
       int n;
-      while ((n = getgrouplist (key[i], -1, grps, &no)) == -1
-            && no > ngrps)
+      while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1
+            && no > gid_list_size (&list))
        {
-         grps = extend_alloca (grps, grpslen, no * sizeof (gid_t));
-         ngrps = no;
+         if (!gid_list_resize (&list, no))
+           {
+             fprintf (stderr, _("Could not allocate group list: %m\n"));
+             return 3;
+           }
        }
 
       if (n == -1)
-       return 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)
@@ -540,6 +546,8 @@ initgroups_keys (int number, char *key[])
       putchar_unlocked ('\n');
     }
 
+  gid_list_free (&list);
+
   return 0;
 }
 
@@ -596,17 +604,11 @@ networks_keys (int number, char *key[])
 }
 
 /* Now is all for passwd */
-static inline void
+static void
 print_passwd (struct passwd *pwd)
 {
-  printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
-         pwd->pw_name ? pwd->pw_name : "",
-         pwd->pw_passwd ? pwd->pw_passwd : "",
-         (unsigned long int) pwd->pw_uid,
-         (unsigned long int) 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 int
@@ -647,7 +649,7 @@ passwd_keys (int number, char *key[])
 }
 
 /* This is for protocols */
-static inline void
+static void
 print_protocols (struct protoent *proto)
 {
   unsigned int i;
@@ -696,8 +698,9 @@ protocols_keys (int number, char *key[])
   return result;
 }
 
+#if HAVE_SUNRPC
 /* Now is all for rpc */
-static inline void
+static void
 print_rpc (struct rpcent *rpc)
 {
   int i;
@@ -741,6 +744,7 @@ rpc_keys (int number, char *key[])
 
   return result;
 }
+#endif
 
 /* for services */
 static void
@@ -784,8 +788,12 @@ services_keys (int number, char *key[])
       if (proto != NULL)
        *proto++ = '\0';
 
-      if (isdigit (key[i][0]))
-       serv = getservbyport (htons (atol (key[i])), proto);
+      char *endptr;
+      long port = strtol (key[i], &endptr, 10);
+
+      if (isdigit (key[i][0]) && *endptr == '\0'
+         && 0 <= port && port <= 65535)
+       serv = getservbyport (htons (port), proto);
       else
        serv = getservbyname (key[i], proto);
 
@@ -802,26 +810,8 @@ services_keys (int number, char *key[])
 static void
 print_shadow (struct spwd *sp)
 {
-  printf ("%s:%s:",
-         sp->sp_namp ? sp->sp_namp : "",
-         sp->sp_pwdp ? sp->sp_pwdp : "");
-
-#define SHADOW_FIELD(n) \
-  if (sp->n == -1)                                                           \
-    putchar_unlocked (':');                                                  \
-  else                                                                       \
-    printf ("%ld:", sp->n)
-
-  SHADOW_FIELD (sp_lstchg);
-  SHADOW_FIELD (sp_min);
-  SHADOW_FIELD (sp_max);
-  SHADOW_FIELD (sp_warn);
-  SHADOW_FIELD (sp_inact);
-  SHADOW_FIELD (sp_expire);
-  if (sp->sp_flag == ~0ul)
-    putchar_unlocked ('\n');
-  else
-    printf ("%lu\n", sp->sp_flag);
+  if (putspent (sp, stdout) != 0)
+    fprintf (stderr, "error writing shadow entry: %m\n");
 }
 
 static int
@@ -837,7 +827,7 @@ shadow_keys (int number, char *key[])
       setspent ();
       while ((sp = getspent ()) != NULL)
        print_shadow (sp);
-      endpwent ();
+      endspent ();
       return result;
     }
 
@@ -876,7 +866,9 @@ D(netgroup)
 D(networks)
 D(passwd)
 D(protocols)
+#if HAVE_SUNRPC
 D(rpc)
+#endif
 D(services)
 D(shadow)
 #undef D
@@ -958,9 +950,9 @@ more_help (int key, const char *text, void *input)
 
          fputs ("\n\n", fp);
 
-         fputs (gettext ("\
+         fprintf (fp, gettext ("\
 For bug reporting instructions, please see:\n\
-<http://www.gnu.org/software/libc/bugs.html>.\n"), fp);
+%s.\n"), REPORT_BUGS_TO);
 
          if (fclose (fp) == 0)
            return doc;