1 /*
    2  * WARNING: automatically generated by kwebapp 0.3.1.
    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 handles statement 
   35  * generation.
   36  * Notice the "AS" part: this allows for multiple inner joins without 
   37  * 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 *db;
   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 	db = ksql_alloc(&cfg);
   78 	if (NULL == db)
   79 		return(NULL);
   80 	ksql_open(db, file);
   81 	return(db);
   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 void
  150 json_user_data(struct kjsonreq *r, const struct user *p)
  151 {
  152 	kjson_putstringp(r, "email", p->email);
  153 	/* Omitting hash: is a password hash. */
  154 	kjson_putintp(r, "id", p->id);
  155 }
  156 
  157 void
  158 json_user_obj(struct kjsonreq *r, const struct user *p)
  159 {
  160 	kjson_objp_open(r, "user");
  161 	json_user_data(r, p);
  162 	kjson_obj_close(r);
  163 }
  164 
  165 int
  166 valid_user_email(struct kpair *p)
  167 {
  168 	if ( ! kvalid_string(p))
  169 		return(0);
  170 	if (p->valsz <= 3)
  171 		return(0);
  172 	if (p->valsz > 128)
  173 		return(0);
  174 	return(1);
  175 }
  176 
  177 int
  178 valid_user_hash(struct kpair *p)
  179 {
  180 	if ( ! kvalid_string(p))
  181 		return(0);
  182 	if (p->valsz <= 6)
  183 		return(0);
  184 	if (p->valsz > 128)
  185 		return(0);
  186 	return(1);
  187 }
  188 
  189 int
  190 valid_user_id(struct kpair *p)
  191 {
  192 	if ( ! kvalid_int(p))
  193 		return(0);
  194 	return(1);
  195 }
  196 
  197 struct user *
  198 db_user_get_creds(struct ksql *db, const char *v1, const char *v2)
  199 {
  200 	struct ksqlstmt *stmt;
  201 	struct user *p = NULL;
  202 
  203 	ksql_stmt_alloc(db, &stmt,
  204 		stmts[STMT_USER_BY_SEARCH_0],
  205 		STMT_USER_BY_SEARCH_0);
  206 	ksql_bind_str(stmt, 0, v1);
  207 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  208 		p = malloc(sizeof(struct user));
  209 		if (NULL == p) {
  210 			perror(NULL);
  211 			exit(EXIT_FAILURE);
  212 		}
  213 		db_user_fill_r(p, stmt, NULL);
  214 		if (NULL != p && crypt_checkpass(v2, p->hash) < 0) {
  215 			db_user_free(p);
  216 			p = NULL;
  217 		}
  218 	}
  219 	ksql_stmt_free(stmt);
  220 	return(p);
  221 }
  222 
  223 static void
  224 db_sess_fill_r(struct sess *p, struct ksqlstmt *stmt, size_t *pos)
  225 {
  226 	size_t i = 0;
  227 
  228 	if (NULL == pos)
  229 		pos = &i;
  230 	db_sess_fill(p, stmt, pos);
  231 	db_user_fill_r(&p->user, stmt, pos);
  232 }
  233 
  234 void
  235 db_sess_fill(struct sess *p, struct ksqlstmt *stmt, size_t *pos)
  236 {
  237 	size_t i = 0;
  238 
  239 	if (NULL == pos)
  240 		pos = &i;
  241 	memset(p, 0, sizeof(*p));
  242 	p->userid = ksql_stmt_int(stmt, (*pos)++);
  243 	p->token = ksql_stmt_int(stmt, (*pos)++);
  244 	p->id = ksql_stmt_int(stmt, (*pos)++);
  245 }
  246 
  247 static void
  248 db_sess_unfill_r(struct sess *p)
  249 {
  250 	if (NULL == p)
  251 		return;
  252 
  253 	db_sess_unfill(p);
  254 	db_user_unfill_r(&p->user);
  255 }
  256 
  257 void
  258 db_sess_unfill(struct sess *p)
  259 {
  260 	if (NULL == p)
  261 		return;
  262 }
  263 
  264 void
  265 db_sess_free(struct sess *p)
  266 {
  267 	db_sess_unfill_r(p);
  268 	free(p);
  269 }
  270 
  271 void
  272 json_sess_data(struct kjsonreq *r, const struct sess *p)
  273 {
  274 	kjson_objp_open(r, "user");
  275 	json_user_data(r, &p->user);
  276 	kjson_obj_close(r);
  277 	kjson_putintp(r, "userid", p->userid);
  278 	kjson_putintp(r, "token", p->token);
  279 	kjson_putintp(r, "id", p->id);
  280 }
  281 
  282 void
  283 json_sess_obj(struct kjsonreq *r, const struct sess *p)
  284 {
  285 	kjson_objp_open(r, "sess");
  286 	json_sess_data(r, p);
  287 	kjson_obj_close(r);
  288 }
  289 
  290 int
  291 valid_sess_userid(struct kpair *p)
  292 {
  293 	if ( ! kvalid_int(p))
  294 		return(0);
  295 	return(1);
  296 }
  297 
  298 int
  299 valid_sess_token(struct kpair *p)
  300 {
  301 	if ( ! kvalid_int(p))
  302 		return(0);
  303 	return(1);
  304 }
  305 
  306 int
  307 valid_sess_id(struct kpair *p)
  308 {
  309 	if ( ! kvalid_int(p))
  310 		return(0);
  311 	return(1);
  312 }
  313 
  314 struct sess *
  315 db_sess_get_creds(struct ksql *db, int64_t v1, int64_t v2)
  316 {
  317 	struct ksqlstmt *stmt;
  318 	struct sess *p = NULL;
  319 
  320 	ksql_stmt_alloc(db, &stmt,
  321 		stmts[STMT_SESS_BY_SEARCH_0],
  322 		STMT_SESS_BY_SEARCH_0);
  323 	ksql_bind_int(stmt, 0, v1);
  324 	ksql_bind_int(stmt, 1, v2);
  325 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  326 		p = malloc(sizeof(struct sess));
  327 		if (NULL == p) {
  328 			perror(NULL);
  329 			exit(EXIT_FAILURE);
  330 		}
  331 		db_sess_fill_r(p, stmt, NULL);
  332 	}
  333 	ksql_stmt_free(stmt);
  334 	return(p);
  335 }
  336 
  337 int
  338 db_sess_delete_id(struct ksql *db, int64_t v1)
  339 {
  340 	struct ksqlstmt *stmt;
  341 	enum ksqlc c;
  342 
  343 	ksql_stmt_alloc(db, &stmt,
  344 		stmts[STMT_SESS_DELETE_0],
  345 		STMT_SESS_DELETE_0);
  346 	ksql_bind_int(stmt, 0, v1);
  347 	c = ksql_stmt_cstep(stmt);
  348 	ksql_stmt_free(stmt);
  349 	return(KSQL_CONSTRAINT != c);
  350 }
  351