Skip to content

Commit

Permalink
WIP : use user defined UID / GID for storage file operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdelnero committed Dec 8, 2021
1 parent f5aa119 commit 43c0f89
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 35 deletions.
15 changes: 13 additions & 2 deletions conf/umtprd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

loop_on_disconnect 0

# Force the default UID/GID to use for each storage file operations.
# Note : When no default UID/GID are specified the umtprd process UID/GID
# are used instead.

#default_uid 33
#default_gid 33

# Storage command : Create add a storage entry point. Up to 16 entry points supported
# Syntax : storage "PATH" "NAME" "OPTIONS"
# Possible store options :
Expand All @@ -20,11 +27,15 @@ storage "/" "root folder" "rw"
storage "/home" "home folder" "ro"
storage "/www" "www folder" "ro,notmounted"

# add the "locked" option to enable the store lock/unlock feature.
# execute "umtprd -cmd:unlock" to unlock the locked stores and "umtprd -cmd:lock" to lock them again.
# Add the "locked" option to enable the store lock/unlock feature.
# Execute "umtprd -cmd:unlock" to unlock the locked stores and "umtprd -cmd:lock" to lock them again.

storage "/" "lockable root folder" "rw,locked"

# A user storage using another UID/GID for file operations.

storage "/home/user" "user folder" "rw,locked,uid=33,gid=33"

#
# Uncomment the following line if you want to
# override the system default umask value for
Expand Down
13 changes: 11 additions & 2 deletions inc/mtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ typedef struct mtp_storage_
char * description;
uint32_t storage_id;
uint32_t flags;
int uid;
int gid;
}mtp_storage;

#define UMTP_STORAGE_LOCKED 0x00000010
Expand Down Expand Up @@ -132,6 +134,12 @@ typedef struct mtp_ctx_

int no_inotify;

int uid,euid;
int gid,egid;

int default_uid;
int default_gid;

volatile int cancel_req;
volatile int transferring_file_data;
}mtp_ctx;
Expand All @@ -143,9 +151,10 @@ void mtp_set_usb_handle(mtp_ctx * ctx, void * handle, uint32_t max_packet_size);

int mtp_load_config_file(mtp_ctx * context, const char * conffile);

uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, uint32_t flags);
uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, int uid, int gid, uint32_t flags);
int mtp_remove_storage(mtp_ctx * ctx, char * name);
int mtp_get_storage_index_by_name(mtp_ctx * ctx, char * name);
int mtp_get_storage_index_by_id(mtp_ctx * ctx, uint32_t storage_id);
uint32_t mtp_get_storage_id_by_name(mtp_ctx * ctx, char * name);
char * mtp_get_storage_description(mtp_ctx * ctx, uint32_t storage_id);
char * mtp_get_storage_root(mtp_ctx * ctx, uint32_t storage_id);
Expand All @@ -161,6 +170,6 @@ int build_response(mtp_ctx * ctx, uint32_t tx_id, uint16_t type, uint16_t status
int check_and_send_USB_ZLP(mtp_ctx * ctx , int size);
int parse_incomming_dataset(mtp_ctx * ctx,void * datain,int size,uint32_t * newhandle, uint32_t parent_handle, uint32_t storage_id);

#define APP_VERSION "v1.5.1"
#define APP_VERSION "v1.6.1"

#endif
6 changes: 6 additions & 0 deletions inc/mtp_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ uint64_t peek64(void * buffer, int index, int typesize);
int poke_string(void * buffer, int index, int maxsize, const char *str);
int poke_array(void * buffer, int index, int maxsize, int size, int elementsize, const unsigned char *bufferin,int prefixed);
uint16_t posix_to_mtp_errcode(int err);

int set_giduid(mtp_ctx * ctx,int uid,int gid);

int set_storage_giduid(mtp_ctx * ctx,uint32_t storage_id);
int restore_giduid(mtp_ctx * ctx);

#endif
16 changes: 14 additions & 2 deletions src/fs_handles_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <dirent.h>

#include "mtp.h"
#include "mtp_helpers.h"

#include "fs_handles_db.h"
#include "inotify.h"
#include "logs_out.h"
Expand Down Expand Up @@ -448,7 +450,10 @@ int scan_and_add_folder(fs_handles_db * db, char * base, uint32_t parent, uint32
}
}while(entry);

return 0;
if(dir)
return 0;
else
return -1;
}

fs_entry * init_search_handle(fs_handles_db * db, uint32_t parent, uint32_t storage_id)
Expand Down Expand Up @@ -602,7 +607,14 @@ int entry_open(fs_handles_db * db, fs_entry * entry)
full_path = build_full_path(db,mtp_get_storage_root(db->mtp_ctx, entry->storage_id), entry);
if( full_path )
{
file = open(full_path,O_RDONLY | O_LARGEFILE);
file = -1;

if(!set_storage_giduid(db->mtp_ctx, entry->storage_id))
{
file = open(full_path,O_RDONLY | O_LARGEFILE);
}

restore_giduid(db->mtp_ctx);

if( file == -1 )
PRINT_DEBUG("entry_open : Can't open %s !",full_path);
Expand Down
65 changes: 61 additions & 4 deletions src/mtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ mtp_ctx * mtp_init_responder()
if(!ctx->temp_array)
goto init_error;

ctx->uid = getuid();
ctx->euid = geteuid();
ctx->gid = getgid();
ctx->egid = getegid();

ctx->default_uid = -1;
ctx->default_gid = -1;

ctx->SetObjectPropValue_Handle = 0xFFFFFFFF;

pthread_mutex_init ( &ctx->inotify_mutex, NULL);
Expand Down Expand Up @@ -172,7 +180,7 @@ int parse_incomming_dataset(mtp_ctx * ctx,void * datain,int size,uint32_t * newh
char * parent_folder;
char * tmp_path;
uint32_t storage_flags;
int i,ret_code;
int i,ret_code,ret;
fs_entry * entry;
int file;
filefoundinfo tmp_file_entry;
Expand Down Expand Up @@ -250,7 +258,16 @@ int parse_incomming_dataset(mtp_ctx * ctx,void * datain,int size,uint32_t * newh
sprintf(tmp_path,"%s/%s",parent_folder,tmp_str);
PRINT_DEBUG("MTP_OPERATION_SEND_OBJECT_INFO : Creating %s ...",tmp_path);

if( mkdir(tmp_path, 0700) )
ret = -1;

if(!set_storage_giduid(ctx, entry->storage_id))
{
ret = mkdir(tmp_path, 0700);
}

restore_giduid(ctx);

if( ret )
{
PRINT_WARN("MTP_OPERATION_SEND_OBJECT_INFO : Can't create %s ...",tmp_path);

Expand Down Expand Up @@ -334,7 +351,15 @@ int parse_incomming_dataset(mtp_ctx * ctx,void * datain,int size,uint32_t * newh
strcpy(tmp_file_entry.filename,tmp_str);
tmp_file_entry.size = objectsize;

file = open(tmp_path,O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR|S_IWUSR);
file = -1;

if(!set_storage_giduid(ctx, storage_id))
{
file = open(tmp_path,O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR|S_IWUSR);
}

restore_giduid(ctx);

if( file == -1)
{
PRINT_WARN("MTP_OPERATION_SEND_OBJECT_INFO : Can't create %s ...",tmp_path);
Expand Down Expand Up @@ -654,7 +679,7 @@ void mtp_set_usb_handle(mtp_ctx * ctx, void * handle, uint32_t max_packet_size)
ctx->max_packet_size = max_packet_size;
}

uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, uint32_t flags)
uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, int uid, int gid, uint32_t flags)
{
int i;

Expand All @@ -673,6 +698,9 @@ uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, uint32_

if(ctx->storages[i].root_path && ctx->storages[i].description)
{
ctx->storages[i].uid = uid;
ctx->storages[i].gid = gid;

strcpy(ctx->storages[i].root_path,path);
strcpy(ctx->storages[i].description,description);
ctx->storages[i].flags = flags;
Expand All @@ -698,6 +726,8 @@ uint32_t mtp_add_storage(mtp_ctx * ctx, char * path, char * description, uint32_
ctx->storages[i].description = NULL;
ctx->storages[i].flags = 0x00000000;
ctx->storages[i].storage_id = 0x00000000;
ctx->storages[i].uid = -1;
ctx->storages[i].gid = -1;

return ctx->storages[i].storage_id;
}
Expand All @@ -722,6 +752,8 @@ int mtp_remove_storage(mtp_ctx * ctx, char * name)
ctx->storages[index].description = NULL;
ctx->storages[index].flags = 0x00000000;
ctx->storages[index].storage_id = 0x00000000;
ctx->storages[index].uid = -1;
ctx->storages[index].gid = -1;

return 0;
}
Expand Down Expand Up @@ -778,6 +810,31 @@ int mtp_get_storage_index_by_name(mtp_ctx * ctx, char * name)
return -1;
}

int mtp_get_storage_index_by_id(mtp_ctx * ctx, uint32_t storage_id)
{
int i;

PRINT_DEBUG("mtp_get_storage_index_by_id : 0x%X", storage_id );

i = 0;
while(i < MAX_STORAGE_NB)
{
if( ctx->storages[i].root_path )
{
if( ctx->storages[i].storage_id == storage_id )
{
PRINT_DEBUG("mtp_get_storage_index_by_id : %.8X -> %d",
storage_id,
i );
return i;
}
}
i++;
}

return -1;
}

char * mtp_get_storage_root(mtp_ctx * ctx, uint32_t storage_id)
{
int i;
Expand Down
Loading

0 comments on commit 43c0f89

Please sign in to comment.