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