]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd_getpw_r.c
* posix/fnmatch_loop.c: Add casts for signedness.
[thirdparty/glibc.git] / nscd / nscd_getpw_r.c
1 /* Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <errno.h>
21 #include <pwd.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <sys/uio.h>
29 #include <sys/un.h>
30
31 #include "nscd-client.h"
32 #include "nscd_proto.h"
33
34 int __nss_not_use_nscd_passwd;
35
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;
39
40 int
41 __nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
42 size_t buflen)
43 {
44 if (name == NULL)
45 return -1;
46
47 return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
48 buffer, buflen);
49 }
50
51 int
52 __nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
53 size_t buflen)
54 {
55 char buf[12];
56 size_t n;
57
58 n = __snprintf (buf, sizeof (buf), "%d", uid) + 1;
59
60 return nscd_getpw_r (buf, n, GETPWBYUID, resultbuf, buffer, buflen);
61 }
62
63 /* Create a socket connected to a name. */
64 static int
65 open_socket (void)
66 {
67 struct sockaddr_un addr;
68 int sock;
69 int saved_errno = errno;
70
71 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
72 if (sock < 0)
73 {
74 __set_errno (saved_errno);
75 return -1;
76 }
77
78 addr.sun_family = AF_UNIX;
79 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
80 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
81 {
82 __close (sock);
83 __set_errno (saved_errno);
84 return -1;
85 }
86
87 return sock;
88 }
89
90 static int
91 internal_function
92 nscd_getpw_r (const char *key, size_t keylen, request_type type,
93 struct passwd *resultbuf, char *buffer, size_t buflen)
94 {
95 int sock = open_socket ();
96 request_header req;
97 pw_response_header pw_resp;
98 ssize_t nbytes;
99 struct iovec vec[2];
100
101 if (sock == -1)
102 {
103 __nss_not_use_nscd_passwd = 1;
104 return -1;
105 }
106
107 req.version = NSCD_VERSION;
108 req.type = type;
109 req.key_len = keylen;
110
111 vec[0].iov_base = &req;
112 vec[0].iov_len = sizeof (request_header);
113 vec[1].iov_base = (void *) key;
114 vec[1].iov_len = keylen;
115
116 nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
117 if (nbytes != (ssize_t) (sizeof (request_header) + keylen))
118 {
119 __close (sock);
120 return -1;
121 }
122
123 nbytes = TEMP_FAILURE_RETRY (__read (sock, &pw_resp,
124 sizeof (pw_response_header)));
125 if (nbytes != (ssize_t) sizeof (pw_response_header))
126 {
127 __close (sock);
128 return -1;
129 }
130
131 if (pw_resp.found == -1)
132 {
133 /* The daemon does not cache this database. */
134 __close (sock);
135 __nss_not_use_nscd_passwd = 1;
136 return -1;
137 }
138
139 if (pw_resp.found == 1)
140 {
141 char *p = buffer;
142 size_t total = (pw_resp.pw_name_len + pw_resp.pw_passwd_len
143 + pw_resp.pw_gecos_len + pw_resp.pw_dir_len
144 + pw_resp.pw_shell_len);
145
146 if (buflen < total)
147 {
148 __set_errno (ERANGE);
149 __close (sock);
150 return ERANGE;
151 }
152
153 /* Set the information we already have. */
154 resultbuf->pw_uid = pw_resp.pw_uid;
155 resultbuf->pw_gid = pw_resp.pw_gid;
156
157 /* get pw_name */
158 resultbuf->pw_name = p;
159 p += pw_resp.pw_name_len;
160 /* get pw_passwd */
161 resultbuf->pw_passwd = p;
162 p += pw_resp.pw_passwd_len;
163 /* get pw_gecos */
164 resultbuf->pw_gecos = p;
165 p += pw_resp.pw_gecos_len;
166 /* get pw_dir */
167 resultbuf->pw_dir = p;
168 p += pw_resp.pw_dir_len;
169 /* get pw_pshell */
170 resultbuf->pw_shell = p;
171
172 nbytes = TEMP_FAILURE_RETRY (__read (sock, buffer, total));
173
174 __close (sock);
175
176 return nbytes == (ssize_t) total ? 0 : -1;
177 }
178 else
179 {
180 __close (sock);
181 /* The `errno' to some value != ERANGE. */
182 __set_errno (ENOENT);
183 return ENOENT;
184 }
185 }