dma: note that we will have to clean up SSL state
[dragonfly.git] / usr.sbin / sdpd / main.c
blob50da99a7160c6af12cb69762f9e5fb51479af29d
1 /* $NetBSD: main.c,v 1.3 2007/03/18 15:53:55 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/sdpd/main.c,v 1.1 2008/01/06 21:51:30 hasso Exp $ */
4 /*
5 * main.c
7 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
31 * $Id: main.c,v 1.2 2007/11/30 07:39:37 griffin Exp $
32 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/main.c,v 1.1 2004/01/20 20:48:26 emax Exp $
35 #include <sys/select.h>
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"
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <sys/queue.h>
52 #include "profile.h"
53 #include "provider.h"
55 #define SDPD "sdpd"
57 static int32_t drop_root (char const *user, char const *group);
58 static void sighandler (int32_t s);
59 static void usage (void);
61 static int32_t done;
64 * Bluetooth Service Discovery Procotol (SDP) daemon
67 int
68 main(int argc, char *argv[])
70 server_t server;
71 char const *control = SDP_LOCAL_PATH;
72 char const *user = "_sdpd", *group = "_sdpd";
73 char const *sgroup = NULL;
74 int32_t detach = 1, opt;
75 struct sigaction sa;
77 while ((opt = getopt(argc, argv, "c:dG:g:hu:")) != -1) {
78 switch (opt) {
79 case 'c': /* control */
80 control = optarg;
81 break;
83 case 'd': /* do not detach */
84 detach = 0;
85 break;
87 case 'G': /* super group */
88 sgroup = optarg;
89 break;
91 case 'g': /* group */
92 group = optarg;
93 break;
95 case 'u': /* user */
96 user = optarg;
97 break;
99 case 'h':
100 default:
101 usage();
102 /* NOT REACHED */
106 log_open(SDPD, !detach);
108 /* Become daemon if required */
109 if (detach && daemon(0, 0) < 0) {
110 log_crit("Could not become daemon. %s (%d)",
111 strerror(errno), errno);
112 exit(1);
115 /* Set signal handlers */
116 memset(&sa, 0, sizeof(sa));
117 sa.sa_handler = sighandler;
119 if (sigaction(SIGTERM, &sa, NULL) < 0 ||
120 sigaction(SIGHUP, &sa, NULL) < 0 ||
121 sigaction(SIGINT, &sa, NULL) < 0) {
122 log_crit("Could not install signal handlers. %s (%d)",
123 strerror(errno), errno);
124 exit(1);
127 sa.sa_handler = SIG_IGN;
128 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
129 log_crit("Could not install signal handlers. %s (%d)",
130 strerror(errno), errno);
131 exit(1);
134 /* Initialize server */
135 if (server_init(&server, control, sgroup) < 0)
136 exit(1);
138 if ((user != NULL || group != NULL) && drop_root(user, group) < 0)
139 exit(1);
141 for (done = 0; !done; ) {
142 if (server_do(&server) != 0)
143 done ++;
146 server_shutdown(&server);
147 log_close();
149 return (0);
153 * Drop root
156 static int32_t
157 drop_root(char const *user, char const *group)
159 int uid, gid;
160 char *ep;
162 if ((uid = getuid()) != 0) {
163 log_notice("Cannot set uid/gid. Not a superuser");
164 return (0); /* dont do anything unless root */
167 gid = getgid();
169 if (user != NULL) {
170 uid = strtol(user, &ep, 10);
171 if (*ep != '\0') {
172 struct passwd *pwd = getpwnam(user);
174 if (pwd == NULL) {
175 log_err("Could not find passwd entry for " \
176 "user %s", user);
177 return (-1);
180 uid = pwd->pw_uid;
184 if (group != NULL) {
185 gid = strtol(group, &ep, 10);
186 if (*ep != '\0') {
187 struct group *grp = getgrnam(group);
189 if (grp == NULL) {
190 log_err("Could not find group entry for " \
191 "group %s", group);
192 return (-1);
195 gid = grp->gr_gid;
199 if (setgid(gid) < 0) {
200 log_err("Could not setgid(%s). %s (%d)",
201 group, strerror(errno), errno);
202 return (-1);
205 if (setuid(uid) < 0) {
206 log_err("Could not setuid(%s). %s (%d)",
207 user, strerror(errno), errno);
208 return (-1);
211 return (0);
215 * Signal handler
218 static void
219 sighandler(int32_t s)
221 log_notice("Got signal %d. Total number of signals received %d",
222 s, ++ done);
226 * Display usage information and quit
229 static void
230 usage(void)
232 fprintf(stderr,
233 "Usage: %s [options]\n" \
234 "Where options are:\n" \
235 " -c specify control socket name (default %s)\n" \
236 " -d do not detach (run in foreground)\n" \
237 " -G grp allow privileges to group\n" \
238 " -g grp specify group\n" \
239 " -h display usage and exit\n" \
240 " -u usr specify user\n",
241 SDPD, SDP_LOCAL_PATH);
242 exit(255);