"Fossies" - the Fresh Open Source Software Archive

Member "nss-pam-ldapd-0.9.12/utils/chsh.py" (15 Nov 2021, 2642 Bytes) of package /linux/privat/nss-pam-ldapd-0.9.12.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/env python
    2 # coding: utf-8
    3 
    4 # chsh.py - program for changing the login shell using nslcd
    5 #
    6 # Copyright (C) 2013-2019 Arthur de Jong
    7 #
    8 # This library is free software; you can redistribute it and/or
    9 # modify it under the terms of the GNU Lesser General Public
   10 # License as published by the Free Software Foundation; either
   11 # version 2.1 of the License, or (at your option) any later version.
   12 #
   13 # This library is distributed in the hope that it will be useful,
   14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16 # Lesser General Public License for more details.
   17 #
   18 # You should have received a copy of the GNU Lesser General Public
   19 # License along with this library; if not, write to the Free Software
   20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   21 # 02110-1301 USA
   22 
   23 import argparse
   24 
   25 import constants
   26 import nslcd
   27 import shells
   28 import users
   29 from cmdline import ListShellsAction, VersionAction
   30 
   31 
   32 # set up command line parser
   33 parser = argparse.ArgumentParser(
   34     description='Change the user login shell in LDAP.',
   35     epilog='Report bugs to <%s>.' % constants.PACKAGE_BUGREPORT)
   36 parser.add_argument('-V', '--version', action=VersionAction)
   37 parser.add_argument('-s', '--shell', help='login shell for the user account')
   38 parser.add_argument('-l', '--list-shells', action=ListShellsAction)
   39 parser.add_argument('username', metavar='USER', nargs='?',
   40                     help="the user who's shell to change")
   41 
   42 
   43 def ask_shell(oldshell):
   44     """Ask the user to provide a shell."""
   45     # Provide Python 2 compatibility
   46     prompt = '  Login Shell [%s]: ' % oldshell
   47     try:
   48         shell = raw_input(prompt)
   49     except NameError:
   50         shell = input(prompt)
   51     return shell or oldshell
   52 
   53 
   54 def main():
   55     # parse arguments
   56     args = parser.parse_args()
   57     # check username part
   58     user = users.User(args.username)
   59     user.check()
   60     # check the command line shell if one was provided (to fail early)
   61     shell = args.shell
   62     if shell is not None:
   63         shells.check(shell, user.asroot)
   64     # prompt for a password if required
   65     password = user.get_passwd()
   66     # prompt for a shell if it was not specified on the command line
   67     if shell is None:
   68         print('Enter the new value, or press ENTER for the default')
   69         shell = ask_shell(user.shell)
   70         shells.check(shell, user.asroot)
   71     # perform the modification
   72     nslcd.usermod(
   73         user.username, user.asroot, password, {
   74             constants.NSLCD_USERMOD_SHELL: shell,
   75         })
   76     # TODO: print proper response
   77 
   78 
   79 if __name__ == '__main__':
   80     main()