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