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