1 /* $OpenBSD: privsep.c,v 1.16 2006/10/25 20:55:04 moritz Exp $ */
4 * Copyright (c) 2003 Can Erkin Acar
5 * Copyright (c) 2003 Anil Madhavapeddy <anil@recoil.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
43 PRIV_SET_SNAPLEN
, /* set the snaplength */
44 PRIV_MOVE_LOG
, /* move logfile away */
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
);
59 static int move_log(const char *name
);
61 extern char *filename
;
64 /* based on syslogd privsep */
68 int i
, fd
, socks
[2], cmd
;
69 int snaplen
, ret
, olderrno
;
72 for (i
= 1; i
< NSIG
; i
++)
76 if (socketpair(AF_LOCAL
, SOCK_STREAM
, PF_UNSPEC
, socks
) == -1)
77 err(1, "socketpair() failed");
79 pw
= getpwnam("_pflogd");
81 errx(1, "unknown user _pflogd");
86 err(1, "fork() failed");
91 /* Child - drop privileges and return */
92 if (chroot(pw
->pw_dir
) != 0)
93 err(1, "unable to chroot");
95 err(1, "unable to chdir");
97 gidset
[0] = pw
->pw_gid
;
98 if (setresgid(pw
->pw_gid
, pw
->pw_gid
, pw
->pw_gid
) == -1)
99 err(1, "setresgid() failed");
100 if (setgroups(1, gidset
) == -1)
101 err(1, "setgroups() failed");
102 if (setresuid(pw
->pw_uid
, pw
->pw_uid
, pw
->pw_uid
) == -1)
103 err(1, "setresuid() failed");
110 /* Pass ALRM/TERM/HUP/INT/QUIT through to child, and accept CHLD */
111 signal(SIGALRM
, sig_pass_to_chld
);
112 signal(SIGTERM
, sig_pass_to_chld
);
113 signal(SIGHUP
, sig_pass_to_chld
);
114 signal(SIGINT
, sig_pass_to_chld
);
115 signal(SIGQUIT
, sig_pass_to_chld
);
116 signal(SIGCHLD
, sig_chld
);
118 setproctitle("[priv]");
121 while (!gotsig_chld
) {
122 if (may_read(socks
[0], &cmd
, sizeof(int)))
125 case PRIV_SET_SNAPLEN
:
127 "[priv]: msg PRIV_SET_SNAPLENGTH received");
128 must_read(socks
[0], &snaplen
, sizeof(int));
130 ret
= set_snaplen(snaplen
);
133 "[priv]: set_snaplen failed for snaplen %d",
137 must_write(socks
[0], &ret
, sizeof(int));
142 "[priv]: msg PRIV_OPEN_LOG received");
143 /* create or append logs but do not follow symlinks */
145 O_RDWR
|O_CREAT
|O_APPEND
|O_NONBLOCK
|O_NOFOLLOW
,
148 send_fd(socks
[0], fd
);
151 "[priv]: failed to open %s: %s",
152 filename
, strerror(olderrno
));
159 "[priv]: msg PRIV_MOVE_LOG received");
160 ret
= move_log(filename
);
161 must_write(socks
[0], &ret
, sizeof(int));
165 logmsg(LOG_ERR
, "[priv]: unknown command %d", cmd
);
174 /* this is called from parent */
176 set_snaplen(int snap
)
181 hpcap
->snapshot
= snap
;
188 move_log(const char *name
)
196 len
= snprintf(ren
, sizeof(ren
), "%s.bad.%08x",
198 if (len
>= sizeof(ren
)) {
199 logmsg(LOG_ERR
, "[priv] new name too long");
203 /* lock destinanion */
204 fd
= open(ren
, O_CREAT
|O_EXCL
, 0);
209 /* if file exists, try another name */
210 if (errno
!= EEXIST
&& errno
!= EINTR
) {
211 logmsg(LOG_ERR
, "[priv] failed to create new name: %s",
217 if (rename(name
, ren
)) {
218 logmsg(LOG_ERR
, "[priv] failed to rename %s to %s: %s",
219 name
, ren
, strerror(errno
));
224 "[priv]: log file %s moved to %s", name
, ren
);
230 * send the snaplength to privileged process
233 priv_set_snaplen(int snaplen
)
238 errx(1, "%s: called from privileged portion", __func__
);
240 cmd
= PRIV_SET_SNAPLEN
;
242 must_write(priv_fd
, &cmd
, sizeof(int));
243 must_write(priv_fd
, &snaplen
, sizeof(int));
245 must_read(priv_fd
, &ret
, sizeof(int));
247 /* also set hpcap->snapshot in child */
249 hpcap
->snapshot
= snaplen
;
261 errx(1, "%s: called from privileged portion", __func__
);
264 must_write(priv_fd
, &cmd
, sizeof(int));
265 fd
= receive_fd(priv_fd
);
269 /* Move-away and reopen log-file */
276 errx(1, "%s: called from privileged portion\n", __func__
);
279 must_write(priv_fd
, &cmd
, sizeof(int));
280 must_read(priv_fd
, &ret
, sizeof(int));
285 /* If priv parent gets a TERM or HUP, pass it through to child instead */
287 sig_pass_to_chld(int sig
)
292 kill(child_pid
, sig
);
296 /* if parent gets a SIGCHLD, it will exit */
298 sig_chld(int sig __unused
)
303 /* Read all data or return 1 for error. */
305 may_read(int fd
, void *buf
, size_t n
)
312 res
= read(fd
, s
+ pos
, n
- pos
);
315 if (errno
== EINTR
|| errno
== EAGAIN
)
326 /* Read data with the assertion that it all must come through, or
327 * else abort the process. Based on atomicio() from openssh. */
329 must_read(int fd
, void *buf
, size_t n
)
336 res
= read(fd
, s
+ pos
, n
- pos
);
339 if (errno
== EINTR
|| errno
== EAGAIN
)
349 /* Write data with the assertion that it all has to be written, or
350 * else abort the process. Based on atomicio() from openssh. */
352 must_write(int fd
, void *buf
, size_t n
)
359 res
= write(fd
, s
+ pos
, n
- pos
);
362 if (errno
== EINTR
|| errno
== EAGAIN
)