Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix default permissions and umask handling #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix default permissions and umask handling
On linux files are usually created with permissions set to 666+umask,
and folders with 777+umask.

The codebase used to create files with 600 and folders with 700
*unless* a umask override was set in the configuration, then both
files and folders would end up with 777+umask.

This patch addresses this by setting the umask override on process
level; Creating files with permissions set to 666 and folders set to
777 - with either the overridden or the system default umask applied.

Signed-off-by: Johannes Schneider <johannes.schneider@leica-geosystems.com>
  • Loading branch information
js731ca committed Apr 27, 2024
commit a6f0a476ef3826061b65d50a0713ed9d8b3e6c8d
9 changes: 4 additions & 5 deletions src/mtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ int parse_incomming_dataset(mtp_ctx * ctx,void * datain,int size,uint32_t * newh

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

restore_giduid(ctx);
Expand All @@ -288,9 +288,6 @@ int parse_incomming_dataset(mtp_ctx * ctx,void * datain,int size,uint32_t * newh
return ret_code;
}

if(ctx->usb_cfg.val_umask >= 0)
chmod(tmp_path, 0777 & (~ctx->usb_cfg.val_umask));

tmp_file_entry.isdirectory = 1;
strcpy(tmp_file_entry.filename,tmp_str);
tmp_file_entry.size = 0;
Expand Down Expand Up @@ -362,7 +359,9 @@ int parse_incomming_dataset(mtp_ctx * ctx,void * datain,int size,uint32_t * newh

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

restore_giduid(ctx);
Expand Down
2 changes: 2 additions & 0 deletions src/mtp_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include "mtp.h"
Expand Down Expand Up @@ -760,6 +761,7 @@ int mtp_load_config_file(mtp_ctx * context, const char * conffile)
PRINT_MSG("Show hidden files : %i",context->usb_cfg.show_hidden_files);
if(context->usb_cfg.val_umask >= 0)
{
umask(context->usb_cfg.val_umask);
PRINT_MSG("File creation umask : %03o",context->usb_cfg.val_umask);
}
else
Expand Down
7 changes: 3 additions & 4 deletions src/mtp_operations/mtp_op_sendobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ uint32_t mtp_op_SendObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr, int
if( mtp_packet_hdr->code == MTP_OPERATION_SEND_PARTIAL_OBJECT )
file = open(full_path,O_RDWR | O_LARGEFILE);
else
file = open(full_path,O_CREAT|O_WRONLY|O_TRUNC| O_LARGEFILE, S_IRUSR|S_IWUSR);
file = open(full_path,
O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
}

restore_giduid(ctx);
Expand Down Expand Up @@ -150,9 +152,6 @@ uint32_t mtp_op_SendObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr, int

close(file);

if(ctx->usb_cfg.val_umask >= 0)
chmod(full_path, 0777 & (~ctx->usb_cfg.val_umask));

if(ctx->cancel_req)
{
ctx->cancel_req = 0;
Expand Down