xref: /netbsd-src/usr.sbin/sdpd/main.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: main.c,v 1.4 2008/07/21 13:36:59 lukem Exp $	*/
2 
3 /*
4  * main.c
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.4 2008/07/21 13:36:59 lukem Exp $
31  * $FreeBSD: src/usr.sbin/bluetooth/sdpd/main.c,v 1.1 2004/01/20 20:48:26 emax Exp $
32  */
33 
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\
36   Copyright (c) 2004 Maksim Yevmenkin m_evmenkin@yahoo.com.\
37   All rights reserved.");
38 __RCSID("$NetBSD: main.c,v 1.4 2008/07/21 13:36:59 lukem Exp $");
39 
40 #include <sys/select.h>
41 #include <bluetooth.h>
42 #include <errno.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #include <signal.h>
46 #include <sdp.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include "log.h"
52 #include "server.h"
53 
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <sys/queue.h>
57 #include "profile.h"
58 #include "provider.h"
59 
60 #define	SDPD			"sdpd"
61 
62 static int32_t	drop_root	(char const *user, char const *group);
63 static void	sighandler	(int32_t s);
64 static void	usage		(void);
65 
66 static int32_t	done;
67 
68 /*
69  * Bluetooth Service Discovery Procotol (SDP) daemon
70  */
71 
72 int
73 main(int argc, char *argv[])
74 {
75 	server_t		 server;
76 	char const		*control = SDP_LOCAL_PATH;
77 	char const		*user = "_sdpd", *group = "_sdpd";
78 	char const		*sgroup = NULL;
79 	int32_t			 detach = 1, opt;
80 	struct sigaction	 sa;
81 
82 	while ((opt = getopt(argc, argv, "c:dG:g:hu:")) != -1) {
83 		switch (opt) {
84 		case 'c': /* control */
85 			control = optarg;
86 			break;
87 
88 		case 'd': /* do not detach */
89 			detach = 0;
90 			break;
91 
92 		case 'G': /* super group */
93 			sgroup = optarg;
94 			break;
95 
96 		case 'g': /* group */
97 			group = optarg;
98 			break;
99 
100 		case 'u': /* user */
101 			user = optarg;
102 			break;
103 
104 		case 'h':
105 		default:
106 			usage();
107 			/* NOT REACHED */
108 		}
109 	}
110 
111 	log_open(SDPD, !detach);
112 
113 	/* Become daemon if required */
114 	if (detach && daemon(0, 0) < 0) {
115 		log_crit("Could not become daemon. %s (%d)",
116 			strerror(errno), errno);
117 		exit(1);
118 	}
119 
120 	/* Set signal handlers */
121 	memset(&sa, 0, sizeof(sa));
122 	sa.sa_handler = sighandler;
123 
124 	if (sigaction(SIGTERM, &sa, NULL) < 0 ||
125 	    sigaction(SIGHUP,  &sa, NULL) < 0 ||
126 	    sigaction(SIGINT,  &sa, NULL) < 0) {
127 		log_crit("Could not install signal handlers. %s (%d)",
128 			strerror(errno), errno);
129 		exit(1);
130 	}
131 
132 	sa.sa_handler = SIG_IGN;
133 	if (sigaction(SIGPIPE, &sa, NULL) < 0) {
134 		log_crit("Could not install signal handlers. %s (%d)",
135 			strerror(errno), errno);
136 		exit(1);
137 	}
138 
139 	/* Initialize server */
140 	if (server_init(&server, control, sgroup) < 0)
141 		exit(1);
142 
143 	if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
144 		exit(1);
145 
146 	for (done = 0; !done; ) {
147 		if (server_do(&server) != 0)
148 			done ++;
149 	}
150 
151 	server_shutdown(&server);
152 	log_close();
153 
154 	return (0);
155 }
156 
157 /*
158  * Drop root
159  */
160 
161 static int32_t
162 drop_root(char const *user, char const *group)
163 {
164 	int	 uid, gid;
165 	char	*ep;
166 
167 	if ((uid = getuid()) != 0) {
168 		log_notice("Cannot set uid/gid. Not a superuser");
169 		return (0); /* dont do anything unless root */
170 	}
171 
172 	gid = getgid();
173 
174 	if (user != NULL) {
175 		uid = strtol(user, &ep, 10);
176 		if (*ep != '\0') {
177 			struct passwd	*pwd = getpwnam(user);
178 
179 			if (pwd == NULL) {
180 				log_err("Could not find passwd entry for " \
181 					"user %s", user);
182 				return (-1);
183 			}
184 
185 			uid = pwd->pw_uid;
186 		}
187 	}
188 
189 	if (group != NULL) {
190 		gid = strtol(group, &ep, 10);
191 		if (*ep != '\0') {
192 			struct group	*grp = getgrnam(group);
193 
194 			if (grp == NULL) {
195 				log_err("Could not find group entry for " \
196 					"group %s", group);
197 				return (-1);
198 			}
199 
200 			gid = grp->gr_gid;
201 		}
202 	}
203 
204 	if (setgid(gid) < 0) {
205 		log_err("Could not setgid(%s). %s (%d)",
206 			group, strerror(errno), errno);
207 		return (-1);
208 	}
209 
210 	if (setuid(uid) < 0) {
211 		log_err("Could not setuid(%s). %s (%d)",
212 			user, strerror(errno), errno);
213 		return (-1);
214 	}
215 
216 	return (0);
217 }
218 
219 /*
220  * Signal handler
221  */
222 
223 static void
224 sighandler(int32_t s)
225 {
226 	log_notice("Got signal %d. Total number of signals received %d",
227 		s, ++ done);
228 }
229 
230 /*
231  * Display usage information and quit
232  */
233 
234 static void
235 usage(void)
236 {
237 	fprintf(stderr,
238 "Usage: %s [options]\n" \
239 "Where options are:\n" \
240 "	-c	specify control socket name (default %s)\n" \
241 "	-d	do not detach (run in foreground)\n" \
242 "	-G grp	allow privileges to group\n" \
243 "	-g grp	specify group\n" \
244 "	-h	display usage and exit\n" \
245 "	-u usr	specify user\n",
246 		SDPD, SDP_LOCAL_PATH);
247 	exit(255);
248 }
249