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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
*
* Copyright (c) 2012-2015
* name : Francis Banyikwa
* email: mhogomchungu@gmail.com
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "../lib/includes.h"
#include <errno.h>
#include <unistd.h>
#include <grp.h>
#include "../constants.h"
#include <sys/types.h>
#include <pwd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/*
* This source file makes sure the user who started the tool( usually non root user ) has permission
* to perform operations they want on paths they presented.
*
* This feature allows tradition unix permissions to be set on a paths to control non user access to volumes
*/
/*
* zuluCryptUserIsAMemberOfAGroup() was moved to ../lib/mount_fs_options.c
*/
#define ZULUDEBUG 0
static uid_t _original_UID ;
static const char * _run_time_path ;
/*
* set the function to be called when an attempt to evelate or downgrade privileges fail.
*/
static void ( *zuluCryptSecurityPrivilegeElevationError )( const char * ) = NULL ;
const char * zuluCryptRunTimePath()
{
return _run_time_path ;
}
void zuluCryptExeSetOriginalUID( uid_t s )
{
_original_UID = s ;
}
int zuluCryptExeOriginalUserIsNotRoot()
{
return _original_UID != 0 ;
}
int zuluCryptSecurityGainElevatedPrivileges( void )
{
/*
* printf( "GAINING:uid=%d:euid=%d\n",getuid(),geteuid() ) ;
*/
if( seteuid( 0 ) == 0 ){
return 1 ;
}else{
if( zuluCryptSecurityPrivilegeElevationError ){
zuluCryptSecurityPrivilegeElevationError( "WARNING: failed to seteuid root" ) ;
}
}
return 0 ;
}
int zuluCryptSecurityConvertUID( uid_t uid,const char * u_id )
{
extern char ** environ ;
const char * sudo_uid = NULL ;
char ** e = environ ;
const char * s ;
for( ; *e != NULL ; e++ ){
s = *e ;
if( StringPrefixMatch( s,"SUDO_UID=",9 ) ){
sudo_uid = s + 9 ;
break ;
}
}
if( u_id != NULL ){
if( uid == 0 ){
return (int)StringConvertToInt( u_id ) ;
}else{
return -1 ;
}
}else if( sudo_uid != NULL ){
if( uid == 0 ){
return (int)StringConvertToInt( sudo_uid ) ;
}else{
return -1 ;
}
}else{
return (int)uid ;
}
}
uid_t global_variable_user_uid ;
void zuluCryptSetUserUIDForPrivilegeManagement( uid_t uid )
{
global_variable_user_uid = uid ;
}
void zuluCryptSecuritySetPrivilegeElevationErrorFunction( void ( *f ) ( const char * ) )
{
zuluCryptSecurityPrivilegeElevationError = f ;
}
int zuluCryptSecurityDropElevatedPrivileges( void )
{
/*
printf( "DROPPING:uid=%d:euid=%d\n",getuid(),geteuid() ) ;
*/
if( seteuid( global_variable_user_uid ) != 0 ){
if( zuluCryptSecurityPrivilegeElevationError ){
zuluCryptSecurityPrivilegeElevationError( "ERROR: seteuid() failed" ) ;
}
}
return 1 ;
}
void zuluCryptSecuritySanitizeTheEnvironment( uid_t uid,stringList_t * stx )
{
extern char ** environ ;
const char ** env = ( const char ** ) environ ;
ssize_t index ;
string_t xt ;
stringList_t stl = StringListVoid ;
string_t st ;
StringListIterator it ;
StringListIterator end ;
/*
* First,we make a copy of the enviromental varibales
* Second,we clear the enviromental variable because we dont want it
* Third,we return a copy of the enviromental variable because we want to pass it along
* the plugins
*/
while( *env ){
stl = StringListAppend( stl,*env ) ;
env++ ;
}
StringListGetIterators( stl,&it,&end ) ;
while( it != end ){
st = *it ;
it++ ;
index = StringIndexOfChar( st,0,'=' ) ;
if( index >= 0 ){
unsetenv( StringSubChar( st,(size_t)index,'\0' ) ) ;
StringSubChar( st,(size_t)index,'=' ) ;
}
}
xt = String( "/tmp/zuluCrypt-" ) ;
StringAppendInt( xt,uid ) ;
StringListAppendString_1( &stl,&xt ) ;
_run_time_path = StringListContentAtLast( stl ) ;
*stx = stl ;
}
int zuluCryptSecurityUserOwnTheFile( const char * device,uid_t uid )
{
if( device && uid ){}
return 0 ;
}
void zuluCryptSecurityLockMemory_1( string_t st )
{
if( st != StringVoid ){
mlock( StringContent( st ),StringLength( st ) ) ;
}
}
void zuluCryptSecurityUnlockMemory_1( string_t st )
{
void * e ;
size_t f ;
if( st != StringVoid ){
e = ( void * )StringContent( st ) ;
f = StringLength( st ) ;
memset( e,'\0',f ) ;
munlock( e,f ) ;
}
}
void zuluCryptSecurityLockMemory( stringList_t stl )
{
StringListIterator it ;
StringListIterator end ;
string_t st ;
StringListGetIterators( stl,&it,&end ) ;
while( it != end ){
st = *it ;
it++ ;
mlock( StringContent( st ),StringLength( st ) ) ;
}
}
void zuluCryptSecurityUnlockMemory( stringList_t stl )
{
StringListIterator it ;
StringListIterator end ;
string_t st ;
void * e ;
size_t f ;
StringListGetIterators( stl,&it,&end ) ;
while( it != end ){
st = *it ;
it++ ;
if( st != StringVoid ){
e = ( void * )StringContent( st ) ;
f = StringLength( st ) ;
memset( e,'\0',f ) ;
munlock( e,f ) ;
}
}
}
void zuluCryptSecurityPrintPermissions( void )
{
puts( "----------------------" ) ;
printf( "uid:%d\neuid:%d\n",(int)getuid(),(int)geteuid() ) ;
puts( "----------------------" ) ;
}
|