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