[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.5

1.5     ! millert     1: /*     $OpenBSD: privs.h,v 1.4 1997/03/01 23:40:12 millert Exp $       */
1.1       deraadt     2: /*     $NetBSD: privs.h,v 1.3 1995/03/25 18:13:41 glass Exp $  */
                      3:
1.4       millert     4: /*
                      5:  *  privs.h - header for privileged operations
                      6:  *  Copyright (C) 1993  Thomas Koenig
1.1       deraadt     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.
1.4       millert    20:  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1.1       deraadt    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: #ifndef _PRIVS_H
                     30: #define _PRIVS_H
                     31:
                     32: #include <unistd.h>
                     33:
1.4       millert    34: /* Relinquish privileges temporarily for a setuid or setgid program
                     35:  * with the option of getting them back later.  This is done by
                     36:  * utilizing POSIX saved user and groups ids.  Call RELINQUISH_PRIVS once
1.1       deraadt    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
1.4       millert    39:  * of the setuid/setgid invocation, call PRIV_START; when you no longer
1.1       deraadt    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:  *
1.4       millert    43:  * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
1.1       deraadt    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:  * Problems: Do not use return between PRIV_START and PRIV_END; this
                     49:  * will cause the program to continue running in an unprivileged
                     50:  * state.
                     51:  *
                     52:  * It is NOT safe to call exec(), system() or popen() with a user-
                     53:  * supplied program (i.e. without carefully checking PATH and any
                     54:  * library load paths) with relinquished privileges; the called program
                     55:  * can aquire them just as easily.  Set both effective and real userid
                     56:  * to the real userid before calling any of them.
                     57:  */
                     58:
                     59: #ifndef MAIN
                     60: extern
                     61: #endif
                     62: uid_t real_uid, effective_uid;
                     63:
1.4       millert    64: #ifndef MAIN
                     65: extern
                     66: #endif
                     67: gid_t real_gid, effective_gid;
                     68:
1.5     ! millert    69: #define RELINQUISH_PRIVS do {                  \
        !            70:       real_uid = getuid();                     \
        !            71:       effective_uid = geteuid();               \
        !            72:       real_gid = getgid();                     \
        !            73:       effective_gid = getegid();               \
        !            74:       setegid(real_gid);                       \
        !            75:       seteuid(real_uid);                       \
        !            76: } while (0)
        !            77:
        !            78: #define RELINQUISH_PRIVS_ROOT(a, b) do {       \
        !            79:        real_uid = (a);                         \
        !            80:        effective_uid = geteuid();              \
        !            81:        real_gid = (b);                         \
        !            82:        effective_gid = getegid();              \
        !            83:        setegid(real_gid);                      \
        !            84:        seteuid(real_uid);                      \
        !            85: } while (0)
        !            86:
        !            87: #define PRIV_START do {                                \
        !            88:        seteuid(effective_uid);                 \
        !            89:        setegid(effective_gid);                 \
        !            90: } while (0)
        !            91:
        !            92: #define PRIV_END do {                          \
        !            93:        setegid(real_gid);                      \
        !            94:        seteuid(real_uid);                      \
        !            95: } while (0)
1.1       deraadt    96:
1.5     ! millert    97: #endif /* _PRIVS_H */