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