1 /* Copyright (c) 1998-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* getent: get entries from administrative database. */
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
39 #include <netinet/ether.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <scratch_buffer.h>
45 /* Get libc version number. */
48 #define PACKAGE _libc_intl_domainname
50 /* Name and version of program. */
51 static void print_version (FILE *stream, struct argp_state *state);
52 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
54 /* Short description of parameters. */
55 static const char args_doc[] = N_("database [key ...]");
57 /* Supported options. */
58 static const struct argp_option args_options[] =
60 { "service", 's', N_("CONFIG"), 0, N_("Service configuration to be used") },
61 { "no-idn", 'i', NULL, 0, N_("disable IDN encoding") },
62 { NULL, 0, NULL, 0, NULL },
65 /* Short description of program. */
66 static const char doc[] = N_("Get entries from administrative database.");
68 /* Prototype for option handler. */
69 static error_t parse_option (int key, char *arg, struct argp_state *state);
71 /* Function to print some extra text in the help message. */
72 static char *more_help (int key, const char *text, void *input);
74 /* Data structure to communicate with argp functions. */
75 static struct argp argp =
77 args_options, parse_option, args_doc, doc, NULL, more_help
80 /* Additional getaddrinfo flags for IDN encoding. */
81 static int idn_flags = AI_IDN | AI_CANONIDN;
83 /* Print the version information. */
85 print_version (FILE *stream, struct argp_state *state)
87 fprintf (stream, "getent %s%s\n", PKGVERSION, VERSION);
88 fprintf (stream, gettext ("\
89 Copyright (C) %s Free Software Foundation, Inc.\n\
90 This is free software; see the source for copying conditions. There is NO\n\
91 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
93 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
96 /* This is for aliases */
98 print_aliases (struct aliasent *alias)
102 printf ("%s: ", alias->alias_name);
103 for (i = strlen (alias->alias_name); i < 14; ++i)
104 fputs_unlocked (" ", stdout);
106 for (i = 0; i < alias->alias_members_len; ++i)
108 alias->alias_members [i],
109 i + 1 == alias->alias_members_len ? "\n" : ", ");
113 aliases_keys (int number, char *key[])
117 struct aliasent *alias;
122 while ((alias = getaliasent ()) != NULL)
123 print_aliases (alias);
128 for (i = 0; i < number; ++i)
130 alias = getaliasbyname (key[i]);
135 print_aliases (alias);
141 /* This is for ethers */
143 ethers_keys (int number, char *key[])
150 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
154 for (i = 0; i < number; ++i)
156 struct ether_addr *ethp, eth;
157 char buffer [1024], *p;
159 ethp = ether_aton (key[i]);
162 if (ether_ntohost (buffer, ethp))
171 if (ether_hostton (key[i], ð))
179 printf ("%s %s\n", ether_ntoa (ethp), p);
185 /* This is for group */
187 print_group (struct group *grp)
189 if (putgrent (grp, stdout) != 0)
190 fprintf (stderr, "error writing group entry: %m\n");
194 group_keys (int number, char *key[])
203 while ((grp = getgrent ()) != NULL)
209 for (i = 0; i < number; ++i)
213 gid_t arg_gid = strtoul(key[i], &ep, 10);
215 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
216 /* Valid numeric gid. */
217 grp = getgrgid (arg_gid);
219 grp = getgrnam (key[i]);
230 /* This is for gshadow */
232 print_gshadow (struct sgrp *sg)
234 if (putsgent (sg, stdout) != 0)
235 fprintf (stderr, "error writing gshadow entry: %m\n");
239 gshadow_keys (int number, char *key[])
249 while ((sg = getsgent ()) != NULL)
255 for (i = 0; i < number; ++i)
259 sg = getsgnam (key[i]);
270 /* This is for hosts */
272 print_hosts (struct hostent *host)
276 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
278 char buf[INET6_ADDRSTRLEN];
279 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
282 printf ("%-15s %s", ip, host->h_name);
285 for (i = 0; host->h_aliases[i] != NULL; ++i)
287 putchar_unlocked (' ');
288 fputs_unlocked (host->h_aliases[i], stdout);
290 putchar_unlocked ('\n');
295 hosts_keys (int number, char *key[])
299 struct hostent *host;
304 while ((host = gethostent ()) != NULL)
310 for (i = 0; i < number; ++i)
312 struct hostent *host = NULL;
313 char addr[IN6ADDRSZ];
315 if (inet_pton (AF_INET6, key[i], &addr) > 0)
316 host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
317 else if (inet_pton (AF_INET, key[i], &addr) > 0)
318 host = gethostbyaddr (addr, INADDRSZ, AF_INET);
319 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
320 host = gethostbyname2 (key[i], AF_INET);
331 /* This is for hosts, but using getaddrinfo */
333 ahosts_keys_int (int af, int xflags, int number, char *key[])
337 struct hostent *host;
342 while ((host = gethostent ()) != NULL)
348 struct addrinfo hint;
349 memset (&hint, '\0', sizeof (hint));
350 hint.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME
351 | idn_flags | xflags);
354 for (i = 0; i < number; ++i)
356 struct addrinfo *res;
358 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
362 struct addrinfo *runp = res;
368 if (runp->ai_socktype == SOCK_STREAM)
370 else if (runp->ai_socktype == SOCK_DGRAM)
372 else if (runp->ai_socktype == SOCK_RAW)
374 #ifdef SOCK_SEQPACKET
375 else if (runp->ai_socktype == SOCK_SEQPACKET)
376 sockstr = "SEQPACKET";
379 else if (runp->ai_socktype == SOCK_RDM)
383 else if (runp->ai_socktype == SOCK_DCCP)
387 else if (runp->ai_socktype == SOCK_PACKET)
392 snprintf (sockbuf, sizeof (sockbuf), "%d",
397 /* Three digits per byte, plus '%' and null terminator. */
398 char scope[3 * sizeof (uint32_t) + 2];
399 struct sockaddr_in6 *addr6
400 = (struct sockaddr_in6 *) runp->ai_addr;
401 if (runp->ai_family != AF_INET6 || addr6->sin6_scope_id == 0)
402 /* No scope ID present. */
405 snprintf (scope, sizeof (scope), "%%%" PRIu32,
406 addr6->sin6_scope_id);
408 char buf[INET6_ADDRSTRLEN];
409 if (inet_ntop (runp->ai_family,
410 runp->ai_family == AF_INET
411 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
413 buf, sizeof (buf)) == NULL)
415 strcpy (buf, "<invalid>");
419 int pad = 15 - strlen (buf) - strlen (scope);
423 printf ("%s%-*s %-6s %s\n",
424 buf, pad, scope, sockstr, runp->ai_canonname ?: "");
426 runp = runp->ai_next;
437 ahosts_keys (int number, char *key[])
439 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
443 ahostsv4_keys (int number, char *key[])
445 return ahosts_keys_int (AF_INET, 0, number, key);
449 ahostsv6_keys (int number, char *key[])
451 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
454 /* This is for netgroup */
456 netgroup_keys (int number, char *key[])
462 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
468 char *host = strcmp (key[1], "*") == 0 ? NULL : key[1];
469 char *user = strcmp (key[2], "*") == 0 ? NULL : key[2];
470 char *domain = strcmp (key[3], "*") == 0 ? NULL : key[3];
472 printf ("%-21s (%s,%s,%s) = %d\n",
473 key[0], host ?: "", user ?: "", domain ?: "",
474 innetgr (key[0], host, user, domain));
476 else if (number == 1)
478 if (!setnetgrent (key[0]))
484 printf ("%-21s", key[0]);
486 while (getnetgrent (p, p + 1, p + 2))
487 printf (" (%s,%s,%s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
488 putchar_unlocked ('\n');
497 #define DYNARRAY_STRUCT gid_list
498 #define DYNARRAY_ELEMENT gid_t
499 #define DYNARRAY_PREFIX gid_list_
500 #define DYNARRAY_INITIAL_SIZE 10
501 #include <malloc/dynarray-skeleton.c>
503 /* This is for initgroups */
505 initgroups_keys (int number, char *key[])
509 fprintf (stderr, _("Enumeration not supported on %s\n"), "initgroups");
513 struct gid_list list;
514 gid_list_init (&list);
515 if (!gid_list_resize (&list, 10))
517 fprintf (stderr, _("Could not allocate group list: %m\n"));
521 for (int i = 0; i < number; ++i)
523 int no = gid_list_size (&list);
525 while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1
526 && no > gid_list_size (&list))
528 if (!gid_list_resize (&list, no))
530 fprintf (stderr, _("Could not allocate group list: %m\n"));
537 gid_list_free (&list);
541 const gid_t *grps = gid_list_begin (&list);
542 printf ("%-21s", key[i]);
543 for (int j = 0; j < n; ++j)
545 printf (" %ld", (long int) grps[j]);
546 putchar_unlocked ('\n');
549 gid_list_free (&list);
554 /* This is for networks */
556 print_networks (struct netent *net)
560 ip.s_addr = htonl (net->n_net);
562 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
565 while (net->n_aliases[i] != NULL)
567 putchar_unlocked (' ');
568 fputs_unlocked (net->n_aliases[i], stdout);
571 putchar_unlocked ('\n');
575 networks_keys (int number, char *key[])
584 while ((net = getnetent ()) != NULL)
585 print_networks (net);
590 for (i = 0; i < number; ++i)
592 if (isdigit (key[i][0]))
593 net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC);
595 net = getnetbyname (key[i]);
600 print_networks (net);
606 /* Now is all for passwd */
608 print_passwd (struct passwd *pwd)
610 if (putpwent (pwd, stdout) != 0)
611 fprintf (stderr, "error writing passwd entry: %m\n");
615 passwd_keys (int number, char *key[])
624 while ((pwd = getpwent ()) != NULL)
630 for (i = 0; i < number; ++i)
634 uid_t arg_uid = strtoul(key[i], &ep, 10);
636 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
637 /* Valid numeric uid. */
638 pwd = getpwuid (arg_uid);
640 pwd = getpwnam (key[i]);
651 /* This is for protocols */
653 print_protocols (struct protoent *proto)
657 printf ("%-21s %d", proto->p_name, proto->p_proto);
660 while (proto->p_aliases[i] != NULL)
662 putchar_unlocked (' ');
663 fputs_unlocked (proto->p_aliases[i], stdout);
666 putchar_unlocked ('\n');
670 protocols_keys (int number, char *key[])
674 struct protoent *proto;
679 while ((proto = getprotoent ()) != NULL)
680 print_protocols (proto);
685 for (i = 0; i < number; ++i)
687 if (isdigit (key[i][0]))
688 proto = getprotobynumber (atol (key[i]));
690 proto = getprotobyname (key[i]);
695 print_protocols (proto);
702 /* Now is all for rpc */
704 print_rpc (struct rpcent *rpc)
708 printf ("%-15s %d%s",
709 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
711 for (i = 0; rpc->r_aliases[i]; ++i)
712 printf (" %s", rpc->r_aliases[i]);
713 putchar_unlocked ('\n');
717 rpc_keys (int number, char *key[])
726 while ((rpc = getrpcent ()) != NULL)
732 for (i = 0; i < number; ++i)
734 if (isdigit (key[i][0]))
735 rpc = getrpcbynumber (atol (key[i]));
737 rpc = getrpcbyname (key[i]);
751 print_services (struct servent *serv)
755 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
758 while (serv->s_aliases[i] != NULL)
760 putchar_unlocked (' ');
761 fputs_unlocked (serv->s_aliases[i], stdout);
764 putchar_unlocked ('\n');
768 services_keys (int number, char *key[])
772 struct servent *serv;
777 while ((serv = getservent ()) != NULL)
778 print_services (serv);
783 for (i = 0; i < number; ++i)
785 struct servent *serv;
786 char *proto = strchr (key[i], '/');
792 long port = strtol (key[i], &endptr, 10);
794 if (isdigit (key[i][0]) && *endptr == '\0'
795 && 0 <= port && port <= 65535)
796 serv = getservbyport (htons (port), proto);
798 serv = getservbyname (key[i], proto);
803 print_services (serv);
809 /* This is for shadow */
811 print_shadow (struct spwd *sp)
813 if (putspent (sp, stdout) != 0)
814 fprintf (stderr, "error writing shadow entry: %m\n");
818 shadow_keys (int number, char *key[])
828 while ((sp = getspent ()) != NULL)
834 for (i = 0; i < number; ++i)
838 sp = getspnam (key[i]);
852 int (*func) (int number, char *key[]);
855 #define D(name) { #name, name ## _keys },
878 /* Handle arguments found by argp. */
880 parse_option (int key, char *arg, struct argp_state *state)
886 endp = strchr (arg, ':');
888 /* No specific database, change them all. */
889 for (int i = 0; databases[i].name != NULL; ++i)
890 __nss_configure_lookup (databases[i].name, arg);
894 for (i = 0; databases[i].name != NULL; ++i)
895 if (strncmp (databases[i].name, arg, endp - arg) == 0)
897 __nss_configure_lookup (databases[i].name, endp + 1);
900 if (databases[i].name == NULL)
901 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
910 return ARGP_ERR_UNKNOWN;
918 more_help (int key, const char *text, void *input)
926 case ARGP_KEY_HELP_EXTRA:
927 /* We print some extra information. */
928 fp = open_memstream (&doc, &len);
931 fputs_unlocked (_("Supported databases:\n"), fp);
933 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
935 len = strlen (databases[i].name);
941 fputc_unlocked ('\n', fp);
944 fputc_unlocked (' ', fp);
947 fputs_unlocked (databases[i].name, fp);
953 fprintf (fp, gettext ("\
954 For bug reporting instructions, please see:\n\
955 %s.\n"), REPORT_BUGS_TO);
957 if (fclose (fp) == 0)
965 return (char *) text;
969 /* the main function */
971 main (int argc, char *argv[])
973 /* Debugging support. */
976 /* Set locale via LC_ALL. */
977 setlocale (LC_ALL, "");
978 /* Set the text message domain. */
979 textdomain (PACKAGE);
981 /* Parse and process arguments. */
983 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
985 if ((argc - remaining) < 1)
987 error (0, 0, gettext ("wrong number of arguments"));
988 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
992 for (int i = 0; databases[i].name; ++i)
993 if (argv[remaining][0] == databases[i].name[0]
994 && !strcmp (argv[remaining], databases[i].name))
995 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
997 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
998 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);