summaryrefslogtreecommitdiff
path: root/sbin/pflogd/privsep.c
diff options
context:
space:
mode:
Diffstat (limited to 'sbin/pflogd/privsep.c')
-rw-r--r--sbin/pflogd/privsep.c69
1 files changed, 67 insertions, 2 deletions
diff --git a/sbin/pflogd/privsep.c b/sbin/pflogd/privsep.c
index 33d6b9c05f8..66734ee2857 100644
--- a/sbin/pflogd/privsep.c
+++ b/sbin/pflogd/privsep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: privsep.c,v 1.13 2004/12/22 09:21:02 otto Exp $ */
+/* $OpenBSD: privsep.c,v 1.14 2006/01/15 16:38:04 canacar Exp $ */
/*
* Copyright (c) 2003 Can Erkin Acar
@@ -16,7 +16,6 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
@@ -28,6 +27,7 @@
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <limits.h>
#include <pcap.h>
#include <pcap-int.h>
#include <pwd.h>
@@ -41,6 +41,7 @@
enum cmd_types {
PRIV_SET_SNAPLEN, /* set the snaplength */
+ PRIV_MOVE_LOG, /* move logfile away */
PRIV_OPEN_LOG /* open logfile for appending */
};
@@ -55,6 +56,7 @@ static int may_read(int, void *, size_t);
static void must_read(int, void *, size_t);
static void must_write(int, void *, size_t);
static int set_snaplen(int snap);
+static int move_log(const char *name);
/* bpf filter expression common to parent and child */
extern char *filter;
@@ -159,6 +161,13 @@ priv_init(void)
close(fd);
break;
+ case PRIV_MOVE_LOG:
+ logmsg(LOG_DEBUG,
+ "[priv]: msg PRIV_MOVE_LOG received");
+ ret = move_log(filename);
+ must_write(socks[0], &ret, sizeof(int));
+ break;
+
default:
logmsg(LOG_ERR, "[priv]: unknown command %d", cmd);
_exit(1);
@@ -182,6 +191,47 @@ set_snaplen(int snap)
return 0;
}
+static int
+move_log(const char *name)
+{
+ char ren[PATH_MAX];
+ int len;
+
+ for (;;) {
+ int fd;
+
+ len = snprintf(ren, sizeof(ren), "%s.bad.%08x",
+ name, arc4random());
+ if (len >= sizeof(ren)) {
+ logmsg(LOG_ERR, "[priv] new name too long");
+ return (1);
+ }
+
+ /* lock destinanion */
+ fd = open(ren, O_CREAT|O_EXCL, 0);
+ if (fd >= 0) {
+ close(fd);
+ break;
+ }
+ /* if file exists, try another name */
+ if (errno != EEXIST && errno != EINTR) {
+ logmsg(LOG_ERR, "[priv] failed to create new name: %s",
+ strerror(errno));
+ return (1);
+ }
+ }
+
+ if (rename(name, ren)) {
+ logmsg(LOG_ERR, "[priv] failed to rename %s to %s: %s",
+ name, ren, strerror(errno));
+ return (1);
+ }
+
+ logmsg(LOG_NOTICE,
+ "[priv]: log file %s moved to %s", name, ren);
+
+ return (0);
+}
/*
* send the snaplength to privileged process
@@ -223,6 +273,21 @@ priv_open_log(void)
return (fd);
}
+/* Move-away and reopen log-file */
+int
+priv_move_log(void)
+{
+ int cmd, ret;
+
+ if (priv_fd < 0)
+ errx(1, "%s: called from privileged portion\n", __func__);
+
+ cmd = PRIV_MOVE_LOG;
+ must_write(priv_fd, &cmd, sizeof(int));
+ must_read(priv_fd, &ret, sizeof(int));
+
+ return (ret);
+}
/* If priv parent gets a TERM or HUP, pass it through to child instead */
static void