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
20 /* getent: get entries from administrative database. */
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. */
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. */
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\
86 fprintf (stream
, gettext ("Written by %s.\n"), "Thorsten Kukuk");
89 /* This is for aliases */
91 print_aliases (struct aliasent
*alias
)
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
)
101 alias
->alias_members
[i
],
102 i
+ 1 == alias
->alias_members_len
? "\n" : ", ");
106 aliases_keys (int number
, char *key
[])
110 struct aliasent
*alias
;
115 while ((alias
= getaliasent ()) != NULL
)
116 print_aliases (alias
);
121 for (i
= 0; i
< number
; ++i
)
123 alias
= getaliasbyname (key
[i
]);
128 print_aliases (alias
);
134 /* This is for ethers */
136 ethers_keys (int number
, char *key
[])
143 fprintf (stderr
, _("Enumeration not supported on %s\n"), "ethers");
147 for (i
= 0; i
< number
; ++i
)
149 struct ether_addr
*ethp
, eth
;
150 char buffer
[1024], *p
;
152 ethp
= ether_aton (key
[i
]);
155 if (ether_ntohost (buffer
, ethp
))
164 if (ether_hostton (key
[i
], ð
))
172 printf ("%s %s\n", ether_ntoa (ethp
), p
);
178 /* This is for group */
180 print_group (struct group
*grp
)
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
);
192 if (grp
->gr_mem
[i
] != NULL
)
193 putchar_unlocked (',');
195 putchar_unlocked ('\n');
199 group_keys (int number
, char *key
[])
208 while ((grp
= getgrent ()) != NULL
)
214 for (i
= 0; i
< number
; ++i
)
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
);
224 grp
= getgrnam (key
[i
]);
235 /* This is for hosts */
237 print_hosts (struct hostent
*host
)
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
],
247 printf ("%-15s %s", ip
, host
->h_name
);
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');
260 hosts_keys (int number
, char *key
[])
264 struct hostent
*host
;
269 while ((host
= gethostent ()) != NULL
)
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
);
296 /* This is for hosts, but using getaddrinfo */
298 ahosts_keys_int (int af
, int xflags
, int number
, char *key
[])
302 struct hostent
*host
;
307 while ((host
= gethostent ()) != NULL
)
313 struct addrinfo hint
;
314 memset (&hint
, '\0', sizeof (hint
));
315 hint
.ai_flags
= AI_V4MAPPED
| AI_ADDRCONFIG
| AI_CANONNAME
| xflags
;
318 for (i
= 0; i
< number
; ++i
)
320 struct addrinfo
*res
;
322 if (getaddrinfo (key
[i
], NULL
, &hint
, &res
) != 0)
326 struct addrinfo
*runp
= res
;
332 if (runp
->ai_socktype
== SOCK_STREAM
)
334 else if (runp
->ai_socktype
== SOCK_DGRAM
)
336 else if (runp
->ai_socktype
== SOCK_RAW
)
338 #ifdef SOCK_SEQPACKET
339 else if (runp
->ai_socktype
== SOCK_SEQPACKET
)
340 sockstr
= "SEQPACKET";
343 else if (runp
->ai_socktype
== SOCK_RDM
)
347 else if (runp
->ai_socktype
== SOCK_DCCP
)
351 else if (runp
->ai_socktype
== SOCK_PACKET
)
356 snprintf (sockbuf
, sizeof (sockbuf
), "%d",
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
,
369 runp
->ai_canonname
?: "");
371 runp
= runp
->ai_next
;
382 ahosts_keys (int number
, char *key
[])
384 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
388 ahostsv4_keys (int number
, char *key
[])
390 return ahosts_keys_int (AF_INET
, 0, number
, key
);
394 ahostsv6_keys (int number
, char *key
[])
396 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
399 /* This is for netgroup */
401 netgroup_keys (int number
, char *key
[])
408 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
412 for (i
= 0; i
< number
; ++i
)
414 if (!setnetgrent (key
[i
]))
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');
433 /* This is for networks */
435 print_networks (struct netent
*net
)
439 ip
.s_addr
= htonl (net
->n_net
);
441 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
444 while (net
->n_aliases
[i
] != NULL
)
446 putchar_unlocked (' ');
447 fputs_unlocked (net
->n_aliases
[i
], stdout
);
450 putchar_unlocked ('\n');
454 networks_keys (int number
, char *key
[])
463 while ((net
= getnetent ()) != NULL
)
464 print_networks (net
);
469 for (i
= 0; i
< number
; ++i
)
471 if (isdigit (key
[i
][0]))
472 net
= getnetbyaddr (inet_addr (key
[i
]), AF_UNIX
);
474 net
= getnetbyname (key
[i
]);
479 print_networks (net
);
485 /* Now is all for passwd */
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
: "");
500 passwd_keys (int number
, char *key
[])
509 while ((pwd
= getpwent ()) != NULL
)
515 for (i
= 0; i
< number
; ++i
)
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
);
525 pwd
= getpwnam (key
[i
]);
536 /* This is for protocols */
538 print_protocols (struct protoent
*proto
)
542 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
545 while (proto
->p_aliases
[i
] != NULL
)
547 putchar_unlocked (' ');
548 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
551 putchar_unlocked ('\n');
555 protocols_keys (int number
, char *key
[])
559 struct protoent
*proto
;
564 while ((proto
= getprotoent ()) != NULL
)
565 print_protocols (proto
);
570 for (i
= 0; i
< number
; ++i
)
572 if (isdigit (key
[i
][0]))
573 proto
= getprotobynumber (atol (key
[i
]));
575 proto
= getprotobyname (key
[i
]);
580 print_protocols (proto
);
586 /* Now is all for rpc */
588 print_rpc (struct rpcent
*rpc
)
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');
601 rpc_keys (int number
, char *key
[])
610 while ((rpc
= getrpcent ()) != NULL
)
616 for (i
= 0; i
< number
; ++i
)
618 if (isdigit (key
[i
][0]))
619 rpc
= getrpcbynumber (atol (key
[i
]));
621 rpc
= getrpcbyname (key
[i
]);
634 print_services (struct servent
*serv
)
638 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
641 while (serv
->s_aliases
[i
] != NULL
)
643 putchar_unlocked (' ');
644 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
647 putchar_unlocked ('\n');
651 services_keys (int number
, char *key
[])
655 struct servent
*serv
;
660 while ((serv
= getservent ()) != NULL
)
661 print_services (serv
);
666 for (i
= 0; i
< number
; ++i
)
668 struct servent
*serv
;
669 char *proto
= strchr (key
[i
], '/');
674 if (isdigit (key
[i
][0]))
675 serv
= getservbyport (htons (atol (key
[i
])), proto
);
677 serv
= getservbyname (key
[i
], proto
);
682 print_services (serv
);
688 /* This is for shadow */
690 print_shadow (struct spwd
*sp
)
693 sp
->sp_namp
? sp
->sp_namp
: "",
694 sp
->sp_pwdp
? sp
->sp_pwdp
: "");
696 #define SHADOW_FIELD(n) \
698 putchar_unlocked (':'); \
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');
711 printf ("%lu\n", sp
->sp_flag
);
715 shadow_keys (int number
, char *key
[])
725 while ((sp
= getspent ()) != NULL
)
731 for (i
= 0; i
< number
; ++i
)
735 sp
= getspnam (key
[i
]);
749 int (*func
) (int number
, char *key
[]);
752 #define D(name) { #name, name ## _keys },
771 /* Handle arguments found by argp. */
773 parse_option (int key
, char *arg
, struct argp_state
*state
)
779 endp
= strchr (arg
, ':');
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
);
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);
793 if (databases
[i
].name
== NULL
)
794 error (EXIT_FAILURE
, 0, gettext ("Unknown database name"));
799 return ARGP_ERR_UNKNOWN
;
807 more_help (int key
, const char *text
, void *input
)
815 case ARGP_KEY_HELP_EXTRA
:
816 /* We print some extra information. */
817 fp
= open_memstream (&doc
, &len
);
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
);
830 fputc_unlocked ('\n', fp
);
833 fputc_unlocked (' ', fp
);
836 fputs_unlocked (databases
[i
].name
, 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)
854 return (char *) text
;
858 /* the main function */
860 main (int argc
, char *argv
[])
862 /* Debugging support. */
865 /* Set locale via LC_ALL. */
866 setlocale (LC_ALL
, "");
867 /* Set the text message domain. */
868 textdomain (PACKAGE
);
870 /* Parse and process arguments. */
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
);
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
);