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