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 "rbac-ex1.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_SESSION_BY_SEARCH_0,
   27 	STMT_SESSION_INSERT,
   28 	STMT_SESSION_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_SESSION_BY_SEARCH_0 */
   45 	"SELECT " DB_SCHEMA_SESSION(session) "," DB_SCHEMA_USER(_a) " FROM session INNER JOIN user AS _a ON _a.id=session.userid WHERE session.id = ?",
   46 	/* STMT_SESSION_INSERT */
   47 	"INSERT INTO session (userid) VALUES (?)",
   48 	/* STMT_SESSION_DELETE_0 */
   49 	"DELETE FROM session 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_session_userid, "session-userid" },
   57 	{ valid_session_id, "session-id" },
   58 };
   59 
   60 /*
   61  * Finally, all of the functions we'll use.
   62  */
   63 
   64 struct ksql *
   65 db_open(const char *file)
   66 {
   67 	struct ksqlcfg cfg;
   68 	struct ksql *db;
   69 
   70 	memset(&cfg, 0, sizeof(struct ksqlcfg));
   71 	cfg.flags = KSQL_EXIT_ON_ERR |
   72 		KSQL_FOREIGN_KEYS | KSQL_SAFE_EXIT;
   73 	cfg.err = ksqlitemsg;
   74 	cfg.dberr = ksqlitedbmsg;
   75 
   76 	db = ksql_alloc(&cfg);
   77 	if (NULL == db)
   78 		return(NULL);
   79 	ksql_open(db, file);
   80 	return(db);
   81 }
   82 
   83 void
   84 db_close(struct ksql *p)
   85 {
   86 	if (NULL == p)
   87 		return;
   88 	ksql_close(p);
   89 	ksql_free(p);
   90 }
   91 
   92 static void
   93 db_user_fill_r(struct user *p, struct ksqlstmt *stmt, size_t *pos)
   94 {
   95 	size_t i = 0;
   96 
   97 	if (NULL == pos)
   98 		pos = &i;
   99 	db_user_fill(p, stmt, pos);
  100 }
  101 
  102 void
  103 db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos)
  104 {
  105 	size_t i = 0;
  106 
  107 	if (NULL == pos)
  108 		pos = &i;
  109 	memset(p, 0, sizeof(*p));
  110 	p->email = strdup(ksql_stmt_str(stmt, (*pos)++));
  111 	if (NULL == p->email) {
  112 		perror(NULL);
  113 		exit(EXIT_FAILURE);
  114 	}
  115 	p->hash = strdup(ksql_stmt_str(stmt, (*pos)++));
  116 	if (NULL == p->hash) {
  117 		perror(NULL);
  118 		exit(EXIT_FAILURE);
  119 	}
  120 	p->id = ksql_stmt_int(stmt, (*pos)++);
  121 }
  122 
  123 static void
  124 db_user_unfill_r(struct user *p)
  125 {
  126 	if (NULL == p)
  127 		return;
  128 
  129 	db_user_unfill(p);
  130 }
  131 
  132 void
  133 db_user_unfill(struct user *p)
  134 {
  135 	if (NULL == p)
  136 		return;
  137 	free(p->email);
  138 	free(p->hash);
  139 }
  140 
  141 void
  142 db_user_free(struct user *p)
  143 {
  144 	db_user_unfill_r(p);
  145 	free(p);
  146 }
  147 
  148 void
  149 json_user_data(struct kjsonreq *r, const struct user *p)
  150 {
  151 	kjson_putstringp(r, "email", p->email);
  152 	/* Omitting hash: is a password hash. */
  153 	kjson_putintp(r, "id", p->id);
  154 }
  155 
  156 void
  157 json_user_obj(struct kjsonreq *r, const struct user *p)
  158 {
  159 	kjson_objp_open(r, "user");
  160 	json_user_data(r, p);
  161 	kjson_obj_close(r);
  162 }
  163 
  164 int
  165 valid_user_email(struct kpair *p)
  166 {
  167 	if ( ! kvalid_email(p))
  168 		return(0);
  169 	return(1);
  170 }
  171 
  172 int
  173 valid_user_hash(struct kpair *p)
  174 {
  175 	if ( ! kvalid_string(p))
  176 		return(0);
  177 	return(1);
  178 }
  179 
  180 int
  181 valid_user_id(struct kpair *p)
  182 {
  183 	if ( ! kvalid_int(p))
  184 		return(0);
  185 	return(1);
  186 }
  187 
  188 struct user *
  189 db_user_get_creds(struct ksql *db, const char *v1, const char *v2)
  190 {
  191 	struct ksqlstmt *stmt;
  192 	struct user *p = NULL;
  193 
  194 	ksql_stmt_alloc(db, &stmt,
  195 		stmts[STMT_USER_BY_SEARCH_0],
  196 		STMT_USER_BY_SEARCH_0);
  197 	ksql_bind_str(stmt, 0, v1);
  198 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  199 		p = malloc(sizeof(struct user));
  200 		if (NULL == p) {
  201 			perror(NULL);
  202 			exit(EXIT_FAILURE);
  203 		}
  204 		db_user_fill_r(p, stmt, NULL);
  205 		if (NULL != p && crypt_checkpass(v2, p->hash) < 0) {
  206 			db_user_free(p);
  207 			p = NULL;
  208 		}
  209 	}
  210 	ksql_stmt_free(stmt);
  211 	return(p);
  212 }
  213 
  214 static void
  215 db_session_fill_r(struct session *p, struct ksqlstmt *stmt, size_t *pos)
  216 {
  217 	size_t i = 0;
  218 
  219 	if (NULL == pos)
  220 		pos = &i;
  221 	db_session_fill(p, stmt, pos);
  222 	db_user_fill_r(&p->user, stmt, pos);
  223 }
  224 
  225 void
  226 db_session_fill(struct session *p, struct ksqlstmt *stmt, size_t *pos)
  227 {
  228 	size_t i = 0;
  229 
  230 	if (NULL == pos)
  231 		pos = &i;
  232 	memset(p, 0, sizeof(*p));
  233 	p->userid = ksql_stmt_int(stmt, (*pos)++);
  234 	p->id = ksql_stmt_int(stmt, (*pos)++);
  235 }
  236 
  237 static void
  238 db_session_unfill_r(struct session *p)
  239 {
  240 	if (NULL == p)
  241 		return;
  242 
  243 	db_session_unfill(p);
  244 	db_user_unfill_r(&p->user);
  245 }
  246 
  247 void
  248 db_session_unfill(struct session *p)
  249 {
  250 	if (NULL == p)
  251 		return;
  252 }
  253 
  254 void
  255 db_session_free(struct session *p)
  256 {
  257 	db_session_unfill_r(p);
  258 	free(p);
  259 }
  260 
  261 int64_t
  262 db_session_insert(struct ksql *db, int64_t v1)
  263 {
  264 	struct ksqlstmt *stmt;
  265 	int64_t id = -1;
  266 
  267 	ksql_stmt_alloc(db, &stmt,
  268 		stmts[STMT_SESSION_INSERT],
  269 		STMT_SESSION_INSERT);
  270 	ksql_bind_int(stmt, 0, v1);
  271 	if (KSQL_DONE == ksql_stmt_cstep(stmt))
  272 		ksql_lastid(db, &id);
  273 	ksql_stmt_free(stmt);
  274 	return(id);
  275 }
  276 
  277 void
  278 json_session_data(struct kjsonreq *r, const struct session *p)
  279 {
  280 	kjson_objp_open(r, "user");
  281 	json_user_data(r, &p->user);
  282 	kjson_obj_close(r);
  283 	kjson_putintp(r, "userid", p->userid);
  284 	kjson_putintp(r, "id", p->id);
  285 }
  286 
  287 void
  288 json_session_obj(struct kjsonreq *r, const struct session *p)
  289 {
  290 	kjson_objp_open(r, "session");
  291 	json_session_data(r, p);
  292 	kjson_obj_close(r);
  293 }
  294 
  295 int
  296 valid_session_userid(struct kpair *p)
  297 {
  298 	if ( ! kvalid_int(p))
  299 		return(0);
  300 	return(1);
  301 }
  302 
  303 int
  304 valid_session_id(struct kpair *p)
  305 {
  306 	if ( ! kvalid_int(p))
  307 		return(0);
  308 	return(1);
  309 }
  310 
  311 struct session *
  312 db_session_get_id(struct ksql *db, int64_t v1)
  313 {
  314 	struct ksqlstmt *stmt;
  315 	struct session *p = NULL;
  316 
  317 	ksql_stmt_alloc(db, &stmt,
  318 		stmts[STMT_SESSION_BY_SEARCH_0],
  319 		STMT_SESSION_BY_SEARCH_0);
  320 	ksql_bind_int(stmt, 0, v1);
  321 	if (KSQL_ROW == ksql_stmt_step(stmt)) {
  322 		p = malloc(sizeof(struct session));
  323 		if (NULL == p) {
  324 			perror(NULL);
  325 			exit(EXIT_FAILURE);
  326 		}
  327 		db_session_fill_r(p, stmt, NULL);
  328 	}
  329 	ksql_stmt_free(stmt);
  330 	return(p);
  331 }
  332 
  333 int
  334 db_session_delete_by_id_eq(struct ksql *db, int64_t v1)
  335 {
  336 	struct ksqlstmt *stmt;
  337 	enum ksqlc c;
  338 
  339 	ksql_stmt_alloc(db, &stmt,
  340 		stmts[STMT_SESSION_DELETE_0],
  341 		STMT_SESSION_DELETE_0);
  342 	ksql_bind_int(stmt, 0, v1);
  343 	c = ksql_stmt_cstep(stmt);
  344 	ksql_stmt_free(stmt);
  345 	return(KSQL_CONSTRAINT != c);
  346 }
  347