Update.
[platform/upstream/glibc.git] / nscd / grpcache.c
1 /* Cache handling for group lookup.
2    Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
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    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <errno.h>
22 #include <error.h>
23 #include <grp.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <libintl.h>
30
31 #include "nscd.h"
32 #include "dbg_log.h"
33
34 /* This is the standard reply in case the service is disabled.  */
35 static const gr_response_header disabled =
36 {
37   version: NSCD_VERSION,
38   found: -1,
39   gr_name_len: 0,
40   gr_passwd_len: 0,
41   gr_gid: -1,
42   gr_mem_cnt: 0,
43 };
44
45 /* This is the struct describing how to write this record.  */
46 const struct iovec grp_iov_disabled =
47 {
48   iov_base: (void *) &disabled,
49   iov_len: sizeof (disabled)
50 };
51
52
53 /* This is the standard reply in case we haven't found the dataset.  */
54 static const gr_response_header notfound =
55 {
56   version: NSCD_VERSION,
57   found: 0,
58   gr_name_len: 0,
59   gr_passwd_len: 0,
60   gr_gid: -1,
61   gr_mem_cnt: 0,
62 };
63
64 /* This is the struct describing how to write this record.  */
65 static const struct iovec iov_notfound =
66 {
67   iov_base: (void *) &notfound,
68   iov_len: sizeof (notfound)
69 };
70
71
72 struct groupdata
73 {
74   gr_response_header resp;
75   char strdata[0];
76 };
77
78
79 static void
80 cache_addgr (struct database *db, int fd, request_header *req, void *key,
81              struct group *grp, uid_t owner)
82 {
83   ssize_t total;
84   ssize_t written;
85   time_t t = time (NULL);
86
87   if (grp == NULL)
88     {
89       /* We have no data.  This means we send the standard reply for this
90          case.  */
91       void *copy;
92
93       total = sizeof (notfound);
94
95       written = writev (fd, &iov_notfound, 1);
96
97       copy = malloc (req->key_len);
98       if (copy == NULL)
99         error (EXIT_FAILURE, errno, _("while allocating key copy"));
100       memcpy (copy, key, req->key_len);
101
102       /* Compute the timeout time.  */
103       t += db->negtimeout;
104
105       /* Now get the lock to safely insert the records.  */
106       pthread_rwlock_rdlock (&db->lock);
107
108       cache_add (req->type, copy, req->key_len, &notfound,
109                  sizeof (notfound), (void *) -1, 0, t, db, owner);
110
111       pthread_rwlock_unlock (&db->lock);
112     }
113   else
114     {
115       /* Determine the I/O structure.  */
116       struct groupdata *data;
117       size_t gr_name_len = strlen (grp->gr_name) + 1;
118       size_t gr_passwd_len = strlen (grp->gr_passwd) + 1;
119       size_t gr_mem_cnt = 0;
120       size_t *gr_mem_len;
121       size_t gr_mem_len_total = 0;
122       char *gr_name;
123       char *cp;
124       char buf[12];
125       ssize_t n;
126       size_t cnt;
127
128       /* We need this to insert the `bygid' entry.  */
129       n = snprintf (buf, sizeof (buf), "%d", grp->gr_gid) + 1;
130
131       /* Determine the length of all members.  */
132       while (grp->gr_mem[gr_mem_cnt])
133         ++gr_mem_cnt;
134       gr_mem_len = (size_t *) alloca (gr_mem_cnt * sizeof (size_t));
135       for (gr_mem_cnt = 0; grp->gr_mem[gr_mem_cnt]; ++gr_mem_cnt)
136         {
137           gr_mem_len[gr_mem_cnt] = strlen (grp->gr_mem[gr_mem_cnt]) + 1;
138           gr_mem_len_total += gr_mem_len[gr_mem_cnt];
139         }
140
141       /* We allocate all data in one memory block: the iov vector,
142          the response header and the dataset itself.  */
143       total = (sizeof (struct groupdata)
144                + gr_mem_cnt * sizeof (size_t)
145                + gr_name_len + gr_passwd_len + gr_mem_len_total);
146       data = (struct groupdata *) malloc (total + n);
147       if (data == NULL)
148         /* There is no reason to go on.  */
149         error (EXIT_FAILURE, errno, _("while allocating cache entry"));
150
151       data->resp.found = 1;
152       data->resp.gr_name_len = gr_name_len;
153       data->resp.gr_passwd_len = gr_passwd_len;
154       data->resp.gr_gid = grp->gr_gid;
155       data->resp.gr_mem_cnt = gr_mem_cnt;
156
157       cp = data->strdata;
158
159       /* This is the member string length array.  */
160       cp = mempcpy (cp, gr_mem_len, gr_mem_cnt * sizeof (size_t));
161       gr_name = cp;
162       cp = mempcpy (cp, grp->gr_name, gr_name_len);
163       cp = mempcpy (cp, grp->gr_passwd, gr_passwd_len);
164
165       for (cnt = 0; cnt < gr_mem_cnt; ++cnt)
166         cp = mempcpy (cp, grp->gr_mem[cnt], gr_mem_len[cnt]);
167
168       /* Finally the stringified GID value.  */
169       memcpy (cp, buf, n);
170
171       /* Write the result.  */
172       written = write (fd, &data->resp, total);
173
174       /* Compute the timeout time.  */
175       t += db->postimeout;
176
177       /* Now get the lock to safely insert the records.  */
178       pthread_rwlock_rdlock (&db->lock);
179
180       /* We have to add the value for both, byname and byuid.  */
181       cache_add (GETGRBYNAME, gr_name, gr_name_len, data,
182                  total, data, 0, t, db, owner);
183
184       cache_add (GETGRBYGID, cp, n, data, total, data, 1, t, db, owner);
185
186       pthread_rwlock_unlock (&db->lock);
187     }
188
189   if (written != total)
190     {
191       char buf[256];
192       dbg_log (_("short write in %s: %s"),  __FUNCTION__,
193                strerror_r (errno, buf, sizeof (buf)));
194     }
195 }
196
197
198 void
199 addgrbyname (struct database *db, int fd, request_header *req,
200              void *key, uid_t uid)
201 {
202   /* Search for the entry matching the key.  Please note that we don't
203      look again in the table whether the dataset is now available.  We
204      simply insert it.  It does not matter if it is in there twice.  The
205      pruning function only will look at the timestamp.  */
206   int buflen = 256;
207   char *buffer = alloca (buflen);
208   struct group resultbuf;
209   struct group *grp;
210   uid_t oldeuid = 0;
211
212   if (debug_level > 0)
213     dbg_log (_("Haven't found \"%s\" in group cache!"), key);
214
215   if (secure[grpdb])
216     {
217       oldeuid = geteuid ();
218       seteuid (uid);
219     }
220
221   while (__getgrnam_r (key, &resultbuf, buffer, buflen, &grp) != 0
222          && errno == ERANGE)
223     {
224       errno = 0;
225       buflen += 256;
226       buffer = alloca (buflen);
227     }
228
229   if (secure[grpdb])
230     seteuid (oldeuid);
231
232   cache_addgr (db, fd, req, key, grp, uid);
233 }
234
235
236 void
237 addgrbygid (struct database *db, int fd, request_header *req,
238             void *key, uid_t uid)
239 {
240   /* Search for the entry matching the key.  Please note that we don't
241      look again in the table whether the dataset is now available.  We
242      simply insert it.  It does not matter if it is in there twice.  The
243      pruning function only will look at the timestamp.  */
244   int buflen = 256;
245   char *buffer = alloca (buflen);
246   struct group resultbuf;
247   struct group *grp;
248   gid_t gid = atol (key);
249   uid_t oldeuid = 0;
250
251   if (debug_level > 0)
252     dbg_log (_("Haven't found \"%d\" in group cache!"), gid);
253
254   if (secure[grpdb])
255     {
256       oldeuid = geteuid ();
257       seteuid (uid);
258     }
259
260   while (__getgrgid_r (gid, &resultbuf, buffer, buflen, &grp) != 0
261          && errno == ERANGE)
262     {
263       errno = 0;
264       buflen += 256;
265       buffer = alloca (buflen);
266     }
267
268   if (secure[grpdb])
269     seteuid (oldeuid);
270
271   cache_addgr (db, fd, req, key, grp, uid);
272 }