-/*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
- *
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
- *
- * @APPLE_LICENSE_HEADER_END@
- */
-/*
- * privs.h - header for privileged operations
- * Copyright (c) 1993 by Thomas Koenig
- * All rights reserved.
+/*
+ * privs.h - header for privileged operations
+ * Copyright (C) 1993 Thomas Koenig
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``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 BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
- * $Id: privs.h,v 1.1.1.2 2000/01/11 02:10:05 wsanchez Exp $
+ * $FreeBSD: src/usr.bin/at/privs.h,v 1.8 2001/09/04 16:15:51 ru Exp $
*/
#ifndef _PRIVS_H
#include <unistd.h>
-/* Relinquish privileges temporarily for a setuid program
- * with the option of getting them back later. This is done by swapping
- * the real and effective userid BSD style. Call RELINQUISH_PRIVS once
- * at the beginning of the main program. This will cause all operatons
+/* Relinquish privileges temporarily for a setuid or setgid program
+ * with the option of getting them back later. This is done by
+ * utilizing POSIX saved user and group IDs. Call RELINQUISH_PRIVS once
+ * at the beginning of the main program. This will cause all operations
* to be executed with the real userid. When you need the privileges
- * of the setuid invocation, call PRIV_START; when you no longer
+ * of the setuid/setgid invocation, call PRIV_START; when you no longer
* need it, call PRIV_END. Note that it is an error to call PRIV_START
* and not PRIV_END within the same function.
*
- * Use RELINQUISH_PRIVS_ROOT(a) if your program started out running
+ * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
* as root, and you want to drop back the effective userid to a
* and the effective group id to b, with the option to get them back
* later.
*
* If you no longer need root privileges, but those of some other
- * userid, you can call REDUCE_PRIV(a) when your effective
+ * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective
* is the user's.
*
* Problems: Do not use return between PRIV_START and PRIV_END; this
* It is NOT safe to call exec(), system() or popen() with a user-
* supplied program (i.e. without carefully checking PATH and any
* library load paths) with relinquished privileges; the called program
- * can aquire them just as easily. Set both effective and real userid
+ * can acquire them just as easily. Set both effective and real userid
* to the real userid before calling any of them.
*/
#endif
uid_t real_uid, effective_uid;
+#ifndef MAIN
+extern
+#endif
+gid_t real_gid, effective_gid;
+
#define RELINQUISH_PRIVS { \
real_uid = getuid(); \
effective_uid = geteuid(); \
+ real_gid = getgid(); \
+ effective_gid = getegid(); \
+ setegid(real_gid); \
seteuid(real_uid); \
}
-#define RELINQUISH_PRIVS_ROOT(a) { \
+#define RELINQUISH_PRIVS_ROOT(a, b) { \
real_uid = (a); \
effective_uid = geteuid(); \
+ real_gid = (b); \
+ effective_gid = getegid(); \
+ setegid(real_gid); \
seteuid(real_uid); \
}
#define PRIV_START { \
- seteuid(effective_uid);
+ if (seteuid(0)<0) perr("cannot regain privs"); \
+ if (setegid(effective_gid)<0) perr("cannot reset gid"); \
+ if (seteuid(effective_uid)<0) perr("cannot reset uid"); \
+}
-#define PRIV_END \
- seteuid(real_uid); \
+#define PRIV_END { \
+ if (seteuid(0)<0) perr("cannot regain privs"); \
+ if (setegid(real_gid)<0) perr("cannot reset gid"); \
+ if (seteuid(real_uid)<0) perr("cannot reset uid"); \
}
-#define REDUCE_PRIV(a) { \
- seteuid(effective_uid); \
- real_uid = effective_uid = (a); \
- setuid(real_uid); \
+#define REDUCE_PRIV(a, b) { \
+ PRIV_START \
+ effective_uid = (a); \
+ effective_gid = (b); \
+ setregid((gid_t)-1, effective_gid); \
+ setreuid((uid_t)-1, effective_uid); \
+ PRIV_END \
}
#endif