2011-03-04 16:32:58 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-03-17 17:53:33 +01:00
|
|
|
#include <fcntl.h>
|
2011-03-17 18:15:04 +01:00
|
|
|
#include <ioall.h>
|
|
|
|
#include <stdlib.h>
|
2011-03-04 16:32:58 +01:00
|
|
|
#include "qrexec.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "glue.h"
|
|
|
|
|
2011-05-04 12:52:54 +02:00
|
|
|
/*
|
|
|
|
There is buffered data in "buffer" for client id "client_id", and select()
|
|
|
|
reports that "fd" is writable. Write as much as possible to fd, if all sent,
|
|
|
|
notify the peer that this client's pipe is no longer full.
|
|
|
|
*/
|
|
|
|
int flush_client_data(int fd, int client_id, struct buffer *buffer)
|
2011-03-04 16:32:58 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int len;
|
|
|
|
for (;;) {
|
|
|
|
len = buffer_len(buffer);
|
|
|
|
if (len > MAX_DATA_CHUNK)
|
|
|
|
len = MAX_DATA_CHUNK;
|
|
|
|
ret = write(fd, buffer_data(buffer), len);
|
|
|
|
if (ret == -1) {
|
|
|
|
if (errno != EAGAIN) {
|
|
|
|
return WRITE_STDIN_ERROR;
|
|
|
|
} else
|
|
|
|
return WRITE_STDIN_BUFFERED;
|
|
|
|
}
|
2011-05-04 12:56:52 +02:00
|
|
|
// we previously called buffer_remove(buffer, len)
|
|
|
|
// it will be wrong if we change MAX_DATA_CHUNK to something large
|
|
|
|
// as pipes writes are atomic only to PIPE_MAX limit
|
|
|
|
buffer_remove(buffer, ret);
|
2011-03-04 16:32:58 +01:00
|
|
|
len = buffer_len(buffer);
|
|
|
|
if (!len) {
|
|
|
|
struct server_header s_hdr;
|
|
|
|
s_hdr.type = MSG_XON;
|
2011-05-04 12:52:54 +02:00
|
|
|
s_hdr.client_id = client_id;
|
2011-03-04 16:32:58 +01:00
|
|
|
s_hdr.len = 0;
|
|
|
|
write_all_vchan_ext(&s_hdr, sizeof s_hdr);
|
|
|
|
return WRITE_STDIN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-05-04 12:52:54 +02:00
|
|
|
/*
|
|
|
|
Write "len" bytes from "data" to "fd". If not all written, buffer the rest
|
|
|
|
to "buffer", and notify the peer that the client "client_id" pipe is full via
|
|
|
|
MSG_XOFF message.
|
|
|
|
*/
|
|
|
|
int write_stdin(int fd, int client_id, char *data, int len,
|
2011-03-04 16:32:58 +01:00
|
|
|
struct buffer *buffer)
|
|
|
|
{
|
|
|
|
int ret;
|
2011-03-18 11:16:05 +01:00
|
|
|
int written = 0;
|
2011-03-17 16:53:29 +01:00
|
|
|
|
|
|
|
if (buffer_len(buffer)) {
|
|
|
|
buffer_append(buffer, data, len);
|
|
|
|
return WRITE_STDIN_BUFFERED;
|
|
|
|
}
|
2011-03-18 11:16:05 +01:00
|
|
|
while (written < len) {
|
|
|
|
ret = write(fd, data + written, len - written);
|
|
|
|
if (ret == 0) {
|
|
|
|
perror("write_stdin: write returns 0 ???");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (ret == -1) {
|
2011-03-04 16:32:58 +01:00
|
|
|
struct server_header s_hdr;
|
2011-03-18 11:16:05 +01:00
|
|
|
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
return WRITE_STDIN_ERROR;
|
|
|
|
|
|
|
|
buffer_append(buffer, data + written,
|
|
|
|
len - written);
|
2011-03-04 16:32:58 +01:00
|
|
|
|
|
|
|
s_hdr.type = MSG_XOFF;
|
2011-05-04 12:52:54 +02:00
|
|
|
s_hdr.client_id = client_id;
|
2011-03-04 16:32:58 +01:00
|
|
|
s_hdr.len = 0;
|
|
|
|
write_all_vchan_ext(&s_hdr, sizeof s_hdr);
|
|
|
|
|
|
|
|
return WRITE_STDIN_BUFFERED;
|
2011-03-18 11:16:05 +01:00
|
|
|
}
|
|
|
|
written += ret;
|
2011-03-04 16:32:58 +01:00
|
|
|
}
|
2011-03-18 11:16:05 +01:00
|
|
|
return WRITE_STDIN_OK;
|
2011-03-04 16:32:58 +01:00
|
|
|
|
|
|
|
}
|
2011-03-17 17:53:33 +01:00
|
|
|
|
2011-05-04 12:52:54 +02:00
|
|
|
/*
|
|
|
|
Data feed process has exited, so we need to clear all control structures for
|
|
|
|
the client. However, if we have buffered data for the client (which is rare btw),
|
|
|
|
fire&forget a separate process to flush them.
|
|
|
|
*/
|
2011-03-17 18:15:04 +01:00
|
|
|
int fork_and_flush_stdin(int fd, struct buffer *buffer)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (!buffer_len(buffer))
|
|
|
|
return 0;
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < MAX_FDS; i++)
|
|
|
|
if (i != fd && i != 2)
|
|
|
|
close(i);
|
|
|
|
set_block(fd);
|
|
|
|
write_all(fd, buffer_data(buffer), buffer_len(buffer));
|
|
|
|
exit(0);
|
|
|
|
}
|