Blame utils/shells.py

Packit 6bd9ab
# coding: utf-8
Packit 6bd9ab
Packit 6bd9ab
# shells.py - functions for validating user shells
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 ctypes
Packit 6bd9ab
import ctypes.util
Packit 6bd9ab
import os
Packit 6bd9ab
import sys
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
def list_shells():
Packit 6bd9ab
    """List the shells from /etc/shells."""
Packit 6bd9ab
    libc = ctypes.CDLL(ctypes.util.find_library("c"))
Packit 6bd9ab
    libc.setusershell()
Packit 6bd9ab
    while True:
Packit 6bd9ab
        shell = ctypes.c_char_p(libc.getusershell()).value
Packit 6bd9ab
        if not shell:
Packit 6bd9ab
            break
Packit 6bd9ab
        yield shell
Packit 6bd9ab
    libc.endusershell()
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
def shellexists(shell):
Packit 6bd9ab
    """Check if the provided shell exists and is executable."""
Packit 6bd9ab
    return os.path.isfile(shell) and os.access(shell, os.X_OK)
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
def check(shell, asroot=False):
Packit 6bd9ab
    """Check if the specified shell is valid and exit if it isn't."""
Packit 6bd9ab
    # if the shell is listed in /etc/shells, everything should be OK
Packit 6bd9ab
    if shell in list_shells():
Packit 6bd9ab
        return
Packit 6bd9ab
    # if we are not root, bail out
Packit 6bd9ab
    if not asroot:
Packit 6bd9ab
        if not shell:
Packit 6bd9ab
            # FIXME: print to stderr
Packit 6bd9ab
            print('%s: empty shell not allowed' % sys.argv[0])
Packit 6bd9ab
        else:
Packit 6bd9ab
            # FIXME: print to stderr
Packit 6bd9ab
            print('%s: %s is an invalid shell' % (sys.argv[0], shell))
Packit 6bd9ab
        sys.exit(1)
Packit 6bd9ab
    # warn if something seems wrong
Packit 6bd9ab
    if not shell:
Packit 6bd9ab
        # FIXME: print to stderr
Packit 6bd9ab
        print('%s: Warning: setting empty shell' % sys.argv[0])
Packit 6bd9ab
    elif not shellexists(shell):
Packit 6bd9ab
        print('%s: Warning: %s does not exist' % (sys.argv[0], shell))