summaryrefslogtreecommitdiff
path: root/modules/pam_wheel/pam_wheel.c
blob: eaf3266051612dd66609356d0e09f69ba0b350a2 (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
/* pam_wheel module */

/*
 * Written by Cristian Gafton <gafton@redhat.com> 1996/09/10
 * See the end of the file for Copyright Information
 *
 *
 * 1.2 - added 'deny' and 'group=' options
 * 1.1 - added 'trust' option
 * 1.0 - the code is working for at least another person, so... :-)
 * 0.1 - use vsyslog instead of vfprintf/syslog in _pam_log
 *     - return PAM_IGNORE on success (take care of sloppy sysadmins..)
 *     - use pam_get_user instead of pam_get_item(...,PAM_USER,...)
 *     - a new arg use_uid to auth the current uid instead of the
 *       initial (logged in) one.
 * 0.0 - first release
 *
 * TODO:
 *  - try to use make_remark from pam_unix/support.c
 *  - consider returning on failure PAM_FAIL_NOW if the user is not
 *    a wheel member.
 */

#include "config.h"

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>

/*
 * here, we make a definition for the externally accessible function
 * in this file (this definition is required for static a module
 * but strongly encouraged generally) it is used to instruct the
 * modules include file to define the function prototypes.
 */

#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT

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

/* checks if a user is on a list of members of the GID 0 group */
static int is_on_list(char * const *list, const char *member)
{
    while (list && *list) {
        if (strcmp(*list, member) == 0)
            return 1;
        list++;
    }
    return 0;
}

/* argument parsing */

#define PAM_DEBUG_ARG       0x0001
#define PAM_USE_UID_ARG     0x0002
#define PAM_TRUST_ARG       0x0004
#define PAM_DENY_ARG        0x0010
#define PAM_ROOT_ONLY_ARG   0x0020

static int
_pam_parse (const pam_handle_t *pamh, int argc, const char **argv,
	    char *use_group, size_t group_length)
{
     int ctrl=0;

     memset(use_group, '\0', group_length);

     /* step through arguments */
     for (ctrl=0; argc-- > 0; ++argv) {

          /* generic options */

          if (!strcmp(*argv,"debug"))
               ctrl |= PAM_DEBUG_ARG;
          else if (!strcmp(*argv,"use_uid"))
               ctrl |= PAM_USE_UID_ARG;
          else if (!strcmp(*argv,"trust"))
               ctrl |= PAM_TRUST_ARG;
          else if (!strcmp(*argv,"deny"))
               ctrl |= PAM_DENY_ARG;
          else if (!strcmp(*argv,"root_only"))
               ctrl |= PAM_ROOT_ONLY_ARG;
          else if (!strncmp(*argv,"group=",6))
	       strncpy(use_group,*argv+6,group_length-1);
          else {
               pam_syslog(pamh,LOG_ERR,"pam_parse: unknown option; %s",*argv);
          }
     }

     return ctrl;
}

static int
perform_check (pam_handle_t *pamh, int ctrl, const char *use_group)
{
    const char *username = NULL;
    const char *fromsu;
    struct passwd *pwd, *tpwd = NULL;
    struct group *grp;
    int retval = PAM_AUTH_ERR;

    retval = pam_get_user(pamh, &username, NULL);
    if ((retval != PAM_SUCCESS) || (!username)) {
        if (ctrl & PAM_DEBUG_ARG) {
            pam_syslog(pamh,LOG_DEBUG,"can not get the username");
	}
        return PAM_SERVICE_ERR;
    }

    pwd = _pammodutil_getpwnam (pamh, username);
    if (!pwd) {
        if (ctrl & PAM_DEBUG_ARG) {
            pam_syslog(pamh,LOG_NOTICE,"unknown user %s",username);
        }
        return PAM_USER_UNKNOWN;
    }
    if (ctrl & PAM_ROOT_ONLY_ARG) {
	/* su to a non uid 0 account ? */
        if (pwd->pw_uid != 0) {
            return PAM_IGNORE;
        }
    }

    if (ctrl & PAM_USE_UID_ARG) {
	tpwd = _pammodutil_getpwuid (pamh, getuid());
	if (!tpwd) {
	    if (ctrl & PAM_DEBUG_ARG) {
                pam_syslog(pamh,LOG_NOTICE, "who is running me ?!");
	    }
	    return PAM_SERVICE_ERR;
	}
	fromsu = tpwd->pw_name;
    } else {
	fromsu = _pammodutil_getlogin(pamh);
	if (fromsu) {
	    tpwd = _pammodutil_getpwnam (pamh, fromsu);
	}
	if (!fromsu || !tpwd) {
	    if (ctrl & PAM_DEBUG_ARG) {
		pam_syslog(pamh,LOG_NOTICE, "who is running me ?!");
	    }
	    return PAM_SERVICE_ERR;
	}
    }

    /*
     * At this point fromsu = username-of-invoker; tpwd = pwd ptr for fromsu
     */

    if (!use_group[0]) {
	if ((grp = _pammodutil_getgrnam (pamh, "wheel")) == NULL) {
	    grp = _pammodutil_getgrgid (pamh, 0);
	}
    } else {
	grp = _pammodutil_getgrnam (pamh, use_group);
    }

    if (!grp || (!grp->gr_mem && (tpwd->pw_gid != grp->gr_gid))) {
	if (ctrl & PAM_DEBUG_ARG) {
	    if (!use_group[0]) {
		pam_syslog(pamh,LOG_NOTICE,"no members in a GID 0 group");
	    } else {
                pam_syslog(pamh,LOG_NOTICE,"no members in '%s' group", use_group);
	    }
	}
	if (ctrl & PAM_DENY_ARG) {
	    /* if this was meant to deny access to the members
	     * of this group and the group does not exist, allow
	     * access
	     */
	    return PAM_IGNORE;
	} else {
	    return PAM_AUTH_ERR;
	}
    }

    /*
     * test if the user is a member of the group, or if the
     * user has the "wheel" (sic) group as its primary group.
     */

    if (is_on_list(grp->gr_mem, fromsu) || (tpwd->pw_gid == grp->gr_gid)) {

	if (ctrl & PAM_DENY_ARG) {
	    retval = PAM_PERM_DENIED;

	} else if (ctrl & PAM_TRUST_ARG) {
	    retval = PAM_SUCCESS;        /* this can be a sufficient check */

	} else {
	    retval = PAM_IGNORE;
	}

    } else {

	if (ctrl & PAM_DENY_ARG) {

	    if (ctrl & PAM_TRUST_ARG) {
		retval = PAM_SUCCESS;    /* this can be a sufficient check */
	    } else {
		retval = PAM_IGNORE;
	    }

	} else {
	    retval = PAM_PERM_DENIED;
	}
    }

    if (ctrl & PAM_DEBUG_ARG) {
	if (retval == PAM_IGNORE) {
	    pam_syslog(pamh,LOG_NOTICE, "Ignoring access request '%s' for '%s'",
		     fromsu, username);
	} else {
	    pam_syslog(pamh,LOG_NOTICE, "Access %s to '%s' for '%s'",
		     (retval != PAM_SUCCESS) ? "denied":"granted",
		     fromsu, username);
	}
    }

    return retval;
}

/* --- authentication management functions --- */

PAM_EXTERN int
pam_sm_authenticate (pam_handle_t *pamh, int flags UNUSED,
		     int argc, const char **argv)
{
    char use_group[BUFSIZ];
    int ctrl;

    ctrl = _pam_parse(pamh, argc, argv, use_group, sizeof(use_group));

    return perform_check(pamh, ctrl, use_group);
}

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

PAM_EXTERN int
pam_sm_acct_mgmt (pam_handle_t *pamh, int flags UNUSED,
		  int argc, const char **argv)
{
    char use_group[BUFSIZ];
    int ctrl;

    ctrl = _pam_parse(pamh, argc, argv, use_group, sizeof(use_group));

    return perform_check(pamh, ctrl, use_group);
}

#ifdef PAM_STATIC

/* static module data */

struct pam_module _pam_wheel_modstruct = {
    "pam_wheel",
    pam_sm_authenticate,
    pam_sm_setcred,
    pam_sm_acct_mgmt,
    NULL,
    NULL,
    NULL,
    NULL,
};

#endif /* PAM_STATIC */

/*
 * Copyright (c) Cristian Gafton <gafton@redhat.com>, 1996, 1997
 *                                              All rights reserved
 *
 * 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.
 */