$OpenBSD$

- drop privileges to a dedicated user

--- main.c.orig	Fri Dec 26 15:31:29 2014
+++ main.c	Fri Nov 11 19:06:41 2016
@@ -27,6 +27,8 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <pcap.h>
+#include <pwd.h>
+#include <errno.h>
 
 #include "hping2.h"
 
@@ -252,6 +254,21 @@ int main(int argc, char **argv)
 	if (open_pcap() == -1) {
 		printf("[main] open_pcap failed\n");
 		exit(1);
+	}
+
+	if (!getuid()) {
+		struct passwd *pw;
+		pw = getpwnam("_hping3");
+		if (pw == NULL) {
+			printf("[main] can't drop privs: no such user\n");
+			exit(1);
+		}
+		if (setgroups(1, &pw->pw_gid) == -1 ||
+		    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
+		    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1) {
+			printf("[main] can't drop privs: %s\n", strerror(errno));
+			exit(1);
+		}
 	}
 
 	/* get physical layer header size */
