privs.h revision 10154
110154Sache/*
210154Sache *  privs.h - header for privileged operations
37767Sache *  Copyright (C) 1993  Thomas Koenig
4941Snate *
510154Sache * Redistribution and use in source and binary forms, with or without
610154Sache * modification, are permitted provided that the following conditions
710154Sache * are met:
810154Sache * 1. Redistributions of source code must retain the above copyright
910154Sache *    notice, this list of conditions and the following disclaimer.
1010154Sache * 2. The name of the author(s) may not be used to endorse or promote
1110154Sache *    products derived from this software without specific prior written
1210154Sache *    permission.
13941Snate *
1410154Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1510154Sache * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1610154Sache * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1710154Sache * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1810154Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1910154Sache * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2010154Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2110154Sache * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2210154Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2310154Sache * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24941Snate */
25941Snate
26941Snate#ifndef _PRIVS_H
27941Snate#define _PRIVS_H
28941Snate
297767Sache#ifndef _USE_BSD
307767Sache#define _USE_BSD 1
31941Snate#include <unistd.h>
327767Sache#undef _USE_BSD
337767Sache#else
347767Sache#include <unistd.h>
357767Sache#endif
36941Snate
377767Sache/* Relinquish privileges temporarily for a setuid or setgid program
38941Snate * with the option of getting them back later.  This is done by swapping
39941Snate * the real and effective userid BSD style.  Call RELINQUISH_PRIVS once
40941Snate * at the beginning of the main program.  This will cause all operatons
41941Snate * to be executed with the real userid.  When you need the privileges
427767Sache * of the setuid/setgid invocation, call PRIV_START; when you no longer
43941Snate * need it, call PRIV_END.  Note that it is an error to call PRIV_START
44941Snate * and not PRIV_END within the same function.
45941Snate *
467767Sache * Use RELINQUISH_PRIVS_ROOT(a,b) if your program started out running
47941Snate * as root, and you want to drop back the effective userid to a
48941Snate * and the effective group id to b, with the option to get them back
49941Snate * later.
50941Snate *
51941Snate * If you no longer need root privileges, but those of some other
527767Sache * userid/groupid, you can call REDUCE_PRIV(a,b) when your effective
53941Snate * is the user's.
54941Snate *
55941Snate * Problems: Do not use return between PRIV_START and PRIV_END; this
56941Snate * will cause the program to continue running in an unprivileged
57941Snate * state.
58941Snate *
59941Snate * It is NOT safe to call exec(), system() or popen() with a user-
60941Snate * supplied program (i.e. without carefully checking PATH and any
61941Snate * library load paths) with relinquished privileges; the called program
62941Snate * can aquire them just as easily.  Set both effective and real userid
63941Snate * to the real userid before calling any of them.
64941Snate */
65941Snate
66941Snate#ifndef MAIN
67941Snateextern
68941Snate#endif
69941Snateuid_t real_uid, effective_uid;
70941Snate
7110154Sache#ifndef MAIN
727767Sacheextern
737767Sache#endif
747767Sachegid_t real_gid, effective_gid;
757767Sache
76941Snate#define RELINQUISH_PRIVS { \
777767Sache			      real_uid = getuid(); \
787767Sache			      effective_uid = geteuid(); \
797767Sache			      real_gid = getgid(); \
807767Sache			      effective_gid = getegid(); \
818112Sache			      setreuid(effective_uid, real_uid); \
827767Sache			      setregid(effective_gid, real_gid); \
837767Sache		          }
84941Snate
857767Sache#define RELINQUISH_PRIVS_ROOT(a,b) { \
867767Sache			      real_uid = (a); \
877767Sache			      effective_uid = geteuid(); \
887767Sache			      real_gid = (b); \
897767Sache			      effective_gid = getegid(); \
9010154Sache			      setregid(effective_gid, real_gid); \
918112Sache			      setreuid(effective_uid, real_uid); \
927767Sache		          }
93941Snate
947767Sache#define PRIV_START {\
957767Sache		    setreuid(real_uid, effective_uid); \
967767Sache		    setregid(real_gid, effective_gid);
97941Snate
98941Snate#define PRIV_END \
9910154Sache		    setregid(effective_gid, real_gid); \
1008112Sache		    setreuid(effective_uid, real_uid); \
1017767Sache		    }
102941Snate
1037767Sache#define REDUCE_PRIV(a,b) {\
1047767Sache			setreuid(real_uid, effective_uid); \
1057767Sache			setregid(real_gid, effective_gid); \
1067767Sache			effective_uid = (a); \
1077767Sache			effective_gid = (b); \
1088112Sache			setregid(effective_gid, real_gid); \
1098112Sache			setreuid(effective_uid, real_uid); \
1107767Sache		    }
111941Snate#endif
112