|
Packit |
6bd9ab |
# coding: utf-8
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
# users.py - functions for validating the user to change information for
|
|
Packit |
6bd9ab |
#
|
|
Packit |
6bd9ab |
# Copyright (C) 2013 Arthur de Jong
|
|
Packit |
6bd9ab |
#
|
|
Packit |
6bd9ab |
# This library is free software; you can redistribute it and/or
|
|
Packit |
6bd9ab |
# modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6bd9ab |
# License as published by the Free Software Foundation; either
|
|
Packit |
6bd9ab |
# version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6bd9ab |
#
|
|
Packit |
6bd9ab |
# This library is distributed in the hope that it will be useful,
|
|
Packit |
6bd9ab |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6bd9ab |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6bd9ab |
# Lesser General Public License for more details.
|
|
Packit |
6bd9ab |
#
|
|
Packit |
6bd9ab |
# You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6bd9ab |
# License along with this library; if not, write to the Free Software
|
|
Packit |
6bd9ab |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit |
6bd9ab |
# 02110-1301 USA
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
import getpass
|
|
Packit |
6bd9ab |
import os
|
|
Packit |
6bd9ab |
import pwd
|
|
Packit |
6bd9ab |
import sys
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
class User(object):
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
def __init__(self, username):
|
|
Packit |
6bd9ab |
self.myuid = os.getuid()
|
|
Packit |
6bd9ab |
if username:
|
|
Packit |
6bd9ab |
userinfo = pwd.getpwnam(username)
|
|
Packit |
6bd9ab |
else:
|
|
Packit |
6bd9ab |
self.asroot = False
|
|
Packit |
6bd9ab |
userinfo = pwd.getpwuid(self.myuid)
|
|
Packit |
6bd9ab |
(self.username, self.password, self.uid, self.gid, self.gecos,
|
|
Packit |
6bd9ab |
self.homedir, self.shell) = userinfo
|
|
Packit |
6bd9ab |
# if we are trying to modify another user we should be root
|
|
Packit |
6bd9ab |
self.asroot = self.myuid != self.uid
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
def check(self):
|
|
Packit |
6bd9ab |
"""Check if the user we want to modify is an LDAP user and whether
|
|
Packit |
6bd9ab |
we may modify the user information."""
|
|
Packit |
6bd9ab |
if self.asroot and self.myuid != 0:
|
|
Packit |
6bd9ab |
print("%s: you may not modify user '%s'.\n" %
|
|
Packit |
6bd9ab |
(sys.argv[0], self.username))
|
|
Packit |
6bd9ab |
sys.exit(1)
|
|
Packit |
6bd9ab |
# FIXME: check if the user is an LDAP user
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
def get_passwd(self):
|
|
Packit |
6bd9ab |
"""Ask and return a password that is required to change the user."""
|
|
Packit |
6bd9ab |
# FIXME: only ask the password if we require it
|
|
Packit |
6bd9ab |
# (e.g. when root and nslcd has userpwmoddn we don't need to)
|
|
Packit |
6bd9ab |
return getpass.getpass(
|
|
Packit |
6bd9ab |
'LDAP administrator password: '
|
|
Packit |
6bd9ab |
if self.asroot else
|
|
Packit |
6bd9ab |
'LDAP password for %s: ' % self.username
|
|
Packit |
6bd9ab |
)
|
|
Packit |
6bd9ab |
# FIXME: check if the provided password is valid
|