239 lines
7.9 KiB
Python
Executable file
239 lines
7.9 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso8859-15 -*-
|
|
|
|
# Copyright (C) Frédéric Pauget
|
|
# Licence : GPLv2
|
|
|
|
import smtplib, sys, commands, shutil, os, traceback
|
|
sys.path.append('/usr/scripts/gestion')
|
|
from affich_tools import cprint, anim, OK, WARNING, ERREUR
|
|
import config
|
|
from time import strftime
|
|
from ldap_crans import hostname, crans_ldap
|
|
|
|
try:
|
|
sys.path.append('/usr/lib/mailman')
|
|
from Mailman.MailList import MailList
|
|
from Mailman.UserDesc import UserDesc
|
|
from Mailman.Errors import MMAlreadyAMember
|
|
except:
|
|
# Machine sans mailman, les ML ne seront pas reconfigurées
|
|
pass
|
|
|
|
|
|
class del_user:
|
|
""" Suppression des fichiers d'un compte utilisateur """
|
|
|
|
debug = True
|
|
|
|
def __init__(self, args):
|
|
self.args = args
|
|
|
|
def delete_directory(self, caption, pattern):
|
|
cprint(caption, "gras")
|
|
for args in self.args:
|
|
anim('\t' + args)
|
|
try:
|
|
login = args.split(',')[0]
|
|
d = pattern % login
|
|
if os.path.isdir(d):
|
|
shutil.rmtree(d)
|
|
print OK
|
|
except:
|
|
print ERREUR
|
|
if self.debug:
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
def delete_vert(self):
|
|
cprint(u'Archivage fichiers utilisateur', 'gras')
|
|
for args in self.args:
|
|
anim('\t' + args)
|
|
try:
|
|
login, home = args.split(',')
|
|
if not login or not home:
|
|
raise ValueError('Argument invalide')
|
|
warn = ''
|
|
f = '%s/files/%s_%s.tar.bz2' % (config.cimetiere,
|
|
strftime('%Y-%m-%d-%Hh%Mm'),
|
|
login)
|
|
status, output = commands.getstatusoutput("tar cjf '%s' '%s' /var/spool/mail/%s" % (f, home, login))
|
|
if (status != 512 and status != 0) or not os.path.isfile(f):
|
|
# La 512 est si un des paths n'exite pas.
|
|
raise OSError(output)
|
|
if os.path.isdir(home) and os.stat(home)[4] >= 500:
|
|
shutil.rmtree(home)
|
|
else:
|
|
warn += '%s incorrect\n' % home
|
|
if os.path.isdir('/var/spool/mail/' + login):
|
|
shutil.rmtree('/var/spool/mail/' + login)
|
|
else:
|
|
warn += '/var/spool/mail/%s incorrect\n' % login
|
|
if warn:
|
|
print WARNING
|
|
if self.debug:
|
|
sys.stderr.write(warn)
|
|
else:
|
|
print OK
|
|
except:
|
|
print ERREUR
|
|
if self.debug:
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
def reconfigure(self):
|
|
if hostname == "vert":
|
|
self.delete_vert()
|
|
elif hostname == "rouge":
|
|
self.delete_directory(u"Suppression des fichiers index de dovecot",
|
|
"/var/dovecot-indexes/%s")
|
|
elif hostname == "zamok":
|
|
self.delete_directory(u"Suppression des fichiers d'impression",
|
|
"/var/spool/impression/%s")
|
|
|
|
class home:
|
|
debug = True
|
|
|
|
def __init__(self, args):
|
|
self.args = args
|
|
|
|
def reconfigure(self):
|
|
cprint(u'Création home', 'gras')
|
|
for args in self.args:
|
|
anim('\t' + args)
|
|
try:
|
|
home, uid, login = args.split(',')
|
|
### Home
|
|
if not os.path.exists(home):
|
|
# Le home n'existe pas
|
|
os.mkdir(home, 0755)
|
|
os.chown(home, int(uid), config.gid)
|
|
elif os.path.isdir(home):
|
|
# Il y un répertoire existant
|
|
# Bon UID ?
|
|
stat = os.stat(home)
|
|
if stat[4] != int(uid) or stat[5] != config.gid:
|
|
# Le home n'est pas pas à la bonne personne
|
|
raise OSError('home existant')
|
|
|
|
### Quota
|
|
status, output = commands.getstatusoutput('/usr/sbin/edquota -p pauget %s' % login )
|
|
if status:
|
|
print WARNING
|
|
if self.debug:
|
|
sys.stderr.write(output+'\n')
|
|
else:
|
|
print OK
|
|
|
|
### Mail
|
|
os.mkdir(home + '/Mail', 0700)
|
|
os.chown(home + '/Mail', int(uid), config.gid)
|
|
os.mkdir('/var/mail/' + login, 0770)
|
|
os.chown('/var/mail/' + login, int(uid), 8)
|
|
|
|
except:
|
|
print ERREUR
|
|
if self.debug:
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
class mail_bienvenue:
|
|
debug = True
|
|
|
|
def __init__(self, args):
|
|
self.args = args
|
|
|
|
def reconfigure(self):
|
|
cprint(u'Envoi mail de bienvenue', 'gras')
|
|
for mail in self.mails:
|
|
anim('\t' + mail)
|
|
try:
|
|
From = "respbats@crans.org"
|
|
To = mail
|
|
if To.find('@') == -1: To += '@crans.org'
|
|
conn=smtplib.SMTP('localhost')
|
|
conn.sendmail(From, To, config.txt_mail_bienvenue % { 'From': From, 'To': To })
|
|
conn.quit()
|
|
print OK
|
|
except Exception, c:
|
|
print ERREUR
|
|
if self.debug:
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
class mail_ajout_droits:
|
|
debug = True
|
|
|
|
def __init__(self, args):
|
|
self.args = args
|
|
|
|
def reconfigure(self):
|
|
cprint(u'Envoi mail ajout de droits', 'gras')
|
|
for arg in self.args:
|
|
login = arg.split(":")[0]
|
|
Droit = arg.split(":")[1]
|
|
anim('\t' + login)
|
|
try:
|
|
From = "root@crans.org"
|
|
To = login
|
|
if To.find('@') == -1: To += '@crans.org'
|
|
print Droit +"\n"
|
|
if Droit in config.pages_infos_droits:
|
|
print "envoi du mail"
|
|
Page = config.pages_infos_droits[Droit.encode('utf-8')]
|
|
conn=smtplib.SMTP('localhost')
|
|
conn.sendmail(From, To, config.txt_ajout_droits % { 'From': From, 'To': To , 'Droit': Droit, 'Page': Page})
|
|
conn.quit()
|
|
print OK
|
|
else:
|
|
print "ca marche pas"
|
|
except Exception, c:
|
|
print ERREUR
|
|
if self.debug:
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
class ML_ens:
|
|
debug = True
|
|
|
|
def __init__(self, mails):
|
|
self.mails = mails
|
|
|
|
def reconfigure(self):
|
|
cprint(u'Inscription ML-ENS', 'gras')
|
|
mlist = MailList('com-ens')
|
|
for mail in self.mails:
|
|
anim('\t' + mail)
|
|
try:
|
|
mlist.ApprovedAddMember(UserDesc(mail))
|
|
except MMAlreadyAMember:
|
|
cprint(u"DÉJÀ INSCRIT", "jaune")
|
|
except:
|
|
print ERREUR
|
|
if self.debug:
|
|
traceback.print_exc()
|
|
else:
|
|
print OK
|
|
mlist.Save()
|
|
mlist.Unlock()
|
|
|
|
class mail_valide:
|
|
debug = True
|
|
|
|
def __init__(self,aids):
|
|
self.aids = aids
|
|
|
|
def reconfigure(self):
|
|
db = crans_ldap()
|
|
cprint(u'Validation d\'adresses', 'gras')
|
|
for aid in self.aids:
|
|
adh = db.search(("aid=%s" %aid), 'w')['adherent'][0]
|
|
if not adh._modifiable:
|
|
db.services_to_restart('mail_valide',aid)
|
|
print "echec pour le mail de %s" %adh.nom()
|
|
else:
|
|
adh.mail_invalide(False)
|
|
adh.save()
|
|
print "mail de %s marque valide" % adh.nom()
|