• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: src/usr.sbin/ppp/id.c,v 1.23.40.1 2010/12/21 17:10:29 kensmith Exp $
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 <string.h>
42 #include <sysexits.h>
43 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
44 #include <sys/linker.h>
45 #endif
46 #include <unistd.h>
47 #ifdef __OpenBSD__
48 #include <util.h>
49 #else
50 #include <libutil.h>
51 #endif
52 #include <utmp.h>
53 
54 #include "log.h"
55 #include "main.h"
56 #include "id.h"
57 
58 static int uid;
59 static int euid;
60 
61 void
ID0init()62 ID0init()
63 {
64   uid = getuid();
65   euid = geteuid();
66 }
67 
68 static void
ID0setuser(void)69 ID0setuser(void)
70 {
71   if (seteuid(uid) == -1) {
72     log_Printf(LogERROR, "ID0setuser: Unable to seteuid!\n");
73     AbortProgram(EX_NOPERM);
74   }
75 }
76 
77 uid_t
ID0realuid()78 ID0realuid()
79 {
80   return uid;
81 }
82 
83 static void
ID0set0(void)84 ID0set0(void)
85 {
86   if (seteuid(euid) == -1) {
87     log_Printf(LogERROR, "ID0set0: Unable to seteuid!\n");
88     AbortProgram(EX_NOPERM);
89   }
90 }
91 
92 int
ID0ioctl(int fd,unsigned long req,void * arg)93 ID0ioctl(int fd, unsigned long req, void *arg)
94 {
95   int ret;
96 
97   ID0set0();
98   ret = ioctl(fd, req, arg);
99   log_Printf(LogID0, "%d = ioctl(%d, %lu, %p)\n", ret, fd, req, arg);
100   ID0setuser();
101   return ret;
102 }
103 
104 int
ID0unlink(const char * name)105 ID0unlink(const char *name)
106 {
107   int ret;
108 
109   ID0set0();
110   ret = unlink(name);
111   log_Printf(LogID0, "%d = unlink(\"%s\")\n", ret, name);
112   ID0setuser();
113   return ret;
114 }
115 
116 int
ID0socket(int domain,int type,int protocol)117 ID0socket(int domain, int type, int protocol)
118 {
119   int ret;
120 
121   ID0set0();
122   ret = socket(domain, type, protocol);
123   log_Printf(LogID0, "%d = socket(%d, %d, %d)\n", ret, domain, type, protocol);
124   ID0setuser();
125   return ret;
126 }
127 
128 FILE *
ID0fopen(const char * path,const char * mode)129 ID0fopen(const char *path, const char *mode)
130 {
131   FILE *ret;
132 
133   ID0set0();
134   ret = fopen(path, mode);
135   log_Printf(LogID0, "%p = fopen(\"%s\", \"%s\")\n", ret, path, mode);
136   ID0setuser();
137   return ret;
138 }
139 
140 int
ID0open(const char * path,int flags,...)141 ID0open(const char *path, int flags, ...)
142 {
143   int ret;
144   va_list ap;
145 
146   va_start(ap, flags);
147   ID0set0();
148   ret = open(path, flags, va_arg(ap, int));
149   log_Printf(LogID0, "%d = open(\"%s\", %d)\n", ret, path, flags);
150   ID0setuser();
151   va_end(ap);
152   return ret;
153 }
154 
155 int
ID0write(int fd,const void * data,size_t len)156 ID0write(int fd, const void *data, size_t len)
157 {
158   int ret;
159 
160   ID0set0();
161   ret = write(fd, data, len);
162   log_Printf(LogID0, "%d = write(%d, data, %ld)\n", ret, fd, (long)len);
163   ID0setuser();
164   return ret;
165 }
166 
167 int
ID0uu_lock(const char * basettyname)168 ID0uu_lock(const char *basettyname)
169 {
170   int ret;
171 
172   ID0set0();
173   ret = uu_lock(basettyname);
174   log_Printf(LogID0, "%d = uu_lock(\"%s\")\n", ret, basettyname);
175   ID0setuser();
176   return ret;
177 }
178 
179 int
ID0uu_lock_txfr(const char * basettyname,pid_t newpid)180 ID0uu_lock_txfr(const char *basettyname, pid_t newpid)
181 {
182   int ret;
183 
184   ID0set0();
185   ret = uu_lock_txfr(basettyname, newpid);
186   log_Printf(LogID0, "%d = uu_lock_txfr(\"%s\", %ld)\n", ret, basettyname,
187              (long)newpid);
188   ID0setuser();
189   return ret;
190 }
191 
192 int
ID0uu_unlock(const char * basettyname)193 ID0uu_unlock(const char *basettyname)
194 {
195   int ret;
196 
197   ID0set0();
198   ret = uu_unlock(basettyname);
199   log_Printf(LogID0, "%d = uu_unlock(\"%s\")\n", ret, basettyname);
200   ID0setuser();
201   return ret;
202 }
203 
204 void
ID0login(struct utmp * ut)205 ID0login(struct utmp *ut)
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   }
213   login(ut);
214   log_Printf(LogID0, "login(\"%s\", \"%.*s\")\n",
215             ut->ut_line, (int)(sizeof ut->ut_name), ut->ut_name);
216   ID0setuser();
217 }
218 
219 void
ID0logout(const char * device,int nologout)220 ID0logout(const char *device, int nologout)
221 {
222   struct utmp ut;
223   char ut_line[sizeof ut.ut_line + 1];
224 
225   strncpy(ut_line, device, sizeof ut_line - 1);
226   ut_line[sizeof ut_line - 1] = '\0';
227 
228   ID0set0();
229   if (nologout || logout(ut_line)) {
230     log_Printf(LogID0, "logout(\"%s\")\n", ut_line);
231     logwtmp(ut_line, "", "");
232     log_Printf(LogID0, "logwtmp(\"%s\", \"\", \"\")\n", ut_line);
233   } else
234     log_Printf(LogERROR, "ID0logout: No longer logged in on %s\n", ut_line);
235   ID0setuser();
236 }
237 
238 int
ID0bind_un(int s,const struct sockaddr_un * name)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
ID0connect_un(int s,const struct sockaddr_un * name)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
ID0kill(pid_t pid,int sig)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(%ld, %d)\n", result, (long)pid, sig);
272   ID0setuser();
273   return result;
274 }
275 
276 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
277 int
ID0kldload(const char * dev)278 ID0kldload(const char *dev)
279 {
280   int result;
281 
282   ID0set0();
283   result = kldload(dev);
284   log_Printf(LogID0, "%d = kldload(\"%s\")\n", result, dev);
285   ID0setuser();
286   return result;
287 }
288 #endif
289 
290 #ifndef NONETGRAPH
291 int
ID0NgMkSockNode(const char * name,int * cs,int * ds)292 ID0NgMkSockNode(const char *name, int *cs, int *ds)
293 {
294   int result;
295 
296   ID0set0();
297   result = NgMkSockNode(name, cs, ds);
298   log_Printf(LogID0, "%d = NgMkSockNode(\"%s\", &cs, &ds)\n",
299              result, name ? name : "");
300   ID0setuser();
301   return result;
302 }
303 #endif
304