xref: /dragonfly/usr.sbin/nscd/nscdcli.c (revision ab709bfb)
1 /*-
2  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.sbin/nscd/nscdcli.c,v 1.5 2008/10/23 00:28:21 delphij Exp $
27  */
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/event.h>
32 #include <sys/uio.h>
33 #include <sys/un.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "debug.h"
42 #include "nscdcli.h"
43 #include "protocol.h"
44 
45 #define DEFAULT_NSCD_IO_TIMEOUT	4
46 
47 static int safe_write(struct nscd_connection_ *, const void *, size_t);
48 static int safe_read(struct nscd_connection_ *, void *, size_t);
49 static int send_credentials(struct nscd_connection_ *, int);
50 
51 static int
52 safe_write(struct nscd_connection_ *connection, const void *data,
53 	size_t data_size)
54 {
55 	struct kevent eventlist;
56 	int	nevents;
57 	size_t result;
58 	ssize_t s_result;
59 	struct timespec	timeout;
60 
61 	if (data_size == 0)
62 		return (0);
63 
64 	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
65 	timeout.tv_nsec = 0;
66 	result = 0;
67 	do {
68 		nevents = kevent(connection->write_queue, NULL, 0, &eventlist,
69 			1, &timeout);
70 		if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
71 			s_result = write(connection->sockfd, data + result,
72 				eventlist.data < data_size - result ?
73 				eventlist.data : data_size - result);
74 			if (s_result == -1)
75 				return (-1);
76 			else
77 				result += s_result;
78 
79 			if (eventlist.flags & EV_EOF)
80 				return (result < data_size ? -1 : 0);
81 		} else
82 			return (-1);
83 	} while (result < data_size);
84 
85 	return (0);
86 }
87 
88 static int
89 safe_read(struct nscd_connection_ *connection, void *data, size_t data_size)
90 {
91 	struct kevent eventlist;
92 	size_t result;
93 	ssize_t s_result;
94 	struct timespec timeout;
95 	int nevents;
96 
97 	if (data_size == 0)
98 		return (0);
99 
100 	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
101 	timeout.tv_nsec = 0;
102 	result = 0;
103 	do {
104 		nevents = kevent(connection->read_queue, NULL, 0, &eventlist, 1,
105 			&timeout);
106 		if ((nevents == 1) && (eventlist.filter == EVFILT_READ)) {
107 			s_result = read(connection->sockfd, data + result,
108 			eventlist.data <= data_size - result ? eventlist.data :
109 				data_size - result);
110 			if (s_result == -1)
111 				return (-1);
112 			else
113 				result += s_result;
114 
115 			if (eventlist.flags & EV_EOF)
116 				return (result < data_size ? -1 : 0);
117 		} else
118 			return (-1);
119 	} while (result < data_size);
120 
121 	return (0);
122 }
123 
124 static int
125 send_credentials(struct nscd_connection_ *connection, int type)
126 {
127 	struct kevent eventlist;
128 	int nevents;
129 	ssize_t result;
130 	int res;
131 
132 	struct msghdr	cred_hdr;
133 	struct iovec	iov;
134 
135 	struct {
136 		struct cmsghdr	hdr;
137 		struct cmsgcred	creds;
138 	} cmsg;
139 
140 	TRACE_IN(send_credentials);
141 	memset(&cmsg, 0, sizeof(cmsg));
142 	cmsg.hdr.cmsg_len = sizeof(cmsg);
143 	cmsg.hdr.cmsg_level = SOL_SOCKET;
144 	cmsg.hdr.cmsg_type = SCM_CREDS;
145 
146 	memset(&cred_hdr, 0, sizeof(struct msghdr));
147 	cred_hdr.msg_iov = &iov;
148 	cred_hdr.msg_iovlen = 1;
149 	cred_hdr.msg_control = &cmsg;
150 	cred_hdr.msg_controllen = sizeof(cmsg);
151 
152 	iov.iov_base = &type;
153 	iov.iov_len = sizeof(int);
154 
155 	EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
156 		NOTE_LOWAT, sizeof(int), NULL);
157 	res = kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
158 
159 	nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 1, NULL);
160 	if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
161 		result = (sendmsg(connection->sockfd, &cred_hdr, 0) == -1) ? -1
162 			: 0;
163 		EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
164 			0, 0, NULL);
165 		kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
166 		TRACE_OUT(send_credentials);
167 		return (result);
168 	} else {
169 		TRACE_OUT(send_credentials);
170 		return (-1);
171 	}
172 }
173 
174 struct nscd_connection_ *
175 open_nscd_connection__(struct nscd_connection_params const *params)
176 {
177 	struct nscd_connection_ *retval;
178 	struct kevent eventlist;
179 	struct sockaddr_un	client_address;
180 	int client_address_len, client_socket;
181 	int res;
182 
183 	TRACE_IN(open_nscd_connection);
184 	assert(params != NULL);
185 
186 	client_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
187 	client_address.sun_family = PF_LOCAL;
188 	strlcpy(client_address.sun_path, params->socket_path,
189 		sizeof(client_address.sun_path));
190 	client_address_len = sizeof(client_address.sun_family) +
191 		strlen(client_address.sun_path) + 1;
192 
193 	res = connect(client_socket, (struct sockaddr *)&client_address,
194 		client_address_len);
195 	if (res == -1) {
196 		close(client_socket);
197 		TRACE_OUT(open_nscd_connection);
198 		return (NULL);
199 	}
200 	fcntl(client_socket, F_SETFL, O_NONBLOCK);
201 
202 	retval = calloc(1, sizeof(struct nscd_connection_));
203 	assert(retval != NULL);
204 
205 	retval->sockfd = client_socket;
206 
207 	retval->write_queue = kqueue();
208 	assert(retval->write_queue != -1);
209 
210 	EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD,
211 		0, 0, NULL);
212 	res = kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
213 
214 	retval->read_queue = kqueue();
215 	assert(retval->read_queue != -1);
216 
217 	EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD,
218 		0, 0, NULL);
219 	res = kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
220 
221 	TRACE_OUT(open_nscd_connection);
222 	return (retval);
223 }
224 
225 void
226 close_nscd_connection__(struct nscd_connection_ *connection)
227 {
228 
229 	TRACE_IN(close_nscd_connection);
230 	assert(connection != NULL);
231 
232 	close(connection->sockfd);
233 	close(connection->read_queue);
234 	close(connection->write_queue);
235 	free(connection);
236 	TRACE_OUT(close_nscd_connection);
237 }
238 
239 int
240 nscd_transform__(struct nscd_connection_ *connection,
241 	const char *entry_name, int transformation_type)
242 {
243 	size_t name_size;
244 	int error_code;
245 	int result;
246 
247 	TRACE_IN(nscd_transform);
248 
249 	error_code = -1;
250 	result = 0;
251 	result = send_credentials(connection, CET_TRANSFORM_REQUEST);
252 	if (result != 0)
253 		goto fin;
254 
255 	if (entry_name != NULL)
256 		name_size = strlen(entry_name);
257 	else
258 		name_size = 0;
259 
260 	result = safe_write(connection, &name_size, sizeof(size_t));
261 	if (result != 0)
262 		goto fin;
263 
264 	result = safe_write(connection, &transformation_type, sizeof(int));
265 	if (result != 0)
266 		goto fin;
267 
268 	if (entry_name != NULL) {
269 		result = safe_write(connection, entry_name, name_size);
270 		if (result != 0)
271 			goto fin;
272 	}
273 
274 	result = safe_read(connection, &error_code, sizeof(int));
275 	if (result != 0)
276 		error_code = -1;
277 
278 fin:
279 	TRACE_OUT(nscd_transform);
280 	return (error_code);
281 }
282