timekeeper

My first (abandoned unfinished) web application for time tracking
git clone git://jacobedwards.org/timekeeper
Log | Files | Refs | README

login.c (3212B)


      1 #include <assert.h>
      2 
      3 #define const
      4 
      5 #include <ctype.h>
      6 
      7 #include "common.h"
      8 #include "pages.h"
      9 
     10 enum kcgi_err
     11 htmlinput(struct pagedata *pd, enum http_key field, char *type, int64_t minl, int64_t maxl)
     12 {
     13 	enum kcgi_err status;
     14 
     15 	assert(pd->keys[field].name[0] != 0);
     16 
     17 	if ((status = khtml_elem(&pd->html, KELEM_LABEL)) != KCGI_OK ||
     18 	    (status = khtml_printf(&pd->html, "%c%s: ",
     19 	        toupper(pd->keys[field].name[0]), pd->keys[field].name + 1)) != KCGI_OK ||
     20 	    (status = khtml_attrx(&pd->html, KELEM_INPUT,
     21 	        KATTR_NAME, KATTRX_STRING, pd->keys[field].name,
     22 	        KATTR_TYPE, KATTRX_STRING, type,
     23 	        KATTR_MINLENGTH, KATTRX_INT, minl,
     24 	        KATTR_MAXLENGTH, KATTRX_INT, maxl,
     25 		KATTR__MAX)) != KCGI_OK ||
     26 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
     27 		return status;
     28 
     29 	return KCGI_OK;
     30 }
     31 
     32 enum kcgi_err
     33 pagelogin(struct pagedata *pd)
     34 {
     35 	static char *css[] = { "css/main.css", NULL };
     36 	static struct pagetemplate template = {
     37 		"Login",
     38 		.css = css
     39 	};
     40 
     41 	enum kcgi_err status;
     42 	struct user *user = NULL;
     43 	char *fuser, *fpass;
     44 	enum user_error userstatus;
     45 
     46 	if (pd->user)
     47 		return redirect(pd, pd->pages[PageMain], "Logged in");
     48 
     49 	userstatus = LoginLast;
     50 	if (pd->req.fieldmap[KeyUsername] && pd->req.fieldmap[KeyPassword]) {
     51 		fuser = pd->req.fieldmap[KeyUsername]->parsed.s;
     52 		fpass = pd->req.fieldmap[KeyPassword]->parsed.s;
     53 
     54 		if (pd->req.fieldmap[KeyCreate]) {
     55 			if (adduser(pd,  fuser, fpass))
     56 				userstatus = LoginSystem;
     57 		}
     58 		if (userstatus == LoginLast) {
     59 			userstatus = loginuser(&user, pd, fuser, fpass);
     60 			if (userstatus == LoginValid) {
     61 				status = khttp_head(&pd->req, kresps[KRESP_SET_COOKIE], "%s=%s; Path=/",
     62 				    pd->keys[KeyHash].name, user->auth);
     63 				freeuser(user);
     64 				if (status != KCGI_OK)
     65 					return status;
     66 				return redirect(pd, pd->pages[PageMain], "Logged in");
     67 			}
     68 		}
     69 	}
     70 
     71 	if ((status = tk_startpage(pd, &template, KHTTP_200)) != KCGI_OK)
     72 		return status;
     73 
     74 	if ((status = htmlwithin(pd, KELEM_H1, "Login")) != KCGI_OK)
     75 		return status;
     76 
     77 	if (userstatus && userstatus < LoginLast) {
     78 		kutil_warn(&pd->req, NULL, "Bad login: %s", user_errors[userstatus]);
     79 		if ((status = tk_htmlerror(pd, user_errors[userstatus])) != KCGI_OK)
     80 			return status;
     81 	}
     82 
     83 	if ((status = khtml_elem(&pd->html, KELEM_FORM)) != KCGI_OK ||
     84 	    (status = htmlinput(pd, KeyUsername, "text", NameMin, NameMax)) != KCGI_OK ||
     85 	    (status = khtml_putc(&pd->html, ' ')) != KCGI_OK ||
     86 	    (status = htmlinput(pd, KeyPassword, "password", PassMin, PassMax)) != KCGI_OK ||
     87 	    (status = khtml_putc(&pd->html, ' ')) != KCGI_OK ||
     88 	    (status = khtml_elem(&pd->html, KELEM_LABEL)) != KCGI_OK ||
     89 	    (status = khtml_puts(&pd->html, "Create user ")) != KCGI_OK ||
     90 	    (status = khtml_attr(&pd->html, KELEM_INPUT,
     91 		KATTR_TYPE, "checkbox",
     92 		KATTR_NAME, pd->keys[KeyCreate].name,
     93 		KATTR__MAX)) != KCGI_OK ||
     94 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK ||
     95 	    (status = khtml_putc(&pd->html, ' ')) != KCGI_OK ||
     96 	    (status = khtml_attr(&pd->html, KELEM_INPUT,
     97 		KATTR_TYPE, "submit",
     98 		KATTR_VALUE, "Submit",
     99 		KATTR__MAX)) != KCGI_OK ||
    100 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
    101 		return status;
    102 
    103 	return endpage(pd, &template);
    104 }