xref: /freebsd/usr.sbin/nscd/nscdcli.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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 #include <sys/types.h>
30 
31 #include <sys/event.h>
32 #include <sys/socket.h>
33 #include <sys/uio.h>
34 #include <sys/un.h>
35 
36 #include <assert.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "debug.h"
44 #include "nscdcli.h"
45 #include "protocol.h"
46 
47 #define DEFAULT_NSCD_IO_TIMEOUT	4
48 
49 static int safe_write(struct nscd_connection_ *, const void *, size_t);
50 static int safe_read(struct nscd_connection_ *, void *, size_t);
51 static int send_credentials(struct nscd_connection_ *, int);
52 
53 static int
54 safe_write(struct nscd_connection_ *connection, const void *data,
55 	size_t data_size)
56 {
57 	struct kevent eventlist;
58 	int	nevents;
59 	size_t result;
60 	ssize_t s_result;
61 	struct timespec	timeout;
62 
63 	if (data_size == 0)
64 		return (0);
65 
66 	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
67 	timeout.tv_nsec = 0;
68 	result = 0;
69 	do {
70 		nevents = kevent(connection->write_queue, NULL, 0, &eventlist,
71 	    		1, &timeout);
72 		if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
73 			s_result = write(connection->sockfd,
74 				(char *)data + result,
75 				(size_t)eventlist.data < data_size - result ?
76 		    		(size_t)eventlist.data : data_size - result);
77 			if (s_result == -1)
78 				return (-1);
79 			else
80 				result += s_result;
81 
82 			if (eventlist.flags & EV_EOF)
83 				return (result < data_size ? -1 : 0);
84 		} else
85 			return (-1);
86 	} while (result < data_size);
87 
88 	return (0);
89 }
90 
91 static int
92 safe_read(struct nscd_connection_ *connection, void *data, size_t data_size)
93 {
94 	struct kevent eventlist;
95 	size_t result;
96 	ssize_t s_result;
97 	struct timespec timeout;
98 	int nevents;
99 
100 	if (data_size == 0)
101 		return (0);
102 
103 	timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT;
104 	timeout.tv_nsec = 0;
105 	result = 0;
106 	do {
107 		nevents = kevent(connection->read_queue, NULL, 0, &eventlist, 1,
108 			&timeout);
109 		if ((nevents == 1) && (eventlist.filter == EVFILT_READ)) {
110 			s_result = read(connection->sockfd,
111 				(char *)data + result,
112 				(size_t)eventlist.data <= data_size - result ?
113 				(size_t)eventlist.data : data_size - result);
114 			if (s_result == -1)
115 				return (-1);
116 			else
117 				result += s_result;
118 
119 			if (eventlist.flags & EV_EOF)
120 				return (result < data_size ? -1 : 0);
121 		} else
122 			return (-1);
123 	} while (result < data_size);
124 
125 	return (0);
126 }
127 
128 static int
129 send_credentials(struct nscd_connection_ *connection, int type)
130 {
131 	union {
132 		struct cmsghdr hdr;
133 		char pad[CMSG_SPACE(sizeof(struct cmsgcred))];
134 	} cmsg;
135 	struct msghdr mhdr;
136 	struct iovec iov;
137 	struct kevent eventlist;
138 	int nevents;
139 	ssize_t result;
140 
141 	TRACE_IN(send_credentials);
142 	memset(&cmsg, 0, sizeof(cmsg));
143 	cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
144 	cmsg.hdr.cmsg_level = SOL_SOCKET;
145 	cmsg.hdr.cmsg_type = SCM_CREDS;
146 
147 	memset(&mhdr, 0, sizeof(mhdr));
148 	mhdr.msg_iov = &iov;
149 	mhdr.msg_iovlen = 1;
150 	mhdr.msg_control = &cmsg;
151 	mhdr.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred));
152 
153 	iov.iov_base = &type;
154 	iov.iov_len = sizeof(int);
155 
156 	EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD,
157 	    NOTE_LOWAT, sizeof(int), NULL);
158 	kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL);
159 
160 	nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 1, NULL);
161 	if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) {
162 		result = sendmsg(connection->sockfd, &mhdr, 0) == -1 ? -1 : 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(*retval));
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