-/* Copyright (c) 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
+/* Copyright (c) 1998-2015 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
+ <http://www.gnu.org/licenses/>. */
/* getent: get entries from administrative database. */
#include <aliases.h>
#include <argp.h>
-#include <grp.h>
-#include <pwd.h>
-#include <shadow.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 <netinet/ether.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
+#include <netinet/ether.h>
+#include <netinet/in.h>
+#include <sys/socket.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.");
+
/* 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 =
{
- args_options, parse_option, args_doc, NULL,
+ 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\
-"), "2003");
+"), "2015");
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:%ld:", grp->gr_name ? grp->gr_name : "",
+ printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
grp->gr_passwd ? grp->gr_passwd : "",
(unsigned long int) grp->gr_gid);
for (i = 0; i < number; ++i)
{
- if (isdigit (key[i][0]))
- {
- char *ep;
- gid_t arg_gid = strtoul (key[i], &ep, 10);
+ errno = 0;
+ char *ep;
+ gid_t arg_gid = strtoul(key[i], &ep, 10);
- if (*key[i] != '\0' && *ep == '\0') /* valid numeric uid */
- grp = getgrgid (arg_gid);
- else
- grp = NULL;
- }
+ if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
+ /* Valid numeric gid. */
+ grp = getgrgid (arg_gid);
else
grp = getgrnam (key[i]);
return result;
}
+/* This is for gshadow */
+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');
+}
+
+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)
for (i = 0; i < number; ++i)
{
struct hostent *host = NULL;
+ char addr[IN6ADDRSZ];
- 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);
- }
+ 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);
/* This is for hosts, but using getaddrinfo */
static int
-ahosts_keys (int number, char *key[])
+ahosts_keys_int (int af, int xflags, int number, char *key[])
{
int result = 0;
int i;
struct addrinfo hint;
memset (&hint, '\0', sizeof (hint));
- hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME;
- hint.ai_family = AF_UNSPEC;
+ 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",
char buf[INET6_ADDRSTRLEN];
printf ("%-15s %-6s %s\n",
inet_ntop (runp->ai_family,
- &((struct sockaddr_in *) runp->ai_addr)->sin_addr,
+ 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);
+ runp->ai_canonname ?: "");
runp = runp->ai_next;
}
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;
- int i;
if (number == 0)
{
return 3;
}
- for (i = 0; i < number; ++i)
+ if (number == 4)
{
- if (!setnetgrent (key[i]))
+ 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[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');
}
}
+ endnetgrent ();
+
return result;
}
+/* 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;
+ }
+
+ for (int i = 0; i < number; ++i)
+ {
+ int no = ngrps;
+ int n;
+ while ((n = getgrouplist (key[i], -1, grps, &no)) == -1
+ && no > ngrps)
+ {
+ grps = extend_alloca (grps, grpslen, no * sizeof (gid_t));
+ ngrps = no;
+ }
+
+ if (n == -1)
+ return 1;
+
+ printf ("%-21s", key[i]);
+ for (int j = 0; j < n; ++j)
+ if (grps[j] != -1)
+ printf (" %ld", (long int) grps[j]);
+ putchar_unlocked ('\n');
+ }
+
+ 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:%ld:%ld:%s:%s:%s\n",
+ 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,
for (i = 0; i < number; ++i)
{
- if (isdigit (key[i][0]))
- {
- char *ep;
- uid_t arg_uid = strtoul (key[i], &ep, 10);
-
- if (*key[i] != '\0' && *ep == '\0') /* valid numeric uid */
- pwd = getpwuid (arg_uid);
- else
- pwd = NULL;
- }
+ errno = 0;
+ char *ep;
+ uid_t arg_uid = strtoul(key[i], &ep, 10);
+
+ if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
+ /* Valid numeric uid. */
+ pwd = getpwuid (arg_uid);
else
pwd = getpwnam (key[i]);
}
/* This is for protocols */
-static inline void
+static void
print_protocols (struct protoent *proto)
{
unsigned int i;
}
/* Now is all for rpc */
-static inline void
+static void
print_rpc (struct rpcent *rpc)
{
int 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
- {
- int j;
-
- while ((serv = getservent ()) != NULL)
- if (strcmp (serv->s_name, key[i]) == 0)
- {
- print_services (serv);
- break;
- }
- else
- for (j = 0; serv->s_aliases[j]; ++j)
- if (strcmp (serv->s_aliases[j], key[i]) == 0)
- {
- print_services (serv);
- break;
- }
- }
- endservent ();
- }
- else
- {
- *proto++ = '\0';
+ if (proto != NULL)
+ *proto++ = '\0';
- if (isdigit (key[i][0]))
- serv = getservbyport (htons (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;
setspent ();
while ((sp = getspent ()) != NULL)
print_shadow (sp);
- endpwent ();
+ endspent ();
return result;
}
{
#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)
static error_t
parse_option (int key, char *arg, struct argp_state *state)
{
- int i;
+ char *endp;
switch (key)
{
case 's':
- for (i = 0; databases[i].name; ++i)
- __nss_configure_lookup (databases[i].name, arg);
+ 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
+ {
+ 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"));
+ }
+ break;
+
+ case 'i':
+ idn_flags = 0;
break;
default:
return 0;
}
-/* build doc */
-static void
-build_doc (void)
-{
- int i, j, len;
- char *short_doc, *long_doc, *doc, *p;
-
- short_doc = _("getent - get entries from administrative database.");
- long_doc = _("Supported databases:");
- len = strlen (short_doc) + strlen (long_doc) + 3;
- for (i = 0; databases[i].name; ++i)
- len += strlen (databases[i].name) + 1;
-
- doc = (char *) malloc (len);
- if (doc == NULL)
- doc = short_doc;
- else
+static char *
+more_help (int key, const char *text, void *input)
+{
+ switch (key)
{
- p = stpcpy (doc, short_doc);
- *p++ = '\v';
- p = stpcpy (p, long_doc);
- *p++ = '\n';
+ size_t len;
+ char *doc;
+ FILE *fp;
- for (i = 0, j = 0; databases[i].name; ++i)
+ case ARGP_KEY_HELP_EXTRA:
+ /* We print some extra information. */
+ fp = open_memstream (&doc, &len);
+ if (fp != NULL)
{
- len = strlen (databases[i].name);
- if (i != 0)
+ fputs_unlocked (_("Supported databases:\n"), fp);
+
+ for (int i = 0, col = 0; databases[i].name != NULL; ++i)
{
- if (j + len > 72)
+ len = strlen (databases[i].name);
+ if (i != 0)
{
- j = 0;
- *p++ = '\n';
+ if (col + len > 72)
+ {
+ col = 0;
+ fputc_unlocked ('\n', fp);
+ }
+ else
+ fputc_unlocked (' ', fp);
}
- else
- *p++ = ' ';
+
+ fputs_unlocked (databases[i].name, fp);
+ col += len + 1;
}
- p = mempcpy (p, databases[i].name, len);
- j += 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;
}
- }
+ break;
- argp.doc = doc;
+ default:
+ break;
+ }
+ return (char *) text;
}
+
/* the main function */
int
main (int argc, char *argv[])
{
- int remaining, i;
+ /* Debugging support. */
+ mtrace ();
/* Set locale via LC_ALL. */
setlocale (LC_ALL, "");
/* Set the text message domain. */
textdomain (PACKAGE);
- /* Build argp.doc. */
- build_doc ();
-
/* Parse and process arguments. */
+ int remaining;
argp_parse (&argp, argc, argv, 0, &remaining, NULL);
if ((argc - remaining) < 1)
return 1;
}
- for (i = 0; databases[i].name; ++i)
+ 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]);