Skip to content

Instantly share code, notes, and snippets.

@jrmoserbaltimore
Created June 24, 2013 19:09
Show Gist options
  • Save jrmoserbaltimore/5852608 to your computer and use it in GitHub Desktop.
Save jrmoserbaltimore/5852608 to your computer and use it in GitHub Desktop.
class WCMFTPUser(Base):
"""WCM FTP Users table"""
__tablename__ = 'user'
username = Column(String(50), nullable=False, unique=True)
password = Column(String(255), nullable=False)
uid = Column(Integer, primary_key=True, nullable=False)
gid = Column(Integer, nullable=False)
homedir = Column(String(1024), nullable=False, default='/var/www/content')
shell = Column(String(255), nullable=False, default='/sbin/nologin')
count = Column(Integer, nullable=False, default=0)
last_login = Column(DateTime, nullable=True)
def set_password(self, pw):
"""Hash a password for storage under ProFTPd
Hash is sha512 stored in base64
Does not accept NULL passwords!
"""
if not pw or pw == '':
raise ValueError
return
h = hashlib.sha512()
h.update(pw)
self.password = '{sha512}' + base64.b64encode(h.digest())
def add_ftp_user(session, username, uid, gid, password, home=None,
shell=None):
"""Creates a user with the given attributes."""
u = WCMFTPUser(username=username, uid=uid, gid=gid, password='',
homedir=home, shell=shell)
u.set_password(password)
try:
session.add(u)
session.commit()
except:
print 'Exception encountered! User exists?'
session.rollback()
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment