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_sockaddr.h"
51 df2d3cd2 2023-03-11 op #include "got_reference.h"
52 a596b957 2022-07-14 tracey
53 a596b957 2022-07-14 tracey #include "proc.h"
54 a596b957 2022-07-14 tracey #include "gotwebd.h"
55 a596b957 2022-07-14 tracey
56 a596b957 2022-07-14 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
57 a596b957 2022-07-14 tracey static struct file {
58 a596b957 2022-07-14 tracey TAILQ_ENTRY(file) entry;
59 a596b957 2022-07-14 tracey FILE *stream;
60 a596b957 2022-07-14 tracey char *name;
61 a596b957 2022-07-14 tracey int lineno;
62 a596b957 2022-07-14 tracey int errors;
63 a596b957 2022-07-14 tracey } *file;
64 a596b957 2022-07-14 tracey struct file *newfile(const char *, int);
65 a596b957 2022-07-14 tracey static void closefile(struct file *);
66 a596b957 2022-07-14 tracey int check_file_secrecy(int, const char *);
67 a596b957 2022-07-14 tracey int yyparse(void);
68 a596b957 2022-07-14 tracey int yylex(void);
69 a596b957 2022-07-14 tracey int yyerror(const char *, ...)
70 a596b957 2022-07-14 tracey __attribute__((__format__ (printf, 1, 2)))
71 a596b957 2022-07-14 tracey __attribute__((__nonnull__ (1)));
72 a596b957 2022-07-14 tracey int kw_cmp(const void *, const void *);
73 a596b957 2022-07-14 tracey int lookup(char *);
74 a596b957 2022-07-14 tracey int lgetc(int);
75 a596b957 2022-07-14 tracey int lungetc(int);
76 a596b957 2022-07-14 tracey int findeol(void);
77 a596b957 2022-07-14 tracey
78 a596b957 2022-07-14 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 a596b957 2022-07-14 tracey struct sym {
80 a596b957 2022-07-14 tracey TAILQ_ENTRY(sym) entry;
81 a596b957 2022-07-14 tracey int used;
82 a596b957 2022-07-14 tracey int persist;
83 a596b957 2022-07-14 tracey char *nam;
84 a596b957 2022-07-14 tracey char *val;
85 a596b957 2022-07-14 tracey };
86 a596b957 2022-07-14 tracey
87 a596b957 2022-07-14 tracey int symset(const char *, const char *, int);
88 a596b957 2022-07-14 tracey char *symget(const char *);
89 a596b957 2022-07-14 tracey
90 a596b957 2022-07-14 tracey static int errors;
91 a596b957 2022-07-14 tracey
92 a596b957 2022-07-14 tracey static struct gotwebd *gotwebd;
93 a596b957 2022-07-14 tracey static struct server *new_srv;
94 a596b957 2022-07-14 tracey static struct server *conf_new_server(const char *);
95 a596b957 2022-07-14 tracey int getservice(const char *);
96 a596b957 2022-07-14 tracey int n;
97 a596b957 2022-07-14 tracey
98 67d8de2a 2022-08-29 stsp int get_addrs(const char *, struct server *, in_port_t);
99 67d8de2a 2022-08-29 stsp int addr_dup_check(struct addresslist *, struct address *,
100 67d8de2a 2022-08-29 stsp const char *, const char *);
101 67d8de2a 2022-08-29 stsp int add_addr(struct server *, struct address *);
102 abf3e3f4 2023-05-29 op int host(const char *, struct server *,
103 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
104 67d8de2a 2022-08-29 stsp int host_if(const char *, struct server *,
105 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
106 a596b957 2022-07-14 tracey int is_if_in_group(const char *, const char *);
107 a596b957 2022-07-14 tracey
108 a596b957 2022-07-14 tracey typedef struct {
109 a596b957 2022-07-14 tracey union {
110 a596b957 2022-07-14 tracey long long number;
111 a596b957 2022-07-14 tracey char *string;
112 a596b957 2022-07-14 tracey in_port_t port;
113 a596b957 2022-07-14 tracey } v;
114 a596b957 2022-07-14 tracey int lineno;
115 a596b957 2022-07-14 tracey } YYSTYPE;
116 a596b957 2022-07-14 tracey
117 a596b957 2022-07-14 tracey %}
118 a596b957 2022-07-14 tracey
119 d8473d93 2022-08-11 stsp %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
120 a596b957 2022-07-14 tracey %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
121 a596b957 2022-07-14 tracey %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
122 d5996b9e 2022-10-31 landry %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
123 3a1c1a1b 2023-01-04 op %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
124 a596b957 2022-07-14 tracey
125 a596b957 2022-07-14 tracey %token <v.string> STRING
126 a596b957 2022-07-14 tracey %type <v.port> fcgiport
127 a596b957 2022-07-14 tracey %token <v.number> NUMBER
128 a596b957 2022-07-14 tracey %type <v.number> boolean
129 a596b957 2022-07-14 tracey
130 a596b957 2022-07-14 tracey %%
131 a596b957 2022-07-14 tracey
132 47b307cd 2022-10-02 op grammar : /* empty */
133 a596b957 2022-07-14 tracey | grammar '\n'
134 47b307cd 2022-10-02 op | grammar varset '\n'
135 a596b957 2022-07-14 tracey | grammar main '\n'
136 a596b957 2022-07-14 tracey | grammar server '\n'
137 47b307cd 2022-10-02 op | grammar error '\n' { file->errors++; }
138 47b307cd 2022-10-02 op ;
139 47b307cd 2022-10-02 op
140 47b307cd 2022-10-02 op varset : STRING '=' STRING {
141 47b307cd 2022-10-02 op char *s = $1;
142 47b307cd 2022-10-02 op while (*s++) {
143 47b307cd 2022-10-02 op if (isspace((unsigned char)*s)) {
144 47b307cd 2022-10-02 op yyerror("macro name cannot contain "
145 47b307cd 2022-10-02 op "whitespace");
146 47b307cd 2022-10-02 op free($1);
147 47b307cd 2022-10-02 op free($3);
148 47b307cd 2022-10-02 op YYERROR;
149 47b307cd 2022-10-02 op }
150 47b307cd 2022-10-02 op }
151 47b307cd 2022-10-02 op if (symset($1, $3, 0) == -1)
152 47b307cd 2022-10-02 op fatal("cannot store variable");
153 47b307cd 2022-10-02 op free($1);
154 47b307cd 2022-10-02 op free($3);
155 47b307cd 2022-10-02 op }
156 a596b957 2022-07-14 tracey ;
157 a596b957 2022-07-14 tracey
158 a596b957 2022-07-14 tracey boolean : STRING {
159 a596b957 2022-07-14 tracey if (strcasecmp($1, "1") == 0 ||
160 a596b957 2022-07-14 tracey strcasecmp($1, "on") == 0)
161 a596b957 2022-07-14 tracey $$ = 1;
162 a596b957 2022-07-14 tracey else if (strcasecmp($1, "0") == 0 ||
163 031687ba 2023-06-15 op strcasecmp($1, "off") == 0)
164 a596b957 2022-07-14 tracey $$ = 0;
165 a596b957 2022-07-14 tracey else {
166 a596b957 2022-07-14 tracey yyerror("invalid boolean value '%s'", $1);
167 a596b957 2022-07-14 tracey free($1);
168 a596b957 2022-07-14 tracey YYERROR;
169 a596b957 2022-07-14 tracey }
170 a596b957 2022-07-14 tracey free($1);
171 a596b957 2022-07-14 tracey }
172 a596b957 2022-07-14 tracey | ON { $$ = 1; }
173 1a0c81fb 2023-06-15 op | NUMBER {
174 1a0c81fb 2023-06-15 op if ($1 != 0 && $1 != 1) {
175 1a0c81fb 2023-06-15 op yyerror("invalid boolean value '%lld'", $1);
176 1a0c81fb 2023-06-15 op YYERROR;
177 1a0c81fb 2023-06-15 op }
178 1a0c81fb 2023-06-15 op $$ = $1;
179 1a0c81fb 2023-06-15 op }
180 a596b957 2022-07-14 tracey ;
181 a596b957 2022-07-14 tracey
182 17132eaa 2022-08-29 stsp fcgiport : PORT NUMBER {
183 17132eaa 2022-08-29 stsp if ($2 <= 0 || $2 > (int)USHRT_MAX) {
184 17132eaa 2022-08-29 stsp yyerror("invalid port: %lld", $2);
185 a596b957 2022-07-14 tracey YYERROR;
186 a596b957 2022-07-14 tracey }
187 17132eaa 2022-08-29 stsp $$ = $2;
188 a596b957 2022-07-14 tracey }
189 17132eaa 2022-08-29 stsp | PORT STRING {
190 a596b957 2022-07-14 tracey int val;
191 a596b957 2022-07-14 tracey
192 17132eaa 2022-08-29 stsp if ((val = getservice($2)) == -1) {
193 17132eaa 2022-08-29 stsp yyerror("invalid port: %s", $2);
194 17132eaa 2022-08-29 stsp free($2);
195 a596b957 2022-07-14 tracey YYERROR;
196 a596b957 2022-07-14 tracey }
197 17132eaa 2022-08-29 stsp free($2);
198 a596b957 2022-07-14 tracey
199 a596b957 2022-07-14 tracey $$ = val;
200 a596b957 2022-07-14 tracey }
201 a596b957 2022-07-14 tracey ;
202 a596b957 2022-07-14 tracey
203 a596b957 2022-07-14 tracey main : PREFORK NUMBER {
204 1a0c81fb 2023-06-15 op if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
205 1a0c81fb 2023-06-15 op yyerror("prefork is %s: %lld",
206 1a0c81fb 2023-06-15 op $2 <= 0 ? "too small" : "too large", $2);
207 1a0c81fb 2023-06-15 op YYERROR;
208 1a0c81fb 2023-06-15 op }
209 a596b957 2022-07-14 tracey gotwebd->prefork_gotwebd = $2;
210 a596b957 2022-07-14 tracey }
211 a596b957 2022-07-14 tracey | CHROOT STRING {
212 a678036d 2023-06-15 op if (*$2 == '\0') {
213 a678036d 2023-06-15 op yyerror("chroot path can't be an empty"
214 a678036d 2023-06-15 op " string");
215 a678036d 2023-06-15 op free($2);
216 a678036d 2023-06-15 op YYERROR;
217 a678036d 2023-06-15 op }
218 a678036d 2023-06-15 op
219 a596b957 2022-07-14 tracey n = strlcpy(gotwebd->httpd_chroot, $2,
220 a596b957 2022-07-14 tracey sizeof(gotwebd->httpd_chroot));
221 a596b957 2022-07-14 tracey if (n >= sizeof(gotwebd->httpd_chroot)) {
222 a596b957 2022-07-14 tracey yyerror("%s: httpd_chroot truncated", __func__);
223 a596b957 2022-07-14 tracey free($2);
224 a596b957 2022-07-14 tracey YYERROR;
225 a596b957 2022-07-14 tracey }
226 a596b957 2022-07-14 tracey free($2);
227 a596b957 2022-07-14 tracey }
228 a596b957 2022-07-14 tracey | UNIX_SOCKET boolean {
229 a596b957 2022-07-14 tracey gotwebd->unix_socket = $2;
230 a596b957 2022-07-14 tracey }
231 a596b957 2022-07-14 tracey | UNIX_SOCKET_NAME STRING {
232 a596b957 2022-07-14 tracey n = snprintf(gotwebd->unix_socket_name,
233 a596b957 2022-07-14 tracey sizeof(gotwebd->unix_socket_name), "%s%s",
234 f4a5cef1 2023-06-15 op gotwebd->httpd_chroot, $2);
235 438d0cc3 2022-08-16 op if (n < 0 ||
236 438d0cc3 2022-08-16 op (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
237 a596b957 2022-07-14 tracey yyerror("%s: unix_socket_name truncated",
238 a596b957 2022-07-14 tracey __func__);
239 a596b957 2022-07-14 tracey free($2);
240 a596b957 2022-07-14 tracey YYERROR;
241 a596b957 2022-07-14 tracey }
242 a596b957 2022-07-14 tracey free($2);
243 a596b957 2022-07-14 tracey }
244 a596b957 2022-07-14 tracey ;
245 a596b957 2022-07-14 tracey
246 a596b957 2022-07-14 tracey server : SERVER STRING {
247 a596b957 2022-07-14 tracey struct server *srv;
248 a596b957 2022-07-14 tracey
249 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
250 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
251 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
252 a596b957 2022-07-14 tracey free($2);
253 a596b957 2022-07-14 tracey YYERROR;
254 a596b957 2022-07-14 tracey }
255 a596b957 2022-07-14 tracey }
256 a596b957 2022-07-14 tracey
257 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
258 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
259 a596b957 2022-07-14 tracey free($2);
260 a596b957 2022-07-14 tracey }
261 a596b957 2022-07-14 tracey | SERVER STRING {
262 a596b957 2022-07-14 tracey struct server *srv;
263 a596b957 2022-07-14 tracey
264 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
265 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
266 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
267 a596b957 2022-07-14 tracey free($2);
268 a596b957 2022-07-14 tracey YYERROR;
269 a596b957 2022-07-14 tracey }
270 a596b957 2022-07-14 tracey }
271 a596b957 2022-07-14 tracey
272 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
273 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
274 a596b957 2022-07-14 tracey free($2);
275 a596b957 2022-07-14 tracey } '{' optnl serveropts2 '}' {
276 a596b957 2022-07-14 tracey }
277 a596b957 2022-07-14 tracey ;
278 a596b957 2022-07-14 tracey
279 a596b957 2022-07-14 tracey serveropts1 : REPOS_PATH STRING {
280 a596b957 2022-07-14 tracey n = strlcpy(new_srv->repos_path, $2,
281 a596b957 2022-07-14 tracey sizeof(new_srv->repos_path));
282 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->repos_path)) {
283 a596b957 2022-07-14 tracey yyerror("%s: repos_path truncated", __func__);
284 a596b957 2022-07-14 tracey free($2);
285 a596b957 2022-07-14 tracey YYERROR;
286 a596b957 2022-07-14 tracey }
287 a596b957 2022-07-14 tracey free($2);
288 a596b957 2022-07-14 tracey }
289 a596b957 2022-07-14 tracey | SITE_NAME STRING {
290 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_name, $2,
291 a596b957 2022-07-14 tracey sizeof(new_srv->site_name));
292 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_name)) {
293 a596b957 2022-07-14 tracey yyerror("%s: site_name truncated", __func__);
294 a596b957 2022-07-14 tracey free($2);
295 a596b957 2022-07-14 tracey YYERROR;
296 a596b957 2022-07-14 tracey }
297 a596b957 2022-07-14 tracey free($2);
298 a596b957 2022-07-14 tracey }
299 a596b957 2022-07-14 tracey | SITE_OWNER STRING {
300 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_owner, $2,
301 a596b957 2022-07-14 tracey sizeof(new_srv->site_owner));
302 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_owner)) {
303 a596b957 2022-07-14 tracey yyerror("%s: site_owner truncated", __func__);
304 a596b957 2022-07-14 tracey free($2);
305 a596b957 2022-07-14 tracey YYERROR;
306 a596b957 2022-07-14 tracey }
307 a596b957 2022-07-14 tracey free($2);
308 a596b957 2022-07-14 tracey }
309 a596b957 2022-07-14 tracey | SITE_LINK STRING {
310 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_link, $2,
311 a596b957 2022-07-14 tracey sizeof(new_srv->site_link));
312 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_link)) {
313 a596b957 2022-07-14 tracey yyerror("%s: site_link truncated", __func__);
314 a596b957 2022-07-14 tracey free($2);
315 a596b957 2022-07-14 tracey YYERROR;
316 a596b957 2022-07-14 tracey }
317 a596b957 2022-07-14 tracey free($2);
318 a596b957 2022-07-14 tracey }
319 a596b957 2022-07-14 tracey | LOGO STRING {
320 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
321 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo)) {
322 a596b957 2022-07-14 tracey yyerror("%s: logo truncated", __func__);
323 a596b957 2022-07-14 tracey free($2);
324 a596b957 2022-07-14 tracey YYERROR;
325 a596b957 2022-07-14 tracey }
326 a596b957 2022-07-14 tracey free($2);
327 a596b957 2022-07-14 tracey }
328 a596b957 2022-07-14 tracey | LOGO_URL STRING {
329 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo_url, $2,
330 a596b957 2022-07-14 tracey sizeof(new_srv->logo_url));
331 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo_url)) {
332 a596b957 2022-07-14 tracey yyerror("%s: logo_url truncated", __func__);
333 a596b957 2022-07-14 tracey free($2);
334 a596b957 2022-07-14 tracey YYERROR;
335 a596b957 2022-07-14 tracey }
336 a596b957 2022-07-14 tracey free($2);
337 a596b957 2022-07-14 tracey }
338 a596b957 2022-07-14 tracey | CUSTOM_CSS STRING {
339 a596b957 2022-07-14 tracey n = strlcpy(new_srv->custom_css, $2,
340 a596b957 2022-07-14 tracey sizeof(new_srv->custom_css));
341 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->custom_css)) {
342 a596b957 2022-07-14 tracey yyerror("%s: custom_css truncated", __func__);
343 a596b957 2022-07-14 tracey free($2);
344 a596b957 2022-07-14 tracey YYERROR;
345 a596b957 2022-07-14 tracey }
346 a596b957 2022-07-14 tracey free($2);
347 a596b957 2022-07-14 tracey }
348 17132eaa 2022-08-29 stsp | LISTEN ON STRING fcgiport {
349 67d8de2a 2022-08-29 stsp if (get_addrs($3, new_srv, $4) == -1) {
350 67d8de2a 2022-08-29 stsp yyerror("could not get addrs");
351 17132eaa 2022-08-29 stsp YYERROR;
352 17132eaa 2022-08-29 stsp }
353 6c8aa58f 2022-08-30 stsp new_srv->fcgi_socket = 1;
354 17132eaa 2022-08-29 stsp }
355 3a1c1a1b 2023-01-04 op | LISTEN ON SOCKET STRING {
356 031687ba 2023-06-15 op if (strcasecmp($4, "off") == 0) {
357 3a1c1a1b 2023-01-04 op new_srv->unix_socket = 0;
358 3a1c1a1b 2023-01-04 op free($4);
359 3a1c1a1b 2023-01-04 op YYACCEPT;
360 3a1c1a1b 2023-01-04 op }
361 3a1c1a1b 2023-01-04 op
362 3a1c1a1b 2023-01-04 op new_srv->unix_socket = 1;
363 3a1c1a1b 2023-01-04 op
364 3a1c1a1b 2023-01-04 op n = snprintf(new_srv->unix_socket_name,
365 3a1c1a1b 2023-01-04 op sizeof(new_srv->unix_socket_name), "%s%s",
366 f4a5cef1 2023-06-15 op gotwebd->httpd_chroot, $4);
367 3a1c1a1b 2023-01-04 op if (n < 0 ||
368 3a1c1a1b 2023-01-04 op (size_t)n >= sizeof(new_srv->unix_socket_name)) {
369 3a1c1a1b 2023-01-04 op yyerror("%s: unix_socket_name truncated",
370 3a1c1a1b 2023-01-04 op __func__);
371 3a1c1a1b 2023-01-04 op free($4);
372 3a1c1a1b 2023-01-04 op YYERROR;
373 3a1c1a1b 2023-01-04 op }
374 3a1c1a1b 2023-01-04 op free($4);
375 3a1c1a1b 2023-01-04 op }
376 a596b957 2022-07-14 tracey | MAX_REPOS NUMBER {
377 1a0c81fb 2023-06-15 op if ($2 <= 0) {
378 1a0c81fb 2023-06-15 op yyerror("max_repos is too small: %lld", $2);
379 1a0c81fb 2023-06-15 op YYERROR;
380 1a0c81fb 2023-06-15 op }
381 1a0c81fb 2023-06-15 op new_srv->max_repos = $2;
382 a596b957 2022-07-14 tracey }
383 a596b957 2022-07-14 tracey | SHOW_SITE_OWNER boolean {
384 a596b957 2022-07-14 tracey new_srv->show_site_owner = $2;
385 a596b957 2022-07-14 tracey }
386 a596b957 2022-07-14 tracey | SHOW_REPO_OWNER boolean {
387 a596b957 2022-07-14 tracey new_srv->show_repo_owner = $2;
388 a596b957 2022-07-14 tracey }
389 a596b957 2022-07-14 tracey | SHOW_REPO_AGE boolean {
390 a596b957 2022-07-14 tracey new_srv->show_repo_age = $2;
391 a596b957 2022-07-14 tracey }
392 a596b957 2022-07-14 tracey | SHOW_REPO_DESCRIPTION boolean {
393 a596b957 2022-07-14 tracey new_srv->show_repo_description = $2;
394 a596b957 2022-07-14 tracey }
395 a596b957 2022-07-14 tracey | SHOW_REPO_CLONEURL boolean {
396 a596b957 2022-07-14 tracey new_srv->show_repo_cloneurl = $2;
397 a596b957 2022-07-14 tracey }
398 d5996b9e 2022-10-31 landry | RESPECT_EXPORTOK boolean {
399 d5996b9e 2022-10-31 landry new_srv->respect_exportok = $2;
400 d5996b9e 2022-10-31 landry }
401 a596b957 2022-07-14 tracey | MAX_REPOS_DISPLAY NUMBER {
402 20f27972 2023-06-19 op if ($2 < 0) {
403 1a0c81fb 2023-06-15 op yyerror("max_repos_display is too small: %lld",
404 1a0c81fb 2023-06-15 op $2);
405 1a0c81fb 2023-06-15 op YYERROR;
406 1a0c81fb 2023-06-15 op }
407 1a0c81fb 2023-06-15 op new_srv->max_repos_display = $2;
408 a596b957 2022-07-14 tracey }
409 a596b957 2022-07-14 tracey | MAX_COMMITS_DISPLAY NUMBER {
410 f4425f95 2023-06-14 op if ($2 <= 1) {
411 f4425f95 2023-06-14 op yyerror("max_commits_display is too small:"
412 f4425f95 2023-06-14 op " %lld", $2);
413 f4425f95 2023-06-14 op YYERROR;
414 f4425f95 2023-06-14 op }
415 f4425f95 2023-06-14 op new_srv->max_commits_display = $2;
416 a596b957 2022-07-14 tracey }
417 a596b957 2022-07-14 tracey ;
418 a596b957 2022-07-14 tracey
419 a596b957 2022-07-14 tracey serveropts2 : serveropts2 serveropts1 nl
420 a596b957 2022-07-14 tracey | serveropts1 optnl
421 a596b957 2022-07-14 tracey ;
422 a596b957 2022-07-14 tracey
423 a596b957 2022-07-14 tracey nl : '\n' optnl
424 a596b957 2022-07-14 tracey ;
425 a596b957 2022-07-14 tracey
426 a596b957 2022-07-14 tracey optnl : '\n' optnl /* zero or more newlines */
427 a596b957 2022-07-14 tracey | /* empty */
428 a596b957 2022-07-14 tracey ;
429 a596b957 2022-07-14 tracey
430 a596b957 2022-07-14 tracey %%
431 a596b957 2022-07-14 tracey
432 a596b957 2022-07-14 tracey struct keywords {
433 a596b957 2022-07-14 tracey const char *k_name;
434 a596b957 2022-07-14 tracey int k_val;
435 a596b957 2022-07-14 tracey };
436 a596b957 2022-07-14 tracey
437 a596b957 2022-07-14 tracey int
438 a596b957 2022-07-14 tracey yyerror(const char *fmt, ...)
439 a596b957 2022-07-14 tracey {
440 a596b957 2022-07-14 tracey va_list ap;
441 a596b957 2022-07-14 tracey char *msg;
442 a596b957 2022-07-14 tracey
443 a596b957 2022-07-14 tracey file->errors++;
444 a596b957 2022-07-14 tracey va_start(ap, fmt);
445 a596b957 2022-07-14 tracey if (vasprintf(&msg, fmt, ap) == -1)
446 a596b957 2022-07-14 tracey fatalx("yyerror vasprintf");
447 a596b957 2022-07-14 tracey va_end(ap);
448 a596b957 2022-07-14 tracey logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
449 a596b957 2022-07-14 tracey free(msg);
450 a596b957 2022-07-14 tracey return (0);
451 a596b957 2022-07-14 tracey }
452 a596b957 2022-07-14 tracey
453 a596b957 2022-07-14 tracey int
454 a596b957 2022-07-14 tracey kw_cmp(const void *k, const void *e)
455 a596b957 2022-07-14 tracey {
456 a596b957 2022-07-14 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
457 a596b957 2022-07-14 tracey }
458 a596b957 2022-07-14 tracey
459 a596b957 2022-07-14 tracey int
460 a596b957 2022-07-14 tracey lookup(char *s)
461 a596b957 2022-07-14 tracey {
462 a596b957 2022-07-14 tracey /* This has to be sorted always. */
463 a596b957 2022-07-14 tracey static const struct keywords keywords[] = {
464 a596b957 2022-07-14 tracey { "chroot", CHROOT },
465 a596b957 2022-07-14 tracey { "custom_css", CUSTOM_CSS },
466 d8473d93 2022-08-11 stsp { "listen", LISTEN },
467 a596b957 2022-07-14 tracey { "logo", LOGO },
468 8556b86b 2023-01-02 op { "logo_url", LOGO_URL },
469 a596b957 2022-07-14 tracey { "max_commits_display", MAX_COMMITS_DISPLAY },
470 a596b957 2022-07-14 tracey { "max_repos", MAX_REPOS },
471 a596b957 2022-07-14 tracey { "max_repos_display", MAX_REPOS_DISPLAY },
472 d8473d93 2022-08-11 stsp { "on", ON },
473 a596b957 2022-07-14 tracey { "port", PORT },
474 a596b957 2022-07-14 tracey { "prefork", PREFORK },
475 a596b957 2022-07-14 tracey { "repos_path", REPOS_PATH },
476 d5996b9e 2022-10-31 landry { "respect_exportok", RESPECT_EXPORTOK },
477 a596b957 2022-07-14 tracey { "server", SERVER },
478 a596b957 2022-07-14 tracey { "show_repo_age", SHOW_REPO_AGE },
479 a596b957 2022-07-14 tracey { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
480 a596b957 2022-07-14 tracey { "show_repo_description", SHOW_REPO_DESCRIPTION },
481 a596b957 2022-07-14 tracey { "show_repo_owner", SHOW_REPO_OWNER },
482 a596b957 2022-07-14 tracey { "show_site_owner", SHOW_SITE_OWNER },
483 a596b957 2022-07-14 tracey { "site_link", SITE_LINK },
484 a596b957 2022-07-14 tracey { "site_name", SITE_NAME },
485 a596b957 2022-07-14 tracey { "site_owner", SITE_OWNER },
486 3a1c1a1b 2023-01-04 op { "socket", SOCKET },
487 a596b957 2022-07-14 tracey { "unix_socket", UNIX_SOCKET },
488 a596b957 2022-07-14 tracey { "unix_socket_name", UNIX_SOCKET_NAME },
489 a596b957 2022-07-14 tracey };
490 a596b957 2022-07-14 tracey const struct keywords *p;
491 a596b957 2022-07-14 tracey
492 a596b957 2022-07-14 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
493 a596b957 2022-07-14 tracey sizeof(keywords[0]), kw_cmp);
494 a596b957 2022-07-14 tracey
495 a596b957 2022-07-14 tracey if (p)
496 a596b957 2022-07-14 tracey return (p->k_val);
497 a596b957 2022-07-14 tracey else
498 a596b957 2022-07-14 tracey return (STRING);
499 a596b957 2022-07-14 tracey }
500 a596b957 2022-07-14 tracey
501 a596b957 2022-07-14 tracey #define MAXPUSHBACK 128
502 a596b957 2022-07-14 tracey
503 a596b957 2022-07-14 tracey unsigned char *parsebuf;
504 a596b957 2022-07-14 tracey int parseindex;
505 a596b957 2022-07-14 tracey unsigned char pushback_buffer[MAXPUSHBACK];
506 a596b957 2022-07-14 tracey int pushback_index = 0;
507 a596b957 2022-07-14 tracey
508 a596b957 2022-07-14 tracey int
509 a596b957 2022-07-14 tracey lgetc(int quotec)
510 a596b957 2022-07-14 tracey {
511 a596b957 2022-07-14 tracey int c, next;
512 a596b957 2022-07-14 tracey
513 a596b957 2022-07-14 tracey if (parsebuf) {
514 a596b957 2022-07-14 tracey /* Read character from the parsebuffer instead of input. */
515 a596b957 2022-07-14 tracey if (parseindex >= 0) {
516 a596b957 2022-07-14 tracey c = parsebuf[parseindex++];
517 a596b957 2022-07-14 tracey if (c != '\0')
518 a596b957 2022-07-14 tracey return (c);
519 a596b957 2022-07-14 tracey parsebuf = NULL;
520 a596b957 2022-07-14 tracey } else
521 a596b957 2022-07-14 tracey parseindex++;
522 a596b957 2022-07-14 tracey }
523 a596b957 2022-07-14 tracey
524 a596b957 2022-07-14 tracey if (pushback_index)
525 a596b957 2022-07-14 tracey return (pushback_buffer[--pushback_index]);
526 a596b957 2022-07-14 tracey
527 a596b957 2022-07-14 tracey if (quotec) {
528 a596b957 2022-07-14 tracey c = getc(file->stream);
529 a596b957 2022-07-14 tracey if (c == EOF)
530 a596b957 2022-07-14 tracey yyerror("reached end of file while parsing "
531 a596b957 2022-07-14 tracey "quoted string");
532 a596b957 2022-07-14 tracey return (c);
533 a596b957 2022-07-14 tracey }
534 a596b957 2022-07-14 tracey
535 a596b957 2022-07-14 tracey c = getc(file->stream);
536 a596b957 2022-07-14 tracey while (c == '\\') {
537 a596b957 2022-07-14 tracey next = getc(file->stream);
538 a596b957 2022-07-14 tracey if (next != '\n') {
539 a596b957 2022-07-14 tracey c = next;
540 a596b957 2022-07-14 tracey break;
541 a596b957 2022-07-14 tracey }
542 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
543 a596b957 2022-07-14 tracey file->lineno++;
544 a596b957 2022-07-14 tracey c = getc(file->stream);
545 a596b957 2022-07-14 tracey }
546 a596b957 2022-07-14 tracey
547 a596b957 2022-07-14 tracey return (c);
548 a596b957 2022-07-14 tracey }
549 a596b957 2022-07-14 tracey
550 a596b957 2022-07-14 tracey int
551 a596b957 2022-07-14 tracey lungetc(int c)
552 a596b957 2022-07-14 tracey {
553 a596b957 2022-07-14 tracey if (c == EOF)
554 a596b957 2022-07-14 tracey return (EOF);
555 a596b957 2022-07-14 tracey if (parsebuf) {
556 a596b957 2022-07-14 tracey parseindex--;
557 a596b957 2022-07-14 tracey if (parseindex >= 0)
558 a596b957 2022-07-14 tracey return (c);
559 a596b957 2022-07-14 tracey }
560 a596b957 2022-07-14 tracey if (pushback_index < MAXPUSHBACK-1)
561 a596b957 2022-07-14 tracey return (pushback_buffer[pushback_index++] = c);
562 a596b957 2022-07-14 tracey else
563 a596b957 2022-07-14 tracey return (EOF);
564 a596b957 2022-07-14 tracey }
565 a596b957 2022-07-14 tracey
566 a596b957 2022-07-14 tracey int
567 a596b957 2022-07-14 tracey findeol(void)
568 a596b957 2022-07-14 tracey {
569 a596b957 2022-07-14 tracey int c;
570 a596b957 2022-07-14 tracey
571 a596b957 2022-07-14 tracey parsebuf = NULL;
572 a596b957 2022-07-14 tracey
573 a596b957 2022-07-14 tracey /* Skip to either EOF or the first real EOL. */
574 a596b957 2022-07-14 tracey while (1) {
575 a596b957 2022-07-14 tracey if (pushback_index)
576 a596b957 2022-07-14 tracey c = pushback_buffer[--pushback_index];
577 a596b957 2022-07-14 tracey else
578 a596b957 2022-07-14 tracey c = lgetc(0);
579 a596b957 2022-07-14 tracey if (c == '\n') {
580 a596b957 2022-07-14 tracey file->lineno++;
581 a596b957 2022-07-14 tracey break;
582 a596b957 2022-07-14 tracey }
583 a596b957 2022-07-14 tracey if (c == EOF)
584 a596b957 2022-07-14 tracey break;
585 a596b957 2022-07-14 tracey }
586 a596b957 2022-07-14 tracey return (ERROR);
587 a596b957 2022-07-14 tracey }
588 a596b957 2022-07-14 tracey
589 a596b957 2022-07-14 tracey int
590 a596b957 2022-07-14 tracey yylex(void)
591 a596b957 2022-07-14 tracey {
592 a596b957 2022-07-14 tracey unsigned char buf[8096];
593 a596b957 2022-07-14 tracey unsigned char *p, *val;
594 a596b957 2022-07-14 tracey int quotec, next, c;
595 a596b957 2022-07-14 tracey int token;
596 a596b957 2022-07-14 tracey
597 a596b957 2022-07-14 tracey top:
598 a596b957 2022-07-14 tracey p = buf;
599 a596b957 2022-07-14 tracey c = lgetc(0);
600 a596b957 2022-07-14 tracey while (c == ' ' || c == '\t')
601 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
602 a596b957 2022-07-14 tracey
603 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
604 a596b957 2022-07-14 tracey if (c == '#') {
605 a596b957 2022-07-14 tracey c = lgetc(0);
606 a596b957 2022-07-14 tracey while (c != '\n' && c != EOF)
607 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
608 a596b957 2022-07-14 tracey }
609 a596b957 2022-07-14 tracey if (c == '$' && parsebuf == NULL) {
610 a596b957 2022-07-14 tracey while (1) {
611 a596b957 2022-07-14 tracey c = lgetc(0);
612 a596b957 2022-07-14 tracey if (c == EOF)
613 a596b957 2022-07-14 tracey return (0);
614 a596b957 2022-07-14 tracey
615 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
616 a596b957 2022-07-14 tracey yyerror("string too long");
617 a596b957 2022-07-14 tracey return (findeol());
618 a596b957 2022-07-14 tracey }
619 a596b957 2022-07-14 tracey if (isalnum(c) || c == '_') {
620 a596b957 2022-07-14 tracey *p++ = c;
621 a596b957 2022-07-14 tracey continue;
622 a596b957 2022-07-14 tracey }
623 a596b957 2022-07-14 tracey *p = '\0';
624 a596b957 2022-07-14 tracey lungetc(c);
625 a596b957 2022-07-14 tracey break;
626 a596b957 2022-07-14 tracey }
627 a596b957 2022-07-14 tracey val = symget(buf);
628 a596b957 2022-07-14 tracey if (val == NULL) {
629 a596b957 2022-07-14 tracey yyerror("macro '%s' not defined", buf);
630 a596b957 2022-07-14 tracey return (findeol());
631 a596b957 2022-07-14 tracey }
632 a596b957 2022-07-14 tracey parsebuf = val;
633 a596b957 2022-07-14 tracey parseindex = 0;
634 a596b957 2022-07-14 tracey goto top;
635 a596b957 2022-07-14 tracey }
636 a596b957 2022-07-14 tracey
637 a596b957 2022-07-14 tracey switch (c) {
638 a596b957 2022-07-14 tracey case '\'':
639 a596b957 2022-07-14 tracey case '"':
640 a596b957 2022-07-14 tracey quotec = c;
641 a596b957 2022-07-14 tracey while (1) {
642 a596b957 2022-07-14 tracey c = lgetc(quotec);
643 a596b957 2022-07-14 tracey if (c == EOF)
644 a596b957 2022-07-14 tracey return (0);
645 a596b957 2022-07-14 tracey if (c == '\n') {
646 a596b957 2022-07-14 tracey file->lineno++;
647 a596b957 2022-07-14 tracey continue;
648 a596b957 2022-07-14 tracey } else if (c == '\\') {
649 a596b957 2022-07-14 tracey next = lgetc(quotec);
650 a596b957 2022-07-14 tracey if (next == EOF)
651 a596b957 2022-07-14 tracey return (0);
652 a596b957 2022-07-14 tracey if (next == quotec || c == ' ' || c == '\t')
653 a596b957 2022-07-14 tracey c = next;
654 a596b957 2022-07-14 tracey else if (next == '\n') {
655 a596b957 2022-07-14 tracey file->lineno++;
656 a596b957 2022-07-14 tracey continue;
657 a596b957 2022-07-14 tracey } else
658 a596b957 2022-07-14 tracey lungetc(next);
659 a596b957 2022-07-14 tracey } else if (c == quotec) {
660 a596b957 2022-07-14 tracey *p = '\0';
661 a596b957 2022-07-14 tracey break;
662 a596b957 2022-07-14 tracey } else if (c == '\0') {
663 a596b957 2022-07-14 tracey yyerror("syntax error");
664 a596b957 2022-07-14 tracey return (findeol());
665 a596b957 2022-07-14 tracey }
666 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
667 a596b957 2022-07-14 tracey yyerror("string too long");
668 a596b957 2022-07-14 tracey return (findeol());
669 a596b957 2022-07-14 tracey }
670 a596b957 2022-07-14 tracey *p++ = c;
671 a596b957 2022-07-14 tracey }
672 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
673 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
674 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
675 a596b957 2022-07-14 tracey return (STRING);
676 a596b957 2022-07-14 tracey }
677 a596b957 2022-07-14 tracey
678 a596b957 2022-07-14 tracey #define allowed_to_end_number(x) \
679 a596b957 2022-07-14 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
680 a596b957 2022-07-14 tracey
681 a596b957 2022-07-14 tracey if (c == '-' || isdigit(c)) {
682 a596b957 2022-07-14 tracey do {
683 a596b957 2022-07-14 tracey *p++ = c;
684 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
685 a596b957 2022-07-14 tracey yyerror("string too long");
686 a596b957 2022-07-14 tracey return (findeol());
687 a596b957 2022-07-14 tracey }
688 a596b957 2022-07-14 tracey c = lgetc(0);
689 a596b957 2022-07-14 tracey } while (c != EOF && isdigit(c));
690 a596b957 2022-07-14 tracey lungetc(c);
691 a596b957 2022-07-14 tracey if (p == buf + 1 && buf[0] == '-')
692 a596b957 2022-07-14 tracey goto nodigits;
693 a596b957 2022-07-14 tracey if (c == EOF || allowed_to_end_number(c)) {
694 a596b957 2022-07-14 tracey const char *errstr = NULL;
695 a596b957 2022-07-14 tracey
696 a596b957 2022-07-14 tracey *p = '\0';
697 a596b957 2022-07-14 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
698 a596b957 2022-07-14 tracey LLONG_MAX, &errstr);
699 a596b957 2022-07-14 tracey if (errstr) {
700 a596b957 2022-07-14 tracey yyerror("\"%s\" invalid number: %s",
701 a596b957 2022-07-14 tracey buf, errstr);
702 a596b957 2022-07-14 tracey return (findeol());
703 a596b957 2022-07-14 tracey }
704 a596b957 2022-07-14 tracey return (NUMBER);
705 a596b957 2022-07-14 tracey } else {
706 a596b957 2022-07-14 tracey nodigits:
707 a596b957 2022-07-14 tracey while (p > buf + 1)
708 a596b957 2022-07-14 tracey lungetc(*--p);
709 a596b957 2022-07-14 tracey c = *--p;
710 a596b957 2022-07-14 tracey if (c == '-')
711 a596b957 2022-07-14 tracey return (c);
712 a596b957 2022-07-14 tracey }
713 a596b957 2022-07-14 tracey }
714 a596b957 2022-07-14 tracey
715 a596b957 2022-07-14 tracey #define allowed_in_string(x) \
716 a596b957 2022-07-14 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
717 a596b957 2022-07-14 tracey x != '{' && x != '}' && \
718 a596b957 2022-07-14 tracey x != '!' && x != '=' && x != '#' && \
719 a596b957 2022-07-14 tracey x != ','))
720 a596b957 2022-07-14 tracey
721 a596b957 2022-07-14 tracey if (isalnum(c) || c == ':' || c == '_') {
722 a596b957 2022-07-14 tracey do {
723 a596b957 2022-07-14 tracey *p++ = c;
724 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
725 a596b957 2022-07-14 tracey yyerror("string too long");
726 a596b957 2022-07-14 tracey return (findeol());
727 a596b957 2022-07-14 tracey }
728 a596b957 2022-07-14 tracey c = lgetc(0);
729 a596b957 2022-07-14 tracey } while (c != EOF && (allowed_in_string(c)));
730 a596b957 2022-07-14 tracey lungetc(c);
731 a596b957 2022-07-14 tracey *p = '\0';
732 a596b957 2022-07-14 tracey token = lookup(buf);
733 a596b957 2022-07-14 tracey if (token == STRING) {
734 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
735 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
736 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
737 a596b957 2022-07-14 tracey }
738 a596b957 2022-07-14 tracey return (token);
739 a596b957 2022-07-14 tracey }
740 a596b957 2022-07-14 tracey if (c == '\n') {
741 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
742 a596b957 2022-07-14 tracey file->lineno++;
743 a596b957 2022-07-14 tracey }
744 a596b957 2022-07-14 tracey if (c == EOF)
745 a596b957 2022-07-14 tracey return (0);
746 a596b957 2022-07-14 tracey return (c);
747 a596b957 2022-07-14 tracey }
748 a596b957 2022-07-14 tracey
749 a596b957 2022-07-14 tracey int
750 a596b957 2022-07-14 tracey check_file_secrecy(int fd, const char *fname)
751 a596b957 2022-07-14 tracey {
752 a596b957 2022-07-14 tracey struct stat st;
753 a596b957 2022-07-14 tracey
754 a596b957 2022-07-14 tracey if (fstat(fd, &st)) {
755 a596b957 2022-07-14 tracey log_warn("cannot stat %s", fname);
756 a596b957 2022-07-14 tracey return (-1);
757 a596b957 2022-07-14 tracey }
758 a596b957 2022-07-14 tracey if (st.st_uid != 0 && st.st_uid != getuid()) {
759 a596b957 2022-07-14 tracey log_warnx("%s: owner not root or current user", fname);
760 a596b957 2022-07-14 tracey return (-1);
761 a596b957 2022-07-14 tracey }
762 a596b957 2022-07-14 tracey if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
763 a596b957 2022-07-14 tracey log_warnx("%s: group writable or world read/writable", fname);
764 a596b957 2022-07-14 tracey return (-1);
765 a596b957 2022-07-14 tracey }
766 a596b957 2022-07-14 tracey return (0);
767 a596b957 2022-07-14 tracey }
768 a596b957 2022-07-14 tracey
769 a596b957 2022-07-14 tracey struct file *
770 a596b957 2022-07-14 tracey newfile(const char *name, int secret)
771 a596b957 2022-07-14 tracey {
772 a596b957 2022-07-14 tracey struct file *nfile;
773 a596b957 2022-07-14 tracey
774 a596b957 2022-07-14 tracey nfile = calloc(1, sizeof(struct file));
775 a596b957 2022-07-14 tracey if (nfile == NULL) {
776 a596b957 2022-07-14 tracey log_warn("calloc");
777 a596b957 2022-07-14 tracey return (NULL);
778 a596b957 2022-07-14 tracey }
779 a596b957 2022-07-14 tracey nfile->name = strdup(name);
780 a596b957 2022-07-14 tracey if (nfile->name == NULL) {
781 a596b957 2022-07-14 tracey log_warn("strdup");
782 a596b957 2022-07-14 tracey free(nfile);
783 a596b957 2022-07-14 tracey return (NULL);
784 a596b957 2022-07-14 tracey }
785 a596b957 2022-07-14 tracey nfile->stream = fopen(nfile->name, "r");
786 a596b957 2022-07-14 tracey if (nfile->stream == NULL) {
787 a596b957 2022-07-14 tracey /* no warning, we don't require a conf file */
788 a596b957 2022-07-14 tracey free(nfile->name);
789 a596b957 2022-07-14 tracey free(nfile);
790 a596b957 2022-07-14 tracey return (NULL);
791 a596b957 2022-07-14 tracey } else if (secret &&
792 a596b957 2022-07-14 tracey check_file_secrecy(fileno(nfile->stream), nfile->name)) {
793 a596b957 2022-07-14 tracey fclose(nfile->stream);
794 a596b957 2022-07-14 tracey free(nfile->name);
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->lineno = 1;
799 a596b957 2022-07-14 tracey return (nfile);
800 a596b957 2022-07-14 tracey }
801 a596b957 2022-07-14 tracey
802 a596b957 2022-07-14 tracey static void
803 a596b957 2022-07-14 tracey closefile(struct file *xfile)
804 a596b957 2022-07-14 tracey {
805 a596b957 2022-07-14 tracey fclose(xfile->stream);
806 a596b957 2022-07-14 tracey free(xfile->name);
807 a596b957 2022-07-14 tracey free(xfile);
808 a596b957 2022-07-14 tracey }
809 a596b957 2022-07-14 tracey
810 a0037b73 2022-08-03 stsp static void
811 a0037b73 2022-08-03 stsp add_default_server(void)
812 a0037b73 2022-08-03 stsp {
813 a0037b73 2022-08-03 stsp new_srv = conf_new_server(D_SITENAME);
814 a0037b73 2022-08-03 stsp log_debug("%s: adding default server %s", __func__, D_SITENAME);
815 a0037b73 2022-08-03 stsp }
816 a0037b73 2022-08-03 stsp
817 a596b957 2022-07-14 tracey int
818 a596b957 2022-07-14 tracey parse_config(const char *filename, struct gotwebd *env)
819 a596b957 2022-07-14 tracey {
820 a596b957 2022-07-14 tracey struct sym *sym, *next;
821 a596b957 2022-07-14 tracey
822 a596b957 2022-07-14 tracey if (config_init(env) == -1)
823 a596b957 2022-07-14 tracey fatalx("failed to initialize configuration");
824 a596b957 2022-07-14 tracey
825 a596b957 2022-07-14 tracey gotwebd = env;
826 a0037b73 2022-08-03 stsp
827 a0037b73 2022-08-03 stsp file = newfile(filename, 0);
828 a0037b73 2022-08-03 stsp if (file == NULL) {
829 a0037b73 2022-08-03 stsp add_default_server();
830 a0037b73 2022-08-03 stsp sockets_parse_sockets(env);
831 a0037b73 2022-08-03 stsp /* just return, as we don't require a conf file */
832 a0037b73 2022-08-03 stsp return (0);
833 a0037b73 2022-08-03 stsp }
834 a596b957 2022-07-14 tracey
835 a596b957 2022-07-14 tracey yyparse();
836 a596b957 2022-07-14 tracey errors = file->errors;
837 a596b957 2022-07-14 tracey closefile(file);
838 a596b957 2022-07-14 tracey
839 a596b957 2022-07-14 tracey /* Free macros and check which have not been used. */
840 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
841 a596b957 2022-07-14 tracey if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
842 a596b957 2022-07-14 tracey fprintf(stderr, "warning: macro '%s' not used\n",
843 a596b957 2022-07-14 tracey sym->nam);
844 a596b957 2022-07-14 tracey if (!sym->persist) {
845 a596b957 2022-07-14 tracey free(sym->nam);
846 a596b957 2022-07-14 tracey free(sym->val);
847 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
848 a596b957 2022-07-14 tracey free(sym);
849 a596b957 2022-07-14 tracey }
850 a596b957 2022-07-14 tracey }
851 a596b957 2022-07-14 tracey
852 a596b957 2022-07-14 tracey if (errors)
853 a596b957 2022-07-14 tracey return (-1);
854 a596b957 2022-07-14 tracey
855 a596b957 2022-07-14 tracey /* just add default server if no config specified */
856 a0037b73 2022-08-03 stsp if (gotwebd->server_cnt == 0)
857 a0037b73 2022-08-03 stsp add_default_server();
858 a596b957 2022-07-14 tracey
859 a596b957 2022-07-14 tracey /* setup our listening sockets */
860 a596b957 2022-07-14 tracey sockets_parse_sockets(env);
861 a596b957 2022-07-14 tracey
862 a596b957 2022-07-14 tracey return (0);
863 a596b957 2022-07-14 tracey }
864 a596b957 2022-07-14 tracey
865 a596b957 2022-07-14 tracey struct server *
866 a596b957 2022-07-14 tracey conf_new_server(const char *name)
867 a596b957 2022-07-14 tracey {
868 a596b957 2022-07-14 tracey struct server *srv = NULL;
869 a596b957 2022-07-14 tracey
870 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
871 a596b957 2022-07-14 tracey if (srv == NULL)
872 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
873 a596b957 2022-07-14 tracey
874 a596b957 2022-07-14 tracey n = strlcpy(srv->name, name, sizeof(srv->name));
875 a596b957 2022-07-14 tracey if (n >= sizeof(srv->name))
876 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
877 a596b957 2022-07-14 tracey n = snprintf(srv->unix_socket_name,
878 a596b957 2022-07-14 tracey sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
879 a596b957 2022-07-14 tracey D_UNIX_SOCKET);
880 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
881 a596b957 2022-07-14 tracey fatalx("%s: snprintf", __func__);
882 a596b957 2022-07-14 tracey n = strlcpy(srv->repos_path, D_GOTPATH,
883 a596b957 2022-07-14 tracey sizeof(srv->repos_path));
884 a596b957 2022-07-14 tracey if (n >= sizeof(srv->repos_path))
885 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
886 a596b957 2022-07-14 tracey n = strlcpy(srv->site_name, D_SITENAME,
887 a596b957 2022-07-14 tracey sizeof(srv->site_name));
888 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_name))
889 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
890 a596b957 2022-07-14 tracey n = strlcpy(srv->site_owner, D_SITEOWNER,
891 a596b957 2022-07-14 tracey sizeof(srv->site_owner));
892 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_owner))
893 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
894 a596b957 2022-07-14 tracey n = strlcpy(srv->site_link, D_SITELINK,
895 a596b957 2022-07-14 tracey sizeof(srv->site_link));
896 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_link))
897 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
898 a596b957 2022-07-14 tracey n = strlcpy(srv->logo, D_GOTLOGO,
899 a596b957 2022-07-14 tracey sizeof(srv->logo));
900 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo))
901 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
902 a596b957 2022-07-14 tracey n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
903 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo_url))
904 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
905 a596b957 2022-07-14 tracey n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
906 a596b957 2022-07-14 tracey if (n >= sizeof(srv->custom_css))
907 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
908 a596b957 2022-07-14 tracey
909 a596b957 2022-07-14 tracey srv->show_site_owner = D_SHOWSOWNER;
910 a596b957 2022-07-14 tracey srv->show_repo_owner = D_SHOWROWNER;
911 a596b957 2022-07-14 tracey srv->show_repo_age = D_SHOWAGE;
912 a596b957 2022-07-14 tracey srv->show_repo_description = D_SHOWDESC;
913 a596b957 2022-07-14 tracey srv->show_repo_cloneurl = D_SHOWURL;
914 d5996b9e 2022-10-31 landry srv->respect_exportok = D_RESPECTEXPORTOK;
915 a596b957 2022-07-14 tracey
916 a596b957 2022-07-14 tracey srv->max_repos_display = D_MAXREPODISP;
917 a596b957 2022-07-14 tracey srv->max_commits_display = D_MAXCOMMITDISP;
918 a596b957 2022-07-14 tracey srv->max_repos = D_MAXREPO;
919 a596b957 2022-07-14 tracey
920 a596b957 2022-07-14 tracey srv->unix_socket = 1;
921 6c8aa58f 2022-08-30 stsp srv->fcgi_socket = 0;
922 a596b957 2022-07-14 tracey
923 e087e1f6 2022-08-16 stsp TAILQ_INIT(&srv->al);
924 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
925 a596b957 2022-07-14 tracey gotwebd->server_cnt++;
926 a596b957 2022-07-14 tracey
927 a596b957 2022-07-14 tracey return srv;
928 a596b957 2022-07-14 tracey };
929 a596b957 2022-07-14 tracey
930 a596b957 2022-07-14 tracey int
931 a596b957 2022-07-14 tracey symset(const char *nam, const char *val, int persist)
932 a596b957 2022-07-14 tracey {
933 a596b957 2022-07-14 tracey struct sym *sym;
934 a596b957 2022-07-14 tracey
935 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
936 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0)
937 a596b957 2022-07-14 tracey break;
938 a596b957 2022-07-14 tracey }
939 a596b957 2022-07-14 tracey
940 a596b957 2022-07-14 tracey if (sym != NULL) {
941 a596b957 2022-07-14 tracey if (sym->persist == 1)
942 a596b957 2022-07-14 tracey return (0);
943 a596b957 2022-07-14 tracey else {
944 a596b957 2022-07-14 tracey free(sym->nam);
945 a596b957 2022-07-14 tracey free(sym->val);
946 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
947 a596b957 2022-07-14 tracey free(sym);
948 a596b957 2022-07-14 tracey }
949 a596b957 2022-07-14 tracey }
950 a596b957 2022-07-14 tracey sym = calloc(1, sizeof(*sym));
951 a596b957 2022-07-14 tracey if (sym == NULL)
952 a596b957 2022-07-14 tracey return (-1);
953 a596b957 2022-07-14 tracey
954 a596b957 2022-07-14 tracey sym->nam = strdup(nam);
955 a596b957 2022-07-14 tracey if (sym->nam == NULL) {
956 a596b957 2022-07-14 tracey free(sym);
957 a596b957 2022-07-14 tracey return (-1);
958 a596b957 2022-07-14 tracey }
959 a596b957 2022-07-14 tracey sym->val = strdup(val);
960 a596b957 2022-07-14 tracey if (sym->val == NULL) {
961 a596b957 2022-07-14 tracey free(sym->nam);
962 a596b957 2022-07-14 tracey free(sym);
963 a596b957 2022-07-14 tracey return (-1);
964 a596b957 2022-07-14 tracey }
965 a596b957 2022-07-14 tracey sym->used = 0;
966 a596b957 2022-07-14 tracey sym->persist = persist;
967 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
968 a596b957 2022-07-14 tracey return (0);
969 a596b957 2022-07-14 tracey }
970 a596b957 2022-07-14 tracey
971 a596b957 2022-07-14 tracey int
972 a596b957 2022-07-14 tracey cmdline_symset(char *s)
973 a596b957 2022-07-14 tracey {
974 a596b957 2022-07-14 tracey char *sym, *val;
975 a596b957 2022-07-14 tracey int ret;
976 a596b957 2022-07-14 tracey
977 a596b957 2022-07-14 tracey val = strrchr(s, '=');
978 a596b957 2022-07-14 tracey if (val == NULL)
979 a596b957 2022-07-14 tracey return (-1);
980 a596b957 2022-07-14 tracey
981 4cdd299d 2022-09-05 op sym = strndup(s, val - s);
982 a596b957 2022-07-14 tracey if (sym == NULL)
983 4cdd299d 2022-09-05 op fatal("%s: strndup", __func__);
984 a596b957 2022-07-14 tracey
985 a596b957 2022-07-14 tracey ret = symset(sym, val + 1, 1);
986 a596b957 2022-07-14 tracey free(sym);
987 a596b957 2022-07-14 tracey
988 a596b957 2022-07-14 tracey return (ret);
989 a596b957 2022-07-14 tracey }
990 a596b957 2022-07-14 tracey
991 a596b957 2022-07-14 tracey char *
992 a596b957 2022-07-14 tracey symget(const char *nam)
993 a596b957 2022-07-14 tracey {
994 a596b957 2022-07-14 tracey struct sym *sym;
995 a596b957 2022-07-14 tracey
996 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
997 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0) {
998 a596b957 2022-07-14 tracey sym->used = 1;
999 a596b957 2022-07-14 tracey return (sym->val);
1000 a596b957 2022-07-14 tracey }
1001 a596b957 2022-07-14 tracey }
1002 a596b957 2022-07-14 tracey return (NULL);
1003 a596b957 2022-07-14 tracey }
1004 a596b957 2022-07-14 tracey
1005 a596b957 2022-07-14 tracey int
1006 a596b957 2022-07-14 tracey getservice(const char *n)
1007 a596b957 2022-07-14 tracey {
1008 a596b957 2022-07-14 tracey struct servent *s;
1009 a596b957 2022-07-14 tracey const char *errstr;
1010 a596b957 2022-07-14 tracey long long llval;
1011 a596b957 2022-07-14 tracey
1012 a596b957 2022-07-14 tracey llval = strtonum(n, 0, UINT16_MAX, &errstr);
1013 a596b957 2022-07-14 tracey if (errstr) {
1014 a596b957 2022-07-14 tracey s = getservbyname(n, "tcp");
1015 a596b957 2022-07-14 tracey if (s == NULL)
1016 a596b957 2022-07-14 tracey s = getservbyname(n, "udp");
1017 a596b957 2022-07-14 tracey if (s == NULL)
1018 a596b957 2022-07-14 tracey return (-1);
1019 859aa9f4 2022-08-19 stsp return ntohs(s->s_port);
1020 a596b957 2022-07-14 tracey }
1021 a596b957 2022-07-14 tracey
1022 859aa9f4 2022-08-19 stsp return (unsigned short)llval;
1023 a596b957 2022-07-14 tracey }
1024 a596b957 2022-07-14 tracey
1025 a596b957 2022-07-14 tracey int
1026 abf3e3f4 2023-05-29 op host(const char *s, struct server *new_srv, int max,
1027 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1028 a596b957 2022-07-14 tracey {
1029 a596b957 2022-07-14 tracey struct addrinfo hints, *res0, *res;
1030 a596b957 2022-07-14 tracey int error, cnt = 0;
1031 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1032 a596b957 2022-07-14 tracey struct sockaddr_in6 *sin6;
1033 a596b957 2022-07-14 tracey struct address *h;
1034 a596b957 2022-07-14 tracey
1035 67d8de2a 2022-08-29 stsp if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1036 a596b957 2022-07-14 tracey return (cnt);
1037 a596b957 2022-07-14 tracey
1038 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
1039 fb307946 2023-05-29 op hints.ai_family = AF_UNSPEC;
1040 fb307946 2023-05-29 op hints.ai_socktype = SOCK_STREAM; /* DUMMY */
1041 a596b957 2022-07-14 tracey hints.ai_flags = AI_ADDRCONFIG;
1042 a596b957 2022-07-14 tracey error = getaddrinfo(s, NULL, &hints, &res0);
1043 a596b957 2022-07-14 tracey if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1044 a596b957 2022-07-14 tracey return (0);
1045 a596b957 2022-07-14 tracey if (error) {
1046 a596b957 2022-07-14 tracey log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1047 a596b957 2022-07-14 tracey gai_strerror(error));
1048 a596b957 2022-07-14 tracey return (-1);
1049 a596b957 2022-07-14 tracey }
1050 a596b957 2022-07-14 tracey
1051 a596b957 2022-07-14 tracey for (res = res0; res && cnt < max; res = res->ai_next) {
1052 a596b957 2022-07-14 tracey if (res->ai_family != AF_INET &&
1053 a596b957 2022-07-14 tracey res->ai_family != AF_INET6)
1054 a596b957 2022-07-14 tracey continue;
1055 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1056 a596b957 2022-07-14 tracey fatal(__func__);
1057 a596b957 2022-07-14 tracey
1058 a596b957 2022-07-14 tracey if (port)
1059 a596b957 2022-07-14 tracey h->port = port;
1060 a596b957 2022-07-14 tracey if (ifname != NULL) {
1061 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1062 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1063 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1064 a596b957 2022-07-14 tracey __func__);
1065 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1066 a596b957 2022-07-14 tracey free(h);
1067 a596b957 2022-07-14 tracey return (-1);
1068 a596b957 2022-07-14 tracey }
1069 a596b957 2022-07-14 tracey }
1070 a596b957 2022-07-14 tracey if (ipproto != -1)
1071 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1072 a596b957 2022-07-14 tracey h->ss.ss_family = res->ai_family;
1073 a596b957 2022-07-14 tracey
1074 a596b957 2022-07-14 tracey if (res->ai_family == AF_INET) {
1075 86b4b772 2022-07-29 stsp struct sockaddr_in *ra;
1076 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1077 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in *)res->ai_addr;
1078 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ra->sin_addr);
1079 a596b957 2022-07-14 tracey } else {
1080 86b4b772 2022-07-29 stsp struct sockaddr_in6 *ra;
1081 a596b957 2022-07-14 tracey sin6 = (struct sockaddr_in6 *)&h->ss;
1082 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)res->ai_addr;
1083 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1084 a596b957 2022-07-14 tracey }
1085 a596b957 2022-07-14 tracey
1086 67d8de2a 2022-08-29 stsp if (add_addr(new_srv, h))
1087 67d8de2a 2022-08-29 stsp return -1;
1088 a596b957 2022-07-14 tracey cnt++;
1089 a596b957 2022-07-14 tracey }
1090 a596b957 2022-07-14 tracey if (cnt == max && res) {
1091 a596b957 2022-07-14 tracey log_warnx("%s: %s resolves to more than %d hosts", __func__,
1092 a596b957 2022-07-14 tracey s, max);
1093 a596b957 2022-07-14 tracey }
1094 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1095 a596b957 2022-07-14 tracey return (cnt);
1096 a596b957 2022-07-14 tracey }
1097 a596b957 2022-07-14 tracey
1098 a596b957 2022-07-14 tracey int
1099 67d8de2a 2022-08-29 stsp host_if(const char *s, struct server *new_srv, int max,
1100 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1101 a596b957 2022-07-14 tracey {
1102 a596b957 2022-07-14 tracey struct ifaddrs *ifap, *p;
1103 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1104 a596b957 2022-07-14 tracey struct sockaddr_in6 *sin6;
1105 a596b957 2022-07-14 tracey struct address *h;
1106 a596b957 2022-07-14 tracey int cnt = 0, af;
1107 a596b957 2022-07-14 tracey
1108 a596b957 2022-07-14 tracey if (getifaddrs(&ifap) == -1)
1109 a596b957 2022-07-14 tracey fatal("getifaddrs");
1110 a596b957 2022-07-14 tracey
1111 a596b957 2022-07-14 tracey /* First search for IPv4 addresses */
1112 a596b957 2022-07-14 tracey af = AF_INET;
1113 a596b957 2022-07-14 tracey
1114 a596b957 2022-07-14 tracey nextaf:
1115 a596b957 2022-07-14 tracey for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1116 a596b957 2022-07-14 tracey if (p->ifa_addr == NULL ||
1117 a596b957 2022-07-14 tracey p->ifa_addr->sa_family != af ||
1118 a596b957 2022-07-14 tracey (strcmp(s, p->ifa_name) != 0 &&
1119 a596b957 2022-07-14 tracey !is_if_in_group(p->ifa_name, s)))
1120 a596b957 2022-07-14 tracey continue;
1121 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1122 a596b957 2022-07-14 tracey fatal("calloc");
1123 a596b957 2022-07-14 tracey
1124 a596b957 2022-07-14 tracey if (port)
1125 a596b957 2022-07-14 tracey h->port = port;
1126 a596b957 2022-07-14 tracey if (ifname != NULL) {
1127 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1128 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1129 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1130 a596b957 2022-07-14 tracey __func__);
1131 a596b957 2022-07-14 tracey free(h);
1132 a596b957 2022-07-14 tracey freeifaddrs(ifap);
1133 a596b957 2022-07-14 tracey return (-1);
1134 a596b957 2022-07-14 tracey }
1135 a596b957 2022-07-14 tracey }
1136 a596b957 2022-07-14 tracey if (ipproto != -1)
1137 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1138 a596b957 2022-07-14 tracey h->ss.ss_family = af;
1139 a596b957 2022-07-14 tracey
1140 a596b957 2022-07-14 tracey if (af == AF_INET) {
1141 86b4b772 2022-07-29 stsp struct sockaddr_in *ra;
1142 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1143 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in *)p->ifa_addr;
1144 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ra->sin_addr);
1145 a596b957 2022-07-14 tracey } else {
1146 86b4b772 2022-07-29 stsp struct sockaddr_in6 *ra;
1147 a596b957 2022-07-14 tracey sin6 = (struct sockaddr_in6 *)&h->ss;
1148 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)p->ifa_addr;
1149 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1150 86b4b772 2022-07-29 stsp ra->sin6_scope_id);
1151 a596b957 2022-07-14 tracey }
1152 a596b957 2022-07-14 tracey
1153 67d8de2a 2022-08-29 stsp if (add_addr(new_srv, h))
1154 67d8de2a 2022-08-29 stsp return -1;
1155 a596b957 2022-07-14 tracey cnt++;
1156 a596b957 2022-07-14 tracey }
1157 a596b957 2022-07-14 tracey if (af == AF_INET) {
1158 a596b957 2022-07-14 tracey /* Next search for IPv6 addresses */
1159 a596b957 2022-07-14 tracey af = AF_INET6;
1160 a596b957 2022-07-14 tracey goto nextaf;
1161 a596b957 2022-07-14 tracey }
1162 a596b957 2022-07-14 tracey
1163 a596b957 2022-07-14 tracey if (cnt > max) {
1164 a596b957 2022-07-14 tracey log_warnx("%s: %s resolves to more than %d hosts", __func__,
1165 a596b957 2022-07-14 tracey s, max);
1166 a596b957 2022-07-14 tracey }
1167 a596b957 2022-07-14 tracey freeifaddrs(ifap);
1168 a596b957 2022-07-14 tracey return (cnt);
1169 a596b957 2022-07-14 tracey }
1170 a596b957 2022-07-14 tracey
1171 a596b957 2022-07-14 tracey int
1172 a596b957 2022-07-14 tracey is_if_in_group(const char *ifname, const char *groupname)
1173 a596b957 2022-07-14 tracey {
1174 a596b957 2022-07-14 tracey unsigned int len;
1175 a596b957 2022-07-14 tracey struct ifgroupreq ifgr;
1176 a596b957 2022-07-14 tracey struct ifg_req *ifg;
1177 a596b957 2022-07-14 tracey int s;
1178 a596b957 2022-07-14 tracey int ret = 0;
1179 a596b957 2022-07-14 tracey
1180 a596b957 2022-07-14 tracey if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1181 a596b957 2022-07-14 tracey err(1, "socket");
1182 a596b957 2022-07-14 tracey
1183 a596b957 2022-07-14 tracey memset(&ifgr, 0, sizeof(ifgr));
1184 a596b957 2022-07-14 tracey if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1185 a596b957 2022-07-14 tracey err(1, "IFNAMSIZ");
1186 a596b957 2022-07-14 tracey if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1187 a596b957 2022-07-14 tracey if (errno == EINVAL || errno == ENOTTY)
1188 a596b957 2022-07-14 tracey goto end;
1189 a596b957 2022-07-14 tracey err(1, "SIOCGIFGROUP");
1190 a596b957 2022-07-14 tracey }
1191 a596b957 2022-07-14 tracey
1192 a596b957 2022-07-14 tracey len = ifgr.ifgr_len;
1193 a596b957 2022-07-14 tracey ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1194 a596b957 2022-07-14 tracey sizeof(struct ifg_req));
1195 a596b957 2022-07-14 tracey if (ifgr.ifgr_groups == NULL)
1196 a596b957 2022-07-14 tracey err(1, "getifgroups");
1197 a596b957 2022-07-14 tracey if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1198 a596b957 2022-07-14 tracey err(1, "SIOCGIFGROUP");
1199 a596b957 2022-07-14 tracey
1200 a596b957 2022-07-14 tracey ifg = ifgr.ifgr_groups;
1201 a596b957 2022-07-14 tracey for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1202 a596b957 2022-07-14 tracey len -= sizeof(struct ifg_req);
1203 a596b957 2022-07-14 tracey if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1204 a596b957 2022-07-14 tracey ret = 1;
1205 a596b957 2022-07-14 tracey break;
1206 a596b957 2022-07-14 tracey }
1207 a596b957 2022-07-14 tracey }
1208 a596b957 2022-07-14 tracey free(ifgr.ifgr_groups);
1209 a596b957 2022-07-14 tracey
1210 a596b957 2022-07-14 tracey end:
1211 a596b957 2022-07-14 tracey close(s);
1212 a596b957 2022-07-14 tracey return (ret);
1213 a596b957 2022-07-14 tracey }
1214 a596b957 2022-07-14 tracey
1215 a596b957 2022-07-14 tracey int
1216 67d8de2a 2022-08-29 stsp get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1217 a596b957 2022-07-14 tracey {
1218 a596b957 2022-07-14 tracey if (strcmp("", addr) == 0) {
1219 67d8de2a 2022-08-29 stsp if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1220 67d8de2a 2022-08-29 stsp -1) <= 0) {
1221 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s",
1222 2fb25d87 2022-08-19 stsp "127.0.0.1");
1223 a596b957 2022-07-14 tracey return (-1);
1224 a596b957 2022-07-14 tracey }
1225 67d8de2a 2022-08-29 stsp if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1226 2fb25d87 2022-08-19 stsp yyerror("invalid listen ip: %s", "::1");
1227 a596b957 2022-07-14 tracey return (-1);
1228 a596b957 2022-07-14 tracey }
1229 a596b957 2022-07-14 tracey } else {
1230 67d8de2a 2022-08-29 stsp if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1231 a596b957 2022-07-14 tracey -1) <= 0) {
1232 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s", addr);
1233 a596b957 2022-07-14 tracey return (-1);
1234 a596b957 2022-07-14 tracey }
1235 a596b957 2022-07-14 tracey }
1236 a596b957 2022-07-14 tracey return (0);
1237 67d8de2a 2022-08-29 stsp }
1238 67d8de2a 2022-08-29 stsp
1239 67d8de2a 2022-08-29 stsp int
1240 67d8de2a 2022-08-29 stsp addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1241 67d8de2a 2022-08-29 stsp const char *other_srv)
1242 67d8de2a 2022-08-29 stsp {
1243 67d8de2a 2022-08-29 stsp struct address *a;
1244 67d8de2a 2022-08-29 stsp void *ia;
1245 67d8de2a 2022-08-29 stsp char buf[INET6_ADDRSTRLEN];
1246 67d8de2a 2022-08-29 stsp const char *addrstr;
1247 67d8de2a 2022-08-29 stsp
1248 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(a, al, entry) {
1249 67d8de2a 2022-08-29 stsp if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1250 67d8de2a 2022-08-29 stsp a->port != h->port)
1251 67d8de2a 2022-08-29 stsp continue;
1252 67d8de2a 2022-08-29 stsp
1253 67d8de2a 2022-08-29 stsp switch (h->ss.ss_family) {
1254 67d8de2a 2022-08-29 stsp case AF_INET:
1255 67d8de2a 2022-08-29 stsp ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1256 67d8de2a 2022-08-29 stsp break;
1257 67d8de2a 2022-08-29 stsp case AF_INET6:
1258 67d8de2a 2022-08-29 stsp ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1259 67d8de2a 2022-08-29 stsp break;
1260 67d8de2a 2022-08-29 stsp default:
1261 67d8de2a 2022-08-29 stsp yyerror("unknown address family: %d", h->ss.ss_family);
1262 67d8de2a 2022-08-29 stsp return -1;
1263 67d8de2a 2022-08-29 stsp }
1264 67d8de2a 2022-08-29 stsp addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1265 67d8de2a 2022-08-29 stsp if (addrstr) {
1266 67d8de2a 2022-08-29 stsp if (other_srv) {
1267 67d8de2a 2022-08-29 stsp yyerror("server %s: duplicate fcgi listen "
1268 67d8de2a 2022-08-29 stsp "address %s:%d, already used by server %s",
1269 67d8de2a 2022-08-29 stsp new_srv, addrstr, h->port, other_srv);
1270 67d8de2a 2022-08-29 stsp } else {
1271 67d8de2a 2022-08-29 stsp log_warnx("server: %s: duplicate fcgi listen "
1272 67d8de2a 2022-08-29 stsp "address %s:%d", new_srv, addrstr, h->port);
1273 67d8de2a 2022-08-29 stsp }
1274 67d8de2a 2022-08-29 stsp } else {
1275 67d8de2a 2022-08-29 stsp if (other_srv) {
1276 67d8de2a 2022-08-29 stsp yyerror("server: %s: duplicate fcgi listen "
1277 67d8de2a 2022-08-29 stsp "address, already used by server %s",
1278 67d8de2a 2022-08-29 stsp new_srv, other_srv);
1279 67d8de2a 2022-08-29 stsp } else {
1280 67d8de2a 2022-08-29 stsp log_warnx("server %s: duplicate fcgi listen "
1281 67d8de2a 2022-08-29 stsp "address", new_srv);
1282 67d8de2a 2022-08-29 stsp }
1283 67d8de2a 2022-08-29 stsp }
1284 67d8de2a 2022-08-29 stsp
1285 67d8de2a 2022-08-29 stsp return -1;
1286 67d8de2a 2022-08-29 stsp }
1287 67d8de2a 2022-08-29 stsp
1288 67d8de2a 2022-08-29 stsp return 0;
1289 a596b957 2022-07-14 tracey }
1290 67d8de2a 2022-08-29 stsp
1291 67d8de2a 2022-08-29 stsp int
1292 67d8de2a 2022-08-29 stsp add_addr(struct server *new_srv, struct address *h)
1293 67d8de2a 2022-08-29 stsp {
1294 67d8de2a 2022-08-29 stsp struct server *srv;
1295 67d8de2a 2022-08-29 stsp
1296 67d8de2a 2022-08-29 stsp /* Address cannot be shared between different servers. */
1297 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1298 67d8de2a 2022-08-29 stsp if (srv == new_srv)
1299 67d8de2a 2022-08-29 stsp continue;
1300 67d8de2a 2022-08-29 stsp if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1301 67d8de2a 2022-08-29 stsp return -1;
1302 67d8de2a 2022-08-29 stsp }
1303 67d8de2a 2022-08-29 stsp
1304 67d8de2a 2022-08-29 stsp /* Tolerate duplicate address lines within the scope of a server. */
1305 67d8de2a 2022-08-29 stsp if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1306 67d8de2a 2022-08-29 stsp TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1307 67d8de2a 2022-08-29 stsp else
1308 67d8de2a 2022-08-29 stsp free(h);
1309 67d8de2a 2022-08-29 stsp
1310 67d8de2a 2022-08-29 stsp return 0;
1311 67d8de2a 2022-08-29 stsp }