nscdcli.c revision 194093
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: head/usr.sbin/nscd/nscdcli.c 194093 2009-06-13 00:43:56Z des $");
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)) {
75158115Sume			s_result = write(connection->sockfd, data + result,
76158115Sume				eventlist.data < data_size - result ?
77158115Sume		    		eventlist.data : data_size - result);
78158115Sume			if (s_result == -1)
79158115Sume				return (-1);
80158115Sume			else
81158115Sume				result += s_result;
82158115Sume
83158115Sume			if (eventlist.flags & EV_EOF)
84158115Sume				return (result < data_size ? -1 : 0);
85158115Sume		} else
86158115Sume			return (-1);
87158115Sume	} while (result < data_size);
88158115Sume
89158115Sume	return (0);
90158115Sume}
91158115Sume
92158115Sumestatic int
93171795Sbushmansafe_read(struct nscd_connection_ *connection, void *data, size_t data_size)
94158115Sume{
95158115Sume	struct kevent eventlist;
96158115Sume	size_t result;
97158115Sume	ssize_t s_result;
98158115Sume	struct timespec timeout;
99158115Sume	int nevents;
100158115Sume
101158115Sume	if (data_size == 0)
102158115Sume		return (0);
103158115Sume
104171795Sbushman	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
105158115Sume	timeout.tv_nsec = 0;
106158115Sume	result = 0;
107158115Sume	do {
108158115Sume		nevents = kevent(connection->read_queue, NULL, 0, &eventlist, 1,
109158115Sume			&timeout);
110158115Sume		if ((nevents == 1) && (eventlist.filter == EVFILT_READ)) {
111158115Sume			s_result = read(connection->sockfd, data + result,
112158115Sume			eventlist.data <= data_size - result ? eventlist.data :
113158115Sume				data_size - result);
114158115Sume			if (s_result == -1)
115158115Sume				return (-1);
116158115Sume			else
117158115Sume				result += s_result;
118158115Sume
119158115Sume			if (eventlist.flags & EV_EOF)
120158115Sume				return (result < data_size ? -1 : 0);
121158115Sume		} else
122158115Sume			return (-1);
123158115Sume	} while (result < data_size);
124158115Sume
125158115Sume	return (0);
126158115Sume}
127158115Sume
128158115Sumestatic int
129171795Sbushmansend_credentials(struct nscd_connection_ *connection, int type)
130158115Sume{
131158115Sume	struct kevent eventlist;
132158115Sume	int nevents;
133158115Sume	ssize_t result;
134158115Sume	int res;
135158115Sume
136158115Sume	struct msghdr	cred_hdr;
137158115Sume	struct iovec	iov;
138158115Sume
139158115Sume	struct {
140158115Sume		struct cmsghdr	hdr;
141158115Sume		struct cmsgcred	creds;
142158115Sume	} cmsg;
143158115Sume
144158115Sume	TRACE_IN(send_credentials);
145158115Sume	memset(&cmsg, 0, sizeof(cmsg));
146158115Sume	cmsg.hdr.cmsg_len = sizeof(cmsg);
147158115Sume	cmsg.hdr.cmsg_level = SOL_SOCKET;
148158115Sume	cmsg.hdr.cmsg_type = SCM_CREDS;
149158115Sume
150158115Sume	memset(&cred_hdr, 0, sizeof(struct msghdr));
151158115Sume	cred_hdr.msg_iov = &iov;
152158115Sume	cred_hdr.msg_iovlen = 1;
153158115Sume	cred_hdr.msg_control = &cmsg;
154158115Sume	cred_hdr.msg_controllen = sizeof(cmsg);
155158115Sume
156158115Sume	iov.iov_base = &type;
157158115Sume	iov.iov_len = sizeof(int);
158158115Sume
159158115Sume	EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
160158115Sume		NOTE_LOWAT, sizeof(int), NULL);
161158115Sume	res = kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
162158115Sume
163158115Sume	nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 1, NULL);
164158115Sume	if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
165158115Sume		result = (sendmsg(connection->sockfd, &cred_hdr, 0) == -1) ? -1
166158115Sume	    		: 0;
167158115Sume		EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
168158115Sume			0, 0, NULL);
169158115Sume		kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
170158115Sume		TRACE_OUT(send_credentials);
171158115Sume		return (result);
172158115Sume	} else {
173158115Sume		TRACE_OUT(send_credentials);
174158115Sume		return (-1);
175158115Sume	}
176158115Sume}
177158115Sume
178171795Sbushmanstruct nscd_connection_ *
179171795Sbushmanopen_nscd_connection__(struct nscd_connection_params const *params)
180158115Sume{
181171795Sbushman	struct nscd_connection_ *retval;
182158115Sume	struct kevent eventlist;
183158115Sume	struct sockaddr_un	client_address;
184158115Sume	int client_address_len, client_socket;
185158115Sume	int res;
186158115Sume
187171795Sbushman	TRACE_IN(open_nscd_connection);
188158115Sume	assert(params != NULL);
189158115Sume
190158115Sume	client_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
191158115Sume	client_address.sun_family = PF_LOCAL;
192184188Sdelphij	strlcpy(client_address.sun_path, params->socket_path,
193158115Sume		sizeof(client_address.sun_path));
194158115Sume	client_address_len = sizeof(client_address.sun_family) +
195158115Sume		strlen(client_address.sun_path) + 1;
196158115Sume
197158115Sume	res = connect(client_socket, (struct sockaddr *)&client_address,
198158115Sume		client_address_len);
199158115Sume	if (res == -1) {
200158115Sume		close(client_socket);
201171795Sbushman		TRACE_OUT(open_nscd_connection);
202158115Sume		return (NULL);
203158115Sume	}
204158115Sume	fcntl(client_socket, F_SETFL, O_NONBLOCK);
205158115Sume
206183770Sdelphij	retval = calloc(1, sizeof(struct nscd_connection_));
207158115Sume	assert(retval != NULL);
208158115Sume
209158115Sume	retval->sockfd = client_socket;
210158115Sume
211158115Sume	retval->write_queue = kqueue();
212158115Sume	assert(retval->write_queue != -1);
213158115Sume
214158115Sume	EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD,
215158115Sume		0, 0, NULL);
216158115Sume	res = kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
217158115Sume
218158115Sume	retval->read_queue = kqueue();
219158115Sume	assert(retval->read_queue != -1);
220158115Sume
221158115Sume	EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD,
222158115Sume		0, 0, NULL);
223158115Sume	res = kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
224158115Sume
225171795Sbushman	TRACE_OUT(open_nscd_connection);
226158115Sume	return (retval);
227158115Sume}
228158115Sume
229158115Sumevoid
230171795Sbushmanclose_nscd_connection__(struct nscd_connection_ *connection)
231158115Sume{
232158115Sume
233171795Sbushman	TRACE_IN(close_nscd_connection);
234158115Sume	assert(connection != NULL);
235158115Sume
236158115Sume	close(connection->sockfd);
237158115Sume	close(connection->read_queue);
238158115Sume	close(connection->write_queue);
239158115Sume	free(connection);
240171795Sbushman	TRACE_OUT(close_nscd_connection);
241158115Sume}
242158115Sume
243158115Sumeint
244171795Sbushmannscd_transform__(struct nscd_connection_ *connection,
245158115Sume	const char *entry_name, int transformation_type)
246158115Sume{
247158115Sume	size_t name_size;
248158115Sume	int error_code;
249158115Sume	int result;
250158115Sume
251171795Sbushman	TRACE_IN(nscd_transform);
252158115Sume
253158115Sume	error_code = -1;
254158115Sume	result = 0;
255158115Sume	result = send_credentials(connection, CET_TRANSFORM_REQUEST);
256158115Sume	if (result != 0)
257158115Sume		goto fin;
258158115Sume
259158115Sume	if (entry_name != NULL)
260158115Sume		name_size = strlen(entry_name);
261158115Sume	else
262158115Sume		name_size = 0;
263158115Sume
264158115Sume	result = safe_write(connection, &name_size, sizeof(size_t));
265158115Sume	if (result != 0)
266158115Sume		goto fin;
267158115Sume
268158115Sume	result = safe_write(connection, &transformation_type, sizeof(int));
269158115Sume	if (result != 0)
270158115Sume		goto fin;
271158115Sume
272158115Sume	if (entry_name != NULL) {
273158115Sume		result = safe_write(connection, entry_name, name_size);
274158115Sume		if (result != 0)
275158115Sume			goto fin;
276158115Sume	}
277158115Sume
278158115Sume	result = safe_read(connection, &error_code, sizeof(int));
279158115Sume	if (result != 0)
280158115Sume		error_code = -1;
281158115Sume
282158115Sumefin:
283171795Sbushman	TRACE_OUT(nscd_transform);
284158115Sume	return (error_code);
285158115Sume}
286