File: //bin/license-manager
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# wrapper for License Manager.
#
# Copyright (c) 2017 BigCloud Enterprise Linux, Inc.
#
# Authors: Rongyin<rongyin@cmss.chinamobile.com>
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# BigCloud Enterprise Linux trademarks are not licensed under GPLv2.
# No permission is granted to use or replicate trademarks that are
# incorporated in this software or its documentation.
#
if __name__ != '__main__':
raise ImportError("module cannot be imported")
import syslog
import sys
_LIBPATH = "/usr/lib64/python3.6/site-packages/"
if _LIBPATH not in sys.path:
sys.path.append(_LIBPATH)
from M2Crypto import RSA
#from Cryptodome.PublicKey import RSA
from datetime import datetime
from dateutil.relativedelta import relativedelta
from os import path
import gettext
_LIBPATH = "/usr/lib64/python3.6/site-packages/"
if _LIBPATH not in sys.path:
sys.path.append(_LIBPATH)
from license_manager.rsa import rsa_decrypt
from license_manager.validity import *
_FILE = r'/etc/bclinux/license'
pubkey_path="/etc/bclinux/bc_pub.key"
pub_key = RSA.load_pub_key(pubkey_path)
t = gettext.translation("demo", "/usr/share/locale", fallback=True)
_=t.gettext
wrong_license_warning = _("*** INFO ***\
Your license is invalid. \
Please register to BigCloud Enterprise Linux(BC-Linux) to get the right license. \
You can use the command 'bclinux-license -g' to get the system UUID, then send to us to complete the registration.")
expired_warning = _("*** INFO ***\
Your license is already expired. \
You no longer have access to the BigCloud Enterprise Linux(BC-Linux) repositories. \
It is important that you apply an active registration in order to resume access to the repositories. \
You can renew the expired license to extend the system service life and resume access to the BC-Linux repositories.")
ten_days_warning = _("*** INFO ***\
Your license will be expired in %s days. \
You can renew the license to extend the system service life and resume access to the BC-Linux repositories.")
def fail_validation(message):
# syslog.openlog("license_check",syslog.LOG_PID)
syslog.syslog(message)
syslog.closelog()
def isBase64(data):
if (len(data) % 4 != 0):
fail_validation(wrong_license_warning)
print (wrong_license_warning)
sys.exit()
else:
speStr = '+/=-_'
for char in data:
if((char >= 'a' and char <='z') or (char >= 'A' and char <='Z') or (char >= '0' and char <= '9') or speStr.find(char) != -1) and data[0] != '=':
continue
else:
fail_validation(wrong_license_warning)
print (wrong_license_warning)
sys.exit()
def read_license():
"""
This function reads the license code from file '/etc/bclinux/license' generated
by BigCloud Enterprise Linux.
There are no parameteres for this function. It uses global variable 'data' to
communicate with other functions.
"""
global data
if path.isfile(_FILE):
file_object = open(_FILE)
try:
data = file_object.read().strip('\n')
except:
fail_validation(wrong_license_warning)
file_object.close()
print (wrong_license_warning)
sys.exit()
finally:
file_object.close()
else:
fail_validation(wrong_license_warning)
print (wrong_license_warning)
sys.exit()
def check_expired():
"""
This function validatas the license information, including the license period
and display warning for expired license.
"""
try:
text = rsa_decrypt(pub_key, data)
text = text.decode()
except:
fail_validation(wrong_license_warning)
print (wrong_license_warning)
sys.exit()
time = text.split(',')[2]
sdata = time.split('-')[0]
period = int(time.split('-')[1])
license_year = int(sdata[:4])
license_month = int(sdata[4:6])
license_day = int(sdata[6:])
expired_time = datetime(license_year, license_month, license_day) + \
relativedelta(days=+period)
system_time = datetime.now()
delta_last = (expired_time - system_time).days
delta_begin = (datetime(license_year, license_month, license_day) - system_time).days
if delta_last < -1 or delta_begin >= 0:
fail_validation(expired_warning)
print (expired_warning)
sys.exit()
elif delta_last >= -1 and delta_last < 10:
delta_last = delta_last+1
fail_validation(_("Your license will be expired in %s days. You can renew the license to extend the system service life and resume access to the BC-Linux repositories.") %(delta_last))
print (_("Your license will be expired in %s days. You can renew the license to extend the system service life and resume access to the BC-Linux repositories.") %(delta_last))
else:
print (_("Your license is ok!"))
pass
if __name__ == "__main__":
# read license information
read_license()
isBase64(data)
# validate the machine code matches
if verify_activation_code(pub_key,data):
pass
else:
fail_validation(wrong_license_warning)
print (wrong_license_warning)
sys.exit()
# validate the license life
check_expired()