| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::Phoebe::Iapetus - uploads using the Iapetus protocol | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This allows known editors to upload files and pages using the Iapetus protocol. | ||
| 26 | See L<Iapetus documentation|https://codeberg.org/oppenlab/iapetus>. | ||
| 27 | |||
| 28 | In order to be a known editor, you need to set C<@known_fingerprints> in your | ||
| 29 | F<config> file. Here’s an example: | ||
| 30 | |||
| 31 | package App::Phoebe; | ||
| 32 | our @known_fingerprints; | ||
| 33 | @known_fingerprints = qw( | ||
| 34 | sha256$fce75346ccbcf0da647e887271c3d3666ef8c7b181f2a3b22e976ddc8fa38401 | ||
| 35 | sha256$54c0b95dd56aebac1432a3665107d3aec0d4e28fef905020ed6762db49e84ee1); | ||
| 36 | use App::Phoebe::Iapetus; | ||
| 37 | |||
| 38 | The way to do it is to run the following, assuming the certificate is named | ||
| 39 | F<client-cert.pem>: | ||
| 40 | |||
| 41 | openssl x509 -in client-cert.pem -noout -sha256 -fingerprint \ | ||
| 42 | | sed -e 's/://g' -e 's/SHA256 Fingerprint=/sha256$/' \ | ||
| 43 | | tr [:upper:] [:lower:] | ||
| 44 | |||
| 45 | This should give you the fingerprint in the correct format to add to the list | ||
| 46 | above. | ||
| 47 | |||
| 48 | Make sure your main menu has a link to the login page. The login page allows | ||
| 49 | people to pick the right certificate without interrupting their uploads. | ||
| 50 | |||
| 51 | => /login Login | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | package App::Phoebe::Iapetus; | ||
| 56 | use App::Phoebe qw($server $log @request_handlers @extensions host_regex space_regex space port result | ||
| 57 | valid_id valid_mime_type valid_size @known_fingerprints process_titan); | ||
| 58 | use Modern::Perl; | ||
| 59 | use File::MimeInfo qw(globs); | ||
| 60 | use Encode qw(decode_utf8); | ||
| 61 | use URI::Escape; | ||
| 62 | |||
| 63 | push(@{$server->{wiki_mime_type}},'text/gemini'); | ||
| 64 | unshift(@request_handlers, '^iapetus://' => \&handle_iapetus); | ||
| 65 | |||
| 66 | sub handle_iapetus { | ||
| 67 | my $stream = shift; | ||
| 68 | my $data = shift; | ||
| 69 | # extra processing of the request if we didn't do that, yet | ||
| 70 | return setup_iapetus($stream, $data) unless $data->{upload}; | ||
| 71 | my $size = $data->{upload}->{params}->{size}; | ||
| 72 | my $actual = length($data->{buffer}); | ||
| 73 | if ($actual == $size) { | ||
| 74 | $log->debug("Handle Iapetus request as Titan request"); | ||
| 75 | process_titan($stream, $data->{request}, $data->{upload}, $data->{buffer}, $size); | ||
| 76 | # do not close in case we're waiting for the lock | ||
| 77 | return; | ||
| 78 | } elsif ($actual > $size) { | ||
| 79 | $log->debug("Received more than the promised $size bytes"); | ||
| 80 | result($stream, "59", "Received more than the promised $size bytes"); | ||
| 81 | $stream->close_gracefully(); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | $log->debug("Waiting for " . ($size - $actual) . " more bytes"); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub setup_iapetus { | ||
| 88 | my $stream = shift; | ||
| 89 | my $data = shift; | ||
| 90 | my $request = $data->{request}; | ||
| 91 | $log->info("Looking at $request"); | ||
| 92 | my $hosts = host_regex(); | ||
| 93 | my $spaces_regex = space_regex(); | ||
| 94 | my $port = port($stream); | ||
| 95 | if ($request =~ m!^iapetus://($hosts)(?::$port)?!) { | ||
| 96 | my $host = $1; | ||
| 97 | my($scheme, $authority, $path, $query, $fragment, $size) = | ||
| 98 | $request =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(\S*))?\s+(\d+)|; | ||
| 99 | if ($path =~ m!^(?:/($spaces_regex))?(?:/raw)?/([^/;=&]+)!) { | ||
| 100 | my ($space, $id) = ($1, $2); | ||
| 101 | return unless valid_id($stream, $host, $space, $id); | ||
| 102 | my $type = globs($id) || mime_type($id); | ||
| 103 | my $params = { size => $size, mime => $type }; | ||
| 104 | return unless valid_mime_type($stream, $host, $space, $id, $params); | ||
| 105 | return unless valid_size($stream, $host, $space, $id, $params); | ||
| 106 | return unless valid_client_cert($stream, $host, $space, $id, $params); | ||
| 107 | $data->{upload} = { | ||
| 108 | host => $host, | ||
| 109 | space => space($stream, $host, $space), | ||
| 110 | id => decode_utf8(uri_unescape($id)), | ||
| 111 | params => $params, | ||
| 112 | }; | ||
| 113 | result($stream, "10", "Continue"); # weird! | ||
| 114 | return 1; | ||
| 115 | } else { | ||
| 116 | $log->debug("The path $path is malformed"); | ||
| 117 | result($stream, "59", "The path $path is malformed"); | ||
| 118 | $stream->close_gracefully(); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | |||
| 124 | # fallback if File::MimeInfo found no data files | ||
| 125 | sub mime_type { | ||
| 126 | $_ = shift; | ||
| 127 | return 'text/gemini' if /\.gmi$/i; | ||
| 128 | return 'text/plain' if /\.te?xt$/i; | ||
| 129 | return 'text/markdown' if /\.md$/i; | ||
| 130 | return 'text/html' if /\.html?$/i; | ||
| 131 | return 'image/png' if /\.png$/i; | ||
| 132 | return 'image/jpeg' if /\.jpe?g$/i; | ||
| 133 | return 'image/gif' if /\.gif$/i; | ||
| 134 | return 'text/plain'; # this is what phoebe expects | ||
| 135 | } | ||
| 136 | |||
| 137 | # duplicates functionality from registered_editor_login.pl | ||
| 138 | |||
| 139 | sub valid_client_cert { | ||
| 140 | my $stream = shift; | ||
| 141 | my $host = shift; | ||
| 142 | my $space = shift; | ||
| 143 | my $id = shift; | ||
| 144 | my $fingerprint = $stream->handle->get_fingerprint(); | ||
| 145 | if ($fingerprint and grep { $_ eq $fingerprint} @known_fingerprints) { | ||
| 146 | $log->info("Successfully identified client certificate"); | ||
| 147 | return 1; | ||
| 148 | } elsif ($fingerprint) { | ||
| 149 | $log->info("Unknown client certificate $fingerprint"); | ||
| 150 | result($stream, "61", "Your client certificate is not authorized for editing"); | ||
| 151 | } else { | ||
| 152 | $log->info("Requested client certificate"); | ||
| 153 | result($stream, "60", "You need a client certificate to edit this wiki"); | ||
| 154 | } | ||
| 155 | $stream->close_gracefully(); | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | # also duplicates functionality from registered_editor_login.pl | ||
| 160 | |||
| 161 | push(@extensions, \&iapetus_login); | ||
| 162 | |||
| 163 | sub iapetus_login { | ||
| 164 | my $stream = shift; | ||
| 165 | my $url = shift; | ||
| 166 | my $hosts = host_regex(); | ||
| 167 | my $spaces = space_regex(); | ||
| 168 | my $port = port($stream); | ||
| 169 | my $fingerprint = $stream->handle->get_fingerprint(); | ||
| 170 | my $host; | ||
| 171 | if (($host) = $url =~ m!^gemini://($hosts)(?::$port)?/login!) { | ||
| 172 | if ($fingerprint and grep { $_ eq $fingerprint} @known_fingerprints) { | ||
| 173 | $log->info("Successfully identified client certificate"); | ||
| 174 | result($stream, "30", "gemini://$host:$port/"); | ||
| 175 | } elsif ($fingerprint) { | ||
| 176 | $log->info("Unknown client certificate $fingerprint"); | ||
| 177 | result($stream, "61", "Your client certificate is not known"); | ||
| 178 | } else { | ||
| 179 | $log->info("Requested client certificate"); | ||
| 180 | result($stream, "60", "You need a client certificate to edit this wiki"); | ||
| 181 | } | ||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | return; | ||
| 185 | } |