main.c revision 1.2 1 1.2 plunky /* $NetBSD: main.c,v 1.2 2007/03/18 10:00:42 plunky 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.2 plunky * $Id: main.c,v 1.2 2007/03/18 10:00:42 plunky 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.2 plunky __RCSID("$NetBSD: main.c,v 1.2 2007/03/18 10:00:42 plunky 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.2 plunky char const *sgroup = NULL;
79 1.1 gdamore int32_t detach = 1, opt;
80 1.1 gdamore struct sigaction sa;
81 1.1 gdamore
82 1.2 plunky while ((opt = getopt(argc, argv, "c:dG:g:hu:")) != -1) {
83 1.1 gdamore switch (opt) {
84 1.1 gdamore case 'c': /* control */
85 1.1 gdamore control = optarg;
86 1.1 gdamore break;
87 1.1 gdamore
88 1.1 gdamore case 'd': /* do not detach */
89 1.1 gdamore detach = 0;
90 1.1 gdamore break;
91 1.1 gdamore
92 1.2 plunky case 'G': /* super group */
93 1.2 plunky sgroup = optarg;
94 1.2 plunky break;
95 1.2 plunky
96 1.1 gdamore case 'g': /* group */
97 1.1 gdamore group = optarg;
98 1.1 gdamore break;
99 1.1 gdamore
100 1.1 gdamore case 'u': /* user */
101 1.1 gdamore user = optarg;
102 1.1 gdamore break;
103 1.1 gdamore
104 1.1 gdamore case 'h':
105 1.1 gdamore default:
106 1.1 gdamore usage();
107 1.1 gdamore /* NOT REACHED */
108 1.1 gdamore }
109 1.1 gdamore }
110 1.1 gdamore
111 1.1 gdamore log_open(SDPD, !detach);
112 1.1 gdamore
113 1.1 gdamore /* Become daemon if required */
114 1.1 gdamore if (detach && daemon(0, 0) < 0) {
115 1.1 gdamore log_crit("Could not become daemon. %s (%d)",
116 1.1 gdamore strerror(errno), errno);
117 1.1 gdamore exit(1);
118 1.1 gdamore }
119 1.1 gdamore
120 1.1 gdamore /* Set signal handlers */
121 1.1 gdamore memset(&sa, 0, sizeof(sa));
122 1.1 gdamore sa.sa_handler = sighandler;
123 1.1 gdamore
124 1.1 gdamore if (sigaction(SIGTERM, &sa, NULL) < 0 ||
125 1.1 gdamore sigaction(SIGHUP, &sa, NULL) < 0 ||
126 1.1 gdamore sigaction(SIGINT, &sa, NULL) < 0) {
127 1.1 gdamore log_crit("Could not install signal handlers. %s (%d)",
128 1.1 gdamore strerror(errno), errno);
129 1.1 gdamore exit(1);
130 1.1 gdamore }
131 1.1 gdamore
132 1.1 gdamore sa.sa_handler = SIG_IGN;
133 1.1 gdamore if (sigaction(SIGPIPE, &sa, NULL) < 0) {
134 1.1 gdamore log_crit("Could not install signal handlers. %s (%d)",
135 1.1 gdamore strerror(errno), errno);
136 1.1 gdamore exit(1);
137 1.1 gdamore }
138 1.1 gdamore
139 1.1 gdamore /* Initialize server */
140 1.2 plunky if (server_init(&server, control, sgroup) < 0)
141 1.1 gdamore exit(1);
142 1.1 gdamore
143 1.1 gdamore if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
144 1.1 gdamore exit(1);
145 1.1 gdamore
146 1.1 gdamore for (done = 0; !done; ) {
147 1.1 gdamore if (server_do(&server) != 0)
148 1.1 gdamore done ++;
149 1.1 gdamore }
150 1.1 gdamore
151 1.1 gdamore server_shutdown(&server);
152 1.1 gdamore log_close();
153 1.1 gdamore
154 1.1 gdamore return (0);
155 1.1 gdamore }
156 1.1 gdamore
157 1.1 gdamore /*
158 1.1 gdamore * Drop root
159 1.1 gdamore */
160 1.1 gdamore
161 1.1 gdamore static int32_t
162 1.1 gdamore drop_root(char const *user, char const *group)
163 1.1 gdamore {
164 1.1 gdamore int uid, gid;
165 1.1 gdamore char *ep;
166 1.1 gdamore
167 1.1 gdamore if ((uid = getuid()) != 0) {
168 1.1 gdamore log_notice("Cannot set uid/gid. Not a superuser");
169 1.1 gdamore return (0); /* dont do anything unless root */
170 1.1 gdamore }
171 1.1 gdamore
172 1.1 gdamore gid = getgid();
173 1.1 gdamore
174 1.1 gdamore if (user != NULL) {
175 1.1 gdamore uid = strtol(user, &ep, 10);
176 1.1 gdamore if (*ep != '\0') {
177 1.1 gdamore struct passwd *pwd = getpwnam(user);
178 1.1 gdamore
179 1.1 gdamore if (pwd == NULL) {
180 1.1 gdamore log_err("Could not find passwd entry for " \
181 1.1 gdamore "user %s", user);
182 1.1 gdamore return (-1);
183 1.1 gdamore }
184 1.1 gdamore
185 1.1 gdamore uid = pwd->pw_uid;
186 1.1 gdamore }
187 1.1 gdamore }
188 1.1 gdamore
189 1.1 gdamore if (group != NULL) {
190 1.1 gdamore gid = strtol(group, &ep, 10);
191 1.1 gdamore if (*ep != '\0') {
192 1.1 gdamore struct group *grp = getgrnam(group);
193 1.1 gdamore
194 1.1 gdamore if (grp == NULL) {
195 1.1 gdamore log_err("Could not find group entry for " \
196 1.1 gdamore "group %s", group);
197 1.1 gdamore return (-1);
198 1.1 gdamore }
199 1.1 gdamore
200 1.1 gdamore gid = grp->gr_gid;
201 1.1 gdamore }
202 1.1 gdamore }
203 1.1 gdamore
204 1.1 gdamore if (setgid(gid) < 0) {
205 1.1 gdamore log_err("Could not setgid(%s). %s (%d)",
206 1.1 gdamore group, strerror(errno), errno);
207 1.1 gdamore return (-1);
208 1.1 gdamore }
209 1.1 gdamore
210 1.1 gdamore if (setuid(uid) < 0) {
211 1.1 gdamore log_err("Could not setuid(%s). %s (%d)",
212 1.1 gdamore user, strerror(errno), errno);
213 1.1 gdamore return (-1);
214 1.1 gdamore }
215 1.1 gdamore
216 1.1 gdamore return (0);
217 1.1 gdamore }
218 1.1 gdamore
219 1.1 gdamore /*
220 1.1 gdamore * Signal handler
221 1.1 gdamore */
222 1.1 gdamore
223 1.1 gdamore static void
224 1.1 gdamore sighandler(int32_t s)
225 1.1 gdamore {
226 1.1 gdamore log_notice("Got signal %d. Total number of signals received %d",
227 1.1 gdamore s, ++ done);
228 1.1 gdamore }
229 1.1 gdamore
230 1.1 gdamore /*
231 1.1 gdamore * Display usage information and quit
232 1.1 gdamore */
233 1.1 gdamore
234 1.1 gdamore static void
235 1.1 gdamore usage(void)
236 1.1 gdamore {
237 1.1 gdamore fprintf(stderr,
238 1.1 gdamore "Usage: %s [options]\n" \
239 1.1 gdamore "Where options are:\n" \
240 1.1 gdamore " -c specify control socket name (default %s)\n" \
241 1.1 gdamore " -d do not detach (run in foreground)\n" \
242 1.2 plunky " -G grp allow privileges to group\n" \
243 1.1 gdamore " -g grp specify group\n" \
244 1.1 gdamore " -h display usage and exit\n" \
245 1.1 gdamore " -u usr specify user\n",
246 1.1 gdamore SDPD, SDP_LOCAL_PATH);
247 1.1 gdamore exit(255);
248 1.1 gdamore }
249