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.
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. */
27 #include <sys/socket.h>
31 #include "nscd-client.h"
32 #include "nscd_proto.h"
34 int __nss_not_use_nscd_group
;
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
) internal_function
;
42 __nscd_getgrnam_r (const char *name
, struct group
*resultbuf
, char *buffer
,
45 return nscd_getgr_r (name
, strlen (name
) + 1, GETGRBYNAME
, resultbuf
,
51 __nscd_getgrgid_r (gid_t gid
, struct group
*resultbuf
, char *buffer
,
57 n
= __snprintf (buf
, sizeof (buf
), "%d", gid
) + 1;
59 return nscd_getgr_r (buf
, n
, GETGRBYGID
, resultbuf
, buffer
, buflen
);
63 /* Create a socket connected to a name. */
67 struct sockaddr_un addr
;
69 int saved_errno
= errno
;
71 sock
= __socket (PF_UNIX
, SOCK_STREAM
, 0);
74 __set_errno (saved_errno
);
78 addr
.sun_family
= AF_UNIX
;
79 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
80 if (__connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
83 __set_errno (saved_errno
);
93 nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
94 struct group
*resultbuf
, char *buffer
, size_t buflen
)
96 int sock
= open_socket ();
98 gr_response_header gr_resp
;
104 __nss_not_use_nscd_group
= 1;
108 req
.version
= NSCD_VERSION
;
110 req
.key_len
= keylen
;
112 vec
[0].iov_base
= &req
;
113 vec
[0].iov_len
= sizeof (request_header
);
114 vec
[1].iov_base
= (void *) key
;
115 vec
[1].iov_len
= keylen
;
117 if (__writev (sock
, vec
, 2) != sizeof (request_header
) + keylen
)
123 nbytes
= __read (sock
, &gr_resp
, sizeof (gr_response_header
));
124 if (nbytes
!= sizeof (gr_response_header
))
130 if (gr_resp
.found
== -1)
132 /* The daemon does not cache this database. */
134 __nss_not_use_nscd_group
= 1;
138 if (gr_resp
.found
== 1)
146 /* Now allocate the buffer the array for the group members. We must
147 align the pointer. */
148 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
149 & (__alignof__ (char *) - 1));
150 if (buflen
< (align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *)
151 + gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
))
154 __set_errno (ERANGE
);
160 resultbuf
->gr_mem
= (char **) p
;
161 p
+= (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
162 buflen
-= align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
164 /* Set pointers for strings. */
165 resultbuf
->gr_name
= p
;
166 p
+= gr_resp
.gr_name_len
;
167 resultbuf
->gr_passwd
= p
;
168 p
+= gr_resp
.gr_passwd_len
;
170 /* Fill in what we know now. */
171 resultbuf
->gr_gid
= gr_resp
.gr_gid
;
173 /* Allocate array to store lengths. */
174 len
= alloca (gr_resp
.gr_mem_cnt
* sizeof (size_t));
176 total_len
= gr_resp
.gr_mem_cnt
* sizeof (size_t);
177 vec
[0].iov_base
= len
;
178 vec
[0].iov_len
= total_len
;
179 vec
[1].iov_base
= resultbuf
->gr_name
;
180 vec
[1].iov_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
181 total_len
+= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
186 if (__readv (sock
, vec
, 2) != total_len
)
192 /* Clear the terminating entry. */
193 resultbuf
->gr_mem
[gr_resp
.gr_mem_cnt
] = NULL
;
195 /* Prepare reading the group members. */
197 for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
199 resultbuf
->gr_mem
[cnt
] = p
;
200 total_len
+= len
[cnt
];
204 if (total_len
> buflen
)
207 if (__read (sock
, resultbuf
->gr_mem
[0], total_len
) != total_len
)
210 /* The `errno' to some value != ERANGE. */
211 __set_errno (ENOENT
);
221 /* The `errno' to some value != ERANGE. */
222 __set_errno (ENOENT
);