D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
custom-ns
/
Filename :
fix_reseller.py
back
Copy
"""Reseller functions for custom-ns""" import re import rads from blessings import Terminal from cpapis import whmapi1, CpAPIError TERM = Terminal() def is_reseller(user: str) -> bool: """Check if a user is a reseller""" return user in whmapi1('listresellers')['data']['reseller'] def fix_reseller(user): """Ensure a user is set up as a reseller with appropiate permissions for managing custom nameservers""" print('Checking if selected user is a reseller ...', end=' ') if not is_reseller(user): if rads.prompt_y_n('no. Make this user a reseller?'): if add_perms(user): # if perms added successfully set_acl(user) else: print('yes') set_acl(user) def set_reseller_nameservers(reseller, domain, ns1_name, ns2_name): """Set reseller's default nameservers with cPanel json-api""" try: whmapi1( 'setresellernameservers', { 'user': reseller, 'nameservers': f'{ns1_name}.{domain},{ns2_name}.{domain}', }, ) except CpAPIError as err: print( TERM.bold_red(f'Failed to set reseller nameservers for {reseller}') ) print(err) try: ns_re = re.compile(r'NS[0-9]?\s') with open('/etc/wwwacct.conf', encoding='utf-8') as wacct_file: wacct_data = wacct_file.read().splitlines() wacct_data = [x for x in wacct_data if ns_re.match(x) is None] wacct_data.append(f'NS {ns1_name}.{domain}') wacct_data.append(f'NS2 {ns2_name}.{domain}') with open('/etc/wwwacct.conf', 'w', encoding='utf-8') as wacct_file: wacct_file.write('%s\n' % '\n'.join(wacct_data)) except OSError: print( TERM.bold_red( 'Could not edit NS and NS2 lines in /etc/wwwacct.conf' ) ) def set_acl(user): """Set reseller ACL to one of our generic ones""" print("Setting reseller's ACL...", end=' ') try: whmapi1( 'setacls', { 'reseller': user, 'acl-acct-summary': True, 'acl-basic-system-info': True, 'acl-basic-whm-functions': True, 'acl-connected-applications': True, 'acl-cors-proxy-get': True, 'acl-cpanel-api': True, 'acl-cpanel-integration': True, 'acl-create-user-session': True, 'acl-digest-auth': True, 'acl-generate-email-config': True, 'acl-list-pkgs': True, 'acl-manage-api-tokens': True, 'acl-manage-dns-records': True, 'acl-add-pkg': True, 'acl-add-pkg-ip': True, 'acl-manage-oidc': True, 'acl-manage-styles': True, 'acl-mysql-info': True, 'acl-ns-config': True, 'acl-public-contact': True, 'acl-ssl-info': True, 'acl-track-email': True, 'acl-add-pkg-shell': True, 'acl-allow-addoncreate': True, 'acl-allow-parkedcreate': True, 'acl-allow-emaillimits-pkgs': True, 'acl-allow-unlimited-bw-pkgs': True, 'acl-allow-unlimited-disk-pkgs': True, 'acl-allow-unlimited-pkgs': True, 'acl-create-acct': True, 'acl-create-dns': True, 'acl-demo-setup': True, 'acl-edit-account': True, 'acl-edit-dns': True, # not in standard ACL 'acl-edit-mx': True, 'acl-edit-pkg': True, 'acl-kill-acct': True, 'acl-kill-dns': True, # not in standard ACL 'acl-limit-bandwidth': True, 'acl-list-accts': True, 'acl-locale-edit': True, 'acl-mailcheck': True, 'acl-news': True, 'acl-park-dns': True, # not in standard ACL 'acl-passwd': True, 'acl-quota': True, 'acl-rearrange-accts': True, 'acl-res-cart': True, 'acl-resftp': True, 'acl-restart': True, 'acl-show-bandwidth': True, 'acl-ssl': True, 'acl-ssl-gencrt': True, 'acl-stats': True, 'acl-status': True, 'acl-suspend-acct': True, 'acl-thirdparty': True, 'acl-upgrade-account': True, 'acl-viewglobalpackages': True, }, check=True, ) except CpAPIError as err: print(TERM.bold_red('FAILED')) print(err) def add_perms(user): """call add_reseller_perms and print to stdout""" print('Adding reseller permissions...', end=' ') try: whmapi1.setupreseller(user) print('done') return True except CpAPIError: print(TERM.bold_red('FAILED')) return False