Blame utils/chsh.py

Packit 6bd9ab
#!/usr/bin/env python
Packit 6bd9ab
# coding: utf-8
Packit 6bd9ab
Packit 6bd9ab
# chsh.py - program for changing the login shell using nslcd
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 argparse
Packit 6bd9ab
Packit 6bd9ab
from cmdline import VersionAction, ListShellsAction
Packit 6bd9ab
import constants
Packit 6bd9ab
import nslcd
Packit 6bd9ab
import shells
Packit 6bd9ab
import users
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
# set up command line parser
Packit 6bd9ab
parser = argparse.ArgumentParser(
Packit 6bd9ab
    description='Change the user login shell in LDAP.',
Packit 6bd9ab
    epilog='Report bugs to <%s>.' % constants.PACKAGE_BUGREPORT)
Packit 6bd9ab
parser.add_argument('-V', '--version', action=VersionAction)
Packit 6bd9ab
parser.add_argument('-s', '--shell', help='login shell for the user account')
Packit 6bd9ab
parser.add_argument('-l', '--list-shells', action=ListShellsAction)
Packit 6bd9ab
parser.add_argument('username', metavar='USER', nargs='?',
Packit 6bd9ab
                    help="the user who's shell to change")
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
def ask_shell(oldshell):
Packit 6bd9ab
    """Ask the user to provide a shell."""
Packit 6bd9ab
    shell = raw_input('  Login Shell [%s]: ' % oldshell)
Packit 6bd9ab
    return shell or oldshell
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
if __name__ == '__main__':
Packit 6bd9ab
    # parse arguments
Packit 6bd9ab
    args = parser.parse_args()
Packit 6bd9ab
    # check username part
Packit 6bd9ab
    user = users.User(args.username)
Packit 6bd9ab
    user.check()
Packit 6bd9ab
    # check the command line shell if one was provided (to fail early)
Packit 6bd9ab
    shell = args.shell
Packit 6bd9ab
    if shell is not None:
Packit 6bd9ab
        shells.check(shell, user.asroot)
Packit 6bd9ab
    # prompt for a password if required
Packit 6bd9ab
    password = user.get_passwd()
Packit 6bd9ab
    # prompt for a shell if it was not specified on the command line
Packit 6bd9ab
    if shell is None:
Packit 6bd9ab
        print('Enter the new value, or press ENTER for the default')
Packit 6bd9ab
        shell = ask_shell(user.shell)
Packit 6bd9ab
        shells.check(shell, user.asroot)
Packit 6bd9ab
    # perform the modification
Packit 6bd9ab
    result = nslcd.usermod(
Packit 6bd9ab
        user.username, user.asroot, password, {
Packit 6bd9ab
            constants.NSLCD_USERMOD_SHELL: shell,
Packit 6bd9ab
        })
Packit 6bd9ab
    # TODO: print proper response