diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2002-10-30 20:15:30 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2002-10-30 20:15:30 +0000 |
commit | 8b855eebd1cada447595924e5d32c1c859e07ff2 (patch) | |
tree | fd45309c752ff4f14c2a81b5993db10012f1433f | |
parent | 60b14187d21c7733684258d7f3443339972d610f (diff) |
Add [gs]etres[ug]id(2) syscall to libc and use it in emulating some 4.3BSD
functions.
-rw-r--r-- | include/unistd.h | 10 | ||||
-rw-r--r-- | lib/libc/compat-43/Makefile.inc | 7 | ||||
-rw-r--r-- | lib/libc/compat-43/setregid.c | 27 | ||||
-rw-r--r-- | lib/libc/compat-43/setreuid.c | 27 | ||||
-rw-r--r-- | lib/libc/compat-43/setrgid.c | 6 | ||||
-rw-r--r-- | lib/libc/compat-43/setruid.c | 6 | ||||
-rw-r--r-- | lib/libc/sys/Makefile.inc | 19 | ||||
-rw-r--r-- | lib/libc/sys/setresuid.2 | 96 |
8 files changed, 162 insertions, 36 deletions
diff --git a/include/unistd.h b/include/unistd.h index a8305e4ebcf..6c9718c5d89 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: unistd.h,v 1.43 2002/10/25 21:55:28 millert Exp $ */ +/* $OpenBSD: unistd.h,v 1.44 2002/10/30 20:15:29 millert Exp $ */ /* $NetBSD: unistd.h,v 1.26.4.1 1996/05/28 02:31:51 mrg Exp $ */ /*- @@ -146,6 +146,8 @@ long gethostid(void); int gethostname(char *, size_t); mode_t getmode(const void *, mode_t); int getpagesize(void); +int getresgid(gid_t *, gid_t *, gid_t *); +int getresuid(uid_t *, uid_t *, uid_t *); char *getpass(const char *); char *getusershell(void); char *getwd(char *); /* obsoleted by getcwd() */ @@ -194,8 +196,10 @@ int setkey(const char *); int setlogin(const char *); void *setmode(const char *); int setpgrp(pid_t pid, pid_t pgrp); /* obsoleted by setpgid() */ -int setregid(int, int); -int setreuid(int, int); +int setregid(gid_t, gid_t); +int setresgid(gid_t, gid_t, gid_t); +int setresuid(uid_t, uid_t, uid_t); +int setreuid(uid_t, uid_t); int setrgid(gid_t); int setruid(uid_t); void setusershell(void); diff --git a/lib/libc/compat-43/Makefile.inc b/lib/libc/compat-43/Makefile.inc index 58227126664..1bf6ec18537 100644 --- a/lib/libc/compat-43/Makefile.inc +++ b/lib/libc/compat-43/Makefile.inc @@ -1,11 +1,10 @@ -# $OpenBSD: Makefile.inc,v 1.4 2000/03/28 22:43:34 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.5 2002/10/30 20:15:29 millert Exp $ # compat-43 sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/compat-43 ${LIBCSRCDIR}/compat-43 -SRCS+= __setreuid.c __setregid.c creat.c getdtablesize.c gethostid.c \ - getwd.c killpg.c sethostid.c setpgrp.c setregid.c setreuid.c \ - setrgid.c setruid.c sigcompat.c +SRCS+= creat.c getdtablesize.c gethostid.c getwd.c killpg.c sethostid.c \ + setpgrp.c setregid.c setreuid.c setrgid.c setruid.c sigcompat.c MAN+= creat.3 getdtablesize.3 gethostid.3 killpg.3 setregid.3 setreuid.3 \ setruid.3 sigblock.3 sigpause.3 sigsetmask.3 sigvec.3 diff --git a/lib/libc/compat-43/setregid.c b/lib/libc/compat-43/setregid.c index ee25b1ac0b3..084b049a369 100644 --- a/lib/libc/compat-43/setregid.c +++ b/lib/libc/compat-43/setregid.c @@ -32,21 +32,36 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: setregid.c,v 1.4 2002/02/16 21:27:21 millert Exp $"; +static char *rcsid = "$OpenBSD: setregid.c,v 1.5 2002/10/30 20:15:29 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> #include <unistd.h> -extern int __setregid(gid_t, gid_t); - #ifndef NO_WARN_REFERENCES __warn_references(setregid, "warning: this program uses setregid(), which is deprecated."); #endif int -setregid(rgid, egid) - int rgid, egid; +setregid(gid_t rgid, gid_t egid) { - return (__setregid(rgid, egid)); + int error; + gid_t sgid, cur_rgid, cur_egid, cur_sgid; + + if (error == (getresgid(&cur_rgid, &cur_egid, &cur_sgid)) != 0) + return (error); + + /* + * The saved gid presents a bit of a dilemma, as it did not + * appear in 4.3BSD. We only set the saved gid when the real + * gid is specified and either its value would change, or, + * where the saved and effective gids are different. + */ + if (rgid != (gid_t)-1 && (rgid != cur_rgid || + cur_sgid != (egid != (gid_t)-1 ? egid : cur_egid))) + sgid = rgid; + else + sgid = (gid_t)-1; + + return (setresgid(rgid, egid, sgid)); } diff --git a/lib/libc/compat-43/setreuid.c b/lib/libc/compat-43/setreuid.c index 7b48b2b0d15..5da47c8e1f3 100644 --- a/lib/libc/compat-43/setreuid.c +++ b/lib/libc/compat-43/setreuid.c @@ -32,19 +32,34 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: setreuid.c,v 1.5 2002/02/16 21:27:21 millert Exp $"; +static char *rcsid = "$OpenBSD: setreuid.c,v 1.6 2002/10/30 20:15:29 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> #include <unistd.h> -extern int __setreuid(uid_t, uid_t); - __warn_references(setreuid, "warning: this program uses setreuid(), which is deprecated."); int -setreuid(ruid, euid) - int ruid, euid; +setreuid(uid_t ruid, uid_t euid) { - return (__setreuid(ruid, euid)); + int error; + uid_t suid, cur_ruid, cur_euid, cur_suid; + + if (error == (getresuid(&cur_ruid, &cur_euid, &cur_suid)) != 0) + return (error); + + /* + * The saved uid presents a bit of a dilemma, as it did not + * appear in 4.3BSD. We only set the saved uid when the real + * uid is specified and either its value would change, or, + * where the saved and effective uids are different. + */ + if (ruid != (uid_t)-1 && (ruid != cur_ruid || + cur_suid != (euid != (uid_t)-1 ? euid : cur_euid))) + suid = ruid; + else + suid = (uid_t)-1; + + return (setresuid(ruid, euid, suid)); } diff --git a/lib/libc/compat-43/setrgid.c b/lib/libc/compat-43/setrgid.c index 1ce12ce86ac..a7829632010 100644 --- a/lib/libc/compat-43/setrgid.c +++ b/lib/libc/compat-43/setrgid.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: setrgid.c,v 1.8 2002/02/19 19:39:36 millert Exp $"; +static char *rcsid = "$OpenBSD: setrgid.c,v 1.9 2002/10/30 20:15:29 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -40,10 +40,8 @@ static char *rcsid = "$OpenBSD: setrgid.c,v 1.8 2002/02/19 19:39:36 millert Exp __warn_references(setrgid, "warning: this program uses setrgid(), which is deprecated."); -extern int __setregid(gid_t, gid_t); - int setrgid(gid_t rgid) { - return (__setregid(rgid, (gid_t)-1)); + return (setresgid(rgid, (gid_t)-1, rgid)); } diff --git a/lib/libc/compat-43/setruid.c b/lib/libc/compat-43/setruid.c index 78d7372e791..a99f5700d8f 100644 --- a/lib/libc/compat-43/setruid.c +++ b/lib/libc/compat-43/setruid.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: setruid.c,v 1.8 2002/02/19 19:39:36 millert Exp $"; +static char *rcsid = "$OpenBSD: setruid.c,v 1.9 2002/10/30 20:15:29 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -40,10 +40,8 @@ static char *rcsid = "$OpenBSD: setruid.c,v 1.8 2002/02/19 19:39:36 millert Exp __warn_references(setruid, "warning: this program uses setruid(), which is deprecated."); -extern int __setreuid(uid_t, uid_t); - int setruid(uid_t ruid) { - return (__setreuid(ruid, (uid_t)-1)); + return (setresuid(ruid, (uid_t)-1, ruid)); } diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc index 89f0f848e18..7ae2a594698 100644 --- a/lib/libc/sys/Makefile.inc +++ b/lib/libc/sys/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.56 2002/08/05 22:42:12 art Exp $ +# $OpenBSD: Makefile.inc,v 1.57 2002/10/30 20:15:29 millert Exp $ # $NetBSD: Makefile.inc,v 1.35 1995/10/16 23:49:07 jtc Exp $ # @(#)Makefile.inc 8.1 (Berkeley) 6/17/93 @@ -40,8 +40,8 @@ ASM= accept.o access.o acct.o adjtime.o bind.o chdir.o chflags.o chmod.o \ getfh.o getfsstat.o getgid.o getgroups.o getitimer.o getpeereid.o \ getpeername.o \ getpgid.o \ - getpgrp.o getpid.o getppid.o getpriority.o getrlimit.o getrusage.o \ - getsid.o \ + getpgrp.o getpid.o getppid.o getpriority.o getresgid.o getresuid.o \ + getrlimit.o getrusage.o getsid.o \ getsockname.o getsockopt.o gettimeofday.o getuid.o issetugid.o \ ioctl.o kill.o kevent.o kqueue.o \ ktrace.o lchown.o lfs_bmapv.o lfs_markv.o lfs_segclean.o lfs_segwait.o \ @@ -53,7 +53,8 @@ ASM= accept.o access.o acct.o adjtime.o bind.o chdir.o chflags.o chmod.o \ read.o readlink.o readv.o reboot.o recvfrom.o recvmsg.o rename.o \ revoke.o rmdir.o select.o semget.o semop.o sendmsg.o sendto.o \ setegid.o seteuid.o setgid.o setgroups.o setitimer.o setpgid.o \ - setpriority.o setrlimit.o setsid.o setsockopt.o settimeofday.o \ + setpriority.o setresgid.o setresuid.o setrlimit.o setsid.o \ + setsockopt.o settimeofday.o \ setuid.o shmat.o shmctl.o shmdt.o shmget.o shutdown.o sigaction.o \ sigaltstack.o socket.o socketpair.o stat.o statfs.o swapon.o swapctl.o \ symlink.o sync.o sysarch.o umask.o undelete.o unlink.o unmount.o \ @@ -223,11 +224,11 @@ MAN+= accept.2 access.2 acct.2 adjtime.2 bind.2 brk.2 chdir.2 chflags.2 \ nfssvc.2 open.2 pathconf.2 \ pipe.2 profil.2 poll.2 ptrace.2 quotactl.2 read.2 readlink.2 reboot.2 \ recv.2 rename.2 revoke.2 rfork.2 rmdir.2 select.2 send.2 setgroups.2 \ - setpgid.2 setsid.2 setuid.2 shutdown.2 sigaction.2 sigaltstack.2 \ - sigpending.2 sigprocmask.2 sigreturn.2 sigstack.2 sigsuspend.2 \ - socket.2 socketpair.2 stat.2 statfs.2 swapctl.2 symlink.2 sync.2 \ - sysarch.2 syscall.2 truncate.2 umask.2 unlink.2 utimes.2 vfork.2 \ - wait.2 write.2 + setpgid.2 setresuid.2 setsid.2 setuid.2 shutdown.2 sigaction.2 \ + sigaltstack.2 sigpending.2 sigprocmask.2 sigreturn.2 sigstack.2 \ + sigsuspend.2 socket.2 socketpair.2 stat.2 statfs.2 swapctl.2 symlink.2 \ + sync.2 sysarch.2 syscall.2 truncate.2 umask.2 unlink.2 utimes.2 \ + vfork.2 wait.2 write.2 MAN+= extattr_get_file.2 MLINKS+=extattr_get_file.2 extattr_set_file.2 \ diff --git a/lib/libc/sys/setresuid.2 b/lib/libc/sys/setresuid.2 new file mode 100644 index 00000000000..523a072c539 --- /dev/null +++ b/lib/libc/sys/setresuid.2 @@ -0,0 +1,96 @@ +.\" $OpenBSD: setresuid.2,v 1.1 2002/10/30 20:15:29 millert Exp $ +.\" +.\" Copyright (c) 2000 +.\" Sheldon Hearn. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES +.\" +.\" $FreeBSD: src/lib/libc/sys/setresuid.2,v 1.12 2001/10/01 16:09:02 ru Exp $ +.\" +.Dd October 20, 2002 +.Dt SETRESUID 2 +.Os +.Sh NAME +.Nm getresgid , +.Nm getresuid , +.Nm setresgid , +.Nm setresuid +.Nd "get or set real, effective and saved user or group ID" +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <unistd.h> +.Ft int +.Fn getresgid "gid_t *rgid" "gid_t *egid" "gid_t *sgid" +.Ft int +.Fn getresuid "uid_t *ruid" "uid_t *euid" "uid_t *suid" +.Ft int +.Fn setresgid "gid_t rgid" "gid_t egid" "gid_t sgid" +.Ft int +.Fn setresuid "uid_t ruid" "uid_t euid" "uid_t suid" +.Sh DESCRIPTION +The +.Fn setresuid +function sets the real, +effective and saved user IDs of the current process. +The analogous +.Fn setresgid +sets the real, effective and saved group IDs. +.Pp +Privileged processes may set these IDs to arbitrary values. +Unprivileged processes are restricted +in that each of the new IDs must match one of the current IDs. +.Pp +Passing \-1 as an argument causes the corresponding value +to remain unchanged. +.Pp +The +.Fn getresgid +and +.Fn getresuid +calls retrieve the real, effective, and saved group and user IDs of +the current process, respectively. +.Sh RETURN VALUES +Upon success, these functions return 0; otherwise \-1 is returned. +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EPERM +The calling process was not privileged and tried to change one or +more IDs to a value which was not the current real ID, the current +effective ID nor the current saved ID. +.It Bq Er EFAULT +An address passed to +.Fn getresgid +or +.Fn getresuid +was invalid. +.El +.Sh SEE ALSO +.Xr getegid 2 , +.Xr geteuid 2 , +.Xr getgid 2 , +.Xr getuid 2 , +.Xr issetugid 2 , +.Xr setgid 2 , +.Xr setregid 2 , +.Xr setreuid 2 , +.Xr setuid 2 +.Sh STANDARDS +These functions are not part of the +.St -p1003.1 +specification and should not be used where portability is desired. +.Sh HISTORY +These functions first appeared in HP-UX. |