]> 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 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 "), "2001");
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 grp = getgrgid (atol (key[i]));
211 else
212 grp = getgrnam (key[i]);
213
214 if (grp == NULL)
215 result = 2;
216 else
217 print_group (grp);
218 }
219
220 return result;
221 }
222
223 /* This is for hosts */
224 static inline void
225 print_hosts (struct hostent *host)
226 {
227 unsigned int i;
228 char buf[INET6_ADDRSTRLEN];
229 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[0],
230 buf, sizeof (buf));
231
232 printf ("%-15s %s", ip, host->h_name);
233
234 i = 0;
235 while (host->h_aliases[i] != NULL)
236 {
237 putchar_unlocked (' ');
238 fputs_unlocked (host->h_aliases[i], stdout);
239 ++i;
240 }
241 putchar_unlocked ('\n');
242 }
243
244 static int
245 hosts_keys (int number, char *key[])
246 {
247 int result = 0;
248 int i;
249 struct hostent *host;
250
251 if (number == 0)
252 {
253 sethostent (0);
254 while ((host = gethostent ()) != NULL)
255 print_hosts (host);
256 endhostent ();
257 return result;
258 }
259
260 for (i = 0; i < number; ++i)
261 {
262 struct hostent *host = NULL;
263
264 if (strchr (key[i], ':') != NULL)
265 {
266 char addr[IN6ADDRSZ];
267 if (inet_pton (AF_INET6, key[i], &addr))
268 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
269 }
270 else if (isdigit (key[i][0]))
271 {
272 char addr[INADDRSZ];
273 if (inet_pton (AF_INET, key[i], &addr))
274 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
275 }
276 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
277 host = gethostbyname2 (key[i], AF_INET);
278
279 if (host == NULL)
280 result = 2;
281 else
282 print_hosts (host);
283 }
284
285 return result;
286 }
287
288 /* This is for netgroup */
289 static int
290 netgroup_keys (int number, char *key[])
291 {
292 int result = 0;
293 int i;
294
295 if (number == 0)
296 {
297 fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup");
298 return 3;
299 }
300
301 for (i = 0; i < number; ++i)
302 {
303 if (!setnetgrent (key[i]))
304 result = 2;
305 else
306 {
307 char *p[3];
308
309 printf ("%-21s", key[i]);
310
311 while (getnetgrent (p, p + 1, p + 2))
312 printf (" (%s, %s, %s)", p[0] ?: " ", p[1] ?: "", p[2] ?: "");
313 putchar_unlocked ('\n');
314 }
315 }
316
317 return result;
318 }
319
320 /* This is for networks */
321 static inline void
322 print_networks (struct netent *net)
323 {
324 unsigned int i;
325 struct in_addr ip;
326 ip.s_addr = htonl (net->n_net);
327
328 printf ("%-21s %s", net->n_name, inet_ntoa (ip));
329
330 i = 0;
331 while (net->n_aliases[i] != NULL)
332 {
333 putchar_unlocked (' ');
334 fputs_unlocked (net->n_aliases[i], stdout);
335 ++i;
336 if (net->n_aliases[i] != NULL)
337 putchar_unlocked (',');
338 }
339 putchar_unlocked ('\n');
340 }
341
342 static int
343 networks_keys (int number, char *key[])
344 {
345 int result = 0;
346 int i;
347 struct netent *net;
348
349 if (number == 0)
350 {
351 setnetent (0);
352 while ((net = getnetent ()) != NULL)
353 print_networks (net);
354 endnetent ();
355 return result;
356 }
357
358 for (i = 0; i < number; ++i)
359 {
360 if (isdigit (key[i][0]))
361 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
362 else
363 net = getnetbyname (key[i]);
364
365 if (net == NULL)
366 result = 2;
367 else
368 print_networks (net);
369 }
370
371 return result;
372 }
373
374 /* Now is all for passwd */
375 static inline void
376 print_passwd (struct passwd *pwd)
377 {
378 printf ("%s:%s:%ld:%ld:%s:%s:%s\n",
379 pwd->pw_name ? pwd->pw_name : "",
380 pwd->pw_passwd ? pwd->pw_passwd : "",
381 (unsigned long int) pwd->pw_uid,
382 (unsigned long int) pwd->pw_gid,
383 pwd->pw_gecos ? pwd->pw_gecos : "",
384 pwd->pw_dir ? pwd->pw_dir : "",
385 pwd->pw_shell ? pwd->pw_shell : "");
386 }
387
388 static int
389 passwd_keys (int number, char *key[])
390 {
391 int result = 0;
392 int i;
393 struct passwd *pwd;
394
395 if (number == 0)
396 {
397 setpwent ();
398 while ((pwd = getpwent ()) != NULL)
399 print_passwd (pwd);
400 endpwent ();
401 return result;
402 }
403
404 for (i = 0; i < number; ++i)
405 {
406 if (isdigit (key[i][0]))
407 pwd = getpwuid (atol (key[i]));
408 else
409 pwd = getpwnam (key[i]);
410
411 if (pwd == NULL)
412 result = 2;
413 else
414 print_passwd (pwd);
415 }
416
417 return result;
418 }
419
420 /* This is for protocols */
421 static inline void
422 print_protocols (struct protoent *proto)
423 {
424 unsigned int i;
425
426 printf ("%-21s %d", proto->p_name, proto->p_proto);
427
428 i = 0;
429 while (proto->p_aliases[i] != NULL)
430 {
431 putchar_unlocked (' ');
432 fputs_unlocked (proto->p_aliases[i], stdout);
433 ++i;
434 }
435 putchar_unlocked ('\n');
436 }
437
438 static int
439 protocols_keys (int number, char *key[])
440 {
441 int result = 0;
442 int i;
443 struct protoent *proto;
444
445 if (number == 0)
446 {
447 setprotoent (0);
448 while ((proto = getprotoent ()) != NULL)
449 print_protocols (proto);
450 endprotoent ();
451 return result;
452 }
453
454 for (i = 0; i < number; ++i)
455 {
456 if (isdigit (key[i][0]))
457 proto = getprotobynumber (atol (key[i]));
458 else
459 proto = getprotobyname (key[i]);
460
461 if (proto == NULL)
462 result = 2;
463 else
464 print_protocols (proto);
465 }
466
467 return result;
468 }
469
470 /* Now is all for rpc */
471 static inline void
472 print_rpc (struct rpcent *rpc)
473 {
474 int i;
475
476 printf ("%-15s %d%s",
477 rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "");
478
479 for (i = 0; rpc->r_aliases[i]; ++i)
480 printf (" %s", rpc->r_aliases[i]);
481 putchar_unlocked ('\n');
482 }
483
484 static int
485 rpc_keys (int number, char *key[])
486 {
487 int result = 0;
488 int i;
489 struct rpcent *rpc;
490
491 if (number == 0)
492 {
493 setrpcent (0);
494 while ((rpc = getrpcent ()) != NULL)
495 print_rpc (rpc);
496 endrpcent ();
497 return result;
498 }
499
500 for (i = 0; i < number; ++i)
501 {
502 if (isdigit (key[i][0]))
503 rpc = getrpcbynumber (atol (key[i]));
504 else
505 rpc = getrpcbyname (key[i]);
506
507 if (rpc == NULL)
508 result = 2;
509 else
510 print_rpc (rpc);
511 }
512
513 return result;
514 }
515
516 /* for services */
517 static void
518 print_services (struct servent *serv)
519 {
520 unsigned int i;
521
522 printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto);
523
524 i = 0;
525 while (serv->s_aliases[i] != NULL)
526 {
527 putchar_unlocked (' ');
528 fputs_unlocked (serv->s_aliases[i], stdout);
529 ++i;
530 }
531 putchar_unlocked ('\n');
532 }
533
534 static int
535 services_keys (int number, char *key[])
536 {
537 int result = 0;
538 int i;
539 struct servent *serv;
540
541 if (!number)
542 {
543 setservent (0);
544 while ((serv = getservent ()) != NULL)
545 print_services (serv);
546 endservent ();
547 return result;
548 }
549
550 for (i = 0; i < number; ++i)
551 {
552 struct servent *serv;
553 char *proto = strchr (key[i], '/');
554
555 if (proto == NULL)
556 {
557 setservent (0);
558 if (isdigit (key[i][0]))
559 {
560 int port = htons (atol (key[i]));
561 while ((serv = getservent ()) != NULL)
562 if (serv->s_port == port)
563 {
564 print_services (serv);
565 break;
566 }
567 }
568 else
569 {
570 int j;
571
572 while ((serv = getservent ()) != NULL)
573 if (strcmp (serv->s_name, key[i]) == 0)
574 {
575 print_services (serv);
576 break;
577 }
578 else
579 for (j = 0; serv->s_aliases[j]; ++j)
580 if (strcmp (serv->s_aliases[j], key[i]) == 0)
581 {
582 print_services (serv);
583 break;
584 }
585 }
586 endservent ();
587 }
588 else
589 {
590 *proto++ = '\0';
591
592 if (isdigit (key[i][0]))
593 serv = getservbyport (htons (atol (key[i])), proto);
594 else
595 serv = getservbyname (key[i], proto);
596
597 if (serv == NULL)
598 result = 2;
599 else
600 print_services (serv);
601 }
602 }
603
604 return result;
605 }
606
607 /* This is for shadow */
608 static inline void
609 print_shadow (struct spwd *sp)
610 {
611 printf ("%s:%s:",
612 sp->sp_namp ? sp->sp_namp : "",
613 sp->sp_pwdp ? sp->sp_pwdp : "");
614
615 #define SHADOW_FIELD(n) \
616 if (sp->n == -1) \
617 putchar_unlocked (':'); \
618 else \
619 printf ("%ld:", sp->n)
620
621 SHADOW_FIELD (sp_lstchg);
622 SHADOW_FIELD (sp_min);
623 SHADOW_FIELD (sp_max);
624 SHADOW_FIELD (sp_warn);
625 SHADOW_FIELD (sp_inact);
626 SHADOW_FIELD (sp_expire);
627 if (sp->sp_flag == ~0ul)
628 putchar_unlocked ('\n');
629 else
630 printf ("%lu\n", sp->sp_flag);
631 }
632
633 static int
634 shadow_keys (int number, char *key[])
635 {
636 int result = 0;
637 int i;
638
639 if (number == 0)
640 {
641 struct spwd *sp;
642
643 setspent ();
644 while ((sp = getspent ()) != NULL)
645 print_shadow (sp);
646 endpwent ();
647 return result;
648 }
649
650 for (i = 0; i < number; ++i)
651 {
652 struct spwd *sp;
653
654 sp = getspnam (key[i]);
655
656 if (sp == NULL)
657 result = 2;
658 else
659 print_shadow (sp);
660 }
661
662 return result;
663 }
664
665 struct
666 {
667 const char *name;
668 int (*func) (int number, char *key[]);
669 } databases[] =
670 {
671 #define D(name) { #name, name ## _keys },
672 D(aliases)
673 D(ethers)
674 D(group)
675 D(hosts)
676 D(netgroup)
677 D(networks)
678 D(passwd)
679 D(protocols)
680 D(rpc)
681 D(services)
682 D(shadow)
683 #undef D
684 { NULL, NULL }
685 };
686
687 /* Handle arguments found by argp. */
688 static error_t
689 parse_option (int key, char *arg, struct argp_state *state)
690 {
691 int i;
692 switch (key)
693 {
694 case 's':
695 for (i = 0; databases[i].name; ++i)
696 __nss_configure_lookup (databases[i].name, arg);
697 break;
698
699 default:
700 return ARGP_ERR_UNKNOWN;
701 }
702
703 return 0;
704 }
705
706 /* build doc */
707 static inline void
708 build_doc (void)
709 {
710 int i, j, len;
711 char *short_doc, *long_doc, *doc, *p;
712
713 short_doc = _("getent - get entries from administrative database.");
714 long_doc = _("Supported databases:");
715 len = strlen (short_doc) + strlen (long_doc) + 3;
716
717 for (i = 0; databases[i].name; ++i)
718 len += strlen (databases[i].name) + 1;
719
720 doc = (char *) malloc (len);
721 if (doc == NULL)
722 doc = short_doc;
723 else
724 {
725 p = stpcpy (doc, short_doc);
726 *p++ = '\v';
727 p = stpcpy (p, long_doc);
728 *p++ = '\n';
729
730 for (i = 0, j = 0; databases[i].name; ++i)
731 {
732 len = strlen (databases[i].name);
733 if (i != 0)
734 {
735 if (j + len > 72)
736 {
737 j = 0;
738 *p++ = '\n';
739 }
740 else
741 *p++ = ' ';
742 }
743
744 p = mempcpy (p, databases[i].name, len);
745 j += len + 1;
746 }
747 }
748
749 argp.doc = doc;
750 }
751
752 /* the main function */
753 int
754 main (int argc, char *argv[])
755 {
756 int remaining, i;
757
758 /* Set locale via LC_ALL. */
759 setlocale (LC_ALL, "");
760 /* Set the text message domain. */
761 textdomain (PACKAGE);
762
763 /* Build argp.doc. */
764 build_doc ();
765
766 /* Parse and process arguments. */
767 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
768
769 if ((argc - remaining) < 1)
770 {
771 error (0, 0, gettext ("wrong number of arguments"));
772 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
773 return 1;
774 }
775
776 for (i = 0; databases[i].name; ++i)
777 if (argv[remaining][0] == databases[i].name[0]
778 && !strcmp (argv[remaining], databases[i].name))
779 return databases[i].func (argc - remaining - 1, &argv[remaining + 1]);
780
781 fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]);
782 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
783 return 1;
784 }