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