#!/usr/bin/python
|
|
|
|
import requests
|
|
import json
|
|
import sys
|
|
import hashlib
|
|
import datetime
|
|
import re
|
|
import collections
|
|
import ldap
|
|
import os
|
|
|
|
|
|
|
|
# set as part of the config
|
|
SECRET_KEY = 'xxxxxx'
|
|
|
|
# or set directly on the app
|
|
|
|
LDAP_IP = "192.168.X.X"
|
|
LDAP_PORT = "7389"
|
|
LDAP_USER = "uid=ldapsearch,cn=users,dc=example,dc=local"
|
|
LDAP_PASSWORD = "XXXXXXXXXX"
|
|
|
|
prot = 'http'
|
|
host = 'starface.example.local'
|
|
user = '0011'
|
|
pw = 'xxxxxxx'
|
|
|
|
|
|
if (len(sys.argv) > 1):
|
|
host = sys.argv[1]
|
|
if (len(sys.argv) > 2):
|
|
user = sys.argv[2]
|
|
if (len(sys.argv) > 3):
|
|
pw = sys.argv[3]
|
|
if (len(sys.argv) > 4):
|
|
prot=sys.argv[4]
|
|
url = prot + '://' + host + '/rest/login'
|
|
|
|
templateResponse = requests.get(url, headers={'Content-Type':'application/json', 'X-Version':'2'}, verify=False)
|
|
templateJson = json.loads(templateResponse.content)
|
|
userandnonce=(user+templateJson['nonce']).encode(encoding='utf_8', errors='strict')
|
|
hpassword=hashlib.sha512(pw.encode(encoding='utf_8', errors='strict')).hexdigest()
|
|
passwordHashed=hpassword.encode(encoding='utf_8')
|
|
hsecret = hashlib.sha512(userandnonce+passwordHashed).hexdigest().encode(encoding='utf_8')
|
|
secretCompound=user+':'+hsecret.decode(encoding='utf_8')
|
|
templateJson['secret'] = secretCompound
|
|
authTokenResponse = requests.post(url, data=json.dumps(templateJson), headers={'Content-Type':'application/json', 'X-Version':'2'}, verify=False)
|
|
authtoken = json.loads(authTokenResponse.content)['token']
|
|
url = 'http://' + host + '/rest/accounts'
|
|
templateResponse = requests.get(url, headers={'Content-Type':'application/json', 'X-Version':'2', 'authToken':authtoken})
|
|
templateJson = json.loads(templateResponse.content)
|
|
|
|
|
|
def ldap_connection_init():
|
|
global connect
|
|
try:
|
|
connect = ldap.initialize('ldap://{}:{}'.format(LDAP_IP, LDAP_PORT))
|
|
connect.set_option(ldap.OPT_REFERRALS, 0)
|
|
connect.simple_bind_s(LDAP_USER, LDAP_PASSWORD)
|
|
except Exception as error_msg:
|
|
return error_msg
|
|
return connect
|
|
|
|
def html_decode(string):
|
|
string = string.decode('utf-8')
|
|
return string
|
|
|
|
def update(dic, upd):
|
|
|
|
# input dict and dixct update.
|
|
# update dict with new users and sort it
|
|
|
|
for share, users in upd.items():
|
|
if isinstance(users, collections.Mapping):
|
|
for key, value in dic.get(share, {}).items():
|
|
if key == next(iter(users)):
|
|
users[next(iter(users))].extend(value)
|
|
users[next(iter(users))] = list(set(users[next(iter(users))]))
|
|
users[next(iter(users))].sort()
|
|
dic[share] = update(dic.get(share, {}), users)
|
|
else:
|
|
dic[share] = users
|
|
return dic
|
|
|
|
def get_table_uid_usernames():
|
|
|
|
ldap_querry = connect.search_s(
|
|
'cn=users,dc=example,dc=local',
|
|
ldap.SCOPE_SUBTREE,
|
|
'objectClass=organizationalPerson',
|
|
['uid', 'displayName', 'shadowExpire'])
|
|
output = {}
|
|
for key, value in ldap_querry:
|
|
display_name = value.get('displayName', '')
|
|
display_name = html_decode(display_name[0])
|
|
uid = value.get('uid', '')
|
|
uid = html_decode(uid[0])
|
|
shadow_expire = value.get('shadowExpire', '')
|
|
dic = {uid: display_name}
|
|
if not shadow_expire and not display_name == 'none':
|
|
output.update(dic)
|
|
return output
|
|
|
|
ldap_connection_init()
|
|
users_names = get_table_uid_usernames()
|
|
|
|
for username, fullname in users_names.items():
|
|
for i in range(len(templateJson)):
|
|
pbx_name = templateJson[i]['firstname'] + ' ' + templateJson[i]['lastname']
|
|
if fullname == pbx_name:
|
|
username = username.encode("utf-8")
|
|
tel = templateJson[i]['primaryInternalPhoneNumber'].encode("utf-8")
|
|
cmd = "/usr/sbin/udm users/user modify --dn uid={},cn=users,dc=example,dc=local --set phone={}".format(username,tel)
|
|
os.system(cmd)
|