mirror of
https://github.com/zhaojh329/rtty.git
synced 2026-02-27 09:53:17 +08:00
file: Change the owner to the operator
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
+18
@@ -181,6 +181,8 @@ bool detect_file_operation(uint8_t *buf, int len, int sid, struct file_context *
|
||||
char fifo_name[128];
|
||||
pid_t pid;
|
||||
int ctlfd;
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
|
||||
if (len != 12)
|
||||
return false;
|
||||
@@ -190,6 +192,16 @@ bool detect_file_operation(uint8_t *buf, int len, int sid, struct file_context *
|
||||
|
||||
memcpy(&pid, buf + 4, 4);
|
||||
|
||||
if (!getuid_pid(pid, &uid)) {
|
||||
kill(pid, SIGTERM);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!getgid_pid(pid, &gid)) {
|
||||
kill(pid, SIGTERM);
|
||||
return true;
|
||||
}
|
||||
|
||||
sprintf(fifo_name, "/tmp/rtty-file-%d.fifo", pid);
|
||||
|
||||
ctlfd = open(fifo_name, O_WRONLY);
|
||||
@@ -218,6 +230,9 @@ bool detect_file_operation(uint8_t *buf, int len, int sid, struct file_context *
|
||||
memset(savepath, 0, sizeof(savepath));
|
||||
getcwd_pid(pid, savepath, sizeof(savepath) - 1);
|
||||
strcat(savepath, "/");
|
||||
|
||||
ctx->uid = uid;
|
||||
ctx->gid = gid;
|
||||
} else {
|
||||
char path[PATH_MAX] = "";
|
||||
char link[128];
|
||||
@@ -284,6 +299,9 @@ static void start_download_file(struct file_context *ctx, struct buffer *info, i
|
||||
|
||||
log_info("download file: %s, size: %u\n", savepath, ctx->total_size);
|
||||
|
||||
if (fchown(fd, ctx->uid, ctx->gid) < 0)
|
||||
log_err("fchown %s fail: %s\n", strerror(errno));
|
||||
|
||||
if (ctx->total_size == 0)
|
||||
close(fd);
|
||||
else
|
||||
|
||||
@@ -51,6 +51,8 @@ struct file_context {
|
||||
int fd;
|
||||
bool busy;
|
||||
int ctlfd;
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
uint32_t total_size;
|
||||
uint32_t remain_size;
|
||||
struct ev_io iof; /* used for upload file */
|
||||
|
||||
+55
-1
@@ -167,4 +167,58 @@ ssize_t getcwd_pid(pid_t pid, char *buf, size_t bufsiz)
|
||||
sprintf(link, "/proc/%d/cwd", pid);
|
||||
|
||||
return readlink(link, buf, bufsiz);
|
||||
}
|
||||
}
|
||||
|
||||
bool getuid_pid(pid_t pid, uid_t *uid)
|
||||
{
|
||||
char status[128];
|
||||
char line[128];
|
||||
int i = 9;
|
||||
FILE *fp;
|
||||
|
||||
sprintf(status, "/proc/%d/status", pid);
|
||||
|
||||
fp = fopen(status, "r");
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
while (i-- > 0) {
|
||||
if (!fgets(line, sizeof(line), fp)) {
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
sscanf(line, "Uid:\t%u", uid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool getgid_pid(pid_t pid, gid_t *gid)
|
||||
{
|
||||
char status[128];
|
||||
char line[128];
|
||||
int i = 10;
|
||||
FILE *fp;
|
||||
|
||||
sprintf(status, "/proc/%d/status", pid);
|
||||
|
||||
fp = fopen(status, "r");
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
while (i-- > 0) {
|
||||
if (!fgets(line, sizeof(line), fp)) {
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
sscanf(line, "Gid:\t%u", gid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -40,4 +40,8 @@ struct mntent *find_mount_point(const char *name);
|
||||
|
||||
ssize_t getcwd_pid(pid_t pid, char *buf, size_t bufsiz);
|
||||
|
||||
bool getuid_pid(pid_t pid, uid_t *uid);
|
||||
|
||||
bool getgid_pid(pid_t pid, gid_t *gid);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user