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