Annotation of embedaddon/strongswan/src/swanctl/commands/initiate.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2014 Martin Willi
        !             3:  * Copyright (C) 2014 revosec AG
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: #include "command.h"
        !            17: 
        !            18: #include <errno.h>
        !            19: 
        !            20: CALLBACK(log_cb, void,
        !            21:        command_format_options_t *format, char *name, vici_res_t *msg)
        !            22: {
        !            23:        if (*format & COMMAND_FORMAT_RAW)
        !            24:        {
        !            25:                vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout);
        !            26:        }
        !            27:        else
        !            28:        {
        !            29:                printf("[%s] %s\n",
        !            30:                           vici_find_str(msg, "   ", "group"),
        !            31:                           vici_find_str(msg, "", "msg"));
        !            32:        }
        !            33: }
        !            34: 
        !            35: static int initiate(vici_conn_t *conn)
        !            36: {
        !            37:        vici_req_t *req;
        !            38:        vici_res_t *res;
        !            39:        command_format_options_t format = COMMAND_FORMAT_NONE;
        !            40:        char *arg, *child = NULL, *ike = NULL;
        !            41:        int ret = 0, timeout = 0, level = 1;
        !            42: 
        !            43:        while (TRUE)
        !            44:        {
        !            45:                switch (command_getopt(&arg))
        !            46:                {
        !            47:                        case 'h':
        !            48:                                return command_usage(NULL);
        !            49:                        case 'P':
        !            50:                                format |= COMMAND_FORMAT_PRETTY;
        !            51:                                /* fall through to raw */
        !            52:                        case 'r':
        !            53:                                format |= COMMAND_FORMAT_RAW;
        !            54:                                continue;
        !            55:                        case 'c':
        !            56:                                child = arg;
        !            57:                                continue;
        !            58:                        case 'i':
        !            59:                                ike = arg;
        !            60:                                continue;
        !            61:                        case 't':
        !            62:                                timeout = atoi(arg);
        !            63:                                continue;
        !            64:                        case 'l':
        !            65:                                level = atoi(arg);
        !            66:                                continue;
        !            67:                        case EOF:
        !            68:                                break;
        !            69:                        default:
        !            70:                                return command_usage("invalid --initiate option");
        !            71:                }
        !            72:                break;
        !            73:        }
        !            74: 
        !            75:        if (vici_register(conn, "control-log", log_cb, &format) != 0)
        !            76:        {
        !            77:                ret = errno;
        !            78:                fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
        !            79:                return ret;
        !            80:        }
        !            81:        req = vici_begin("initiate");
        !            82:        if (child)
        !            83:        {
        !            84:                vici_add_key_valuef(req, "child", "%s", child);
        !            85:        }
        !            86:        if (ike)
        !            87:        {
        !            88:                vici_add_key_valuef(req, "ike", "%s", ike);
        !            89:        }
        !            90:        if (timeout)
        !            91:        {
        !            92:                vici_add_key_valuef(req, "timeout", "%d", timeout * 1000);
        !            93:        }
        !            94:        vici_add_key_valuef(req, "loglevel", "%d", level);
        !            95:        res = vici_submit(req, conn);
        !            96:        if (!res)
        !            97:        {
        !            98:                ret = errno;
        !            99:                fprintf(stderr, "initiate request failed: %s\n", strerror(errno));
        !           100:                return ret;
        !           101:        }
        !           102:        if (format & COMMAND_FORMAT_RAW)
        !           103:        {
        !           104:                vici_dump(res, "initiate reply", format & COMMAND_FORMAT_PRETTY,
        !           105:                                  stdout);
        !           106:        }
        !           107:        else
        !           108:        {
        !           109:                if (streq(vici_find_str(res, "no", "success"), "yes"))
        !           110:                {
        !           111:                        printf("initiate completed successfully\n");
        !           112:                }
        !           113:                else
        !           114:                {
        !           115:                        fprintf(stderr, "initiate failed: %s\n",
        !           116:                                        vici_find_str(res, "", "errmsg"));
        !           117:                        ret = 1;
        !           118:                }
        !           119:        }
        !           120:        vici_free_res(res);
        !           121:        return ret;
        !           122: }
        !           123: 
        !           124: /**
        !           125:  * Register the command.
        !           126:  */
        !           127: static void __attribute__ ((constructor))reg()
        !           128: {
        !           129:        command_register((command_t) {
        !           130:                initiate, 'i', "initiate", "initiate a connection",
        !           131:                {"[--child <name>] [--ike <name>] [--timeout <s>] [--raw|--pretty]"},
        !           132:                {
        !           133:                        {"help",                'h', 0, "show usage information"},
        !           134:                        {"child",               'c', 1, "initiate a CHILD_SA configuration"},
        !           135:                        {"ike",                 'i', 1, "initiate an IKE_SA, or name of child's parent"},
        !           136:                        {"timeout",             't', 1, "timeout in seconds before detaching"},
        !           137:                        {"raw",                 'r', 0, "dump raw response message"},
        !           138:                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
        !           139:                        {"loglevel",    'l', 1, "verbosity of redirected log"},
        !           140:                }
        !           141:        });
        !           142: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>