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