e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech/*
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech SSSD
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech NSS Responder
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech Copyright (C) Petr Čech <pcech@redhat.com> 2016
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech This program is free software; you can redistribute it and/or modify
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech it under the terms of the GNU General Public License as published by
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech the Free Software Foundation; either version 3 of the License, or
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech (at your option) any later version.
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech This program is distributed in the hope that it will be useful,
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech but WITHOUT ANY WARRANTY; without even the implied warranty of
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech GNU General Public License for more details.
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech You should have received a copy of the GNU General Public License
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech along with this program. If not, see <http://www.gnu.org/licenses/>.
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech*/
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech#include <sys/types.h>
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech#include <pwd.h>
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech#include <grp.h>
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech#include "util/util.h"
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech#include "responder/common/negcache_files.h"
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech#define BUFFER_SIZE 16384
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cechbool is_user_local_by_name(const char *name)
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech{
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct passwd pwd = { 0 };
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct passwd *pwd_result;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech char buffer[BUFFER_SIZE];
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech bool is_local = false;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech int ret;
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek char *shortname = NULL;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek ret = sss_parse_internal_fqname(NULL, name, &shortname, NULL);
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek if (ret != EOK) {
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek return false;
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek }
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek ret = getpwnam_r(shortname, &pwd, buffer, BUFFER_SIZE, &pwd_result);
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek talloc_free(shortname);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech if (ret == EOK && pwd_result != NULL) {
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech DEBUG(SSSDBG_TRACE_FUNC, "User %s is a local user\n", name);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech is_local = true;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech }
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech return is_local;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech}
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cechbool is_user_local_by_uid(uid_t uid)
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech{
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct passwd pwd = { 0 };
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct passwd *pwd_result;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech char buffer[BUFFER_SIZE];
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech bool is_local = false;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech int ret;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech ret = getpwuid_r(uid, &pwd, buffer, BUFFER_SIZE, &pwd_result);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech if (ret == EOK && pwd_result != NULL) {
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech DEBUG(SSSDBG_TRACE_FUNC,
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech "User with UID %"SPRIuid" is a local user\n", uid);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech is_local = true;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech }
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech return is_local;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech}
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cechbool is_group_local_by_name(const char *name)
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech{
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct group grp = { 0 };
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct group *grp_result;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech char buffer[BUFFER_SIZE];
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech bool is_local = false;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech int ret;
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek char *shortname = NULL;
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek ret = sss_parse_internal_fqname(NULL, name, &shortname, NULL);
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek if (ret != EOK) {
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek return false;
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek }
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek ret = getgrnam_r(shortname, &grp, buffer, BUFFER_SIZE, &grp_result);
27bf39ed3e197497cf4aca58038d788ea5b5ddbcJakub Hrozek talloc_free(shortname);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech if (ret == EOK && grp_result != NULL) {
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech DEBUG(SSSDBG_TRACE_FUNC, "Group %s is a local group\n", name);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech is_local = true;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech }
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech return is_local;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech}
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cechbool is_group_local_by_gid(uid_t gid)
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech{
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct group grp = { 0 };
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech struct group *grp_result;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech char buffer[BUFFER_SIZE];
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech bool is_local = false;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech int ret;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech ret = getgrgid_r(gid, &grp, buffer, BUFFER_SIZE, &grp_result);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech if (ret == EOK && grp_result != NULL) {
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech DEBUG(SSSDBG_TRACE_FUNC,
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech "Group with GID %"SPRIgid" is a local group\n", gid);
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech is_local = true;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech }
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech return is_local;
e7ccfb139388c947ec2dee16cfe3005f5643b90dPetr Cech}