2013-03-16 17:54:00 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <grp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/fsuid.h>
|
|
|
|
#include <errno.h>
|
2013-03-20 05:24:17 +00:00
|
|
|
#include <libqubes-rpc-filecopy.h>
|
2013-03-16 17:54:00 +00:00
|
|
|
|
2014-01-12 11:34:50 +00:00
|
|
|
#define DEFAULT_MAX_UPDATES_BYTES (2LL<<30)
|
2013-03-16 17:54:00 +00:00
|
|
|
#define DEFAULT_MAX_UPDATES_FILES 2048
|
2013-03-20 05:24:17 +00:00
|
|
|
|
|
|
|
void notify_progress(int p1, int p2)
|
|
|
|
{
|
|
|
|
}
|
2013-03-16 17:54:00 +00:00
|
|
|
|
2014-01-12 11:38:53 +00:00
|
|
|
int prepare_creds_return_uid(const char *username)
|
2013-03-16 17:54:00 +00:00
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
// First try name
|
|
|
|
pwd = getpwnam(username);
|
|
|
|
if (!pwd) {
|
|
|
|
// Then try UID
|
|
|
|
pwd = getpwuid(atoi(username));
|
|
|
|
if (!pwd) {
|
|
|
|
perror("getpwuid");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setenv("HOME", pwd->pw_dir, 1);
|
|
|
|
setenv("USER", pwd->pw_name, 1);
|
2014-01-12 11:39:58 +00:00
|
|
|
if (setgid(pwd->pw_gid) < 0) {
|
|
|
|
perror("setgid");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-16 17:54:00 +00:00
|
|
|
initgroups(pwd->pw_name, pwd->pw_gid);
|
2014-01-12 11:39:58 +00:00
|
|
|
if (setfsuid(pwd->pw_uid) < 0) {
|
|
|
|
perror("setfsuid");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-16 17:54:00 +00:00
|
|
|
return pwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2014-01-12 11:38:53 +00:00
|
|
|
const char *incoming_dir;
|
2013-03-16 17:54:00 +00:00
|
|
|
int uid;
|
|
|
|
char *var;
|
|
|
|
long long files_limit = DEFAULT_MAX_UPDATES_FILES;
|
|
|
|
long long bytes_limit = DEFAULT_MAX_UPDATES_BYTES;
|
|
|
|
|
|
|
|
if (argc < 3) {
|
2013-11-09 18:05:30 +00:00
|
|
|
fprintf(stderr, "Invalid parameters, usage: %s user dir [-v]\n", argv[0]);
|
2013-03-16 17:54:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((var=getenv("UPDATES_MAX_BYTES")))
|
|
|
|
bytes_limit = atoll(var);
|
|
|
|
if ((var=getenv("UPDATES_MAX_FILES")))
|
|
|
|
files_limit = atoll(var);
|
|
|
|
|
|
|
|
uid = prepare_creds_return_uid(argv[1]);
|
|
|
|
|
|
|
|
incoming_dir = argv[2];
|
|
|
|
mkdir(incoming_dir, 0700);
|
|
|
|
if (chdir(incoming_dir)) {
|
2014-01-12 11:40:20 +00:00
|
|
|
fprintf(stderr, "Error chdir to %s\n", incoming_dir);
|
2013-03-16 17:54:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (chroot(incoming_dir)) {//impossible
|
2014-01-12 11:40:20 +00:00
|
|
|
fprintf(stderr, "Error chroot to %s\n", incoming_dir);
|
2013-03-16 17:54:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-01-12 11:39:58 +00:00
|
|
|
if (setuid(uid) < 0) {
|
|
|
|
perror("setuid");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-16 17:54:00 +00:00
|
|
|
set_size_limit(bytes_limit, files_limit);
|
2013-11-09 18:05:30 +00:00
|
|
|
if (argc > 3 && strcmp(argv[3],"-v")==0)
|
|
|
|
set_verbose(1);
|
2013-03-16 17:54:00 +00:00
|
|
|
return do_unpack();
|
|
|
|
}
|