#include <sys/cdefs.h>
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#ifdef __APPLE__
#include <membership.h>
#include <stdlib.h>
#endif
#define PAM_SM_AUTH
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/openpam.h>
PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
int argc __unused, const char *argv[] __unused)
{
const char *group, *user;
const void *ruser;
#ifndef __APPLE__
char *const *list;
#endif
struct passwd *pwd;
struct group *grp;
#ifdef __APPLE__
char *str1, *str, *p;
int found_group = 0;
uuid_t u_uuid, g_uuid;
int ismember;
#endif
if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS ||
user == NULL || (pwd = getpwnam(user)) == NULL)
return (PAM_AUTH_ERR);
if (pwd->pw_uid != 0 && openpam_get_option(pamh, "root_only"))
return (PAM_IGNORE);
if (openpam_get_option(pamh, "ruser") &&
(pam_get_item(pamh, PAM_RUSER, &ruser) != PAM_SUCCESS ||
ruser == NULL || (pwd = getpwnam(ruser)) == NULL))
return (PAM_AUTH_ERR);
if ((group = openpam_get_option(pamh, "group")) == NULL)
group = "wheel";
#ifdef __APPLE__
str1 = str = strdup(group);
while ((p = strsep(&str, ",")) != NULL) {
if ((grp = getgrnam(p)) == NULL || grp->gr_mem == NULL)
continue;
if (*grp->gr_mem == NULL)
continue;
found_group = 1;
if (mbr_uid_to_uuid(pwd->pw_uid, u_uuid) != 0)
continue;
if (mbr_gid_to_uuid(grp->gr_gid, g_uuid) != 0)
continue;
if (mbr_check_membership(u_uuid, g_uuid, &ismember) != 0)
continue;
if (ismember)
goto found;
}
if (!found_group)
goto failed;
#else
if ((grp = getgrnam(group)) == NULL || grp->gr_mem == NULL)
goto failed;
if (*grp->gr_mem == NULL)
goto failed;
if (pwd->pw_gid == grp->gr_gid)
goto found;
for (list = grp->gr_mem; *list != NULL; ++list)
if (strcmp(*list, pwd->pw_name) == 0)
goto found;
#endif
not_found:
#ifdef __APPLE__
free(str1);
#endif
if (openpam_get_option(pamh, "deny"))
return (PAM_SUCCESS);
return (PAM_AUTH_ERR);
found:
#ifdef __APPLE__
free(str1);
#endif
if (openpam_get_option(pamh, "deny"))
return (PAM_AUTH_ERR);
return (PAM_SUCCESS);
failed:
if (openpam_get_option(pamh, "fail_safe"))
goto found;
else
goto not_found;
}
PAM_MODULE_ENTRY("pam_group");