commit ecd858425fa2e2ebad40fcfc7ed7a1db07f55388
Author: kristapsdz <kristaps@bsd.lv>
Date: Tue, 3 Mar 2020 12:05:37 +0000
Author: kristapsdz <kristaps@bsd.lv>
Date: Tue, 3 Mar 2020 12:05:37 +0000
5 files changed, 598 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile @@ -0,0 +1,45 @@ +PREFIX = /var/www/vhosts/kristaps.bsd.lv +DATADIR = /vhosts/kristaps.bsd.lv/data + +sinclude Makefile.local + +OBJS = db.o main.o +CPPFLAGS += -I/usr/local/include +CFLAGS += -g -W -Wall -Wextra -Wmissing-prototypes +CFLAGS += -Wstrict-prototypes -Wwrite-strings -Wno-unused-parameter +CFLAGS += -DDATADIR=\"$(DATADIR)\" +CFLAGS += `pkg-config --cflags kcgi sqlbox` + +all: minci.cgi + +install: all + mkdir -p $(PREFIX)/cgi-bin + mkdir -p $(PREFIX)/data + install -o www -m 0500 minci.cgi $(PREFIX)/cgi-bin + install -o www -m 0600 minci.db $(PREFIX)/data + +update: all + mkdir -p $(PREFIX)/cgi-bin + install -o www -m 0500 minci.cgi $(PREFIX)/cgi-bin + +minci.cgi: $(OBJS) minci.db + $(CC) -o $@ -static $(OBJS) `pkg-config --libs --static kcgi sqlbox` + +clean: + rm -f $(OBJS) minci.cgi db.c extern.h minci.db db.sql + +$(OBJS): extern.h + +db.c: db.ort + ort-c-source -vh extern.h db.ort >$@ + +extern.h: db.ort + ort-c-header -v db.ort >$@ + +db.sql: db.ort + ort-sql db.ort >$@ + +minci.db: db.sql + rm -f $@ + sqlite3 $@ < db.sql + [ ! -r db.local.sql ] || sqlite3 $@ < db.local.sql
diff --git a/db.ort b/db.ort @@ -0,0 +1,38 @@ +struct project { + field name text unique; + field id int rowid; + + search name: name byname; +}; + +struct user { + field email email; + field apikey int limit gt 0 unique; + field apisecret text limit eq 32; + field ctime epoch; + field id int rowid; + + search apikey: name bykey; +}; + +struct report { + field projectid:project.id; + field userid:user.id; + field start epoch; + field env epoch; + field depend epoch; + field build epoch; + field test epoch; + field install epoch; + field distcheck epoch; + field ctime epoch; + field log text; + field unamem text limit le 128; + field unamen text limit le 128; + field unamer text limit le 128; + field unames text limit le 128; + field unamev text limit le 128; + field id int rowid; + + insert; +};
diff --git a/main.c b/main.c @@ -0,0 +1,248 @@ +/* $Id$ */ +/* + * Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include <sys/types.h> + +#include <inttypes.h> +#include <md5.h> +#include <stdarg.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include <kcgi.h> + +#include "extern.h" + +enum page { + PAGE_SUBMIT, + PAGE__MAX +}; + +static const char *const pages[PAGE__MAX] = { + "submit", /* PAGE_SUBMIT */ +}; + +static void +http_open(struct kreq *r, enum khttp code) +{ + + khttp_head(r, kresps[KRESP_STATUS], + "%s", khttps[code]); + khttp_head(r, kresps[KRESP_CACHE_CONTROL], + "%s", "no-cache, no-store, must-revalidate"); + khttp_head(r, kresps[KRESP_PRAGMA], + "%s", "no-cache"); + khttp_head(r, kresps[KRESP_EXPIRES], + "%s", "0"); + khttp_head(r, kresps[KRESP_CONTENT_TYPE], + "%s", kmimetypes[KMIME_APP_JSON]); + khttp_body(r); +} + +static void +send_submit(struct kreq *r) +{ + struct project *proj = NULL; + struct user *user = NULL; + struct kpair *kps, *kpe, *kpd, *kpb, *kpt, + *kpi, *kpc, *kpn, *kpl, *sig, + *kpu, *kpum, *kpun, *kpur, *kpus, + *kpuv; + size_t i, sz; + MD5_CTX ctx; + char *buf = NULL; + char digest[MD5_DIGEST_STRING_LENGTH], + logdigest[MD5_DIGEST_STRING_LENGTH]; + + /* Signature must be 32-byte string. */ + + for (sig = NULL, i = 0; i < r->fieldsz; i++) + if (strcmp(r->fields[i].key, "signature") == 0 && + kvalid_stringne(&r->fields[i]) && + r->fields[i].valsz == 32) { + sig = &r->fields[i]; + break; + } + + /* Check our input is sane. */ + + if (sig == NULL || + (kpn = r->fieldmap[VALID_PROJECT_NAME]) == NULL || + (kpd = r->fieldmap[VALID_REPORT_DEPEND]) == NULL || + (kpc = r->fieldmap[VALID_REPORT_DISTCHECK]) == NULL || + (kpe = r->fieldmap[VALID_REPORT_ENV]) == NULL || + (kpi = r->fieldmap[VALID_REPORT_INSTALL]) == NULL || + (kpl = r->fieldmap[VALID_REPORT_LOG]) == NULL || + (kps = r->fieldmap[VALID_REPORT_START]) == NULL || + (kpb = r->fieldmap[VALID_REPORT_BUILD]) == NULL || + (kpt = r->fieldmap[VALID_REPORT_TEST]) == NULL || + (kpum = r->fieldmap[VALID_REPORT_UNAMEM]) == NULL || + (kpun = r->fieldmap[VALID_REPORT_UNAMEN]) == NULL || + (kpur = r->fieldmap[VALID_REPORT_UNAMER]) == NULL || + (kpus = r->fieldmap[VALID_REPORT_UNAMES]) == NULL || + (kpuv = r->fieldmap[VALID_REPORT_UNAMEV]) == NULL || + (kpu = r->fieldmap[VALID_USER_APIKEY]) == NULL) { + kutil_warnx(r, NULL, "invalid request"); + http_open(r, KHTTP_403); + return; + } + + MD5Init(&ctx); + MD5Update(&ctx, kpl->parsed.s, kpl->valsz); + MD5End(&ctx, logdigest); + + /* Get the project. */ + + proj = db_project_get_byname(r->arg, + kpn->parsed.s); /* name */ + if (proj == NULL) { + kutil_warnx(r, NULL, "invalid project"); + http_open(r, KHTTP_403); + goto out; + } + + /* Get the user. */ + + user = db_user_get_bykey(r->arg, + kpu->parsed.i); /* apikey */ + if (user == NULL) { + kutil_warnx(r, NULL, "invalid user"); + http_open(r, KHTTP_403); + goto out; + } + + /* + * Re-create the signature with the user's secret key. + * This authenticates the message. + */ + + sz = (size_t)kasprintf(&buf, + "project-name=%s&" + "report-build=%" PRId64 "&" + "report-distcheck=%" PRId64 "&" + "report-env=%" PRId64 "&" + "report-depend=%" PRId64 "&" + "report-install=%" PRId64 "&" + "report-log=%s&" + "report-start=%" PRId64 "&" + "report-test=%" PRId64 "&" + "report-unamem=%s&" + "report-unamen=%s&" + "report-unamer=%s&" + "report-unames=%s&" + "report-unamev=%s&" + "user-apisecret=%s", + proj->name, + kpb->parsed.i, + kpc->parsed.i, + kpe->parsed.i, + kpd->parsed.i, + kpi->parsed.i, + logdigest, + kps->parsed.i, + kpt->parsed.i, + kpum->parsed.s, + kpun->parsed.s, + kpur->parsed.s, + kpus->parsed.s, + kpuv->parsed.s, + user->apisecret); + MD5Init(&ctx); + MD5Update(&ctx, buf, sz); + MD5End(&ctx, digest); + + if (strcasecmp(digest, sig->parsed.s)) { + kutil_warnx(r, NULL, "bad signature"); + http_open(r, KHTTP_403); + goto out; + } + + /* Insert the record. */ + + db_report_insert(r->arg, + proj->id, /* projectid */ + user->id, /* userid */ + kps->parsed.i, /* start */ + kpe->parsed.i, /* env */ + kpd->parsed.i, /* depend */ + kpb->parsed.i, /* build */ + kpt->parsed.i, /* test */ + kpi->parsed.i, /* install */ + kpc->parsed.i, /* distcheck */ + time(NULL), /* ctime */ + kpl == NULL ? "" : kpl->parsed.s, /* log */ + kpum->parsed.s, /* unamem */ + kpun->parsed.s, /* unamen */ + kpur->parsed.s, /* unamer */ + kpus->parsed.s, /* unames */ + kpuv->parsed.s); /* unamev */ + + kutil_info(r, user->email, "log submitted: %s", proj->name); + http_open(r, KHTTP_201); +out: + db_project_free(proj); + db_user_free(user); + free(buf); +} + +int +main(void) +{ + struct kreq r; + enum kcgi_err er; + + er = khttp_parse(&r, valid_keys, + VALID__MAX, pages, PAGE__MAX, PAGE_SUBMIT); + + if (er != KCGI_OK) + kutil_errx(&r, NULL, + "khttp_parse: %s", kcgi_strerror(er)); + + if (r.page == PAGE__MAX) { + http_open(&r, KHTTP_404); + khttp_free(&r); + return EXIT_SUCCESS; + } + + if ((r.arg = db_open(DATADIR "/minci.db")) == NULL) { + kutil_errx(&r, NULL, "db_open: %s", + DATADIR "/minci.db"); + khttp_free(&r); + return EXIT_SUCCESS; + } + + if (pledge("stdio", NULL) == -1) { + kutil_warn(NULL, NULL, "pledge"); + db_close(r.arg); + khttp_free(&r); + return EXIT_FAILURE; + } + + switch (r.page) { + case PAGE_SUBMIT: + send_submit(&r); + break; + default: + abort(); + } + + db_close(r.arg); + khttp_free(&r); + return EXIT_SUCCESS; +}
diff --git a/minci.c b/minci.c @@ -0,0 +1,6 @@ +#include "config.h" + +int +main(int argc, char *argv[]) +{ +}
diff --git a/minci.sh b/minci.sh @@ -0,0 +1,261 @@ +#! /bin/sh + +API_SECRET= +API_KEY= +SERVER= +TESTS_GLOBAL="/usr/local/share/minci/deps" +TESTS_LOCAL="$HOME/.local/share/minci/deps" +STAGING="$HOME/.local/cache/minci" +CONFIG= +CONFIG_LOCAL="$HOME/.minci" +CONFIG_GLOBAL="/etc/minci" + +# Start with the local then global configuration. +# Require at least one of them. + +if [ ! -r "$CONFIG_LOCAL" ] +then + if [ ! -r "$CONFIG_GLOBAL" ] + then + echo "$0: no config found" 1>&2 + echo "$0: ...tried: $CONFIG_GLOBAL" 1>&2 + echo "$0: ...tried: $CONFIG_LOCAL" 1>&2 + exit 1 + fi + CONFIG="$CONFIG_GLOBAL" +else + CONFIG="$CONFIG_LOCAL" +fi +echo "$0: using config: $CONFIG" + +# Read the API secret from the configuration. + +while read ln +do + api_secret="$(echo $ln | sed -n 's!^[ ]*apisecret[ ]*=[ ]*!!p')" + [ -z "$api_secret" ] && continue + API_SECRET="$api_secret" +done < "$CONFIG" +if [ -z "$API_SECRET" ] +then + echo "$0: no API secret specified" 1>&2 + exit 1 +fi + +# Read the API key from the configuration. + +while read ln +do + api_key="$(echo $ln | sed -n 's!^[ ]*apikey[ ]*=[ ]*!!p')" + [ -z "$api_key" ] && continue + API_KEY="$api_key" +done < "$CONFIG" +if [ -z "$API_KEY" ] +then + echo "$0: no API key specified" 1>&2 + exit 1 +fi + +# Read the server from the configuration. + +while read ln +do + server="$(echo $ln | sed -n 's!^[ ]*server[ ]*=[ ]*!!p')" + [ -z "$server" ] && continue + SERVER="$server" +done < "$CONFIG" +if [ -z "$SERVER" ] +then + echo "$0: no server specified" 1>&2 + exit 1 +fi + +echo "$0: using API key: $API_KEY" +echo "$0: using API secret: $API_SECRET" +echo "$0: using server: $SERVER" + +# Check or create where we'll put our repositories. + +if [ ! -d "$STAGING" ] +then + mkdir -p "$STAGING" + if [ $? -ne 0 ] + then + echo "$0: could not create: $STAGING" 1>&2 + exit 1 + fi + echo "$0: created: $STAGING" +fi + +# Process each repository line. + +while read ln +do + repo="$(echo $ln | sed -n 's!^[ ]*repo[ ]*=[ ]*!!p')" + [ -z "$repo" ] && continue + + reponame="$(echo $repo | sed -e 's!.*/!!' -e 's!\.git$!!')" + if [ -z "$reponame" ] + then + echo "$0: malformed repo: $repo" 1>&2 + exit 1 + fi + + # Now errors without || are fatal. + + set -e + + echo "$0: processing repo: $reponame" + echo "$0: ...parsed from: $repo" + + cd "$STAGING" + + # Set all of our times to zero. + # If a time is non-zero when we send our ping, that means that + # we've finished a phase. + + TIME_start=0 + TIME_env=0 + TIME_depend=0 + TIME_build=0 + TIME_test=0 + TIME_install=0 + TIME_distcheck=0 + + # This is wrapped in an infinite loop so we can just use `break` + # to come out of error situations. + + exec 3>/tmp/minci.log + TIME_start=`date +%s` + + while `true` + do + if [ -d "$reponame" ] + then + cd "$reponame" + + echo "$0: git fetch origin: $reponame" + echo "$0: git fetch origin: $reponame" 1>&3 + git fetch origin 1>&3 2>&3 || break + + echo "$0: git reset --hard origin/master: $reponame" + echo "$0: git reset --hard origin/master: $reponame" 1>&3 + git reset --hard origin/master 1>&3 2>&3 || break + + echo "$0: git clean -fdx: $reponame" + echo "$0: git clean -fdx: $reponame" 1>&3 + git clean -fdx 1>&3 2>&3 || break + else + echo "$0: git clone $repo $reponame" + echo "$0: git clone $repo $reponame" 1>&3 + git clone "$repo" "$reponame" 1>&3 2>&3 || break + cd "$reponame" + fi + TIME_env=`date +%s` + + if [ -r "minci.cfg" ] + then + while read mln + do + dep="$(echo $ln | sed -n 's!^[ ]*dep[ ]*=[ ]*!!p')" + [ -n "$dep" ] || continue + + echo "$0: pkg-config --exists $dep: $reponame" + echo "$0: pkg-config --exists $dep: $reponame" 1>&3 + pkg-config --exists "$dep" 1>&3 2>&3 || break + done < "minci.cfg" + [ -n "$mln" ] || break + fi + TIME_depend=`date +%s` + + echo "$0: ./configure PREFIX=build: $reponame" + echo "$0: ./configure PREFIX=build: $reponame" 1>&3 + ./configure PREFIX=build 1>&3 2>&3 || break + + echo "$0: make: $reponame" + echo "$0: make: $reponame" 1>&3 + make 1>&3 2>&3 || break + TIME_build=`date +%s` + + echo "$0: make regress: $reponame" + echo "$0: make regress: $reponame" 1>&3 + make regress 1>&3 2>&3 || break + TIME_test=`date +%s` + + echo "$0: make install: $reponame" + echo "$0: make install: $reponame" 1>&3 + make install 1>&3 2>&3 || break + TIME_install=`date +%s` + + echo "$0: make distcheck: $reponame" + echo "$0: make distcheck: $reponame" 1>&3 + make distcheck 1>&3 2>&3 || break + TIME_distcheck=`date +%s` + break + done + + exec 3>&- + + set +e + + # Create the signature for this entry. + # It consists of all the MD5 hash of all arguments in + # alphabetical order (by key), with the report-log (or /dev/null + # if there's no need to report) being replaced by its MD5 hash. + + QUERY="project-name=${reponame}" + QUERY="${QUERY}&report-build=${TIME_build}" + QUERY="${QUERY}&report-distcheck=${TIME_distcheck}" + QUERY="${QUERY}&report-env=${TIME_env}" + QUERY="${QUERY}&report-depend=${TIME_depend}" + QUERY="${QUERY}&report-install=${TIME_install}" + if [ $TIME_distcheck -eq 0 ] + then + QUERY="${QUERY}&report-log=`md5 -q /tmp/minci.log`" + else + QUERY="${QUERY}&report-log=`md5 -q /dev/null`" + fi + QUERY="${QUERY}&report-start=${TIME_start}" + QUERY="${QUERY}&report-test=${TIME_test}" + QUERY="${QUERY}&report-unamem=`uname -m`" + QUERY="${QUERY}&report-unamen=`uname -n`" + QUERY="${QUERY}&report-unamer=`uname -r`" + QUERY="${QUERY}&report-unames=`uname -s`" + QUERY="${QUERY}&report-unamev=`uname -v`" + QUERY="${QUERY}&user-apisecret=${API_SECRET}" + + SIGNATURE=`/bin/echo -n "$QUERY" | openssl dgst -md5 -hex | sed 's!^[^=]*= !!'` + + # Now actually send the report. + # It includes the signature and optionally the build log (only + # if we didn't get to the end). + + REPORT_LOG= + if [ $TIME_distcheck -eq 0 ] + then + REPORT_LOG="-F report-log=</tmp/minci.log" + else + REPORT_LOG="-F report-log=" + fi + curl ${REPORT_LOG} \ + -F "project-name=${reponame}" \ + -F "report-start=${TIME_start}" \ + -F "report-env=${TIME_env}" \ + -F "report-depend=${TIME_depend}" \ + -F "report-build=${TIME_build}" \ + -F "report-test=${TIME_test}" \ + -F "report-install=${TIME_install}" \ + -F "report-distcheck=${TIME_distcheck}" \ + -F "report-unamem=`uname -m`" \ + -F "report-unamen=`uname -n`" \ + -F "report-unamer=`uname -r`" \ + -F "report-unames=`uname -s`" \ + -F "report-unamev=`uname -v`" \ + -F "user-apikey=${API_KEY}" \ + -F "signature=${SIGNATURE}" \ + "${SERVER}" + + rm -f /tmp/minci.log +done < "$CONFIG" + +exit 0
: nply/minci