1158115Sume/*-
2158115Sume * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3158115Sume * All rights reserved.
4158115Sume *
5158115Sume * Redistribution and use in source and binary forms, with or without
6158115Sume * modification, are permitted provided that the following conditions
7158115Sume * are met:
8158115Sume * 1. Redistributions of source code must retain the above copyright
9158115Sume *    notice, this list of conditions and the following disclaimer.
10158115Sume * 2. Redistributions in binary form must reproduce the above copyright
11158115Sume *    notice, this list of conditions and the following disclaimer in the
12158115Sume *    documentation and/or other materials provided with the distribution.
13158115Sume *
14158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17158115Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24158115Sume * SUCH DAMAGE.
25158115Sume *
26158115Sume */
27158115Sume
28158115Sume#include <sys/cdefs.h>
29158115Sume__FBSDID("$FreeBSD$");
30158115Sume
31158115Sume#include <sys/types.h>
32194093Sdes
33194093Sdes#include <sys/event.h>
34158115Sume#include <sys/socket.h>
35158115Sume#include <sys/uio.h>
36158115Sume#include <sys/un.h>
37194093Sdes
38158115Sume#include <assert.h>
39158115Sume#include <errno.h>
40158115Sume#include <fcntl.h>
41158115Sume#include <stdlib.h>
42158115Sume#include <string.h>
43158115Sume#include <unistd.h>
44158115Sume
45158115Sume#include "debug.h"
46171795Sbushman#include "nscdcli.h"
47158115Sume#include "protocol.h"
48158115Sume
49171795Sbushman#define DEFAULT_NSCD_IO_TIMEOUT	4
50158115Sume
51171795Sbushmanstatic int safe_write(struct nscd_connection_ *, const void *, size_t);
52171795Sbushmanstatic int safe_read(struct nscd_connection_ *, void *, size_t);
53171795Sbushmanstatic int send_credentials(struct nscd_connection_ *, int);
54158115Sume
55158115Sumestatic int
56171795Sbushmansafe_write(struct nscd_connection_ *connection, const void *data,
57158115Sume	size_t data_size)
58158115Sume{
59158115Sume	struct kevent eventlist;
60158115Sume	int	nevents;
61158115Sume	size_t result;
62158115Sume	ssize_t s_result;
63158115Sume	struct timespec	timeout;
64158115Sume
65158115Sume	if (data_size == 0)
66158115Sume		return (0);
67158115Sume
68171795Sbushman	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
69158115Sume	timeout.tv_nsec = 0;
70158115Sume	result = 0;
71158115Sume	do {
72158115Sume		nevents = kevent(connection->write_queue, NULL, 0, &eventlist,
73158115Sume	    		1, &timeout);
74158115Sume		if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
75194096Sdes			s_result = write(connection->sockfd,
76194096Sdes				(char *)data + result,
77194096Sdes				(size_t)eventlist.data < data_size - result ?
78194096Sdes		    		(size_t)eventlist.data : data_size - result);
79158115Sume			if (s_result == -1)
80158115Sume				return (-1);
81158115Sume			else
82158115Sume				result += s_result;
83158115Sume
84158115Sume			if (eventlist.flags & EV_EOF)
85158115Sume				return (result < data_size ? -1 : 0);
86158115Sume		} else
87158115Sume			return (-1);
88158115Sume	} while (result < data_size);
89158115Sume
90158115Sume	return (0);
91158115Sume}
92158115Sume
93158115Sumestatic int
94171795Sbushmansafe_read(struct nscd_connection_ *connection, void *data, size_t data_size)
95158115Sume{
96158115Sume	struct kevent eventlist;
97158115Sume	size_t result;
98158115Sume	ssize_t s_result;
99158115Sume	struct timespec timeout;
100158115Sume	int nevents;
101158115Sume
102158115Sume	if (data_size == 0)
103158115Sume		return (0);
104158115Sume
105171795Sbushman	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
106158115Sume	timeout.tv_nsec = 0;
107158115Sume	result = 0;
108158115Sume	do {
109158115Sume		nevents = kevent(connection->read_queue, NULL, 0, &eventlist, 1,
110158115Sume			&timeout);
111158115Sume		if ((nevents == 1) && (eventlist.filter == EVFILT_READ)) {
112194096Sdes			s_result = read(connection->sockfd,
113194096Sdes				(char *)data + result,
114194096Sdes				(size_t)eventlist.data <= data_size - result ?
115194096Sdes				(size_t)eventlist.data : data_size - result);
116158115Sume			if (s_result == -1)
117158115Sume				return (-1);
118158115Sume			else
119158115Sume				result += s_result;
120158115Sume
121158115Sume			if (eventlist.flags & EV_EOF)
122158115Sume				return (result < data_size ? -1 : 0);
123158115Sume		} else
124158115Sume			return (-1);
125158115Sume	} while (result < data_size);
126158115Sume
127158115Sume	return (0);
128158115Sume}
129158115Sume
130158115Sumestatic int
131171795Sbushmansend_credentials(struct nscd_connection_ *connection, int type)
132158115Sume{
133158115Sume	struct kevent eventlist;
134158115Sume	int nevents;
135158115Sume	ssize_t result;
136158115Sume	int res;
137158115Sume
138158115Sume	struct msghdr	cred_hdr;
139158115Sume	struct iovec	iov;
140158115Sume
141158115Sume	struct {
142158115Sume		struct cmsghdr	hdr;
143158115Sume		struct cmsgcred	creds;
144158115Sume	} cmsg;
145158115Sume
146158115Sume	TRACE_IN(send_credentials);
147158115Sume	memset(&cmsg, 0, sizeof(cmsg));
148158115Sume	cmsg.hdr.cmsg_len = sizeof(cmsg);
149158115Sume	cmsg.hdr.cmsg_level = SOL_SOCKET;
150158115Sume	cmsg.hdr.cmsg_type = SCM_CREDS;
151158115Sume
152158115Sume	memset(&cred_hdr, 0, sizeof(struct msghdr));
153158115Sume	cred_hdr.msg_iov = &iov;
154158115Sume	cred_hdr.msg_iovlen = 1;
155158115Sume	cred_hdr.msg_control = &cmsg;
156158115Sume	cred_hdr.msg_controllen = sizeof(cmsg);
157158115Sume
158158115Sume	iov.iov_base = &type;
159158115Sume	iov.iov_len = sizeof(int);
160158115Sume
161158115Sume	EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
162158115Sume		NOTE_LOWAT, sizeof(int), NULL);
163158115Sume	res = kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
164158115Sume
165158115Sume	nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 1, NULL);
166158115Sume	if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
167158115Sume		result = (sendmsg(connection->sockfd, &cred_hdr, 0) == -1) ? -1
168158115Sume	    		: 0;
169158115Sume		EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
170158115Sume			0, 0, NULL);
171158115Sume		kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
172158115Sume		TRACE_OUT(send_credentials);
173158115Sume		return (result);
174158115Sume	} else {
175158115Sume		TRACE_OUT(send_credentials);
176158115Sume		return (-1);
177158115Sume	}
178158115Sume}
179158115Sume
180171795Sbushmanstruct nscd_connection_ *
181171795Sbushmanopen_nscd_connection__(struct nscd_connection_params const *params)
182158115Sume{
183171795Sbushman	struct nscd_connection_ *retval;
184158115Sume	struct kevent eventlist;
185158115Sume	struct sockaddr_un	client_address;
186158115Sume	int client_address_len, client_socket;
187158115Sume	int res;
188158115Sume
189171795Sbushman	TRACE_IN(open_nscd_connection);
190158115Sume	assert(params != NULL);
191158115Sume
192158115Sume	client_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
193158115Sume	client_address.sun_family = PF_LOCAL;
194184188Sdelphij	strlcpy(client_address.sun_path, params->socket_path,
195158115Sume		sizeof(client_address.sun_path));
196158115Sume	client_address_len = sizeof(client_address.sun_family) +
197158115Sume		strlen(client_address.sun_path) + 1;
198158115Sume
199158115Sume	res = connect(client_socket, (struct sockaddr *)&client_address,
200158115Sume		client_address_len);
201158115Sume	if (res == -1) {
202158115Sume		close(client_socket);
203171795Sbushman		TRACE_OUT(open_nscd_connection);
204158115Sume		return (NULL);
205158115Sume	}
206158115Sume	fcntl(client_socket, F_SETFL, O_NONBLOCK);
207158115Sume
208194104Sdes	retval = calloc(1, sizeof(*retval));
209158115Sume	assert(retval != NULL);
210158115Sume
211158115Sume	retval->sockfd = client_socket;
212158115Sume
213158115Sume	retval->write_queue = kqueue();
214158115Sume	assert(retval->write_queue != -1);
215158115Sume
216158115Sume	EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD,
217158115Sume		0, 0, NULL);
218158115Sume	res = kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
219158115Sume
220158115Sume	retval->read_queue = kqueue();
221158115Sume	assert(retval->read_queue != -1);
222158115Sume
223158115Sume	EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD,
224158115Sume		0, 0, NULL);
225158115Sume	res = kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
226158115Sume
227171795Sbushman	TRACE_OUT(open_nscd_connection);
228158115Sume	return (retval);
229158115Sume}
230158115Sume
231158115Sumevoid
232171795Sbushmanclose_nscd_connection__(struct nscd_connection_ *connection)
233158115Sume{
234158115Sume
235171795Sbushman	TRACE_IN(close_nscd_connection);
236158115Sume	assert(connection != NULL);
237158115Sume
238158115Sume	close(connection->sockfd);
239158115Sume	close(connection->read_queue);
240158115Sume	close(connection->write_queue);
241158115Sume	free(connection);
242171795Sbushman	TRACE_OUT(close_nscd_connection);
243158115Sume}
244158115Sume
245158115Sumeint
246171795Sbushmannscd_transform__(struct nscd_connection_ *connection,
247158115Sume	const char *entry_name, int transformation_type)
248158115Sume{
249158115Sume	size_t name_size;
250158115Sume	int error_code;
251158115Sume	int result;
252158115Sume
253171795Sbushman	TRACE_IN(nscd_transform);
254158115Sume
255158115Sume	error_code = -1;
256158115Sume	result = 0;
257158115Sume	result = send_credentials(connection, CET_TRANSFORM_REQUEST);
258158115Sume	if (result != 0)
259158115Sume		goto fin;
260158115Sume
261158115Sume	if (entry_name != NULL)
262158115Sume		name_size = strlen(entry_name);
263158115Sume	else
264158115Sume		name_size = 0;
265158115Sume
266158115Sume	result = safe_write(connection, &name_size, sizeof(size_t));
267158115Sume	if (result != 0)
268158115Sume		goto fin;
269158115Sume
270158115Sume	result = safe_write(connection, &transformation_type, sizeof(int));
271158115Sume	if (result != 0)
272158115Sume		goto fin;
273158115Sume
274158115Sume	if (entry_name != NULL) {
275158115Sume		result = safe_write(connection, entry_name, name_size);
276158115Sume		if (result != 0)
277158115Sume			goto fin;
278158115Sume	}
279158115Sume
280158115Sume	result = safe_read(connection, &error_code, sizeof(int));
281158115Sume	if (result != 0)
282158115Sume		error_code = -1;
283158115Sume
284158115Sumefin:
285171795Sbushman	TRACE_OUT(nscd_transform);
286158115Sume	return (error_code);
287158115Sume}
288