2 * Copyright (C) 2017 Andreas Steffen
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <bio/bio_writer.h>
24 #define SWID_GENERATOR "/usr/local/bin/swid_generator"
26 typedef struct private_swid_gen_t private_swid_gen_t
;
29 * Private data of a swid_gen_t object.
32 struct private_swid_gen_t
{
35 * Public swid_gen_t interface.
40 * Path of the SWID generator command
45 * Entity name of the tagCreator
50 * Regid of the tagCreator
56 METHOD(swid_gen_t
, generate_tag
, char*,
57 private_swid_gen_t
*this, char *sw_id
, char *package
, char *version
,
58 bool full
, bool pretty
)
61 size_t tag_buf_len
= 8192;
62 char tag_buf
[tag_buf_len
], command
[BUF_LEN
];
67 /* Compose the SWID generator command */
68 if (full
|| !package
|| !version
)
70 snprintf(command
, BUF_LEN
, "%s swid --entity-name \"%s\" "
71 "--regid %s --software-id %s%s%s",
72 this->generator
, this->entity
, this->regid
, sw_id
,
73 full
? " --full" : "", pretty
? " --pretty" : "");
77 snprintf(command
, BUF_LEN
, "%s swid --entity-name \"%s\" "
78 "--regid %s --name %s --version-string %s%s",
79 this->generator
, this->entity
, this->regid
, package
,
80 version
, pretty
? " --pretty" : "");
83 /* Open a pipe stream for reading the SWID generator output */
84 file
= popen(command
, "r");
87 writer
= bio_writer_create(tag_buf_len
);
90 if (!fgets(tag_buf
, tag_buf_len
, file
))
94 writer
->write_data(writer
, chunk_create(tag_buf
, strlen(tag_buf
)));
97 swid_tag
= writer
->extract_buf(writer
);
98 writer
->destroy(writer
);
100 if (swid_tag
.len
> 0)
103 tag
[swid_tag
.len
- 1] = '\0';
107 chunk_free(&swid_tag
);
112 DBG1(DBG_IMC
, "failed to run swid_generator command");
119 /** public enumerator interface */
121 /** swid_generator output stream */
123 /** generate software identifier only */
125 } swid_gen_enumerator_t
;
127 METHOD(enumerator_t
, enumerate
, bool,
128 swid_gen_enumerator_t
*this, va_list args
)
132 VA_ARGS_VGET(args
, out
);
134 if (this->sw_id_only
)
139 if (!fgets(line
, sizeof(line
), this->file
))
150 /* remove trailing newline if present */
151 if (line
[len
- 1] == '\n')
155 DBG3(DBG_IMC
, " %.*s", len
, line
);
156 *out
= chunk_clone(chunk_create(line
, len
));
160 bool last_newline
= TRUE
;
161 size_t len
, line_len
= 8192;
163 bio_writer_t
*writer
;
166 writer
= bio_writer_create(line_len
);
169 if (!fgets(line
, line_len
, this->file
))
175 if (last_newline
&& line
[0] == '\n')
181 last_newline
= (line
[len
-1] == '\n');
182 writer
->write_data(writer
, chunk_create(line
, len
));
185 swid_tag
= writer
->extract_buf(writer
);
186 writer
->destroy(writer
);
188 if (swid_tag
.len
<= 1)
190 chunk_free(&swid_tag
);
194 /* remove trailing newline if present */
195 if (swid_tag
.ptr
[swid_tag
.len
- 1] == '\n')
199 DBG3(DBG_IMC
, " %.*s", swid_tag
.len
, swid_tag
.ptr
);
206 METHOD(enumerator_t
, enumerator_destroy
, void,
207 swid_gen_enumerator_t
*this)
213 METHOD(swid_gen_t
, create_tag_enumerator
, enumerator_t
*,
214 private_swid_gen_t
*this, bool sw_id_only
, bool full
, bool pretty
)
216 swid_gen_enumerator_t
*enumerator
;
217 char command
[BUF_LEN
];
218 char doc_separator
[] = "'\n\n'";
221 /* Assemble the SWID generator command */
224 snprintf(command
, BUF_LEN
, "%s software-id --regid %s ",
225 this->generator
, this->regid
);
229 snprintf(command
, BUF_LEN
, "%s swid --entity-name \"%s\" --regid %s "
230 "--doc-separator %s%s%s", this->generator
, this->entity
,
231 this->regid
, doc_separator
, pretty
? " --pretty" : "",
232 full
? " --full" : "");
235 /* Open a pipe stream for reading the SWID generator output */
236 file
= popen(command
, "r");
239 DBG1(DBG_IMC
, "failed to run swid_generator command");
245 .enumerate
= enumerator_enumerate_default
,
246 .venumerate
= _enumerate
,
247 .destroy
= _enumerator_destroy
,
249 .sw_id_only
= sw_id_only
,
253 return &enumerator
->public;
256 METHOD(swid_gen_t
, destroy
, void,
257 private_swid_gen_t
*this)
259 free(this->generator
);
268 swid_gen_t
*swid_gen_create(void)
270 private_swid_gen_t
*this;
271 char *entity
, *regid
, *generator
;
273 entity
= lib
->settings
->get_str(lib
->settings
,
274 "libimcv.swid_gen.tag_creator.name", "strongSwan Project");
275 regid
= lib
->settings
->get_str(lib
->settings
,
276 "libimcv.swid_gen.tag_creator.regid", "strongswan.org");
277 generator
= lib
->settings
->get_str(lib
->settings
,
278 "libimcv.swid_gen.command", SWID_GENERATOR
);
282 .generate_tag
= _generate_tag
,
283 .create_tag_enumerator
= _create_tag_enumerator
,
286 .generator
= strdup(generator
),
287 .entity
= strdup(entity
),
288 .regid
= strdup(regid
),
291 return &this->public;