Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/utils/chsh.py
blob: 0f24bfcc2bf4fe815893bc97365859c0aceba793 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# coding: utf-8

# chsh.py - program for changing the login shell using nslcd
#
# Copyright (C) 2013-2019 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 argparse

import constants
import nslcd
import shells
import users
from cmdline import ListShellsAction, VersionAction


# set up command line parser
parser = argparse.ArgumentParser(
    description='Change the user login shell in LDAP.',
    epilog='Report bugs to <%s>.' % constants.PACKAGE_BUGREPORT)
parser.add_argument('-V', '--version', action=VersionAction)
parser.add_argument('-s', '--shell', help='login shell for the user account')
parser.add_argument('-l', '--list-shells', action=ListShellsAction)
parser.add_argument('username', metavar='USER', nargs='?',
                    help="the user who's shell to change")


def ask_shell(oldshell):
    """Ask the user to provide a shell."""
    # Provide Python 2 compatibility
    prompt = '  Login Shell [%s]: ' % oldshell
    try:
        shell = raw_input(prompt)
    except NameError:
        shell = input(prompt)
    return shell or oldshell


def main():
    # parse arguments
    args = parser.parse_args()
    # check username part
    user = users.User(args.username)
    user.check()
    # check the command line shell if one was provided (to fail early)
    shell = args.shell
    if shell is not None:
        shells.check(shell, user.asroot)
    # prompt for a password if required
    password = user.get_passwd()
    # prompt for a shell if it was not specified on the command line
    if shell is None:
        print('Enter the new value, or press ENTER for the default')
        shell = ask_shell(user.shell)
        shells.check(shell, user.asroot)
    # perform the modification
    nslcd.usermod(
        user.username, user.asroot, password, {
            constants.NSLCD_USERMOD_SHELL: shell,
        })
    # TODO: print proper response


if __name__ == '__main__':
    main()