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>
42 #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 char buf
[INET6_ADDRSTRLEN
];
397 printf ("%-15s %-6s %s\n",
398 inet_ntop (runp
->ai_family
,
399 runp
->ai_family
== AF_INET
400 ? (void *) &((struct sockaddr_in
*) runp
->ai_addr
)->sin_addr
401 : (void *) &((struct sockaddr_in6
*) runp
->ai_addr
)->sin6_addr
,
404 runp
->ai_canonname
?: "");
406 runp
= runp
->ai_next
;
417 ahosts_keys (int number
, char *key
[])
419 return ahosts_keys_int (AF_UNSPEC
, 0, number
, key
);
423 ahostsv4_keys (int number
, char *key
[])
425 return ahosts_keys_int (AF_INET
, 0, number
, key
);
429 ahostsv6_keys (int number
, char *key
[])
431 return ahosts_keys_int (AF_INET6
, AI_V4MAPPED
, number
, key
);
434 /* This is for netgroup */
436 netgroup_keys (int number
, char *key
[])
442 fprintf (stderr
, _("Enumeration not supported on %s\n"), "netgroup");
448 char *host
= strcmp (key
[1], "*") == 0 ? NULL
: key
[1];
449 char *user
= strcmp (key
[2], "*") == 0 ? NULL
: key
[2];
450 char *domain
= strcmp (key
[3], "*") == 0 ? NULL
: key
[3];
452 printf ("%-21s (%s,%s,%s) = %d\n",
453 key
[0], host
?: "", user
?: "", domain
?: "",
454 innetgr (key
[0], host
, user
, domain
));
456 else if (number
== 1)
458 if (!setnetgrent (key
[0]))
464 printf ("%-21s", key
[0]);
466 while (getnetgrent (p
, p
+ 1, p
+ 2))
467 printf (" (%s,%s,%s)", p
[0] ?: " ", p
[1] ?: "", p
[2] ?: "");
468 putchar_unlocked ('\n');
477 #define DYNARRAY_STRUCT gid_list
478 #define DYNARRAY_ELEMENT gid_t
479 #define DYNARRAY_PREFIX gid_list_
480 #define DYNARRAY_INITIAL_SIZE 10
481 #include <malloc/dynarray-skeleton.c>
483 /* This is for initgroups */
485 initgroups_keys (int number
, char *key
[])
489 fprintf (stderr
, _("Enumeration not supported on %s\n"), "initgroups");
493 struct gid_list list
;
494 gid_list_init (&list
);
495 if (!gid_list_resize (&list
, 10))
497 fprintf (stderr
, _("Could not allocate group list: %m\n"));
501 for (int i
= 0; i
< number
; ++i
)
503 int no
= gid_list_size (&list
);
505 while ((n
= getgrouplist (key
[i
], -1, gid_list_begin (&list
), &no
)) == -1
506 && no
> gid_list_size (&list
))
508 if (!gid_list_resize (&list
, no
))
510 fprintf (stderr
, _("Could not allocate group list: %m\n"));
517 gid_list_free (&list
);
521 const gid_t
*grps
= gid_list_begin (&list
);
522 printf ("%-21s", key
[i
]);
523 for (int j
= 0; j
< n
; ++j
)
525 printf (" %ld", (long int) grps
[j
]);
526 putchar_unlocked ('\n');
529 gid_list_free (&list
);
534 /* This is for networks */
536 print_networks (struct netent
*net
)
540 ip
.s_addr
= htonl (net
->n_net
);
542 printf ("%-21s %s", net
->n_name
, inet_ntoa (ip
));
545 while (net
->n_aliases
[i
] != NULL
)
547 putchar_unlocked (' ');
548 fputs_unlocked (net
->n_aliases
[i
], stdout
);
551 putchar_unlocked ('\n');
555 networks_keys (int number
, char *key
[])
564 while ((net
= getnetent ()) != NULL
)
565 print_networks (net
);
570 for (i
= 0; i
< number
; ++i
)
572 if (isdigit (key
[i
][0]))
573 net
= getnetbyaddr (ntohl (inet_addr (key
[i
])), AF_UNSPEC
);
575 net
= getnetbyname (key
[i
]);
580 print_networks (net
);
586 /* Now is all for passwd */
588 print_passwd (struct passwd
*pwd
)
590 if (putpwent (pwd
, stdout
) != 0)
591 fprintf (stderr
, "error writing passwd entry: %m\n");
595 passwd_keys (int number
, char *key
[])
604 while ((pwd
= getpwent ()) != NULL
)
610 for (i
= 0; i
< number
; ++i
)
614 uid_t arg_uid
= strtoul(key
[i
], &ep
, 10);
616 if (errno
!= EINVAL
&& *key
[i
] != '\0' && *ep
== '\0')
617 /* Valid numeric uid. */
618 pwd
= getpwuid (arg_uid
);
620 pwd
= getpwnam (key
[i
]);
631 /* This is for protocols */
633 print_protocols (struct protoent
*proto
)
637 printf ("%-21s %d", proto
->p_name
, proto
->p_proto
);
640 while (proto
->p_aliases
[i
] != NULL
)
642 putchar_unlocked (' ');
643 fputs_unlocked (proto
->p_aliases
[i
], stdout
);
646 putchar_unlocked ('\n');
650 protocols_keys (int number
, char *key
[])
654 struct protoent
*proto
;
659 while ((proto
= getprotoent ()) != NULL
)
660 print_protocols (proto
);
665 for (i
= 0; i
< number
; ++i
)
667 if (isdigit (key
[i
][0]))
668 proto
= getprotobynumber (atol (key
[i
]));
670 proto
= getprotobyname (key
[i
]);
675 print_protocols (proto
);
682 /* Now is all for rpc */
684 print_rpc (struct rpcent
*rpc
)
688 printf ("%-15s %d%s",
689 rpc
->r_name
, rpc
->r_number
, rpc
->r_aliases
[0] ? " " : "");
691 for (i
= 0; rpc
->r_aliases
[i
]; ++i
)
692 printf (" %s", rpc
->r_aliases
[i
]);
693 putchar_unlocked ('\n');
697 rpc_keys (int number
, char *key
[])
706 while ((rpc
= getrpcent ()) != NULL
)
712 for (i
= 0; i
< number
; ++i
)
714 if (isdigit (key
[i
][0]))
715 rpc
= getrpcbynumber (atol (key
[i
]));
717 rpc
= getrpcbyname (key
[i
]);
731 print_services (struct servent
*serv
)
735 printf ("%-21s %d/%s", serv
->s_name
, ntohs (serv
->s_port
), serv
->s_proto
);
738 while (serv
->s_aliases
[i
] != NULL
)
740 putchar_unlocked (' ');
741 fputs_unlocked (serv
->s_aliases
[i
], stdout
);
744 putchar_unlocked ('\n');
748 services_keys (int number
, char *key
[])
752 struct servent
*serv
;
757 while ((serv
= getservent ()) != NULL
)
758 print_services (serv
);
763 for (i
= 0; i
< number
; ++i
)
765 struct servent
*serv
;
766 char *proto
= strchr (key
[i
], '/');
772 long port
= strtol (key
[i
], &endptr
, 10);
774 if (isdigit (key
[i
][0]) && *endptr
== '\0'
775 && 0 <= port
&& port
<= 65535)
776 serv
= getservbyport (htons (port
), proto
);
778 serv
= getservbyname (key
[i
], proto
);
783 print_services (serv
);
789 /* This is for shadow */
791 print_shadow (struct spwd
*sp
)
793 if (putspent (sp
, stdout
) != 0)
794 fprintf (stderr
, "error writing shadow entry: %m\n");
798 shadow_keys (int number
, char *key
[])
808 while ((sp
= getspent ()) != NULL
)
814 for (i
= 0; i
< number
; ++i
)
818 sp
= getspnam (key
[i
]);
832 int (*func
) (int number
, char *key
[]);
835 #define D(name) { #name, name ## _keys },
858 /* Handle arguments found by argp. */
860 parse_option (int key
, char *arg
, struct argp_state
*state
)
866 endp
= strchr (arg
, ':');
868 /* No specific database, change them all. */
869 for (int i
= 0; databases
[i
].name
!= NULL
; ++i
)
870 __nss_configure_lookup (databases
[i
].name
, arg
);
874 for (i
= 0; databases
[i
].name
!= NULL
; ++i
)
875 if (strncmp (databases
[i
].name
, arg
, endp
- arg
) == 0)
877 __nss_configure_lookup (databases
[i
].name
, endp
+ 1);
880 if (databases
[i
].name
== NULL
)
881 error (EXIT_FAILURE
, 0, gettext ("Unknown database name"));
890 return ARGP_ERR_UNKNOWN
;
898 more_help (int key
, const char *text
, void *input
)
906 case ARGP_KEY_HELP_EXTRA
:
907 /* We print some extra information. */
908 fp
= open_memstream (&doc
, &len
);
911 fputs_unlocked (_("Supported databases:\n"), fp
);
913 for (int i
= 0, col
= 0; databases
[i
].name
!= NULL
; ++i
)
915 len
= strlen (databases
[i
].name
);
921 fputc_unlocked ('\n', fp
);
924 fputc_unlocked (' ', fp
);
927 fputs_unlocked (databases
[i
].name
, fp
);
933 fprintf (fp
, gettext ("\
934 For bug reporting instructions, please see:\n\
935 %s.\n"), REPORT_BUGS_TO
);
937 if (fclose (fp
) == 0)
945 return (char *) text
;
949 /* the main function */
951 main (int argc
, char *argv
[])
953 /* Debugging support. */
956 /* Set locale via LC_ALL. */
957 setlocale (LC_ALL
, "");
958 /* Set the text message domain. */
959 textdomain (PACKAGE
);
961 /* Parse and process arguments. */
963 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
965 if ((argc
- remaining
) < 1)
967 error (0, 0, gettext ("wrong number of arguments"));
968 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
972 for (int i
= 0; databases
[i
].name
; ++i
)
973 if (argv
[remaining
][0] == databases
[i
].name
[0]
974 && !strcmp (argv
[remaining
], databases
[i
].name
))
975 return databases
[i
].func (argc
- remaining
- 1, &argv
[remaining
+ 1]);
977 fprintf (stderr
, _("Unknown database: %s\n"), argv
[remaining
]);
978 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);