Re: Automatiser la définition d'un mot de passe

Pàgina inicial

Reply to this message
Autor: Jerome KIEFFER
Data:  
A: guilde
Assumpte: Re: Automatiser la définition d'un mot de passe
On Wed, 9 Jun 2004 16:08:18 +0200
Yves Martin <ymartin59@???> wrote:

Genere un mot de passe, le change dans le système, et l'envoie par mail
à l'admin (executé par cron)...
car contre c'est "mal" de bidouiller shadow

#!/usr/bin/python

import string,whrandom,smtplib,authlib,os,tempfile,shutil


shadow="/etc/shadow"
tmpdir="/tmp"
user="monpetituser"
fromadd="root@???"
toadd="admint@???"



def generatePassword(minlen=7, maxlen=10):
        chars = string.letters + string.digits
        passwd = ""
# determine password size (randomly, but between the given range)
        passwd_size = whrandom.randint(minlen, maxlen)
        for x in range(passwd_size):
# choose a random alpha-numeric character
                passwd += whrandom.choice(chars)
        return passwd


def sendpass(password):
        msg=("From: %s\r\nTo: %s\r\nSubject: Changement de mot de
passe\r\n\r\n"
 % (fromadd, toadd)) 
        msg=msg+"Compte : %s \nMot de passe : %s"%(user,password)
        server=smtplib.SMTP('localhost')
        #server.set_debuglevel(1)
        server.sendmail(fromadd,toadd,msg)
        server.quit()


def set_passwd(user, passwd):
    cyptpass=authlib.passcrypt(Pass, method='md5') 
    shadowfile=open(shadow,"r")
    fd,tmpfile=tempfile.mkstemp(".tmp","shadow",tmpdir)
    for  ligne in shadowfile.readlines():
                mots=string.split(ligne,":",2)
                if mots[0]!=user:
                        os.write(fd,ligne)
                        continue
                mots[1]=cyptpass
                ligne=string.join(mots,":")
                os.write(fd,ligne)
    os.close(fd)
    shutil.move(tmpfile,shadow)



if __name__ == '__main__':
    Pass=generatePassword()
    set_passwd(user,Pass)
    sendpass(Pass)


-- 
Jérôme KIEFFER
The programming of today's high speed digital computers is still an art 
rather than a science.                
            Savitsky and Golay, Anal. Chem., 36, p1638 (1964)