Annotation of embedaddon/strongswan/src/swanctl/commands/install.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: static int manage_policy(vici_conn_t *conn, char *label)
        !            21: {
        !            22:        vici_req_t *req;
        !            23:        vici_res_t *res;
        !            24:        command_format_options_t format = COMMAND_FORMAT_NONE;
        !            25:        char *arg, *child = NULL, *ike = NULL;
        !            26:        int ret = 0;
        !            27: 
        !            28:        while (TRUE)
        !            29:        {
        !            30:                switch (command_getopt(&arg))
        !            31:                {
        !            32:                        case 'h':
        !            33:                                return command_usage(NULL);
        !            34:                        case 'P':
        !            35:                                format |= COMMAND_FORMAT_RAW;
        !            36:                                /* fall through to raw */
        !            37:                        case 'r':
        !            38:                                format |= COMMAND_FORMAT_PRETTY;
        !            39:                                continue;
        !            40:                        case 'c':
        !            41:                                child = arg;
        !            42:                                continue;
        !            43:                        case 'i':
        !            44:                                ike = arg;
        !            45:                                continue;
        !            46:                        case EOF:
        !            47:                                break;
        !            48:                        default:
        !            49:                                return command_usage("invalid --%s option", label);
        !            50:                }
        !            51:                break;
        !            52:        }
        !            53:        req = vici_begin(label);
        !            54:        if (child)
        !            55:        {
        !            56:                vici_add_key_valuef(req, "child", "%s", child);
        !            57:        }
        !            58:        if (ike)
        !            59:        {
        !            60:                vici_add_key_valuef(req, "ike", "%s", ike);
        !            61:        }
        !            62:        res = vici_submit(req, conn);
        !            63:        if (!res)
        !            64:        {
        !            65:                ret = errno;
        !            66:                fprintf(stderr, "%s request failed: %s\n", label, strerror(errno));
        !            67:                return ret;
        !            68:        }
        !            69:        if (format & COMMAND_FORMAT_RAW)
        !            70:        {
        !            71:                puts(label);
        !            72:                vici_dump(res, " reply", format & COMMAND_FORMAT_PRETTY, stdout);
        !            73:        }
        !            74:        else
        !            75:        {
        !            76:                if (streq(vici_find_str(res, "no", "success"), "yes"))
        !            77:                {
        !            78:                        printf("%s completed successfully\n", label);
        !            79:                }
        !            80:                else
        !            81:                {
        !            82:                        fprintf(stderr, "%s failed: %s\n",
        !            83:                                        label, vici_find_str(res, "", "errmsg"));
        !            84:                        ret = 1;
        !            85:                }
        !            86:        }
        !            87:        vici_free_res(res);
        !            88:        return ret;
        !            89: }
        !            90: 
        !            91: static int uninstall(vici_conn_t *conn)
        !            92: {
        !            93:        return manage_policy(conn, "uninstall");
        !            94: }
        !            95: 
        !            96: static int install(vici_conn_t *conn)
        !            97: {
        !            98:        return manage_policy(conn, "install");
        !            99: }
        !           100: 
        !           101: /**
        !           102:  * Register the uninstall command.
        !           103:  */
        !           104: static void __attribute__ ((constructor))reg_uninstall()
        !           105: {
        !           106:        command_register((command_t) {
        !           107:                uninstall, 'u', "uninstall", "uninstall a trap or shunt policy",
        !           108:                {"--child <name> [--ike <name>] [--raw|--pretty]"},
        !           109:                {
        !           110:                        {"help",                'h', 0, "show usage information"},
        !           111:                        {"child",               'c', 1, "CHILD_SA configuration to uninstall"},
        !           112:                        {"ike",                 'i', 1, "name of the connection to which the child belongs"},
        !           113:                        {"raw",                 'r', 0, "dump raw response message"},
        !           114:                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
        !           115:                }
        !           116:        });
        !           117: }
        !           118: 
        !           119: /**
        !           120:  * Register install the command.
        !           121:  */
        !           122: static void __attribute__ ((constructor))reg_install()
        !           123: {
        !           124:        command_register((command_t) {
        !           125:                install, 'p', "install", "install a trap or shunt policy",
        !           126:                {"--child <name> [--ike <name>] [--raw|--pretty]"},
        !           127:                {
        !           128:                        {"help",                'h', 0, "show usage information"},
        !           129:                        {"child",               'c', 1, "CHILD_SA configuration to install"},
        !           130:                        {"ike",                 'i', 1, "name of the connection to which the child belongs"},
        !           131:                        {"raw",                 'r', 0, "dump raw response message"},
        !           132:                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
        !           133:                }
        !           134:        });
        !           135: }

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