summaryrefslogtreecommitdiff
path: root/modules/pam_usertype/pam_usertype.c
blob: a15599e392bc16fcd8f08298d928aa428ae4079e (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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/******************************************************************************
 * Check user type based on login.defs.
 *
 * Copyright (c) 2020 Red Hat, Inc.
 * Written by Pavel Březina <pbrezina@redhat.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, and the entire permission notice in its entirety,
 *    including the disclaimer of warranties.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * ALTERNATIVELY, this product may be distributed under the terms of
 * the GNU Public License, in which case the provisions of the GPL are
 * required INSTEAD OF the above restrictions.  (This clause is
 * necessary due to a potential bad interaction between the GPL and
 * the restrictions contained in a BSD-style copyright.)
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "config.h"

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <pwd.h>
#include <ctype.h>
#include <errno.h>

#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_SESSION
#define PAM_SM_PASSWORD

#include <security/pam_modules.h>
#include <security/pam_modutil.h>
#include <security/pam_ext.h>

#define LOGIN_DEFS "/etc/login.defs"

enum pam_usertype_op {
    OP_IS_SYSTEM,
    OP_IS_REGULAR,

    OP_SENTINEL
};

struct pam_usertype_opts {
    enum pam_usertype_op op;
    int use_uid;
    int audit;
};

static int
pam_usertype_parse_args(struct pam_usertype_opts *opts,
                        pam_handle_t *pamh,
                        int argc,
                        const char **argv)
{
    int i;

    memset(opts, 0, sizeof(struct pam_usertype_opts));
    opts->op = OP_SENTINEL;

    for (i = 0; i < argc; i++) {
        if (strcmp(argv[i], "use_uid") == 0) {
            opts->use_uid = 1;
        } else if (strcmp(argv[i], "audit") == 0) {
            opts->audit = 1;
        } else if (strcmp(argv[i], "issystem") == 0) {
            opts->op = OP_IS_SYSTEM;
        } else if (strcmp(argv[i], "isregular") == 0) {
            opts->op = OP_IS_REGULAR;
        } else {
            pam_syslog(pamh, LOG_WARNING, "Unknown argument: %s", argv[i]);
            /* Just continue. */
        }
    }

    if (opts->op == OP_SENTINEL) {
        pam_syslog(pamh, LOG_ERR, "Operation not specified");
        return PAM_SERVICE_ERR;
    }

    return PAM_SUCCESS;
}

static int
pam_usertype_get_uid(struct pam_usertype_opts *opts,
                     pam_handle_t *pamh,
                     uid_t *_uid)
{
    struct passwd *pwd;
    const void *prompt;
    const char *username;
    int ret;

    /* Get uid of user that runs the application. */
    if (opts->use_uid) {
        pwd = pam_modutil_getpwuid(pamh, getuid());
        if (pwd == NULL) {
            pam_syslog(pamh, LOG_ERR,
                       "error retrieving information about user %lu",
                       (unsigned long)getuid());
            return PAM_USER_UNKNOWN;
        }

        *_uid = pwd->pw_uid;
        return PAM_SUCCESS;
    }

    /* Get uid of user that is being authenticated. */
    ret = pam_get_item(pamh, PAM_USER_PROMPT, &prompt);
    if (ret != PAM_SUCCESS || prompt == NULL || strlen(prompt) == 0) {
        prompt = "login: ";
    }

    ret = pam_get_user(pamh, &username, prompt);
    if (ret != PAM_SUCCESS || username == NULL) {
        pam_syslog(pamh, LOG_ERR, "error retrieving user name: %s",
                   pam_strerror(pamh, ret));
        return ret;
    }

    pwd = pam_modutil_getpwnam(pamh, username);
    if (pwd == NULL) {
        if (opts->audit) {
            pam_syslog(pamh, LOG_NOTICE,
                       "error retrieving information about user %s", username);
        }

        return PAM_USER_UNKNOWN;
    }

    *_uid = pwd->pw_uid;

    return PAM_SUCCESS;
}

#define MAX_UID_VALUE 0xFFFFFFFFUL

static uid_t
pam_usertype_get_id(pam_handle_t *pamh,
                    const char *key,
                    uid_t default_value)
{
    unsigned long ul;
    char *value;
    char *ep;
    uid_t uid;

    value = pam_modutil_search_key(pamh, LOGIN_DEFS, key);
    if (value == NULL) {
        return default_value;
    }

    /* taken from get_lastlog_uid_max() */
    ep = value + strlen(value);
    while (ep > value && isspace(*(--ep))) {
        *ep = '\0';
    }

    errno = 0;
    ul = strtoul(value, &ep, 10);
    if (!(ul >= MAX_UID_VALUE
        || (uid_t)ul >= MAX_UID_VALUE
        || (errno != 0 && ul == 0)
        || value == ep
        || *ep != '\0')) {
        uid = (uid_t)ul;
    } else {
        uid = default_value;
    }

    free(value);

    return uid;
}

static int
pam_usertype_is_system(pam_handle_t *pamh, uid_t uid)
{
    uid_t uid_min;
    uid_t sys_min;
    uid_t sys_max;

    if (uid == (uid_t)-1) {
        pam_syslog(pamh, LOG_WARNING, "invalid uid");
        return PAM_USER_UNKNOWN;
    }

    if (uid <= 99) {
        /* Reserved. */
        return PAM_SUCCESS;
    }

    if (uid == PAM_USERTYPE_OVERFLOW_UID) {
        /* nobody */
        return PAM_SUCCESS;
    }

    uid_min = pam_usertype_get_id(pamh, "UID_MIN", PAM_USERTYPE_UIDMIN);
    sys_min = pam_usertype_get_id(pamh, "SYS_UID_MIN", PAM_USERTYPE_SYSUIDMIN);
    sys_max = pam_usertype_get_id(pamh, "SYS_UID_MAX", uid_min - 1);

    return uid >= sys_min && uid <= sys_max ? PAM_SUCCESS : PAM_AUTH_ERR;
}

static int
pam_usertype_is_regular(pam_handle_t *pamh, uid_t uid)
{
    int ret;

    ret = pam_usertype_is_system(pamh, uid);
    switch (ret) {
    case PAM_SUCCESS:
        return PAM_AUTH_ERR;
    case PAM_USER_UNKNOWN:
        return PAM_USER_UNKNOWN;
    default:
        return PAM_SUCCESS;
    }
}

static int
pam_usertype_evaluate(struct pam_usertype_opts *opts,
                      pam_handle_t *pamh,
                      uid_t uid)
{
    switch (opts->op) {
    case OP_IS_SYSTEM:
        return pam_usertype_is_system(pamh, uid);
    case OP_IS_REGULAR:
        return pam_usertype_is_regular(pamh, uid);
    default:
        pam_syslog(pamh, LOG_ERR, "Unknown operation: %d", opts->op);
        return PAM_SERVICE_ERR;
    }
}

/**
 * Arguments:
 * - issystem: uid in <SYS_UID_MIN, SYS_UID_MAX>
 * - isregular: not issystem
 * - use_uid: use user that runs application not that is being authenticate (same as in pam_succeed_if)
 * - audit: log unknown users to syslog
 */
int
pam_sm_authenticate(pam_handle_t *pamh, int flags UNUSED,
                    int argc, const char **argv)
{
    struct pam_usertype_opts opts;
    uid_t uid = -1;
    int ret;

    ret = pam_usertype_parse_args(&opts, pamh, argc, argv);
    if (ret != PAM_SUCCESS) {
        return ret;
    }

    ret = pam_usertype_get_uid(&opts, pamh, &uid);
    if (ret != PAM_SUCCESS) {
        return ret;
    }

    return pam_usertype_evaluate(&opts, pamh, uid);
}

int
pam_sm_setcred(pam_handle_t *pamh UNUSED, int flags UNUSED,
               int argc UNUSED, const char **argv UNUSED)
{
	return PAM_IGNORE;
}

int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	return pam_sm_authenticate(pamh, flags, argc, argv);
}

int
pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	return pam_sm_authenticate(pamh, flags, argc, argv);
}

int
pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	return pam_sm_authenticate(pamh, flags, argc, argv);
}

int
pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	return pam_sm_authenticate(pamh, flags, argc, argv);
}