1 /* Copyright (c) 1998-2008, 2009, 2010 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>
43 /* Get libc version number. */
46 #define PACKAGE _libc_intl_domainname
48 /* Name and version of program. */
49 static void print_version (FILE *stream, struct argp_state *state);
50 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
52 /* Short description of parameters. */
53 static const char args_doc[] = N_("database [key ...]");
55 /* Supported options. */
56 static const struct argp_option args_options[] =
58 { "service", 's', "CONFIG", 0, N_("Service configuration to be used") },
59 { NULL, 0, NULL, 0, NULL },
62 /* Short description of program. */
63 static const char doc[] = N_("Get entries from administrative database.");
65 /* Prototype for option handler. */
66 static error_t parse_option (int key, char *arg, struct argp_state *state);
68 /* Function to print some extra text in the help message. */
69 static char *more_help (int key, const char *text, void *input);
71 /* Data structure to communicate with argp functions. */
72 static struct argp argp =
74 args_options, parse_option, args_doc, doc, NULL, more_help
77 /* Print the version information. */
79 print_version (FILE *stream, struct argp_state *state)
81 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
82 fprintf (stream, gettext ("\
83 Copyright (C) %s Free Software Foundation, Inc.\n\
84 This is free software; see the source for copying conditions. There is NO\n\
85 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
87 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
90 /* This is for aliases */
92 print_aliases (struct aliasent *alias)
96 printf ("%s: ", alias->alias_name);
97 for (i = strlen (alias->alias_name); i < 14; ++i)
98 fputs_unlocked (" ", stdout);
100 for (i = 0; i < alias->alias_members_len; ++i)
102 alias->alias_members [i],
103 i + 1 == alias->alias_members_len ? "\n" : ", ");
107 aliases_keys (int number, char *key[])
111 struct aliasent *alias;
116 while ((alias = getaliasent ()) != NULL)
117 print_aliases (alias);
122 for (i = 0; i < number; ++i)
124 alias = getaliasbyname (key[i]);
129 print_aliases (alias);
135 /* This is for ethers */
137 ethers_keys (int number, char *key[])
144 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
148 for (i = 0; i < number; ++i)
150 struct ether_addr *ethp, eth;
151 char buffer [1024], *p;
153 ethp = ether_aton (key[i]);
156 if (ether_ntohost (buffer, ethp))
165 if (ether_hostton (key[i], ð))
173 printf ("%s %s\n", ether_ntoa (ethp), p);
179 /* This is for group */
181 print_group (struct group *grp)
185 printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
186 grp->gr_passwd ? grp->gr_passwd : "",
187 (unsigned long int) grp->gr_gid);
189 while (grp->gr_mem[i] != NULL)
191 fputs_unlocked (grp->gr_mem[i], stdout);
193 if (grp->gr_mem[i] != NULL)
194 putchar_unlocked (',');
196 putchar_unlocked ('\n');
200 group_keys (int number, char *key[])
209 while ((grp = getgrent ()) != NULL)
215 for (i = 0; i < number; ++i)
219 gid_t arg_gid = strtoul(key[i], &ep, 10);
221 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
222 /* Valid numeric gid. */
223 grp = getgrgid (arg_gid);
225 grp = getgrnam (key[i]);
236 /* This is for gshadow */
238 print_gshadow (struct sgrp *sg)
243 sg->sg_namp ? sg->sg_namp : "",
244 sg->sg_passwd ? sg->sg_passwd : "");
246 while (sg->sg_adm[i] != NULL)
248 fputs_unlocked (sg->sg_adm[i], stdout);
250 if (sg->sg_adm[i] != NULL)
251 putchar_unlocked (',');
254 putchar_unlocked (':');
257 while (sg->sg_mem[i] != NULL)
259 fputs_unlocked (sg->sg_mem[i], stdout);
261 if (sg->sg_mem[i] != NULL)
262 putchar_unlocked (',');
265 putchar_unlocked ('\n');
269 gshadow_keys (int number, char *key[])
279 while ((sg = getsgent ()) != NULL)
285 for (i = 0; i < number; ++i)
289 sg = getsgnam (key[i]);
300 /* This is for hosts */
302 print_hosts (struct hostent *host)
306 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
308 char buf[INET6_ADDRSTRLEN];
309 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
312 printf ("%-15s %s", ip, host->h_name);
315 for (i = 0; host->h_aliases[i] != NULL; ++i)
317 putchar_unlocked (' ');
318 fputs_unlocked (host->h_aliases[i], stdout);
320 putchar_unlocked ('\n');
325 hosts_keys (int number, char *key[])
329 struct hostent *host;
334 while ((host = gethostent ()) != NULL)
340 for (i = 0; i < number; ++i)
342 struct hostent *host = NULL;
343 char addr[IN6ADDRSZ];
345 if (inet_pton (AF_INET6, key[i], &addr) > 0)
346 host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
347 else if (inet_pton (AF_INET, key[i], &addr) > 0)
348 host = gethostbyaddr (addr, INADDRSZ, AF_INET);
349 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
350 host = gethostbyname2 (key[i], AF_INET);
361 /* This is for hosts, but using getaddrinfo */
363 ahosts_keys_int (int af, int xflags, int number, char *key[])
367 struct hostent *host;
372 while ((host = gethostent ()) != NULL)
378 struct addrinfo hint;
379 memset (&hint, '\0', sizeof (hint));
380 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
383 for (i = 0; i < number; ++i)
385 struct addrinfo *res;
387 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
391 struct addrinfo *runp = res;
397 if (runp->ai_socktype == SOCK_STREAM)
399 else if (runp->ai_socktype == SOCK_DGRAM)
401 else if (runp->ai_socktype == SOCK_RAW)
403 #ifdef SOCK_SEQPACKET
404 else if (runp->ai_socktype == SOCK_SEQPACKET)
405 sockstr = "SEQPACKET";
408 else if (runp->ai_socktype == SOCK_RDM)
412 else if (runp->ai_socktype == SOCK_DCCP)
416 else if (runp->ai_socktype == SOCK_PACKET)
421 snprintf (sockbuf, sizeof (sockbuf), "%d",
426 char buf[INET6_ADDRSTRLEN];
427 printf ("%-15s %-6s %s\n",
428 inet_ntop (runp->ai_family,
429 runp->ai_family == AF_INET
430 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
431 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
434 runp->ai_canonname ?: "");
436 runp = runp->ai_next;
447 ahosts_keys (int number, char *key[])
449 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
453 ahostsv4_keys (int number, char *key[])
455 return ahosts_keys_int (AF_INET, 0, number, key);
459 ahostsv6_keys (int number, char *key[])
461 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
464 /* This is for netgroup */
466 netgroup_keys (int number, char *key[])
473 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
477 for (i = 0; i < number; ++i)
479 if (!setnetgrent (key[i]))
485 printf ("%-21s", key[i]);
487 while (getnetgrent (p, p + 1, p + 2))
488 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
489 putchar_unlocked ('\n');
498 /* This is for networks */
500 print_networks (struct netent *net)
504 ip.s_addr = htonl (net->n_net);
506 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
509 while (net->n_aliases[i] != NULL)
511 putchar_unlocked (' ');
512 fputs_unlocked (net->n_aliases[i], stdout);
515 putchar_unlocked ('\n');
519 networks_keys (int number, char *key[])
528 while ((net = getnetent ()) != NULL)
529 print_networks (net);
534 for (i = 0; i < number; ++i)
536 if (isdigit (key[i][0]))
537 net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC);
539 net = getnetbyname (key[i]);
544 print_networks (net);
550 /* Now is all for passwd */
552 print_passwd (struct passwd *pwd)
554 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
555 pwd->pw_name ? pwd->pw_name : "",
556 pwd->pw_passwd ? pwd->pw_passwd : "",
557 (unsigned long int) pwd->pw_uid,
558 (unsigned long int) pwd->pw_gid,
559 pwd->pw_gecos ? pwd->pw_gecos : "",
560 pwd->pw_dir ? pwd->pw_dir : "",
561 pwd->pw_shell ? pwd->pw_shell : "");
565 passwd_keys (int number, char *key[])
574 while ((pwd = getpwent ()) != NULL)
580 for (i = 0; i < number; ++i)
584 uid_t arg_uid = strtoul(key[i], &ep, 10);
586 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
587 /* Valid numeric uid. */
588 pwd = getpwuid (arg_uid);
590 pwd = getpwnam (key[i]);
601 /* This is for protocols */
603 print_protocols (struct protoent *proto)
607 printf ("%-21s %d", proto->p_name, proto->p_proto);
610 while (proto->p_aliases[i] != NULL)
612 putchar_unlocked (' ');
613 fputs_unlocked (proto->p_aliases[i], stdout);
616 putchar_unlocked ('\n');
620 protocols_keys (int number, char *key[])
624 struct protoent *proto;
629 while ((proto = getprotoent ()) != NULL)
630 print_protocols (proto);
635 for (i = 0; i < number; ++i)
637 if (isdigit (key[i][0]))
638 proto = getprotobynumber (atol (key[i]));
640 proto = getprotobyname (key[i]);
645 print_protocols (proto);
651 /* Now is all for rpc */
653 print_rpc (struct rpcent *rpc)
657 printf ("%-15s %d%s",
658 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
660 for (i = 0; rpc->r_aliases[i]; ++i)
661 printf (" %s", rpc->r_aliases[i]);
662 putchar_unlocked ('\n');
666 rpc_keys (int number, char *key[])
675 while ((rpc = getrpcent ()) != NULL)
681 for (i = 0; i < number; ++i)
683 if (isdigit (key[i][0]))
684 rpc = getrpcbynumber (atol (key[i]));
686 rpc = getrpcbyname (key[i]);
699 print_services (struct servent *serv)
703 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
706 while (serv->s_aliases[i] != NULL)
708 putchar_unlocked (' ');
709 fputs_unlocked (serv->s_aliases[i], stdout);
712 putchar_unlocked ('\n');
716 services_keys (int number, char *key[])
720 struct servent *serv;
725 while ((serv = getservent ()) != NULL)
726 print_services (serv);
731 for (i = 0; i < number; ++i)
733 struct servent *serv;
734 char *proto = strchr (key[i], '/');
739 if (isdigit (key[i][0]))
740 serv = getservbyport (htons (atol (key[i])), proto);
742 serv = getservbyname (key[i], proto);
747 print_services (serv);
753 /* This is for shadow */
755 print_shadow (struct spwd *sp)
758 sp->sp_namp ? sp->sp_namp : "",
759 sp->sp_pwdp ? sp->sp_pwdp : "");
761 #define SHADOW_FIELD(n) \
763 putchar_unlocked (':'); \
765 printf ("%ld:", sp->n)
767 SHADOW_FIELD (sp_lstchg);
768 SHADOW_FIELD (sp_min);
769 SHADOW_FIELD (sp_max);
770 SHADOW_FIELD (sp_warn);
771 SHADOW_FIELD (sp_inact);
772 SHADOW_FIELD (sp_expire);
773 if (sp->sp_flag == ~0ul)
774 putchar_unlocked ('\n');
776 printf ("%lu\n", sp->sp_flag);
780 shadow_keys (int number, char *key[])
790 while ((sp = getspent ()) != NULL)
796 for (i = 0; i < number; ++i)
800 sp = getspnam (key[i]);
814 int (*func) (int number, char *key[]);
817 #define D(name) { #name, name ## _keys },
837 /* Handle arguments found by argp. */
839 parse_option (int key, char *arg, struct argp_state *state)
845 endp = strchr (arg, ':');
847 /* No specific database, change them all. */
848 for (int i = 0; databases[i].name != NULL; ++i)
849 __nss_configure_lookup (databases[i].name, arg);
853 for (i = 0; databases[i].name != NULL; ++i)
854 if (strncmp (databases[i].name, arg, endp - arg) == 0)
856 __nss_configure_lookup (databases[i].name, endp + 1);
859 if (databases[i].name == NULL)
860 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
865 return ARGP_ERR_UNKNOWN;
873 more_help (int key, const char *text, void *input)
881 case ARGP_KEY_HELP_EXTRA:
882 /* We print some extra information. */
883 fp = open_memstream (&doc, &len);
886 fputs_unlocked (_("Supported databases:\n"), fp);
888 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
890 len = strlen (databases[i].name);
896 fputc_unlocked ('\n', fp);
899 fputc_unlocked (' ', fp);
902 fputs_unlocked (databases[i].name, fp);
909 For bug reporting instructions, please see:\n\
910 <http://www.gnu.org/software/libc/bugs.html>.\n"), fp);
912 if (fclose (fp) == 0)
920 return (char *) text;
924 /* the main function */
926 main (int argc, char *argv[])
928 /* Debugging support. */
931 /* Set locale via LC_ALL. */
932 setlocale (LC_ALL, "");
933 /* Set the text message domain. */
934 textdomain (PACKAGE);
936 /* Parse and process arguments. */
938 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
940 if ((argc - remaining) < 1)
942 error (0, 0, gettext ("wrong number of arguments"));
943 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
947 for (int i = 0; databases[i].name; ++i)
948 if (argv[remaining][0] == databases[i].name[0]
949 && !strcmp (argv[remaining], databases[i].name))
950 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
952 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
953 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);