1 | /* Copyright (c) 1998-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | /* getent: get entries from administrative database. */ |
19 | |
20 | #include <aliases.h> |
21 | #include <argp.h> |
22 | #include <ctype.h> |
23 | #include <error.h> |
24 | #include <grp.h> |
25 | #include <gshadow.h> |
26 | #include <libintl.h> |
27 | #include <locale.h> |
28 | #include <mcheck.h> |
29 | #include <netdb.h> |
30 | #include <pwd.h> |
31 | #include <shadow.h> |
32 | #include <stdbool.h> |
33 | #include <stdio.h> |
34 | #include <stdlib.h> |
35 | #include <string.h> |
36 | #include <arpa/inet.h> |
37 | #include <arpa/nameser.h> |
38 | #include <netinet/ether.h> |
39 | #include <netinet/in.h> |
40 | #include <sys/socket.h> |
41 | #include <scratch_buffer.h> |
42 | #include <inttypes.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 | " ), "2022" ); |
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 | /* Three digits per byte, plus '%' and null terminator. */ |
397 | char scope[3 * sizeof (uint32_t) + 2]; |
398 | struct sockaddr_in6 *addr6 |
399 | = (struct sockaddr_in6 *) runp->ai_addr; |
400 | if (runp->ai_family != AF_INET6 || addr6->sin6_scope_id == 0) |
401 | /* No scope ID present. */ |
402 | scope[0] = '\0'; |
403 | else |
404 | snprintf (scope, sizeof (scope), "%%%" PRIu32, |
405 | addr6->sin6_scope_id); |
406 | |
407 | char buf[INET6_ADDRSTRLEN]; |
408 | if (inet_ntop (runp->ai_family, |
409 | runp->ai_family == AF_INET |
410 | ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr |
411 | : &addr6->sin6_addr, |
412 | buf, sizeof (buf)) == NULL) |
413 | { |
414 | strcpy (buf, "<invalid>" ); |
415 | scope[0] = '\0'; |
416 | } |
417 | |
418 | int pad = 15 - strlen (buf) - strlen (scope); |
419 | if (pad < 0) |
420 | pad = 0; |
421 | |
422 | printf ("%s%-*s %-6s %s\n" , |
423 | buf, pad, scope, sockstr, runp->ai_canonname ?: "" ); |
424 | |
425 | runp = runp->ai_next; |
426 | } |
427 | |
428 | freeaddrinfo (res); |
429 | } |
430 | } |
431 | |
432 | return result; |
433 | } |
434 | |
435 | static int |
436 | ahosts_keys (int number, char *key[]) |
437 | { |
438 | return ahosts_keys_int (AF_UNSPEC, 0, number, key); |
439 | } |
440 | |
441 | static int |
442 | ahostsv4_keys (int number, char *key[]) |
443 | { |
444 | return ahosts_keys_int (AF_INET, 0, number, key); |
445 | } |
446 | |
447 | static int |
448 | ahostsv6_keys (int number, char *key[]) |
449 | { |
450 | return ahosts_keys_int (AF_INET6, AI_V4MAPPED, number, key); |
451 | } |
452 | |
453 | /* This is for netgroup */ |
454 | static int |
455 | netgroup_keys (int number, char *key[]) |
456 | { |
457 | int result = 0; |
458 | |
459 | if (number == 0) |
460 | { |
461 | fprintf (stderr, _("Enumeration not supported on %s\n" ), "netgroup" ); |
462 | return 3; |
463 | } |
464 | |
465 | if (number == 4) |
466 | { |
467 | char *host = strcmp (key[1], "*" ) == 0 ? NULL : key[1]; |
468 | char *user = strcmp (key[2], "*" ) == 0 ? NULL : key[2]; |
469 | char *domain = strcmp (key[3], "*" ) == 0 ? NULL : key[3]; |
470 | |
471 | printf ("%-21s (%s,%s,%s) = %d\n" , |
472 | key[0], host ?: "" , user ?: "" , domain ?: "" , |
473 | innetgr (key[0], host, user, domain)); |
474 | } |
475 | else if (number == 1) |
476 | { |
477 | if (!setnetgrent (key[0])) |
478 | result = 2; |
479 | else |
480 | { |
481 | char *p[3]; |
482 | |
483 | printf ("%-21s" , key[0]); |
484 | |
485 | while (getnetgrent (p, p + 1, p + 2)) |
486 | printf (" (%s,%s,%s)" , p[0] ?: " " , p[1] ?: "" , p[2] ?: "" ); |
487 | putchar_unlocked ('\n'); |
488 | } |
489 | } |
490 | |
491 | endnetgrent (); |
492 | |
493 | return result; |
494 | } |
495 | |
496 | #define DYNARRAY_STRUCT gid_list |
497 | #define DYNARRAY_ELEMENT gid_t |
498 | #define DYNARRAY_PREFIX gid_list_ |
499 | #define DYNARRAY_INITIAL_SIZE 10 |
500 | #include <malloc/dynarray-skeleton.c> |
501 | |
502 | /* This is for initgroups */ |
503 | static int |
504 | initgroups_keys (int number, char *key[]) |
505 | { |
506 | if (number == 0) |
507 | { |
508 | fprintf (stderr, _("Enumeration not supported on %s\n" ), "initgroups" ); |
509 | return 3; |
510 | } |
511 | |
512 | struct gid_list list; |
513 | gid_list_init (&list); |
514 | if (!gid_list_resize (&list, 10)) |
515 | { |
516 | fprintf (stderr, _("Could not allocate group list: %m\n" )); |
517 | return 3; |
518 | } |
519 | |
520 | for (int i = 0; i < number; ++i) |
521 | { |
522 | int no = gid_list_size (&list); |
523 | int n; |
524 | while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1 |
525 | && no > gid_list_size (&list)) |
526 | { |
527 | if (!gid_list_resize (&list, no)) |
528 | { |
529 | fprintf (stderr, _("Could not allocate group list: %m\n" )); |
530 | return 3; |
531 | } |
532 | } |
533 | |
534 | if (n == -1) |
535 | { |
536 | gid_list_free (&list); |
537 | return 1; |
538 | } |
539 | |
540 | const gid_t *grps = gid_list_begin (&list); |
541 | printf ("%-21s" , key[i]); |
542 | for (int j = 0; j < n; ++j) |
543 | if (grps[j] != -1) |
544 | printf (" %ld" , (long int) grps[j]); |
545 | putchar_unlocked ('\n'); |
546 | } |
547 | |
548 | gid_list_free (&list); |
549 | |
550 | return 0; |
551 | } |
552 | |
553 | /* This is for networks */ |
554 | static void |
555 | print_networks (struct netent *net) |
556 | { |
557 | unsigned int i; |
558 | struct in_addr ip; |
559 | ip.s_addr = htonl (net->n_net); |
560 | |
561 | printf ("%-21s %s" , net->n_name, inet_ntoa (ip)); |
562 | |
563 | i = 0; |
564 | while (net->n_aliases[i] != NULL) |
565 | { |
566 | putchar_unlocked (' '); |
567 | fputs_unlocked (net->n_aliases[i], stdout); |
568 | ++i; |
569 | } |
570 | putchar_unlocked ('\n'); |
571 | } |
572 | |
573 | static int |
574 | networks_keys (int number, char *key[]) |
575 | { |
576 | int result = 0; |
577 | int i; |
578 | struct netent *net; |
579 | |
580 | if (number == 0) |
581 | { |
582 | setnetent (0); |
583 | while ((net = getnetent ()) != NULL) |
584 | print_networks (net); |
585 | endnetent (); |
586 | return result; |
587 | } |
588 | |
589 | for (i = 0; i < number; ++i) |
590 | { |
591 | if (isdigit (key[i][0])) |
592 | net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC); |
593 | else |
594 | net = getnetbyname (key[i]); |
595 | |
596 | if (net == NULL) |
597 | result = 2; |
598 | else |
599 | print_networks (net); |
600 | } |
601 | |
602 | return result; |
603 | } |
604 | |
605 | /* Now is all for passwd */ |
606 | static void |
607 | print_passwd (struct passwd *pwd) |
608 | { |
609 | if (putpwent (pwd, stdout) != 0) |
610 | fprintf (stderr, "error writing passwd entry: %m\n" ); |
611 | } |
612 | |
613 | static int |
614 | passwd_keys (int number, char *key[]) |
615 | { |
616 | int result = 0; |
617 | int i; |
618 | struct passwd *pwd; |
619 | |
620 | if (number == 0) |
621 | { |
622 | setpwent (); |
623 | while ((pwd = getpwent ()) != NULL) |
624 | print_passwd (pwd); |
625 | endpwent (); |
626 | return result; |
627 | } |
628 | |
629 | for (i = 0; i < number; ++i) |
630 | { |
631 | errno = 0; |
632 | char *ep; |
633 | uid_t arg_uid = strtoul(key[i], &ep, 10); |
634 | |
635 | if (errno != EINVAL && *key[i] != '\0' && *ep == '\0') |
636 | /* Valid numeric uid. */ |
637 | pwd = getpwuid (arg_uid); |
638 | else |
639 | pwd = getpwnam (key[i]); |
640 | |
641 | if (pwd == NULL) |
642 | result = 2; |
643 | else |
644 | print_passwd (pwd); |
645 | } |
646 | |
647 | return result; |
648 | } |
649 | |
650 | /* This is for protocols */ |
651 | static void |
652 | print_protocols (struct protoent *proto) |
653 | { |
654 | unsigned int i; |
655 | |
656 | printf ("%-21s %d" , proto->p_name, proto->p_proto); |
657 | |
658 | i = 0; |
659 | while (proto->p_aliases[i] != NULL) |
660 | { |
661 | putchar_unlocked (' '); |
662 | fputs_unlocked (proto->p_aliases[i], stdout); |
663 | ++i; |
664 | } |
665 | putchar_unlocked ('\n'); |
666 | } |
667 | |
668 | static int |
669 | protocols_keys (int number, char *key[]) |
670 | { |
671 | int result = 0; |
672 | int i; |
673 | struct protoent *proto; |
674 | |
675 | if (number == 0) |
676 | { |
677 | setprotoent (0); |
678 | while ((proto = getprotoent ()) != NULL) |
679 | print_protocols (proto); |
680 | endprotoent (); |
681 | return result; |
682 | } |
683 | |
684 | for (i = 0; i < number; ++i) |
685 | { |
686 | if (isdigit (key[i][0])) |
687 | proto = getprotobynumber (atol (key[i])); |
688 | else |
689 | proto = getprotobyname (key[i]); |
690 | |
691 | if (proto == NULL) |
692 | result = 2; |
693 | else |
694 | print_protocols (proto); |
695 | } |
696 | |
697 | return result; |
698 | } |
699 | |
700 | #if HAVE_SUNRPC |
701 | /* Now is all for rpc */ |
702 | static void |
703 | print_rpc (struct rpcent *rpc) |
704 | { |
705 | int i; |
706 | |
707 | printf ("%-15s %d%s" , |
708 | rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : "" ); |
709 | |
710 | for (i = 0; rpc->r_aliases[i]; ++i) |
711 | printf (" %s" , rpc->r_aliases[i]); |
712 | putchar_unlocked ('\n'); |
713 | } |
714 | |
715 | static int |
716 | rpc_keys (int number, char *key[]) |
717 | { |
718 | int result = 0; |
719 | int i; |
720 | struct rpcent *rpc; |
721 | |
722 | if (number == 0) |
723 | { |
724 | setrpcent (0); |
725 | while ((rpc = getrpcent ()) != NULL) |
726 | print_rpc (rpc); |
727 | endrpcent (); |
728 | return result; |
729 | } |
730 | |
731 | for (i = 0; i < number; ++i) |
732 | { |
733 | if (isdigit (key[i][0])) |
734 | rpc = getrpcbynumber (atol (key[i])); |
735 | else |
736 | rpc = getrpcbyname (key[i]); |
737 | |
738 | if (rpc == NULL) |
739 | result = 2; |
740 | else |
741 | print_rpc (rpc); |
742 | } |
743 | |
744 | return result; |
745 | } |
746 | #endif |
747 | |
748 | /* for services */ |
749 | static void |
750 | print_services (struct servent *serv) |
751 | { |
752 | unsigned int i; |
753 | |
754 | printf ("%-21s %d/%s" , serv->s_name, ntohs (serv->s_port), serv->s_proto); |
755 | |
756 | i = 0; |
757 | while (serv->s_aliases[i] != NULL) |
758 | { |
759 | putchar_unlocked (' '); |
760 | fputs_unlocked (serv->s_aliases[i], stdout); |
761 | ++i; |
762 | } |
763 | putchar_unlocked ('\n'); |
764 | } |
765 | |
766 | static int |
767 | services_keys (int number, char *key[]) |
768 | { |
769 | int result = 0; |
770 | int i; |
771 | struct servent *serv; |
772 | |
773 | if (!number) |
774 | { |
775 | setservent (0); |
776 | while ((serv = getservent ()) != NULL) |
777 | print_services (serv); |
778 | endservent (); |
779 | return result; |
780 | } |
781 | |
782 | for (i = 0; i < number; ++i) |
783 | { |
784 | struct servent *serv; |
785 | char *proto = strchr (key[i], '/'); |
786 | |
787 | if (proto != NULL) |
788 | *proto++ = '\0'; |
789 | |
790 | char *endptr; |
791 | long port = strtol (key[i], &endptr, 10); |
792 | |
793 | if (isdigit (key[i][0]) && *endptr == '\0' |
794 | && 0 <= port && port <= 65535) |
795 | serv = getservbyport (htons (port), proto); |
796 | else |
797 | serv = getservbyname (key[i], proto); |
798 | |
799 | if (serv == NULL) |
800 | result = 2; |
801 | else |
802 | print_services (serv); |
803 | } |
804 | |
805 | return result; |
806 | } |
807 | |
808 | /* This is for shadow */ |
809 | static void |
810 | print_shadow (struct spwd *sp) |
811 | { |
812 | if (putspent (sp, stdout) != 0) |
813 | fprintf (stderr, "error writing shadow entry: %m\n" ); |
814 | } |
815 | |
816 | static int |
817 | shadow_keys (int number, char *key[]) |
818 | { |
819 | int result = 0; |
820 | int i; |
821 | |
822 | if (number == 0) |
823 | { |
824 | struct spwd *sp; |
825 | |
826 | setspent (); |
827 | while ((sp = getspent ()) != NULL) |
828 | print_shadow (sp); |
829 | endspent (); |
830 | return result; |
831 | } |
832 | |
833 | for (i = 0; i < number; ++i) |
834 | { |
835 | struct spwd *sp; |
836 | |
837 | sp = getspnam (key[i]); |
838 | |
839 | if (sp == NULL) |
840 | result = 2; |
841 | else |
842 | print_shadow (sp); |
843 | } |
844 | |
845 | return result; |
846 | } |
847 | |
848 | struct |
849 | { |
850 | const char *name; |
851 | int (*func) (int number, char *key[]); |
852 | } databases[] = |
853 | { |
854 | #define D(name) { #name, name ## _keys }, |
855 | D(ahosts) |
856 | D(ahostsv4) |
857 | D(ahostsv6) |
858 | D(aliases) |
859 | D(ethers) |
860 | D(group) |
861 | D(gshadow) |
862 | D(hosts) |
863 | D(initgroups) |
864 | D(netgroup) |
865 | D(networks) |
866 | D(passwd) |
867 | D(protocols) |
868 | #if HAVE_SUNRPC |
869 | D(rpc) |
870 | #endif |
871 | D(services) |
872 | D(shadow) |
873 | #undef D |
874 | { NULL, NULL } |
875 | }; |
876 | |
877 | /* Handle arguments found by argp. */ |
878 | static error_t |
879 | parse_option (int key, char *arg, struct argp_state *state) |
880 | { |
881 | char *endp; |
882 | switch (key) |
883 | { |
884 | case 's': |
885 | endp = strchr (arg, ':'); |
886 | if (endp == NULL) |
887 | /* No specific database, change them all. */ |
888 | for (int i = 0; databases[i].name != NULL; ++i) |
889 | __nss_configure_lookup (databases[i].name, arg); |
890 | else |
891 | { |
892 | int i; |
893 | for (i = 0; databases[i].name != NULL; ++i) |
894 | if (strncmp (databases[i].name, arg, endp - arg) == 0) |
895 | { |
896 | __nss_configure_lookup (databases[i].name, endp + 1); |
897 | break; |
898 | } |
899 | if (databases[i].name == NULL) |
900 | error (EXIT_FAILURE, 0, gettext ("Unknown database name" )); |
901 | } |
902 | break; |
903 | |
904 | case 'i': |
905 | idn_flags = 0; |
906 | break; |
907 | |
908 | default: |
909 | return ARGP_ERR_UNKNOWN; |
910 | } |
911 | |
912 | return 0; |
913 | } |
914 | |
915 | |
916 | static char * |
917 | more_help (int key, const char *text, void *input) |
918 | { |
919 | switch (key) |
920 | { |
921 | size_t len; |
922 | char *doc; |
923 | FILE *fp; |
924 | |
925 | case ARGP_KEY_HELP_EXTRA: |
926 | /* We print some extra information. */ |
927 | fp = open_memstream (&doc, &len); |
928 | if (fp != NULL) |
929 | { |
930 | fputs_unlocked (_("Supported databases:\n" ), fp); |
931 | |
932 | for (int i = 0, col = 0; databases[i].name != NULL; ++i) |
933 | { |
934 | len = strlen (databases[i].name); |
935 | if (i != 0) |
936 | { |
937 | if (col + len > 72) |
938 | { |
939 | col = 0; |
940 | fputc_unlocked ('\n', fp); |
941 | } |
942 | else |
943 | fputc_unlocked (' ', fp); |
944 | } |
945 | |
946 | fputs_unlocked (databases[i].name, fp); |
947 | col += len + 1; |
948 | } |
949 | |
950 | fputs ("\n\n" , fp); |
951 | |
952 | fprintf (fp, gettext ("\ |
953 | For bug reporting instructions, please see:\n\ |
954 | %s.\n" ), REPORT_BUGS_TO); |
955 | |
956 | if (fclose (fp) == 0) |
957 | return doc; |
958 | } |
959 | break; |
960 | |
961 | default: |
962 | break; |
963 | } |
964 | return (char *) text; |
965 | } |
966 | |
967 | |
968 | /* the main function */ |
969 | int |
970 | main (int argc, char *argv[]) |
971 | { |
972 | /* Debugging support. */ |
973 | mtrace (); |
974 | |
975 | /* Set locale via LC_ALL. */ |
976 | setlocale (LC_ALL, "" ); |
977 | /* Set the text message domain. */ |
978 | textdomain (PACKAGE); |
979 | |
980 | /* Parse and process arguments. */ |
981 | int remaining; |
982 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
983 | |
984 | if ((argc - remaining) < 1) |
985 | { |
986 | error (0, 0, gettext ("wrong number of arguments" )); |
987 | argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name); |
988 | return 1; |
989 | } |
990 | |
991 | for (int i = 0; databases[i].name; ++i) |
992 | if (argv[remaining][0] == databases[i].name[0] |
993 | && !strcmp (argv[remaining], databases[i].name)) |
994 | return databases[i].func (argc - remaining - 1, &argv[remaining + 1]); |
995 | |
996 | fprintf (stderr, _("Unknown database: %s\n" ), argv[remaining]); |
997 | argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name); |
998 | return 1; |
999 | } |
1000 | |