1 /* Cache handling for group lookup.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 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
32 #include <stackinfo.h>
37 /* This is the standard reply in case the service is disabled. */
38 static const gr_response_header disabled
=
40 version
: NSCD_VERSION
,
48 /* This is the struct describing how to write this record. */
49 const struct iovec grp_iov_disabled
=
51 iov_base
: (void *) &disabled
,
52 iov_len
: sizeof (disabled
)
56 /* This is the standard reply in case we haven't found the dataset. */
57 static const gr_response_header notfound
=
59 version
: NSCD_VERSION
,
67 /* This is the struct describing how to write this record. */
68 static const struct iovec iov_notfound
=
70 iov_base
: (void *) ¬found
,
71 iov_len
: sizeof (notfound
)
77 gr_response_header resp
;
83 cache_addgr (struct database
*db
, int fd
, request_header
*req
, void *key
,
84 struct group
*grp
, uid_t owner
)
88 time_t t
= time (NULL
);
92 /* We have no data. This means we send the standard reply for this
96 total
= sizeof (notfound
);
98 written
= writev (fd
, &iov_notfound
, 1);
100 copy
= malloc (req
->key_len
);
102 error (EXIT_FAILURE
, errno
, _("while allocating key copy"));
103 memcpy (copy
, key
, req
->key_len
);
105 /* Compute the timeout time. */
108 /* Now get the lock to safely insert the records. */
109 pthread_rwlock_rdlock (&db
->lock
);
111 cache_add (req
->type
, copy
, req
->key_len
, ¬found
,
112 sizeof (notfound
), (void *) -1, 0, t
, db
, owner
);
114 pthread_rwlock_unlock (&db
->lock
);
118 /* Determine the I/O structure. */
119 struct groupdata
*data
;
120 size_t gr_name_len
= strlen (grp
->gr_name
) + 1;
121 size_t gr_passwd_len
= strlen (grp
->gr_passwd
) + 1;
122 size_t gr_mem_cnt
= 0;
123 uint32_t *gr_mem_len
;
124 size_t gr_mem_len_total
= 0;
131 /* We need this to insert the `bygid' entry. */
132 n
= snprintf (buf
, sizeof (buf
), "%d", grp
->gr_gid
) + 1;
134 /* Determine the length of all members. */
135 while (grp
->gr_mem
[gr_mem_cnt
])
137 gr_mem_len
= (uint32_t *) alloca (gr_mem_cnt
* sizeof (uint32_t));
138 for (gr_mem_cnt
= 0; grp
->gr_mem
[gr_mem_cnt
]; ++gr_mem_cnt
)
140 gr_mem_len
[gr_mem_cnt
] = strlen (grp
->gr_mem
[gr_mem_cnt
]) + 1;
141 gr_mem_len_total
+= gr_mem_len
[gr_mem_cnt
];
144 /* We allocate all data in one memory block: the iov vector,
145 the response header and the dataset itself. */
146 total
= (sizeof (struct groupdata
)
147 + gr_mem_cnt
* sizeof (uint32_t)
148 + gr_name_len
+ gr_passwd_len
+ gr_mem_len_total
);
149 data
= (struct groupdata
*) malloc (total
+ n
);
151 /* There is no reason to go on. */
152 error (EXIT_FAILURE
, errno
, _("while allocating cache entry"));
154 data
->resp
.found
= 1;
155 data
->resp
.gr_name_len
= gr_name_len
;
156 data
->resp
.gr_passwd_len
= gr_passwd_len
;
157 data
->resp
.gr_gid
= grp
->gr_gid
;
158 data
->resp
.gr_mem_cnt
= gr_mem_cnt
;
162 /* This is the member string length array. */
163 cp
= mempcpy (cp
, gr_mem_len
, gr_mem_cnt
* sizeof (uint32_t));
165 cp
= mempcpy (cp
, grp
->gr_name
, gr_name_len
);
166 cp
= mempcpy (cp
, grp
->gr_passwd
, gr_passwd_len
);
168 for (cnt
= 0; cnt
< gr_mem_cnt
; ++cnt
)
169 cp
= mempcpy (cp
, grp
->gr_mem
[cnt
], gr_mem_len
[cnt
]);
171 /* Finally the stringified GID value. */
174 /* Write the result. */
175 written
= TEMP_FAILURE_RETRY (write (fd
, &data
->resp
, total
));
177 /* Compute the timeout time. */
180 /* Now get the lock to safely insert the records. */
181 pthread_rwlock_rdlock (&db
->lock
);
183 /* We have to add the value for both, byname and byuid. */
184 cache_add (GETGRBYNAME
, gr_name
, gr_name_len
, data
,
185 total
, data
, 0, t
, db
, owner
);
187 cache_add (GETGRBYGID
, cp
, n
, data
, total
, data
, 1, t
, db
, owner
);
189 pthread_rwlock_unlock (&db
->lock
);
192 if (__builtin_expect (written
!= total
, 0) && debug_level
> 0)
195 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
196 strerror_r (errno
, buf
, sizeof (buf
)));
202 addgrbyname (struct database
*db
, int fd
, request_header
*req
,
203 void *key
, uid_t uid
)
205 /* Search for the entry matching the key. Please note that we don't
206 look again in the table whether the dataset is now available. We
207 simply insert it. It does not matter if it is in there twice. The
208 pruning function only will look at the timestamp. */
210 char *buffer
= (char *) alloca (buflen
);
211 struct group resultbuf
;
214 bool use_malloc
= false;
216 if (__builtin_expect (debug_level
> 0, 0))
217 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key
);
221 oldeuid
= geteuid ();
225 while (__getgrnam_r (key
, &resultbuf
, buffer
, buflen
, &grp
) != 0
228 char *old_buffer
= buffer
;
232 if (__builtin_expect (buflen
> 32768, 0))
234 buffer
= (char *) realloc (use_malloc
? buffer
: NULL
, buflen
);
237 /* We ran out of memory. We cannot do anything but
238 sending a negative response. In reality this should
248 buffer
= (char *) alloca (buflen
);
249 #if _STACK_GROWS_DOWN
250 if (buffer
+ buflen
== old_buffer
)
251 buflen
= 2 * buflen
- 1024;
252 #elif _STACK_GROWS_UP
253 if (old_buffer
+ buflen
- 1024 == buffer
)
256 buflen
= 2 * buflen
- 1024;
265 cache_addgr (db
, fd
, req
, key
, grp
, uid
);
273 addgrbygid (struct database
*db
, int fd
, request_header
*req
,
274 void *key
, uid_t uid
)
276 /* Search for the entry matching the key. Please note that we don't
277 look again in the table whether the dataset is now available. We
278 simply insert it. It does not matter if it is in there twice. The
279 pruning function only will look at the timestamp. */
281 char *buffer
= (char *) alloca (buflen
);
282 struct group resultbuf
;
286 gid_t gid
= strtoul ((char *)key
, &ep
, 10);
287 bool use_malloc
= false;
289 if (*(char *) key
== '\0' || *ep
!= '\0') /* invalid numeric gid */
292 dbg_log (_("Invalid numeric gid \"%s\"!"), (char *) key
);
298 if (__builtin_expect (debug_level
> 0, 0))
299 dbg_log (_("Haven't found \"%d\" in group cache!"), gid
);
303 oldeuid
= geteuid ();
307 while (__getgrgid_r (gid
, &resultbuf
, buffer
, buflen
, &grp
) != 0
310 char *old_buffer
= buffer
;
314 if (__builtin_expect (buflen
> 32768, 0))
316 buffer
= (char *) realloc (use_malloc
? buffer
: NULL
, buflen
);
319 /* We ran out of memory. We cannot do anything but
320 sending a negative response. In reality this should
330 buffer
= (char *) alloca (buflen
);
331 #if _STACK_GROWS_DOWN
332 if (buffer
+ buflen
== old_buffer
)
333 buflen
= 2 * buflen
- 1024;
334 #elif _STACK_GROWS_UP
335 if (old_buffer
+ buflen
- 1024 == buffer
)
338 buflen
= 2 * buflen
- 1024;
347 cache_addgr (db
, fd
, req
, key
, grp
, uid
);