Commit Graph

53 Commits

Author SHA1 Message Date
Xavier Del Campo Romero b3ef22ac2a
Define _POSIX_C_SOURCE
This allows using the default compiler defined by make(1) (i.e.,
c99(1)), thus improving POSIX compatibility.
2023-03-24 02:49:11 +01:00
Xavier Del Campo Romero 4501c66ebd
http.c: Add missing #include
As required by strncasecmp(3).
2023-03-24 02:46:55 +01:00
Xavier Del Campo Romero d9bb874591
Send response on quota exceeded
So far, slcl would just close the connection with a client when the
Content-Length of an incoming request exceeded the user quota, without
any meaningful information given back to the user.

Now, slcl responds with a HTML file with meaningful information about
the error.

Limitations:

- While this commits has been successfully tested on ungoogled-chromium,
LibreWolf (and I assume Firefox and any other derivates too) does not
seem to receive the response from the server.
    - However, this issue only occurred during local testing, but not
on remote instances.
2023-03-20 10:57:20 +01:00
Xavier Del Campo Romero d51b191ab7
README.md: Fix wrong links 2023-03-20 00:10:26 +01:00
Xavier Del Campo Romero f0936adb34
README.md: Update according to current status 2023-03-19 23:59:45 +01:00
Xavier Del Campo Romero 66bc98275f
main.c: Fix double free(3) and refactor form handling
- When a non-empty username and an empty password was given, slcl would
crash due to a double free(3). This happened because append_form would
grow the form list before sanitizing the input and, since the output
pointer was not updated to the caller function, the latter would attempt
to free a now-old pointer.
- Additionally, some compilers such as clang complained about the
potential use of an uninitialized variable when calling forms_free.
- Also, it was a good opportunity to refactor get_forms and its caller
functions, as get_forms was not differentiate fatal errors from user
input errors.
2023-03-19 23:58:03 +01:00
Xavier Del Campo Romero 9624e81144
main.c: Fix undefined value for cur
As otherwise reported by clang 14.0.0:

main.c:679:14: warning: variable 'cur' is used uninitialized whenever '&&' condition is false [-Wsometimes-uninitialized]
    else if (available && quota_current(a, username, &cur))

This was a minor issue after all, as pq was not used unless available
were set.
2023-03-16 02:23:05 +01:00
Xavier Del Campo Romero fa474603cc
auth.c: Add friendly reminder 2023-03-16 01:59:17 +01:00
Xavier Del Campo Romero aefd6c6fda
Add man page for usergen(1) 2023-03-16 01:57:04 +01:00
Xavier Del Campo Romero f6fd20fdc4
Make usergen a bit more useful
So far, usergen printed a JSON object over standard output that had to
be manually copied into db.json. Now, this step is done automatically,
thanks to jq(1). OTOH, user directory is now also created by usergen.
2023-03-16 01:55:22 +01:00
Xavier Del Campo Romero 27b8a69718
page.c: Set Content-Disposition when serving files
So far, slcl used the default browser behaviour (i.e.,
Content-Disposition: inline), which means files were typically shown on
the web browser itself. However, this caused two issues:

- Users would have to right-click -> "Save Link As..." to download a
file, which might be inconvenient for some users.
- The original file name would not be retrieved for publicly shared
files.

Now, file download is always requested to the browser, and the original
file path is retrieved via readlink(2).
2023-03-16 01:49:18 +01:00
Xavier Del Campo Romero 7e2e37d40a
main.c: Ensure essential directories on startup
So far, slcl failed with poorly described error messages when any of the
essential directories were missing. Now, these are created automatically
so that the initial setup is easier.
2023-03-16 01:46:49 +01:00
Xavier Del Campo Romero 955ae07f55
slcl.1: Update TODO
- User quota was implemented by commit ff8da797a.
- Public file sharing was implemented by commit 2e1b1313.
2023-03-16 01:18:04 +01:00
Xavier Del Campo Romero 69a059e0df
page.c: Fix wrong error message 2023-03-09 02:04:52 +01:00
Xavier Del Campo Romero d18243c019
main.c: Call form_free instead of repeating code 2023-03-09 02:04:52 +01:00
Xavier Del Campo Romero 2e1b131396
Implement public file sharing
An HTML form is now added next to each regular file, that generates a
POST request. Then, slcl replies with a HTML document with a link to the
public resource (which are implemented as symlinks).

Limitations:

- For now, only regular files can be shared i.e., sharing directories is
not possible. While feasible, it still requires a larger refactor to
list_dir and resource_layout, so that read-only access to the directory
is provided to anonymous users.
2023-03-09 02:02:40 +01:00
Xavier Del Campo Romero 13f96054f6
page.c: Provide meaningful description on HTTP 404 2023-03-09 01:23:02 +01:00
Xavier Del Campo Romero 8016f537ca
page.c: Implement function for common <head> nodes
This will be used by future commits.
2023-03-09 01:20:18 +01:00
Xavier Del Campo Romero a4c12e7ccc
Translate whitespace to '+' in append_form
Otherwise, every function calling get_forms must implement this.
2023-03-09 01:17:37 +01:00
Xavier Del Campo Romero ad7fb045ad
Move decode_hex into its own file
- Error detection against strotul(3) has been improved, as done in other
places.
- New function encode_hex has been implemented, which will be used
by future commits.
2023-03-09 01:14:10 +01:00
Xavier Del Campo Romero e0fc222f1d
http.c: Minor formatting change 2023-03-08 18:52:36 +01:00
Xavier Del Campo Romero c4488c5e81
page.c: Set common <head> parameters 2023-03-08 18:52:03 +01:00
Xavier Del Campo Romero f4c16b8e56
main.c: Always append trailing slash to directories
Otherwise, slcl would fail to generate the URLs for the elements inside
a directory, because of how cust_dirname worked, which also turned out
to be redundant.
2023-03-08 18:50:15 +01:00
Xavier Del Campo Romero 7176bd3c94
Call page_forbidden on invalid auth 2023-03-08 18:48:58 +01:00
Xavier Del Campo Romero e37a0248e8
Remove(3) f->tmpname from ctx_free
Until now, f->tmpname was removed by move_file when the move
operation succeeded. However, since a HTTP operation can fail before
move_file is called, the temporary file must also be removed.
2023-03-08 18:17:32 +01:00
Xavier Del Campo Romero 6cbab96869
Update dynstr 2023-03-08 01:22:51 +01:00
Xavier Del Campo Romero 5e100ff3c1
server.c: Fix wrong size for memcpy(3) 2023-03-08 01:17:34 +01:00
Xavier Del Campo Romero 76664d2006
main.c: Avoid leaking forms 2023-03-08 00:38:03 +01:00
Xavier Del Campo Romero 4e1a0484e5
main.c: Print error message only on errno != EEXIST 2023-03-08 00:37:28 +01:00
Xavier Del Campo Romero 0471658177
page.c: Show units for user quota 2023-03-08 00:33:55 +01:00
Xavier Del Campo Romero ba612d6621
page.c: Sort elements alphabetically 2023-03-08 00:33:20 +01:00
Xavier Del Campo Romero ee75d8e3e5
html.c: Allow empty strings on html_encode 2023-03-08 00:26:31 +01:00
Xavier Del Campo Romero 0954abcf09
server.c: Minor const-correctness improvement 2023-03-07 12:41:03 +01:00
Xavier Del Campo Romero 0ca8ea19bc
main.c: Replace + with whitespace for directories 2023-03-07 12:40:30 +01:00
Xavier Del Campo Romero 42c23ec1a5
main.c: Fix memory leak if append_form fails 2023-03-07 12:38:59 +01:00
Xavier Del Campo Romero bd3d2aabc3
main.c: Simplify code a bit 2023-03-07 12:38:32 +01:00
Xavier Del Campo Romero f33e1c9053
cftw.c: Add missing call to closedir(2) 2023-03-07 12:36:27 +01:00
Xavier Del Campo Romero 61dc4f42b3
Perform some minor optimizations 2023-03-06 05:51:53 +01:00
Xavier Del Campo Romero ff8da797a1
Implement user quota
This feature allows admins to set a specific quota for each user, in
MiB. This feature is particularly useful for shared instances, where
unlimited user storage might be unfeasible or even dangerous for the
server.

Also, a nice HTML5 <progress> element has been added to the site that
shows how much of the quota has been consumed.

If no quota is set, slcl falls back to the default behaviour i.e.,
assume unlimited storage.

Limitations:

- While HTTP does specify a Content-Length, which determines the length
of the whole request, it does not specify how many files are involved
or their individual sizes.
- Because of this, if multiple files are uploaded simultaneously, the
whole request would be dropped if user quota is exceeded, even if not
all files exceeded it.
- Also, Content-Length adds the length of some HTTP boilerplate
(e.g.: boundaries), but slcl must rely on this before accepting the
whole request. In other words, this means some requests might be
rejected by slcl because of the extra bytes caused by such boilerplate.
- When the quota is exceeded, slcl must close the connection so that
the rest of the transfer is cancelled. Unfortunately, this means no
HTML can be sent back to the customer to inform about the situation.
2023-03-06 05:51:49 +01:00
Xavier Del Campo Romero 804b8841f3
Add cftw
POSIX functions ftw(3) and nftw(3) do not allow passing an opaque
pointer to the callback they call, so it forces the use of statically
allocated data.

ctfw (from "custom ftw") is a custom implementation that solves this,
while also removing unneeded stuff.

This function will be used by future commits.
2023-03-06 05:06:30 +01:00
Xavier Del Campo Romero 3e95727e27
README.md: Fix broken link to usergen 2023-03-06 02:27:38 +01:00
Xavier Del Campo Romero 4cd577afa9
Perform some minor optimizations 2023-03-06 00:59:07 +01:00
Xavier Del Campo Romero 8bc191d08a
main.c: Simplify failed login response
- Location seems to have no effect on 401 HTTP responses.
- Content-Type is already set by page_failed_login.
2023-03-06 00:56:48 +01:00
Xavier Del Campo Romero 8319dfe58b
http.c: Compare headers as case-insensitive
Web browsers such as lynx send "Content-length" instead of
"Content-Length" (as done by LibreWolf and Chromium).
2023-03-04 04:04:09 +01:00
Xavier Del Campo Romero fff6ed4e0a
http.c: Use persistent cookies
Cookies without "Expires" are considered non-persistent and thus can be
removed by the web browser. Instead, slcl now sets persistent cookies
that last for 1 year.
2023-03-04 04:02:14 +01:00
Xavier Del Campo Romero c033b3006c
http.c: Improve error detection for strotull(3)
set_length relies on user input to determine Content-Length, so it
should be considered unreliable.
2023-03-04 03:03:22 +01:00
Xavier Del Campo Romero 62bdf9f72f
Fix memory leak on failed realloc(3)
According to C99 §7.20.3.4:

If memory for the new object cannot be allocated, the old object is not
deallocated and its value is unchanged.

Therefore, a temporary pointer must be used to ensure the original
object can still be deallocated should realloc(3) return a null pointer.
2023-03-04 03:03:15 +01:00
Xavier Del Campo Romero d83e8ad1ba
Add man page 2023-02-28 01:43:59 +01:00
Xavier Del Campo Romero 08a38aea94
Add usergen
This script takes a username and password from standard input and
generates a JSON object that can be appended to the "users" object in
db.json.
2023-02-28 01:43:59 +01:00
Xavier Del Campo Romero 1b990951f5
Add Makefile 2023-02-28 01:43:59 +01:00