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/gen_sid.py

209 lines
6.4 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.
13 years ago
import os, datetime
import random, pickle
import threading
13 years ago
import soaplib
from soaplib.serializers.primitive import String, Integer
from soaplib.serializers.clazz import Array
13 years ago
# session management
class CoreWsdl () :
13 years ago
# delete client session from file (close session)
13 years ago
def del_sid_from_file(self, sid):
try:
rst = []
# temp file
SID_FILE = self.sids_file
SID_FILE_T = SID_FILE + 'temp'
13 years ago
fd = open(SID_FILE, 'r')
ft = open(SID_FILE_T, 'w')
while 1:
try:
# read all on one record
13 years ago
list_sid = pickle.load(fd)
except:
break
# Leave all but removed
13 years ago
if sid != list_sid[0]:
pickle.dump(list_sid, ft)
fd.close()
ft.close()
# copy all from temp file
13 years ago
ft = open(SID_FILE_T, 'rb')
fd = open(SID_FILE, 'wb')
ft.seek(0)
fd.write(ft.read())
ft.close()
fd.close()
# delete temp file
13 years ago
os.unlink(SID_FILE_T)
return ['0']
13 years ago
except:
return ['1']
13 years ago
# find session id in file
13 years ago
def find_sid_in_file(self, sid):
SID_FILE = self.sids_file
# create, if file not exists
13 years ago
if not os.path.exists(SID_FILE):
temp = open(SID_FILE, 'w')
temp.close()
fd = open(SID_FILE, 'r')
while 1:
try:
# read all on one record
13 years ago
list_sid = pickle.load(fd)
except:
break
# if session id found
13 years ago
if sid == list_sid[0]:
fd.close()
return 1
fd.close()
return 0
# add session id in file
13 years ago
def add_sid_in_file(self, sid, cert_id, lang):
# list Format (session number, cert number, time start session)
13 years ago
list_sid = [sid, cert_id, datetime.datetime.now()]
# session's file
if not os.path.exists(self.sids):
os.system('mkdir %s' %self.sids)
SIDS_DIR = self.sids
sid_file = SIDS_DIR+"/%d.sid" %sid
13 years ago
# create session's file
13 years ago
fp = open(sid_file, 'w')
13 years ago
sid_list = [sid, 0, 0, lang]
13 years ago
pickle.dump(sid_list,fp)
fp.close()
# add session in list sessions
fd = open(self.sids_file, 'a')
13 years ago
pickle.dump(list_sid,fd)
fd.close()
return 0
def set_sid_lang(self, sid, lang):
SIDS_DIR = self.sids
sid_file = SIDS_DIR+"/%d.sid" %sid
if not os.path.isfile(sid_file):
print 'sid file not found'
return 1
fd = open(sid_file, 'r')
list_sid = pickle.load(fd)
fd.close()
fp = open(sid_file, 'w')
list_sid[3] = lang
pickle.dump(list_sid,fp)
fp.close()
# issue number of new session (and registered its)
13 years ago
def sid_cmp (self, sid, cert_id, lang):
if sid < 0 or sid > self.max_sid:
13 years ago
sid = 0
session = 1
flag = 1
# if session is new
13 years ago
if sid == 0:
while flag == 1:
# generation number
new_sid = random.randint(1, self.max_sid)
# Check there is already the number
13 years ago
flag = self.find_sid_in_file(sid)
# add session id
13 years ago
self.add_sid_in_file(new_sid, cert_id, lang)
13 years ago
sid = new_sid
# if session is old
13 years ago
else:
# find number in file registered
13 years ago
flag = self.find_sid_in_file(sid)
# if not registered
13 years ago
if flag == 0:
# add session id in file
13 years ago
self.add_sid_in_file(sid, cert_id, lang)
13 years ago
else:
self.set_sid_lang(sid, lang)
# set - old session
13 years ago
session = 0
# session id and flad (new or old) session
13 years ago
result = []
result.append(sid)
result.append(session)
return result
def serv_sid_info(self, sid):
""" Get information about sid """
cert_id = 0
results = []
SID_FILE = self.sids_file
fd = open(SID_FILE, 'r')
while 1:
try:
# read all on one record
list_sid = pickle.load(fd)
except:
break
# if sid found
if sid == list_sid[0]:
cert_id = list_sid[1]
fd.close()
# Get information about sid
if cert_id == 0:
return ["-1"]
with open(self.certbase) as fd:
t = fd.read()
# See each line
for line in t.splitlines():
# and each word in line
words = line.split()
# if in line present certificate id
if words[0] == str(cert_id):
# certificate id
results.append(words[0])
# Date issue certificate
results.append(words[2]+' '+words[3])
# ip
results.append(words[4])
# mac
results.append(words[5])
# client type
results.append(words[6])
if not os.path.exists(self.sids):
os.makedirs(self.sids)
sid_path = self.sids + "/%d.sid"%sid
with open(sid_path) as fs:
# read info about session
sid_inf = pickle.load(fs)
# flag absence client
results.append(str(sid_inf[2]))
return results
fd.close()
return ["-2"]