def check(data_file, key): import bdocpython hexhash = ksum.compute(data_file) bhash = binascii.a2b_hex(hexhash) sig_file = None for ext in EXTENSIONS: tmp = "%s%s" % (data_file, ext) if _has(tmp): sig_file = tmp break if sig_file is None: return False, "Ei leia signatuuri faili: %s" % data_file with open(sig_file, "r") as f: sig = f.read() sv = bdocpython.SignatureVerifier() sv.setPubkey(key) sv.setSignature(sig) sv.setHash(bhash) res = sv.isSignatureOk() if not res: return False, sv.error return True, None
def delete_question(self, elid): sub = self.get_sub_reg(elid) key = [self.get_server_str(), evcommon.VOTERS_FILES] if sub.check(key): for vf in sub.list_keys(key): self.delete_voters_file_hash(ksum.compute(sub.path(key + [vf]))) self.reg.delete_key(['questions', elid]) if self.count_questions() == 0: self.init_conf_done(False)
def process_candidate_files(reg): if not reg.check([evcommon.CANDIDATE_FILES]): print "\n\tValikute nimekirja pole laaditud" return file_list = sorted(reg.list_keys([evcommon.CANDIDATE_FILES])) if len(file_list) == 0: print "\n\tValikute nimekirja pole laaditud" else: print "\n\tValikute nimekiri" j = 1 for ff_i in file_list: file_date, file_name = date_name(ff_i) print "\t%02d. %s - %s" % (j, file_date, file_name) dgst = ksum.compute(reg.path([evcommon.CANDIDATE_FILES, ff_i])) print_dgst(dgst.upper()) j += 1
def process_district_files(reg): if not reg.check([evcommon.DISTRICT_FILES]): print "\tJaoskondade nimekirja pole laaditud" return file_list = sorted(reg.list_keys([evcommon.DISTRICT_FILES])) if len(file_list) == 0: print "\tJaoskondade nimekirja pole laaditud" else: print "\tJaoskondade nimekiri" j = 1 for ff_i in file_list: file_date, file_name = date_name(ff_i) print "\t%02d. %s - %s" % (j, file_date, file_name) dgst = ksum.compute(reg.path([evcommon.DISTRICT_FILES, ff_i])) print_dgst(dgst.upper()) j += 1
def copy_voters_file(self, elid, server, voters_file): voters_files = "voters_files" _r = self.get_sub_reg(elid, [server]) _r.ensure_key([voters_files]) time_str = time.strftime("%Y%m%d%H%M%S") copy_voters_file = _r.path([voters_files, time_str + "_" + os.path.basename(voters_file)]) shutil.copyfile(voters_file, copy_voters_file) voters_file_sha1 = ksum.compute(voters_file) voters_file_hashes = ["common", "voters_file_hashes"] self.reg.ensure_key(voters_file_hashes) self.reg.create_string_value(voters_file_hashes, voters_file_sha1, "") voters_files_sha1 = ksum.compute_voters_files_sha1(self.reg.path(voters_file_hashes)) self.reg.create_string_value(["common"], "voters_files_sha1", voters_files_sha1)
def copy_voters_file(self, elid, server, voters_file): voters_files = 'voters_files' _r = self.get_sub_reg(elid, [server]) _r.ensure_key([voters_files]) time_str = time.strftime("%Y%m%d%H%M%S") copy_voters_file = _r.path([voters_files, time_str + '_' + \ os.path.basename(voters_file)]) shutil.copyfile(voters_file, copy_voters_file) voters_file_sha1 = ksum.compute(voters_file) voters_file_hashes = ['common', 'voters_file_hashes'] self.reg.ensure_key(voters_file_hashes) self.reg.create_string_value(voters_file_hashes, voters_file_sha1, '') voters_files_sha1 = \ ksum.compute_voters_files_sha1(self.reg.path(voters_file_hashes)) self.reg.create_string_value(['common'], \ 'voters_files_sha1', voters_files_sha1)
def copy_voters_file(self, elid, server, voters_file): voters_files = 'voters_files' _r = self.get_sub_reg(elid, [server]) _r.ensure_key([voters_files]) time_str = time.strftime("%Y%m%d%H%M%S") copy_voters_file = _r.path([voters_files, time_str + '_' + \ os.path.basename(voters_file)]) shutil.copyfile(voters_file, copy_voters_file) voters_file_sha256 = ksum.compute(voters_file) voters_file_hashes = ['common', 'voters_file_hashes'] self.reg.ensure_key(voters_file_hashes) self.reg.create_string_value(voters_file_hashes, voters_file_sha256, '') voters_files_sha256 = \ ksum.compute_voters_files_sha256(self.reg.path(voters_file_hashes)) self.reg.create_string_value(['common'], \ 'voters_files_sha256', voters_files_sha256)
def process_voters_files(reg): if not reg.check([evcommon.VOTERS_FILES]): print "\n\tValijanimekirju pole laaditud" return file_list = sorted(reg.list_keys([evcommon.VOTERS_FILES])) if len(file_list) == 0: print "\n\tValijanimekirju pole laaditud" else: print "\n\tValijanimekirjad" j = 1 added_total = 0 for ff_i in file_list: file_date, file_name = date_name(ff_i) print "\t%02d. %s - %s" % (j, file_date, file_name) dgst = ksum.compute(reg.path([evcommon.VOTERS_FILES, ff_i])) print_dgst(dgst.upper()) j += 1 # nimekirja kokkulugemine _rf = file(reg.path([evcommon.VOTERS_FILES, ff_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 True: 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 %s: " % filetype.strip().capitalize() + \ "lisamisi %d, " % added + "eemaldamisi %d\n" % removed + \ "\t Kokku peale laadimist: %d\n" % added_total
def add_voters_file_hash(self, voters_file): voters_file_hash = ksum.compute(voters_file) voters_file_hashes = ['common', 'voters_file_hashes'] self.reg.ensure_key(voters_file_hashes) self.reg.create_string_value(voters_file_hashes, voters_file_hash, '') self.recalculate_voters_files_hash()
def apply_changes(elid, voter_f): """Muudatuste rakendamine""" vl = None tokend = {} def check_state(): if not ElectionState().can_apply_changes(): sys.stderr.write( "Selles hääletuse faasis (%s) pole võimalik " "nimekirju uuendada\n" % ElectionState().str() ) sys.exit(1) try: buflog = None if Election().is_hes(): root = "hes" elif Election().is_hts(): root = "hts" else: raise Exception("Vigane serveritüüp") buflog = BufferedLog(Election().get_path(evcommon.VOTER_LIST_LOG_FILE), "APPLY-CHANGES", elid) check_state() reg = Election().get_sub_reg(elid) ed = inputlists.Districts() ed.load(root, reg) vl = inputlists.VotersList(root, reg, ed) vl.attach_elid(elid) vl.ignore_errors() evlog.AppLog().set_app("APPLY-CHANGES") vl.attach_logger(evlog.AppLog()) print "Kontrollin valijate faili terviklikkust" checkResult = sigverify.check_existent(voter_f, elid) if not checkResult[0]: raise Exception("Kontrollimisel tekkis viga: %s\n" % checkResult[1]) else: print "Valijate faili terviklikkus OK" voters_file_sha256 = ksum.compute(voter_f) if Election().get_root_reg().check(["common", "voters_file_hashes", voters_file_sha256]): raise Exception("Kontrollsummaga %s fail on juba laaditud\n" % voters_file_sha256) if not vl.check_format(voter_f, "Kontrollin valijate nimekirja: "): print "Valijate nimekiri ei vasta vormingunõuetele" sys.exit(1) else: print "Valijate nimekiri OK" vl.attach_logger(buflog) if not vl.check_muudatus("Kontrollin muudatuste kooskõlalisust: ", ElectionState().election_on(), tokend): print "Sisend ei ole kooskõlas olemasoleva konfiguratsiooniga" else: print "Muudatuste kooskõlalisus OK" _apply = 1 if not buflog.empty(): print "Muudatuste sisselaadimisel esines vigu" buflog.output_errors() _apply = uiutil.ask_yes_no("Kas rakendan kooskõlalised muudatused?") if not _apply: buflog.log_error("Muudatusi ei rakendatud") print "Muudatusi ei rakendatud" else: if ElectionState().election_on() and root == "hts": create_tokend_file(tokend, reg, elid) a_count, d_count = vl.create("Paigaldan valijaid: ") print "Teostasin %d lisamist ja %d eemaldamist" % (a_count, d_count) Election().copy_config_file(elid, root, voter_f, evcommon.VOTERS_FILES) Election().add_voters_file_hash(voter_f) print "Muudatuste rakendamine lõppes edukalt" except SystemExit: sys.stderr.write("Viga muudatuste laadimisel\n") if buflog: buflog.output_errors() raise except Exception as ex: sys.stderr.write("Viga muudatuste laadimisel: " + str(ex) + "\n") if buflog: buflog.output_errors() sys.exit(1) finally: if vl is not None: vl.close()
def apply_changes(elid, voter_f): """Muudatuste rakendamine""" vl = None tokend = {} def check_state(): if not ElectionState().can_apply_changes(): sys.stderr.write('Selles hääletuse faasis (%s) pole võimalik '\ 'nimekirju uuendada\n' \ % ElectionState().str()) sys.exit(1) try: buflog = None if Election().is_hes(): root = 'hes' elif Election().is_hts(): root = 'hts' else: raise Exception('Vigane serveritüüp') buflog = BufferedLog(Election(). \ get_path(evcommon.VOTER_LIST_LOG_FILE), \ 'APPLY-CHANGES', elid) check_state() reg = Election().get_sub_reg(elid) ed = inputlists.Districts() ed.load(root, reg) vl = inputlists.VotersList(root, reg, ed) vl.attach_elid(elid) vl.ignore_errors() evlog.AppLog().set_app('APPLY-CHANGES') vl.attach_logger(evlog.AppLog()) print "Kontrollin valijate faili kontrollsummat" if not ksum.check(voter_f): raise Exception('Valijate faili kontrollsumma ei klapi\n') voters_file_sha256 = ksum.compute(voter_f) if Election().get_root_reg().check(\ ['common', 'voters_file_hashes', voters_file_sha256]): raise Exception('Kontrollsummaga %s fail on juba laetud\n' \ % voters_file_sha256) if not vl.check_format(voter_f, 'Kontrollin valijate nimekirja: '): print "Valijate nimekiri ei vasta vormingunõuetele" sys.exit(1) else: print 'Valijate nimekiri OK' vl.attach_logger(buflog) if not vl.check_muudatus( \ 'Kontrollin muudatuste kooskõlalisust: ', \ ElectionState().election_on(), tokend): print "Sisend ei ole kooskõlas olemasoleva konfiguratsiooniga" else: print "Muudatuste kooskõlalisus OK" _apply = 1 if not buflog.empty(): print 'Muudatuste sisselaadimisel esines vigu' buflog.output_errors() _apply = uiutil.ask_yes_no(\ 'Kas rakendan kooskõlalised muudatused?') if not _apply: buflog.log_error('Muudatusi ei rakendatud') print 'Muudatusi ei rakendatud' else: if ElectionState().election_on() and root == 'hts': create_tokend_file(tokend, reg, elid) a_count, d_count = vl.create('Paigaldan valijaid: ') print 'Teostasin %d lisamist ja %d eemaldamist' \ % (a_count, d_count) Election().copy_voters_file(elid, root, voter_f) print 'Muudatuste rakendamine lõppes edukalt' except SystemExit: sys.stderr.write('Viga muudatuste laadimisel\n') if buflog: buflog.output_errors() raise except Exception, ex: sys.stderr.write('Viga muudatuste laadimisel: ' + str(ex) + '\n') if buflog: buflog.output_errors() sys.exit(1)
def apply_changes(elid, voter_f): """Muudatuste rakendamine""" vl = None tokend = {} def check_state(): if not ElectionState().can_apply_changes(): sys.stderr.write('Selles hääletuse faasis (%s) pole võimalik ' 'nimekirju uuendada\n' % ElectionState().str()) sys.exit(1) try: buflog = None if Election().is_hes(): root = 'hes' elif Election().is_hts(): root = 'hts' else: raise Exception('Vigane serveritüüp') buflog = BufferedLog(Election(). get_path(evcommon.VOTER_LIST_LOG_FILE), 'APPLY-CHANGES', elid) check_state() reg = Election().get_sub_reg(elid) ed = inputlists.Districts() ed.load(root, reg) vl = inputlists.VotersList(root, reg, ed) vl.attach_elid(elid) vl.ignore_errors() evlog.AppLog().set_app('APPLY-CHANGES') vl.attach_logger(evlog.AppLog()) print "Kontrollin valijate faili terviklikkust" checkResult = sigverify.check_existent(voter_f, elid) if not checkResult[0]: raise Exception('Kontrollimisel tekkis viga: %s\n' % checkResult[1]) else: print "Valijate faili terviklikkus OK" voters_file_sha256 = ksum.compute(voter_f) if Election().get_root_reg().check( ['common', 'voters_file_hashes', voters_file_sha256]): raise Exception('Kontrollsummaga %s fail on juba laaditud\n' % voters_file_sha256) if not vl.check_format(voter_f, 'Kontrollin valijate nimekirja: '): print "Valijate nimekiri ei vasta vormingunõuetele" sys.exit(1) else: print 'Valijate nimekiri OK' vl.attach_logger(buflog) if not vl.check_muudatus( 'Kontrollin muudatuste kooskõlalisust: ', ElectionState().election_on(), tokend): print "Sisend ei ole kooskõlas olemasoleva konfiguratsiooniga" else: print "Muudatuste kooskõlalisus OK" _apply = 1 if not buflog.empty(): print 'Muudatuste sisselaadimisel esines vigu' buflog.output_errors() _apply = uiutil.ask_yes_no( 'Kas rakendan kooskõlalised muudatused?') if not _apply: buflog.log_error('Muudatusi ei rakendatud') print 'Muudatusi ei rakendatud' else: if ElectionState().election_on() and root == 'hts': create_tokend_file(tokend, reg, elid) a_count, d_count = vl.create('Paigaldan valijaid: ') print 'Teostasin %d lisamist ja %d eemaldamist' \ % (a_count, d_count) Election().copy_config_file( elid, root, voter_f, evcommon.VOTERS_FILES) Election().add_voters_file_hash(voter_f) print 'Muudatuste rakendamine lõppes edukalt' except SystemExit: sys.stderr.write('Viga muudatuste laadimisel\n') if buflog: buflog.output_errors() raise except Exception as ex: sys.stderr.write('Viga muudatuste laadimisel: ' + str(ex) + '\n') if buflog: buflog.output_errors() sys.exit(1) finally: if vl is not None: vl.close()