1 /* Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/socket.h>
31 #include <not-cancel.h>
32 #include <stdio-common/_itoa.h>
34 #include "nscd-client.h"
35 #include "nscd_proto.h"
37 int __nss_not_use_nscd_group
;
39 static int nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
40 struct group
*resultbuf
, char *buffer
,
41 size_t buflen
, struct group
**result
)
46 __nscd_getgrnam_r (const char *name
, struct group
*resultbuf
, char *buffer
,
47 size_t buflen
, struct group
**result
)
49 return nscd_getgr_r (name
, strlen (name
) + 1, GETGRBYNAME
, resultbuf
,
50 buffer
, buflen
, result
);
55 __nscd_getgrgid_r (gid_t gid
, struct group
*resultbuf
, char *buffer
,
56 size_t buflen
, struct group
**result
)
58 char buf
[3 * sizeof (gid_t
)];
59 buf
[sizeof (buf
) - 1] = '\0';
60 char *cp
= _itoa_word (gid
, buf
+ sizeof (buf
) - 1, 10, 0);
62 return nscd_getgr_r (cp
, buf
+ sizeof (buf
) - cp
, GETGRBYGID
, resultbuf
,
63 buffer
, buflen
, result
);
69 nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
70 struct group
*resultbuf
, char *buffer
, size_t buflen
,
71 struct group
**result
)
73 gr_response_header gr_resp
;
74 int sock
= __nscd_open_socket (key
, keylen
, type
, &gr_resp
,
78 __nss_not_use_nscd_group
= 1;
82 /* No value found so far. */
86 if (gr_resp
.found
== -1)
88 /* The daemon does not cache this database. */
89 __nss_not_use_nscd_group
= 1;
93 if (gr_resp
.found
== 1)
102 /* Now allocate the buffer the array for the group members. We must
103 align the pointer. */
104 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
105 & (__alignof__ (char *) - 1));
106 total_len
= align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *)
107 + gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
108 if (__builtin_expect (buflen
< total_len
, 0))
111 __set_errno (ERANGE
);
118 resultbuf
->gr_mem
= (char **) p
;
119 p
+= (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
121 /* Set pointers for strings. */
122 resultbuf
->gr_name
= p
;
123 p
+= gr_resp
.gr_name_len
;
124 resultbuf
->gr_passwd
= p
;
125 p
+= gr_resp
.gr_passwd_len
;
127 /* Fill in what we know now. */
128 resultbuf
->gr_gid
= gr_resp
.gr_gid
;
130 /* Allocate array to store lengths. */
131 len
= (uint32_t *) alloca (gr_resp
.gr_mem_cnt
* sizeof (uint32_t));
133 total_len
= gr_resp
.gr_mem_cnt
* sizeof (uint32_t);
134 vec
[0].iov_base
= len
;
135 vec
[0].iov_len
= total_len
;
136 vec
[1].iov_base
= resultbuf
->gr_name
;
137 vec
[1].iov_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
138 total_len
+= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
141 size_t n
= TEMP_FAILURE_RETRY (__readv (sock
, vec
, 2));
142 if (__builtin_expect (n
!= total_len
, 0))
145 /* Clear the terminating entry. */
146 resultbuf
->gr_mem
[gr_resp
.gr_mem_cnt
] = NULL
;
148 /* Prepare reading the group members. */
150 for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
152 resultbuf
->gr_mem
[cnt
] = p
;
153 total_len
+= len
[cnt
];
157 if (__builtin_expect (total_len
> buflen
, 0))
161 n
= TEMP_FAILURE_RETRY (__read (sock
, resultbuf
->gr_mem
[0],
163 if (__builtin_expect (n
!= total_len
, 0))
165 /* The `errno' to some value != ERANGE. */
166 __set_errno (ENOENT
);
174 /* The `errno' to some value != ERANGE. */
175 __set_errno (ENOENT
);
176 /* Even though we have not found anything, the result is zero. */
181 close_not_cancel_no_status (sock
);