1 /* Cache handling for passwd 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.
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. */
35 /* This is the standard reply in case the service is disabled. */
36 static const pw_response_header disabled
=
38 version
: NSCD_VERSION
,
49 /* This is the struct describing how to write this record. */
50 const struct iovec pwd_iov_disabled
=
52 iov_base
: (void *) &disabled
,
53 iov_len
: sizeof (disabled
)
57 /* This is the standard reply in case we haven't found the dataset. */
58 static const pw_response_header notfound
=
60 version
: NSCD_VERSION
,
71 /* This is the struct describing how to write this record. */
72 static const struct iovec iov_notfound
=
74 iov_base
: (void *) ¬found
,
75 iov_len
: sizeof (notfound
)
81 pw_response_header resp
;
87 cache_addpw (struct database
*db
, int fd
, request_header
*req
, void *key
,
88 struct passwd
*pwd
, uid_t owner
)
92 time_t t
= time (NULL
);
96 /* We have no data. This means we send the standard reply for this
100 total
= sizeof (notfound
);
102 written
= writev (fd
, &iov_notfound
, 1);
104 copy
= malloc (req
->key_len
);
106 error (EXIT_FAILURE
, errno
, _("while allocating key copy"));
107 memcpy (copy
, key
, req
->key_len
);
109 /* Compute the timeout time. */
112 /* Now get the lock to safely insert the records. */
113 pthread_rwlock_rdlock (&db
->lock
);
115 cache_add (req
->type
, copy
, req
->key_len
, ¬found
,
116 sizeof (notfound
), (void *) -1, 0, t
, db
, owner
);
118 pthread_rwlock_unlock (&db
->lock
);
122 /* Determine the I/O structure. */
123 struct passwddata
*data
;
124 size_t pw_name_len
= strlen (pwd
->pw_name
) + 1;
125 size_t pw_passwd_len
= strlen (pwd
->pw_passwd
) + 1;
126 size_t pw_gecos_len
= strlen (pwd
->pw_gecos
) + 1;
127 size_t pw_dir_len
= strlen (pwd
->pw_dir
) + 1;
128 size_t pw_shell_len
= strlen (pwd
->pw_shell
) + 1;
133 /* We need this to insert the `byuid' entry. */
134 n
= snprintf (buf
, sizeof (buf
), "%d", pwd
->pw_uid
) + 1;
136 /* We allocate all data in one memory block: the iov vector,
137 the response header and the dataset itself. */
138 total
= (sizeof (struct passwddata
) + pw_name_len
+ pw_passwd_len
139 + pw_gecos_len
+ pw_dir_len
+ pw_shell_len
);
140 data
= (struct passwddata
*) malloc (total
+ n
);
142 /* There is no reason to go on. */
143 error (EXIT_FAILURE
, errno
, _("while allocating cache entry"));
145 data
->resp
.found
= 1;
146 data
->resp
.pw_name_len
= pw_name_len
;
147 data
->resp
.pw_passwd_len
= pw_passwd_len
;
148 data
->resp
.pw_uid
= pwd
->pw_uid
;
149 data
->resp
.pw_gid
= pwd
->pw_gid
;
150 data
->resp
.pw_gecos_len
= pw_gecos_len
;
151 data
->resp
.pw_dir_len
= pw_dir_len
;
152 data
->resp
.pw_shell_len
= pw_shell_len
;
156 /* Copy the strings over into the buffer. */
157 cp
= mempcpy (cp
, pwd
->pw_name
, pw_name_len
);
158 cp
= mempcpy (cp
, pwd
->pw_passwd
, pw_passwd_len
);
159 cp
= mempcpy (cp
, pwd
->pw_gecos
, pw_gecos_len
);
160 cp
= mempcpy (cp
, pwd
->pw_dir
, pw_dir_len
);
161 cp
= mempcpy (cp
, pwd
->pw_shell
, pw_shell_len
);
163 /* Finally the stringified UID value. */
166 /* We write the dataset before inserting it to the database
167 since while inserting this thread might block and so would
168 unnecessarily let the receiver wait. */
169 written
= write (fd
, &data
->resp
, total
);
171 /* Compute the timeout time. */
174 /* Now get the lock to safely insert the records. */
175 pthread_rwlock_rdlock (&db
->lock
);
177 /* We have to add the value for both, byname and byuid. */
178 cache_add (GETPWBYNAME
, data
->strdata
, pw_name_len
, data
,
179 total
, data
, 0, t
, db
, owner
);
181 cache_add (GETPWBYUID
, cp
, n
, data
, total
, data
, 1, t
, db
, owner
);
183 pthread_rwlock_unlock (&db
->lock
);
186 if (written
!= total
)
189 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
190 strerror_r (errno
, buf
, sizeof (buf
)));
196 addpwbyname (struct database
*db
, int fd
, request_header
*req
,
197 void *key
, uid_t c_uid
)
199 /* Search for the entry matching the key. Please note that we don't
200 look again in the table whether the dataset is now available. We
201 simply insert it. It does not matter if it is in there twice. The
202 pruning function only will look at the timestamp. */
204 char *buffer
= alloca (buflen
);
205 struct passwd resultbuf
;
210 dbg_log (_("Haven't found \"%s\" in password cache!"), (char *)key
);
214 oldeuid
= geteuid ();
218 while (__getpwnam_r (key
, &resultbuf
, buffer
, buflen
, &pwd
) != 0
223 buffer
= alloca (buflen
);
229 cache_addpw (db
, fd
, req
, key
, pwd
, c_uid
);
234 addpwbyuid (struct database
*db
, int fd
, request_header
*req
,
235 void *key
, uid_t c_uid
)
237 /* Search for the entry matching the key. Please note that we don't
238 look again in the table whether the dataset is now available. We
239 simply insert it. It does not matter if it is in there twice. The
240 pruning function only will look at the timestamp. */
242 char *buffer
= alloca (buflen
);
243 struct passwd resultbuf
;
245 uid_t uid
= atol (key
);
249 dbg_log (_("Haven't found \"%d\" in password cache!"), uid
);
253 oldeuid
= geteuid ();
257 while (__getpwuid_r (uid
, &resultbuf
, buffer
, buflen
, &pwd
) != 0
262 buffer
= alloca (buflen
);
268 cache_addpw (db
, fd
, req
, key
, pwd
, c_uid
);