xref: /freebsd/usr.sbin/nscd/nscdcli.c (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
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  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/types.h>
32 
33 #include <sys/event.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <sys/un.h>
37 
38 #include <assert.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "debug.h"
46 #include "nscdcli.h"
47 #include "protocol.h"
48 
49 #define DEFAULT_NSCD_IO_TIMEOUT	4
50 
51 static int safe_write(struct nscd_connection_ *, const void *, size_t);
52 static int safe_read(struct nscd_connection_ *, void *, size_t);
53 static int send_credentials(struct nscd_connection_ *, int);
54 
55 static int
56 safe_write(struct nscd_connection_ *connection, const void *data,
57 	size_t data_size)
58 {
59 	struct kevent eventlist;
60 	int	nevents;
61 	size_t result;
62 	ssize_t s_result;
63 	struct timespec	timeout;
64 
65 	if (data_size == 0)
66 		return (0);
67 
68 	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
69 	timeout.tv_nsec = 0;
70 	result = 0;
71 	do {
72 		nevents = kevent(connection->write_queue, NULL, 0, &eventlist,
73 	    		1, &timeout);
74 		if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
75 			s_result = write(connection->sockfd,
76 				(char *)data + result,
77 				(size_t)eventlist.data < data_size - result ?
78 		    		(size_t)eventlist.data : data_size - result);
79 			if (s_result == -1)
80 				return (-1);
81 			else
82 				result += s_result;
83 
84 			if (eventlist.flags & EV_EOF)
85 				return (result < data_size ? -1 : 0);
86 		} else
87 			return (-1);
88 	} while (result < data_size);
89 
90 	return (0);
91 }
92 
93 static int
94 safe_read(struct nscd_connection_ *connection, void *data, size_t data_size)
95 {
96 	struct kevent eventlist;
97 	size_t result;
98 	ssize_t s_result;
99 	struct timespec timeout;
100 	int nevents;
101 
102 	if (data_size == 0)
103 		return (0);
104 
105 	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
106 	timeout.tv_nsec = 0;
107 	result = 0;
108 	do {
109 		nevents = kevent(connection->read_queue, NULL, 0, &eventlist, 1,
110 			&timeout);
111 		if ((nevents == 1) && (eventlist.filter == EVFILT_READ)) {
112 			s_result = read(connection->sockfd,
113 				(char *)data + result,
114 				(size_t)eventlist.data <= data_size - result ?
115 				(size_t)eventlist.data : data_size - result);
116 			if (s_result == -1)
117 				return (-1);
118 			else
119 				result += s_result;
120 
121 			if (eventlist.flags & EV_EOF)
122 				return (result < data_size ? -1 : 0);
123 		} else
124 			return (-1);
125 	} while (result < data_size);
126 
127 	return (0);
128 }
129 
130 static int
131 send_credentials(struct nscd_connection_ *connection, int type)
132 {
133 	struct kevent eventlist;
134 	int nevents;
135 	ssize_t result;
136 	int res;
137 
138 	struct msghdr	cred_hdr;
139 	struct iovec	iov;
140 
141 	struct {
142 		struct cmsghdr	hdr;
143 		struct cmsgcred	creds;
144 	} cmsg;
145 
146 	TRACE_IN(send_credentials);
147 	memset(&cmsg, 0, sizeof(cmsg));
148 	cmsg.hdr.cmsg_len = sizeof(cmsg);
149 	cmsg.hdr.cmsg_level = SOL_SOCKET;
150 	cmsg.hdr.cmsg_type = SCM_CREDS;
151 
152 	memset(&cred_hdr, 0, sizeof(struct msghdr));
153 	cred_hdr.msg_iov = &iov;
154 	cred_hdr.msg_iovlen = 1;
155 	cred_hdr.msg_control = &cmsg;
156 	cred_hdr.msg_controllen = sizeof(cmsg);
157 
158 	iov.iov_base = &type;
159 	iov.iov_len = sizeof(int);
160 
161 	EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
162 		NOTE_LOWAT, sizeof(int), NULL);
163 	res = kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
164 
165 	nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 1, NULL);
166 	if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
167 		result = (sendmsg(connection->sockfd, &cred_hdr, 0) == -1) ? -1
168 	    		: 0;
169 		EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
170 			0, 0, NULL);
171 		kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
172 		TRACE_OUT(send_credentials);
173 		return (result);
174 	} else {
175 		TRACE_OUT(send_credentials);
176 		return (-1);
177 	}
178 }
179 
180 struct nscd_connection_ *
181 open_nscd_connection__(struct nscd_connection_params const *params)
182 {
183 	struct nscd_connection_ *retval;
184 	struct kevent eventlist;
185 	struct sockaddr_un	client_address;
186 	int client_address_len, client_socket;
187 	int res;
188 
189 	TRACE_IN(open_nscd_connection);
190 	assert(params != NULL);
191 
192 	client_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
193 	client_address.sun_family = PF_LOCAL;
194 	strlcpy(client_address.sun_path, params->socket_path,
195 		sizeof(client_address.sun_path));
196 	client_address_len = sizeof(client_address.sun_family) +
197 		strlen(client_address.sun_path) + 1;
198 
199 	res = connect(client_socket, (struct sockaddr *)&client_address,
200 		client_address_len);
201 	if (res == -1) {
202 		close(client_socket);
203 		TRACE_OUT(open_nscd_connection);
204 		return (NULL);
205 	}
206 	fcntl(client_socket, F_SETFL, O_NONBLOCK);
207 
208 	retval = calloc(1, sizeof(*retval));
209 	assert(retval != NULL);
210 
211 	retval->sockfd = client_socket;
212 
213 	retval->write_queue = kqueue();
214 	assert(retval->write_queue != -1);
215 
216 	EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD,
217 		0, 0, NULL);
218 	res = kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL);
219 
220 	retval->read_queue = kqueue();
221 	assert(retval->read_queue != -1);
222 
223 	EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD,
224 		0, 0, NULL);
225 	res = kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL);
226 
227 	TRACE_OUT(open_nscd_connection);
228 	return (retval);
229 }
230 
231 void
232 close_nscd_connection__(struct nscd_connection_ *connection)
233 {
234 
235 	TRACE_IN(close_nscd_connection);
236 	assert(connection != NULL);
237 
238 	close(connection->sockfd);
239 	close(connection->read_queue);
240 	close(connection->write_queue);
241 	free(connection);
242 	TRACE_OUT(close_nscd_connection);
243 }
244 
245 int
246 nscd_transform__(struct nscd_connection_ *connection,
247 	const char *entry_name, int transformation_type)
248 {
249 	size_t name_size;
250 	int error_code;
251 	int result;
252 
253 	TRACE_IN(nscd_transform);
254 
255 	error_code = -1;
256 	result = 0;
257 	result = send_credentials(connection, CET_TRANSFORM_REQUEST);
258 	if (result != 0)
259 		goto fin;
260 
261 	if (entry_name != NULL)
262 		name_size = strlen(entry_name);
263 	else
264 		name_size = 0;
265 
266 	result = safe_write(connection, &name_size, sizeof(size_t));
267 	if (result != 0)
268 		goto fin;
269 
270 	result = safe_write(connection, &transformation_type, sizeof(int));
271 	if (result != 0)
272 		goto fin;
273 
274 	if (entry_name != NULL) {
275 		result = safe_write(connection, entry_name, name_size);
276 		if (result != 0)
277 			goto fin;
278 	}
279 
280 	result = safe_read(connection, &error_code, sizeof(int));
281 	if (result != 0)
282 		error_code = -1;
283 
284 fin:
285 	TRACE_OUT(nscd_transform);
286 	return (error_code);
287 }
288