]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/getent.c
[BZ #1318]
[thirdparty/glibc.git] / nss / getent.c
CommitLineData
1b85ed0c 1/* Copyright (c) 1998-2003, 2004, 2005 Free Software Foundation, Inc.
7d6a8338 2 This file is part of the GNU C Library.
ce75c139 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
7d6a8338
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
7d6a8338
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
7d6a8338 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
7d6a8338 19
232b4655 20/* getent: get entries from administrative database. */
7d6a8338 21
232b4655 22#include <aliases.h>
7d6a8338 23#include <argp.h>
a334319f
UD
24#include <grp.h>
25#include <pwd.h>
26#include <shadow.h>
7d6a8338
UD
27#include <ctype.h>
28#include <error.h>
29#include <libintl.h>
30#include <locale.h>
31#include <netdb.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
a334319f
UD
35#include <sys/socket.h>
36#include <netinet/in.h>
37#include <netinet/ether.h>
7d6a8338 38#include <arpa/inet.h>
ce75c139 39#include <arpa/nameser.h>
7d6a8338
UD
40
41/* Get libc version number. */
42#include <version.h>
43
44#define PACKAGE _libc_intl_domainname
45
46/* Name and version of program. */
47static void print_version (FILE *stream, struct argp_state *state);
48void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
49
50/* Short description of parameters. */
51static const char args_doc[] = N_("database [key ...]");
52
9d0881aa
UD
53/* Supported options. */
54static const struct argp_option args_options[] =
55 {
56 { "service", 's', "CONFIG", 0, N_("Service configuration to be used") },
57 { NULL, 0, NULL, 0, NULL },
58 };
59
f69425fa
UD
60/* Short description of program. */
61static const char doc[] = N_("Get entries from administrative database.\v\
62For bug reporting instructions, please see:\n\
63<http://www.gnu.org/software/libc/bugs.html>.\n");
64
9d0881aa
UD
65/* Prototype for option handler. */
66static error_t parse_option (int key, char *arg, struct argp_state *state);
67
f69425fa
UD
68/* Function to print some extra text in the help message. */
69static char *more_help (int key, const char *text, void *input);
70
7d6a8338 71/* Data structure to communicate with argp functions. */
9d0881aa
UD
72static struct argp argp =
73 {
f69425fa 74 args_options, parse_option, args_doc, doc, NULL, more_help
9d0881aa 75 };
7d6a8338
UD
76
77/* Print the version information. */
78static void
79print_version (FILE *stream, struct argp_state *state)
80{
81 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
82 fprintf (stream, gettext ("\
83Copyright (C) %s Free Software Foundation, Inc.\n\
84This is free software; see the source for copying conditions. There is NO\n\
85warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
1b85ed0c 86"), "2005");
7d6a8338
UD
87 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
88}
89
232b4655
UD
90/* This is for aliases */
91static inline void
92print_aliases (struct aliasent *alias)
93{
94 unsigned int i = 0;
95
96 printf ("%s: ", alias->alias_name);
97 for (i = strlen (alias->alias_name); i < 14; ++i)
9d0881aa 98 fputs_unlocked (" ", stdout);
232b4655 99
9d0881aa 100 for (i = 0; i < alias->alias_members_len; ++i)
232b4655
UD
101 printf ("%s%s",
102 alias->alias_members [i],
103 i + 1 == alias->alias_members_len ? "\n" : ", ");
104}
105
106static int
107aliases_keys (int number, char *key[])
108{
109 int result = 0;
110 int i;
111 struct aliasent *alias;
112
9d0881aa 113 if (number == 0)
232b4655
UD
114 {
115 setaliasent ();
9d0881aa 116 while ((alias = getaliasent ()) != NULL)
232b4655
UD
117 print_aliases (alias);
118 endaliasent ();
119 return result;
120 }
121
122 for (i = 0; i < number; ++i)
123 {
124 alias = getaliasbyname (key[i]);
125
126 if (alias == NULL)
127 result = 2;
128 else
129 print_aliases (alias);
130 }
131
132 return result;
133}
134
135/* This is for ethers */
136static int
137ethers_keys (int number, char *key[])
138{
139 int result = 0;
140 int i;
141
9d0881aa 142 if (number == 0)
232b4655
UD
143 {
144 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
145 return 3;
146 }
147
148 for (i = 0; i < number; ++i)
149 {
150 struct ether_addr *ethp, eth;
151 char buffer [1024], *p;
152
153 ethp = ether_aton (key[i]);
9d0881aa 154 if (ethp != NULL)
232b4655
UD
155 {
156 if (ether_ntohost (buffer, ethp))
157 {
158 result = 2;
159 continue;
160 }
161 p = buffer;
162 }
163 else
164 {
165 if (ether_hostton (key[i], &eth))
166 {
167 result = 2;
168 continue;
169 }
170 p = key[i];
171 ethp = &eth;
172 }
173 printf ("%s %s\n", ether_ntoa (ethp), p);
174 }
175
176 return result;
177}
178
7d6a8338
UD
179/* This is for group */
180static inline void
181print_group (struct group *grp)
182{
183 unsigned int i = 0;
184
c61a8bb4 185 printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
7d6a8338 186 grp->gr_passwd ? grp->gr_passwd : "",
9d0881aa 187 (unsigned long int) grp->gr_gid);
7d6a8338
UD
188
189 while (grp->gr_mem[i] != NULL)
190 {
9d0881aa 191 fputs_unlocked (grp->gr_mem[i], stdout);
7d6a8338
UD
192 ++i;
193 if (grp->gr_mem[i] != NULL)
9d0881aa 194 putchar_unlocked (',');
7d6a8338 195 }
9d0881aa 196 putchar_unlocked ('\n');
7d6a8338
UD
197}
198
232b4655 199static int
7d6a8338
UD
200group_keys (int number, char *key[])
201{
202 int result = 0;
203 int i;
232b4655 204 struct group *grp;
7d6a8338 205
9d0881aa 206 if (number == 0)
7d6a8338 207 {
232b4655 208 setgrent ();
9d0881aa 209 while ((grp = getgrent ()) != NULL)
232b4655
UD
210 print_group (grp);
211 endgrent ();
212 return result;
213 }
7d6a8338 214
232b4655
UD
215 for (i = 0; i < number; ++i)
216 {
9030e7c4
UD
217 errno = 0;
218 char *ep;
219 gid_t arg_gid = strtoul(key[i], &ep, 10);
8e9b2075 220
9030e7c4
UD
221 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
222 /* Valid numeric gid. */
223 grp = getgrgid (arg_gid);
7d6a8338
UD
224 else
225 grp = getgrnam (key[i]);
226
227 if (grp == NULL)
228 result = 2;
229 else
230 print_group (grp);
231 }
232
233 return result;
234}
235
232b4655 236/* This is for hosts */
25337753 237static void
232b4655
UD
238print_hosts (struct hostent *host)
239{
fdcd8f9c 240 unsigned int cnt;
232b4655 241
fdcd8f9c 242 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
232b4655 243 {
fdcd8f9c
UD
244 char buf[INET6_ADDRSTRLEN];
245 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[cnt],
246 buf, sizeof (buf));
247
248 printf ("%-15s %s", ip, host->h_name);
249
250 unsigned int i;
251 for (i = 0; host->h_aliases[i] != NULL; ++i)
252 {
253 putchar_unlocked (' ');
254 fputs_unlocked (host->h_aliases[i], stdout);
255 }
256 putchar_unlocked ('\n');
232b4655 257 }
232b4655
UD
258}
259
260static int
261hosts_keys (int number, char *key[])
262{
263 int result = 0;
264 int i;
265 struct hostent *host;
266
9d0881aa 267 if (number == 0)
232b4655
UD
268 {
269 sethostent (0);
9d0881aa 270 while ((host = gethostent ()) != NULL)
232b4655
UD
271 print_hosts (host);
272 endhostent ();
273 return result;
274 }
275
276 for (i = 0; i < number; ++i)
277 {
278 struct hostent *host = NULL;
653aeda5 279 char addr[IN6ADDRSZ];
232b4655 280
653aeda5 281 if (inet_pton (AF_INET6, key[i], &addr) > 0)
a334319f 282 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
653aeda5 283 else if (inet_pton (AF_INET, key[i], &addr) > 0)
a334319f 284 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
232b4655
UD
285 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
286 host = gethostbyname2 (key[i], AF_INET);
287
288 if (host == NULL)
289 result = 2;
290 else
291 print_hosts (host);
292 }
293
294 return result;
295}
296
29bfc945
UD
297/* This is for hosts, but using getaddrinfo */
298static int
a7d24833 299ahosts_keys_int (int af, int xflags, int number, char *key[])
29bfc945
UD
300{
301 int result = 0;
302 int i;
303 struct hostent *host;
304
305 if (number == 0)
306 {
307 sethostent (0);
308 while ((host = gethostent ()) != NULL)
309 print_hosts (host);
310 endhostent ();
311 return result;
312 }
313
314 struct addrinfo hint;
315 memset (&hint, '\0', sizeof (hint));
a7d24833
UD
316 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
317 hint.ai_family = af;
29bfc945
UD
318
319 for (i = 0; i < number; ++i)
320 {
321 struct addrinfo *res;
322
323 if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
324 result = 2;
325 else
326 {
327 struct addrinfo *runp = res;
328
329 while (runp != NULL)
330 {
331 char sockbuf[20];
332 const char *sockstr;
333 if (runp->ai_socktype == SOCK_STREAM)
334 sockstr = "STREAM";
335 else if (runp->ai_socktype == SOCK_DGRAM)
336 sockstr = "DGRAM";
337 else if (runp->ai_socktype == SOCK_RAW)
338 sockstr = "RAW";
339 else
340 {
341 snprintf (sockbuf, sizeof (sockbuf), "%d",
342 runp->ai_socktype);
343 sockstr = sockbuf;
344 }
345
346 char buf[INET6_ADDRSTRLEN];
347 printf ("%-15s %-6s %s\n",
348 inet_ntop (runp->ai_family,
a691b217
UD
349 runp->ai_family == AF_INET
350 ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
351 : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
29bfc945
UD
352 buf, sizeof (buf)),
353 sockstr,
5c81b3e1 354 runp->ai_canonname ?: "");
29bfc945
UD
355
356 runp = runp->ai_next;
357 }
358
359 freeaddrinfo (res);
360 }
361 }
362
363 return result;
364}
365
a7d24833
UD
366static int
367ahosts_keys (int number, char *key[])
368{
369 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
370}
371
372static int
373ahostsv4_keys (int number, char *key[])
374{
375 return ahosts_keys_int (AF_INET, 0, number, key);
376}
377
378static int
379ahostsv6_keys (int number, char *key[])
380{
381 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
382}
383
232b4655
UD
384/* This is for netgroup */
385static int
386netgroup_keys (int number, char *key[])
387{
388 int result = 0;
9d0881aa 389 int i;
232b4655 390
9d0881aa 391 if (number == 0)
232b4655
UD
392 {
393 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
394 return 3;
395 }
396
397 for (i = 0; i < number; ++i)
398 {
399 if (!setnetgrent (key[i]))
400 result = 2;
401 else
402 {
403 char *p[3];
404
9d0881aa 405 printf ("%-21s", key[i]);
232b4655
UD
406
407 while (getnetgrent (p, p + 1, p + 2))
408 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
9d0881aa 409 putchar_unlocked ('\n');
232b4655
UD
410 }
411 }
412
413 return result;
414}
415
7d6a8338 416/* This is for networks */
25337753 417static void
7d6a8338
UD
418print_networks (struct netent *net)
419{
420 unsigned int i;
421 struct in_addr ip;
422 ip.s_addr = htonl (net->n_net);
423
9d0881aa 424 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
7d6a8338
UD
425
426 i = 0;
427 while (net->n_aliases[i] != NULL)
428 {
9d0881aa
UD
429 putchar_unlocked (' ');
430 fputs_unlocked (net->n_aliases[i], stdout);
7d6a8338
UD
431 ++i;
432 if (net->n_aliases[i] != NULL)
9d0881aa 433 putchar_unlocked (',');
7d6a8338 434 }
9d0881aa 435 putchar_unlocked ('\n');
7d6a8338
UD
436}
437
232b4655 438static int
7d6a8338
UD
439networks_keys (int number, char *key[])
440{
441 int result = 0;
442 int i;
232b4655 443 struct netent *net;
7d6a8338 444
9d0881aa 445 if (number == 0)
7d6a8338 446 {
232b4655 447 setnetent (0);
9d0881aa 448 while ((net = getnetent ()) != NULL)
232b4655
UD
449 print_networks (net);
450 endnetent ();
451 return result;
452 }
7d6a8338 453
232b4655
UD
454 for (i = 0; i < number; ++i)
455 {
7d6a8338
UD
456 if (isdigit (key[i][0]))
457 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
458 else
459 net = getnetbyname (key[i]);
460
461 if (net == NULL)
462 result = 2;
463 else
464 print_networks (net);
465 }
466
467 return result;
468}
469
470/* Now is all for passwd */
471static inline void
472print_passwd (struct passwd *pwd)
473{
c61a8bb4 474 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
7d6a8338
UD
475 pwd->pw_name ? pwd->pw_name : "",
476 pwd->pw_passwd ? pwd->pw_passwd : "",
9d0881aa
UD
477 (unsigned long int) pwd->pw_uid,
478 (unsigned long int) pwd->pw_gid,
7d6a8338
UD
479 pwd->pw_gecos ? pwd->pw_gecos : "",
480 pwd->pw_dir ? pwd->pw_dir : "",
481 pwd->pw_shell ? pwd->pw_shell : "");
482}
483
232b4655 484static int
7d6a8338
UD
485passwd_keys (int number, char *key[])
486{
487 int result = 0;
488 int i;
232b4655 489 struct passwd *pwd;
7d6a8338 490
9d0881aa 491 if (number == 0)
7d6a8338 492 {
232b4655 493 setpwent ();
9d0881aa 494 while ((pwd = getpwent ()) != NULL)
232b4655
UD
495 print_passwd (pwd);
496 endpwent ();
497 return result;
498 }
7d6a8338 499
232b4655
UD
500 for (i = 0; i < number; ++i)
501 {
9030e7c4
UD
502 errno = 0;
503 char *ep;
504 uid_t arg_uid = strtoul(key[i], &ep, 10);
505
506 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
507 /* Valid numeric uid. */
508 pwd = getpwuid (arg_uid);
7d6a8338
UD
509 else
510 pwd = getpwnam (key[i]);
511
512 if (pwd == NULL)
513 result = 2;
514 else
515 print_passwd (pwd);
516 }
517
518 return result;
519}
520
521/* This is for protocols */
522static inline void
523print_protocols (struct protoent *proto)
524{
525 unsigned int i;
526
9d0881aa 527 printf ("%-21s %d", proto->p_name, proto->p_proto);
7d6a8338
UD
528
529 i = 0;
530 while (proto->p_aliases[i] != NULL)
531 {
9d0881aa
UD
532 putchar_unlocked (' ');
533 fputs_unlocked (proto->p_aliases[i], stdout);
7d6a8338
UD
534 ++i;
535 }
9d0881aa 536 putchar_unlocked ('\n');
7d6a8338
UD
537}
538
232b4655 539static int
7d6a8338
UD
540protocols_keys (int number, char *key[])
541{
542 int result = 0;
543 int i;
232b4655 544 struct protoent *proto;
7d6a8338 545
9d0881aa 546 if (number == 0)
7d6a8338 547 {
232b4655 548 setprotoent (0);
9d0881aa 549 while ((proto = getprotoent ()) != NULL)
232b4655
UD
550 print_protocols (proto);
551 endprotoent ();
552 return result;
553 }
7d6a8338 554
232b4655
UD
555 for (i = 0; i < number; ++i)
556 {
7d6a8338
UD
557 if (isdigit (key[i][0]))
558 proto = getprotobynumber (atol (key[i]));
559 else
560 proto = getprotobyname (key[i]);
561
562 if (proto == NULL)
563 result = 2;
564 else
565 print_protocols (proto);
566 }
567
568 return result;
569}
570
232b4655 571/* Now is all for rpc */
7d6a8338 572static inline void
232b4655 573print_rpc (struct rpcent *rpc)
7d6a8338 574{
232b4655 575 int i;
7d6a8338 576
9d0881aa
UD
577 printf ("%-15s %d%s",
578 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
7d6a8338 579
232b4655
UD
580 for (i = 0; rpc->r_aliases[i]; ++i)
581 printf (" %s", rpc->r_aliases[i]);
9d0881aa 582 putchar_unlocked ('\n');
7d6a8338
UD
583}
584
232b4655
UD
585static int
586rpc_keys (int number, char *key[])
7d6a8338
UD
587{
588 int result = 0;
589 int i;
232b4655 590 struct rpcent *rpc;
7d6a8338 591
9d0881aa 592 if (number == 0)
7d6a8338 593 {
232b4655 594 setrpcent (0);
9d0881aa 595 while ((rpc = getrpcent ()) != NULL)
232b4655
UD
596 print_rpc (rpc);
597 endrpcent ();
598 return result;
599 }
7d6a8338 600
232b4655
UD
601 for (i = 0; i < number; ++i)
602 {
603 if (isdigit (key[i][0]))
604 rpc = getrpcbynumber (atol (key[i]));
605 else
606 rpc = getrpcbyname (key[i]);
7d6a8338 607
232b4655 608 if (rpc == NULL)
7d6a8338
UD
609 result = 2;
610 else
232b4655 611 print_rpc (rpc);
7d6a8338
UD
612 }
613
614 return result;
615}
616
617/* for services */
232b4655 618static void
7d6a8338
UD
619print_services (struct servent *serv)
620{
621 unsigned int i;
622
9d0881aa 623 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
7d6a8338
UD
624
625 i = 0;
626 while (serv->s_aliases[i] != NULL)
627 {
9d0881aa
UD
628 putchar_unlocked (' ');
629 fputs_unlocked (serv->s_aliases[i], stdout);
7d6a8338
UD
630 ++i;
631 }
9d0881aa 632 putchar_unlocked ('\n');
7d6a8338
UD
633}
634
232b4655 635static int
7d6a8338
UD
636services_keys (int number, char *key[])
637{
638 int result = 0;
639 int i;
232b4655
UD
640 struct servent *serv;
641
642 if (!number)
643 {
644 setservent (0);
9d0881aa 645 while ((serv = getservent ()) != NULL)
232b4655
UD
646 print_services (serv);
647 endservent ();
648 return result;
649 }
7d6a8338
UD
650
651 for (i = 0; i < number; ++i)
652 {
653 struct servent *serv;
654 char *proto = strchr (key[i], '/');
655
a70e964e
UD
656 if (proto != NULL)
657 *proto++ = '\0';
7d6a8338 658
a70e964e
UD
659 if (isdigit (key[i][0]))
660 serv = getservbyport (htons (atol (key[i])), proto);
661 else
662 serv = getservbyname (key[i], proto);
7d6a8338 663
a70e964e
UD
664 if (serv == NULL)
665 result = 2;
666 else
667 print_services (serv);
7d6a8338
UD
668 }
669
670 return result;
671}
672
232b4655 673/* This is for shadow */
25337753 674static void
232b4655 675print_shadow (struct spwd *sp)
7d6a8338 676{
232b4655
UD
677 printf ("%s:%s:",
678 sp->sp_namp ? sp->sp_namp : "",
679 sp->sp_pwdp ? sp->sp_pwdp : "");
680
9d0881aa
UD
681#define SHADOW_FIELD(n) \
682 if (sp->n == -1) \
683 putchar_unlocked (':'); \
684 else \
232b4655
UD
685 printf ("%ld:", sp->n)
686
687 SHADOW_FIELD (sp_lstchg);
688 SHADOW_FIELD (sp_min);
689 SHADOW_FIELD (sp_max);
690 SHADOW_FIELD (sp_warn);
691 SHADOW_FIELD (sp_inact);
692 SHADOW_FIELD (sp_expire);
693 if (sp->sp_flag == ~0ul)
9d0881aa 694 putchar_unlocked ('\n');
232b4655
UD
695 else
696 printf ("%lu\n", sp->sp_flag);
697}
7d6a8338 698
232b4655
UD
699static int
700shadow_keys (int number, char *key[])
701{
702 int result = 0;
703 int i;
7d6a8338 704
9d0881aa 705 if (number == 0)
7d6a8338 706 {
232b4655
UD
707 struct spwd *sp;
708
709 setspent ();
9d0881aa 710 while ((sp = getspent ()) != NULL)
232b4655
UD
711 print_shadow (sp);
712 endpwent ();
713 return result;
7d6a8338
UD
714 }
715
232b4655 716 for (i = 0; i < number; ++i)
7d6a8338 717 {
232b4655 718 struct spwd *sp;
7d6a8338 719
232b4655 720 sp = getspnam (key[i]);
7d6a8338 721
232b4655
UD
722 if (sp == NULL)
723 result = 2;
7d6a8338 724 else
232b4655
UD
725 print_shadow (sp);
726 }
7d6a8338 727
232b4655
UD
728 return result;
729}
7d6a8338 730
232b4655
UD
731struct
732 {
733 const char *name;
734 int (*func) (int number, char *key[]);
735 } databases[] =
736 {
737#define D(name) { #name, name ## _keys },
29bfc945 738D(ahosts)
a7d24833
UD
739D(ahostsv4)
740D(ahostsv6)
232b4655
UD
741D(aliases)
742D(ethers)
743D(group)
744D(hosts)
745D(netgroup)
746D(networks)
747D(passwd)
748D(protocols)
749D(rpc)
750D(services)
751D(shadow)
752#undef D
9d0881aa 753 { NULL, NULL }
232b4655
UD
754 };
755
9d0881aa
UD
756/* Handle arguments found by argp. */
757static error_t
758parse_option (int key, char *arg, struct argp_state *state)
759{
b4f6f4be 760 char *endp;
9d0881aa
UD
761 switch (key)
762 {
763 case 's':
b4f6f4be
UD
764 endp = strchr (arg, ':');
765 if (endp == NULL)
766 /* No specific database, change them all. */
767 for (int i = 0; databases[i].name != NULL; ++i)
768 __nss_configure_lookup (databases[i].name, arg);
769 else
770 {
771 int i;
772 for (i = 0; databases[i].name != NULL; ++i)
773 if (strncmp (databases[i].name, arg, endp - arg) == 0)
774 {
775 __nss_configure_lookup (databases[i].name, endp + 1);
776 break;
777 }
778 if (databases[i].name == NULL)
779 error (EXIT_FAILURE, 0, gettext ("Unknown database name"));
780 }
9d0881aa
UD
781 break;
782
783 default:
784 return ARGP_ERR_UNKNOWN;
785 }
786
787 return 0;
788}
789
232b4655 790
f69425fa
UD
791static char *
792more_help (int key, const char *text, void *input)
793{
f69425fa 794 switch (key)
232b4655 795 {
b4f6f4be
UD
796 size_t len;
797 char *doc;
798 FILE *fp;
799
f69425fa
UD
800 case ARGP_KEY_HELP_EXTRA:
801 /* We print some extra information. */
b4f6f4be
UD
802 fp = open_memstream (&doc, &len);
803 if (fp != NULL)
7d6a8338 804 {
b4f6f4be 805 fputs_unlocked (_("Supported databases:\n"), fp);
f69425fa 806
b4f6f4be 807 for (int i = 0, col = 0; databases[i].name != NULL; ++i)
7d6a8338 808 {
f69425fa
UD
809 len = strlen (databases[i].name);
810 if (i != 0)
232b4655 811 {
f69425fa
UD
812 if (col + len > 72)
813 {
814 col = 0;
b4f6f4be 815 fputc_unlocked ('\n', fp);
f69425fa
UD
816 }
817 else
b4f6f4be 818 fputc_unlocked (' ', fp);
232b4655 819 }
f69425fa 820
b4f6f4be 821 fputs_unlocked (databases[i].name, fp);
f69425fa 822 col += len + 1;
7d6a8338 823 }
232b4655 824
b4f6f4be
UD
825 if (fclose (fp) == 0)
826 return doc;
7d6a8338 827 }
f69425fa 828 break;
232b4655 829
f69425fa
UD
830 default:
831 break;
832 }
833 return (char *) text;
232b4655
UD
834}
835
f69425fa 836
232b4655
UD
837/* the main function */
838int
839main (int argc, char *argv[])
840{
a334319f 841 int remaining, i;
232b4655
UD
842
843 /* Set locale via LC_ALL. */
844 setlocale (LC_ALL, "");
845 /* Set the text message domain. */
846 textdomain (PACKAGE);
847
232b4655
UD
848 /* Parse and process arguments. */
849 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
850
851 if ((argc - remaining) < 1)
852 {
853 error (0, 0, gettext ("wrong number of arguments"));
7d6a8338
UD
854 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
855 return 1;
856 }
857
a334319f 858 for (i = 0; databases[i].name; ++i)
9d0881aa
UD
859 if (argv[remaining][0] == databases[i].name[0]
860 && !strcmp (argv[remaining], databases[i].name))
861 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
232b4655 862
9d0881aa 863 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
232b4655
UD
864 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
865 return 1;
7d6a8338 866}