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  * Our roles for access control.
   18  * When the database is first opened, the system is set to 
   19  * ROLE_default.
   20  * Roles may then be set using the ort_role() function.
   21  */
   22 enum	ort_role {
   23 	/* Role that isn't allowed to do anything. */
   24 	ROLE_none,
   25 	/*
   26 	 * The default role.
   27 	 * This is assigned when db_open() is called.
   28 	 * It should be limited only to those functions required to narrow 
   29 	 * the role.
   30 	 */
   31 	ROLE_default,
   32 	ROLE_user,
   33 	ROLE_admin
   34 };
   35 
   36 struct	user {
   37 	char	*email;
   38 	char	*hash;
   39 	int64_t	 id;
   40 	/* Private data used for role analysis. */
   41 	struct ort_store *priv_store;
   42 };
   43 
   44 struct	session {
   45 	struct user user;
   46 	/* User associated with session. */
   47 	int64_t	 userid;
   48 	int64_t	 id;
   49 	/* Private data used for role analysis. */
   50 	struct ort_store *priv_store;
   51 };
   52 
   53 /*
   54  * All of the fields we validate.
   55  * These are as VALID_XXX_YYY, where XXX is the structure and YYY is the 
   56  * field.
   57  * Only native types are listed.
   58  */
   59 enum	valid_keys {
   60 	VALID_USER_EMAIL,
   61 	VALID_USER_HASH,
   62 	VALID_USER_ID,
   63 	VALID_SESSION_USERID,
   64 	VALID_SESSION_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 an opaque pointer or NULL on memory exhaustion.
   85  * The returned pointer must be closed with db_close().
   86  */
   87 struct ort *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 ort *p, size_t id, int mode);
  100 
  101 /*
  102  * Roll-back an open transaction.
  103  */
  104 void db_trans_rollback(struct ort *p, size_t id);
  105 
  106 /*
  107  * Commit an open transaction.
  108  */
  109 void db_trans_commit(struct ort *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 ort *p);
  116 
  117 /*
  118  * Drop into a new role.
  119  * If the role is the same as the current one, this is a noop.
  120  * We can only refine roles (i.e., descend the role tree), not ascend or 
  121  * move laterally.
  122  * Attempting to do so causes abort(2) to be called.
  123  * The only exceptions are when leaving ROLE_default or when entering 
  124  * ROLE_none.
  125  */
  126 void db_role(struct ort *ctx, enum ort_role r);
  127 
  128 /*
  129  * Get the current role.
  130  */
  131 enum ort_role db_role_current(struct ort *ctx);
  132 
  133 /*
  134  * Get the role stored into "s".
  135  * This role is set when the object containing the stored role is 
  136  * created, such as when a "search" query function is called.
  137  */
  138 enum ort_role db_role_stored(struct ort_store *s);
  139 
  140 /*
  141  * Clear resources and free "p".
  142  * Has no effect if "p" is NULL.
  143  */
  144 void db_user_free(struct user *p);
  145 
  146 /*
  147  * Lookup by credentials.
  148  * Queries on the following fields in struct user:
  149  * 	v1: email (equals)
  150  * 	v2: hash (pre-hashed password)
  151  * Returns a pointer or NULL on fail.
  152  * Free the pointer with db_user_free().
  153  */
  154 struct user *db_user_get_creds(struct ort *ctx, const char *v1, const char *v2);
  155 
  156 /*
  157  * Clear resources and free "p".
  158  * Has no effect if "p" is NULL.
  159  */
  160 void db_session_free(struct session *p);
  161 
  162 /*
  163  * Insert a new row into the database.
  164  * Only native (and non-rowid) fields may be set.
  165  * 	v1: userid
  166  * Returns the new row's identifier on success or <0 otherwise.
  167  */
  168 int64_t db_session_insert(struct ort *ctx, int64_t v1);
  169 
  170 /*
  171  * Lookup by unique identifier.
  172  * Queries on the following fields in struct session:
  173  * 	v1: id (equals)
  174  * Returns a pointer or NULL on fail.
  175  * Free the pointer with db_session_free().
  176  */
  177 struct session *db_session_get_id(struct ort *ctx, int64_t v1);
  178 
  179 /*
  180  * Constrains the deleted records to:
  181  * 	v1: id (equals)
  182  * Returns zero on constraint violation, <0 on error, >0 on success
  183  */
  184 int db_session_delete_by_id_eq(struct ort *ctx, 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 session in JSON including nested 
  203  * structures.
  204  * Omits any password entries or those marked "noexport".
  205  * See json_session_obj() for the full object.
  206  */
  207 void json_session_data(struct kjsonreq *r, const struct session *p);
  208 
  209 /*
  210  * Emit the JSON key-value pair for the object:
  211  * 	"session" : { [data]+ }
  212  * See json_session_data() for the data.
  213  */
  214 void json_session_obj(struct kjsonreq *r, const struct session *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 session.
  233  */
  234 int valid_session_user(struct kpair *p);
  235 
  236 /*
  237  * Validation routines for the userid field in struct session.
  238  */
  239 int valid_session_userid(struct kpair *p);
  240 
  241 /*
  242  * Validation routines for the id field in struct session.
  243  */
  244 int valid_session_id(struct kpair *p);
  245 
  246 __END_DECLS
  247 
  248 #endif