]> mj.ucw.cz Git - subauth.git/blob - server/subauthd.h
Debian: Re-packaged for Bookworm
[subauth.git] / server / subauthd.h
1 /*
2  *      Sub-authentication Daemon
3  *
4  *      (c) 2017 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "autoconf.h"
8
9 #include <sys/types.h>
10
11 #include <ucw/clists.h>
12 #include <ucw/mainloop.h>
13 #include <ucw/mempool.h>
14 #include <ucw-json/json.h>
15
16 #define SOCKET_TIMEOUT 60000            // in ms
17 #define MAX_OOB_DATA_SIZE 4096
18
19 struct client {
20   struct main_file socket;
21   struct main_timer timer;
22   int uid;
23   struct mempool *pool;
24   struct json_context *json;
25   struct json_node *request;
26   struct json_node *reply;
27 };
28
29 extern clist zone_list;                 // of struct auth_zone
30 extern char *database_name;
31 extern char *temp_key_file;
32 extern uint max_comment_size;
33
34 /* cmd.c */
35
36 void cmd_dispatch(struct client *c);
37
38 const char *get_string(struct json_node *n, const char *key);
39 bool get_uint(struct json_node *n, const char *key, uint *dest);
40 struct json_node **get_array(struct json_node *n, const char *key);
41 struct json_node *get_object(struct json_node *n, const char *key);
42
43 /* auth.c */
44
45 #define DEFAULT_SALT_BYTES 8
46 #define DEFAULT_IDENT_BYTES 2
47 #define DEFAULT_GENERATED_BYTES 8
48 #define HASH_BYTES 32                   // We are using SHA-256
49 #define DEFAULT_HASH_ITERATIONS 64      // Number of hash function iterations per PBKDF2
50 #define MAX_TEXT_HASH_SIZE 256
51
52 struct auth_zone {
53   cnode n;
54   char *name;
55   char *desc;
56   uint auto_create_acct;
57   uint allow_passwd;
58   uint allow_tokens;
59   uint allow_passwd_auth;
60   uint max_temp_validity;
61 };
62
63 struct auth_user {
64   clist accounts;                       // of struct auth_acct
65   char login[1];
66 };
67
68 struct auth_acct {
69   cnode n;
70   struct auth_user *user;
71   struct auth_zone *zone;
72   clist tokens;                         // of struct auth_token
73   uint allow_passwd_auth;
74 };
75
76 enum token_type {
77   TOKEN_UNDEFINED,
78   TOKEN_PASSWORD,
79   TOKEN_GENERATED,
80   TOKEN_NUM_TYPES,
81 };
82
83 struct auth_token {
84   cnode n;
85   struct auth_acct *acct;
86   enum token_type type;
87   char *salt;
88   char *hash;
89   char *ident;
90   char *comment;
91   time_t last_modified;
92   uint iterations;
93 };
94
95 void auth_init(void);
96 void db_write(void);
97 struct auth_zone *auth_find_zone(const char *name);
98 struct auth_user *auth_find_user(const char *login, bool create);
99 struct auth_acct *auth_find_acct(struct auth_user *au, struct auth_zone *az, bool create);
100 struct auth_token *auth_find_token_passwd(struct auth_acct *aa);
101 struct auth_token *auth_find_token_generated(struct auth_acct *aa, const char *ident);
102 void auth_delete_user(struct auth_user *au);
103 void auth_delete_acct(struct auth_acct *aa);
104 void auth_delete_token(struct auth_token *at);
105 struct auth_token *auth_create_token(struct auth_acct *aa);
106 void auth_set_token_passwd(struct auth_token *at, const char *passwd);
107 char *auth_set_token_generated(struct auth_token *at, const char *comment, struct mempool *pool);
108 bool auth_check_token(struct auth_token *at, const char *passwd);
109 void auth_change_token_comment(struct auth_token *at, const char *comment);
110
111 extern struct auth_token *auth_fake_token;
112
113 /* temp.c */
114
115 void temp_init(void);
116 char *temp_generate(const char *zone, const char *login, uint validity, struct mempool *pool);
117 const char *temp_check(const char *zone, const char *login, const char *token, struct mempool *pool);
118 const char *temp_shorten(const char *token, struct mempool *pool);