2 * Copyright (C) 2017 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
19 #include "swid_gen_info.h"
22 #include <utils/lexparser.h>
24 typedef struct private_swid_gen_info_t private_swid_gen_info_t
;
27 * Private data of an swid_gen_info_t object.
29 struct private_swid_gen_info_t
{
32 * Public members of swid_gen_info_state_t
34 swid_gen_info_t
public;
42 * OS string 'Name_Version-Arch'
47 * Product string 'Name Version Arch'
52 * OS info about endpoint
54 imc_os_info_t
*os_info
;
59 * Replaces invalid character by a valid one
61 static void sanitize_uri(char *uri
, char a
, char b
)
77 METHOD(swid_gen_info_t
, get_os_type
, os_type_t
,
78 private_swid_gen_info_t
*this)
80 return this->os_info
->get_type(this->os_info
);
83 METHOD(swid_gen_info_t
, get_os
, char*,
84 private_swid_gen_info_t
*this, char **product
)
88 *product
= this->product
;
93 METHOD(swid_gen_info_t
, create_sw_id
, char*,
94 private_swid_gen_info_t
*this, char *package
, char *version
)
98 if (asprintf(&sw_id
, "%s__%s-%s%s%s", this->tag_creator
, this->os
,
99 package
, strlen(version
) ? "-" : "", version
) == -1)
103 sanitize_uri(sw_id
, ':', '~');
104 sanitize_uri(sw_id
, '+', '~');
109 METHOD(swid_gen_info_t
, destroy
, void,
110 private_swid_gen_info_t
*this)
112 this->os_info
->destroy(this->os_info
);
115 free(this->tag_creator
);
120 * Described in header.
122 swid_gen_info_t
*swid_gen_info_create(void)
124 private_swid_gen_info_t
*this;
125 chunk_t os_name
, os_version
, os_arch
;
128 tag_creator
= lib
->settings
->get_str(lib
->settings
,
129 "libimcv.swid_gen.tag_creator.regid", "strongswan.org");
133 .get_os_type
= _get_os_type
,
135 .create_sw_id
= _create_sw_id
,
138 .os_info
= imc_os_info_create(),
139 .tag_creator
= strdup(tag_creator
),
142 os_name
= this->os_info
->get_name(this->os_info
);
143 os_arch
= this->os_info
->get_version(this->os_info
);
145 /* get_version() returns version followed by arch */
146 if (!extract_token(&os_version
, ' ', &os_arch
))
148 DBG1(DBG_IMC
, "separation of OS version from arch failed");
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
,
158 DBG1(DBG_IMC
, "construction of OS string failed");
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)
168 DBG1(DBG_IMC
, "construction of product string failed");
173 return &this->public;