Update.
[platform/upstream/glibc.git] / nss / getent.c
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.
4
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.
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    Library General Public License for more details.
14
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. */
19
20 /* getent: get entries from administrative database
21    supported databases: passwd, group, hosts, services, protocols
22    and networks */
23
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>
39
40 /* Get libc version number.  */
41 #include <version.h>
42
43 #define PACKAGE _libc_intl_domainname
44
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;
48
49 /* Short description of parameters.  */
50 static const char args_doc[] = N_("database [key ...]");
51
52 /* Short description of program.  */
53 static const char doc[] =
54                 N_("getent - get entries from administrative database.");
55
56 /* Data structure to communicate with argp functions.  */
57 static struct argp argp = {
58   NULL, NULL, args_doc, doc,
59 };
60
61 /* Print the version information.  */
62 static void
63 print_version (FILE *stream, struct argp_state *state)
64 {
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");
72 }
73
74 /* This is for group */
75 static inline void
76 print_group (struct group *grp)
77 {
78   unsigned int i = 0;
79
80   printf ("%s:%s:%ld:", grp->gr_name ? grp->gr_name : "",
81           grp->gr_passwd ? grp->gr_passwd : "",
82           (unsigned long)grp->gr_gid);
83
84   while (grp->gr_mem[i] != NULL)
85     {
86       fputs (grp->gr_mem[i], stdout);
87       ++i;
88       if (grp->gr_mem[i] != NULL)
89         fputs (",", stdout);
90     }
91   fputs ("\n", stdout);
92 }
93
94 static inline int
95 group_keys (int number, char *key[])
96 {
97   int result = 0;
98   int i;
99
100   for (i = 0; i < number; ++i)
101     {
102       struct group *grp;
103
104       if (isdigit (key[i][0]))
105         grp = getgrgid (atol (key[i]));
106       else
107         grp = getgrnam (key[i]);
108
109       if (grp == NULL)
110         result = 2;
111       else
112         print_group (grp);
113     }
114
115   return result;
116 }
117
118 /* This is for networks */
119 static inline void
120 print_networks (struct netent *net)
121 {
122   unsigned int i;
123   struct in_addr ip;
124   ip.s_addr = htonl (net->n_net);
125
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);
130
131   i = 0;
132   while (net->n_aliases[i] != NULL)
133     {
134       fputs (" ", stdout);
135       fputs (net->n_aliases[i], stdout);
136       ++i;
137       if (net->n_aliases[i] != NULL)
138         fputs (",", stdout);
139     }
140   fputs ("\n", stdout);
141 }
142
143 static inline int
144 networks_keys (int number, char *key[])
145 {
146   int result = 0;
147   int i;
148
149   for (i = 0; i < number; ++i)
150     {
151       struct netent *net;
152
153       if (isdigit (key[i][0]))
154         net = getnetbyaddr (inet_addr (key[i]), AF_UNIX);
155       else
156         net = getnetbyname (key[i]);
157
158       if (net == NULL)
159         result = 2;
160       else
161         print_networks (net);
162     }
163
164   return result;
165 }
166
167 /* Now is all for passwd */
168 static inline void
169 print_passwd (struct passwd *pwd)
170 {
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 : "");
179 }
180
181 static inline int
182 passwd_keys (int number, char *key[])
183 {
184   int result = 0;
185   int i;
186
187   for (i = 0; i < number; ++i)
188     {
189       struct passwd *pwd;
190
191       if (isdigit (key[i][0]))
192         pwd = getpwuid (atol (key[i]));
193       else
194         pwd = getpwnam (key[i]);
195
196       if (pwd == NULL)
197         result = 2;
198       else
199         print_passwd (pwd);
200     }
201
202   return result;
203 }
204
205 /* This is for protocols */
206 static inline void
207 print_protocols (struct protoent *proto)
208 {
209   unsigned int i;
210
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);
215
216   i = 0;
217   while (proto->p_aliases[i] != NULL)
218     {
219       fputs (" ", stdout);
220       fputs (proto->p_aliases[i], stdout);
221       ++i;
222     }
223   fputs ("\n", stdout);
224 }
225
226 static inline int
227 protocols_keys (int number, char *key[])
228 {
229   int result = 0;
230   int i;
231
232   for (i = 0; i < number; ++i)
233     {
234       struct protoent *proto;
235
236       if (isdigit (key[i][0]))
237         proto = getprotobynumber (atol (key[i]));
238       else
239         proto = getprotobyname (key[i]);
240
241       if (proto == NULL)
242         result = 2;
243       else
244         print_protocols (proto);
245     }
246
247   return result;
248 }
249
250 /* This is for hosts */
251 static inline void
252 print_hosts (struct hostent *host)
253 {
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));
258
259   fputs (ip, stdout);
260   for (i = strlen (ip); i < 16; ++i)
261     fputs (" ", stdout);
262   fputs (host->h_name, stdout);
263
264   i = 0;
265   while (host->h_aliases[i] != NULL)
266     {
267       fputs (" ", stdout);
268       fputs (host->h_aliases[i], stdout);
269       ++i;
270     }
271   fputs ("\n", stdout);
272 }
273
274 static inline int
275 hosts_keys (int number, char *key[])
276 {
277   int result = 0;
278   int i;
279
280   for (i = 0; i < number; ++i)
281     {
282       struct hostent *host = NULL;
283
284       if (strchr (key[i], ':') != NULL)
285         {
286           char addr[IN6ADDRSZ];
287           if (inet_pton (AF_INET6, key[i], &addr))
288             host = gethostbyaddr (addr, sizeof (addr), AF_INET6);
289         }
290       else if (isdigit (key[i][0]))
291         {
292           char addr[INADDRSZ];
293           if (inet_pton (AF_INET, key[i], &addr))
294             host = gethostbyaddr (addr, sizeof (addr), AF_INET);
295         }
296       else if ((host = gethostbyname2 (key[i], AF_INET6)) == NULL)
297         host = gethostbyname2 (key[i], AF_INET);
298
299       if (host == NULL)
300         result = 2;
301       else
302         print_hosts (host);
303     }
304
305   return result;
306 }
307
308 /* for services */
309 static inline void
310 print_services (struct servent *serv)
311 {
312   unsigned int i;
313
314   fputs (serv->s_name, stdout);
315   for (i = strlen (serv->s_name); i < 22; ++i)
316     fputs (" ", stdout);
317   printf ("%d/%s", ntohs (serv->s_port), serv->s_proto);
318
319   i = 0;
320   while (serv->s_aliases[i] != NULL)
321     {
322       fputs (" ", stdout);
323       fputs (serv->s_aliases[i], stdout);
324       ++i;
325     }
326   fputs ("\n", stdout);
327 }
328
329 static inline int
330 services_keys (int number, char *key[])
331 {
332   int result = 0;
333   int i;
334
335   for (i = 0; i < number; ++i)
336     {
337       struct servent *serv;
338       char *proto = strchr (key[i], '/');
339
340       if (proto == NULL)
341         {
342           setservent (0);
343           if (isdigit (key[i][0]))
344             {
345               int port = htons (atol (key[i]));
346               while ((serv = getservent ()) != NULL)
347                 if (serv->s_port == port)
348                   {
349                     print_services (serv);
350                     break;
351                   }
352             }
353           else
354             {
355               while ((serv = getservent ()) != NULL)
356                 if (strcmp (serv->s_name, key[i]) == 0)
357                   {
358                     print_services (serv);
359                     break;
360                   }
361             }
362           endservent ();
363         }
364       else
365         {
366           *proto++ = '\0';
367
368           if (isdigit (key[i][0]))
369             serv = getservbyport (htons (atol (key[i])), proto);
370           else
371             serv = getservbyname (key[i], proto);
372
373           if (serv == NULL)
374             result = 2;
375           else
376             print_services (serv);
377         }
378     }
379
380   return result;
381 }
382
383 /* the main function */
384 int
385 main (int argc, char *argv[])
386 {
387   int remaining;
388
389   /* Set locale via LC_ALL.  */
390   setlocale (LC_ALL, "");
391   /* Set the text message domain.  */
392   textdomain (PACKAGE);
393
394   /* Parse and process arguments.  */
395   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
396
397   if ((argc - remaining) < 1)
398     {
399       error (0, 0, gettext ("wrong number of arguments"));
400       argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
401       return 1;
402     }
403
404   switch(argv[1][0])
405     {
406     case 'g': /* group */
407       if (strcmp (argv[1], "group") == 0)
408         {
409           if (argc == 2)
410             {
411               struct group *grp;
412
413               setgrent ();
414               while ((grp = getgrent()) != NULL)
415                 print_group (grp);
416               endgrent ();
417             }
418           else
419             return group_keys (argc - 2, &argv[2]);
420         }
421       else
422         goto error;
423       break;
424     case 'h': /* hosts */
425       if (strcmp (argv[1], "hosts") == 0)
426         {
427           if (argc == 2)
428             {
429               struct hostent *host;
430
431               sethostent (0);
432               while ((host = gethostent()) != NULL)
433                 print_hosts (host);
434               endhostent ();
435             }
436           else
437             return hosts_keys (argc - 2, &argv[2]);
438         }
439       else
440         goto error;
441       break;
442     case 'n': /* networks */
443       if (strcmp (argv[1], "networks") == 0)
444         {
445           if (argc == 2)
446             {
447               struct netent *net;
448
449               setnetent (0);
450               while ((net = getnetent()) != NULL)
451                 print_networks (net);
452               endnetent ();
453             }
454           else
455             return networks_keys (argc - 2, &argv[2]);
456         }
457       else
458         goto error;
459       break;
460     case 'p': /* passwd, protocols */
461       if (strcmp (argv[1], "passwd") == 0)
462         {
463           if (argc == 2)
464             {
465               struct passwd *pwd;
466
467               setpwent ();
468               while ((pwd = getpwent()) != NULL)
469                 print_passwd (pwd);
470               endpwent ();
471             }
472           else
473             return passwd_keys (argc - 2, &argv[2]);
474         }
475       else if (strcmp (argv[1], "protocols") == 0)
476         {
477           if (argc == 2)
478             {
479               struct protoent *proto;
480
481               setprotoent (0);
482               while ((proto = getprotoent()) != NULL)
483                 print_protocols (proto);
484               endprotoent ();
485             }
486           else
487             return protocols_keys (argc - 2, &argv[2]);
488         }
489       else
490         goto error;
491       break;
492     case 's': /* services */
493       if (strcmp (argv[1], "services") == 0)
494         {
495           if (argc == 2)
496             {
497               struct servent *serv;
498
499               setservent (0);
500               while ((serv = getservent()) != NULL)
501                 print_services (serv);
502               endservent ();
503             }
504           else
505             return services_keys (argc - 2, &argv[2]);
506         }
507       else
508         goto error;
509       break;
510     default:
511     error:
512       fprintf (stderr, _("Unknown database: %s\n"), argv[1]);
513       argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
514       return 1;
515     }
516
517   return 0;
518 }