1 /* Copyright (c) 1998-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* 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>
41 #include <scratch_buffer.h>
44 /* Get libc version number. */
47 #define PACKAGE _libc_intl_domainname
49 /* Name and version of program. */
50 static void print_version (FILE *stream
, struct argp_state
*state
);
51 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
53 /* Short description of parameters. */
54 static const char args_doc
[] = N_("database [key ...]");
56 /* Supported options. */
57 static const struct argp_option args_options
[] =
59 { "service", 's', N_("CONFIG"), 0, N_("Service configuration to be used") },
60 { "no-idn", 'i', NULL
, 0, N_("disable IDN encoding") },
61 { "no-addrconfig", 'A', NULL
, 0,
62 N_("do not filter out unsupported IPv4/IPv6 addresses (with ahosts*)") },
63 { NULL
, 0, NULL
, 0, NULL
},
66 /* Short description of program. */
67 static const char doc
[] = N_("Get entries from administrative database.");
69 /* Prototype for option handler. */
70 static error_t
parse_option (int key
, char *arg
, struct argp_state
*state
);
72 /* Function to print some extra text in the help message. */
73 static char *more_help (int key
, const char *text
, void *input
);
75 /* Data structure to communicate with argp functions. */
76 static struct argp argp
=
78 args_options
, parse_option
, args_doc
, doc
, NULL
, more_help
81 /* Additional getaddrinfo flags for IDN encoding. */
82 static int idn_flags
= AI_IDN
| AI_CANONIDN
;
84 /* Set to 0 by --no-addrconfig. */
85 static int addrconfig_flags
= AI_ADDRCONFIG
;
87 /* Print the version information. */
89 print_version (FILE *stream
, struct argp_state
*state
)
91 fprintf (stream
, "getent %s%s\n", PKGVERSION
, VERSION
);
92 fprintf (stream
, gettext ("\
93 Copyright (C) %s Free Software Foundation, Inc.\n\
94 This is free software; see the source for copying conditions. There is NO\n\
95 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
97 fprintf (stream
, gettext ("Written by %s.\n"), "Thorsten Kukuk");
100 /* This is for aliases */
102 print_aliases (struct aliasent
*alias
)
106 printf ("%s: ", alias
->alias_name
);
107 for (i
= strlen (alias
->alias_name
); i
< 14; ++i
)
108 fputs_unlocked (" ", stdout
);
110 for (i
= 0; i
< alias
->alias_members_len
; ++i
)
112 alias
->alias_members
[i
],
113 i
+ 1 == alias
->alias_members_len
? "\n" : ", ");
117 aliases_keys (int number
, char *key
[])
121 struct aliasent
*alias
;
126 while ((alias
= getaliasent ()) != NULL
)
127 print_aliases (alias
);
132 for (i
= 0; i
< number
; ++i
)
134 alias
= getaliasbyname (key
[i
]);
139 print_aliases (alias
);
145 /* This is for ethers */
147 ethers_keys (int number
, char *key
[])
154 fprintf (stderr
, _("Enumeration not supported on %s\n"), "ethers");
158 for (i
= 0; i
< number
; ++i
)
160 struct ether_addr
*ethp
, eth
;
161 char buffer
[1024], *p
;
163 ethp
= ether_aton (key
[i
]);
166 if (ether_ntohost (buffer
, ethp
))
175 if (ether_hostton (key
[i
], ð
))
183 printf ("%s %s\n", ether_ntoa (ethp
), p
);
189 /* This is for group */
191 print_group (struct group
*grp
)
193 if (putgrent (grp
, stdout
) != 0)
194 fprintf (stderr
, "error writing group entry: %m\n");
198 group_keys (int number
, char *key
[])
207 while ((grp
= getgrent ()) != NULL
)
213 for (i
= 0; i
< number
; ++i
)
217 gid_t arg_gid
= strtoul(key
[i
], &ep
, 10);
219 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
220 /* Valid numeric gid. */
221 grp
= getgrgid (arg_gid
);
223 grp
= getgrnam (key
[i
]);
234 /* This is for gshadow */
236 print_gshadow (struct sgrp
*sg
)
238 if (putsgent (sg
, stdout
) != 0)
239 fprintf (stderr
, "error writing gshadow entry: %m\n");
243 gshadow_keys (int number
, char *key
[])
253 while ((sg
= getsgent ()) != NULL
)
259 for (i
= 0; i
< number
; ++i
)
263 sg
= getsgnam (key
[i
]);
274 /* This is for hosts */
276 print_hosts (struct hostent
*host
)
280 for (cnt
= 0; host
->h_addr_list
[cnt
] != NULL
; ++cnt
)
282 char buf
[INET6_ADDRSTRLEN
];
283 const char *ip
= inet_ntop (host
->h_addrtype
, host
->h_addr_list
[cnt
],
286 printf ("%-15s %s", ip
, host
->h_name
);
289 for (i
= 0; host
->h_aliases
[i
] != NULL
; ++i
)
291 putchar_unlocked (' ');
292 fputs_unlocked (host
->h_aliases
[i
], stdout
);
294 putchar_unlocked ('\n');
299 hosts_keys (int number
, char *key
[])
303 struct hostent
*host
;
308 while ((host
= gethostent ()) != NULL
)
314 for (i
= 0; i
< number
; ++i
)
316 struct hostent
*host
= NULL
;
317 char addr
[IN6ADDRSZ
];
319 if (inet_pton (AF_INET6
, key
[i
], &addr
) > 0)
320 host
= gethostbyaddr (addr
, IN6ADDRSZ
, AF_INET6
);
321 else if (inet_pton (AF_INET
, key
[i
], &addr
) > 0)
322 host
= gethostbyaddr (addr
, INADDRSZ
, AF_INET
);
323 else if ((host
= gethostbyname2 (key
[i
], AF_INET6
)) == NULL
)
324 host
= gethostbyname2 (key
[i
], AF_INET
);
335 /* This is for hosts, but using getaddrinfo */
337 ahosts_keys_int (int af
, int xflags
, int number
, char *key
[])
341 struct hostent
*host
;
346 while ((host
= gethostent ()) != NULL
)
352 struct addrinfo hint
;
353 memset (&hint
, '\0', sizeof (hint
));
354 hint
.ai_flags
= (AI_V4MAPPED
| addrconfig_flags
| AI_CANONNAME
355 | idn_flags
| xflags
);
358 for (i
= 0; i
< number
; ++i
)
360 struct addrinfo
*res
;
362 if (getaddrinfo (key
[i
], NULL
, &hint
, &res
) != 0)
366 struct addrinfo
*runp
= res
;
372 if (runp
->ai_socktype
== SOCK_STREAM
)
374 else if (runp
->ai_socktype
== SOCK_DGRAM
)
376 else if (runp
->ai_socktype
== SOCK_RAW
)
378 #ifdef SOCK_SEQPACKET
379 else if (runp
->ai_socktype
== SOCK_SEQPACKET
)
380 sockstr
= "SEQPACKET";
383 else if (runp
->ai_socktype
== SOCK_RDM
)
387 else if (runp
->ai_socktype
== SOCK_DCCP
)
391 else if (runp
->ai_socktype
== SOCK_PACKET
)
396 snprintf (sockbuf
, sizeof (sockbuf
), "%d",
401 /* Three digits per byte, plus '%' and null terminator. */
402 char scope
[3 * sizeof (uint32_t) + 2];
403 struct sockaddr_in6
*addr6
404 = (struct sockaddr_in6
*) runp
->ai_addr
;
405 if (runp
->ai_family
!= AF_INET6
|| addr6
->sin6_scope_id
== 0)
406 /* No scope ID present. */
409 snprintf (scope
, sizeof (scope
), "%%%" PRIu32
,
410 addr6
->sin6_scope_id
);
412 char buf
[INET6_ADDRSTRLEN
];
413 if (inet_ntop (runp
->ai_family
,
414 runp
->ai_family
== AF_INET
415 ? (void *) &((struct sockaddr_in
*) runp
->ai_addr
)->sin_addr
417 buf
, sizeof (buf
)) == NULL
)
419 strcpy (buf
, "<invalid>");
423 int pad
= 15 - strlen (buf
) - strlen (scope
);
427 printf ("%s%-*s %-6s %s\n",
428 buf
, pad
, scope
, sockstr
, runp
->ai_canonname
?: "");
430 runp
= runp
->ai_next
;
441 ahosts_keys (int number
, char *key
[])
443 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
447 ahostsv4_keys (int number
, char *key
[])
449 return ahosts_keys_int (AF_INET
, 0, number
, key
);
453 ahostsv6_keys (int number
, char *key
[])
455 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
458 /* This is for netgroup */
460 netgroup_keys (int number
, char *key
[])
466 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
472 char *host
= strcmp (key
[1], "*") == 0 ? NULL
: key
[1];
473 char *user
= strcmp (key
[2], "*") == 0 ? NULL
: key
[2];
474 char *domain
= strcmp (key
[3], "*") == 0 ? NULL
: key
[3];
476 printf ("%-21s (%s,%s,%s) = %d\n",
477 key
[0], host
?: "", user
?: "", domain
?: "",
478 innetgr (key
[0], host
, user
, domain
));
480 else if (number
== 1)
482 if (!setnetgrent (key
[0]))
488 printf ("%-21s", key
[0]);
490 while (getnetgrent (p
, p
+ 1, p
+ 2))
491 printf (" (%s,%s,%s)", p
[0] ?: " ", p
[1] ?: "", p
[2] ?: "");
492 putchar_unlocked ('\n');
501 #define DYNARRAY_STRUCT gid_list
502 #define DYNARRAY_ELEMENT gid_t
503 #define DYNARRAY_PREFIX gid_list_
504 #define DYNARRAY_INITIAL_SIZE 10
505 #include <malloc/dynarray-skeleton.c>
507 /* This is for initgroups */
509 initgroups_keys (int number
, char *key
[])
513 fprintf (stderr
, _("Enumeration not supported on %s\n"), "initgroups");
517 struct gid_list list
;
518 gid_list_init (&list
);
519 if (!gid_list_resize (&list
, 10))
521 fprintf (stderr
, _("Could not allocate group list: %m\n"));
525 for (int i
= 0; i
< number
; ++i
)
527 int no
= gid_list_size (&list
);
529 while ((n
= getgrouplist (key
[i
], -1, gid_list_begin (&list
), &no
)) == -1
530 && no
> gid_list_size (&list
))
532 if (!gid_list_resize (&list
, no
))
534 fprintf (stderr
, _("Could not allocate group list: %m\n"));
541 gid_list_free (&list
);
545 const gid_t
*grps
= gid_list_begin (&list
);
546 printf ("%-21s", key
[i
]);
547 for (int j
= 0; j
< n
; ++j
)
549 printf (" %ld", (long int) grps
[j
]);
550 putchar_unlocked ('\n');
553 gid_list_free (&list
);
558 /* This is for networks */
560 print_networks (struct netent
*net
)
564 ip
.s_addr
= htonl (net
->n_net
);
566 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
569 while (net
->n_aliases
[i
] != NULL
)
571 putchar_unlocked (' ');
572 fputs_unlocked (net
->n_aliases
[i
], stdout
);
575 putchar_unlocked ('\n');
579 networks_keys (int number
, char *key
[])
588 while ((net
= getnetent ()) != NULL
)
589 print_networks (net
);
594 for (i
= 0; i
< number
; ++i
)
596 if (isdigit (key
[i
][0]))
597 net
= getnetbyaddr (ntohl (inet_addr (key
[i
])), AF_UNSPEC
);
599 net
= getnetbyname (key
[i
]);
604 print_networks (net
);
610 /* Now is all for passwd */
612 print_passwd (struct passwd
*pwd
)
614 if (putpwent (pwd
, stdout
) != 0)
615 fprintf (stderr
, "error writing passwd entry: %m\n");
619 passwd_keys (int number
, char *key
[])
628 while ((pwd
= getpwent ()) != NULL
)
634 for (i
= 0; i
< number
; ++i
)
638 uid_t arg_uid
= strtoul(key
[i
], &ep
, 10);
640 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
641 /* Valid numeric uid. */
642 pwd
= getpwuid (arg_uid
);
644 pwd
= getpwnam (key
[i
]);
655 /* This is for protocols */
657 print_protocols (struct protoent
*proto
)
661 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
664 while (proto
->p_aliases
[i
] != NULL
)
666 putchar_unlocked (' ');
667 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
670 putchar_unlocked ('\n');
674 protocols_keys (int number
, char *key
[])
678 struct protoent
*proto
;
683 while ((proto
= getprotoent ()) != NULL
)
684 print_protocols (proto
);
689 for (i
= 0; i
< number
; ++i
)
691 if (isdigit (key
[i
][0]))
692 proto
= getprotobynumber (atol (key
[i
]));
694 proto
= getprotobyname (key
[i
]);
699 print_protocols (proto
);
706 /* Now is all for rpc */
708 print_rpc (struct rpcent
*rpc
)
712 printf ("%-15s %d%s",
713 rpc
->r_name
, rpc
->r_number
, rpc
->r_aliases
[0] ? " " : "");
715 for (i
= 0; rpc
->r_aliases
[i
]; ++i
)
716 printf (" %s", rpc
->r_aliases
[i
]);
717 putchar_unlocked ('\n');
721 rpc_keys (int number
, char *key
[])
730 while ((rpc
= getrpcent ()) != NULL
)
736 for (i
= 0; i
< number
; ++i
)
738 if (isdigit (key
[i
][0]))
739 rpc
= getrpcbynumber (atol (key
[i
]));
741 rpc
= getrpcbyname (key
[i
]);
755 print_services (struct servent
*serv
)
759 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
762 while (serv
->s_aliases
[i
] != NULL
)
764 putchar_unlocked (' ');
765 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
768 putchar_unlocked ('\n');
772 services_keys (int number
, char *key
[])
776 struct servent
*serv
;
781 while ((serv
= getservent ()) != NULL
)
782 print_services (serv
);
787 for (i
= 0; i
< number
; ++i
)
789 struct servent
*serv
;
790 char *proto
= strchr (key
[i
], '/');
796 long port
= strtol (key
[i
], &endptr
, 10);
798 if (isdigit (key
[i
][0]) && *endptr
== '\0'
799 && 0 <= port
&& port
<= 65535)
800 serv
= getservbyport (htons (port
), proto
);
802 serv
= getservbyname (key
[i
], proto
);
807 print_services (serv
);
813 /* This is for shadow */
815 print_shadow (struct spwd
*sp
)
817 if (putspent (sp
, stdout
) != 0)
818 fprintf (stderr
, "error writing shadow entry: %m\n");
822 shadow_keys (int number
, char *key
[])
832 while ((sp
= getspent ()) != NULL
)
838 for (i
= 0; i
< number
; ++i
)
842 sp
= getspnam (key
[i
]);
856 int (*func
) (int number
, char *key
[]);
859 #define D(name) { #name, name ## _keys },
882 /* Handle arguments found by argp. */
884 parse_option (int key
, char *arg
, struct argp_state
*state
)
890 endp
= strchr (arg
, ':');
892 /* No specific database, change them all. */
893 for (int i
= 0; databases
[i
].name
!= NULL
; ++i
)
894 __nss_configure_lookup (databases
[i
].name
, arg
);
898 for (i
= 0; databases
[i
].name
!= NULL
; ++i
)
899 if (strncmp (databases
[i
].name
, arg
, endp
- arg
) == 0)
901 __nss_configure_lookup (databases
[i
].name
, endp
+ 1);
904 if (databases
[i
].name
== NULL
)
905 error (EXIT_FAILURE
, 0, gettext ("Unknown database name"));
914 addrconfig_flags
= 0;
918 return ARGP_ERR_UNKNOWN
;
926 more_help (int key
, const char *text
, void *input
)
934 case ARGP_KEY_HELP_EXTRA
:
935 /* We print some extra information. */
936 fp
= open_memstream (&doc
, &len
);
939 fputs_unlocked (_("Supported databases:\n"), fp
);
941 for (int i
= 0, col
= 0; databases
[i
].name
!= NULL
; ++i
)
943 len
= strlen (databases
[i
].name
);
949 fputc_unlocked ('\n', fp
);
952 fputc_unlocked (' ', fp
);
955 fputs_unlocked (databases
[i
].name
, fp
);
961 fprintf (fp
, gettext ("\
962 For bug reporting instructions, please see:\n\
963 %s.\n"), REPORT_BUGS_TO
);
965 if (fclose (fp
) == 0)
973 return (char *) text
;
977 /* the main function */
979 main (int argc
, char *argv
[])
981 /* Debugging support. */
984 /* Set locale via LC_ALL. */
985 setlocale (LC_ALL
, "");
986 /* Set the text message domain. */
987 textdomain (PACKAGE
);
989 /* Parse and process arguments. */
991 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
993 if ((argc
- remaining
) < 1)
995 error (0, 0, gettext ("wrong number of arguments"));
996 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
1000 for (int i
= 0; databases
[i
].name
; ++i
)
1001 if (argv
[remaining
][0] == databases
[i
].name
[0]
1002 && !strcmp (argv
[remaining
], databases
[i
].name
))
1003 return databases
[i
].func (argc
- remaining
- 1, &argv
[remaining
+ 1]);
1005 fprintf (stderr
, _("Unknown database: %s\n"), argv
[remaining
]);
1006 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);