xref: /freebsd/usr.sbin/bluetooth/sdpd/main.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*-
2  * main.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: main.c,v 1.8 2004/01/13 19:31:54 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/select.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <errno.h>
38 #include <grp.h>
39 #include <pwd.h>
40 #include <signal.h>
41 #include <sdp.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include "log.h"
47 #include "server.h"
48 
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <sys/queue.h>
52 #include "profile.h"
53 #include "provider.h"
54 
55 #define	SDPD			"sdpd"
56 
57 static int32_t 	drop_root	(char const *user, char const *group);
58 static void	sighandler	(int32_t s);
59 static void	usage		(void);
60 
61 static int32_t	done;
62 
63 /*
64  * Bluetooth Service Discovery Procotol (SDP) daemon
65  */
66 
67 int
68 main(int argc, char *argv[])
69 {
70 	server_t		 server;
71 	char const		*control = SDP_LOCAL_PATH;
72 	char const		*user = "nobody", *group = "nobody";
73 	int32_t			 detach = 1, opt;
74 	struct sigaction	 sa;
75 
76 	while ((opt = getopt(argc, argv, "c:dg:hu:")) != -1) {
77 		switch (opt) {
78 		case 'c': /* control */
79 			control = optarg;
80 			break;
81 
82 		case 'd': /* do not detach */
83 			detach = 0;
84 			break;
85 
86 		case 'g': /* group */
87 			group = optarg;
88 			break;
89 
90 		case 'u': /* user */
91 			user = optarg;
92 			break;
93 
94 		case 'h':
95 		default:
96 			usage();
97 			/* NOT REACHED */
98 		}
99 	}
100 
101 	log_open(SDPD, !detach);
102 
103 	/* Become daemon if required */
104 	if (detach && daemon(0, 0) < 0) {
105 		log_crit("Could not become daemon. %s (%d)",
106 			strerror(errno), errno);
107 		exit(1);
108 	}
109 
110 	/* Set signal handlers */
111 	memset(&sa, 0, sizeof(sa));
112 	sa.sa_handler = sighandler;
113 
114 	if (sigaction(SIGTERM, &sa, NULL) < 0 ||
115 	    sigaction(SIGHUP,  &sa, NULL) < 0 ||
116 	    sigaction(SIGINT,  &sa, NULL) < 0) {
117 		log_crit("Could not install signal handlers. %s (%d)",
118 			strerror(errno), errno);
119 		exit(1);
120 	}
121 
122 	sa.sa_handler = SIG_IGN;
123 	if (sigaction(SIGPIPE, &sa, NULL) < 0) {
124 		log_crit("Could not install signal handlers. %s (%d)",
125 			strerror(errno), errno);
126 		exit(1);
127 	}
128 
129 	/* Initialize server */
130 	if (server_init(&server, control) < 0)
131 		exit(1);
132 
133 	if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
134 		exit(1);
135 
136 	for (done = 0; !done; ) {
137 		if (server_do(&server) != 0)
138 			done ++;
139 	}
140 
141 	server_shutdown(&server);
142 	log_close();
143 
144 	return (0);
145 }
146 
147 /*
148  * Drop root
149  */
150 
151 static int32_t
152 drop_root(char const *user, char const *group)
153 {
154 	int	 uid, gid;
155 	char	*ep;
156 
157 	if ((uid = getuid()) != 0) {
158 		log_notice("Cannot set uid/gid. Not a superuser");
159 		return (0); /* dont do anything unless root */
160 	}
161 
162 	gid = getgid();
163 
164 	if (user != NULL) {
165 		uid = strtol(user, &ep, 10);
166 		if (*ep != '\0') {
167 			struct passwd	*pwd = getpwnam(user);
168 
169 			if (pwd == NULL) {
170 				log_err("Could not find passwd entry for " \
171 					"user %s", user);
172 				return (-1);
173 			}
174 
175 			uid = pwd->pw_uid;
176 		}
177 	}
178 
179 	if (group != NULL) {
180 		gid = strtol(group, &ep, 10);
181 		if (*ep != '\0') {
182 			struct group	*grp = getgrnam(group);
183 
184 			if (grp == NULL) {
185 				log_err("Could not find group entry for " \
186 					"group %s", group);
187 				return (-1);
188 			}
189 
190 			gid = grp->gr_gid;
191 		}
192 	}
193 
194 	if (setgid(gid) < 0) {
195 		log_err("Could not setgid(%s). %s (%d)",
196 			group, strerror(errno), errno);
197 		return (-1);
198 	}
199 
200 	if (setuid(uid) < 0) {
201 		log_err("Could not setuid(%s). %s (%d)",
202 			user, strerror(errno), errno);
203 		return (-1);
204 	}
205 
206 	return (0);
207 }
208 
209 /*
210  * Signal handler
211  */
212 
213 static void
214 sighandler(int32_t s)
215 {
216 	log_notice("Got signal %d. Total number of signals received %d",
217 		s, ++ done);
218 }
219 
220 /*
221  * Display usage information and quit
222  */
223 
224 static void
225 usage(void)
226 {
227 	fprintf(stderr,
228 "Usage: %s [options]\n" \
229 "Where options are:\n" \
230 "	-c	specify control socket name (default %s)\n" \
231 "	-d	do not detach (run in foreground)\n" \
232 "	-g grp	specify group\n" \
233 "	-h	display usage and exit\n" \
234 "	-u usr	specify user\n",
235 		SDPD, SDP_LOCAL_PATH);
236 	exit(255);
237 }
238 
239