rfcomm_pppd.c revision 126169
1114879Sjulian/*
2114879Sjulian * rfcomm_pppd.c
3114879Sjulian *
4114879Sjulian * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5114879Sjulian * All rights reserved.
6114879Sjulian *
7114879Sjulian * Redistribution and use in source and binary forms, with or without
8114879Sjulian * modification, are permitted provided that the following conditions
9114879Sjulian * are met:
10114879Sjulian * 1. Redistributions of source code must retain the above copyright
11114879Sjulian *    notice, this list of conditions and the following disclaimer.
12114879Sjulian * 2. Redistributions in binary form must reproduce the above copyright
13114879Sjulian *    notice, this list of conditions and the following disclaimer in the
14114879Sjulian *    documentation and/or other materials provided with the distribution.
15114879Sjulian *
16114879Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17114879Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18114879Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19114879Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20114879Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21114879Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22114879Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23114879Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24114879Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25114879Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26114879Sjulian * SUCH DAMAGE.
27114879Sjulian *
28121054Semax * $Id: rfcomm_pppd.c,v 1.5 2003/09/07 18:32:11 max Exp $
29114879Sjulian * $FreeBSD: head/usr.sbin/bluetooth/rfcomm_pppd/rfcomm_pppd.c 126169 2004-02-23 17:30:59Z emax $
30114879Sjulian */
31114879Sjulian
32121054Semax#include <bluetooth.h>
33121054Semax#include <ctype.h>
34121054Semax#include <err.h>
35114879Sjulian#include <errno.h>
36114879Sjulian#include <fcntl.h>
37121054Semax#include <sdp.h>
38114879Sjulian#include <signal.h>
39114879Sjulian#include <stdarg.h>
40114879Sjulian#include <stdio.h>
41114879Sjulian#include <stdlib.h>
42114879Sjulian#include <string.h>
43114879Sjulian#include <syslog.h>
44114879Sjulian#include <unistd.h>
45114879Sjulian
46114879Sjulian#define RFCOMM_PPPD	"rfcomm_pppd"
47114879Sjulian
48121054Semaxint		rfcomm_channel_lookup	(bdaddr_t const *local,
49121054Semax					 bdaddr_t const *remote,
50121054Semax					 int service, int *channel, int *error);
51121054Semax
52126169Semaxstatic void	exec_ppp	(int s, char *unit, char *label);
53114879Sjulianstatic void	sighandler	(int s);
54114879Sjulianstatic void	usage		(void);
55114879Sjulian
56114879Sjulianstatic int	done;
57114879Sjulian
58114879Sjulian/* Main */
59114879Sjulianint
60114879Sjulianmain(int argc, char *argv[])
61114879Sjulian{
62114879Sjulian	struct sockaddr_rfcomm   sock_addr;
63126169Semax	char			*label = NULL, *unit = NULL, *ep = NULL;
64114879Sjulian	bdaddr_t		 addr;
65121054Semax	int			 s, channel, detach, server, service;
66114879Sjulian	pid_t			 pid;
67114879Sjulian
68114879Sjulian	memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
69114879Sjulian	channel = 0;
70114879Sjulian	detach = 1;
71114879Sjulian	server = 0;
72121054Semax	service = 0;
73114879Sjulian
74114879Sjulian	/* Parse command line arguments */
75126169Semax	while ((s = getopt(argc, argv, "a:cC:dhl:su:")) != -1) {
76114879Sjulian		switch (s) {
77121054Semax		case 'a': /* BDADDR */
78121054Semax			if (!bt_aton(optarg, &addr)) {
79121054Semax				struct hostent	*he = NULL;
80114879Sjulian
81121054Semax				if ((he = bt_gethostbyname(optarg)) == NULL)
82121054Semax					errx(1, "%s: %s", optarg, hstrerror(h_errno));
83114879Sjulian
84121054Semax				memcpy(&addr, he->h_addr, sizeof(addr));
85121054Semax			}
86121054Semax			break;
87114879Sjulian
88114879Sjulian		case 'c': /* client */
89114879Sjulian			server = 0;
90114879Sjulian			break;
91114879Sjulian
92114879Sjulian		case 'C': /* RFCOMM channel */
93121054Semax			channel = strtoul(optarg, &ep, 10);
94126169Semax			if (*ep != '\0') {
95121054Semax				channel = 0;
96121054Semax				switch (tolower(optarg[0])) {
97121054Semax				case 'd': /* DialUp Networking */
98121054Semax					service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
99121054Semax					break;
100121054Semax
101121054Semax				case 'l': /* LAN Access Using PPP */
102121054Semax					service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
103121054Semax					break;
104121054Semax				}
105121054Semax			}
106114879Sjulian			break;
107114879Sjulian
108114879Sjulian		case 'd': /* do not detach */
109114879Sjulian			detach = 0;
110114879Sjulian			break;
111114879Sjulian
112114879Sjulian		case 'l': /* PPP label */
113114879Sjulian			label = optarg;
114114879Sjulian			break;
115114879Sjulian
116126169Semax		case 's': /* server */
117114879Sjulian			server = 1;
118114879Sjulian			break;
119114879Sjulian
120126169Semax		case 'u': /* PPP -unit option */
121126169Semax			strtoul(optarg, &ep, 10);
122126169Semax			if (*ep != '\0')
123126169Semax				usage();
124126169Semax				/* NOT REACHED */
125126169Semax
126126169Semax			unit = optarg;
127126169Semax			break;
128126169Semax
129114879Sjulian		case 'h':
130114879Sjulian		default:
131114879Sjulian			usage();
132114879Sjulian			/* NOT REACHED */
133114879Sjulian		}
134114879Sjulian	}
135114879Sjulian
136114879Sjulian	/* Check if we got everything we wanted */
137121054Semax	if (label == NULL)
138121054Semax                errx(1, "Must specify PPP label");
139114879Sjulian
140121054Semax	if (!server) {
141121054Semax		if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
142121054Semax                	errx(1, "Must specify server BD_ADDR");
143121054Semax
144121054Semax		/* Check channel, if was not set then obtain it via SDP */
145121054Semax		if (channel == 0 && service != 0)
146121054Semax			if (rfcomm_channel_lookup(NULL, &addr, service,
147121054Semax							&channel, &s) != 0)
148121054Semax				errc(1, s, "Could not obtain RFCOMM channel");
149121054Semax	}
150121054Semax
151121054Semax        if (channel <= 0 || channel > 30)
152121054Semax                errx(1, "Invalid RFCOMM channel number %d", channel);
153121054Semax
154114879Sjulian	openlog(RFCOMM_PPPD, LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
155114879Sjulian
156114879Sjulian	if (detach) {
157114879Sjulian		pid = fork();
158114879Sjulian		if (pid == (pid_t) -1) {
159114879Sjulian			syslog(LOG_ERR, "Could not fork(). %s (%d)",
160114879Sjulian				strerror(errno), errno);
161114879Sjulian			exit(1);
162114879Sjulian		}
163114879Sjulian
164114879Sjulian		if (pid != 0)
165114879Sjulian			exit(0);
166114879Sjulian
167114879Sjulian		if (daemon(0, 0) < 0) {
168114879Sjulian			syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)",
169114879Sjulian				strerror(errno), errno);
170114879Sjulian			exit(1);
171114879Sjulian		}
172114879Sjulian	}
173114879Sjulian
174114879Sjulian	s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
175114879Sjulian	if (s < 0) {
176114879Sjulian		syslog(LOG_ERR, "Could not create socket. %s (%d)",
177114879Sjulian			strerror(errno), errno);
178114879Sjulian		exit(1);
179114879Sjulian	}
180114879Sjulian
181114879Sjulian	if (server) {
182126169Semax		struct sigaction	 sa;
183126169Semax		void			*ss = NULL;
184126169Semax		sdp_lan_profile_t	 lan;
185114879Sjulian
186114879Sjulian		/* Install signal handler */
187114879Sjulian		memset(&sa, 0, sizeof(sa));
188114879Sjulian		sa.sa_handler = sighandler;
189114879Sjulian
190114879Sjulian		if (sigaction(SIGTERM, &sa, NULL) < 0) {
191114879Sjulian			syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)",
192114879Sjulian				strerror(errno), errno);
193114879Sjulian			exit(1);
194114879Sjulian		}
195114879Sjulian
196114879Sjulian		if (sigaction(SIGHUP, &sa, NULL) < 0) {
197114879Sjulian			syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)",
198114879Sjulian				strerror(errno), errno);
199114879Sjulian			exit(1);
200114879Sjulian		}
201114879Sjulian
202114879Sjulian		if (sigaction(SIGINT, &sa, NULL) < 0) {
203114879Sjulian			syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)",
204114879Sjulian				strerror(errno), errno);
205114879Sjulian			exit(1);
206114879Sjulian		}
207114879Sjulian
208114879Sjulian		sa.sa_handler = SIG_IGN;
209114879Sjulian		sa.sa_flags = SA_NOCLDWAIT;
210114879Sjulian
211114879Sjulian		if (sigaction(SIGCHLD, &sa, NULL) < 0) {
212114879Sjulian			syslog(LOG_ERR, "Could not sigaction(SIGCHLD). %s (%d)",
213114879Sjulian				strerror(errno), errno);
214114879Sjulian			exit(1);
215114879Sjulian		}
216114879Sjulian
217114879Sjulian		/* bind socket and listen for incoming connections */
218114879Sjulian		sock_addr.rfcomm_len = sizeof(sock_addr);
219114879Sjulian		sock_addr.rfcomm_family = AF_BLUETOOTH;
220114879Sjulian		memcpy(&sock_addr.rfcomm_bdaddr, &addr,
221114879Sjulian			sizeof(sock_addr.rfcomm_bdaddr));
222114879Sjulian		sock_addr.rfcomm_channel = channel;
223114879Sjulian
224114879Sjulian		if (bind(s, (struct sockaddr *) &sock_addr,
225114879Sjulian				sizeof(sock_addr)) < 0) {
226114879Sjulian			syslog(LOG_ERR, "Could not bind socket. %s (%d)",
227114879Sjulian				strerror(errno), errno);
228114879Sjulian			exit(1);
229114879Sjulian		}
230114879Sjulian
231114879Sjulian		if (listen(s, 10) < 0) {
232114879Sjulian			syslog(LOG_ERR, "Could not listen on socket. %s (%d)",
233114879Sjulian				strerror(errno), errno);
234114879Sjulian			exit(1);
235114879Sjulian		}
236114879Sjulian
237126169Semax		ss = sdp_open_local(NULL);
238126169Semax		if (ss == NULL) {
239126169Semax			syslog(LOG_ERR, "Unable to create local SDP session");
240126169Semax			exit(1);
241126169Semax		}
242126169Semax
243126169Semax		if (sdp_error(ss) != 0) {
244126169Semax			syslog(LOG_ERR, "Unable to open local SDP session. " \
245126169Semax				"%s (%d)", strerror(sdp_error(ss)),
246126169Semax				sdp_error(ss));
247126169Semax			exit(1);
248126169Semax		}
249126169Semax
250126169Semax		memset(&lan, 0, sizeof(lan));
251126169Semax		lan.server_channel = channel;
252126169Semax
253126169Semax		if (sdp_register_service(ss,
254126169Semax				SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
255126169Semax				&addr, (void *) &lan, sizeof(lan), NULL) != 0) {
256126169Semax			syslog(LOG_ERR, "Unable to register LAN service with " \
257126169Semax				"local SDP daemon. %s (%d)",
258126169Semax				strerror(sdp_error(ss)), sdp_error(ss));
259126169Semax			exit(1);
260126169Semax		}
261126169Semax
262114879Sjulian		for (done = 0; !done; ) {
263114879Sjulian			int	len = sizeof(sock_addr);
264114879Sjulian			int	s1 = accept(s, (struct sockaddr *) &sock_addr, &len);
265114879Sjulian
266114879Sjulian			if (s1 < 0) {
267114879Sjulian				syslog(LOG_ERR, "Could not accept connection " \
268114879Sjulian					"on socket. %s (%d)", strerror(errno),
269114879Sjulian					errno);
270114879Sjulian				exit(1);
271114879Sjulian			}
272114879Sjulian
273114879Sjulian			pid = fork();
274114879Sjulian			if (pid == (pid_t) -1) {
275114879Sjulian				syslog(LOG_ERR, "Could not fork(). %s (%d)",
276114879Sjulian					strerror(errno), errno);
277114879Sjulian				exit(1);
278114879Sjulian			}
279114879Sjulian
280114879Sjulian			if (pid == 0) {
281126169Semax				sdp_close(ss);
282114879Sjulian				close(s);
283114879Sjulian
284114879Sjulian				/* Reset signal handler */
285114879Sjulian				memset(&sa, 0, sizeof(sa));
286114879Sjulian				sa.sa_handler = SIG_DFL;
287114879Sjulian
288114879Sjulian				sigaction(SIGTERM, &sa, NULL);
289114879Sjulian				sigaction(SIGHUP, &sa, NULL);
290114879Sjulian				sigaction(SIGINT, &sa, NULL);
291114879Sjulian				sigaction(SIGCHLD, &sa, NULL);
292114879Sjulian
293114879Sjulian				/* Become daemon */
294114879Sjulian				daemon(0, 0);
295114879Sjulian
296126169Semax				/*
297126169Semax				 * XXX Make sure user does not shoot himself
298126169Semax				 * in the foot. Do not pass unit option to the
299126169Semax				 * PPP when operating in the server mode.
300126169Semax				 */
301126169Semax
302126169Semax				exec_ppp(s1, NULL, label);
303114879Sjulian			} else
304114879Sjulian				close(s1);
305114879Sjulian		}
306114879Sjulian	} else {
307114879Sjulian		sock_addr.rfcomm_len = sizeof(sock_addr);
308114879Sjulian		sock_addr.rfcomm_family = AF_BLUETOOTH;
309114879Sjulian		memcpy(&sock_addr.rfcomm_bdaddr, NG_HCI_BDADDR_ANY,
310114879Sjulian			sizeof(sock_addr.rfcomm_bdaddr));
311114879Sjulian		sock_addr.rfcomm_channel = 0;
312114879Sjulian
313114879Sjulian		if (bind(s, (struct sockaddr *) &sock_addr,
314114879Sjulian				sizeof(sock_addr)) < 0) {
315114879Sjulian			syslog(LOG_ERR, "Could not bind socket. %s (%d)",
316114879Sjulian				strerror(errno), errno);
317114879Sjulian			exit(1);
318114879Sjulian		}
319114879Sjulian
320114879Sjulian		memcpy(&sock_addr.rfcomm_bdaddr, &addr,
321114879Sjulian			sizeof(sock_addr.rfcomm_bdaddr));
322114879Sjulian		sock_addr.rfcomm_channel = channel;
323114879Sjulian
324114879Sjulian		if (connect(s, (struct sockaddr *) &sock_addr,
325114879Sjulian				sizeof(sock_addr)) < 0) {
326114879Sjulian			syslog(LOG_ERR, "Could not connect socket. %s (%d)",
327114879Sjulian				strerror(errno), errno);
328114879Sjulian			exit(1);
329114879Sjulian		}
330114879Sjulian
331126169Semax		exec_ppp(s, unit, label);
332114879Sjulian	}
333114879Sjulian
334114879Sjulian	exit(0);
335114879Sjulian} /* main */
336114879Sjulian
337114879Sjulian/*
338126169Semax * Redirects stdin/stdout to s, stderr to /dev/null and exec
339126169Semax * 'ppp -direct -quiet [-unit N] label'. Never returns.
340114879Sjulian */
341114879Sjulian
342114879Sjulianstatic void
343126169Semaxexec_ppp(int s, char *unit, char *label)
344114879Sjulian{
345114879Sjulian	char	 ppp[] = "/usr/sbin/ppp";
346126169Semax	char	*ppp_args[] = { ppp,  "-direct", "-quiet",
347126169Semax				NULL, NULL,      NULL,     NULL };
348114879Sjulian
349114879Sjulian	close(0);
350114879Sjulian	if (dup(s) < 0) {
351114879Sjulian		syslog(LOG_ERR, "Could not dup(0). %s (%d)",
352114879Sjulian			strerror(errno), errno);
353114879Sjulian		exit(1);
354114879Sjulian	}
355114879Sjulian
356114879Sjulian	close(1);
357114879Sjulian	if (dup(s) < 0) {
358114879Sjulian		syslog(LOG_ERR, "Could not dup(1). %s (%d)",
359114879Sjulian			strerror(errno), errno);
360114879Sjulian		exit(1);
361114879Sjulian	}
362114879Sjulian
363114879Sjulian	close(2);
364114879Sjulian	open("/dev/null", O_RDWR);
365114879Sjulian
366126169Semax	if (unit != NULL) {
367126169Semax		ppp_args[3] = "-unit";
368126169Semax		ppp_args[4] = unit;
369126169Semax		ppp_args[5] = label;
370126169Semax	} else
371126169Semax		ppp_args[3] = label;
372126169Semax
373114879Sjulian	if (execv(ppp, ppp_args) < 0) {
374126169Semax		syslog(LOG_ERR, "Could not exec(%s -direct -quiet%s%s %s). " \
375126169Semax			"%s (%d)", ppp, (unit != NULL)? " -unit " : "",
376126169Semax			(unit != NULL)? unit : "", label,
377126169Semax			strerror(errno), errno);
378114879Sjulian		exit(1);
379114879Sjulian	}
380114879Sjulian} /* run_ppp */
381114879Sjulian
382114879Sjulian/* Signal handler */
383114879Sjulianstatic void
384114879Sjuliansighandler(int s)
385114879Sjulian{
386114879Sjulian	done = 1;
387114879Sjulian} /* sighandler */
388114879Sjulian
389114879Sjulian/* Display usage and exit */
390114879Sjulianstatic void
391114879Sjulianusage(void)
392114879Sjulian{
393114879Sjulian	fprintf(stdout,
394114879Sjulian"Usage: %s options\n" \
395114879Sjulian"Where options are:\n" \
396114879Sjulian"\t-a bdaddr    BDADDR to listen on or connect to (required for client)\n" \
397114879Sjulian"\t-c           Act as a clinet (default)\n" \
398114879Sjulian"\t-C channel   RFCOMM channel to listen on or connect to (required)\n" \
399114879Sjulian"\t-d           Run in foreground\n" \
400114879Sjulian"\t-l label     Use PPP label (required)\n" \
401114879Sjulian"\t-s           Act as a server\n" \
402126169Semax"\t-u N         Tell PPP to operate on /dev/tunN (client mode only)\n" \
403114879Sjulian"\t-h           Display this message\n", RFCOMM_PPPD);
404114879Sjulian
405114879Sjulian	exit(255);
406114879Sjulian} /* usage */
407114879Sjulian
408