Update.
[glibc.git] / nss / getent.c
blob5834d790a01c203e89c2074ec65ea405e4d30ce4
1 /* Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* getent: get entries from administrative database
21 supported databases: passwd, group, hosts, services, protocols
22 and networks */
24 #include <argp.h>
25 #include <grp.h>
26 #include <pwd.h>
27 #include <ctype.h>
28 #include <error.h>
29 #include <libintl.h>
30 #include <locale.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
40 /* Get libc version number. */
41 #include <version.h>
43 #define PACKAGE _libc_intl_domainname
45 /* Name and version of program. */
46 static void print_version (FILE *stream, struct argp_state *state);
47 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
49 /* Short description of parameters. */
50 static const char args_doc[] = N_("database [key ...]");
52 /* Short description of program. */
53 static const char doc[] =
54 N_("getent - get entries from administrative database.");
56 /* Data structure to communicate with argp functions. */
57 static struct argp argp = {
58 NULL, NULL, args_doc, doc,
61 /* Print the version information. */
62 static void
63 print_version (FILE *stream, struct argp_state *state)
65 fprintf (stream, "getent (GNU %s) %s\n", PACKAGE, VERSION);
66 fprintf (stream, gettext ("\
67 Copyright (C) %s Free Software Foundation, Inc.\n\
68 This is free software; see the source for copying conditions. There is NO\n\
69 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
70 "), "1999");
71 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
74 /* This is for group */
75 static inline void
76 print_group (struct group *grp)
78 unsigned int i = 0;
80 printf ("%s:%s:%ld:", grp->gr_name ? grp->gr_name : "",
81 grp->gr_passwd ? grp->gr_passwd : "",
82 (unsigned long)grp->gr_gid);
84 while (grp->gr_mem[i] != NULL)
86 fputs (grp->gr_mem[i], stdout);
87 ++i;
88 if (grp->gr_mem[i] != NULL)
89 fputs (",", stdout);
91 fputs ("\n", stdout);
94 static inline int
95 group_keys (int number, char *key[])
97 int result = 0;
98 int i;
100 for (i = 0; i < number; ++i)
102 struct group *grp;
104 if (isdigit (key[i][0]))
105 grp = getgrgid (atol (key[i]));
106 else
107 grp = getgrnam (key[i]);
109 if (grp == NULL)
110 result = 2;
111 else
112 print_group (grp);
115 return result;
118 /* This is for networks */
119 static inline void
120 print_networks (struct netent *net)
122 unsigned int i;
123 struct in_addr ip;
124 ip.s_addr = htonl (net->n_net);
126 fputs (net->n_name, stdout);
127 for (i = strlen (net->n_name); i < 22; ++i)
128 fputs (" ", stdout);
129 fputs (inet_ntoa (ip), stdout);
131 i = 0;
132 while (net->n_aliases[i] != NULL)
134 fputs (" ", stdout);
135 fputs (net->n_aliases[i], stdout);
136 ++i;
137 if (net->n_aliases[i] != NULL)
138 fputs (",", stdout);
140 fputs ("\n", stdout);
143 static inline int
144 networks_keys (int number, char *key[])
146 int result = 0;
147 int i;
149 for (i = 0; i < number; ++i)
151 struct netent *net;
153 if (isdigit (key[i][0]))
154 net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
155 else
156 net = getnetbyname (key[i]);
158 if (net == NULL)
159 result = 2;
160 else
161 print_networks (net);
164 return result;
167 /* Now is all for passwd */
168 static inline void
169 print_passwd (struct passwd *pwd)
171 printf ("%s:%s:%ld:%ld:%s:%s:%s\n",
172 pwd->pw_name ? pwd->pw_name : "",
173 pwd->pw_passwd ? pwd->pw_passwd : "",
174 (unsigned long)pwd->pw_uid,
175 (unsigned long)pwd->pw_gid,
176 pwd->pw_gecos ? pwd->pw_gecos : "",
177 pwd->pw_dir ? pwd->pw_dir : "",
178 pwd->pw_shell ? pwd->pw_shell : "");
181 static inline int
182 passwd_keys (int number, char *key[])
184 int result = 0;
185 int i;
187 for (i = 0; i < number; ++i)
189 struct passwd *pwd;
191 if (isdigit (key[i][0]))
192 pwd = getpwuid (atol (key[i]));
193 else
194 pwd = getpwnam (key[i]);
196 if (pwd == NULL)
197 result = 2;
198 else
199 print_passwd (pwd);
202 return result;
205 /* This is for protocols */
206 static inline void
207 print_protocols (struct protoent *proto)
209 unsigned int i;
211 fputs (proto->p_name, stdout);
212 for (i = strlen (proto->p_name); i < 22; ++i)
213 fputs (" ", stdout);
214 printf ("%d", proto->p_proto);
216 i = 0;
217 while (proto->p_aliases[i] != NULL)
219 fputs (" ", stdout);
220 fputs (proto->p_aliases[i], stdout);
221 ++i;
223 fputs ("\n", stdout);
226 static inline int
227 protocols_keys (int number, char *key[])
229 int result = 0;
230 int i;
232 for (i = 0; i < number; ++i)
234 struct protoent *proto;
236 if (isdigit (key[i][0]))
237 proto = getprotobynumber (atol (key[i]));
238 else
239 proto = getprotobyname (key[i]);
241 if (proto == NULL)
242 result = 2;
243 else
244 print_protocols (proto);
247 return result;
250 /* This is for hosts */
251 static inline void
252 print_hosts (struct hostent *host)
254 unsigned int i;
255 char buf[INET6_ADDRSTRLEN];
256 const char *ip = inet_ntop (host->h_addrtype, host->h_addr_list[0],
257 buf, sizeof (buf));
259 fputs (ip, stdout);
260 for (i = strlen (ip); i < 15; ++i)
261 fputs (" ", stdout);
262 fputs (" ", stdout);
263 fputs (host->h_name, stdout);
265 i = 0;
266 while (host->h_aliases[i] != NULL)
268 fputs (" ", stdout);
269 fputs (host->h_aliases[i], stdout);
270 ++i;
272 fputs ("\n", stdout);
275 static inline int
276 hosts_keys (int number, char *key[])
278 int result = 0;
279 int i;
281 for (i = 0; i < number; ++i)
283 struct hostent *host = NULL;
285 if (strchr (key[i], ':') != NULL)
287 char addr[IN6ADDRSZ];
288 if (inet_pton (AF_INET6, key[i], &addr))
289 host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
291 else if (isdigit (key[i][0]))
293 char addr[INADDRSZ];
294 if (inet_pton (AF_INET, key[i], &addr))
295 host = gethostbyaddr (addr, sizeof (addr), AF_INET);
297 else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
298 host = gethostbyname2 (key[i], AF_INET);
300 if (host == NULL)
301 result = 2;
302 else
303 print_hosts (host);
306 return result;
309 /* for services */
310 static inline void
311 print_services (struct servent *serv)
313 unsigned int i;
315 fputs (serv->s_name, stdout);
316 for (i = strlen (serv->s_name); i < 22; ++i)
317 fputs (" ", stdout);
318 printf ("%d/%s", ntohs (serv->s_port), serv->s_proto);
320 i = 0;
321 while (serv->s_aliases[i] != NULL)
323 fputs (" ", stdout);
324 fputs (serv->s_aliases[i], stdout);
325 ++i;
327 fputs ("\n", stdout);
330 static inline int
331 services_keys (int number, char *key[])
333 int result = 0;
334 int i;
336 for (i = 0; i < number; ++i)
338 struct servent *serv;
339 char *proto = strchr (key[i], '/');
341 if (proto == NULL)
343 setservent (0);
344 if (isdigit (key[i][0]))
346 int port = htons (atol (key[i]));
347 while ((serv = getservent ()) != NULL)
348 if (serv->s_port == port)
350 print_services (serv);
351 break;
354 else
356 while ((serv = getservent ()) != NULL)
357 if (strcmp (serv->s_name, key[i]) == 0)
359 print_services (serv);
360 break;
363 endservent ();
365 else
367 *proto++ = '\0';
369 if (isdigit (key[i][0]))
370 serv = getservbyport (htons (atol (key[i])), proto);
371 else
372 serv = getservbyname (key[i], proto);
374 if (serv == NULL)
375 result = 2;
376 else
377 print_services (serv);
381 return result;
384 /* the main function */
386 main (int argc, char *argv[])
388 int remaining;
390 /* Set locale via LC_ALL. */
391 setlocale (LC_ALL, "");
392 /* Set the text message domain. */
393 textdomain (PACKAGE);
395 /* Parse and process arguments. */
396 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
398 if ((argc - remaining) < 1)
400 error (0, 0, gettext ("wrong number of arguments"));
401 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
402 return 1;
405 switch(argv[1][0])
407 case 'g': /* group */
408 if (strcmp (argv[1], "group") == 0)
410 if (argc == 2)
412 struct group *grp;
414 setgrent ();
415 while ((grp = getgrent()) != NULL)
416 print_group (grp);
417 endgrent ();
419 else
420 return group_keys (argc - 2, &argv[2]);
422 else
423 goto error;
424 break;
425 case 'h': /* hosts */
426 if (strcmp (argv[1], "hosts") == 0)
428 if (argc == 2)
430 struct hostent *host;
432 sethostent (0);
433 while ((host = gethostent()) != NULL)
434 print_hosts (host);
435 endhostent ();
437 else
438 return hosts_keys (argc - 2, &argv[2]);
440 else
441 goto error;
442 break;
443 case 'n': /* networks */
444 if (strcmp (argv[1], "networks") == 0)
446 if (argc == 2)
448 struct netent *net;
450 setnetent (0);
451 while ((net = getnetent()) != NULL)
452 print_networks (net);
453 endnetent ();
455 else
456 return networks_keys (argc - 2, &argv[2]);
458 else
459 goto error;
460 break;
461 case 'p': /* passwd, protocols */
462 if (strcmp (argv[1], "passwd") == 0)
464 if (argc == 2)
466 struct passwd *pwd;
468 setpwent ();
469 while ((pwd = getpwent()) != NULL)
470 print_passwd (pwd);
471 endpwent ();
473 else
474 return passwd_keys (argc - 2, &argv[2]);
476 else if (strcmp (argv[1], "protocols") == 0)
478 if (argc == 2)
480 struct protoent *proto;
482 setprotoent (0);
483 while ((proto = getprotoent()) != NULL)
484 print_protocols (proto);
485 endprotoent ();
487 else
488 return protocols_keys (argc - 2, &argv[2]);
490 else
491 goto error;
492 break;
493 case 's': /* services */
494 if (strcmp (argv[1], "services") == 0)
496 if (argc == 2)
498 struct servent *serv;
500 setservent (0);
501 while ((serv = getservent()) != NULL)
502 print_services (serv);
503 endservent ();
505 else
506 return services_keys (argc - 2, &argv[2]);
508 else
509 goto error;
510 break;
511 default:
512 error:
513 fprintf (stderr, _("Unknown database: %s\n"), argv[1]);
514 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
515 return 1;
518 return 0;