1 /* $OpenBSD: privsep.c,v 1.8 2004/03/14 19:17:05 otto Exp $ */
2 /* $DragonFly: src/usr.sbin/pflogd/privsep.c,v 1.1 2004/09/21 21:25:28 joerg Exp $ */
5 * Copyright (c) 2003 Can Erkin Acar
6 * Copyright (c) 2003 Anil Madhavapeddy <anil@recoil.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
44 PRIV_SET_SNAPLEN
, /* set the snaplength */
45 PRIV_OPEN_LOG
/* open logfile for appending */
48 static int priv_fd
= -1;
49 static volatile pid_t child_pid
= -1;
51 volatile sig_atomic_t gotsig_chld
= 0;
53 static void sig_pass_to_chld(int);
54 static void sig_chld(int);
55 static int may_read(int, void *, size_t);
56 static void must_read(int, void *, size_t);
57 static void must_write(int, void *, size_t);
58 static int set_snaplen(int snap
);
60 /* bpf filter expression common to parent and child */
63 extern char *filename
;
66 /* based on syslogd privsep */
70 int i
, fd
, socks
[2], cmd
;
74 for (i
= 1; i
< NSIG
; i
++)
78 if (socketpair(AF_LOCAL
, SOCK_STREAM
, PF_UNSPEC
, socks
) == -1)
79 err(1, "socketpair() failed");
81 pw
= getpwnam("_pflogd");
83 errx(1, "unknown user _pflogd");
88 err(1, "fork() failed");
93 /* Child - drop privileges and return */
94 if (chroot(pw
->pw_dir
) != 0)
95 err(1, "unable to chroot");
97 err(1, "unable to chdir");
99 gidset
[0] = pw
->pw_gid
;
100 if (setgroups(1, gidset
) == -1)
101 err(1, "setgroups() failed");
102 if (setegid(pw
->pw_gid
) == -1)
103 err(1, "setegid() failed");
104 if (setgid(pw
->pw_gid
) == -1)
105 err(1, "setgid() failed");
106 if (seteuid(pw
->pw_uid
) == -1)
107 err(1, "seteuid() failed");
108 if (setuid(pw
->pw_uid
) == -1)
109 err(1, "setuid() failed");
116 /* Pass ALRM/TERM/HUP through to child, and accept CHLD */
117 signal(SIGALRM
, sig_pass_to_chld
);
118 signal(SIGTERM
, sig_pass_to_chld
);
119 signal(SIGHUP
, sig_pass_to_chld
);
120 signal(SIGCHLD
, sig_chld
);
122 setproctitle("[priv]");
125 while (!gotsig_chld
) {
126 if (may_read(socks
[0], &cmd
, sizeof(int)))
129 case PRIV_SET_SNAPLEN
:
131 "[priv]: msg PRIV_SET_SNAPLENGTH received");
132 must_read(socks
[0], &snaplen
, sizeof(int));
134 ret
= set_snaplen(snaplen
);
137 "[priv]: set_snaplen failed for snaplen %d",
141 must_write(socks
[0], &ret
, sizeof(int));
146 "[priv]: msg PRIV_OPEN_LOG received");
147 /* create or append logs but do not follow symlinks */
149 O_RDWR
|O_CREAT
|O_APPEND
|O_NONBLOCK
|O_NOFOLLOW
,
153 "[priv]: failed to open %s: %s",
154 filename
, strerror(errno
));
155 send_fd(socks
[0], fd
);
160 logmsg(LOG_ERR
, "[priv]: unknown command %d", cmd
);
169 /* this is called from parent */
171 set_snaplen(int snap
)
176 hpcap
->snapshot
= snap
;
184 * send the snaplength to privileged process
187 priv_set_snaplen(int snaplen
)
192 errx(1, "%s: called from privileged portion", __func__
);
194 cmd
= PRIV_SET_SNAPLEN
;
196 must_write(priv_fd
, &cmd
, sizeof(int));
197 must_write(priv_fd
, &snaplen
, sizeof(int));
199 must_read(priv_fd
, &ret
, sizeof(int));
201 /* also set hpcap->snapshot in child */
203 hpcap
->snapshot
= snaplen
;
215 errx(1, "%s: called from privileged portion\n", __func__
);
218 must_write(priv_fd
, &cmd
, sizeof(int));
219 fd
= receive_fd(priv_fd
);
224 /* If priv parent gets a TERM or HUP, pass it through to child instead */
226 sig_pass_to_chld(int sig
)
231 kill(child_pid
, sig
);
235 /* if parent gets a SIGCHLD, it will exit */
237 sig_chld(int sig __unused
)
242 /* Read all data or return 1 for error. */
244 may_read(int fd
, void *buf
, size_t n
)
251 res
= read(fd
, s
+ pos
, n
- pos
);
254 if (errno
== EINTR
|| errno
== EAGAIN
)
265 /* Read data with the assertion that it all must come through, or
266 * else abort the process. Based on atomicio() from openssh. */
268 must_read(int fd
, void *buf
, size_t n
)
275 res
= read(fd
, s
+ pos
, n
- pos
);
278 if (errno
== EINTR
|| errno
== EAGAIN
)
288 /* Write data with the assertion that it all has to be written, or
289 * else abort the process. Based on atomicio() from openssh. */
291 must_write(int fd
, void *buf
, size_t n
)
298 res
= write(fd
, s
+ pos
, n
- pos
);
301 if (errno
== EINTR
|| errno
== EAGAIN
)