varnish-cache/bin/varnishd/mgt/mgt_symtab.c
0
/*-
1
 * Copyright (c) 2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * VCL/VMOD symbol table
30
 */
31
32
#include "config.h"
33
34
#include <fcntl.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <unistd.h>
39
#include <sys/stat.h>
40
41
#include "mgt/mgt.h"
42
#include "mgt/mgt_vcl.h"
43
44
#include "vcli_serve.h"
45
#include "vjsn.h"
46
47
/*--------------------------------------------------------------------*/
48
49
static const char *
50 10155
mgt_vcl_symtab_val(const struct vjsn_val *vv, const char *val)
51
{
52
        const struct vjsn_val *jv;
53
54 10155
        jv = vjsn_child(vv, val);
55 10155
        AN(jv);
56 10155
        assert(vjsn_is_string(jv));
57 10155
        AN(jv->value);
58 10155
        return (jv->value);
59
}
60
61
static void
62 105
mgt_vcl_import_vcl(struct vclprog *vp1, const struct vjsn_val *vv)
63
{
64
        struct vclprog *vp2;
65
66 105
        CHECK_OBJ_NOTNULL(vp1, VCLPROG_MAGIC);
67 105
        AN(vv);
68
69 105
        vp2 = mcf_vcl_byname(mgt_vcl_symtab_val(vv, "name"));
70 105
        CHECK_OBJ_NOTNULL(vp2, VCLPROG_MAGIC);
71 105
        mgt_vcl_dep_add(vp1, vp2)->vj = vv;
72 105
}
73
74
static int
75 1780
mgt_vcl_cache_vmod(const char *nm, const char *fm, const char *to)
76
{
77
        int fi, fo;
78 1780
        int ret = 0;
79
        ssize_t sz;
80
        char buf[BUFSIZ];
81
82 1780
        fo = open(to, O_WRONLY | O_CREAT | O_EXCL, 0744);
83 1780
        if (fo < 0 && errno == EEXIST)
84 0
                return (0);
85 1780
        if (fo < 0) {
86 0
                fprintf(stderr, "While creating copy of vmod %s:\n\t%s: %s\n",
87 0
                    nm, to, VAS_errtxt(errno));
88 0
                return (1);
89
        }
90 1780
        fi = open(fm, O_RDONLY);
91 1780
        if (fi < 0) {
92 0
                fprintf(stderr, "Opening vmod %s from %s: %s\n",
93 0
                    nm, fm, VAS_errtxt(errno));
94 0
                AZ(unlink(to));
95 0
                closefd(&fo);
96 0
                return (1);
97
        }
98 373190
        while (1) {
99 373190
                sz = read(fi, buf, sizeof buf);
100 373190
                if (sz == 0)
101 1780
                        break;
102 371410
                if (sz < 0 || sz != write(fo, buf, sz)) {
103 0
                        fprintf(stderr, "Copying vmod %s: %s\n",
104 0
                            nm, VAS_errtxt(errno));
105 0
                        AZ(unlink(to));
106 0
                        ret = 1;
107 0
                        break;
108
                }
109
        }
110 1780
        closefd(&fi);
111 1780
        AZ(fchmod(fo, 0444));
112 1780
        closefd(&fo);
113 1780
        return (ret);
114 1780
}
115
116
static void
117 2490
mgt_vcl_import_vmod(struct vclprog *vp, const struct vjsn_val *vv)
118
{
119
        struct vmodfile *vf;
120
        struct vmoddep *vd;
121
        const char *v_name;
122
        const char *v_file;
123
        const char *v_dst;
124
        const struct vjsn_val *jv;
125
126 2490
        CHECK_OBJ_NOTNULL(vp, VCLPROG_MAGIC);
127 2490
        AN(vv);
128
129 2490
        jv = vjsn_child(vv, "vext");
130 2490
        if (vjsn_is_true(jv))
131 5
                return;
132 2485
        AN(jv);
133 2485
        v_name = mgt_vcl_symtab_val(vv, "name");
134 2485
        v_file = mgt_vcl_symtab_val(vv, "file");
135 2485
        v_dst = mgt_vcl_symtab_val(vv, "dst");
136
137 3220
        VTAILQ_FOREACH(vf, &vmodhead, list)
138 1440
                if (!strcmp(vf->fname, v_dst))
139 705
                        break;
140 2485
        if (vf == NULL) {
141 1780
                ALLOC_OBJ(vf, VMODFILE_MAGIC);
142 1780
                AN(vf);
143 1780
                REPLACE(vf->fname, v_dst);
144 1780
                VTAILQ_INIT(&vf->vcls);
145 1780
                AZ(mgt_vcl_cache_vmod(v_name, v_file, v_dst));
146 1780
                VTAILQ_INSERT_TAIL(&vmodhead, vf, list);
147 1780
        }
148 2485
        ALLOC_OBJ(vd, VMODDEP_MAGIC);
149 2485
        AN(vd);
150 2485
        vd->to = vf;
151 2485
        VTAILQ_INSERT_TAIL(&vp->vmods, vd, lfrom);
152 2485
        VTAILQ_INSERT_TAIL(&vf->vcls, vd, lto);
153 2490
}
154
155
void
156 6045
mgt_vcl_symtab(struct vclprog *vp, const char *input)
157
{
158
        struct vjsn *vj;
159
        struct vjsn_val *v1, *v2;
160
        const char *typ, *err;
161
162 6045
        CHECK_OBJ_NOTNULL(vp, VCLPROG_MAGIC);
163 6045
        AN(input);
164 6045
        vj = vjsn_parse(input, &err);
165 6045
        if (err != NULL) {
166 0
                fprintf(stderr, "FATAL: Symtab parse error: %s\n%s\n",
167 0
                    err, input);
168 0
        }
169 6045
        AZ(err);
170 6045
        AN(vj);
171 6045
        vp->symtab = vj;
172 6045
        assert(vjsn_is_array(vj->value));
173 14685
        VTAILQ_FOREACH(v1, &vj->value->children, list) {
174 8640
                assert(vjsn_is_object(v1));
175 8640
                v2 = vjsn_child(v1, "dir");
176 8640
                if (v2 == NULL)
177 6045
                        continue;
178 2595
                assert(vjsn_is_string(v2));
179 2595
                if (strcmp(v2->value, "import"))
180 0
                        continue;
181 2595
                typ = mgt_vcl_symtab_val(v1, "type");
182 2595
                if (!strcmp(typ, "$VMOD"))
183 2490
                        mgt_vcl_import_vmod(vp, v1);
184 105
                else if (!strcmp(typ, "$VCL"))
185 105
                        mgt_vcl_import_vcl(vp, v1);
186
                else
187 0
                        WRONG("Bad symtab import entry");
188 2595
        }
189 6045
}
190
191
void
192 7640
mgt_vcl_symtab_clean(struct vclprog *vp)
193
{
194
195 7640
        if (vp->symtab)
196 6045
                vjsn_delete(&vp->symtab);
197 7640
}
198
199
/*--------------------------------------------------------------------*/
200
201
static void
202 550
mcf_vcl_vjsn_dump(struct cli *cli, const struct vjsn_val *vj, int indent)
203
{
204
        struct vjsn_val *vj1;
205 550
        AN(cli);
206 550
        AN(vj);
207
208 550
        VCLI_Out(cli, "%*s", indent, "");
209 550
        if (vj->name != NULL)
210 370
                VCLI_Out(cli, "[\"%s\"]: ", vj->name);
211 550
        VCLI_Out(cli, "{%s}", vj->type);
212 550
        if (vj->value != NULL)
213 360
                VCLI_Out(cli, " <%s>", vj->value);
214 550
        VCLI_Out(cli, "\n");
215 1000
        VTAILQ_FOREACH(vj1, &vj->children, list)
216 450
                mcf_vcl_vjsn_dump(cli, vj1, indent + 2);
217 550
}
218
219
void v_matchproto_(cli_func_t)
220 5
mcf_vcl_symtab(struct cli *cli, const char * const *av, void *priv)
221
{
222
        struct vclprog *vp;
223
        struct vcldep *vd;
224
225 5
        (void)av;
226 5
        (void)priv;
227 55
        VTAILQ_FOREACH(vp, &vclhead, list) {
228 50
                if (mcf_is_label(vp))
229 10
                        VCLI_Out(cli, "Label: %s\n", vp->name);
230
                else
231 40
                        VCLI_Out(cli, "Vcl: %s\n", vp->name);
232 50
                if (!VTAILQ_EMPTY(&vp->dfrom)) {
233 40
                        VCLI_Out(cli, "  imports from:\n");
234 80
                        VTAILQ_FOREACH(vd, &vp->dfrom, lfrom) {
235 40
                                VCLI_Out(cli, "    %s\n", vd->to->name);
236 40
                                if (vd->vj)
237 30
                                        mcf_vcl_vjsn_dump(cli, vd->vj, 6);
238 40
                        }
239 40
                }
240 50
                if (!VTAILQ_EMPTY(&vp->dto)) {
241 20
                        VCLI_Out(cli, "  exports to:\n");
242 60
                        VTAILQ_FOREACH(vd, &vp->dto, lto) {
243 40
                                VCLI_Out(cli, "    %s\n", vd->from->name);
244 40
                                if (vd->vj)
245 30
                                        mcf_vcl_vjsn_dump(cli, vd->vj, 6);
246 40
                        }
247 20
                }
248 50
                if (vp->symtab != NULL) {
249 40
                        VCLI_Out(cli, "  symtab:\n");
250 40
                        mcf_vcl_vjsn_dump(cli, vp->symtab->value, 4);
251 40
                }
252 50
        }
253 5
}