librostlab
1.0.20
Toggle main menu visibility
Loading...
Searching...
No Matches
rostlab
euid_egid_resource.h
Go to the documentation of this file.
1
/*
2
Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
3
4
This file is part of librostlab.
5
6
librostlab is free software: you can redistribute it and/or modify
7
it under the terms of the GNU Lesser General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU Lesser General Public License for more details.
15
16
You should have received a copy of the GNU Lesser General Public License
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
#ifndef ROSTLAB_EUID_EGID
20
#define ROSTLAB_EUID_EGID 1
21
22
#include <errno.h>
23
#include <sstream>
24
#include <stdexcept>
25
#include <string.h>
26
#include <sys/types.h>
27
#include <unistd.h>
28
29
#include "
rostlab/rostlab_stdexcept.h
"
30
31
namespace
rostlab
{
32
33
class
euid_egid_resource
34
{
35
private
:
36
uid_t _olduid;
37
gid_t _oldgid;
38
bool
_changeduid;
39
bool
_changedgid;
40
// this is a resource - disable copy contructor and copy assignment
41
euid_egid_resource(
const
euid_egid_resource& ){};
42
euid_egid_resource&
43
operator=(
const
euid_egid_resource&){
return
*
this
;};
44
45
public
:
46
euid_egid_resource
( uid_t __neweuid, gid_t __newegid ) : _changeduid(false), _changedgid(false)
47
{
48
_olduid = getuid();
49
_oldgid = getgid();
50
if
( getegid() != __newegid )
51
{
52
if
( setregid( getegid(), __newegid ) ){ std::ostringstream s; s <<
"failed to setregid "
<< getegid() <<
":"
<< __newegid <<
" : "
<< strerror( errno );
throw
runtime_error
( s.str() ); }
53
_changedgid =
true
;
54
}
55
if
( geteuid() != __neweuid )
56
{
57
if
( setreuid( geteuid(), __neweuid ) ){ std::ostringstream s; s <<
"failed to setreuid "
<< geteuid() <<
":"
<< __neweuid <<
" : "
<< strerror( errno );
throw
runtime_error
( s.str() ); }
58
_changeduid =
true
;
59
}
60
}
61
62
virtual
~euid_egid_resource
()
63
{
64
if
( _changeduid )
65
{
66
if
( setreuid( geteuid(), getuid() ) ){ std::ostringstream s; s <<
"failed revert setreuid to "
<< geteuid() <<
":"
<< getuid() <<
" : "
<< strerror( errno );
throw
runtime_error
( s.str() ); }
67
if
( setreuid( _olduid, -1 ) ){ std::ostringstream s; s <<
"failed revert setreuid to "
<< _olduid <<
":-1"
<<
" : "
<< strerror( errno );
throw
runtime_error
( s.str() ); }
68
}
69
if
( _changedgid )
70
{
71
if
( setregid( getegid(), getgid() ) ){ std::ostringstream s; s <<
"failed revert setregid to "
<< getegid() <<
":"
<< getgid() <<
" : "
<< strerror( errno );
throw
runtime_error
( s.str() ); }
72
if
( setregid( _oldgid, -1 ) ){ std::ostringstream s; s <<
"failed revert setregid to "
<< _oldgid <<
":-1"
<<
" : "
<< strerror( errno );
throw
runtime_error
( s.str() ); }
73
}
74
}
75
};
76
77
};
78
79
#endif
// ROSTLAB_EUID_EGID
80
// vim:et:ai:ts=2:
rostlab::euid_egid_resource::euid_egid_resource
euid_egid_resource(uid_t __neweuid, gid_t __newegid)
Definition
euid_egid_resource.h:46
rostlab::euid_egid_resource::~euid_egid_resource
virtual ~euid_egid_resource()
Definition
euid_egid_resource.h:62
rostlab::runtime_error
Definition
rostlab_stdexcept.h:69
rostlab
Definition
aux_functions.h:27
rostlab_stdexcept.h
Generated on
for librostlab by
1.17.0