core-agent-linux/appvm/qubes_add_pendrive_script.c

285 lines
6.9 KiB
C
Raw Normal View History

/*
* The Qubes OS Project, http://www.qubes-os.org
*
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
2010-06-29 12:16:32 +02:00
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <stdlib.h>
2010-06-29 12:16:32 +02:00
#include <xs.h>
#include <syslog.h>
#include <sys/stat.h>
#include <string.h>
#include "dvm.h"
int parse_events(char *buf, int len)
{
int i = 0;
while (i < len) {
2010-06-29 12:16:32 +02:00
struct inotify_event *ev =
(struct inotify_event *) (buf + i);
if ((ev->mask & IN_UNMOUNT) || (ev->mask & IN_IGNORED))
return 1;
i += sizeof(struct inotify_event) + ev->len;
}
return 0;
}
#define BUFLEN 1024
void wait_for_umount(char *name)
{
char buf[BUFLEN];
int fd = inotify_init();
int len;
int ret = inotify_add_watch(fd, name, IN_ATTRIB);
if (ret < 0) {
perror("inotify_add_watch");
return;
}
for (;;) {
len = read(fd, buf, BUFLEN - 1);
if (len <= 0) {
perror("read inotify");
return;
}
if (parse_events(buf, len))
return;
}
}
void background()
{
int i, fd;
for (i = 0; i < 256; i++)
close(i);
fd = open("/dev/null", O_RDWR);
for (i = 0; i <= 2; i++)
dup2(fd, i);
switch (fork()) {
case -1:
exit(1);
2010-06-29 12:16:32 +02:00
case 0:
break;
default:
exit(0);
}
}
int check_legal_filename(char *name)
2010-06-29 12:16:32 +02:00
{
if (index(name, '/')) {
syslog(LOG_DAEMON | LOG_ERR,
"the received filename contains /");
return 0;
2010-06-29 12:16:32 +02:00
}
return 1;
2010-06-29 12:16:32 +02:00
}
void drop_to_user()
{
struct passwd *pw = getpwnam("user");
if (pw)
setuid(pw->pw_uid);
}
2010-06-29 14:35:20 +02:00
int copy_from_xvdh(int destfd, int srcfd, unsigned long long count)
2010-06-29 12:16:32 +02:00
{
int n, size;
char buf[4096];
unsigned long long total = 0;
while (total < count) {
if (count - total > sizeof(buf))
size = sizeof(buf);
else
size = count - total;
n = read(srcfd, buf, size);
if (n != size) {
syslog(LOG_DAEMON | LOG_ERR, "reading xvdh");
2010-06-29 14:35:20 +02:00
return 0;
2010-06-29 12:16:32 +02:00
}
if (write(destfd, buf, size) != size) {
syslog(LOG_DAEMON | LOG_ERR, "writing file");
2010-06-29 14:35:20 +02:00
return 0;
2010-06-29 12:16:32 +02:00
}
total += size;
}
2010-06-29 14:35:20 +02:00
return 1;
2010-06-29 12:16:32 +02:00
}
void redirect_stderr()
{
int fd =
open("/var/log/dvm.log", O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (fd < 0) {
syslog(LOG_DAEMON | LOG_ERR, "open dvm.log");
exit(1);
}
dup2(fd, 2);
}
void suicide(struct xs_handle *xs)
{
xs_write(xs, XBT_NULL, "device/qpen", "killme", 6);
xs_daemon_close(xs);
exit(1);
}
// we are DVM, AppVM sends us a document to work on
// /dev/xvdh contains the dvm_header and document data
2010-06-29 12:16:32 +02:00
void dvm_transaction_request(char *seq, struct xs_handle *xs)
{
char filename[1024], cmdbuf[1024];
struct dvm_header header;
int xvdh_fd, file_fd;
char *src_vm;
unsigned int len;
struct stat stat_pre, stat_post;
2010-06-29 12:16:32 +02:00
xvdh_fd = open("/dev/xvdh", O_RDONLY);
if (read(xvdh_fd, &header, sizeof(header)) != sizeof(header)) {
syslog(LOG_DAEMON | LOG_ERR, "read dvm_header");
suicide(xs);
2010-06-29 12:16:32 +02:00
}
header.name[sizeof(header.name) - 1] = 0;
if (!check_legal_filename(header.name))
suicide(xs);
2010-06-29 12:16:32 +02:00
snprintf(filename, sizeof(filename), "/tmp/%s", header.name);
drop_to_user();
file_fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (file_fd < 0) {
syslog(LOG_DAEMON | LOG_ERR, "open file");
suicide(xs);
2010-06-29 12:16:32 +02:00
}
2010-06-29 14:35:20 +02:00
if (!copy_from_xvdh(file_fd, xvdh_fd, header.file_size))
suicide(xs);
2010-06-29 12:16:32 +02:00
close(xvdh_fd);
close(file_fd);
if (stat(filename, &stat_pre)) {
syslog(LOG_DAEMON | LOG_ERR, "stat pre");
suicide(xs);
}
2010-06-29 12:16:32 +02:00
snprintf(cmdbuf, sizeof(cmdbuf),
"HOME=/home/user DISPLAY=:0 /usr/bin/mimeopen -n -M '/tmp/%s' 2>&1 > /tmp/kde-open.log", header.name);
if (system(cmdbuf))
system("HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file!'");
if (stat(filename, &stat_post)) {
syslog(LOG_DAEMON | LOG_ERR, "stat post");
suicide(xs);
}
2010-06-29 12:16:32 +02:00
src_vm = xs_read(xs, XBT_NULL, "qubes_blocksrc", &len);
xs_write(xs, XBT_NULL, "device/qpen", "umount", 6);
if (stat_pre.st_mtime == stat_post.st_mtime)
suicide(xs);
2010-06-29 12:16:32 +02:00
xs_daemon_close(xs);
// if the modify timestamp of the document file has changed, write back the
// modified content to the requestor AppVM
execl("/usr/lib/qubes/qvm-dvm-transfer", "qvm-dvm-transfer", src_vm,
2010-06-29 12:16:32 +02:00
filename, seq, NULL);
syslog(LOG_DAEMON | LOG_ERR, "execl qvm-dvm-transfer");
suicide(xs);
2010-06-29 12:16:32 +02:00
}
// we are AppVM, DVM sends us a modified document
// /dev/xvdh contains the dvm_header and document data
2010-06-29 12:16:32 +02:00
void dvm_transaction_return(char *seq_string, struct xs_handle *xs)
{
int seq = strtoul(seq_string, 0, 10);
char db_name[1024];
char file_name[1024];
int db_fd, file_fd, xvdh_fd;
struct dvm_header header;
xvdh_fd = open("/dev/xvdh", O_RDONLY);
if (xvdh_fd < 0) {
syslog(LOG_DAEMON | LOG_ERR, "open xvdh");
2010-06-29 14:35:20 +02:00
goto out_err;
2010-06-29 12:16:32 +02:00
}
if (read(xvdh_fd, &header, sizeof(header)) != sizeof(header)) {
syslog(LOG_DAEMON | LOG_ERR, "read dvm_header");
2010-06-29 14:35:20 +02:00
goto out_err;
2010-06-29 12:16:32 +02:00
}
drop_to_user();
// read the file name for which the open-in-dvm with transaction=="seq" was started
2010-06-29 12:16:32 +02:00
snprintf(db_name, sizeof(db_name), DBDIR "/%d", seq);
db_fd = open(db_name, O_RDONLY);
if (!db_fd) {
syslog(LOG_DAEMON | LOG_ERR, "open db");
2010-06-29 14:35:20 +02:00
goto out_err;
2010-06-29 12:16:32 +02:00
}
if (read(db_fd, file_name, sizeof(file_name)) < 0) {
syslog(LOG_DAEMON | LOG_ERR, "read db");
2010-06-29 14:35:20 +02:00
goto out_err;
2010-06-29 12:16:32 +02:00
}
close(db_fd);
file_fd = open(file_name, O_WRONLY | O_TRUNC);
if (file_fd < 0) {
syslog(LOG_DAEMON | LOG_ERR, "open filename");
2010-06-29 14:35:20 +02:00
goto out_err;
2010-06-29 12:16:32 +02:00
}
copy_from_xvdh(file_fd, xvdh_fd, header.file_size);
close(xvdh_fd);
close(file_fd);
2010-06-29 14:35:20 +02:00
out_err:
2010-06-29 12:16:32 +02:00
xs_write(xs, XBT_NULL, "device/qpen", "umount", 6);
xs_daemon_close(xs);
}
void dvm_transaction(char *seq, struct xs_handle *xs)
{
struct stat st;
redirect_stderr();
if (stat("/etc/this_is_dvm", &st))
dvm_transaction_return(seq, xs);
else
dvm_transaction_request(seq, xs);
}
#define MOUNTDIR "/mnt/incoming"
int main()
{
2010-06-29 12:16:32 +02:00
struct xs_handle *xs;
char *seq;
unsigned int len;
background();
2010-06-29 12:16:32 +02:00
openlog("qubes_add_pendrive_script", LOG_CONS | LOG_PID,
LOG_DAEMON);
xs = xs_domain_open();
if (!xs) {
syslog(LOG_DAEMON | LOG_ERR, "xs_domain_open");
exit(1);
}
seq = xs_read(xs, XBT_NULL, "qubes_transaction_seq", &len);
if (seq && len > 0 && strcmp(seq, "0")) {
dvm_transaction(seq, xs);
exit(0);
}
if (!system("su - user -c 'mount " MOUNTDIR "'"))
wait_for_umount(MOUNTDIR "/.");
2010-06-29 12:16:32 +02:00
xs_write(xs, XBT_NULL, "device/qpen", "umount", 6);
xs_daemon_close(xs);
return 0;
}