]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/getent.c
elf: Make glibc.rtld.enable_secure ignore alias environment variables
[thirdparty/glibc.git] / nss / getent.c
CommitLineData
dff8da6b 1/* Copyright (c) 1998-2024 Free Software Foundation, Inc.
7d6a8338 2 This file is part of the GNU C Library.
7d6a8338
UD
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
7d6a8338
UD
8
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
41bdb6e2 12 Lesser General Public License for more details.
7d6a8338 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
7d6a8338 17
232b4655 18/* getent: get entries from administrative database. */
7d6a8338 19
232b4655 20#include <aliases.h>
7d6a8338 21#include <argp.h>
7d6a8338
UD
22#include <ctype.h>
23#include <error.h>
ff3cacc5 24#include <grp.h>
c518f9a4 25#include <gshadow.h>
7d6a8338
UD
26#include <libintl.h>
27#include <locale.h>
ff3cacc5 28#include <mcheck.h>
7d6a8338 29#include <netdb.h>
ff3cacc5
UD
30#include <pwd.h>
31#include <shadow.h>
a160f8d8 32#include <stdbool.h>
7d6a8338
UD
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
7d6a8338 36#include <arpa/inet.h>
ce75c139 37#include <arpa/nameser.h>
ff3cacc5
UD
38#include <netinet/ether.h>
39#include <netinet/in.h>
40#include <sys/socket.h>
1599ed4e 41#include <scratch_buffer.h>
0c6d82e9 42#include <inttypes.h>
7d6a8338
UD
43
44/* Get libc version number. */
45#include <version.h>
46
47#define PACKAGE _libc_intl_domainname
48
49/* Name and version of program. */
50static void print_version (FILE *stream, struct argp_state *state);
51void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
52
53/* Short description of parameters. */
54static const char args_doc[] = N_("database [key ...]");
55
9d0881aa
UD
56/* Supported options. */
57static const struct argp_option args_options[] =
58 {
0e2b9cdd 59 { "service", 's', N_("CONFIG"), 0, N_("Service configuration to be used") },
a160f8d8 60 { "no-idn", 'i', NULL, 0, N_("disable IDN encoding") },
a623f13a
FW
61 { "no-addrconfig", 'A', NULL, 0,
62 N_("do not filter out unsupported IPv4/IPv6 addresses (with ahosts*)") },
9d0881aa
UD
63 { NULL, 0, NULL, 0, NULL },
64 };
65
f69425fa 66/* Short description of program. */
cbbcaf23 67static const char doc[] = N_("Get entries from administrative database.");
f69425fa 68
9d0881aa
UD
69/* Prototype for option handler. */
70static error_t parse_option (int key, char *arg, struct argp_state *state);
71
f69425fa
UD
72/* Function to print some extra text in the help message. */
73static char *more_help (int key, const char *text, void *input);
74
7d6a8338 75/* Data structure to communicate with argp functions. */
9d0881aa
UD
76static struct argp argp =
77 {
f69425fa 78 args_options, parse_option, args_doc, doc, NULL, more_help
9d0881aa 79 };
7d6a8338 80
a160f8d8
UD
81/* Additional getaddrinfo flags for IDN encoding. */
82static int idn_flags = AI_IDN | AI_CANONIDN;
83
a623f13a
FW
84/* Set to 0 by --no-addrconfig. */
85static int addrconfig_flags = AI_ADDRCONFIG;
86
7d6a8338
UD
87/* Print the version information. */
88static void
89print_version (FILE *stream, struct argp_state *state)
90{
8b748aed 91 fprintf (stream, "getent %s%s\n", PKGVERSION, VERSION);
7d6a8338
UD
92 fprintf (stream, gettext ("\
93Copyright (C) %s Free Software Foundation, Inc.\n\
94This is free software; see the source for copying conditions. There is NO\n\
95warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
1059defe 96"), "2024");
7d6a8338
UD
97 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
98}
99
232b4655 100/* This is for aliases */
f1d70dad 101static void
232b4655
UD
102print_aliases (struct aliasent *alias)
103{
104 unsigned int i = 0;
105
106 printf ("%s: ", alias->alias_name);
107 for (i = strlen (alias->alias_name); i < 14; ++i)
9d0881aa 108 fputs_unlocked (" ", stdout);
232b4655 109
9d0881aa 110 for (i = 0; i < alias->alias_members_len; ++i)
232b4655
UD
111 printf ("%s%s",
112 alias->alias_members [i],
113 i + 1 == alias->alias_members_len ? "\n" : ", ");
114}
115
116static int
117aliases_keys (int number, char *key[])
118{
119 int result = 0;
120 int i;
121 struct aliasent *alias;
122
9d0881aa 123 if (number == 0)
232b4655
UD
124 {
125 setaliasent ();
9d0881aa 126 while ((alias = getaliasent ()) != NULL)
232b4655
UD
127 print_aliases (alias);
128 endaliasent ();
129 return result;
130 }
131
132 for (i = 0; i < number; ++i)
133 {
134 alias = getaliasbyname (key[i]);
135
136 if (alias == NULL)
137 result = 2;
138 else
139 print_aliases (alias);
140 }
141
142 return result;
143}
144
145/* This is for ethers */
146static int
147ethers_keys (int number, char *key[])
148{
149 int result = 0;
150 int i;
151
9d0881aa 152 if (number == 0)
232b4655
UD
153 {
154 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
155 return 3;
156 }
157
158 for (i = 0; i < number; ++i)
159 {
160 struct ether_addr *ethp, eth;
161 char buffer [1024], *p;
162
163 ethp = ether_aton (key[i]);
9d0881aa 164 if (ethp != NULL)
232b4655
UD
165 {
166 if (ether_ntohost (buffer, ethp))
167 {
168 result = 2;
169 continue;
170 }
171 p = buffer;
172 }
173 else
174 {
175 if (ether_hostton (key[i], &eth))
176 {
177 result = 2;
178 continue;
179 }
180 p = key[i];
181 ethp = &eth;
182 }
183 printf ("%s %s\n", ether_ntoa (ethp), p);
184 }
185
186 return result;
187}
188
7d6a8338 189/* This is for group */
f1d70dad 190static void
7d6a8338
UD
191print_group (struct group *grp)
192{
676599b3
FW
193 if (putgrent (grp, stdout) != 0)
194 fprintf (stderr, "error writing group entry: %m\n");
7d6a8338
UD
195}
196
232b4655 197static int
7d6a8338
UD
198group_keys (int number, char *key[])
199{
200 int result = 0;
201 int i;
232b4655 202 struct group *grp;
7d6a8338 203
9d0881aa 204 if (number == 0)
7d6a8338 205 {
232b4655 206 setgrent ();
9d0881aa 207 while ((grp = getgrent ()) != NULL)
232b4655
UD
208 print_group (grp);
209 endgrent ();
210 return result;
211 }
7d6a8338 212
232b4655
UD
213 for (i = 0; i < number; ++i)
214 {
9030e7c4
UD
215 errno = 0;
216 char *ep;
217 gid_t arg_gid = strtoul(key[i], &ep, 10);
8e9b2075 218
9030e7c4
UD
219 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
220 /* Valid numeric gid. */
221 grp = getgrgid (arg_gid);
7d6a8338
UD
222 else
223 grp = getgrnam (key[i]);
224
225 if (grp == NULL)
226 result = 2;
227 else
228 print_group (grp);
229 }
230
231 return result;
232}
233
c518f9a4
AJ
234/* This is for gshadow */
235static void
236print_gshadow (struct sgrp *sg)
237{
676599b3
FW
238 if (putsgent (sg, stdout) != 0)
239 fprintf (stderr, "error writing gshadow entry: %m\n");
c518f9a4
AJ
240}
241
242static int
243gshadow_keys (int number, char *key[])
244{
245 int result = 0;
246 int i;
247
248 if (number == 0)
249 {
250 struct sgrp *sg;
251
252 setsgent ();
253 while ((sg = getsgent ()) != NULL)
254 print_gshadow (sg);
255 endsgent ();
256 return result;
257 }
258
259 for (i = 0; i < number; ++i)
260 {
261 struct sgrp *sg;
262
263 sg = getsgnam (key[i]);
264
265 if (sg == NULL)
266 result = 2;
267 else
268 print_gshadow (sg);
269 }
270
271 return result;
272}
273
232b4655 274/* This is for hosts */
25337753 275static void
232b4655
UD
276print_hosts (struct hostent *host)
277{
fdcd8f9c 278 unsigned int cnt;
232b4655 279
fdcd8f9c 280 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
232b4655 281 {
fdcd8f9c
UD
282 char buf[INET6_ADDRSTRLEN];
283 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
284 buf, sizeof (buf));
285
286 printf ("%-15s %s", ip, host->h_name);
287
288 unsigned int i;
289 for (i = 0; host->h_aliases[i] != NULL; ++i)
290 {
291 putchar_unlocked (' ');
292 fputs_unlocked (host->h_aliases[i], stdout);
293 }
294 putchar_unlocked ('\n');
232b4655 295 }
232b4655
UD
296}
297
298static int
299hosts_keys (int number, char *key[])
300{
301 int result = 0;
302 int i;
303 struct hostent *host;
304
9d0881aa 305 if (number == 0)
232b4655
UD
306 {
307 sethostent (0);
9d0881aa 308 while ((host = gethostent ()) != NULL)
232b4655
UD
309 print_hosts (host);
310 endhostent ();
311 return result;
312 }
313
314 for (i = 0; i < number; ++i)
315 {
316 struct hostent *host = NULL;
653aeda5 317 char addr[IN6ADDRSZ];
232b4655 318
653aeda5 319 if (inet_pton (AF_INET6, key[i], &addr) > 0)
4f3d6536 320 host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6);
653aeda5 321 else if (inet_pton (AF_INET, key[i], &addr) > 0)
4f3d6536 322 host = gethostbyaddr (addr, INADDRSZ, AF_INET);
232b4655
UD
323 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
324 host = gethostbyname2 (key[i], AF_INET);
325
326 if (host == NULL)
327 result = 2;
328 else
329 print_hosts (host);
330 }
331
332 return result;
333}
334
29bfc945
UD
335/* This is for hosts, but using getaddrinfo */
336static int
a7d24833 337ahosts_keys_int (int af, int xflags, int number, char *key[])
29bfc945
UD
338{
339 int result = 0;
340 int i;
341 struct hostent *host;
342
343 if (number == 0)
344 {
345 sethostent (0);
346 while ((host = gethostent ()) != NULL)
347 print_hosts (host);
348 endhostent ();
349 return result;
350 }
351
352 struct addrinfo hint;
353 memset (&hint, '\0', sizeof (hint));
a623f13a 354 hint.ai_flags = (AI_V4MAPPED | addrconfig_flags | AI_CANONNAME
a160f8d8 355 | idn_flags | xflags);
a7d24833 356 hint.ai_family = af;
29bfc945
UD
357
358 for (i = 0; i < number; ++i)
359 {
360 struct addrinfo *res;
361
362 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
363 result = 2;
364 else
365 {
366 struct addrinfo *runp = res;
367
368 while (runp != NULL)
369 {
370 char sockbuf[20];
371 const char *sockstr;
372 if (runp->ai_socktype == SOCK_STREAM)
373 sockstr = "STREAM";
374 else if (runp->ai_socktype == SOCK_DGRAM)
375 sockstr = "DGRAM";
376 else if (runp->ai_socktype == SOCK_RAW)
377 sockstr = "RAW";
372bfcac
UD
378#ifdef SOCK_SEQPACKET
379 else if (runp->ai_socktype == SOCK_SEQPACKET)
380 sockstr = "SEQPACKET";
381#endif
382#ifdef SOCK_RDM
383 else if (runp->ai_socktype == SOCK_RDM)
384 sockstr = "RDM";
385#endif
386#ifdef SOCK_DCCP
387 else if (runp->ai_socktype == SOCK_DCCP)
388 sockstr = "DCCP";
389#endif
390#ifdef SOCK_PACKET
391 else if (runp->ai_socktype == SOCK_PACKET)
392 sockstr = "PACKET";
393#endif
29bfc945
UD
394 else
395 {
396 snprintf (sockbuf, sizeof (sockbuf), "%d",
397 runp->ai_socktype);
398 sockstr = sockbuf;
399 }
400
0c6d82e9
FW
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. */
407 scope[0] = '\0';
408 else
409 snprintf (scope, sizeof (scope), "%%%" PRIu32,
410 addr6->sin6_scope_id);
411
29bfc945 412 char buf[INET6_ADDRSTRLEN];
0c6d82e9
FW
413 if (inet_ntop (runp->ai_family,
414 runp->ai_family == AF_INET
415 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
416 : &addr6->sin6_addr,
417 buf, sizeof (buf)) == NULL)
418 {
419 strcpy (buf, "<invalid>");
420 scope[0] = '\0';
421 }
422
423 int pad = 15 - strlen (buf) - strlen (scope);
424 if (pad < 0)
425 pad = 0;
426
427 printf ("%s%-*s %-6s %s\n",
428 buf, pad, scope, sockstr, runp->ai_canonname ?: "");
29bfc945
UD
429
430 runp = runp->ai_next;
431 }
432
433 freeaddrinfo (res);
434 }
435 }
436
437 return result;
438}
439
a7d24833
UD
440static int
441ahosts_keys (int number, char *key[])
442{
443 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
444}
445
446static int
447ahostsv4_keys (int number, char *key[])
448{
449 return ahosts_keys_int (AF_INET, 0, number, key);
450}
451
452static int
453ahostsv6_keys (int number, char *key[])
454{
455 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
456}
457
232b4655
UD
458/* This is for netgroup */
459static int
460netgroup_keys (int number, char *key[])
461{
462 int result = 0;
232b4655 463
9d0881aa 464 if (number == 0)
232b4655
UD
465 {
466 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
467 return 3;
468 }
469
684ae515
UD
470 if (number == 4)
471 {
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];
475
476 printf ("%-21s (%s,%s,%s) = %d\n",
477 key[0], host ?: "", user ?: "", domain ?: "",
478 innetgr (key[0], host, user, domain));
479 }
480 else if (number == 1)
232b4655 481 {
684ae515 482 if (!setnetgrent (key[0]))
232b4655
UD
483 result = 2;
484 else
485 {
486 char *p[3];
487
684ae515 488 printf ("%-21s", key[0]);
232b4655
UD
489
490 while (getnetgrent (p, p + 1, p + 2))
684ae515 491 printf (" (%s,%s,%s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
9d0881aa 492 putchar_unlocked ('\n');
232b4655
UD
493 }
494 }
495
ff3cacc5
UD
496 endnetgrent ();
497
232b4655
UD
498 return result;
499}
500
1599ed4e
FW
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>
506
82e9a1f7
UD
507/* This is for initgroups */
508static int
509initgroups_keys (int number, char *key[])
510{
f4ec4833
UD
511 if (number == 0)
512 {
513 fprintf (stderr, _("Enumeration not supported on %s\n"), "initgroups");
514 return 3;
515 }
516
1599ed4e
FW
517 struct gid_list list;
518 gid_list_init (&list);
519 if (!gid_list_resize (&list, 10))
520 {
521 fprintf (stderr, _("Could not allocate group list: %m\n"));
522 return 3;
523 }
524
82e9a1f7
UD
525 for (int i = 0; i < number; ++i)
526 {
1599ed4e 527 int no = gid_list_size (&list);
82e9a1f7 528 int n;
1599ed4e
FW
529 while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1
530 && no > gid_list_size (&list))
82e9a1f7 531 {
1599ed4e
FW
532 if (!gid_list_resize (&list, no))
533 {
534 fprintf (stderr, _("Could not allocate group list: %m\n"));
535 return 3;
536 }
82e9a1f7
UD
537 }
538
539 if (n == -1)
1599ed4e
FW
540 {
541 gid_list_free (&list);
542 return 1;
543 }
82e9a1f7 544
1599ed4e 545 const gid_t *grps = gid_list_begin (&list);
82e9a1f7
UD
546 printf ("%-21s", key[i]);
547 for (int j = 0; j < n; ++j)
548 if (grps[j] != -1)
549 printf (" %ld", (long int) grps[j]);
550 putchar_unlocked ('\n');
551 }
552
1599ed4e
FW
553 gid_list_free (&list);
554
82e9a1f7
UD
555 return 0;
556}
557
7d6a8338 558/* This is for networks */
25337753 559static void
7d6a8338
UD
560print_networks (struct netent *net)
561{
562 unsigned int i;
563 struct in_addr ip;
564 ip.s_addr = htonl (net->n_net);
565
9d0881aa 566 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
7d6a8338
UD
567
568 i = 0;
569 while (net->n_aliases[i] != NULL)
570 {
9d0881aa
UD
571 putchar_unlocked (' ');
572 fputs_unlocked (net->n_aliases[i], stdout);
7d6a8338 573 ++i;
7d6a8338 574 }
9d0881aa 575 putchar_unlocked ('\n');
7d6a8338
UD
576}
577
232b4655 578static int
7d6a8338
UD
579networks_keys (int number, char *key[])
580{
581 int result = 0;
582 int i;
232b4655 583 struct netent *net;
7d6a8338 584
9d0881aa 585 if (number == 0)
7d6a8338 586 {
232b4655 587 setnetent (0);
9d0881aa 588 while ((net = getnetent ()) != NULL)
232b4655
UD
589 print_networks (net);
590 endnetent ();
591 return result;
592 }
7d6a8338 593
232b4655
UD
594 for (i = 0; i < number; ++i)
595 {
7d6a8338 596 if (isdigit (key[i][0]))
5cd1f906 597 net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC);
7d6a8338
UD
598 else
599 net = getnetbyname (key[i]);
600
601 if (net == NULL)
602 result = 2;
603 else
604 print_networks (net);
605 }
606
607 return result;
608}
609
610/* Now is all for passwd */
f1d70dad 611static void
7d6a8338
UD
612print_passwd (struct passwd *pwd)
613{
676599b3
FW
614 if (putpwent (pwd, stdout) != 0)
615 fprintf (stderr, "error writing passwd entry: %m\n");
7d6a8338
UD
616}
617
232b4655 618static int
7d6a8338
UD
619passwd_keys (int number, char *key[])
620{
621 int result = 0;
622 int i;
232b4655 623 struct passwd *pwd;
7d6a8338 624
9d0881aa 625 if (number == 0)
7d6a8338 626 {
232b4655 627 setpwent ();
9d0881aa 628 while ((pwd = getpwent ()) != NULL)
232b4655
UD
629 print_passwd (pwd);
630 endpwent ();
631 return result;
632 }
7d6a8338 633
232b4655
UD
634 for (i = 0; i < number; ++i)
635 {
9030e7c4
UD
636 errno = 0;
637 char *ep;
638 uid_t arg_uid = strtoul(key[i], &ep, 10);
639
640 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
641 /* Valid numeric uid. */
642 pwd = getpwuid (arg_uid);
7d6a8338
UD
643 else
644 pwd = getpwnam (key[i]);
645
646 if (pwd == NULL)
647 result = 2;
648 else
649 print_passwd (pwd);
650 }
651
652 return result;
653}
654
655/* This is for protocols */
f1d70dad 656static void
7d6a8338
UD
657print_protocols (struct protoent *proto)
658{
659 unsigned int i;
660
9d0881aa 661 printf ("%-21s %d", proto->p_name, proto->p_proto);
7d6a8338
UD
662
663 i = 0;
664 while (proto->p_aliases[i] != NULL)
665 {
9d0881aa
UD
666 putchar_unlocked (' ');
667 fputs_unlocked (proto->p_aliases[i], stdout);
7d6a8338
UD
668 ++i;
669 }
9d0881aa 670 putchar_unlocked ('\n');
7d6a8338
UD
671}
672
232b4655 673static int
7d6a8338
UD
674protocols_keys (int number, char *key[])
675{
676 int result = 0;
677 int i;
232b4655 678 struct protoent *proto;
7d6a8338 679
9d0881aa 680 if (number == 0)
7d6a8338 681 {
232b4655 682 setprotoent (0);
9d0881aa 683 while ((proto = getprotoent ()) != NULL)
232b4655
UD
684 print_protocols (proto);
685 endprotoent ();
686 return result;
687 }
7d6a8338 688
232b4655
UD
689 for (i = 0; i < number; ++i)
690 {
7d6a8338
UD
691 if (isdigit (key[i][0]))
692 proto = getprotobynumber (atol (key[i]));
693 else
694 proto = getprotobyname (key[i]);
695
696 if (proto == NULL)
697 result = 2;
698 else
699 print_protocols (proto);
700 }
701
702 return result;
703}
704
a1309c2b 705#if HAVE_SUNRPC
232b4655 706/* Now is all for rpc */
f1d70dad 707static void
232b4655 708print_rpc (struct rpcent *rpc)
7d6a8338 709{
232b4655 710 int i;
7d6a8338 711
9d0881aa
UD
712 printf ("%-15s %d%s",
713 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
7d6a8338 714
232b4655
UD
715 for (i = 0; rpc->r_aliases[i]; ++i)
716 printf (" %s", rpc->r_aliases[i]);
9d0881aa 717 putchar_unlocked ('\n');
7d6a8338
UD
718}
719
232b4655
UD
720static int
721rpc_keys (int number, char *key[])
7d6a8338
UD
722{
723 int result = 0;
724 int i;
232b4655 725 struct rpcent *rpc;
7d6a8338 726
9d0881aa 727 if (number == 0)
7d6a8338 728 {
232b4655 729 setrpcent (0);
9d0881aa 730 while ((rpc = getrpcent ()) != NULL)
232b4655
UD
731 print_rpc (rpc);
732 endrpcent ();
733 return result;
734 }
7d6a8338 735
232b4655
UD
736 for (i = 0; i < number; ++i)
737 {
738 if (isdigit (key[i][0]))
739 rpc = getrpcbynumber (atol (key[i]));
740 else
741 rpc = getrpcbyname (key[i]);
7d6a8338 742
232b4655 743 if (rpc == NULL)
7d6a8338
UD
744 result = 2;
745 else
232b4655 746 print_rpc (rpc);
7d6a8338
UD
747 }
748
749 return result;
750}
a1309c2b 751#endif
7d6a8338
UD
752
753/* for services */
232b4655 754static void
7d6a8338
UD
755print_services (struct servent *serv)
756{
757 unsigned int i;
758
9d0881aa 759 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
7d6a8338
UD
760
761 i = 0;
762 while (serv->s_aliases[i] != NULL)
763 {
9d0881aa
UD
764 putchar_unlocked (' ');
765 fputs_unlocked (serv->s_aliases[i], stdout);
7d6a8338
UD
766 ++i;
767 }
9d0881aa 768 putchar_unlocked ('\n');
7d6a8338
UD
769}
770
232b4655 771static int
7d6a8338
UD
772services_keys (int number, char *key[])
773{
774 int result = 0;
775 int i;
232b4655
UD
776 struct servent *serv;
777
778 if (!number)
779 {
780 setservent (0);
9d0881aa 781 while ((serv = getservent ()) != NULL)
232b4655
UD
782 print_services (serv);
783 endservent ();
784 return result;
785 }
7d6a8338
UD
786
787 for (i = 0; i < number; ++i)
788 {
789 struct servent *serv;
790 char *proto = strchr (key[i], '/');
791
a70e964e
UD
792 if (proto != NULL)
793 *proto++ = '\0';
7d6a8338 794
e4368156
OB
795 char *endptr;
796 long port = strtol (key[i], &endptr, 10);
797
798 if (isdigit (key[i][0]) && *endptr == '\0'
799 && 0 <= port && port <= 65535)
800 serv = getservbyport (htons (port), proto);
a70e964e
UD
801 else
802 serv = getservbyname (key[i], proto);
7d6a8338 803
a70e964e
UD
804 if (serv == NULL)
805 result = 2;
806 else
807 print_services (serv);
7d6a8338
UD
808 }
809
810 return result;
811}
812
232b4655 813/* This is for shadow */
25337753 814static void
232b4655 815print_shadow (struct spwd *sp)
7d6a8338 816{
676599b3
FW
817 if (putspent (sp, stdout) != 0)
818 fprintf (stderr, "error writing shadow entry: %m\n");
232b4655 819}
7d6a8338 820
232b4655
UD
821static int
822shadow_keys (int number, char *key[])
823{
824 int result = 0;
825 int i;
7d6a8338 826
9d0881aa 827 if (number == 0)
7d6a8338 828 {
232b4655
UD
829 struct spwd *sp;
830
831 setspent ();
9d0881aa 832 while ((sp = getspent ()) != NULL)
232b4655 833 print_shadow (sp);
d34c9158 834 endspent ();
232b4655 835 return result;
7d6a8338
UD
836 }
837
232b4655 838 for (i = 0; i < number; ++i)
7d6a8338 839 {
232b4655 840 struct spwd *sp;
7d6a8338 841
232b4655 842 sp = getspnam (key[i]);
7d6a8338 843
232b4655
UD
844 if (sp == NULL)
845 result = 2;
7d6a8338 846 else
232b4655
UD
847 print_shadow (sp);
848 }
7d6a8338 849
232b4655
UD
850 return result;
851}
7d6a8338 852
232b4655
UD
853struct
854 {
855 const char *name;
856 int (*func) (int number, char *key[]);
857 } databases[] =
858 {
859#define D(name) { #name, name ## _keys },
29bfc945 860D(ahosts)
a7d24833
UD
861D(ahostsv4)
862D(ahostsv6)
232b4655
UD
863D(aliases)
864D(ethers)
865D(group)
c518f9a4 866D(gshadow)
232b4655 867D(hosts)
82e9a1f7 868D(initgroups)
232b4655
UD
869D(netgroup)
870D(networks)
871D(passwd)
872D(protocols)
a1309c2b 873#if HAVE_SUNRPC
232b4655 874D(rpc)
a1309c2b 875#endif
232b4655
UD
876D(services)
877D(shadow)
878#undef D
9d0881aa 879 { NULL, NULL }
232b4655
UD
880 };
881
9d0881aa
UD
882/* Handle arguments found by argp. */
883static error_t
884parse_option (int key, char *arg, struct argp_state *state)
885{
b4f6f4be 886 char *endp;
9d0881aa
UD
887 switch (key)
888 {
889 case 's':
b4f6f4be
UD
890 endp = strchr (arg, ':');
891 if (endp == NULL)
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);
895 else
896 {
897 int i;
898 for (i = 0; databases[i].name != NULL; ++i)
899 if (strncmp (databases[i].name, arg, endp - arg) == 0)
900 {
901 __nss_configure_lookup (databases[i].name, endp + 1);
902 break;
903 }
904 if (databases[i].name == NULL)
905 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
906 }
9d0881aa
UD
907 break;
908
a160f8d8
UD
909 case 'i':
910 idn_flags = 0;
911 break;
912
a623f13a
FW
913 case 'A':
914 addrconfig_flags = 0;
915 break;
916
9d0881aa
UD
917 default:
918 return ARGP_ERR_UNKNOWN;
919 }
920
921 return 0;
922}
923
232b4655 924
f69425fa
UD
925static char *
926more_help (int key, const char *text, void *input)
927{
f69425fa 928 switch (key)
232b4655 929 {
b4f6f4be
UD
930 size_t len;
931 char *doc;
932 FILE *fp;
933
f69425fa
UD
934 case ARGP_KEY_HELP_EXTRA:
935 /* We print some extra information. */
b4f6f4be
UD
936 fp = open_memstream (&doc, &len);
937 if (fp != NULL)
7d6a8338 938 {
b4f6f4be 939 fputs_unlocked (_("Supported databases:\n"), fp);
f69425fa 940
b4f6f4be 941 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
7d6a8338 942 {
f69425fa
UD
943 len = strlen (databases[i].name);
944 if (i != 0)
232b4655 945 {
f69425fa
UD
946 if (col + len > 72)
947 {
948 col = 0;
b4f6f4be 949 fputc_unlocked ('\n', fp);
f69425fa
UD
950 }
951 else
b4f6f4be 952 fputc_unlocked (' ', fp);
232b4655 953 }
f69425fa 954
b4f6f4be 955 fputs_unlocked (databases[i].name, fp);
f69425fa 956 col += len + 1;
7d6a8338 957 }
232b4655 958
cbbcaf23
UD
959 fputs ("\n\n", fp);
960
8b748aed 961 fprintf (fp, gettext ("\
cbbcaf23 962For bug reporting instructions, please see:\n\
8b748aed 963%s.\n"), REPORT_BUGS_TO);
cbbcaf23 964
b4f6f4be
UD
965 if (fclose (fp) == 0)
966 return doc;
7d6a8338 967 }
f69425fa 968 break;
232b4655 969
f69425fa
UD
970 default:
971 break;
972 }
973 return (char *) text;
232b4655
UD
974}
975
f69425fa 976
232b4655
UD
977/* the main function */
978int
979main (int argc, char *argv[])
980{
ff3cacc5
UD
981 /* Debugging support. */
982 mtrace ();
232b4655
UD
983
984 /* Set locale via LC_ALL. */
985 setlocale (LC_ALL, "");
986 /* Set the text message domain. */
987 textdomain (PACKAGE);
988
232b4655 989 /* Parse and process arguments. */
ff3cacc5 990 int remaining;
232b4655
UD
991 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
992
993 if ((argc - remaining) < 1)
994 {
995 error (0, 0, gettext ("wrong number of arguments"));
7d6a8338
UD
996 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
997 return 1;
998 }
999
ff3cacc5 1000 for (int i = 0; databases[i].name; ++i)
9d0881aa
UD
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]);
232b4655 1004
9d0881aa 1005 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
232b4655
UD
1006 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
1007 return 1;
7d6a8338 1008}