1 /* Cache handling for group lookup.
2 Copyright (C) 1998, 1999, 2000, 2001 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
35 /* This is the standard reply in case the service is disabled. */
36 static const gr_response_header disabled =
38 version: NSCD_VERSION,
46 /* This is the struct describing how to write this record. */
47 const struct iovec grp_iov_disabled =
49 iov_base: (void *) &disabled,
50 iov_len: sizeof (disabled)
54 /* This is the standard reply in case we haven't found the dataset. */
55 static const gr_response_header notfound =
57 version: NSCD_VERSION,
65 /* This is the struct describing how to write this record. */
66 static const struct iovec iov_notfound =
68 iov_base: (void *) ¬found,
69 iov_len: sizeof (notfound)
75 gr_response_header resp;
81 cache_addgr (struct database *db, int fd, request_header *req, void *key,
82 struct group *grp, uid_t owner)
86 time_t t = time (NULL);
90 /* We have no data. This means we send the standard reply for this
94 total = sizeof (notfound);
96 written = writev (fd, &iov_notfound, 1);
98 copy = malloc (req->key_len);
100 error (EXIT_FAILURE, errno, _("while allocating key copy"));
101 memcpy (copy, key, req->key_len);
103 /* Compute the timeout time. */
106 /* Now get the lock to safely insert the records. */
107 pthread_rwlock_rdlock (&db->lock);
109 cache_add (req->type, copy, req->key_len, ¬found,
110 sizeof (notfound), (void *) -1, 0, t, db, owner);
112 pthread_rwlock_unlock (&db->lock);
116 /* Determine the I/O structure. */
117 struct groupdata *data;
118 size_t gr_name_len = strlen (grp->gr_name) + 1;
119 size_t gr_passwd_len = strlen (grp->gr_passwd) + 1;
120 size_t gr_mem_cnt = 0;
121 uint32_t *gr_mem_len;
122 size_t gr_mem_len_total = 0;
129 /* We need this to insert the `bygid' entry. */
130 n = snprintf (buf, sizeof (buf), "%d", grp->gr_gid) + 1;
132 /* Determine the length of all members. */
133 while (grp->gr_mem[gr_mem_cnt])
135 gr_mem_len = (uint32_t *) alloca (gr_mem_cnt * sizeof (uint32_t));
136 for (gr_mem_cnt = 0; grp->gr_mem[gr_mem_cnt]; ++gr_mem_cnt)
138 gr_mem_len[gr_mem_cnt] = strlen (grp->gr_mem[gr_mem_cnt]) + 1;
139 gr_mem_len_total += gr_mem_len[gr_mem_cnt];
142 /* We allocate all data in one memory block: the iov vector,
143 the response header and the dataset itself. */
144 total = (sizeof (struct groupdata)
145 + gr_mem_cnt * sizeof (uint32_t)
146 + gr_name_len + gr_passwd_len + gr_mem_len_total);
147 data = (struct groupdata *) malloc (total + n);
149 /* There is no reason to go on. */
150 error (EXIT_FAILURE, errno, _("while allocating cache entry"));
152 data->resp.found = 1;
153 data->resp.gr_name_len = gr_name_len;
154 data->resp.gr_passwd_len = gr_passwd_len;
155 data->resp.gr_gid = grp->gr_gid;
156 data->resp.gr_mem_cnt = gr_mem_cnt;
160 /* This is the member string length array. */
161 cp = mempcpy (cp, gr_mem_len, gr_mem_cnt * sizeof (uint32_t));
163 cp = mempcpy (cp, grp->gr_name, gr_name_len);
164 cp = mempcpy (cp, grp->gr_passwd, gr_passwd_len);
166 for (cnt = 0; cnt < gr_mem_cnt; ++cnt)
167 cp = mempcpy (cp, grp->gr_mem[cnt], gr_mem_len[cnt]);
169 /* Finally the stringified GID value. */
172 /* Write the result. */
173 written = TEMP_FAILURE_RETRY (write (fd, &data->resp, total));
175 /* Compute the timeout time. */
178 /* Now get the lock to safely insert the records. */
179 pthread_rwlock_rdlock (&db->lock);
181 /* We have to add the value for both, byname and byuid. */
182 cache_add (GETGRBYNAME, gr_name, gr_name_len, data,
183 total, data, 0, t, db, owner);
185 cache_add (GETGRBYGID, cp, n, data, total, data, 1, t, db, owner);
187 pthread_rwlock_unlock (&db->lock);
190 if (__builtin_expect (written != total, 0) && debug_level > 0)
193 dbg_log (_("short write in %s: %s"), __FUNCTION__,
194 strerror_r (errno, buf, sizeof (buf)));
200 addgrbyname (struct database *db, int fd, request_header *req,
201 void *key, uid_t uid)
203 /* Search for the entry matching the key. Please note that we don't
204 look again in the table whether the dataset is now available. We
205 simply insert it. It does not matter if it is in there twice. The
206 pruning function only will look at the timestamp. */
208 char *buffer = alloca (buflen);
209 struct group resultbuf;
214 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *)key);
218 oldeuid = geteuid ();
222 while (__getgrnam_r (key, &resultbuf, buffer, buflen, &grp) != 0
227 buffer = alloca (buflen);
233 cache_addgr (db, fd, req, key, grp, uid);
238 addgrbygid (struct database *db, int fd, request_header *req,
239 void *key, uid_t uid)
241 /* Search for the entry matching the key. Please note that we don't
242 look again in the table whether the dataset is now available. We
243 simply insert it. It does not matter if it is in there twice. The
244 pruning function only will look at the timestamp. */
246 char *buffer = alloca (buflen);
247 struct group resultbuf;
249 gid_t gid = atol (key);
253 dbg_log (_("Haven't found \"%d\" in group cache!"), gid);
257 oldeuid = geteuid ();
261 while (__getgrgid_r (gid, &resultbuf, buffer, buflen, &grp) != 0
266 buffer = alloca (buflen);
272 cache_addgr (db, fd, req, key, grp, uid);