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
20 /* getent: get entries from administrative database. */
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. */
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. */
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\
87 fprintf (stream
, gettext ("Written by %s.\n"), "Thorsten Kukuk");
90 /* This is for aliases */
92 print_aliases (struct aliasent
*alias
)
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
)
102 alias
->alias_members
[i
],
103 i
+ 1 == alias
->alias_members_len
? "\n" : ", ");
107 aliases_keys (int number
, char *key
[])
111 struct aliasent
*alias
;
116 while ((alias
= getaliasent ()) != NULL
)
117 print_aliases (alias
);
122 for (i
= 0; i
< number
; ++i
)
124 alias
= getaliasbyname (key
[i
]);
129 print_aliases (alias
);
135 /* This is for ethers */
137 ethers_keys (int number
, char *key
[])
144 fprintf (stderr
, _("Enumeration not supported on %s\n"), "ethers");
148 for (i
= 0; i
< number
; ++i
)
150 struct ether_addr
*ethp
, eth
;
151 char buffer
[1024], *p
;
153 ethp
= ether_aton (key
[i
]);
156 if (ether_ntohost (buffer
, ethp
))
165 if (ether_hostton (key
[i
], ð
))
173 printf ("%s %s\n", ether_ntoa (ethp
), p
);
179 /* This is for group */
181 print_group (struct group
*grp
)
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
);
193 if (grp
->gr_mem
[i
] != NULL
)
194 putchar_unlocked (',');
196 putchar_unlocked ('\n');
200 group_keys (int number
, char *key
[])
209 while ((grp
= getgrent ()) != NULL
)
215 for (i
= 0; i
< number
; ++i
)
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
);
225 grp
= getgrnam (key
[i
]);
236 /* This is for hosts */
238 print_hosts (struct hostent
*host
)
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
],
248 printf ("%-15s %s", ip
, host
->h_name
);
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');
261 hosts_keys (int number
, char *key
[])
265 struct hostent
*host
;
270 while ((host
= gethostent ()) != NULL
)
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
);
297 /* This is for hosts, but using getaddrinfo */
299 ahosts_keys_int (int af
, int xflags
, int number
, char *key
[])
303 struct hostent
*host
;
308 while ((host
= gethostent ()) != NULL
)
314 struct addrinfo hint
;
315 memset (&hint
, '\0', sizeof (hint
));
316 hint
.ai_flags
= AI_V4MAPPED
| AI_ADDRCONFIG
| AI_CANONNAME
| xflags
;
319 for (i
= 0; i
< number
; ++i
)
321 struct addrinfo
*res
;
323 if (getaddrinfo (key
[i
], NULL
, &hint
, &res
) != 0)
327 struct addrinfo
*runp
= res
;
333 if (runp
->ai_socktype
== SOCK_STREAM
)
335 else if (runp
->ai_socktype
== SOCK_DGRAM
)
337 else if (runp
->ai_socktype
== SOCK_RAW
)
341 snprintf (sockbuf
, sizeof (sockbuf
), "%d",
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
,
354 runp
->ai_canonname
?: "");
356 runp
= runp
->ai_next
;
367 ahosts_keys (int number
, char *key
[])
369 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
373 ahostsv4_keys (int number
, char *key
[])
375 return ahosts_keys_int (AF_INET
, 0, number
, key
);
379 ahostsv6_keys (int number
, char *key
[])
381 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
384 /* This is for netgroup */
386 netgroup_keys (int number
, char *key
[])
393 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
397 for (i
= 0; i
< number
; ++i
)
399 if (!setnetgrent (key
[i
]))
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');
416 /* This is for networks */
418 print_networks (struct netent
*net
)
422 ip
.s_addr
= htonl (net
->n_net
);
424 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
427 while (net
->n_aliases
[i
] != NULL
)
429 putchar_unlocked (' ');
430 fputs_unlocked (net
->n_aliases
[i
], stdout
);
432 if (net
->n_aliases
[i
] != NULL
)
433 putchar_unlocked (',');
435 putchar_unlocked ('\n');
439 networks_keys (int number
, char *key
[])
448 while ((net
= getnetent ()) != NULL
)
449 print_networks (net
);
454 for (i
= 0; i
< number
; ++i
)
456 if (isdigit (key
[i
][0]))
457 net
= getnetbyaddr (inet_addr (key
[i
]), AF_UNIX
);
459 net
= getnetbyname (key
[i
]);
464 print_networks (net
);
470 /* Now is all for passwd */
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
: "");
485 passwd_keys (int number
, char *key
[])
494 while ((pwd
= getpwent ()) != NULL
)
500 for (i
= 0; i
< number
; ++i
)
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
);
510 pwd
= getpwnam (key
[i
]);
521 /* This is for protocols */
523 print_protocols (struct protoent
*proto
)
527 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
530 while (proto
->p_aliases
[i
] != NULL
)
532 putchar_unlocked (' ');
533 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
536 putchar_unlocked ('\n');
540 protocols_keys (int number
, char *key
[])
544 struct protoent
*proto
;
549 while ((proto
= getprotoent ()) != NULL
)
550 print_protocols (proto
);
555 for (i
= 0; i
< number
; ++i
)
557 if (isdigit (key
[i
][0]))
558 proto
= getprotobynumber (atol (key
[i
]));
560 proto
= getprotobyname (key
[i
]);
565 print_protocols (proto
);
571 /* Now is all for rpc */
573 print_rpc (struct rpcent
*rpc
)
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');
586 rpc_keys (int number
, char *key
[])
595 while ((rpc
= getrpcent ()) != NULL
)
601 for (i
= 0; i
< number
; ++i
)
603 if (isdigit (key
[i
][0]))
604 rpc
= getrpcbynumber (atol (key
[i
]));
606 rpc
= getrpcbyname (key
[i
]);
619 print_services (struct servent
*serv
)
623 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
626 while (serv
->s_aliases
[i
] != NULL
)
628 putchar_unlocked (' ');
629 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
632 putchar_unlocked ('\n');
636 services_keys (int number
, char *key
[])
640 struct servent
*serv
;
645 while ((serv
= getservent ()) != NULL
)
646 print_services (serv
);
651 for (i
= 0; i
< number
; ++i
)
653 struct servent
*serv
;
654 char *proto
= strchr (key
[i
], '/');
659 if (isdigit (key
[i
][0]))
660 serv
= getservbyport (htons (atol (key
[i
])), proto
);
662 serv
= getservbyname (key
[i
], proto
);
667 print_services (serv
);
673 /* This is for shadow */
675 print_shadow (struct spwd
*sp
)
678 sp
->sp_namp
? sp
->sp_namp
: "",
679 sp
->sp_pwdp
? sp
->sp_pwdp
: "");
681 #define SHADOW_FIELD(n) \
683 putchar_unlocked (':'); \
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');
696 printf ("%lu\n", sp
->sp_flag
);
700 shadow_keys (int number
, char *key
[])
710 while ((sp
= getspent ()) != NULL
)
716 for (i
= 0; i
< number
; ++i
)
720 sp
= getspnam (key
[i
]);
734 int (*func
) (int number
, char *key
[]);
737 #define D(name) { #name, name ## _keys },
756 /* Handle arguments found by argp. */
758 parse_option (int key
, char *arg
, struct argp_state
*state
)
764 for (i
= 0; databases
[i
].name
; ++i
)
765 __nss_configure_lookup (databases
[i
].name
, arg
);
769 return ARGP_ERR_UNKNOWN
;
777 more_help (int key
, const char *text
, void *input
)
780 char *long_doc
, *doc
, *p
;
784 case ARGP_KEY_HELP_EXTRA
:
785 /* We print some extra information. */
787 return xstrdup (gettext ("\
788 For bug reporting instructions, please see:\n\
789 <http://www.gnu.org/software/libc/bugs.html>.\n"));
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
);
800 p
= stpcpy (doc
, long_doc
);
803 for (int i
= 0, col
= 0; databases
[i
].name
; ++i
)
805 len
= strlen (databases
[i
].name
);
817 p
= mempcpy (p
, databases
[i
].name
, len
);
828 return (char *) text
;
832 /* the main function */
834 main (int argc
, char *argv
[])
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
);
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
);