.
[glibc/history.git] / nss / getent.c
blobea5608f1e2783edecef56d9b8809f5f8ad20b84b
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
18 02111-1307 USA. */
20 /* getent: get entries from administrative database. */
22 #include <aliases.h>
23 #include <argp.h>
24 #include <ctype.h>
25 #include <error.h>
26 #include <grp.h>
27 #include <libintl.h>
28 #include <locale.h>
29 #include <mcheck.h>
30 #include <netdb.h>
31 #include <pwd.h>
32 #include <shadow.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
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. */
43 #include <version.h>
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.");
64 /* Prototype for option handler. */
65 static error_t parse_option (int key, char *arg, struct argp_state *state);
67 /* Function to print some extra text in the help message. */
68 static char *more_help (int key, const char *text, void *input);
70 /* Data structure to communicate with argp functions. */
71 static struct argp argp =
73 args_options, parse_option, args_doc, doc, NULL, more_help
76 /* Print the version information. */
77 static void
78 print_version (FILE *stream, struct argp_state *state)
80 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
81 fprintf (stream, gettext ("\
82 Copyright (C) %s Free Software Foundation, Inc.\n\
83 This is free software; see the source for copying conditions. There is NO\n\
84 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
85 "), "2009");
86 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
89 /* This is for aliases */
90 static inline void
91 print_aliases (struct aliasent *alias)
93 unsigned int i = 0;
95 printf ("%s: ", alias->alias_name);
96 for (i = strlen (alias->alias_name); i < 14; ++i)
97 fputs_unlocked (" ", stdout);
99 for (i = 0; i < alias->alias_members_len; ++i)
100 printf ("%s%s",
101 alias->alias_members [i],
102 i + 1 == alias->alias_members_len ? "\n" : ", ");
105 static int
106 aliases_keys (int number, char *key[])
108 int result = 0;
109 int i;
110 struct aliasent *alias;
112 if (number == 0)
114 setaliasent ();
115 while ((alias = getaliasent ()) != NULL)
116 print_aliases (alias);
117 endaliasent ();
118 return result;
121 for (i = 0; i < number; ++i)
123 alias = getaliasbyname (key[i]);
125 if (alias == NULL)
126 result = 2;
127 else
128 print_aliases (alias);
131 return result;
134 /* This is for ethers */
135 static int
136 ethers_keys (int number, char *key[])
138 int result = 0;
139 int i;
141 if (number == 0)
143 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
144 return 3;
147 for (i = 0; i < number; ++i)
149 struct ether_addr *ethp, eth;
150 char buffer [1024], *p;
152 ethp = ether_aton (key[i]);
153 if (ethp != NULL)
155 if (ether_ntohost (buffer, ethp))
157 result = 2;
158 continue;
160 p = buffer;
162 else
164 if (ether_hostton (key[i], &eth))
166 result = 2;
167 continue;
169 p = key[i];
170 ethp = &eth;
172 printf ("%s %s\n", ether_ntoa (ethp), p);
175 return result;
178 /* This is for group */
179 static inline void
180 print_group (struct group *grp)
182 unsigned int i = 0;
184 printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
185 grp->gr_passwd ? grp->gr_passwd : "",
186 (unsigned long int) grp->gr_gid);
188 while (grp->gr_mem[i] != NULL)
190 fputs_unlocked (grp->gr_mem[i], stdout);
191 ++i;
192 if (grp->gr_mem[i] != NULL)
193 putchar_unlocked (',');
195 putchar_unlocked ('\n');
198 static int
199 group_keys (int number, char *key[])
201 int result = 0;
202 int i;
203 struct group *grp;
205 if (number == 0)
207 setgrent ();
208 while ((grp = getgrent ()) != NULL)
209 print_group (grp);
210 endgrent ();
211 return result;
214 for (i = 0; i < number; ++i)
216 errno = 0;
217 char *ep;
218 gid_t arg_gid = strtoul(key[i], &ep, 10);
220 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
221 /* Valid numeric gid. */
222 grp = getgrgid (arg_gid);
223 else
224 grp = getgrnam (key[i]);
226 if (grp == NULL)
227 result = 2;
228 else
229 print_group (grp);
232 return result;
235 /* This is for hosts */
236 static void
237 print_hosts (struct hostent *host)
239 unsigned int cnt;
241 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
243 char buf[INET6_ADDRSTRLEN];
244 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
245 buf, sizeof (buf));
247 printf ("%-15s %s", ip, host->h_name);
249 unsigned int i;
250 for (i = 0; host->h_aliases[i] != NULL; ++i)
252 putchar_unlocked (' ');
253 fputs_unlocked (host->h_aliases[i], stdout);
255 putchar_unlocked ('\n');
259 static int
260 hosts_keys (int number, char *key[])
262 int result = 0;
263 int i;
264 struct hostent *host;
266 if (number == 0)
268 sethostent (0);
269 while ((host = gethostent ()) != NULL)
270 print_hosts (host);
271 endhostent ();
272 return result;
275 for (i = 0; i < number; ++i)
277 struct hostent *host = NULL;
278 char addr[IN6ADDRSZ];
280 if (inet_pton (AF_INET6, key[i], &addr) > 0)
281 host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
282 else if (inet_pton (AF_INET, key[i], &addr) > 0)
283 host = gethostbyaddr (addr, INADDRSZ, AF_INET);
284 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
285 host = gethostbyname2 (key[i], AF_INET);
287 if (host == NULL)
288 result = 2;
289 else
290 print_hosts (host);
293 return result;
296 /* This is for hosts, but using getaddrinfo */
297 static int
298 ahosts_keys_int (int af, int xflags, int number, char *key[])
300 int result = 0;
301 int i;
302 struct hostent *host;
304 if (number == 0)
306 sethostent (0);
307 while ((host = gethostent ()) != NULL)
308 print_hosts (host);
309 endhostent ();
310 return result;
313 struct addrinfo hint;
314 memset (&hint, '\0', sizeof (hint));
315 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
316 hint.ai_family = af;
318 for (i = 0; i < number; ++i)
320 struct addrinfo *res;
322 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
323 result = 2;
324 else
326 struct addrinfo *runp = res;
328 while (runp != NULL)
330 char sockbuf[20];
331 const char *sockstr;
332 if (runp->ai_socktype == SOCK_STREAM)
333 sockstr = "STREAM";
334 else if (runp->ai_socktype == SOCK_DGRAM)
335 sockstr = "DGRAM";
336 else if (runp->ai_socktype == SOCK_RAW)
337 sockstr = "RAW";
338 #ifdef SOCK_SEQPACKET
339 else if (runp->ai_socktype == SOCK_SEQPACKET)
340 sockstr = "SEQPACKET";
341 #endif
342 #ifdef SOCK_RDM
343 else if (runp->ai_socktype == SOCK_RDM)
344 sockstr = "RDM";
345 #endif
346 #ifdef SOCK_DCCP
347 else if (runp->ai_socktype == SOCK_DCCP)
348 sockstr = "DCCP";
349 #endif
350 #ifdef SOCK_PACKET
351 else if (runp->ai_socktype == SOCK_PACKET)
352 sockstr = "PACKET";
353 #endif
354 else
356 snprintf (sockbuf, sizeof (sockbuf), "%d",
357 runp->ai_socktype);
358 sockstr = sockbuf;
361 char buf[INET6_ADDRSTRLEN];
362 printf ("%-15s %-6s %s\n",
363 inet_ntop (runp->ai_family,
364 runp->ai_family == AF_INET
365 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
366 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
367 buf, sizeof (buf)),
368 sockstr,
369 runp->ai_canonname ?: "");
371 runp = runp->ai_next;
374 freeaddrinfo (res);
378 return result;
381 static int
382 ahosts_keys (int number, char *key[])
384 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
387 static int
388 ahostsv4_keys (int number, char *key[])
390 return ahosts_keys_int (AF_INET, 0, number, key);
393 static int
394 ahostsv6_keys (int number, char *key[])
396 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
399 /* This is for netgroup */
400 static int
401 netgroup_keys (int number, char *key[])
403 int result = 0;
404 int i;
406 if (number == 0)
408 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
409 return 3;
412 for (i = 0; i < number; ++i)
414 if (!setnetgrent (key[i]))
415 result = 2;
416 else
418 char *p[3];
420 printf ("%-21s", key[i]);
422 while (getnetgrent (p, p + 1, p + 2))
423 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
424 putchar_unlocked ('\n');
428 endnetgrent ();
430 return result;
433 /* This is for networks */
434 static void
435 print_networks (struct netent *net)
437 unsigned int i;
438 struct in_addr ip;
439 ip.s_addr = htonl (net->n_net);
441 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
443 i = 0;
444 while (net->n_aliases[i] != NULL)
446 putchar_unlocked (' ');
447 fputs_unlocked (net->n_aliases[i], stdout);
448 ++i;
450 putchar_unlocked ('\n');
453 static int
454 networks_keys (int number, char *key[])
456 int result = 0;
457 int i;
458 struct netent *net;
460 if (number == 0)
462 setnetent (0);
463 while ((net = getnetent ()) != NULL)
464 print_networks (net);
465 endnetent ();
466 return result;
469 for (i = 0; i < number; ++i)
471 if (isdigit (key[i][0]))
472 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
473 else
474 net = getnetbyname (key[i]);
476 if (net == NULL)
477 result = 2;
478 else
479 print_networks (net);
482 return result;
485 /* Now is all for passwd */
486 static inline void
487 print_passwd (struct passwd *pwd)
489 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
490 pwd->pw_name ? pwd->pw_name : "",
491 pwd->pw_passwd ? pwd->pw_passwd : "",
492 (unsigned long int) pwd->pw_uid,
493 (unsigned long int) pwd->pw_gid,
494 pwd->pw_gecos ? pwd->pw_gecos : "",
495 pwd->pw_dir ? pwd->pw_dir : "",
496 pwd->pw_shell ? pwd->pw_shell : "");
499 static int
500 passwd_keys (int number, char *key[])
502 int result = 0;
503 int i;
504 struct passwd *pwd;
506 if (number == 0)
508 setpwent ();
509 while ((pwd = getpwent ()) != NULL)
510 print_passwd (pwd);
511 endpwent ();
512 return result;
515 for (i = 0; i < number; ++i)
517 errno = 0;
518 char *ep;
519 uid_t arg_uid = strtoul(key[i], &ep, 10);
521 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
522 /* Valid numeric uid. */
523 pwd = getpwuid (arg_uid);
524 else
525 pwd = getpwnam (key[i]);
527 if (pwd == NULL)
528 result = 2;
529 else
530 print_passwd (pwd);
533 return result;
536 /* This is for protocols */
537 static inline void
538 print_protocols (struct protoent *proto)
540 unsigned int i;
542 printf ("%-21s %d", proto->p_name, proto->p_proto);
544 i = 0;
545 while (proto->p_aliases[i] != NULL)
547 putchar_unlocked (' ');
548 fputs_unlocked (proto->p_aliases[i], stdout);
549 ++i;
551 putchar_unlocked ('\n');
554 static int
555 protocols_keys (int number, char *key[])
557 int result = 0;
558 int i;
559 struct protoent *proto;
561 if (number == 0)
563 setprotoent (0);
564 while ((proto = getprotoent ()) != NULL)
565 print_protocols (proto);
566 endprotoent ();
567 return result;
570 for (i = 0; i < number; ++i)
572 if (isdigit (key[i][0]))
573 proto = getprotobynumber (atol (key[i]));
574 else
575 proto = getprotobyname (key[i]);
577 if (proto == NULL)
578 result = 2;
579 else
580 print_protocols (proto);
583 return result;
586 /* Now is all for rpc */
587 static inline void
588 print_rpc (struct rpcent *rpc)
590 int i;
592 printf ("%-15s %d%s",
593 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
595 for (i = 0; rpc->r_aliases[i]; ++i)
596 printf (" %s", rpc->r_aliases[i]);
597 putchar_unlocked ('\n');
600 static int
601 rpc_keys (int number, char *key[])
603 int result = 0;
604 int i;
605 struct rpcent *rpc;
607 if (number == 0)
609 setrpcent (0);
610 while ((rpc = getrpcent ()) != NULL)
611 print_rpc (rpc);
612 endrpcent ();
613 return result;
616 for (i = 0; i < number; ++i)
618 if (isdigit (key[i][0]))
619 rpc = getrpcbynumber (atol (key[i]));
620 else
621 rpc = getrpcbyname (key[i]);
623 if (rpc == NULL)
624 result = 2;
625 else
626 print_rpc (rpc);
629 return result;
632 /* for services */
633 static void
634 print_services (struct servent *serv)
636 unsigned int i;
638 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
640 i = 0;
641 while (serv->s_aliases[i] != NULL)
643 putchar_unlocked (' ');
644 fputs_unlocked (serv->s_aliases[i], stdout);
645 ++i;
647 putchar_unlocked ('\n');
650 static int
651 services_keys (int number, char *key[])
653 int result = 0;
654 int i;
655 struct servent *serv;
657 if (!number)
659 setservent (0);
660 while ((serv = getservent ()) != NULL)
661 print_services (serv);
662 endservent ();
663 return result;
666 for (i = 0; i < number; ++i)
668 struct servent *serv;
669 char *proto = strchr (key[i], '/');
671 if (proto != NULL)
672 *proto++ = '\0';
674 if (isdigit (key[i][0]))
675 serv = getservbyport (htons (atol (key[i])), proto);
676 else
677 serv = getservbyname (key[i], proto);
679 if (serv == NULL)
680 result = 2;
681 else
682 print_services (serv);
685 return result;
688 /* This is for shadow */
689 static void
690 print_shadow (struct spwd *sp)
692 printf ("%s:%s:",
693 sp->sp_namp ? sp->sp_namp : "",
694 sp->sp_pwdp ? sp->sp_pwdp : "");
696 #define SHADOW_FIELD(n) \
697 if (sp->n == -1) \
698 putchar_unlocked (':'); \
699 else \
700 printf ("%ld:", sp->n)
702 SHADOW_FIELD (sp_lstchg);
703 SHADOW_FIELD (sp_min);
704 SHADOW_FIELD (sp_max);
705 SHADOW_FIELD (sp_warn);
706 SHADOW_FIELD (sp_inact);
707 SHADOW_FIELD (sp_expire);
708 if (sp->sp_flag == ~0ul)
709 putchar_unlocked ('\n');
710 else
711 printf ("%lu\n", sp->sp_flag);
714 static int
715 shadow_keys (int number, char *key[])
717 int result = 0;
718 int i;
720 if (number == 0)
722 struct spwd *sp;
724 setspent ();
725 while ((sp = getspent ()) != NULL)
726 print_shadow (sp);
727 endpwent ();
728 return result;
731 for (i = 0; i < number; ++i)
733 struct spwd *sp;
735 sp = getspnam (key[i]);
737 if (sp == NULL)
738 result = 2;
739 else
740 print_shadow (sp);
743 return result;
746 struct
748 const char *name;
749 int (*func) (int number, char *key[]);
750 } databases[] =
752 #define D(name) { #name, name ## _keys },
753 D(ahosts)
754 D(ahostsv4)
755 D(ahostsv6)
756 D(aliases)
757 D(ethers)
758 D(group)
759 D(hosts)
760 D(netgroup)
761 D(networks)
762 D(passwd)
763 D(protocols)
764 D(rpc)
765 D(services)
766 D(shadow)
767 #undef D
768 { NULL, NULL }
771 /* Handle arguments found by argp. */
772 static error_t
773 parse_option (int key, char *arg, struct argp_state *state)
775 char *endp;
776 switch (key)
778 case 's':
779 endp = strchr (arg, ':');
780 if (endp == NULL)
781 /* No specific database, change them all. */
782 for (int i = 0; databases[i].name != NULL; ++i)
783 __nss_configure_lookup (databases[i].name, arg);
784 else
786 int i;
787 for (i = 0; databases[i].name != NULL; ++i)
788 if (strncmp (databases[i].name, arg, endp - arg) == 0)
790 __nss_configure_lookup (databases[i].name, endp + 1);
791 break;
793 if (databases[i].name == NULL)
794 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
796 break;
798 default:
799 return ARGP_ERR_UNKNOWN;
802 return 0;
806 static char *
807 more_help (int key, const char *text, void *input)
809 switch (key)
811 size_t len;
812 char *doc;
813 FILE *fp;
815 case ARGP_KEY_HELP_EXTRA:
816 /* We print some extra information. */
817 fp = open_memstream (&doc, &len);
818 if (fp != NULL)
820 fputs_unlocked (_("Supported databases:\n"), fp);
822 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
824 len = strlen (databases[i].name);
825 if (i != 0)
827 if (col + len > 72)
829 col = 0;
830 fputc_unlocked ('\n', fp);
832 else
833 fputc_unlocked (' ', fp);
836 fputs_unlocked (databases[i].name, fp);
837 col += len + 1;
840 fputs ("\n\n", fp);
842 fprintf (fp, gettext ("\
843 For bug reporting instructions, please see:\n\
844 <http://www.gnu.org/software/libc/bugs.html>.\n"));
846 if (fclose (fp) == 0)
847 return doc;
849 break;
851 default:
852 break;
854 return (char *) text;
858 /* the main function */
860 main (int argc, char *argv[])
862 /* Debugging support. */
863 mtrace ();
865 /* Set locale via LC_ALL. */
866 setlocale (LC_ALL, "");
867 /* Set the text message domain. */
868 textdomain (PACKAGE);
870 /* Parse and process arguments. */
871 int remaining;
872 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
874 if ((argc - remaining) < 1)
876 error (0, 0, gettext ("wrong number of arguments"));
877 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
878 return 1;
881 for (int i = 0; databases[i].name; ++i)
882 if (argv[remaining][0] == databases[i].name[0]
883 && !strcmp (argv[remaining], databases[i].name))
884 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
886 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
887 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
888 return 1;