Define ISO 639-3 "tok" [BZ #28950]
[glibc.git] / nss / getent.c
blob8178b4b470063f27dc1975c7f186c3fbdb8b9c10
1 /* Copyright (c) 1998-2022 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. */
20 #include <aliases.h>
21 #include <argp.h>
22 #include <ctype.h>
23 #include <error.h>
24 #include <grp.h>
25 #include <gshadow.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <mcheck.h>
29 #include <netdb.h>
30 #include <pwd.h>
31 #include <shadow.h>
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
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>
42 #include <inttypes.h>
44 /* Get libc version number. */
45 #include <version.h>
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. */
83 static void
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\
91 "), "2022");
92 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
95 /* This is for aliases */
96 static void
97 print_aliases (struct aliasent *alias)
99 unsigned int i = 0;
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)
106 printf ("%s%s",
107 alias->alias_members [i],
108 i + 1 == alias->alias_members_len ? "\n" : ", ");
111 static int
112 aliases_keys (int number, char *key[])
114 int result = 0;
115 int i;
116 struct aliasent *alias;
118 if (number == 0)
120 setaliasent ();
121 while ((alias = getaliasent ()) != NULL)
122 print_aliases (alias);
123 endaliasent ();
124 return result;
127 for (i = 0; i < number; ++i)
129 alias = getaliasbyname (key[i]);
131 if (alias == NULL)
132 result = 2;
133 else
134 print_aliases (alias);
137 return result;
140 /* This is for ethers */
141 static int
142 ethers_keys (int number, char *key[])
144 int result = 0;
145 int i;
147 if (number == 0)
149 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
150 return 3;
153 for (i = 0; i < number; ++i)
155 struct ether_addr *ethp, eth;
156 char buffer [1024], *p;
158 ethp = ether_aton (key[i]);
159 if (ethp != NULL)
161 if (ether_ntohost (buffer, ethp))
163 result = 2;
164 continue;
166 p = buffer;
168 else
170 if (ether_hostton (key[i], &eth))
172 result = 2;
173 continue;
175 p = key[i];
176 ethp = &eth;
178 printf ("%s %s\n", ether_ntoa (ethp), p);
181 return result;
184 /* This is for group */
185 static void
186 print_group (struct group *grp)
188 if (putgrent (grp, stdout) != 0)
189 fprintf (stderr, "error writing group entry: %m\n");
192 static int
193 group_keys (int number, char *key[])
195 int result = 0;
196 int i;
197 struct group *grp;
199 if (number == 0)
201 setgrent ();
202 while ((grp = getgrent ()) != NULL)
203 print_group (grp);
204 endgrent ();
205 return result;
208 for (i = 0; i < number; ++i)
210 errno = 0;
211 char *ep;
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);
217 else
218 grp = getgrnam (key[i]);
220 if (grp == NULL)
221 result = 2;
222 else
223 print_group (grp);
226 return result;
229 /* This is for gshadow */
230 static void
231 print_gshadow (struct sgrp *sg)
233 if (putsgent (sg, stdout) != 0)
234 fprintf (stderr, "error writing gshadow entry: %m\n");
237 static int
238 gshadow_keys (int number, char *key[])
240 int result = 0;
241 int i;
243 if (number == 0)
245 struct sgrp *sg;
247 setsgent ();
248 while ((sg = getsgent ()) != NULL)
249 print_gshadow (sg);
250 endsgent ();
251 return result;
254 for (i = 0; i < number; ++i)
256 struct sgrp *sg;
258 sg = getsgnam (key[i]);
260 if (sg == NULL)
261 result = 2;
262 else
263 print_gshadow (sg);
266 return result;
269 /* This is for hosts */
270 static void
271 print_hosts (struct hostent *host)
273 unsigned int cnt;
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],
279 buf, sizeof (buf));
281 printf ("%-15s %s", ip, host->h_name);
283 unsigned int i;
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');
293 static int
294 hosts_keys (int number, char *key[])
296 int result = 0;
297 int i;
298 struct hostent *host;
300 if (number == 0)
302 sethostent (0);
303 while ((host = gethostent ()) != NULL)
304 print_hosts (host);
305 endhostent ();
306 return result;
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);
321 if (host == NULL)
322 result = 2;
323 else
324 print_hosts (host);
327 return result;
330 /* This is for hosts, but using getaddrinfo */
331 static int
332 ahosts_keys_int (int af, int xflags, int number, char *key[])
334 int result = 0;
335 int i;
336 struct hostent *host;
338 if (number == 0)
340 sethostent (0);
341 while ((host = gethostent ()) != NULL)
342 print_hosts (host);
343 endhostent ();
344 return result;
347 struct addrinfo hint;
348 memset (&hint, '\0', sizeof (hint));
349 hint.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME
350 | idn_flags | xflags);
351 hint.ai_family = af;
353 for (i = 0; i < number; ++i)
355 struct addrinfo *res;
357 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
358 result = 2;
359 else
361 struct addrinfo *runp = res;
363 while (runp != NULL)
365 char sockbuf[20];
366 const char *sockstr;
367 if (runp->ai_socktype == SOCK_STREAM)
368 sockstr = "STREAM";
369 else if (runp->ai_socktype == SOCK_DGRAM)
370 sockstr = "DGRAM";
371 else if (runp->ai_socktype == SOCK_RAW)
372 sockstr = "RAW";
373 #ifdef SOCK_SEQPACKET
374 else if (runp->ai_socktype == SOCK_SEQPACKET)
375 sockstr = "SEQPACKET";
376 #endif
377 #ifdef SOCK_RDM
378 else if (runp->ai_socktype == SOCK_RDM)
379 sockstr = "RDM";
380 #endif
381 #ifdef SOCK_DCCP
382 else if (runp->ai_socktype == SOCK_DCCP)
383 sockstr = "DCCP";
384 #endif
385 #ifdef SOCK_PACKET
386 else if (runp->ai_socktype == SOCK_PACKET)
387 sockstr = "PACKET";
388 #endif
389 else
391 snprintf (sockbuf, sizeof (sockbuf), "%d",
392 runp->ai_socktype);
393 sockstr = sockbuf;
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. */
402 scope[0] = '\0';
403 else
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
411 : &addr6->sin6_addr,
412 buf, sizeof (buf)) == NULL)
414 strcpy (buf, "<invalid>");
415 scope[0] = '\0';
418 int pad = 15 - strlen (buf) - strlen (scope);
419 if (pad < 0)
420 pad = 0;
422 printf ("%s%-*s %-6s %s\n",
423 buf, pad, scope, sockstr, runp->ai_canonname ?: "");
425 runp = runp->ai_next;
428 freeaddrinfo (res);
432 return result;
435 static int
436 ahosts_keys (int number, char *key[])
438 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
441 static int
442 ahostsv4_keys (int number, char *key[])
444 return ahosts_keys_int (AF_INET, 0, number, key);
447 static int
448 ahostsv6_keys (int number, char *key[])
450 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
453 /* This is for netgroup */
454 static int
455 netgroup_keys (int number, char *key[])
457 int result = 0;
459 if (number == 0)
461 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
462 return 3;
465 if (number == 4)
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]))
478 result = 2;
479 else
481 char *p[3];
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');
491 endnetgrent ();
493 return result;
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 */
503 static int
504 initgroups_keys (int number, char *key[])
506 if (number == 0)
508 fprintf (stderr, _("Enumeration not supported on %s\n"), "initgroups");
509 return 3;
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"));
517 return 3;
520 for (int i = 0; i < number; ++i)
522 int no = gid_list_size (&list);
523 int n;
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"));
530 return 3;
534 if (n == -1)
536 gid_list_free (&list);
537 return 1;
540 const gid_t *grps = gid_list_begin (&list);
541 printf ("%-21s", key[i]);
542 for (int j = 0; j < n; ++j)
543 if (grps[j] != -1)
544 printf (" %ld", (long int) grps[j]);
545 putchar_unlocked ('\n');
548 gid_list_free (&list);
550 return 0;
553 /* This is for networks */
554 static void
555 print_networks (struct netent *net)
557 unsigned int i;
558 struct in_addr ip;
559 ip.s_addr = htonl (net->n_net);
561 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
563 i = 0;
564 while (net->n_aliases[i] != NULL)
566 putchar_unlocked (' ');
567 fputs_unlocked (net->n_aliases[i], stdout);
568 ++i;
570 putchar_unlocked ('\n');
573 static int
574 networks_keys (int number, char *key[])
576 int result = 0;
577 int i;
578 struct netent *net;
580 if (number == 0)
582 setnetent (0);
583 while ((net = getnetent ()) != NULL)
584 print_networks (net);
585 endnetent ();
586 return result;
589 for (i = 0; i < number; ++i)
591 if (isdigit (key[i][0]))
592 net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC);
593 else
594 net = getnetbyname (key[i]);
596 if (net == NULL)
597 result = 2;
598 else
599 print_networks (net);
602 return result;
605 /* Now is all for passwd */
606 static void
607 print_passwd (struct passwd *pwd)
609 if (putpwent (pwd, stdout) != 0)
610 fprintf (stderr, "error writing passwd entry: %m\n");
613 static int
614 passwd_keys (int number, char *key[])
616 int result = 0;
617 int i;
618 struct passwd *pwd;
620 if (number == 0)
622 setpwent ();
623 while ((pwd = getpwent ()) != NULL)
624 print_passwd (pwd);
625 endpwent ();
626 return result;
629 for (i = 0; i < number; ++i)
631 errno = 0;
632 char *ep;
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);
638 else
639 pwd = getpwnam (key[i]);
641 if (pwd == NULL)
642 result = 2;
643 else
644 print_passwd (pwd);
647 return result;
650 /* This is for protocols */
651 static void
652 print_protocols (struct protoent *proto)
654 unsigned int i;
656 printf ("%-21s %d", proto->p_name, proto->p_proto);
658 i = 0;
659 while (proto->p_aliases[i] != NULL)
661 putchar_unlocked (' ');
662 fputs_unlocked (proto->p_aliases[i], stdout);
663 ++i;
665 putchar_unlocked ('\n');
668 static int
669 protocols_keys (int number, char *key[])
671 int result = 0;
672 int i;
673 struct protoent *proto;
675 if (number == 0)
677 setprotoent (0);
678 while ((proto = getprotoent ()) != NULL)
679 print_protocols (proto);
680 endprotoent ();
681 return result;
684 for (i = 0; i < number; ++i)
686 if (isdigit (key[i][0]))
687 proto = getprotobynumber (atol (key[i]));
688 else
689 proto = getprotobyname (key[i]);
691 if (proto == NULL)
692 result = 2;
693 else
694 print_protocols (proto);
697 return result;
700 #if HAVE_SUNRPC
701 /* Now is all for rpc */
702 static void
703 print_rpc (struct rpcent *rpc)
705 int i;
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');
715 static int
716 rpc_keys (int number, char *key[])
718 int result = 0;
719 int i;
720 struct rpcent *rpc;
722 if (number == 0)
724 setrpcent (0);
725 while ((rpc = getrpcent ()) != NULL)
726 print_rpc (rpc);
727 endrpcent ();
728 return result;
731 for (i = 0; i < number; ++i)
733 if (isdigit (key[i][0]))
734 rpc = getrpcbynumber (atol (key[i]));
735 else
736 rpc = getrpcbyname (key[i]);
738 if (rpc == NULL)
739 result = 2;
740 else
741 print_rpc (rpc);
744 return result;
746 #endif
748 /* for services */
749 static void
750 print_services (struct servent *serv)
752 unsigned int i;
754 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
756 i = 0;
757 while (serv->s_aliases[i] != NULL)
759 putchar_unlocked (' ');
760 fputs_unlocked (serv->s_aliases[i], stdout);
761 ++i;
763 putchar_unlocked ('\n');
766 static int
767 services_keys (int number, char *key[])
769 int result = 0;
770 int i;
771 struct servent *serv;
773 if (!number)
775 setservent (0);
776 while ((serv = getservent ()) != NULL)
777 print_services (serv);
778 endservent ();
779 return result;
782 for (i = 0; i < number; ++i)
784 struct servent *serv;
785 char *proto = strchr (key[i], '/');
787 if (proto != NULL)
788 *proto++ = '\0';
790 char *endptr;
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);
796 else
797 serv = getservbyname (key[i], proto);
799 if (serv == NULL)
800 result = 2;
801 else
802 print_services (serv);
805 return result;
808 /* This is for shadow */
809 static void
810 print_shadow (struct spwd *sp)
812 if (putspent (sp, stdout) != 0)
813 fprintf (stderr, "error writing shadow entry: %m\n");
816 static int
817 shadow_keys (int number, char *key[])
819 int result = 0;
820 int i;
822 if (number == 0)
824 struct spwd *sp;
826 setspent ();
827 while ((sp = getspent ()) != NULL)
828 print_shadow (sp);
829 endspent ();
830 return result;
833 for (i = 0; i < number; ++i)
835 struct spwd *sp;
837 sp = getspnam (key[i]);
839 if (sp == NULL)
840 result = 2;
841 else
842 print_shadow (sp);
845 return result;
848 struct
850 const char *name;
851 int (*func) (int number, char *key[]);
852 } databases[] =
854 #define D(name) { #name, name ## _keys },
855 D(ahosts)
856 D(ahostsv4)
857 D(ahostsv6)
858 D(aliases)
859 D(ethers)
860 D(group)
861 D(gshadow)
862 D(hosts)
863 D(initgroups)
864 D(netgroup)
865 D(networks)
866 D(passwd)
867 D(protocols)
868 #if HAVE_SUNRPC
869 D(rpc)
870 #endif
871 D(services)
872 D(shadow)
873 #undef D
874 { NULL, NULL }
877 /* Handle arguments found by argp. */
878 static error_t
879 parse_option (int key, char *arg, struct argp_state *state)
881 char *endp;
882 switch (key)
884 case 's':
885 endp = strchr (arg, ':');
886 if (endp == NULL)
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);
890 else
892 int i;
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);
897 break;
899 if (databases[i].name == NULL)
900 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
902 break;
904 case 'i':
905 idn_flags = 0;
906 break;
908 default:
909 return ARGP_ERR_UNKNOWN;
912 return 0;
916 static char *
917 more_help (int key, const char *text, void *input)
919 switch (key)
921 size_t len;
922 char *doc;
923 FILE *fp;
925 case ARGP_KEY_HELP_EXTRA:
926 /* We print some extra information. */
927 fp = open_memstream (&doc, &len);
928 if (fp != NULL)
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);
935 if (i != 0)
937 if (col + len > 72)
939 col = 0;
940 fputc_unlocked ('\n', fp);
942 else
943 fputc_unlocked (' ', fp);
946 fputs_unlocked (databases[i].name, fp);
947 col += len + 1;
950 fputs ("\n\n", fp);
952 fprintf (fp, gettext ("\
953 For bug reporting instructions, please see:\n\
954 %s.\n"), REPORT_BUGS_TO);
956 if (fclose (fp) == 0)
957 return doc;
959 break;
961 default:
962 break;
964 return (char *) text;
968 /* the main function */
970 main (int argc, char *argv[])
972 /* Debugging support. */
973 mtrace ();
975 /* Set locale via LC_ALL. */
976 setlocale (LC_ALL, "");
977 /* Set the text message domain. */
978 textdomain (PACKAGE);
980 /* Parse and process arguments. */
981 int remaining;
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);
988 return 1;
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);
998 return 1;