2012-06-28 22:59:51 +04:00
/*
Unix SMB / CIFS implementation .
setXXid ( ) functions for Samba .
Copyright ( C ) Jeremy Allison 2012
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifndef AUTOCONF_TEST
# include "replace.h"
# include "system/passwd.h"
2012-06-30 00:46:23 +04:00
# include "../lib/util/setid.h"
2012-06-28 22:59:51 +04:00
2012-06-30 00:46:23 +04:00
# else
/* Inside autoconf test. */
# if defined(HAVE_UNISTD_H)
# include <unistd.h>
2012-06-28 22:59:51 +04:00
# endif
2012-06-30 00:46:23 +04:00
# include <stdlib.h>
# include <stdio.h>
# include <sys/types.h>
# include <errno.h>
2018-11-20 12:37:01 +03:00
# if defined(HAVE_UNISTD_H)
# include <unistd.h>
# endif
2012-06-30 00:46:23 +04:00
# ifdef HAVE_SYS_PRIV_H
# include <sys/priv.h>
# endif
# ifdef HAVE_SYS_ID_H
# include <sys/id.h>
2012-06-28 22:59:51 +04:00
# endif
2012-06-30 00:46:23 +04:00
/* autoconf tests don't include setid.h */
int samba_setresuid ( uid_t ruid , uid_t euid , uid_t suid ) ;
int samba_setresgid ( gid_t rgid , gid_t egid , gid_t sgid ) ;
int samba_setreuid ( uid_t ruid , uid_t euid ) ;
int samba_setregid ( gid_t rgid , gid_t egid ) ;
int samba_seteuid ( uid_t euid ) ;
int samba_setegid ( gid_t egid ) ;
int samba_setuid ( uid_t uid ) ;
int samba_setgid ( gid_t gid ) ;
int samba_setuidx ( int flags , uid_t uid ) ;
int samba_setgidx ( int flags , gid_t gid ) ;
int samba_setgroups ( size_t setlen , const gid_t * gidset ) ;
# endif
2012-06-28 22:59:51 +04:00
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-06-29 00:41:19 +04:00
# if defined(HAVE_SYSCALL_H)
# include <syscall.h>
# endif
# if defined(HAVE_SYS_SYSCALL_H)
# include <sys/syscall.h>
# endif
2012-07-02 21:22:10 +04:00
/* Ensure we can't compile in a mixed syscall setup. */
# if !defined(USE_LINUX_32BIT_SYSCALLS)
# if defined(SYS_setresuid32) || defined(SYS_setresgid32) || defined(SYS_setreuid32) || defined(SYS_setregid32) || defined(SYS_setuid32) || defined(SYS_setgid32) || defined(SYS_setgroups32)
# error Mixture of 32-bit Linux system calls and 64-bit calls.
# endif
# endif
2012-06-29 00:41:19 +04:00
# endif
2012-06-28 22:59:51 +04:00
/* All the setXX[ug]id functions and setgroups Samba uses. */
int samba_setresuid ( uid_t ruid , uid_t euid , uid_t suid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
return syscall ( SYS_setresuid32 , ruid , euid , suid ) ;
# else
2012-06-29 00:41:19 +04:00
return syscall ( SYS_setresuid , ruid , euid , suid ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETRESUID)
2012-06-28 22:59:51 +04:00
return setresuid ( ruid , euid , suid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setresgid ( gid_t rgid , gid_t egid , gid_t sgid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
return syscall ( SYS_setresgid32 , rgid , egid , sgid ) ;
# else
2012-06-29 00:41:19 +04:00
return syscall ( SYS_setresgid , rgid , egid , sgid ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETRESGID)
2012-06-28 22:59:51 +04:00
return setresgid ( rgid , egid , sgid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setreuid ( uid_t ruid , uid_t euid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
return syscall ( SYS_setreuid32 , ruid , euid ) ;
# else
2012-06-29 00:41:19 +04:00
return syscall ( SYS_setreuid , ruid , euid ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETREUID)
2012-06-28 22:59:51 +04:00
return setreuid ( ruid , euid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setregid ( gid_t rgid , gid_t egid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
return syscall ( SYS_setregid32 , rgid , egid ) ;
# else
2012-06-29 00:41:19 +04:00
return syscall ( SYS_setregid , rgid , egid ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETREGID)
2012-06-28 22:59:51 +04:00
return setregid ( rgid , egid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_seteuid ( uid_t euid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
/* seteuid is not a separate system call. */
return syscall ( SYS_setresuid32 , - 1 , euid , - 1 ) ;
# else
2012-06-29 00:41:19 +04:00
/* seteuid is not a separate system call. */
return syscall ( SYS_setresuid , - 1 , euid , - 1 ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETEUID)
2012-06-28 22:59:51 +04:00
return seteuid ( euid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setegid ( gid_t egid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
/* setegid is not a separate system call. */
return syscall ( SYS_setresgid32 , - 1 , egid , - 1 ) ;
# else
2012-06-29 00:41:19 +04:00
/* setegid is not a separate system call. */
return syscall ( SYS_setresgid , - 1 , egid , - 1 ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETEGID)
2012-06-28 22:59:51 +04:00
return setegid ( egid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setuid ( uid_t uid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
return syscall ( SYS_setuid32 , uid ) ;
# else
2012-06-29 00:41:19 +04:00
return syscall ( SYS_setuid , uid ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETUID)
2012-06-28 22:59:51 +04:00
return setuid ( uid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setgid ( gid_t gid )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
return syscall ( SYS_setgid32 , gid ) ;
# else
2012-06-29 00:41:19 +04:00
return syscall ( SYS_setgid , gid ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETGID)
2012-06-28 22:59:51 +04:00
return setgid ( gid ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setuidx ( int flags , uid_t uid )
{
# if defined(HAVE_SETUIDX)
return setuidx ( flags , uid ) ;
# else
2018-06-28 15:28:34 +03:00
/* HAVE_LINUX_THREAD_CREDENTIALS doesn't have this. */
2012-06-28 22:59:51 +04:00
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setgidx ( int flags , gid_t gid )
{
# if defined(HAVE_SETGIDX)
return setgidx ( flags , gid ) ;
# else
2018-06-28 15:28:34 +03:00
/* HAVE_LINUX_THREAD_CREDENTIALS doesn't have this. */
2012-06-28 22:59:51 +04:00
errno = ENOSYS ;
return - 1 ;
# endif
}
int samba_setgroups ( size_t setlen , const gid_t * gidset )
{
2018-06-28 15:28:34 +03:00
# if defined(HAVE_LINUX_THREAD_CREDENTIALS)
2012-07-02 21:22:10 +04:00
# if defined(USE_LINUX_32BIT_SYSCALLS)
return syscall ( SYS_setgroups32 , setlen , gidset ) ;
# else
2012-06-29 00:41:19 +04:00
return syscall ( SYS_setgroups , setlen , gidset ) ;
2012-07-02 21:22:10 +04:00
# endif
2012-06-29 00:41:19 +04:00
# elif defined(HAVE_SETGROUPS)
2012-06-28 22:59:51 +04:00
return setgroups ( setlen , gidset ) ;
# else
errno = ENOSYS ;
return - 1 ;
# endif
}