1 /* Copyright (c) 1998-2003, 2004 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. */
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/ether.h>
38 #include <arpa/inet.h>
39 #include <arpa/nameser.h>
41 /* Get libc version number. */
44 #define PACKAGE _libc_intl_domainname
46 /* Name and version of program. */
47 static void print_version (FILE *stream, struct argp_state *state);
48 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
50 /* Short description of parameters. */
51 static const char args_doc[] = N_("database [key ...]");
53 /* Supported options. */
54 static const struct argp_option args_options[] =
56 { "service", 's', "CONFIG", 0, N_("Service configuration to be used") },
57 { NULL, 0, NULL, 0, NULL },
60 /* Prototype for option handler. */
61 static error_t parse_option (int key, char *arg, struct argp_state *state);
63 /* Data structure to communicate with argp functions. */
64 static struct argp argp =
66 args_options, parse_option, args_doc, NULL,
69 /* Print the version information. */
71 print_version (FILE *stream, struct argp_state *state)
73 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
74 fprintf (stream, gettext ("\
75 Copyright (C) %s Free Software Foundation, Inc.\n\
76 This is free software; see the source for copying conditions. There is NO\n\
77 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
79 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
82 /* This is for aliases */
84 print_aliases (struct aliasent *alias)
88 printf ("%s: ", alias->alias_name);
89 for (i = strlen (alias->alias_name); i < 14; ++i)
90 fputs_unlocked (" ", stdout);
92 for (i = 0; i < alias->alias_members_len; ++i)
94 alias->alias_members [i],
95 i + 1 == alias->alias_members_len ? "\n" : ", ");
99 aliases_keys (int number, char *key[])
103 struct aliasent *alias;
108 while ((alias = getaliasent ()) != NULL)
109 print_aliases (alias);
114 for (i = 0; i < number; ++i)
116 alias = getaliasbyname (key[i]);
121 print_aliases (alias);
127 /* This is for ethers */
129 ethers_keys (int number, char *key[])
136 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
140 for (i = 0; i < number; ++i)
142 struct ether_addr *ethp, eth;
143 char buffer [1024], *p;
145 ethp = ether_aton (key[i]);
148 if (ether_ntohost (buffer, ethp))
157 if (ether_hostton (key[i], ð))
165 printf ("%s %s\n", ether_ntoa (ethp), p);
171 /* This is for group */
173 print_group (struct group *grp)
177 printf ("%s:%s:%ld:", grp->gr_name ? grp->gr_name : "",
178 grp->gr_passwd ? grp->gr_passwd : "",
179 (unsigned long int) grp->gr_gid);
181 while (grp->gr_mem[i] != NULL)
183 fputs_unlocked (grp->gr_mem[i], stdout);
185 if (grp->gr_mem[i] != NULL)
186 putchar_unlocked (',');
188 putchar_unlocked ('\n');
192 group_keys (int number, char *key[])
201 while ((grp = getgrent ()) != NULL)
207 for (i = 0; i < number; ++i)
209 if (isdigit (key[i][0]))
212 gid_t arg_gid = strtoul (key[i], &ep, 10);
214 if (*key[i] != '\0' && *ep == '\0') /* valid numeric uid */
215 grp = getgrgid (arg_gid);
220 grp = getgrnam (key[i]);
231 /* This is for hosts */
233 print_hosts (struct hostent *host)
237 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
239 char buf[INET6_ADDRSTRLEN];
240 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
243 printf ("%-15s %s", ip, host->h_name);
246 for (i = 0; host->h_aliases[i] != NULL; ++i)
248 putchar_unlocked (' ');
249 fputs_unlocked (host->h_aliases[i], stdout);
251 putchar_unlocked ('\n');
256 hosts_keys (int number, char *key[])
260 struct hostent *host;
265 while ((host = gethostent ()) != NULL)
271 for (i = 0; i < number; ++i)
273 struct hostent *host = NULL;
275 if (strchr (key[i], ':') != NULL)
277 char addr[IN6ADDRSZ];
278 if (inet_pton (AF_INET6, key[i], &addr))
279 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
281 else if (isdigit (key[i][0]))
284 if (inet_pton (AF_INET, key[i], &addr))
285 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
287 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
288 host = gethostbyname2 (key[i], AF_INET);
299 /* This is for hosts, but using getaddrinfo */
301 ahosts_keys (int number, char *key[])
305 struct hostent *host;
310 while ((host = gethostent ()) != NULL)
316 struct addrinfo hint;
317 memset (&hint, '\0', sizeof (hint));
318 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME;
319 hint.ai_family = AF_UNSPEC;
321 for (i = 0; i < number; ++i)
323 struct addrinfo *res;
325 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
329 struct addrinfo *runp = res;
335 if (runp->ai_socktype == SOCK_STREAM)
337 else if (runp->ai_socktype == SOCK_DGRAM)
339 else if (runp->ai_socktype == SOCK_RAW)
343 snprintf (sockbuf, sizeof (sockbuf), "%d",
348 char buf[INET6_ADDRSTRLEN];
349 printf ("%-15s %-6s %s\n",
350 inet_ntop (runp->ai_family,
351 &((struct sockaddr_in *) runp->ai_addr)->sin_addr,
356 runp = runp->ai_next;
366 /* This is for netgroup */
368 netgroup_keys (int number, char *key[])
375 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
379 for (i = 0; i < number; ++i)
381 if (!setnetgrent (key[i]))
387 printf ("%-21s", key[i]);
389 while (getnetgrent (p, p + 1, p + 2))
390 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
391 putchar_unlocked ('\n');
398 /* This is for networks */
400 print_networks (struct netent *net)
404 ip.s_addr = htonl (net->n_net);
406 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
409 while (net->n_aliases[i] != NULL)
411 putchar_unlocked (' ');
412 fputs_unlocked (net->n_aliases[i], stdout);
414 if (net->n_aliases[i] != NULL)
415 putchar_unlocked (',');
417 putchar_unlocked ('\n');
421 networks_keys (int number, char *key[])
430 while ((net = getnetent ()) != NULL)
431 print_networks (net);
436 for (i = 0; i < number; ++i)
438 if (isdigit (key[i][0]))
439 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
441 net = getnetbyname (key[i]);
446 print_networks (net);
452 /* Now is all for passwd */
454 print_passwd (struct passwd *pwd)
456 printf ("%s:%s:%ld:%ld:%s:%s:%s\n",
457 pwd->pw_name ? pwd->pw_name : "",
458 pwd->pw_passwd ? pwd->pw_passwd : "",
459 (unsigned long int) pwd->pw_uid,
460 (unsigned long int) pwd->pw_gid,
461 pwd->pw_gecos ? pwd->pw_gecos : "",
462 pwd->pw_dir ? pwd->pw_dir : "",
463 pwd->pw_shell ? pwd->pw_shell : "");
467 passwd_keys (int number, char *key[])
476 while ((pwd = getpwent ()) != NULL)
482 for (i = 0; i < number; ++i)
484 if (isdigit (key[i][0]))
487 uid_t arg_uid = strtoul (key[i], &ep, 10);
489 if (*key[i] != '\0' && *ep == '\0') /* valid numeric uid */
490 pwd = getpwuid (arg_uid);
495 pwd = getpwnam (key[i]);
506 /* This is for protocols */
508 print_protocols (struct protoent *proto)
512 printf ("%-21s %d", proto->p_name, proto->p_proto);
515 while (proto->p_aliases[i] != NULL)
517 putchar_unlocked (' ');
518 fputs_unlocked (proto->p_aliases[i], stdout);
521 putchar_unlocked ('\n');
525 protocols_keys (int number, char *key[])
529 struct protoent *proto;
534 while ((proto = getprotoent ()) != NULL)
535 print_protocols (proto);
540 for (i = 0; i < number; ++i)
542 if (isdigit (key[i][0]))
543 proto = getprotobynumber (atol (key[i]));
545 proto = getprotobyname (key[i]);
550 print_protocols (proto);
556 /* Now is all for rpc */
558 print_rpc (struct rpcent *rpc)
562 printf ("%-15s %d%s",
563 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
565 for (i = 0; rpc->r_aliases[i]; ++i)
566 printf (" %s", rpc->r_aliases[i]);
567 putchar_unlocked ('\n');
571 rpc_keys (int number, char *key[])
580 while ((rpc = getrpcent ()) != NULL)
586 for (i = 0; i < number; ++i)
588 if (isdigit (key[i][0]))
589 rpc = getrpcbynumber (atol (key[i]));
591 rpc = getrpcbyname (key[i]);
604 print_services (struct servent *serv)
608 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
611 while (serv->s_aliases[i] != NULL)
613 putchar_unlocked (' ');
614 fputs_unlocked (serv->s_aliases[i], stdout);
617 putchar_unlocked ('\n');
621 services_keys (int number, char *key[])
625 struct servent *serv;
630 while ((serv = getservent ()) != NULL)
631 print_services (serv);
636 for (i = 0; i < number; ++i)
638 struct servent *serv;
639 char *proto = strchr (key[i], '/');
644 if (isdigit (key[i][0]))
646 int port = htons (atol (key[i]));
647 while ((serv = getservent ()) != NULL)
648 if (serv->s_port == port)
650 print_services (serv);
658 while ((serv = getservent ()) != NULL)
659 if (strcmp (serv->s_name, key[i]) == 0)
661 print_services (serv);
665 for (j = 0; serv->s_aliases[j]; ++j)
666 if (strcmp (serv->s_aliases[j], key[i]) == 0)
668 print_services (serv);
678 if (isdigit (key[i][0]))
679 serv = getservbyport (htons (atol (key[i])), proto);
681 serv = getservbyname (key[i], proto);
686 print_services (serv);
693 /* This is for shadow */
695 print_shadow (struct spwd *sp)
698 sp->sp_namp ? sp->sp_namp : "",
699 sp->sp_pwdp ? sp->sp_pwdp : "");
701 #define SHADOW_FIELD(n) \
703 putchar_unlocked (':'); \
705 printf ("%ld:", sp->n)
707 SHADOW_FIELD (sp_lstchg);
708 SHADOW_FIELD (sp_min);
709 SHADOW_FIELD (sp_max);
710 SHADOW_FIELD (sp_warn);
711 SHADOW_FIELD (sp_inact);
712 SHADOW_FIELD (sp_expire);
713 if (sp->sp_flag == ~0ul)
714 putchar_unlocked ('\n');
716 printf ("%lu\n", sp->sp_flag);
720 shadow_keys (int number, char *key[])
730 while ((sp = getspent ()) != NULL)
736 for (i = 0; i < number; ++i)
740 sp = getspnam (key[i]);
754 int (*func) (int number, char *key[]);
757 #define D(name) { #name, name ## _keys },
774 /* Handle arguments found by argp. */
776 parse_option (int key, char *arg, struct argp_state *state)
782 for (i = 0; databases[i].name; ++i)
783 __nss_configure_lookup (databases[i].name, arg);
787 return ARGP_ERR_UNKNOWN;
798 char *short_doc, *long_doc, *doc, *p;
800 short_doc = _("getent - get entries from administrative database.");
801 long_doc = _("Supported databases:");
802 len = strlen (short_doc) + strlen (long_doc) + 3;
804 for (i = 0; databases[i].name; ++i)
805 len += strlen (databases[i].name) + 1;
807 doc = (char *) malloc (len);
812 p = stpcpy (doc, short_doc);
814 p = stpcpy (p, long_doc);
817 for (i = 0, j = 0; databases[i].name; ++i)
819 len = strlen (databases[i].name);
831 p = mempcpy (p, databases[i].name, len);
839 /* the main function */
841 main (int argc, char *argv[])
845 /* Set locale via LC_ALL. */
846 setlocale (LC_ALL, "");
847 /* Set the text message domain. */
848 textdomain (PACKAGE);
850 /* Build argp.doc. */
853 /* 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 (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);