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