Updated to fedora-glibc-20050208T0948
[glibc.git] / nss / getent.c
blob0aea5eed2ae4bc9424751011c50987569edaddb4
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
18 02111-1307 USA. */
20 /* getent: get entries from administrative database. */
22 #include <aliases.h>
23 #include <argp.h>
24 #include <grp.h>
25 #include <pwd.h>
26 #include <shadow.h>
27 #include <ctype.h>
28 #include <error.h>
29 #include <libintl.h>
30 #include <locale.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
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. */
42 #include <version.h>
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 /* Short description of program. */
61 static const char doc[] = N_("Get entries from administrative database.\v\
62 For bug reporting instructions, please see:\n\
63 <http://www.gnu.org/software/libc/bugs.html>.\n");
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. */
78 static void
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\
86 "), "2005");
87 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
90 /* This is for aliases */
91 static inline void
92 print_aliases (struct aliasent *alias)
94 unsigned int i = 0;
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)
101 printf ("%s%s",
102 alias->alias_members [i],
103 i + 1 == alias->alias_members_len ? "\n" : ", ");
106 static int
107 aliases_keys (int number, char *key[])
109 int result = 0;
110 int i;
111 struct aliasent *alias;
113 if (number == 0)
115 setaliasent ();
116 while ((alias = getaliasent ()) != NULL)
117 print_aliases (alias);
118 endaliasent ();
119 return result;
122 for (i = 0; i < number; ++i)
124 alias = getaliasbyname (key[i]);
126 if (alias == NULL)
127 result = 2;
128 else
129 print_aliases (alias);
132 return result;
135 /* This is for ethers */
136 static int
137 ethers_keys (int number, char *key[])
139 int result = 0;
140 int i;
142 if (number == 0)
144 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
145 return 3;
148 for (i = 0; i < number; ++i)
150 struct ether_addr *ethp, eth;
151 char buffer [1024], *p;
153 ethp = ether_aton (key[i]);
154 if (ethp != NULL)
156 if (ether_ntohost (buffer, ethp))
158 result = 2;
159 continue;
161 p = buffer;
163 else
165 if (ether_hostton (key[i], &eth))
167 result = 2;
168 continue;
170 p = key[i];
171 ethp = &eth;
173 printf ("%s %s\n", ether_ntoa (ethp), p);
176 return result;
179 /* This is for group */
180 static inline void
181 print_group (struct group *grp)
183 unsigned int i = 0;
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);
192 ++i;
193 if (grp->gr_mem[i] != NULL)
194 putchar_unlocked (',');
196 putchar_unlocked ('\n');
199 static int
200 group_keys (int number, char *key[])
202 int result = 0;
203 int i;
204 struct group *grp;
206 if (number == 0)
208 setgrent ();
209 while ((grp = getgrent ()) != NULL)
210 print_group (grp);
211 endgrent ();
212 return result;
215 for (i = 0; i < number; ++i)
217 errno = 0;
218 char *ep;
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);
224 else
225 grp = getgrnam (key[i]);
227 if (grp == NULL)
228 result = 2;
229 else
230 print_group (grp);
233 return result;
236 /* This is for hosts */
237 static void
238 print_hosts (struct hostent *host)
240 unsigned int cnt;
242 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
244 char buf[INET6_ADDRSTRLEN];
245 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
246 buf, sizeof (buf));
248 printf ("%-15s %s", ip, host->h_name);
250 unsigned int i;
251 for (i = 0; host->h_aliases[i] != NULL; ++i)
253 putchar_unlocked (' ');
254 fputs_unlocked (host->h_aliases[i], stdout);
256 putchar_unlocked ('\n');
260 static int
261 hosts_keys (int number, char *key[])
263 int result = 0;
264 int i;
265 struct hostent *host;
267 if (number == 0)
269 sethostent (0);
270 while ((host = gethostent ()) != NULL)
271 print_hosts (host);
272 endhostent ();
273 return result;
276 for (i = 0; i < number; ++i)
278 struct hostent *host = NULL;
279 char addr[IN6ADDRSZ];
281 if (inet_pton (AF_INET6, key[i], &addr) > 0)
282 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
283 else if (inet_pton (AF_INET, key[i], &addr) > 0)
284 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
285 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
286 host = gethostbyname2 (key[i], AF_INET);
288 if (host == NULL)
289 result = 2;
290 else
291 print_hosts (host);
294 return result;
297 /* This is for hosts, but using getaddrinfo */
298 static int
299 ahosts_keys_int (int af, int xflags, int number, char *key[])
301 int result = 0;
302 int i;
303 struct hostent *host;
305 if (number == 0)
307 sethostent (0);
308 while ((host = gethostent ()) != NULL)
309 print_hosts (host);
310 endhostent ();
311 return result;
314 struct addrinfo hint;
315 memset (&hint, '\0', sizeof (hint));
316 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
317 hint.ai_family = af;
319 for (i = 0; i < number; ++i)
321 struct addrinfo *res;
323 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
324 result = 2;
325 else
327 struct addrinfo *runp = res;
329 while (runp != NULL)
331 char sockbuf[20];
332 const char *sockstr;
333 if (runp->ai_socktype == SOCK_STREAM)
334 sockstr = "STREAM";
335 else if (runp->ai_socktype == SOCK_DGRAM)
336 sockstr = "DGRAM";
337 else if (runp->ai_socktype == SOCK_RAW)
338 sockstr = "RAW";
339 else
341 snprintf (sockbuf, sizeof (sockbuf), "%d",
342 runp->ai_socktype);
343 sockstr = sockbuf;
346 char buf[INET6_ADDRSTRLEN];
347 printf ("%-15s %-6s %s\n",
348 inet_ntop (runp->ai_family,
349 runp->ai_family == AF_INET
350 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
351 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
352 buf, sizeof (buf)),
353 sockstr,
354 runp->ai_canonname ?: "");
356 runp = runp->ai_next;
359 freeaddrinfo (res);
363 return result;
366 static int
367 ahosts_keys (int number, char *key[])
369 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
372 static int
373 ahostsv4_keys (int number, char *key[])
375 return ahosts_keys_int (AF_INET, 0, number, key);
378 static int
379 ahostsv6_keys (int number, char *key[])
381 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
384 /* This is for netgroup */
385 static int
386 netgroup_keys (int number, char *key[])
388 int result = 0;
389 int i;
391 if (number == 0)
393 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
394 return 3;
397 for (i = 0; i < number; ++i)
399 if (!setnetgrent (key[i]))
400 result = 2;
401 else
403 char *p[3];
405 printf ("%-21s", key[i]);
407 while (getnetgrent (p, p + 1, p + 2))
408 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
409 putchar_unlocked ('\n');
413 return result;
416 /* This is for networks */
417 static void
418 print_networks (struct netent *net)
420 unsigned int i;
421 struct in_addr ip;
422 ip.s_addr = htonl (net->n_net);
424 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
426 i = 0;
427 while (net->n_aliases[i] != NULL)
429 putchar_unlocked (' ');
430 fputs_unlocked (net->n_aliases[i], stdout);
431 ++i;
432 if (net->n_aliases[i] != NULL)
433 putchar_unlocked (',');
435 putchar_unlocked ('\n');
438 static int
439 networks_keys (int number, char *key[])
441 int result = 0;
442 int i;
443 struct netent *net;
445 if (number == 0)
447 setnetent (0);
448 while ((net = getnetent ()) != NULL)
449 print_networks (net);
450 endnetent ();
451 return result;
454 for (i = 0; i < number; ++i)
456 if (isdigit (key[i][0]))
457 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
458 else
459 net = getnetbyname (key[i]);
461 if (net == NULL)
462 result = 2;
463 else
464 print_networks (net);
467 return result;
470 /* Now is all for passwd */
471 static inline void
472 print_passwd (struct passwd *pwd)
474 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
475 pwd->pw_name ? pwd->pw_name : "",
476 pwd->pw_passwd ? pwd->pw_passwd : "",
477 (unsigned long int) pwd->pw_uid,
478 (unsigned long int) pwd->pw_gid,
479 pwd->pw_gecos ? pwd->pw_gecos : "",
480 pwd->pw_dir ? pwd->pw_dir : "",
481 pwd->pw_shell ? pwd->pw_shell : "");
484 static int
485 passwd_keys (int number, char *key[])
487 int result = 0;
488 int i;
489 struct passwd *pwd;
491 if (number == 0)
493 setpwent ();
494 while ((pwd = getpwent ()) != NULL)
495 print_passwd (pwd);
496 endpwent ();
497 return result;
500 for (i = 0; i < number; ++i)
502 errno = 0;
503 char *ep;
504 uid_t arg_uid = strtoul(key[i], &ep, 10);
506 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
507 /* Valid numeric uid. */
508 pwd = getpwuid (arg_uid);
509 else
510 pwd = getpwnam (key[i]);
512 if (pwd == NULL)
513 result = 2;
514 else
515 print_passwd (pwd);
518 return result;
521 /* This is for protocols */
522 static inline void
523 print_protocols (struct protoent *proto)
525 unsigned int i;
527 printf ("%-21s %d", proto->p_name, proto->p_proto);
529 i = 0;
530 while (proto->p_aliases[i] != NULL)
532 putchar_unlocked (' ');
533 fputs_unlocked (proto->p_aliases[i], stdout);
534 ++i;
536 putchar_unlocked ('\n');
539 static int
540 protocols_keys (int number, char *key[])
542 int result = 0;
543 int i;
544 struct protoent *proto;
546 if (number == 0)
548 setprotoent (0);
549 while ((proto = getprotoent ()) != NULL)
550 print_protocols (proto);
551 endprotoent ();
552 return result;
555 for (i = 0; i < number; ++i)
557 if (isdigit (key[i][0]))
558 proto = getprotobynumber (atol (key[i]));
559 else
560 proto = getprotobyname (key[i]);
562 if (proto == NULL)
563 result = 2;
564 else
565 print_protocols (proto);
568 return result;
571 /* Now is all for rpc */
572 static inline void
573 print_rpc (struct rpcent *rpc)
575 int i;
577 printf ("%-15s %d%s",
578 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
580 for (i = 0; rpc->r_aliases[i]; ++i)
581 printf (" %s", rpc->r_aliases[i]);
582 putchar_unlocked ('\n');
585 static int
586 rpc_keys (int number, char *key[])
588 int result = 0;
589 int i;
590 struct rpcent *rpc;
592 if (number == 0)
594 setrpcent (0);
595 while ((rpc = getrpcent ()) != NULL)
596 print_rpc (rpc);
597 endrpcent ();
598 return result;
601 for (i = 0; i < number; ++i)
603 if (isdigit (key[i][0]))
604 rpc = getrpcbynumber (atol (key[i]));
605 else
606 rpc = getrpcbyname (key[i]);
608 if (rpc == NULL)
609 result = 2;
610 else
611 print_rpc (rpc);
614 return result;
617 /* for services */
618 static void
619 print_services (struct servent *serv)
621 unsigned int i;
623 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
625 i = 0;
626 while (serv->s_aliases[i] != NULL)
628 putchar_unlocked (' ');
629 fputs_unlocked (serv->s_aliases[i], stdout);
630 ++i;
632 putchar_unlocked ('\n');
635 static int
636 services_keys (int number, char *key[])
638 int result = 0;
639 int i;
640 struct servent *serv;
642 if (!number)
644 setservent (0);
645 while ((serv = getservent ()) != NULL)
646 print_services (serv);
647 endservent ();
648 return result;
651 for (i = 0; i < number; ++i)
653 struct servent *serv;
654 char *proto = strchr (key[i], '/');
656 if (proto != NULL)
657 *proto++ = '\0';
659 if (isdigit (key[i][0]))
660 serv = getservbyport (htons (atol (key[i])), proto);
661 else
662 serv = getservbyname (key[i], proto);
664 if (serv == NULL)
665 result = 2;
666 else
667 print_services (serv);
670 return result;
673 /* This is for shadow */
674 static void
675 print_shadow (struct spwd *sp)
677 printf ("%s:%s:",
678 sp->sp_namp ? sp->sp_namp : "",
679 sp->sp_pwdp ? sp->sp_pwdp : "");
681 #define SHADOW_FIELD(n) \
682 if (sp->n == -1) \
683 putchar_unlocked (':'); \
684 else \
685 printf ("%ld:", sp->n)
687 SHADOW_FIELD (sp_lstchg);
688 SHADOW_FIELD (sp_min);
689 SHADOW_FIELD (sp_max);
690 SHADOW_FIELD (sp_warn);
691 SHADOW_FIELD (sp_inact);
692 SHADOW_FIELD (sp_expire);
693 if (sp->sp_flag == ~0ul)
694 putchar_unlocked ('\n');
695 else
696 printf ("%lu\n", sp->sp_flag);
699 static int
700 shadow_keys (int number, char *key[])
702 int result = 0;
703 int i;
705 if (number == 0)
707 struct spwd *sp;
709 setspent ();
710 while ((sp = getspent ()) != NULL)
711 print_shadow (sp);
712 endpwent ();
713 return result;
716 for (i = 0; i < number; ++i)
718 struct spwd *sp;
720 sp = getspnam (key[i]);
722 if (sp == NULL)
723 result = 2;
724 else
725 print_shadow (sp);
728 return result;
731 struct
733 const char *name;
734 int (*func) (int number, char *key[]);
735 } databases[] =
737 #define D(name) { #name, name ## _keys },
738 D(ahosts)
739 D(ahostsv4)
740 D(ahostsv6)
741 D(aliases)
742 D(ethers)
743 D(group)
744 D(hosts)
745 D(netgroup)
746 D(networks)
747 D(passwd)
748 D(protocols)
749 D(rpc)
750 D(services)
751 D(shadow)
752 #undef D
753 { NULL, NULL }
756 /* Handle arguments found by argp. */
757 static error_t
758 parse_option (int key, char *arg, struct argp_state *state)
760 int i;
761 switch (key)
763 case 's':
764 for (i = 0; databases[i].name; ++i)
765 __nss_configure_lookup (databases[i].name, arg);
766 break;
768 default:
769 return ARGP_ERR_UNKNOWN;
772 return 0;
776 static char *
777 more_help (int key, const char *text, void *input)
779 int len;
780 char *long_doc, *doc, *p;
782 switch (key)
784 case ARGP_KEY_HELP_EXTRA:
785 /* We print some extra information. */
786 #if 0
787 return xstrdup (gettext ("\
788 For bug reporting instructions, please see:\n\
789 <http://www.gnu.org/software/libc/bugs.html>.\n"));
790 #endif
791 long_doc = _("Supported databases:");
792 len = strlen (long_doc) + 2;
794 for (int i = 0; databases[i].name; ++i)
795 len += strlen (databases[i].name) + 1;
797 doc = (char *) malloc (len);
798 if (doc != NULL)
800 p = stpcpy (doc, long_doc);
801 *p++ = '\n';
803 for (int i = 0, col = 0; databases[i].name; ++i)
805 len = strlen (databases[i].name);
806 if (i != 0)
808 if (col + len > 72)
810 col = 0;
811 *p++ = '\n';
813 else
814 *p++ = ' ';
817 p = mempcpy (p, databases[i].name, len);
818 col += len + 1;
821 return doc;
823 break;
825 default:
826 break;
828 return (char *) text;
832 /* the main function */
834 main (int argc, char *argv[])
836 int remaining, i;
838 /* Set locale via LC_ALL. */
839 setlocale (LC_ALL, "");
840 /* Set the text message domain. */
841 textdomain (PACKAGE);
843 /* Parse and process arguments. */
844 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
846 if ((argc - remaining) < 1)
848 error (0, 0, gettext ("wrong number of arguments"));
849 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
850 return 1;
853 for (i = 0; databases[i].name; ++i)
854 if (argv[remaining][0] == databases[i].name[0]
855 && !strcmp (argv[remaining], databases[i].name))
856 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
858 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
859 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
860 return 1;