Blame


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