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