def pwd_chg(arguments): if misc.arg_range(arguments): exit(" Error! Usage: onyx-api --userpwd username old_pwd") import crypt username = arguments[0] old_pwd = arguments[1] gen_pwd = misc.pass_gen() if pwd_chk([username, old_pwd], False) is True: new_pwd = crypt.crypt(gen_pwd, crypt.mksalt(crypt.METHOD_SHA512)) print(gen_pwd) else: print(False) return False
def add(cmd): if misc.arg_range(cmd): exit(' Error! Usage: onyx-api --webadd user domain') user = cmd[0] domain = cmd[1] if misc.user_chk(user): base = '/home/' + user + '/web/' + domain # Create domain dirs if missing if not os.path.exists(base): os.makedirs(base + '/logs/') misc.copy_to('/opt/onyx/etc/web/usr/public_html/', base + '/public_html/') misc.copy_to('/opt/onyx/etc/web/usr/nginx/', base + '/nginx/') # Update user nginx template with correct values for line in fileinput.input([base + '/nginx/default.conf', base + '/nginx/default_s.conf'], inplace=True): line = line.replace('%web_port%', '80') line = line.replace('%web_ssl_port%', '443') line = line.replace('%proxy_port%', '8080') line = line.replace('%proxy_ssl_port%', '8443') line = line.replace('%user%', user) line = line.replace('%domain_idn%', domain) line = line.replace('%alias_idn%', 'www.' + domain) line = line.replace('%docroot%', base + '/public_html/') line = line.replace('%sdocroot%', base + '/public_html/') sys.stdout.write(line) # Update user html template with correct values for line in fileinput.input([base + '/public_html/50x.html', base + '/public_html/403.html', base + '/public_html/404.html', base + '/public_html/index.html'], inplace=True): line = line.replace('%domain%', domain) sys.stdout.write(line) # Set user as owner of dirs and files misc.cmd(['chown', '-R', user + ':' + user, '/home/' + user]) exit(' %s was added successfully.' % domain) else: exit(' %s already exists.' % domain)
def rmv(cmd): if misc.arg_range(cmd): exit(' Error! Usage: onyx-api --webdel user domain') user = cmd[0] domain = cmd[1] if misc.user_chk(user): path = '/home/' + user + '/web/' # Remove the domain's dir recursively if os.path.exists(path + domain): shutil.rmtree(path + domain) # Remove empty web folder if not os.listdir(path): os.rmdir(path) exit(" %s was removed." % domain) else: exit(' %s doesn\'t exist.' % domain)
def pwd_chk(arguments, verbose=True): if misc.arg_range(arguments): exit(" Error! Usage: onyx-api --userauth username password") import crypt import spwd username = arguments[0] password = arguments[1] try: shadowpwd = str(spwd.getspnam(username)[1]) i = shadowpwd.rfind("$") salt = shadowpwd[:i] password = crypt.crypt(password, salt) except KeyError: print(False) if verbose is True else None return False if password == shadowpwd: print(True) if verbose is True else None return True else: print(False) if verbose is True else None return False