From d627da4e476f500f1d40bbfa9b3911f3efac37d0 Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Sat, 30 Aug 2014 17:51:07 +0200 Subject: [PATCH] reset mdp --- client.py | 35 +++++++++++++++++++++++++++++++++++ dump_creds.py | 41 +++++++++++++++++------------------------ reset_pass.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 24 deletions(-) create mode 100755 client.py create mode 100755 reset_pass.py diff --git a/client.py b/client.py new file mode 100755 index 0000000..570afcf --- /dev/null +++ b/client.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- + +from __future__ import print_function + +import pika +import json +import config + +rabbit_c = pika.BlockingConnection(config.PARAMS) +ch = rabbit_c.channel() +ch.queue_declare('CransTicket') + +class Ticket(object): + + data = [] + + def add_entry(self, x): + self.data.append(x) + + def add_account(self, login, mdp): + self.add_entry({'login': login, 'pass': mdp}) + + def add_machine(self, machine): + login = machine['host'][0].split('.', 1)[0] + todo = {'login': login, 'macAddress': machine['macAddress'][0].value, + 'type': 'fil'} + if machine.has_key('ipsec'): + todo['secret'] = machine['ipsec'][0].value + todo['type'] = 'wifi' + self.add_entry(todo) + + def print(self): + 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 a61b659..4627ade 100755 --- a/dump_creds.py +++ b/dump_creds.py @@ -1,5 +1,7 @@ #!/bin/bash /usr/scripts/python.sh +from __future__ import print_function + import pika import json import sys @@ -7,25 +9,21 @@ import sys from lc_ldap.shortcuts import lc_ldap_readonly from affich_tools import prompt import lc_ldap.filter2 as filter -import config -rabbit_c = pika.BlockingConnection(config.PARAMS) -ch = rabbit_c.channel() -ch.queue_declare('CransTicket') +from client import Ticket ldap = lc_ldap_readonly() - f = filter.human_to_ldap(sys.argv[1].decode('utf-8')) -adh = ldap.search(f) -if len(adh) > 1: - print "More than one result" +res = ldap.search(f) +if len(res) > 1: + print("More than one result") exit() -elif not adh: - print "Nobody" +elif not res: + print("Nobody found") else: - adh = adh[0] - adh.display() + item = res[0] + item.display() while True: c = prompt("[O/N]").lower() if c == 'n': @@ -33,16 +31,11 @@ else: elif c == 'o': break -m = adh.machines() +ticket = Ticket() +if hasattr(item, 'machines'): + for m in item.machines(): + ticket.add_machine(m) +else: + ticket.add_machine(item) +ticket.print() -to_print = [] -for machine in m: - if u'' in machine['macAddress']: - continue - login = machine['host'][0].split('.', 1)[0] - todo = {'login': login, 'macAddress': machine['macAddress'][0].value} - if machine.has_key('ipsec'): - todo['secret'] = machine['ipsec'][0].value - to_print.append(todo) - -ch.basic_publish(exchange='', routing_key='CransTicket', body=json.dumps(to_print)) diff --git a/reset_pass.py b/reset_pass.py new file mode 100755 index 0000000..286240b --- /dev/null +++ b/reset_pass.py @@ -0,0 +1,41 @@ +#!/bin/bash /usr/scripts/python.sh +# -*- 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 +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) +login = sys.argv[1] +adh = conn.search(u'uid=%s' % crans_utils.escape(login), mode='rw') +if not adh: + print("Adhérent introuvable") + 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.print()