1 /* Copyright (c) 1998-2018 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
)
187 if (putgrent (grp
, stdout
) != 0)
188 fprintf (stderr
, "error writing group entry: %m\n");
192 group_keys (int number
, char *key
[])
201 while ((grp
= getgrent ()) != NULL
)
207 for (i
= 0; i
< number
; ++i
)
211 gid_t arg_gid
= strtoul(key
[i
], &ep
, 10);
213 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
214 /* Valid numeric gid. */
215 grp
= getgrgid (arg_gid
);
217 grp
= getgrnam (key
[i
]);
228 /* This is for gshadow */
230 print_gshadow (struct sgrp
*sg
)
232 if (putsgent (sg
, stdout
) != 0)
233 fprintf (stderr
, "error writing gshadow entry: %m\n");
237 gshadow_keys (int number
, char *key
[])
247 while ((sg
= getsgent ()) != NULL
)
253 for (i
= 0; i
< number
; ++i
)
257 sg
= getsgnam (key
[i
]);
268 /* This is for hosts */
270 print_hosts (struct hostent
*host
)
274 for (cnt
= 0; host
->h_addr_list
[cnt
] != NULL
; ++cnt
)
276 char buf
[INET6_ADDRSTRLEN
];
277 const char *ip
= inet_ntop (host
->h_addrtype
, host
->h_addr_list
[cnt
],
280 printf ("%-15s %s", ip
, host
->h_name
);
283 for (i
= 0; host
->h_aliases
[i
] != NULL
; ++i
)
285 putchar_unlocked (' ');
286 fputs_unlocked (host
->h_aliases
[i
], stdout
);
288 putchar_unlocked ('\n');
293 hosts_keys (int number
, char *key
[])
297 struct hostent
*host
;
302 while ((host
= gethostent ()) != NULL
)
308 for (i
= 0; i
< number
; ++i
)
310 struct hostent
*host
= NULL
;
311 char addr
[IN6ADDRSZ
];
313 if (inet_pton (AF_INET6
, key
[i
], &addr
) > 0)
314 host
= gethostbyaddr (addr
, IN6ADDRSZ
, AF_INET6
);
315 else if (inet_pton (AF_INET
, key
[i
], &addr
) > 0)
316 host
= gethostbyaddr (addr
, INADDRSZ
, AF_INET
);
317 else if ((host
= gethostbyname2 (key
[i
], AF_INET6
)) == NULL
)
318 host
= gethostbyname2 (key
[i
], AF_INET
);
329 /* This is for hosts, but using getaddrinfo */
331 ahosts_keys_int (int af
, int xflags
, int number
, char *key
[])
335 struct hostent
*host
;
340 while ((host
= gethostent ()) != NULL
)
346 struct addrinfo hint
;
347 memset (&hint
, '\0', sizeof (hint
));
348 hint
.ai_flags
= (AI_V4MAPPED
| AI_ADDRCONFIG
| AI_CANONNAME
349 | idn_flags
| xflags
);
352 for (i
= 0; i
< number
; ++i
)
354 struct addrinfo
*res
;
356 if (getaddrinfo (key
[i
], NULL
, &hint
, &res
) != 0)
360 struct addrinfo
*runp
= res
;
366 if (runp
->ai_socktype
== SOCK_STREAM
)
368 else if (runp
->ai_socktype
== SOCK_DGRAM
)
370 else if (runp
->ai_socktype
== SOCK_RAW
)
372 #ifdef SOCK_SEQPACKET
373 else if (runp
->ai_socktype
== SOCK_SEQPACKET
)
374 sockstr
= "SEQPACKET";
377 else if (runp
->ai_socktype
== SOCK_RDM
)
381 else if (runp
->ai_socktype
== SOCK_DCCP
)
385 else if (runp
->ai_socktype
== SOCK_PACKET
)
390 snprintf (sockbuf
, sizeof (sockbuf
), "%d",
395 char buf
[INET6_ADDRSTRLEN
];
396 printf ("%-15s %-6s %s\n",
397 inet_ntop (runp
->ai_family
,
398 runp
->ai_family
== AF_INET
399 ? (void *) &((struct sockaddr_in
*) runp
->ai_addr
)->sin_addr
400 : (void *) &((struct sockaddr_in6
*) runp
->ai_addr
)->sin6_addr
,
403 runp
->ai_canonname
?: "");
405 runp
= runp
->ai_next
;
416 ahosts_keys (int number
, char *key
[])
418 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
422 ahostsv4_keys (int number
, char *key
[])
424 return ahosts_keys_int (AF_INET
, 0, number
, key
);
428 ahostsv6_keys (int number
, char *key
[])
430 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
433 /* This is for netgroup */
435 netgroup_keys (int number
, char *key
[])
441 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
447 char *host
= strcmp (key
[1], "*") == 0 ? NULL
: key
[1];
448 char *user
= strcmp (key
[2], "*") == 0 ? NULL
: key
[2];
449 char *domain
= strcmp (key
[3], "*") == 0 ? NULL
: key
[3];
451 printf ("%-21s (%s,%s,%s) = %d\n",
452 key
[0], host
?: "", user
?: "", domain
?: "",
453 innetgr (key
[0], host
, user
, domain
));
455 else if (number
== 1)
457 if (!setnetgrent (key
[0]))
463 printf ("%-21s", key
[0]);
465 while (getnetgrent (p
, p
+ 1, p
+ 2))
466 printf (" (%s,%s,%s)", p
[0] ?: " ", p
[1] ?: "", p
[2] ?: "");
467 putchar_unlocked ('\n');
476 /* This is for initgroups */
478 initgroups_keys (int number
, char *key
[])
481 size_t grpslen
= ngrps
* sizeof (gid_t
);
482 gid_t
*grps
= alloca (grpslen
);
486 fprintf (stderr
, _("Enumeration not supported on %s\n"), "initgroups");
490 for (int i
= 0; i
< number
; ++i
)
494 while ((n
= getgrouplist (key
[i
], -1, grps
, &no
)) == -1
497 grps
= extend_alloca (grps
, grpslen
, no
* sizeof (gid_t
));
504 printf ("%-21s", key
[i
]);
505 for (int j
= 0; j
< n
; ++j
)
507 printf (" %ld", (long int) grps
[j
]);
508 putchar_unlocked ('\n');
514 /* This is for networks */
516 print_networks (struct netent
*net
)
520 ip
.s_addr
= htonl (net
->n_net
);
522 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
525 while (net
->n_aliases
[i
] != NULL
)
527 putchar_unlocked (' ');
528 fputs_unlocked (net
->n_aliases
[i
], stdout
);
531 putchar_unlocked ('\n');
535 networks_keys (int number
, char *key
[])
544 while ((net
= getnetent ()) != NULL
)
545 print_networks (net
);
550 for (i
= 0; i
< number
; ++i
)
552 if (isdigit (key
[i
][0]))
553 net
= getnetbyaddr (ntohl (inet_addr (key
[i
])), AF_UNSPEC
);
555 net
= getnetbyname (key
[i
]);
560 print_networks (net
);
566 /* Now is all for passwd */
568 print_passwd (struct passwd
*pwd
)
570 if (putpwent (pwd
, stdout
) != 0)
571 fprintf (stderr
, "error writing passwd entry: %m\n");
575 passwd_keys (int number
, char *key
[])
584 while ((pwd
= getpwent ()) != NULL
)
590 for (i
= 0; i
< number
; ++i
)
594 uid_t arg_uid
= strtoul(key
[i
], &ep
, 10);
596 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
597 /* Valid numeric uid. */
598 pwd
= getpwuid (arg_uid
);
600 pwd
= getpwnam (key
[i
]);
611 /* This is for protocols */
613 print_protocols (struct protoent
*proto
)
617 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
620 while (proto
->p_aliases
[i
] != NULL
)
622 putchar_unlocked (' ');
623 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
626 putchar_unlocked ('\n');
630 protocols_keys (int number
, char *key
[])
634 struct protoent
*proto
;
639 while ((proto
= getprotoent ()) != NULL
)
640 print_protocols (proto
);
645 for (i
= 0; i
< number
; ++i
)
647 if (isdigit (key
[i
][0]))
648 proto
= getprotobynumber (atol (key
[i
]));
650 proto
= getprotobyname (key
[i
]);
655 print_protocols (proto
);
662 /* Now is all for rpc */
664 print_rpc (struct rpcent
*rpc
)
668 printf ("%-15s %d%s",
669 rpc
->r_name
, rpc
->r_number
, rpc
->r_aliases
[0] ? " " : "");
671 for (i
= 0; rpc
->r_aliases
[i
]; ++i
)
672 printf (" %s", rpc
->r_aliases
[i
]);
673 putchar_unlocked ('\n');
677 rpc_keys (int number
, char *key
[])
686 while ((rpc
= getrpcent ()) != NULL
)
692 for (i
= 0; i
< number
; ++i
)
694 if (isdigit (key
[i
][0]))
695 rpc
= getrpcbynumber (atol (key
[i
]));
697 rpc
= getrpcbyname (key
[i
]);
711 print_services (struct servent
*serv
)
715 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
718 while (serv
->s_aliases
[i
] != NULL
)
720 putchar_unlocked (' ');
721 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
724 putchar_unlocked ('\n');
728 services_keys (int number
, char *key
[])
732 struct servent
*serv
;
737 while ((serv
= getservent ()) != NULL
)
738 print_services (serv
);
743 for (i
= 0; i
< number
; ++i
)
745 struct servent
*serv
;
746 char *proto
= strchr (key
[i
], '/');
752 long port
= strtol (key
[i
], &endptr
, 10);
754 if (isdigit (key
[i
][0]) && *endptr
== '\0'
755 && 0 <= port
&& port
<= 65535)
756 serv
= getservbyport (htons (port
), proto
);
758 serv
= getservbyname (key
[i
], proto
);
763 print_services (serv
);
769 /* This is for shadow */
771 print_shadow (struct spwd
*sp
)
773 if (putspent (sp
, stdout
) != 0)
774 fprintf (stderr
, "error writing shadow entry: %m\n");
778 shadow_keys (int number
, char *key
[])
788 while ((sp
= getspent ()) != NULL
)
794 for (i
= 0; i
< number
; ++i
)
798 sp
= getspnam (key
[i
]);
812 int (*func
) (int number
, char *key
[]);
815 #define D(name) { #name, name ## _keys },
838 /* Handle arguments found by argp. */
840 parse_option (int key
, char *arg
, struct argp_state
*state
)
846 endp
= strchr (arg
, ':');
848 /* No specific database, change them all. */
849 for (int i
= 0; databases
[i
].name
!= NULL
; ++i
)
850 __nss_configure_lookup (databases
[i
].name
, arg
);
854 for (i
= 0; databases
[i
].name
!= NULL
; ++i
)
855 if (strncmp (databases
[i
].name
, arg
, endp
- arg
) == 0)
857 __nss_configure_lookup (databases
[i
].name
, endp
+ 1);
860 if (databases
[i
].name
== NULL
)
861 error (EXIT_FAILURE
, 0, gettext ("Unknown database name"));
870 return ARGP_ERR_UNKNOWN
;
878 more_help (int key
, const char *text
, void *input
)
886 case ARGP_KEY_HELP_EXTRA
:
887 /* We print some extra information. */
888 fp
= open_memstream (&doc
, &len
);
891 fputs_unlocked (_("Supported databases:\n"), fp
);
893 for (int i
= 0, col
= 0; databases
[i
].name
!= NULL
; ++i
)
895 len
= strlen (databases
[i
].name
);
901 fputc_unlocked ('\n', fp
);
904 fputc_unlocked (' ', fp
);
907 fputs_unlocked (databases
[i
].name
, fp
);
913 fprintf (fp
, gettext ("\
914 For bug reporting instructions, please see:\n\
915 %s.\n"), REPORT_BUGS_TO
);
917 if (fclose (fp
) == 0)
925 return (char *) text
;
929 /* the main function */
931 main (int argc
, char *argv
[])
933 /* Debugging support. */
936 /* Set locale via LC_ALL. */
937 setlocale (LC_ALL
, "");
938 /* Set the text message domain. */
939 textdomain (PACKAGE
);
941 /* Parse and process arguments. */
943 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
945 if ((argc
- remaining
) < 1)
947 error (0, 0, gettext ("wrong number of arguments"));
948 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
952 for (int i
= 0; databases
[i
].name
; ++i
)
953 if (argv
[remaining
][0] == databases
[i
].name
[0]
954 && !strcmp (argv
[remaining
], databases
[i
].name
))
955 return databases
[i
].func (argc
- remaining
- 1, &argv
[remaining
+ 1]);
957 fprintf (stderr
, _("Unknown database: %s\n"), argv
[remaining
]);
958 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);