title: Perform operation with different users
body: <p>I am developing a file manager in <code>Django</code> using <code>Python 3.5.2</code>. I need to manage files that are owned by different Linux user, but each user do not have any permission to read other users' files.</p>
  
  <p>I have tried to develop a <code>root</code> daemon process that only accept commands from main application and cannot be accessed directly. The daemon uses <code>os.fork()</code> to start a new child that will perform <code>seteuid()</code> and <code>setguid()</code>, then perform file manager operation.</p>
  
  <p>However, I do not have much success in dealing with forked child. What I need is I am able to run commands under different users with <code>seteuid()</code> and <code>setguid()</code>, so that each users can manage their own files properly.</p>
  
  <p>The following are my code in Django:</p>
  
  <p><strong><em>Main App <code>views.py</code></em></strong></p>
  
  <pre><code>def file_operation(request):
  
      user = {
          'username': request.session.get("username"),
          'home_dir': request.session.get('home_dir'),
          'uid': request.session.get("uid"),
          'gid': request.session.get("gid")
      }
  
      url = u'%s:%s/files?username=%s&amp;home_dir=%s&amp;uid=%s&amp;gid=%s' % (
          settings.DAEMON_FILE_HOST, settings.DAEMON_FILE_PORT,
          user['username'], user['home_dir'], user['uid'], user['gid']
      )
  
      return ProxyRequest().proxy_view(request, url)
  </code></pre>
  
  <p><code>ProxyRequest()</code> is a custom class that pass all the request from Main App to another url (daemon) and retrieve the response.</p>
  
  <p><strong><em>Daemon <code>views.py</code></em></strong></p>
  
  <pre><code>def connector(request):
  
      user = {
          'username': request.GET.get("username"),
          'home_dir': request.GET.get('home_dir'),
          'uid': int(request.GET.get("uid")),
          'gid': int(request.GET.get("gid"))
      }
  
      newpid = os.fork()
      response = None
      if newpid == 0:
          setegid(user['gid'])
          seteuid(user['uid'])
          response = RunCommand(user, request.GET.copy(), request.POST.copy(), request.FILES.copy())
  
      os.waitpid(newpid,0)
      return response
  </code></pre>
  
  <p><code>RunCommand()</code> is the backend process that perform file operation based on different type of <code>request</code> received.</p>
  
  <p>If I do not use <code>os.fork()</code>, my daemon process will stuck in another user after <code>seteuid()</code> instead of <code>root</code>, so I can no longer change the <code>euid</code> of the process to read another user directory.</p>
  
  <p>I am not expert in Python, so maybe I am not doing this correct? Is there a proper way to do this?</p>