From 6173bc8e89ffef35cb7f426366b429c372aa482a Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Sat, 20 Sep 2014 15:15:16 +0200 Subject: [PATCH] dump_creds reset les mdp --- client.py | 27 ++++++++++++++++++++++++++- dump_creds.py | 34 ++++++++++++++++++++++++++++------ reset_pass.py | 19 +------------------ 3 files changed, 55 insertions(+), 25 deletions(-) mode change 100755 => 100644 client.py diff --git a/client.py b/client.py old mode 100755 new mode 100644 index 5fa1b6e..f537d0a --- a/client.py +++ b/client.py @@ -5,6 +5,10 @@ from __future__ import print_function from gestion import secrets_new as secrets import pika import json +import random +import datetime +import string +from lc_ldap import crans_utils CREDS = pika.credentials.PlainCredentials('oie', secrets.get('rabbitmq_oie'), True) PARAMS = pika.ConnectionParameters(host='rabbitmq.crans.org', @@ -14,6 +18,13 @@ rabbit_c = pika.BlockingConnection(PARAMS) ch = rabbit_c.channel() ch.queue_declare('CransTicket') +def gen_password(): + """Génère un mot de passe aléatoire""" + random.seed(datetime.datetime.now().microsecond) + chars = string.letters + string.digits + '/=+*' + length = 10 + return ''.join([random.choice(chars) for _ in xrange(length)]) + class Ticket(object): data = [] @@ -35,7 +46,21 @@ class Ticket(object): todo['type'] = 'wifi' self.add_entry(todo) + def reset_password(self, adh): + login = adh['uid'][0].value + try: + value = gen_password() + adh['userPassword'] = [crans_utils.hash_password(value).decode('ascii')] + except EnvironmentError: + print("Impossible de changer le mot de passe de %s" % login) + exit(2) + self.add_account(login, value) + def print(self): - ch.basic_publish(exchange='', routing_key='CransTicket', body=json.dumps(self.data)) + if not self.data: + print("Nothing to print !") + return + ch.basic_publish(exchange='', routing_key='CransTicket', + body=json.dumps(self.data)) print("Un nouveau ticket est en cours d'impression ...") diff --git a/dump_creds.py b/dump_creds.py index 7554ec1..474c078 100755 --- a/dump_creds.py +++ b/dump_creds.py @@ -6,17 +6,35 @@ import pika import json import sys -from lc_ldap.shortcuts import lc_ldap_readonly +from lc_ldap.shortcuts import lc_ldap_admin from affich_tools import prompt import lc_ldap.filter2 as filter from client import Ticket -ldap = lc_ldap_readonly() +ldap = lc_ldap_admin() -f = filter.human_to_ldap(sys.argv[1].decode('utf-8')) -res = ldap.search(f) -if len(res) > 1: +conf_wifi_only = True +conf_reset_password = False +conf_filter = None + +for arg in sys.argv[1:]: + if arg == '--all': + conf_wifi_only = False + elif arg == '--pass': + conf_reset_password = True + elif arg.startswith('--'): + print("Unknown arg") + exit(12) + else: + conf_filter = arg + +f = filter.human_to_ldap(conf_filter.decode('utf-8')) +res = ldap.search(f, mode='rw') +if not conf_filter: + print("Give a filter !") + exit(3) +elif len(res) > 1: print("More than one result") exit(1) elif not res: @@ -33,10 +51,14 @@ else: break ticket = Ticket() +if 'uid' in item and conf_reset_password: + ticket.reset_password(item) if hasattr(item, 'machines'): for m in item.machines(): - ticket.add_machine(m) + if not conf_wifi_only or 'machineWifi' in m['objectClass']: + ticket.add_machine(m) else: ticket.add_machine(item) + ticket.print() diff --git a/reset_pass.py b/reset_pass.py index 286240b..1ec9d94 100755 --- a/reset_pass.py +++ b/reset_pass.py @@ -2,9 +2,6 @@ # -*- coding: utf-8 -*- from __future__ import print_function -import string -import datetime -import random import sys from lc_ldap import crans_utils from lc_ldap.shortcuts import lc_ldap_admin @@ -12,13 +9,6 @@ from client import Ticket conn = lc_ldap_admin() -def gen_password(): - """Génère un mot de passe aléatoire""" - random.seed(datetime.datetime.now().microsecond) - chars = string.letters + string.digits + '/=+*' - length = 10 - return ''.join([random.choice(chars) for _ in xrange(length)]) - if len(sys.argv) != 2: print("Veuillez taper UN login") exit(1) @@ -29,13 +19,6 @@ if not adh: exit(1) adh = adh[0] -try: - value = gen_password() - adh['userPassword'] = [crans_utils.hash_password(value).decode('ascii')] -except EnvironmentError: - print("Impossible de changer le mot de passe de %s" % login) - exit(2) - ticket = Ticket() -ticket.add_account(login, value) +ticket.reset_password(adh) ticket.print()