1 /* Copyright (c) 1998-2003, 2004, 2005 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. */
36 #include <arpa/inet.h>
37 #include <arpa/nameser.h>
38 #include <netinet/ether.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
42 /* Get libc version number. */
45 #define PACKAGE _libc_intl_domainname
47 /* Name and version of program. */
48 static void print_version (FILE *stream, struct argp_state *state);
49 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
51 /* Short description of parameters. */
52 static const char args_doc[] = N_("database [key ...]");
54 /* Supported options. */
55 static const struct argp_option args_options[] =
57 { "service", 's', "CONFIG", 0, N_("Service configuration to be used") },
58 { NULL, 0, NULL, 0, NULL },
61 /* Short description of program. */
62 static const char doc[] = N_("Get entries from administrative database.\v\
63 For bug reporting instructions, please see:\n\
64 <http://www.gnu.org/software/libc/bugs.html>.\n");
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 (GNU %s) %s\n", PACKAGE, 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 /* This is for aliases */
93 print_aliases (struct aliasent *alias)
97 printf ("%s: ", alias->alias_name);
98 for (i = strlen (alias->alias_name); i < 14; ++i)
99 fputs_unlocked (" ", stdout);
101 for (i = 0; i < alias->alias_members_len; ++i)
103 alias->alias_members [i],
104 i + 1 == alias->alias_members_len ? "\n" : ", ");
108 aliases_keys (int number, char *key[])
112 struct aliasent *alias;
117 while ((alias = getaliasent ()) != NULL)
118 print_aliases (alias);
123 for (i = 0; i < number; ++i)
125 alias = getaliasbyname (key[i]);
130 print_aliases (alias);
136 /* This is for ethers */
138 ethers_keys (int number, char *key[])
145 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
149 for (i = 0; i < number; ++i)
151 struct ether_addr *ethp, eth;
152 char buffer [1024], *p;
154 ethp = ether_aton (key[i]);
157 if (ether_ntohost (buffer, ethp))
166 if (ether_hostton (key[i], ð))
174 printf ("%s %s\n", ether_ntoa (ethp), p);
180 /* This is for group */
182 print_group (struct group *grp)
186 printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
187 grp->gr_passwd ? grp->gr_passwd : "",
188 (unsigned long int) grp->gr_gid);
190 while (grp->gr_mem[i] != NULL)
192 fputs_unlocked (grp->gr_mem[i], stdout);
194 if (grp->gr_mem[i] != NULL)
195 putchar_unlocked (',');
197 putchar_unlocked ('\n');
201 group_keys (int number, char *key[])
210 while ((grp = getgrent ()) != NULL)
216 for (i = 0; i < number; ++i)
220 gid_t arg_gid = strtoul(key[i], &ep, 10);
222 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
223 /* Valid numeric gid. */
224 grp = getgrgid (arg_gid);
226 grp = getgrnam (key[i]);
237 /* This is for hosts */
239 print_hosts (struct hostent *host)
243 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
245 char buf[INET6_ADDRSTRLEN];
246 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
249 printf ("%-15s %s", ip, host->h_name);
252 for (i = 0; host->h_aliases[i] != NULL; ++i)
254 putchar_unlocked (' ');
255 fputs_unlocked (host->h_aliases[i], stdout);
257 putchar_unlocked ('\n');
262 hosts_keys (int number, char *key[])
266 struct hostent *host;
271 while ((host = gethostent ()) != NULL)
277 for (i = 0; i < number; ++i)
279 struct hostent *host = NULL;
280 char addr[IN6ADDRSZ];
282 if (inet_pton (AF_INET6, key[i], &addr) > 0)
283 host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
284 else if (inet_pton (AF_INET, key[i], &addr) > 0)
285 host = gethostbyaddr (addr, INADDRSZ, AF_INET);
286 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
287 host = gethostbyname2 (key[i], AF_INET);
298 /* This is for hosts, but using getaddrinfo */
300 ahosts_keys_int (int af, int xflags, int number, char *key[])
304 struct hostent *host;
309 while ((host = gethostent ()) != NULL)
315 struct addrinfo hint;
316 memset (&hint, '\0', sizeof (hint));
317 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
320 for (i = 0; i < number; ++i)
322 struct addrinfo *res;
324 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
328 struct addrinfo *runp = res;
334 if (runp->ai_socktype == SOCK_STREAM)
336 else if (runp->ai_socktype == SOCK_DGRAM)
338 else if (runp->ai_socktype == SOCK_RAW)
342 snprintf (sockbuf, sizeof (sockbuf), "%d",
347 char buf[INET6_ADDRSTRLEN];
348 printf ("%-15s %-6s %s\n",
349 inet_ntop (runp->ai_family,
350 runp->ai_family == AF_INET
351 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
352 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
355 runp->ai_canonname ?: "");
357 runp = runp->ai_next;
368 ahosts_keys (int number, char *key[])
370 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
374 ahostsv4_keys (int number, char *key[])
376 return ahosts_keys_int (AF_INET, 0, number, key);
380 ahostsv6_keys (int number, char *key[])
382 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
385 /* This is for netgroup */
387 netgroup_keys (int number, char *key[])
394 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
398 for (i = 0; i < number; ++i)
400 if (!setnetgrent (key[i]))
406 printf ("%-21s", key[i]);
408 while (getnetgrent (p, p + 1, p + 2))
409 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
410 putchar_unlocked ('\n');
419 /* This is for networks */
421 print_networks (struct netent *net)
425 ip.s_addr = htonl (net->n_net);
427 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
430 while (net->n_aliases[i] != NULL)
432 putchar_unlocked (' ');
433 fputs_unlocked (net->n_aliases[i], stdout);
435 if (net->n_aliases[i] != NULL)
436 putchar_unlocked (',');
438 putchar_unlocked ('\n');
442 networks_keys (int number, char *key[])
451 while ((net = getnetent ()) != NULL)
452 print_networks (net);
457 for (i = 0; i < number; ++i)
459 if (isdigit (key[i][0]))
460 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
462 net = getnetbyname (key[i]);
467 print_networks (net);
473 /* Now is all for passwd */
475 print_passwd (struct passwd *pwd)
477 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
478 pwd->pw_name ? pwd->pw_name : "",
479 pwd->pw_passwd ? pwd->pw_passwd : "",
480 (unsigned long int) pwd->pw_uid,
481 (unsigned long int) pwd->pw_gid,
482 pwd->pw_gecos ? pwd->pw_gecos : "",
483 pwd->pw_dir ? pwd->pw_dir : "",
484 pwd->pw_shell ? pwd->pw_shell : "");
488 passwd_keys (int number, char *key[])
497 while ((pwd = getpwent ()) != NULL)
503 for (i = 0; i < number; ++i)
507 uid_t arg_uid = strtoul(key[i], &ep, 10);
509 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
510 /* Valid numeric uid. */
511 pwd = getpwuid (arg_uid);
513 pwd = getpwnam (key[i]);
524 /* This is for protocols */
526 print_protocols (struct protoent *proto)
530 printf ("%-21s %d", proto->p_name, proto->p_proto);
533 while (proto->p_aliases[i] != NULL)
535 putchar_unlocked (' ');
536 fputs_unlocked (proto->p_aliases[i], stdout);
539 putchar_unlocked ('\n');
543 protocols_keys (int number, char *key[])
547 struct protoent *proto;
552 while ((proto = getprotoent ()) != NULL)
553 print_protocols (proto);
558 for (i = 0; i < number; ++i)
560 if (isdigit (key[i][0]))
561 proto = getprotobynumber (atol (key[i]));
563 proto = getprotobyname (key[i]);
568 print_protocols (proto);
574 /* Now is all for rpc */
576 print_rpc (struct rpcent *rpc)
580 printf ("%-15s %d%s",
581 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
583 for (i = 0; rpc->r_aliases[i]; ++i)
584 printf (" %s", rpc->r_aliases[i]);
585 putchar_unlocked ('\n');
589 rpc_keys (int number, char *key[])
598 while ((rpc = getrpcent ()) != NULL)
604 for (i = 0; i < number; ++i)
606 if (isdigit (key[i][0]))
607 rpc = getrpcbynumber (atol (key[i]));
609 rpc = getrpcbyname (key[i]);
622 print_services (struct servent *serv)
626 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
629 while (serv->s_aliases[i] != NULL)
631 putchar_unlocked (' ');
632 fputs_unlocked (serv->s_aliases[i], stdout);
635 putchar_unlocked ('\n');
639 services_keys (int number, char *key[])
643 struct servent *serv;
648 while ((serv = getservent ()) != NULL)
649 print_services (serv);
654 for (i = 0; i < number; ++i)
656 struct servent *serv;
657 char *proto = strchr (key[i], '/');
662 if (isdigit (key[i][0]))
663 serv = getservbyport (htons (atol (key[i])), proto);
665 serv = getservbyname (key[i], proto);
670 print_services (serv);
676 /* This is for shadow */
678 print_shadow (struct spwd *sp)
681 sp->sp_namp ? sp->sp_namp : "",
682 sp->sp_pwdp ? sp->sp_pwdp : "");
684 #define SHADOW_FIELD(n) \
686 putchar_unlocked (':'); \
688 printf ("%ld:", sp->n)
690 SHADOW_FIELD (sp_lstchg);
691 SHADOW_FIELD (sp_min);
692 SHADOW_FIELD (sp_max);
693 SHADOW_FIELD (sp_warn);
694 SHADOW_FIELD (sp_inact);
695 SHADOW_FIELD (sp_expire);
696 if (sp->sp_flag == ~0ul)
697 putchar_unlocked ('\n');
699 printf ("%lu\n", sp->sp_flag);
703 shadow_keys (int number, char *key[])
713 while ((sp = getspent ()) != NULL)
719 for (i = 0; i < number; ++i)
723 sp = getspnam (key[i]);
737 int (*func) (int number, char *key[]);
740 #define D(name) { #name, name ## _keys },
759 /* Handle arguments found by argp. */
761 parse_option (int key, char *arg, struct argp_state *state)
767 endp = strchr (arg, ':');
769 /* No specific database, change them all. */
770 for (int i = 0; databases[i].name != NULL; ++i)
771 __nss_configure_lookup (databases[i].name, arg);
775 for (i = 0; databases[i].name != NULL; ++i)
776 if (strncmp (databases[i].name, arg, endp - arg) == 0)
778 __nss_configure_lookup (databases[i].name, endp + 1);
781 if (databases[i].name == NULL)
782 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
787 return ARGP_ERR_UNKNOWN;
795 more_help (int key, const char *text, void *input)
803 case ARGP_KEY_HELP_EXTRA:
804 /* We print some extra information. */
805 fp = open_memstream (&doc, &len);
808 fputs_unlocked (_("Supported databases:\n"), fp);
810 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
812 len = strlen (databases[i].name);
818 fputc_unlocked ('\n', fp);
821 fputc_unlocked (' ', fp);
824 fputs_unlocked (databases[i].name, fp);
828 if (fclose (fp) == 0)
836 return (char *) text;
840 /* the main function */
842 main (int argc, char *argv[])
844 /* Debugging support. */
847 /* Set locale via LC_ALL. */
848 setlocale (LC_ALL, "");
849 /* Set the text message domain. */
850 textdomain (PACKAGE);
852 /* Parse and process arguments. */
854 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
856 if ((argc - remaining) < 1)
858 error (0, 0, gettext ("wrong number of arguments"));
859 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
863 for (int i = 0; databases[i].name; ++i)
864 if (argv[remaining][0] == databases[i].name[0]
865 && !strcmp (argv[remaining], databases[i].name))
866 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
868 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
869 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);