Annotation of src/usr.bin/at/privs.h, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: privs.h,v 1.3 1996/12/22 03:39:24 tholo 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.1 deraadt 69: #define RELINQUISH_PRIVS { \
1.4 ! millert 70: real_uid = getuid(); \
! 71: effective_uid = geteuid(); \
! 72: real_gid = getgid(); \
! 73: effective_gid = getegid(); \
! 74: setegid(real_gid); \
! 75: seteuid(real_uid); \
1.1 deraadt 76: }
77:
1.4 ! millert 78: #define RELINQUISH_PRIVS_ROOT(a, b) { \
1.1 deraadt 79: real_uid = (a); \
80: effective_uid = geteuid(); \
1.4 ! millert 81: real_gid = (b); \
! 82: effective_gid = getegid(); \
! 83: setegid(real_gid); \
1.1 deraadt 84: seteuid(real_uid); \
85: }
86:
87: #define PRIV_START { \
1.4 ! millert 88: seteuid(effective_uid); \
! 89: setegid(effective_gid); \
! 90: }
1.1 deraadt 91:
1.4 ! millert 92: #define PRIV_END { \
! 93: setegid(real_gid); \
1.1 deraadt 94: seteuid(real_uid); \
95: }
96:
97: #endif