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