]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/getent.c
Update.
[thirdparty/glibc.git] / nss / getent.c
1 /* Copyright (c) 1998, 1999, 2000, 2001, 2002 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 /* Prototype for option handler. */
61 static error_t parse_option (int key, char *arg, struct argp_state *state);
62
63 /* Data structure to communicate with argp functions. */
64 static struct argp argp =
65 {
66 args_options, parse_option, args_doc, NULL,
67 };
68
69 /* Print the version information. */
70 static void
71 print_version (FILE *stream, struct argp_state *state)
72 {
73 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
74 fprintf (stream, gettext ("\
75 Copyright (C) %s Free Software Foundation, Inc.\n\
76 This is free software; see the source for copying conditions. There is NO\n\
77 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
78 "), "2002");
79 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
80 }
81
82 /* This is for aliases */
83 static inline void
84 print_aliases (struct aliasent *alias)
85 {
86 unsigned int i = 0;
87
88 printf ("%s: ", alias->alias_name);
89 for (i = strlen (alias->alias_name); i < 14; ++i)
90 fputs_unlocked (" ", stdout);
91
92 for (i = 0; i < alias->alias_members_len; ++i)
93 printf ("%s%s",
94 alias->alias_members [i],
95 i + 1 == alias->alias_members_len ? "\n" : ", ");
96 }
97
98 static int
99 aliases_keys (int number, char *key[])
100 {
101 int result = 0;
102 int i;
103 struct aliasent *alias;
104
105 if (number == 0)
106 {
107 setaliasent ();
108 while ((alias = getaliasent ()) != NULL)
109 print_aliases (alias);
110 endaliasent ();
111 return result;
112 }
113
114 for (i = 0; i < number; ++i)
115 {
116 alias = getaliasbyname (key[i]);
117
118 if (alias == NULL)
119 result = 2;
120 else
121 print_aliases (alias);
122 }
123
124 return result;
125 }
126
127 /* This is for ethers */
128 static int
129 ethers_keys (int number, char *key[])
130 {
131 int result = 0;
132 int i;
133
134 if (number == 0)
135 {
136 fprintf (stderr, _("Enumeration not supported on %s\n"), "ethers");
137 return 3;
138 }
139
140 for (i = 0; i < number; ++i)
141 {
142 struct ether_addr *ethp, eth;
143 char buffer [1024], *p;
144
145 ethp = ether_aton (key[i]);
146 if (ethp != NULL)
147 {
148 if (ether_ntohost (buffer, ethp))
149 {
150 result = 2;
151 continue;
152 }
153 p = buffer;
154 }
155 else
156 {
157 if (ether_hostton (key[i], &eth))
158 {
159 result = 2;
160 continue;
161 }
162 p = key[i];
163 ethp = &eth;
164 }
165 printf ("%s %s\n", ether_ntoa (ethp), p);
166 }
167
168 return result;
169 }
170
171 /* This is for group */
172 static inline void
173 print_group (struct group *grp)
174 {
175 unsigned int i = 0;
176
177 printf ("%s:%s:%ld:", grp->gr_name ? grp->gr_name : "",
178 grp->gr_passwd ? grp->gr_passwd : "",
179 (unsigned long int) grp->gr_gid);
180
181 while (grp->gr_mem[i] != NULL)
182 {
183 fputs_unlocked (grp->gr_mem[i], stdout);
184 ++i;
185 if (grp->gr_mem[i] != NULL)
186 putchar_unlocked (',');
187 }
188 putchar_unlocked ('\n');
189 }
190
191 static int
192 group_keys (int number, char *key[])
193 {
194 int result = 0;
195 int i;
196 struct group *grp;
197
198 if (number == 0)
199 {
200 setgrent ();
201 while ((grp = getgrent ()) != NULL)
202 print_group (grp);
203 endgrent ();
204 return result;
205 }
206
207 for (i = 0; i < number; ++i)
208 {
209 if (isdigit (key[i][0]))
210 {
211 char *ep;
212 gid_t arg_gid = strtoul (key[i], &ep, 10);
213
214 if (*key[i] != '\0' && *ep == '\0') /* valid numeric uid */
215 grp = getgrgid (arg_gid);
216 else
217 grp = NULL;
218 }
219 else
220 grp = getgrnam (key[i]);
221
222 if (grp == NULL)
223 result = 2;
224 else
225 print_group (grp);
226 }
227
228 return result;
229 }
230
231 /* This is for hosts */
232 static inline void
233 print_hosts (struct hostent *host)
234 {
235 unsigned int i;
236 char buf[INET6_ADDRSTRLEN];
237 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[0],
238 buf, sizeof (buf));
239
240 printf ("%-15s %s", ip, host->h_name);
241
242 i = 0;
243 while (host->h_aliases[i] != NULL)
244 {
245 putchar_unlocked (' ');
246 fputs_unlocked (host->h_aliases[i], stdout);
247 ++i;
248 }
249 putchar_unlocked ('\n');
250 }
251
252 static int
253 hosts_keys (int number, char *key[])
254 {
255 int result = 0;
256 int i;
257 struct hostent *host;
258
259 if (number == 0)
260 {
261 sethostent (0);
262 while ((host = gethostent ()) != NULL)
263 print_hosts (host);
264 endhostent ();
265 return result;
266 }
267
268 for (i = 0; i < number; ++i)
269 {
270 struct hostent *host = NULL;
271
272 if (strchr (key[i], ':') != NULL)
273 {
274 char addr[IN6ADDRSZ];
275 if (inet_pton (AF_INET6, key[i], &addr))
276 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
277 }
278 else if (isdigit (key[i][0]))
279 {
280 char addr[INADDRSZ];
281 if (inet_pton (AF_INET, key[i], &addr))
282 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
283 }
284 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
285 host = gethostbyname2 (key[i], AF_INET);
286
287 if (host == NULL)
288 result = 2;
289 else
290 print_hosts (host);
291 }
292
293 return result;
294 }
295
296 /* This is for netgroup */
297 static int
298 netgroup_keys (int number, char *key[])
299 {
300 int result = 0;
301 int i;
302
303 if (number == 0)
304 {
305 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
306 return 3;
307 }
308
309 for (i = 0; i < number; ++i)
310 {
311 if (!setnetgrent (key[i]))
312 result = 2;
313 else
314 {
315 char *p[3];
316
317 printf ("%-21s", key[i]);
318
319 while (getnetgrent (p, p + 1, p + 2))
320 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
321 putchar_unlocked ('\n');
322 }
323 }
324
325 return result;
326 }
327
328 /* This is for networks */
329 static inline void
330 print_networks (struct netent *net)
331 {
332 unsigned int i;
333 struct in_addr ip;
334 ip.s_addr = htonl (net->n_net);
335
336 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
337
338 i = 0;
339 while (net->n_aliases[i] != NULL)
340 {
341 putchar_unlocked (' ');
342 fputs_unlocked (net->n_aliases[i], stdout);
343 ++i;
344 if (net->n_aliases[i] != NULL)
345 putchar_unlocked (',');
346 }
347 putchar_unlocked ('\n');
348 }
349
350 static int
351 networks_keys (int number, char *key[])
352 {
353 int result = 0;
354 int i;
355 struct netent *net;
356
357 if (number == 0)
358 {
359 setnetent (0);
360 while ((net = getnetent ()) != NULL)
361 print_networks (net);
362 endnetent ();
363 return result;
364 }
365
366 for (i = 0; i < number; ++i)
367 {
368 if (isdigit (key[i][0]))
369 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
370 else
371 net = getnetbyname (key[i]);
372
373 if (net == NULL)
374 result = 2;
375 else
376 print_networks (net);
377 }
378
379 return result;
380 }
381
382 /* Now is all for passwd */
383 static inline void
384 print_passwd (struct passwd *pwd)
385 {
386 printf ("%s:%s:%ld:%ld:%s:%s:%s\n",
387 pwd->pw_name ? pwd->pw_name : "",
388 pwd->pw_passwd ? pwd->pw_passwd : "",
389 (unsigned long int) pwd->pw_uid,
390 (unsigned long int) pwd->pw_gid,
391 pwd->pw_gecos ? pwd->pw_gecos : "",
392 pwd->pw_dir ? pwd->pw_dir : "",
393 pwd->pw_shell ? pwd->pw_shell : "");
394 }
395
396 static int
397 passwd_keys (int number, char *key[])
398 {
399 int result = 0;
400 int i;
401 struct passwd *pwd;
402
403 if (number == 0)
404 {
405 setpwent ();
406 while ((pwd = getpwent ()) != NULL)
407 print_passwd (pwd);
408 endpwent ();
409 return result;
410 }
411
412 for (i = 0; i < number; ++i)
413 {
414 if (isdigit (key[i][0]))
415 {
416 char *ep;
417 uid_t arg_uid = strtoul (key[i], &ep, 10);
418
419 if (*key[i] != '\0' && *ep == '\0') /* valid numeric uid */
420 pwd = getpwuid (arg_uid);
421 else
422 pwd = NULL;
423 }
424 else
425 pwd = getpwnam (key[i]);
426
427 if (pwd == NULL)
428 result = 2;
429 else
430 print_passwd (pwd);
431 }
432
433 return result;
434 }
435
436 /* This is for protocols */
437 static inline void
438 print_protocols (struct protoent *proto)
439 {
440 unsigned int i;
441
442 printf ("%-21s %d", proto->p_name, proto->p_proto);
443
444 i = 0;
445 while (proto->p_aliases[i] != NULL)
446 {
447 putchar_unlocked (' ');
448 fputs_unlocked (proto->p_aliases[i], stdout);
449 ++i;
450 }
451 putchar_unlocked ('\n');
452 }
453
454 static int
455 protocols_keys (int number, char *key[])
456 {
457 int result = 0;
458 int i;
459 struct protoent *proto;
460
461 if (number == 0)
462 {
463 setprotoent (0);
464 while ((proto = getprotoent ()) != NULL)
465 print_protocols (proto);
466 endprotoent ();
467 return result;
468 }
469
470 for (i = 0; i < number; ++i)
471 {
472 if (isdigit (key[i][0]))
473 proto = getprotobynumber (atol (key[i]));
474 else
475 proto = getprotobyname (key[i]);
476
477 if (proto == NULL)
478 result = 2;
479 else
480 print_protocols (proto);
481 }
482
483 return result;
484 }
485
486 /* Now is all for rpc */
487 static inline void
488 print_rpc (struct rpcent *rpc)
489 {
490 int i;
491
492 printf ("%-15s %d%s",
493 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
494
495 for (i = 0; rpc->r_aliases[i]; ++i)
496 printf (" %s", rpc->r_aliases[i]);
497 putchar_unlocked ('\n');
498 }
499
500 static int
501 rpc_keys (int number, char *key[])
502 {
503 int result = 0;
504 int i;
505 struct rpcent *rpc;
506
507 if (number == 0)
508 {
509 setrpcent (0);
510 while ((rpc = getrpcent ()) != NULL)
511 print_rpc (rpc);
512 endrpcent ();
513 return result;
514 }
515
516 for (i = 0; i < number; ++i)
517 {
518 if (isdigit (key[i][0]))
519 rpc = getrpcbynumber (atol (key[i]));
520 else
521 rpc = getrpcbyname (key[i]);
522
523 if (rpc == NULL)
524 result = 2;
525 else
526 print_rpc (rpc);
527 }
528
529 return result;
530 }
531
532 /* for services */
533 static void
534 print_services (struct servent *serv)
535 {
536 unsigned int i;
537
538 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
539
540 i = 0;
541 while (serv->s_aliases[i] != NULL)
542 {
543 putchar_unlocked (' ');
544 fputs_unlocked (serv->s_aliases[i], stdout);
545 ++i;
546 }
547 putchar_unlocked ('\n');
548 }
549
550 static int
551 services_keys (int number, char *key[])
552 {
553 int result = 0;
554 int i;
555 struct servent *serv;
556
557 if (!number)
558 {
559 setservent (0);
560 while ((serv = getservent ()) != NULL)
561 print_services (serv);
562 endservent ();
563 return result;
564 }
565
566 for (i = 0; i < number; ++i)
567 {
568 struct servent *serv;
569 char *proto = strchr (key[i], '/');
570
571 if (proto == NULL)
572 {
573 setservent (0);
574 if (isdigit (key[i][0]))
575 {
576 int port = htons (atol (key[i]));
577 while ((serv = getservent ()) != NULL)
578 if (serv->s_port == port)
579 {
580 print_services (serv);
581 break;
582 }
583 }
584 else
585 {
586 int j;
587
588 while ((serv = getservent ()) != NULL)
589 if (strcmp (serv->s_name, key[i]) == 0)
590 {
591 print_services (serv);
592 break;
593 }
594 else
595 for (j = 0; serv->s_aliases[j]; ++j)
596 if (strcmp (serv->s_aliases[j], key[i]) == 0)
597 {
598 print_services (serv);
599 break;
600 }
601 }
602 endservent ();
603 }
604 else
605 {
606 *proto++ = '\0';
607
608 if (isdigit (key[i][0]))
609 serv = getservbyport (htons (atol (key[i])), proto);
610 else
611 serv = getservbyname (key[i], proto);
612
613 if (serv == NULL)
614 result = 2;
615 else
616 print_services (serv);
617 }
618 }
619
620 return result;
621 }
622
623 /* This is for shadow */
624 static inline void
625 print_shadow (struct spwd *sp)
626 {
627 printf ("%s:%s:",
628 sp->sp_namp ? sp->sp_namp : "",
629 sp->sp_pwdp ? sp->sp_pwdp : "");
630
631 #define SHADOW_FIELD(n) \
632 if (sp->n == -1) \
633 putchar_unlocked (':'); \
634 else \
635 printf ("%ld:", sp->n)
636
637 SHADOW_FIELD (sp_lstchg);
638 SHADOW_FIELD (sp_min);
639 SHADOW_FIELD (sp_max);
640 SHADOW_FIELD (sp_warn);
641 SHADOW_FIELD (sp_inact);
642 SHADOW_FIELD (sp_expire);
643 if (sp->sp_flag == ~0ul)
644 putchar_unlocked ('\n');
645 else
646 printf ("%lu\n", sp->sp_flag);
647 }
648
649 static int
650 shadow_keys (int number, char *key[])
651 {
652 int result = 0;
653 int i;
654
655 if (number == 0)
656 {
657 struct spwd *sp;
658
659 setspent ();
660 while ((sp = getspent ()) != NULL)
661 print_shadow (sp);
662 endpwent ();
663 return result;
664 }
665
666 for (i = 0; i < number; ++i)
667 {
668 struct spwd *sp;
669
670 sp = getspnam (key[i]);
671
672 if (sp == NULL)
673 result = 2;
674 else
675 print_shadow (sp);
676 }
677
678 return result;
679 }
680
681 struct
682 {
683 const char *name;
684 int (*func) (int number, char *key[]);
685 } databases[] =
686 {
687 #define D(name) { #name, name ## _keys },
688 D(aliases)
689 D(ethers)
690 D(group)
691 D(hosts)
692 D(netgroup)
693 D(networks)
694 D(passwd)
695 D(protocols)
696 D(rpc)
697 D(services)
698 D(shadow)
699 #undef D
700 { NULL, NULL }
701 };
702
703 /* Handle arguments found by argp. */
704 static error_t
705 parse_option (int key, char *arg, struct argp_state *state)
706 {
707 int i;
708 switch (key)
709 {
710 case 's':
711 for (i = 0; databases[i].name; ++i)
712 __nss_configure_lookup (databases[i].name, arg);
713 break;
714
715 default:
716 return ARGP_ERR_UNKNOWN;
717 }
718
719 return 0;
720 }
721
722 /* build doc */
723 static inline void
724 build_doc (void)
725 {
726 int i, j, len;
727 char *short_doc, *long_doc, *doc, *p;
728
729 short_doc = _("getent - get entries from administrative database.");
730 long_doc = _("Supported databases:");
731 len = strlen (short_doc) + strlen (long_doc) + 3;
732
733 for (i = 0; databases[i].name; ++i)
734 len += strlen (databases[i].name) + 1;
735
736 doc = (char *) malloc (len);
737 if (doc == NULL)
738 doc = short_doc;
739 else
740 {
741 p = stpcpy (doc, short_doc);
742 *p++ = '\v';
743 p = stpcpy (p, long_doc);
744 *p++ = '\n';
745
746 for (i = 0, j = 0; databases[i].name; ++i)
747 {
748 len = strlen (databases[i].name);
749 if (i != 0)
750 {
751 if (j + len > 72)
752 {
753 j = 0;
754 *p++ = '\n';
755 }
756 else
757 *p++ = ' ';
758 }
759
760 p = mempcpy (p, databases[i].name, len);
761 j += len + 1;
762 }
763 }
764
765 argp.doc = doc;
766 }
767
768 /* the main function */
769 int
770 main (int argc, char *argv[])
771 {
772 int remaining, i;
773
774 /* Set locale via LC_ALL. */
775 setlocale (LC_ALL, "");
776 /* Set the text message domain. */
777 textdomain (PACKAGE);
778
779 /* Build argp.doc. */
780 build_doc ();
781
782 /* Parse and process arguments. */
783 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
784
785 if ((argc - remaining) < 1)
786 {
787 error (0, 0, gettext ("wrong number of arguments"));
788 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
789 return 1;
790 }
791
792 for (i = 0; databases[i].name; ++i)
793 if (argv[remaining][0] == databases[i].name[0]
794 && !strcmp (argv[remaining], databases[i].name))
795 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
796
797 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
798 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
799 return 1;
800 }