master uint/parados / server / include / users.h
 1#ifndef USERS_H
 2#define USERS_H
 3
 4#include <stdbool.h>
 5#include <stddef.h>
 6
 7struct user {
 8	char           name[64];
 9	char           pass[128];
10
11	char**         allow;
12	size_t         allow_len;
13	size_t         allow_cap;
14};
15
16struct users {
17	struct user*   v;
18	size_t         len;
19	size_t         cap;
20};
21
22extern struct users users;
23
24/**
25 * @brief Check whether relpath is permitted for a given user.
26 *
27 * @param u User entry.
28 * @param relpath Path relative to media_dir.
29 *
30 * @return true=permitted, false=not-permitted
31 */
32bool user_allows_path(const struct user* u, const char* relpath);
33
34/**
35 * @brief Add an allow prefix to the most recently pushed user
36 *
37 * @param prefix Allowed prefix (e.g. "Music/", "TV/", "*")
38 *
39 * @return 0=Success, -1=Failure
40 */
41int users_add_allow(const char* prefix);
42
43/**
44 * @brief Authenticate request using the "Authorization" header.
45 *
46 * @note Returns NULL if authentication is disabled
47 *
48 * @param hdr Full HTTP request header buffer.
49 *
50 * @return User Ptr=Success, NULL=Failure.
51 */
52const struct user* users_auth_from_hdr(const char* hdr);
53
54/**
55 * @brief Free all memory owned by the global user table.
56 */
57void users_free(void);
58
59/**
60 * @brief Create new user entry and append it to the global users table
61 *
62 * @param name Username
63 *
64 * @return 0=Success, -1=Failure
65 */
66int users_push(const char* name);
67
68/**
69 * @brief Set the password for most recently pushed user
70 *
71 * @note Empty password creates a passwordless user
72 *
73 * @param pass Password string
74 *
75 * @return 0=Success, -1=Failure
76 */
77int users_set_pass(const char* pass);
78
79#endif /* USERS_H */
80