id.c revision 36285
174667Sjedgar/*-
290781Sjedgar * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
374667Sjedgar * All rights reserved.
474667Sjedgar *
574667Sjedgar * Redistribution and use in source and binary forms, with or without
674667Sjedgar * modification, are permitted provided that the following conditions
774667Sjedgar * are met:
874667Sjedgar * 1. Redistributions of source code must retain the above copyright
974667Sjedgar *    notice, this list of conditions and the following disclaimer.
1074667Sjedgar * 2. Redistributions in binary form must reproduce the above copyright
1174667Sjedgar *    notice, this list of conditions and the following disclaimer in the
1274667Sjedgar *    documentation and/or other materials provided with the distribution.
1374667Sjedgar *
1474667Sjedgar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1574667Sjedgar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1674667Sjedgar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1774667Sjedgar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1874667Sjedgar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1974667Sjedgar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2074667Sjedgar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2174667Sjedgar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2274667Sjedgar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2374667Sjedgar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2474667Sjedgar * SUCH DAMAGE.
2574667Sjedgar *
2674667Sjedgar *	$Id: id.c,v 1.6.4.10 1998/05/01 19:24:41 brian Exp $
2792986Sobrien */
2892986Sobrien
2992986Sobrien#include <sys/types.h>
3074667Sjedgar#include <sys/socket.h>
3175185Stmm#include <sys/un.h>
3274667Sjedgar
3375185Stmm#include <sys/ioctl.h>
3474667Sjedgar#include <fcntl.h>
3574667Sjedgar#include <stdarg.h>
3674667Sjedgar#include <stdio.h>
3774667Sjedgar#include <string.h>
3874667Sjedgar#include <sysexits.h>
3974667Sjedgar#include <unistd.h>
4074667Sjedgar#ifdef __OpenBSD__
4174667Sjedgar#include <util.h>
4290781Sjedgar#else
4374667Sjedgar#include <libutil.h>
44#endif
45#include <utmp.h>
46
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    log_Printf(LogERROR, "ID0setuser: Unable to seteuid!\n");
66    AbortProgram(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    log_Printf(LogERROR, "ID0set0: Unable to seteuid!\n");
81    AbortProgram(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  log_Printf(LogID0, "%d = ioctl(%d, %lu, %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  log_Printf(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  log_Printf(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  log_Printf(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  va_list ap;
138
139  va_start(ap, flags);
140  ID0set0();
141  ret = open(path, flags, va_arg(ap, int));
142  log_Printf(LogID0, "%d = open(\"%s\", %d)\n", ret, path, flags);
143  ID0setuser();
144  va_end(ap);
145  return ret;
146}
147
148int
149ID0write(int fd, const void *data, size_t len)
150{
151  int ret;
152
153  ID0set0();
154  ret = write(fd, data, len);
155  log_Printf(LogID0, "%d = write(%d, data, %d)\n", ret, fd, len);
156  ID0setuser();
157  return ret;
158}
159
160int
161ID0uu_lock(const char *basettyname)
162{
163  int ret;
164
165  ID0set0();
166  ret = uu_lock(basettyname);
167  log_Printf(LogID0, "%d = uu_lock(\"%s\")\n", ret, basettyname);
168  ID0setuser();
169  return ret;
170}
171
172int
173ID0uu_unlock(const char *basettyname)
174{
175  int ret;
176
177  ID0set0();
178  ret = uu_unlock(basettyname);
179  log_Printf(LogID0, "%d = uu_unlock(\"%s\")\n", ret, basettyname);
180  ID0setuser();
181  return ret;
182}
183
184void
185ID0login(struct utmp *ut)
186{
187  ID0set0();
188  if (logout(ut->ut_line)) {
189    log_Printf(LogID0, "logout(\"%s\")\n", ut->ut_line);
190    logwtmp(ut->ut_line, "", "");
191    log_Printf(LogID0, "logwtmp(\"%s\", \"\", \"\")\n", ut->ut_line);
192  }
193  login(ut);
194  log_Printf(LogID0, "login(\"%s\", \"%.*s\")\n",
195            ut->ut_line, (int)(sizeof ut->ut_name), ut->ut_name);
196  ID0setuser();
197}
198
199void
200ID0logout(const char *device)
201{
202  struct utmp ut;
203
204  strncpy(ut.ut_line, device, sizeof ut.ut_line - 1);
205  ut.ut_line[sizeof ut.ut_line - 1] = '\0';
206
207  ID0set0();
208  if (logout(ut.ut_line)) {
209    log_Printf(LogID0, "logout(\"%s\")\n", ut.ut_line);
210    logwtmp(ut.ut_line, "", "");
211    log_Printf(LogID0, "logwtmp(\"%s\", \"\", \"\")\n", ut.ut_line);
212  } else
213    log_Printf(LogERROR, "ID0logout: No longer logged in on %s\n", ut.ut_line);
214  ID0setuser();
215}
216
217int
218ID0bind_un(int s, const struct sockaddr_un *name)
219{
220  int result;
221
222  ID0set0();
223  result = bind(s, (const struct sockaddr *)name, sizeof *name);
224  log_Printf(LogID0, "%d = bind(%d, \"%s\", %d)\n",
225            result, s, name->sun_path, sizeof *name);
226  ID0setuser();
227  return result;
228}
229
230int
231ID0connect_un(int s, const struct sockaddr_un *name)
232{
233  int result;
234
235  ID0set0();
236  result = connect(s, (const struct sockaddr *)name, sizeof *name);
237  log_Printf(LogID0, "%d = connect(%d, \"%s\", %d)\n",
238            result, s, name->sun_path, sizeof *name);
239  ID0setuser();
240  return result;
241}
242