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