Browse Source

Improvements on code

Ricardo Iván Vieitez Parra 12 years ago
parent
commit
cd8b9c9634
2 changed files with 48 additions and 28 deletions
  1. 13 5
      configure.ac
  2. 35 23
      miner.c

+ 13 - 5
configure.ac

@@ -74,6 +74,14 @@ AC_CHECK_HEADERS([sys/epoll.h])
 AC_CHECK_HEADERS([sys/prctl.h])
 AC_CHECK_HEADERS([sys/file.h])
 
+AC_CHECK_HEADERS([sys/file.h])
+
+# Setuid
+AC_CHECK_HEADERS([pwd.h])
+
+# Check for chroot support
+AC_CHECK_FUNCS([chroot])
+
 AC_FUNC_ALLOCA
 
 have_cygwin=false
@@ -314,20 +322,20 @@ AC_ARG_ENABLE([icarus],
 	[icarus=$enableval],
 	[icarus=yes]
 	)
-AM_CONDITIONAL([HAS_ICARUS], [test x$icarus = xyes])
 if test "x$icarus" = xyes; then
 	AC_DEFINE([USE_ICARUS], [1], [Defined to 1 if Icarus support is wanted])
 fi
+AM_CONDITIONAL([HAS_ICARUS], [test x$icarus = xyes])
 
-chroot = "no"
+chroot = "yes"
 
 AC_ARG_ENABLE([chroot],
-	[AC_HELP_STRING([--disable-chroot],[Compile support for running inside a chroot jail])],
+	[AC_HELP_STRING([--disable-chroot],[Compile support for running inside a chroot jail (default enabled)])],
 	[chroot=$enableval],
 	[chroot=yes]
 	)
-if test "x$chroot" = xchroot; then
-	AC_DEFINE([CHROOT], [1], [Defined to 1 if chroot jail support is wanted])
+if test "x$chroot" = xyes; then
+	AC_DEFINE([BFG_CHROOT], [1], [Defined to 1 if chroot jail support is wanted])
 fi
 
 avalon="no"

+ 35 - 23
miner.c

@@ -40,7 +40,7 @@
 #include <sys/types.h>
 #include <dirent.h>
 
-#ifdef CHROOT
+#ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
 
@@ -382,8 +382,12 @@ char *cmd_idle, *cmd_sick, *cmd_dead;
 	static int forkpid;
 #endif // defined(unix)
 
-#ifdef CHROOT
-char *chroot_dir, *chroot_user;
+#if defined(BFG_CHROOT) && defined(HAVE_CHROOT)
+char *chroot_dir;
+#endif
+
+#ifdef HAVE_PWD
+char *opt_setuid;
 #endif
 
 struct sigaction termhandler, inthandler;
@@ -1482,13 +1486,10 @@ static struct opt_table opt_config_table[] = {
 		     set_int_0_to_9999, opt_show_intval, &opt_bench_algo,
 		     opt_hidden),
 #endif
-#ifdef CHROOT
+#if defined(BFG_CHROOT) && defined(HAVE_CHOOT)
         OPT_WITH_ARG("--chroot-dir",
                      opt_set_charp, NULL, &chroot_dir,
                      "Chroot to a directory right after startup"),
-        OPT_WITH_ARG("--chroot-user",
-                     opt_set_charp, NULL, &chroot_user,
-                     "Username of an unprivileged user to run as"),
 #endif
 	OPT_WITH_ARG("--cmd-idle",
 	             opt_set_charp, NULL, &cmd_idle,
@@ -1792,6 +1793,11 @@ static struct opt_table opt_config_table[] = {
 		     set_shaders, NULL, NULL,
 		     "GPU shaders per card for tuning scrypt, comma separated"),
 #endif
+#endif
+#ifdef HAVE_PWD_H
+        OPT_WITH_ARG("--setuid",
+                     opt_set_charp, NULL, &opt_setuid,
+                     "Username of an unprivileged user to run as"),
 #endif
 	OPT_WITH_ARG("--sharelog",
 		     set_sharelog, NULL, NULL,
@@ -10063,29 +10069,35 @@ int main(int argc, char *argv[])
 	applog(LOG_DEBUG, "pthread_cancel workaround in use");
 #endif
 
-#ifdef CHROOT
+#ifdef HAVE_PWD_H
+	struct passwd *user_info = NULL;
+	if (opt_setuid != NULL) {
+		if ((user_info = getpwnam(opt_setuid)) == NULL) {
+			quit(1, "Unable to find setuid user information");
+		}
+	}
+#endif
+
+#if defined(BFG_CHROOT) && defined(HAVE_CHROOT)
         if (chroot_dir != NULL) {
-                struct passwd *user_info = NULL;
-                if (chroot_user != NULL) {
-                        if ((user_info = getpwnam(chroot_user)) == NULL) {
-                                quit(1, "Unable to find user information");
-                        }
-                } else if (getuid() == 0) {
-                        quit(1, "Running as root is not allowed");
+#ifdef HAVE_PWD_H
+                if (user_info == NULL && getuid() == 0) {
+                        applog(LOG_WARNING, "Running as root inside chroot");
                 }
-
-                if (chroot(chroot_dir) == 0) {
-                        if (user_info != NULL) {
-                                if (setgid((*user_info).pw_gid) == 0 && setuid((*user_info).pw_uid) != 0) {
-                                        quit(1, "Unable to setuid");
-                                }
-                        }
-                } else {
+#endif
+                if (chroot(chroot_dir) != 0) {
                        quit(1, "Unable to chroot");
                 }
         }
 #endif
 
+#ifdef HAVE_PWD_H
+		if (user_info != NULL) {
+			if (setgid((*user_info).pw_gid) == 0 && setuid((*user_info).pw_uid) != 0) {
+				quit(1, "Unable to setuid");
+			}
+		}
+#endif
 	raise_fd_limits();
 	
 	if (opt_benchmark) {