/* /share to the HTML artifact publisher: see share.h. One POST over pic's own HTTP and TLS (plain
* http only to loopback), the token sent as the Authorization header and nowhere else. */
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cred.h"
#include "http.h"
#include "net.h"
#include "noise.h"
#include "settings.h"
#include "share.h"
#include "userdb.h"
#include "webfetch.h"
#define SHARE_BLOB_OVER (256 * 1024) /* the publisher keeps a larger text file as a blob */
#define SHARE_REPLY_MAX (1024 * 1024)
#define SHARE_DEADLINE 25
typedef struct {
char *p;
size_t n;
int bad;
} share_body;
static int share_on_body(void *ctx, const char *bytes, size_t n) {
share_body *b = ctx;
char *g;
if (b->n + n > SHARE_REPLY_MAX || !(g = realloc(b->p, b->n + n + 1))) {
b->bad = 1;
return 1;
}
b->p = g;
memcpy(b->p + b->n, bytes, n);
b->n += n;
b->p[b->n] = '\0';
return 0;
}
/* The address with no trailing slashes (malloc'd): artifactsUrl, else HTML_PUBLISHER_URL; NULL
* when neither is set. */
static char *share_base(void) {
char *v = pic_setting("artifactsUrl");
const char *e;
size_t n;
if ((!v || !*v) && (e = getenv("HTML_PUBLISHER_URL")) && *e) {
free(v);
v = strdup(e);
}
if (!v || !*v) {
free(v);
return NULL;
}
n = strlen(v);
while (n > 0 && v[n - 1] == '/') {
v[--n] = '\0';
}
return v;
}
char *pic_share_address(void) { return share_base(); }
int pic_share_artifacts(void) {
char *how = pic_setting("sessionShareDestination"), *base;
int on;
if (how && strcmp(how, "artifacts") == 0) {
free(how);
return 1;
}
on = !how || !*how || strcmp(how, "auto") == 0;
free(how);
if (!on) {
return 0;
}
base = share_base();
on = base && (pic_cred_stored(PIC_SHARE_CRED) || (getenv("HTML_PUBLISHER_TOKEN") && *getenv("HTML_PUBLISHER_TOKEN")));
free(base);
return on;
}
static const char B64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* Standard base64 with padding (malloc'd). */
static char *share_b64(const unsigned char *in, size_t n) {
char *out = malloc(4 * ((n + 2) / 3) + 1), *o = out;
if (!out) {
return NULL;
}
for (size_t i = 0; i < n; i += 3) {
unsigned v = (unsigned)in[i] << 16 | (i + 1 < n ? (unsigned)in[i + 1] << 8 : 0u) | (i + 2 < n ? in[i + 2] : 0u);
*o++ = B64[v >> 18 & 63];
*o++ = B64[v >> 12 & 63];
*o++ = i + 1 < n ? B64[v >> 6 & 63] : '=';
*o++ = i + 2 < n ? B64[v & 63] : '=';
}
*o = '\0';
return out;
}
/* A request: 0 when the publisher answered with a status in 200-299 (BODY holds its reply). */
static int share_call(pic_net *net, const char *method, const char *url, const char *token, const char *ctype,
const char *body, size_t len, share_body *b, const char *what, char *err, size_t errcap) {
char auth[PIC_CRED_TOKEN_CAP + 32], ct[96], e[200] = "";
const char *hdrs[3];
pic_http_resp hr;
int rc;
(void)snprintf(auth, sizeof auth, "Authorization: Bearer %s", token);
(void)snprintf(ct, sizeof ct, "Content-Type: %s", ctype);
hdrs[0] = auth;
hdrs[1] = ct;
hdrs[2] = "Accept: application/json";
pic_http_resp_init(&hr, share_on_body, b);
rc = pic_http_request(net, method, url, hdrs, 3, body, len, &hr, SHARE_DEADLINE, SHARE_DEADLINE, e, sizeof e);
pic_cred_wipe(auth, sizeof auth);
if (b->bad) {
(void)snprintf(err, errcap, "%s: the publisher's reply was too large or memory ran out", what);
} else if (rc != 0) {
pic_cred_scrub(e, token);
(void)snprintf(err, errcap, "%s: could not reach the publisher: %s", what, e);
} else if (hr.status == 401 || hr.status == 403) {
(void)snprintf(err, errcap, "%s: the publisher refused the token (HTTP %d); check it with pic login %s", what,
hr.status, PIC_SHARE_CRED);
} else if (hr.status < 200 || hr.status > 299) {
(void)snprintf(err, errcap, "%s: the publisher answered HTTP %d", what, hr.status);
} else {
return 0;
}
return -1;
}
int pic_share_publish(const char *to, const char *html, size_t n, char *url, size_t urlcap, char *err, size_t errcap) {
char token[PIC_CRED_TOKEN_CAP], neterr[160], *base = to && *to ? strdup(to) : share_base(), *u = NULL, *file = NULL, *json = NULL, *link = NULL;
pic_net *net = NULL;
share_body b = {NULL, 0, 0};
int got, rc = -1;
token[0] = '\0';
if (!base) {
(void)snprintf(err, errcap, "no publisher address is set: pic settings artifactsUrl https://YOUR-PUBLISHER");
return -1;
}
for (size_t m = strlen(base); m > 0 && base[m - 1] == '/'; ) {
base[--m] = '\0';
}
if (!pic_secure_url(base)) {
(void)snprintf(err, errcap, "the publisher address must be https:// (http:// only to this machine)");
free(base);
return -1;
}
got = pic_cred_get(PIC_SHARE_CRED, "HTML_PUBLISHER_TOKEN", "", token, sizeof token, err, errcap);
if (got > 0) {
(void)snprintf(err, errcap, "no publisher token is stored: pic login %s (or set HTML_PUBLISHER_TOKEN)", PIC_SHARE_CRED);
}
if (got != 0 || !(net = pic_net_new(neterr, sizeof neterr))) {
if (got == 0) {
(void)snprintf(err, errcap, "cannot start the network: %s", neterr);
}
goto out;
}
if (n > SHARE_BLOB_OVER) { /* the page goes up as a blob, and the artifact holds its pointer */
unsigned char d[32];
char hex[65], ptr[160];
size_t at = strlen(base) + 96;
pic_noise_sha256(d, html, n, NULL, 0);
for (int i = 0; i < 32; i++) {
(void)snprintf(hex + 2 * i, 3, "%02x", d[i]);
}
u = malloc(at);
if (!u) {
(void)snprintf(err, errcap, "out of memory");
goto out;
}
(void)snprintf(u, at, "%s/api/blobs/%s", base, hex);
if (share_call(net, "PUT", u, token, "text/html", html, n, &b, "upload", err, errcap) != 0) {
goto out;
}
free(b.p);
b = (share_body){NULL, 0, 0};
(void)snprintf(ptr, sizeof ptr, "artifact-blob 1\nsha256 %s\nsize %zu\ntype text/html\n", hex, n);
file = share_b64((const unsigned char *)ptr, strlen(ptr));
} else {
file = share_b64((const unsigned char *)html, n);
}
if (!file || !(json = malloc(strlen(file) + 160))) {
(void)snprintf(err, errcap, "out of memory");
goto out;
}
(void)snprintf(json, strlen(file) + 160,
"{\"title\":\"pic conversation\",\"message\":\"Share from pic\",\"user\":\"pic\",\"files\":{\"index.html\":\"%s\"}}",
file);
free(u);
u = malloc(strlen(base) + 32);
if (!u) {
(void)snprintf(err, errcap, "out of memory");
goto out;
}
(void)snprintf(u, strlen(base) + 32, "%s/api/artifacts", base);
if (share_call(net, "POST", u, token, "application/json", json, strlen(json), &b, "publish", err, errcap) != 0) {
goto out;
}
link = b.p ? pic_web_json("SELECT json_extract(?1, '$.viewerUrl')", b.p) : NULL;
if (!link || !pic_secure_url(link)) {
(void)snprintf(err, errcap, "publish: the publisher's reply had no link");
goto out;
}
(void)snprintf(url, urlcap, "%s", link);
rc = 0;
out:
pic_cred_wipe(token, sizeof token);
if (net) {
pic_net_free(net);
}
free(base);
free(u);
free(file);
free(json);
free(link);
free(b.p);
return rc;
}