1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by kwebapp 0.3.1.
    6  * DO NOT EDIT!
    7  */
    8 
    9 /*
   10  * Our roles for access control.
   11  * When the database is first opened, the system is set to 
   12  * ROLE_default.
   13  * Roles may then be set using the kwbp_role() function.
   14  */
   15 enum	kwbp_role {
   16 	/* Role that isn't allowed to do anything. */
   17 	ROLE_none,
   18 	/*
   19 	 * The default role.
   20 	 * This is assigned when db_open() is called.
   21 	 * It should be limited only to those functions required to narrow 
   22 	 * the role.
   23 	 */
   24 	ROLE_default,
   25 	ROLE_user,
   26 	ROLE_admin
   27 };
   28 
   29 struct	user {
   30 	char	*email;
   31 	char	*hash;
   32 	int64_t	 id;
   33 };
   34 
   35 struct	session {
   36 	struct user user;
   37 	/* User associated with session. */
   38 	int64_t	 userid;
   39 	int64_t	 id;
   40 };
   41 
   42 /*
   43  * Define our table columns.
   44  * Use these when creating your own SQL statements, combined with the 
   45  * db_xxxx_fill functions.
   46  * Each macro must be given a unique alias name.
   47  * This allows for doing multiple inner joins on the same table.
   48  */
   49 #define DB_SCHEMA_USER(_x) \
   50 	#_x ".email" "," \
   51 	#_x ".hash" "," \
   52 	#_x ".id"
   53 #define DB_SCHEMA_SESSION(_x) \
   54 	#_x ".userid" "," \
   55 	#_x ".id"
   56 
   57 /*
   58  * All of the fields we validate.
   59  * These are as VALID_XXX_YYY, where XXX is the structure and YYY is the 
   60  * field.
   61  * Only native types are listed.
   62  */
   63 enum	valid_keys {
   64 	VALID_USER_EMAIL,
   65 	VALID_USER_HASH,
   66 	VALID_USER_ID,
   67 	VALID_SESSION_USERID,
   68 	VALID_SESSION_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 an opaque pointer or NULL on memory exhaustion.
   89  * The returned pointer must be closed with db_close().
   90  */
   91 struct kwbp *db_open(const char *file);
   92 
   93 /*
   94  * Close the context opened by db_open().
   95  * Has no effect if "p" is NULL.
   96  */
   97 void db_close(struct kwbp *p);
   98 
   99 /*
  100  * Drop into a new role.
  101  * If the role is the same as the current one, this is a noop.
  102  * We can only refine roles (i.e., descend the role tree), not ascend or 
  103  * move laterally.
  104  * Attempting to do so causes abort(2) to be called.
  105  * The only exceptions are when leaving ROLE_default or when entering 
  106  * ROLE_none.
  107  */
  108 void db_role(struct kwbp *ctx, enum kwbp_role r);
  109 
  110 /*
  111  * Unfill resources and free "p".
  112  * Has no effect if "p" is NULL.
  113  */
  114 void db_user_free(struct user *p);
  115 
  116 /*
  117  * Fill in a user from an open statement "stmt".
  118  * This starts grabbing results from "pos", which may be NULL to start 
  119  * from zero.
  120  * This follows DB_SCHEMA_USER's order for columns.
  121  */
  122 void db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos);
  123 
  124 /*
  125  * Free memory allocated by db_user_fill().
  126  * Has not effect if "p" is NULL.
  127  */
  128 void db_user_unfill(struct user *p);
  129 
  130 /*
  131  * Lookup by credentials.
  132  * Uses the given fields in struct user:
  133  * 	v1: email
  134  * 	v2: hash (pre-hashed password)
  135  * Returns a pointer or NULL on fail.
  136  * Free the pointer with db_user_free().
  137  */
  138 struct user *db_user_get_creds(struct kwbp *ctx, const char *v1, const char *v2);
  139 
  140 /*
  141  * Print out the fields of a user in JSON including nested 
  142  * structures.
  143  * Omits any password entries or those marked "noexport".
  144  * See json_user_obj() for the full object.
  145  */
  146 void json_user_data(struct kjsonreq *r, const struct user *p);
  147 
  148 /*
  149  * Emit the JSON key-value pair for the object:
  150  * 	"user" : { [data]+ }
  151  * See json_user_data() for the data.
  152  */
  153 void json_user_obj(struct kjsonreq *r, const struct user *p);
  154 
  155 /*
  156  * Validation routines for the email field in struct user.
  157  */
  158 int valid_user_email(struct kpair *p);
  159 
  160 /*
  161  * Validation routines for the hash field in struct user.
  162  */
  163 int valid_user_hash(struct kpair *p);
  164 
  165 /*
  166  * Validation routines for the id field in struct user.
  167  */
  168 int valid_user_id(struct kpair *p);
  169 
  170 /*
  171  * Unfill resources and free "p".
  172  * Has no effect if "p" is NULL.
  173  */
  174 void db_session_free(struct session *p);
  175 
  176 /*
  177  * Fill in a session from an open statement "stmt".
  178  * This starts grabbing results from "pos", which may be NULL to start 
  179  * from zero.
  180  * This follows DB_SCHEMA_SESSION's order for columns.
  181  */
  182 void db_session_fill(struct session *p, struct ksqlstmt *stmt, size_t *pos);
  183 
  184 /*
  185  * Insert a new row into the database.
  186  * Only native (and non-rowid) fields may be set.
  187  * 	v1: userid
  188  * Returns the new row's identifier on success or <0 otherwise.
  189  */
  190 int64_t db_session_insert(struct kwbp *ctx, int64_t v1);
  191 
  192 /*
  193  * Free memory allocated by db_session_fill().
  194  * Has not effect if "p" is NULL.
  195  */
  196 void db_session_unfill(struct session *p);
  197 
  198 /*
  199  * Lookup by unique identifier.
  200  * Uses the given fields in struct session:
  201  * 	v1: id
  202  * Returns a pointer or NULL on fail.
  203  * Free the pointer with db_session_free().
  204  */
  205 struct session *db_session_get_id(struct kwbp *ctx, int64_t v1);
  206 
  207 /*
  208  * Constrains the deleted records to:
  209  * 	v1: id
  210  * Returns zero on failure, non-zero on constraint errors.
  211  */
  212 int db_session_delete_by_id_eq(struct kwbp *ctx, int64_t v1);
  213 
  214 /*
  215  * Print out the fields of a session in JSON including nested 
  216  * structures.
  217  * Omits any password entries or those marked "noexport".
  218  * See json_session_obj() for the full object.
  219  */
  220 void json_session_data(struct kjsonreq *r, const struct session *p);
  221 
  222 /*
  223  * Emit the JSON key-value pair for the object:
  224  * 	"session" : { [data]+ }
  225  * See json_session_data() for the data.
  226  */
  227 void json_session_obj(struct kjsonreq *r, const struct session *p);
  228 
  229 /*
  230  * Validation routines for the user field in struct session.
  231  */
  232 int valid_session_user(struct kpair *p);
  233 
  234 /*
  235  * Validation routines for the userid field in struct session.
  236  */
  237 int valid_session_userid(struct kpair *p);
  238 
  239 /*
  240  * Validation routines for the id field in struct session.
  241  */
  242 int valid_session_id(struct kpair *p);
  243 
  244 __END_DECLS
  245 
  246 #endif