Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets web server application |
| 3 | * |
Andy Green | f44e38f | 2018-08-02 19:13:53 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com> |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 5 | * |
Andy Green | 0c98401 | 2016-09-19 19:16:47 +0800 | [diff] [blame] | 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 10 | * |
Andy Green | 0c98401 | 2016-09-19 19:16:47 +0800 | [diff] [blame] | 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 15 | * |
Andy Green | 0c98401 | 2016-09-19 19:16:47 +0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 20 | */ |
| 21 | |
Andy Green | de064fd | 2018-05-03 10:49:36 +0800 | [diff] [blame] | 22 | #include "core/private.h" |
Andy Green | 3358c53 | 2016-05-26 21:12:11 +0800 | [diff] [blame] | 23 | |
| 24 | #ifndef _WIN32 |
| 25 | /* this is needed for Travis CI */ |
| 26 | #include <dirent.h> |
| 27 | #endif |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 28 | |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 29 | #define ESC_INSTALL_DATADIR "_lws_ddir_" |
| 30 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 31 | static const char * const paths_global[] = { |
| 32 | "global.uid", |
| 33 | "global.gid", |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 34 | "global.count-threads", |
| 35 | "global.init-ssl", |
Andy Green | b21c20b | 2016-04-15 13:33:52 +0800 | [diff] [blame] | 36 | "global.server-string", |
Andy Green | f32d250 | 2016-07-15 13:41:38 +0800 | [diff] [blame] | 37 | "global.plugin-dir", |
| 38 | "global.ws-pingpong-secs", |
Andy Green | 58a26eb | 2017-01-02 19:57:54 +0800 | [diff] [blame] | 39 | "global.timeout-secs", |
Andy Green | 3b93e34 | 2016-10-13 06:32:57 +0800 | [diff] [blame] | 40 | "global.reject-service-keywords[].*", |
| 41 | "global.reject-service-keywords[]", |
Andy Green | aa816e9 | 2018-04-12 15:56:38 +0800 | [diff] [blame] | 42 | "global.default-alpn", |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | enum lejp_global_paths { |
| 46 | LEJPGP_UID, |
| 47 | LEJPGP_GID, |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 48 | LEJPGP_COUNT_THREADS, |
| 49 | LWJPGP_INIT_SSL, |
Andy Green | b21c20b | 2016-04-15 13:33:52 +0800 | [diff] [blame] | 50 | LEJPGP_SERVER_STRING, |
Andy Green | f32d250 | 2016-07-15 13:41:38 +0800 | [diff] [blame] | 51 | LEJPGP_PLUGIN_DIR, |
| 52 | LWJPGP_PINGPONG_SECS, |
Andy Green | 58a26eb | 2017-01-02 19:57:54 +0800 | [diff] [blame] | 53 | LWJPGP_TIMEOUT_SECS, |
Andy Green | 3b93e34 | 2016-10-13 06:32:57 +0800 | [diff] [blame] | 54 | LWJPGP_REJECT_SERVICE_KEYWORDS_NAME, |
Andy Green | aa816e9 | 2018-04-12 15:56:38 +0800 | [diff] [blame] | 55 | LWJPGP_REJECT_SERVICE_KEYWORDS, |
| 56 | LWJPGP_DEFAULT_ALPN, |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static const char * const paths_vhosts[] = { |
| 60 | "vhosts[]", |
| 61 | "vhosts[].mounts[]", |
| 62 | "vhosts[].name", |
| 63 | "vhosts[].port", |
Andy Green | 1042b34 | 2016-04-14 11:50:05 +0800 | [diff] [blame] | 64 | "vhosts[].interface", |
Andy Green | a1ab201 | 2016-04-14 12:18:33 +0800 | [diff] [blame] | 65 | "vhosts[].unix-socket", |
Andy Green | 868b9f2 | 2016-04-14 12:37:21 +0800 | [diff] [blame] | 66 | "vhosts[].sts", |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 67 | "vhosts[].host-ssl-key", |
| 68 | "vhosts[].host-ssl-cert", |
| 69 | "vhosts[].host-ssl-ca", |
Andy Green | 2f0bc93 | 2016-04-15 12:00:23 +0800 | [diff] [blame] | 70 | "vhosts[].access-log", |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 71 | "vhosts[].mounts[].mountpoint", |
| 72 | "vhosts[].mounts[].origin", |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 73 | "vhosts[].mounts[].protocol", |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 74 | "vhosts[].mounts[].default", |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 75 | "vhosts[].mounts[].auth-mask", |
Andy Green | f5efa74 | 2016-04-13 11:42:53 +0800 | [diff] [blame] | 76 | "vhosts[].mounts[].cgi-timeout", |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 77 | "vhosts[].mounts[].cgi-env[].*", |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 78 | "vhosts[].mounts[].cache-max-age", |
| 79 | "vhosts[].mounts[].cache-reuse", |
| 80 | "vhosts[].mounts[].cache-revalidate", |
Andy Green | ee94621 | 2016-12-03 15:13:15 +0800 | [diff] [blame] | 81 | "vhosts[].mounts[].basic-auth", |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 82 | "vhosts[].mounts[].cache-intermediaries", |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 83 | "vhosts[].mounts[].extra-mimetypes.*", |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 84 | "vhosts[].mounts[].interpret.*", |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 85 | "vhosts[].ws-protocols[].*.*", |
| 86 | "vhosts[].ws-protocols[].*", |
Andy Green | d459a6f | 2016-04-08 18:30:45 +0800 | [diff] [blame] | 87 | "vhosts[].ws-protocols[]", |
Andy Green | b46e4a8 | 2016-04-12 16:26:03 +0800 | [diff] [blame] | 88 | "vhosts[].keepalive_timeout", |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 89 | "vhosts[].enable-client-ssl", |
Andy Green | 6954daa | 2016-05-14 09:50:34 +0800 | [diff] [blame] | 90 | "vhosts[].ciphers", |
| 91 | "vhosts[].ecdh-curve", |
Andy Green | 2dc7dde | 2016-06-03 21:19:40 +0800 | [diff] [blame] | 92 | "vhosts[].noipv6", |
Andy Green | 011f915 | 2016-06-04 09:01:28 +0800 | [diff] [blame] | 93 | "vhosts[].ipv6only", |
Andy Green | 1ec8ba8 | 2016-06-10 08:37:26 +0800 | [diff] [blame] | 94 | "vhosts[].ssl-option-set", |
| 95 | "vhosts[].ssl-option-clear", |
Andy Green | cd6a706 | 2016-07-06 07:37:34 +0800 | [diff] [blame] | 96 | "vhosts[].mounts[].pmo[].*", |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 97 | "vhosts[].headers[].*", |
| 98 | "vhosts[].headers[]", |
Joel Winarske | 390ba34 | 2017-02-20 20:53:58 -0800 | [diff] [blame] | 99 | "vhosts[].client-ssl-key", |
| 100 | "vhosts[].client-ssl-cert", |
| 101 | "vhosts[].client-ssl-ca", |
| 102 | "vhosts[].client-ssl-ciphers", |
Andy Green | 7597ac3 | 2017-10-16 16:59:57 +0800 | [diff] [blame] | 103 | "vhosts[].onlyraw", |
Andy Green | c2488dd | 2017-11-07 08:30:57 +0800 | [diff] [blame] | 104 | "vhosts[].client-cert-required", |
Andy Green | a5514d2 | 2017-11-02 06:22:44 +0800 | [diff] [blame] | 105 | "vhosts[].ignore-missing-cert", |
Andy Green | b490079 | 2018-03-07 19:57:34 +0800 | [diff] [blame] | 106 | "vhosts[].error-document-404", |
Andy Green | aa816e9 | 2018-04-12 15:56:38 +0800 | [diff] [blame] | 107 | "vhosts[].alpn", |
Andy Green | 20fb559 | 2018-10-26 09:44:58 +0800 | [diff] [blame] | 108 | "vhosts[].ssl-client-option-set", |
| 109 | "vhosts[].ssl-client-option-clear", |
Andy Green | 93d4e18 | 2018-10-27 08:05:21 +0800 | [diff] [blame] | 110 | "vhosts[].tls13-ciphers", |
| 111 | "vhosts[].client-tls13-ciphers", |
Andy Green | f6ae0ed | 2018-11-13 09:33:13 +0800 | [diff] [blame] | 112 | "vhosts[].strict-host-check", |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 113 | |
| 114 | "vhosts[].listen-accept-role", |
| 115 | "vhosts[].listen-accept-protocol", |
| 116 | "vhosts[].apply-listen-accept", /* deprecates "onlyraw" */ |
| 117 | "vhosts[].fallback-listen-accept", |
| 118 | "vhosts[].allow-non-tls", |
| 119 | "vhosts[].redirect-http", |
| 120 | "vhosts[].allow-http-on-https", |
Andy Green | 3a31c47 | 2019-02-22 14:27:21 +0800 | [diff] [blame] | 121 | |
| 122 | "vhosts[].disable-no-protocol-ws-upgrades", |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | enum lejp_vhost_paths { |
| 126 | LEJPVP, |
| 127 | LEJPVP_MOUNTS, |
| 128 | LEJPVP_NAME, |
| 129 | LEJPVP_PORT, |
Andy Green | 1042b34 | 2016-04-14 11:50:05 +0800 | [diff] [blame] | 130 | LEJPVP_INTERFACE, |
Andy Green | a1ab201 | 2016-04-14 12:18:33 +0800 | [diff] [blame] | 131 | LEJPVP_UNIXSKT, |
Andy Green | 868b9f2 | 2016-04-14 12:37:21 +0800 | [diff] [blame] | 132 | LEJPVP_STS, |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 133 | LEJPVP_HOST_SSL_KEY, |
| 134 | LEJPVP_HOST_SSL_CERT, |
| 135 | LEJPVP_HOST_SSL_CA, |
Andy Green | 2f0bc93 | 2016-04-15 12:00:23 +0800 | [diff] [blame] | 136 | LEJPVP_ACCESS_LOG, |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 137 | LEJPVP_MOUNTPOINT, |
| 138 | LEJPVP_ORIGIN, |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 139 | LEJPVP_MOUNT_PROTOCOL, |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 140 | LEJPVP_DEFAULT, |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 141 | LEJPVP_DEFAULT_AUTH_MASK, |
Andy Green | f5efa74 | 2016-04-13 11:42:53 +0800 | [diff] [blame] | 142 | LEJPVP_CGI_TIMEOUT, |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 143 | LEJPVP_CGI_ENV, |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 144 | LEJPVP_MOUNT_CACHE_MAX_AGE, |
| 145 | LEJPVP_MOUNT_CACHE_REUSE, |
| 146 | LEJPVP_MOUNT_CACHE_REVALIDATE, |
Andy Green | ee94621 | 2016-12-03 15:13:15 +0800 | [diff] [blame] | 147 | LEJPVP_MOUNT_BASIC_AUTH, |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 148 | LEJPVP_MOUNT_CACHE_INTERMEDIARIES, |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 149 | LEJPVP_MOUNT_EXTRA_MIMETYPES, |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 150 | LEJPVP_MOUNT_INTERPRET, |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 151 | LEJPVP_PROTOCOL_NAME_OPT, |
| 152 | LEJPVP_PROTOCOL_NAME, |
| 153 | LEJPVP_PROTOCOL, |
Andy Green | b46e4a8 | 2016-04-12 16:26:03 +0800 | [diff] [blame] | 154 | LEJPVP_KEEPALIVE_TIMEOUT, |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 155 | LEJPVP_ENABLE_CLIENT_SSL, |
Andy Green | 6954daa | 2016-05-14 09:50:34 +0800 | [diff] [blame] | 156 | LEJPVP_CIPHERS, |
| 157 | LEJPVP_ECDH_CURVE, |
Andy Green | 2dc7dde | 2016-06-03 21:19:40 +0800 | [diff] [blame] | 158 | LEJPVP_NOIPV6, |
Andy Green | 011f915 | 2016-06-04 09:01:28 +0800 | [diff] [blame] | 159 | LEJPVP_IPV6ONLY, |
Andy Green | 1ec8ba8 | 2016-06-10 08:37:26 +0800 | [diff] [blame] | 160 | LEJPVP_SSL_OPTION_SET, |
| 161 | LEJPVP_SSL_OPTION_CLEAR, |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 162 | LEJPVP_PMO, |
| 163 | LEJPVP_HEADERS_NAME, |
| 164 | LEJPVP_HEADERS, |
Joel Winarske | 390ba34 | 2017-02-20 20:53:58 -0800 | [diff] [blame] | 165 | LEJPVP_CLIENT_SSL_KEY, |
| 166 | LEJPVP_CLIENT_SSL_CERT, |
| 167 | LEJPVP_CLIENT_SSL_CA, |
| 168 | LEJPVP_CLIENT_CIPHERS, |
Andy Green | 7597ac3 | 2017-10-16 16:59:57 +0800 | [diff] [blame] | 169 | LEJPVP_FLAG_ONLYRAW, |
Andy Green | c2488dd | 2017-11-07 08:30:57 +0800 | [diff] [blame] | 170 | LEJPVP_FLAG_CLIENT_CERT_REQUIRED, |
Andy Green | a5514d2 | 2017-11-02 06:22:44 +0800 | [diff] [blame] | 171 | LEJPVP_IGNORE_MISSING_CERT, |
Andy Green | b490079 | 2018-03-07 19:57:34 +0800 | [diff] [blame] | 172 | LEJPVP_ERROR_DOCUMENT_404, |
Andy Green | aa816e9 | 2018-04-12 15:56:38 +0800 | [diff] [blame] | 173 | LEJPVP_ALPN, |
Andy Green | 20fb559 | 2018-10-26 09:44:58 +0800 | [diff] [blame] | 174 | LEJPVP_SSL_CLIENT_OPTION_SET, |
| 175 | LEJPVP_SSL_CLIENT_OPTION_CLEAR, |
Andy Green | 93d4e18 | 2018-10-27 08:05:21 +0800 | [diff] [blame] | 176 | LEJPVP_TLS13_CIPHERS, |
| 177 | LEJPVP_CLIENT_TLS13_CIPHERS, |
Andy Green | f6ae0ed | 2018-11-13 09:33:13 +0800 | [diff] [blame] | 178 | LEJPVP_FLAG_STRICT_HOST_CHECK, |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 179 | |
| 180 | LEJPVP_LISTEN_ACCEPT_ROLE, |
| 181 | LEJPVP_LISTEN_ACCEPT_PROTOCOL, |
| 182 | LEJPVP_FLAG_APPLY_LISTEN_ACCEPT, |
| 183 | LEJPVP_FLAG_FALLBACK_LISTEN_ACCEPT, |
| 184 | LEJPVP_FLAG_ALLOW_NON_TLS, |
| 185 | LEJPVP_FLAG_REDIRECT_HTTP, |
| 186 | LEJPVP_FLAG_ALLOW_HTTP_ON_HTTPS, |
Andy Green | 3a31c47 | 2019-02-22 14:27:21 +0800 | [diff] [blame] | 187 | |
| 188 | LEJPVP_FLAG_DISABLE_NO_PROTOCOL_WS_UPGRADES, |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 189 | }; |
| 190 | |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 191 | #define MAX_PLUGIN_DIRS 10 |
| 192 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 193 | struct jpargs { |
| 194 | struct lws_context_creation_info *info; |
| 195 | struct lws_context *context; |
| 196 | const struct lws_protocols *protocols; |
Andy Green | 0079099 | 2019-03-10 05:34:02 +0800 | [diff] [blame] | 197 | const struct lws_protocols **pprotocols; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 198 | const struct lws_extension *extensions; |
| 199 | char *p, *end, valid; |
| 200 | struct lws_http_mount *head, *last; |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 201 | |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 202 | struct lws_protocol_vhost_options *pvo; |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 203 | struct lws_protocol_vhost_options *pvo_em; |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 204 | struct lws_protocol_vhost_options *pvo_int; |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 205 | struct lws_http_mount m; |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 206 | const char **plugin_dirs; |
| 207 | int count_plugin_dirs; |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 208 | |
Andy Green | 3a31c47 | 2019-02-22 14:27:21 +0800 | [diff] [blame] | 209 | unsigned int reject_ws_with_no_protocol:1; |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 210 | unsigned int enable_client_ssl:1; |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 211 | unsigned int fresh_mount:1; |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 212 | unsigned int any_vhosts:1; |
Andy Green | 844b779 | 2018-08-24 16:00:33 +0800 | [diff] [blame] | 213 | unsigned int chunk:1; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 214 | }; |
| 215 | |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 216 | static void * |
| 217 | lwsws_align(struct jpargs *a) |
| 218 | { |
Andy Green | 7262e14 | 2017-07-07 08:32:04 +0800 | [diff] [blame] | 219 | if ((lws_intptr_t)(a->p) & 15) |
| 220 | a->p += 16 - ((lws_intptr_t)(a->p) & 15); |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 221 | |
Andy Green | 844b779 | 2018-08-24 16:00:33 +0800 | [diff] [blame] | 222 | a->chunk = 0; |
| 223 | |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 224 | return a->p; |
| 225 | } |
| 226 | |
Andy Green | a1ab201 | 2016-04-14 12:18:33 +0800 | [diff] [blame] | 227 | static int |
| 228 | arg_to_bool(const char *s) |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 229 | { |
| 230 | static const char * const on[] = { "on", "yes", "true" }; |
| 231 | int n = atoi(s); |
| 232 | |
| 233 | if (n) |
| 234 | return 1; |
| 235 | |
Andy Green | d461f46 | 2018-08-16 19:10:32 +0800 | [diff] [blame] | 236 | for (n = 0; n < (int)LWS_ARRAY_SIZE(on); n++) |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 237 | if (!strcasecmp(s, on[n])) |
| 238 | return 1; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 243 | static void |
| 244 | set_reset_flag(unsigned int *p, const char *state, unsigned int flag) |
| 245 | { |
| 246 | if (arg_to_bool(state)) |
| 247 | *p |= flag; |
| 248 | else |
| 249 | *p &= ~(flag); |
| 250 | } |
| 251 | |
Andy Green | 5a0b529 | 2017-10-25 02:53:27 +0800 | [diff] [blame] | 252 | static signed char |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 253 | lejp_globals_cb(struct lejp_ctx *ctx, char reason) |
| 254 | { |
| 255 | struct jpargs *a = (struct jpargs *)ctx->user; |
Andy Green | 3b93e34 | 2016-10-13 06:32:57 +0800 | [diff] [blame] | 256 | struct lws_protocol_vhost_options *rej; |
| 257 | int n; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 258 | |
| 259 | /* we only match on the prepared path strings */ |
| 260 | if (!(reason & LEJP_FLAG_CB_IS_VALUE) || !ctx->path_match) |
| 261 | return 0; |
| 262 | |
Andy Green | 3b93e34 | 2016-10-13 06:32:57 +0800 | [diff] [blame] | 263 | /* this catches, eg, vhosts[].headers[].xxx */ |
| 264 | if (reason == LEJPCB_VAL_STR_END && |
| 265 | ctx->path_match == LWJPGP_REJECT_SERVICE_KEYWORDS_NAME + 1) { |
| 266 | rej = lwsws_align(a); |
| 267 | a->p += sizeof(*rej); |
| 268 | |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 269 | n = lejp_get_wildcard(ctx, 0, a->p, lws_ptr_diff(a->end, a->p)); |
Andy Green | 3b93e34 | 2016-10-13 06:32:57 +0800 | [diff] [blame] | 270 | rej->next = a->info->reject_service_keywords; |
| 271 | a->info->reject_service_keywords = rej; |
| 272 | rej->name = a->p; |
| 273 | lwsl_notice(" adding rej %s=%s\n", a->p, ctx->buf); |
| 274 | a->p += n - 1; |
| 275 | *(a->p++) = '\0'; |
| 276 | rej->value = a->p; |
| 277 | rej->options = NULL; |
| 278 | goto dostring; |
| 279 | } |
| 280 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 281 | switch (ctx->path_match - 1) { |
| 282 | case LEJPGP_UID: |
| 283 | a->info->uid = atoi(ctx->buf); |
| 284 | return 0; |
| 285 | case LEJPGP_GID: |
| 286 | a->info->gid = atoi(ctx->buf); |
| 287 | return 0; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 288 | case LEJPGP_COUNT_THREADS: |
| 289 | a->info->count_threads = atoi(ctx->buf); |
| 290 | return 0; |
| 291 | case LWJPGP_INIT_SSL: |
| 292 | if (arg_to_bool(ctx->buf)) |
| 293 | a->info->options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; |
| 294 | return 0; |
Andy Green | b21c20b | 2016-04-15 13:33:52 +0800 | [diff] [blame] | 295 | case LEJPGP_SERVER_STRING: |
| 296 | a->info->server_string = a->p; |
| 297 | break; |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 298 | case LEJPGP_PLUGIN_DIR: |
| 299 | if (a->count_plugin_dirs == MAX_PLUGIN_DIRS - 1) { |
| 300 | lwsl_err("Too many plugin dirs\n"); |
| 301 | return -1; |
| 302 | } |
| 303 | a->plugin_dirs[a->count_plugin_dirs++] = a->p; |
| 304 | break; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 305 | |
Andy Green | f32d250 | 2016-07-15 13:41:38 +0800 | [diff] [blame] | 306 | case LWJPGP_PINGPONG_SECS: |
| 307 | a->info->ws_ping_pong_interval = atoi(ctx->buf); |
| 308 | return 0; |
| 309 | |
Andy Green | 58a26eb | 2017-01-02 19:57:54 +0800 | [diff] [blame] | 310 | case LWJPGP_TIMEOUT_SECS: |
| 311 | a->info->timeout_secs = atoi(ctx->buf); |
| 312 | return 0; |
| 313 | |
Andy Green | aa816e9 | 2018-04-12 15:56:38 +0800 | [diff] [blame] | 314 | case LWJPGP_DEFAULT_ALPN: |
| 315 | a->info->alpn = a->p; |
| 316 | break; |
| 317 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 318 | default: |
| 319 | return 0; |
| 320 | } |
| 321 | |
Andy Green | 3b93e34 | 2016-10-13 06:32:57 +0800 | [diff] [blame] | 322 | dostring: |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 323 | a->p += lws_snprintf(a->p, a->end - a->p, "%s", ctx->buf); |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 324 | *(a->p)++ = '\0'; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Andy Green | 5a0b529 | 2017-10-25 02:53:27 +0800 | [diff] [blame] | 329 | static signed char |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 330 | lejp_vhosts_cb(struct lejp_ctx *ctx, char reason) |
| 331 | { |
| 332 | struct jpargs *a = (struct jpargs *)ctx->user; |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 333 | struct lws_protocol_vhost_options *pvo, *mp_cgienv, *headers; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 334 | struct lws_http_mount *m; |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 335 | char *p, *p1; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 336 | int n; |
| 337 | |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 338 | #if 0 |
| 339 | lwsl_notice(" %d: %s (%d)\n", reason, ctx->path, ctx->path_match); |
| 340 | for (n = 0; n < ctx->wildcount; n++) |
| 341 | lwsl_notice(" %d\n", ctx->wild[n]); |
| 342 | #endif |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 343 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 344 | if (reason == LEJPCB_OBJECT_START && ctx->path_match == LEJPVP + 1) { |
Andy Green | feeca91 | 2018-03-23 07:50:44 +0800 | [diff] [blame] | 345 | uint32_t i[4]; |
| 346 | const char *ss; |
| 347 | |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 348 | /* set the defaults for this vhost */ |
Andy Green | 3a31c47 | 2019-02-22 14:27:21 +0800 | [diff] [blame] | 349 | a->reject_ws_with_no_protocol = 0; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 350 | a->valid = 1; |
| 351 | a->head = NULL; |
| 352 | a->last = NULL; |
Andy Green | feeca91 | 2018-03-23 07:50:44 +0800 | [diff] [blame] | 353 | |
| 354 | i[0] = a->info->count_threads; |
| 355 | i[1] = a->info->options & ( |
| 356 | LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME | |
| 357 | LWS_SERVER_OPTION_LIBUV | |
| 358 | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | |
| 359 | LWS_SERVER_OPTION_EXPLICIT_VHOSTS | |
| 360 | LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN | |
| 361 | LWS_SERVER_OPTION_LIBEVENT | |
| 362 | LWS_SERVER_OPTION_LIBEV |
| 363 | ); |
| 364 | ss = a->info->server_string; |
| 365 | i[2] = a->info->ws_ping_pong_interval; |
| 366 | i[3] = a->info->timeout_secs; |
| 367 | |
| 368 | memset(a->info, 0, sizeof(*a->info)); |
| 369 | |
| 370 | a->info->count_threads = i[0]; |
| 371 | a->info->options = i[1]; |
| 372 | a->info->server_string = ss; |
| 373 | a->info->ws_ping_pong_interval = i[2]; |
| 374 | a->info->timeout_secs = i[3]; |
| 375 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 376 | a->info->protocols = a->protocols; |
Andy Green | 0079099 | 2019-03-10 05:34:02 +0800 | [diff] [blame] | 377 | a->info->pprotocols = a->pprotocols; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 378 | a->info->extensions = a->extensions; |
Andy Green | 126be3c | 2018-04-11 13:39:42 +0800 | [diff] [blame] | 379 | #if defined(LWS_WITH_TLS) |
Joel Winarske | 390ba34 | 2017-02-20 20:53:58 -0800 | [diff] [blame] | 380 | a->info->client_ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:" |
| 381 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 382 | "DHE-RSA-AES256-GCM-SHA384:" |
| 383 | "ECDHE-RSA-AES256-SHA384:" |
| 384 | "HIGH:!aNULL:!eNULL:!EXPORT:" |
| 385 | "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:" |
| 386 | "!SHA1:!DHE-RSA-AES128-GCM-SHA256:" |
| 387 | "!DHE-RSA-AES128-SHA256:" |
| 388 | "!AES128-GCM-SHA256:" |
| 389 | "!AES128-SHA256:" |
| 390 | "!DHE-RSA-AES256-SHA256:" |
| 391 | "!AES256-GCM-SHA384:" |
| 392 | "!AES256-SHA256"; |
Anzey | a903d73 | 2018-01-04 10:42:07 +0800 | [diff] [blame] | 393 | #endif |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 394 | a->info->ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:" |
| 395 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 396 | "DHE-RSA-AES256-GCM-SHA384:" |
| 397 | "ECDHE-RSA-AES256-SHA384:" |
| 398 | "HIGH:!aNULL:!eNULL:!EXPORT:" |
| 399 | "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:" |
| 400 | "!SHA1:!DHE-RSA-AES128-GCM-SHA256:" |
| 401 | "!DHE-RSA-AES128-SHA256:" |
| 402 | "!AES128-GCM-SHA256:" |
| 403 | "!AES128-SHA256:" |
| 404 | "!DHE-RSA-AES256-SHA256:" |
| 405 | "!AES256-GCM-SHA384:" |
| 406 | "!AES256-SHA256"; |
Andy Green | 7acf76c | 2016-07-23 14:18:25 +0800 | [diff] [blame] | 407 | a->info->keepalive_timeout = 5; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (reason == LEJPCB_OBJECT_START && |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 411 | ctx->path_match == LEJPVP_MOUNTS + 1) { |
| 412 | a->fresh_mount = 1; |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 413 | memset(&a->m, 0, sizeof(a->m)); |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 414 | } |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 415 | |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 416 | /* this catches, eg, vhosts[].ws-protocols[].xxx-protocol */ |
| 417 | if (reason == LEJPCB_OBJECT_START && |
| 418 | ctx->path_match == LEJPVP_PROTOCOL_NAME + 1) { |
| 419 | a->pvo = lwsws_align(a); |
| 420 | a->p += sizeof(*a->pvo); |
| 421 | |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 422 | n = lejp_get_wildcard(ctx, 0, a->p, lws_ptr_diff(a->end, a->p)); |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 423 | /* ie, enable this protocol, no options yet */ |
| 424 | a->pvo->next = a->info->pvo; |
| 425 | a->info->pvo = a->pvo; |
| 426 | a->pvo->name = a->p; |
Andy Green | 09f59ba | 2017-10-29 16:22:54 +0800 | [diff] [blame] | 427 | lwsl_info(" adding protocol %s\n", a->p); |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 428 | a->p += n; |
| 429 | a->pvo->value = a->p; |
| 430 | a->pvo->options = NULL; |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 431 | goto dostring; |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 432 | } |
| 433 | |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 434 | /* this catches, eg, vhosts[].headers[].xxx */ |
Andy Green | 844b779 | 2018-08-24 16:00:33 +0800 | [diff] [blame] | 435 | if ((reason == LEJPCB_VAL_STR_END || reason == LEJPCB_VAL_STR_CHUNK) && |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 436 | ctx->path_match == LEJPVP_HEADERS_NAME + 1) { |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 437 | |
Andy Green | 844b779 | 2018-08-24 16:00:33 +0800 | [diff] [blame] | 438 | if (!a->chunk) { |
| 439 | headers = lwsws_align(a); |
| 440 | a->p += sizeof(*headers); |
| 441 | |
| 442 | n = lejp_get_wildcard(ctx, 0, a->p, |
| 443 | lws_ptr_diff(a->end, a->p)); |
| 444 | /* ie, add this header */ |
| 445 | headers->next = a->info->headers; |
| 446 | a->info->headers = headers; |
| 447 | headers->name = a->p; |
| 448 | |
| 449 | lwsl_notice(" adding header %s=%s\n", a->p, ctx->buf); |
| 450 | a->p += n - 1; |
| 451 | *(a->p++) = ':'; |
| 452 | if (a->p < a->end) |
| 453 | *(a->p++) = '\0'; |
| 454 | else |
| 455 | *(a->p - 1) = '\0'; |
| 456 | headers->value = a->p; |
| 457 | headers->options = NULL; |
| 458 | } |
| 459 | a->chunk = reason == LEJPCB_VAL_STR_CHUNK; |
Andy Green | e35d91a | 2016-08-27 17:07:06 +0800 | [diff] [blame] | 460 | goto dostring; |
| 461 | } |
| 462 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 463 | if (reason == LEJPCB_OBJECT_END && |
| 464 | (ctx->path_match == LEJPVP + 1 || !ctx->path[0]) && |
| 465 | a->valid) { |
| 466 | |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 467 | struct lws_vhost *vhost; |
| 468 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 469 | //lwsl_notice("%s\n", ctx->path); |
Andy Green | f44e38f | 2018-08-02 19:13:53 +0800 | [diff] [blame] | 470 | if (!a->info->port && |
| 471 | !(a->info->options & LWS_SERVER_OPTION_UNIX_SOCK)) { |
| 472 | lwsl_err("Port required (eg, 443)\n"); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 473 | return 1; |
| 474 | } |
| 475 | a->valid = 0; |
Andy Green | 4664f71 | 2016-05-02 04:59:54 +0800 | [diff] [blame] | 476 | a->info->mounts = a->head; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 477 | |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 478 | vhost = lws_create_vhost(a->context, a->info); |
| 479 | if (!vhost) { |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 480 | lwsl_err("Failed to create vhost %s\n", |
| 481 | a->info->vhost_name); |
| 482 | return 1; |
| 483 | } |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 484 | a->any_vhosts = 1; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 485 | |
Andy Green | 3a31c47 | 2019-02-22 14:27:21 +0800 | [diff] [blame] | 486 | if (a->reject_ws_with_no_protocol) { |
| 487 | a->reject_ws_with_no_protocol = 0; |
| 488 | |
| 489 | vhost->default_protocol_index = 255; |
| 490 | } |
| 491 | |
Andy Green | 126be3c | 2018-04-11 13:39:42 +0800 | [diff] [blame] | 492 | #if defined(LWS_WITH_TLS) |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 493 | if (a->enable_client_ssl) { |
Andy Green | 9bed6d6 | 2018-11-23 08:47:56 +0800 | [diff] [blame] | 494 | const char *cert_filepath = |
| 495 | a->info->client_ssl_cert_filepath; |
| 496 | const char *private_key_filepath = |
| 497 | a->info->client_ssl_private_key_filepath; |
| 498 | const char *ca_filepath = |
| 499 | a->info->client_ssl_ca_filepath; |
| 500 | const char *cipher_list = |
| 501 | a->info->client_ssl_cipher_list; |
| 502 | |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 503 | memset(a->info, 0, sizeof(*a->info)); |
Joel Winarske | 390ba34 | 2017-02-20 20:53:58 -0800 | [diff] [blame] | 504 | a->info->client_ssl_cert_filepath = cert_filepath; |
Andy Green | 9bed6d6 | 2018-11-23 08:47:56 +0800 | [diff] [blame] | 505 | a->info->client_ssl_private_key_filepath = |
| 506 | private_key_filepath; |
Joel Winarske | 390ba34 | 2017-02-20 20:53:58 -0800 | [diff] [blame] | 507 | a->info->client_ssl_ca_filepath = ca_filepath; |
| 508 | a->info->client_ssl_cipher_list = cipher_list; |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 509 | a->info->options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; |
| 510 | lws_init_vhost_client_ssl(a->info, vhost); |
| 511 | } |
Anzey | a903d73 | 2018-01-04 10:42:07 +0800 | [diff] [blame] | 512 | #endif |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 513 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | if (reason == LEJPCB_OBJECT_END && |
| 518 | ctx->path_match == LEJPVP_MOUNTS + 1) { |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 519 | static const char * const mount_protocols[] = { |
| 520 | "http://", |
| 521 | "https://", |
| 522 | "file://", |
| 523 | "cgi://", |
| 524 | ">http://", |
| 525 | ">https://", |
Andy Green | 19cc7ac | 2017-03-03 12:38:10 +0800 | [diff] [blame] | 526 | "callback://", |
| 527 | "gzip://", |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 528 | }; |
| 529 | |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 530 | if (!a->fresh_mount) |
| 531 | return 0; |
| 532 | |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 533 | if (!a->m.mountpoint || !a->m.origin) { |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 534 | lwsl_err("mountpoint and origin required\n"); |
| 535 | return 1; |
| 536 | } |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 537 | lwsl_debug("adding mount %s\n", a->m.mountpoint); |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 538 | m = lwsws_align(a); |
| 539 | memcpy(m, &a->m, sizeof(*m)); |
| 540 | if (a->last) |
| 541 | a->last->mount_next = m; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 542 | |
Andy Green | d461f46 | 2018-08-16 19:10:32 +0800 | [diff] [blame] | 543 | for (n = 0; n < (int)LWS_ARRAY_SIZE(mount_protocols); n++) |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 544 | if (!strncmp(a->m.origin, mount_protocols[n], |
| 545 | strlen(mount_protocols[n]))) { |
Andy Green | 904a9c0 | 2017-10-13 10:33:02 +0800 | [diff] [blame] | 546 | lwsl_info("----%s\n", a->m.origin); |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 547 | m->origin_protocol = n; |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 548 | m->origin = a->m.origin + |
| 549 | strlen(mount_protocols[n]); |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | |
Andy Green | d461f46 | 2018-08-16 19:10:32 +0800 | [diff] [blame] | 553 | if (n == (int)LWS_ARRAY_SIZE(mount_protocols)) { |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 554 | lwsl_err("unsupported protocol:// %s\n", a->m.origin); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 555 | return 1; |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | a->p += sizeof(*m); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 559 | if (!a->head) |
| 560 | a->head = m; |
| 561 | |
| 562 | a->last = m; |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 563 | a->fresh_mount = 0; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* we only match on the prepared path strings */ |
| 567 | if (!(reason & LEJP_FLAG_CB_IS_VALUE) || !ctx->path_match) |
| 568 | return 0; |
| 569 | |
| 570 | switch (ctx->path_match - 1) { |
| 571 | case LEJPVP_NAME: |
| 572 | a->info->vhost_name = a->p; |
| 573 | break; |
| 574 | case LEJPVP_PORT: |
| 575 | a->info->port = atoi(ctx->buf); |
| 576 | return 0; |
Andy Green | 1042b34 | 2016-04-14 11:50:05 +0800 | [diff] [blame] | 577 | case LEJPVP_INTERFACE: |
| 578 | a->info->iface = a->p; |
| 579 | break; |
Andy Green | a1ab201 | 2016-04-14 12:18:33 +0800 | [diff] [blame] | 580 | case LEJPVP_UNIXSKT: |
| 581 | if (arg_to_bool(ctx->buf)) |
| 582 | a->info->options |= LWS_SERVER_OPTION_UNIX_SOCK; |
| 583 | else |
| 584 | a->info->options &= ~(LWS_SERVER_OPTION_UNIX_SOCK); |
| 585 | return 0; |
Andy Green | 868b9f2 | 2016-04-14 12:37:21 +0800 | [diff] [blame] | 586 | case LEJPVP_STS: |
| 587 | if (arg_to_bool(ctx->buf)) |
| 588 | a->info->options |= LWS_SERVER_OPTION_STS; |
| 589 | else |
| 590 | a->info->options &= ~(LWS_SERVER_OPTION_STS); |
| 591 | return 0; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 592 | case LEJPVP_HOST_SSL_KEY: |
| 593 | a->info->ssl_private_key_filepath = a->p; |
| 594 | break; |
| 595 | case LEJPVP_HOST_SSL_CERT: |
| 596 | a->info->ssl_cert_filepath = a->p; |
| 597 | break; |
| 598 | case LEJPVP_HOST_SSL_CA: |
| 599 | a->info->ssl_ca_filepath = a->p; |
| 600 | break; |
Andy Green | 2f0bc93 | 2016-04-15 12:00:23 +0800 | [diff] [blame] | 601 | case LEJPVP_ACCESS_LOG: |
| 602 | a->info->log_filepath = a->p; |
| 603 | break; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 604 | case LEJPVP_MOUNTPOINT: |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 605 | a->m.mountpoint = a->p; |
Andy Green | 7a9970f | 2016-05-15 08:29:37 +0800 | [diff] [blame] | 606 | a->m.mountpoint_len = (unsigned char)strlen(ctx->buf); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 607 | break; |
| 608 | case LEJPVP_ORIGIN: |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 609 | if (!strncmp(ctx->buf, "callback://", 11)) |
| 610 | a->m.protocol = a->p + 11; |
| 611 | |
| 612 | if (!a->m.origin) |
| 613 | a->m.origin = a->p; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 614 | break; |
| 615 | case LEJPVP_DEFAULT: |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 616 | a->m.def = a->p; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 617 | break; |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 618 | case LEJPVP_DEFAULT_AUTH_MASK: |
| 619 | a->m.auth_mask = atoi(ctx->buf); |
| 620 | return 0; |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 621 | case LEJPVP_MOUNT_CACHE_MAX_AGE: |
| 622 | a->m.cache_max_age = atoi(ctx->buf); |
| 623 | return 0; |
| 624 | case LEJPVP_MOUNT_CACHE_REUSE: |
| 625 | a->m.cache_reusable = arg_to_bool(ctx->buf); |
| 626 | return 0; |
| 627 | case LEJPVP_MOUNT_CACHE_REVALIDATE: |
| 628 | a->m.cache_revalidate = arg_to_bool(ctx->buf); |
| 629 | return 0; |
| 630 | case LEJPVP_MOUNT_CACHE_INTERMEDIARIES: |
| 631 | a->m.cache_intermediaries = arg_to_bool(ctx->buf);; |
| 632 | return 0; |
Andy Green | ee94621 | 2016-12-03 15:13:15 +0800 | [diff] [blame] | 633 | case LEJPVP_MOUNT_BASIC_AUTH: |
| 634 | a->m.basic_auth_login_file = a->p; |
| 635 | break; |
Andy Green | f5efa74 | 2016-04-13 11:42:53 +0800 | [diff] [blame] | 636 | case LEJPVP_CGI_TIMEOUT: |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 637 | a->m.cgi_timeout = atoi(ctx->buf); |
Andy Green | f5efa74 | 2016-04-13 11:42:53 +0800 | [diff] [blame] | 638 | return 0; |
Andy Green | b46e4a8 | 2016-04-12 16:26:03 +0800 | [diff] [blame] | 639 | case LEJPVP_KEEPALIVE_TIMEOUT: |
| 640 | a->info->keepalive_timeout = atoi(ctx->buf); |
| 641 | return 0; |
Andy Green | 126be3c | 2018-04-11 13:39:42 +0800 | [diff] [blame] | 642 | #if defined(LWS_WITH_TLS) |
Joel Winarske | 390ba34 | 2017-02-20 20:53:58 -0800 | [diff] [blame] | 643 | case LEJPVP_CLIENT_CIPHERS: |
| 644 | a->info->client_ssl_cipher_list = a->p; |
| 645 | break; |
Anzey | a903d73 | 2018-01-04 10:42:07 +0800 | [diff] [blame] | 646 | #endif |
Andy Green | 6954daa | 2016-05-14 09:50:34 +0800 | [diff] [blame] | 647 | case LEJPVP_CIPHERS: |
| 648 | a->info->ssl_cipher_list = a->p; |
| 649 | break; |
Andy Green | 93d4e18 | 2018-10-27 08:05:21 +0800 | [diff] [blame] | 650 | case LEJPVP_TLS13_CIPHERS: |
| 651 | a->info->tls1_3_plus_cipher_list = a->p; |
| 652 | break; |
| 653 | case LEJPVP_CLIENT_TLS13_CIPHERS: |
| 654 | a->info->client_tls_1_3_plus_cipher_list = a->p; |
| 655 | break; |
| 656 | |
Andy Green | 6954daa | 2016-05-14 09:50:34 +0800 | [diff] [blame] | 657 | case LEJPVP_ECDH_CURVE: |
| 658 | a->info->ecdh_curve = a->p; |
| 659 | break; |
Andy Green | cd6a706 | 2016-07-06 07:37:34 +0800 | [diff] [blame] | 660 | case LEJPVP_PMO: |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 661 | case LEJPVP_CGI_ENV: |
| 662 | mp_cgienv = lwsws_align(a); |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 663 | a->p += sizeof(*a->m.cgienv); |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 664 | |
Andy Green | 42e8b18 | 2016-04-22 08:53:49 +0800 | [diff] [blame] | 665 | mp_cgienv->next = a->m.cgienv; |
| 666 | a->m.cgienv = mp_cgienv; |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 667 | |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 668 | n = lejp_get_wildcard(ctx, 0, a->p, lws_ptr_diff(a->end, a->p)); |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 669 | mp_cgienv->name = a->p; |
| 670 | a->p += n; |
| 671 | mp_cgienv->value = a->p; |
| 672 | mp_cgienv->options = NULL; |
Andy Green | 9bed6d6 | 2018-11-23 08:47:56 +0800 | [diff] [blame] | 673 | //lwsl_notice(" adding pmo / cgi-env '%s' = '%s'\n", |
| 674 | // mp_cgienv->name, mp_cgienv->value); |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 675 | goto dostring; |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 676 | |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 677 | case LEJPVP_PROTOCOL_NAME_OPT: |
| 678 | /* this catches, eg, |
| 679 | * vhosts[].ws-protocols[].xxx-protocol.yyy-option |
| 680 | * ie, these are options attached to a protocol with { } |
| 681 | */ |
| 682 | pvo = lwsws_align(a); |
| 683 | a->p += sizeof(*a->pvo); |
| 684 | |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 685 | n = lejp_get_wildcard(ctx, 1, a->p, lws_ptr_diff(a->end, a->p)); |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 686 | /* ie, enable this protocol, no options yet */ |
| 687 | pvo->next = a->pvo->options; |
| 688 | a->pvo->options = pvo; |
| 689 | pvo->name = a->p; |
| 690 | a->p += n; |
| 691 | pvo->value = a->p; |
| 692 | pvo->options = NULL; |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 693 | break; |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 694 | |
| 695 | case LEJPVP_MOUNT_EXTRA_MIMETYPES: |
| 696 | a->pvo_em = lwsws_align(a); |
| 697 | a->p += sizeof(*a->pvo_em); |
| 698 | |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 699 | n = lejp_get_wildcard(ctx, 0, a->p, lws_ptr_diff(a->end, a->p)); |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 700 | /* ie, enable this protocol, no options yet */ |
| 701 | a->pvo_em->next = a->m.extra_mimetypes; |
| 702 | a->m.extra_mimetypes = a->pvo_em; |
| 703 | a->pvo_em->name = a->p; |
Andy Green | 9bed6d6 | 2018-11-23 08:47:56 +0800 | [diff] [blame] | 704 | lwsl_notice(" + extra-mimetypes %s -> %s\n", a->p, ctx->buf); |
Andy Green | 8ef14c0 | 2016-05-14 10:00:21 +0800 | [diff] [blame] | 705 | a->p += n; |
| 706 | a->pvo_em->value = a->p; |
| 707 | a->pvo_em->options = NULL; |
| 708 | break; |
| 709 | |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 710 | case LEJPVP_MOUNT_INTERPRET: |
| 711 | a->pvo_int = lwsws_align(a); |
| 712 | a->p += sizeof(*a->pvo_int); |
| 713 | |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 714 | n = lejp_get_wildcard(ctx, 0, a->p, lws_ptr_diff(a->end, a->p)); |
Andy Green | 7a2fc44 | 2016-05-19 15:28:31 +0800 | [diff] [blame] | 715 | /* ie, enable this protocol, no options yet */ |
| 716 | a->pvo_int->next = a->m.interpret; |
| 717 | a->m.interpret = a->pvo_int; |
| 718 | a->pvo_int->name = a->p; |
| 719 | lwsl_notice(" adding interpret %s -> %s\n", a->p, |
| 720 | ctx->buf); |
| 721 | a->p += n; |
| 722 | a->pvo_int->value = a->p; |
| 723 | a->pvo_int->options = NULL; |
| 724 | break; |
| 725 | |
Andy Green | fb8be05 | 2016-05-12 19:39:29 +0800 | [diff] [blame] | 726 | case LEJPVP_ENABLE_CLIENT_SSL: |
| 727 | a->enable_client_ssl = arg_to_bool(ctx->buf); |
| 728 | return 0; |
Andy Green | 126be3c | 2018-04-11 13:39:42 +0800 | [diff] [blame] | 729 | #if defined(LWS_WITH_TLS) |
Joel Winarske | 390ba34 | 2017-02-20 20:53:58 -0800 | [diff] [blame] | 730 | case LEJPVP_CLIENT_SSL_KEY: |
| 731 | a->info->client_ssl_private_key_filepath = a->p; |
| 732 | break; |
| 733 | case LEJPVP_CLIENT_SSL_CERT: |
| 734 | a->info->client_ssl_cert_filepath = a->p; |
| 735 | break; |
| 736 | case LEJPVP_CLIENT_SSL_CA: |
| 737 | a->info->client_ssl_ca_filepath = a->p; |
| 738 | break; |
Anzey | a903d73 | 2018-01-04 10:42:07 +0800 | [diff] [blame] | 739 | #endif |
Andy Green | 37098ae | 2016-04-08 13:25:34 +0800 | [diff] [blame] | 740 | |
Andy Green | 2dc7dde | 2016-06-03 21:19:40 +0800 | [diff] [blame] | 741 | case LEJPVP_NOIPV6: |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 742 | set_reset_flag(&a->info->options, ctx->buf, |
| 743 | LWS_SERVER_OPTION_DISABLE_IPV6); |
Andy Green | 2dc7dde | 2016-06-03 21:19:40 +0800 | [diff] [blame] | 744 | return 0; |
| 745 | |
Andy Green | 7597ac3 | 2017-10-16 16:59:57 +0800 | [diff] [blame] | 746 | case LEJPVP_FLAG_ONLYRAW: |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 747 | set_reset_flag(&a->info->options, ctx->buf, |
| 748 | LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG); |
Andy Green | 7597ac3 | 2017-10-16 16:59:57 +0800 | [diff] [blame] | 749 | return 0; |
| 750 | |
Andy Green | 011f915 | 2016-06-04 09:01:28 +0800 | [diff] [blame] | 751 | case LEJPVP_IPV6ONLY: |
| 752 | a->info->options |= LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY; |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 753 | set_reset_flag(&a->info->options, ctx->buf, |
| 754 | LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE); |
Andy Green | 011f915 | 2016-06-04 09:01:28 +0800 | [diff] [blame] | 755 | return 0; |
| 756 | |
Andy Green | c2488dd | 2017-11-07 08:30:57 +0800 | [diff] [blame] | 757 | case LEJPVP_FLAG_CLIENT_CERT_REQUIRED: |
| 758 | if (arg_to_bool(ctx->buf)) |
| 759 | a->info->options |= |
| 760 | LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT; |
| 761 | return 0; |
| 762 | |
Andy Green | a5514d2 | 2017-11-02 06:22:44 +0800 | [diff] [blame] | 763 | case LEJPVP_IGNORE_MISSING_CERT: |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 764 | set_reset_flag(&a->info->options, ctx->buf, |
| 765 | LWS_SERVER_OPTION_IGNORE_MISSING_CERT); |
Andy Green | a5514d2 | 2017-11-02 06:22:44 +0800 | [diff] [blame] | 766 | return 0; |
| 767 | |
Andy Green | f6ae0ed | 2018-11-13 09:33:13 +0800 | [diff] [blame] | 768 | case LEJPVP_FLAG_STRICT_HOST_CHECK: |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 769 | set_reset_flag(&a->info->options, ctx->buf, |
| 770 | LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK); |
Andy Green | f6ae0ed | 2018-11-13 09:33:13 +0800 | [diff] [blame] | 771 | return 0; |
| 772 | |
Andy Green | b490079 | 2018-03-07 19:57:34 +0800 | [diff] [blame] | 773 | case LEJPVP_ERROR_DOCUMENT_404: |
| 774 | a->info->error_document_404 = a->p; |
| 775 | break; |
| 776 | |
Andy Green | 1ec8ba8 | 2016-06-10 08:37:26 +0800 | [diff] [blame] | 777 | case LEJPVP_SSL_OPTION_SET: |
| 778 | a->info->ssl_options_set |= atol(ctx->buf); |
| 779 | return 0; |
| 780 | case LEJPVP_SSL_OPTION_CLEAR: |
| 781 | a->info->ssl_options_clear |= atol(ctx->buf); |
| 782 | return 0; |
| 783 | |
Andy Green | 20fb559 | 2018-10-26 09:44:58 +0800 | [diff] [blame] | 784 | case LEJPVP_SSL_CLIENT_OPTION_SET: |
| 785 | a->info->ssl_client_options_set |= atol(ctx->buf); |
| 786 | return 0; |
| 787 | case LEJPVP_SSL_CLIENT_OPTION_CLEAR: |
| 788 | a->info->ssl_client_options_clear |= atol(ctx->buf); |
| 789 | return 0; |
| 790 | |
Andy Green | aa816e9 | 2018-04-12 15:56:38 +0800 | [diff] [blame] | 791 | case LEJPVP_ALPN: |
| 792 | a->info->alpn = a->p; |
| 793 | break; |
| 794 | |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 795 | case LEJPVP_LISTEN_ACCEPT_ROLE: |
| 796 | a->info->listen_accept_role = a->p; |
| 797 | break; |
| 798 | case LEJPVP_LISTEN_ACCEPT_PROTOCOL: |
| 799 | a->info->listen_accept_protocol = a->p; |
| 800 | break; |
| 801 | |
| 802 | case LEJPVP_FLAG_APPLY_LISTEN_ACCEPT: |
| 803 | set_reset_flag(&a->info->options, ctx->buf, |
| 804 | LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG); |
| 805 | return 0; |
| 806 | case LEJPVP_FLAG_FALLBACK_LISTEN_ACCEPT: |
Andy Green | 08b5ad9 | 2018-11-29 08:29:48 +0800 | [diff] [blame] | 807 | lwsl_notice("vh %s: LEJPVP_FLAG_FALLBACK_LISTEN_ACCEPT: %s\n", |
| 808 | a->info->vhost_name, ctx->buf); |
Andy Green | b318877 | 2018-11-29 08:47:49 +0800 | [diff] [blame] | 809 | set_reset_flag(&a->info->options, ctx->buf, |
| 810 | LWS_SERVER_OPTION_FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG); |
| 811 | return 0; |
| 812 | case LEJPVP_FLAG_ALLOW_NON_TLS: |
| 813 | set_reset_flag(&a->info->options, ctx->buf, |
| 814 | LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT); |
| 815 | return 0; |
| 816 | case LEJPVP_FLAG_REDIRECT_HTTP: |
| 817 | set_reset_flag(&a->info->options, ctx->buf, |
| 818 | LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS); |
| 819 | return 0; |
| 820 | case LEJPVP_FLAG_ALLOW_HTTP_ON_HTTPS: |
| 821 | set_reset_flag(&a->info->options, ctx->buf, |
| 822 | LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER); |
| 823 | return 0; |
| 824 | |
Andy Green | 3a31c47 | 2019-02-22 14:27:21 +0800 | [diff] [blame] | 825 | case LEJPVP_FLAG_DISABLE_NO_PROTOCOL_WS_UPGRADES: |
| 826 | a->reject_ws_with_no_protocol = 1; |
| 827 | return 0; |
| 828 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 829 | default: |
| 830 | return 0; |
| 831 | } |
| 832 | |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 833 | dostring: |
| 834 | p = ctx->buf; |
Andy Green | 844b779 | 2018-08-24 16:00:33 +0800 | [diff] [blame] | 835 | p[LEJP_STRING_CHUNK] = '\0'; |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 836 | p1 = strstr(p, ESC_INSTALL_DATADIR); |
| 837 | if (p1) { |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 838 | n = lws_ptr_diff(p1, p); |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 839 | if (n > a->end - a->p) |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 840 | n = lws_ptr_diff(a->end, a->p); |
Andy Green | aae2c24 | 2018-03-12 09:28:26 +0800 | [diff] [blame] | 841 | lws_strncpy(a->p, p, n + 1); |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 842 | a->p += n; |
Andy Green | 9bed6d6 | 2018-11-23 08:47:56 +0800 | [diff] [blame] | 843 | a->p += lws_snprintf(a->p, a->end - a->p, "%s", |
| 844 | LWS_INSTALL_DATADIR); |
Andy Green | 0aa382f | 2016-07-06 10:48:41 +0800 | [diff] [blame] | 845 | p += n + strlen(ESC_INSTALL_DATADIR); |
| 846 | } |
| 847 | |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 848 | a->p += lws_snprintf(a->p, a->end - a->p, "%s", p); |
Andy Green | 844b779 | 2018-08-24 16:00:33 +0800 | [diff] [blame] | 849 | if (reason == LEJPCB_VAL_STR_END) |
| 850 | *(a->p)++ = '\0'; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * returns 0 = OK, 1 = can't open, 2 = parsing error |
| 857 | */ |
| 858 | |
| 859 | static int |
| 860 | lwsws_get_config(void *user, const char *f, const char * const *paths, |
| 861 | int count_paths, lejp_callback cb) |
| 862 | { |
| 863 | unsigned char buf[128]; |
| 864 | struct lejp_ctx ctx; |
| 865 | int n, m, fd; |
| 866 | |
Andy Green | f2f9685 | 2018-06-23 12:56:21 +0800 | [diff] [blame] | 867 | fd = lws_open(f, O_RDONLY); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 868 | if (fd < 0) { |
| 869 | lwsl_err("Cannot open %s\n", f); |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 870 | return 2; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 871 | } |
| 872 | lwsl_info("%s: %s\n", __func__, f); |
| 873 | lejp_construct(&ctx, cb, user, paths, count_paths); |
| 874 | |
| 875 | do { |
| 876 | n = read(fd, buf, sizeof(buf)); |
| 877 | if (!n) |
| 878 | break; |
| 879 | |
Andy Green | 6e7b79b | 2016-04-09 09:32:01 +0800 | [diff] [blame] | 880 | m = (int)(signed char)lejp_parse(&ctx, buf, n); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 881 | } while (m == LEJP_CONTINUE); |
| 882 | |
| 883 | close(fd); |
| 884 | n = ctx.line; |
| 885 | lejp_destruct(&ctx); |
| 886 | |
| 887 | if (m < 0) { |
Andy Green | 4bd5b96 | 2016-06-18 06:36:37 +0800 | [diff] [blame] | 888 | lwsl_err("%s(%u): parsing error %d: %s\n", f, n, m, |
Andy Green | 6a88483 | 2019-03-12 07:54:27 +0800 | [diff] [blame^] | 889 | lejp_error_to_string(m)); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 890 | return 2; |
| 891 | } |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
Andy Green | fc995df | 2017-09-28 11:29:03 +0800 | [diff] [blame] | 896 | #if defined(LWS_WITH_LIBUV) && UV_VERSION_MAJOR > 0 |
Andy Green | 0a18354 | 2016-04-09 07:22:40 +0800 | [diff] [blame] | 897 | |
| 898 | static int |
| 899 | lwsws_get_config_d(void *user, const char *d, const char * const *paths, |
| 900 | int count_paths, lejp_callback cb) |
| 901 | { |
| 902 | uv_dirent_t dent; |
| 903 | uv_fs_t req; |
| 904 | char path[256]; |
Andy Green | be9fb91 | 2016-12-16 07:37:43 +0800 | [diff] [blame] | 905 | int ret = 0, ir; |
Andy Green | 0a18354 | 2016-04-09 07:22:40 +0800 | [diff] [blame] | 906 | uv_loop_t loop; |
| 907 | |
Andy Green | be9fb91 | 2016-12-16 07:37:43 +0800 | [diff] [blame] | 908 | ir = uv_loop_init(&loop); |
| 909 | if (ir) { |
| 910 | lwsl_err("%s: loop init failed %d\n", __func__, ir); |
| 911 | } |
Andy Green | 0a18354 | 2016-04-09 07:22:40 +0800 | [diff] [blame] | 912 | |
| 913 | if (!uv_fs_scandir(&loop, &req, d, 0, NULL)) { |
| 914 | lwsl_err("Scandir on %s failed\n", d); |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 915 | return 2; |
Andy Green | 0a18354 | 2016-04-09 07:22:40 +0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | while (uv_fs_scandir_next(&req, &dent) != UV_EOF) { |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 919 | lws_snprintf(path, sizeof(path) - 1, "%s/%s", d, dent.name); |
Andy Green | 0a18354 | 2016-04-09 07:22:40 +0800 | [diff] [blame] | 920 | ret = lwsws_get_config(user, path, paths, count_paths, cb); |
| 921 | if (ret) |
| 922 | goto bail; |
| 923 | } |
| 924 | |
| 925 | bail: |
| 926 | uv_fs_req_cleanup(&req); |
Andy Green | 2d313bd | 2017-07-19 14:28:53 +0800 | [diff] [blame] | 927 | while (uv_loop_close(&loop)) |
| 928 | ; |
Andy Green | 0a18354 | 2016-04-09 07:22:40 +0800 | [diff] [blame] | 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | #else |
| 934 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 935 | #ifndef _WIN32 |
| 936 | static int filter(const struct dirent *ent) |
| 937 | { |
| 938 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
| 939 | return 0; |
| 940 | |
| 941 | return 1; |
| 942 | } |
| 943 | #endif |
| 944 | |
| 945 | static int |
| 946 | lwsws_get_config_d(void *user, const char *d, const char * const *paths, |
| 947 | int count_paths, lejp_callback cb) |
| 948 | { |
negativekelvin | 9b35bc1 | 2018-09-30 12:01:56 -0700 | [diff] [blame] | 949 | #if !defined(_WIN32) && !defined(LWS_WITH_ESP32) |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 950 | struct dirent **namelist; |
| 951 | char path[256]; |
| 952 | int n, i, ret = 0; |
| 953 | |
pblemel | 39e19c8 | 2019-01-11 16:48:53 +0800 | [diff] [blame] | 954 | n = scandir((char *) d, &namelist, filter, alphasort); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 955 | if (n < 0) { |
Andy Green | ce37ee9 | 2017-02-05 22:07:34 +0800 | [diff] [blame] | 956 | lwsl_err("Scandir on %s failed\n", d); |
Andy Green | 6f3f2df | 2017-10-12 07:03:44 +0800 | [diff] [blame] | 957 | return 1; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | for (i = 0; i < n; i++) { |
Andy Green | 6f3f2df | 2017-10-12 07:03:44 +0800 | [diff] [blame] | 961 | if (strchr(namelist[i]->d_name, '~')) |
| 962 | goto skip; |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 963 | lws_snprintf(path, sizeof(path) - 1, "%s/%s", d, |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 964 | namelist[i]->d_name); |
| 965 | ret = lwsws_get_config(user, path, paths, count_paths, cb); |
| 966 | if (ret) { |
| 967 | while (i++ < n) |
| 968 | free(namelist[i]); |
| 969 | goto bail; |
| 970 | } |
Andy Green | 6f3f2df | 2017-10-12 07:03:44 +0800 | [diff] [blame] | 971 | skip: |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 972 | free(namelist[i]); |
| 973 | } |
| 974 | |
| 975 | bail: |
| 976 | free(namelist); |
| 977 | |
| 978 | return ret; |
| 979 | #else |
| 980 | return 0; |
| 981 | #endif |
| 982 | } |
| 983 | |
Andy Green | 0a18354 | 2016-04-09 07:22:40 +0800 | [diff] [blame] | 984 | #endif |
| 985 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 986 | int |
| 987 | lwsws_get_config_globals(struct lws_context_creation_info *info, const char *d, |
| 988 | char **cs, int *len) |
| 989 | { |
| 990 | struct jpargs a; |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 991 | const char * const *old = info->plugin_dirs; |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 992 | char dd[128]; |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 993 | |
| 994 | memset(&a, 0, sizeof(a)); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 995 | |
| 996 | a.info = info; |
| 997 | a.p = *cs; |
Andy Green | a5e73a1 | 2016-04-13 11:49:07 +0800 | [diff] [blame] | 998 | a.end = (a.p + *len) - 1; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 999 | a.valid = 0; |
| 1000 | |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 1001 | lwsws_align(&a); |
| 1002 | info->plugin_dirs = (void *)a.p; |
| 1003 | a.plugin_dirs = (void *)a.p; /* writeable version */ |
| 1004 | a.p += MAX_PLUGIN_DIRS * sizeof(void *); |
| 1005 | |
| 1006 | /* copy any default paths */ |
| 1007 | |
| 1008 | while (old && *old) { |
| 1009 | a.plugin_dirs[a.count_plugin_dirs++] = *old; |
| 1010 | old++; |
| 1011 | } |
| 1012 | |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 1013 | lws_snprintf(dd, sizeof(dd) - 1, "%s/conf", d); |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 1014 | if (lwsws_get_config(&a, dd, paths_global, |
Andy Green | d461f46 | 2018-08-16 19:10:32 +0800 | [diff] [blame] | 1015 | LWS_ARRAY_SIZE(paths_global), lejp_globals_cb) > 1) |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1016 | return 1; |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 1017 | lws_snprintf(dd, sizeof(dd) - 1, "%s/conf.d", d); |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 1018 | if (lwsws_get_config_d(&a, dd, paths_global, |
Andy Green | 9bed6d6 | 2018-11-23 08:47:56 +0800 | [diff] [blame] | 1019 | LWS_ARRAY_SIZE(paths_global), |
| 1020 | lejp_globals_cb) > 1) |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1021 | return 1; |
| 1022 | |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 1023 | a.plugin_dirs[a.count_plugin_dirs] = NULL; |
| 1024 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1025 | *cs = a.p; |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 1026 | *len = lws_ptr_diff(a.end, a.p); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | int |
| 1032 | lwsws_get_config_vhosts(struct lws_context *context, |
| 1033 | struct lws_context_creation_info *info, const char *d, |
| 1034 | char **cs, int *len) |
| 1035 | { |
| 1036 | struct jpargs a; |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 1037 | char dd[128]; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1038 | |
Andy Green | cae57ad | 2016-05-02 10:03:25 +0800 | [diff] [blame] | 1039 | memset(&a, 0, sizeof(a)); |
| 1040 | |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1041 | a.info = info; |
| 1042 | a.p = *cs; |
| 1043 | a.end = a.p + *len; |
| 1044 | a.valid = 0; |
| 1045 | a.context = context; |
| 1046 | a.protocols = info->protocols; |
Andy Green | 0079099 | 2019-03-10 05:34:02 +0800 | [diff] [blame] | 1047 | a.pprotocols = info->pprotocols; |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1048 | a.extensions = info->extensions; |
| 1049 | |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 1050 | lws_snprintf(dd, sizeof(dd) - 1, "%s/conf", d); |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 1051 | if (lwsws_get_config(&a, dd, paths_vhosts, |
Andy Green | d461f46 | 2018-08-16 19:10:32 +0800 | [diff] [blame] | 1052 | LWS_ARRAY_SIZE(paths_vhosts), lejp_vhosts_cb) > 1) |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1053 | return 1; |
Andy Green | a496700 | 2016-09-15 02:22:57 +0800 | [diff] [blame] | 1054 | lws_snprintf(dd, sizeof(dd) - 1, "%s/conf.d", d); |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 1055 | if (lwsws_get_config_d(&a, dd, paths_vhosts, |
Andy Green | d461f46 | 2018-08-16 19:10:32 +0800 | [diff] [blame] | 1056 | LWS_ARRAY_SIZE(paths_vhosts), lejp_vhosts_cb) > 1) |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1057 | return 1; |
| 1058 | |
| 1059 | *cs = a.p; |
Andy Green | 253942c | 2018-08-23 11:29:45 +0800 | [diff] [blame] | 1060 | *len = lws_ptr_diff(a.end, a.p); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1061 | |
Andy Green | 92b0d8a | 2016-05-22 07:01:35 +0800 | [diff] [blame] | 1062 | if (!a.any_vhosts) { |
| 1063 | lwsl_err("Need at least one vhost\n"); |
| 1064 | return 1; |
| 1065 | } |
| 1066 | |
Andy Green | 7a77c0b | 2016-12-03 15:23:00 +0800 | [diff] [blame] | 1067 | // lws_finalize_startup(context); |
Andy Green | cd0c696 | 2016-03-28 10:12:37 +0800 | [diff] [blame] | 1068 | |
| 1069 | return 0; |
| 1070 | } |