From 9d0b51a2abdacb5fc4eed95cb3249934b607fdc7 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sat, 9 Feb 2019 00:04:45 +0800 Subject: Navigator. --- xp/navigator/cgi-src/navigator_cgi.cpp | 329 +++++++++++++++++++++++++ xp/navigator/index.html | 133 ++++++++++ xp/navigator/main.js | 435 +++++++++++++++++++++++++++++++++ xp/navigator/sha256.js | 173 +++++++++++++ xp/navigator/shit.php | 38 +++ 5 files changed, 1108 insertions(+) create mode 100644 xp/navigator/cgi-src/navigator_cgi.cpp create mode 100644 xp/navigator/index.html create mode 100644 xp/navigator/main.js create mode 100644 xp/navigator/sha256.js create mode 100644 xp/navigator/shit.php diff --git a/xp/navigator/cgi-src/navigator_cgi.cpp b/xp/navigator/cgi-src/navigator_cgi.cpp new file mode 100644 index 0000000..20e152e --- /dev/null +++ b/xp/navigator/cgi-src/navigator_cgi.cpp @@ -0,0 +1,329 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +MYSQL* sql; +const char* rand_ch="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; +Json::Value do_login(Json::Value o) +{ + MYSQL_RES* sqlr=NULL; + MYSQL_ROW row; + char q[256]; + Json::Value ret; + std::string usrname=o["username"].asString(); + std::string passwd=o["passwd"].asString(); + std::string sessname=o["sessionname"].asString(); + std::string token; + std::string qpwd; + if(usrname.length()<1){ret["result"]=1;goto fail;} + for(size_t i=0;i0){ret["result"]=2;goto fail;} + mysql_free_result(sqlr);sqlr=NULL; + } + + do{ + if(sqlr){mysql_free_result(sqlr);sqlr=NULL;} + token=""; + for(int i=0;i<32;++i)token.push_back(rand_ch[rand()%62]); + snprintf(q,256,"select token from navigator_session where token='%s'", + token.c_str()); + mysql_query(sql,q); + sqlr=mysql_store_result(sql); + }while(mysql_num_rows(sqlr)); + mysql_free_result(sqlr);sqlr=NULL; + + snprintf(q,256,"insert into navigator_session values('%s','%s','%s',%lld)", + usrname.c_str(), + token.c_str(), + sessname.c_str(), + time(NULL) + ); + mysql_query(sql,q); + mysql_commit(sql); + ret["result"]=0; + ret["token"]=token; +fail: + if(sqlr){mysql_free_result(sqlr);sqlr=NULL;} + return ret; +} +const int tsl[]={1800,86400,604800,1296000,2592000,5184000,7776000,15552000,31104000}; +void set_session_length(std::string user,int c) +{ + char q[256]; + if(c>7)c=7;if(c<0)c=0; + snprintf(q,256,"update navigator_user set session_length=%d where username='%s'",user.c_str()); + mysql_query(sql,q); + mysql_commit(sql); +} +int get_session_length(std::string user) +{ + MYSQL_RES* sqlr=NULL; + MYSQL_ROW row; + char q[256]; + snprintf(q,256,"select session_length from navigator_user where username='%s'",user.c_str()); + mysql_query(sql,q); + sqlr=mysql_store_result(sql); + if(mysql_num_rows(sqlr)!=1){mysql_free_result(sqlr);return -1;} + row=mysql_fetch_row(sqlr); + int sl=atoi(row[0]); + mysql_free_result(sqlr); + return sl; +} +std::string authenticate(std::string token) +{ + MYSQL_RES* sqlr=NULL; + MYSQL_ROW row; + char q[256]; + if(token.length()!=32)return ""; + snprintf(q,256,"select username,sessionname,date from navigator_session where token='%s'",token.c_str()); + mysql_query(sql,q); + sqlr=mysql_store_result(sql); + if(mysql_num_rows(sqlr)!=1){mysql_free_result(sqlr);return "";} + row=mysql_fetch_row(sqlr); + std::string ret=std::string(row[0]); + bool is_temporary=strlen(row[1])>0; + long long d=atoll(row[2]); + mysql_free_result(sqlr); + int sl=get_session_length(ret); + if(sl<0||sl>7){return "";} + int reall=tsl[is_temporary?0:sl+1]; + if(time(NULL)-d>reall) + { + snprintf(q,256,"delete from navigator_session where token='%s'",token.c_str()); + mysql_query(sql,q); + mysql_commit(sql); + return ""; + } + return ret; +} +Json::Value set_option(Json::Value o) +{ + Json::Value ret; + std::string token=o["token"].asString(); + std::string usr=authenticate(token); + int opt=o["option"].asInt(); + int val=o["value"].asInt(); + if(!usr.length()){ret["result"]=1;goto fail;} + switch(opt) + { + case 0: + ret["result"]=0; + set_session_length(usr,val); + break; + default: + ret["result"]=1; + } +fail: + return ret; +} +Json::Value get_option(Json::Value o) +{ + Json::Value ret; + std::string token=o["token"].asString(); + std::string usr=authenticate(token); + int opt=o["option"].asInt(); + if(!usr.length()){ret["result"]=1;goto fail;} + switch(opt) + { + case 0: + ret["result"]=0; + ret["value"]=get_session_length(usr); + break; + default: + ret["result"]=1; + } +fail: + return ret; +} +Json::Value get_bookmarks(Json::Value o) +{ + MYSQL_RES* sqlr=NULL; + MYSQL_ROW row; + char q[256]; + Json::Value ret; + std::string token=o["token"].asString(); + std::string usr=authenticate(token); + if(!usr.length()){ret["result"]=1;goto fail;} + snprintf(q,256,"select bookmarks from navigator_user where username='%s'",usr.c_str()); + mysql_query(sql,q); + sqlr=mysql_store_result(sql); + if(mysql_num_rows(sqlr)!=1){mysql_free_result(sqlr);sqlr=NULL;ret["result"]=4;goto fail;} + row=mysql_fetch_row(sqlr); + ret["result"]=0; + ret["bookmarks"]=std::string(row[0]); + mysql_free_result(sqlr);sqlr=NULL; +fail: + if(sqlr){mysql_free_result(sqlr);sqlr=NULL;} + return ret; +} +Json::Value set_bookmarks(Json::Value o) +{ + char *q=(char*)malloc(65537); + Json::Value ret; + std::string token=o["token"].asString(); + std::string usr=authenticate(token); + if(!usr.length()){ret["result"]=1;goto fail;} + snprintf(q,65536,"update navigator_user set bookmarks='%s' where username='%s'",o["bookmarks"].asString().c_str(),usr.c_str()); + mysql_query(sql,q); + mysql_commit(sql); + ret["result"]=0; +fail: + free(q); + return ret; +} +Json::Value list_sessions(Json::Value o) +{ + MYSQL_RES* sqlr=NULL; + MYSQL_ROW row; + char q[256]; + Json::Value ret,ss; + std::string token=o["token"].asString(); + std::string usr=authenticate(token); + if(!usr.length()){ret["result"]=1;goto fail;} + snprintf(q,256,"select sessionname,date from navigator_session where username='%s'",usr.c_str()); + + mysql_query(sql,q); + sqlr=mysql_store_result(sql); + for(int i=0;row=mysql_fetch_row(sqlr);++i) + { + Json::Value c; + c["sessionname"]=std::string(row[0]); + c["date"]=atoi(row[1]); + ss[i]=c; + } + mysql_free_result(sqlr);sqlr=NULL; + ret["result"]=0;ret["sessions"]=ss; +fail: + return ret; +} +Json::Value remove_session(Json::Value o) +{ + char q[256]; + Json::Value ret; + std::string token=o["token"].asString(); + std::string usr=authenticate(token); + std::string sess=o["session"].asString(); + if(!usr.length()){ret["result"]=1;goto fail;} + snprintf(q,256,"delete from navigator_session where username='%s' and sessionname='%s'",usr.c_str(),sess.c_str()); + mysql_query(sql,q); + mysql_commit(sql); + ret["result"]=0; +fail: + return ret; +} +int main() +{ + if(!getenv("CONTENT_LENGTH"))return -1; + int len=atoi(getenv("CONTENT_LENGTH")); + char *buf;buf=(char*)malloc(len+1); + fread(buf,1,len,stdin);buf[len]=0; + std::string sbuf(buf,len); + free(buf); + std::stringstream ss(sbuf); + Json::Value o,r;ss>>o; + + sql=mysql_init(NULL); + if(!sql)return -1; + if(!mysql_real_connect(sql,"localhost","chrisoft",NULL,"chrisoft",0,"/var/run/mysqld/mysqld.sock",0)) + return -1; + + switch(o.get("op",-1).asInt()) + { + case 0://login + r=do_login(o); + break; + case 1://get bookmarks + r=get_bookmarks(o); + break; + case 2://set bookmarks + r=set_bookmarks(o); + break; + case 3://list sessions + r=list_sessions(o); + break; + case 4://remove session + r=remove_session(o); + break; + case 5://set option + r=set_option(o); + break; + case 6://get option + r=get_option(o); + break; + } + printf("Status: 200 OK\r\n"); + printf("Content-type: application/json; charset=utf-8\r\n\r\n"); + std::ostringstream oss; + oss< + + + + +Chrisoft::Navigator + + + + + + + + + + + +
+ +
+
+
+

Login


+
+

+

+

+

+ +
+
+
+
    +
+
+
+

Sessions

+ + + + + + + + + + +
Session nameLogin dateInvalidate session
+
+
+ + + diff --git a/xp/navigator/main.js b/xp/navigator/main.js new file mode 100644 index 0000000..c17d025 --- /dev/null +++ b/xp/navigator/main.js @@ -0,0 +1,435 @@ +let o={}; +const ui={}; +let curo,curp; +let editinge=null; +function getcookie(key) +{return document.cookie.replace(new RegExp('(?:(?:^|.*;\\s*)'+key+'\\s*\\=\\s*([^;]*).*$)|^.*$'),'$1');} +function make_sortable(e,updatefunc) +{ + let dragging,nxt; + function _drag(ev) + { + ev.preventDefault(); + ev.dataTransfer.dropEffect='move'; + const targ=ev.target; + if(targ&&targ!=dragging&&targ.draggable&&targ.parentNode==e) + { + const rect=targ.getBoundingClientRect(); + const after=(ev.clientY-rect.top)/(rect.bottom-rect.top)>.5; + e.insertBefore(dragging,after?targ.nextSibling:targ); + } + } + function _drag_end(ev) + { + ev.preventDefault(); + dragging.classList.remove('ghost'); + e.removeEventListener('dragover',_drag,false); + e.removeEventListener('dragend',_drag_end,false); + if(nxt!=dragging.nextSibling)updatefunc(dragging); + } + function _drag_start(ev) + { + dragging=ev.target; + nxt=dragging.nextSibling; + ev.dataTransfer.effectAllowed='move'; + ev.dataTransfer.setData('Text',dragging.textContent); + e.addEventListener('dragover',_drag,false); + e.addEventListener('dragend',_drag_end,false); + setTimeout(function(){dragging.classList.add('ghost');},0); + } + e.addEventListener('dragstart',_drag_start,false); +} +function remove_children(e) +{ + while(e.firstChild)e.removeChild(e.firstChild); +} +function switch_folder(path) +{ + //path starts and ends with / + ui.main.style.display='block'; + ui.settings.style.display=null; + const p=path.split('/'); + remove_children(ui.cont); + let cur=o; + for(let i=1;cur&&i{ + if(r) + { + document.getElementById('loginerr').innerHTML='login failed:' + switch(r) + { + case 1: + document.getElementById('loginerr').innerHTML+='authentication failure'; + break; + case 2: + document.getElementById('loginerr').innerHTML+='duplicate session'; + break; + } + } + else + { + document.getElementById('loginerr').innerHTML=''; + init(); + } + } + ); +} +function display_bookmarks() +{ + remove_children(ui.folderlist); + for(let i=0;i{ + if(r) + { + ui.login.style.display='block'; + ui.main.style.display=null; + } + else + { + ui.login.style.display=null; + display_bookmarks(); + ui.main.style.display='block'; + } + } + ); +} diff --git a/xp/navigator/sha256.js b/xp/navigator/sha256.js new file mode 100644 index 0000000..5ce6b7e --- /dev/null +++ b/xp/navigator/sha256.js @@ -0,0 +1,173 @@ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ +/* SHA-256 (FIPS 180-4) implementation in JavaScript (c) Chris Veness 2002-2017 */ +/* MIT Licence */ +/* www.movable-type.co.uk/scripts/sha256.html */ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +'use strict'; + + +/** + * SHA-256 hash function reference implementation. + * + * This is an annotated direct implementation of FIPS 180-4, without any optimisations. It is + * intended to aid understanding of the algorithm rather than for production use. + * + * While it could be used where performance is not critical, I would recommend using the ‘Web + * Cryptography API’ (developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) for the browser, + * or the ‘crypto’ library (nodejs.org/api/crypto.html#crypto_class_hash) in Node.js. + * + * See csrc.nist.gov/groups/ST/toolkit/secure_hashing.html + * csrc.nist.gov/groups/ST/toolkit/examples.html + */ +class Sha256 { + + /** + * Generates SHA-256 hash of string. + * + * @param {string} msg - (Unicode) string to be hashed. + * @param {Object} [options] + * @param {string} [options.msgFormat=string] - Message format: 'string' for JavaScript string + * (gets converted to UTF-8 for hashing); 'hex-bytes' for string of hex bytes ('616263' ≡ 'abc') . + * @param {string} [options.outFormat=hex] - Output format: 'hex' for string of contiguous + * hex bytes; 'hex-w' for grouping hex bytes into groups of (4 byte / 8 character) words. + * @returns {string} Hash of msg as hex character string. + */ + static hash(msg, options) { + const defaults = { msgFormat: 'string', outFormat: 'hex' }; + const opt = Object.assign(defaults, options); + + // note use throughout this routine of 'n >>> 0' to coerce Number 'n' to unsigned 32-bit integer + + switch (opt.msgFormat) { + default: // default is to convert string to UTF-8, as SHA only deals with byte-streams + case 'string': msg = utf8Encode(msg); break; + case 'hex-bytes':msg = hexBytesToString(msg); break; // mostly for running tests + } + + // constants [§4.2.2] + const K = [ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ]; + + // initial hash value [§5.3.3] + const H = [ + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ]; + + // PREPROCESSING [§6.2.1] + + msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1] + + // convert string msg into 512-bit blocks (array of 16 32-bit integers) [§5.2.1] + const l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length + const N = Math.ceil(l/16); // number of 16-integer (512-bit) blocks required to hold 'l' ints + const M = new Array(N); // message M is N×16 array of 32-bit integers + + for (let i=0; i>> 32, but since JS converts + // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators + const lenHi = ((msg.length-1)*8) / Math.pow(2, 32); + const lenLo = ((msg.length-1)*8) >>> 0; + M[N-1][14] = Math.floor(lenHi); + M[N-1][15] = lenLo; + + + // HASH COMPUTATION [§6.2.2] + + for (let i=0; i>> 0; + } + + // 2 - initialise working variables a, b, c, d, e, f, g, h with previous hash value + let a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7]; + + // 3 - main loop (note '>>> 0' for 'addition modulo 2^32') + for (let t=0; t<64; t++) { + const T1 = h + Sha256.Σ1(e) + Sha256.Ch(e, f, g) + K[t] + W[t]; + const T2 = Sha256.Σ0(a) + Sha256.Maj(a, b, c); + h = g; + g = f; + f = e; + e = (d + T1) >>> 0; + d = c; + c = b; + b = a; + a = (T1 + T2) >>> 0; + } + + // 4 - compute the new intermediate hash value (note '>>> 0' for 'addition modulo 2^32') + H[0] = (H[0]+a) >>> 0; + H[1] = (H[1]+b) >>> 0; + H[2] = (H[2]+c) >>> 0; + H[3] = (H[3]+d) >>> 0; + H[4] = (H[4]+e) >>> 0; + H[5] = (H[5]+f) >>> 0; + H[6] = (H[6]+g) >>> 0; + H[7] = (H[7]+h) >>> 0; + } + + // convert H0..H7 to hex strings (with leading zeros) + for (let h=0; h prev + String.fromCharCode(curr), ''); + } catch (e) { // no TextEncoder available? + return unescape(encodeURIComponent(str)); // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html + } + } + + function hexBytesToString(hexStr) { // convert string of hex numbers to a string of chars (eg '616263' -> 'abc'). + const str = hexStr.replace(' ', ''); // allow space-separated groups + return str=='' ? '' : str.match(/.{2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join(''); + } + } + + + + /** + * Rotates right (circular right shift) value x by n positions [§3.2.4]. + * @private + */ + static ROTR(n, x) { + return (x >>> n) | (x << (32-n)); + } + + + /** + * Logical functions [§4.1.2]. + * @private + */ + static Σ0(x) { return Sha256.ROTR(2, x) ^ Sha256.ROTR(13, x) ^ Sha256.ROTR(22, x); } + static Σ1(x) { return Sha256.ROTR(6, x) ^ Sha256.ROTR(11, x) ^ Sha256.ROTR(25, x); } + static σ0(x) { return Sha256.ROTR(7, x) ^ Sha256.ROTR(18, x) ^ (x>>>3); } + static σ1(x) { return Sha256.ROTR(17, x) ^ Sha256.ROTR(19, x) ^ (x>>>10); } + static Ch(x, y, z) { return (x & y) ^ (~x & z); } // 'choice' + static Maj(x, y, z) { return (x & y) ^ (x & z) ^ (y & z); } // 'majority' + +} \ No newline at end of file diff --git a/xp/navigator/shit.php b/xp/navigator/shit.php new file mode 100644 index 0000000..8602a94 --- /dev/null +++ b/xp/navigator/shit.php @@ -0,0 +1,38 @@ +query(sprintf('select passwd from navigator_user where username=\'%s\'',$usrname)); + if($qr->rowCount()!=1){return array('result'=>1);} + if($passwd!=$qr->fetchColumn(0)){return array('result'=>1);} + $token=uniqid(); + $pdo->query(sprintf('insert into navigator_session values(\'%s\',\'%s\',\'%s\',%d)', + $usrname,$passwd,$sessname,time())); + $pdo->commit(); + {return array('result'=>0,'token'=>$token);} + } + $pdo=new PDO('mysql:host=localhost;dbname=chrisoft','chrisoft',null); + $o=json_decode(file_get_contents('php://stdin')); + $r=null; + switch($o['op']) + { + case 0: + $r=do_login($pdo,$o); + break; + case 1: + //get bookmarks + break; + case 2: + //set bookmarks + break; + case 3: + //list sessions + break; + case 4: + //remove session + break; + } + echo json_encode($r); +?> -- cgit v1.2.3