Update.
[glibc.git] / nscd / nscd_getpw_r.c
blob4360f8fd7128f13cb2de24b31b027a9f3549e442
1 /* Copyright (C) 1998 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. */
20 #include <errno.h>
21 #include <pwd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/uio.h>
28 #include <sys/un.h>
30 #include "nscd-client.h"
31 #include "nscd_proto.h"
33 int __nss_not_use_nscd_passwd;
35 static int nscd_getpw_r (const char *key, request_type type,
36 struct passwd *resultbuf, char *buffer,
37 size_t buflen);
39 int
40 __nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
41 size_t buflen)
43 if (name == NULL)
44 return 1;
46 return nscd_getpw_r (name, GETPWBYNAME, resultbuf, buffer, buflen);
49 int
50 __nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
51 size_t buflen)
53 char *p = buffer;
54 int plen;
56 plen = __snprintf (buffer, buflen, "%d", uid);
57 if (plen == -1)
59 __set_errno (ERANGE);
60 return -1;
62 p = buffer + plen + 1;
64 return nscd_getpw_r (buffer, GETPWBYUID, resultbuf, p, buflen - plen - 1);
67 /* Create a socket connected to a name. */
68 static int
69 open_socket (void)
71 struct sockaddr_un addr;
72 int sock;
73 int saved_errno = errno;
75 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
76 if (sock < 0)
78 __set_errno (saved_errno);
79 return -1;
82 addr.sun_family = AF_UNIX;
83 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
84 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
86 __close (sock);
87 __set_errno (saved_errno);
88 return -1;
91 return sock;
94 static int
95 nscd_getpw_r (const char *key, request_type type, struct passwd *resultbuf,
96 char *buffer, size_t buflen)
98 int sock = open_socket ();
99 request_header req;
100 pw_response_header pw_resp;
101 ssize_t nbytes;
103 if (sock == -1)
105 __nss_not_use_nscd_passwd = 1;
106 return 1;
109 req.version = NSCD_VERSION;
110 req.type = type;
111 req.key_len = strlen (key) + 1;
112 nbytes = __write (sock, &req, sizeof (request_header));
113 if (nbytes != sizeof (request_header))
115 __close (sock);
116 return 1;
119 nbytes = __write (sock, key, req.key_len);
120 if (nbytes != req.key_len)
122 __close (sock);
123 return 1;
126 nbytes = __read (sock, &pw_resp, sizeof (pw_response_header));
127 if (nbytes != sizeof (pw_response_header))
129 __close (sock);
130 return 1;
133 if (pw_resp.found == -1)
135 /* The daemon does not cache this database. */
136 __close (sock);
137 __nss_not_use_nscd_passwd = 1;
138 return 1;
141 if (pw_resp.found == 1)
143 char *p = buffer;
144 size_t total = (pw_resp.pw_name_len + pw_resp.pw_passwd_len
145 + pw_resp.pw_gecos_len + pw_resp.pw_dir_len
146 + pw_resp.pw_shell_len);
148 if (buflen < total)
150 __set_errno (ERANGE);
151 __close (sock);
152 return -1;
155 /* Set the information we already have. */
156 resultbuf->pw_uid = pw_resp.pw_uid;
157 resultbuf->pw_gid = pw_resp.pw_gid;
159 /* get pw_name */
160 resultbuf->pw_name = p;
161 p += pw_resp.pw_name_len;
162 /* get pw_passwd */
163 resultbuf->pw_passwd = p;
164 p += pw_resp.pw_passwd_len;
165 /* get pw_gecos */
166 resultbuf->pw_gecos = p;
167 p += pw_resp.pw_gecos_len;
168 /* get pw_dir */
169 resultbuf->pw_dir = p;
170 p += pw_resp.pw_dir_len;
171 /* get pw_pshell */
172 resultbuf->pw_shell = p;
174 nbytes = __read (sock, buffer, total);
176 __close (sock);
178 return nbytes == total ? 0 : 1;
180 else
182 __close (sock);
183 return -1;