etingof / snmpsim

SNMP Simulator
http://snmplabs.com/snmpsim/
BSD 2-Clause "Simplified" License
376 stars 119 forks source link

Permissions problems running snmpsim as root #164

Open e107steved opened 2 years ago

e107steved commented 2 years ago

Environment: Debian Buster (on Raspberry Pi) Running snmpsim as root (so that I can attach to port 161) using sudo, so created a non-privileged user to run the listener. Any directory or file created (in /tmp) at startup is given root-only permissions; so access attempts by the non-privileged user fail. The workaround is to open another console after starting snmpsim, to firstly extend the permissions on /tmp/snmpsim to 'all', and secondly to delete the root-created data files so that they are recreated by the non-privileged user. (For the second case, likely that extending permissions on the files would also work). I've made some really nasty additions to the code to deal with this for my use case: a) in snmpsimd.py, around line 835: if not os.path.exists(confdir.cache): try: os.makedirs(confdir.cache)

Added - change permissions of temporary directory

    os.chmod(confdir.cache, 0o777)
except OSError:
    log.error('failed to create cache directory "%s": %s' % (confdir.cache, sys.exc_info()[1]))
    sys.exit(-1)
else:
    log.info('Cache directory "%s" created' % confdir.cache)

b) In snmpsim\record\search\data.py (around line 158) I've extended permissions: log.msg('...%d entries indexed' % lineNo)

Added - change permissions of temporary directory

        ## - but bodge because '.db' appended to the file name we store
        dbFileName = self.__dbFile + '.db'
        log.msg("Change permissions on %s" % (dbFileName))
        os.chmod(dbFileName, 0o777)

A 'proper' solution would, I suggest, need to just add permissions to the non-privileged user, or implement some other solution (which might involve telling me what I've done wrong!)