Update.
[glibc.git] / nscd / nscd_getgr_r.c
blob71acc0608a3c81cc9557d6d40f26ee9f12faa15d
1 /* Copyright (C) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@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 #include <errno.h>
21 #include <grp.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
29 #include "nscd.h"
30 #include "nscd_proto.h"
32 int __nss_not_use_nscd_group;
34 static int __nscd_getgr_r (const char *key, request_type type,
35 struct group *resultbuf, char *buffer,
36 size_t buflen);
38 int
39 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
40 size_t buflen)
42 if (name == NULL)
43 return 1;
45 return __nscd_getgr_r (name, GETGRBYNAME, resultbuf, buffer, buflen);
48 int
49 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
50 size_t buflen)
52 char *p = buffer;
53 int plen;
55 plen = snprintf (buffer, buflen, "%d", gid);
56 if (plen == -1)
58 __set_errno (ERANGE);
59 return -1;
61 p = buffer + plen + 1;
63 return __nscd_getgr_r (buffer, GETGRBYGID, resultbuf, p, buflen - plen -1);
66 /* Create a socket connected to a name. */
67 static int
68 nscd_open_socket (void)
70 struct sockaddr_un addr;
71 int sock;
72 int saved_errno = errno;
74 sock = socket (PF_UNIX, SOCK_STREAM, 0);
75 if (sock < 0)
77 __set_errno (saved_errno);
78 return -1;
81 addr.sun_family = AF_UNIX;
82 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
83 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
85 close (sock);
86 __set_errno (saved_errno);
87 return -1;
90 return sock;
93 static int
94 __nscd_getgr_r (const char *key, request_type type, struct group *resultbuf,
95 char *buffer, size_t buflen)
97 int sock = nscd_open_socket ();
98 request_header req;
99 gr_response_header gr_resp;
100 ssize_t nbytes;
102 if (sock == -1)
104 __nss_not_use_nscd_group = 1;
105 return 1;
108 req.version = NSCD_VERSION;
109 req.type = type;
110 req.key_len = strlen (key);
111 nbytes = write (sock, &req, sizeof (request_header));
112 if (nbytes != sizeof (request_header))
114 close (sock);
115 return 1;
118 nbytes = write (sock, key, req.key_len);
119 if (nbytes != req.key_len)
121 close (sock);
122 return 1;
125 nbytes = read (sock, &gr_resp, sizeof (gr_response_header));
126 if (nbytes != sizeof (gr_response_header))
128 close (sock);
129 return 1;
132 if (gr_resp.found == -1)
134 /* The daemon does not cache this database. */
135 close (sock);
136 __nss_not_use_nscd_group = 1;
137 return 1;
140 if (gr_resp.found == 1)
142 size_t i;
143 char *p = buffer;
145 if (buflen < gr_resp.gr_name_len + 1)
147 __set_errno (ERANGE);
148 close (sock);
149 return -1;
151 resultbuf->gr_name = p;
152 p += gr_resp.gr_name_len + 1;
153 buflen -= (gr_resp.gr_name_len + 1);
154 nbytes = read (sock, resultbuf->gr_name, gr_resp.gr_name_len);
155 if (nbytes != gr_resp.gr_name_len)
157 close (sock);
158 return 1;
160 resultbuf->gr_name[gr_resp.gr_name_len] = '\0';
162 if (buflen < gr_resp.gr_passwd_len + 1)
164 __set_errno (ERANGE);
165 close (sock);
166 return -1;
168 resultbuf->gr_passwd = p;
169 p += gr_resp.gr_passwd_len + 1;
170 buflen -= (gr_resp.gr_passwd_len + 1);
171 nbytes = read (sock, resultbuf->gr_passwd, gr_resp.gr_passwd_len);
172 if (nbytes != gr_resp.gr_passwd_len)
174 close (sock);
175 return 1;
177 resultbuf->gr_passwd[gr_resp.gr_passwd_len] = '\0';
179 resultbuf->gr_gid = gr_resp.gr_gid;
181 if (buflen < ((gr_resp.gr_mem_len + 1) * sizeof (char *)))
183 __set_errno (ERANGE);
184 close (sock);
185 return -1;
187 resultbuf->gr_mem = (char **)p;
188 p += ((gr_resp.gr_mem_len + 1) * sizeof (char *));
189 buflen -= ((gr_resp.gr_mem_len + 1) * sizeof (char *));
191 resultbuf->gr_mem[gr_resp.gr_mem_len] = NULL;
193 for (i = 0; i < gr_resp.gr_mem_len; ++i)
195 size_t len;
196 nbytes = read (sock, &len, sizeof (len));
197 if (nbytes != sizeof (len))
199 close (sock);
200 return 1;
203 if (buflen < (len + 1))
205 __set_errno (ERANGE);
206 close (sock);
207 return -1;
209 resultbuf->gr_mem[i] = p;
210 p += len + 1;
211 buflen -= (len + 1);
212 nbytes = read (sock, resultbuf->gr_mem[i], len);
213 resultbuf->gr_mem[i][len] = '\0';
214 if (nbytes != len)
216 close (sock);
217 return 1;
220 close (sock);
221 return 0;
223 else
225 close (sock);
226 return -1;