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