Ticket #1473: deluged-setuid.patch

File deluged-setuid.patch, 3.3 KB (added by Cameron Tacklind, 14 years ago)

Updated version that sets gid correctly and before uid

  • deluge/main.py

     deluge/main.py |   52 ++++++++++++++++++++++++++++++++--------------------
     1 files changed, 32 insertions(+), 20 deletions(-)
    
    diff --git a/deluge/main.py b/deluge/main.py
    index 12959a1..76686ae 100644
    a b def start_daemon():  
    149149    parser.add_option("-u", "--ui-interface", dest="ui_interface",
    150150        help="Interface daemon will listen for UI connections on, this should be\
    151151 an IP address", metavar="IFACE", action="store", type="str")
    152     parser.add_option("-d", "--do-not-daemonize", dest="donot",
    153         help="Do not daemonize", action="store_true", default=False)
     152    if not (deluge.common.windows_check() or deluge.common.osx_check()):
     153        parser.add_option("-d", "--do-not-daemonize", dest="donot",
     154            help="Do not daemonize", action="store_true", default=False)
    154155    parser.add_option("-c", "--config", dest="config",
    155156        help="Set the config location", action="store", type="str")
    156157    parser.add_option("-l", "--logfile", dest="logfile",
    157158        help="Set the logfile location", action="store", type="str")
    158159    parser.add_option("-P", "--pidfile", dest="pidfile",
    159160        help="Use pidfile to store process id", action="store", type="str")
     161    if not deluge.common.windows_check():
     162        parser.add_option("-U", "--user", dest="user",
     163            help="User to switch to. Only use it when starting as root", action="store", type="str")
     164        parser.add_option("-g", "--group", dest="group",
     165            help="Group to switch to. Only use it when starting as root", action="store", type="str")
    160166    parser.add_option("-L", "--loglevel", dest="loglevel",
    161167        help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str")
    162168    parser.add_option("-q", "--quiet", dest="quiet",
    def write_pidfile():  
    197203            open(options.pidfile, "wb").write("%d\n" % os.getpid())
    198204
    199205    # If the donot daemonize is set, then we just skip the forking
    200     if not options.donot:
    201         # Windows check, we log to the config folder by default
    202         if deluge.common.windows_check() or deluge.common.osx_check():
    203             open_logfile()
    204             write_pidfile()
    205         else:
    206             if os.fork() == 0:
    207                 os.setsid()
    208                 if os.fork() == 0:
    209                     open_logfile()
    210                     write_pidfile()
    211                 else:
    212                     os._exit(0)
    213             else:
    214                 os._exit(0)
    215     else:
    216         # Do not daemonize
    217         write_pidfile()
     206    if not (deluge.common.windows_check() or deluge.common.osx_check() or options.donot):
     207        if os.fork():
     208            # We've forked and this is now the parent process, so die!
     209            os._exit(0)
     210        os.setsid()
     211        # Do second fork
     212        if os.fork():
     213            os._exit(0)
     214
     215    # Write pid file before chuid
     216    write_pidfile()
     217
     218    if options.user:
     219        if not options.user.isdigit():
     220            import pwd
     221            options.user = pwd.getpwnam(options.user)[2]
     222        os.setuid(options.user)
     223    if options.group:
     224        if not options.group.isdigit():
     225            import grp
     226            options.group = grp.getgrnam(options.group)[2]
     227        os.setuid(options.group)
     228
     229    open_logfile()
    218230
    219231    # Setup the logger
    220232    try: