1 /* Copyright (c) 1998-2008, 2009 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* 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 <gnu/option-groups.h>
44 /* Get libc version number. */
47 #define PACKAGE _libc_intl_domainname
49 /* Name and version of program. */
50 static void print_version (FILE *stream, struct argp_state *state);
51 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
53 /* Short description of parameters. */
54 static const char args_doc[] = N_("database [key ...]");
56 /* Supported options. */
57 static const struct argp_option args_options[] =
59 { "service", 's', "CONFIG", 0, N_("Service configuration to be used") },
60 { NULL, 0, NULL, 0, NULL },
63 /* Short description of program. */
64 static const char doc[] = N_("Get entries from administrative database.");
66 /* Prototype for option handler. */
67 static error_t parse_option (int key, char *arg, struct argp_state *state);
69 /* Function to print some extra text in the help message. */
70 static char *more_help (int key, const char *text, void *input);
72 /* Data structure to communicate with argp functions. */
73 static struct argp argp =
75 args_options, parse_option, args_doc, doc, NULL, more_help
78 /* Print the version information. */
80 print_version (FILE *stream, struct argp_state *state)
82 fprintf (stream, "getent %s%s\n", PKGVERSION, VERSION);
83 fprintf (stream, gettext ("\
84 Copyright (C) %s Free Software Foundation, Inc.\n\
85 This is free software; see the source for copying conditions. There is NO\n\
86 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
88 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
91 #if __OPTION_EGLIBC_DB_ALIASES
92 /* This is for aliases */
94 print_aliases (struct aliasent *alias)
98 printf ("%s: ", alias->alias_name);
99 for (i = strlen (alias->alias_name); i < 14; ++i)
100 fputs_unlocked (" ", stdout);
102 for (i = 0; i < alias->alias_members_len; ++i)
104 alias->alias_members [i],
105 i + 1 == alias->alias_members_len ? "\n" : ", ");
109 aliases_keys (int number, char *key[])
113 struct aliasent *alias;
118 while ((alias = getaliasent ()) != NULL)
119 print_aliases (alias);
124 for (i = 0; i < number; ++i)
126 alias = getaliasbyname (key[i]);
131 print_aliases (alias);
136 #endif /* __OPTION_EGLIBC_DB_ALIASES */
138 #if __OPTION_EGLIBC_INET
139 /* This is for ethers */
141 ethers_keys (int number, char *key[])
148 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
152 for (i = 0; i < number; ++i)
154 struct ether_addr *ethp, eth;
155 char buffer [1024], *p;
157 ethp = ether_aton (key[i]);
160 if (ether_ntohost (buffer, ethp))
169 if (ether_hostton (key[i], ð))
177 printf ("%s %s\n", ether_ntoa (ethp), p);
182 #endif /* __OPTION_EGLIBC_INET */
184 /* This is for group */
186 print_group (struct group *grp)
190 printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
191 grp->gr_passwd ? grp->gr_passwd : "",
192 (unsigned long int) grp->gr_gid);
194 while (grp->gr_mem[i] != NULL)
196 fputs_unlocked (grp->gr_mem[i], stdout);
198 if (grp->gr_mem[i] != NULL)
199 putchar_unlocked (',');
201 putchar_unlocked ('\n');
205 group_keys (int number, char *key[])
214 while ((grp = getgrent ()) != NULL)
220 for (i = 0; i < number; ++i)
224 gid_t arg_gid = strtoul(key[i], &ep, 10);
226 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
227 /* Valid numeric gid. */
228 grp = getgrgid (arg_gid);
230 grp = getgrnam (key[i]);
241 /* This is for gshadow */
243 print_gshadow (struct sgrp *sg)
248 sg->sg_namp ? sg->sg_namp : "",
249 sg->sg_passwd ? sg->sg_passwd : "");
251 while (sg->sg_adm[i] != NULL)
253 fputs_unlocked (sg->sg_adm[i], stdout);
255 if (sg->sg_adm[i] != NULL)
256 putchar_unlocked (',');
259 putchar_unlocked (':');
262 while (sg->sg_mem[i] != NULL)
264 fputs_unlocked (sg->sg_mem[i], stdout);
266 if (sg->sg_mem[i] != NULL)
267 putchar_unlocked (',');
270 putchar_unlocked ('\n');
274 gshadow_keys (int number, char *key[])
284 while ((sg = getsgent ()) != NULL)
290 for (i = 0; i < number; ++i)
294 sg = getsgnam (key[i]);
305 #if __OPTION_EGLIBC_INET
306 /* This is for hosts */
308 print_hosts (struct hostent *host)
312 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
314 char buf[INET6_ADDRSTRLEN];
315 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
318 printf ("%-15s %s", ip, host->h_name);
321 for (i = 0; host->h_aliases[i] != NULL; ++i)
323 putchar_unlocked (' ');
324 fputs_unlocked (host->h_aliases[i], stdout);
326 putchar_unlocked ('\n');
331 hosts_keys (int number, char *key[])
335 struct hostent *host;
340 while ((host = gethostent ()) != NULL)
346 for (i = 0; i < number; ++i)
348 struct hostent *host = NULL;
349 char addr[IN6ADDRSZ];
351 if (inet_pton (AF_INET6, key[i], &addr) > 0)
352 host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
353 else if (inet_pton (AF_INET, key[i], &addr) > 0)
354 host = gethostbyaddr (addr, INADDRSZ, AF_INET);
355 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
356 host = gethostbyname2 (key[i], AF_INET);
367 /* This is for hosts, but using getaddrinfo */
369 ahosts_keys_int (int af, int xflags, int number, char *key[])
373 struct hostent *host;
378 while ((host = gethostent ()) != NULL)
384 struct addrinfo hint;
385 memset (&hint, '\0', sizeof (hint));
386 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
389 for (i = 0; i < number; ++i)
391 struct addrinfo *res;
393 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
397 struct addrinfo *runp = res;
403 if (runp->ai_socktype == SOCK_STREAM)
405 else if (runp->ai_socktype == SOCK_DGRAM)
407 else if (runp->ai_socktype == SOCK_RAW)
409 #ifdef SOCK_SEQPACKET
410 else if (runp->ai_socktype == SOCK_SEQPACKET)
411 sockstr = "SEQPACKET";
414 else if (runp->ai_socktype == SOCK_RDM)
418 else if (runp->ai_socktype == SOCK_DCCP)
422 else if (runp->ai_socktype == SOCK_PACKET)
427 snprintf (sockbuf, sizeof (sockbuf), "%d",
432 char buf[INET6_ADDRSTRLEN];
433 printf ("%-15s %-6s %s\n",
434 inet_ntop (runp->ai_family,
435 runp->ai_family == AF_INET
436 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
437 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
440 runp->ai_canonname ?: "");
442 runp = runp->ai_next;
453 ahosts_keys (int number, char *key[])
455 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
459 ahostsv4_keys (int number, char *key[])
461 return ahosts_keys_int (AF_INET, 0, number, key);
465 ahostsv6_keys (int number, char *key[])
467 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
470 /* This is for netgroup */
472 netgroup_keys (int number, char *key[])
479 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
483 for (i = 0; i < number; ++i)
485 if (!setnetgrent (key[i]))
491 printf ("%-21s", key[i]);
493 while (getnetgrent (p, p + 1, p + 2))
494 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
495 putchar_unlocked ('\n');
504 /* This is for networks */
506 print_networks (struct netent *net)
510 ip.s_addr = htonl (net->n_net);
512 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
515 while (net->n_aliases[i] != NULL)
517 putchar_unlocked (' ');
518 fputs_unlocked (net->n_aliases[i], stdout);
521 putchar_unlocked ('\n');
525 networks_keys (int number, char *key[])
534 while ((net = getnetent ()) != NULL)
535 print_networks (net);
540 for (i = 0; i < number; ++i)
542 if (isdigit (key[i][0]))
543 net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC);
545 net = getnetbyname (key[i]);
550 print_networks (net);
555 #endif /* __OPTION_EGLIBC_INET */
557 /* Now is all for passwd */
559 print_passwd (struct passwd *pwd)
561 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
562 pwd->pw_name ? pwd->pw_name : "",
563 pwd->pw_passwd ? pwd->pw_passwd : "",
564 (unsigned long int) pwd->pw_uid,
565 (unsigned long int) pwd->pw_gid,
566 pwd->pw_gecos ? pwd->pw_gecos : "",
567 pwd->pw_dir ? pwd->pw_dir : "",
568 pwd->pw_shell ? pwd->pw_shell : "");
572 passwd_keys (int number, char *key[])
581 while ((pwd = getpwent ()) != NULL)
587 for (i = 0; i < number; ++i)
591 uid_t arg_uid = strtoul(key[i], &ep, 10);
593 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
594 /* Valid numeric uid. */
595 pwd = getpwuid (arg_uid);
597 pwd = getpwnam (key[i]);
608 #if __OPTION_EGLIBC_INET
609 /* This is for protocols */
611 print_protocols (struct protoent *proto)
615 printf ("%-21s %d", proto->p_name, proto->p_proto);
618 while (proto->p_aliases[i] != NULL)
620 putchar_unlocked (' ');
621 fputs_unlocked (proto->p_aliases[i], stdout);
624 putchar_unlocked ('\n');
628 protocols_keys (int number, char *key[])
632 struct protoent *proto;
637 while ((proto = getprotoent ()) != NULL)
638 print_protocols (proto);
643 for (i = 0; i < number; ++i)
645 if (isdigit (key[i][0]))
646 proto = getprotobynumber (atol (key[i]));
648 proto = getprotobyname (key[i]);
653 print_protocols (proto);
659 /* Now is all for rpc */
661 print_rpc (struct rpcent *rpc)
665 printf ("%-15s %d%s",
666 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
668 for (i = 0; rpc->r_aliases[i]; ++i)
669 printf (" %s", rpc->r_aliases[i]);
670 putchar_unlocked ('\n');
674 rpc_keys (int number, char *key[])
683 while ((rpc = getrpcent ()) != NULL)
689 for (i = 0; i < number; ++i)
691 if (isdigit (key[i][0]))
692 rpc = getrpcbynumber (atol (key[i]));
694 rpc = getrpcbyname (key[i]);
707 print_services (struct servent *serv)
711 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
714 while (serv->s_aliases[i] != NULL)
716 putchar_unlocked (' ');
717 fputs_unlocked (serv->s_aliases[i], stdout);
720 putchar_unlocked ('\n');
724 services_keys (int number, char *key[])
728 struct servent *serv;
733 while ((serv = getservent ()) != NULL)
734 print_services (serv);
739 for (i = 0; i < number; ++i)
741 struct servent *serv;
742 char *proto = strchr (key[i], '/');
747 if (isdigit (key[i][0]))
748 serv = getservbyport (htons (atol (key[i])), proto);
750 serv = getservbyname (key[i], proto);
755 print_services (serv);
760 #endif /* __OPTION_EGLIBC_INET */
762 /* This is for shadow */
764 print_shadow (struct spwd *sp)
767 sp->sp_namp ? sp->sp_namp : "",
768 sp->sp_pwdp ? sp->sp_pwdp : "");
770 #define SHADOW_FIELD(n) \
772 putchar_unlocked (':'); \
774 printf ("%ld:", sp->n)
776 SHADOW_FIELD (sp_lstchg);
777 SHADOW_FIELD (sp_min);
778 SHADOW_FIELD (sp_max);
779 SHADOW_FIELD (sp_warn);
780 SHADOW_FIELD (sp_inact);
781 SHADOW_FIELD (sp_expire);
782 if (sp->sp_flag == ~0ul)
783 putchar_unlocked ('\n');
785 printf ("%lu\n", sp->sp_flag);
789 shadow_keys (int number, char *key[])
799 while ((sp = getspent ()) != NULL)
805 for (i = 0; i < number; ++i)
809 sp = getspnam (key[i]);
823 int (*func) (int number, char *key[]);
826 #define D(name) { #name, name ## _keys },
828 #if __OPTION_EGLIBC_INET
829 #define DN(name) D(name)
834 #if __OPTION_EGLIBC_DB_ALIASES
835 #define DA(name) D(name)
859 /* Handle arguments found by argp. */
861 parse_option (int key, char *arg, struct argp_state *state)
867 endp = strchr (arg, ':');
869 /* No specific database, change them all. */
870 for (int i = 0; databases[i].name != NULL; ++i)
871 __nss_configure_lookup (databases[i].name, arg);
875 for (i = 0; databases[i].name != NULL; ++i)
876 if (strncmp (databases[i].name, arg, endp - arg) == 0)
878 __nss_configure_lookup (databases[i].name, endp + 1);
881 if (databases[i].name == NULL)
882 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
887 return ARGP_ERR_UNKNOWN;
895 more_help (int key, const char *text, void *input)
903 case ARGP_KEY_HELP_EXTRA:
904 /* We print some extra information. */
905 fp = open_memstream (&doc, &len);
908 fputs_unlocked (_("Supported databases:\n"), fp);
910 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
912 len = strlen (databases[i].name);
918 fputc_unlocked ('\n', fp);
921 fputc_unlocked (' ', fp);
924 fputs_unlocked (databases[i].name, fp);
930 fprintf (fp, gettext ("\
931 For bug reporting instructions, please see:\n\
932 %s.\n"), REPORT_BUGS_TO);
934 if (fclose (fp) == 0)
942 return (char *) text;
946 /* the main function */
948 main (int argc, char *argv[])
950 /* Debugging support. */
953 /* Set locale via LC_ALL. */
954 setlocale (LC_ALL, "");
955 /* Set the text message domain. */
956 textdomain (PACKAGE);
958 /* Parse and process arguments. */
960 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
962 if ((argc - remaining) < 1)
964 error (0, 0, gettext ("wrong number of arguments"));
965 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
969 for (int i = 0; databases[i].name; ++i)
970 if (argv[remaining][0] == databases[i].name[0]
971 && !strcmp (argv[remaining], databases[i].name))
972 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
974 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
975 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);