1 #ifndef DB_H 2 #define DB_H 3 4 /* 5 * WARNING: automatically generated by ort-c-header 0.8.5. 6 * DO NOT EDIT! 7 */ 8 9 #ifndef KWBP_VERSION 10 # define KWBP_VERSION "0.8.5" 11 #endif 12 #ifndef KWBP_VSTAMP 13 # define KWBP_VSTAMP 10906 14 #endif 15 16 /* 17 * A user within our system. 18 */ 19 struct user { 20 /* E-mail address. */ 21 char *email; 22 /* Hashed password. */ 23 char *hash; 24 int64_t id; 25 }; 26 27 /* 28 * A logged-in user. 29 */ 30 struct sess { 31 /* User attached to session. */ 32 struct user user; 33 /* User identifier attached to session. */ 34 int64_t userid; 35 /* Random token for authentication. */ 36 int64_t token; 37 int64_t id; 38 }; 39 40 /* 41 * All of the fields we validate. 42 * These are as VALID_XXX_YYY, where XXX is the structure and YYY is the 43 * field. 44 * Only native types are listed. 45 */ 46 enum valid_keys { 47 VALID_USER_EMAIL, 48 VALID_USER_HASH, 49 VALID_USER_ID, 50 VALID_SESS_USERID, 51 VALID_SESS_TOKEN, 52 VALID_SESS_ID, 53 VALID__MAX 54 }; 55 56 /* 57 * Validation fields. 58 * Pass this directly into khttp_parse(3) to use them as-is. 59 * The functions are "valid_xxx_yyy", where "xxx" is the struct and "yyy" 60 * the field, and can be used standalone. 61 * The form inputs are named "xxx-yyy". 62 */ 63 extern const struct kvalid valid_keys[VALID__MAX]; 64 65 __BEGIN_DECLS 66 67 /* 68 * Forward declaration of opaque pointer. 69 */ 70 struct ort; 71 72 /* 73 * Set the argument given to the logging function specified to 74 * db_open_logging(). 75 * Has no effect if no logging function has been set. 76 * The buffer is copied into a child process, so serialised objects may 77 * not have any pointers in the current address space or they will fail 78 * (at best). 79 * Set length to zero to unset the logging function callback argument. 80 */ 81 void db_logging_data(struct ort *ort, const void *arg, size_t sz); 82 83 /* 84 * Allocate and open the database in "file". 85 * Returns an opaque pointer or NULL on memory exhaustion. 86 * The returned pointer must be closed with db_close(). 87 * See db_open_logging() for the equivalent function that accepts logging 88 * callbacks. 89 * This function starts a child with fork(), the child of which opens the 90 * database, so a constraint environment (e.g., with pledge) must take 91 * this into account. 92 * Subsequent this function, all database operations take place over IPC. 93 */ 94 struct ort *db_open(const char *file); 95 96 /* 97 * Like db_open() but accepts a function for logging. 98 * If both are provided, the "long" form overrides the "short" form. 99 * The logging function is run both in a child and parent process, so it 100 * must not have side effects. 101 * The optional pointer is passed to the long form logging function and 102 * is inherited by the child process as-is, without being copied by 103 * value. 104 * See db_logging_data() to set the pointer after initialisation. 105 */ 106 struct ort *db_open_logging(const char *file, 107 void (*log)(const char *, void *), 108 void (*log_short)(const char *, ...), void *log_arg); 109 110 /* 111 * Open a transaction with identifier "id". 112 * If "mode" is 0, the transaction is opened in "deferred" mode, meaning 113 * that the database is read-locked (no writes allowed) on the first read 114 * operation, and write-locked on the first write (only the current 115 * process can write). 116 * If "mode" is >0, the transaction immediately starts a write-lock. 117 * If "mode" is <0, the transaction starts in a write-pending, where no 118 * other locks can be held at the same time. 119 * The DB_TRANS_OPEN_IMMEDIATE, DB_TRANS_OPEN_DEFERRED, and 120 * DB_TRANS_OPEN_EXCLUSIVE macros accomplish the same but with the "mode" 121 * being explicit in the name and not needing to be specified. 122 */ 123 void db_trans_open(struct ort *ctx, size_t id, int mode); 124 125 #define DB_TRANS_OPEN_IMMEDIATE(_ctx, _id) \ 126 db_trans_open((_ctx), (_id), 1) 127 #define DB_TRANS_OPEN_DEFERRED(_ctx, _id)\ 128 db_trans_open((_ctx), (_id), 0) 129 #define DB_TRANS_OPEN_EXCLUSIVE(_ctx, _id)\ 130 db_trans_open((_ctx), (_id), -1) 131 132 /* 133 * Roll-back an open transaction. 134 */ 135 void db_trans_rollback(struct ort *ctx, size_t id); 136 137 /* 138 * Commit an open transaction. 139 */ 140 void db_trans_commit(struct ort *ctx, size_t id); 141 142 /* 143 * Close the context opened by db_open(). 144 * Has no effect if "p" is NULL. 145 */ 146 void db_close(struct ort *p); 147 148 /* 149 * Clear resources and free "p". 150 * Has no effect if "p" is NULL. 151 */ 152 void db_user_free(struct user *p); 153 154 /* 155 * Search for a user by email/password. 156 * Queries on the following fields in struct user: 157 * v1: email (equals) 158 * v2: hash (pre-hashed password, equals) 159 * Returns a pointer or NULL on fail. 160 * Free the pointer with db_user_free(). 161 */ 162 struct user *db_user_get_creds(struct ort *ctx, const char *v1, const char *v2); 163 164 /* 165 * Clear resources and free "p". 166 * Has no effect if "p" is NULL. 167 */ 168 void db_sess_free(struct sess *p); 169 170 /* 171 * Search by token and identifier. 172 * Queries on the following fields in struct sess: 173 * v1: token (equals) 174 * v2: id (equals) 175 * Returns a pointer or NULL on fail. 176 * Free the pointer with db_sess_free(). 177 */ 178 struct sess *db_sess_get_creds(struct ort *ctx, int64_t v1, int64_t v2); 179 180 /* 181 * Delete session. 182 * 183 * Constrains the deleted records to: 184 * v1: id (equals) 185 * Returns zero on constraint violation, non-zero on success. 186 */ 187 int db_sess_delete_id(struct ort *ctx, int64_t v1); 188 189 /* 190 * Print out the fields of a user in JSON including nested structures. 191 * Omits any password entries or those marked "noexport". 192 * See json_user_obj() for the full object. 193 */ 194 void json_user_data(struct kjsonreq *r, const struct user *p); 195 196 /* 197 * Emit the JSON key-value pair for the object: 198 * "user" : { [data]+ } 199 * See json_user_data() for the data. 200 */ 201 void json_user_obj(struct kjsonreq *r, const struct user *p); 202 203 /* 204 * Print out the fields of a sess in JSON including nested structures. 205 * Omits any password entries or those marked "noexport". 206 * See json_sess_obj() for the full object. 207 */ 208 void json_sess_data(struct kjsonreq *r, const struct sess *p); 209 210 /* 211 * Emit the JSON key-value pair for the object: 212 * "sess" : { [data]+ } 213 * See json_sess_data() for the data. 214 */ 215 void json_sess_obj(struct kjsonreq *r, const struct sess *p); 216 217 /* 218 * Validation routines for the email field in struct user. 219 */ 220 int valid_user_email(struct kpair *p); 221 222 /* 223 * Validation routines for the hash field in struct user. 224 */ 225 int valid_user_hash(struct kpair *p); 226 227 /* 228 * Validation routines for the id field in struct user. 229 */ 230 int valid_user_id(struct kpair *p); 231 232 /* 233 * Validation routines for the user field in struct sess. 234 */ 235 int valid_sess_user(struct kpair *p); 236 237 /* 238 * Validation routines for the userid field in struct sess. 239 */ 240 int valid_sess_userid(struct kpair *p); 241 242 /* 243 * Validation routines for the token field in struct sess. 244 */ 245 int valid_sess_token(struct kpair *p); 246 247 /* 248 * Validation routines for the id field in struct sess. 249 */ 250 int valid_sess_id(struct kpair *p); 251 252 __END_DECLS 253 254 #endif