1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by ort-c-header 0.7.4.
    6  * DO NOT EDIT!
    7  */
    8 
    9 #ifndef KWBP_VERSION
   10 # define KWBP_VERSION "0.7.4"
   11 #endif
   12 #ifndef KWBP_VSTAMP
   13 # define KWBP_VSTAMP 10805
   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  * Define our table columns.
   42  * Use these when creating your own SQL statements, combined with the 
   43  * db_xxxx_fill functions.
   44  * Each macro must be given a unique alias name.
   45  * This allows for doing multiple inner joins on the same table.
   46  */
   47 #define DB_SCHEMA_USER(_x) \
   48 	#_x ".email" "," \
   49 	#_x ".hash" "," \
   50 	#_x ".id"
   51 #define DB_SCHEMA_SESS(_x) \
   52 	#_x ".userid" "," \
   53 	#_x ".token" "," \
   54 	#_x ".id"
   55 
   56 /*
   57  * All of the fields we validate.
   58  * These are as VALID_XXX_YYY, where XXX is the structure and YYY is the 
   59  * field.
   60  * Only native types are listed.
   61  */
   62 enum	valid_keys {
   63 	VALID_USER_EMAIL,
   64 	VALID_USER_HASH,
   65 	VALID_USER_ID,
   66 	VALID_SESS_USERID,
   67 	VALID_SESS_TOKEN,
   68 	VALID_SESS_ID,
   69 	VALID__MAX
   70 };
   71 
   72 /*
   73  * Validation fields.
   74  * Pass this directly into khttp_parse(3) to use them as-is.
   75  * The functions are "valid_xxx_yyy", where "xxx" is the struct and "yyy" 
   76  * the field, and can be used standalone.
   77  * The form inputs are named "xxx-yyy".
   78  */
   79 extern const struct kvalid valid_keys[VALID__MAX];
   80 
   81 __BEGIN_DECLS
   82 
   83 /*
   84  * Allocate and open the database in "file". This opens the database in 
   85  * "safe exit" mode (see ksql(3)).
   86  * Note: if you're using a sandbox, you must accommodate for the SQLite 
   87  * database within process memory.
   88  * Returns a pointer to the database or NULL on memory exhaustion.
   89  * The returned pointer must be closed with db_close().
   90  */
   91 struct ksql *db_open(const char *file);
   92 
   93 /*
   94  * Open a transaction with identifier "id".
   95  * If "mode" is 0, the transaction is opened in "deferred" mode, meaning 
   96  * that the database is read-locked (no writes allowed) on the first read 
   97  * operation, and write-locked on the first write (only the current 
   98  * process can write).
   99  * If "mode" is >0, the transaction immediately starts a write-lock.
  100  * If "mode" is <0, the transaction starts in a write-pending, where no 
  101  * other locks can be held at the same time.
  102  */
  103 void db_trans_open(struct ksql *p, size_t id, int mode);
  104 
  105 /*
  106  * Roll-back an open transaction.
  107  */
  108 void db_trans_rollback(struct ksql *p, size_t id);
  109 
  110 /*
  111  * Commit an open transaction.
  112  */
  113 void db_trans_commit(struct ksql *p, size_t id);
  114 
  115 /*
  116  * Close the context opened by db_open().
  117  * Has no effect if "p" is NULL.
  118  */
  119 void db_close(struct ksql *p);
  120 
  121 /*
  122  * Clear resources and free "p".
  123  * Has no effect if "p" is NULL.
  124  */
  125 void db_user_free(struct user *p);
  126 
  127 /*
  128  * Fill in a user from an open statement "stmt".
  129  * This starts grabbing results from "pos", which may be NULL to start 
  130  * from zero.
  131  * This follows DB_SCHEMA_USER's order for columns.
  132  */
  133 void db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos);
  134 /*
  135  * Free resources from "p" and all nested objects.
  136  * Does not free the "p" pointer itself.
  137  * Has no effect if "p" is NULL.
  138  */
  139 void db_user_unfill(struct user *p);
  140 
  141 /*
  142  * Search for a user by email/password.
  143  * Queries on the following fields in struct user:
  144  * 	v1: email (equals)
  145  * 	v2: hash (pre-hashed password)
  146  * Returns a pointer or NULL on fail.
  147  * Free the pointer with db_user_free().
  148  */
  149 struct user *db_user_get_creds(struct ksql *db, const char *v1, const char *v2);
  150 
  151 /*
  152  * Clear resources and free "p".
  153  * Has no effect if "p" is NULL.
  154  */
  155 void db_sess_free(struct sess *p);
  156 
  157 /*
  158  * Fill in a sess from an open statement "stmt".
  159  * This starts grabbing results from "pos", which may be NULL to start 
  160  * from zero.
  161  * This follows DB_SCHEMA_SESS's order for columns.
  162  */
  163 void db_sess_fill(struct sess *p, struct ksqlstmt *stmt, size_t *pos);
  164 /*
  165  * Free resources from "p" and all nested objects.
  166  * Does not free the "p" pointer itself.
  167  * Has no effect if "p" is NULL.
  168  */
  169 void db_sess_unfill(struct sess *p);
  170 
  171 /*
  172  * Search by token and identifier.
  173  * Queries on the following fields in struct sess:
  174  * 	v1: token (equals)
  175  * 	v2: id (equals)
  176  * Returns a pointer or NULL on fail.
  177  * Free the pointer with db_sess_free().
  178  */
  179 struct sess *db_sess_get_creds(struct ksql *db, int64_t v1, int64_t v2);
  180 
  181 /*
  182  * Delete session.
  183  * 
  184  * Constrains the deleted records to:
  185  * 	v1: id (equals)
  186  * Returns zero on constraint violation, <0 on error, >0 on success
  187  */
  188 int db_sess_delete_id(struct ksql *db, int64_t v1);
  189 
  190 /*
  191  * Print out the fields of a user in JSON including nested 
  192  * structures.
  193  * Omits any password entries or those marked "noexport".
  194  * See json_user_obj() for the full object.
  195  */
  196 void json_user_data(struct kjsonreq *r, const struct user *p);
  197 
  198 /*
  199  * Emit the JSON key-value pair for the object:
  200  * 	"user" : { [data]+ }
  201  * See json_user_data() for the data.
  202  */
  203 void json_user_obj(struct kjsonreq *r, const struct user *p);
  204 
  205 /*
  206  * Print out the fields of a sess in JSON including nested 
  207  * structures.
  208  * Omits any password entries or those marked "noexport".
  209  * See json_sess_obj() for the full object.
  210  */
  211 void json_sess_data(struct kjsonreq *r, const struct sess *p);
  212 
  213 /*
  214  * Emit the JSON key-value pair for the object:
  215  * 	"sess" : { [data]+ }
  216  * See json_sess_data() for the data.
  217  */
  218 void json_sess_obj(struct kjsonreq *r, const struct sess *p);
  219 
  220 /*
  221  * Validation routines for the email field in struct user.
  222  */
  223 int valid_user_email(struct kpair *p);
  224 
  225 /*
  226  * Validation routines for the hash field in struct user.
  227  */
  228 int valid_user_hash(struct kpair *p);
  229 
  230 /*
  231  * Validation routines for the id field in struct user.
  232  */
  233 int valid_user_id(struct kpair *p);
  234 
  235 /*
  236  * Validation routines for the user field in struct sess.
  237  */
  238 int valid_sess_user(struct kpair *p);
  239 
  240 /*
  241  * Validation routines for the userid field in struct sess.
  242  */
  243 int valid_sess_userid(struct kpair *p);
  244 
  245 /*
  246  * Validation routines for the token field in struct sess.
  247  */
  248 int valid_sess_token(struct kpair *p);
  249 
  250 /*
  251  * Validation routines for the id field in struct sess.
  252  */
  253 int valid_sess_id(struct kpair *p);
  254 
  255 __END_DECLS
  256 
  257 #endif