/*-1* privs.h - header for privileged operations2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (C) 1993 Thomas Koenig6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. The name of the author(s) may not be used to endorse or promote13* products derived from this software without specific prior written14* permission.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#ifndef _PRIVS_H29#define _PRIVS_H3031#include <unistd.h>3233/* Relinquish privileges temporarily for a setuid or setgid program34* with the option of getting them back later. This is done by35* utilizing POSIX saved user and group IDs. Call RELINQUISH_PRIVS once36* at the beginning of the main program. This will cause all operations37* to be executed with the real userid. When you need the privileges38* of the setuid/setgid invocation, call PRIV_START; when you no longer39* need it, call PRIV_END. Note that it is an error to call PRIV_START40* and not PRIV_END within the same function.41*42* Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running43* as root, and you want to drop back the effective userid to a44* and the effective group id to b, with the option to get them back45* later.46*47* If you no longer need root privileges, but those of some other48* userid/groupid, you can call REDUCE_PRIV(a,b) when your effective49* is the user's.50*51* Problems: Do not use return between PRIV_START and PRIV_END; this52* will cause the program to continue running in an unprivileged53* state.54*55* It is NOT safe to call exec(), system() or popen() with a user-56* supplied program (i.e. without carefully checking PATH and any57* library load paths) with relinquished privileges; the called program58* can acquire them just as easily. Set both effective and real userid59* to the real userid before calling any of them.60*/6162extern uid_t real_uid, effective_uid;63extern gid_t real_gid, effective_gid;6465#ifdef MAIN66uid_t real_uid, effective_uid;67gid_t real_gid, effective_gid;68#endif6970#define RELINQUISH_PRIVS { \71real_uid = getuid(); \72effective_uid = geteuid(); \73real_gid = getgid(); \74effective_gid = getegid(); \75if (seteuid(real_uid) != 0) err(1, "seteuid failed"); \76if (setegid(real_gid) != 0) err(1, "setegid failed"); \77}7879#define RELINQUISH_PRIVS_ROOT(a, b) { \80real_uid = (a); \81effective_uid = geteuid(); \82real_gid = (b); \83effective_gid = getegid(); \84if (setegid(real_gid) != 0) err(1, "setegid failed"); \85if (seteuid(real_uid) != 0) err(1, "seteuid failed"); \86}8788#define PRIV_START { \89if (seteuid(effective_uid) != 0) err(1, "seteuid failed"); \90if (setegid(effective_gid) != 0) err(1, "setegid failed"); \91}9293#define PRIV_END { \94if (setegid(real_gid) != 0) err(1, "setegid failed"); \95if (seteuid(real_uid) != 0) err(1, "seteuid failed"); \96}9798#define REDUCE_PRIV(a, b) { \99PRIV_START \100effective_uid = (a); \101effective_gid = (b); \102if (setregid((gid_t)-1, effective_gid) != 0) err(1, "setregid failed"); \103if (setreuid((uid_t)-1, effective_uid) != 0) err(1, "setreuid failed"); \104PRIV_END \105}106#endif107108109