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
;
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]))
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
);
304 /* This is for hosts, but using getaddrinfo */
306 ahosts_keys_int (int af
, int xflags
, int number
, char *key
[])
310 struct hostent
*host
;
315 while ((host
= gethostent ()) != NULL
)
321 struct addrinfo hint
;
322 memset (&hint
, '\0', sizeof (hint
));
323 hint
.ai_flags
= AI_V4MAPPED
| AI_ADDRCONFIG
| AI_CANONNAME
| xflags
;
326 for (i
= 0; i
< number
; ++i
)
328 struct addrinfo
*res
;
330 if (getaddrinfo (key
[i
], NULL
, &hint
, &res
) != 0)
334 struct addrinfo
*runp
= res
;
340 if (runp
->ai_socktype
== SOCK_STREAM
)
342 else if (runp
->ai_socktype
== SOCK_DGRAM
)
344 else if (runp
->ai_socktype
== SOCK_RAW
)
348 snprintf (sockbuf
, sizeof (sockbuf
), "%d",
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
,
361 runp
->ai_canonname
?: "");
363 runp
= runp
->ai_next
;
374 ahosts_keys (int number
, char *key
[])
376 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
380 ahostsv4_keys (int number
, char *key
[])
382 return ahosts_keys_int (AF_INET
, 0, number
, key
);
386 ahostsv6_keys (int number
, char *key
[])
388 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
391 /* This is for netgroup */
393 netgroup_keys (int number
, char *key
[])
400 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
404 for (i
= 0; i
< number
; ++i
)
406 if (!setnetgrent (key
[i
]))
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');
423 /* This is for networks */
425 print_networks (struct netent
*net
)
429 ip
.s_addr
= htonl (net
->n_net
);
431 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
434 while (net
->n_aliases
[i
] != NULL
)
436 putchar_unlocked (' ');
437 fputs_unlocked (net
->n_aliases
[i
], stdout
);
439 if (net
->n_aliases
[i
] != NULL
)
440 putchar_unlocked (',');
442 putchar_unlocked ('\n');
446 networks_keys (int number
, char *key
[])
455 while ((net
= getnetent ()) != NULL
)
456 print_networks (net
);
461 for (i
= 0; i
< number
; ++i
)
463 if (isdigit (key
[i
][0]))
464 net
= getnetbyaddr (inet_addr (key
[i
]), AF_UNIX
);
466 net
= getnetbyname (key
[i
]);
471 print_networks (net
);
477 /* Now is all for passwd */
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
: "");
492 passwd_keys (int number
, char *key
[])
501 while ((pwd
= getpwent ()) != NULL
)
507 for (i
= 0; i
< number
; ++i
)
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
);
517 pwd
= getpwnam (key
[i
]);
528 /* This is for protocols */
530 print_protocols (struct protoent
*proto
)
534 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
537 while (proto
->p_aliases
[i
] != NULL
)
539 putchar_unlocked (' ');
540 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
543 putchar_unlocked ('\n');
547 protocols_keys (int number
, char *key
[])
551 struct protoent
*proto
;
556 while ((proto
= getprotoent ()) != NULL
)
557 print_protocols (proto
);
562 for (i
= 0; i
< number
; ++i
)
564 if (isdigit (key
[i
][0]))
565 proto
= getprotobynumber (atol (key
[i
]));
567 proto
= getprotobyname (key
[i
]);
572 print_protocols (proto
);
578 /* Now is all for rpc */
580 print_rpc (struct rpcent
*rpc
)
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');
593 rpc_keys (int number
, char *key
[])
602 while ((rpc
= getrpcent ()) != NULL
)
608 for (i
= 0; i
< number
; ++i
)
610 if (isdigit (key
[i
][0]))
611 rpc
= getrpcbynumber (atol (key
[i
]));
613 rpc
= getrpcbyname (key
[i
]);
626 print_services (struct servent
*serv
)
630 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
633 while (serv
->s_aliases
[i
] != NULL
)
635 putchar_unlocked (' ');
636 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
639 putchar_unlocked ('\n');
643 services_keys (int number
, char *key
[])
647 struct servent
*serv
;
652 while ((serv
= getservent ()) != NULL
)
653 print_services (serv
);
658 for (i
= 0; i
< number
; ++i
)
660 struct servent
*serv
;
661 char *proto
= strchr (key
[i
], '/');
666 if (isdigit (key
[i
][0]))
667 serv
= getservbyport (htons (atol (key
[i
])), proto
);
669 serv
= getservbyname (key
[i
], proto
);
674 print_services (serv
);
680 /* This is for shadow */
682 print_shadow (struct spwd
*sp
)
685 sp
->sp_namp
? sp
->sp_namp
: "",
686 sp
->sp_pwdp
? sp
->sp_pwdp
: "");
688 #define SHADOW_FIELD(n) \
690 putchar_unlocked (':'); \
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');
703 printf ("%lu\n", sp
->sp_flag
);
707 shadow_keys (int number
, char *key
[])
717 while ((sp
= getspent ()) != NULL
)
723 for (i
= 0; i
< number
; ++i
)
727 sp
= getspnam (key
[i
]);
741 int (*func
) (int number
, char *key
[]);
744 #define D(name) { #name, name ## _keys },
763 /* Handle arguments found by argp. */
765 parse_option (int key
, char *arg
, struct argp_state
*state
)
771 for (i
= 0; databases
[i
].name
; ++i
)
772 __nss_configure_lookup (databases
[i
].name
, arg
);
776 return ARGP_ERR_UNKNOWN
;
784 more_help (int key
, const char *text
, void *input
)
787 char *long_doc
, *doc
, *p
;
791 case ARGP_KEY_HELP_EXTRA
:
792 /* We print some extra information. */
794 return xstrdup (gettext ("\
795 For bug reporting instructions, please see:\n\
796 <http://www.gnu.org/software/libc/bugs.html>.\n"));
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
);
807 p
= stpcpy (doc
, long_doc
);
810 for (int i
= 0, col
= 0; databases
[i
].name
; ++i
)
812 len
= strlen (databases
[i
].name
);
824 p
= mempcpy (p
, databases
[i
].name
, len
);
835 return (char *) text
;
839 /* the main function */
841 main (int argc
, char *argv
[])
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
);
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
);