(Patch0): We do include the glibc snapshot date in the patch file name.
[glibc/history.git] / nss / getent.c
blobf9f0a6e22ac9957971ef459630144095be0dab96
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 /* 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 "), "2004");
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;
280 if (strchr (key[i], ':') != NULL)
282 char addr[IN6ADDRSZ];
283 if (inet_pton (AF_INET6, key[i], &addr))
284 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
286 else if (isdigit (key[i][0]))
288 char addr[INADDRSZ];
289 if (inet_pton (AF_INET, key[i], &addr))
290 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
292 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
293 host = gethostbyname2 (key[i], AF_INET);
295 if (host == NULL)
296 result = 2;
297 else
298 print_hosts (host);
301 return result;
304 /* This is for hosts, but using getaddrinfo */
305 static int
306 ahosts_keys_int (int af, int xflags, int number, char *key[])
308 int result = 0;
309 int i;
310 struct hostent *host;
312 if (number == 0)
314 sethostent (0);
315 while ((host = gethostent ()) != NULL)
316 print_hosts (host);
317 endhostent ();
318 return result;
321 struct addrinfo hint;
322 memset (&hint, '\0', sizeof (hint));
323 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
324 hint.ai_family = af;
326 for (i = 0; i < number; ++i)
328 struct addrinfo *res;
330 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
331 result = 2;
332 else
334 struct addrinfo *runp = res;
336 while (runp != NULL)
338 char sockbuf[20];
339 const char *sockstr;
340 if (runp->ai_socktype == SOCK_STREAM)
341 sockstr = "STREAM";
342 else if (runp->ai_socktype == SOCK_DGRAM)
343 sockstr = "DGRAM";
344 else if (runp->ai_socktype == SOCK_RAW)
345 sockstr = "RAW";
346 else
348 snprintf (sockbuf, sizeof (sockbuf), "%d",
349 runp->ai_socktype);
350 sockstr = sockbuf;
353 char buf[INET6_ADDRSTRLEN];
354 printf ("%-15s %-6s %s\n",
355 inet_ntop (runp->ai_family,
356 runp->ai_family == AF_INET
357 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
358 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
359 buf, sizeof (buf)),
360 sockstr,
361 runp->ai_canonname ?: "");
363 runp = runp->ai_next;
366 freeaddrinfo (res);
370 return result;
373 static int
374 ahosts_keys (int number, char *key[])
376 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
379 static int
380 ahostsv4_keys (int number, char *key[])
382 return ahosts_keys_int (AF_INET, 0, number, key);
385 static int
386 ahostsv6_keys (int number, char *key[])
388 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
391 /* This is for netgroup */
392 static int
393 netgroup_keys (int number, char *key[])
395 int result = 0;
396 int i;
398 if (number == 0)
400 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
401 return 3;
404 for (i = 0; i < number; ++i)
406 if (!setnetgrent (key[i]))
407 result = 2;
408 else
410 char *p[3];
412 printf ("%-21s", key[i]);
414 while (getnetgrent (p, p + 1, p + 2))
415 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
416 putchar_unlocked ('\n');
420 return result;
423 /* This is for networks */
424 static void
425 print_networks (struct netent *net)
427 unsigned int i;
428 struct in_addr ip;
429 ip.s_addr = htonl (net->n_net);
431 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
433 i = 0;
434 while (net->n_aliases[i] != NULL)
436 putchar_unlocked (' ');
437 fputs_unlocked (net->n_aliases[i], stdout);
438 ++i;
439 if (net->n_aliases[i] != NULL)
440 putchar_unlocked (',');
442 putchar_unlocked ('\n');
445 static int
446 networks_keys (int number, char *key[])
448 int result = 0;
449 int i;
450 struct netent *net;
452 if (number == 0)
454 setnetent (0);
455 while ((net = getnetent ()) != NULL)
456 print_networks (net);
457 endnetent ();
458 return result;
461 for (i = 0; i < number; ++i)
463 if (isdigit (key[i][0]))
464 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
465 else
466 net = getnetbyname (key[i]);
468 if (net == NULL)
469 result = 2;
470 else
471 print_networks (net);
474 return result;
477 /* Now is all for passwd */
478 static inline void
479 print_passwd (struct passwd *pwd)
481 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
482 pwd->pw_name ? pwd->pw_name : "",
483 pwd->pw_passwd ? pwd->pw_passwd : "",
484 (unsigned long int) pwd->pw_uid,
485 (unsigned long int) pwd->pw_gid,
486 pwd->pw_gecos ? pwd->pw_gecos : "",
487 pwd->pw_dir ? pwd->pw_dir : "",
488 pwd->pw_shell ? pwd->pw_shell : "");
491 static int
492 passwd_keys (int number, char *key[])
494 int result = 0;
495 int i;
496 struct passwd *pwd;
498 if (number == 0)
500 setpwent ();
501 while ((pwd = getpwent ()) != NULL)
502 print_passwd (pwd);
503 endpwent ();
504 return result;
507 for (i = 0; i < number; ++i)
509 errno = 0;
510 char *ep;
511 uid_t arg_uid = strtoul(key[i], &ep, 10);
513 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
514 /* Valid numeric uid. */
515 pwd = getpwuid (arg_uid);
516 else
517 pwd = getpwnam (key[i]);
519 if (pwd == NULL)
520 result = 2;
521 else
522 print_passwd (pwd);
525 return result;
528 /* This is for protocols */
529 static inline void
530 print_protocols (struct protoent *proto)
532 unsigned int i;
534 printf ("%-21s %d", proto->p_name, proto->p_proto);
536 i = 0;
537 while (proto->p_aliases[i] != NULL)
539 putchar_unlocked (' ');
540 fputs_unlocked (proto->p_aliases[i], stdout);
541 ++i;
543 putchar_unlocked ('\n');
546 static int
547 protocols_keys (int number, char *key[])
549 int result = 0;
550 int i;
551 struct protoent *proto;
553 if (number == 0)
555 setprotoent (0);
556 while ((proto = getprotoent ()) != NULL)
557 print_protocols (proto);
558 endprotoent ();
559 return result;
562 for (i = 0; i < number; ++i)
564 if (isdigit (key[i][0]))
565 proto = getprotobynumber (atol (key[i]));
566 else
567 proto = getprotobyname (key[i]);
569 if (proto == NULL)
570 result = 2;
571 else
572 print_protocols (proto);
575 return result;
578 /* Now is all for rpc */
579 static inline void
580 print_rpc (struct rpcent *rpc)
582 int i;
584 printf ("%-15s %d%s",
585 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
587 for (i = 0; rpc->r_aliases[i]; ++i)
588 printf (" %s", rpc->r_aliases[i]);
589 putchar_unlocked ('\n');
592 static int
593 rpc_keys (int number, char *key[])
595 int result = 0;
596 int i;
597 struct rpcent *rpc;
599 if (number == 0)
601 setrpcent (0);
602 while ((rpc = getrpcent ()) != NULL)
603 print_rpc (rpc);
604 endrpcent ();
605 return result;
608 for (i = 0; i < number; ++i)
610 if (isdigit (key[i][0]))
611 rpc = getrpcbynumber (atol (key[i]));
612 else
613 rpc = getrpcbyname (key[i]);
615 if (rpc == NULL)
616 result = 2;
617 else
618 print_rpc (rpc);
621 return result;
624 /* for services */
625 static void
626 print_services (struct servent *serv)
628 unsigned int i;
630 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
632 i = 0;
633 while (serv->s_aliases[i] != NULL)
635 putchar_unlocked (' ');
636 fputs_unlocked (serv->s_aliases[i], stdout);
637 ++i;
639 putchar_unlocked ('\n');
642 static int
643 services_keys (int number, char *key[])
645 int result = 0;
646 int i;
647 struct servent *serv;
649 if (!number)
651 setservent (0);
652 while ((serv = getservent ()) != NULL)
653 print_services (serv);
654 endservent ();
655 return result;
658 for (i = 0; i < number; ++i)
660 struct servent *serv;
661 char *proto = strchr (key[i], '/');
663 if (proto != NULL)
664 *proto++ = '\0';
666 if (isdigit (key[i][0]))
667 serv = getservbyport (htons (atol (key[i])), proto);
668 else
669 serv = getservbyname (key[i], proto);
671 if (serv == NULL)
672 result = 2;
673 else
674 print_services (serv);
677 return result;
680 /* This is for shadow */
681 static void
682 print_shadow (struct spwd *sp)
684 printf ("%s:%s:",
685 sp->sp_namp ? sp->sp_namp : "",
686 sp->sp_pwdp ? sp->sp_pwdp : "");
688 #define SHADOW_FIELD(n) \
689 if (sp->n == -1) \
690 putchar_unlocked (':'); \
691 else \
692 printf ("%ld:", sp->n)
694 SHADOW_FIELD (sp_lstchg);
695 SHADOW_FIELD (sp_min);
696 SHADOW_FIELD (sp_max);
697 SHADOW_FIELD (sp_warn);
698 SHADOW_FIELD (sp_inact);
699 SHADOW_FIELD (sp_expire);
700 if (sp->sp_flag == ~0ul)
701 putchar_unlocked ('\n');
702 else
703 printf ("%lu\n", sp->sp_flag);
706 static int
707 shadow_keys (int number, char *key[])
709 int result = 0;
710 int i;
712 if (number == 0)
714 struct spwd *sp;
716 setspent ();
717 while ((sp = getspent ()) != NULL)
718 print_shadow (sp);
719 endpwent ();
720 return result;
723 for (i = 0; i < number; ++i)
725 struct spwd *sp;
727 sp = getspnam (key[i]);
729 if (sp == NULL)
730 result = 2;
731 else
732 print_shadow (sp);
735 return result;
738 struct
740 const char *name;
741 int (*func) (int number, char *key[]);
742 } databases[] =
744 #define D(name) { #name, name ## _keys },
745 D(ahosts)
746 D(ahostsv4)
747 D(ahostsv6)
748 D(aliases)
749 D(ethers)
750 D(group)
751 D(hosts)
752 D(netgroup)
753 D(networks)
754 D(passwd)
755 D(protocols)
756 D(rpc)
757 D(services)
758 D(shadow)
759 #undef D
760 { NULL, NULL }
763 /* Handle arguments found by argp. */
764 static error_t
765 parse_option (int key, char *arg, struct argp_state *state)
767 int i;
768 switch (key)
770 case 's':
771 for (i = 0; databases[i].name; ++i)
772 __nss_configure_lookup (databases[i].name, arg);
773 break;
775 default:
776 return ARGP_ERR_UNKNOWN;
779 return 0;
783 static char *
784 more_help (int key, const char *text, void *input)
786 int len;
787 char *long_doc, *doc, *p;
789 switch (key)
791 case ARGP_KEY_HELP_EXTRA:
792 /* We print some extra information. */
793 #if 0
794 return xstrdup (gettext ("\
795 For bug reporting instructions, please see:\n\
796 <http://www.gnu.org/software/libc/bugs.html>.\n"));
797 #endif
798 long_doc = _("Supported databases:");
799 len = strlen (long_doc) + 2;
801 for (int i = 0; databases[i].name; ++i)
802 len += strlen (databases[i].name) + 1;
804 doc = (char *) malloc (len);
805 if (doc != NULL)
807 p = stpcpy (doc, long_doc);
808 *p++ = '\n';
810 for (int i = 0, col = 0; databases[i].name; ++i)
812 len = strlen (databases[i].name);
813 if (i != 0)
815 if (col + len > 72)
817 col = 0;
818 *p++ = '\n';
820 else
821 *p++ = ' ';
824 p = mempcpy (p, databases[i].name, len);
825 col += len + 1;
828 return doc;
830 break;
832 default:
833 break;
835 return (char *) text;
839 /* the main function */
841 main (int argc, char *argv[])
843 int remaining, i;
845 /* Set locale via LC_ALL. */
846 setlocale (LC_ALL, "");
847 /* Set the text message domain. */
848 textdomain (PACKAGE);
850 /* Parse and process arguments. */
851 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
853 if ((argc - remaining) < 1)
855 error (0, 0, gettext ("wrong number of arguments"));
856 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
857 return 1;
860 for (i = 0; databases[i].name; ++i)
861 if (argv[remaining][0] == databases[i].name[0]
862 && !strcmp (argv[remaining], databases[i].name))
863 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
865 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
866 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
867 return 1;