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>
30 #include "nscd-client.h"
31 #include "nscd_proto.h"
33 int __nss_not_use_nscd_group
;
35 static int nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
36 struct group
*resultbuf
, char *buffer
,
41 __nscd_getgrnam_r (const char *name
, struct group
*resultbuf
, char *buffer
,
44 return nscd_getgr_r (name
, strlen (name
) + 1, GETGRBYNAME
, resultbuf
,
50 __nscd_getgrgid_r (gid_t gid
, struct group
*resultbuf
, char *buffer
,
56 n
= __snprintf (buf
, sizeof (buf
), "%d", gid
) + 1;
58 return nscd_getgr_r (buf
, n
, GETGRBYGID
, resultbuf
, buffer
, buflen
);
62 /* Create a socket connected to a name. */
66 struct sockaddr_un addr
;
68 int saved_errno
= errno
;
70 sock
= __socket (PF_UNIX
, SOCK_STREAM
, 0);
73 __set_errno (saved_errno
);
77 addr
.sun_family
= AF_UNIX
;
78 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
79 if (__connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
82 __set_errno (saved_errno
);
91 nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
92 struct group
*resultbuf
, char *buffer
, size_t buflen
)
94 int sock
= open_socket ();
96 gr_response_header gr_resp
;
102 __nss_not_use_nscd_group
= 1;
106 req
.version
= NSCD_VERSION
;
108 req
.key_len
= keylen
;
110 vec
[0].iov_base
= &req
;
111 vec
[0].iov_len
= sizeof (request_header
);
112 vec
[1].iov_base
= (void *) key
;
113 vec
[1].iov_len
= keylen
;
115 if (__writev (sock
, vec
, 2) != sizeof (request_header
) + keylen
)
121 nbytes
= __read (sock
, &gr_resp
, sizeof (gr_response_header
));
122 if (nbytes
!= sizeof (gr_response_header
))
128 if (gr_resp
.found
== -1)
130 /* The daemon does not cache this database. */
132 __nss_not_use_nscd_group
= 1;
136 if (gr_resp
.found
== 1)
144 /* Now allocate the buffer the array for the group members. We must
145 align the pointer. */
146 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
147 & (__alignof__ (char *) - 1));
148 if (buflen
< (align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *)
149 + gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
))
152 __set_errno (ERANGE
);
158 resultbuf
->gr_mem
= (char **) p
;
159 p
+= (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
160 buflen
-= align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
162 /* Set pointers for strings. */
163 resultbuf
->gr_name
= p
;
164 p
+= gr_resp
.gr_name_len
;
165 resultbuf
->gr_passwd
= p
;
166 p
+= gr_resp
.gr_passwd_len
;
168 /* Fill in what we know now. */
169 resultbuf
->gr_gid
= gr_resp
.gr_gid
;
171 /* Allocate array to store lengths. */
172 len
= alloca (gr_resp
.gr_mem_cnt
* sizeof (size_t));
174 total_len
= gr_resp
.gr_mem_cnt
* sizeof (size_t);
175 vec
[0].iov_base
= len
;
176 vec
[0].iov_len
= total_len
;
177 vec
[1].iov_base
= resultbuf
->gr_name
;
178 vec
[1].iov_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
179 total_len
+= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
184 if (__readv (sock
, vec
, 2) != total_len
)
190 /* Clear the terminating entry. */
191 resultbuf
->gr_mem
[gr_resp
.gr_mem_cnt
] = NULL
;
193 /* Prepare reading the group members. */
195 for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
197 resultbuf
->gr_mem
[cnt
] = p
;
198 total_len
+= len
[cnt
];
202 if (total_len
> buflen
)
205 if (__read (sock
, resultbuf
->gr_mem
[0], total_len
) != total_len
)