c - override mkdir with LD_PRELOAD -


i'm trying modify syscall mkdir(), filter users don't want them create directories, maybe not elegant way it, want know why it's not working.

the mkdir() replacement code is:

#define _gnu_source #define maxgrupos 30 #define dim(x) (sizeof(x)/sizeof(x[0])) #define true 1 #define false 0 #include <stdio.h> #include <stdint.h> #include <stdlib.h> // //me permite llamar al metodo original #include <dlfcn.h> //codigos de error #include <errno.h>  //mkdir() #include <sys/stat.h> #include <sys/types.h>  //para obtener los usuarios #include <unistd.h>  //parser de la config #include <libconfig.h> //##################################################### #define ruta_config "/etc/samba/gruposhabilitados.txt" //#####################################################     int *obtenergruposvalidos(){          int  indice = 1;          int (*gruposvalidos) = malloc(sizeof (int) * maxgrupos);          config_t cfg, *cf;         const config_setting_t *grupos;         int count = 0, n = 0, activo = 0;          //add root          gruposvalidos[0]=0;          //init parser         cf = &cfg;         config_init(cf);           if (!config_read_file(cf, ruta_config )) {            puts("error al parsear\n");            config_destroy(cf);            return gruposvalidos;         }           if(config_lookup_bool(cf, "carpetas.activo", &activo)){                 if( activo == 0 ){                            config_destroy(cf);                             gruposvalidos[1] = (int) getgid();                            return gruposvalidos;                 }//if                 else{                          grupos = config_lookup(cf, "carpetas.gruposhabilitados");                          count = config_setting_length(grupos);                          (n = 0; n < count; n++) {                                 printf("grupo: %d\n", (int) config_setting_get_int_elem(grupos, n));                                  gruposvalidos[n+1] = ((int) config_setting_get_int_elem(grupos, n));                         }                             config_destroy(cf);                 }//else         }//if             indice = n+1;         while ( indice < maxgrupos  )                 gruposvalidos[indice++] = -1;          return gruposvalidos;   }//obtenergruposvalidos   int verificargrupos(void){   gid_t gids[maxgrupos];   int *gruposvalidos = obtenergruposvalidos();   int count, curr, aux;     if ((count = getgroups(dim(gids), gids)) == -1){     perror("getgroups() error");     return false;   }     else {     fflush(null);//me tiraba un error sin esto     (curr=0; curr<count; curr++){         for(aux=0; aux < maxgrupos; aux++){               printf("%i-%i) guid: %d - grupovalido: %d\n",curr,aux,((int) gids[curr]) ,(int) ((gruposvalidos)[aux]));                if  (((int) gids[curr]) == ((int) ((gruposvalidos)[aux]) )){                 free(gruposvalidos);                 return true;                 }//if         }//for      }//for      free(gruposvalidos);     return false;   }//else }  int mkdir(const char *pathname, mode_t mode){   int habilitadoaescribir = false;    puts("false mkdir");     habilitadoaescribir = verificargrupos();    if ( habilitadoaescribir == true ){          int (*mkdir_real)(const char *pathname, mode_t mode);         mkdir_real = dlsym(rtld_next,"mkdir");          mkdir_real(pathname, mode);         return 0;    }    else{          errno=eperm;         return(errno);    }  } int rmdir(const char *pathname){   int habilitadoaescribir = false;    habilitadoaescribir = verificargrupos();    if ( habilitadoaescribir == true ){          int (*rmdir_real)(const char *pathname);         rmdir_real = dlsym(rtld_next,"rmdir");         rmdir_real(pathname);         return 0;    }    else{         errno=eperm;         return(errno);        } } 

compilling -wall -std=c99 -fpic -lconfig -ldl -shared

when use mkdir binary, works perfectly

ld_preload=wrapper.so /bin/mkdir adirectory

but when use way:

ld_preload=wrapper.so bindfs b mkdir() not used.

i've tryed implementation, dosn't cover needs, works binfs

#include <errno.h> int mkdir() { errno = eperm; return -1; } int rmdir() { errno = eperm; return -1; } 


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -