1 /* Copyright (c) 1998-2013 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, see
17 <http://www.gnu.org/licenses/>. */
19 /* getent: get entries from administrative database. */
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
39 #include <netinet/ether.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
43 /* Get libc version number. */
46 #define PACKAGE _libc_intl_domainname
48 /* Name and version of program. */
49 static void print_version (FILE *stream
, struct argp_state
*state
);
50 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
52 /* Short description of parameters. */
53 static const char args_doc
[] = N_("database [key ...]");
55 /* Supported options. */
56 static const struct argp_option args_options
[] =
58 { "service", 's', N_("CONFIG"), 0, N_("Service configuration to be used") },
59 { "no-idn", 'i', NULL
, 0, N_("disable IDN encoding") },
60 { NULL
, 0, NULL
, 0, NULL
},
63 /* Short description of program. */
64 static const char doc
[] = N_("Get entries from administrative database.");
66 /* Prototype for option handler. */
67 static error_t
parse_option (int key
, char *arg
, struct argp_state
*state
);
69 /* Function to print some extra text in the help message. */
70 static char *more_help (int key
, const char *text
, void *input
);
72 /* Data structure to communicate with argp functions. */
73 static struct argp argp
=
75 args_options
, parse_option
, args_doc
, doc
, NULL
, more_help
78 /* Additional getaddrinfo flags for IDN encoding. */
79 static int idn_flags
= AI_IDN
| AI_CANONIDN
;
81 /* Print the version information. */
83 print_version (FILE *stream
, struct argp_state
*state
)
85 fprintf (stream
, "getent %s%s\n", PKGVERSION
, VERSION
);
86 fprintf (stream
, gettext ("\
87 Copyright (C) %s Free Software Foundation, Inc.\n\
88 This is free software; see the source for copying conditions. There is NO\n\
89 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
91 fprintf (stream
, gettext ("Written by %s.\n"), "Thorsten Kukuk");
94 /* This is for aliases */
96 print_aliases (struct aliasent
*alias
)
100 printf ("%s: ", alias
->alias_name
);
101 for (i
= strlen (alias
->alias_name
); i
< 14; ++i
)
102 fputs_unlocked (" ", stdout
);
104 for (i
= 0; i
< alias
->alias_members_len
; ++i
)
106 alias
->alias_members
[i
],
107 i
+ 1 == alias
->alias_members_len
? "\n" : ", ");
111 aliases_keys (int number
, char *key
[])
115 struct aliasent
*alias
;
120 while ((alias
= getaliasent ()) != NULL
)
121 print_aliases (alias
);
126 for (i
= 0; i
< number
; ++i
)
128 alias
= getaliasbyname (key
[i
]);
133 print_aliases (alias
);
139 /* This is for ethers */
141 ethers_keys (int number
, char *key
[])
148 fprintf (stderr
, _("Enumeration not supported on %s\n"), "ethers");
152 for (i
= 0; i
< number
; ++i
)
154 struct ether_addr
*ethp
, eth
;
155 char buffer
[1024], *p
;
157 ethp
= ether_aton (key
[i
]);
160 if (ether_ntohost (buffer
, ethp
))
169 if (ether_hostton (key
[i
], ð
))
177 printf ("%s %s\n", ether_ntoa (ethp
), p
);
183 /* This is for group */
185 print_group (struct group
*grp
)
189 printf ("%s:%s:%lu:", grp
->gr_name
? grp
->gr_name
: "",
190 grp
->gr_passwd
? grp
->gr_passwd
: "",
191 (unsigned long int) grp
->gr_gid
);
193 while (grp
->gr_mem
[i
] != NULL
)
195 fputs_unlocked (grp
->gr_mem
[i
], stdout
);
197 if (grp
->gr_mem
[i
] != NULL
)
198 putchar_unlocked (',');
200 putchar_unlocked ('\n');
204 group_keys (int number
, char *key
[])
213 while ((grp
= getgrent ()) != NULL
)
219 for (i
= 0; i
< number
; ++i
)
223 gid_t arg_gid
= strtoul(key
[i
], &ep
, 10);
225 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
226 /* Valid numeric gid. */
227 grp
= getgrgid (arg_gid
);
229 grp
= getgrnam (key
[i
]);
240 /* This is for gshadow */
242 print_gshadow (struct sgrp
*sg
)
247 sg
->sg_namp
? sg
->sg_namp
: "",
248 sg
->sg_passwd
? sg
->sg_passwd
: "");
250 while (sg
->sg_adm
[i
] != NULL
)
252 fputs_unlocked (sg
->sg_adm
[i
], stdout
);
254 if (sg
->sg_adm
[i
] != NULL
)
255 putchar_unlocked (',');
258 putchar_unlocked (':');
261 while (sg
->sg_mem
[i
] != NULL
)
263 fputs_unlocked (sg
->sg_mem
[i
], stdout
);
265 if (sg
->sg_mem
[i
] != NULL
)
266 putchar_unlocked (',');
269 putchar_unlocked ('\n');
273 gshadow_keys (int number
, char *key
[])
283 while ((sg
= getsgent ()) != NULL
)
289 for (i
= 0; i
< number
; ++i
)
293 sg
= getsgnam (key
[i
]);
304 /* This is for hosts */
306 print_hosts (struct hostent
*host
)
310 for (cnt
= 0; host
->h_addr_list
[cnt
] != NULL
; ++cnt
)
312 char buf
[INET6_ADDRSTRLEN
];
313 const char *ip
= inet_ntop (host
->h_addrtype
, host
->h_addr_list
[cnt
],
316 printf ("%-15s %s", ip
, host
->h_name
);
319 for (i
= 0; host
->h_aliases
[i
] != NULL
; ++i
)
321 putchar_unlocked (' ');
322 fputs_unlocked (host
->h_aliases
[i
], stdout
);
324 putchar_unlocked ('\n');
329 hosts_keys (int number
, char *key
[])
333 struct hostent
*host
;
338 while ((host
= gethostent ()) != NULL
)
344 for (i
= 0; i
< number
; ++i
)
346 struct hostent
*host
= NULL
;
347 char addr
[IN6ADDRSZ
];
349 if (inet_pton (AF_INET6
, key
[i
], &addr
) > 0)
350 host
= gethostbyaddr (addr
, IN6ADDRSZ
, AF_INET6
);
351 else if (inet_pton (AF_INET
, key
[i
], &addr
) > 0)
352 host
= gethostbyaddr (addr
, INADDRSZ
, AF_INET
);
353 else if ((host
= gethostbyname2 (key
[i
], AF_INET6
)) == NULL
)
354 host
= gethostbyname2 (key
[i
], AF_INET
);
365 /* This is for hosts, but using getaddrinfo */
367 ahosts_keys_int (int af
, int xflags
, int number
, char *key
[])
371 struct hostent
*host
;
376 while ((host
= gethostent ()) != NULL
)
382 struct addrinfo hint
;
383 memset (&hint
, '\0', sizeof (hint
));
384 hint
.ai_flags
= (AI_V4MAPPED
| AI_ADDRCONFIG
| AI_CANONNAME
385 | idn_flags
| xflags
);
388 for (i
= 0; i
< number
; ++i
)
390 struct addrinfo
*res
;
392 if (getaddrinfo (key
[i
], NULL
, &hint
, &res
) != 0)
396 struct addrinfo
*runp
= res
;
402 if (runp
->ai_socktype
== SOCK_STREAM
)
404 else if (runp
->ai_socktype
== SOCK_DGRAM
)
406 else if (runp
->ai_socktype
== SOCK_RAW
)
408 #ifdef SOCK_SEQPACKET
409 else if (runp
->ai_socktype
== SOCK_SEQPACKET
)
410 sockstr
= "SEQPACKET";
413 else if (runp
->ai_socktype
== SOCK_RDM
)
417 else if (runp
->ai_socktype
== SOCK_DCCP
)
421 else if (runp
->ai_socktype
== SOCK_PACKET
)
426 snprintf (sockbuf
, sizeof (sockbuf
), "%d",
431 char buf
[INET6_ADDRSTRLEN
];
432 printf ("%-15s %-6s %s\n",
433 inet_ntop (runp
->ai_family
,
434 runp
->ai_family
== AF_INET
435 ? (void *) &((struct sockaddr_in
*) runp
->ai_addr
)->sin_addr
436 : (void *) &((struct sockaddr_in6
*) runp
->ai_addr
)->sin6_addr
,
439 runp
->ai_canonname
?: "");
441 runp
= runp
->ai_next
;
452 ahosts_keys (int number
, char *key
[])
454 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
458 ahostsv4_keys (int number
, char *key
[])
460 return ahosts_keys_int (AF_INET
, 0, number
, key
);
464 ahostsv6_keys (int number
, char *key
[])
466 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
469 /* This is for netgroup */
471 netgroup_keys (int number
, char *key
[])
477 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
483 char *host
= strcmp (key
[1], "*") == 0 ? NULL
: key
[1];
484 char *user
= strcmp (key
[2], "*") == 0 ? NULL
: key
[2];
485 char *domain
= strcmp (key
[3], "*") == 0 ? NULL
: key
[3];
487 printf ("%-21s (%s,%s,%s) = %d\n",
488 key
[0], host
?: "", user
?: "", domain
?: "",
489 innetgr (key
[0], host
, user
, domain
));
491 else if (number
== 1)
493 if (!setnetgrent (key
[0]))
499 printf ("%-21s", key
[0]);
501 while (getnetgrent (p
, p
+ 1, p
+ 2))
502 printf (" (%s,%s,%s)", p
[0] ?: " ", p
[1] ?: "", p
[2] ?: "");
503 putchar_unlocked ('\n');
512 /* This is for initgroups */
514 initgroups_keys (int number
, char *key
[])
517 size_t grpslen
= ngrps
* sizeof (gid_t
);
518 gid_t
*grps
= alloca (grpslen
);
522 fprintf (stderr
, _("Enumeration not supported on %s\n"), "initgroups");
526 for (int i
= 0; i
< number
; ++i
)
530 while ((n
= getgrouplist (key
[i
], -1, grps
, &no
)) == -1
533 grps
= extend_alloca (grps
, grpslen
, no
* sizeof (gid_t
));
540 printf ("%-21s", key
[i
]);
541 for (int j
= 0; j
< n
; ++j
)
543 printf (" %ld", (long int) grps
[j
]);
544 putchar_unlocked ('\n');
550 /* This is for networks */
552 print_networks (struct netent
*net
)
556 ip
.s_addr
= htonl (net
->n_net
);
558 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
561 while (net
->n_aliases
[i
] != NULL
)
563 putchar_unlocked (' ');
564 fputs_unlocked (net
->n_aliases
[i
], stdout
);
567 putchar_unlocked ('\n');
571 networks_keys (int number
, char *key
[])
580 while ((net
= getnetent ()) != NULL
)
581 print_networks (net
);
586 for (i
= 0; i
< number
; ++i
)
588 if (isdigit (key
[i
][0]))
589 net
= getnetbyaddr (ntohl (inet_addr (key
[i
])), AF_UNSPEC
);
591 net
= getnetbyname (key
[i
]);
596 print_networks (net
);
602 /* Now is all for passwd */
604 print_passwd (struct passwd
*pwd
)
606 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
607 pwd
->pw_name
? pwd
->pw_name
: "",
608 pwd
->pw_passwd
? pwd
->pw_passwd
: "",
609 (unsigned long int) pwd
->pw_uid
,
610 (unsigned long int) pwd
->pw_gid
,
611 pwd
->pw_gecos
? pwd
->pw_gecos
: "",
612 pwd
->pw_dir
? pwd
->pw_dir
: "",
613 pwd
->pw_shell
? pwd
->pw_shell
: "");
617 passwd_keys (int number
, char *key
[])
626 while ((pwd
= getpwent ()) != NULL
)
632 for (i
= 0; i
< number
; ++i
)
636 uid_t arg_uid
= strtoul(key
[i
], &ep
, 10);
638 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
639 /* Valid numeric uid. */
640 pwd
= getpwuid (arg_uid
);
642 pwd
= getpwnam (key
[i
]);
653 /* This is for protocols */
655 print_protocols (struct protoent
*proto
)
659 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
662 while (proto
->p_aliases
[i
] != NULL
)
664 putchar_unlocked (' ');
665 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
668 putchar_unlocked ('\n');
672 protocols_keys (int number
, char *key
[])
676 struct protoent
*proto
;
681 while ((proto
= getprotoent ()) != NULL
)
682 print_protocols (proto
);
687 for (i
= 0; i
< number
; ++i
)
689 if (isdigit (key
[i
][0]))
690 proto
= getprotobynumber (atol (key
[i
]));
692 proto
= getprotobyname (key
[i
]);
697 print_protocols (proto
);
703 /* Now is all for rpc */
705 print_rpc (struct rpcent
*rpc
)
709 printf ("%-15s %d%s",
710 rpc
->r_name
, rpc
->r_number
, rpc
->r_aliases
[0] ? " " : "");
712 for (i
= 0; rpc
->r_aliases
[i
]; ++i
)
713 printf (" %s", rpc
->r_aliases
[i
]);
714 putchar_unlocked ('\n');
718 rpc_keys (int number
, char *key
[])
727 while ((rpc
= getrpcent ()) != NULL
)
733 for (i
= 0; i
< number
; ++i
)
735 if (isdigit (key
[i
][0]))
736 rpc
= getrpcbynumber (atol (key
[i
]));
738 rpc
= getrpcbyname (key
[i
]);
751 print_services (struct servent
*serv
)
755 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
758 while (serv
->s_aliases
[i
] != NULL
)
760 putchar_unlocked (' ');
761 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
764 putchar_unlocked ('\n');
768 services_keys (int number
, char *key
[])
772 struct servent
*serv
;
777 while ((serv
= getservent ()) != NULL
)
778 print_services (serv
);
783 for (i
= 0; i
< number
; ++i
)
785 struct servent
*serv
;
786 char *proto
= strchr (key
[i
], '/');
792 long port
= strtol (key
[i
], &endptr
, 10);
794 if (isdigit (key
[i
][0]) && *endptr
== '\0'
795 && 0 <= port
&& port
<= 65535)
796 serv
= getservbyport (htons (port
), proto
);
798 serv
= getservbyname (key
[i
], proto
);
803 print_services (serv
);
809 /* This is for shadow */
811 print_shadow (struct spwd
*sp
)
814 sp
->sp_namp
? sp
->sp_namp
: "",
815 sp
->sp_pwdp
? sp
->sp_pwdp
: "");
817 #define SHADOW_FIELD(n) \
819 putchar_unlocked (':'); \
821 printf ("%ld:", sp->n)
823 SHADOW_FIELD (sp_lstchg
);
824 SHADOW_FIELD (sp_min
);
825 SHADOW_FIELD (sp_max
);
826 SHADOW_FIELD (sp_warn
);
827 SHADOW_FIELD (sp_inact
);
828 SHADOW_FIELD (sp_expire
);
829 if (sp
->sp_flag
== ~0ul)
830 putchar_unlocked ('\n');
832 printf ("%lu\n", sp
->sp_flag
);
836 shadow_keys (int number
, char *key
[])
846 while ((sp
= getspent ()) != NULL
)
852 for (i
= 0; i
< number
; ++i
)
856 sp
= getspnam (key
[i
]);
870 int (*func
) (int number
, char *key
[]);
873 #define D(name) { #name, name ## _keys },
894 /* Handle arguments found by argp. */
896 parse_option (int key
, char *arg
, struct argp_state
*state
)
902 endp
= strchr (arg
, ':');
904 /* No specific database, change them all. */
905 for (int i
= 0; databases
[i
].name
!= NULL
; ++i
)
906 __nss_configure_lookup (databases
[i
].name
, arg
);
910 for (i
= 0; databases
[i
].name
!= NULL
; ++i
)
911 if (strncmp (databases
[i
].name
, arg
, endp
- arg
) == 0)
913 __nss_configure_lookup (databases
[i
].name
, endp
+ 1);
916 if (databases
[i
].name
== NULL
)
917 error (EXIT_FAILURE
, 0, gettext ("Unknown database name"));
926 return ARGP_ERR_UNKNOWN
;
934 more_help (int key
, const char *text
, void *input
)
942 case ARGP_KEY_HELP_EXTRA
:
943 /* We print some extra information. */
944 fp
= open_memstream (&doc
, &len
);
947 fputs_unlocked (_("Supported databases:\n"), fp
);
949 for (int i
= 0, col
= 0; databases
[i
].name
!= NULL
; ++i
)
951 len
= strlen (databases
[i
].name
);
957 fputc_unlocked ('\n', fp
);
960 fputc_unlocked (' ', fp
);
963 fputs_unlocked (databases
[i
].name
, fp
);
969 fprintf (fp
, gettext ("\
970 For bug reporting instructions, please see:\n\
971 %s.\n"), REPORT_BUGS_TO
);
973 if (fclose (fp
) == 0)
981 return (char *) text
;
985 /* the main function */
987 main (int argc
, char *argv
[])
989 /* Debugging support. */
992 /* Set locale via LC_ALL. */
993 setlocale (LC_ALL
, "");
994 /* Set the text message domain. */
995 textdomain (PACKAGE
);
997 /* Parse and process arguments. */
999 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
1001 if ((argc
- remaining
) < 1)
1003 error (0, 0, gettext ("wrong number of arguments"));
1004 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
1008 for (int i
= 0; databases
[i
].name
; ++i
)
1009 if (argv
[remaining
][0] == databases
[i
].name
[0]
1010 && !strcmp (argv
[remaining
], databases
[i
].name
))
1011 return databases
[i
].func (argc
- remaining
- 1, &argv
[remaining
+ 1]);
1013 fprintf (stderr
, _("Unknown database: %s\n"), argv
[remaining
]);
1014 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);