8786a97
diff -up tcpdump-4.4.0/tcpdump.1.in.eperm tcpdump-4.4.0/tcpdump.1.in
f3b5ddb
--- tcpdump-4.4.0/tcpdump.1.in.eperm	2013-10-07 15:21:26.795602764 +0200
f3b5ddb
+++ tcpdump-4.4.0/tcpdump.1.in	2013-10-07 15:21:26.800602762 +0200
8786a97
@@ -221,6 +221,9 @@ have the name specified with the
aca9b7a
 flag, with a number after it, starting at 1 and continuing upward.
aca9b7a
 The units of \fIfile_size\fP are millions of bytes (1,000,000 bytes,
aca9b7a
 not 1,048,576 bytes).
aca9b7a
+
aca9b7a
+Note that when used with \fB\-Z\fR option (enabled by default), privileges
aca9b7a
+are dropped before opening first savefile.
aca9b7a
 .TP
aca9b7a
 .B \-d
aca9b7a
 Dump the compiled packet-matching code in a human readable form to
8786a97
@@ -720,7 +723,9 @@ but before opening any savefiles for out
aca9b7a
 and the group ID to the primary group of
aca9b7a
 .IR user .
aca9b7a
 .IP
aca9b7a
-This behavior can also be enabled by default at compile time.
aca9b7a
+This behavior is enabled by default (\fB\-Z tcpdump\fR), and can
aca9b7a
+be disabled by \fB\-Z root\fR.
aca9b7a
+
aca9b7a
 .IP "\fI expression\fP"
aca9b7a
 .RS
aca9b7a
 selects which packets will be dumped.
8786a97
diff -up tcpdump-4.4.0/tcpdump.c.eperm tcpdump-4.4.0/tcpdump.c
8786a97
--- tcpdump-4.4.0/tcpdump.c.eperm	2013-03-24 22:49:18.000000000 +0100
f3b5ddb
+++ tcpdump-4.4.0/tcpdump.c	2013-10-07 15:22:26.360590143 +0200
8786a97
@@ -1426,11 +1426,24 @@ main(int argc, char **argv)
8786a97
 	}
8786a97
 #endif /* HAVE_CAP_NG_H */
8786a97
 
aca9b7a
-	if (getuid() == 0 || geteuid() == 0) {
aca9b7a
-		if (username || chroot_dir)
aca9b7a
+	/* If user is running tcpdump as root and wants to write to the savefile,
aca9b7a
+	 * we will check if -C is set and if it is, we will drop root
aca9b7a
+	 * privileges right away and consequent call to	pcap_dump_open()
aca9b7a
+	 * will most likely fail for the first file. If -C flag is not set we
aca9b7a
+	 * will create file as root then change ownership of file to proper
aca9b7a
+	 * user(default tcpdump) and drop root privileges.
aca9b7a
+	 */
aca9b7a
+	int chown_flag = 0;
8786a97
+
8786a97
+	if (WFileName && (getuid() == 0 || geteuid() == 0))
8786a97
+		if (Cflag && (username || chroot_dir))
8786a97
+			droproot(username, chroot_dir);
8786a97
+                else 
aca9b7a
+			chown_flag = 1;
8786a97
+	else
8786a97
+		if ((getuid() == 0 || geteuid() == 0) && (username || chroot_dir))
8786a97
 			droproot(username, chroot_dir);
8786a97
 
8786a97
-	}
aca9b7a
 #endif /* WIN32 */
aca9b7a
 
8786a97
 	if (pcap_setfilter(pd, &fcode) < 0)
8786a97
@@ -1450,6 +1463,21 @@ main(int argc, char **argv)
aca9b7a
 		  MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
aca9b7a
 
aca9b7a
 		p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
8786a97
+
aca9b7a
+		/* Change ownership of file and drop root privileges */
aca9b7a
+		if (chown_flag) {
8786a97
+			struct passwd *pwd;
aca9b7a
+
8786a97
+			pwd = getpwnam(username);
8786a97
+			if (!pwd)
aca9b7a
+				error("Couldn't find user '%s'", username);
aca9b7a
+
f3b5ddb
+			if (strcmp(WFileName, "-") && chown(dumpinfo.CurrentFileName, pwd->pw_uid, pwd->pw_gid) < 0)
8786a97
+				error("Couldn't change ownership of savefile");
aca9b7a
+
aca9b7a
+			if (username || chroot_dir)
aca9b7a
+				droproot(username, chroot_dir);
aca9b7a
+		}
8786a97
 #ifdef HAVE_CAP_NG_H
8786a97
         /* Give up capabilities, clear Effective set */
8786a97
         capng_clear(CAPNG_EFFECTIVE);