1 /* Copyright (c) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
39 /* Get libc version number. */
42 #define PACKAGE _libc_intl_domainname
44 /* Name and version of program. */
45 static void print_version (FILE *stream
, struct argp_state
*state
);
46 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
48 /* Short description of parameters. */
49 static const char args_doc
[] = N_("database [key ...]");
51 /* Short description of program. */
52 static const char doc
[] =
53 N_("getent - get entries from administrative database.");
55 /* Data structure to communicate with argp functions. */
56 static struct argp argp
= {
57 NULL
, NULL
, args_doc
, doc
,
60 /* Print the version information. */
62 print_version (FILE *stream
, struct argp_state
*state
)
64 fprintf (stream
, "getent (GNU %s) %s\n", PACKAGE
, VERSION
);
65 fprintf (stream
, gettext ("\
66 Copyright (C) %s Free Software Foundation, Inc.\n\
67 This is free software; see the source for copying conditions. There is NO\n\
68 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
70 fprintf (stream
, gettext ("Written by %s.\n"), "Thorsten Kukuk");
73 /* This is for group */
75 print_group (struct group
*grp
)
79 printf ("%s:%s:%ld:", grp
->gr_name
? grp
->gr_name
: "",
80 grp
->gr_passwd
? grp
->gr_passwd
: "",
81 (unsigned long)grp
->gr_gid
);
83 while (grp
->gr_mem
[i
] != NULL
)
85 fputs (grp
->gr_mem
[i
], stdout
);
87 if (grp
->gr_mem
[i
] != NULL
)
94 group_keys (int number
, char *key
[])
99 for (i
= 0; i
< number
; ++i
)
103 if (isdigit (key
[i
][0]))
104 grp
= getgrgid (atol (key
[i
]));
106 grp
= getgrnam (key
[i
]);
117 /* This is for networks */
119 print_networks (struct netent
*net
)
123 ip
.s_addr
= htonl (net
->n_net
);
125 fputs (net
->n_name
, stdout
);
126 for (i
= strlen (net
->n_name
); i
< 22; ++i
)
128 fputs (inet_ntoa (ip
), stdout
);
131 while (net
->n_aliases
[i
] != NULL
)
134 fputs (net
->n_aliases
[i
], stdout
);
136 if (net
->n_aliases
[i
] != NULL
)
139 fputs ("\n", stdout
);
143 networks_keys (int number
, char *key
[])
148 for (i
= 0; i
< number
; ++i
)
152 if (isdigit (key
[i
][0]))
153 net
= getnetbyaddr (inet_addr (key
[i
]), AF_UNIX
);
155 net
= getnetbyname (key
[i
]);
160 print_networks (net
);
166 /* Now is all for passwd */
168 print_passwd (struct passwd
*pwd
)
170 printf ("%s:%s:%ld:%ld:%s:%s:%s\n",
171 pwd
->pw_name
? pwd
->pw_name
: "",
172 pwd
->pw_passwd
? pwd
->pw_passwd
: "",
173 (unsigned long)pwd
->pw_uid
,
174 (unsigned long)pwd
->pw_gid
,
175 pwd
->pw_gecos
? pwd
->pw_gecos
: "",
176 pwd
->pw_dir
? pwd
->pw_dir
: "",
177 pwd
->pw_shell
? pwd
->pw_shell
: "");
181 passwd_keys (int number
, char *key
[])
186 for (i
= 0; i
< number
; ++i
)
190 if (isdigit (key
[i
][0]))
191 pwd
= getpwuid (atol (key
[i
]));
193 pwd
= getpwnam (key
[i
]);
204 /* This is for protocols */
206 print_protocols (struct protoent
*proto
)
210 fputs (proto
->p_name
, stdout
);
211 for (i
= strlen (proto
->p_name
); i
< 22; ++i
)
213 printf ("%d", proto
->p_proto
);
216 while (proto
->p_aliases
[i
] != NULL
)
219 fputs (proto
->p_aliases
[i
], stdout
);
222 fputs ("\n", stdout
);
226 protocols_keys (int number
, char *key
[])
231 for (i
= 0; i
< number
; ++i
)
233 struct protoent
*proto
;
235 if (isdigit (key
[i
][0]))
236 proto
= getprotobynumber (atol (key
[i
]));
238 proto
= getprotobyname (key
[i
]);
243 print_protocols (proto
);
249 /* This is for hosts */
251 print_hosts (struct hostent
*host
)
254 char *ip
= inet_ntoa(* (struct in_addr
*) host
->h_addr_list
[0]);
257 for (i
= strlen (ip
); i
< 16; ++i
)
259 fputs (host
->h_name
, stdout
);
262 while (host
->h_aliases
[i
] != NULL
)
265 fputs (host
->h_aliases
[i
], stdout
);
268 fputs ("\n", stdout
);
272 hosts_keys (int number
, char *key
[])
277 for (i
= 0; i
< number
; ++i
)
279 struct hostent
*host
;
281 if (isdigit (key
[i
][0]))
284 addr
.s_addr
= inet_addr (key
[i
]);
286 host
= gethostbyaddr ((char *)&addr
, sizeof (struct in_addr
),
290 host
= gethostbyname (key
[i
]);
303 print_services (struct servent
*serv
)
307 fputs (serv
->s_name
, stdout
);
308 for (i
= strlen (serv
->s_name
); i
< 22; ++i
)
310 printf ("%d/%s", ntohs (serv
->s_port
), serv
->s_proto
);
313 while (serv
->s_aliases
[i
] != NULL
)
316 fputs (serv
->s_aliases
[i
], stdout
);
319 fputs ("\n", stdout
);
323 services_keys (int number
, char *key
[])
328 for (i
= 0; i
< number
; ++i
)
330 struct servent
*serv
;
331 char *proto
= strchr (key
[i
], '/');
336 if (isdigit (key
[i
][0]))
338 int port
= htons (atol (key
[i
]));
339 while ((serv
= getservent ()) != NULL
)
340 if (serv
->s_port
== port
)
342 print_services (serv
);
348 while ((serv
= getservent ()) != NULL
)
349 if (strcmp (serv
->s_name
, key
[i
]) == 0)
351 print_services (serv
);
361 if (isdigit (key
[i
][0]))
362 serv
= getservbyport (atol (key
[i
]), proto
);
364 serv
= getservbyname (key
[i
], proto
);
369 print_services (serv
);
376 /* the main function */
378 main (int argc
, char *argv
[])
382 /* Set locale via LC_ALL. */
383 setlocale (LC_ALL
, "");
384 /* Set the text message domain. */
385 textdomain (PACKAGE
);
387 /* Parse and process arguments. */
388 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
390 if ((argc
- remaining
) < 1)
392 error (0, 0, gettext ("wrong number of arguments"));
393 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
399 case 'g': /* group */
400 if (strcmp (argv
[1], "group") == 0)
407 while ((grp
= getgrent()) != NULL
)
412 return group_keys (argc
- 2, &argv
[2]);
417 case 'h': /* hosts */
418 if (strcmp (argv
[1], "hosts") == 0)
422 struct hostent
*host
;
425 while ((host
= gethostent()) != NULL
)
430 return hosts_keys (argc
- 2, &argv
[2]);
435 case 'n': /* networks */
436 if (strcmp (argv
[1], "networks") == 0)
443 while ((net
= getnetent()) != NULL
)
444 print_networks (net
);
448 return networks_keys (argc
- 2, &argv
[2]);
453 case 'p': /* passwd, protocols */
454 if (strcmp (argv
[1], "passwd") == 0)
461 while ((pwd
= getpwent()) != NULL
)
466 return passwd_keys (argc
- 2, &argv
[2]);
468 else if (strcmp (argv
[1], "protocols") == 0)
472 struct protoent
*proto
;
475 while ((proto
= getprotoent()) != NULL
)
476 print_protocols (proto
);
480 return protocols_keys (argc
- 2, &argv
[2]);
485 case 's': /* services */
486 if (strcmp (argv
[1], "services") == 0)
490 struct servent
*serv
;
493 while ((serv
= getservent()) != NULL
)
494 print_services (serv
);
498 return services_keys (argc
- 2, &argv
[2]);
505 fprintf (stderr
, _("Unknown database: %s\n"), argv
[1]);
506 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);