You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-core/core/server/writetodb.py

82 lines
2.1 KiB

#-*- coding: utf-8 -*-
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pickle, os
import datetime
from threading import Lock
import hashlib
#write id, certificate and right in database file
def writeToDb (num, ip, mac, client_type, data_path, certbase):
CERT_FILE = data_path + '/client%d.crt' %num
import time
time.sleep(1)
#Finding Id for the current certificate
i = 1
ID_FILE = data_path + '/client_certs/id.int'
if not os.path.exists(certbase):
fp = open(certbase, 'w')
fp.close()
if os.path.exists(ID_FILE):
fi = open(ID_FILE, 'r')
temp = fi.read()
fi.close()
i = int(temp)
else:
count = 0
with open(certbase) as fd:
t = fd.read()
# See each line
for line in t.splitlines():
count += 1
count += 1
fi = open(ID_FILE, 'w')
fi.write(str(count))
fi.close()
i = count
# read client certificate in buffer
fp = open(CERT_FILE, 'r')
c = fp.read()
fp.close()
md5 = hashlib.md5()
md5.update(c)
md5sum = md5.hexdigest()
date = datetime.datetime.now()
# record
fc = open(certbase,"a")
fc.write("%d %s %s %s %s %s\n" %(i, md5sum, date, ip, mac, client_type))
fc.close()
file_name = data_path+'/client_certs/' + str(i)
fp = open(file_name, 'w')
fp.write(c)
fp.close()
# record next Id in id.int file
i += 1
fi = open(ID_FILE, 'w')
temp = str(i)
fi.write(temp)
fi.close()
return i-1