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(S) 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 * $FreeBSD: src/usr.bin/at/privs.h,v 1.7 1999/12/05 19:57:14 charnier Exp $ 26 * $DragonFly: src/usr.bin/at/privs.h,v 1.3 2004/09/20 13:11:54 joerg Exp $ 27 */ 28 29 #ifndef _PRIVS_H 30 #define _PRIVS_H 31 32 #ifndef _USE_BSD 33 #define _USE_BSD 1 34 #include <unistd.h> 35 #undef _USE_BSD 36 #else 37 #include <unistd.h> 38 #endif 39 40 /* Relinquish privileges temporarily for a setuid or setgid program 41 * with the option of getting them back later. This is done by swapping 42 * the real and effective userid BSD style. Call RELINQUISH_PRIVS once 43 * at the beginning of the main program. This will cause all operations 44 * to be executed with the real userid. When you need the privileges 45 * of the setuid/setgid invocation, call PRIV_START; when you no longer 46 * need it, call PRIV_END. Note that it is an error to call PRIV_START 47 * and not PRIV_END within the same function. 48 * 49 * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running 50 * as root, and you want to drop back the effective userid to a 51 * and the effective group id to b, with the option to get them back 52 * later. 53 * 54 * If you no longer need root privileges, but those of some other 55 * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective 56 * is the user's. 57 * 58 * Problems: Do not use return between PRIV_START and PRIV_END; this 59 * will cause the program to continue running in an unprivileged 60 * state. 61 * 62 * It is NOT safe to call exec(), system() or popen() with a user- 63 * supplied program (i.e. without carefully checking PATH and any 64 * library load paths) with relinquished privileges; the called program 65 * can acquire them just as easily. Set both effective and real userid 66 * to the real userid before calling any of them. 67 */ 68 69 extern uid_t real_uid, effective_uid; 70 extern gid_t real_gid, effective_gid; 71 72 #define RELINQUISH_PRIVS { \ 73 real_uid = getuid(); \ 74 effective_uid = geteuid(); \ 75 real_gid = getgid(); \ 76 effective_gid = getegid(); \ 77 setreuid(effective_uid, real_uid); \ 78 setregid(effective_gid, real_gid); \ 79 } 80 81 #define RELINQUISH_PRIVS_ROOT(a,b) { \ 82 real_uid = (a); \ 83 effective_uid = geteuid(); \ 84 real_gid = (b); \ 85 effective_gid = getegid(); \ 86 setregid(effective_gid, real_gid); \ 87 setreuid(effective_uid, real_uid); \ 88 } 89 90 #define PRIV_START {\ 91 setreuid(real_uid, effective_uid); \ 92 setregid(real_gid, effective_gid); 93 94 #define PRIV_END \ 95 setregid(effective_gid, real_gid); \ 96 setreuid(effective_uid, real_uid); \ 97 } 98 99 #define REDUCE_PRIV(a,b) {\ 100 setreuid(real_uid, effective_uid); \ 101 setregid(real_gid, effective_gid); \ 102 effective_uid = (a); \ 103 effective_gid = (b); \ 104 setregid(effective_gid, real_gid); \ 105 setreuid(effective_uid, real_uid); \ 106 } 107 #endif 108