# Author: Andrea Parrella <yap@yapsoft.it>
#
# From psybnc directory apply the patch using the following command and rebuild psybnc
# 	patch -p1 < psybnc-chroot.patch
#

diff -ur psybnc/src/p_global.h psybnc.yap/src/p_global.h
--- psybnc/src/p_global.h	2005-06-04 20:22:45.000000000 +0200
+++ psybnc.yap/src/p_global.h	2008-08-25 16:08:55.000000000 +0200
@@ -40,6 +40,7 @@
 #include <signal.h>
 #include <setjmp.h>
 #include <errno.h>
+#include <pwd.h>
 #ifdef HAVE_CONFIG
 #include <config.h>
 #endif
diff -ur psybnc/src/psybnc.c psybnc.yap/src/psybnc.c
--- psybnc/src/psybnc.c	2005-06-04 20:22:45.000000000 +0200
+++ psybnc.yap/src/psybnc.c	2008-08-25 16:24:48.000000000 +0200
@@ -168,12 +168,44 @@
   char *bversion;
   FILE *pidfile,*conffile;
   int i;
-  if(argc==2)
-  {
-      strmncpy(configfile,argv[1],sizeof(configfile));
-  } else {
-      strcpy(configfile,"psybnc.conf");  /* rcsid */
+  int c;
+  struct passwd *pwinfo = 0;
+
+  strcpy(configfile,"psybnc.conf");  /* rcsid */
+  while ((c = getopt(argc, argv, "c:u:r:")) != -1) {
+	switch (c) {
+        	case 'c' :
+			strmncpy(configfile,argv[1],sizeof(configfile));
+			break;
+		case 'u' :
+			if ((pwinfo = getpwnam(optarg)) == NULL) {
+				printf("Invalid user\n");
+				exit(0x0);
+			}
+			break;
+		case 'r' :
+			if (chroot(optarg) != 0) {
+				printf("Chroot failed\n");
+				exit(0x0);
+			}
+			break;
+		default :
+			printf("Usage: %s [-c <config_file>] [-u <username>] [-r <chroot>]\n", argv[0]);
+			exit(0x0);
+	}
   }
+
+  if (pwinfo != 0) {
+	if (setgid(pwinfo->pw_gid) != 0) {
+		perror("setgid()");
+		exit(0x0);
+	}
+	if (setuid(pwinfo->pw_uid) != 0) {
+		perror("setgid()");
+		exit(0x0);
+	}	
+  }
+
   conffile=fopen(configfile,"r");
   if(conffile==NULL)
   {
