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