eb9d280eee3145ce34f29aa7f7b571357bb33849
[platform/upstream/glibc.git] / nscd / nscd_getgr_r.c
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@uni-paderborn.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 #include <errno.h>
21 #include <grp.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <sys/uio.h>
29 #include <sys/un.h>
30
31 #include "nscd-client.h"
32 #include "nscd_proto.h"
33
34 int __nss_not_use_nscd_group;
35
36 static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
37                          struct group *resultbuf, char *buffer,
38                          size_t buflen);
39
40
41 int
42 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
43                    size_t buflen)
44 {
45   return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
46                        buffer, buflen);
47 }
48
49
50 int
51 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
52                    size_t buflen)
53 {
54   char buf[12];
55   size_t n;
56
57   n = __snprintf (buf, sizeof (buf), "%d", gid) + 1;
58
59   return nscd_getgr_r (buf, n, GETGRBYGID, resultbuf, buffer, buflen);
60 }
61
62
63 /* Create a socket connected to a name. */
64 static int
65 open_socket (void)
66 {
67   struct sockaddr_un addr;
68   int sock;
69   int saved_errno = errno;
70
71   sock = __socket (PF_UNIX, SOCK_STREAM, 0);
72   if (sock < 0)
73     {
74       __set_errno (saved_errno);
75       return -1;
76     }
77
78   addr.sun_family = AF_UNIX;
79   strcpy (addr.sun_path, _PATH_NSCDSOCKET);
80   if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
81     {
82       __close (sock);
83       __set_errno (saved_errno);
84       return -1;
85     }
86
87   return sock;
88 }
89
90
91 static int
92 nscd_getgr_r (const char *key, size_t keylen, request_type type,
93               struct group *resultbuf, char *buffer, size_t buflen)
94 {
95   int sock = open_socket ();
96   request_header req;
97   gr_response_header gr_resp;
98   ssize_t nbytes;
99   struct iovec vec[2];
100
101   if (sock == -1)
102     {
103       __nss_not_use_nscd_group = 1;
104       return 1;
105     }
106
107   req.version = NSCD_VERSION;
108   req.type = type;
109   req.key_len = keylen;
110
111   vec[0].iov_base = &req;
112   vec[0].iov_len = sizeof (request_header);
113   vec[1].iov_base = (void *) key;
114   vec[1].iov_len = keylen;
115
116   if (__writev (sock, vec, 2) != sizeof (request_header) + keylen)
117     {
118       __close (sock);
119       return 1;
120     }
121
122   nbytes = __read (sock, &gr_resp, sizeof (gr_response_header));
123   if (nbytes != sizeof (gr_response_header))
124     {
125       __close (sock);
126       return 1;
127     }
128
129   if (gr_resp.found == -1)
130     {
131       /* The daemon does not cache this database.  */
132       __close (sock);
133       __nss_not_use_nscd_group = 1;
134       return 1;
135     }
136
137   if (gr_resp.found == 1)
138     {
139       size_t *len;
140       char *p = buffer;
141       size_t total_len;
142       uintptr_t align;
143       size_t cnt;
144
145       /* Now allocate the buffer the array for the group members.  We must
146          align the pointer.  */
147       align = ((__alignof__ (char *) - (p - ((char *) 0)))
148                & (__alignof__ (char *) - 1));
149       if (buflen < (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
150                     + gr_resp.gr_name_len + gr_resp.gr_passwd_len))
151         {
152         no_room:
153           __set_errno (ERANGE);
154           __close (sock);
155           return -1;
156         }
157
158       p += align;
159       resultbuf->gr_mem = (char **) p;
160       p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
161       buflen -= align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
162
163       /* Set pointers for strings.  */
164       resultbuf->gr_name = p;
165       p += gr_resp.gr_name_len;
166       resultbuf->gr_passwd = p;
167       p += gr_resp.gr_passwd_len;
168
169       /* Fill in what we know now.  */
170       resultbuf->gr_gid = gr_resp.gr_gid;
171
172       /* Allocate array to store lengths.  */
173       len = alloca (gr_resp.gr_mem_cnt * sizeof (size_t));
174
175       total_len = gr_resp.gr_mem_cnt * sizeof (size_t);
176       vec[0].iov_base = len;
177       vec[0].iov_len = total_len;
178       vec[1].iov_base = resultbuf->gr_name;
179       vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
180       total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
181
182       buflen -= total_len;
183
184       /* Get this data.  */
185       if (__readv (sock, vec, 2) != total_len)
186         {
187           __close (sock);
188           return 1;
189         }
190
191       /* Clear the terminating entry.  */
192       resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
193
194       /* Prepare reading the group members.  */
195       total_len = 0;
196       for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
197         {
198           resultbuf->gr_mem[cnt] = p;
199           total_len += len[cnt];
200           p += len[cnt];
201         }
202
203       if (total_len > buflen)
204         goto no_room;
205
206       if (__read (sock, resultbuf->gr_mem[0], total_len) != total_len)
207         {
208           __close (sock);
209           return -1;
210         }
211
212       __close (sock);
213       return 0;
214     }
215   else
216     {
217       __close (sock);
218       return -1;
219     }
220 }