1 /* Copyright (C) 1998, 1999, 2003, 2004 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
27 #include <sys/socket.h>
30 #include <not-cancel.h>
31 #include <stdio-common/_itoa.h>
33 #include "nscd-client.h"
34 #include "nscd_proto.h"
36 int __nss_not_use_nscd_passwd
;
38 static int nscd_getpw_r (const char *key
, size_t keylen
, request_type type
,
39 struct passwd
*resultbuf
, char *buffer
,
40 size_t buflen
, struct passwd
**result
)
44 __nscd_getpwnam_r (const char *name
, struct passwd
*resultbuf
, char *buffer
,
45 size_t buflen
, struct passwd
**result
)
50 return nscd_getpw_r (name
, strlen (name
) + 1, GETPWBYNAME
, resultbuf
,
51 buffer
, buflen
, result
);
55 __nscd_getpwuid_r (uid_t uid
, struct passwd
*resultbuf
, char *buffer
,
56 size_t buflen
, struct passwd
**result
)
58 char buf
[3 * sizeof (uid_t
)];
59 buf
[sizeof (buf
) - 1] = '\0';
60 char *cp
= _itoa_word (uid
, buf
+ sizeof (buf
) - 1, 10, 0);
62 return nscd_getpw_r (cp
, buf
+ sizeof (buf
) - cp
, GETPWBYUID
, resultbuf
,
63 buffer
, buflen
, result
);
69 nscd_getpw_r (const char *key
, size_t keylen
, request_type type
,
70 struct passwd
*resultbuf
, char *buffer
, size_t buflen
,
71 struct passwd
**result
)
73 pw_response_header pw_resp
;
74 int sock
= __nscd_open_socket (key
, keylen
, type
, &pw_resp
,
78 __nss_not_use_nscd_passwd
= 1;
82 /* No value found so far. */
86 if (__builtin_expect (pw_resp
.found
== -1, 0))
88 /* The daemon does not cache this database. */
89 __nss_not_use_nscd_passwd
= 1;
93 if (pw_resp
.found
== 1)
96 size_t total
= (pw_resp
.pw_name_len
+ pw_resp
.pw_passwd_len
97 + pw_resp
.pw_gecos_len
+ pw_resp
.pw_dir_len
98 + pw_resp
.pw_shell_len
);
100 if (__builtin_expect (buflen
< total
, 0))
102 __set_errno (ERANGE
);
107 /* Set the information we already have. */
108 resultbuf
->pw_uid
= pw_resp
.pw_uid
;
109 resultbuf
->pw_gid
= pw_resp
.pw_gid
;
112 resultbuf
->pw_name
= p
;
113 p
+= pw_resp
.pw_name_len
;
115 resultbuf
->pw_passwd
= p
;
116 p
+= pw_resp
.pw_passwd_len
;
118 resultbuf
->pw_gecos
= p
;
119 p
+= pw_resp
.pw_gecos_len
;
121 resultbuf
->pw_dir
= p
;
122 p
+= pw_resp
.pw_dir_len
;
124 resultbuf
->pw_shell
= p
;
126 ssize_t nbytes
= TEMP_FAILURE_RETRY (__read (sock
, buffer
, total
));
128 if (nbytes
== (ssize_t
) total
)
136 /* The `errno' to some value != ERANGE. */
137 __set_errno (ENOENT
);
138 /* Even though we have not found anything, the result is zero. */
143 close_not_cancel_no_status (sock
);