Annotation of embedaddon/strongswan/src/libimcv/swid_gen/swid_gen_info.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2017 Andreas Steffen
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             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: #define _GNU_SOURCE
        !            17: #include <stdio.h>
        !            18: 
        !            19: #include "swid_gen_info.h"
        !            20: 
        !            21: #include <library.h>
        !            22: #include <utils/lexparser.h>
        !            23: 
        !            24: typedef struct private_swid_gen_info_t private_swid_gen_info_t;
        !            25: 
        !            26: /**
        !            27:  * Private data of an swid_gen_info_t object.
        !            28:  */
        !            29: struct private_swid_gen_info_t {
        !            30: 
        !            31:        /**
        !            32:         * Public members of swid_gen_info_state_t
        !            33:         */
        !            34:        swid_gen_info_t public;
        !            35: 
        !            36:        /**
        !            37:         * tagCreator
        !            38:         */
        !            39:        char *tag_creator;
        !            40: 
        !            41:        /**
        !            42:         * OS string 'Name_Version-Arch'
        !            43:         */
        !            44:        char *os;
        !            45: 
        !            46:        /**
        !            47:         * Product string 'Name Version Arch'
        !            48:         */
        !            49:        char *product;
        !            50: 
        !            51:        /**
        !            52:         * OS info about endpoint
        !            53:         */
        !            54:        imc_os_info_t *os_info;
        !            55: 
        !            56: };
        !            57: 
        !            58: /**
        !            59:  * Replaces invalid character by a valid one
        !            60:  */
        !            61: static void sanitize_uri(char *uri, char a, char b)
        !            62: {
        !            63:        char *pos = uri;
        !            64: 
        !            65:        while (TRUE)
        !            66:        {
        !            67:                pos = strchr(pos, a);
        !            68:                if (!pos)
        !            69:                {
        !            70:                        break;
        !            71:                }
        !            72:                *pos = b;
        !            73:                pos++;
        !            74:        }
        !            75: }
        !            76: 
        !            77: METHOD(swid_gen_info_t, get_os_type, os_type_t,
        !            78:        private_swid_gen_info_t *this)
        !            79: {
        !            80:        return this->os_info->get_type(this->os_info);
        !            81: }
        !            82: 
        !            83: METHOD(swid_gen_info_t, get_os, char*,
        !            84:        private_swid_gen_info_t *this, char **product)
        !            85: {
        !            86:        if (product)
        !            87:        {
        !            88:                *product = this->product;
        !            89:        }
        !            90:        return this->os;
        !            91: }
        !            92: 
        !            93: METHOD(swid_gen_info_t, create_sw_id, char*,
        !            94:        private_swid_gen_info_t *this, char *package, char *version)
        !            95: {
        !            96:        char *sw_id;
        !            97: 
        !            98:        if (asprintf(&sw_id, "%s__%s-%s%s%s", this->tag_creator, this->os,
        !            99:                                 package, strlen(version) ? "-" : "", version) == -1)
        !           100:        {
        !           101:                return NULL;
        !           102:        }
        !           103:        sanitize_uri(sw_id, ':', '~');
        !           104:        sanitize_uri(sw_id, '+', '~');
        !           105: 
        !           106:        return sw_id;
        !           107: }
        !           108: 
        !           109: METHOD(swid_gen_info_t, destroy, void,
        !           110:        private_swid_gen_info_t *this)
        !           111: {
        !           112:        this->os_info->destroy(this->os_info);
        !           113:        free(this->os);
        !           114:        free(this->product);
        !           115:        free(this->tag_creator);
        !           116:        free(this);
        !           117: }
        !           118: 
        !           119: /**
        !           120:  * Described in header.
        !           121:  */
        !           122: swid_gen_info_t *swid_gen_info_create(void)
        !           123: {
        !           124:        private_swid_gen_info_t *this;
        !           125:        chunk_t os_name, os_version, os_arch;
        !           126:        char *tag_creator;
        !           127: 
        !           128:        tag_creator = lib->settings->get_str(lib->settings,
        !           129:                                        "libimcv.swid_gen.tag_creator.regid", "strongswan.org");
        !           130: 
        !           131:        INIT(this,
        !           132:                .public = {
        !           133:                        .get_os_type = _get_os_type,
        !           134:                        .get_os = _get_os,
        !           135:                        .create_sw_id = _create_sw_id,
        !           136:                        .destroy = _destroy,
        !           137:                },
        !           138:                .os_info = imc_os_info_create(),
        !           139:                .tag_creator = strdup(tag_creator),
        !           140:        );
        !           141: 
        !           142:        os_name = this->os_info->get_name(this->os_info);
        !           143:        os_arch = this->os_info->get_version(this->os_info);
        !           144: 
        !           145:        /* get_version() returns version followed by arch */
        !           146:        if (!extract_token(&os_version, ' ', &os_arch))
        !           147:        {
        !           148:                DBG1(DBG_IMC, "separation of OS version from arch failed");
        !           149:                destroy(this);
        !           150:                return NULL;
        !           151:        }
        !           152: 
        !           153:        /* construct OS string */
        !           154:        if (asprintf(&this->os, "%.*s_%.*s-%.*s", (int)os_name.len, os_name.ptr,
        !           155:                                 (int)os_version.len, os_version.ptr, (int)os_arch.len,
        !           156:                                 os_arch.ptr) == -1)
        !           157:        {
        !           158:                DBG1(DBG_IMC, "construction of OS string failed");
        !           159:                destroy(this);
        !           160:                return NULL;
        !           161:        }
        !           162: 
        !           163:        /* construct product string */
        !           164:        if (asprintf(&this->product, "%.*s %.*s %.*s", (int)os_name.len,
        !           165:                                 os_name.ptr, (int)os_version.len, os_version.ptr,
        !           166:                                 (int)os_arch.len, os_arch.ptr) == -1)
        !           167:        {
        !           168:                DBG1(DBG_IMC, "construction of product string failed");
        !           169:                destroy(this);
        !           170:                return NULL;
        !           171:        }
        !           172: 
        !           173:        return &this->public;
        !           174: }

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