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