[BACK]Return to privs.h CVS log [TXT][DIR] Up to [local] / src / usr.bin / at

Annotation of src/usr.bin/at/privs.h, Revision 1.1.1.1

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