1 /* 2 * privs.h - header for privileged operations 3 * Copyright (C) 1993 Thomas Koenig 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. The name of the author(s) may not be used to endorse or promote 11 * products derived from this software without specific prior written 12 * permission. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef _PRIVS_H 27 #define _PRIVS_H 28 29 #ifndef _USE_BSD 30 #define _USE_BSD 1 31 #include <unistd.h> 32 #undef _USE_BSD 33 #else 34 #include <unistd.h> 35 #endif 36 37 #ifdef __FreeBSD__ 38 /* 39 * setre[ug]id() not change r[ug]id for FreeBSD, but check it incorrectly 40 * for this program 41 */ 42 #define setreuid(r, e) seteuid(e) 43 #define setregid(r, e) setegid(e) 44 #define SET_REAL_PRIV(a, b) {\ 45 setgid(b); \ 46 setuid(a); \ 47 } 48 #else 49 #define SET_REAL_PRIV(a. b) {\ 50 setregid((b), real_gid); \ 51 setreuid((a), real_uid); \ 52 } 53 #endif 54 55 /* Relinquish privileges temporarily for a setuid or setgid program 56 * with the option of getting them back later. This is done by swapping 57 * the real and effective userid BSD style. Call RELINQUISH_PRIVS once 58 * at the beginning of the main program. This will cause all operatons 59 * to be executed with the real userid. When you need the privileges 60 * of the setuid/setgid invocation, call PRIV_START; when you no longer 61 * need it, call PRIV_END. Note that it is an error to call PRIV_START 62 * and not PRIV_END within the same function. 63 * 64 * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running 65 * as root, and you want to drop back the effective userid to a 66 * and the effective group id to b, with the option to get them back 67 * later. 68 * 69 * If you no longer need root privileges, but those of some other 70 * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective 71 * is the user's. 72 * 73 * Problems: Do not use return between PRIV_START and PRIV_END; this 74 * will cause the program to continue running in an unprivileged 75 * state. 76 * 77 * It is NOT safe to call exec(), system() or popen() with a user- 78 * supplied program (i.e. without carefully checking PATH and any 79 * library load paths) with relinquished privileges; the called program 80 * can aquire them just as easily. Set both effective and real userid 81 * to the real userid before calling any of them. 82 */ 83 84 #ifndef MAIN 85 extern 86 #endif 87 uid_t real_uid, effective_uid; 88 89 #ifndef MAIN 90 extern 91 #endif 92 gid_t real_gid, effective_gid; 93 94 #define RELINQUISH_PRIVS { \ 95 real_uid = getuid(); \ 96 effective_uid = geteuid(); \ 97 real_gid = getgid(); \ 98 effective_gid = getegid(); \ 99 setregid(effective_gid, real_gid); \ 100 setreuid(effective_uid, real_uid); \ 101 } 102 103 #define RELINQUISH_PRIVS_ROOT(a,b) { \ 104 real_uid = (a); \ 105 effective_uid = geteuid(); \ 106 real_gid = (b); \ 107 effective_gid = getegid(); \ 108 setregid(effective_gid, real_gid); \ 109 setreuid(effective_uid, real_uid); \ 110 } 111 112 #define PRIV_START {\ 113 setreuid(real_uid, effective_uid); \ 114 setregid(real_gid, effective_gid); 115 116 #define PRIV_END \ 117 setregid(effective_gid, real_gid); \ 118 setreuid(effective_uid, real_uid); \ 119 } 120 121 #define REDUCE_PRIV(a,b) {\ 122 setreuid(real_uid, effective_uid); \ 123 setregid(real_gid, effective_gid); \ 124 effective_uid = (a); \ 125 effective_gid = (b); \ 126 SET_REAL_PRIV(a, b); \ 127 } 128 #endif 129