]> mj.ucw.cz Git - subauth.git/blob - server/subauthd.h
Real cryptography
[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-json/json.h>
14
15 #define SOCKET_TIMEOUT 60000            // in ms
16 #define MAX_PACKET_SIZE 16384
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 json_context *json;
24   struct json_node *request;
25   struct json_node *reply;
26 };
27
28 extern clist zone_list;                 // of struct auth_zone
29 extern char *database_name;
30
31 /* cmd.c */
32
33 void cmd_dispatch(struct client *c);
34
35 const char *get_string(struct json_node *n, const char *key);
36 bool get_uint(struct json_node *n, const char *key, uint *dest);
37 struct json_node **get_array(struct json_node *n, const char *key);
38 struct json_node *get_object(struct json_node *n, const char *key);
39
40 /* auth.c */
41
42 #define DEFAULT_SALT_BYTES 8
43 #define DEFAULT_IDENT_BYTES 2
44 #define DEFAULT_GENERATED_BYTES 8
45 #define HASH_BYTES 32                   // We are using SHA-256
46 #define DEFAULT_HASH_ITERATIONS 64      // Number of hash function iterations per PBKDF2
47 #define MAX_TEXT_HASH_SIZE 256
48
49 struct auth_zone {
50   cnode n;
51   char *name;
52   uint auto_create_acct;
53   uint allow_passwd;
54   uint allow_tokens;
55 };
56
57 struct auth_user {
58   clist accounts;                       // of struct auth_acct
59   char login[1];
60 };
61
62 struct auth_acct {
63   cnode n;
64   struct auth_user *user;
65   struct auth_zone *zone;
66   clist tokens;                         // of struct auth_token
67 };
68
69 enum token_type {
70   TOKEN_UNDEFINED,
71   TOKEN_PASSWORD,
72   TOKEN_GENERATED,
73   TOKEN_NUM_TYPES,
74 };
75
76 struct auth_token {
77   cnode n;
78   struct auth_acct *acct;
79   enum token_type type;
80   char *salt;
81   char *hash;
82   char *ident;
83   char *comment;
84   time_t last_modified;
85   uint iterations;
86 };
87
88 void auth_init(void);
89 void db_write(void);
90 struct auth_zone *auth_find_zone(const char *name);
91 struct auth_user *auth_find_user(const char *login, bool create);
92 struct auth_acct *auth_find_acct(struct auth_user *au, struct auth_zone *az, bool create);
93 struct auth_token *auth_find_token_passwd(struct auth_acct *aa);
94 struct auth_token *auth_find_token_generated(struct auth_acct *aa, char *ident);
95 void auth_delete_user(struct auth_user *au);
96 void auth_delete_acct(struct auth_acct *aa);
97 void auth_delete_token(struct auth_token *at);
98 struct auth_token *auth_create_token(struct auth_acct *aa);
99 void auth_set_token_passwd(struct auth_token *at, const char *passwd);
100 char *auth_set_token_generated(struct auth_token *at, const char *comment);
101 bool auth_check_token(struct auth_token *at, const char *passwd);
102
103 extern struct auth_token *auth_fake_token;