semtimedop implementation for Linux/m68k.
[glibc.git] / nscd / nscd_getpw_r.c
blobabd059e5392acfd32259f9d018aa127a7eda1b1b
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.
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.
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.
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. */
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>
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;
40 int
41 __nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
42 size_t buflen)
44 if (name == NULL)
45 return -1;
47 return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
48 buffer, buflen);
51 int
52 __nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
53 size_t buflen)
55 char buf[12];
56 size_t n;
58 n = __snprintf (buf, sizeof (buf), "%d", uid) + 1;
60 return nscd_getpw_r (buf, n, GETPWBYUID, resultbuf, buffer, buflen);
64 static int
65 internal_function
66 nscd_getpw_r (const char *key, size_t keylen, request_type type,
67 struct passwd *resultbuf, char *buffer, size_t buflen)
69 int sock = __nscd_open_socket ();
70 request_header req;
71 pw_response_header pw_resp;
72 ssize_t nbytes;
73 struct iovec vec[2];
75 if (sock == -1)
77 __nss_not_use_nscd_passwd = 1;
78 return -1;
81 req.version = NSCD_VERSION;
82 req.type = type;
83 req.key_len = keylen;
85 vec[0].iov_base = &req;
86 vec[0].iov_len = sizeof (request_header);
87 vec[1].iov_base = (void *) key;
88 vec[1].iov_len = keylen;
90 nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
91 if (nbytes != (ssize_t) (sizeof (request_header) + keylen))
93 __close (sock);
94 return -1;
97 nbytes = TEMP_FAILURE_RETRY (__read (sock, &pw_resp,
98 sizeof (pw_response_header)));
99 if (nbytes != (ssize_t) sizeof (pw_response_header))
101 __close (sock);
102 return -1;
105 if (pw_resp.found == -1)
107 /* The daemon does not cache this database. */
108 __close (sock);
109 __nss_not_use_nscd_passwd = 1;
110 return -1;
113 if (pw_resp.found == 1)
115 char *p = buffer;
116 size_t total = (pw_resp.pw_name_len + pw_resp.pw_passwd_len
117 + pw_resp.pw_gecos_len + pw_resp.pw_dir_len
118 + pw_resp.pw_shell_len);
120 if (buflen < total)
122 __set_errno (ERANGE);
123 __close (sock);
124 return ERANGE;
127 /* Set the information we already have. */
128 resultbuf->pw_uid = pw_resp.pw_uid;
129 resultbuf->pw_gid = pw_resp.pw_gid;
131 /* get pw_name */
132 resultbuf->pw_name = p;
133 p += pw_resp.pw_name_len;
134 /* get pw_passwd */
135 resultbuf->pw_passwd = p;
136 p += pw_resp.pw_passwd_len;
137 /* get pw_gecos */
138 resultbuf->pw_gecos = p;
139 p += pw_resp.pw_gecos_len;
140 /* get pw_dir */
141 resultbuf->pw_dir = p;
142 p += pw_resp.pw_dir_len;
143 /* get pw_pshell */
144 resultbuf->pw_shell = p;
146 nbytes = TEMP_FAILURE_RETRY (__read (sock, buffer, total));
148 __close (sock);
150 return nbytes == (ssize_t) total ? 0 : -1;
152 else
154 __close (sock);
155 /* The `errno' to some value != ERANGE. */
156 __set_errno (ENOENT);
157 return ENOENT;