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