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