File: pam-python.py

package info (click to toggle)
libpam-mklocaluser 0.20
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 64 kB
  • sloc: python: 152; sh: 4; makefile: 2
file content (240 lines) | stat: -rwxr-xr-x 7,905 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
# Copyright (C) 2010-2016 Petter Reinholdtsen <pere@hungry.com>
#               2023 Guido Berhoerster <guido+freiesoftware@berhoerster.name>
#               2010 Morten Werner Forsbring <werner@debian.org>
#
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301, USA.

__author__ = "Petter Reinholdtsen <pere@hungry.com>"

#
# Create local user and redirected home directory.
# If the local user logging in have uid >= 1000, create primary group
# and user in /etc/passwd and /etc/group, and create a home directory
# under /home/ if none exist already.

import os
import sys
import pwd
import grp
import subprocess
import shutil
import tempfile
import syslog
from pathlib import Path


HOOK_PATH = Path("/etc/mklocaluser.d")


def get_minimum_uid():
    min_uid = 1000
    with open("/etc/login.defs") as f:
        for line in f:
            parts = line.strip().split(maxsplit=1)
            if len(parts) == 2 and parts[0] == "UID_MIN":
                try:
                    min_uid = int(parts[1])
                except ValueError:
                    pass
                break

    return min_uid


def check_and_create_localuser(pamh, user):
    # Fetch current user and group info, possibly from LDAP or NIS.
    try:
        userinfo = pwd.getpwnam(user)
    except KeyError as err:
        syslog.syslog(f"Unknown username, should never happen: {err}")
        return pamh.PAM_USER_UNKNOWN

    # Ignore users belwo minimum UID
    if userinfo.pw_uid < get_minimum_uid():
        return pamh.PAM_SUCCESS

    # Ignore users with existing entry in /etc/passwd
    try:
        subprocess.run(
            ["getent", "passwd", "-s", "compat", user],
            capture_output=True, text=True, check=True
        )
    except subprocess.CalledProcessError as err:
        if err.returncode != 2:
            syslog.syslog(f"{err} {err.stderr.strip()}")
            return pamh.PAM_SYSTEM_ERR
    else:
        return pamh.PAM_SUCCESS

    # Check whether home directory is set
    if userinfo.pw_dir is None:
        syslog.syslog(f"Home directory is not set for user {user}")
        return pamh.PAM_USER_UNKNOWN
    home = Path(userinfo.pw_dir)

    # Determine location of local home directory
    try:
        result = subprocess.run(
            ["useradd", "-D"], capture_output=True, text=True, check=True
        )
    except subprocess.CalledProcessError as err:
        syslog.syslog(f"{err} {err.stderr.strip()}")
        return pamh.PAM_SYSTEM_ERR
    useradd_defaults = dict(
        line.split("=", maxsplit=1) for line in result.stdout.split()
    )
    new_home = Path(useradd_defaults.get("HOME", "/home")) / user

    # Ensure neither old nor new home already exist
    if home.is_dir() or new_home.is_dir():
        return pamh.PAM_SUCCESS

    try:
        groupname = grp.getgrgid(userinfo.pw_gid).gr_name
    except KeyError:
        syslog.syslog(f"Unknown primary group with gid {userinfo.pw_gid}")
        groupname = "[unknown]"

    # Create local user
    syslog.syslog(
        f"Creating local passwd/shadow entry uid={userinfo.pw_uid}({user}) "
        f"gid={userinfo.pw_gid}({groupname}) gecos='{userinfo.pw_gecos}' "
        f"home={new_home} shell='{userinfo.pw_shell}'"
    )
    with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
        # Use alternative path to the root directory to trick useradd into
        # using files
        root = Path(tmpdir) / "root"
        root.symlink_to("/")
        try:
            # Use "--prefix" option in order to create a local user, do not set
            # a group since it will not be found
            subprocess.run(
                [
                    "useradd", "--prefix", root, "--uid", str(userinfo.pw_uid),
                    "--no-user-group", "--create-home", "--home-dir", new_home,
                    "--shell", userinfo.pw_shell,
                    "--comment", userinfo.pw_gecos, user
                ],
                capture_output=True, text=True, check=True
            )
            # Set the correct group
            subprocess.run(
                ["usermod", "-g", str(userinfo.pw_gid), user],
                capture_output=True, text=True, check=True
            )
        except subprocess.CalledProcessError as err:
            syslog.syslog(f"{err} {err.stderr.strip()}")
            return pamh.PAM_SYSTEM_ERR

    # Flush nscd cache to get rid of original user entry
    nscd = shutil.which("nscd")
    if nscd:
        subprocess.run(
            [nscd, "-i", "passwd"],
            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
        )

    # Hook for adjusting the freshly created home directory
    if HOOK_PATH.is_dir:
        try:
            subprocess.run(
                ["run-parts", HOOK_PATH],
                env=os.environ | {"ORIGHOMEDIR": home, "USER": user},
                check=True
            )
        except subprocess.CalledProcessError as err:
            syslog.syslog(f"{err} {err.stderr.strip()}")

    # At this point, the HOME environment variable is still set to the
    # value (i.e. path) as provided by the LDAP database. With pam_mklocaluser,
    # we want a HOME path with the pattern /<topdir>/<user>. Luckily
    # the pam_python.so implementation provides an easy-to-use interface to
    # pam_getenv/pam_putenv:
    pamh.env['HOME'] = str(new_home)

    return pamh.PAM_SUCCESS


def pam_sm_setcred(pamh, flags, argv):
    return pamh.PAM_SUCCESS


def pam_sm_authenticate(pamh, flags, argv):
    return pamh.PAM_SUCCESS


def pam_sm_acct_mgmt(pamh, flags, argv):
    return pamh.PAM_SUCCESS


def pam_sm_open_session(pamh, flags, argv):
    syslog.openlog("pam_mklocaluser", syslog.LOG_PID, syslog.LOG_AUTH)
    try:
        user = pamh.get_user(None)
    except pamh.exception as exc:
        return exc.pam_result
    if user is None:
        syslog.syslog("No user, ignoring pam-python for mklocaluser")
        return pamh.PAM_USER_UNKNOWN

    # Only create local users for console logins
    try:
        if pamh.rhost is not None and len(pamh.rhost) != 0:
            syslog.syslog("Remote login, ignoring pam-python for mklocaluser")
            return pamh.PAM_SUCCESS
    except pamh.exception as exc:
        return exc.pam_result

    try:
        return check_and_create_localuser(pamh, user)
    except Exception as exc:
        syslog.syslog(f"Unexpected exception, should never happen: {exc}")
        return pamh.PAM_SYSTEM_ERR


def pam_sm_close_session(pamh, flags, argv):
    return pamh.PAM_SUCCESS


def pam_sm_chauthtok(pamh, flags, argv):
    return pamh.PAM_SUCCESS


# Test if the code work.    Argument is username to simulate login for.
if __name__ == '__main__':
    syslog.openlog("pam_mklocaluser", syslog.LOG_PID, syslog.LOG_AUTH)

    class pam_handler:
        PAM_SUCCESS = 1
        PAM_USER_UNKNOWN = 2
        PAM_SYSTEM_ERR = 3
        PAM_TRY_AGAIN = 4
        PAM_TEXT_INFO = 5

        def Message(self, tag, str):
            return str

        def conversation(self, msg):
            print("PAM conversation: " + msg)
            return

    pamh = pam_handler()
    user = sys.argv[1]
    check_and_create_localuser(pamh, user)