1 /* Copyright (c) 1998-2021 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 { NULL
, 0, NULL
, 0, NULL
},
64 /* Short description of program. */
65 static const char doc
[] = N_("Get entries from administrative database.");
67 /* Prototype for option handler. */
68 static error_t
parse_option (int key
, char *arg
, struct argp_state
*state
);
70 /* Function to print some extra text in the help message. */
71 static char *more_help (int key
, const char *text
, void *input
);
73 /* Data structure to communicate with argp functions. */
74 static struct argp argp
=
76 args_options
, parse_option
, args_doc
, doc
, NULL
, more_help
79 /* Additional getaddrinfo flags for IDN encoding. */
80 static int idn_flags
= AI_IDN
| AI_CANONIDN
;
82 /* Print the version information. */
84 print_version (FILE *stream
, struct argp_state
*state
)
86 fprintf (stream
, "getent %s%s\n", PKGVERSION
, VERSION
);
87 fprintf (stream
, gettext ("\
88 Copyright (C) %s Free Software Foundation, Inc.\n\
89 This is free software; see the source for copying conditions. There is NO\n\
90 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
92 fprintf (stream
, gettext ("Written by %s.\n"), "Thorsten Kukuk");
95 /* This is for aliases */
97 print_aliases (struct aliasent
*alias
)
101 printf ("%s: ", alias
->alias_name
);
102 for (i
= strlen (alias
->alias_name
); i
< 14; ++i
)
103 fputs_unlocked (" ", stdout
);
105 for (i
= 0; i
< alias
->alias_members_len
; ++i
)
107 alias
->alias_members
[i
],
108 i
+ 1 == alias
->alias_members_len
? "\n" : ", ");
112 aliases_keys (int number
, char *key
[])
116 struct aliasent
*alias
;
121 while ((alias
= getaliasent ()) != NULL
)
122 print_aliases (alias
);
127 for (i
= 0; i
< number
; ++i
)
129 alias
= getaliasbyname (key
[i
]);
134 print_aliases (alias
);
140 /* This is for ethers */
142 ethers_keys (int number
, char *key
[])
149 fprintf (stderr
, _("Enumeration not supported on %s\n"), "ethers");
153 for (i
= 0; i
< number
; ++i
)
155 struct ether_addr
*ethp
, eth
;
156 char buffer
[1024], *p
;
158 ethp
= ether_aton (key
[i
]);
161 if (ether_ntohost (buffer
, ethp
))
170 if (ether_hostton (key
[i
], ð
))
178 printf ("%s %s\n", ether_ntoa (ethp
), p
);
184 /* This is for group */
186 print_group (struct group
*grp
)
188 if (putgrent (grp
, stdout
) != 0)
189 fprintf (stderr
, "error writing group entry: %m\n");
193 group_keys (int number
, char *key
[])
202 while ((grp
= getgrent ()) != NULL
)
208 for (i
= 0; i
< number
; ++i
)
212 gid_t arg_gid
= strtoul(key
[i
], &ep
, 10);
214 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
215 /* Valid numeric gid. */
216 grp
= getgrgid (arg_gid
);
218 grp
= getgrnam (key
[i
]);
229 /* This is for gshadow */
231 print_gshadow (struct sgrp
*sg
)
233 if (putsgent (sg
, stdout
) != 0)
234 fprintf (stderr
, "error writing gshadow entry: %m\n");
238 gshadow_keys (int number
, char *key
[])
248 while ((sg
= getsgent ()) != NULL
)
254 for (i
= 0; i
< number
; ++i
)
258 sg
= getsgnam (key
[i
]);
269 /* This is for hosts */
271 print_hosts (struct hostent
*host
)
275 for (cnt
= 0; host
->h_addr_list
[cnt
] != NULL
; ++cnt
)
277 char buf
[INET6_ADDRSTRLEN
];
278 const char *ip
= inet_ntop (host
->h_addrtype
, host
->h_addr_list
[cnt
],
281 printf ("%-15s %s", ip
, host
->h_name
);
284 for (i
= 0; host
->h_aliases
[i
] != NULL
; ++i
)
286 putchar_unlocked (' ');
287 fputs_unlocked (host
->h_aliases
[i
], stdout
);
289 putchar_unlocked ('\n');
294 hosts_keys (int number
, char *key
[])
298 struct hostent
*host
;
303 while ((host
= gethostent ()) != NULL
)
309 for (i
= 0; i
< number
; ++i
)
311 struct hostent
*host
= NULL
;
312 char addr
[IN6ADDRSZ
];
314 if (inet_pton (AF_INET6
, key
[i
], &addr
) > 0)
315 host
= gethostbyaddr (addr
, IN6ADDRSZ
, AF_INET6
);
316 else if (inet_pton (AF_INET
, key
[i
], &addr
) > 0)
317 host
= gethostbyaddr (addr
, INADDRSZ
, AF_INET
);
318 else if ((host
= gethostbyname2 (key
[i
], AF_INET6
)) == NULL
)
319 host
= gethostbyname2 (key
[i
], AF_INET
);
330 /* This is for hosts, but using getaddrinfo */
332 ahosts_keys_int (int af
, int xflags
, int number
, char *key
[])
336 struct hostent
*host
;
341 while ((host
= gethostent ()) != NULL
)
347 struct addrinfo hint
;
348 memset (&hint
, '\0', sizeof (hint
));
349 hint
.ai_flags
= (AI_V4MAPPED
| AI_ADDRCONFIG
| AI_CANONNAME
350 | idn_flags
| xflags
);
353 for (i
= 0; i
< number
; ++i
)
355 struct addrinfo
*res
;
357 if (getaddrinfo (key
[i
], NULL
, &hint
, &res
) != 0)
361 struct addrinfo
*runp
= res
;
367 if (runp
->ai_socktype
== SOCK_STREAM
)
369 else if (runp
->ai_socktype
== SOCK_DGRAM
)
371 else if (runp
->ai_socktype
== SOCK_RAW
)
373 #ifdef SOCK_SEQPACKET
374 else if (runp
->ai_socktype
== SOCK_SEQPACKET
)
375 sockstr
= "SEQPACKET";
378 else if (runp
->ai_socktype
== SOCK_RDM
)
382 else if (runp
->ai_socktype
== SOCK_DCCP
)
386 else if (runp
->ai_socktype
== SOCK_PACKET
)
391 snprintf (sockbuf
, sizeof (sockbuf
), "%d",
396 /* Three digits per byte, plus '%' and null terminator. */
397 char scope
[3 * sizeof (uint32_t) + 2];
398 struct sockaddr_in6
*addr6
399 = (struct sockaddr_in6
*) runp
->ai_addr
;
400 if (runp
->ai_family
!= AF_INET6
|| addr6
->sin6_scope_id
== 0)
401 /* No scope ID present. */
404 snprintf (scope
, sizeof (scope
), "%%%" PRIu32
,
405 addr6
->sin6_scope_id
);
407 char buf
[INET6_ADDRSTRLEN
];
408 if (inet_ntop (runp
->ai_family
,
409 runp
->ai_family
== AF_INET
410 ? (void *) &((struct sockaddr_in
*) runp
->ai_addr
)->sin_addr
412 buf
, sizeof (buf
)) == NULL
)
414 strcpy (buf
, "<invalid>");
418 int pad
= 15 - strlen (buf
) - strlen (scope
);
422 printf ("%s%-*s %-6s %s\n",
423 buf
, pad
, scope
, sockstr
, runp
->ai_canonname
?: "");
425 runp
= runp
->ai_next
;
436 ahosts_keys (int number
, char *key
[])
438 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
442 ahostsv4_keys (int number
, char *key
[])
444 return ahosts_keys_int (AF_INET
, 0, number
, key
);
448 ahostsv6_keys (int number
, char *key
[])
450 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
453 /* This is for netgroup */
455 netgroup_keys (int number
, char *key
[])
461 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
467 char *host
= strcmp (key
[1], "*") == 0 ? NULL
: key
[1];
468 char *user
= strcmp (key
[2], "*") == 0 ? NULL
: key
[2];
469 char *domain
= strcmp (key
[3], "*") == 0 ? NULL
: key
[3];
471 printf ("%-21s (%s,%s,%s) = %d\n",
472 key
[0], host
?: "", user
?: "", domain
?: "",
473 innetgr (key
[0], host
, user
, domain
));
475 else if (number
== 1)
477 if (!setnetgrent (key
[0]))
483 printf ("%-21s", key
[0]);
485 while (getnetgrent (p
, p
+ 1, p
+ 2))
486 printf (" (%s,%s,%s)", p
[0] ?: " ", p
[1] ?: "", p
[2] ?: "");
487 putchar_unlocked ('\n');
496 #define DYNARRAY_STRUCT gid_list
497 #define DYNARRAY_ELEMENT gid_t
498 #define DYNARRAY_PREFIX gid_list_
499 #define DYNARRAY_INITIAL_SIZE 10
500 #include <malloc/dynarray-skeleton.c>
502 /* This is for initgroups */
504 initgroups_keys (int number
, char *key
[])
508 fprintf (stderr
, _("Enumeration not supported on %s\n"), "initgroups");
512 struct gid_list list
;
513 gid_list_init (&list
);
514 if (!gid_list_resize (&list
, 10))
516 fprintf (stderr
, _("Could not allocate group list: %m\n"));
520 for (int i
= 0; i
< number
; ++i
)
522 int no
= gid_list_size (&list
);
524 while ((n
= getgrouplist (key
[i
], -1, gid_list_begin (&list
), &no
)) == -1
525 && no
> gid_list_size (&list
))
527 if (!gid_list_resize (&list
, no
))
529 fprintf (stderr
, _("Could not allocate group list: %m\n"));
536 gid_list_free (&list
);
540 const gid_t
*grps
= gid_list_begin (&list
);
541 printf ("%-21s", key
[i
]);
542 for (int j
= 0; j
< n
; ++j
)
544 printf (" %ld", (long int) grps
[j
]);
545 putchar_unlocked ('\n');
548 gid_list_free (&list
);
553 /* This is for networks */
555 print_networks (struct netent
*net
)
559 ip
.s_addr
= htonl (net
->n_net
);
561 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
564 while (net
->n_aliases
[i
] != NULL
)
566 putchar_unlocked (' ');
567 fputs_unlocked (net
->n_aliases
[i
], stdout
);
570 putchar_unlocked ('\n');
574 networks_keys (int number
, char *key
[])
583 while ((net
= getnetent ()) != NULL
)
584 print_networks (net
);
589 for (i
= 0; i
< number
; ++i
)
591 if (isdigit (key
[i
][0]))
592 net
= getnetbyaddr (ntohl (inet_addr (key
[i
])), AF_UNSPEC
);
594 net
= getnetbyname (key
[i
]);
599 print_networks (net
);
605 /* Now is all for passwd */
607 print_passwd (struct passwd
*pwd
)
609 if (putpwent (pwd
, stdout
) != 0)
610 fprintf (stderr
, "error writing passwd entry: %m\n");
614 passwd_keys (int number
, char *key
[])
623 while ((pwd
= getpwent ()) != NULL
)
629 for (i
= 0; i
< number
; ++i
)
633 uid_t arg_uid
= strtoul(key
[i
], &ep
, 10);
635 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
636 /* Valid numeric uid. */
637 pwd
= getpwuid (arg_uid
);
639 pwd
= getpwnam (key
[i
]);
650 /* This is for protocols */
652 print_protocols (struct protoent
*proto
)
656 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
659 while (proto
->p_aliases
[i
] != NULL
)
661 putchar_unlocked (' ');
662 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
665 putchar_unlocked ('\n');
669 protocols_keys (int number
, char *key
[])
673 struct protoent
*proto
;
678 while ((proto
= getprotoent ()) != NULL
)
679 print_protocols (proto
);
684 for (i
= 0; i
< number
; ++i
)
686 if (isdigit (key
[i
][0]))
687 proto
= getprotobynumber (atol (key
[i
]));
689 proto
= getprotobyname (key
[i
]);
694 print_protocols (proto
);
701 /* Now is all for rpc */
703 print_rpc (struct rpcent
*rpc
)
707 printf ("%-15s %d%s",
708 rpc
->r_name
, rpc
->r_number
, rpc
->r_aliases
[0] ? " " : "");
710 for (i
= 0; rpc
->r_aliases
[i
]; ++i
)
711 printf (" %s", rpc
->r_aliases
[i
]);
712 putchar_unlocked ('\n');
716 rpc_keys (int number
, char *key
[])
725 while ((rpc
= getrpcent ()) != NULL
)
731 for (i
= 0; i
< number
; ++i
)
733 if (isdigit (key
[i
][0]))
734 rpc
= getrpcbynumber (atol (key
[i
]));
736 rpc
= getrpcbyname (key
[i
]);
750 print_services (struct servent
*serv
)
754 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
757 while (serv
->s_aliases
[i
] != NULL
)
759 putchar_unlocked (' ');
760 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
763 putchar_unlocked ('\n');
767 services_keys (int number
, char *key
[])
771 struct servent
*serv
;
776 while ((serv
= getservent ()) != NULL
)
777 print_services (serv
);
782 for (i
= 0; i
< number
; ++i
)
784 struct servent
*serv
;
785 char *proto
= strchr (key
[i
], '/');
791 long port
= strtol (key
[i
], &endptr
, 10);
793 if (isdigit (key
[i
][0]) && *endptr
== '\0'
794 && 0 <= port
&& port
<= 65535)
795 serv
= getservbyport (htons (port
), proto
);
797 serv
= getservbyname (key
[i
], proto
);
802 print_services (serv
);
808 /* This is for shadow */
810 print_shadow (struct spwd
*sp
)
812 if (putspent (sp
, stdout
) != 0)
813 fprintf (stderr
, "error writing shadow entry: %m\n");
817 shadow_keys (int number
, char *key
[])
827 while ((sp
= getspent ()) != NULL
)
833 for (i
= 0; i
< number
; ++i
)
837 sp
= getspnam (key
[i
]);
851 int (*func
) (int number
, char *key
[]);
854 #define D(name) { #name, name ## _keys },
877 /* Handle arguments found by argp. */
879 parse_option (int key
, char *arg
, struct argp_state
*state
)
885 endp
= strchr (arg
, ':');
887 /* No specific database, change them all. */
888 for (int i
= 0; databases
[i
].name
!= NULL
; ++i
)
889 __nss_configure_lookup (databases
[i
].name
, arg
);
893 for (i
= 0; databases
[i
].name
!= NULL
; ++i
)
894 if (strncmp (databases
[i
].name
, arg
, endp
- arg
) == 0)
896 __nss_configure_lookup (databases
[i
].name
, endp
+ 1);
899 if (databases
[i
].name
== NULL
)
900 error (EXIT_FAILURE
, 0, gettext ("Unknown database name"));
909 return ARGP_ERR_UNKNOWN
;
917 more_help (int key
, const char *text
, void *input
)
925 case ARGP_KEY_HELP_EXTRA
:
926 /* We print some extra information. */
927 fp
= open_memstream (&doc
, &len
);
930 fputs_unlocked (_("Supported databases:\n"), fp
);
932 for (int i
= 0, col
= 0; databases
[i
].name
!= NULL
; ++i
)
934 len
= strlen (databases
[i
].name
);
940 fputc_unlocked ('\n', fp
);
943 fputc_unlocked (' ', fp
);
946 fputs_unlocked (databases
[i
].name
, fp
);
952 fprintf (fp
, gettext ("\
953 For bug reporting instructions, please see:\n\
954 %s.\n"), REPORT_BUGS_TO
);
956 if (fclose (fp
) == 0)
964 return (char *) text
;
968 /* the main function */
970 main (int argc
, char *argv
[])
972 /* Debugging support. */
975 /* Set locale via LC_ALL. */
976 setlocale (LC_ALL
, "");
977 /* Set the text message domain. */
978 textdomain (PACKAGE
);
980 /* Parse and process arguments. */
982 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
984 if ((argc
- remaining
) < 1)
986 error (0, 0, gettext ("wrong number of arguments"));
987 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
991 for (int i
= 0; databases
[i
].name
; ++i
)
992 if (argv
[remaining
][0] == databases
[i
].name
[0]
993 && !strcmp (argv
[remaining
], databases
[i
].name
))
994 return databases
[i
].func (argc
- remaining
- 1, &argv
[remaining
+ 1]);
996 fprintf (stderr
, _("Unknown database: %s\n"), argv
[remaining
]);
997 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);