1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by kwebapp 0.6.5.
    6  * DO NOT EDIT!
    7  */
    8 
    9 #ifndef KWBP_VERSION
   10 # define KWBP_VERSION "0.6.5"
   11 #endif
   12 #ifndef KWBP_VSTAMP
   13 # define KWBP_VSTAMP 10706
   14 #endif
   15 
   16 /*
   17  * Birthsex of individual
   18  */
   19 enum	sex {
   20 	/* Male */
   21 	SEX_male = 0,
   22 	/* Female */
   23 	SEX_female = 1,
   24 	/* Other */
   25 	SEX_other = 2
   26 };
   27 
   28 /*
   29  * Controlling organisation.
   30  */
   31 struct	company {
   32 	/* Name of the organisation. */
   33 	char	*name;
   34 	int64_t	 id;
   35 	/* Simply a check for null values. */
   36 	int64_t	 somenum;
   37 	/* Non-zero if "somenum" field is null/unset. */
   38 	int has_somenum;
   39 	TAILQ_ENTRY(company) _entries;
   40 };
   41 
   42 /*
   43  * Queue of company for listings.
   44  */
   45 TAILQ_HEAD(company_q, company);
   46 
   47 /*
   48  * A regular user.
   49  */
   50 struct	user {
   51 	/*
   52 	 * This struct will be filled in from an inner join
   53 	 * on the "cid" variable.
   54 	 */
   55 	struct company company;
   56 	/* A foreign key reference. */
   57 	int64_t	 cid;
   58 	/* User's birth sex. */
   59 	enum sex sex;
   60 	/*
   61 	 * Password hash.
   62 	 * This is passed to inserts and updates as a password,
   63 	 * then hashed within the implementation and extracted
   64 	 * (in listings and searches) as the hash value.
   65 	 */
   66 	char	*hash;
   67 	/* Unique e-mail address. */
   68 	char	*email;
   69 	/* A PNG image or something. */
   70 	void	*image;
   71 	size_t	 image_sz;
   72 	/* User's full name. */
   73 	char	*name;
   74 	int64_t	 uid;
   75 	/* Non-zero if "image" field is null/unset. */
   76 	int has_image;
   77 };
   78 
   79 /*
   80  * Callback of user for iteration.
   81  * The arg parameter is the opaque pointer passed into the iterate 
   82  * function.
   83  */
   84 typedef void (*user_cb)(const struct user *v, void *arg);
   85 
   86 /*
   87  * Authenticated session.
   88  */
   89 struct	session {
   90 	struct user user;
   91 	/* Associated user. */
   92 	int64_t	 userid;
   93 	/* Random cookie. */
   94 	int64_t	 token;
   95 	time_t	 mtime;
   96 	int64_t	 id;
   97 };
   98 
   99 /*
  100  * Callback of session for iteration.
  101  * The arg parameter is the opaque pointer passed into the iterate 
  102  * function.
  103  */
  104 typedef void (*session_cb)(const struct session *v, void *arg);
  105 
  106 /*
  107  * Define our table columns.
  108  * Use these when creating your own SQL statements, combined with the 
  109  * db_xxxx_fill functions.
  110  * Each macro must be given a unique alias name.
  111  * This allows for doing multiple inner joins on the same table.
  112  */
  113 #define DB_SCHEMA_COMPANY(_x) \
  114 	#_x ".name" "," \
  115 	#_x ".id" "," \
  116 	#_x ".somenum"
  117 #define DB_SCHEMA_USER(_x) \
  118 	#_x ".cid" "," \
  119 	#_x ".sex" "," \
  120 	#_x ".hash" "," \
  121 	#_x ".email" "," \
  122 	#_x ".image" "," \
  123 	#_x ".name" "," \
  124 	#_x ".uid"
  125 #define DB_SCHEMA_SESSION(_x) \
  126 	#_x ".userid" "," \
  127 	#_x ".token" "," \
  128 	#_x ".mtime" "," \
  129 	#_x ".id"
  130 
  131 /*
  132  * All of the fields we validate.
  133  * These are as VALID_XXX_YYY, where XXX is the structure and YYY is the 
  134  * field.
  135  * Only native types are listed.
  136  */
  137 enum	valid_keys {
  138 	VALID_COMPANY_NAME,
  139 	VALID_COMPANY_ID,
  140 	VALID_COMPANY_SOMENUM,
  141 	VALID_USER_CID,
  142 	VALID_USER_SEX,
  143 	VALID_USER_HASH,
  144 	VALID_USER_EMAIL,
  145 	VALID_USER_IMAGE,
  146 	VALID_USER_NAME,
  147 	VALID_USER_UID,
  148 	VALID_SESSION_USERID,
  149 	VALID_SESSION_TOKEN,
  150 	VALID_SESSION_MTIME,
  151 	VALID_SESSION_ID,
  152 	VALID__MAX
  153 };
  154 
  155 /*
  156  * Validation fields.
  157  * Pass this directly into khttp_parse(3) to use them as-is.
  158  * The functions are "valid_xxx_yyy", where "xxx" is the struct and "yyy" 
  159  * the field, and can be used standalone.
  160  * The form inputs are named "xxx-yyy".
  161  */
  162 extern const struct kvalid valid_keys[VALID__MAX];
  163 
  164 /*
  165  * Possible error returns from jsmn_parse(), if returning a <0 error 
  166  * code.
  167  */
  168 enum jsmnerr_t {
  169 	JSMN_ERROR_NOMEM = -1,
  170 	JSMN_ERROR_INVAL = -2,
  171 	JSMN_ERROR_PART = -3
  172 };
  173 
  174 /*
  175  * Type of JSON token
  176  */
  177 typedef enum {
  178 	JSMN_UNDEFINED = 0,
  179 	JSMN_OBJECT = 1,
  180 	JSMN_ARRAY = 2,
  181 	JSMN_STRING = 3,
  182 	JSMN_PRIMITIVE = 4
  183 } jsmntype_t;
  184 
  185 /*
  186  * JSON token description.
  187  */
  188 typedef struct {
  189 	jsmntype_t type;
  190 	int start;
  191 	int end;
  192 	int size;
  193 } jsmntok_t;
  194 
  195 /*
  196  * JSON parser. Contains an array of token blocks available. Also stores 
  197  * the string being parsed now and current position in that string.
  198  */
  199 typedef struct {
  200 	unsigned int pos;
  201 	unsigned int toknext;
  202 	int toksuper;
  203 } jsmn_parser;
  204 
  205 __BEGIN_DECLS
  206 
  207 /*
  208  * Allocate and open the database in "file". This opens the database in 
  209  * "safe exit" mode (see ksql(3)).
  210  * Note: the database has been opened in a child process, so the 
  211  * application may be sandboxed liberally.
  212  * Returns a pointer to the database or NULL on memory exhaustion.
  213  * The returned pointer must be closed with db_close().
  214  */
  215 struct ksql *db_open(const char *file);
  216 
  217 /*
  218  * Open a transaction with identifier "id".
  219  * If "mode" is 0, the transaction is opened in "deferred" mode, meaning 
  220  * that the database is read-locked (no writes allowed) on the first read 
  221  * operation, and write-locked on the first write (only the current 
  222  * process can write).
  223  * If "mode" is >0, the transaction immediately starts a write-lock.
  224  * If "mode" is <0, the transaction starts in a write-pending, where no 
  225  * other locks can be held at the same time.
  226  */
  227 void db_trans_open(struct ksql *p, size_t id, int mode);
  228 
  229 /*
  230  * Roll-back an open transaction.
  231  */
  232 void db_trans_rollback(struct ksql *p, size_t id);
  233 
  234 /*
  235  * Commit an open transaction.
  236  */
  237 void db_trans_commit(struct ksql *p, size_t id);
  238 
  239 /*
  240  * Close the context opened by db_open().
  241  * Has no effect if "p" is NULL.
  242  */
  243 void db_close(struct ksql *p);
  244 
  245 /*
  246  * Clear resources and free "p".
  247  * Has no effect if "p" is NULL.
  248  */
  249 void db_company_free(struct company *p);
  250 
  251 /*
  252  * Unfill and free all queue members.
  253  * Has no effect if "q" is NULL.
  254  */
  255 void db_company_freeq(struct company_q *q);
  256 
  257 /*
  258  * Fill in a company from an open statement "stmt".
  259  * This starts grabbing results from "pos", which may be NULL to start 
  260  * from zero.
  261  * This follows DB_SCHEMA_COMPANY's order for columns.
  262  */
  263 void db_company_fill(struct company *p, struct ksqlstmt *stmt, size_t *pos);
  264 /*
  265  * Free resources from "p" and all nested objects.
  266  * Does not free the "p" pointer itself.
  267  * Has no effect if "p" is NULL.
  268  */
  269 void db_company_unfill(struct company *p);
  270 
  271 /*
  272  * Insert a new row into the database.
  273  * Only native (and non-rowid) fields may be set.
  274  * 	v1: name
  275  * 	v2: somenum
  276  * Returns the new row's identifier on success or <0 otherwise.
  277  */
  278 int64_t db_company_insert(struct ksql *db, const char *v1, int64_t *v2);
  279 
  280 /*
  281  * Search for a set of company.
  282  * Queries on the following fields in struct company:
  283  * 	somenum (not an argument: checked is null)
  284  * Always returns a queue pointer.
  285  * Free this with db_company_freeq().
  286  */
  287 struct company_q *db_company_list_by_somenum_isnull(struct ksql *db);
  288 
  289 /*
  290  * Clear resources and free "p".
  291  * Has no effect if "p" is NULL.
  292  */
  293 void db_user_free(struct user *p);
  294 
  295 /*
  296  * Fill in a user from an open statement "stmt".
  297  * This starts grabbing results from "pos", which may be NULL to start 
  298  * from zero.
  299  * This follows DB_SCHEMA_USER's order for columns.
  300  */
  301 void db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos);
  302 /*
  303  * Free resources from "p" and all nested objects.
  304  * Does not free the "p" pointer itself.
  305  * Has no effect if "p" is NULL.
  306  */
  307 void db_user_unfill(struct user *p);
  308 
  309 /*
  310  * Insert a new row into the database.
  311  * Only native (and non-rowid) fields may be set.
  312  * 	v1: cid
  313  * 	v2: sex
  314  * 	v3: hash (pre-hashed password)
  315  * 	v4: email
  316  * 	v5: image
  317  * 	v6: name
  318  * Returns the new row's identifier on success or <0 otherwise.
  319  */
  320 int64_t db_user_insert(struct ksql *db, int64_t v1, enum sex v2, const char *v3,
  321 	const char *v4, size_t v5_sz, const void **v5, const char *v6);
  322 
  323 /*
  324  * Create a function that searches for users by a given
  325  * name; and, when found, invokes a callback function
  326  * provided the user structure.
  327  * This callback function is called during an implicit transaction: thus, 
  328  * it should not invoke any database modifications or risk deadlock.
  329  * Queries on the following fields in struct user:
  330  * 	v1: name (equals)
  331  * Invokes the given callback with retrieved data.
  332  */
  333 void db_user_iterate_by_name_eq(struct ksql *db, user_cb cb, void *arg, const char *v1);
  334 
  335 /*
  336  * Search for a unique user with their e-mail and
  337  * password.
  338  * This is a quick way to verify that a user has entered
  339  * the correct password for logging in.
  340  * Queries on the following fields in struct user:
  341  * 	v1: email (equals)
  342  * 	v2: hash (pre-hashed password)
  343  * Returns a pointer or NULL on fail.
  344  * Free the pointer with db_user_free().
  345  */
  346 struct user *db_user_get_creds(struct ksql *db, const char *v1, const char *v2);
  347 
  348 /*
  349  * Lookup by unique identifier.
  350  * Queries on the following fields in struct user:
  351  * 	v1: uid (equals)
  352  * Returns a pointer or NULL on fail.
  353  * Free the pointer with db_user_free().
  354  */
  355 struct user *db_user_get_by_uid_eq(struct ksql *db, int64_t v1);
  356 
  357 /*
  358  * Updates the given fields in struct user:
  359  * 	v1: hash (password)
  360  * Constrains the updated records to:
  361  * 	v2: uid (equals)
  362  * Returns zero on failure, non-zero on constraint errors.
  363  */
  364 int db_user_update_hash_by_uid_eq(struct ksql *db, const char *v1, int64_t v2);
  365 
  366 /*
  367  * Updates the given fields in struct user:
  368  * 	v1: email
  369  * Constrains the updated records to:
  370  * 	v2: uid (equals)
  371  * Returns zero on failure, non-zero on constraint errors.
  372  */
  373 int db_user_update_email_by_uid_eq(struct ksql *db, const char *v1, int64_t v2);
  374 
  375 /*
  376  * Clear resources and free "p".
  377  * Has no effect if "p" is NULL.
  378  */
  379 void db_session_free(struct session *p);
  380 
  381 /*
  382  * Fill in a session from an open statement "stmt".
  383  * This starts grabbing results from "pos", which may be NULL to start 
  384  * from zero.
  385  * This follows DB_SCHEMA_SESSION's order for columns.
  386  */
  387 void db_session_fill(struct session *p, struct ksqlstmt *stmt, size_t *pos);
  388 /*
  389  * Free resources from "p" and all nested objects.
  390  * Does not free the "p" pointer itself.
  391  * Has no effect if "p" is NULL.
  392  */
  393 void db_session_unfill(struct session *p);
  394 
  395 /*
  396  * Insert a new row into the database.
  397  * Only native (and non-rowid) fields may be set.
  398  * 	v1: userid
  399  * 	v2: token
  400  * 	v3: mtime
  401  * Returns the new row's identifier on success or <0 otherwise.
  402  */
  403 int64_t db_session_insert(struct ksql *db, int64_t v1, int64_t v2, time_t v3);
  404 
  405 /*
  406  * Search for company's logged-in users.
  407  * This callback function is called during an implicit transaction: thus, 
  408  * it should not invoke any database modifications or risk deadlock.
  409  * Queries on the following fields in struct session:
  410  * 	v1: user.company.name (equals)
  411  * 	v2: mtime (equals)
  412  * Invokes the given callback with retrieved data.
  413  */
  414 void db_session_iterate_foo(struct ksql *db, session_cb cb, void *arg, const char *v1,
  415 	time_t v2);
  416 
  417 /*
  418  * Constrains the deleted records to:
  419  * 	v1: id (equals)
  420  * Returns zero on failure, non-zero on constraint errors.
  421  */
  422 int db_session_delete_by_id_eq(struct ksql *db, int64_t v1);
  423 
  424 /*
  425  * Print out the fields of a company in JSON including nested 
  426  * structures.
  427  * Omits any password entries or those marked "noexport".
  428  * See json_company_obj() for the full object.
  429  */
  430 void json_company_data(struct kjsonreq *r, const struct company *p);
  431 
  432 /*
  433  * Emit the JSON key-value pair for the object:
  434  * 	"company" : { [data]+ }
  435  * See json_company_data() for the data.
  436  */
  437 void json_company_obj(struct kjsonreq *r, const struct company *p);
  438 
  439 /*
  440  * Emit the JSON key-value pair for the array:
  441  * 	"company_q" : [ [{data}]+ ]
  442  * See json_company_data() for the data.
  443  */
  444 void json_company_array(struct kjsonreq *r, const struct company_q *q);
  445 
  446 /*
  447  * Print out the fields of a user in JSON including nested 
  448  * structures.
  449  * Omits any password entries or those marked "noexport".
  450  * See json_user_obj() for the full object.
  451  */
  452 void json_user_data(struct kjsonreq *r, const struct user *p);
  453 
  454 /*
  455  * Emit the JSON key-value pair for the object:
  456  * 	"user" : { [data]+ }
  457  * See json_user_data() for the data.
  458  */
  459 void json_user_obj(struct kjsonreq *r, const struct user *p);
  460 
  461 /*
  462  * Emit the object as a standalone part of (presumably) an array:
  463  * 	"{ data }
  464  * See json_user_data() for the data.
  465  * The "void" argument is taken to be a kjsonreq as if were invoked from 
  466  * an iterator.
  467  */
  468 void json_user_iterate(const struct user *p, void *arg);
  469 
  470 /*
  471  * Print out the fields of a session in JSON including nested 
  472  * structures.
  473  * Omits any password entries or those marked "noexport".
  474  * See json_session_obj() for the full object.
  475  */
  476 void json_session_data(struct kjsonreq *r, const struct session *p);
  477 
  478 /*
  479  * Emit the JSON key-value pair for the object:
  480  * 	"session" : { [data]+ }
  481  * See json_session_data() for the data.
  482  */
  483 void json_session_obj(struct kjsonreq *r, const struct session *p);
  484 
  485 /*
  486  * Emit the object as a standalone part of (presumably) an array:
  487  * 	"{ data }
  488  * See json_session_data() for the data.
  489  * The "void" argument is taken to be a kjsonreq as if were invoked from 
  490  * an iterator.
  491  */
  492 void json_session_iterate(const struct session *p, void *arg);
  493 
  494 /*
  495  * Check whether the current token in a JSON parse sequence "tok" parsed 
  496  * from "json" is equal to a string.
  497  * Usually used when checking for key equality.
  498  * Returns non-zero on equality, zero otherwise.
  499  */
  500 int jsmn_eq(const char *json,
  501 	const jsmntok_t *tok, const char *s);
  502 
  503 /*
  504  * Initialise a JSON parser sequence "p".
  505  */
  506 void jsmn_init(jsmn_parser *p);
  507 
  508 /*
  509  * Parse a buffer "buf" of length "sz" into tokens "toks" of length 
  510  * "toksz" with parser "p".
  511  * Returns the number of tokens parsed or <0 on failure (possible errors 
  512  * described in enum jsmnerr_t).
  513  * If passed NULL "toks", simply computes the number of tokens required.
  514  */
  515 int jsmn_parse(jsmn_parser *p, const char *buf,
  516 	size_t sz, jsmntok_t *toks, unsigned int toksz);
  517 
  518 /*
  519  * Deserialise the parsed JSON buffer "buf", which need not be NUL 
  520  * terminated, with parse tokens "t" of length "toksz", into "p".
  521  * Returns 0 on parse failure, <0 on memory allocation failure, or the 
  522  * count of tokens parsed on success.
  523  */
  524 int jsmn_company(struct company *p, const char *buf, const jsmntok_t *t, size_t toksz);
  525 
  526 /*
  527  * Deserialise the parsed JSON buffer "buf", which need not be NUL 
  528  * terminated, with parse tokens "t" of length "toksz", into an array "p" 
  529  * allocated with "sz" elements.
  530  * The array must be freed with jsmn_company_free_array().
  531  * Returns 0 on parse failure, <0 on memory allocation failure, or the 
  532  * count of tokens parsed on success.
  533  */
  534 int jsmn_company_array(struct company **p, size_t *sz, const char *buf, const jsmntok_t *t, size_t toksz);
  535 
  536 /*
  537  * Free an array from jsmn_company_array(). Frees the pointer as 
  538  * well.
  539  * May be passed NULL.
  540  */
  541 void jsmn_company_free_array(struct company *p, size_t sz);
  542 
  543 /*
  544  * Clear memory from jsmn_company(). Does not touch the pointer 
  545  * itself.
  546  * May be passed NULL.
  547  */
  548 void jsmn_company_clear(struct company *p);
  549 
  550 /*
  551  * Deserialise the parsed JSON buffer "buf", which need not be NUL 
  552  * terminated, with parse tokens "t" of length "toksz", into "p".
  553  * Returns 0 on parse failure, <0 on memory allocation failure, or the 
  554  * count of tokens parsed on success.
  555  */
  556 int jsmn_user(struct user *p, const char *buf, const jsmntok_t *t, size_t toksz);
  557 
  558 /*
  559  * Deserialise the parsed JSON buffer "buf", which need not be NUL 
  560  * terminated, with parse tokens "t" of length "toksz", into an array "p" 
  561  * allocated with "sz" elements.
  562  * The array must be freed with jsmn_user_free_array().
  563  * Returns 0 on parse failure, <0 on memory allocation failure, or the 
  564  * count of tokens parsed on success.
  565  */
  566 int jsmn_user_array(struct user **p, size_t *sz, const char *buf, const jsmntok_t *t, size_t toksz);
  567 
  568 /*
  569  * Free an array from jsmn_user_array(). Frees the pointer as well.
  570  * May be passed NULL.
  571  */
  572 void jsmn_user_free_array(struct user *p, size_t sz);
  573 
  574 /*
  575  * Clear memory from jsmn_user(). Does not touch the pointer itself.
  576  * May be passed NULL.
  577  */
  578 void jsmn_user_clear(struct user *p);
  579 
  580 /*
  581  * Deserialise the parsed JSON buffer "buf", which need not be NUL 
  582  * terminated, with parse tokens "t" of length "toksz", into "p".
  583  * Returns 0 on parse failure, <0 on memory allocation failure, or the 
  584  * count of tokens parsed on success.
  585  */
  586 int jsmn_session(struct session *p, const char *buf, const jsmntok_t *t, size_t toksz);
  587 
  588 /*
  589  * Deserialise the parsed JSON buffer "buf", which need not be NUL 
  590  * terminated, with parse tokens "t" of length "toksz", into an array "p" 
  591  * allocated with "sz" elements.
  592  * The array must be freed with jsmn_session_free_array().
  593  * Returns 0 on parse failure, <0 on memory allocation failure, or the 
  594  * count of tokens parsed on success.
  595  */
  596 int jsmn_session_array(struct session **p, size_t *sz, const char *buf, const jsmntok_t *t, size_t toksz);
  597 
  598 /*
  599  * Free an array from jsmn_session_array(). Frees the pointer as 
  600  * well.
  601  * May be passed NULL.
  602  */
  603 void jsmn_session_free_array(struct session *p, size_t sz);
  604 
  605 /*
  606  * Clear memory from jsmn_session(). Does not touch the pointer 
  607  * itself.
  608  * May be passed NULL.
  609  */
  610 void jsmn_session_clear(struct session *p);
  611 
  612 /*
  613  * Validation routines for the name field in struct company.
  614  */
  615 int valid_company_name(struct kpair *p);
  616 
  617 /*
  618  * Validation routines for the id field in struct company.
  619  */
  620 int valid_company_id(struct kpair *p);
  621 
  622 /*
  623  * Validation routines for the somenum field in struct company.
  624  */
  625 int valid_company_somenum(struct kpair *p);
  626 
  627 /*
  628  * Validation routines for the company field in struct user.
  629  */
  630 int valid_user_company(struct kpair *p);
  631 
  632 /*
  633  * Validation routines for the cid field in struct user.
  634  */
  635 int valid_user_cid(struct kpair *p);
  636 
  637 /*
  638  * Validation routines for the sex field in struct user.
  639  */
  640 int valid_user_sex(struct kpair *p);
  641 
  642 /*
  643  * Validation routines for the hash field in struct user.
  644  */
  645 int valid_user_hash(struct kpair *p);
  646 
  647 /*
  648  * Validation routines for the email field in struct user.
  649  */
  650 int valid_user_email(struct kpair *p);
  651 
  652 /*
  653  * Validation routines for the image field in struct user.
  654  */
  655 int valid_user_image(struct kpair *p);
  656 
  657 /*
  658  * Validation routines for the name field in struct user.
  659  */
  660 int valid_user_name(struct kpair *p);
  661 
  662 /*
  663  * Validation routines for the uid field in struct user.
  664  */
  665 int valid_user_uid(struct kpair *p);
  666 
  667 /*
  668  * Validation routines for the user field in struct session.
  669  */
  670 int valid_session_user(struct kpair *p);
  671 
  672 /*
  673  * Validation routines for the userid field in struct session.
  674  */
  675 int valid_session_userid(struct kpair *p);
  676 
  677 /*
  678  * Validation routines for the token field in struct session.
  679  */
  680 int valid_session_token(struct kpair *p);
  681 
  682 /*
  683  * Validation routines for the mtime field in struct session.
  684  */
  685 int valid_session_mtime(struct kpair *p);
  686 
  687 /*
  688  * Validation routines for the id field in struct session.
  689  */
  690 int valid_session_id(struct kpair *p);
  691 
  692 __END_DECLS
  693 
  694 #endif