1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos #include <string.h>
11*b0d17251Schristos #include <stdarg.h>
12*b0d17251Schristos #include <openssl/bio.h>
13*b0d17251Schristos #include <openssl/safestack.h>
14*b0d17251Schristos #include "opt.h"
15*b0d17251Schristos
16*b0d17251Schristos static BIO *bio_in = NULL;
17*b0d17251Schristos static BIO *bio_out = NULL;
18*b0d17251Schristos static BIO *bio_err = NULL;
19*b0d17251Schristos
20*b0d17251Schristos /*-
21*b0d17251Schristos * This program sets up a chain of BIO_f_filter() on top of bio_out, how
22*b0d17251Schristos * many is governed by the user through -n. It allows the user to set the
23*b0d17251Schristos * indentation for each individual filter using -i and -p. Then it reads
24*b0d17251Schristos * text from bio_in and prints it out through the BIO chain.
25*b0d17251Schristos *
26*b0d17251Schristos * The filter index is counted from the source/sink, i.e. index 0 is closest
27*b0d17251Schristos * to it.
28*b0d17251Schristos *
29*b0d17251Schristos * Example:
30*b0d17251Schristos *
31*b0d17251Schristos * $ echo foo | ./bio_prefix_text -n 2 -i 1:32 -p 1:FOO -i 0:3
32*b0d17251Schristos * FOO foo
33*b0d17251Schristos * ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34*b0d17251Schristos * | |
35*b0d17251Schristos * | +------ 32 spaces from filter 1
36*b0d17251Schristos * +-------------------------- 3 spaces from filter 0
37*b0d17251Schristos */
38*b0d17251Schristos
39*b0d17251Schristos static size_t amount = 0;
40*b0d17251Schristos static BIO **chain = NULL;
41*b0d17251Schristos
42*b0d17251Schristos typedef enum OPTION_choice {
43*b0d17251Schristos OPT_ERR = -1,
44*b0d17251Schristos OPT_EOF = 0,
45*b0d17251Schristos OPT_AMOUNT,
46*b0d17251Schristos OPT_INDENT,
47*b0d17251Schristos OPT_PREFIX
48*b0d17251Schristos } OPTION_CHOICE;
49*b0d17251Schristos
50*b0d17251Schristos static const OPTIONS options[] = {
51*b0d17251Schristos { "n", OPT_AMOUNT, 'p', "Amount of BIO_f_prefix() filters" },
52*b0d17251Schristos /*
53*b0d17251Schristos * idx is the index to the BIO_f_filter chain(), where 0 is closest
54*b0d17251Schristos * to the source/sink BIO. If idx isn't given, 0 is assumed
55*b0d17251Schristos */
56*b0d17251Schristos { "i", OPT_INDENT, 's', "Indentation in form '[idx:]indent'" },
57*b0d17251Schristos { "p", OPT_PREFIX, 's', "Prefix in form '[idx:]prefix'" },
58*b0d17251Schristos { NULL }
59*b0d17251Schristos };
60*b0d17251Schristos
opt_printf_stderr(const char * fmt,...)61*b0d17251Schristos int opt_printf_stderr(const char *fmt, ...)
62*b0d17251Schristos {
63*b0d17251Schristos va_list ap;
64*b0d17251Schristos int ret;
65*b0d17251Schristos
66*b0d17251Schristos va_start(ap, fmt);
67*b0d17251Schristos ret = BIO_vprintf(bio_err, fmt, ap);
68*b0d17251Schristos va_end(ap);
69*b0d17251Schristos return ret;
70*b0d17251Schristos }
71*b0d17251Schristos
run_pipe(void)72*b0d17251Schristos static int run_pipe(void)
73*b0d17251Schristos {
74*b0d17251Schristos char buf[4096];
75*b0d17251Schristos
76*b0d17251Schristos while (!BIO_eof(bio_in)) {
77*b0d17251Schristos size_t bytes_in;
78*b0d17251Schristos size_t bytes_out;
79*b0d17251Schristos
80*b0d17251Schristos if (!BIO_read_ex(bio_in, buf, sizeof(buf), &bytes_in))
81*b0d17251Schristos return 0;
82*b0d17251Schristos bytes_out = 0;
83*b0d17251Schristos while (bytes_out < bytes_in) {
84*b0d17251Schristos size_t bytes;
85*b0d17251Schristos
86*b0d17251Schristos if (!BIO_write_ex(chain[amount - 1], buf, bytes_in, &bytes))
87*b0d17251Schristos return 0;
88*b0d17251Schristos bytes_out += bytes;
89*b0d17251Schristos }
90*b0d17251Schristos }
91*b0d17251Schristos return 1;
92*b0d17251Schristos }
93*b0d17251Schristos
setup_bio_chain(const char * progname)94*b0d17251Schristos static int setup_bio_chain(const char *progname)
95*b0d17251Schristos {
96*b0d17251Schristos BIO *next = NULL;
97*b0d17251Schristos size_t n = amount;
98*b0d17251Schristos
99*b0d17251Schristos chain = OPENSSL_zalloc(sizeof(*chain) * n);
100*b0d17251Schristos
101*b0d17251Schristos if (chain != NULL) {
102*b0d17251Schristos size_t i;
103*b0d17251Schristos
104*b0d17251Schristos next = bio_out;
105*b0d17251Schristos BIO_up_ref(next); /* Protection against freeing */
106*b0d17251Schristos
107*b0d17251Schristos for (i = 0; n > 0; i++, n--) {
108*b0d17251Schristos BIO *curr = BIO_new(BIO_f_prefix());
109*b0d17251Schristos
110*b0d17251Schristos if (curr == NULL)
111*b0d17251Schristos goto err;
112*b0d17251Schristos chain[i] = BIO_push(curr, next);
113*b0d17251Schristos if (chain[i] == NULL)
114*b0d17251Schristos goto err;
115*b0d17251Schristos next = chain[i];
116*b0d17251Schristos }
117*b0d17251Schristos }
118*b0d17251Schristos return chain != NULL;
119*b0d17251Schristos err:
120*b0d17251Schristos /* Free the chain we built up */
121*b0d17251Schristos BIO_free_all(next);
122*b0d17251Schristos OPENSSL_free(chain);
123*b0d17251Schristos return 0;
124*b0d17251Schristos }
125*b0d17251Schristos
cleanup(void)126*b0d17251Schristos static void cleanup(void)
127*b0d17251Schristos {
128*b0d17251Schristos if (chain != NULL) {
129*b0d17251Schristos BIO_free_all(chain[amount - 1]);
130*b0d17251Schristos OPENSSL_free(chain);
131*b0d17251Schristos }
132*b0d17251Schristos
133*b0d17251Schristos BIO_free_all(bio_in);
134*b0d17251Schristos BIO_free_all(bio_out);
135*b0d17251Schristos BIO_free_all(bio_err);
136*b0d17251Schristos }
137*b0d17251Schristos
setup(void)138*b0d17251Schristos static int setup(void)
139*b0d17251Schristos {
140*b0d17251Schristos OPTION_CHOICE o;
141*b0d17251Schristos char *arg;
142*b0d17251Schristos char *colon;
143*b0d17251Schristos char *endptr;
144*b0d17251Schristos size_t idx, indent;
145*b0d17251Schristos const char *progname = opt_getprog();
146*b0d17251Schristos
147*b0d17251Schristos bio_in = BIO_new_fp(stdin, BIO_NOCLOSE | BIO_FP_TEXT);
148*b0d17251Schristos bio_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
149*b0d17251Schristos bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
150*b0d17251Schristos #ifdef __VMS
151*b0d17251Schristos bio_out = BIO_push(BIO_new(BIO_f_linebuffer()), bio_out);
152*b0d17251Schristos bio_err = BIO_push(BIO_new(BIO_f_linebuffer()), bio_err);
153*b0d17251Schristos #endif
154*b0d17251Schristos
155*b0d17251Schristos OPENSSL_assert(bio_in != NULL);
156*b0d17251Schristos OPENSSL_assert(bio_out != NULL);
157*b0d17251Schristos OPENSSL_assert(bio_err != NULL);
158*b0d17251Schristos
159*b0d17251Schristos
160*b0d17251Schristos while ((o = opt_next()) != OPT_EOF) {
161*b0d17251Schristos switch (o) {
162*b0d17251Schristos case OPT_AMOUNT:
163*b0d17251Schristos arg = opt_arg();
164*b0d17251Schristos amount = strtoul(arg, &endptr, 10);
165*b0d17251Schristos if (endptr[0] != '\0') {
166*b0d17251Schristos BIO_printf(bio_err,
167*b0d17251Schristos "%s: -n argument isn't a decimal number: %s",
168*b0d17251Schristos progname, arg);
169*b0d17251Schristos return 0;
170*b0d17251Schristos }
171*b0d17251Schristos if (amount < 1) {
172*b0d17251Schristos BIO_printf(bio_err, "%s: must set up at least one filter",
173*b0d17251Schristos progname);
174*b0d17251Schristos return 0;
175*b0d17251Schristos }
176*b0d17251Schristos if (!setup_bio_chain(progname)) {
177*b0d17251Schristos BIO_printf(bio_err, "%s: failed setting up filter chain",
178*b0d17251Schristos progname);
179*b0d17251Schristos return 0;
180*b0d17251Schristos }
181*b0d17251Schristos break;
182*b0d17251Schristos case OPT_INDENT:
183*b0d17251Schristos if (chain == NULL) {
184*b0d17251Schristos BIO_printf(bio_err, "%s: -i given before -n", progname);
185*b0d17251Schristos return 0;
186*b0d17251Schristos }
187*b0d17251Schristos arg = opt_arg();
188*b0d17251Schristos colon = strchr(arg, ':');
189*b0d17251Schristos idx = 0;
190*b0d17251Schristos if (colon != NULL) {
191*b0d17251Schristos idx = strtoul(arg, &endptr, 10);
192*b0d17251Schristos if (endptr[0] != ':') {
193*b0d17251Schristos BIO_printf(bio_err,
194*b0d17251Schristos "%s: -i index isn't a decimal number: %s",
195*b0d17251Schristos progname, arg);
196*b0d17251Schristos return 0;
197*b0d17251Schristos }
198*b0d17251Schristos colon++;
199*b0d17251Schristos } else {
200*b0d17251Schristos colon = arg;
201*b0d17251Schristos }
202*b0d17251Schristos indent = strtoul(colon, &endptr, 10);
203*b0d17251Schristos if (endptr[0] != '\0') {
204*b0d17251Schristos BIO_printf(bio_err,
205*b0d17251Schristos "%s: -i value isn't a decimal number: %s",
206*b0d17251Schristos progname, arg);
207*b0d17251Schristos return 0;
208*b0d17251Schristos }
209*b0d17251Schristos if (idx >= amount) {
210*b0d17251Schristos BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu",
211*b0d17251Schristos progname, idx, amount - 1);
212*b0d17251Schristos return 0;
213*b0d17251Schristos }
214*b0d17251Schristos if (BIO_set_indent(chain[idx], (long)indent) <= 0) {
215*b0d17251Schristos BIO_printf(bio_err, "%s: failed setting indentation: %s",
216*b0d17251Schristos progname, arg);
217*b0d17251Schristos return 0;
218*b0d17251Schristos }
219*b0d17251Schristos break;
220*b0d17251Schristos case OPT_PREFIX:
221*b0d17251Schristos if (chain == NULL) {
222*b0d17251Schristos BIO_printf(bio_err, "%s: -p given before -n", progname);
223*b0d17251Schristos return 0;
224*b0d17251Schristos }
225*b0d17251Schristos arg = opt_arg();
226*b0d17251Schristos colon = strchr(arg, ':');
227*b0d17251Schristos idx = 0;
228*b0d17251Schristos if (colon != NULL) {
229*b0d17251Schristos idx = strtoul(arg, &endptr, 10);
230*b0d17251Schristos if (endptr[0] != ':') {
231*b0d17251Schristos BIO_printf(bio_err,
232*b0d17251Schristos "%s: -p index isn't a decimal number: %s",
233*b0d17251Schristos progname, arg);
234*b0d17251Schristos return 0;
235*b0d17251Schristos }
236*b0d17251Schristos colon++;
237*b0d17251Schristos } else {
238*b0d17251Schristos colon = arg;
239*b0d17251Schristos }
240*b0d17251Schristos if (idx >= amount) {
241*b0d17251Schristos BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu",
242*b0d17251Schristos progname, idx, amount - 1);
243*b0d17251Schristos return 0;
244*b0d17251Schristos }
245*b0d17251Schristos if (BIO_set_prefix(chain[idx], colon) <= 0) {
246*b0d17251Schristos BIO_printf(bio_err, "%s: failed setting prefix: %s",
247*b0d17251Schristos progname, arg);
248*b0d17251Schristos return 0;
249*b0d17251Schristos }
250*b0d17251Schristos break;
251*b0d17251Schristos default:
252*b0d17251Schristos case OPT_ERR:
253*b0d17251Schristos return 0;
254*b0d17251Schristos }
255*b0d17251Schristos }
256*b0d17251Schristos return 1;
257*b0d17251Schristos }
258*b0d17251Schristos
main(int argc,char ** argv)259*b0d17251Schristos int main(int argc, char **argv)
260*b0d17251Schristos {
261*b0d17251Schristos int rv = EXIT_SUCCESS;
262*b0d17251Schristos
263*b0d17251Schristos opt_init(argc, argv, options);
264*b0d17251Schristos rv = (setup() && run_pipe()) ? EXIT_SUCCESS : EXIT_FAILURE;
265*b0d17251Schristos cleanup();
266*b0d17251Schristos return rv;
267*b0d17251Schristos }
268