💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Vgmi › files › 5700799c3048b8dc45dce142e8189… captured on 2023-12-28 at 15:41:41. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 /*
1 * ISC License
2 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include "macro.h"
9 #include "config.h"
10 #include "gemini.h"
11 #include "page.h"
12 #include "request.h"
13 #define ABOUT_INTERNAL
14 #include "about.h"
15 #include "error.h"
16 #include "strlcpy.h"
17 #include "memory.h"
18 #include "sandbox.h"
19 #include "parser.h"
20
21 char help_page[] =
22 HEADER \
23 "# Help\n\n" \
24 HELP_INFO;
25
26 char about_page[] =
27 HEADER \
28 "# List of \"about\" pages\n\n" \
29 "=>about:about\n" \
30 "=>about:blank\n" \
31 "=>about:bookmarks\n" \
32 "=>about:certificates\n" \
33 "=>about:config\n" \
34 "=>about:help\n" \
35 "=>about:history\n" \
36 "=>about:known-hosts\n" \
37 "=>about:license\n" \
38 "=>about:newtab\n" \
39 "=>about:sandbox\n";
40
41 char license_page[] =
42 HEADER \
43 "# Vgmi license\n\n" \
44 "Copyright (c) 2023 RMF <rawmonk@firemail.cc>\n\n" \
45 "Permission to use, copy, modify, and distribute this software for any\n" \
46 "purpose with or without fee is hereby granted, provided that the above\n" \
47 "copyright notice and this permission notice appear in all copies.\n\n" \
48 "THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n" \
49 "WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n" \
50 "MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n" \
51 "ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n" \
52 "WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n" \
53 "ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n" \
54 "OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\n";
55
56 char sandbox_page[] =
57 HEADER \
58 "# Sandbox information\n\n" \
59 ""SANDBOX_INFO"\n" \
60 "Filesystem access\t: "SANDBOX_FILESYSTEM"\n" \
61 "IPC access\t\t\t: "SANDBOX_IPC"\n" \
62 "Devices access\t\t: "SANDBOX_DEVICE"\n" \
63 "Parser isolation\t: "SANDBOX_PARSER"\n";
64
65 char sandbox_disabled_page[] =
66 HEADER \
67 "# Sandbox information\n\n" \
68 "The sandbox is disabled, enable it in about:config.\n" \
69 "Filesystem access\t: Unrestricted\n" \
70 "IPC access\t\t\t: Unrestricted\n" \
71 "Devices access\t\t: Unrestricted\n" \
72 "Parser isolation\t: Unrestricted\n";
73
74
75 void *dyn_strcat(char *dst, size_t *dst_length,
76 const char *src, size_t src_len) {
77 const size_t sum = (dst ? *dst_length : 0) + src_len + 2;
78 void *ptr = realloc(dst, sum + 1);
79 if (!ptr) return NULL;
80 dst = ptr;
81 strlcpy(&dst[*dst_length ? strnlen(dst, *dst_length) : 0], src, sum);
82 *dst_length = strnlen(dst, sum);
83 return dst;
84 }
85
86 static int parse_data_status(struct request *request,
87 char *data, size_t len, int status) {
88 if (readonly(data, len, &request->data)) return ERROR_MEMORY_FAILURE;
89 request->status = status;
90 request->length = len;
91 free(data);
92 return parse_request(NULL, request);
93 }
94
95 static int parse_data(struct request *request, char *data, size_t len) {
96 return parse_data_status(request, data, len, GMI_SUCCESS);
97 }
98
99 static int static_page(struct request *request, const char *data, size_t len) {
100 if (readonly(data, len, &request->data)) return ERROR_MEMORY_FAILURE;
101 request->status = GMI_SUCCESS;
102 request->length = len - 1;
103 return parse_request(NULL, request);
104 }
105
106 int about_parse(struct request *request) {
107
108 char param[MAX_URL];
109 char *ptr = strchr(request->url, '/');
110 char *data;
111 size_t length;
112 int ret;
113
114 if (ptr) {
115 *ptr = 0;
116 ptr++;
117 STRLCPY(param, ptr);
118 } else param[0] = 0;
119 if (!strcmp(request->url, "about:about")) {
120 return static_page(request, V(about_page));
121 }
122 if (!strcmp(request->url, "about:blank")) {
123 request->status = GMI_SUCCESS;
124 STRLCPY(request->page.title, "about:blank");
125 return 0;
126 }
127 if (!strcmp(request->url, "about:bookmarks")) {
128 if (ptr && (ret = about_bookmarks_param(param))) return ret;
129 if ((ret = about_bookmarks(&data, &length))) return ret;
130 return parse_data(request, data, length);
131 }
132 if (!strcmp(request->url, "about:certificates")) {
133 if (ptr && (ret = about_certificates_param(param))) return ret;
134 if ((ret = about_certificates(&data, &length))) return ret;
135 return parse_data(request, data, length);
136 }
137 if (!strcmp(request->url, "about:help")) {
138 return static_page(request, V(help_page));
139 }
140 if (!strcmp(request->url, "about:license")) {
141 return static_page(request, V(license_page));
142 }
143 if (!strcmp(request->url, "about:newtab")) {
144 if ((ret = about_newtab(&data, &length))) return ret;
145 return parse_data(request, data, length);
146 }
147 if (!strcmp(request->url, "about:known-hosts")) {
148 if (ptr && (ret = about_known_hosts_arg(param))) return ret;
149 if ((ret = about_known_hosts(&data, &length))) return ret;
150 return parse_data(request, data, length);
151 }
152 if (!strcmp(request->url, "about:config")) {
153 int query = ptr ? (strchr(param, '?') != NULL) : 0;
154 ret = ptr ? about_config_arg(param, &data, &length) :
155 about_config(&data, &length);
156 if (ret) return ret;
157 if (ptr) {
158 int len = strnlen(V(request->url));
159 if (len + strnlen(V(param)) + 1 > sizeof(request->url))
160 return ERROR_INVALID_ARGUMENT;
161 request->url[len] = '/';
162 strlcpy(&request->url[len + 1], param,
163 sizeof(request->url) - len - 1);
164 }
165 /* reset url on succesful query */
166 if (query) STRLCPY(request->url, "about:config");
167 return parse_data_status(request, data, length - 1,
168 ptr ? GMI_INPUT : GMI_SUCCESS);
169 }
170 if (!strcmp(request->url, "about:history")) {
171 if (ptr && (ret = about_history_param(param))) return ret;
172 if ((ret = about_history(&data, &length))) return ret;
173 return parse_data(request, data, length - 1);
174 }
175 if (!strcmp(request->url, "about:sandbox")) {
176 if (!config.enableSandbox) {
177 return static_page(request, V(sandbox_disabled_page));
178 }
179 return static_page(request, V(sandbox_page));
180 }
181 return ERROR_INVALID_URL;
182 }
183