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
23 * $FreeBSD: src/contrib/libpam/modules/pam_wheel/pam_wheel.c,v 1.3.2.2 2001/06/11 15:28:35 markm Exp $
24 * $DragonFly: src/contrib/libpam/modules/pam_wheel/Attic/pam_wheel.c,v 1.2 2003/06/17 04:24:03 dillon Exp $
34 #include <sys/types.h>
39 * here, we make a definition for the externally accessible function
40 * in this file (this definition is required for static a module
41 * but strongly encouraged generally) it is used to instruct the
42 * modules include file to define the function prototypes.
47 #include <security/pam_modules.h>
51 static void _pam_log(int err, const char *format, ...)
55 va_start(args, format);
56 openlog("PAM-Wheel", LOG_CONS|LOG_PID, LOG_AUTH);
57 vsyslog(err, format, args);
62 /* checks if a user is on a list of members of the GID 0 group */
64 static int is_on_list(char * const *list, const char *member)
67 if (strcmp(*list, member) == 0)
74 /* argument parsing */
76 #define PAM_DEBUG_ARG 0x0001
77 #define PAM_USE_UID_ARG 0x0002
78 #define PAM_TRUST_ARG 0x0004
79 #define PAM_DENY_ARG 0x0010
81 static int _pam_parse(int argc, const char **argv, char *use_group)
85 /* step through arguments */
86 for (ctrl=0; argc-- > 0; ++argv) {
90 if (!strcmp(*argv,"debug"))
91 ctrl |= PAM_DEBUG_ARG;
92 else if (!strcmp(*argv,"use_uid"))
93 ctrl |= PAM_USE_UID_ARG;
94 else if (!strcmp(*argv,"trust"))
95 ctrl |= PAM_TRUST_ARG;
96 else if (!strcmp(*argv,"deny"))
98 else if (!strncmp(*argv,"group=",6))
99 strcpy(use_group,*argv+6);
101 _pam_log(LOG_ERR,"pam_parse: unknown option; %s",*argv);
109 /* --- authentication management functions (only) --- */
112 int pam_sm_authenticate(pam_handle_t *pamh,int flags,int argc
116 const char *username;
118 struct passwd *pwd, *tpwd;
120 int retval = PAM_AUTH_ERR;
121 char use_group[BUFSIZ];
123 /* Init the optional group */
124 bzero(use_group,BUFSIZ);
126 ctrl = _pam_parse(argc, argv, use_group);
127 retval = pam_get_user(pamh,&username,NULL);
128 if ((retval != PAM_SUCCESS) || (!username)) {
129 if (ctrl & PAM_DEBUG_ARG)
130 _pam_log(LOG_DEBUG,"can not get the username");
131 return PAM_SERVICE_ERR;
134 /* su to a uid 0 account ? */
135 pwd = getpwnam(username);
137 if (ctrl & PAM_DEBUG_ARG)
138 _pam_log(LOG_NOTICE,"unknown user %s",username);
139 return PAM_USER_UNKNOWN;
142 /* Now we know that the username exists, pass on to other modules...
143 * the call to pam_get_user made this obsolete, so is commented out
145 * pam_set_item(pamh,PAM_USER,(const void *)username);
148 /* is this user an UID 0 account ? */
150 /* no need to check for wheel */
154 if (ctrl & PAM_USE_UID_ARG) {
155 tpwd = getpwuid(getuid());
157 if (ctrl & PAM_DEBUG_ARG)
158 _pam_log(LOG_NOTICE,"who is running me ?!");
159 return PAM_SERVICE_ERR;
161 fromsu = tpwd->pw_name;
165 if (ctrl & PAM_DEBUG_ARG)
166 _pam_log(LOG_NOTICE,"who is running me ?!");
167 return PAM_SERVICE_ERR;
172 if ((grp = getgrnam("wheel")) == NULL) {
176 grp = getgrnam(use_group);
178 if (!grp || !grp->gr_mem) {
179 if (ctrl & PAM_DEBUG_ARG) {
181 _pam_log(LOG_NOTICE,"no members in a GID 0 group");
183 _pam_log(LOG_NOTICE,"no members in '%s' group",use_group);
185 if (ctrl & PAM_DENY_ARG)
186 /* if this was meant to deny access to the members
187 * of this group and the group does not exist, allow
195 if (is_on_list(grp->gr_mem, fromsu)) {
196 if (ctrl & PAM_DEBUG_ARG)
197 _pam_log(LOG_NOTICE,"Access %s to '%s' for '%s'",
198 (ctrl & PAM_DENY_ARG)?"denied":"granted",
200 if (ctrl & PAM_DENY_ARG)
201 return PAM_PERM_DENIED;
203 if (ctrl & PAM_TRUST_ARG)
209 if (ctrl & PAM_DEBUG_ARG)
210 _pam_log(LOG_NOTICE,"Access %s for '%s' to '%s'",
211 (ctrl & PAM_DENY_ARG)?"granted":"denied",fromsu,username);
212 if (ctrl & PAM_DENY_ARG)
215 return PAM_PERM_DENIED;
219 int pam_sm_setcred(pam_handle_t *pamh,int flags,int argc
226 /* end of module definition */
228 PAM_MODULE_ENTRY("pam_wheel");
231 * Copyright (c) Cristian Gafton <gafton@redhat.com>, 1996, 1997
232 * All rights reserved
234 * Redistribution and use in source and binary forms, with or without
235 * modification, are permitted provided that the following conditions
237 * 1. Redistributions of source code must retain the above copyright
238 * notice, and the entire permission notice in its entirety,
239 * including the disclaimer of warranties.
240 * 2. Redistributions in binary form must reproduce the above copyright
241 * notice, this list of conditions and the following disclaimer in the
242 * documentation and/or other materials provided with the distribution.
243 * 3. The name of the author may not be used to endorse or promote
244 * products derived from this software without specific prior
245 * written permission.
247 * ALTERNATIVELY, this product may be distributed under the terms of
248 * the GNU Public License, in which case the provisions of the GPL are
249 * required INSTEAD OF the above restrictions. (This clause is
250 * necessary due to a potential bad interaction between the GPL and
251 * the restrictions contained in a BSD-style copyright.)
253 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
254 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
255 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
256 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
257 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
258 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
259 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
261 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
262 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
263 * OF THE POSSIBILITY OF SUCH DAMAGE.