Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 89c016a02d
Fetching contributors…

Cannot retrieve contributors at this time

308 lines (274 sloc) 8.047 kB
# -*- coding: utf-8 -*-
class userInfo( object ):
'''
userInfo class to get and set info or set last matched otp
'''
def __init__( self, config, userinfostore ):
self.config = config
self.log = self.config.log
self.valid = True
self.username = userinfostore.get_username()
self.eusername = userinfostore.get_eusername()
try:
from pwd import getpwnam
self.userid = getpwnam( self.username ).pw_uid
except:
import traceback
self.log.debug( 'There is no userid! Traceback: %s' % (
str( traceback.format_exc() ) ), 1 )
self.userid = None
self.valid = False
try:
from grp import getgrnam
self.groupid = getgrnam( self.username ).gr_gid
except:
import traceback
self.log.debug( 'There is no groupid! Traceback: %s' % (
str( traceback.format_exc() ) ), 1 )
self.groupid = None
self.valid = False
import os
try:
cdir = self.config.get( 'homedir', 'dir' )
except:
cdir = os.path.join( '.config', 'motpy' )
try:
self.motp_dir = os.path.join( getpwnam( self.username ).pw_dir, cdir )
self.lastotp_file = os.path.join( self.motp_dir, 'lastotp.pickle' )
self.db_file = os.path.join( self.motp_dir, 'motpy.pickle' )
except:
import traceback
self.log.debug( 'There is no userdata dir! Traceback: %s' % (
str( traceback.format_exc() ) ), 1 )
self.motp_dir = None
self.lastopt_file = None
self.db_file = None
self.valid = False
def isValidDbForUser( self ):
'''
Return true if there is a motp set already
'''
import os
if not self.valid:
return False
if os.access( self.db_file, os.R_OK ):
return True
return False
def checkPath( self, path ):
'''
Check if path exist and has right ownership and rights
'''
import os
ret = True
#
# Check exists
# #############
if not os.access( path, os.R_OK ):
self.log.log( "ERR: %s path doesn't exist or not readable" % path )
return False
#
# Check mode
# ###########
fstat = os.lstat( path )
if fstat.st_mode != 16832 and fstat.st_mode != 33152:
self.log.log( 'ERR: %s path has invalid mode' % path )
ret = False
#
# Check owner
# ############
from pwd import getpwnam
if fstat.st_uid != getpwnam( self.username )[2]:
self.log.log( 'ERR: %s path has invalid ownership' % path )
ret = False
return ret
def getAllLastOtps( self ):
'''
Return the list of saved Otps or None if something went wrong
'''
if not self.valid:
return None
#
# Pre checkings - path
# #####################
if not self.checkPath( self.motp_dir ) or \
not self.checkPath( self.lastotp_file ):
return None
import cPickle
try:
flastotp = open( self.lastotp_file, 'rb' )
picklelastotp = dict( cPickle.load( flastotp ) )
flastotp.close()
except:
import traceback
self.log.log( 'ERR: Something went wrong with lastotp: %s' % str( traceback.format_exc() ) )
return None
self.log.debug( 'DEBUG: picklelastotp: [[%s]]' % str( picklelastotp ), 10 )
return dict( picklelastotp )
def getLastOtps( self ):
'''
Get the last valid otps for the user
'''
lastotps = self.getAllLastOtps()
if not lastotps:
return None
try:
ret = list( lastotps[self.eusername] )
except:
ret = list()
return ret
def getAllInfo( self ):
'''
get all info dict
'''
#
# Pre checkings - path
# #####################
if not self.valid:
return None
if not self.checkPath( self.motp_dir ) or not self.checkPath( self.db_file ):
return None
import cPickle
try:
fuserinfo = open( self.db_file, 'rb' )
pickleuserinfo = dict( cPickle.load( fuserinfo ) )
fuserinfo.close()
except:
import traceback
self.log.log( 'ERR: Something went wrong with userinfo db: %s' % str( traceback.format_exc() ) )
return None
return pickleuserinfo
def getInfo( self ):
'''
get all info dict
'''
try:
userinfo = dict( self.getAllInfo()[self.eusername] )
except:
return dict( pin = None,
secret = None,
offset = None )
return userinfo
def setLastOtp( self, otp ):
'''
store the matched otp that it can't be used next
'''
lastotp = self.getAllLastOtps()
self.log.debug( 'DEBUG: before lastotp: [[%s]] otp: <%s>' % ( str( lastotp ),
str( otp ) ), 10 )
if not self.valid:
return None
if not lastotp:
lastotp = dict()
if not lastotp.has_key( self.eusername ):
lastotp[self.eusername] = list()
lastotp[self.eusername].insert( 0, otp )
lastotp[self.eusername] = lastotp[self.eusername][:10]
self.log.debug( 'DEBUG: after lastotp: [[%s]]' % str( lastotp ), 10 )
import cPickle
try:
flastotp = open( self.lastotp_file, 'wb' )
cPickle.dump( lastotp, flastotp )
flastotp.close()
except:
import traceback
self.log.log( "ERR: Can't store lastotps in file: %s" % str( traceback.format_exc() ) )
return None
return True
def setUserInfo( self, pin, secret = None, offset = None ):
'''
set the userinfo
'''
import os
if not self.valid:
return False
#
# Check or create motp config dir
# ################################
if not os.access( self.motp_dir, os.R_OK ):
try:
os.makedirs( self.motp_dir, 0700 )
os.chown( self.motp_dir, self.userid, self.groupid )
except:
import traceback
self.log.log( "ERR: Something went wrong with %s path creating! Traceback: %s" % (
self.motp_dir, str( traceback.format_exc() ) ) )
return False
#
# Check or create lastotp database
# #################################
if not os.access( self.lastotp_file, os.R_OK ):
import cPickle
try:
flastotp = open( self.lastotp_file, 'wb' )
cPickle.dump( list(), flastotp )
flastotp.close()
except:
import traceback
self.log.log( "ERR: Can't inicialise lastotps %s file: %s" % (
self.lastotp_file, str( traceback.format_exc() ) ) )
return False
try:
os.chmod( self.lastotp_file, 0600 )
os.chown( self.lastotp_file, self.userid, self.groupid )
except:
import traceback
self.log.log( "ERR: Something went wrong with %s lastotp inicialization! Traceback: %s" % (
self.lastotp_file, str( traceback.format_exc() ) ) )
return False
#
# Check or create motp database
# ###############################
if not os.access( self.db_file, os.R_OK ):
import cPickle
try:
fdb = open( self.db_file, 'wb' )
cPickle.dump( dict(), fdb )
fdb.close()
except:
import traceback
self.log.log( "ERR: Can't inicialise motp db %s file: %s" % (
self.db_file, str( traceback.format_exc() ) ) )
return False
try:
os.chmod( self.db_file, 0600 )
os.chown( self.db_file, self.userid, self.groupid )
except:
import traceback
self.log.log( "ERR: Something went wrong with %s motp db inicialization! Traceback: %s" % (
self.db_file, str( traceback.format_exc() ) ) )
return False
#
# Load existing motp database
# ############################
try:
alluserinfo = dict( self.getAllInfo() )
except:
return False
if not alluserinfo.has_key( self.eusername ):
alluserinfo[self.eusername] = dict( offset = 0 )
try:
if not secret:
secret = alluserinfo[self.eusername]['secret']
except:
self.log.log( "ERR: No secret is set for the account!" )
return False
if not offset:
offset = alluserinfo[self.eusername]['offset']
#
# Replace with new motp database
# ###############################
alluserinfo[self.eusername] = dict( pin = pin,
secret = secret,
offset = offset )
import cPickle
try:
fdb = open( self.db_file, 'wb' )
cPickle.dump( alluserinfo, fdb )
fdb.close()
except:
import traceback
self.log.log( "ERR: Can't store motp data in %s file: %s" % (
self.db_file,
str( traceback.format_exc() ) ) )
return False
return True
Jump to Line
Something went wrong with that request. Please try again.