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 "kwebapp.db.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_SESS_BY_UNIQUE_id,
   31 	STMT_SESS_BY_SEARCH_0,
   32 	STMT_SESS_INSERT,
   33 	STMT_SESS_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_SESS_BY_UNIQUE_id */
   55 	"SELECT " DB_SCHEMA_SESS(sess) "," DB_SCHEMA_USER(_a) " FROM sess "
   56 		"INNER JOIN user AS _a ON _a.id=sess.userid "
   57 		"WHERE sess.id = ?",
   58 	/* STMT_SESS_BY_SEARCH_0 */
   59 	"SELECT " DB_SCHEMA_SESS(sess) "," DB_SCHEMA_USER(_a) " FROM sess "
   60 		"INNER JOIN user AS _a ON _a.id=sess.userid "
   61 		"WHERE sess.token = ? AND sess.id = ?",
   62 	/* STMT_SESS_INSERT */
   63 	"INSERT INTO sess (userid,token) VALUES (?,?)",
   64 	/* STMT_SESS_DELETE_0 */
   65 	"DELETE FROM sess 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_sess_userid, "sess-userid" },
   73 	{ valid_sess_token, "sess-token" },
   74 	{ valid_sess_id, "sess-id" },
   75 };
   76 
   77 /*
   78  * Finally, all of the functions we'll use.
   79  */
   80 
   81 void
   82 db_trans_open(struct ksql *p, size_t id, int mode)
   83 {
   84 	if (mode < 0)
   85 		ksql_trans_exclopen(p, id);
   86 	else if (mode > 0)
   87 		ksql_trans_singleopen(p, id);
   88 	else
   89 		ksql_trans_open(p, id);
   90 }
   91 
   92 void
   93 db_trans_rollback(struct ksql *p, size_t id)
   94 {
   95 	ksql_trans_rollback(p, id);
   96 }
   97 
   98 void
   99 db_trans_commit(struct ksql *p, size_t id)
  100 {
  101 	ksql_trans_commit(p, id);
  102 }
  103 
  104 struct ksql *
  105 db_open(const char *file)
  106 {
  107 	struct ksqlcfg cfg;
  108 	struct ksql *db;
  109 
  110 	ksql_cfg_defaults(&cfg);
  111 	cfg.flags |= KSQL_FOREIGN_KEYS;
  112 
  113 	db = ksql_alloc(&cfg);
  114 	if (NULL == db)
  115 		return(NULL);
  116 	ksql_open(db, file);
  117 	return(db);
  118 }
  119 
  120 void
  121 db_close(struct ksql *p)
  122 {
  123 	if (NULL == p)
  124 		return;
  125 	ksql_close(p);
  126 	ksql_free(p);
  127 }
  128 
  129 void
  130 db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos)
  131 {
  132 	size_t i = 0;
  133 	enum ksqlc c;
  134 
  135 	if (NULL == pos)
  136 		pos = &i;
  137 	memset(p, 0, sizeof(*p));
  138 	c = ksql_result_str_alloc(stmt, &p->email, (*pos)++);
  139 	if (KSQL_OK != c)
  140 		exit(EXIT_FAILURE);
  141 	c = ksql_result_str_alloc(stmt, &p->hash, (*pos)++);
  142 	if (KSQL_OK != c)
  143 		exit(EXIT_FAILURE);
  144 	c = ksql_result_int(stmt, &p->id, (*pos)++);
  145 	if (KSQL_OK != c)
  146 		exit(EXIT_FAILURE);
  147 }
  148 
  149 static void
  150 db_user_fill_r(struct user *p, struct ksqlstmt *stmt, size_t *pos)
  151 {
  152 	size_t i = 0;
  153 
  154 	if (NULL == pos)
  155 		pos = &i;
  156 	db_user_fill(p, stmt, pos);
  157 }
  158 
  159 void
  160 db_user_unfill(struct user *p)
  161 {
  162 	if (NULL == p)
  163 		return;
  164 	free(p->email);
  165 	free(p->hash);
  166 }
  167 
  168 static void
  169 db_user_unfill_r(struct user *p)
  170 {
  171 	if (NULL == p)
  172 		return;
  173 
  174 	db_user_unfill(p);
  175 }
  176 
  177 void
  178 db_user_free(struct user *p)
  179 {
  180 	db_user_unfill_r(p);
  181 	free(p);
  182 }
  183 
  184 void
  185 json_user_data(struct kjsonreq *r, const struct user *p)
  186 {
  187 	kjson_putstringp(r, "email", p->email);
  188 
  189 	/* Omitting hash: is a password hash. */
  190 
  191 	kjson_putintp(r, "id", p->id);
  192 }
  193 
  194 void
  195 json_user_obj(struct kjsonreq *r, const struct user *p)
  196 {
  197 	kjson_objp_open(r, "user");
  198 	json_user_data(r, p);
  199 	kjson_obj_close(r);
  200 }
  201 
  202 int
  203 valid_user_email(struct kpair *p)
  204 {
  205 	if ( ! kvalid_string(p))
  206 		return 0;
  207 	if (p->valsz <= 3)
  208 		return 0;
  209 	if (p->valsz > 128)
  210 		return 0;
  211 	return 1;
  212 }
  213 
  214 int
  215 valid_user_hash(struct kpair *p)
  216 {
  217 	if ( ! kvalid_string(p))
  218 		return 0;
  219 	if (p->valsz <= 6)
  220 		return 0;
  221 	if (p->valsz > 128)
  222 		return 0;
  223 	return 1;
  224 }
  225 
  226 int
  227 valid_user_id(struct kpair *p)
  228 {
  229 	if ( ! kvalid_int(p))
  230 		return 0;
  231 	return 1;
  232 }
  233 
  234 struct user *
  235 db_user_get_creds(struct ksql *db, const char *v1, const char *v2)
  236 {
  237 	struct ksqlstmt *stmt;
  238 	struct user *p = NULL;
  239 
  240 	ksql_stmt_alloc(db, &stmt,
  241 		stmts[STMT_USER_BY_SEARCH_0],
  242 		STMT_USER_BY_SEARCH_0);
  243 	ksql_bind_str(stmt, 0, v1);
  244 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  245 		p = malloc(sizeof(struct user));
  246 		if (NULL == p) {
  247 			perror(NULL);
  248 			exit(EXIT_FAILURE);
  249 		}
  250 		db_user_fill_r(p, stmt, NULL);
  251 		if (NULL != p && (crypt_checkpass(v2, p->hash) < 0)) {
  252 			db_user_free(p);
  253 			p = NULL;
  254 		}
  255 	}
  256 	ksql_stmt_free(stmt);
  257 	return p;
  258 }
  259 
  260 void
  261 db_sess_fill(struct sess *p, struct ksqlstmt *stmt, size_t *pos)
  262 {
  263 	size_t i = 0;
  264 	enum ksqlc c;
  265 
  266 	if (NULL == pos)
  267 		pos = &i;
  268 	memset(p, 0, sizeof(*p));
  269 	c = ksql_result_int(stmt, &p->userid, (*pos)++);
  270 	if (KSQL_OK != c)
  271 		exit(EXIT_FAILURE);
  272 	c = ksql_result_int(stmt, &p->token, (*pos)++);
  273 	if (KSQL_OK != c)
  274 		exit(EXIT_FAILURE);
  275 	c = ksql_result_int(stmt, &p->id, (*pos)++);
  276 	if (KSQL_OK != c)
  277 		exit(EXIT_FAILURE);
  278 }
  279 
  280 static void
  281 db_sess_fill_r(struct sess *p, struct ksqlstmt *stmt, size_t *pos)
  282 {
  283 	size_t i = 0;
  284 
  285 	if (NULL == pos)
  286 		pos = &i;
  287 	db_sess_fill(p, stmt, pos);
  288 	db_user_fill_r(&p->user, stmt, pos);
  289 }
  290 
  291 void
  292 db_sess_unfill(struct sess *p)
  293 {
  294 	if (NULL == p)
  295 		return;
  296 }
  297 
  298 static void
  299 db_sess_unfill_r(struct sess *p)
  300 {
  301 	if (NULL == p)
  302 		return;
  303 
  304 	db_sess_unfill(p);
  305 	db_user_unfill_r(&p->user);
  306 }
  307 
  308 void
  309 db_sess_free(struct sess *p)
  310 {
  311 	db_sess_unfill_r(p);
  312 	free(p);
  313 }
  314 
  315 void
  316 json_sess_data(struct kjsonreq *r, const struct sess *p)
  317 {
  318 	kjson_objp_open(r, "user");
  319 	json_user_data(r, &p->user);
  320 	kjson_obj_close(r);
  321 	kjson_putintp(r, "userid", p->userid);
  322 	kjson_putintp(r, "token", p->token);
  323 	kjson_putintp(r, "id", p->id);
  324 }
  325 
  326 void
  327 json_sess_obj(struct kjsonreq *r, const struct sess *p)
  328 {
  329 	kjson_objp_open(r, "sess");
  330 	json_sess_data(r, p);
  331 	kjson_obj_close(r);
  332 }
  333 
  334 int
  335 valid_sess_userid(struct kpair *p)
  336 {
  337 	if ( ! kvalid_int(p))
  338 		return 0;
  339 	return 1;
  340 }
  341 
  342 int
  343 valid_sess_token(struct kpair *p)
  344 {
  345 	if ( ! kvalid_int(p))
  346 		return 0;
  347 	return 1;
  348 }
  349 
  350 int
  351 valid_sess_id(struct kpair *p)
  352 {
  353 	if ( ! kvalid_int(p))
  354 		return 0;
  355 	return 1;
  356 }
  357 
  358 struct sess *
  359 db_sess_get_creds(struct ksql *db, int64_t v1, int64_t v2)
  360 {
  361 	struct ksqlstmt *stmt;
  362 	struct sess *p = NULL;
  363 
  364 	ksql_stmt_alloc(db, &stmt,
  365 		stmts[STMT_SESS_BY_SEARCH_0],
  366 		STMT_SESS_BY_SEARCH_0);
  367 	ksql_bind_int(stmt, 0, v1);
  368 	ksql_bind_int(stmt, 1, v2);
  369 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  370 		p = malloc(sizeof(struct sess));
  371 		if (NULL == p) {
  372 			perror(NULL);
  373 			exit(EXIT_FAILURE);
  374 		}
  375 		db_sess_fill_r(p, stmt, NULL);
  376 	}
  377 	ksql_stmt_free(stmt);
  378 	return p;
  379 }
  380 
  381 int
  382 db_sess_delete_id(struct ksql *db, int64_t v1)
  383 {
  384 	struct ksqlstmt *stmt;
  385 	enum ksqlc c;
  386 
  387 	ksql_stmt_alloc(db, &stmt,
  388 		stmts[STMT_SESS_DELETE_0],
  389 		STMT_SESS_DELETE_0);
  390 	ksql_bind_int(stmt, 0, v1);
  391 	c = ksql_stmt_cstep(stmt);
  392 	ksql_stmt_free(stmt);
  393 	return(KSQL_DONE == c ? 1 : (KSQL_CONSTRAINT == c ? 0 : -1));
  394 }
  395