2005-02-22 23:58:32 +01:00
|
|
|
/* Copyright (C) 1998, 1999, 2003, 2004, 2005 Free Software Foundation, Inc.
|
1998-01-31 09:39:55 +01:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 06:58:11 +02:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1998-01-31 09:39:55 +01:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 06:58:11 +02:00
|
|
|
Lesser General Public License for more details.
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2001-07-06 06:58:11 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-09-08 17:46:42 +02:00
|
|
|
#include <assert.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
1999-01-11 21:13:43 +01:00
|
|
|
#include <stdint.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2004-09-08 17:46:42 +02:00
|
|
|
#include <sys/mman.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/un.h>
|
2004-06-06 08:06:02 +02:00
|
|
|
#include <not-cancel.h>
|
|
|
|
#include <stdio-common/_itoa.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
|
1998-10-19 02:40:00 +02:00
|
|
|
#include "nscd-client.h"
|
1998-11-30 15:21:53 +01:00
|
|
|
#include "nscd_proto.h"
|
1998-01-31 09:39:55 +01:00
|
|
|
|
1998-03-06 12:39:36 +01:00
|
|
|
int __nss_not_use_nscd_passwd;
|
|
|
|
|
1999-02-02 17:27:57 +01:00
|
|
|
static int nscd_getpw_r (const char *key, size_t keylen, request_type type,
|
1998-10-18 17:16:22 +02:00
|
|
|
struct passwd *resultbuf, char *buffer,
|
2004-02-27 01:55:39 +01:00
|
|
|
size_t buflen, struct passwd **result)
|
|
|
|
internal_function;
|
1998-01-31 09:39:55 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
__nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
|
2004-02-27 01:55:39 +01:00
|
|
|
size_t buflen, struct passwd **result)
|
1998-01-31 09:39:55 +01:00
|
|
|
{
|
|
|
|
if (name == NULL)
|
1999-06-17 14:33:08 +02:00
|
|
|
return -1;
|
1998-01-31 09:39:55 +01:00
|
|
|
|
1999-02-02 17:27:57 +01:00
|
|
|
return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
|
2004-02-27 01:55:39 +01:00
|
|
|
buffer, buflen, result);
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
|
2004-02-27 01:55:39 +01:00
|
|
|
size_t buflen, struct passwd **result)
|
1998-01-31 09:39:55 +01:00
|
|
|
{
|
2004-06-06 08:06:02 +02:00
|
|
|
char buf[3 * sizeof (uid_t)];
|
|
|
|
buf[sizeof (buf) - 1] = '\0';
|
|
|
|
char *cp = _itoa_word (uid, buf + sizeof (buf) - 1, 10, 0);
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-06-06 08:06:02 +02:00
|
|
|
return nscd_getpw_r (cp, buf + sizeof (buf) - cp, GETPWBYUID, resultbuf,
|
|
|
|
buffer, buflen, result);
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-10 08:50:03 +01:00
|
|
|
libc_locked_map_ptr (static, map_handle);
|
2004-09-08 17:46:42 +02:00
|
|
|
/* Note that we only free the structure if necessary. The memory
|
|
|
|
mapping is not removed since it is not visible to the malloc
|
|
|
|
handling. */
|
2004-11-10 08:50:03 +01:00
|
|
|
libc_freeres_fn (pw_map_free)
|
2004-09-08 17:46:42 +02:00
|
|
|
{
|
|
|
|
if (map_handle.mapped != NO_MAPPING)
|
2004-11-10 08:50:03 +01:00
|
|
|
{
|
|
|
|
void *p = map_handle.mapped;
|
|
|
|
map_handle.mapped = NO_MAPPING;
|
|
|
|
free (p);
|
|
|
|
}
|
2004-09-08 17:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-31 09:39:55 +01:00
|
|
|
static int
|
1999-02-02 17:27:57 +01:00
|
|
|
internal_function
|
|
|
|
nscd_getpw_r (const char *key, size_t keylen, request_type type,
|
2004-02-27 01:55:39 +01:00
|
|
|
struct passwd *resultbuf, char *buffer, size_t buflen,
|
|
|
|
struct passwd **result)
|
1998-01-31 09:39:55 +01:00
|
|
|
{
|
2004-09-30 10:08:40 +02:00
|
|
|
int gc_cycle;
|
|
|
|
/* If the mapping is available, try to search there instead of
|
|
|
|
communicating with the nscd. */
|
|
|
|
struct mapped_database *mapped;
|
|
|
|
mapped = __nscd_get_map_ref (GETFDPW, "passwd", &map_handle, &gc_cycle);
|
|
|
|
|
|
|
|
retry:;
|
2004-12-22 21:10:10 +01:00
|
|
|
const pw_response_header *pw_resp = NULL;
|
2004-09-08 17:46:42 +02:00
|
|
|
const char *pw_name = NULL;
|
|
|
|
int retval = -1;
|
|
|
|
const char *recend = (const char *) ~UINTMAX_C (0);
|
|
|
|
|
|
|
|
if (mapped != NO_MAPPING)
|
1998-03-06 12:39:36 +01:00
|
|
|
{
|
2004-12-22 21:10:10 +01:00
|
|
|
const struct datahead *found = __nscd_cache_search (type, key, keylen,
|
|
|
|
mapped);
|
2004-09-08 17:46:42 +02:00
|
|
|
if (found != NULL)
|
|
|
|
{
|
2004-12-22 21:10:10 +01:00
|
|
|
pw_resp = &found->data[0].pwdata;
|
|
|
|
pw_name = (const char *) (pw_resp + 1);
|
2004-09-08 17:46:42 +02:00
|
|
|
recend = (const char *) found->data + found->recsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
pw_response_header pw_resp_mem;
|
2004-09-08 17:46:42 +02:00
|
|
|
int sock = -1;
|
2004-12-22 21:10:10 +01:00
|
|
|
if (pw_resp == NULL)
|
2004-09-08 17:46:42 +02:00
|
|
|
{
|
2004-12-22 21:10:10 +01:00
|
|
|
sock = __nscd_open_socket (key, keylen, type, &pw_resp_mem,
|
|
|
|
sizeof (pw_resp_mem));
|
2004-09-08 17:46:42 +02:00
|
|
|
if (sock == -1)
|
|
|
|
{
|
|
|
|
__nss_not_use_nscd_passwd = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2004-12-22 21:10:10 +01:00
|
|
|
|
|
|
|
pw_resp = &pw_resp_mem;
|
1998-03-06 12:39:36 +01:00
|
|
|
}
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-02-27 01:55:39 +01:00
|
|
|
/* No value found so far. */
|
|
|
|
*result = NULL;
|
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
if (__builtin_expect (pw_resp->found == -1, 0))
|
1998-01-31 09:39:55 +01:00
|
|
|
{
|
1998-03-06 12:39:36 +01:00
|
|
|
/* The daemon does not cache this database. */
|
|
|
|
__nss_not_use_nscd_passwd = 1;
|
2004-09-08 17:46:42 +02:00
|
|
|
goto out_close;
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
if (pw_resp->found == 1)
|
1998-01-31 09:39:55 +01:00
|
|
|
{
|
1998-10-18 17:16:22 +02:00
|
|
|
/* Set the information we already have. */
|
2004-12-22 21:10:10 +01:00
|
|
|
resultbuf->pw_uid = pw_resp->pw_uid;
|
|
|
|
resultbuf->pw_gid = pw_resp->pw_gid;
|
1998-10-18 17:16:22 +02:00
|
|
|
|
2004-09-08 17:46:42 +02:00
|
|
|
char *p = buffer;
|
1998-01-31 09:39:55 +01:00
|
|
|
/* get pw_name */
|
1998-10-18 17:16:22 +02:00
|
|
|
resultbuf->pw_name = p;
|
2004-12-22 21:10:10 +01:00
|
|
|
p += pw_resp->pw_name_len;
|
1998-01-31 09:39:55 +01:00
|
|
|
/* get pw_passwd */
|
1998-10-18 17:16:22 +02:00
|
|
|
resultbuf->pw_passwd = p;
|
2004-12-22 21:10:10 +01:00
|
|
|
p += pw_resp->pw_passwd_len;
|
1998-01-31 09:39:55 +01:00
|
|
|
/* get pw_gecos */
|
1998-10-18 17:16:22 +02:00
|
|
|
resultbuf->pw_gecos = p;
|
2004-12-22 21:10:10 +01:00
|
|
|
p += pw_resp->pw_gecos_len;
|
1998-01-31 09:39:55 +01:00
|
|
|
/* get pw_dir */
|
1998-10-18 17:16:22 +02:00
|
|
|
resultbuf->pw_dir = p;
|
2004-12-22 21:10:10 +01:00
|
|
|
p += pw_resp->pw_dir_len;
|
1998-01-31 09:39:55 +01:00
|
|
|
/* get pw_pshell */
|
1998-10-18 17:16:22 +02:00
|
|
|
resultbuf->pw_shell = p;
|
2004-12-22 21:10:10 +01:00
|
|
|
p += pw_resp->pw_shell_len;
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-09-08 17:46:42 +02:00
|
|
|
ssize_t total = p - buffer;
|
|
|
|
if (__builtin_expect (pw_name + total > recend, 0))
|
|
|
|
goto out_close;
|
|
|
|
if (__builtin_expect (buflen < total, 0))
|
|
|
|
{
|
|
|
|
__set_errno (ERANGE);
|
|
|
|
retval = ERANGE;
|
|
|
|
goto out_close;
|
|
|
|
}
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-09-08 17:46:42 +02:00
|
|
|
retval = 0;
|
|
|
|
if (pw_name == NULL)
|
|
|
|
{
|
2005-02-22 23:58:32 +01:00
|
|
|
ssize_t nbytes = __readall (sock, buffer, total);
|
2004-09-08 17:46:42 +02:00
|
|
|
|
|
|
|
if (__builtin_expect (nbytes != total, 0))
|
|
|
|
{
|
|
|
|
/* The `errno' to some value != ERANGE. */
|
|
|
|
__set_errno (ENOENT);
|
|
|
|
retval = ENOENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
else
|
2004-02-27 01:55:39 +01:00
|
|
|
{
|
2004-09-08 17:46:42 +02:00
|
|
|
/* Copy the various strings. */
|
|
|
|
memcpy (resultbuf->pw_name, pw_name, total);
|
|
|
|
|
2004-11-10 08:50:03 +01:00
|
|
|
/* Try to detect corrupt databases. */
|
2004-12-22 21:10:10 +01:00
|
|
|
if (resultbuf->pw_name[pw_resp->pw_name_len - 1] != '\0'
|
|
|
|
|| resultbuf->pw_passwd[pw_resp->pw_passwd_len - 1] != '\0'
|
|
|
|
|| resultbuf->pw_gecos[pw_resp->pw_gecos_len - 1] != '\0'
|
|
|
|
|| resultbuf->pw_dir[pw_resp->pw_dir_len - 1] != '\0'
|
|
|
|
|| resultbuf->pw_shell[pw_resp->pw_shell_len - 1] != '\0')
|
2004-11-10 08:50:03 +01:00
|
|
|
{
|
|
|
|
/* We cannot use the database. */
|
2004-12-22 21:10:10 +01:00
|
|
|
retval = -1;
|
2004-11-10 08:50:03 +01:00
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2004-02-27 01:55:39 +01:00
|
|
|
*result = resultbuf;
|
|
|
|
}
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-03-14 22:12:49 +01:00
|
|
|
/* The `errno' to some value != ERANGE. */
|
|
|
|
__set_errno (ENOENT);
|
2004-02-27 01:55:39 +01:00
|
|
|
/* Even though we have not found anything, the result is zero. */
|
|
|
|
retval = 0;
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|
2003-04-28 10:17:29 +02:00
|
|
|
|
2004-09-08 17:46:42 +02:00
|
|
|
out_close:
|
|
|
|
if (sock != -1)
|
|
|
|
close_not_cancel_no_status (sock);
|
2003-04-28 10:17:29 +02:00
|
|
|
out:
|
2004-12-22 21:10:10 +01:00
|
|
|
if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1)
|
2004-09-30 10:08:40 +02:00
|
|
|
{
|
|
|
|
/* When we come here this means there has been a GC cycle while we
|
|
|
|
were looking for the data. This means the data might have been
|
|
|
|
inconsistent. Retry if possible. */
|
2004-12-22 21:10:10 +01:00
|
|
|
if ((gc_cycle & 1) != 0)
|
2004-09-30 10:08:40 +02:00
|
|
|
{
|
|
|
|
/* nscd is just running gc now. Disable using the mapping. */
|
2004-12-22 21:10:10 +01:00
|
|
|
__nscd_unmap (mapped);
|
2004-09-30 10:08:40 +02:00
|
|
|
mapped = NO_MAPPING;
|
|
|
|
}
|
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
goto retry;
|
2004-09-30 10:08:40 +02:00
|
|
|
}
|
2003-04-28 10:17:29 +02:00
|
|
|
|
2004-02-27 01:55:39 +01:00
|
|
|
return retval;
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|