commit 3ebcad0d204a51a087b6cfcd1006b7716ba149f4 Author: Steve Date: Tue Nov 10 15:57:02 2020 -0500 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..060476e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +ldap_cred.py diff --git a/ldap_cred.py.sample b/ldap_cred.py.sample new file mode 100644 index 0000000..68dc304 --- /dev/null +++ b/ldap_cred.py.sample @@ -0,0 +1,7 @@ +ldap_cred = dict( + ldap_url = "ldap.example.org", + ldap_user = "uid=minecraft,ou=services,dc=example,dc=org", + ldap_pass = "Password!", + ldap_search_base = ",ou=accounts,dc=example,dc=org", + ldap_filter = "(objectclass=account)" +) diff --git a/whitelist.json b/whitelist.json new file mode 100644 index 0000000..3eddd8f --- /dev/null +++ b/whitelist.json @@ -0,0 +1,10 @@ +[ + { + "uuid": "f430dbb6-5d9a-444e-b542-e47329b2c5a0", + "name": "username" + }, + { + "uuid": "e5aa0f99-2727-4a11-981f-dded8b1cd032", + "name": "username2" + } +] diff --git a/whitelist_gen.py b/whitelist_gen.py new file mode 100755 index 0000000..504d361 --- /dev/null +++ b/whitelist_gen.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +import json +import requests +import argparse +from ldap3 import Server, Connection, ALL +from ldap_cred import ldap_cred + + +class Person(dict): + + def __init__(self, username, uuid): + dict.__init__(self, name=username, uuid=uuid) + + +def lookupLDAPUsers(): + server = Server(ldap_cred['ldap_url'], get_info=ALL) + conn = Connection(server, ldap_cred['ldap_user'], ldap_cred['ldap_pass'], + auto_bind=True) + check = conn.search(ldap_cred['ldap_search_base'], + ldap_cred['ldap_filter'], attributes=['uid']) + if check is False: + print("Error performing LDAP search") + return [] + res = [] + for i in conn.entries: + res.append(str(i['uid'])) + return res + + +def lookupMCPlayer(username): + url = "https://playerdb.co/api/player/minecraft/" + username + response = requests.get(url) + if (response.status_code != 200): + print("Error looking up {}, reponse code {}" + .format(username, response.status_code)) + return None + uid = response.json()["data"]['player']["id"] + return Person(username, uid) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--filename", + help="Whitelist file location," + + "defaults to ./whitelist.json", + type=str) + args = parser.parse_args() + filename = args.filename + if filename is None: + filename = "whitelist.json" + + print("Loading current whitelist") + with open(filename) as json_file: + players = json.load(json_file) + + player_usernames = [] + for p in players: + player_usernames.append(p['name']) + + print("Loading players from LDAP") + ldap_players = lookupLDAPUsers() + for p in ldap_players: + if p not in player_usernames: + m = lookupMCPlayer(p) + if m is not None: + print("Added new player {}".format(p)) + players.append(m) + else: + print("Could not add new player {}".format(p)) + + print("Writing player whitelist to file") + with open(filename, 'w') as outfile: + json.dump(players, outfile, indent=4) + + +main()