Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 8a35f56c 2022-07-16 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 8a35f56c 2022-07-16 thomas *
10 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
11 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
12 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
13 8a35f56c 2022-07-16 thomas *
14 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 8a35f56c 2022-07-16 thomas */
22 8a35f56c 2022-07-16 thomas
23 8a35f56c 2022-07-16 thomas %{
24 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
25 4fccd2fe 2023-03-08 thomas
26 8a35f56c 2022-07-16 thomas #include <sys/ioctl.h>
27 8a35f56c 2022-07-16 thomas #include <sys/types.h>
28 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
29 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
30 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
31 8a35f56c 2022-07-16 thomas
32 8a35f56c 2022-07-16 thomas #include <net/if.h>
33 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
34 8a35f56c 2022-07-16 thomas
35 8a35f56c 2022-07-16 thomas #include <arpa/inet.h>
36 8a35f56c 2022-07-16 thomas
37 8a35f56c 2022-07-16 thomas #include <ctype.h>
38 8a35f56c 2022-07-16 thomas #include <err.h>
39 8a35f56c 2022-07-16 thomas #include <errno.h>
40 8a35f56c 2022-07-16 thomas #include <event.h>
41 8a35f56c 2022-07-16 thomas #include <ifaddrs.h>
42 8a35f56c 2022-07-16 thomas #include <limits.h>
43 8a35f56c 2022-07-16 thomas #include <netdb.h>
44 8a35f56c 2022-07-16 thomas #include <stdarg.h>
45 8a35f56c 2022-07-16 thomas #include <stdlib.h>
46 8a35f56c 2022-07-16 thomas #include <stdio.h>
47 8a35f56c 2022-07-16 thomas #include <string.h>
48 8a35f56c 2022-07-16 thomas #include <syslog.h>
49 8a35f56c 2022-07-16 thomas #include <unistd.h>
50 161663e7 2023-03-11 thomas
51 161663e7 2023-03-11 thomas #include "got_reference.h"
52 8a35f56c 2022-07-16 thomas
53 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
54 8a35f56c 2022-07-16 thomas
55 8a35f56c 2022-07-16 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 8a35f56c 2022-07-16 thomas static struct file {
57 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(file) entry;
58 8a35f56c 2022-07-16 thomas FILE *stream;
59 8a35f56c 2022-07-16 thomas char *name;
60 8a35f56c 2022-07-16 thomas int lineno;
61 8a35f56c 2022-07-16 thomas int errors;
62 8a35f56c 2022-07-16 thomas } *file;
63 8a35f56c 2022-07-16 thomas struct file *newfile(const char *, int);
64 8a35f56c 2022-07-16 thomas static void closefile(struct file *);
65 8a35f56c 2022-07-16 thomas int check_file_secrecy(int, const char *);
66 8a35f56c 2022-07-16 thomas int yyparse(void);
67 8a35f56c 2022-07-16 thomas int yylex(void);
68 8a35f56c 2022-07-16 thomas int yyerror(const char *, ...)
69 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)))
70 8a35f56c 2022-07-16 thomas __attribute__((__nonnull__ (1)));
71 8a35f56c 2022-07-16 thomas int kw_cmp(const void *, const void *);
72 8a35f56c 2022-07-16 thomas int lookup(char *);
73 8a35f56c 2022-07-16 thomas int lgetc(int);
74 8a35f56c 2022-07-16 thomas int lungetc(int);
75 8a35f56c 2022-07-16 thomas int findeol(void);
76 8a35f56c 2022-07-16 thomas
77 8a35f56c 2022-07-16 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 8a35f56c 2022-07-16 thomas struct sym {
79 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(sym) entry;
80 8a35f56c 2022-07-16 thomas int used;
81 8a35f56c 2022-07-16 thomas int persist;
82 8a35f56c 2022-07-16 thomas char *nam;
83 8a35f56c 2022-07-16 thomas char *val;
84 8a35f56c 2022-07-16 thomas };
85 8a35f56c 2022-07-16 thomas
86 8a35f56c 2022-07-16 thomas int symset(const char *, const char *, int);
87 8a35f56c 2022-07-16 thomas char *symget(const char *);
88 8a35f56c 2022-07-16 thomas
89 8a35f56c 2022-07-16 thomas static int errors;
90 8a35f56c 2022-07-16 thomas
91 8a35f56c 2022-07-16 thomas static struct gotwebd *gotwebd;
92 8a35f56c 2022-07-16 thomas static struct server *new_srv;
93 8a35f56c 2022-07-16 thomas static struct server *conf_new_server(const char *);
94 8a35f56c 2022-07-16 thomas int getservice(const char *);
95 8a35f56c 2022-07-16 thomas int n;
96 8a35f56c 2022-07-16 thomas
97 e760210d 2023-11-16 thomas int get_addrs(const char *, const char *, struct server *);
98 e4c7e0b0 2022-08-30 thomas int addr_dup_check(struct addresslist *, struct address *,
99 e4c7e0b0 2022-08-30 thomas const char *, const char *);
100 e4c7e0b0 2022-08-30 thomas int add_addr(struct server *, struct address *);
101 8a35f56c 2022-07-16 thomas
102 8a35f56c 2022-07-16 thomas typedef struct {
103 8a35f56c 2022-07-16 thomas union {
104 8a35f56c 2022-07-16 thomas long long number;
105 8a35f56c 2022-07-16 thomas char *string;
106 8a35f56c 2022-07-16 thomas } v;
107 8a35f56c 2022-07-16 thomas int lineno;
108 8a35f56c 2022-07-16 thomas } YYSTYPE;
109 8a35f56c 2022-07-16 thomas
110 8a35f56c 2022-07-16 thomas %}
111 8a35f56c 2022-07-16 thomas
112 c24a47af 2022-08-11 thomas %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
113 8a35f56c 2022-07-16 thomas %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
114 8a35f56c 2022-07-16 thomas %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
115 3991b2a5 2022-10-31 thomas %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
116 bea82c4f 2023-01-06 thomas %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
117 8a35f56c 2022-07-16 thomas
118 8a35f56c 2022-07-16 thomas %token <v.string> STRING
119 8a35f56c 2022-07-16 thomas %token <v.number> NUMBER
120 8a35f56c 2022-07-16 thomas %type <v.number> boolean
121 61bbe977 2023-11-16 thomas %type <v.string> listen_addr
122 8a35f56c 2022-07-16 thomas
123 8a35f56c 2022-07-16 thomas %%
124 8a35f56c 2022-07-16 thomas
125 cfab1835 2022-10-04 thomas grammar : /* empty */
126 8a35f56c 2022-07-16 thomas | grammar '\n'
127 cfab1835 2022-10-04 thomas | grammar varset '\n'
128 8a35f56c 2022-07-16 thomas | grammar main '\n'
129 8a35f56c 2022-07-16 thomas | grammar server '\n'
130 cfab1835 2022-10-04 thomas | grammar error '\n' { file->errors++; }
131 cfab1835 2022-10-04 thomas ;
132 cfab1835 2022-10-04 thomas
133 cfab1835 2022-10-04 thomas varset : STRING '=' STRING {
134 cfab1835 2022-10-04 thomas char *s = $1;
135 cfab1835 2022-10-04 thomas while (*s++) {
136 cfab1835 2022-10-04 thomas if (isspace((unsigned char)*s)) {
137 cfab1835 2022-10-04 thomas yyerror("macro name cannot contain "
138 cfab1835 2022-10-04 thomas "whitespace");
139 cfab1835 2022-10-04 thomas free($1);
140 cfab1835 2022-10-04 thomas free($3);
141 cfab1835 2022-10-04 thomas YYERROR;
142 cfab1835 2022-10-04 thomas }
143 cfab1835 2022-10-04 thomas }
144 cfab1835 2022-10-04 thomas if (symset($1, $3, 0) == -1)
145 cfab1835 2022-10-04 thomas fatal("cannot store variable");
146 cfab1835 2022-10-04 thomas free($1);
147 cfab1835 2022-10-04 thomas free($3);
148 cfab1835 2022-10-04 thomas }
149 8a35f56c 2022-07-16 thomas ;
150 8a35f56c 2022-07-16 thomas
151 8a35f56c 2022-07-16 thomas boolean : STRING {
152 8a35f56c 2022-07-16 thomas if (strcasecmp($1, "1") == 0 ||
153 8a35f56c 2022-07-16 thomas strcasecmp($1, "on") == 0)
154 8a35f56c 2022-07-16 thomas $$ = 1;
155 8a35f56c 2022-07-16 thomas else if (strcasecmp($1, "0") == 0 ||
156 ee003af8 2023-06-15 thomas strcasecmp($1, "off") == 0)
157 8a35f56c 2022-07-16 thomas $$ = 0;
158 8a35f56c 2022-07-16 thomas else {
159 8a35f56c 2022-07-16 thomas yyerror("invalid boolean value '%s'", $1);
160 8a35f56c 2022-07-16 thomas free($1);
161 8a35f56c 2022-07-16 thomas YYERROR;
162 8a35f56c 2022-07-16 thomas }
163 8a35f56c 2022-07-16 thomas free($1);
164 8a35f56c 2022-07-16 thomas }
165 8a35f56c 2022-07-16 thomas | ON { $$ = 1; }
166 a0bd573d 2023-06-15 thomas | NUMBER {
167 a0bd573d 2023-06-15 thomas if ($1 != 0 && $1 != 1) {
168 a0bd573d 2023-06-15 thomas yyerror("invalid boolean value '%lld'", $1);
169 a0bd573d 2023-06-15 thomas YYERROR;
170 a0bd573d 2023-06-15 thomas }
171 a0bd573d 2023-06-15 thomas $$ = $1;
172 a0bd573d 2023-06-15 thomas }
173 61bbe977 2023-11-16 thomas ;
174 61bbe977 2023-11-16 thomas
175 61bbe977 2023-11-16 thomas listen_addr : '*' { $$ = NULL; }
176 61bbe977 2023-11-16 thomas | STRING
177 8a35f56c 2022-07-16 thomas ;
178 8a35f56c 2022-07-16 thomas
179 8a35f56c 2022-07-16 thomas main : PREFORK NUMBER {
180 a0bd573d 2023-06-15 thomas if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
181 a0bd573d 2023-06-15 thomas yyerror("prefork is %s: %lld",
182 a0bd573d 2023-06-15 thomas $2 <= 0 ? "too small" : "too large", $2);
183 a0bd573d 2023-06-15 thomas YYERROR;
184 a0bd573d 2023-06-15 thomas }
185 8a35f56c 2022-07-16 thomas gotwebd->prefork_gotwebd = $2;
186 8a35f56c 2022-07-16 thomas }
187 8a35f56c 2022-07-16 thomas | CHROOT STRING {
188 90757f0a 2023-06-15 thomas if (*$2 == '\0') {
189 90757f0a 2023-06-15 thomas yyerror("chroot path can't be an empty"
190 90757f0a 2023-06-15 thomas " string");
191 90757f0a 2023-06-15 thomas free($2);
192 90757f0a 2023-06-15 thomas YYERROR;
193 90757f0a 2023-06-15 thomas }
194 90757f0a 2023-06-15 thomas
195 8a35f56c 2022-07-16 thomas n = strlcpy(gotwebd->httpd_chroot, $2,
196 8a35f56c 2022-07-16 thomas sizeof(gotwebd->httpd_chroot));
197 8a35f56c 2022-07-16 thomas if (n >= sizeof(gotwebd->httpd_chroot)) {
198 8a35f56c 2022-07-16 thomas yyerror("%s: httpd_chroot truncated", __func__);
199 8a35f56c 2022-07-16 thomas free($2);
200 8a35f56c 2022-07-16 thomas YYERROR;
201 8a35f56c 2022-07-16 thomas }
202 8a35f56c 2022-07-16 thomas free($2);
203 8a35f56c 2022-07-16 thomas }
204 8a35f56c 2022-07-16 thomas | UNIX_SOCKET boolean {
205 8a35f56c 2022-07-16 thomas gotwebd->unix_socket = $2;
206 8a35f56c 2022-07-16 thomas }
207 8a35f56c 2022-07-16 thomas | UNIX_SOCKET_NAME STRING {
208 8a35f56c 2022-07-16 thomas n = snprintf(gotwebd->unix_socket_name,
209 8a35f56c 2022-07-16 thomas sizeof(gotwebd->unix_socket_name), "%s%s",
210 0b16f49b 2023-06-22 thomas gotwebd->httpd_chroot, $2);
211 717a78d4 2022-08-16 thomas if (n < 0 ||
212 717a78d4 2022-08-16 thomas (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
213 8a35f56c 2022-07-16 thomas yyerror("%s: unix_socket_name truncated",
214 8a35f56c 2022-07-16 thomas __func__);
215 8a35f56c 2022-07-16 thomas free($2);
216 8a35f56c 2022-07-16 thomas YYERROR;
217 8a35f56c 2022-07-16 thomas }
218 8a35f56c 2022-07-16 thomas free($2);
219 8a35f56c 2022-07-16 thomas }
220 8a35f56c 2022-07-16 thomas ;
221 8a35f56c 2022-07-16 thomas
222 8a35f56c 2022-07-16 thomas server : SERVER STRING {
223 8a35f56c 2022-07-16 thomas struct server *srv;
224 8a35f56c 2022-07-16 thomas
225 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
226 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
227 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
228 8a35f56c 2022-07-16 thomas free($2);
229 8a35f56c 2022-07-16 thomas YYERROR;
230 8a35f56c 2022-07-16 thomas }
231 8a35f56c 2022-07-16 thomas }
232 8a35f56c 2022-07-16 thomas
233 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
234 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
235 8a35f56c 2022-07-16 thomas free($2);
236 8a35f56c 2022-07-16 thomas }
237 8a35f56c 2022-07-16 thomas | SERVER STRING {
238 8a35f56c 2022-07-16 thomas struct server *srv;
239 8a35f56c 2022-07-16 thomas
240 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
241 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
242 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
243 8a35f56c 2022-07-16 thomas free($2);
244 8a35f56c 2022-07-16 thomas YYERROR;
245 8a35f56c 2022-07-16 thomas }
246 8a35f56c 2022-07-16 thomas }
247 8a35f56c 2022-07-16 thomas
248 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
249 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
250 8a35f56c 2022-07-16 thomas free($2);
251 8a35f56c 2022-07-16 thomas } '{' optnl serveropts2 '}' {
252 8a35f56c 2022-07-16 thomas }
253 8a35f56c 2022-07-16 thomas ;
254 8a35f56c 2022-07-16 thomas
255 8a35f56c 2022-07-16 thomas serveropts1 : REPOS_PATH STRING {
256 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->repos_path, $2,
257 8a35f56c 2022-07-16 thomas sizeof(new_srv->repos_path));
258 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->repos_path)) {
259 8a35f56c 2022-07-16 thomas yyerror("%s: repos_path truncated", __func__);
260 8a35f56c 2022-07-16 thomas free($2);
261 8a35f56c 2022-07-16 thomas YYERROR;
262 8a35f56c 2022-07-16 thomas }
263 8a35f56c 2022-07-16 thomas free($2);
264 8a35f56c 2022-07-16 thomas }
265 8a35f56c 2022-07-16 thomas | SITE_NAME STRING {
266 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_name, $2,
267 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_name));
268 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_name)) {
269 8a35f56c 2022-07-16 thomas yyerror("%s: site_name truncated", __func__);
270 8a35f56c 2022-07-16 thomas free($2);
271 8a35f56c 2022-07-16 thomas YYERROR;
272 8a35f56c 2022-07-16 thomas }
273 8a35f56c 2022-07-16 thomas free($2);
274 8a35f56c 2022-07-16 thomas }
275 8a35f56c 2022-07-16 thomas | SITE_OWNER STRING {
276 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_owner, $2,
277 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_owner));
278 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_owner)) {
279 8a35f56c 2022-07-16 thomas yyerror("%s: site_owner truncated", __func__);
280 8a35f56c 2022-07-16 thomas free($2);
281 8a35f56c 2022-07-16 thomas YYERROR;
282 8a35f56c 2022-07-16 thomas }
283 8a35f56c 2022-07-16 thomas free($2);
284 8a35f56c 2022-07-16 thomas }
285 8a35f56c 2022-07-16 thomas | SITE_LINK STRING {
286 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_link, $2,
287 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_link));
288 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_link)) {
289 8a35f56c 2022-07-16 thomas yyerror("%s: site_link truncated", __func__);
290 8a35f56c 2022-07-16 thomas free($2);
291 8a35f56c 2022-07-16 thomas YYERROR;
292 8a35f56c 2022-07-16 thomas }
293 8a35f56c 2022-07-16 thomas free($2);
294 8a35f56c 2022-07-16 thomas }
295 8a35f56c 2022-07-16 thomas | LOGO STRING {
296 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
297 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo)) {
298 8a35f56c 2022-07-16 thomas yyerror("%s: logo truncated", __func__);
299 8a35f56c 2022-07-16 thomas free($2);
300 8a35f56c 2022-07-16 thomas YYERROR;
301 8a35f56c 2022-07-16 thomas }
302 8a35f56c 2022-07-16 thomas free($2);
303 8a35f56c 2022-07-16 thomas }
304 8a35f56c 2022-07-16 thomas | LOGO_URL STRING {
305 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo_url, $2,
306 8a35f56c 2022-07-16 thomas sizeof(new_srv->logo_url));
307 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo_url)) {
308 8a35f56c 2022-07-16 thomas yyerror("%s: logo_url truncated", __func__);
309 8a35f56c 2022-07-16 thomas free($2);
310 8a35f56c 2022-07-16 thomas YYERROR;
311 8a35f56c 2022-07-16 thomas }
312 8a35f56c 2022-07-16 thomas free($2);
313 8a35f56c 2022-07-16 thomas }
314 8a35f56c 2022-07-16 thomas | CUSTOM_CSS STRING {
315 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->custom_css, $2,
316 8a35f56c 2022-07-16 thomas sizeof(new_srv->custom_css));
317 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->custom_css)) {
318 8a35f56c 2022-07-16 thomas yyerror("%s: custom_css truncated", __func__);
319 8a35f56c 2022-07-16 thomas free($2);
320 8a35f56c 2022-07-16 thomas YYERROR;
321 8a35f56c 2022-07-16 thomas }
322 8a35f56c 2022-07-16 thomas free($2);
323 8a35f56c 2022-07-16 thomas }
324 e760210d 2023-11-16 thomas | LISTEN ON listen_addr PORT STRING {
325 e760210d 2023-11-16 thomas if (get_addrs($3, $5, new_srv) == -1) {
326 e4c7e0b0 2022-08-30 thomas yyerror("could not get addrs");
327 77fb808d 2022-08-29 thomas YYERROR;
328 77fb808d 2022-08-29 thomas }
329 e760210d 2023-11-16 thomas free($3);
330 e760210d 2023-11-16 thomas free($5);
331 e4317279 2022-08-30 thomas new_srv->fcgi_socket = 1;
332 77fb808d 2022-08-29 thomas }
333 e760210d 2023-11-16 thomas | LISTEN ON listen_addr PORT NUMBER {
334 e760210d 2023-11-16 thomas char portno[32];
335 e760210d 2023-11-16 thomas int n;
336 e760210d 2023-11-16 thomas
337 e760210d 2023-11-16 thomas n = snprintf(portno, sizeof(portno), "%lld",
338 e760210d 2023-11-16 thomas (long long)$5);
339 e760210d 2023-11-16 thomas if (n < 0 || (size_t)n >= sizeof(portno))
340 e760210d 2023-11-16 thomas fatalx("port number too long: %lld",
341 e760210d 2023-11-16 thomas (long long)$5);
342 e760210d 2023-11-16 thomas
343 e760210d 2023-11-16 thomas if (get_addrs($3, portno, new_srv) == -1) {
344 e760210d 2023-11-16 thomas yyerror("could not get addrs");
345 e760210d 2023-11-16 thomas YYERROR;
346 e760210d 2023-11-16 thomas }
347 e760210d 2023-11-16 thomas free($3);
348 e760210d 2023-11-16 thomas new_srv->fcgi_socket = 1;
349 e760210d 2023-11-16 thomas }
350 bea82c4f 2023-01-06 thomas | LISTEN ON SOCKET STRING {
351 ee003af8 2023-06-15 thomas if (strcasecmp($4, "off") == 0) {
352 bea82c4f 2023-01-06 thomas new_srv->unix_socket = 0;
353 bea82c4f 2023-01-06 thomas free($4);
354 bea82c4f 2023-01-06 thomas YYACCEPT;
355 bea82c4f 2023-01-06 thomas }
356 bea82c4f 2023-01-06 thomas
357 bea82c4f 2023-01-06 thomas new_srv->unix_socket = 1;
358 bea82c4f 2023-01-06 thomas
359 bea82c4f 2023-01-06 thomas n = snprintf(new_srv->unix_socket_name,
360 bea82c4f 2023-01-06 thomas sizeof(new_srv->unix_socket_name), "%s%s",
361 0b16f49b 2023-06-22 thomas gotwebd->httpd_chroot, $4);
362 bea82c4f 2023-01-06 thomas if (n < 0 ||
363 bea82c4f 2023-01-06 thomas (size_t)n >= sizeof(new_srv->unix_socket_name)) {
364 bea82c4f 2023-01-06 thomas yyerror("%s: unix_socket_name truncated",
365 bea82c4f 2023-01-06 thomas __func__);
366 bea82c4f 2023-01-06 thomas free($4);
367 bea82c4f 2023-01-06 thomas YYERROR;
368 bea82c4f 2023-01-06 thomas }
369 bea82c4f 2023-01-06 thomas free($4);
370 bea82c4f 2023-01-06 thomas }
371 8a35f56c 2022-07-16 thomas | MAX_REPOS NUMBER {
372 a0bd573d 2023-06-15 thomas if ($2 <= 0) {
373 a0bd573d 2023-06-15 thomas yyerror("max_repos is too small: %lld", $2);
374 a0bd573d 2023-06-15 thomas YYERROR;
375 a0bd573d 2023-06-15 thomas }
376 a0bd573d 2023-06-15 thomas new_srv->max_repos = $2;
377 8a35f56c 2022-07-16 thomas }
378 8a35f56c 2022-07-16 thomas | SHOW_SITE_OWNER boolean {
379 8a35f56c 2022-07-16 thomas new_srv->show_site_owner = $2;
380 8a35f56c 2022-07-16 thomas }
381 8a35f56c 2022-07-16 thomas | SHOW_REPO_OWNER boolean {
382 8a35f56c 2022-07-16 thomas new_srv->show_repo_owner = $2;
383 8a35f56c 2022-07-16 thomas }
384 8a35f56c 2022-07-16 thomas | SHOW_REPO_AGE boolean {
385 8a35f56c 2022-07-16 thomas new_srv->show_repo_age = $2;
386 8a35f56c 2022-07-16 thomas }
387 8a35f56c 2022-07-16 thomas | SHOW_REPO_DESCRIPTION boolean {
388 8a35f56c 2022-07-16 thomas new_srv->show_repo_description = $2;
389 8a35f56c 2022-07-16 thomas }
390 8a35f56c 2022-07-16 thomas | SHOW_REPO_CLONEURL boolean {
391 8a35f56c 2022-07-16 thomas new_srv->show_repo_cloneurl = $2;
392 8a35f56c 2022-07-16 thomas }
393 3991b2a5 2022-10-31 thomas | RESPECT_EXPORTOK boolean {
394 3991b2a5 2022-10-31 thomas new_srv->respect_exportok = $2;
395 3991b2a5 2022-10-31 thomas }
396 8a35f56c 2022-07-16 thomas | MAX_REPOS_DISPLAY NUMBER {
397 04833bad 2023-06-22 thomas if ($2 < 0) {
398 a0bd573d 2023-06-15 thomas yyerror("max_repos_display is too small: %lld",
399 a0bd573d 2023-06-15 thomas $2);
400 a0bd573d 2023-06-15 thomas YYERROR;
401 a0bd573d 2023-06-15 thomas }
402 a0bd573d 2023-06-15 thomas new_srv->max_repos_display = $2;
403 8a35f56c 2022-07-16 thomas }
404 8a35f56c 2022-07-16 thomas | MAX_COMMITS_DISPLAY NUMBER {
405 1fa505c4 2023-06-15 thomas if ($2 <= 1) {
406 1fa505c4 2023-06-15 thomas yyerror("max_commits_display is too small:"
407 1fa505c4 2023-06-15 thomas " %lld", $2);
408 1fa505c4 2023-06-15 thomas YYERROR;
409 1fa505c4 2023-06-15 thomas }
410 1fa505c4 2023-06-15 thomas new_srv->max_commits_display = $2;
411 8a35f56c 2022-07-16 thomas }
412 8a35f56c 2022-07-16 thomas ;
413 8a35f56c 2022-07-16 thomas
414 8a35f56c 2022-07-16 thomas serveropts2 : serveropts2 serveropts1 nl
415 8a35f56c 2022-07-16 thomas | serveropts1 optnl
416 8a35f56c 2022-07-16 thomas ;
417 8a35f56c 2022-07-16 thomas
418 8a35f56c 2022-07-16 thomas nl : '\n' optnl
419 8a35f56c 2022-07-16 thomas ;
420 8a35f56c 2022-07-16 thomas
421 8a35f56c 2022-07-16 thomas optnl : '\n' optnl /* zero or more newlines */
422 8a35f56c 2022-07-16 thomas | /* empty */
423 8a35f56c 2022-07-16 thomas ;
424 8a35f56c 2022-07-16 thomas
425 8a35f56c 2022-07-16 thomas %%
426 8a35f56c 2022-07-16 thomas
427 8a35f56c 2022-07-16 thomas struct keywords {
428 8a35f56c 2022-07-16 thomas const char *k_name;
429 8a35f56c 2022-07-16 thomas int k_val;
430 8a35f56c 2022-07-16 thomas };
431 8a35f56c 2022-07-16 thomas
432 8a35f56c 2022-07-16 thomas int
433 8a35f56c 2022-07-16 thomas yyerror(const char *fmt, ...)
434 8a35f56c 2022-07-16 thomas {
435 8a35f56c 2022-07-16 thomas va_list ap;
436 8a35f56c 2022-07-16 thomas char *msg;
437 8a35f56c 2022-07-16 thomas
438 8a35f56c 2022-07-16 thomas file->errors++;
439 8a35f56c 2022-07-16 thomas va_start(ap, fmt);
440 8a35f56c 2022-07-16 thomas if (vasprintf(&msg, fmt, ap) == -1)
441 8a35f56c 2022-07-16 thomas fatalx("yyerror vasprintf");
442 8a35f56c 2022-07-16 thomas va_end(ap);
443 8a35f56c 2022-07-16 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
444 8a35f56c 2022-07-16 thomas free(msg);
445 8a35f56c 2022-07-16 thomas return (0);
446 8a35f56c 2022-07-16 thomas }
447 8a35f56c 2022-07-16 thomas
448 8a35f56c 2022-07-16 thomas int
449 8a35f56c 2022-07-16 thomas kw_cmp(const void *k, const void *e)
450 8a35f56c 2022-07-16 thomas {
451 8a35f56c 2022-07-16 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
452 8a35f56c 2022-07-16 thomas }
453 8a35f56c 2022-07-16 thomas
454 8a35f56c 2022-07-16 thomas int
455 8a35f56c 2022-07-16 thomas lookup(char *s)
456 8a35f56c 2022-07-16 thomas {
457 8a35f56c 2022-07-16 thomas /* This has to be sorted always. */
458 8a35f56c 2022-07-16 thomas static const struct keywords keywords[] = {
459 8a35f56c 2022-07-16 thomas { "chroot", CHROOT },
460 8a35f56c 2022-07-16 thomas { "custom_css", CUSTOM_CSS },
461 c24a47af 2022-08-11 thomas { "listen", LISTEN },
462 8a35f56c 2022-07-16 thomas { "logo", LOGO },
463 f0b4ad15 2023-01-02 thomas { "logo_url", LOGO_URL },
464 8a35f56c 2022-07-16 thomas { "max_commits_display", MAX_COMMITS_DISPLAY },
465 8a35f56c 2022-07-16 thomas { "max_repos", MAX_REPOS },
466 8a35f56c 2022-07-16 thomas { "max_repos_display", MAX_REPOS_DISPLAY },
467 c24a47af 2022-08-11 thomas { "on", ON },
468 8a35f56c 2022-07-16 thomas { "port", PORT },
469 8a35f56c 2022-07-16 thomas { "prefork", PREFORK },
470 8a35f56c 2022-07-16 thomas { "repos_path", REPOS_PATH },
471 3991b2a5 2022-10-31 thomas { "respect_exportok", RESPECT_EXPORTOK },
472 8a35f56c 2022-07-16 thomas { "server", SERVER },
473 8a35f56c 2022-07-16 thomas { "show_repo_age", SHOW_REPO_AGE },
474 8a35f56c 2022-07-16 thomas { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
475 8a35f56c 2022-07-16 thomas { "show_repo_description", SHOW_REPO_DESCRIPTION },
476 8a35f56c 2022-07-16 thomas { "show_repo_owner", SHOW_REPO_OWNER },
477 8a35f56c 2022-07-16 thomas { "show_site_owner", SHOW_SITE_OWNER },
478 8a35f56c 2022-07-16 thomas { "site_link", SITE_LINK },
479 8a35f56c 2022-07-16 thomas { "site_name", SITE_NAME },
480 8a35f56c 2022-07-16 thomas { "site_owner", SITE_OWNER },
481 bea82c4f 2023-01-06 thomas { "socket", SOCKET },
482 8a35f56c 2022-07-16 thomas { "unix_socket", UNIX_SOCKET },
483 8a35f56c 2022-07-16 thomas { "unix_socket_name", UNIX_SOCKET_NAME },
484 8a35f56c 2022-07-16 thomas };
485 8a35f56c 2022-07-16 thomas const struct keywords *p;
486 8a35f56c 2022-07-16 thomas
487 8a35f56c 2022-07-16 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
488 8a35f56c 2022-07-16 thomas sizeof(keywords[0]), kw_cmp);
489 8a35f56c 2022-07-16 thomas
490 8a35f56c 2022-07-16 thomas if (p)
491 8a35f56c 2022-07-16 thomas return (p->k_val);
492 8a35f56c 2022-07-16 thomas else
493 8a35f56c 2022-07-16 thomas return (STRING);
494 8a35f56c 2022-07-16 thomas }
495 8a35f56c 2022-07-16 thomas
496 8a35f56c 2022-07-16 thomas #define MAXPUSHBACK 128
497 8a35f56c 2022-07-16 thomas
498 8a35f56c 2022-07-16 thomas unsigned char *parsebuf;
499 8a35f56c 2022-07-16 thomas int parseindex;
500 8a35f56c 2022-07-16 thomas unsigned char pushback_buffer[MAXPUSHBACK];
501 8a35f56c 2022-07-16 thomas int pushback_index = 0;
502 8a35f56c 2022-07-16 thomas
503 8a35f56c 2022-07-16 thomas int
504 8a35f56c 2022-07-16 thomas lgetc(int quotec)
505 8a35f56c 2022-07-16 thomas {
506 8a35f56c 2022-07-16 thomas int c, next;
507 8a35f56c 2022-07-16 thomas
508 8a35f56c 2022-07-16 thomas if (parsebuf) {
509 8a35f56c 2022-07-16 thomas /* Read character from the parsebuffer instead of input. */
510 8a35f56c 2022-07-16 thomas if (parseindex >= 0) {
511 8a35f56c 2022-07-16 thomas c = parsebuf[parseindex++];
512 8a35f56c 2022-07-16 thomas if (c != '\0')
513 8a35f56c 2022-07-16 thomas return (c);
514 8a35f56c 2022-07-16 thomas parsebuf = NULL;
515 8a35f56c 2022-07-16 thomas } else
516 8a35f56c 2022-07-16 thomas parseindex++;
517 8a35f56c 2022-07-16 thomas }
518 8a35f56c 2022-07-16 thomas
519 8a35f56c 2022-07-16 thomas if (pushback_index)
520 8a35f56c 2022-07-16 thomas return (pushback_buffer[--pushback_index]);
521 8a35f56c 2022-07-16 thomas
522 8a35f56c 2022-07-16 thomas if (quotec) {
523 8a35f56c 2022-07-16 thomas c = getc(file->stream);
524 8a35f56c 2022-07-16 thomas if (c == EOF)
525 8a35f56c 2022-07-16 thomas yyerror("reached end of file while parsing "
526 8a35f56c 2022-07-16 thomas "quoted string");
527 8a35f56c 2022-07-16 thomas return (c);
528 8a35f56c 2022-07-16 thomas }
529 8a35f56c 2022-07-16 thomas
530 8a35f56c 2022-07-16 thomas c = getc(file->stream);
531 8a35f56c 2022-07-16 thomas while (c == '\\') {
532 8a35f56c 2022-07-16 thomas next = getc(file->stream);
533 8a35f56c 2022-07-16 thomas if (next != '\n') {
534 8a35f56c 2022-07-16 thomas c = next;
535 8a35f56c 2022-07-16 thomas break;
536 8a35f56c 2022-07-16 thomas }
537 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
538 8a35f56c 2022-07-16 thomas file->lineno++;
539 8a35f56c 2022-07-16 thomas c = getc(file->stream);
540 8a35f56c 2022-07-16 thomas }
541 8a35f56c 2022-07-16 thomas
542 8a35f56c 2022-07-16 thomas return (c);
543 8a35f56c 2022-07-16 thomas }
544 8a35f56c 2022-07-16 thomas
545 8a35f56c 2022-07-16 thomas int
546 8a35f56c 2022-07-16 thomas lungetc(int c)
547 8a35f56c 2022-07-16 thomas {
548 8a35f56c 2022-07-16 thomas if (c == EOF)
549 8a35f56c 2022-07-16 thomas return (EOF);
550 8a35f56c 2022-07-16 thomas if (parsebuf) {
551 8a35f56c 2022-07-16 thomas parseindex--;
552 8a35f56c 2022-07-16 thomas if (parseindex >= 0)
553 8a35f56c 2022-07-16 thomas return (c);
554 8a35f56c 2022-07-16 thomas }
555 8a35f56c 2022-07-16 thomas if (pushback_index < MAXPUSHBACK-1)
556 8a35f56c 2022-07-16 thomas return (pushback_buffer[pushback_index++] = c);
557 8a35f56c 2022-07-16 thomas else
558 8a35f56c 2022-07-16 thomas return (EOF);
559 8a35f56c 2022-07-16 thomas }
560 8a35f56c 2022-07-16 thomas
561 8a35f56c 2022-07-16 thomas int
562 8a35f56c 2022-07-16 thomas findeol(void)
563 8a35f56c 2022-07-16 thomas {
564 8a35f56c 2022-07-16 thomas int c;
565 8a35f56c 2022-07-16 thomas
566 8a35f56c 2022-07-16 thomas parsebuf = NULL;
567 8a35f56c 2022-07-16 thomas
568 8a35f56c 2022-07-16 thomas /* Skip to either EOF or the first real EOL. */
569 8a35f56c 2022-07-16 thomas while (1) {
570 8a35f56c 2022-07-16 thomas if (pushback_index)
571 8a35f56c 2022-07-16 thomas c = pushback_buffer[--pushback_index];
572 8a35f56c 2022-07-16 thomas else
573 8a35f56c 2022-07-16 thomas c = lgetc(0);
574 8a35f56c 2022-07-16 thomas if (c == '\n') {
575 8a35f56c 2022-07-16 thomas file->lineno++;
576 8a35f56c 2022-07-16 thomas break;
577 8a35f56c 2022-07-16 thomas }
578 8a35f56c 2022-07-16 thomas if (c == EOF)
579 8a35f56c 2022-07-16 thomas break;
580 8a35f56c 2022-07-16 thomas }
581 8a35f56c 2022-07-16 thomas return (ERROR);
582 8a35f56c 2022-07-16 thomas }
583 8a35f56c 2022-07-16 thomas
584 8a35f56c 2022-07-16 thomas int
585 8a35f56c 2022-07-16 thomas yylex(void)
586 8a35f56c 2022-07-16 thomas {
587 8a35f56c 2022-07-16 thomas unsigned char buf[8096];
588 8a35f56c 2022-07-16 thomas unsigned char *p, *val;
589 8a35f56c 2022-07-16 thomas int quotec, next, c;
590 8a35f56c 2022-07-16 thomas int token;
591 8a35f56c 2022-07-16 thomas
592 8a35f56c 2022-07-16 thomas top:
593 8a35f56c 2022-07-16 thomas p = buf;
594 8a35f56c 2022-07-16 thomas c = lgetc(0);
595 8a35f56c 2022-07-16 thomas while (c == ' ' || c == '\t')
596 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
597 8a35f56c 2022-07-16 thomas
598 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
599 8a35f56c 2022-07-16 thomas if (c == '#') {
600 8a35f56c 2022-07-16 thomas c = lgetc(0);
601 8a35f56c 2022-07-16 thomas while (c != '\n' && c != EOF)
602 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
603 8a35f56c 2022-07-16 thomas }
604 8a35f56c 2022-07-16 thomas if (c == '$' && parsebuf == NULL) {
605 8a35f56c 2022-07-16 thomas while (1) {
606 8a35f56c 2022-07-16 thomas c = lgetc(0);
607 8a35f56c 2022-07-16 thomas if (c == EOF)
608 8a35f56c 2022-07-16 thomas return (0);
609 8a35f56c 2022-07-16 thomas
610 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
611 8a35f56c 2022-07-16 thomas yyerror("string too long");
612 8a35f56c 2022-07-16 thomas return (findeol());
613 8a35f56c 2022-07-16 thomas }
614 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == '_') {
615 8a35f56c 2022-07-16 thomas *p++ = c;
616 8a35f56c 2022-07-16 thomas continue;
617 8a35f56c 2022-07-16 thomas }
618 8a35f56c 2022-07-16 thomas *p = '\0';
619 8a35f56c 2022-07-16 thomas lungetc(c);
620 8a35f56c 2022-07-16 thomas break;
621 8a35f56c 2022-07-16 thomas }
622 8a35f56c 2022-07-16 thomas val = symget(buf);
623 8a35f56c 2022-07-16 thomas if (val == NULL) {
624 8a35f56c 2022-07-16 thomas yyerror("macro '%s' not defined", buf);
625 8a35f56c 2022-07-16 thomas return (findeol());
626 8a35f56c 2022-07-16 thomas }
627 8a35f56c 2022-07-16 thomas parsebuf = val;
628 8a35f56c 2022-07-16 thomas parseindex = 0;
629 8a35f56c 2022-07-16 thomas goto top;
630 8a35f56c 2022-07-16 thomas }
631 8a35f56c 2022-07-16 thomas
632 8a35f56c 2022-07-16 thomas switch (c) {
633 8a35f56c 2022-07-16 thomas case '\'':
634 8a35f56c 2022-07-16 thomas case '"':
635 8a35f56c 2022-07-16 thomas quotec = c;
636 8a35f56c 2022-07-16 thomas while (1) {
637 8a35f56c 2022-07-16 thomas c = lgetc(quotec);
638 8a35f56c 2022-07-16 thomas if (c == EOF)
639 8a35f56c 2022-07-16 thomas return (0);
640 8a35f56c 2022-07-16 thomas if (c == '\n') {
641 8a35f56c 2022-07-16 thomas file->lineno++;
642 8a35f56c 2022-07-16 thomas continue;
643 8a35f56c 2022-07-16 thomas } else if (c == '\\') {
644 8a35f56c 2022-07-16 thomas next = lgetc(quotec);
645 8a35f56c 2022-07-16 thomas if (next == EOF)
646 8a35f56c 2022-07-16 thomas return (0);
647 8a35f56c 2022-07-16 thomas if (next == quotec || c == ' ' || c == '\t')
648 8a35f56c 2022-07-16 thomas c = next;
649 8a35f56c 2022-07-16 thomas else if (next == '\n') {
650 8a35f56c 2022-07-16 thomas file->lineno++;
651 8a35f56c 2022-07-16 thomas continue;
652 8a35f56c 2022-07-16 thomas } else
653 8a35f56c 2022-07-16 thomas lungetc(next);
654 8a35f56c 2022-07-16 thomas } else if (c == quotec) {
655 8a35f56c 2022-07-16 thomas *p = '\0';
656 8a35f56c 2022-07-16 thomas break;
657 8a35f56c 2022-07-16 thomas } else if (c == '\0') {
658 8a35f56c 2022-07-16 thomas yyerror("syntax error");
659 8a35f56c 2022-07-16 thomas return (findeol());
660 8a35f56c 2022-07-16 thomas }
661 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
662 8a35f56c 2022-07-16 thomas yyerror("string too long");
663 8a35f56c 2022-07-16 thomas return (findeol());
664 8a35f56c 2022-07-16 thomas }
665 8a35f56c 2022-07-16 thomas *p++ = c;
666 8a35f56c 2022-07-16 thomas }
667 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
668 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
669 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
670 8a35f56c 2022-07-16 thomas return (STRING);
671 8a35f56c 2022-07-16 thomas }
672 8a35f56c 2022-07-16 thomas
673 8a35f56c 2022-07-16 thomas #define allowed_to_end_number(x) \
674 8a35f56c 2022-07-16 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
675 8a35f56c 2022-07-16 thomas
676 8a35f56c 2022-07-16 thomas if (c == '-' || isdigit(c)) {
677 8a35f56c 2022-07-16 thomas do {
678 8a35f56c 2022-07-16 thomas *p++ = c;
679 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
680 8a35f56c 2022-07-16 thomas yyerror("string too long");
681 8a35f56c 2022-07-16 thomas return (findeol());
682 8a35f56c 2022-07-16 thomas }
683 8a35f56c 2022-07-16 thomas c = lgetc(0);
684 8a35f56c 2022-07-16 thomas } while (c != EOF && isdigit(c));
685 8a35f56c 2022-07-16 thomas lungetc(c);
686 8a35f56c 2022-07-16 thomas if (p == buf + 1 && buf[0] == '-')
687 8a35f56c 2022-07-16 thomas goto nodigits;
688 8a35f56c 2022-07-16 thomas if (c == EOF || allowed_to_end_number(c)) {
689 8a35f56c 2022-07-16 thomas const char *errstr = NULL;
690 8a35f56c 2022-07-16 thomas
691 8a35f56c 2022-07-16 thomas *p = '\0';
692 8a35f56c 2022-07-16 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
693 8a35f56c 2022-07-16 thomas LLONG_MAX, &errstr);
694 8a35f56c 2022-07-16 thomas if (errstr) {
695 8a35f56c 2022-07-16 thomas yyerror("\"%s\" invalid number: %s",
696 8a35f56c 2022-07-16 thomas buf, errstr);
697 8a35f56c 2022-07-16 thomas return (findeol());
698 8a35f56c 2022-07-16 thomas }
699 8a35f56c 2022-07-16 thomas return (NUMBER);
700 8a35f56c 2022-07-16 thomas } else {
701 8a35f56c 2022-07-16 thomas nodigits:
702 8a35f56c 2022-07-16 thomas while (p > buf + 1)
703 8a35f56c 2022-07-16 thomas lungetc(*--p);
704 8a35f56c 2022-07-16 thomas c = *--p;
705 8a35f56c 2022-07-16 thomas if (c == '-')
706 8a35f56c 2022-07-16 thomas return (c);
707 8a35f56c 2022-07-16 thomas }
708 8a35f56c 2022-07-16 thomas }
709 8a35f56c 2022-07-16 thomas
710 8a35f56c 2022-07-16 thomas #define allowed_in_string(x) \
711 8a35f56c 2022-07-16 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
712 8a35f56c 2022-07-16 thomas x != '{' && x != '}' && \
713 8a35f56c 2022-07-16 thomas x != '!' && x != '=' && x != '#' && \
714 8a35f56c 2022-07-16 thomas x != ','))
715 8a35f56c 2022-07-16 thomas
716 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == ':' || c == '_') {
717 8a35f56c 2022-07-16 thomas do {
718 8a35f56c 2022-07-16 thomas *p++ = c;
719 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
720 8a35f56c 2022-07-16 thomas yyerror("string too long");
721 8a35f56c 2022-07-16 thomas return (findeol());
722 8a35f56c 2022-07-16 thomas }
723 8a35f56c 2022-07-16 thomas c = lgetc(0);
724 8a35f56c 2022-07-16 thomas } while (c != EOF && (allowed_in_string(c)));
725 8a35f56c 2022-07-16 thomas lungetc(c);
726 8a35f56c 2022-07-16 thomas *p = '\0';
727 8a35f56c 2022-07-16 thomas token = lookup(buf);
728 8a35f56c 2022-07-16 thomas if (token == STRING) {
729 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
730 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
731 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
732 8a35f56c 2022-07-16 thomas }
733 8a35f56c 2022-07-16 thomas return (token);
734 8a35f56c 2022-07-16 thomas }
735 8a35f56c 2022-07-16 thomas if (c == '\n') {
736 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
737 8a35f56c 2022-07-16 thomas file->lineno++;
738 8a35f56c 2022-07-16 thomas }
739 8a35f56c 2022-07-16 thomas if (c == EOF)
740 8a35f56c 2022-07-16 thomas return (0);
741 8a35f56c 2022-07-16 thomas return (c);
742 8a35f56c 2022-07-16 thomas }
743 8a35f56c 2022-07-16 thomas
744 8a35f56c 2022-07-16 thomas int
745 8a35f56c 2022-07-16 thomas check_file_secrecy(int fd, const char *fname)
746 8a35f56c 2022-07-16 thomas {
747 8a35f56c 2022-07-16 thomas struct stat st;
748 8a35f56c 2022-07-16 thomas
749 8a35f56c 2022-07-16 thomas if (fstat(fd, &st)) {
750 8a35f56c 2022-07-16 thomas log_warn("cannot stat %s", fname);
751 8a35f56c 2022-07-16 thomas return (-1);
752 8a35f56c 2022-07-16 thomas }
753 8a35f56c 2022-07-16 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
754 8a35f56c 2022-07-16 thomas log_warnx("%s: owner not root or current user", fname);
755 8a35f56c 2022-07-16 thomas return (-1);
756 8a35f56c 2022-07-16 thomas }
757 8a35f56c 2022-07-16 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
758 8a35f56c 2022-07-16 thomas log_warnx("%s: group writable or world read/writable", fname);
759 8a35f56c 2022-07-16 thomas return (-1);
760 8a35f56c 2022-07-16 thomas }
761 8a35f56c 2022-07-16 thomas return (0);
762 8a35f56c 2022-07-16 thomas }
763 8a35f56c 2022-07-16 thomas
764 8a35f56c 2022-07-16 thomas struct file *
765 8a35f56c 2022-07-16 thomas newfile(const char *name, int secret)
766 8a35f56c 2022-07-16 thomas {
767 8a35f56c 2022-07-16 thomas struct file *nfile;
768 8a35f56c 2022-07-16 thomas
769 8a35f56c 2022-07-16 thomas nfile = calloc(1, sizeof(struct file));
770 8a35f56c 2022-07-16 thomas if (nfile == NULL) {
771 8a35f56c 2022-07-16 thomas log_warn("calloc");
772 8a35f56c 2022-07-16 thomas return (NULL);
773 8a35f56c 2022-07-16 thomas }
774 8a35f56c 2022-07-16 thomas nfile->name = strdup(name);
775 8a35f56c 2022-07-16 thomas if (nfile->name == NULL) {
776 8a35f56c 2022-07-16 thomas log_warn("strdup");
777 8a35f56c 2022-07-16 thomas free(nfile);
778 8a35f56c 2022-07-16 thomas return (NULL);
779 8a35f56c 2022-07-16 thomas }
780 8a35f56c 2022-07-16 thomas nfile->stream = fopen(nfile->name, "r");
781 8a35f56c 2022-07-16 thomas if (nfile->stream == NULL) {
782 8a35f56c 2022-07-16 thomas /* no warning, we don't require a conf file */
783 8a35f56c 2022-07-16 thomas free(nfile->name);
784 8a35f56c 2022-07-16 thomas free(nfile);
785 8a35f56c 2022-07-16 thomas return (NULL);
786 8a35f56c 2022-07-16 thomas } else if (secret &&
787 8a35f56c 2022-07-16 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
788 8a35f56c 2022-07-16 thomas fclose(nfile->stream);
789 8a35f56c 2022-07-16 thomas free(nfile->name);
790 8a35f56c 2022-07-16 thomas free(nfile);
791 8a35f56c 2022-07-16 thomas return (NULL);
792 8a35f56c 2022-07-16 thomas }
793 8a35f56c 2022-07-16 thomas nfile->lineno = 1;
794 8a35f56c 2022-07-16 thomas return (nfile);
795 8a35f56c 2022-07-16 thomas }
796 8a35f56c 2022-07-16 thomas
797 8a35f56c 2022-07-16 thomas static void
798 8a35f56c 2022-07-16 thomas closefile(struct file *xfile)
799 8a35f56c 2022-07-16 thomas {
800 8a35f56c 2022-07-16 thomas fclose(xfile->stream);
801 8a35f56c 2022-07-16 thomas free(xfile->name);
802 8a35f56c 2022-07-16 thomas free(xfile);
803 8a35f56c 2022-07-16 thomas }
804 8a35f56c 2022-07-16 thomas
805 9f849004 2022-08-06 thomas static void
806 9f849004 2022-08-06 thomas add_default_server(void)
807 9f849004 2022-08-06 thomas {
808 9f849004 2022-08-06 thomas new_srv = conf_new_server(D_SITENAME);
809 9f849004 2022-08-06 thomas log_debug("%s: adding default server %s", __func__, D_SITENAME);
810 9f849004 2022-08-06 thomas }
811 9f849004 2022-08-06 thomas
812 8a35f56c 2022-07-16 thomas int
813 8a35f56c 2022-07-16 thomas parse_config(const char *filename, struct gotwebd *env)
814 8a35f56c 2022-07-16 thomas {
815 8a35f56c 2022-07-16 thomas struct sym *sym, *next;
816 8a35f56c 2022-07-16 thomas
817 8a35f56c 2022-07-16 thomas if (config_init(env) == -1)
818 8a35f56c 2022-07-16 thomas fatalx("failed to initialize configuration");
819 8a35f56c 2022-07-16 thomas
820 8a35f56c 2022-07-16 thomas gotwebd = env;
821 9f849004 2022-08-06 thomas
822 9f849004 2022-08-06 thomas file = newfile(filename, 0);
823 9f849004 2022-08-06 thomas if (file == NULL) {
824 9f849004 2022-08-06 thomas add_default_server();
825 9f849004 2022-08-06 thomas sockets_parse_sockets(env);
826 9f849004 2022-08-06 thomas /* just return, as we don't require a conf file */
827 9f849004 2022-08-06 thomas return (0);
828 9f849004 2022-08-06 thomas }
829 8a35f56c 2022-07-16 thomas
830 8a35f56c 2022-07-16 thomas yyparse();
831 8a35f56c 2022-07-16 thomas errors = file->errors;
832 8a35f56c 2022-07-16 thomas closefile(file);
833 8a35f56c 2022-07-16 thomas
834 8a35f56c 2022-07-16 thomas /* Free macros and check which have not been used. */
835 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
836 8a35f56c 2022-07-16 thomas if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
837 8a35f56c 2022-07-16 thomas fprintf(stderr, "warning: macro '%s' not used\n",
838 8a35f56c 2022-07-16 thomas sym->nam);
839 8a35f56c 2022-07-16 thomas if (!sym->persist) {
840 8a35f56c 2022-07-16 thomas free(sym->nam);
841 8a35f56c 2022-07-16 thomas free(sym->val);
842 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
843 8a35f56c 2022-07-16 thomas free(sym);
844 8a35f56c 2022-07-16 thomas }
845 8a35f56c 2022-07-16 thomas }
846 8a35f56c 2022-07-16 thomas
847 8a35f56c 2022-07-16 thomas if (errors)
848 8a35f56c 2022-07-16 thomas return (-1);
849 8a35f56c 2022-07-16 thomas
850 8a35f56c 2022-07-16 thomas /* just add default server if no config specified */
851 9f849004 2022-08-06 thomas if (gotwebd->server_cnt == 0)
852 9f849004 2022-08-06 thomas add_default_server();
853 8a35f56c 2022-07-16 thomas
854 8a35f56c 2022-07-16 thomas /* setup our listening sockets */
855 8a35f56c 2022-07-16 thomas sockets_parse_sockets(env);
856 8a35f56c 2022-07-16 thomas
857 8a35f56c 2022-07-16 thomas return (0);
858 8a35f56c 2022-07-16 thomas }
859 8a35f56c 2022-07-16 thomas
860 8a35f56c 2022-07-16 thomas struct server *
861 8a35f56c 2022-07-16 thomas conf_new_server(const char *name)
862 8a35f56c 2022-07-16 thomas {
863 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
864 8a35f56c 2022-07-16 thomas
865 8a35f56c 2022-07-16 thomas srv = calloc(1, sizeof(*srv));
866 8a35f56c 2022-07-16 thomas if (srv == NULL)
867 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
868 8a35f56c 2022-07-16 thomas
869 8a35f56c 2022-07-16 thomas n = strlcpy(srv->name, name, sizeof(srv->name));
870 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->name))
871 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
872 8a35f56c 2022-07-16 thomas n = snprintf(srv->unix_socket_name,
873 8a35f56c 2022-07-16 thomas sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
874 8a35f56c 2022-07-16 thomas D_UNIX_SOCKET);
875 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
876 8a35f56c 2022-07-16 thomas fatalx("%s: snprintf", __func__);
877 8a35f56c 2022-07-16 thomas n = strlcpy(srv->repos_path, D_GOTPATH,
878 8a35f56c 2022-07-16 thomas sizeof(srv->repos_path));
879 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->repos_path))
880 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
881 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_name, D_SITENAME,
882 8a35f56c 2022-07-16 thomas sizeof(srv->site_name));
883 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_name))
884 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
885 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_owner, D_SITEOWNER,
886 8a35f56c 2022-07-16 thomas sizeof(srv->site_owner));
887 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_owner))
888 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
889 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_link, D_SITELINK,
890 8a35f56c 2022-07-16 thomas sizeof(srv->site_link));
891 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_link))
892 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
893 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo, D_GOTLOGO,
894 8a35f56c 2022-07-16 thomas sizeof(srv->logo));
895 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo))
896 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
897 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
898 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo_url))
899 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
900 8a35f56c 2022-07-16 thomas n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
901 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->custom_css))
902 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
903 8a35f56c 2022-07-16 thomas
904 8a35f56c 2022-07-16 thomas srv->show_site_owner = D_SHOWSOWNER;
905 8a35f56c 2022-07-16 thomas srv->show_repo_owner = D_SHOWROWNER;
906 8a35f56c 2022-07-16 thomas srv->show_repo_age = D_SHOWAGE;
907 8a35f56c 2022-07-16 thomas srv->show_repo_description = D_SHOWDESC;
908 8a35f56c 2022-07-16 thomas srv->show_repo_cloneurl = D_SHOWURL;
909 3991b2a5 2022-10-31 thomas srv->respect_exportok = D_RESPECTEXPORTOK;
910 8a35f56c 2022-07-16 thomas
911 8a35f56c 2022-07-16 thomas srv->max_repos_display = D_MAXREPODISP;
912 8a35f56c 2022-07-16 thomas srv->max_commits_display = D_MAXCOMMITDISP;
913 8a35f56c 2022-07-16 thomas srv->max_repos = D_MAXREPO;
914 8a35f56c 2022-07-16 thomas
915 8a35f56c 2022-07-16 thomas srv->unix_socket = 1;
916 e4317279 2022-08-30 thomas srv->fcgi_socket = 0;
917 8a35f56c 2022-07-16 thomas
918 62f85214 2022-08-16 thomas TAILQ_INIT(&srv->al);
919 90d63d47 2022-08-16 thomas TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
920 8a35f56c 2022-07-16 thomas gotwebd->server_cnt++;
921 8a35f56c 2022-07-16 thomas
922 8a35f56c 2022-07-16 thomas return srv;
923 8a35f56c 2022-07-16 thomas };
924 8a35f56c 2022-07-16 thomas
925 8a35f56c 2022-07-16 thomas int
926 8a35f56c 2022-07-16 thomas symset(const char *nam, const char *val, int persist)
927 8a35f56c 2022-07-16 thomas {
928 8a35f56c 2022-07-16 thomas struct sym *sym;
929 8a35f56c 2022-07-16 thomas
930 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
931 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0)
932 8a35f56c 2022-07-16 thomas break;
933 8a35f56c 2022-07-16 thomas }
934 8a35f56c 2022-07-16 thomas
935 8a35f56c 2022-07-16 thomas if (sym != NULL) {
936 8a35f56c 2022-07-16 thomas if (sym->persist == 1)
937 8a35f56c 2022-07-16 thomas return (0);
938 8a35f56c 2022-07-16 thomas else {
939 8a35f56c 2022-07-16 thomas free(sym->nam);
940 8a35f56c 2022-07-16 thomas free(sym->val);
941 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
942 8a35f56c 2022-07-16 thomas free(sym);
943 8a35f56c 2022-07-16 thomas }
944 8a35f56c 2022-07-16 thomas }
945 8a35f56c 2022-07-16 thomas sym = calloc(1, sizeof(*sym));
946 8a35f56c 2022-07-16 thomas if (sym == NULL)
947 8a35f56c 2022-07-16 thomas return (-1);
948 8a35f56c 2022-07-16 thomas
949 8a35f56c 2022-07-16 thomas sym->nam = strdup(nam);
950 8a35f56c 2022-07-16 thomas if (sym->nam == NULL) {
951 8a35f56c 2022-07-16 thomas free(sym);
952 8a35f56c 2022-07-16 thomas return (-1);
953 8a35f56c 2022-07-16 thomas }
954 8a35f56c 2022-07-16 thomas sym->val = strdup(val);
955 8a35f56c 2022-07-16 thomas if (sym->val == NULL) {
956 8a35f56c 2022-07-16 thomas free(sym->nam);
957 8a35f56c 2022-07-16 thomas free(sym);
958 8a35f56c 2022-07-16 thomas return (-1);
959 8a35f56c 2022-07-16 thomas }
960 8a35f56c 2022-07-16 thomas sym->used = 0;
961 8a35f56c 2022-07-16 thomas sym->persist = persist;
962 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
963 8a35f56c 2022-07-16 thomas return (0);
964 8a35f56c 2022-07-16 thomas }
965 8a35f56c 2022-07-16 thomas
966 8a35f56c 2022-07-16 thomas int
967 8a35f56c 2022-07-16 thomas cmdline_symset(char *s)
968 8a35f56c 2022-07-16 thomas {
969 8a35f56c 2022-07-16 thomas char *sym, *val;
970 8a35f56c 2022-07-16 thomas int ret;
971 8a35f56c 2022-07-16 thomas
972 8a35f56c 2022-07-16 thomas val = strrchr(s, '=');
973 8a35f56c 2022-07-16 thomas if (val == NULL)
974 8a35f56c 2022-07-16 thomas return (-1);
975 8a35f56c 2022-07-16 thomas
976 43be1edb 2022-09-05 thomas sym = strndup(s, val - s);
977 8a35f56c 2022-07-16 thomas if (sym == NULL)
978 43be1edb 2022-09-05 thomas fatal("%s: strndup", __func__);
979 8a35f56c 2022-07-16 thomas
980 8a35f56c 2022-07-16 thomas ret = symset(sym, val + 1, 1);
981 8a35f56c 2022-07-16 thomas free(sym);
982 8a35f56c 2022-07-16 thomas
983 8a35f56c 2022-07-16 thomas return (ret);
984 8a35f56c 2022-07-16 thomas }
985 8a35f56c 2022-07-16 thomas
986 8a35f56c 2022-07-16 thomas char *
987 8a35f56c 2022-07-16 thomas symget(const char *nam)
988 8a35f56c 2022-07-16 thomas {
989 8a35f56c 2022-07-16 thomas struct sym *sym;
990 8a35f56c 2022-07-16 thomas
991 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
992 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0) {
993 8a35f56c 2022-07-16 thomas sym->used = 1;
994 8a35f56c 2022-07-16 thomas return (sym->val);
995 8a35f56c 2022-07-16 thomas }
996 8a35f56c 2022-07-16 thomas }
997 8a35f56c 2022-07-16 thomas return (NULL);
998 8a35f56c 2022-07-16 thomas }
999 8a35f56c 2022-07-16 thomas
1000 8a35f56c 2022-07-16 thomas int
1001 e760210d 2023-11-16 thomas get_addrs(const char *hostname, const char *servname, struct server *new_srv)
1002 8a35f56c 2022-07-16 thomas {
1003 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res0, *res;
1004 e760210d 2023-11-16 thomas int error;
1005 2edd250d 2023-11-16 thomas struct sockaddr_in *sin;
1006 2edd250d 2023-11-16 thomas struct sockaddr_in6 *sin6;
1007 8a35f56c 2022-07-16 thomas struct address *h;
1008 8a35f56c 2022-07-16 thomas
1009 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1010 fb0cb708 2023-06-01 thomas hints.ai_family = AF_UNSPEC;
1011 e760210d 2023-11-16 thomas hints.ai_socktype = SOCK_STREAM;
1012 61bbe977 2023-11-16 thomas hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
1013 e760210d 2023-11-16 thomas error = getaddrinfo(hostname, servname, &hints, &res0);
1014 8a35f56c 2022-07-16 thomas if (error) {
1015 e760210d 2023-11-16 thomas log_warnx("%s: could not parse \"%s:%s\": %s", __func__,
1016 e760210d 2023-11-16 thomas hostname, servname, gai_strerror(error));
1017 8a35f56c 2022-07-16 thomas return (-1);
1018 8a35f56c 2022-07-16 thomas }
1019 8a35f56c 2022-07-16 thomas
1020 61bbe977 2023-11-16 thomas for (res = res0; res; res = res->ai_next) {
1021 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1022 8a35f56c 2022-07-16 thomas fatal(__func__);
1023 8a35f56c 2022-07-16 thomas
1024 e760210d 2023-11-16 thomas if (hostname == NULL) {
1025 61bbe977 2023-11-16 thomas strlcpy(h->ifname, "*", sizeof(h->ifname));
1026 61bbe977 2023-11-16 thomas } else {
1027 e760210d 2023-11-16 thomas if (strlcpy(h->ifname, hostname, sizeof(h->ifname)) >=
1028 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1029 ac485591 2023-11-16 thomas log_warnx("%s: address truncated: %s",
1030 ac485591 2023-11-16 thomas __func__, hostname);
1031 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1032 8a35f56c 2022-07-16 thomas free(h);
1033 8a35f56c 2022-07-16 thomas return (-1);
1034 8a35f56c 2022-07-16 thomas }
1035 8a35f56c 2022-07-16 thomas }
1036 2edd250d 2023-11-16 thomas
1037 7d1d4b6f 2023-11-16 thomas h->ai_family = res->ai_family;
1038 7d1d4b6f 2023-11-16 thomas h->ai_socktype = res->ai_socktype;
1039 7d1d4b6f 2023-11-16 thomas h->ai_protocol = res->ai_protocol;
1040 2edd250d 2023-11-16 thomas memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
1041 2edd250d 2023-11-16 thomas h->slen = res->ai_addrlen;
1042 8a35f56c 2022-07-16 thomas
1043 e760210d 2023-11-16 thomas switch (res->ai_family) {
1044 e760210d 2023-11-16 thomas case AF_INET:
1045 2edd250d 2023-11-16 thomas sin = (struct sockaddr_in *)res->ai_addr;
1046 2edd250d 2023-11-16 thomas h->port = ntohs(sin->sin_port);
1047 e760210d 2023-11-16 thomas break;
1048 e760210d 2023-11-16 thomas case AF_INET6:
1049 2edd250d 2023-11-16 thomas sin6 = (struct sockaddr_in6 *)res->ai_addr;
1050 2edd250d 2023-11-16 thomas h->port = ntohs(sin6->sin6_port);
1051 e760210d 2023-11-16 thomas break;
1052 e760210d 2023-11-16 thomas default:
1053 e760210d 2023-11-16 thomas fatalx("unknown address family %d", res->ai_family);
1054 8a35f56c 2022-07-16 thomas }
1055 8a35f56c 2022-07-16 thomas
1056 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1057 e4c7e0b0 2022-08-30 thomas return -1;
1058 8a35f56c 2022-07-16 thomas }
1059 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1060 8a35f56c 2022-07-16 thomas return (0);
1061 e4c7e0b0 2022-08-30 thomas }
1062 e4c7e0b0 2022-08-30 thomas
1063 e4c7e0b0 2022-08-30 thomas int
1064 e4c7e0b0 2022-08-30 thomas addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1065 e4c7e0b0 2022-08-30 thomas const char *other_srv)
1066 e4c7e0b0 2022-08-30 thomas {
1067 e4c7e0b0 2022-08-30 thomas struct address *a;
1068 e4c7e0b0 2022-08-30 thomas void *ia;
1069 e4c7e0b0 2022-08-30 thomas char buf[INET6_ADDRSTRLEN];
1070 e4c7e0b0 2022-08-30 thomas const char *addrstr;
1071 e4c7e0b0 2022-08-30 thomas
1072 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(a, al, entry) {
1073 7d1d4b6f 2023-11-16 thomas if (a->ai_family != h->ai_family ||
1074 7d1d4b6f 2023-11-16 thomas a->ai_socktype != h->ai_socktype ||
1075 7d1d4b6f 2023-11-16 thomas a->ai_protocol != h->ai_protocol ||
1076 7d1d4b6f 2023-11-16 thomas a->slen != h->slen ||
1077 2edd250d 2023-11-16 thomas memcmp(&a->ss, &h->ss, a->slen) != 0)
1078 e4c7e0b0 2022-08-30 thomas continue;
1079 e4c7e0b0 2022-08-30 thomas
1080 e4c7e0b0 2022-08-30 thomas switch (h->ss.ss_family) {
1081 e4c7e0b0 2022-08-30 thomas case AF_INET:
1082 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1083 e4c7e0b0 2022-08-30 thomas break;
1084 e4c7e0b0 2022-08-30 thomas case AF_INET6:
1085 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1086 e4c7e0b0 2022-08-30 thomas break;
1087 e4c7e0b0 2022-08-30 thomas default:
1088 e4c7e0b0 2022-08-30 thomas yyerror("unknown address family: %d", h->ss.ss_family);
1089 e4c7e0b0 2022-08-30 thomas return -1;
1090 e4c7e0b0 2022-08-30 thomas }
1091 e4c7e0b0 2022-08-30 thomas addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1092 e4c7e0b0 2022-08-30 thomas if (addrstr) {
1093 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1094 e4c7e0b0 2022-08-30 thomas yyerror("server %s: duplicate fcgi listen "
1095 e4c7e0b0 2022-08-30 thomas "address %s:%d, already used by server %s",
1096 e4c7e0b0 2022-08-30 thomas new_srv, addrstr, h->port, other_srv);
1097 e4c7e0b0 2022-08-30 thomas } else {
1098 e4c7e0b0 2022-08-30 thomas log_warnx("server: %s: duplicate fcgi listen "
1099 e4c7e0b0 2022-08-30 thomas "address %s:%d", new_srv, addrstr, h->port);
1100 e4c7e0b0 2022-08-30 thomas }
1101 e4c7e0b0 2022-08-30 thomas } else {
1102 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1103 e4c7e0b0 2022-08-30 thomas yyerror("server: %s: duplicate fcgi listen "
1104 e4c7e0b0 2022-08-30 thomas "address, already used by server %s",
1105 e4c7e0b0 2022-08-30 thomas new_srv, other_srv);
1106 e4c7e0b0 2022-08-30 thomas } else {
1107 e4c7e0b0 2022-08-30 thomas log_warnx("server %s: duplicate fcgi listen "
1108 e4c7e0b0 2022-08-30 thomas "address", new_srv);
1109 e4c7e0b0 2022-08-30 thomas }
1110 e4c7e0b0 2022-08-30 thomas }
1111 e4c7e0b0 2022-08-30 thomas
1112 e4c7e0b0 2022-08-30 thomas return -1;
1113 e4c7e0b0 2022-08-30 thomas }
1114 e4c7e0b0 2022-08-30 thomas
1115 e4c7e0b0 2022-08-30 thomas return 0;
1116 8a35f56c 2022-07-16 thomas }
1117 e4c7e0b0 2022-08-30 thomas
1118 e4c7e0b0 2022-08-30 thomas int
1119 e4c7e0b0 2022-08-30 thomas add_addr(struct server *new_srv, struct address *h)
1120 e4c7e0b0 2022-08-30 thomas {
1121 e4c7e0b0 2022-08-30 thomas struct server *srv;
1122 e4c7e0b0 2022-08-30 thomas
1123 e4c7e0b0 2022-08-30 thomas /* Address cannot be shared between different servers. */
1124 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1125 e4c7e0b0 2022-08-30 thomas if (srv == new_srv)
1126 e4c7e0b0 2022-08-30 thomas continue;
1127 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1128 e4c7e0b0 2022-08-30 thomas return -1;
1129 e4c7e0b0 2022-08-30 thomas }
1130 e4c7e0b0 2022-08-30 thomas
1131 e4c7e0b0 2022-08-30 thomas /* Tolerate duplicate address lines within the scope of a server. */
1132 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1133 e4c7e0b0 2022-08-30 thomas TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1134 e4c7e0b0 2022-08-30 thomas else
1135 e4c7e0b0 2022-08-30 thomas free(h);
1136 e4c7e0b0 2022-08-30 thomas
1137 e4c7e0b0 2022-08-30 thomas return 0;
1138 e4c7e0b0 2022-08-30 thomas }