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 2043
mgt_vcl_symtab_val(const struct vjsn_val *vv, const char *val)
51
{
52
        const struct vjsn_val *jv;
53
54 2043
        jv = vjsn_child(vv, val);
55 2043
        AN(jv);
56 2043
        assert(vjsn_is_string(jv));
57 2043
        AN(jv->value);
58 2043
        return (jv->value);
59
}
60
61
static void
62 21
mgt_vcl_import_vcl(struct vclprog *vp1, const struct vjsn_val *vv)
63
{
64
        struct vclprog *vp2;
65
66 21
        CHECK_OBJ_NOTNULL(vp1, VCLPROG_MAGIC);
67 21
        AN(vv);
68
69 21
        vp2 = mcf_vcl_byname(mgt_vcl_symtab_val(vv, "name"));
70 21
        CHECK_OBJ_NOTNULL(vp2, VCLPROG_MAGIC);
71 21
        mgt_vcl_dep_add(vp1, vp2)->vj = vv;
72 21
}
73
74
static int
75 359
mgt_vcl_cache_vmod(const char *nm, const char *fm, const char *to)
76
{
77
        int fi, fo;
78 359
        int ret = 0;
79
        ssize_t sz;
80
        char buf[BUFSIZ];
81
82 359
        fo = open(to, O_WRONLY | O_CREAT | O_EXCL, 0744);
83 359
        if (fo < 0 && errno == EEXIST)
84 0
                return (0);
85 359
        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 359
        fi = open(fm, O_RDONLY);
91 359
        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 76901
        while (1) {
99 76901
                sz = read(fi, buf, sizeof buf);
100 76901
                if (sz == 0)
101 359
                        break;
102 76542
                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 359
        closefd(&fi);
111 359
        AZ(fchmod(fo, 0444));
112 359
        closefd(&fo);
113 359
        return (ret);
114 359
}
115
116
static void
117 501
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 501
        CHECK_OBJ_NOTNULL(vp, VCLPROG_MAGIC);
127 501
        AN(vv);
128
129 501
        jv = vjsn_child(vv, "vext");
130 501
        if (vjsn_is_true(jv))
131 1
                return;
132 500
        AN(jv);
133 500
        v_name = mgt_vcl_symtab_val(vv, "name");
134 500
        v_file = mgt_vcl_symtab_val(vv, "file");
135 500
        v_dst = mgt_vcl_symtab_val(vv, "dst");
136
137 648
        VTAILQ_FOREACH(vf, &vmodhead, list)
138 289
                if (!strcmp(vf->fname, v_dst))
139 141
                        break;
140 500
        if (vf == NULL) {
141 359
                ALLOC_OBJ(vf, VMODFILE_MAGIC);
142 359
                AN(vf);
143 359
                REPLACE(vf->fname, v_dst);
144 359
                VTAILQ_INIT(&vf->vcls);
145 359
                AZ(mgt_vcl_cache_vmod(v_name, v_file, v_dst));
146 359
                VTAILQ_INSERT_TAIL(&vmodhead, vf, list);
147 359
        }
148 500
        ALLOC_OBJ(vd, VMODDEP_MAGIC);
149 500
        AN(vd);
150 500
        vd->to = vf;
151 500
        VTAILQ_INSERT_TAIL(&vp->vmods, vd, lfrom);
152 500
        VTAILQ_INSERT_TAIL(&vf->vcls, vd, lto);
153 501
}
154
155
void
156 1227
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 1227
        CHECK_OBJ_NOTNULL(vp, VCLPROG_MAGIC);
163 1227
        AN(input);
164 1227
        vj = vjsn_parse(input, &err);
165 1227
        if (err != NULL) {
166 0
                fprintf(stderr, "FATAL: Symtab parse error: %s\n%s\n",
167 0
                    err, input);
168 0
        }
169 1227
        AZ(err);
170 1227
        AN(vj);
171 1227
        vp->symtab = vj;
172 1227
        assert(vjsn_is_array(vj->value));
173 2976
        VTAILQ_FOREACH(v1, &vj->value->children, list) {
174 1749
                assert(vjsn_is_object(v1));
175 1749
                v2 = vjsn_child(v1, "dir");
176 1749
                if (v2 == NULL)
177 1227
                        continue;
178 522
                assert(vjsn_is_string(v2));
179 522
                if (strcmp(v2->value, "import"))
180 0
                        continue;
181 522
                typ = mgt_vcl_symtab_val(v1, "type");
182 522
                if (!strcmp(typ, "$VMOD"))
183 501
                        mgt_vcl_import_vmod(vp, v1);
184 21
                else if (!strcmp(typ, "$VCL"))
185 21
                        mgt_vcl_import_vcl(vp, v1);
186
                else
187 0
                        WRONG("Bad symtab import entry");
188 522
        }
189 1227
}
190
191
void
192 1550
mgt_vcl_symtab_clean(struct vclprog *vp)
193
{
194
195 1550
        if (vp->symtab)
196 1227
                vjsn_delete(&vp->symtab);
197 1550
}
198
199
/*--------------------------------------------------------------------*/
200
201
static void
202 110
mcf_vcl_vjsn_dump(struct cli *cli, const struct vjsn_val *vj, int indent)
203
{
204
        struct vjsn_val *vj1;
205 110
        AN(cli);
206 110
        AN(vj);
207
208 110
        VCLI_Out(cli, "%*s", indent, "");
209 110
        if (vj->name != NULL)
210 74
                VCLI_Out(cli, "[\"%s\"]: ", vj->name);
211 110
        VCLI_Out(cli, "{%s}", vj->type);
212 110
        if (vj->value != NULL)
213 72
                VCLI_Out(cli, " <%s>", vj->value);
214 110
        VCLI_Out(cli, "\n");
215 200
        VTAILQ_FOREACH(vj1, &vj->children, list)
216 90
                mcf_vcl_vjsn_dump(cli, vj1, indent + 2);
217 110
}
218
219
void v_matchproto_(cli_func_t)
220 1
mcf_vcl_symtab(struct cli *cli, const char * const *av, void *priv)
221
{
222
        struct vclprog *vp;
223
        struct vcldep *vd;
224
225 1
        (void)av;
226 1
        (void)priv;
227 11
        VTAILQ_FOREACH(vp, &vclhead, list) {
228 10
                if (mcf_is_label(vp))
229 2
                        VCLI_Out(cli, "Label: %s\n", vp->name);
230
                else
231 8
                        VCLI_Out(cli, "Vcl: %s\n", vp->name);
232 10
                if (!VTAILQ_EMPTY(&vp->dfrom)) {
233 8
                        VCLI_Out(cli, "  imports from:\n");
234 16
                        VTAILQ_FOREACH(vd, &vp->dfrom, lfrom) {
235 8
                                VCLI_Out(cli, "    %s\n", vd->to->name);
236 8
                                if (vd->vj)
237 6
                                        mcf_vcl_vjsn_dump(cli, vd->vj, 6);
238 8
                        }
239 8
                }
240 10
                if (!VTAILQ_EMPTY(&vp->dto)) {
241 4
                        VCLI_Out(cli, "  exports to:\n");
242 12
                        VTAILQ_FOREACH(vd, &vp->dto, lto) {
243 8
                                VCLI_Out(cli, "    %s\n", vd->from->name);
244 8
                                if (vd->vj)
245 6
                                        mcf_vcl_vjsn_dump(cli, vd->vj, 6);
246 8
                        }
247 4
                }
248 10
                if (vp->symtab != NULL) {
249 8
                        VCLI_Out(cli, "  symtab:\n");
250 8
                        mcf_vcl_vjsn_dump(cli, vp->symtab->value, 4);
251 8
                }
252 10
        }
253 1
}