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