1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 #include <sys/socket.h>
31 #include "nscd-client.h"
32 #include "nscd_proto.h"
34 int __nss_not_use_nscd_passwd;
36 static int nscd_getpw_r (const char *key, size_t keylen, request_type type,
37 struct passwd *resultbuf, char *buffer,
38 size_t buflen) internal_function;
41 __nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
47 return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
52 __nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
58 n = __snprintf (buf, sizeof (buf), "%d", uid) + 1;
60 return nscd_getpw_r (buf, n, GETPWBYUID, resultbuf, buffer, buflen);
63 /* Create a socket connected to a name. */
67 struct sockaddr_un addr;
69 int saved_errno = errno;
71 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
74 __set_errno (saved_errno);
78 addr.sun_family = AF_UNIX;
79 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
80 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
83 __set_errno (saved_errno);
92 nscd_getpw_r (const char *key, size_t keylen, request_type type,
93 struct passwd *resultbuf, char *buffer, size_t buflen)
95 int sock = open_socket ();
97 pw_response_header pw_resp;
102 __nss_not_use_nscd_passwd = 1;
106 req.version = NSCD_VERSION;
108 req.key_len = keylen;
109 nbytes = __write (sock, &req, sizeof (request_header));
110 if (nbytes != sizeof (request_header))
116 nbytes = __write (sock, key, keylen);
117 if (nbytes != keylen)
123 nbytes = __read (sock, &pw_resp, sizeof (pw_response_header));
124 if (nbytes != sizeof (pw_response_header))
130 if (pw_resp.found == -1)
132 /* The daemon does not cache this database. */
134 __nss_not_use_nscd_passwd = 1;
138 if (pw_resp.found == 1)
141 size_t total = (pw_resp.pw_name_len + pw_resp.pw_passwd_len
142 + pw_resp.pw_gecos_len + pw_resp.pw_dir_len
143 + pw_resp.pw_shell_len);
147 __set_errno (ERANGE);
152 /* Set the information we already have. */
153 resultbuf->pw_uid = pw_resp.pw_uid;
154 resultbuf->pw_gid = pw_resp.pw_gid;
157 resultbuf->pw_name = p;
158 p += pw_resp.pw_name_len;
160 resultbuf->pw_passwd = p;
161 p += pw_resp.pw_passwd_len;
163 resultbuf->pw_gecos = p;
164 p += pw_resp.pw_gecos_len;
166 resultbuf->pw_dir = p;
167 p += pw_resp.pw_dir_len;
169 resultbuf->pw_shell = p;
171 nbytes = __read (sock, buffer, total);
175 return nbytes == total ? 0 : -1;
180 /* The `errno' to some value != ERANGE. */
181 __set_errno (ENOENT);