server.c revision 1.4 1 1.4 perry /* $NetBSD: server.c,v 1.4 2007/12/15 16:03:30 perry Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Itronix Inc.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Redistribution and use in source and binary forms, with or without
8 1.1 gdamore * modification, are permitted provided that the following conditions
9 1.1 gdamore * are met:
10 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
11 1.1 gdamore * notice, this list of conditions and the following disclaimer.
12 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
14 1.1 gdamore * documentation and/or other materials provided with the distribution.
15 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse
16 1.1 gdamore * or promote products derived from this software without specific
17 1.1 gdamore * prior written permission.
18 1.1 gdamore *
19 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
30 1.1 gdamore */
31 1.1 gdamore /*
32 1.1 gdamore * server.c
33 1.1 gdamore *
34 1.1 gdamore * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>
35 1.1 gdamore * All rights reserved.
36 1.1 gdamore *
37 1.1 gdamore * Redistribution and use in source and binary forms, with or without
38 1.1 gdamore * modification, are permitted provided that the following conditions
39 1.1 gdamore * are met:
40 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
41 1.1 gdamore * notice, this list of conditions and the following disclaimer.
42 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
44 1.1 gdamore * documentation and/or other materials provided with the distribution.
45 1.1 gdamore *
46 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 1.1 gdamore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 1.1 gdamore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 1.1 gdamore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 1.1 gdamore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 1.1 gdamore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 1.1 gdamore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 1.1 gdamore * SUCH DAMAGE.
57 1.1 gdamore *
58 1.4 perry * $Id: server.c,v 1.4 2007/12/15 16:03:30 perry Exp $
59 1.1 gdamore * $FreeBSD: src/usr.sbin/bluetooth/sdpd/server.c,v 1.2 2005/12/06 17:56:36 emax Exp $
60 1.1 gdamore */
61 1.1 gdamore
62 1.1 gdamore #include <sys/cdefs.h>
63 1.4 perry __RCSID("$NetBSD: server.c,v 1.4 2007/12/15 16:03:30 perry Exp $");
64 1.1 gdamore
65 1.1 gdamore #include <sys/param.h>
66 1.1 gdamore #include <sys/select.h>
67 1.1 gdamore #include <sys/stat.h>
68 1.1 gdamore #include <sys/queue.h>
69 1.1 gdamore #include <sys/ucred.h>
70 1.1 gdamore #include <sys/un.h>
71 1.1 gdamore #include <netinet/in.h>
72 1.1 gdamore #include <arpa/inet.h>
73 1.1 gdamore #include <assert.h>
74 1.1 gdamore #include <bluetooth.h>
75 1.1 gdamore #include <errno.h>
76 1.3 plunky #include <grp.h>
77 1.1 gdamore #include <pwd.h>
78 1.1 gdamore #include <sdp.h>
79 1.1 gdamore #include <stdio.h>
80 1.1 gdamore #include <stdlib.h>
81 1.1 gdamore #include <string.h>
82 1.1 gdamore #include <unistd.h>
83 1.1 gdamore #include "log.h"
84 1.1 gdamore #include "profile.h"
85 1.1 gdamore #include "provider.h"
86 1.1 gdamore #include "server.h"
87 1.1 gdamore
88 1.1 gdamore static void server_accept_client (server_p srv, int32_t fd);
89 1.1 gdamore static int32_t server_process_request (server_p srv, int32_t fd);
90 1.1 gdamore static int32_t server_send_error_response (server_p srv, int32_t fd,
91 1.1 gdamore uint16_t error);
92 1.1 gdamore static void server_close_fd (server_p srv, int32_t fd);
93 1.3 plunky static int server_auth_check (server_p srv, struct sockcred *cred);
94 1.1 gdamore
95 1.1 gdamore /*
96 1.1 gdamore * Initialize server
97 1.1 gdamore */
98 1.1 gdamore
99 1.1 gdamore int32_t
100 1.3 plunky server_init(server_p srv, char const *control, char const *sgroup)
101 1.1 gdamore {
102 1.1 gdamore struct sockaddr_un un;
103 1.1 gdamore struct sockaddr_bt l2;
104 1.1 gdamore socklen_t size;
105 1.1 gdamore int32_t unsock, l2sock;
106 1.1 gdamore uint16_t imtu;
107 1.1 gdamore int opt;
108 1.1 gdamore
109 1.1 gdamore assert(srv != NULL);
110 1.1 gdamore assert(control != NULL);
111 1.1 gdamore
112 1.1 gdamore memset(srv, 0, sizeof(srv));
113 1.3 plunky srv->sgroup = sgroup;
114 1.1 gdamore
115 1.1 gdamore /* Open control socket */
116 1.1 gdamore if (unlink(control) < 0 && errno != ENOENT) {
117 1.1 gdamore log_crit("Could not unlink(%s). %s (%d)",
118 1.1 gdamore control, strerror(errno), errno);
119 1.1 gdamore return (-1);
120 1.1 gdamore }
121 1.1 gdamore
122 1.1 gdamore unsock = socket(PF_LOCAL, SOCK_STREAM, 0);
123 1.1 gdamore if (unsock < 0) {
124 1.1 gdamore log_crit("Could not create control socket. %s (%d)",
125 1.1 gdamore strerror(errno), errno);
126 1.1 gdamore return (-1);
127 1.1 gdamore }
128 1.1 gdamore
129 1.1 gdamore opt = 1;
130 1.1 gdamore if (setsockopt(unsock, 0, LOCAL_CREDS, &opt, sizeof(opt)) < 0)
131 1.1 gdamore log_crit("Warning: No credential checks on control socket");
132 1.1 gdamore
133 1.1 gdamore memset(&un, 0, sizeof(un));
134 1.1 gdamore un.sun_len = sizeof(un);
135 1.1 gdamore un.sun_family = AF_LOCAL;
136 1.1 gdamore strlcpy(un.sun_path, control, sizeof(un.sun_path));
137 1.1 gdamore
138 1.1 gdamore if (bind(unsock, (struct sockaddr *) &un, sizeof(un)) < 0) {
139 1.1 gdamore log_crit("Could not bind control socket. %s (%d)",
140 1.1 gdamore strerror(errno), errno);
141 1.1 gdamore close(unsock);
142 1.1 gdamore return (-1);
143 1.1 gdamore }
144 1.1 gdamore
145 1.1 gdamore if (chmod(control, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) < 0) {
146 1.1 gdamore log_crit("Could not change permissions on control socket. " \
147 1.1 gdamore "%s (%d)", strerror(errno), errno);
148 1.1 gdamore close(unsock);
149 1.1 gdamore return (-1);
150 1.1 gdamore }
151 1.1 gdamore
152 1.1 gdamore if (listen(unsock, 10) < 0) {
153 1.1 gdamore log_crit("Could not listen on control socket. %s (%d)",
154 1.1 gdamore strerror(errno), errno);
155 1.1 gdamore close(unsock);
156 1.1 gdamore return (-1);
157 1.1 gdamore }
158 1.1 gdamore
159 1.1 gdamore /* Open L2CAP socket */
160 1.1 gdamore l2sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
161 1.1 gdamore if (l2sock < 0) {
162 1.1 gdamore log_crit("Could not create L2CAP socket. %s (%d)",
163 1.1 gdamore strerror(errno), errno);
164 1.1 gdamore close(unsock);
165 1.1 gdamore return (-1);
166 1.1 gdamore }
167 1.1 gdamore
168 1.1 gdamore size = sizeof(imtu);
169 1.1 gdamore if (getsockopt(l2sock, BTPROTO_L2CAP, SO_L2CAP_IMTU, &imtu, &size) < 0) {
170 1.1 gdamore log_crit("Could not get L2CAP IMTU. %s (%d)",
171 1.1 gdamore strerror(errno), errno);
172 1.1 gdamore close(unsock);
173 1.1 gdamore close(l2sock);
174 1.1 gdamore return (-1);
175 1.1 gdamore }
176 1.1 gdamore
177 1.1 gdamore memset(&l2, 0, sizeof(l2));
178 1.1 gdamore l2.bt_len = sizeof(l2);
179 1.1 gdamore l2.bt_family = AF_BLUETOOTH;
180 1.1 gdamore l2.bt_psm = L2CAP_PSM_SDP;
181 1.1 gdamore bdaddr_copy(&l2.bt_bdaddr, BDADDR_ANY);
182 1.1 gdamore
183 1.1 gdamore if (bind(l2sock, (struct sockaddr *) &l2, sizeof(l2)) < 0) {
184 1.1 gdamore log_crit("Could not bind L2CAP socket. %s (%d)",
185 1.1 gdamore strerror(errno), errno);
186 1.1 gdamore close(unsock);
187 1.1 gdamore close(l2sock);
188 1.1 gdamore return (-1);
189 1.1 gdamore }
190 1.1 gdamore
191 1.1 gdamore if (listen(l2sock, 10) < 0) {
192 1.1 gdamore log_crit("Could not listen on L2CAP socket. %s (%d)",
193 1.1 gdamore strerror(errno), errno);
194 1.1 gdamore close(unsock);
195 1.1 gdamore close(l2sock);
196 1.1 gdamore return (-1);
197 1.1 gdamore }
198 1.1 gdamore
199 1.1 gdamore /* Allocate incoming buffer */
200 1.1 gdamore srv->imtu = (imtu > SDP_LOCAL_MTU)? imtu : SDP_LOCAL_MTU;
201 1.1 gdamore srv->req = (uint8_t *) calloc(srv->imtu, sizeof(srv->req[0]));
202 1.1 gdamore if (srv->req == NULL) {
203 1.1 gdamore log_crit("Could not allocate request buffer");
204 1.1 gdamore close(unsock);
205 1.1 gdamore close(l2sock);
206 1.1 gdamore return (-1);
207 1.1 gdamore }
208 1.1 gdamore
209 1.1 gdamore /* Allocate memory for descriptor index */
210 1.1 gdamore srv->fdidx = (fd_idx_p) calloc(FD_SETSIZE, sizeof(srv->fdidx[0]));
211 1.1 gdamore if (srv->fdidx == NULL) {
212 1.1 gdamore log_crit("Could not allocate fd index");
213 1.1 gdamore free(srv->req);
214 1.1 gdamore close(unsock);
215 1.1 gdamore close(l2sock);
216 1.1 gdamore return (-1);
217 1.1 gdamore }
218 1.1 gdamore
219 1.1 gdamore /* Register Service Discovery profile (attach it to control socket) */
220 1.1 gdamore if (provider_register_sd(unsock) < 0) {
221 1.1 gdamore log_crit("Could not register Service Discovery profile");
222 1.1 gdamore free(srv->fdidx);
223 1.1 gdamore free(srv->req);
224 1.1 gdamore close(unsock);
225 1.1 gdamore close(l2sock);
226 1.1 gdamore return (-1);
227 1.1 gdamore }
228 1.1 gdamore
229 1.1 gdamore /*
230 1.1 gdamore * If we got here then everything is fine. Add both control sockets
231 1.1 gdamore * to the index.
232 1.1 gdamore */
233 1.1 gdamore
234 1.1 gdamore FD_ZERO(&srv->fdset);
235 1.1 gdamore srv->maxfd = (unsock > l2sock)? unsock : l2sock;
236 1.1 gdamore
237 1.1 gdamore FD_SET(unsock, &srv->fdset);
238 1.1 gdamore srv->fdidx[unsock].valid = 1;
239 1.1 gdamore srv->fdidx[unsock].server = 1;
240 1.1 gdamore srv->fdidx[unsock].control = 1;
241 1.1 gdamore srv->fdidx[unsock].priv = 0;
242 1.1 gdamore srv->fdidx[unsock].rsp_cs = 0;
243 1.1 gdamore srv->fdidx[unsock].rsp_size = 0;
244 1.1 gdamore srv->fdidx[unsock].rsp_limit = 0;
245 1.1 gdamore srv->fdidx[unsock].omtu = SDP_LOCAL_MTU;
246 1.1 gdamore srv->fdidx[unsock].rsp = NULL;
247 1.1 gdamore
248 1.1 gdamore FD_SET(l2sock, &srv->fdset);
249 1.1 gdamore srv->fdidx[l2sock].valid = 1;
250 1.1 gdamore srv->fdidx[l2sock].server = 1;
251 1.1 gdamore srv->fdidx[l2sock].control = 0;
252 1.1 gdamore srv->fdidx[l2sock].priv = 0;
253 1.1 gdamore srv->fdidx[l2sock].rsp_cs = 0;
254 1.1 gdamore srv->fdidx[l2sock].rsp_size = 0;
255 1.1 gdamore srv->fdidx[l2sock].rsp_limit = 0;
256 1.1 gdamore srv->fdidx[l2sock].omtu = 0; /* unknown */
257 1.1 gdamore srv->fdidx[l2sock].rsp = NULL;
258 1.1 gdamore
259 1.1 gdamore return (0);
260 1.1 gdamore }
261 1.1 gdamore
262 1.1 gdamore /*
263 1.1 gdamore * Shutdown server
264 1.1 gdamore */
265 1.1 gdamore
266 1.1 gdamore void
267 1.1 gdamore server_shutdown(server_p srv)
268 1.1 gdamore {
269 1.1 gdamore int fd;
270 1.1 gdamore
271 1.1 gdamore assert(srv != NULL);
272 1.1 gdamore
273 1.1 gdamore for (fd = 0; fd < srv->maxfd + 1; fd ++)
274 1.1 gdamore if (srv->fdidx[fd].valid)
275 1.1 gdamore server_close_fd(srv, fd);
276 1.1 gdamore
277 1.1 gdamore free(srv->req);
278 1.1 gdamore free(srv->fdidx);
279 1.1 gdamore
280 1.1 gdamore memset(srv, 0, sizeof(*srv));
281 1.1 gdamore }
282 1.1 gdamore
283 1.1 gdamore /*
284 1.1 gdamore * Do one server iteration
285 1.1 gdamore */
286 1.1 gdamore
287 1.1 gdamore int32_t
288 1.1 gdamore server_do(server_p srv)
289 1.1 gdamore {
290 1.1 gdamore fd_set fdset;
291 1.1 gdamore int32_t n, fd;
292 1.1 gdamore
293 1.1 gdamore assert(srv != NULL);
294 1.1 gdamore
295 1.1 gdamore /* Copy cached version of the fd set and call select */
296 1.1 gdamore memcpy(&fdset, &srv->fdset, sizeof(fdset));
297 1.1 gdamore n = select(srv->maxfd + 1, &fdset, NULL, NULL, NULL);
298 1.1 gdamore if (n < 0) {
299 1.1 gdamore if (errno == EINTR)
300 1.1 gdamore return (0);
301 1.1 gdamore
302 1.1 gdamore log_err("Could not select(%d, %p). %s (%d)",
303 1.1 gdamore srv->maxfd + 1, &fdset, strerror(errno), errno);
304 1.1 gdamore
305 1.1 gdamore return (-1);
306 1.1 gdamore }
307 1.1 gdamore
308 1.1 gdamore /* Process descriptors */
309 1.1 gdamore for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) {
310 1.1 gdamore if (!FD_ISSET(fd, &fdset))
311 1.1 gdamore continue;
312 1.1 gdamore
313 1.1 gdamore assert(srv->fdidx[fd].valid);
314 1.1 gdamore n --;
315 1.1 gdamore
316 1.1 gdamore if (srv->fdidx[fd].server)
317 1.1 gdamore server_accept_client(srv, fd);
318 1.1 gdamore else if (server_process_request(srv, fd) != 0)
319 1.1 gdamore server_close_fd(srv, fd);
320 1.1 gdamore }
321 1.1 gdamore
322 1.1 gdamore return (0);
323 1.1 gdamore
324 1.1 gdamore }
325 1.1 gdamore
326 1.1 gdamore /*
327 1.1 gdamore * Accept new client connection and register it with index
328 1.1 gdamore */
329 1.1 gdamore
330 1.1 gdamore static void
331 1.1 gdamore server_accept_client(server_p srv, int32_t fd)
332 1.1 gdamore {
333 1.1 gdamore uint8_t *rsp = NULL;
334 1.1 gdamore socklen_t size;
335 1.1 gdamore int32_t cfd;
336 1.1 gdamore uint16_t omtu;
337 1.1 gdamore
338 1.1 gdamore do {
339 1.1 gdamore cfd = accept(fd, NULL, NULL);
340 1.1 gdamore } while (cfd < 0 && errno == EINTR);
341 1.1 gdamore
342 1.1 gdamore if (cfd < 0) {
343 1.1 gdamore log_err("Could not accept connection on %s socket. %s (%d)",
344 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
345 1.1 gdamore strerror(errno), errno);
346 1.1 gdamore return;
347 1.1 gdamore }
348 1.1 gdamore
349 1.1 gdamore assert(!FD_ISSET(cfd, &srv->fdset));
350 1.1 gdamore assert(!srv->fdidx[cfd].valid);
351 1.1 gdamore
352 1.1 gdamore if (!srv->fdidx[fd].control) {
353 1.1 gdamore /* Get local BD_ADDR */
354 1.1 gdamore size = sizeof(srv->req_sa);
355 1.1 gdamore if (getsockname(cfd,(struct sockaddr*)&srv->req_sa, &size) < 0) {
356 1.1 gdamore log_err("Could not get local BD_ADDR. %s (%d)",
357 1.1 gdamore strerror(errno), errno);
358 1.1 gdamore close(cfd);
359 1.1 gdamore return;
360 1.1 gdamore }
361 1.1 gdamore
362 1.1 gdamore /* Get outgoing MTU */
363 1.1 gdamore size = sizeof(omtu);
364 1.1 gdamore if (getsockopt(cfd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &omtu, &size) < 0) {
365 1.1 gdamore log_err("Could not get L2CAP OMTU. %s (%d)",
366 1.1 gdamore strerror(errno), errno);
367 1.1 gdamore close(cfd);
368 1.1 gdamore return;
369 1.1 gdamore }
370 1.1 gdamore
371 1.1 gdamore /*
372 1.1 gdamore * The maximum size of the L2CAP packet is 65536 bytes.
373 1.1 gdamore * The minimum L2CAP MTU is 43 bytes. That means we need
374 1.1 gdamore * 65536 / 43 = ~1524 chunks to transfer maximum packet
375 1.1 gdamore * size with minimum MTU. The "rsp_cs" field in fd_idx_t
376 1.1 gdamore * is 11 bit wide that gives us upto 2048 chunks.
377 1.1 gdamore */
378 1.1 gdamore
379 1.1 gdamore if (omtu < L2CAP_MTU_MINIMUM) {
380 1.1 gdamore log_err("L2CAP OMTU is too small (%d bytes)", omtu);
381 1.1 gdamore close(cfd);
382 1.1 gdamore return;
383 1.1 gdamore }
384 1.1 gdamore } else {
385 1.1 gdamore bdaddr_copy(&srv->req_sa.bt_bdaddr, BDADDR_ANY);
386 1.1 gdamore omtu = srv->fdidx[fd].omtu;
387 1.1 gdamore }
388 1.1 gdamore
389 1.1 gdamore /*
390 1.1 gdamore * Allocate buffer. This is an overkill, but we can not know how
391 1.1 gdamore * big our reply is going to be.
392 1.1 gdamore */
393 1.1 gdamore
394 1.1 gdamore rsp = (uint8_t *) calloc(L2CAP_MTU_MAXIMUM, sizeof(rsp[0]));
395 1.1 gdamore if (rsp == NULL) {
396 1.1 gdamore log_crit("Could not allocate response buffer");
397 1.1 gdamore close(cfd);
398 1.1 gdamore return;
399 1.1 gdamore }
400 1.1 gdamore
401 1.1 gdamore /* Add client descriptor to the index */
402 1.1 gdamore FD_SET(cfd, &srv->fdset);
403 1.1 gdamore if (srv->maxfd < cfd)
404 1.1 gdamore srv->maxfd = cfd;
405 1.1 gdamore srv->fdidx[cfd].valid = 1;
406 1.1 gdamore srv->fdidx[cfd].server = 0;
407 1.1 gdamore srv->fdidx[cfd].control = srv->fdidx[fd].control;
408 1.1 gdamore srv->fdidx[cfd].priv = 0;
409 1.1 gdamore srv->fdidx[cfd].rsp_cs = 0;
410 1.1 gdamore srv->fdidx[cfd].rsp_size = 0;
411 1.1 gdamore srv->fdidx[cfd].rsp_limit = 0;
412 1.1 gdamore srv->fdidx[cfd].omtu = omtu;
413 1.1 gdamore srv->fdidx[cfd].rsp = rsp;
414 1.1 gdamore }
415 1.1 gdamore
416 1.1 gdamore /*
417 1.1 gdamore * Process request from the client
418 1.1 gdamore */
419 1.1 gdamore
420 1.1 gdamore static int32_t
421 1.1 gdamore server_process_request(server_p srv, int32_t fd)
422 1.1 gdamore {
423 1.1 gdamore uint8_t ctl[128];
424 1.1 gdamore sdp_pdu_p pdu = (sdp_pdu_p) srv->req;
425 1.1 gdamore struct msghdr msg;
426 1.1 gdamore struct iovec iov;
427 1.1 gdamore int32_t len, error;
428 1.1 gdamore struct cmsghdr *cmsg;
429 1.1 gdamore
430 1.1 gdamore assert(srv->imtu > 0);
431 1.1 gdamore assert(srv->req != NULL);
432 1.1 gdamore assert(FD_ISSET(fd, &srv->fdset));
433 1.1 gdamore assert(srv->fdidx[fd].valid);
434 1.1 gdamore assert(!srv->fdidx[fd].server);
435 1.1 gdamore assert(srv->fdidx[fd].rsp != NULL);
436 1.1 gdamore assert(srv->fdidx[fd].omtu >= L2CAP_MTU_MINIMUM);
437 1.1 gdamore
438 1.1 gdamore iov.iov_base = srv->req;
439 1.1 gdamore iov.iov_len = srv->imtu;
440 1.1 gdamore
441 1.1 gdamore msg.msg_name = NULL;
442 1.1 gdamore msg.msg_namelen = 0;
443 1.1 gdamore msg.msg_iov = &iov;
444 1.1 gdamore msg.msg_iovlen = 1;
445 1.1 gdamore msg.msg_control = ctl;
446 1.1 gdamore msg.msg_controllen = sizeof(ctl);
447 1.1 gdamore msg.msg_flags = 0;
448 1.1 gdamore
449 1.1 gdamore do {
450 1.1 gdamore len = recvmsg(fd, &msg, 0);
451 1.1 gdamore } while (len < 0 && errno == EINTR);
452 1.1 gdamore
453 1.1 gdamore if (len < 0) {
454 1.1 gdamore log_err("Could not receive SDP request from %s socket. %s (%d)",
455 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
456 1.1 gdamore strerror(errno), errno);
457 1.1 gdamore return (-1);
458 1.1 gdamore }
459 1.1 gdamore if (len == 0) {
460 1.1 gdamore log_info("Client on %s socket has disconnected",
461 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP");
462 1.1 gdamore return (-1);
463 1.1 gdamore }
464 1.1 gdamore
465 1.1 gdamore if ((cmsg = CMSG_FIRSTHDR(&msg)) != NULL
466 1.1 gdamore && cmsg->cmsg_level == SOL_SOCKET
467 1.1 gdamore && cmsg->cmsg_type == SCM_CREDS
468 1.3 plunky && cmsg->cmsg_len >= CMSG_LEN(SOCKCREDSIZE(0)))
469 1.3 plunky srv->fdidx[fd].priv =
470 1.3 plunky server_auth_check(srv, (struct sockcred *)CMSG_DATA(cmsg));
471 1.2 plunky
472 1.2 plunky if (len >= sizeof(*pdu)
473 1.2 plunky && (sizeof(*pdu) + (pdu->len = ntohs(pdu->len))) == len) {
474 1.1 gdamore switch (pdu->pid) {
475 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_REQUEST:
476 1.1 gdamore error = server_prepare_service_search_response(srv, fd);
477 1.1 gdamore break;
478 1.1 gdamore
479 1.1 gdamore case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST:
480 1.1 gdamore error = server_prepare_service_attribute_response(srv, fd);
481 1.1 gdamore break;
482 1.1 gdamore
483 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST:
484 1.1 gdamore error = server_prepare_service_search_attribute_response(srv, fd);
485 1.1 gdamore break;
486 1.1 gdamore
487 1.1 gdamore case SDP_PDU_SERVICE_REGISTER_REQUEST:
488 1.1 gdamore error = server_prepare_service_register_response(srv, fd);
489 1.1 gdamore break;
490 1.1 gdamore
491 1.1 gdamore case SDP_PDU_SERVICE_UNREGISTER_REQUEST:
492 1.1 gdamore error = server_prepare_service_unregister_response(srv, fd);
493 1.1 gdamore break;
494 1.1 gdamore
495 1.1 gdamore case SDP_PDU_SERVICE_CHANGE_REQUEST:
496 1.1 gdamore error = server_prepare_service_change_response(srv, fd);
497 1.1 gdamore break;
498 1.1 gdamore
499 1.1 gdamore default:
500 1.1 gdamore error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
501 1.1 gdamore break;
502 1.1 gdamore }
503 1.1 gdamore } else
504 1.1 gdamore error = SDP_ERROR_CODE_INVALID_PDU_SIZE;
505 1.1 gdamore
506 1.1 gdamore if (error == 0) {
507 1.1 gdamore switch (pdu->pid) {
508 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_REQUEST:
509 1.1 gdamore error = server_send_service_search_response(srv, fd);
510 1.1 gdamore break;
511 1.1 gdamore
512 1.1 gdamore case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST:
513 1.1 gdamore error = server_send_service_attribute_response(srv, fd);
514 1.1 gdamore break;
515 1.1 gdamore
516 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST:
517 1.1 gdamore error = server_send_service_search_attribute_response(srv, fd);
518 1.1 gdamore break;
519 1.1 gdamore
520 1.1 gdamore case SDP_PDU_SERVICE_REGISTER_REQUEST:
521 1.1 gdamore error = server_send_service_register_response(srv, fd);
522 1.1 gdamore break;
523 1.1 gdamore
524 1.1 gdamore case SDP_PDU_SERVICE_UNREGISTER_REQUEST:
525 1.1 gdamore error = server_send_service_unregister_response(srv, fd);
526 1.1 gdamore break;
527 1.1 gdamore
528 1.1 gdamore case SDP_PDU_SERVICE_CHANGE_REQUEST:
529 1.1 gdamore error = server_send_service_change_response(srv, fd);
530 1.1 gdamore break;
531 1.1 gdamore
532 1.1 gdamore default:
533 1.1 gdamore error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
534 1.1 gdamore break;
535 1.1 gdamore }
536 1.1 gdamore
537 1.1 gdamore if (error != 0)
538 1.1 gdamore log_err("Could not send SDP response to %s socket, " \
539 1.1 gdamore "pdu->pid=%d, pdu->tid=%d, error=%d",
540 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
541 1.1 gdamore pdu->pid, ntohs(pdu->tid), error);
542 1.1 gdamore } else {
543 1.1 gdamore log_err("Could not process SDP request from %s socket, " \
544 1.1 gdamore "pdu->pid=%d, pdu->tid=%d, pdu->len=%d, len=%d, " \
545 1.1 gdamore "error=%d",
546 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
547 1.1 gdamore pdu->pid, ntohs(pdu->tid), pdu->len, len, error);
548 1.1 gdamore
549 1.1 gdamore error = server_send_error_response(srv, fd, error);
550 1.1 gdamore if (error != 0)
551 1.1 gdamore log_err("Could not send SDP error response to %s " \
552 1.1 gdamore "socket, pdu->pid=%d, pdu->tid=%d, error=%d",
553 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
554 1.1 gdamore pdu->pid, ntohs(pdu->tid), error);
555 1.1 gdamore }
556 1.1 gdamore
557 1.1 gdamore /* On error forget response (if any) */
558 1.1 gdamore if (error != 0) {
559 1.1 gdamore srv->fdidx[fd].rsp_cs = 0;
560 1.1 gdamore srv->fdidx[fd].rsp_size = 0;
561 1.1 gdamore srv->fdidx[fd].rsp_limit = 0;
562 1.1 gdamore }
563 1.1 gdamore
564 1.1 gdamore return (error);
565 1.1 gdamore }
566 1.1 gdamore
567 1.1 gdamore /*
568 1.1 gdamore * Send SDP_Error_Response PDU
569 1.1 gdamore */
570 1.1 gdamore
571 1.1 gdamore static int32_t
572 1.1 gdamore server_send_error_response(server_p srv, int32_t fd, uint16_t error)
573 1.1 gdamore {
574 1.1 gdamore int32_t size;
575 1.1 gdamore
576 1.1 gdamore struct {
577 1.1 gdamore sdp_pdu_t pdu;
578 1.1 gdamore uint16_t error;
579 1.4 perry } __packed rsp;
580 1.1 gdamore
581 1.1 gdamore /* Prepare and send SDP error response */
582 1.1 gdamore rsp.pdu.pid = SDP_PDU_ERROR_RESPONSE;
583 1.1 gdamore rsp.pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
584 1.1 gdamore rsp.pdu.len = htons(sizeof(rsp.error));
585 1.1 gdamore rsp.error = htons(error);
586 1.1 gdamore
587 1.1 gdamore do {
588 1.1 gdamore size = write(fd, &rsp, sizeof(rsp));
589 1.1 gdamore } while (size < 0 && errno == EINTR);
590 1.1 gdamore
591 1.1 gdamore return ((size < 0)? errno : 0);
592 1.1 gdamore }
593 1.1 gdamore
594 1.1 gdamore /*
595 1.1 gdamore * Close descriptor and remove it from index
596 1.1 gdamore */
597 1.1 gdamore
598 1.1 gdamore static void
599 1.1 gdamore server_close_fd(server_p srv, int32_t fd)
600 1.1 gdamore {
601 1.1 gdamore provider_p provider = NULL, provider_next = NULL;
602 1.1 gdamore
603 1.1 gdamore assert(FD_ISSET(fd, &srv->fdset));
604 1.1 gdamore assert(srv->fdidx[fd].valid);
605 1.1 gdamore
606 1.1 gdamore close(fd);
607 1.1 gdamore
608 1.1 gdamore FD_CLR(fd, &srv->fdset);
609 1.1 gdamore if (fd == srv->maxfd)
610 1.1 gdamore srv->maxfd --;
611 1.1 gdamore
612 1.1 gdamore if (srv->fdidx[fd].rsp != NULL)
613 1.1 gdamore free(srv->fdidx[fd].rsp);
614 1.1 gdamore
615 1.1 gdamore memset(&srv->fdidx[fd], 0, sizeof(srv->fdidx[fd]));
616 1.1 gdamore
617 1.1 gdamore for (provider = provider_get_first();
618 1.1 gdamore provider != NULL;
619 1.1 gdamore provider = provider_next) {
620 1.1 gdamore provider_next = provider_get_next(provider);
621 1.1 gdamore
622 1.1 gdamore if (provider->fd == fd)
623 1.1 gdamore provider_unregister(provider);
624 1.1 gdamore }
625 1.1 gdamore }
626 1.3 plunky
627 1.3 plunky static int
628 1.3 plunky server_auth_check(server_p srv, struct sockcred *cred)
629 1.3 plunky {
630 1.3 plunky struct group *grp;
631 1.3 plunky int n;
632 1.3 plunky
633 1.3 plunky if (cred == NULL)
634 1.3 plunky return 0;
635 1.3 plunky
636 1.3 plunky if (cred->sc_uid == 0 || cred->sc_euid == 0)
637 1.3 plunky return 1;
638 1.3 plunky
639 1.3 plunky if (srv->sgroup == NULL)
640 1.3 plunky return 0;
641 1.3 plunky
642 1.3 plunky grp = getgrnam(srv->sgroup);
643 1.3 plunky if (grp == NULL) {
644 1.3 plunky log_err("No gid for group '%s'", srv->sgroup);
645 1.3 plunky srv->sgroup = NULL;
646 1.3 plunky return 0;
647 1.3 plunky }
648 1.3 plunky
649 1.3 plunky if (cred->sc_gid == grp->gr_gid || cred->sc_egid == grp->gr_gid)
650 1.3 plunky return 1;
651 1.3 plunky
652 1.3 plunky for (n = 0 ; n < cred->sc_ngroups ; n++) {
653 1.3 plunky if (cred->sc_groups[n] == grp->gr_gid)
654 1.3 plunky return 1;
655 1.3 plunky }
656 1.3 plunky
657 1.3 plunky return 0;
658 1.3 plunky }
659