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. */
26 #include <sys/socket.h>
32 static int __nscd_getpw_r (const char *key
, request_type type
,
33 struct passwd
*resultbuf
, char *buffer
,
37 __nscd_getpwnam_r (const char *name
, struct passwd
*resultbuf
, char *buffer
,
43 return __nscd_getpw_r (name
, GETPWBYNAME
, resultbuf
, buffer
, buflen
);
47 __nscd_getpwuid_r (uid_t uid
, struct passwd
*resultbuf
, char *buffer
,
53 plen
= snprintf (buffer
, buflen
, "%d", uid
);
59 p
= buffer
+ plen
+ 1;
61 return __nscd_getpw_r (buffer
, GETPWBYUID
, resultbuf
, p
, buflen
- plen
-1);
64 /* Create a socket connected to a name. */
66 nscd_open_socket (void)
68 struct sockaddr_un addr
;
70 int saved_errno
= errno
;
72 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
75 __set_errno (saved_errno
);
79 addr
.sun_family
= AF_UNIX
;
80 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
81 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
84 __set_errno (saved_errno
);
92 __nscd_getpw_r (const char *key
, request_type type
, struct passwd
*resultbuf
,
93 char *buffer
, size_t buflen
)
95 int sock
= nscd_open_socket ();
97 pw_response_header pw_resp
;
103 req
.version
= NSCD_VERSION
;
105 req
.key_len
= strlen (key
);
106 nbytes
= write (sock
, &req
, sizeof (request_header
));
107 if (nbytes
!= sizeof (request_header
))
113 nbytes
= write (sock
, key
, req
.key_len
);
114 if (nbytes
!= req
.key_len
)
120 nbytes
= read (sock
, &pw_resp
, sizeof (pw_response_header
));
121 if (nbytes
!= sizeof (pw_response_header
))
127 if (pw_resp
.found
== -1)
133 if (pw_resp
.found
== 1)
138 if (buflen
< pw_resp
.pw_name_len
+ 1 + pw_resp
.pw_passwd_len
+ 1
139 + pw_resp
.pw_gecos_len
+ 1 + pw_resp
.pw_dir_len
+ 1
140 + pw_resp
.pw_shell_len
+ 1)
142 __set_errno (ERANGE
);
149 vec
[0].iov_len
= pw_resp
.pw_name_len
;
150 p
+= pw_resp
.pw_name_len
+ 1;
151 buflen
-= (pw_resp
.pw_name_len
+ 1);
154 vec
[1].iov_len
= pw_resp
.pw_passwd_len
;
155 p
+= pw_resp
.pw_passwd_len
+ 1;
156 buflen
-= (pw_resp
.pw_passwd_len
+ 1);
159 vec
[2].iov_len
= pw_resp
.pw_gecos_len
;
160 p
+= pw_resp
.pw_gecos_len
+ 1;
161 buflen
-= (pw_resp
.pw_gecos_len
+ 1);
164 vec
[3].iov_len
= pw_resp
.pw_dir_len
;
165 p
+= pw_resp
.pw_dir_len
+ 1;
166 buflen
-= (pw_resp
.pw_dir_len
+ 1);
169 vec
[4].iov_len
= pw_resp
.pw_dir_len
;
170 p
+= pw_resp
.pw_dir_len
+ 1;
171 buflen
-= (pw_resp
.pw_dir_len
+ 1);
173 nbytes
= readv (sock
, vec
, 5);
174 if (nbytes
!= pw_resp
.pw_name_len
+ 1 + pw_resp
.pw_passwd_len
+ 1 +
175 pw_resp
.pw_gecos_len
+ 1 + pw_resp
.pw_dir_len
+ 1 +
176 pw_resp
.pw_shell_len
+ 1)
182 resultbuf
->pw_name
= vec
[0].iov_base
;
183 resultbuf
->pw_name
[pw_resp
.pw_name_len
] = '\0';
184 resultbuf
->pw_passwd
= vec
[1].iov_base
;
185 resultbuf
->pw_passwd
[pw_resp
.pw_passwd_len
] = '\0';
186 resultbuf
->pw_uid
= pw_resp
.pw_uid
;
187 resultbuf
->pw_gid
= pw_resp
.pw_gid
;
188 resultbuf
->pw_gecos
= vec
[2].iov_base
;
189 resultbuf
->pw_gecos
[pw_resp
.pw_gecos_len
] = '\0';
190 resultbuf
->pw_dir
= vec
[3].iov_base
;
191 resultbuf
->pw_dir
[pw_resp
.pw_dir_len
] = '\0';
192 resultbuf
->pw_shell
= vec
[4].iov_base
;
193 resultbuf
->pw_shell
[pw_resp
.pw_shell_len
] = '\0';