File: privs.h

package info (click to toggle)
proftpd 1.2.0pre9-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,392 kB
  • ctags: 2,648
  • sloc: ansic: 24,012; sh: 1,754; makefile: 536; perl: 281
file content (121 lines) | stat: -rw-r--r-- 4,084 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * ProFTPD - FTP server daemon
 * Copyright (c) 1997, 1998 Public Flood Software
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 */

/* $Id: privs.h,v 1.2 1999/10/01 03:49:07 macgyver Exp $
 */

#ifndef __PRIVS_H
#define __PRIVS_H

/* Macros for manipulating saved, real and effective uid for easy 
 * switching from/to root.
 *
 * Note: In version 1.1.5, all of this changed.  We USED to play games
 * with the saved-uid/gid _and_ setreuid()/setregid(), however this
 * appears to be slightly non-portable (i.e. w/ BSDs).  However, since
 * POSIX.1 saved-uids are pretty much useless without setre* (in the
 * case of root), so we now use basic uid swapping if we have seteuid(),
 * and setreuid() swapping if not.
 */

/* Porters, please put the most reasonable and secure method of
 * doing this in here:
 */

#ifdef __hpux
#define setreuid(x,y) setresuid(x,y,0)
#endif

#if !defined(HAVE_SETEUID)
 
/* Use setreuid() to perform uid swapping.
 */

#define PRIVS_SETUP(u,g)	{ if(getuid()) { \
				  session.ouid = session.uid = (int)getuid(); \
				  session.gid = (int)getgid(); \
                                  setgid(session.gid); \
                                  setreuid(session.uid,session.uid); \
				} else {  \
                                  session.ouid = (int)getuid(); \
                                  session.uid = (u); session.gid = (g); \
                                  setgid(session.gid); \
				  setreuid(0,session.uid); \
				} }

#define PRIVS_ROOT		{ log_debug(DEBUG4,"ROOT %s %d", \
				  __FILE__, __LINE__); \
				  if(!session.disable_id_switching) \
				    { setreuid(session.uid,0); \
				} }

#define PRIVS_RELINQUISH	{ log_debug(DEBUG4,"NONROOT %s %d", \
				  __FILE__, __LINE__); \
				  if(!session.disable_id_switching) \
				    { setreuid(session.uid,session.uid); \
				} }

#define PRIVS_REVOKE		{ setreuid(0,0); \
				  setgid(session.gid); \
                                  setuid(session.uid); }
#else /* HAVE_SETEUID */

/* Set the saved uid/gid using setuid/seteuid().  setreuid() is
 * no longer used as it is considered obsolete on many systems.
 * gids are also no longer swapped, as they are unnecessary.
 * If run as root, proftpd now normally runs as:
 *   real user            : root
 *   effective user       : <user>
 *   saved user           : root
 *   real/eff/saved group : <group>
 */

#define PRIVS_SETUP(u,g)	{ if(getuid()) { \
                                  session.ouid = session.uid = (int)getuid(); \
                                  session.gid = (int)getgid(); \
                                  setgid(session.gid); \
                                  setuid(session.uid); \
				  seteuid(session.uid); \
                                } else { \
				  session.ouid = (int)getuid(); \
                                  session.uid = (u); session.gid = (g); \
                                  setuid(0); \
                                  setgid((g)); seteuid((u)); \
                                } }


/* Switch back to root */

#define PRIVS_ROOT		if(!session.disable_id_switching) \
				{ seteuid(0); }

/* Relinquish privs granted by PRIVS_ROOT */

#define PRIVS_RELINQUISH	if(!session.disable_id_switching) \
				{ seteuid(session.uid); }

/* Revoke all privs */

#define PRIVS_REVOKE		{ seteuid(0); \
				  setgid(session.gid); \
				  setuid(session.uid); }

#endif /* HAVE_SETEUID */

#endif /* __PRIVS_H */