* elf/soinit.c (__EH_FRAME_BEGIN__): Remove.
[glibc/pb-stable.git] / nss / getent.c
blobc8173d0b517ca8e021c69f40875952ceb7a34356
1 /* Copyright (c) 1998-2007, 2008 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.\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. */
79 static void
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\
87 "), "2008");
88 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
91 /* This is for aliases */
92 static inline void
93 print_aliases (struct aliasent *alias)
95 unsigned int i = 0;
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)
102 printf ("%s%s",
103 alias->alias_members [i],
104 i + 1 == alias->alias_members_len ? "\n" : ", ");
107 static int
108 aliases_keys (int number, char *key[])
110 int result = 0;
111 int i;
112 struct aliasent *alias;
114 if (number == 0)
116 setaliasent ();
117 while ((alias = getaliasent ()) != NULL)
118 print_aliases (alias);
119 endaliasent ();
120 return result;
123 for (i = 0; i < number; ++i)
125 alias = getaliasbyname (key[i]);
127 if (alias == NULL)
128 result = 2;
129 else
130 print_aliases (alias);
133 return result;
136 /* This is for ethers */
137 static int
138 ethers_keys (int number, char *key[])
140 int result = 0;
141 int i;
143 if (number == 0)
145 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
146 return 3;
149 for (i = 0; i < number; ++i)
151 struct ether_addr *ethp, eth;
152 char buffer [1024], *p;
154 ethp = ether_aton (key[i]);
155 if (ethp != NULL)
157 if (ether_ntohost (buffer, ethp))
159 result = 2;
160 continue;
162 p = buffer;
164 else
166 if (ether_hostton (key[i], &eth))
168 result = 2;
169 continue;
171 p = key[i];
172 ethp = &eth;
174 printf ("%s %s\n", ether_ntoa (ethp), p);
177 return result;
180 /* This is for group */
181 static inline void
182 print_group (struct group *grp)
184 unsigned int i = 0;
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);
193 ++i;
194 if (grp->gr_mem[i] != NULL)
195 putchar_unlocked (',');
197 putchar_unlocked ('\n');
200 static int
201 group_keys (int number, char *key[])
203 int result = 0;
204 int i;
205 struct group *grp;
207 if (number == 0)
209 setgrent ();
210 while ((grp = getgrent ()) != NULL)
211 print_group (grp);
212 endgrent ();
213 return result;
216 for (i = 0; i < number; ++i)
218 errno = 0;
219 char *ep;
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);
225 else
226 grp = getgrnam (key[i]);
228 if (grp == NULL)
229 result = 2;
230 else
231 print_group (grp);
234 return result;
237 /* This is for hosts */
238 static void
239 print_hosts (struct hostent *host)
241 unsigned int cnt;
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],
247 buf, sizeof (buf));
249 printf ("%-15s %s", ip, host->h_name);
251 unsigned int i;
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');
261 static int
262 hosts_keys (int number, char *key[])
264 int result = 0;
265 int i;
266 struct hostent *host;
268 if (number == 0)
270 sethostent (0);
271 while ((host = gethostent ()) != NULL)
272 print_hosts (host);
273 endhostent ();
274 return result;
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);
289 if (host == NULL)
290 result = 2;
291 else
292 print_hosts (host);
295 return result;
298 /* This is for hosts, but using getaddrinfo */
299 static int
300 ahosts_keys_int (int af, int xflags, int number, char *key[])
302 int result = 0;
303 int i;
304 struct hostent *host;
306 if (number == 0)
308 sethostent (0);
309 while ((host = gethostent ()) != NULL)
310 print_hosts (host);
311 endhostent ();
312 return result;
315 struct addrinfo hint;
316 memset (&hint, '\0', sizeof (hint));
317 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
318 hint.ai_family = af;
320 for (i = 0; i < number; ++i)
322 struct addrinfo *res;
324 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
325 result = 2;
326 else
328 struct addrinfo *runp = res;
330 while (runp != NULL)
332 char sockbuf[20];
333 const char *sockstr;
334 if (runp->ai_socktype == SOCK_STREAM)
335 sockstr = "STREAM";
336 else if (runp->ai_socktype == SOCK_DGRAM)
337 sockstr = "DGRAM";
338 else if (runp->ai_socktype == SOCK_RAW)
339 sockstr = "RAW";
340 #ifdef SOCK_SEQPACKET
341 else if (runp->ai_socktype == SOCK_SEQPACKET)
342 sockstr = "SEQPACKET";
343 #endif
344 #ifdef SOCK_RDM
345 else if (runp->ai_socktype == SOCK_RDM)
346 sockstr = "RDM";
347 #endif
348 #ifdef SOCK_DCCP
349 else if (runp->ai_socktype == SOCK_DCCP)
350 sockstr = "DCCP";
351 #endif
352 #ifdef SOCK_PACKET
353 else if (runp->ai_socktype == SOCK_PACKET)
354 sockstr = "PACKET";
355 #endif
356 else
358 snprintf (sockbuf, sizeof (sockbuf), "%d",
359 runp->ai_socktype);
360 sockstr = sockbuf;
363 char buf[INET6_ADDRSTRLEN];
364 printf ("%-15s %-6s %s\n",
365 inet_ntop (runp->ai_family,
366 runp->ai_family == AF_INET
367 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
368 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
369 buf, sizeof (buf)),
370 sockstr,
371 runp->ai_canonname ?: "");
373 runp = runp->ai_next;
376 freeaddrinfo (res);
380 return result;
383 static int
384 ahosts_keys (int number, char *key[])
386 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
389 static int
390 ahostsv4_keys (int number, char *key[])
392 return ahosts_keys_int (AF_INET, 0, number, key);
395 static int
396 ahostsv6_keys (int number, char *key[])
398 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
401 /* This is for netgroup */
402 static int
403 netgroup_keys (int number, char *key[])
405 int result = 0;
406 int i;
408 if (number == 0)
410 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
411 return 3;
414 for (i = 0; i < number; ++i)
416 if (!setnetgrent (key[i]))
417 result = 2;
418 else
420 char *p[3];
422 printf ("%-21s", key[i]);
424 while (getnetgrent (p, p + 1, p + 2))
425 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
426 putchar_unlocked ('\n');
430 endnetgrent ();
432 return result;
435 /* This is for networks */
436 static void
437 print_networks (struct netent *net)
439 unsigned int i;
440 struct in_addr ip;
441 ip.s_addr = htonl (net->n_net);
443 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
445 i = 0;
446 while (net->n_aliases[i] != NULL)
448 putchar_unlocked (' ');
449 fputs_unlocked (net->n_aliases[i], stdout);
450 ++i;
451 if (net->n_aliases[i] != NULL)
452 putchar_unlocked (',');
454 putchar_unlocked ('\n');
457 static int
458 networks_keys (int number, char *key[])
460 int result = 0;
461 int i;
462 struct netent *net;
464 if (number == 0)
466 setnetent (0);
467 while ((net = getnetent ()) != NULL)
468 print_networks (net);
469 endnetent ();
470 return result;
473 for (i = 0; i < number; ++i)
475 if (isdigit (key[i][0]))
476 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
477 else
478 net = getnetbyname (key[i]);
480 if (net == NULL)
481 result = 2;
482 else
483 print_networks (net);
486 return result;
489 /* Now is all for passwd */
490 static inline void
491 print_passwd (struct passwd *pwd)
493 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
494 pwd->pw_name ? pwd->pw_name : "",
495 pwd->pw_passwd ? pwd->pw_passwd : "",
496 (unsigned long int) pwd->pw_uid,
497 (unsigned long int) pwd->pw_gid,
498 pwd->pw_gecos ? pwd->pw_gecos : "",
499 pwd->pw_dir ? pwd->pw_dir : "",
500 pwd->pw_shell ? pwd->pw_shell : "");
503 static int
504 passwd_keys (int number, char *key[])
506 int result = 0;
507 int i;
508 struct passwd *pwd;
510 if (number == 0)
512 setpwent ();
513 while ((pwd = getpwent ()) != NULL)
514 print_passwd (pwd);
515 endpwent ();
516 return result;
519 for (i = 0; i < number; ++i)
521 errno = 0;
522 char *ep;
523 uid_t arg_uid = strtoul(key[i], &ep, 10);
525 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
526 /* Valid numeric uid. */
527 pwd = getpwuid (arg_uid);
528 else
529 pwd = getpwnam (key[i]);
531 if (pwd == NULL)
532 result = 2;
533 else
534 print_passwd (pwd);
537 return result;
540 /* This is for protocols */
541 static inline void
542 print_protocols (struct protoent *proto)
544 unsigned int i;
546 printf ("%-21s %d", proto->p_name, proto->p_proto);
548 i = 0;
549 while (proto->p_aliases[i] != NULL)
551 putchar_unlocked (' ');
552 fputs_unlocked (proto->p_aliases[i], stdout);
553 ++i;
555 putchar_unlocked ('\n');
558 static int
559 protocols_keys (int number, char *key[])
561 int result = 0;
562 int i;
563 struct protoent *proto;
565 if (number == 0)
567 setprotoent (0);
568 while ((proto = getprotoent ()) != NULL)
569 print_protocols (proto);
570 endprotoent ();
571 return result;
574 for (i = 0; i < number; ++i)
576 if (isdigit (key[i][0]))
577 proto = getprotobynumber (atol (key[i]));
578 else
579 proto = getprotobyname (key[i]);
581 if (proto == NULL)
582 result = 2;
583 else
584 print_protocols (proto);
587 return result;
590 /* Now is all for rpc */
591 static inline void
592 print_rpc (struct rpcent *rpc)
594 int i;
596 printf ("%-15s %d%s",
597 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
599 for (i = 0; rpc->r_aliases[i]; ++i)
600 printf (" %s", rpc->r_aliases[i]);
601 putchar_unlocked ('\n');
604 static int
605 rpc_keys (int number, char *key[])
607 int result = 0;
608 int i;
609 struct rpcent *rpc;
611 if (number == 0)
613 setrpcent (0);
614 while ((rpc = getrpcent ()) != NULL)
615 print_rpc (rpc);
616 endrpcent ();
617 return result;
620 for (i = 0; i < number; ++i)
622 if (isdigit (key[i][0]))
623 rpc = getrpcbynumber (atol (key[i]));
624 else
625 rpc = getrpcbyname (key[i]);
627 if (rpc == NULL)
628 result = 2;
629 else
630 print_rpc (rpc);
633 return result;
636 /* for services */
637 static void
638 print_services (struct servent *serv)
640 unsigned int i;
642 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
644 i = 0;
645 while (serv->s_aliases[i] != NULL)
647 putchar_unlocked (' ');
648 fputs_unlocked (serv->s_aliases[i], stdout);
649 ++i;
651 putchar_unlocked ('\n');
654 static int
655 services_keys (int number, char *key[])
657 int result = 0;
658 int i;
659 struct servent *serv;
661 if (!number)
663 setservent (0);
664 while ((serv = getservent ()) != NULL)
665 print_services (serv);
666 endservent ();
667 return result;
670 for (i = 0; i < number; ++i)
672 struct servent *serv;
673 char *proto = strchr (key[i], '/');
675 if (proto != NULL)
676 *proto++ = '\0';
678 if (isdigit (key[i][0]))
679 serv = getservbyport (htons (atol (key[i])), proto);
680 else
681 serv = getservbyname (key[i], proto);
683 if (serv == NULL)
684 result = 2;
685 else
686 print_services (serv);
689 return result;
692 /* This is for shadow */
693 static void
694 print_shadow (struct spwd *sp)
696 printf ("%s:%s:",
697 sp->sp_namp ? sp->sp_namp : "",
698 sp->sp_pwdp ? sp->sp_pwdp : "");
700 #define SHADOW_FIELD(n) \
701 if (sp->n == -1) \
702 putchar_unlocked (':'); \
703 else \
704 printf ("%ld:", sp->n)
706 SHADOW_FIELD (sp_lstchg);
707 SHADOW_FIELD (sp_min);
708 SHADOW_FIELD (sp_max);
709 SHADOW_FIELD (sp_warn);
710 SHADOW_FIELD (sp_inact);
711 SHADOW_FIELD (sp_expire);
712 if (sp->sp_flag == ~0ul)
713 putchar_unlocked ('\n');
714 else
715 printf ("%lu\n", sp->sp_flag);
718 static int
719 shadow_keys (int number, char *key[])
721 int result = 0;
722 int i;
724 if (number == 0)
726 struct spwd *sp;
728 setspent ();
729 while ((sp = getspent ()) != NULL)
730 print_shadow (sp);
731 endpwent ();
732 return result;
735 for (i = 0; i < number; ++i)
737 struct spwd *sp;
739 sp = getspnam (key[i]);
741 if (sp == NULL)
742 result = 2;
743 else
744 print_shadow (sp);
747 return result;
750 struct
752 const char *name;
753 int (*func) (int number, char *key[]);
754 } databases[] =
756 #define D(name) { #name, name ## _keys },
757 D(ahosts)
758 D(ahostsv4)
759 D(ahostsv6)
760 D(aliases)
761 D(ethers)
762 D(group)
763 D(hosts)
764 D(netgroup)
765 D(networks)
766 D(passwd)
767 D(protocols)
768 D(rpc)
769 D(services)
770 D(shadow)
771 #undef D
772 { NULL, NULL }
775 /* Handle arguments found by argp. */
776 static error_t
777 parse_option (int key, char *arg, struct argp_state *state)
779 char *endp;
780 switch (key)
782 case 's':
783 endp = strchr (arg, ':');
784 if (endp == NULL)
785 /* No specific database, change them all. */
786 for (int i = 0; databases[i].name != NULL; ++i)
787 __nss_configure_lookup (databases[i].name, arg);
788 else
790 int i;
791 for (i = 0; databases[i].name != NULL; ++i)
792 if (strncmp (databases[i].name, arg, endp - arg) == 0)
794 __nss_configure_lookup (databases[i].name, endp + 1);
795 break;
797 if (databases[i].name == NULL)
798 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
800 break;
802 default:
803 return ARGP_ERR_UNKNOWN;
806 return 0;
810 static char *
811 more_help (int key, const char *text, void *input)
813 switch (key)
815 size_t len;
816 char *doc;
817 FILE *fp;
819 case ARGP_KEY_HELP_EXTRA:
820 /* We print some extra information. */
821 fp = open_memstream (&doc, &len);
822 if (fp != NULL)
824 fputs_unlocked (_("Supported databases:\n"), fp);
826 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
828 len = strlen (databases[i].name);
829 if (i != 0)
831 if (col + len > 72)
833 col = 0;
834 fputc_unlocked ('\n', fp);
836 else
837 fputc_unlocked (' ', fp);
840 fputs_unlocked (databases[i].name, fp);
841 col += len + 1;
844 if (fclose (fp) == 0)
845 return doc;
847 break;
849 default:
850 break;
852 return (char *) text;
856 /* the main function */
858 main (int argc, char *argv[])
860 /* Debugging support. */
861 mtrace ();
863 /* Set locale via LC_ALL. */
864 setlocale (LC_ALL, "");
865 /* Set the text message domain. */
866 textdomain (PACKAGE);
868 /* Parse and process arguments. */
869 int remaining;
870 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
872 if ((argc - remaining) < 1)
874 error (0, 0, gettext ("wrong number of arguments"));
875 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
876 return 1;
879 for (int i = 0; databases[i].name; ++i)
880 if (argv[remaining][0] == databases[i].name[0]
881 && !strcmp (argv[remaining], databases[i].name))
882 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
884 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
885 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
886 return 1;