Code style changes
* make code PEP8 clean (addresses #18) * add copyright information to all python files git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/trunk@257 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
parent
7c4d25da43
commit
09180938f1
45 changed files with 759 additions and 514 deletions
|
@ -1,4 +1,3 @@
|
|||
# -*- python -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# -*- python -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
||||
|
@ -22,10 +21,13 @@
|
|||
|
||||
"""Tools for handling user and group information."""
|
||||
|
||||
import pwd, grp
|
||||
import pwd
|
||||
import grp
|
||||
|
||||
|
||||
class PasswdUser(object):
|
||||
"""This class represents users in the user database."""
|
||||
|
||||
def __init__(self, username, pw, uid, gid, gecos, home, shell):
|
||||
"""Create a new PasswdUser."""
|
||||
self.username = username
|
||||
|
@ -45,8 +47,10 @@ class PasswdUser(object):
|
|||
self.home,
|
||||
self.shell)
|
||||
|
||||
|
||||
class PasswdGroup(object):
|
||||
"""This class represents lines in the groups database."""
|
||||
|
||||
def __init__(self, groupname, pw, gid, members):
|
||||
"""Create a new PasswdGroup."""
|
||||
self.groupname = groupname
|
||||
|
@ -60,18 +64,22 @@ class PasswdGroup(object):
|
|||
self.gid,
|
||||
",".join(self.members))
|
||||
|
||||
|
||||
def parse_groups():
|
||||
"""Parses all available groups to PasswdGroup instances."""
|
||||
return [PasswdGroup(*arr) for arr in grp.getgrall()]
|
||||
|
||||
|
||||
def parse_users():
|
||||
"""Parses all available users to PasswdUser instances."""
|
||||
return [PasswdUser(*arr) for arr in pwd.getpwall()]
|
||||
|
||||
|
||||
def find_user_by_prefix(prefix):
|
||||
"""Finds all user entries with the given prefix."""
|
||||
"""Finds all user entries with the given prefix."""
|
||||
return [user for user in parse_users() if user.username.startswith(prefix)]
|
||||
|
||||
|
||||
def get_user_by_id(uid):
|
||||
"""Gets the user with the given user id."""
|
||||
users = [user for user in parse_users() if user.uid == uid]
|
||||
|
@ -79,6 +87,7 @@ def get_user_by_id(uid):
|
|||
return users[0]
|
||||
return None
|
||||
|
||||
|
||||
def get_group_by_id(gid):
|
||||
"""Gets the group with the given group id."""
|
||||
groups = [group for group in parse_groups() if group.gid == gid]
|
||||
|
@ -86,6 +95,7 @@ def get_group_by_id(gid):
|
|||
return groups[0]
|
||||
return None
|
||||
|
||||
|
||||
def get_next_uid(lowerboundary = 10000, upperboundary = 65536):
|
||||
"""Gets the first available user id in the given range.
|
||||
|
||||
|
@ -98,16 +108,19 @@ def get_next_uid(lowerboundary = 10000, upperboundary = 65536):
|
|||
|
||||
"""
|
||||
for uid in range(lowerboundary, upperboundary):
|
||||
try:
|
||||
user = pwd.getpwuid(uid)
|
||||
except KeyError:
|
||||
return uid
|
||||
raise Exception("no free uid found in range %d to %d", lowerboundary, upperboundary)
|
||||
try:
|
||||
user = pwd.getpwuid(uid)
|
||||
except KeyError:
|
||||
return uid
|
||||
raise Exception("no free uid found in range %d to %d",
|
||||
lowerboundary, upperboundary)
|
||||
|
||||
|
||||
def get_max_uid(boundary = 65536):
|
||||
"""Gets the highest uid value."""
|
||||
return max([user.uid for user in parse_users() if user.uid <= boundary])
|
||||
|
||||
|
||||
def get_max_gid(boundary = 65536):
|
||||
"""Gets the highest gid value."""
|
||||
return max([group.gid for group in parse_groups() \
|
||||
|
@ -119,4 +132,3 @@ if __name__ == "__main__":
|
|||
print "User with max UID is %s" % (get_user_by_id(get_max_uid(40000)))
|
||||
print "Group with max GID is %s" % (get_group_by_id(get_max_gid(40000)))
|
||||
print "First free UID is %s" % (get_next_uid(10000, 40000))
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# -*- python -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
||||
|
@ -33,6 +32,7 @@ from pyme.constants.sig import mode
|
|||
|
||||
from gnuviechadmin.backend.settings import config
|
||||
|
||||
|
||||
def send_mail(subject, text):
|
||||
"""Send a signed and possibly encrypted mail.
|
||||
|
||||
|
@ -56,33 +56,33 @@ def send_mail(subject, text):
|
|||
rcpt = config.get('common', 'mailto')
|
||||
c.signers_clear()
|
||||
for sigkey in [x for x in c.op_keylist_all(signer, 1)]:
|
||||
if sigkey.can_sign:
|
||||
c.signers_add(sigkey)
|
||||
if not c.signers_enum(0):
|
||||
raise Exception("No secret keys for signing available for %s." % (
|
||||
signer))
|
||||
if sigkey.can_sign:
|
||||
c.signers_add(sigkey)
|
||||
if not c.signers_enum(0):
|
||||
raise Exception("No secret keys for signing available for %s." % (
|
||||
signer))
|
||||
keylist = []
|
||||
for key in c.op_keylist_all(rcpt, 0):
|
||||
valid = 0
|
||||
subkey = key.subkeys
|
||||
while subkey:
|
||||
keyid = subkey.keyid
|
||||
if keyid == None:
|
||||
break
|
||||
can_encrypt = subkey.can_encrypt
|
||||
valid += can_encrypt
|
||||
subkey = subkey.next
|
||||
if valid:
|
||||
keylist.append(key)
|
||||
valid = 0
|
||||
subkey = key.subkeys
|
||||
while subkey:
|
||||
keyid = subkey.keyid
|
||||
if keyid == None:
|
||||
break
|
||||
can_encrypt = subkey.can_encrypt
|
||||
valid += can_encrypt
|
||||
subkey = subkey.next
|
||||
if valid:
|
||||
keylist.append(key)
|
||||
if keylist:
|
||||
c.op_encrypt_sign(keylist, 1, plain, cipher)
|
||||
c.op_encrypt_sign(keylist, 1, plain, cipher)
|
||||
else:
|
||||
c.op_sign(plain, cipher, mode.CLEAR)
|
||||
cipher.seek(0,0)
|
||||
c.op_sign(plain, cipher, mode.CLEAR)
|
||||
cipher.seek(0, 0)
|
||||
|
||||
msg = MIMEText(cipher.read())
|
||||
if keylist:
|
||||
msg.set_param("x-action", "pgp-encrypted")
|
||||
msg.set_param("x-action", "pgp-encrypted")
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = signer
|
||||
msg['To'] = rcpt
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# -*- python -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
||||
|
@ -21,7 +20,9 @@
|
|||
# Version: $Id$
|
||||
|
||||
"""This module provides some functions for password handling."""
|
||||
import crypt, crack, random
|
||||
import crypt
|
||||
import crack
|
||||
import random
|
||||
|
||||
_pwchars = []
|
||||
for _pair in (('0', '9'), ('A', 'Z'), ('a', 'z')):
|
||||
|
@ -30,6 +31,7 @@ _saltchars = [_char for _char in _pwchars]
|
|||
for _char in "-+/*_@":
|
||||
_pwchars.append(ord(_char))
|
||||
|
||||
|
||||
def generatepassword(minlength = 8, maxlength = 12):
|
||||
"""Generates a new random password with a given length.
|
||||
|
||||
|
@ -44,6 +46,7 @@ def generatepassword(minlength = 8, maxlength = 12):
|
|||
random.sample(_pwchars,
|
||||
random.randint(minlength, maxlength))])
|
||||
|
||||
|
||||
def checkpassword(password):
|
||||
"""Checks the password with cracklib.
|
||||
|
||||
|
@ -58,7 +61,8 @@ def checkpassword(password):
|
|||
except ValueError, ve:
|
||||
print "Weak password:", ve
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def md5_crypt_password(password):
|
||||
"""Hashes the given password with MD5 and a random salt value.
|
||||
|
||||
|
@ -71,6 +75,7 @@ def md5_crypt_password(password):
|
|||
random.sample(_saltchars, 8)])
|
||||
return crypt.crypt(password, '$1$' + salt)
|
||||
|
||||
|
||||
def get_pw_tuple(password = None):
|
||||
"""Gets a valid (password, hashvalue) tuple.
|
||||
|
||||
|
@ -82,3 +87,6 @@ def get_pw_tuple(password = None):
|
|||
while password == None or checkpassword(password) == None:
|
||||
password = generatepassword()
|
||||
return (password, md5_crypt_password(password))
|
||||
|
||||
|
||||
# TODO: implement a is_password_valid(hash, password) function
|
||||
|
|
31
gnuviechadmin/util/stmtcreator.py
Normal file → Executable file
31
gnuviechadmin/util/stmtcreator.py
Normal file → Executable file
|
@ -1,4 +1,4 @@
|
|||
# -*- python -*-
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
||||
|
@ -24,16 +24,23 @@
|
|||
creation."""
|
||||
|
||||
if __name__ == '__main__':
|
||||
from passwordutils import get_pw_tuple
|
||||
import sys
|
||||
from passwordutils import get_pw_tuple
|
||||
import sys
|
||||
|
||||
for line in sys.stdin.readlines():
|
||||
parts = line.split()
|
||||
if len(parts) < 4:
|
||||
raise ValueError("""lines must consist of the elements:
|
||||
for line in sys.stdin.readlines():
|
||||
parts = line.split()
|
||||
if len(parts) < 4:
|
||||
raise ValueError("""lines must consist of the elements:
|
||||
email@domain username uid domainid""")
|
||||
(email, domain) = parts[0].split("@")
|
||||
username = parts[1][0:5]
|
||||
pwtuple = get_pw_tuple()
|
||||
print "INSERT INTO mailpassword (id, clearpass, cryptpass, uid, gid, home, spamcheck) VALUES ('%s', '%s', '%s', %d, %d, '/home/mail/%s/%s', 'false');" % (parts[1], pwtuple[0], pwtuple[1], int(parts[2]), 119, username, parts[1])
|
||||
print "INSERT INTO mailaddress (domainid, email, target) VALUES (%d, '%s', '%s');" % (int(parts[3]), email, parts[1])
|
||||
(email, domain) = parts[0].split("@")
|
||||
username = parts[1][0:5]
|
||||
pwtuple = get_pw_tuple()
|
||||
print "INSERT INTO mailpassword " + \
|
||||
"(id, clearpass, cryptpass, uid, gid, home, spamcheck) " + \
|
||||
"VALUES " + \
|
||||
"('%s', '%s', '%s', %d, %d, '/home/mail/%s/%s', 'false');" % (
|
||||
parts[1], pwtuple[0], pwtuple[1], int(parts[2]), 119,
|
||||
username, parts[1])
|
||||
print "INSERT INTO mailaddress (domainid, email, target) " + \
|
||||
"VALUES (%d, '%s', '%s');" % (
|
||||
int(parts[3]), email, parts[1])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue