Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/utils/users.py
blob: 02216d6a709b561e40915f860efc3df15d25d658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding: utf-8

# users.py - functions for validating the user to change information for
#
# Copyright (C) 2013 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

import getpass
import os
import pwd
import sys


class User(object):

    def __init__(self, username):
        self.myuid = os.getuid()
        if username:
            userinfo = pwd.getpwnam(username)
        else:
            self.asroot = False
            userinfo = pwd.getpwuid(self.myuid)
        (self.username, ignore, self.uid, self.gid, self.gecos, self.homedir,
            self.shell) = userinfo
        # if we are trying to modify another user we should be root
        self.asroot = self.myuid != self.uid

    def check(self):
        """Check if the user we want to modify is an LDAP user and whether
        we may modify the user information."""
        if self.asroot and self.myuid != 0:
            print "%s: you may not modify user '%s'.\n" % \
                    (sys.argv[0], self.username)
            sys.exit(1)
        # FIXME: check if the user is an LDAP user

    def get_passwd(self):
        """Ask and return a password that is required to change the user."""
        # FIXME: only ask the password if we require it
        # (e.g. when root and nslcd has userpwmoddn we don't need to)
        return getpass.getpass(
                'LDAP administrator password: '
                if self.asroot else
                'LDAP password for %s: ' % self.username
            )
        # FIXME: check if the provided password is valid