You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.8 KiB
76 lines
1.8 KiB
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <err.h>
|
|
#include "dpi.h"
|
|
#include "io.h"
|
|
|
|
int dpi_check_auth(int fd) {
|
|
char buf[30];
|
|
unsigned key[4], local_key[4];
|
|
char keys[128];
|
|
char *home;
|
|
ssize_t sz;
|
|
int rc;
|
|
rc = read_all(fd, (unsigned char *)buf, 28);
|
|
if (rc < 0) return -1;
|
|
buf[29] = '\0';
|
|
// first byte was read already
|
|
rc = sscanf(buf, "cmd='auth' msg='%4x' '>", key);
|
|
if (rc < 1) { errno = EPROTO; return -1; }
|
|
home = getenv("HOME");
|
|
if (!home) home = ".";
|
|
sz = read_file(keys, sizeof(keys), "%s/.dillo/dpid_comm_keys", home);
|
|
if (sz < 0) { errno = ENOKEY; return -1; }
|
|
rc = sscanf(keys, "%*d %4x", local_key);
|
|
if (rc < 1) { errno = ENOKEY; return -1; }
|
|
if (memcmp(key, local_key, 4)) { errno = EKEYREJECTED; return -1; }
|
|
return 0;
|
|
}
|
|
|
|
int dpi_read_request(int fd, char *url_buf, size_t url_len) {
|
|
char buf[21];
|
|
size_t len;
|
|
int rc;
|
|
size_t i;
|
|
|
|
rc = read_all(fd, (unsigned char *)buf, sizeof(buf));
|
|
if (rc < 0 && errno == EPIPE && !strncmp(buf, "<cmd='DpiBye' '>", 16)) {
|
|
errno = ESHUTDOWN;
|
|
return -1;
|
|
}
|
|
if (rc < 0) return -1;
|
|
if (strncmp(buf, "<cmd='open_url' url='", 21)) {
|
|
errno = EPROTO;
|
|
return -1;
|
|
}
|
|
len = url_len;
|
|
rc = read_some(fd, (unsigned char *)url_buf, &len);
|
|
if (rc < 0) return -1;
|
|
|
|
for (i = 0; i < len-1; i++) {
|
|
if (url_buf[i] == '\'' && url_buf[i+1] == ' ') break;
|
|
}
|
|
if (i > len-3 || strncmp(url_buf + i, "' '>", 4)) {
|
|
errno = EPROTO;
|
|
return -1;
|
|
}
|
|
url_buf[i] = '\0';
|
|
return 0;
|
|
}
|
|
|
|
int dpi_send_status(int fd, const char *status) {
|
|
return dprintf(fd, "<cmd='send_status_message' msg='%s' '>", status);
|
|
}
|
|
|
|
int dpi_send_header(int fd, const char *content_type) {
|
|
return dprintf(fd, "<cmd='start_send_page' url='' '>"
|
|
"Content-Type: %s\n\n", content_type);
|
|
}
|
|
|
|
int dpi_reload(int fd) {
|
|
return dprintf(fd, "<cmd='reload_request' url='' '>");
|
|
}
|