nscdcli.c revision 158115
1139738Simp/*-
2110211Smarcel * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3110211Smarcel * All rights reserved.
4110211Smarcel *
5110211Smarcel * Redistribution and use in source and binary forms, with or without
6110211Smarcel * modification, are permitted provided that the following conditions
7110211Smarcel * are met:
8110211Smarcel * 1. Redistributions of source code must retain the above copyright
9110211Smarcel *    notice, this list of conditions and the following disclaimer.
10110211Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11110211Smarcel *    notice, this list of conditions and the following disclaimer in the
12110211Smarcel *    documentation and/or other materials provided with the distribution.
13110211Smarcel *
14110211Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15110211Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16110211Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17110211Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18110211Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19110211Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20110211Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21110211Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22110211Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23110211Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24110211Smarcel * SUCH DAMAGE.
25110211Smarcel *
26110211Smarcel */
27110211Smarcel
28110211Smarcel#include <sys/cdefs.h>
29110211Smarcel__FBSDID("$FreeBSD: head/usr.sbin/nscd/nscdcli.c 158115 2006-04-28 12:03:38Z ume $");
30110211Smarcel
31110211Smarcel#include <sys/types.h>
32110211Smarcel#include <sys/socket.h>
33110211Smarcel#include <sys/event.h>
34110211Smarcel#include <sys/uio.h>
35110211Smarcel#include <sys/un.h>
36110211Smarcel#include <assert.h>
37110211Smarcel#include <errno.h>
38110211Smarcel#include <fcntl.h>
39110211Smarcel#include <stdlib.h>
40110211Smarcel#include <string.h>
41110211Smarcel#include <unistd.h>
42110211Smarcel
43110211Smarcel#include "debug.h"
44122351Smarcel#include "cachedcli.h"
45122351Smarcel#include "protocol.h"
46122351Smarcel
47110211Smarcel#define DEFAULT_CACHED_IO_TIMEOUT	4
48110211Smarcel
49110211Smarcelstatic int safe_write(struct cached_connection_ *, const void *, size_t);
50110211Smarcelstatic int safe_read(struct cached_connection_ *, void *, size_t);
51110211Smarcelstatic int send_credentials(struct cached_connection_ *, int);
52110211Smarcel
53110211Smarcelstatic int
54110211Smarcelsafe_write(struct cached_connection_ *connection, const void *data,
55110211Smarcel	size_t data_size)
56110211Smarcel{
57110211Smarcel	struct kevent eventlist;
58110211Smarcel	int	nevents;
59110211Smarcel	size_t result;
60110211Smarcel	ssize_t s_result;
61110211Smarcel	struct timespec	timeout;
62110211Smarcel
63110211Smarcel	if (data_size == 0)
64110211Smarcel		return (0);
65110211Smarcel
66110211Smarcel	timeout.tv_sec = DEFAULT_CACHED_IO_TIMEOUT;
67110211Smarcel	timeout.tv_nsec = 0;
68122351Smarcel	result = 0;
69122351Smarcel	do {
70122351Smarcel		nevents = kevent(connection->write_queue, NULL, 0, &eventlist,
71122351Smarcel	    		1, &timeout);
72122351Smarcel		if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
73122351Smarcel			s_result = write(connection->sockfd, data + result,
74110211Smarcel				eventlist.data < data_size - result ?
75		    		eventlist.data : data_size - result);
76			if (s_result == -1)
77				return (-1);
78			else
79				result += s_result;
80
81			if (eventlist.flags & EV_EOF)
82				return (result < data_size ? -1 : 0);
83		} else
84			return (-1);
85	} while (result < data_size);
86
87	return (0);
88}
89
90static int
91safe_read(struct cached_connection_ *connection, void *data, size_t data_size)
92{
93	struct kevent eventlist;
94	size_t result;
95	ssize_t s_result;
96	struct timespec timeout;
97	int nevents;
98
99	if (data_size == 0)
100		return (0);
101
102	timeout.tv_sec = DEFAULT_CACHED_IO_TIMEOUT;
103	timeout.tv_nsec = 0;
104	result = 0;
105	do {
106		nevents = kevent(connection->read_queue, NULL, 0, &eventlist, 1,
107			&timeout);
108		if ((nevents == 1) && (eventlist.filter == EVFILT_READ)) {
109			s_result = read(connection->sockfd, data + result,
110			eventlist.data <= data_size - result ? eventlist.data :
111				data_size - result);
112			if (s_result == -1)
113				return (-1);
114			else
115				result += s_result;
116
117			if (eventlist.flags & EV_EOF)
118				return (result < data_size ? -1 : 0);
119		} else
120			return (-1);
121	} while (result < data_size);
122
123	return (0);
124}
125
126static int
127send_credentials(struct cached_connection_ *connection, int type)
128{
129	struct kevent eventlist;
130	int nevents;
131	ssize_t result;
132	int res;
133
134	struct msghdr	cred_hdr;
135	struct iovec	iov;
136
137	struct {
138		struct cmsghdr	hdr;
139		struct cmsgcred	creds;
140	} cmsg;
141
142	TRACE_IN(send_credentials);
143	memset(&cmsg, 0, sizeof(cmsg));
144	cmsg.hdr.cmsg_len = sizeof(cmsg);
145	cmsg.hdr.cmsg_level = SOL_SOCKET;
146	cmsg.hdr.cmsg_type = SCM_CREDS;
147
148	memset(&cred_hdr, 0, sizeof(struct msghdr));
149	cred_hdr.msg_iov = &iov;
150	cred_hdr.msg_iovlen = 1;
151	cred_hdr.msg_control = &cmsg;
152	cred_hdr.msg_controllen = sizeof(cmsg);
153
154	iov.iov_base = &type;
155	iov.iov_len = sizeof(int);
156
157	EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
158		NOTE_LOWAT, sizeof(int), NULL);
159	res = kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
160
161	nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 1, NULL);
162	if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
163		result = (sendmsg(connection->sockfd, &cred_hdr, 0) == -1) ? -1
164	    		: 0;
165		EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
166			0, 0, NULL);
167		kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
168		TRACE_OUT(send_credentials);
169		return (result);
170	} else {
171		TRACE_OUT(send_credentials);
172		return (-1);
173	}
174}
175
176struct cached_connection_ *
177open_cached_connection__(struct cached_connection_params const *params)
178{
179	struct cached_connection_ *retval;
180	struct kevent eventlist;
181	struct sockaddr_un	client_address;
182	int client_address_len, client_socket;
183	int res;
184
185	TRACE_IN(open_cached_connection);
186	assert(params != NULL);
187
188	client_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
189	client_address.sun_family = PF_LOCAL;
190	strncpy(client_address.sun_path, params->socket_path,
191		sizeof(client_address.sun_path));
192	client_address_len = sizeof(client_address.sun_family) +
193		strlen(client_address.sun_path) + 1;
194
195	res = connect(client_socket, (struct sockaddr *)&client_address,
196		client_address_len);
197	if (res == -1) {
198		close(client_socket);
199		TRACE_OUT(open_cached_connection);
200		return (NULL);
201	}
202	fcntl(client_socket, F_SETFL, O_NONBLOCK);
203
204	retval = malloc(sizeof(struct cached_connection_));
205	assert(retval != NULL);
206	memset(retval, 0, sizeof(struct cached_connection_));
207
208	retval->sockfd = client_socket;
209
210	retval->write_queue = kqueue();
211	assert(retval->write_queue != -1);
212
213	EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD,
214		0, 0, NULL);
215	res = kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
216
217	retval->read_queue = kqueue();
218	assert(retval->read_queue != -1);
219
220	EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD,
221		0, 0, NULL);
222	res = kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
223
224	TRACE_OUT(open_cached_connection);
225	return (retval);
226}
227
228void
229close_cached_connection__(struct cached_connection_ *connection)
230{
231
232	TRACE_IN(close_cached_connection);
233	assert(connection != NULL);
234
235	close(connection->sockfd);
236	close(connection->read_queue);
237	close(connection->write_queue);
238	free(connection);
239	TRACE_OUT(close_cached_connection);
240}
241
242int
243cached_transform__(struct cached_connection_ *connection,
244	const char *entry_name, int transformation_type)
245{
246	size_t name_size;
247	int error_code;
248	int result;
249
250	TRACE_IN(cached_transform);
251
252	error_code = -1;
253	result = 0;
254	result = send_credentials(connection, CET_TRANSFORM_REQUEST);
255	if (result != 0)
256		goto fin;
257
258	if (entry_name != NULL)
259		name_size = strlen(entry_name);
260	else
261		name_size = 0;
262
263	result = safe_write(connection, &name_size, sizeof(size_t));
264	if (result != 0)
265		goto fin;
266
267	result = safe_write(connection, &transformation_type, sizeof(int));
268	if (result != 0)
269		goto fin;
270
271	if (entry_name != NULL) {
272		result = safe_write(connection, entry_name, name_size);
273		if (result != 0)
274			goto fin;
275	}
276
277	result = safe_read(connection, &error_code, sizeof(int));
278	if (result != 0)
279		error_code = -1;
280
281fin:
282	TRACE_OUT(cached_transform);
283	return (error_code);
284}
285