]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (c) 1998-2019 Free Software Foundation, Inc. |
7d6a8338 | 2 | This file is part of the GNU C Library. |
ce75c139 | 3 | Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998. |
7d6a8338 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
7d6a8338 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
7d6a8338 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
7d6a8338 | 18 | |
232b4655 | 19 | /* getent: get entries from administrative database. */ |
7d6a8338 | 20 | |
232b4655 | 21 | #include <aliases.h> |
7d6a8338 | 22 | #include <argp.h> |
7d6a8338 UD |
23 | #include <ctype.h> |
24 | #include <error.h> | |
ff3cacc5 | 25 | #include <grp.h> |
c518f9a4 | 26 | #include <gshadow.h> |
7d6a8338 UD |
27 | #include <libintl.h> |
28 | #include <locale.h> | |
ff3cacc5 | 29 | #include <mcheck.h> |
7d6a8338 | 30 | #include <netdb.h> |
ff3cacc5 UD |
31 | #include <pwd.h> |
32 | #include <shadow.h> | |
a160f8d8 | 33 | #include <stdbool.h> |
7d6a8338 UD |
34 | #include <stdio.h> |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
7d6a8338 | 37 | #include <arpa/inet.h> |
ce75c139 | 38 | #include <arpa/nameser.h> |
ff3cacc5 UD |
39 | #include <netinet/ether.h> |
40 | #include <netinet/in.h> | |
41 | #include <sys/socket.h> | |
1599ed4e | 42 | #include <scratch_buffer.h> |
7d6a8338 UD |
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 | ||
9d0881aa UD |
56 | /* Supported options. */ |
57 | static const struct argp_option args_options[] = | |
58 | { | |
0e2b9cdd | 59 | { "service", 's', N_("CONFIG"), 0, N_("Service configuration to be used") }, |
a160f8d8 | 60 | { "no-idn", 'i', NULL, 0, N_("disable IDN encoding") }, |
9d0881aa UD |
61 | { NULL, 0, NULL, 0, NULL }, |
62 | }; | |
63 | ||
f69425fa | 64 | /* Short description of program. */ |
cbbcaf23 | 65 | static const char doc[] = N_("Get entries from administrative database."); |
f69425fa | 66 | |
9d0881aa UD |
67 | /* Prototype for option handler. */ |
68 | static error_t parse_option (int key, char *arg, struct argp_state *state); | |
69 | ||
f69425fa UD |
70 | /* Function to print some extra text in the help message. */ |
71 | static char *more_help (int key, const char *text, void *input); | |
72 | ||
7d6a8338 | 73 | /* Data structure to communicate with argp functions. */ |
9d0881aa UD |
74 | static struct argp argp = |
75 | { | |
f69425fa | 76 | args_options, parse_option, args_doc, doc, NULL, more_help |
9d0881aa | 77 | }; |
7d6a8338 | 78 | |
a160f8d8 UD |
79 | /* Additional getaddrinfo flags for IDN encoding. */ |
80 | static int idn_flags = AI_IDN | AI_CANONIDN; | |
81 | ||
7d6a8338 UD |
82 | /* Print the version information. */ |
83 | static void | |
84 | print_version (FILE *stream, struct argp_state *state) | |
85 | { | |
8b748aed | 86 | fprintf (stream, "getent %s%s\n", PKGVERSION, VERSION); |
7d6a8338 UD |
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\ | |
09533208 | 91 | "), "2018"); |
7d6a8338 UD |
92 | fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk"); |
93 | } | |
94 | ||
232b4655 | 95 | /* This is for aliases */ |
f1d70dad | 96 | static void |
232b4655 UD |
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) | |
9d0881aa | 103 | fputs_unlocked (" ", stdout); |
232b4655 | 104 | |
9d0881aa | 105 | for (i = 0; i < alias->alias_members_len; ++i) |
232b4655 UD |
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 | ||
9d0881aa | 118 | if (number == 0) |
232b4655 UD |
119 | { |
120 | setaliasent (); | |
9d0881aa | 121 | while ((alias = getaliasent ()) != NULL) |
232b4655 UD |
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 | ||
9d0881aa | 147 | if (number == 0) |
232b4655 UD |
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]); | |
9d0881aa | 159 | if (ethp != NULL) |
232b4655 UD |
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 | ||
7d6a8338 | 184 | /* This is for group */ |
f1d70dad | 185 | static void |
7d6a8338 UD |
186 | print_group (struct group *grp) |
187 | { | |
676599b3 FW |
188 | if (putgrent (grp, stdout) != 0) |
189 | fprintf (stderr, "error writing group entry: %m\n"); | |
7d6a8338 UD |
190 | } |
191 | ||
232b4655 | 192 | static int |
7d6a8338 UD |
193 | group_keys (int number, char *key[]) |
194 | { | |
195 | int result = 0; | |
196 | int i; | |
232b4655 | 197 | struct group *grp; |
7d6a8338 | 198 | |
9d0881aa | 199 | if (number == 0) |
7d6a8338 | 200 | { |
232b4655 | 201 | setgrent (); |
9d0881aa | 202 | while ((grp = getgrent ()) != NULL) |
232b4655 UD |
203 | print_group (grp); |
204 | endgrent (); | |
205 | return result; | |
206 | } | |
7d6a8338 | 207 | |
232b4655 UD |
208 | for (i = 0; i < number; ++i) |
209 | { | |
9030e7c4 UD |
210 | errno = 0; |
211 | char *ep; | |
212 | gid_t arg_gid = strtoul(key[i], &ep, 10); | |
8e9b2075 | 213 | |
9030e7c4 UD |
214 | if (errno != EINVAL && *key[i] != '\0' && *ep == '\0') |
215 | /* Valid numeric gid. */ | |
216 | grp = getgrgid (arg_gid); | |
7d6a8338 UD |
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 | ||
c518f9a4 AJ |
229 | /* This is for gshadow */ |
230 | static void | |
231 | print_gshadow (struct sgrp *sg) | |
232 | { | |
676599b3 FW |
233 | if (putsgent (sg, stdout) != 0) |
234 | fprintf (stderr, "error writing gshadow entry: %m\n"); | |
c518f9a4 AJ |
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 | ||
232b4655 | 269 | /* This is for hosts */ |
25337753 | 270 | static void |
232b4655 UD |
271 | print_hosts (struct hostent *host) |
272 | { | |
fdcd8f9c | 273 | unsigned int cnt; |
232b4655 | 274 | |
fdcd8f9c | 275 | for (cnt = 0; host->h_addr_list[cnt] != NULL; ++cnt) |
232b4655 | 276 | { |
fdcd8f9c UD |
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'); | |
232b4655 | 290 | } |
232b4655 UD |
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 | ||
9d0881aa | 300 | if (number == 0) |
232b4655 UD |
301 | { |
302 | sethostent (0); | |
9d0881aa | 303 | while ((host = gethostent ()) != NULL) |
232b4655 UD |
304 | print_hosts (host); |
305 | endhostent (); | |
306 | return result; | |
307 | } | |
308 | ||
309 | for (i = 0; i < number; ++i) | |
310 | { | |
311 | struct hostent *host = NULL; | |
653aeda5 | 312 | char addr[IN6ADDRSZ]; |
232b4655 | 313 | |
653aeda5 | 314 | if (inet_pton (AF_INET6, key[i], &addr) > 0) |
4f3d6536 | 315 | host = gethostbyaddr (addr, IN6ADDRSZ, AF_INET6); |
653aeda5 | 316 | else if (inet_pton (AF_INET, key[i], &addr) > 0) |
4f3d6536 | 317 | host = gethostbyaddr (addr, INADDRSZ, AF_INET); |
232b4655 UD |
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 | ||
29bfc945 UD |
330 | /* This is for hosts, but using getaddrinfo */ |
331 | static int | |
a7d24833 | 332 | ahosts_keys_int (int af, int xflags, int number, char *key[]) |
29bfc945 UD |
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)); | |
a160f8d8 UD |
349 | hint.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG | AI_CANONNAME |
350 | | idn_flags | xflags); | |
a7d24833 | 351 | hint.ai_family = af; |
29bfc945 UD |
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"; | |
372bfcac UD |
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 | |
29bfc945 UD |
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, | |
a691b217 UD |
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, | |
29bfc945 UD |
402 | buf, sizeof (buf)), |
403 | sockstr, | |
5c81b3e1 | 404 | runp->ai_canonname ?: ""); |
29bfc945 UD |
405 | |
406 | runp = runp->ai_next; | |
407 | } | |
408 | ||
409 | freeaddrinfo (res); | |
410 | } | |
411 | } | |
412 | ||
413 | return result; | |
414 | } | |
415 | ||
a7d24833 UD |
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 | ||
232b4655 UD |
434 | /* This is for netgroup */ |
435 | static int | |
436 | netgroup_keys (int number, char *key[]) | |
437 | { | |
438 | int result = 0; | |
232b4655 | 439 | |
9d0881aa | 440 | if (number == 0) |
232b4655 UD |
441 | { |
442 | fprintf (stderr, _("Enumeration not supported on %s\n"), "netgroup"); | |
443 | return 3; | |
444 | } | |
445 | ||
684ae515 UD |
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) | |
232b4655 | 457 | { |
684ae515 | 458 | if (!setnetgrent (key[0])) |
232b4655 UD |
459 | result = 2; |
460 | else | |
461 | { | |
462 | char *p[3]; | |
463 | ||
684ae515 | 464 | printf ("%-21s", key[0]); |
232b4655 UD |
465 | |
466 | while (getnetgrent (p, p + 1, p + 2)) | |
684ae515 | 467 | printf (" (%s,%s,%s)", p[0] ?: " ", p[1] ?: "", p[2] ?: ""); |
9d0881aa | 468 | putchar_unlocked ('\n'); |
232b4655 UD |
469 | } |
470 | } | |
471 | ||
ff3cacc5 UD |
472 | endnetgrent (); |
473 | ||
232b4655 UD |
474 | return result; |
475 | } | |
476 | ||
1599ed4e FW |
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 | ||
82e9a1f7 UD |
483 | /* This is for initgroups */ |
484 | static int | |
485 | initgroups_keys (int number, char *key[]) | |
486 | { | |
f4ec4833 UD |
487 | if (number == 0) |
488 | { | |
489 | fprintf (stderr, _("Enumeration not supported on %s\n"), "initgroups"); | |
490 | return 3; | |
491 | } | |
492 | ||
1599ed4e FW |
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 | ||
82e9a1f7 UD |
501 | for (int i = 0; i < number; ++i) |
502 | { | |
1599ed4e | 503 | int no = gid_list_size (&list); |
82e9a1f7 | 504 | int n; |
1599ed4e FW |
505 | while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1 |
506 | && no > gid_list_size (&list)) | |
82e9a1f7 | 507 | { |
1599ed4e FW |
508 | if (!gid_list_resize (&list, no)) |
509 | { | |
510 | fprintf (stderr, _("Could not allocate group list: %m\n")); | |
511 | return 3; | |
512 | } | |
82e9a1f7 UD |
513 | } |
514 | ||
515 | if (n == -1) | |
1599ed4e FW |
516 | { |
517 | gid_list_free (&list); | |
518 | return 1; | |
519 | } | |
82e9a1f7 | 520 | |
1599ed4e | 521 | const gid_t *grps = gid_list_begin (&list); |
82e9a1f7 UD |
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 | ||
1599ed4e FW |
529 | gid_list_free (&list); |
530 | ||
82e9a1f7 UD |
531 | return 0; |
532 | } | |
533 | ||
7d6a8338 | 534 | /* This is for networks */ |
25337753 | 535 | static void |
7d6a8338 UD |
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 | ||
9d0881aa | 542 | printf ("%-21s %s", net->n_name, inet_ntoa (ip)); |
7d6a8338 UD |
543 | |
544 | i = 0; | |
545 | while (net->n_aliases[i] != NULL) | |
546 | { | |
9d0881aa UD |
547 | putchar_unlocked (' '); |
548 | fputs_unlocked (net->n_aliases[i], stdout); | |
7d6a8338 | 549 | ++i; |
7d6a8338 | 550 | } |
9d0881aa | 551 | putchar_unlocked ('\n'); |
7d6a8338 UD |
552 | } |
553 | ||
232b4655 | 554 | static int |
7d6a8338 UD |
555 | networks_keys (int number, char *key[]) |
556 | { | |
557 | int result = 0; | |
558 | int i; | |
232b4655 | 559 | struct netent *net; |
7d6a8338 | 560 | |
9d0881aa | 561 | if (number == 0) |
7d6a8338 | 562 | { |
232b4655 | 563 | setnetent (0); |
9d0881aa | 564 | while ((net = getnetent ()) != NULL) |
232b4655 UD |
565 | print_networks (net); |
566 | endnetent (); | |
567 | return result; | |
568 | } | |
7d6a8338 | 569 | |
232b4655 UD |
570 | for (i = 0; i < number; ++i) |
571 | { | |
7d6a8338 | 572 | if (isdigit (key[i][0])) |
5cd1f906 | 573 | net = getnetbyaddr (ntohl (inet_addr (key[i])), AF_UNSPEC); |
7d6a8338 UD |
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 */ | |
f1d70dad | 587 | static void |
7d6a8338 UD |
588 | print_passwd (struct passwd *pwd) |
589 | { | |
676599b3 FW |
590 | if (putpwent (pwd, stdout) != 0) |
591 | fprintf (stderr, "error writing passwd entry: %m\n"); | |
7d6a8338 UD |
592 | } |
593 | ||
232b4655 | 594 | static int |
7d6a8338 UD |
595 | passwd_keys (int number, char *key[]) |
596 | { | |
597 | int result = 0; | |
598 | int i; | |
232b4655 | 599 | struct passwd *pwd; |
7d6a8338 | 600 | |
9d0881aa | 601 | if (number == 0) |
7d6a8338 | 602 | { |
232b4655 | 603 | setpwent (); |
9d0881aa | 604 | while ((pwd = getpwent ()) != NULL) |
232b4655 UD |
605 | print_passwd (pwd); |
606 | endpwent (); | |
607 | return result; | |
608 | } | |
7d6a8338 | 609 | |
232b4655 UD |
610 | for (i = 0; i < number; ++i) |
611 | { | |
9030e7c4 UD |
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); | |
7d6a8338 UD |
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 */ | |
f1d70dad | 632 | static void |
7d6a8338 UD |
633 | print_protocols (struct protoent *proto) |
634 | { | |
635 | unsigned int i; | |
636 | ||
9d0881aa | 637 | printf ("%-21s %d", proto->p_name, proto->p_proto); |
7d6a8338 UD |
638 | |
639 | i = 0; | |
640 | while (proto->p_aliases[i] != NULL) | |
641 | { | |
9d0881aa UD |
642 | putchar_unlocked (' '); |
643 | fputs_unlocked (proto->p_aliases[i], stdout); | |
7d6a8338 UD |
644 | ++i; |
645 | } | |
9d0881aa | 646 | putchar_unlocked ('\n'); |
7d6a8338 UD |
647 | } |
648 | ||
232b4655 | 649 | static int |
7d6a8338 UD |
650 | protocols_keys (int number, char *key[]) |
651 | { | |
652 | int result = 0; | |
653 | int i; | |
232b4655 | 654 | struct protoent *proto; |
7d6a8338 | 655 | |
9d0881aa | 656 | if (number == 0) |
7d6a8338 | 657 | { |
232b4655 | 658 | setprotoent (0); |
9d0881aa | 659 | while ((proto = getprotoent ()) != NULL) |
232b4655 UD |
660 | print_protocols (proto); |
661 | endprotoent (); | |
662 | return result; | |
663 | } | |
7d6a8338 | 664 | |
232b4655 UD |
665 | for (i = 0; i < number; ++i) |
666 | { | |
7d6a8338 UD |
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 | ||
a1309c2b | 681 | #if HAVE_SUNRPC |
232b4655 | 682 | /* Now is all for rpc */ |
f1d70dad | 683 | static void |
232b4655 | 684 | print_rpc (struct rpcent *rpc) |
7d6a8338 | 685 | { |
232b4655 | 686 | int i; |
7d6a8338 | 687 | |
9d0881aa UD |
688 | printf ("%-15s %d%s", |
689 | rpc->r_name, rpc->r_number, rpc->r_aliases[0] ? " " : ""); | |
7d6a8338 | 690 | |
232b4655 UD |
691 | for (i = 0; rpc->r_aliases[i]; ++i) |
692 | printf (" %s", rpc->r_aliases[i]); | |
9d0881aa | 693 | putchar_unlocked ('\n'); |
7d6a8338 UD |
694 | } |
695 | ||
232b4655 UD |
696 | static int |
697 | rpc_keys (int number, char *key[]) | |
7d6a8338 UD |
698 | { |
699 | int result = 0; | |
700 | int i; | |
232b4655 | 701 | struct rpcent *rpc; |
7d6a8338 | 702 | |
9d0881aa | 703 | if (number == 0) |
7d6a8338 | 704 | { |
232b4655 | 705 | setrpcent (0); |
9d0881aa | 706 | while ((rpc = getrpcent ()) != NULL) |
232b4655 UD |
707 | print_rpc (rpc); |
708 | endrpcent (); | |
709 | return result; | |
710 | } | |
7d6a8338 | 711 | |
232b4655 UD |
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]); | |
7d6a8338 | 718 | |
232b4655 | 719 | if (rpc == NULL) |
7d6a8338 UD |
720 | result = 2; |
721 | else | |
232b4655 | 722 | print_rpc (rpc); |
7d6a8338 UD |
723 | } |
724 | ||
725 | return result; | |
726 | } | |
a1309c2b | 727 | #endif |
7d6a8338 UD |
728 | |
729 | /* for services */ | |
232b4655 | 730 | static void |
7d6a8338 UD |
731 | print_services (struct servent *serv) |
732 | { | |
733 | unsigned int i; | |
734 | ||
9d0881aa | 735 | printf ("%-21s %d/%s", serv->s_name, ntohs (serv->s_port), serv->s_proto); |
7d6a8338 UD |
736 | |
737 | i = 0; | |
738 | while (serv->s_aliases[i] != NULL) | |
739 | { | |
9d0881aa UD |
740 | putchar_unlocked (' '); |
741 | fputs_unlocked (serv->s_aliases[i], stdout); | |
7d6a8338 UD |
742 | ++i; |
743 | } | |
9d0881aa | 744 | putchar_unlocked ('\n'); |
7d6a8338 UD |
745 | } |
746 | ||
232b4655 | 747 | static int |
7d6a8338 UD |
748 | services_keys (int number, char *key[]) |
749 | { | |
750 | int result = 0; | |
751 | int i; | |
232b4655 UD |
752 | struct servent *serv; |
753 | ||
754 | if (!number) | |
755 | { | |
756 | setservent (0); | |
9d0881aa | 757 | while ((serv = getservent ()) != NULL) |
232b4655 UD |
758 | print_services (serv); |
759 | endservent (); | |
760 | return result; | |
761 | } | |
7d6a8338 UD |
762 | |
763 | for (i = 0; i < number; ++i) | |
764 | { | |
765 | struct servent *serv; | |
766 | char *proto = strchr (key[i], '/'); | |
767 | ||
a70e964e UD |
768 | if (proto != NULL) |
769 | *proto++ = '\0'; | |
7d6a8338 | 770 | |
e4368156 OB |
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); | |
a70e964e UD |
777 | else |
778 | serv = getservbyname (key[i], proto); | |
7d6a8338 | 779 | |
a70e964e UD |
780 | if (serv == NULL) |
781 | result = 2; | |
782 | else | |
783 | print_services (serv); | |
7d6a8338 UD |
784 | } |
785 | ||
786 | return result; | |
787 | } | |
788 | ||
232b4655 | 789 | /* This is for shadow */ |
25337753 | 790 | static void |
232b4655 | 791 | print_shadow (struct spwd *sp) |
7d6a8338 | 792 | { |
676599b3 FW |
793 | if (putspent (sp, stdout) != 0) |
794 | fprintf (stderr, "error writing shadow entry: %m\n"); | |
232b4655 | 795 | } |
7d6a8338 | 796 | |
232b4655 UD |
797 | static int |
798 | shadow_keys (int number, char *key[]) | |
799 | { | |
800 | int result = 0; | |
801 | int i; | |
7d6a8338 | 802 | |
9d0881aa | 803 | if (number == 0) |
7d6a8338 | 804 | { |
232b4655 UD |
805 | struct spwd *sp; |
806 | ||
807 | setspent (); | |
9d0881aa | 808 | while ((sp = getspent ()) != NULL) |
232b4655 | 809 | print_shadow (sp); |
d34c9158 | 810 | endspent (); |
232b4655 | 811 | return result; |
7d6a8338 UD |
812 | } |
813 | ||
232b4655 | 814 | for (i = 0; i < number; ++i) |
7d6a8338 | 815 | { |
232b4655 | 816 | struct spwd *sp; |
7d6a8338 | 817 | |
232b4655 | 818 | sp = getspnam (key[i]); |
7d6a8338 | 819 | |
232b4655 UD |
820 | if (sp == NULL) |
821 | result = 2; | |
7d6a8338 | 822 | else |
232b4655 UD |
823 | print_shadow (sp); |
824 | } | |
7d6a8338 | 825 | |
232b4655 UD |
826 | return result; |
827 | } | |
7d6a8338 | 828 | |
232b4655 UD |
829 | struct |
830 | { | |
831 | const char *name; | |
832 | int (*func) (int number, char *key[]); | |
833 | } databases[] = | |
834 | { | |
835 | #define D(name) { #name, name ## _keys }, | |
29bfc945 | 836 | D(ahosts) |
a7d24833 UD |
837 | D(ahostsv4) |
838 | D(ahostsv6) | |
232b4655 UD |
839 | D(aliases) |
840 | D(ethers) | |
841 | D(group) | |
c518f9a4 | 842 | D(gshadow) |
232b4655 | 843 | D(hosts) |
82e9a1f7 | 844 | D(initgroups) |
232b4655 UD |
845 | D(netgroup) |
846 | D(networks) | |
847 | D(passwd) | |
848 | D(protocols) | |
a1309c2b | 849 | #if HAVE_SUNRPC |
232b4655 | 850 | D(rpc) |
a1309c2b | 851 | #endif |
232b4655 UD |
852 | D(services) |
853 | D(shadow) | |
854 | #undef D | |
9d0881aa | 855 | { NULL, NULL } |
232b4655 UD |
856 | }; |
857 | ||
9d0881aa UD |
858 | /* Handle arguments found by argp. */ |
859 | static error_t | |
860 | parse_option (int key, char *arg, struct argp_state *state) | |
861 | { | |
b4f6f4be | 862 | char *endp; |
9d0881aa UD |
863 | switch (key) |
864 | { | |
865 | case 's': | |
b4f6f4be UD |
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 | } | |
9d0881aa UD |
883 | break; |
884 | ||
a160f8d8 UD |
885 | case 'i': |
886 | idn_flags = 0; | |
887 | break; | |
888 | ||
9d0881aa UD |
889 | default: |
890 | return ARGP_ERR_UNKNOWN; | |
891 | } | |
892 | ||
893 | return 0; | |
894 | } | |
895 | ||
232b4655 | 896 | |
f69425fa UD |
897 | static char * |
898 | more_help (int key, const char *text, void *input) | |
899 | { | |
f69425fa | 900 | switch (key) |
232b4655 | 901 | { |
b4f6f4be UD |
902 | size_t len; |
903 | char *doc; | |
904 | FILE *fp; | |
905 | ||
f69425fa UD |
906 | case ARGP_KEY_HELP_EXTRA: |
907 | /* We print some extra information. */ | |
b4f6f4be UD |
908 | fp = open_memstream (&doc, &len); |
909 | if (fp != NULL) | |
7d6a8338 | 910 | { |
b4f6f4be | 911 | fputs_unlocked (_("Supported databases:\n"), fp); |
f69425fa | 912 | |
b4f6f4be | 913 | for (int i = 0, col = 0; databases[i].name != NULL; ++i) |
7d6a8338 | 914 | { |
f69425fa UD |
915 | len = strlen (databases[i].name); |
916 | if (i != 0) | |
232b4655 | 917 | { |
f69425fa UD |
918 | if (col + len > 72) |
919 | { | |
920 | col = 0; | |
b4f6f4be | 921 | fputc_unlocked ('\n', fp); |
f69425fa UD |
922 | } |
923 | else | |
b4f6f4be | 924 | fputc_unlocked (' ', fp); |
232b4655 | 925 | } |
f69425fa | 926 | |
b4f6f4be | 927 | fputs_unlocked (databases[i].name, fp); |
f69425fa | 928 | col += len + 1; |
7d6a8338 | 929 | } |
232b4655 | 930 | |
cbbcaf23 UD |
931 | fputs ("\n\n", fp); |
932 | ||
8b748aed | 933 | fprintf (fp, gettext ("\ |
cbbcaf23 | 934 | For bug reporting instructions, please see:\n\ |
8b748aed | 935 | %s.\n"), REPORT_BUGS_TO); |
cbbcaf23 | 936 | |
b4f6f4be UD |
937 | if (fclose (fp) == 0) |
938 | return doc; | |
7d6a8338 | 939 | } |
f69425fa | 940 | break; |
232b4655 | 941 | |
f69425fa UD |
942 | default: |
943 | break; | |
944 | } | |
945 | return (char *) text; | |
232b4655 UD |
946 | } |
947 | ||
f69425fa | 948 | |
232b4655 UD |
949 | /* the main function */ |
950 | int | |
951 | main (int argc, char *argv[]) | |
952 | { | |
ff3cacc5 UD |
953 | /* Debugging support. */ |
954 | mtrace (); | |
232b4655 UD |
955 | |
956 | /* Set locale via LC_ALL. */ | |
957 | setlocale (LC_ALL, ""); | |
958 | /* Set the text message domain. */ | |
959 | textdomain (PACKAGE); | |
960 | ||
232b4655 | 961 | /* Parse and process arguments. */ |
ff3cacc5 | 962 | int remaining; |
232b4655 UD |
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")); | |
7d6a8338 UD |
968 | argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name); |
969 | return 1; | |
970 | } | |
971 | ||
ff3cacc5 | 972 | for (int i = 0; databases[i].name; ++i) |
9d0881aa UD |
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]); | |
232b4655 | 976 | |
9d0881aa | 977 | fprintf (stderr, _("Unknown database: %s\n"), argv[remaining]); |
232b4655 UD |
978 | argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name); |
979 | return 1; | |
7d6a8338 | 980 | } |