1 /*
    2  * WARNING: automatically generated by kwebapp 0.4.12.
    3  * DO NOT EDIT!
    4  */
    5 #include <sys/queue.h>
    6 
    7 #include <assert.h>
    8 #include <stdarg.h>
    9 #include <stdint.h>
   10 #include <stdio.h>
   11 #include <stdlib.h>
   12 #include <string.h>
   13 #include <unistd.h>
   14 
   15 #include <ksql.h>
   16 #include <kcgi.h>
   17 #include <kcgijson.h>
   18 
   19 #include "rbac-ex1.h"
   20 
   21 /*
   22  * All SQL statements we'll later define in "stmts".
   23  */
   24 enum	stmt {
   25 	STMT_USER_BY_UNIQUE_email,
   26 	STMT_USER_BY_UNIQUE_id,
   27 	STMT_USER_BY_SEARCH_0,
   28 	STMT_USER_INSERT,
   29 	STMT_SESSION_BY_UNIQUE_id,
   30 	STMT_SESSION_BY_SEARCH_0,
   31 	STMT_SESSION_INSERT,
   32 	STMT_SESSION_DELETE_0,
   33 	STMT__MAX
   34 };
   35 
   36 /*
   37  * Our full set of SQL statements.
   38  * We define these beforehand because that's how ksql(3) handles 
   39  * statement generation.
   40  * Notice the "AS" part: this allows for multiple inner joins without 
   41  * ambiguity.
   42  */
   43 static	const char *const stmts[STMT__MAX] = {
   44 	/* STMT_USER_BY_UNIQUE_email */
   45 	"SELECT " DB_SCHEMA_USER(user) " FROM user WHERE user.email = ?",
   46 	/* STMT_USER_BY_UNIQUE_id */
   47 	"SELECT " DB_SCHEMA_USER(user) " FROM user WHERE user.id = ?",
   48 	/* STMT_USER_BY_SEARCH_0 */
   49 	"SELECT " DB_SCHEMA_USER(user) " FROM user "
   50 		"WHERE user.email = ?",
   51 	/* STMT_USER_INSERT */
   52 	"INSERT INTO user (email,hash) VALUES (?,?)",
   53 	/* STMT_SESSION_BY_UNIQUE_id */
   54 	"SELECT " DB_SCHEMA_SESSION(session) "," DB_SCHEMA_USER(_a) " FROM session "
   55 		"INNER JOIN user AS _a ON _a.id=session.userid "
   56 		"WHERE session.id = ?",
   57 	/* STMT_SESSION_BY_SEARCH_0 */
   58 	"SELECT " DB_SCHEMA_SESSION(session) "," DB_SCHEMA_USER(_a) " FROM session "
   59 		"INNER JOIN user AS _a ON _a.id=session.userid "
   60 		"WHERE session.id = ?",
   61 	/* STMT_SESSION_INSERT */
   62 	"INSERT INTO session (userid) VALUES (?)",
   63 	/* STMT_SESSION_DELETE_0 */
   64 	"DELETE FROM session WHERE id = ?",
   65 };
   66 
   67 const struct kvalid valid_keys[VALID__MAX] = {
   68 	{ valid_user_email, "user-email" },
   69 	{ valid_user_hash, "user-hash" },
   70 	{ valid_user_id, "user-id" },
   71 	{ valid_session_userid, "session-userid" },
   72 	{ valid_session_id, "session-id" },
   73 };
   74 
   75 /*
   76  * Finally, all of the functions we'll use.
   77  */
   78 
   79 void
   80 db_trans_open(struct ksql *p, size_t id, int mode)
   81 {
   82 	if (mode < 0)
   83 		ksql_trans_exclopen(p, id);
   84 	else if (mode > 0)
   85 		ksql_trans_singleopen(p, id);
   86 	else
   87 		ksql_trans_open(p, id);
   88 }
   89 
   90 void
   91 db_trans_rollback(struct ksql *p, size_t id)
   92 {
   93 	ksql_trans_rollback(p, id);
   94 }
   95 
   96 void
   97 db_trans_commit(struct ksql *p, size_t id)
   98 {
   99 	ksql_trans_commit(p, id);
  100 }
  101 
  102 struct ksql *
  103 db_open(const char *file)
  104 {
  105 	struct ksqlcfg cfg;
  106 	struct ksql *db;
  107 
  108 	ksql_cfg_defaults(&cfg);
  109 
  110 	db = ksql_alloc(&cfg);
  111 	if (NULL == db)
  112 		return(NULL);
  113 	ksql_open(db, file);
  114 	return(db);
  115 }
  116 
  117 void
  118 db_close(struct ksql *p)
  119 {
  120 	if (NULL == p)
  121 		return;
  122 	ksql_close(p);
  123 	ksql_free(p);
  124 }
  125 
  126 void
  127 db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos)
  128 {
  129 	size_t i = 0;
  130 	enum ksqlc c;
  131 
  132 	if (NULL == pos)
  133 		pos = &i;
  134 	memset(p, 0, sizeof(*p));
  135 	c = ksql_result_str_alloc(stmt, &p->email, (*pos)++);
  136 	if (KSQL_OK != c)
  137 		exit(EXIT_FAILURE);
  138 	c = ksql_result_str_alloc(stmt, &p->hash, (*pos)++);
  139 	if (KSQL_OK != c)
  140 		exit(EXIT_FAILURE);
  141 	c = ksql_result_int(stmt, &p->id, (*pos)++);
  142 	if (KSQL_OK != c)
  143 		exit(EXIT_FAILURE);
  144 }
  145 
  146 static void
  147 db_user_fill_r(struct user *p, struct ksqlstmt *stmt, size_t *pos)
  148 {
  149 	size_t i = 0;
  150 
  151 	if (NULL == pos)
  152 		pos = &i;
  153 	db_user_fill(p, stmt, pos);
  154 }
  155 
  156 void
  157 db_user_unfill(struct user *p)
  158 {
  159 	if (NULL == p)
  160 		return;
  161 	free(p->email);
  162 	free(p->hash);
  163 }
  164 
  165 static void
  166 db_user_unfill_r(struct user *p)
  167 {
  168 	if (NULL == p)
  169 		return;
  170 
  171 	db_user_unfill(p);
  172 }
  173 
  174 void
  175 db_user_free(struct user *p)
  176 {
  177 	db_user_unfill_r(p);
  178 	free(p);
  179 }
  180 
  181 void
  182 json_user_data(struct kjsonreq *r, const struct user *p)
  183 {
  184 	kjson_putstringp(r, "email", p->email);
  185 
  186 	/* Omitting hash: is a password hash. */
  187 
  188 	kjson_putintp(r, "id", p->id);
  189 }
  190 
  191 void
  192 json_user_obj(struct kjsonreq *r, const struct user *p)
  193 {
  194 	kjson_objp_open(r, "user");
  195 	json_user_data(r, p);
  196 	kjson_obj_close(r);
  197 }
  198 
  199 int
  200 valid_user_email(struct kpair *p)
  201 {
  202 	if ( ! kvalid_email(p))
  203 		return 0;
  204 	return 1;
  205 }
  206 
  207 int
  208 valid_user_hash(struct kpair *p)
  209 {
  210 	if ( ! kvalid_string(p))
  211 		return 0;
  212 	return 1;
  213 }
  214 
  215 int
  216 valid_user_id(struct kpair *p)
  217 {
  218 	if ( ! kvalid_int(p))
  219 		return 0;
  220 	return 1;
  221 }
  222 
  223 struct user *
  224 db_user_get_creds(struct ksql *db, const char *v1, const char *v2)
  225 {
  226 	struct ksqlstmt *stmt;
  227 	struct user *p = NULL;
  228 
  229 	ksql_stmt_alloc(db, &stmt,
  230 		stmts[STMT_USER_BY_SEARCH_0],
  231 		STMT_USER_BY_SEARCH_0);
  232 	ksql_bind_str(stmt, 0, v1);
  233 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  234 		p = malloc(sizeof(struct user));
  235 		if (NULL == p) {
  236 			perror(NULL);
  237 			exit(EXIT_FAILURE);
  238 		}
  239 		db_user_fill_r(p, stmt, NULL);
  240 		if (NULL != p && crypt_checkpass(v2, p->hash) < 0) {
  241 			db_user_free(p);
  242 			p = NULL;
  243 		}
  244 	}
  245 	ksql_stmt_free(stmt);
  246 	return p;
  247 }
  248 
  249 void
  250 db_session_fill(struct session *p, struct ksqlstmt *stmt, size_t *pos)
  251 {
  252 	size_t i = 0;
  253 	enum ksqlc c;
  254 
  255 	if (NULL == pos)
  256 		pos = &i;
  257 	memset(p, 0, sizeof(*p));
  258 	c = ksql_result_int(stmt, &p->userid, (*pos)++);
  259 	if (KSQL_OK != c)
  260 		exit(EXIT_FAILURE);
  261 	c = ksql_result_int(stmt, &p->id, (*pos)++);
  262 	if (KSQL_OK != c)
  263 		exit(EXIT_FAILURE);
  264 }
  265 
  266 static void
  267 db_session_fill_r(struct session *p, struct ksqlstmt *stmt, size_t *pos)
  268 {
  269 	size_t i = 0;
  270 
  271 	if (NULL == pos)
  272 		pos = &i;
  273 	db_session_fill(p, stmt, pos);
  274 	db_user_fill_r(&p->user, stmt, pos);
  275 }
  276 
  277 void
  278 db_session_unfill(struct session *p)
  279 {
  280 	if (NULL == p)
  281 		return;
  282 }
  283 
  284 static void
  285 db_session_unfill_r(struct session *p)
  286 {
  287 	if (NULL == p)
  288 		return;
  289 
  290 	db_session_unfill(p);
  291 	db_user_unfill_r(&p->user);
  292 }
  293 
  294 void
  295 db_session_free(struct session *p)
  296 {
  297 	db_session_unfill_r(p);
  298 	free(p);
  299 }
  300 
  301 int64_t
  302 db_session_insert(struct ksql *db, int64_t v1)
  303 {
  304 	struct ksqlstmt *stmt;
  305 	int64_t id = -1;
  306 
  307 	ksql_stmt_alloc(db, &stmt,
  308 		stmts[STMT_SESSION_INSERT],
  309 		STMT_SESSION_INSERT);
  310 	ksql_bind_int(stmt, 0, v1);
  311 	if (KSQL_DONE == ksql_stmt_cstep(stmt))
  312 		ksql_lastid(db, &id);
  313 	ksql_stmt_free(stmt);
  314 	return(id);
  315 }
  316 
  317 void
  318 json_session_data(struct kjsonreq *r, const struct session *p)
  319 {
  320 	kjson_objp_open(r, "user");
  321 	json_user_data(r, &p->user);
  322 	kjson_obj_close(r);
  323 	kjson_putintp(r, "userid", p->userid);
  324 	kjson_putintp(r, "id", p->id);
  325 }
  326 
  327 void
  328 json_session_obj(struct kjsonreq *r, const struct session *p)
  329 {
  330 	kjson_objp_open(r, "session");
  331 	json_session_data(r, p);
  332 	kjson_obj_close(r);
  333 }
  334 
  335 int
  336 valid_session_userid(struct kpair *p)
  337 {
  338 	if ( ! kvalid_int(p))
  339 		return 0;
  340 	return 1;
  341 }
  342 
  343 int
  344 valid_session_id(struct kpair *p)
  345 {
  346 	if ( ! kvalid_int(p))
  347 		return 0;
  348 	return 1;
  349 }
  350 
  351 struct session *
  352 db_session_get_id(struct ksql *db, int64_t v1)
  353 {
  354 	struct ksqlstmt *stmt;
  355 	struct session *p = NULL;
  356 
  357 	ksql_stmt_alloc(db, &stmt,
  358 		stmts[STMT_SESSION_BY_SEARCH_0],
  359 		STMT_SESSION_BY_SEARCH_0);
  360 	ksql_bind_int(stmt, 0, v1);
  361 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  362 		p = malloc(sizeof(struct session));
  363 		if (NULL == p) {
  364 			perror(NULL);
  365 			exit(EXIT_FAILURE);
  366 		}
  367 		db_session_fill_r(p, stmt, NULL);
  368 	}
  369 	ksql_stmt_free(stmt);
  370 	return p;
  371 }
  372 
  373 int
  374 db_session_delete_by_id_eq(struct ksql *db, int64_t v1)
  375 {
  376 	struct ksqlstmt *stmt;
  377 	enum ksqlc c;
  378 
  379 	ksql_stmt_alloc(db, &stmt,
  380 		stmts[STMT_SESSION_DELETE_0],
  381 		STMT_SESSION_DELETE_0);
  382 	ksql_bind_int(stmt, 0, v1);
  383 	c = ksql_stmt_cstep(stmt);
  384 	ksql_stmt_free(stmt);
  385 	return(KSQL_CONSTRAINT != c);
  386 }
  387