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. */
26 #include <sys/socket.h>
31 #include "nscd_proto.h"
33 int __nss_not_use_nscd_group
;
35 static int __nscd_getgr_r (const char *key
, request_type type
,
36 struct group
*resultbuf
, char *buffer
,
40 __nscd_getgrnam_r (const char *name
, struct group
*resultbuf
, char *buffer
,
46 return __nscd_getgr_r (name
, GETGRBYNAME
, resultbuf
, buffer
, buflen
);
50 __nscd_getgrgid_r (gid_t gid
, struct group
*resultbuf
, char *buffer
,
56 plen
= __snprintf (buffer
, buflen
, "%d", gid
);
62 p
= buffer
+ plen
+ 1;
64 return __nscd_getgr_r (buffer
, GETGRBYGID
, resultbuf
, p
, buflen
- plen
-1);
67 /* Create a socket connected to a name. */
69 nscd_open_socket (void)
71 struct sockaddr_un addr
;
73 int saved_errno
= errno
;
75 sock
= __socket (PF_UNIX
, SOCK_STREAM
, 0);
78 __set_errno (saved_errno
);
82 addr
.sun_family
= AF_UNIX
;
83 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
84 if (__connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
87 __set_errno (saved_errno
);
95 __nscd_getgr_r (const char *key
, request_type type
, struct group
*resultbuf
,
96 char *buffer
, size_t buflen
)
98 int sock
= nscd_open_socket ();
100 gr_response_header gr_resp
;
107 __nss_not_use_nscd_group
= 1;
111 req
.version
= NSCD_VERSION
;
113 req
.key_len
= strlen (key
);
114 nbytes
= __write (sock
, &req
, sizeof (request_header
));
115 if (nbytes
!= sizeof (request_header
))
121 nbytes
= __write (sock
, key
, req
.key_len
);
122 if (nbytes
!= req
.key_len
)
128 nbytes
= __read (sock
, &gr_resp
, sizeof (gr_response_header
));
129 if (nbytes
!= sizeof (gr_response_header
))
135 if (gr_resp
.found
== -1)
137 /* The daemon does not cache this database. */
139 __nss_not_use_nscd_group
= 1;
143 if (gr_resp
.found
== 1)
152 /* A first check whether the buffer is sufficently large is possible. */
153 if (buflen
< gr_resp
.gr_name_len
+ 1 + gr_resp
.gr_passwd_len
+ 1)
155 __set_errno (ERANGE
);
160 /* Allocate the IOVEC. */
161 vec
= alloca ((2 + gr_resp
.gr_mem_len
) * sizeof (struct iovec
));
162 len
= alloca (gr_resp
.gr_mem_len
* sizeof (size_t));
164 vec
[0].iov_base
= resultbuf
->gr_name
= p
;
165 vec
[0].iov_len
= gr_resp
.gr_name_len
;
166 total_len
= gr_resp
.gr_name_len
;
167 p
+= gr_resp
.gr_name_len
+ 1;
169 vec
[1].iov_base
= resultbuf
->gr_passwd
= p
;
170 vec
[1].iov_len
= gr_resp
.gr_passwd_len
;
171 total_len
+= gr_resp
.gr_passwd_len
;
172 p
+= gr_resp
.gr_passwd_len
+ 1;
176 if (gr_resp
.gr_mem_len
> 0)
178 vec
[2].iov_base
= len
;
179 vec
[2].iov_len
= gr_resp
.gr_mem_len
* sizeof (size_t);
180 total_len
+= gr_resp
.gr_mem_len
* sizeof (size_t);
185 if (__readv (sock
, vec
, nblocks
) != total_len
)
191 /* Now we know the sizes. First terminate the strings we just read. */
192 resultbuf
->gr_name
[gr_resp
.gr_name_len
] = '\0';
193 resultbuf
->gr_passwd
[gr_resp
.gr_passwd_len
] = '\0';
195 resultbuf
->gr_gid
= gr_resp
.gr_gid
;
197 /* Now allocate the buffer the array for the group members. We must
198 align the pointer. */
199 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
200 & (__alignof__ (char *) - 1));
201 if (align
+ (1 + gr_resp
.gr_mem_len
) * sizeof (char *) > buflen
)
203 __set_errno (ERANGE
);
208 resultbuf
->gr_mem
= (char **) p
;
209 p
+= (1 + gr_resp
.gr_mem_len
) * sizeof (char *);
210 buflen
-= align
+ (1 + gr_resp
.gr_mem_len
) * sizeof (char *);
212 resultbuf
->gr_mem
[gr_resp
.gr_mem_len
] = NULL
;
214 if (gr_resp
.gr_mem_len
> 0)
216 /* Prepare reading the group members. */
220 for (i
= 0; i
< gr_resp
.gr_mem_len
; ++i
)
222 if (len
[i
] >= buflen
)
224 __set_errno (ERANGE
);
229 vec
[i
].iov_base
= resultbuf
->gr_mem
[i
] = p
;
230 vec
[i
].iov_len
= len
[i
];
240 maxiov
= sysconf (_SC_UIO_MAXIOV
);
246 sum
+= __readv (sock
, vec
, maxiov
);
251 if (sum
+ __readv (sock
, vec
, i
) != total_len
)