1 /* Cache handling for group lookup.
2 Copyright (C) 1998, 1999 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 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.
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.
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. */
34 /* This is the standard reply in case the service is disabled. */
35 static const gr_response_header disabled
=
37 version
: NSCD_VERSION
,
45 /* This is the struct describing how to write this record. */
46 const struct iovec grp_iov_disabled
=
48 iov_base
: (void *) &disabled
,
49 iov_len
: sizeof (disabled
)
53 /* This is the standard reply in case we haven't found the dataset. */
54 static const gr_response_header notfound
=
56 version
: NSCD_VERSION
,
64 /* This is the struct describing how to write this record. */
65 static const struct iovec iov_notfound
=
67 iov_base
: (void *) ¬found
,
68 iov_len
: sizeof (notfound
)
74 gr_response_header resp
;
80 cache_addgr (struct database
*db
, int fd
, request_header
*req
, void *key
,
81 struct group
*grp
, uid_t owner
)
85 time_t t
= time (NULL
);
89 /* We have no data. This means we send the standard reply for this
93 total
= sizeof (notfound
);
95 written
= writev (fd
, &iov_notfound
, 1);
97 copy
= malloc (req
->key_len
);
99 error (EXIT_FAILURE
, errno
, _("while allocating key copy"));
100 memcpy (copy
, key
, req
->key_len
);
102 /* Compute the timeout time. */
105 /* Now get the lock to safely insert the records. */
106 pthread_rwlock_rdlock (&db
->lock
);
108 cache_add (req
->type
, copy
, req
->key_len
, ¬found
,
109 sizeof (notfound
), (void *) -1, 0, t
, db
, owner
);
111 pthread_rwlock_unlock (&db
->lock
);
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;
121 size_t gr_mem_len_total
= 0;
128 /* We need this to insert the `bygid' entry. */
129 n
= snprintf (buf
, sizeof (buf
), "%d", grp
->gr_gid
) + 1;
131 /* Determine the length of all members. */
132 while (grp
->gr_mem
[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
)
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
];
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
);
148 /* There is no reason to go on. */
149 error (EXIT_FAILURE
, errno
, _("while allocating cache entry"));
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
;
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
= mempcpy (cp
, grp
->gr_name
, gr_name_len
);
162 cp
= mempcpy (cp
, grp
->gr_passwd
, gr_passwd_len
);
164 for (cnt
= 0; cnt
< gr_mem_cnt
; ++cnt
)
165 cp
= mempcpy (cp
, grp
->gr_mem
[cnt
], gr_mem_len
[cnt
]);
167 /* Finally the stringified GID value. */
170 /* Write the result. */
171 written
= write (fd
, &data
->resp
, total
);
173 /* Compute the timeout time. */
176 /* Now get the lock to safely insert the records. */
177 pthread_rwlock_rdlock (&db
->lock
);
179 /* We have to add the value for both, byname and byuid. */
180 cache_add (GETGRBYNAME
, gr_name
, gr_name_len
, data
,
181 total
, data
, 0, t
, db
, owner
);
183 cache_add (GETGRBYGID
, cp
, n
, data
, total
, data
, 1, t
, db
, owner
);
185 pthread_rwlock_unlock (&db
->lock
);
188 if (written
!= total
)
191 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
192 strerror_r (errno
, buf
, sizeof (buf
)));
198 addgrbyname (struct database
*db
, int fd
, request_header
*req
,
199 void *key
, uid_t uid
)
201 /* Search for the entry matching the key. Please note that we don't
202 look again in the table whether the dataset is now available. We
203 simply insert it. It does not matter if it is in there twice. The
204 pruning function only will look at the timestamp. */
206 char *buffer
= alloca (buflen
);
207 struct group resultbuf
;
212 dbg_log (_("Haven't found \"%s\" in group cache!"), key
);
216 oldeuid
= geteuid ();
220 while (__getgrnam_r (key
, &resultbuf
, buffer
, buflen
, &grp
) != 0
225 buffer
= alloca (buflen
);
231 cache_addgr (db
, fd
, req
, key
, grp
, uid
);
236 addgrbygid (struct database
*db
, int fd
, request_header
*req
,
237 void *key
, uid_t uid
)
239 /* Search for the entry matching the key. Please note that we don't
240 look again in the table whether the dataset is now available. We
241 simply insert it. It does not matter if it is in there twice. The
242 pruning function only will look at the timestamp. */
244 char *buffer
= alloca (buflen
);
245 struct group resultbuf
;
247 gid_t gid
= atol (key
);
251 dbg_log (_("Haven't found \"%d\" in group cache!"), gid
);
255 oldeuid
= geteuid ();
259 while (__getgrgid_r (gid
, &resultbuf
, buffer
, buflen
, &grp
) != 0
264 buffer
= alloca (buflen
);
270 cache_addgr (db
, fd
, req
, key
, grp
, uid
);