1 /* $NetBSD: main.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $ */ 2 3 /* 4 * main.c 5 * 6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 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.1 2006/06/19 15:44:56 gdamore Exp $ 31 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/main.c,v 1.1 2004/01/20 20:48:26 emax Exp $ 32 */ 33 34 #include <sys/cdefs.h> 35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n" 36 "@(#) Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>\n" 37 "All rights reserved.\n"); 38 __RCSID("$NetBSD: main.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $"); 39 40 #include <sys/select.h> 41 #include <bluetooth.h> 42 #include <errno.h> 43 #include <grp.h> 44 #include <pwd.h> 45 #include <signal.h> 46 #include <sdp.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include "log.h" 52 #include "server.h" 53 54 #include <netinet/in.h> 55 #include <arpa/inet.h> 56 #include <sys/queue.h> 57 #include "profile.h" 58 #include "provider.h" 59 60 #define SDPD "sdpd" 61 62 static int32_t drop_root (char const *user, char const *group); 63 static void sighandler (int32_t s); 64 static void usage (void); 65 66 static int32_t done; 67 68 /* 69 * Bluetooth Service Discovery Procotol (SDP) daemon 70 */ 71 72 int 73 main(int argc, char *argv[]) 74 { 75 server_t server; 76 char const *control = SDP_LOCAL_PATH; 77 char const *user = "nobody", *group = "nobody"; 78 int32_t detach = 1, opt; 79 struct sigaction sa; 80 81 while ((opt = getopt(argc, argv, "c:dg:hu:")) != -1) { 82 switch (opt) { 83 case 'c': /* control */ 84 control = optarg; 85 break; 86 87 case 'd': /* do not detach */ 88 detach = 0; 89 break; 90 91 case 'g': /* group */ 92 group = optarg; 93 break; 94 95 case 'u': /* user */ 96 user = optarg; 97 break; 98 99 case 'h': 100 default: 101 usage(); 102 /* NOT REACHED */ 103 } 104 } 105 106 log_open(SDPD, !detach); 107 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); 113 } 114 115 /* Set signal handlers */ 116 memset(&sa, 0, sizeof(sa)); 117 sa.sa_handler = sighandler; 118 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); 125 } 126 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); 132 } 133 134 /* Initialize server */ 135 if (server_init(&server, control) < 0) 136 exit(1); 137 138 if ((user != NULL || group != NULL) && drop_root(user, group) < 0) 139 exit(1); 140 141 for (done = 0; !done; ) { 142 if (server_do(&server) != 0) 143 done ++; 144 } 145 146 server_shutdown(&server); 147 log_close(); 148 149 return (0); 150 } 151 152 /* 153 * Drop root 154 */ 155 156 static int32_t 157 drop_root(char const *user, char const *group) 158 { 159 int uid, gid; 160 char *ep; 161 162 if ((uid = getuid()) != 0) { 163 log_notice("Cannot set uid/gid. Not a superuser"); 164 return (0); /* 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("Could not find passwd entry for " \ 176 "user %s", user); 177 return (-1); 178 } 179 180 uid = pwd->pw_uid; 181 } 182 } 183 184 if (group != NULL) { 185 gid = strtol(group, &ep, 10); 186 if (*ep != '\0') { 187 struct group *grp = getgrnam(group); 188 189 if (grp == NULL) { 190 log_err("Could not find group entry for " \ 191 "group %s", group); 192 return (-1); 193 } 194 195 gid = grp->gr_gid; 196 } 197 } 198 199 if (setgid(gid) < 0) { 200 log_err("Could not setgid(%s). %s (%d)", 201 group, strerror(errno), errno); 202 return (-1); 203 } 204 205 if (setuid(uid) < 0) { 206 log_err("Could not setuid(%s). %s (%d)", 207 user, strerror(errno), errno); 208 return (-1); 209 } 210 211 return (0); 212 } 213 214 /* 215 * Signal handler 216 */ 217 218 static void 219 sighandler(int32_t s) 220 { 221 log_notice("Got signal %d. Total number of signals received %d", 222 s, ++ done); 223 } 224 225 /* 226 * Display usage information and quit 227 */ 228 229 static void 230 usage(void) 231 { 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 specify group\n" \ 238 " -h display usage and exit\n" \ 239 " -u usr specify user\n", 240 SDPD, SDP_LOCAL_PATH); 241 exit(255); 242 } 243