]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/getent.c
Update.
[thirdparty/glibc.git] / nss / getent.c
1 /* Copyright (c) 1998-2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
4
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.
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
13 Lesser General Public License for more details.
14
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. */
19
20 /* getent: get entries from administrative database. */
21
22 #include <aliases.h>
23 #include <argp.h>
24 #include <grp.h>
25 #include <pwd.h>
26 #include <shadow.h>
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>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/ether.h>
38 #include <arpa/inet.h>
39 #include <arpa/nameser.h>
40
41 /* Get libc version number. */
42 #include <version.h>
43
44 #define PACKAGE _libc_intl_domainname
45
46 /* Name and version of program. */
47 static void print_version (FILE *stream, struct argp_state *state);
48 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
49
50 /* Short description of parameters. */
51 static const char args_doc[] = N_("database [key ...]");
52
53 /* Supported options. */
54 static 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
60 /* Short description of program. */
61 static const char doc[] = N_("Get entries from administrative database.\v\
62 For bug reporting instructions, please see:\n\
63 <http://www.gnu.org/software/libc/bugs.html>.\n");
64
65 /* Prototype for option handler. */
66 static error_t parse_option (int key, char *arg, struct argp_state *state);
67
68 /* Function to print some extra text in the help message. */
69 static char *more_help (int key, const char *text, void *input);
70
71 /* Data structure to communicate with argp functions. */
72 static struct argp argp =
73 {
74 args_options, parse_option, args_doc, doc, NULL, more_help
75 };
76
77 /* Print the version information. */
78 static void
79 print_version (FILE *stream, struct argp_state *state)
80 {
81 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
82 fprintf (stream, gettext ("\
83 Copyright (C) %s Free Software Foundation, Inc.\n\
84 This is free software; see the source for copying conditions. There is NO\n\
85 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
86 "), "2004");
87 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
88 }
89
90 /* This is for aliases */
91 static inline void
92 print_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)
98 fputs_unlocked (" ", stdout);
99
100 for (i = 0; i < alias->alias_members_len; ++i)
101 printf ("%s%s",
102 alias->alias_members [i],
103 i + 1 == alias->alias_members_len ? "\n" : ", ");
104 }
105
106 static int
107 aliases_keys (int number, char *key[])
108 {
109 int result = 0;
110 int i;
111 struct aliasent *alias;
112
113 if (number == 0)
114 {
115 setaliasent ();
116 while ((alias = getaliasent ()) != NULL)
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 */
136 static int
137 ethers_keys (int number, char *key[])
138 {
139 int result = 0;
140 int i;
141
142 if (number == 0)
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]);
154 if (ethp != NULL)
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
179 /* This is for group */
180 static inline void
181 print_group (struct group *grp)
182 {
183 unsigned int i = 0;
184
185 printf ("%s:%s:%lu:", grp->gr_name ? grp->gr_name : "",
186 grp->gr_passwd ? grp->gr_passwd : "",
187 (unsigned long int) grp->gr_gid);
188
189 while (grp->gr_mem[i] != NULL)
190 {
191 fputs_unlocked (grp->gr_mem[i], stdout);
192 ++i;
193 if (grp->gr_mem[i] != NULL)
194 putchar_unlocked (',');
195 }
196 putchar_unlocked ('\n');
197 }
198
199 static int
200 group_keys (int number, char *key[])
201 {
202 int result = 0;
203 int i;
204 struct group *grp;
205
206 if (number == 0)
207 {
208 setgrent ();
209 while ((grp = getgrent ()) != NULL)
210 print_group (grp);
211 endgrent ();
212 return result;
213 }
214
215 for (i = 0; i < number; ++i)
216 {
217 errno = 0;
218 char *ep;
219 gid_t arg_gid = strtoul(key[i], &ep, 10);
220
221 if (errno != EINVAL && *key[i] != '\0' && *ep == '\0')
222 /* Valid numeric gid. */
223 grp = getgrgid (arg_gid);
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
236 /* This is for hosts */
237 static void
238 print_hosts (struct hostent *host)
239 {
240 unsigned int cnt;
241
242 for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt)
243 {
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');
257 }
258 }
259
260 static int
261 hosts_keys (int number, char *key[])
262 {
263 int result = 0;
264 int i;
265 struct hostent *host;
266
267 if (number == 0)
268 {
269 sethostent (0);
270 while ((host = gethostent ()) != NULL)
271 print_hosts (host);
272 endhostent ();
273 return result;
274 }
275
276 for (i = 0; i < number; ++i)
277 {
278 struct hostent *host = NULL;
279 char addr[IN6ADDRSZ];
280
281 if (inet_pton (AF_INET6, key[i], &addr) > 0)
282 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
283 else if (inet_pton (AF_INET, key[i], &addr) > 0)
284 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
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
297 /* This is for hosts, but using getaddrinfo */
298 static int
299 ahosts_keys_int (int af, int xflags, int number, char *key[])
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));
316 hint.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME | xflags;
317 hint.ai_family = af;
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,
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,
352 buf, sizeof (buf)),
353 sockstr,
354 runp->ai_canonname ?: "");
355
356 runp = runp->ai_next;
357 }
358
359 freeaddrinfo (res);
360 }
361 }
362
363 return result;
364 }
365
366 static int
367 ahosts_keys (int number, char *key[])
368 {
369 return ahosts_keys_int (AF_UNSPEC, 0, number, key);
370 }
371
372 static int
373 ahostsv4_keys (int number, char *key[])
374 {
375 return ahosts_keys_int (AF_INET, 0, number, key);
376 }
377
378 static int
379 ahostsv6_keys (int number, char *key[])
380 {
381 return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key);
382 }
383
384 /* This is for netgroup */
385 static int
386 netgroup_keys (int number, char *key[])
387 {
388 int result = 0;
389 int i;
390
391 if (number == 0)
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
405 printf ("%-21s", key[i]);
406
407 while (getnetgrent (p, p + 1, p + 2))
408 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
409 putchar_unlocked ('\n');
410 }
411 }
412
413 return result;
414 }
415
416 /* This is for networks */
417 static void
418 print_networks (struct netent *net)
419 {
420 unsigned int i;
421 struct in_addr ip;
422 ip.s_addr = htonl (net->n_net);
423
424 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
425
426 i = 0;
427 while (net->n_aliases[i] != NULL)
428 {
429 putchar_unlocked (' ');
430 fputs_unlocked (net->n_aliases[i], stdout);
431 ++i;
432 if (net->n_aliases[i] != NULL)
433 putchar_unlocked (',');
434 }
435 putchar_unlocked ('\n');
436 }
437
438 static int
439 networks_keys (int number, char *key[])
440 {
441 int result = 0;
442 int i;
443 struct netent *net;
444
445 if (number == 0)
446 {
447 setnetent (0);
448 while ((net = getnetent ()) != NULL)
449 print_networks (net);
450 endnetent ();
451 return result;
452 }
453
454 for (i = 0; i < number; ++i)
455 {
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 */
471 static inline void
472 print_passwd (struct passwd *pwd)
473 {
474 printf ("%s:%s:%lu:%lu:%s:%s:%s\n",
475 pwd->pw_name ? pwd->pw_name : "",
476 pwd->pw_passwd ? pwd->pw_passwd : "",
477 (unsigned long int) pwd->pw_uid,
478 (unsigned long int) pwd->pw_gid,
479 pwd->pw_gecos ? pwd->pw_gecos : "",
480 pwd->pw_dir ? pwd->pw_dir : "",
481 pwd->pw_shell ? pwd->pw_shell : "");
482 }
483
484 static int
485 passwd_keys (int number, char *key[])
486 {
487 int result = 0;
488 int i;
489 struct passwd *pwd;
490
491 if (number == 0)
492 {
493 setpwent ();
494 while ((pwd = getpwent ()) != NULL)
495 print_passwd (pwd);
496 endpwent ();
497 return result;
498 }
499
500 for (i = 0; i < number; ++i)
501 {
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);
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 */
522 static inline void
523 print_protocols (struct protoent *proto)
524 {
525 unsigned int i;
526
527 printf ("%-21s %d", proto->p_name, proto->p_proto);
528
529 i = 0;
530 while (proto->p_aliases[i] != NULL)
531 {
532 putchar_unlocked (' ');
533 fputs_unlocked (proto->p_aliases[i], stdout);
534 ++i;
535 }
536 putchar_unlocked ('\n');
537 }
538
539 static int
540 protocols_keys (int number, char *key[])
541 {
542 int result = 0;
543 int i;
544 struct protoent *proto;
545
546 if (number == 0)
547 {
548 setprotoent (0);
549 while ((proto = getprotoent ()) != NULL)
550 print_protocols (proto);
551 endprotoent ();
552 return result;
553 }
554
555 for (i = 0; i < number; ++i)
556 {
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
571 /* Now is all for rpc */
572 static inline void
573 print_rpc (struct rpcent *rpc)
574 {
575 int i;
576
577 printf ("%-15s %d%s",
578 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
579
580 for (i = 0; rpc->r_aliases[i]; ++i)
581 printf (" %s", rpc->r_aliases[i]);
582 putchar_unlocked ('\n');
583 }
584
585 static int
586 rpc_keys (int number, char *key[])
587 {
588 int result = 0;
589 int i;
590 struct rpcent *rpc;
591
592 if (number == 0)
593 {
594 setrpcent (0);
595 while ((rpc = getrpcent ()) != NULL)
596 print_rpc (rpc);
597 endrpcent ();
598 return result;
599 }
600
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]);
607
608 if (rpc == NULL)
609 result = 2;
610 else
611 print_rpc (rpc);
612 }
613
614 return result;
615 }
616
617 /* for services */
618 static void
619 print_services (struct servent *serv)
620 {
621 unsigned int i;
622
623 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
624
625 i = 0;
626 while (serv->s_aliases[i] != NULL)
627 {
628 putchar_unlocked (' ');
629 fputs_unlocked (serv->s_aliases[i], stdout);
630 ++i;
631 }
632 putchar_unlocked ('\n');
633 }
634
635 static int
636 services_keys (int number, char *key[])
637 {
638 int result = 0;
639 int i;
640 struct servent *serv;
641
642 if (!number)
643 {
644 setservent (0);
645 while ((serv = getservent ()) != NULL)
646 print_services (serv);
647 endservent ();
648 return result;
649 }
650
651 for (i = 0; i < number; ++i)
652 {
653 struct servent *serv;
654 char *proto = strchr (key[i], '/');
655
656 if (proto != NULL)
657 *proto++ = '\0';
658
659 if (isdigit (key[i][0]))
660 serv = getservbyport (htons (atol (key[i])), proto);
661 else
662 serv = getservbyname (key[i], proto);
663
664 if (serv == NULL)
665 result = 2;
666 else
667 print_services (serv);
668 }
669
670 return result;
671 }
672
673 /* This is for shadow */
674 static void
675 print_shadow (struct spwd *sp)
676 {
677 printf ("%s:%s:",
678 sp->sp_namp ? sp->sp_namp : "",
679 sp->sp_pwdp ? sp->sp_pwdp : "");
680
681 #define SHADOW_FIELD(n) \
682 if (sp->n == -1) \
683 putchar_unlocked (':'); \
684 else \
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)
694 putchar_unlocked ('\n');
695 else
696 printf ("%lu\n", sp->sp_flag);
697 }
698
699 static int
700 shadow_keys (int number, char *key[])
701 {
702 int result = 0;
703 int i;
704
705 if (number == 0)
706 {
707 struct spwd *sp;
708
709 setspent ();
710 while ((sp = getspent ()) != NULL)
711 print_shadow (sp);
712 endpwent ();
713 return result;
714 }
715
716 for (i = 0; i < number; ++i)
717 {
718 struct spwd *sp;
719
720 sp = getspnam (key[i]);
721
722 if (sp == NULL)
723 result = 2;
724 else
725 print_shadow (sp);
726 }
727
728 return result;
729 }
730
731 struct
732 {
733 const char *name;
734 int (*func) (int number, char *key[]);
735 } databases[] =
736 {
737 #define D(name) { #name, name ## _keys },
738 D(ahosts)
739 D(ahostsv4)
740 D(ahostsv6)
741 D(aliases)
742 D(ethers)
743 D(group)
744 D(hosts)
745 D(netgroup)
746 D(networks)
747 D(passwd)
748 D(protocols)
749 D(rpc)
750 D(services)
751 D(shadow)
752 #undef D
753 { NULL, NULL }
754 };
755
756 /* Handle arguments found by argp. */
757 static error_t
758 parse_option (int key, char *arg, struct argp_state *state)
759 {
760 int i;
761 switch (key)
762 {
763 case 's':
764 for (i = 0; databases[i].name; ++i)
765 __nss_configure_lookup (databases[i].name, arg);
766 break;
767
768 default:
769 return ARGP_ERR_UNKNOWN;
770 }
771
772 return 0;
773 }
774
775
776 static char *
777 more_help (int key, const char *text, void *input)
778 {
779 int len;
780 char *long_doc, *doc, *p;
781
782 switch (key)
783 {
784 case ARGP_KEY_HELP_EXTRA:
785 /* We print some extra information. */
786 #if 0
787 return xstrdup (gettext ("\
788 For bug reporting instructions, please see:\n\
789 <http://www.gnu.org/software/libc/bugs.html>.\n"));
790 #endif
791 long_doc = _("Supported databases:");
792 len = strlen (long_doc) + 2;
793
794 for (int i = 0; databases[i].name; ++i)
795 len += strlen (databases[i].name) + 1;
796
797 doc = (char *) malloc (len);
798 if (doc != NULL)
799 {
800 p = stpcpy (doc, long_doc);
801 *p++ = '\n';
802
803 for (int i = 0, col = 0; databases[i].name; ++i)
804 {
805 len = strlen (databases[i].name);
806 if (i != 0)
807 {
808 if (col + len > 72)
809 {
810 col = 0;
811 *p++ = '\n';
812 }
813 else
814 *p++ = ' ';
815 }
816
817 p = mempcpy (p, databases[i].name, len);
818 col += len + 1;
819 }
820
821 return doc;
822 }
823 break;
824
825 default:
826 break;
827 }
828 return (char *) text;
829 }
830
831
832 /* the main function */
833 int
834 main (int argc, char *argv[])
835 {
836 int remaining, i;
837
838 /* Set locale via LC_ALL. */
839 setlocale (LC_ALL, "");
840 /* Set the text message domain. */
841 textdomain (PACKAGE);
842
843 /* Parse and process arguments. */
844 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
845
846 if ((argc - remaining) < 1)
847 {
848 error (0, 0, gettext ("wrong number of arguments"));
849 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
850 return 1;
851 }
852
853 for (i = 0; databases[i].name; ++i)
854 if (argv[remaining][0] == databases[i].name[0]
855 && !strcmp (argv[remaining], databases[i].name))
856 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
857
858 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
859 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
860 return 1;
861 }