tcurl_test_tool.c revision 36e49a842e257ac9bde71728ee3bef4299b6e6e2
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek/*
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek SSSD
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek libcurl tevent integration test tool
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek Copyright (C) Red Hat, 2016
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek This program is free software; you can redistribute it and/or modify
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek it under the terms of the GNU General Public License as published by
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek the Free Software Foundation; either version 3 of the License, or
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek (at your option) any later version.
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek This program is distributed in the hope that it will be useful,
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek GNU General Public License for more details.
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek You should have received a copy of the GNU General Public License
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek*/
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek#include <popt.h>
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek#include "util/util.h"
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek#include "util/tev_curl.h"
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek#define MAXREQ 64
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozekstruct tool_ctx {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek bool verbose;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek bool done;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek size_t nreqs;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek};
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinastruct tool_options {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina int debug;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina int verbose;
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina int raw;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina enum tcurl_http_method method;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina const char *socket_path;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina};
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozekstatic void request_done(struct tevent_req *req)
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek{
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tool_ctx *tool_ctx;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek struct sss_iobuf *outbuf;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina int http_code;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina errno_t ret;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tool_ctx = tevent_req_callback_data(req, struct tool_ctx);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = tcurl_request_recv(tool_ctx, req, &outbuf, &http_code);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek talloc_zfree(req);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tool_ctx->nreqs--;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (tool_ctx->nreqs == 0) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek tool_ctx->done = true;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (ret != EOK) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "HTTP request failed [%d]: %s\n",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret, sss_strerror(ret));
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek return;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek } else if (tool_ctx->verbose) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek printf("Request HTTP code: %d\n", http_code);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek printf("Request HTTP body: \n%s\n",
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek (const char *) sss_iobuf_get_data(outbuf));
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek talloc_zfree(outbuf);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek}
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinastatic errno_t
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinaparse_options(poptContext pc, struct tool_options *opts)
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek{
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek int opt;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek while ((opt = poptGetNextOpt(pc)) > 0) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek switch (opt) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek case 'g':
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina opts->method = TCURL_HTTP_GET;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek break;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek case 'p':
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina opts->method = TCURL_HTTP_PUT;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek break;
c9db8b8b19827c3d492b8d2769aa77a37dbc12d3Jakub Hrozek case 'o':
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina opts->method = TCURL_HTTP_POST;
300b9e9217ee1ed8d845ed2370c5ccf5c87afb36Pavel Březina break;
300b9e9217ee1ed8d845ed2370c5ccf5c87afb36Pavel Březina case 'd':
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina opts->method = TCURL_HTTP_DELETE;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek break;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek default:
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unexpected option\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return EINVAL;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (opt != -1) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina poptPrintUsage(pc, stderr, 0);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina fprintf(stderr, "%s", poptStrerror(opt));
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return EINVAL;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return EOK;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina}
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinastatic errno_t
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinaprepare_requests(TALLOC_CTX *mem_ctx,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina poptContext pc,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tool_options *opts,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tcurl_request ***_requests,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina size_t *_num_requests)
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina{
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tcurl_request **requests;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina const char *arg;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina const char *url;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct sss_iobuf *body;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina errno_t ret;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina size_t i;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina static const char *headers[] = {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina "Content-type: application/octet-stream",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina NULL,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina };
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina requests = talloc_zero_array(mem_ctx, struct tcurl_request *, MAXREQ + 1);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (requests == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return ENOMEM;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina i = 0;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina while ((arg = poptGetArg(pc)) != NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (i >= MAXREQ) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina fprintf(stderr, _("Too many requests!\n"));
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = EINVAL;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina switch (opts->method) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek case TCURL_HTTP_GET:
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek case TCURL_HTTP_DELETE:
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina url = arg;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina body = NULL;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek break;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek case TCURL_HTTP_PUT:
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina case TCURL_HTTP_POST:
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina url = arg;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina arg = poptGetArg(pc);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (arg == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina body = NULL;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina break;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina body = sss_iobuf_init_readonly(requests,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina discard_const_p(uint8_t, arg),
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina strlen(arg));
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (body == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = ENOMEM;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek break;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina default:
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Invalid method!\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = EINVAL;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina requests[i] = tcurl_http(requests, opts->method, opts->socket_path,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina url, headers, body);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (requests[i] == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = ENOMEM;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina if (opts->raw) {
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina ret = tcurl_req_enable_rawoutput(requests[i]);
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina if (ret != EOK) {
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina goto done;
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina }
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina }
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina i++;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina *_requests = requests;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina *_num_requests = i;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = EOK;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinadone:
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (ret != EOK) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina talloc_free(requests);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return ret;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina}
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinastatic errno_t
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinarun_requests(struct tool_ctx *tool_ctx,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tcurl_request **requests)
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina{
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina TALLOC_CTX *tmp_ctx;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tcurl_ctx *tcurl_ctx;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tevent_context *ev;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tevent_req *req;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina errno_t ret;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina int i;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tmp_ctx = talloc_new(NULL);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (tmp_ctx == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return ENOMEM;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (requests == NULL || requests[0] == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = EOK;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ev = tevent_context_init(tmp_ctx);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek if (ev == NULL) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "Could not init tevent context\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = ENOMEM;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tcurl_ctx = tcurl_init(tmp_ctx, ev);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (tcurl_ctx == NULL) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Could not init tcurl context\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = ENOMEM;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina for (i = 0; requests[i] != NULL; i++) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina req = tcurl_request_send(tmp_ctx, ev, tcurl_ctx, requests[i], 5);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (req == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Could not create tevent request\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = ENOMEM;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
300b9e9217ee1ed8d845ed2370c5ccf5c87afb36Pavel Březina }
300b9e9217ee1ed8d845ed2370c5ccf5c87afb36Pavel Březina
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek tevent_req_set_callback(req, request_done, tool_ctx);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek while (tool_ctx->done == false) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek tevent_loop_once(ev);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek if (tool_ctx->nreqs > 0) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek "The tool finished with some pending requests, fail!\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = EEXIST;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek }
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = EOK;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinadone:
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina talloc_free(tmp_ctx);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return ret;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina}
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinaint main(int argc, const char *argv[])
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina{
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tool_options opts = { 0 };
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tool_ctx *tool_ctx;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct tcurl_request **requests;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina poptContext pc;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina errno_t ret;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina struct poptOption long_options[] = {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina POPT_AUTOHELP
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "debug", '\0', POPT_ARG_INT, &opts.debug, 0, "The debug level to run with", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "socket-path", 's', POPT_ARG_STRING, &opts.socket_path, 0, "The path to the HTTP server socket", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "get", 'g', POPT_ARG_NONE, NULL, 'g', "Perform a HTTP GET (default)", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "put", 'p', POPT_ARG_NONE, NULL, 'p', "Perform a HTTP PUT", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "post", 'o', POPT_ARG_NONE, NULL, 'o', "Perform a HTTP POST", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "del", 'd', POPT_ARG_NONE, NULL, 'd', "Perform a HTTP DELETE", NULL },
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina { "raw", 'r', POPT_ARG_NONE, &opts.raw, '\0', "Print raw protocol output", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "verbose", 'v', POPT_ARG_NONE, &opts.verbose, '\0', "Print response code and body", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina POPT_TABLEEND
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina };
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina pc = poptGetContext(NULL, argc, argv, long_options, 0);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina poptSetOtherOptionHelp(pc, "[URL HTTPDATA]*");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tool_ctx = talloc_zero(NULL, struct tool_ctx);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (tool_ctx == NULL) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Could not init tool context\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = ENOMEM;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = parse_options(pc, &opts);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (ret != EOK) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Unable to parse options [%d]: %s\n",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret, sss_strerror(ret));
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG_CLI_INIT(opts.debug);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tool_ctx->verbose = opts.verbose;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = prepare_requests(tool_ctx, pc, &opts, &requests, &tool_ctx->nreqs);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (ret != EOK) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Unable to prepare requests [%d]: %s\n",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret, sss_strerror(ret));
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = run_requests(tool_ctx, requests);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (ret != EOK) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Unable to issue requests [%d]: %s\n",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret, sss_strerror(ret));
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina goto done;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinadone:
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek talloc_free(tool_ctx);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek poptFreeContext(pc);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (ret != EOK) {
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return EXIT_FAILURE;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina }
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina return EXIT_SUCCESS;
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek}