4 * Written by Cristian Gafton <gafton@redhat.com> 1996/09/10
5 * See the end of the file for Copyright Information
8 * 1.2 - added 'deny' and 'group=' options
9 * 1.1 - added 'trust' option
10 * 1.0 - the code is working for at least another person, so... :-)
11 * 0.1 - use vsyslog instead of vfprintf/syslog in _pam_log
12 * - return PAM_IGNORE on success (take care of sloppy sysadmins..)
13 * - use pam_get_user instead of pam_get_item(...,PAM_USER,...)
14 * - a new arg use_uid to auth the current uid instead of the
15 * initial (logged in) one.
19 * - try to use make_remark from pam_unix/support.c
20 * - consider returning on failure PAM_FAIL_NOW if the user is not
31 #include <sys/types.h>
36 * here, we make a definition for the externally accessible function
37 * in this file (this definition is required for static a module
38 * but strongly encouraged generally) it is used to instruct the
39 * modules include file to define the function prototypes.
43 #define PAM_SM_ACCOUNT
45 #include <security/pam_modules.h>
46 #include <security/_pam_modutil.h>
50 static void _pam_log(int err, const char *format, ...)
54 va_start(args, format);
55 openlog("PAM-Wheel", LOG_CONS|LOG_PID, LOG_AUTH);
56 vsyslog(err, format, args);
61 /* checks if a user is on a list of members of the GID 0 group */
63 static int is_on_list(char * const *list, const char *member)
65 while (list && *list) {
66 if (strcmp(*list, member) == 0)
73 /* argument parsing */
75 #define PAM_DEBUG_ARG 0x0001
76 #define PAM_USE_UID_ARG 0x0002
77 #define PAM_TRUST_ARG 0x0004
78 #define PAM_DENY_ARG 0x0010
80 static int _pam_parse(int argc, const char **argv, char *use_group,
85 memset(use_group, '\0', group_length);
87 /* step through arguments */
88 for (ctrl=0; argc-- > 0; ++argv) {
92 if (!strcmp(*argv,"debug"))
93 ctrl |= PAM_DEBUG_ARG;
94 else if (!strcmp(*argv,"use_uid"))
95 ctrl |= PAM_USE_UID_ARG;
96 else if (!strcmp(*argv,"trust"))
97 ctrl |= PAM_TRUST_ARG;
98 else if (!strcmp(*argv,"deny"))
100 else if (!strncmp(*argv,"group=",6))
101 strncpy(use_group,*argv+6,group_length-1);
103 _pam_log(LOG_ERR,"pam_parse: unknown option; %s",*argv);
110 static int perform_check(pam_handle_t *pamh, int flags, int ctrl,
111 const char *use_group)
113 const char *username = NULL;
115 struct passwd *pwd, *tpwd = NULL;
117 int retval = PAM_AUTH_ERR;
119 retval = pam_get_user(pamh, &username, NULL);
120 if ((retval != PAM_SUCCESS) || (!username)) {
121 if (ctrl & PAM_DEBUG_ARG) {
122 _pam_log(LOG_DEBUG,"can not get the username");
124 return PAM_SERVICE_ERR;
127 /* su to a uid 0 account ? */
128 pwd = _pammodutil_getpwnam (pamh, username);
130 if (ctrl & PAM_DEBUG_ARG) {
131 _pam_log(LOG_NOTICE,"unknown user %s",username);
133 return PAM_USER_UNKNOWN;
136 if (ctrl & PAM_USE_UID_ARG) {
137 tpwd = _pammodutil_getpwuid (pamh, getuid());
139 if (ctrl & PAM_DEBUG_ARG) {
140 _pam_log(LOG_NOTICE, "who is running me ?!");
142 return PAM_SERVICE_ERR;
144 fromsu = tpwd->pw_name;
146 fromsu = _pammodutil_getlogin(pamh);
148 tpwd = _pammodutil_getpwnam (pamh, fromsu);
150 if (!fromsu || !tpwd) {
151 if (ctrl & PAM_DEBUG_ARG) {
152 _pam_log(LOG_NOTICE, "who is running me ?!");
154 return PAM_SERVICE_ERR;
159 * At this point fromsu = username-of-invoker; tpwd = pwd ptr for fromsu
163 if ((grp = _pammodutil_getgrnam (pamh, "wheel")) == NULL) {
164 grp = _pammodutil_getgrgid (pamh, 0);
167 grp = _pammodutil_getgrnam (pamh, use_group);
170 if (!grp || (!grp->gr_mem && (tpwd->pw_gid != grp->gr_gid))) {
171 if (ctrl & PAM_DEBUG_ARG) {
173 _pam_log(LOG_NOTICE,"no members in a GID 0 group");
175 _pam_log(LOG_NOTICE,"no members in '%s' group", use_group);
178 if (ctrl & PAM_DENY_ARG) {
179 /* if this was meant to deny access to the members
180 * of this group and the group does not exist, allow
190 * test if the user is a member of the group, or if the
191 * user has the "wheel" (sic) group as its primary group.
194 if (is_on_list(grp->gr_mem, fromsu) || (tpwd->pw_gid == grp->gr_gid)) {
196 if (ctrl & PAM_DENY_ARG) {
197 retval = PAM_PERM_DENIED;
199 } else if (ctrl & PAM_TRUST_ARG) {
200 retval = PAM_SUCCESS; /* this can be a sufficient check */
208 if (ctrl & PAM_DENY_ARG) {
210 if (ctrl & PAM_TRUST_ARG) {
211 retval = PAM_SUCCESS; /* this can be a sufficient check */
217 retval = PAM_PERM_DENIED;
221 if (ctrl & PAM_DEBUG_ARG) {
222 if (retval == PAM_IGNORE) {
223 _pam_log(LOG_NOTICE, "Ignoring access request '%s' for '%s'",
226 _pam_log(LOG_NOTICE, "Access %s to '%s' for '%s'",
227 (retval != PAM_SUCCESS) ? "denied":"granted",
235 /* --- authentication management functions --- */
238 int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
241 char use_group[BUFSIZ];
244 ctrl = _pam_parse(argc, argv, use_group, sizeof(use_group));
246 return perform_check(pamh, flags, ctrl, use_group);
250 int pam_sm_setcred(pam_handle_t *pamh,int flags,int argc
257 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc,
260 char use_group[BUFSIZ];
263 ctrl = _pam_parse(argc, argv, use_group, sizeof(use_group));
265 return perform_check(pamh, flags, ctrl, use_group);
270 /* static module data */
272 struct pam_module _pam_wheel_modstruct = {
283 #endif /* PAM_STATIC */
286 * Copyright (c) Cristian Gafton <gafton@redhat.com>, 1996, 1997
287 * All rights reserved
289 * Redistribution and use in source and binary forms, with or without
290 * modification, are permitted provided that the following conditions
292 * 1. Redistributions of source code must retain the above copyright
293 * notice, and the entire permission notice in its entirety,
294 * including the disclaimer of warranties.
295 * 2. Redistributions in binary form must reproduce the above copyright
296 * notice, this list of conditions and the following disclaimer in the
297 * documentation and/or other materials provided with the distribution.
298 * 3. The name of the author may not be used to endorse or promote
299 * products derived from this software without specific prior
300 * written permission.
302 * ALTERNATIVELY, this product may be distributed under the terms of
303 * the GNU Public License, in which case the provisions of the GPL are
304 * required INSTEAD OF the above restrictions. (This clause is
305 * necessary due to a potential bad interaction between the GPL and
306 * the restrictions contained in a BSD-style copyright.)
308 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
309 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
310 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
311 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
312 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
313 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
314 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
315 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
316 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
317 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
318 * OF THE POSSIBILITY OF SUCH DAMAGE.