1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 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 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/socket.h> 33 #include <sys/un.h> 34 35 #include <sys/ioctl.h> 36 #include <fcntl.h> 37 #ifndef NONETGRAPH 38 #include <netgraph.h> 39 #endif 40 #include <signal.h> 41 #include <stdarg.h> 42 #include <stdio.h> 43 #include <string.h> 44 #include <sysexits.h> 45 #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 46 #include <sys/linker.h> 47 #endif 48 #include <unistd.h> 49 #ifdef __OpenBSD__ 50 #include <util.h> 51 #else 52 #include <libutil.h> 53 #endif 54 #include <utmpx.h> 55 56 #include "log.h" 57 #include "main.h" 58 #include "id.h" 59 60 static int uid; 61 static int euid; 62 63 void 64 ID0init(void) 65 { 66 uid = getuid(); 67 euid = geteuid(); 68 } 69 70 static void 71 ID0setuser(void) 72 { 73 if (seteuid(uid) == -1) { 74 log_Printf(LogERROR, "ID0setuser: Unable to seteuid!\n"); 75 AbortProgram(EX_NOPERM); 76 } 77 } 78 79 uid_t 80 ID0realuid(void) 81 { 82 return uid; 83 } 84 85 static void 86 ID0set0(void) 87 { 88 if (seteuid(euid) == -1) { 89 log_Printf(LogERROR, "ID0set0: Unable to seteuid!\n"); 90 AbortProgram(EX_NOPERM); 91 } 92 } 93 94 int 95 ID0ioctl(int fd, unsigned long req, void *arg) 96 { 97 int ret; 98 99 ID0set0(); 100 ret = ioctl(fd, req, arg); 101 log_Printf(LogID0, "%d = ioctl(%d, %lu, %p)\n", ret, fd, req, arg); 102 ID0setuser(); 103 return ret; 104 } 105 106 int 107 ID0unlink(const char *name) 108 { 109 int ret; 110 111 ID0set0(); 112 ret = unlink(name); 113 log_Printf(LogID0, "%d = unlink(\"%s\")\n", ret, name); 114 ID0setuser(); 115 return ret; 116 } 117 118 int 119 ID0socket(int domain, int type, int protocol) 120 { 121 int ret; 122 123 ID0set0(); 124 ret = socket(domain, type, protocol); 125 log_Printf(LogID0, "%d = socket(%d, %d, %d)\n", ret, domain, type, protocol); 126 ID0setuser(); 127 return ret; 128 } 129 130 FILE * 131 ID0fopen(const char *path, const char *mode) 132 { 133 FILE *ret; 134 135 ID0set0(); 136 ret = fopen(path, mode); 137 log_Printf(LogID0, "%p = fopen(\"%s\", \"%s\")\n", ret, path, mode); 138 ID0setuser(); 139 return ret; 140 } 141 142 int 143 ID0open(const char *path, int flags, ...) 144 { 145 int ret; 146 va_list ap; 147 148 va_start(ap, flags); 149 ID0set0(); 150 ret = open(path, flags, va_arg(ap, int)); 151 log_Printf(LogID0, "%d = open(\"%s\", %d)\n", ret, path, flags); 152 ID0setuser(); 153 va_end(ap); 154 return ret; 155 } 156 157 int 158 ID0write(int fd, const void *data, size_t len) 159 { 160 int ret; 161 162 ID0set0(); 163 ret = write(fd, data, len); 164 log_Printf(LogID0, "%d = write(%d, data, %ld)\n", ret, fd, (long)len); 165 ID0setuser(); 166 return ret; 167 } 168 169 int 170 ID0uu_lock(const char *basettyname) 171 { 172 int ret; 173 174 ID0set0(); 175 ret = uu_lock(basettyname); 176 log_Printf(LogID0, "%d = uu_lock(\"%s\")\n", ret, basettyname); 177 ID0setuser(); 178 return ret; 179 } 180 181 int 182 ID0uu_lock_txfr(const char *basettyname, pid_t newpid) 183 { 184 int ret; 185 186 ID0set0(); 187 ret = uu_lock_txfr(basettyname, newpid); 188 log_Printf(LogID0, "%d = uu_lock_txfr(\"%s\", %ld)\n", ret, basettyname, 189 (long)newpid); 190 ID0setuser(); 191 return ret; 192 } 193 194 int 195 ID0uu_unlock(const char *basettyname) 196 { 197 int ret; 198 199 ID0set0(); 200 ret = uu_unlock(basettyname); 201 log_Printf(LogID0, "%d = uu_unlock(\"%s\")\n", ret, basettyname); 202 ID0setuser(); 203 return ret; 204 } 205 206 void 207 ID0login(const struct utmpx *ut) 208 { 209 ID0set0(); 210 pututxline(ut); 211 log_Printf(LogID0, "pututxline(\"%.*s\", \"%.*s\", \"%.*s\", \"%.*s\")\n", 212 (int)sizeof ut->ut_id, ut->ut_id, 213 (int)sizeof ut->ut_user, ut->ut_user, 214 (int)sizeof ut->ut_line, ut->ut_line, 215 (int)sizeof ut->ut_host, ut->ut_host); 216 ID0setuser(); 217 } 218 219 void 220 ID0logout(const struct utmpx *ut) 221 { 222 ID0set0(); 223 pututxline(ut); 224 log_Printf(LogID0, "pututxline(\"%.*s\")\n", 225 (int)sizeof ut->ut_id, ut->ut_id); 226 ID0setuser(); 227 } 228 229 int 230 ID0bind_un(int s, const struct sockaddr_un *name) 231 { 232 int result; 233 234 ID0set0(); 235 result = bind(s, (const struct sockaddr *)name, sizeof *name); 236 log_Printf(LogID0, "%d = bind(%d, \"%s\", %d)\n", 237 result, s, name->sun_path, (int)sizeof(*name)); 238 ID0setuser(); 239 return result; 240 } 241 242 int 243 ID0connect_un(int s, const struct sockaddr_un *name) 244 { 245 int result; 246 247 ID0set0(); 248 result = connect(s, (const struct sockaddr *)name, sizeof *name); 249 log_Printf(LogID0, "%d = connect(%d, \"%s\", %d)\n", 250 result, s, name->sun_path, (int)sizeof(*name)); 251 ID0setuser(); 252 return result; 253 } 254 255 int 256 ID0kill(pid_t pid, int sig) 257 { 258 int result; 259 260 ID0set0(); 261 result = kill(pid, sig); 262 log_Printf(LogID0, "%d = kill(%ld, %d)\n", result, (long)pid, sig); 263 ID0setuser(); 264 return result; 265 } 266 267 #if defined(__FreeBSD__) && !defined(NOKLDLOAD) 268 int 269 ID0kldload(const char *dev) 270 { 271 int result; 272 273 ID0set0(); 274 result = kldload(dev); 275 log_Printf(LogID0, "%d = kldload(\"%s\")\n", result, dev); 276 ID0setuser(); 277 return result; 278 } 279 #endif 280 281 #ifndef NONETGRAPH 282 int 283 ID0NgMkSockNode(const char *name, int *cs, int *ds) 284 { 285 int result; 286 287 ID0set0(); 288 result = NgMkSockNode(name, cs, ds); 289 log_Printf(LogID0, "%d = NgMkSockNode(\"%s\", &cs, &ds)\n", 290 result, name ? name : ""); 291 ID0setuser(); 292 return result; 293 } 294 #endif 295