1 /* Copyright (c) 1998-2008, 2009, 2010, 2011 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. */
38 #include <arpa/inet.h>
39 #include <arpa/nameser.h>
40 #include <netinet/ether.h>
41 #include <netinet/in.h>
42 #include <sys/socket.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 { "no-idn", 'i', NULL, 0, N_("disable IDN encoding") },
61 { NULL, 0, NULL, 0, NULL },
64 /* Short description of program. */
65 static const char doc[] = N_("Get entries from administrative database.");
67 /* Prototype for option handler. */
68 static error_t parse_option (int key, char *arg, struct argp_state *state);
70 /* Function to print some extra text in the help message. */
71 static char *more_help (int key, const char *text, void *input);
73 /* Data structure to communicate with argp functions. */
74 static struct argp argp =
76 args_options, parse_option, args_doc, doc, NULL, more_help
79 /* Additional getaddrinfo flags for IDN encoding. */
80 static int idn_flags = AI_IDN | AI_CANONIDN;
82 /* Print the version information. */
84 print_version (FILE *stream, struct argp_state *state)
86 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
87 fprintf (stream, gettext ("\
88 Copyright (C) %s Free Software Foundation, Inc.\n\
89 This is free software; see the source for copying conditions. There is NO\n\
90 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
92 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
95 /* This is for aliases */
97 print_aliases (struct aliasent *alias)
101 printf ("%s: ", alias->alias_name);
102 for (i = strlen (alias->alias_name); i < 14; ++i)
103 fputs_unlocked (" ", stdout);
105 for (i = 0; i < alias->alias_members_len; ++i)
107 alias->alias_members [i],
108 i + 1 == alias->alias_members_len ? "\n" : ", ");
112 aliases_keys (int number, char *key[])
116 struct aliasent *alias;
121 while ((alias = getaliasent ()) != NULL)
122 print_aliases (alias);
127 for (i = 0; i < number; ++i)
129 alias = getaliasbyname (key[i]);
134 print_aliases (alias);
140 /* This is for ethers */
142 ethers_keys (int number, char *key[])
149 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
153 for (i = 0; i < number; ++i)
155 struct ether_addr *ethp, eth;
156 char buffer [1024], *p;
158 ethp = ether_aton (key[i]);
161 if (ether_ntohost (buffer, ethp))
170 if (ether_hostton (key[i], ð))
178 printf ("%s %s\n", ether_ntoa (ethp), p);
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 /* This is for hosts */
307 print_hosts (struct hostent *host)
311 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
313 char buf[INET6_ADDRSTRLEN];
314 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
317 printf ("%-15s %s", ip, host->h_name);
320 for (i = 0; host->h_aliases[i] != NULL; ++i)
322 putchar_unlocked (' ');
323 fputs_unlocked (host->h_aliases[i], stdout);
325 putchar_unlocked ('\n');
330 hosts_keys (int number, char *key[])
334 struct hostent *host;
339 while ((host = gethostent ()) != NULL)
345 for (i = 0; i < number; ++i)
347 struct hostent *host = NULL;
348 char addr[IN6ADDRSZ];
350 if (inet_pton (AF_INET6, key[i], &addr) > 0)
351 host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
352 else if (inet_pton (AF_INET, key[i], &addr) > 0)
353 host = gethostbyaddr (addr, INADDRSZ, AF_INET);
354 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
355 host = gethostbyname2 (key[i], AF_INET);
366 /* This is for hosts, but using getaddrinfo */
368 ahosts_keys_int (int af, int xflags, int number, char *key[])
372 struct hostent *host;
377 while ((host = gethostent ()) != NULL)
383 struct addrinfo hint;
384 memset (&hint, '\0', sizeof (hint));
385 hint.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME
386 | idn_flags | 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");
485 char *host = strcmp (key[1], "*") == 0 ? NULL : key[1];
486 char *user = strcmp (key[2], "*") == 0 ? NULL : key[2];
487 char *domain = strcmp (key[3], "*") == 0 ? NULL : key[3];
489 printf ("%-21s (%s,%s,%s) = %d\n",
490 key[0], host ?: "", user ?: "", domain ?: "",
491 innetgr (key[0], host, user, domain));
493 else if (number == 1)
495 if (!setnetgrent (key[0]))
501 printf ("%-21s", key[0]);
503 while (getnetgrent (p, p + 1, p + 2))
504 printf (" (%s,%s,%s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
505 putchar_unlocked ('\n');
514 /* This is for initgroups */
516 initgroups_keys (int number, char *key[])
519 size_t grpslen = ngrps * sizeof (gid_t);
520 gid_t *grps = alloca (grpslen);
522 for (int i = 0; i < number; ++i)
526 while ((n = getgrouplist (key[i], -1, grps, &no)) == -1
529 grps = extend_alloca (grps, grpslen, no * sizeof (gid_t));
536 printf ("%-21s", key[i]);
537 for (int j = 0; j < n; ++j)
539 printf (" %ld", (long int) grps[j]);
540 putchar_unlocked ('\n');
546 /* This is for networks */
548 print_networks (struct netent *net)
552 ip.s_addr = htonl (net->n_net);
554 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
557 while (net->n_aliases[i] != NULL)
559 putchar_unlocked (' ');
560 fputs_unlocked (net->n_aliases[i], stdout);
563 putchar_unlocked ('\n');
567 networks_keys (int number, char *key[])
576 while ((net = getnetent ()) != NULL)
577 print_networks (net);
582 for (i = 0; i < number; ++i)
584 if (isdigit (key[i][0]))
585 net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC);
587 net = getnetbyname (key[i]);
592 print_networks (net);
598 /* Now is all for passwd */
600 print_passwd (struct passwd *pwd)
602 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
603 pwd->pw_name ? pwd->pw_name : "",
604 pwd->pw_passwd ? pwd->pw_passwd : "",
605 (unsigned long int) pwd->pw_uid,
606 (unsigned long int) pwd->pw_gid,
607 pwd->pw_gecos ? pwd->pw_gecos : "",
608 pwd->pw_dir ? pwd->pw_dir : "",
609 pwd->pw_shell ? pwd->pw_shell : "");
613 passwd_keys (int number, char *key[])
622 while ((pwd = getpwent ()) != NULL)
628 for (i = 0; i < number; ++i)
632 uid_t arg_uid = strtoul(key[i], &ep, 10);
634 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
635 /* Valid numeric uid. */
636 pwd = getpwuid (arg_uid);
638 pwd = getpwnam (key[i]);
649 /* This is for protocols */
651 print_protocols (struct protoent *proto)
655 printf ("%-21s %d", proto->p_name, proto->p_proto);
658 while (proto->p_aliases[i] != NULL)
660 putchar_unlocked (' ');
661 fputs_unlocked (proto->p_aliases[i], stdout);
664 putchar_unlocked ('\n');
668 protocols_keys (int number, char *key[])
672 struct protoent *proto;
677 while ((proto = getprotoent ()) != NULL)
678 print_protocols (proto);
683 for (i = 0; i < number; ++i)
685 if (isdigit (key[i][0]))
686 proto = getprotobynumber (atol (key[i]));
688 proto = getprotobyname (key[i]);
693 print_protocols (proto);
699 /* Now is all for rpc */
701 print_rpc (struct rpcent *rpc)
705 printf ("%-15s %d%s",
706 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
708 for (i = 0; rpc->r_aliases[i]; ++i)
709 printf (" %s", rpc->r_aliases[i]);
710 putchar_unlocked ('\n');
714 rpc_keys (int number, char *key[])
723 while ((rpc = getrpcent ()) != NULL)
729 for (i = 0; i < number; ++i)
731 if (isdigit (key[i][0]))
732 rpc = getrpcbynumber (atol (key[i]));
734 rpc = getrpcbyname (key[i]);
747 print_services (struct servent *serv)
751 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
754 while (serv->s_aliases[i] != NULL)
756 putchar_unlocked (' ');
757 fputs_unlocked (serv->s_aliases[i], stdout);
760 putchar_unlocked ('\n');
764 services_keys (int number, char *key[])
768 struct servent *serv;
773 while ((serv = getservent ()) != NULL)
774 print_services (serv);
779 for (i = 0; i < number; ++i)
781 struct servent *serv;
782 char *proto = strchr (key[i], '/');
787 if (isdigit (key[i][0]))
788 serv = getservbyport (htons (atol (key[i])), proto);
790 serv = getservbyname (key[i], proto);
795 print_services (serv);
801 /* This is for shadow */
803 print_shadow (struct spwd *sp)
806 sp->sp_namp ? sp->sp_namp : "",
807 sp->sp_pwdp ? sp->sp_pwdp : "");
809 #define SHADOW_FIELD(n) \
811 putchar_unlocked (':'); \
813 printf ("%ld:", sp->n)
815 SHADOW_FIELD (sp_lstchg);
816 SHADOW_FIELD (sp_min);
817 SHADOW_FIELD (sp_max);
818 SHADOW_FIELD (sp_warn);
819 SHADOW_FIELD (sp_inact);
820 SHADOW_FIELD (sp_expire);
821 if (sp->sp_flag == ~0ul)
822 putchar_unlocked ('\n');
824 printf ("%lu\n", sp->sp_flag);
828 shadow_keys (int number, char *key[])
838 while ((sp = getspent ()) != NULL)
844 for (i = 0; i < number; ++i)
848 sp = getspnam (key[i]);
862 int (*func) (int number, char *key[]);
865 #define D(name) { #name, name ## _keys },
886 /* Handle arguments found by argp. */
888 parse_option (int key, char *arg, struct argp_state *state)
894 endp = strchr (arg, ':');
896 /* No specific database, change them all. */
897 for (int i = 0; databases[i].name != NULL; ++i)
898 __nss_configure_lookup (databases[i].name, arg);
902 for (i = 0; databases[i].name != NULL; ++i)
903 if (strncmp (databases[i].name, arg, endp - arg) == 0)
905 __nss_configure_lookup (databases[i].name, endp + 1);
908 if (databases[i].name == NULL)
909 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
918 return ARGP_ERR_UNKNOWN;
926 more_help (int key, const char *text, void *input)
934 case ARGP_KEY_HELP_EXTRA:
935 /* We print some extra information. */
936 fp = open_memstream (&doc, &len);
939 fputs_unlocked (_("Supported databases:\n"), fp);
941 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
943 len = strlen (databases[i].name);
949 fputc_unlocked ('\n', fp);
952 fputc_unlocked (' ', fp);
955 fputs_unlocked (databases[i].name, fp);
962 For bug reporting instructions, please see:\n\
963 <http://www.gnu.org/software/libc/bugs.html>.\n"), fp);
965 if (fclose (fp) == 0)
973 return (char *) text;
977 /* the main function */
979 main (int argc, char *argv[])
981 /* Debugging support. */
984 /* Set locale via LC_ALL. */
985 setlocale (LC_ALL, "");
986 /* Set the text message domain. */
987 textdomain (PACKAGE);
989 /* Parse and process arguments. */
991 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
993 if ((argc - remaining) < 1)
995 error (0, 0, gettext ("wrong number of arguments"));
996 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
1000 for (int i = 0; databases[i].name; ++i)
1001 if (argv[remaining][0] == databases[i].name[0]
1002 && !strcmp (argv[remaining], databases[i].name))
1003 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
1005 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
1006 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);