Update.
[glibc.git] / nss / getent.c
blob45a6a1ffe54d03c8db14ea9845850d0d719f37b1
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
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 /* 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. */
70 static void
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\
78 "), "2004");
79 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
82 /* This is for aliases */
83 static inline void
84 print_aliases (struct aliasent *alias)
86 unsigned int i = 0;
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)
93 printf ("%s%s",
94 alias->alias_members [i],
95 i + 1 == alias->alias_members_len ? "\n" : ", ");
98 static int
99 aliases_keys (int number, char *key[])
101 int result = 0;
102 int i;
103 struct aliasent *alias;
105 if (number == 0)
107 setaliasent ();
108 while ((alias = getaliasent ()) != NULL)
109 print_aliases (alias);
110 endaliasent ();
111 return result;
114 for (i = 0; i < number; ++i)
116 alias = getaliasbyname (key[i]);
118 if (alias == NULL)
119 result = 2;
120 else
121 print_aliases (alias);
124 return result;
127 /* This is for ethers */
128 static int
129 ethers_keys (int number, char *key[])
131 int result = 0;
132 int i;
134 if (number == 0)
136 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
137 return 3;
140 for (i = 0; i < number; ++i)
142 struct ether_addr *ethp, eth;
143 char buffer [1024], *p;
145 ethp = ether_aton (key[i]);
146 if (ethp != NULL)
148 if (ether_ntohost (buffer, ethp))
150 result = 2;
151 continue;
153 p = buffer;
155 else
157 if (ether_hostton (key[i], &eth))
159 result = 2;
160 continue;
162 p = key[i];
163 ethp = &eth;
165 printf ("%s %s\n", ether_ntoa (ethp), p);
168 return result;
171 /* This is for group */
172 static inline void
173 print_group (struct group *grp)
175 unsigned int i = 0;
177 printf ("%s:%s:%lu:", 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);
184 ++i;
185 if (grp->gr_mem[i] != NULL)
186 putchar_unlocked (',');
188 putchar_unlocked ('\n');
191 static int
192 group_keys (int number, char *key[])
194 int result = 0;
195 int i;
196 struct group *grp;
198 if (number == 0)
200 setgrent ();
201 while ((grp = getgrent ()) != NULL)
202 print_group (grp);
203 endgrent ();
204 return result;
207 for (i = 0; i < number; ++i)
209 errno = 0;
210 char *ep;
211 gid_t arg_gid = strtoul(key[i], &ep, 10);
213 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
214 /* Valid numeric gid. */
215 grp = getgrgid (arg_gid);
216 else
217 grp = getgrnam (key[i]);
219 if (grp == NULL)
220 result = 2;
221 else
222 print_group (grp);
225 return result;
228 /* This is for hosts */
229 static void
230 print_hosts (struct hostent *host)
232 unsigned int cnt;
234 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
236 char buf[INET6_ADDRSTRLEN];
237 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
238 buf, sizeof (buf));
240 printf ("%-15s %s", ip, host->h_name);
242 unsigned int i;
243 for (i = 0; host->h_aliases[i] != NULL; ++i)
245 putchar_unlocked (' ');
246 fputs_unlocked (host->h_aliases[i], stdout);
248 putchar_unlocked ('\n');
252 static int
253 hosts_keys (int number, char *key[])
255 int result = 0;
256 int i;
257 struct hostent *host;
259 if (number == 0)
261 sethostent (0);
262 while ((host = gethostent ()) != NULL)
263 print_hosts (host);
264 endhostent ();
265 return result;
268 for (i = 0; i < number; ++i)
270 struct hostent *host = NULL;
272 if (strchr (key[i], ':') != NULL)
274 char addr[IN6ADDRSZ];
275 if (inet_pton (AF_INET6, key[i], &addr))
276 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
278 else if (isdigit (key[i][0]))
280 char addr[INADDRSZ];
281 if (inet_pton (AF_INET, key[i], &addr))
282 host = gethostbyaddr (addr, sizeof (addr), 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 else
340 snprintf (sockbuf, sizeof (sockbuf), "%d",
341 runp->ai_socktype);
342 sockstr = sockbuf;
345 char buf[INET6_ADDRSTRLEN];
346 printf ("%-15s %-6s %s\n",
347 inet_ntop (runp->ai_family,
348 runp->ai_family == AF_INET
349 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
350 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
351 buf, sizeof (buf)),
352 sockstr,
353 runp->ai_canonname ?: "");
355 runp = runp->ai_next;
358 freeaddrinfo (res);
362 return result;
365 static int
366 ahosts_keys (int number, char *key[])
368 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
371 static int
372 ahostsv4_keys (int number, char *key[])
374 return ahosts_keys_int (AF_INET, 0, number, key);
377 static int
378 ahostsv6_keys (int number, char *key[])
380 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
383 /* This is for netgroup */
384 static int
385 netgroup_keys (int number, char *key[])
387 int result = 0;
388 int i;
390 if (number == 0)
392 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
393 return 3;
396 for (i = 0; i < number; ++i)
398 if (!setnetgrent (key[i]))
399 result = 2;
400 else
402 char *p[3];
404 printf ("%-21s", key[i]);
406 while (getnetgrent (p, p + 1, p + 2))
407 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
408 putchar_unlocked ('\n');
412 return result;
415 /* This is for networks */
416 static void
417 print_networks (struct netent *net)
419 unsigned int i;
420 struct in_addr ip;
421 ip.s_addr = htonl (net->n_net);
423 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
425 i = 0;
426 while (net->n_aliases[i] != NULL)
428 putchar_unlocked (' ');
429 fputs_unlocked (net->n_aliases[i], stdout);
430 ++i;
431 if (net->n_aliases[i] != NULL)
432 putchar_unlocked (',');
434 putchar_unlocked ('\n');
437 static int
438 networks_keys (int number, char *key[])
440 int result = 0;
441 int i;
442 struct netent *net;
444 if (number == 0)
446 setnetent (0);
447 while ((net = getnetent ()) != NULL)
448 print_networks (net);
449 endnetent ();
450 return result;
453 for (i = 0; i < number; ++i)
455 if (isdigit (key[i][0]))
456 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
457 else
458 net = getnetbyname (key[i]);
460 if (net == NULL)
461 result = 2;
462 else
463 print_networks (net);
466 return result;
469 /* Now is all for passwd */
470 static inline void
471 print_passwd (struct passwd *pwd)
473 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
474 pwd->pw_name ? pwd->pw_name : "",
475 pwd->pw_passwd ? pwd->pw_passwd : "",
476 (unsigned long int) pwd->pw_uid,
477 (unsigned long int) pwd->pw_gid,
478 pwd->pw_gecos ? pwd->pw_gecos : "",
479 pwd->pw_dir ? pwd->pw_dir : "",
480 pwd->pw_shell ? pwd->pw_shell : "");
483 static int
484 passwd_keys (int number, char *key[])
486 int result = 0;
487 int i;
488 struct passwd *pwd;
490 if (number == 0)
492 setpwent ();
493 while ((pwd = getpwent ()) != NULL)
494 print_passwd (pwd);
495 endpwent ();
496 return result;
499 for (i = 0; i < number; ++i)
501 errno = 0;
502 char *ep;
503 uid_t arg_uid = strtoul(key[i], &ep, 10);
505 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
506 /* Valid numeric uid. */
507 pwd = getpwuid (arg_uid);
508 else
509 pwd = getpwnam (key[i]);
511 if (pwd == NULL)
512 result = 2;
513 else
514 print_passwd (pwd);
517 return result;
520 /* This is for protocols */
521 static inline void
522 print_protocols (struct protoent *proto)
524 unsigned int i;
526 printf ("%-21s %d", proto->p_name, proto->p_proto);
528 i = 0;
529 while (proto->p_aliases[i] != NULL)
531 putchar_unlocked (' ');
532 fputs_unlocked (proto->p_aliases[i], stdout);
533 ++i;
535 putchar_unlocked ('\n');
538 static int
539 protocols_keys (int number, char *key[])
541 int result = 0;
542 int i;
543 struct protoent *proto;
545 if (number == 0)
547 setprotoent (0);
548 while ((proto = getprotoent ()) != NULL)
549 print_protocols (proto);
550 endprotoent ();
551 return result;
554 for (i = 0; i < number; ++i)
556 if (isdigit (key[i][0]))
557 proto = getprotobynumber (atol (key[i]));
558 else
559 proto = getprotobyname (key[i]);
561 if (proto == NULL)
562 result = 2;
563 else
564 print_protocols (proto);
567 return result;
570 /* Now is all for rpc */
571 static inline void
572 print_rpc (struct rpcent *rpc)
574 int i;
576 printf ("%-15s %d%s",
577 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
579 for (i = 0; rpc->r_aliases[i]; ++i)
580 printf (" %s", rpc->r_aliases[i]);
581 putchar_unlocked ('\n');
584 static int
585 rpc_keys (int number, char *key[])
587 int result = 0;
588 int i;
589 struct rpcent *rpc;
591 if (number == 0)
593 setrpcent (0);
594 while ((rpc = getrpcent ()) != NULL)
595 print_rpc (rpc);
596 endrpcent ();
597 return result;
600 for (i = 0; i < number; ++i)
602 if (isdigit (key[i][0]))
603 rpc = getrpcbynumber (atol (key[i]));
604 else
605 rpc = getrpcbyname (key[i]);
607 if (rpc == NULL)
608 result = 2;
609 else
610 print_rpc (rpc);
613 return result;
616 /* for services */
617 static void
618 print_services (struct servent *serv)
620 unsigned int i;
622 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
624 i = 0;
625 while (serv->s_aliases[i] != NULL)
627 putchar_unlocked (' ');
628 fputs_unlocked (serv->s_aliases[i], stdout);
629 ++i;
631 putchar_unlocked ('\n');
634 static int
635 services_keys (int number, char *key[])
637 int result = 0;
638 int i;
639 struct servent *serv;
641 if (!number)
643 setservent (0);
644 while ((serv = getservent ()) != NULL)
645 print_services (serv);
646 endservent ();
647 return result;
650 for (i = 0; i < number; ++i)
652 struct servent *serv;
653 char *proto = strchr (key[i], '/');
655 if (proto != NULL)
656 *proto++ = '\0';
658 if (isdigit (key[i][0]))
659 serv = getservbyport (htons (atol (key[i])), proto);
660 else
661 serv = getservbyname (key[i], proto);
663 if (serv == NULL)
664 result = 2;
665 else
666 print_services (serv);
669 return result;
672 /* This is for shadow */
673 static void
674 print_shadow (struct spwd *sp)
676 printf ("%s:%s:",
677 sp->sp_namp ? sp->sp_namp : "",
678 sp->sp_pwdp ? sp->sp_pwdp : "");
680 #define SHADOW_FIELD(n) \
681 if (sp->n == -1) \
682 putchar_unlocked (':'); \
683 else \
684 printf ("%ld:", sp->n)
686 SHADOW_FIELD (sp_lstchg);
687 SHADOW_FIELD (sp_min);
688 SHADOW_FIELD (sp_max);
689 SHADOW_FIELD (sp_warn);
690 SHADOW_FIELD (sp_inact);
691 SHADOW_FIELD (sp_expire);
692 if (sp->sp_flag == ~0ul)
693 putchar_unlocked ('\n');
694 else
695 printf ("%lu\n", sp->sp_flag);
698 static int
699 shadow_keys (int number, char *key[])
701 int result = 0;
702 int i;
704 if (number == 0)
706 struct spwd *sp;
708 setspent ();
709 while ((sp = getspent ()) != NULL)
710 print_shadow (sp);
711 endpwent ();
712 return result;
715 for (i = 0; i < number; ++i)
717 struct spwd *sp;
719 sp = getspnam (key[i]);
721 if (sp == NULL)
722 result = 2;
723 else
724 print_shadow (sp);
727 return result;
730 struct
732 const char *name;
733 int (*func) (int number, char *key[]);
734 } databases[] =
736 #define D(name) { #name, name ## _keys },
737 D(ahosts)
738 D(ahostsv4)
739 D(ahostsv6)
740 D(aliases)
741 D(ethers)
742 D(group)
743 D(hosts)
744 D(netgroup)
745 D(networks)
746 D(passwd)
747 D(protocols)
748 D(rpc)
749 D(services)
750 D(shadow)
751 #undef D
752 { NULL, NULL }
755 /* Handle arguments found by argp. */
756 static error_t
757 parse_option (int key, char *arg, struct argp_state *state)
759 int i;
760 switch (key)
762 case 's':
763 for (i = 0; databases[i].name; ++i)
764 __nss_configure_lookup (databases[i].name, arg);
765 break;
767 default:
768 return ARGP_ERR_UNKNOWN;
771 return 0;
774 /* build doc */
775 static void
776 build_doc (void)
778 int i, j, len;
779 char *short_doc, *long_doc, *doc, *p;
781 short_doc = _("getent - get entries from administrative database.");
782 long_doc = _("Supported databases:");
783 len = strlen (short_doc) + strlen (long_doc) + 3;
785 for (i = 0; databases[i].name; ++i)
786 len += strlen (databases[i].name) + 1;
788 doc = (char *) malloc (len);
789 if (doc == NULL)
790 doc = short_doc;
791 else
793 p = stpcpy (doc, short_doc);
794 *p++ = '\v';
795 p = stpcpy (p, long_doc);
796 *p++ = '\n';
798 for (i = 0, j = 0; databases[i].name; ++i)
800 len = strlen (databases[i].name);
801 if (i != 0)
803 if (j + len > 72)
805 j = 0;
806 *p++ = '\n';
808 else
809 *p++ = ' ';
812 p = mempcpy (p, databases[i].name, len);
813 j += len + 1;
817 argp.doc = doc;
820 /* the main function */
822 main (int argc, char *argv[])
824 int remaining, i;
826 /* Set locale via LC_ALL. */
827 setlocale (LC_ALL, "");
828 /* Set the text message domain. */
829 textdomain (PACKAGE);
831 /* Build argp.doc. */
832 build_doc ();
834 /* Parse and process arguments. */
835 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
837 if ((argc - remaining) < 1)
839 error (0, 0, gettext ("wrong number of arguments"));
840 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
841 return 1;
844 for (i = 0; databases[i].name; ++i)
845 if (argv[remaining][0] == databases[i].name[0]
846 && !strcmp (argv[remaining], databases[i].name))
847 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
849 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
850 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
851 return 1;