Ejemplo n.º 1
0
    def __load_bdoc(self, elid):
        voter_key = htscommon.get_user_key(self._voter_code)
        sreg = Election().get_sub_reg(elid)
        for votefile in sreg.list_keys(voter_key):
            if htscommon.VALID_VOTE_PATTERN.match(votefile):
                bdoc = bdocpythonutils.BDocContainer()
                bdoc.load(sreg.path(voter_key + [votefile]))
                bdoc.validate(bdocpythonutils.ManifestProfile("TM"))

                self._voter = htscommon.get_votefile_voter(votefile)
                break

        if not bdoc:
            evlog.log_error("No valid BDOC found for voter %s using vote ID %s" % \
                    (self._voter, self._vote_id))
            raise HTSVerifyException, evcommon.VERIFY_ERROR

        return bdoc
Ejemplo n.º 2
0
    def __load_bdoc(self, elid):
        voter_key = htscommon.get_user_key(self._voter_code)
        sreg = Election().get_sub_reg(elid)
        vote_files = []
        for vfile in sreg.list_keys(voter_key):
            if htscommon.VALID_VOTE_PATTERN.match(vfile):
                vote_files.append(vfile)

        vote_files.sort()
        latest = vote_files.pop()
        if latest:
            bdoc = htsbase.get_vote(sreg.path(voter_key + [latest]))

        if not bdoc:
            evlog.log_error("No valid BDOC found for voter %s using vote ID %s" % \
                    (self._voter_code, self._vote_id))
            raise HTSVerifyException, evcommon.VERIFY_ERROR

        return bdoc
Ejemplo n.º 3
0
    def __load_bdoc(self, elid):
        voter_key = htscommon.get_user_key(self._voter_code)
        sreg = Election().get_sub_reg(elid)
        vote_files = []
        for vfile in sreg.list_keys(voter_key):
            if htscommon.VALID_VOTE_PATTERN.match(vfile):
                vote_files.append(vfile)

        vote_files.sort()
        latest = vote_files.pop()
        if latest:
            bdoc = htsbase.get_vote(sreg.path(voter_key + [latest]))

        if not bdoc:
            evlog.log_error("No valid BDOC found for voter %s using vote ID %s" % \
                    (self._voter_code, self._vote_id))
            raise HTSVerifyException, evcommon.VERIFY_ERROR

        return bdoc
Ejemplo n.º 4
0
    def work(self, check_fn, purge_fn):
        reg = Election().get_root_reg()
        runtime = int(time.time())
        AppLog().log("Purging %ss as of %s: START" % (self._desc,
                time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(runtime))))

        try:
            if not reg.check(self._key):
                return
            for key in reg.list_keys(self._key):
                try:
                    if check_fn(reg, key, runtime):
                        AppLog().log("Purging %s %s" % (self._desc, key))
                        purge_fn(reg, key)
                except:
                    AppLog().log("Error processing %s %s" % (self._desc, key))
                    AppLog().log_exception()
        except:
            AppLog().log_exception()
        finally:
            AppLog().log("Purging %ss: DONE" % self._desc)
Ejemplo n.º 5
0
    def work(self, check_fn, purge_fn):
        reg = Election().get_root_reg()
        runtime = int(time.time())
        AppLog().log(
            "Purging %ss as of %s: START" %
            (self._desc,
             time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(runtime))))

        try:
            if not reg.check(self._key):
                return
            for key in reg.list_keys(self._key):
                try:
                    if check_fn(reg, key, runtime):
                        AppLog().log("Purging %s %s" % (self._desc, key))
                        purge_fn(reg, key)
                except:
                    AppLog().log("Error processing %s %s" % (self._desc, key))
                    AppLog().log_exception()
        except:
            AppLog().log_exception()
        finally:
            AppLog().log("Purging %ss: DONE" % self._desc)
Ejemplo n.º 6
0
class HTSBase:

    def __init__(self, elid):
        self._elid = elid
        self._errmsg = None
        self._reg = Election().get_sub_reg(self._elid)

        self._log1 = evlog.Logger()
        self._log1.set_format(evlog.EvLogFormat())
        self._log1.set_logs(self._reg.path(['common', 'log1']))

        self._log2 = evlog.Logger()
        self._log2.set_format(evlog.EvLogFormat())
        self._log2.set_logs(self._reg.path(['common', 'log2']))

        self._log3 = evlog.Logger()
        self._log3.set_format(evlog.EvLogFormat())
        self._log3.set_logs(self._reg.path(['common', 'log3']))

        self._revlog = evlog.Logger()
        self._revlog.set_format(evlog.RevLogFormat())
        self._revlog.set_logs(self._reg.path(['common', evcommon.REVLOG_FILE]))

    def _haaletanud_konf(self, isikukood):
        user_key = htscommon.get_user_key(isikukood)
        return self._reg.check(user_key) and \
                len(self._reg.list_keys(user_key)) > 0

    def haaletanud(self, isikukood):
        logi = self._log1.contains(isikukood)
        konf = self._haaletanud_konf(isikukood)
        if logi and konf:
            return True
        if (not logi) and (not konf):
            return False
        self._errmsg = 'Logifail LOG1 ja serveri olek ei ole kooskõlalised'
        raise Exception(self._errmsg)
Ejemplo n.º 7
0
class MobileIDContext:

    phoneno = None
    lang = None
    challenge = None
    midsess = None
    origvote = None
    votefiles = {}
    __sessid = None
    __reg = None

    def __init__(self, sessid):
        if sessid is None:
            raise Exception('Puuduv sessiooniidentifikaator')
        self.__sessid = sessid
        self.__reg = Election().get_root_reg()
        self.lang = 'EST'

    def sessid(self):
        return self.__sessid

    def kill(self):
        self.__reg.ensure_no_key([evcommon.MIDSPOOL, self.__sessid])

    def set_phone(self, phone):
        self.phoneno = phone

    def set_origvote(self, hv):
        self.origvote = hv

    def get_origvote(self):
        self.origvote = self.__reg.read_value(
            [evcommon.MIDSPOOL, self.__sessid], 'origvote').value
        return self.origvote

    def add_votefile(self, filename, data):
        self.votefiles[filename] = data

    def get_votefiles(self):
        for key in self.__reg.list_keys(
            [evcommon.MIDSPOOL, self.__sessid, 'votefiles']):
            self.votefiles[key] = self.__reg.read_value(
                [evcommon.MIDSPOOL, self.__sessid, 'votefiles'], key).value
        return self.votefiles

    def generate_challenge(self):
        self.challenge = binascii.b2a_hex(os.urandom(10))

    def verify_challenge(self, signature):
        return challenge_ok(self.certificate(), self.mychallenge(),
                            self.ourchallenge(), signature)

    def mychallenge(self):
        return self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid],
                                     'mychallenge').value

    def ourchallenge(self):
        return self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid],
                                     'ourchallenge').value

    def certificate(self):
        return self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid],
                                     'cert').value

    def set_auth_succ(self):
        self.__reg.ensure_key([evcommon.MIDSPOOL, self.__sessid, 'authsucc'])

    def auth_succ(self):
        return self.__reg.check([evcommon.MIDSPOOL, self.__sessid, 'authsucc'])

    def check_session(self):
        if not self.__reg.check([evcommon.MIDSPOOL, self.__sessid]):
            return False
        start = self.__reg.read_integer_value(
            [evcommon.MIDSPOOL, self.__sessid], 'start').value
        length = Election().get_session_length() * 60
        return start + length >= int(time.time()) and self.auth_succ()

    def save_post_auth(self, rsp):

        self.__reg.reset_key([evcommon.MIDSPOOL, self.__sessid])
        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], 'cert',
                                rsp._CertificateData)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], 'phone',
                                self.phoneno)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], 'midsess',
                                rsp._Sesscode)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid],
                                'mychallenge', self.challenge)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid],
                                'ourchallenge', rsp._Challenge)

        self.__reg.create_integer_value([evcommon.MIDSPOOL, self.__sessid],
                                        'start', int(time.time()))

    def load_pre_sign(self):
        self.phoneno = self.__reg.read_value(
            [evcommon.MIDSPOOL, self.__sessid], 'phone').value

    def save_post_sign(self, midsess):
        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], 'midsess',
                                midsess)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], 'origvote',
                                self.origvote)

        self.__reg.ensure_key([evcommon.MIDSPOOL, self.__sessid, 'votefiles'])
        for el in self.votefiles:
            self.__reg.create_value(
                [evcommon.MIDSPOOL, self.__sessid, 'votefiles'], el,
                self.votefiles[el])

    def load_pre_poll(self):
        self.midsess = int(
            self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid],
                                  'midsess').value)
Ejemplo n.º 8
0
class Rights:
    def __init__(self, elid):
        self.reg = Election().get_sub_reg(elid, ['common', 'rights'])

    def descr(self, code):
        """
        Tagastab tegelase kohta käiva kirjelduse
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        if self.reg.check([code, 'description']):
            return self.reg.read_string_value([code], 'description').value

        return 'Andmed puuduvad'

    def listall(self):
        """
        Tagastab kõik volitused
        """
        lst = self.reg.list_keys()

        ret = ''
        for ele in lst:
            ret += '\n' + self.listuser(ele)
        return ret.strip()

    def _create_user(self, code):
        """     Loome kasutaja, kui teda veel pole
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        self.reg.ensure_key([code, 'rights'])

    def add(self, code, right):
        """
        Lisab uue volituse
        """
        new_right = right.upper()
        if not _proper_right(new_right):
            raise Exception('Vigane volitus')

        self._create_user(code)

        if not self.has(code, new_right):
            self.reg.create_value([code, 'rights'], new_right, '')
            return True

        return False

    def adddesc(self, code, desc):
        """
        Lisab kasutajale kirjelduse
        """
        self._create_user(code)
        self.reg.create_value([code], 'description', desc)
        return True

    def remove(self, code, right):
        """
        Võtab kasutajalt volituse
        """

        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        new_right = right.upper()
        if not _proper_right(new_right):
            raise Exception('Vigane volitus')

        if self.has(code, new_right):
            self.reg.delete_value([code, 'rights'], new_right)
            return True

        return False

    def remuser(self, code):
        """
        Eemaldab ühe kasutaja volituste failist
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        return self.reg.ensure_no_key([code])

    def remall(self):
        """
        Eemaldab kõik volitused
        """
        self.reg.reset_key([''])
        return True

    def has(self, code, right):
        """
        Kas koodil on vastav volitus
        """

        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        new_right = right.upper()
        if not _proper_right(new_right):
            raise Exception('Vigane volitus')

        if not self.reg.check([code, 'rights', new_right]):
            return False
        return True

    def listuser(self, code):
        """
        Ainult konkreetse kasutaja õigused
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        ret = ''
        if self.reg.check([code]):
            ret += code + ' (%s)' % self.descr(code)
            sub_list = self.reg.list_keys([code, 'rights'])
            if len(sub_list) > 0:
                for _s in sub_list:
                    ret += '\n\t' + G_DESCS[_s]
            else:
                ret += '\n\tVolitused puuduvad'
        return ret.strip()
Ejemplo n.º 9
0
class MobileIDContext:

    phoneno = None
    lang = None
    challenge = None
    midsess = None
    origvote = None
    votefiles = {}
    __sessid = None
    __reg = None

    def __init__(self, sessid):
        if sessid == None:
            raise Exception('Puuduv sessiooniidentifikaator')
        self.__sessid = sessid
        self.__reg = Election().get_root_reg()
        self.lang = 'EST'

    def sessid(self):
        return self.__sessid

    def kill(self):
        self.__reg.ensure_no_key([evcommon.MIDSPOOL, self.__sessid])

    def set_phone(self, phone):
        self.phoneno = phone

    def set_origvote(self, hv):
        self.origvote = hv

    def get_origvote(self):
        self.origvote = self.__reg.read_value(\
                            [evcommon.MIDSPOOL, self.__sessid], \
                                                    'origvote').value
        return self.origvote

    def add_votefile(self, filename, data):
        self.votefiles[filename] = data

    def get_votefiles(self):
        for key in self.__reg.list_keys([evcommon.MIDSPOOL, self.__sessid, \
                'votefiles']):
            self.votefiles[key] = self.__reg.read_value(\
                    [evcommon.MIDSPOOL, self.__sessid, 'votefiles'], key).value
        return self.votefiles

    def generate_challenge(self):
        self.challenge = binascii.b2a_hex(os.urandom(10))

    def verify_challenge(self, signature):
        return challenge_ok(self.certificate(), self.mychallenge(), \
                            self.ourchallenge(), signature)

    def mychallenge(self):
        return self.__reg.read_value(\
                            [evcommon.MIDSPOOL, self.__sessid], \
                                    'mychallenge').value

    def ourchallenge(self):
        return self.__reg.read_value(\
                            [evcommon.MIDSPOOL, self.__sessid], \
                                    'ourchallenge').value

    def certificate(self):
        return self.__reg.read_value(\
                            [evcommon.MIDSPOOL, self.__sessid], \
                                    'cert').value

    def set_auth_succ(self):
        self.__reg.ensure_key([evcommon.MIDSPOOL, self.__sessid, 'authsucc'])

    def auth_succ(self):
        return self.__reg.check(\
                [evcommon.MIDSPOOL, self.__sessid, 'authsucc'])

    def save_post_auth(self, rsp):

        self.__reg.reset_key([evcommon.MIDSPOOL, self.__sessid])
        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], \
                        'cert', rsp._CertificateData)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], \
                        'phone', self.phoneno)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], \
                        'midsess', rsp._Sesscode)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], \
                        'mychallenge', self.challenge)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], \
                        'ourchallenge', rsp._Challenge)

    def load_pre_sign(self):
        self.phoneno = self.__reg.read_value(\
                [evcommon.MIDSPOOL, self.__sessid], 'phone').value

    def save_post_sign(self, midsess):
        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], \
                        'midsess', midsess)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], \
                        'origvote', self.origvote)

        self.__reg.ensure_key([evcommon.MIDSPOOL, self.__sessid, 'votefiles'])
        for el in self.votefiles:
            self.__reg.create_value(\
                    [evcommon.MIDSPOOL, self.__sessid, 'votefiles'],\
                    el, self.votefiles[el])

    def load_pre_poll(self):
        self.midsess = int(self.__reg.read_value(\
                [evcommon.MIDSPOOL, self.__sessid], 'midsess').value)
Ejemplo n.º 10
0
class HTSBase:
    def __init__(self, elid):
        self._elid = elid
        self._errmsg = None
        self._reg = Election().get_sub_reg(self._elid)

        self._revlog = evlog.Logger()
        self._revlog.set_format(evlog.RevLogFormat())
        self._revlog.set_logs(self._reg.path(['common', evcommon.REVLOG_FILE]))

    def haaletanud(self, isikukood):
        latest = self.get_latest_vote(isikukood)
        return latest <> None

    def get_revoked_path(self, pc):
        user_key = htscommon.get_user_key(pc)
        return self._reg.path(user_key + ['reason'])

    def get_latest_vote(self, pc):
        user_key = htscommon.get_user_key(pc)
        if self._reg.check(user_key):
            files = self._reg.list_keys(user_key)
            votes = []
            for el in files:
                if htscommon.VALID_VOTE_PATTERN.match(el):
                    votes.append(el)
            votes.sort()
            latest = votes.pop()
            if latest:
                return htscommon.get_votefile_time(latest)

        return None

    def restore_vote(self, revline, operator):
        timest = self.get_latest_vote(revline[0])
        os.unlink(self.get_revoked_path(revline[0]))
        self._revlog.log_info(tegevus='ennistamine',
                              isikukood=revline[0],
                              nimi=revline[1],
                              timestamp=timest,
                              operaator=operator,
                              pohjus=revline[2])

    def revoke_vote(self, revline, operator):
        timest = self.get_latest_vote(revline[0])
        nowstr = time.strftime("%Y%m%d%H%M%S", time.localtime())
        self._reg.create_string_value(\
            htscommon.get_user_key(revline[0]), 'reason', \
            "%s\t%s" % (nowstr, revline[2]))

        self._revlog.log_info(tegevus='tühistamine',
                              testtime=nowstr,
                              isikukood=revline[0],
                              nimi=revline[1],
                              timestamp=timest,
                              operaator=operator,
                              pohjus=revline[2])

    def is_user_revoked(self, pc):
        user_key = htscommon.get_user_key(pc)
        if self._reg.check(user_key + ['reason']):
            line = self._reg.read_string_value(user_key, 'reason').value
            data = line.split('\t')
            return True, data[1], data[0]
        return False, '', ''

    def save_log(self, lines, log):
        fn = self._reg.path(['common', 'log%s' % log])
        lf = htscommon.LoggedFile(fn)
        lf.open('w')
        lf.write(evcommon.VERSION + "\n")
        lf.write(self._elid + "\n")
        lf.write("%s\n" % log)
        for el in lines:
            lf.write(el + '\n')
        lf.close()

    def save_revocation_report(self, report):
        fn = self._reg.path(['hts', 'output', evcommon.REVREPORT_FILE])
        outfile = htscommon.LoggedFile(fn)
        outfile.open('a')
        for el in report:
            outfile.write("\t".join(el) + "\n")

        outfile.close()

    def talletaja(self, ik):
        vl = None
        try:
            vl = inputlists.VotersList('hts', self._reg)
            if not vl.has_voter(ik):
                return None
            ret = vl.get_voter(ik)
            return ret
        finally:
            if vl != None:
                vl.close()
                vl = None
Ejemplo n.º 11
0
class Rights:

    def __init__(self, elid):
        self.reg = Election().get_sub_reg(elid, ['common', 'rights'])

    def descr(self, code):
        """
        Tagastab tegelase kohta käiva kirjelduse
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        if self.reg.check([code, 'description']):
            return self.reg.read_string_value([code], 'description').value
        else:
            return 'Andmed puuduvad'

    def listall(self):
        """
        Tagastab kõik volitused
        """
        lst = self.reg.list_keys()

        ret = ''
        for ele in lst:
            ret = ret + '\n' + self.listuser(ele)
        return ret.strip()

    def _create_user(self, code):
        """     Loome kasutaja, kui teda veel pole
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        self.reg.ensure_key([code, 'rights'])

    def add(self, code, right):
        """
        Lisab uue volituse
        """
        new_right = right.upper()
        if not _proper_right(new_right):
            raise Exception('Vigane volitus')

        self._create_user(code)

        if not self.has(code, new_right):
            self.reg.create_value([code, 'rights'], new_right, '')
            return True

        return False

    def adddesc(self, code, desc):
        """
        Lisab kasutajale kirjelduse
        """
        self._create_user(code)
        self.reg.create_value([code], 'description', desc)
        return True

    def remove(self, code, right):
        """
        Võtab kasutajalt volituse
        """

        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        new_right = right.upper()
        if not _proper_right(new_right):
            raise Exception('Vigane volitus')

        if self.has(code, new_right):
            self.reg.delete_value([code, 'rights'], new_right)
            return True

        return False

    def remuser(self, code):
        """
        Eemaldab ühe kasutaja volituste failist
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        return self.reg.ensure_no_key([code])

    def remall(self):
        """
        Eemaldab kõik volitused
        """
        self.reg.reset_key([''])
        return True

    def has(self, code, right):
        """
        Kas koodil on vastav volitus
        """

        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        new_right = right.upper()
        if not _proper_right(new_right):
            raise Exception('Vigane volitus')

        if not self.reg.check([code, 'rights', new_right]):
            return False
        return True

    def listuser(self, code):
        """
        Ainult konkreetse kasutaja õigused
        """
        if not formatutil.is_isikukood(code):
            raise Exception('Vigane isikukood')

        ret = ''
        if self.reg.check([code]):
            ret = ret + code
            ret = ret + ' (' + self.descr(code) + ')'
            sub_list = self.reg.list_keys([code, 'rights'])
            if len(sub_list) > 0:
                for _s in sub_list:
                    ret = ret + '\n\t' + G_DESCS[_s]
            else:
                ret = ret + '\n\tVolitused puuduvad'
        return ret.strip()
Ejemplo n.º 12
0
class HTSBase:

    def __init__(self, elid):
        self._elid = elid
        self._errmsg = None
        self._reg = Election().get_sub_reg(self._elid)

        self._revlog = evlog.Logger()
        self._revlog.set_format(evlog.RevLogFormat())
        self._revlog.set_logs(self._reg.path(['common', evcommon.REVLOG_FILE]))

    def haaletanud(self, isikukood):
        latest = self.get_latest_vote(isikukood)
        return latest is not None

    def get_revoked_path(self, pc):
        user_key = htscommon.get_user_key(pc)
        return self._reg.path(user_key + ['reason'])

    def get_latest_vote(self, pc):
        user_key = htscommon.get_user_key(pc)
        if self._reg.check(user_key):
            files = self._reg.list_keys(user_key)
            votes = []
            for el in files:
                if htscommon.VALID_VOTE_PATTERN.match(el):
                    votes.append(el)
            votes.sort()
            latest = votes.pop()
            if latest:
                return htscommon.get_votefile_time(latest)

        return None

    def restore_vote(self, revline, operator):
        timest = self.get_latest_vote(revline[0])
        os.unlink(self.get_revoked_path(revline[0]))
        self._revlog.log_info(
            tegevus='ennistamine',
            isikukood=revline[0],
            nimi=revline[1],
            timestamp=timest,
            operaator=operator,
            pohjus=revline[2])

    def revoke_vote(self, revline, operator):
        timest = self.get_latest_vote(revline[0])
        nowstr = time.strftime("%Y%m%d%H%M%S", time.localtime())
        self._reg.create_string_value(
            htscommon.get_user_key(revline[0]), 'reason',
            "%s\t%s" % (nowstr, revline[2]))

        self._revlog.log_info(
            tegevus='tühistamine',
            testtime=nowstr,
            isikukood=revline[0],
            nimi=revline[1],
            timestamp=timest,
            operaator=operator,
            pohjus=revline[2])

    def is_user_revoked(self, pc):
        user_key = htscommon.get_user_key(pc)
        if self._reg.check(user_key + ['reason']):
            line = self._reg.read_string_value(user_key, 'reason').value
            data = line.split('\t')
            return True, data[1], data[0]
        return False, '', ''

    def save_log(self, lines, log):
        fn = self._reg.path(['common', 'log%s' % log])
        lf = htscommon.LoggedFile(fn)
        lf.open('w')
        lf.write(evcommon.VERSION + "\n")
        lf.write(self._elid + "\n")
        lf.write("%s\n" % log)
        for el in lines:
            lf.write(el + '\n')
        lf.close()

    def save_revocation_report(self, report):
        fn = self._reg.path(['hts', 'output', evcommon.REVREPORT_FILE])
        outfile = htscommon.LoggedFile(fn)
        outfile.open('a')
        for el in report:
            outfile.write("\t".join(el) + "\n")

        outfile.close()

    def talletaja(self, ik):
        vl = None
        try:
            vl = inputlists.VotersList('hts', self._reg)
            if not vl.has_voter(ik):
                return None
            ret = vl.get_voter(ik)
            return ret
        finally:
            if vl is not None:
                vl.close()
                vl = None
Ejemplo n.º 13
0
class MobileIDContext:

    phoneno = None
    lang = None
    challenge = None
    midsess = None
    origvote = None
    votefiles = {}
    __sessid = None
    __reg = None

    def __init__(self, sessid):
        if sessid is None:
            raise Exception("Puuduv sessiooniidentifikaator")
        self.__sessid = sessid
        self.__reg = Election().get_root_reg()
        self.lang = "EST"

    def sessid(self):
        return self.__sessid

    def kill(self):
        self.__reg.ensure_no_key([evcommon.MIDSPOOL, self.__sessid])

    def set_phone(self, phone):
        self.phoneno = phone

    def set_origvote(self, hv):
        self.origvote = hv

    def get_origvote(self):
        self.origvote = self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid], "origvote").value
        return self.origvote

    def add_votefile(self, filename, data):
        self.votefiles[filename] = data

    def get_votefiles(self):
        for key in self.__reg.list_keys([evcommon.MIDSPOOL, self.__sessid, "votefiles"]):
            self.votefiles[key] = self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid, "votefiles"], key).value
        return self.votefiles

    def generate_challenge(self):
        self.challenge = binascii.b2a_hex(os.urandom(10))

    def verify_challenge(self, signature):
        return challenge_ok(self.certificate(), self.mychallenge(), self.ourchallenge(), signature)

    def mychallenge(self):
        return self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid], "mychallenge").value

    def ourchallenge(self):
        return self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid], "ourchallenge").value

    def certificate(self):
        return self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid], "cert").value

    def set_auth_succ(self):
        self.__reg.ensure_key([evcommon.MIDSPOOL, self.__sessid, "authsucc"])

    def auth_succ(self):
        return self.__reg.check([evcommon.MIDSPOOL, self.__sessid, "authsucc"])

    def check_session(self):
        if not self.__reg.check([evcommon.MIDSPOOL, self.__sessid]):
            return False
        start = self.__reg.read_integer_value([evcommon.MIDSPOOL, self.__sessid], "start").value
        length = Election().get_session_length() * 60
        return start + length >= int(time.time()) and self.auth_succ()

    def save_post_auth(self, rsp):

        self.__reg.reset_key([evcommon.MIDSPOOL, self.__sessid])
        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], "cert", rsp._CertificateData)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], "phone", self.phoneno)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], "midsess", rsp._Sesscode)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], "mychallenge", self.challenge)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], "ourchallenge", rsp._Challenge)

        self.__reg.create_integer_value([evcommon.MIDSPOOL, self.__sessid], "start", int(time.time()))

    def load_pre_sign(self):
        self.phoneno = self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid], "phone").value

    def save_post_sign(self, midsess):
        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], "midsess", midsess)

        self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid], "origvote", self.origvote)

        self.__reg.ensure_key([evcommon.MIDSPOOL, self.__sessid, "votefiles"])
        for el in self.votefiles:
            self.__reg.create_value([evcommon.MIDSPOOL, self.__sessid, "votefiles"], el, self.votefiles[el])

    def load_pre_poll(self):
        self.midsess = int(self.__reg.read_value([evcommon.MIDSPOOL, self.__sessid], "midsess").value)
Ejemplo n.º 14
0
def main_function():

    try:

        if Election().is_hes():
            server = 'hes'
        elif Election().is_hts():
            server = 'hts'
        else:
            raise Exception('Vigane serveri tüüp')

        election_ids = Election().get_questions()

        if len(election_ids) == 0:
            print "Valijanimekirju pole laaditud"
        else:
            election_ids.sort()
            for el_i in election_ids:
                print "%s" % el_i
                reg_i = Election().get_sub_reg(el_i, [server])
                if not reg_i.check(['voters_files']):
                    print "\tValijanimekirju pole laaditud"
                    continue

                file_list = reg_i.list_keys(['voters_files'])
                file_list.sort()
                if len(file_list) == 0:
                    print "\tValijanimekirju pole laaditud"
                else:
                    prefix_len = 8 + 1 + 14 + 1
                    j = 1
                    added_total = 0
                    for vf_i in file_list:
                        file_date = vf_i[0:4] + "." + vf_i[4:6] + \
                            "." + vf_i[6:8] + " " + vf_i[8:10] + \
                            ":" + vf_i[10:12] + ":" + vf_i[12:14]
                        file_name = vf_i[15:]
                        if len(file_name) > prefix_len:
                            file_name = file_name[prefix_len:]
                        print "\t%02d. %s - %s" % (j, file_date, file_name)
                        j += 1

                        # nimekirja kokkulugemine
                        _rf = file(reg_i.path(['voters_files', vf_i]), "r")
                        line = _rf.readline()      # päise 1. rida
                        line = _rf.readline()      # päise 2. rida
                        filetype = _rf.readline()  # päise 3. rida
                        added = 0
                        removed = 0
                        while 1:
                            line = _rf.readline()
                            if len(line) == 0:
                                break
                            fields = line.split("\t")
                            if fields[2] == "lisamine":
                                added += 1
                                added_total += 1
                            else:
                                removed += 1
                                added_total -= 1
                        print \
                            "\t    Faili tüüp: %s\n" % filetype.strip() + \
                            "\t    Lisamisi %d, " % added + \
                                  "eemaldamisi %d\n" % removed + \
                            "\t    Kokku peale laadimist: %d" % added_total

    except Exception, ex:
        print 'Viga valijanimekirjade uuenduste ajaloo kuvamisel: ' + str(ex)
        sys.exit(1)