-/* Copyright (c) 1998-2006, 2007 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.
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. */
#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 <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>
/* 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 },
};
/* Short description of program. */
-static const char doc[] = N_("Get entries from administrative database.\v\
-For bug reporting instructions, please see:\n\
-<http://www.gnu.org/software/libc/bugs.html>.\n");
+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);
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\
-"), "2007");
+"), "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;
}
/* 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
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)
struct addrinfo hint;
memset (&hint, '\0', sizeof (hint));
- hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
+ hint.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME
+ | idn_flags | xflags);
hint.ai_family = af;
for (i = 0; i < number; ++i)
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",
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;
}
netgroup_keys (int number, char *key[])
{
int result = 0;
- int i;
if (number == 0)
{
return 3;
}
- for (i = 0; i < number; ++i)
+ 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[i]))
+ if (!setnetgrent (key[0]))
result = 2;
else
{
char *p[3];
- printf ("%-21s", key[i]);
+ printf ("%-21s", key[0]);
while (getnetgrent (p, p + 1, p + 2))
- printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
+ printf (" (%s,%s,%s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
putchar_unlocked ('\n');
}
}
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 void
print_networks (struct netent *net)
putchar_unlocked (' ');
fputs_unlocked (net->n_aliases[i], stdout);
++i;
- if (net->n_aliases[i] != NULL)
- putchar_unlocked (',');
}
putchar_unlocked ('\n');
}
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]);
}
/* 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
}
/* This is for protocols */
-static inline void
+static void
print_protocols (struct protoent *proto)
{
unsigned int i;
return result;
}
+#if HAVE_SUNRPC
/* Now is all for rpc */
-static inline void
+static void
print_rpc (struct rpcent *rpc)
{
int i;
return result;
}
+#endif
/* for services */
static void
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);
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
setspent ();
while ((sp = getspent ()) != NULL)
print_shadow (sp);
- endpwent ();
+ endspent ();
return result;
}
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
}
break;
+ case 'i':
+ idn_flags = 0;
+ break;
+
default:
return ARGP_ERR_UNKNOWN;
}
col += len + 1;
}
+ 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;
}