Index: Makefile
===================================================================
RCS file: /cvs/fedora/mock/Makefile,v
retrieving revision 1.8
diff -u -r1.8 Makefile
--- Makefile	12 Apr 2006 14:23:17 -0000	1.8
+++ Makefile	21 Jun 2006 19:35:35 -0000
@@ -21,7 +21,7 @@
 install:
 	mkdir -p $(DESTDIR)/usr/bin/
 	mkdir -p $(DESTDIR)/usr/libexec
-	install -m 755 mock.py $(DESTDIR)/usr/bin/mock
+	install -m 755 mock.py $(DESTDIR)/usr/bin
 	install -m 755 mock-yum $(DESTDIR)/usr/libexec/mock-yum
 	mkdir -p $(DESTDIR)/var/lib/mock
 	for d in $(SUBDIRS); do make  DESTDIR=`cd $(DESTDIR); pwd` -C $$d install; [ $$? = 0 ] || exit 1; done
Index: mock.py
===================================================================
RCS file: /cvs/fedora/mock/mock.py,v
retrieving revision 1.53
diff -u -r1.53 mock.py
--- mock.py	8 Jun 2006 21:28:46 -0000	1.53
+++ mock.py	21 Jun 2006 19:35:35 -0000
@@ -106,6 +106,17 @@
         self._state = 'unstarted'
         self.tmplog = LogBuffer()
         self.config = config
+
+        # save our uids
+        self.highuid = os.geteuid()
+        self.lowuid = os.getuid()
+
+        # set gid to mock group
+        os.setgid(os.getegid())
+            
+        # insure we're *not* running privileged
+        self.drop()
+        
         root = config['root']
         if config.has_key('unique-ext'):
             root = "%s-%s" % (root, config['unique-ext'])
@@ -151,6 +162,14 @@
         cfgout.flush()
         cfgout.close()
     
+    def elevate(self):
+        self.debug("elevate: setting uid to %d" % self.highuid)
+        os.setreuid(self.highuid, self.lowuid)
+
+    def drop(self):
+        self.debug("drop: setting uid to %d" % self.lowuid)
+        os.setreuid(self.lowuid, self.highuid)
+        
     def log(self, msg):
         if self.config['quiet']: return
         print msg
@@ -184,8 +203,7 @@
             
         if os.path.exists(self.basedir):
             cmd = '%s -rf %s' % (self.config['rm'], self.basedir)
-            (retval, output) = self.do(cmd)
-
+            (retval, output) = self.do_elevated(cmd)
             if retval != 0:
                 error("Errors cleaning out chroot: %s" % output)
                 if os.path.exists(self.rootdir):
@@ -209,14 +227,28 @@
 
     def unpack(self):
         self.state('unpack cache')
-        cmd = '%s %s %s' % (self.config['unpack_cmd'], self.basedir, self.cache_file)
-        self.do(cmd)
+        if self.cache_file.find(".bz2") != -1:
+            opts = "-jxpf"
+        elif self.cache_file.find(".gz") != -1:
+            opts = "-zxpf"
+        else:
+            opts = "-xpf"
+        cmd = '%s %s %s %s' % (self.config['unpack_cmd'], opts, self.basedir, self.cache_file)
+        (retval, output) = self.do_elevated(cmd)
+        return retval
 
     def pack(self):
         self.state('create cache')
         self._ensure_dir(os.path.join(self.config['basedir'], self.config['cache_topdir']))
-        cmd = '%s %s %s root' % (self.config['pack_cmd'], self.basedir, self.cache_file)
-        self.do(cmd)
+        if self.cache_file.find(".bz2") != -1:
+            opts = "-jlcf"
+        elif self.cache_file.find(".gz") != -1:
+            opts = "-zlcf"
+        else:
+            opts = "-clf"
+        cmd = '%s %s %s root' % (self.config['pack_cmd'], opts, self.cache_file)
+        (retval, output) = self.do_elevated(cmd)
+        return retval
     
     def prep(self):
         self.state("prep")
@@ -260,7 +292,7 @@
         command = '%s %s' % (basecmd, cmd)
         self.debug("yum: command %s" % command)
 
-        (retval, output) = self.do(command)
+        (retval, output) = self.do_elevated(command)
 
         if retval != 0:
             raise YumError, "Error performing yum command: %s" % command
@@ -412,10 +444,10 @@
         self._ensure_dir(procdir)
 
         self.debug("mounting proc in %s" % procdir)
-        command = '%s -t proc proc %s/proc' % (self.config['mount'], 
+        command = '%s -n -t proc proc %s/proc' % (self.config['mount'], 
                                                self.rootdir)
         track.write('proc\n')
-        (retval, output) = self.do(command)
+        (retval, output) = self.do_elevated(command)
         track.flush()
         
         if retval != 0:
@@ -427,9 +459,9 @@
         devptsdir = os.path.join(self.rootdir, 'dev/pts')
         self._ensure_dir(devptsdir)
         self.debug("mounting devpts in %s" % devptsdir)
-        command = '%s -t devpts devpts %s' % (self.config['mount'], devptsdir)
+        command = '%s -n -t devpts devpts %s' % (self.config['mount'], devptsdir)
         track.write('dev/pts\n')
-        (retval, output) = self.do(command)
+        (retval, output) = self.do_elevated(command)
         track.flush()
         track.close()
 
@@ -442,7 +474,7 @@
     
         item = '%s/%s' % (self.rootdir, path)
         command = '%s %s' % (self.config['umount'], item)
-        (retval, output) = self.do(command)
+        (retval, output) = self.do_elevated(command)
     
         if retval != 0:
             if output.find('not mounted') == -1: # this probably won't work in other LOCALES
@@ -464,7 +496,9 @@
             if len(item.strip()) < 1:
                 continue
             
+            self.elevate()
             self._umount(item)
+            self.drop()
             
         # poof, no more file
         if os.path.exists(mf):
@@ -504,6 +538,30 @@
 
         return (retval, output)
 
+    def do_elevated(self, cmd):
+        if os.getuid() != self.highuid:
+            need_drop = 1
+        else:
+            need_drop = 0
+        try:
+            self.elevate()
+            (retval, output) = self.do(cmd)
+        finally:
+            if need_drop: self.drop()
+        return (retval, output)
+
+    def do_asuser(self, cmd):
+        if os.getuid() != self.lowuid:
+            need_elevate = 1
+        else:
+            need_elevate = 0
+        try:
+            self.drop()
+            (retval, output) = self.do(cmd)
+        finally:
+            if need_elevate: self.elevate()
+        return (retval, output)
+
     def do_chroot(self, command, fatal = False, exitcode=None):
         """execute given command in root"""
         cmd = ""
@@ -518,7 +576,7 @@
                                                  self.rootdir,
                                                  self.config['runuser'],
                                                  command)
-        (ret, output) = self.do(cmd)
+        (ret, output) = self.do_elevated(cmd)
         if (ret != 0) and fatal:
             self.close()
             if exitcode:
@@ -544,13 +602,17 @@
             if n.startswith('rpmlib'):
                 continue
 
+            self.elevate()
             req = rpmUtils.miscutils.formatRequire(n, v, f)
+            self.drop()
             reqlist.append(req)
         
         # Extract SRPM name components - still not nice, shouldn't this
         # be somewhere in the "hdr" parameter?
         fname = os.path.split(str(srpm))[1]
+        self.elevate()
         name, ver, rel, epoch, arch = rpmUtils.miscutils.splitFilename(fname)
+        self.drop()
 
         # Add the 'more_buildreqs' for this SRPM (if defined)
         for this_srpm in ['-'.join([name,ver,rel]),
@@ -564,7 +626,10 @@
                     reqlist.append(req)
                 break
         
-        return rpmUtils.miscutils.unique(reqlist)
+        self.elevate()
+        ret = rpmUtils.miscutils.unique(reqlist)
+        self.drop()
+        return ret
     
     def _prep_install(self):
         """prep chroot for installation"""
@@ -597,7 +662,7 @@
             cmd = '%s %s -m %s %s %s %s' % (self.config['mknod'], 
                       devpath, perm, devtype, major, minor)
             if not os.path.exists(devpath):
-                (retval, output) = self.do(cmd)
+                (retval, output) = self.do_elevated(cmd)
                 if retval != 0:
                     raise RootError, "could not mknod error was: %s" % output
 
@@ -760,12 +825,12 @@
 
 def setup_default_config_opts(config_opts):
     config_opts['basedir'] = '/var/lib/mock/' # root name is automatically added to this
-    config_opts['chroot'] = '/usr/sbin/mock-helper chroot'
-    config_opts['mount'] = '/usr/sbin/mock-helper mount'
-    config_opts['umount'] = '/usr/sbin/mock-helper umount'
-    config_opts['rm'] = '/usr/sbin/mock-helper rm'
-    config_opts['mknod'] = '/usr/sbin/mock-helper mknod'
-    config_opts['yum'] = '/usr/sbin/mock-helper yum'
+    config_opts['chroot'] = 'chroot'
+    config_opts['mount'] = 'mount'
+    config_opts['umount'] = 'umount'
+    config_opts['rm'] = 'rm'
+    config_opts['mknod'] = 'mknod'
+    config_opts['yum'] = 'yum'
     config_opts['runuser'] = '/sbin/runuser'
     config_opts['chroot_setup_cmd'] = 'install buildsys-build'
     config_opts['chrootuser'] = 'mockbuild'
@@ -792,8 +857,8 @@
     # caching-related config options
     config_opts['rebuild_cache'] = False
     config_opts['use_cache'] = False
-    config_opts['pack_cmd'] = "/usr/sbin/mock-helper pack"
-    config_opts['unpack_cmd'] = "/usr/sbin/mock-helper unpack"
+    config_opts['pack_cmd'] = "tar"
+    config_opts['unpack_cmd'] = "tar"
     config_opts['cache_ext'] = ".tar.gz"
     config_opts['cache_topdir'] = "root-cache"
     config_opts['max_cache_age_days'] = 15
@@ -902,11 +967,6 @@
         print "You need to be a member of the mock group for this to work"
         sys.exit(1)
 
-    # and make sure they're not root
-    if os.geteuid() == 0:
-        error("Don't try to run mock as root!")
-        sys.exit(1)
-        
     # defaults
     config_opts = {}
     setup_default_config_opts(config_opts)
Index: mock.spec
===================================================================
RCS file: /cvs/fedora/mock/mock.spec,v
retrieving revision 1.16
diff -u -r1.16 mock.spec
--- mock.spec	7 Jun 2006 12:29:15 -0000	1.16
+++ mock.spec	21 Jun 2006 19:35:35 -0000
@@ -1,6 +1,6 @@
 Summary: Builds packages inside chroots
 Name: mock
-Version: 0.6
+Version: 0.7
 Release: 1
 License: GPL
 Group: Development/Tools
@@ -28,7 +28,7 @@
 # make the default.cfg link
 cd $RPM_BUILD_ROOT/%{_sysconfdir}/%{name}
 ln -s fedora-development-i386-core.cfg default.cfg
-
+cd $RPM_BUILD_ROOT/%{_bindir}
 
 %clean
 rm -rf $RPM_BUILD_ROOT
@@ -45,10 +45,10 @@
 %doc README ChangeLog
 %dir  %{_sysconfdir}/%{name}
 %config(noreplace) %{_sysconfdir}/%{name}/*.cfg
-%{_bindir}/%{name}
+%attr(04750, root, mock) %{_bindir}/%{name}
+%{_bindir}/%{name}.py*
 %{_libexecdir}/mock-yum
 %{_mandir}/man1/mock.1*
-%attr(04750, root, mock) %{_sbindir}/mock-helper
 %attr(02775, root, mock) %dir /var/lib/mock
 %{_libdir}/libselinux-mock.so
 
Index: etc/defaults.cfg
===================================================================
RCS file: /cvs/fedora/mock/etc/defaults.cfg,v
retrieving revision 1.5
diff -u -r1.5 defaults.cfg
--- etc/defaults.cfg	13 Jun 2006 05:31:30 -0000	1.5
+++ etc/defaults.cfg	21 Jun 2006 19:35:35 -0000
@@ -7,29 +7,29 @@
 #
 # config_opts['foo'] = bar
 config_opts['basedir'] = '/var/lib/mock/'
-config_opts['chroot'] = '/usr/sbin/mock-helper chroot'
-config_opts['mount'] = '/usr/sbin/mock-helper mount'
-config_opts['umount'] = '/usr/sbin/mock-helper umount'
-config_opts['rm'] = '/usr/sbin/mock-helper rm'
-config_opts['mknod'] = '/usr/sbin/mock-helper mknod'
-config_opts['yum'] = '/usr/sbin/mock-helper yum'
+config_opts['chroot'] = 'chroot'
+config_opts['mount'] = 'mount'
+config_opts['umount'] = 'umount'
+config_opts['rm'] = 'rm'
+config_opts['mknod'] = 'mknod'
+config_opts['yum'] = 'yum'
 config_opts['runuser'] = '/sbin/runuser'
 config_opts['chrootuser'] = 'mockbuild'
 config_opts['chrootgroup'] = 'mockbuild'
-config_opts['chrootuid'] = os.geteuid()
-config_opts['chrootgid'] = os.getegid()
+#config_opts['chrootuid'] = os.geteuid()
+#config_opts['chrootgid'] = os.getegid()
 config_opts['chroothome'] = '/builddir'
 config_opts['clean'] = True
 
 # caching related options
-config_opts['rebuild_cache'] = False
-config_opts['use_cache'] = False
-config_opts['pack_cmd'] = "/usr/sbin/mock-helper pack"
-config_opts['unpack_cmd'] = "/usr/sbin/mock-helper unpack"
+config_opts['rebuild_cache'] = True
+config_opts['use_cache'] = True
+config_opts['pack_cmd'] = "tar"
+config_opts['unpack_cmd'] = "tar"
 config_opts['cache_ext'] = ".tar.gz"
 config_opts['cache_topdir'] = "root-cache"
 config_opts['max_cache_age_days'] = 15
 
 
 config_opts['chroot_setup_cmd'] = 'install buildsys-build'
-#config_opts['chroot_setup_cmd'] = 'groupinstall build'
\ No newline at end of file
+#config_opts['chroot_setup_cmd'] = 'groupinstall build'
Index: src/Makefile
===================================================================
RCS file: /cvs/fedora/mock/src/Makefile,v
retrieving revision 1.2
diff -u -r1.2 Makefile
--- src/Makefile	17 Jun 2005 20:53:31 -0000	1.2
+++ src/Makefile	21 Jun 2006 19:35:35 -0000
@@ -1,32 +1,43 @@
-CC=gcc
-EXECUTABLE=mock-helper
-SBINDIR=/usr/sbin
-MOCKGROUP=mock
-DESTDIR=''
-INSTALL=/usr/bin/install
-MKDIR=/bin/mkdir
+CC	:=	gcc
+CFLAGS 	:= 	-O2 -g -Wall -Werror
+#DEBUG	:= 	-DDEBUG=1
+EXECUTABLE:=	mock
+LIBRARY	:=	libselinux-mock.so
+BINDIR	:=	/usr/bin
+MOCKGROUP:=	mock
+#DESTDIR	:=	''
+INSTALL	:=	/usr/bin/install
+MKDIR	:=	/bin/mkdir
 
 ifndef NOSELINUX
 SELINUXFLAGS=-DUSE_SELINUX=1 -lselinux
 endif
 
 ifneq (,$(filter ppc64 x86_64 s390x,$(shell uname -m)))
-LIBDIR = /usr/lib64
+LIBDIR 	:=	/usr/lib64
 else
-LIBDIR = /usr/lib
+LIBDIR 	:=	/usr/lib
 endif
 
-all: 
-	$(CC) $(CFLAGS) -o $(EXECUTABLE) mock-helper.c $(SELINUXFLAGS)
+all: $(EXECUTABLE) $(LIBRARY)
+
+$(LIBRARY): selinux-mock.o
+	$(LD) -shared -o $(LIBRARY) selinux-mock.o
+
+selinux-mock.o:  selinux-mock.c
 	$(CC) $(CFLAGS) -fPIC -c selinux-mock.c
-	$(LD) -shared -o libselinux-mock.so selinux-mock.o
+
+$(EXECUTABLE):	mock.c
+	$(CC) $(CFLAGS) $(DEBUG) -o $@ $<
 
 clean:
-	rm -f $(EXECUTABLE)
+	rm -f $(EXECUTABLE) $(LIBRARY)
 	rm -f *~ *.bak *.o *.so
 
-install:
-	$(MKDIR) -p $(DESTDIR)/$(SBINDIR) $(DESTDIR)/$(LIBDIR)
-	$(INSTALL) -m 4750 $(EXECUTABLE) $(DESTDIR)/$(SBINDIR)/$(EXECUTABLE)
-	$(INSTALL) -m 755 libselinux-mock.so $(DESTDIR)/$(LIBDIR)
-
+install:   $(EXECUTABLE) $(LIBRARY)
+	[ -d $(DESTDIR)/$(BINDIR) ] || \
+		$(MKDIR) -p $(DESTDIR)/$(BINDIR)
+	[ -d $(DESTDIR)/$(LIBDIR) ] || \
+		$(MKDIR) -p $(DESTDIR)/$(LIBDIR)
+	$(INSTALL) -o root -g $(MOCKGROUP) -m 4750 $(EXECUTABLE) $(DESTDIR)/$(BINDIR)
+	$(INSTALL) -m 755 $(LIBRARY) $(DESTDIR)/$(LIBDIR)
