id.c revision 33603
1/*- 2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: id.c,v 1.6 1998/01/21 02:15:16 brian Exp $ 27 */ 28 29#include <sys/types.h> 30#include <sys/socket.h> 31 32#include <sys/ioctl.h> 33#include <fcntl.h> 34#include <stdio.h> 35#include <string.h> 36#include <sysexits.h> 37#include <unistd.h> 38#ifdef __OpenBSD__ 39#include <util.h> 40#else 41#include <libutil.h> 42#endif 43#include <utmp.h> 44 45#include "command.h" 46#include "mbuf.h" 47#include "log.h" 48#include "main.h" 49#include "id.h" 50 51static int uid; 52static int euid; 53 54void 55ID0init() 56{ 57 uid = getuid(); 58 euid = geteuid(); 59} 60 61static void 62ID0setuser(void) 63{ 64 if (seteuid(uid) == -1) { 65 LogPrintf(LogERROR, "ID0setuser: Unable to seteuid!\n"); 66 Cleanup(EX_NOPERM); 67 } 68} 69 70uid_t 71ID0realuid() 72{ 73 return uid; 74} 75 76static void 77ID0set0(void) 78{ 79 if (seteuid(euid) == -1) { 80 LogPrintf(LogERROR, "ID0set0: Unable to seteuid!\n"); 81 Cleanup(EX_NOPERM); 82 } 83} 84 85int 86ID0ioctl(int fd, unsigned long req, void *arg) 87{ 88 int ret; 89 90 ID0set0(); 91 ret = ioctl(fd, req, arg); 92 LogPrintf(LogID0, "%d = ioctl(%d, %d, %p)\n", ret, fd, req, arg); 93 ID0setuser(); 94 return ret; 95} 96 97int 98ID0unlink(const char *name) 99{ 100 int ret; 101 102 ID0set0(); 103 ret = unlink(name); 104 LogPrintf(LogID0, "%d = unlink(\"%s\")\n", ret, name); 105 ID0setuser(); 106 return ret; 107} 108 109int 110ID0socket(int domain, int type, int protocol) 111{ 112 int ret; 113 114 ID0set0(); 115 ret = socket(domain, type, protocol); 116 LogPrintf(LogID0, "%d = socket(%d, %d, %d)\n", ret, domain, type, protocol); 117 ID0setuser(); 118 return ret; 119} 120 121FILE * 122ID0fopen(const char *path, const char *mode) 123{ 124 FILE *ret; 125 126 ID0set0(); 127 ret = fopen(path, mode); 128 LogPrintf(LogID0, "%p = fopen(\"%s\", \"%s\")\n", ret, path, mode); 129 ID0setuser(); 130 return ret; 131} 132 133int 134ID0open(const char *path, int flags) 135{ 136 int ret; 137 138 ID0set0(); 139 ret = open(path, flags); 140 LogPrintf(LogID0, "%d = open(\"%s\", %d)\n", ret, path, flags); 141 ID0setuser(); 142 return ret; 143} 144 145int 146ID0write(int fd, const void *data, size_t len) 147{ 148 int ret; 149 150 ID0set0(); 151 ret = write(fd, data, len); 152 LogPrintf(LogID0, "%d = write(%d, data, %d)\n", ret, fd, len); 153 ID0setuser(); 154 return ret; 155} 156 157int 158ID0uu_lock(const char *basettyname) 159{ 160 int ret; 161 162 ID0set0(); 163 ret = uu_lock(basettyname); 164 LogPrintf(LogID0, "%d = uu_lock(\"%s\")\n", ret, basettyname); 165 ID0setuser(); 166 return ret; 167} 168 169int 170ID0uu_unlock(const char *basettyname) 171{ 172 int ret; 173 174 ID0set0(); 175 ret = uu_unlock(basettyname); 176 LogPrintf(LogID0, "%d = uu_unlock(\"%s\")\n", ret, basettyname); 177 ID0setuser(); 178 return ret; 179} 180 181void 182ID0login(struct utmp *ut) 183{ 184 ID0set0(); 185 if (logout(ut->ut_line)) { 186 LogPrintf(LogID0, "logout(\"%s\")\n", ut->ut_line); 187 logwtmp(ut->ut_line, "", ""); 188 LogPrintf(LogID0, "logwtmp(\"%s\", \"\", \"\")\n", ut->ut_line); 189 } 190 login(ut); 191 LogPrintf(LogID0, "login(\"%s\", \"%.*s\")\n", 192 ut->ut_line, sizeof ut->ut_name, ut->ut_name); 193 ID0setuser(); 194} 195 196void 197ID0logout(const char *device) 198{ 199 struct utmp ut; 200 201 strncpy(ut.ut_line, device, sizeof ut.ut_line - 1); 202 ut.ut_line[sizeof ut.ut_line - 1] = '\0'; 203 204 ID0set0(); 205 if (logout(ut.ut_line)) { 206 LogPrintf(LogID0, "logout(\"%s\")\n", ut.ut_line); 207 logwtmp(ut.ut_line, "", ""); 208 LogPrintf(LogID0, "logwtmp(\"%s\", \"\", \"\")\n", ut.ut_line); 209 } else 210 LogPrintf(LogERROR, "ID0logout: No longer logged in on %s\n", ut.ut_line); 211 ID0setuser(); 212} 213