/* _sist_fast(): offer to save passcode by mug in home directory. */ static void _sist_fast(u3_noun pas, c3_l key_l) { c3_c ful_c[2048]; c3_c* hom_c = u3_Host.dir_c; u3_noun gum = u3dc("scot", 'p', key_l); c3_c* gum_c = u3r_string(gum); u3_noun yek = u3dc("scot", 'p', pas); c3_c* yek_c = u3r_string(yek); printf("saving passcode in %s/.urb/code.%s\r\n", hom_c, gum_c); printf("(for real security, write it down and delete the file...)\r\n"); { c3_i fid_i; snprintf(ful_c, 2048, "%s/.urb/code.%s", hom_c, gum_c); if ( (fid_i = open(ful_c, O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0 ) { uL(fprintf(uH, "fast: could not save %s\n", ful_c)); u3_lo_bail(); } write(fid_i, yek_c, strlen(yek_c)); close(fid_i); } free(gum_c); u3z(gum); free(yek_c); u3z(yek); }
/* u3_sist_boot(): restore or create. */ void u3_sist_boot(void) { // uL(fprintf(uH, "sist: booting\n")); if ( c3y == u3_Host.ops_u.nuu ) { u3_noun pig = u3_none; if ( 0 == u3_Host.ops_u.imp_c ) { c3_c get_c[2049]; snprintf(get_c, 2048, "%s/.urb/get", u3_Host.dir_c); if ( 0 == access(get_c, 0) ) { uL(fprintf(uH, "pier: already built\n")); u3_lo_bail(); } u3_noun ten = _sist_zen(); uL(fprintf(uH, "generating 2048-bit RSA pair...\n")); pig = u3nq(c3__make, u3_nul, 11, u3nc(ten, u3_Host.ops_u.fak)); } else { u3_noun imp = u3i_string(u3_Host.ops_u.imp_c); u3_noun whu = u3dc("slaw", 'p', u3k(imp)); if ( (u3_nul == whu) ) { fprintf(stderr, "czar: incorrect format\r\n"); u3_lo_bail(); } else { u3_noun gen = u3_nul; u3_noun gun = u3_nul; if (c3n == u3_Host.ops_u.fak) { gen = _sist_text("generator"); gun = u3dc("slaw", c3__uw, gen); if ( u3_nul == gun ) { fprintf(stderr, "czar: incorrect format\r\n"); u3_lo_bail(); } } else { gun = u3nc(u3_nul, u3_nul); } pig = u3nq(c3__sith, u3k(u3t(whu)), u3k(u3t(gun)), u3_Host.ops_u.fak); u3z(whu); u3z(gun); } u3z(imp); } _sist_make(pig); } else { _sist_rest(); } }
/* _http_pox_to_noun(): translate srv/con/req to path noun (pox). */ static u3_noun _http_pox_to_noun(c3_w sev_l, c3_w coq_l, c3_w seq_l) { return u3nt( u3_blip, c3__http, u3nq(u3dc("scot", c3_s2('u','v'), sev_l), u3dc("scot", c3_s2('u','d'), coq_l), u3dc("scot", c3_s2('u','d'), seq_l), u3_nul)); }
/* _term_close_cb(): free terminal. */ static void _term_close_cb(uv_handle_t* han_t) { u3_utty* tty_u = (void*) han_t; if ( u3_Host.uty_u == tty_u ) { u3_Host.uty_u = tty_u->nex_u; } else { u3_utty* uty_u; for (uty_u = u3_Host.uty_u; uty_u; uty_u = uty_u->nex_u ) { if ( uty_u->nex_u == tty_u ) { uty_u->nex_u = tty_u->nex_u; break; } } } { u3_noun tid = u3dc("scot", c3__ud, tty_u->tid_l); u3_noun pax = u3nq(u3_blip, c3__term, tid, u3_nul); u3v_plan(u3k(pax), u3nc(c3__hook, u3_nul)); u3z(pax); } free(tty_u); }
/* _term_io_belt(): send belt. */ static void _term_io_belt(u3_utty* uty_u, u3_noun blb) { u3_noun tid = u3dc("scot", c3__ud, uty_u->tid_l); u3_noun pax = u3nq(u3_blip, c3__term, tid, u3_nul); u3v_plan(pax, u3nc(c3__belt, blb)); }
/* _sist_fatt(): stretch a 64-bit passcode to make a 128-bit key. */ static u3_noun _sist_fatt(c3_l sal_l, u3_noun pas) { c3_w i_w; u3_noun key = pas; // XX use scrypt() - this is a stupid iterated hash // for ( i_w = 0; i_w < 32768; i_w++ ) { key = u3dc("shaf", sal_l, key); } return key; }
/* u3_term_ef_ticket(): initial effects for new ticket. */ void u3_term_ef_ticket(c3_c* who_c, c3_c* tic_c) { u3_noun pax = u3nq(u3_blip, c3__term, '1', u3_nul); u3_noun who, tic; u3_noun whu, tuc; whu = u3dc("slaw", 'p', u3i_string(who_c)); if ( u3_nul == whu ) { fprintf(stderr, "ticket: invalid planet '%s'\r\n", who_c); exit(1); } else { who = u3k(u3t(whu)); u3z(whu); } tuc = u3dc("slaw", 'p', u3i_string(tic_c)); if ( u3_nul == tuc ) { fprintf(stderr, "ticket: invalid secret '%s'\r\n", tic_c); exit(1); } else { tic = u3k(u3t(tuc)); u3z(tuc); } u3v_plan(pax, u3nt(c3__tick, who, tic)); }
/* _sist_staf(): try to load passcode by mug from home directory. */ static u3_noun _sist_staf(c3_l key_l) { c3_c ful_c[2048]; c3_c* hom_c = u3_Host.dir_c; u3_noun gum = u3dc("scot", 'p', key_l); c3_c* gum_c = u3r_string(gum); u3_noun txt; snprintf(ful_c, 2048, "%s/.urb/code.%s", hom_c, gum_c); free(gum_c); u3z(gum); txt = u3_walk_safe(ful_c); if ( 0 == txt ) { uL(fprintf(uH, "staf: no passcode %s\n", ful_c)); return 0; } else { // c3_c* txt_c = u3r_string(txt); u3_noun say = u3do("slay", txt); u3_noun pas; if ( (u3_nul == say) || (u3_blip != u3h(u3t(say))) || ('p' != u3h(u3t(u3t(say)))) ) { uL(fprintf(uH, "staf: %s is corrupt\n", ful_c)); u3z(say); return 0; } uL(fprintf(uH, "loaded passcode from %s\n", ful_c)); pas = u3k(u3t(u3t(u3t(say)))); u3z(say); return pas; } }
/* _tel_opt(): telnet event sucker */ static void _tel_opt(_te_nvt* nvt, telnet_byte opt, _to_evt* evt) { switch (evt->type) { default: break; case TELNET_EV_TELOPT_DATA: { _to_dvt* dv = (_to_dvt*)evt; u3_utel* tel_u; u3_noun pax; u3_noun blu; u3_noun tid; c3_s col_s; c3_s row_s; if ( opt != _T_NAWS ) { return; } c3_assert(0 < telnet_get_userdata(nvt, (void**)&tel_u)); col_s = dv->data[1] | (dv->data[0] << 8); row_s = dv->data[3] | (dv->data[2] << 8); tel_u->uty_t.tat_u.siz.col_l = col_s; tel_u->uty_t.tat_u.siz.row_l = row_s; tid = u3dc("scot", c3__ud, tel_u->uty_t.tid_l); pax = u3nq(u3_blip, c3__term, tid, u3_nul); blu = u3nc(col_s, row_s); u3v_plan(pax, u3nc(c3__blew, blu)); break; } } }
void _term_listen_cb(uv_stream_t *wax_u, int sas_i) { u3_utel* pty_u = calloc(1, sizeof(*pty_u)); u3_utty* tty_u = &pty_u->uty_t; uv_tcp_init(u3L, &tty_u->wax_u); c3_w ret_w; if ( 0 != (ret_w = uv_accept(wax_u, (uv_stream_t*)&tty_u->wax_u)) ) { uL(fprintf(uH, "term: accept: %s\n", uv_strerror(ret_w))); uv_close((uv_handle_t*)&tty_u->wax_u, NULL); free(tty_u); } else { uv_read_start((uv_stream_t*)&tty_u->wax_u, _term_alloc, _term_read_tn_cb); tty_u->ufo_u.out.clear_y = (const c3_y*)"\033[H\033[J"; tty_u->ufo_u.out.el_y = (const c3_y*)"\033[K"; tty_u->ufo_u.out.ed_y = (const c3_y*)"\033[J"; tty_u->ufo_u.out.bel_y = (const c3_y*)"\007"; tty_u->ufo_u.out.cub1_y = (const c3_y*)"\010"; tty_u->ufo_u.out.cud1_y = (const c3_y*)"\033[B"; tty_u->ufo_u.out.cuu1_y = (const c3_y*)"\033[A"; tty_u->ufo_u.out.cuf1_y = (const c3_y*)"\033[C"; tty_u->ufo_u.inn.kcuu1_y = (const c3_y*)"\033[A"; tty_u->ufo_u.inn.kcud1_y = (const c3_y*)"\033[B"; tty_u->ufo_u.inn.kcuf1_y = (const c3_y*)"\033[C"; tty_u->ufo_u.inn.kcub1_y = (const c3_y*)"\033[D"; tty_u->ufo_u.inn.max_w = strlen("\033[D"); tty_u->fid_i = -1; tty_u->tat_u.mir.lin_w = 0; tty_u->tat_u.mir.len_w = 0; tty_u->tat_u.mir.cus_w = 0; tty_u->tat_u.esc.ape = c3n; tty_u->tat_u.esc.bra = c3n; tty_u->tat_u.fut.len_w = 0; tty_u->tat_u.fut.wid_w = 0; tty_u->tat_u.siz.col_l = 80; tty_u->tat_u.siz.row_l = 25; tty_u->tid_l = u3_Host.uty_u->tid_l + 1; tty_u->nex_u = u3_Host.uty_u; u3_Host.uty_u = tty_u; pty_u->tel_u = telnet_nvt_new(tty_u, _tel_event, _tel_opt, NULL); { u3_noun tid = u3dc("scot", c3__ud, tty_u->tid_l); u3_noun pax = u3nq(u3_blip, c3__term, tid, u3_nul); u3v_plan(u3k(pax), u3nc(c3__blew, u3nc(80, 25))); u3v_plan(u3k(pax), u3nc(c3__hail, u3_nul)); u3z(pax); } telnet_telopt_enable(pty_u->tel_u, _T_ECHO, TELNET_LOCAL); telnet_telopt_enable(pty_u->tel_u, _T_CTIM, TELNET_LOCAL); telnet_telopt_enable(pty_u->tel_u, _T_NAWS, TELNET_REMOTE); } }
/* _sist_rest(): restore from record, or exit. */ static void _sist_rest() { struct stat buf_b; c3_i fid_i; c3_c ful_c[2048]; c3_d old_d = u3A->ent_d; c3_d las_d = 0; u3_noun roe = u3_nul; u3_noun sev_l, key_l, sal_l; u3_noun ohh = c3n; if ( 0 != u3A->ent_d ) { u3_noun ent; c3_c* ent_c; ent = u3i_chubs(1, &u3A->ent_d); ent = u3dc("scot", c3__ud, ent); ent_c = u3r_string(ent); uL(fprintf(uH, "rest: checkpoint to event %s\n", ent_c)); free(ent_c); u3z(ent); } // Open the fscking file. Does it even exist? { c3_i pig_i = O_RDWR; #ifdef O_DSYNC pig_i |= O_DSYNC; #endif snprintf(ful_c, 2048, "%s/.urb/egz.hope", u3_Host.dir_c); if ( ((fid_i = open(ful_c, pig_i)) < 0) || (fstat(fid_i, &buf_b) < 0) ) { uL(fprintf(uH, "rest: can't open record (%s)\n", ful_c)); u3_lo_bail(); return; } #ifdef F_NOCACHE if ( -1 == fcntl(fid_i, F_NOCACHE, 1) ) { uL(fprintf(uH, "rest: can't uncache %s: %s\n", ful_c, strerror(errno))); u3_lo_bail(); return; } #endif u3Z->lug_u.fid_i = fid_i; u3Z->lug_u.len_d = ((buf_b.st_size + 3ULL) >> 2ULL); } // Check the fscking header. It's probably corrupt. { u3_uled led_u; if ( sizeof(led_u) != read(fid_i, &led_u, sizeof(led_u)) ) { uL(fprintf(uH, "record (%s) is corrupt (a)\n", ful_c)); u3_lo_bail(); } if ( u3r_mug('f') == led_u.mag_l ) { _sist_rest_nuu(&u3Z->lug_u, led_u, ful_c); fid_i = u3Z->lug_u.fid_i; } else if (u3r_mug('g') != led_u.mag_l ) { uL(fprintf(uH, "record (%s) is obsolete (or corrupt)\n", ful_c)); u3_lo_bail(); } if ( led_u.kno_w != 163 ) { // XX perhaps we should actually do something here // uL(fprintf(uH, "rest: (not) translating events (old %d, now %d)\n", led_u.kno_w, 163)); } sev_l = led_u.sev_l; sal_l = led_u.sal_l; key_l = led_u.key_l; { u3_noun old = u3dc("scot", c3__uv, sev_l); u3_noun nuu = u3dc("scot", c3__uv, u3A->sev_l); c3_c* old_c = u3r_string(old); c3_c* nuu_c = u3r_string(nuu); uL(fprintf(uH, "rest: old %s, new %s\n", old_c, nuu_c)); free(old_c); free(nuu_c); u3z(old); u3z(nuu); } c3_assert(sev_l != u3A->sev_l); // 1 in 2 billion, just retry } // Oh, and let's hope you didn't forget the fscking passcode. { if ( 0 != key_l ) { u3_noun pas = _sist_staf(key_l); u3_noun key; while ( 1 ) { pas = pas ? pas : _sist_cask(u3_Host.dir_c, c3n); key = _sist_fatt(sal_l, pas); if ( u3r_mug(key) != key_l ) { uL(fprintf(uH, "incorrect passcode\n")); u3z(key); pas = 0; } else { u3z(u3A->key); u3A->key = key; break; } } } } // Read in the fscking events. These are probably corrupt as well. { c3_d ent_d; c3_d end_d; u3_noun rup = c3n; end_d = u3Z->lug_u.len_d; ent_d = 0; if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { fprintf(stderr, "end_d %" PRIu64 "\n", end_d); perror("lseek"); uL(fprintf(uH, "record (%s) is corrupt (c)\n", ful_c)); u3_lo_bail(); } while ( end_d != c3_wiseof(u3_uled) ) { c3_d tar_d = (end_d - (c3_d)c3_wiseof(u3_ular)); u3_ular lar_u; c3_w* img_w; u3_noun ron; // uL(fprintf(uH, "rest: reading event at %" PRIx64 "\n", end_d)); if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "record (%s) is corrupt (d)\n", ful_c)); u3_lo_bail(); } if ( sizeof(u3_ular) != read(fid_i, &lar_u, sizeof(u3_ular)) ) { uL(fprintf(uH, "record (%s) is corrupt (e)\n", ful_c)); u3_lo_bail(); } if ( lar_u.syn_w != u3r_mug((c3_w)tar_d) ) { if ( c3n == rup ) { uL(fprintf(uH, "corruption detected; attempting to fix\n")); rup = c3y; } uL(fprintf(uH, "lar:%x mug:%x\n", lar_u.syn_w, u3r_mug((c3_w)tar_d))); end_d--; u3Z->lug_u.len_d--; continue; } else if ( c3y == rup ) { uL(fprintf(uH, "matched at %x\n", lar_u.syn_w)); rup = c3n; } if ( lar_u.ent_d == 0 ) { ohh = c3y; } #if 0 uL(fprintf(uH, "log: read: at %d, %d: lar ent %" PRIu64 ", len %d, mug %x\n", (tar_w - lar_u.len_w), tar_w, lar_u.ent_d, lar_u.len_w, lar_u.mug_w)); #endif if ( end_d == u3Z->lug_u.len_d ) { ent_d = las_d = lar_u.ent_d; } else { if ( lar_u.ent_d != (ent_d - 1ULL) ) { uL(fprintf(uH, "record (%s) is corrupt (g)\n", ful_c)); uL(fprintf(uH, "lar_u.ent_d %" PRIx64 ", ent_d %" PRIx64 "\n", lar_u.ent_d, ent_d)); u3_lo_bail(); } ent_d -= 1ULL; } end_d = (tar_d - (c3_d)lar_u.len_w); if ( ent_d < old_d ) { // XX this could be a break if we didn't want to see the sequence // number of the first event. continue; } img_w = c3_malloc(4 * lar_u.len_w); if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { uL(fprintf(uH, "record (%s) is corrupt (h)\n", ful_c)); u3_lo_bail(); } if ( (4 * lar_u.len_w) != read(fid_i, img_w, (4 * lar_u.len_w)) ) { uL(fprintf(uH, "record (%s) is corrupt (i)\n", ful_c)); u3_lo_bail(); } ron = u3i_words(lar_u.len_w, img_w); free(img_w); if ( lar_u.mug_w != u3r_mug_both(u3r_mug(ron), u3r_mug_both(u3r_mug(lar_u.tem_w), u3r_mug(lar_u.typ_w))) ) { uL(fprintf(uH, "record (%s) is corrupt (j)\n", ful_c)); u3_lo_bail(); } if ( c3__ov != lar_u.typ_w ) { u3z(ron); continue; } if ( u3A->key ) { u3_noun dep; dep = u3dc("de:crua", u3k(u3A->key), ron); if ( c3n == u3du(dep) ) { uL(fprintf(uH, "record (%s) is corrupt (k)\n", ful_c)); u3_lo_bail(); } else { ron = u3k(u3t(dep)); u3z(dep); } } roe = u3nc(u3ke_cue(ron), roe); } u3A->ent_d = c3_max(las_d + 1ULL, old_d); } if ( u3_nul == roe ) { // Nothing in the log that was not also in the checkpoint. // c3_assert(u3A->ent_d == old_d); if ( las_d + 1 != old_d ) { uL(fprintf(uH, "checkpoint and log disagree! las:%" PRIu64 " old:%" PRIu64 "\n", las_d + 1, old_d)); uL(fprintf(uH, "Some events appear to be missing from the log.\n" "Please contact the authorities, " "and do not delete your pier!\n")); u3_lo_bail(); } } else { u3_noun rou = roe; c3_w xno_w; // Execute the fscking things. This is pretty much certain to crash. // uL(fprintf(uH, "rest: replaying through event %" PRIu64 "\n", las_d)); fprintf(uH, "---------------- playback starting----------------\n"); xno_w = 0; while ( u3_nul != roe ) { u3_noun i_roe = u3h(roe); u3_noun t_roe = u3t(roe); u3_noun now = u3h(i_roe); u3_noun ovo = u3t(i_roe); u3v_time(u3k(now)); if ( (c3y == u3_Host.ops_u.vno) && ( (c3__veer == u3h(u3t(ovo)) || (c3__vega == u3h(u3t(ovo)))) ) ) { fprintf(stderr, "replay: skipped veer\n"); } else if ( c3y == u3_Host.ops_u.fog && u3_nul == t_roe ) { fprintf(stderr, "replay: -Xwtf, skipped last event\n"); } else { _sist_sing(u3k(ovo)); fputc('.', stderr); } // fprintf(stderr, "playback: sing: %d\n", xno_w)); roe = t_roe; xno_w++; if ( 0 == (xno_w % 1000) ) { uL(fprintf(uH, "{%d}\n", xno_w)); // u3_lo_grab("rest", rou, u3_none); } } u3z(rou); } uL(fprintf(stderr, "\n---------------- playback complete----------------\n")); #if 0 // If you see this error, your record is totally fscking broken! // Which probably serves you right. Please consult a consultant. { if ( u3_nul == u3A->own ) { uL(fprintf(uH, "record did not install a master!\n")); u3_lo_bail(); } u3A->our = u3k(u3h(u3A->own)); u3A->pod = u3dc("scot", 'p', u3k(u3A->our))); } // Now, who the fsck are you? No, really. { u3_noun who; c3_c* fil_c; c3_c* who_c; if ( (fil_c = strrchr(u3_Host.dir_c, '/')) ) { fil_c++; } else fil_c = u3_Host.dir_c; who = u3dc("scot", 'p', u3k(u3A->our))); who_c = u3r_string(who); u3z(who); if ( strncmp(fil_c, who_c + 1, strlen(fil_c)) ) { uL(fprintf(uH, "record master (%s) does not match filename!\n", who_c)); u3_lo_bail(); } free(who_c); } #endif // Increment sequence numbers. New logs start at 1. if ( c3y == ohh ) { uL(fprintf(uH, "rest: bumping ent_d, don't panic.\n")); u3_ular lar_u; c3_d end_d; c3_d tar_d; u3A->ent_d++; end_d = u3Z->lug_u.len_d; while ( end_d != c3_wiseof(u3_uled) ) { tar_d = end_d - c3_wiseof(u3_ular); if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "bumping sequence numbers failed (a)\n")); u3_lo_bail(); } if ( sizeof(lar_u) != read(fid_i, &lar_u, sizeof(lar_u)) ) { uL(fprintf(uH, "bumping sequence numbers failed (b)\n")); u3_lo_bail(); } lar_u.ent_d++; if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { uL(fprintf(uH, "bumping sequence numbers failed (c)\n")); u3_lo_bail(); } if ( sizeof(lar_u) != write(fid_i, &lar_u, sizeof(lar_u)) ) { uL(fprintf(uH, "bumping sequence numbers failed (d)\n")); u3_lo_bail(); } end_d = tar_d - lar_u.len_w; } } // Rewrite the header. Will probably corrupt the record. { u3_uled led_u; led_u.mag_l = u3r_mug('g'); led_u.sal_l = sal_l; led_u.sev_l = u3A->sev_l; led_u.key_l = u3A->key ? u3r_mug(u3A->key) : 0; led_u.kno_w = 163; // may need actual translation! led_u.tno_l = 1; if ( (-1 == lseek64(fid_i, 0, SEEK_SET)) || (sizeof(led_u) != write(fid_i, &led_u, sizeof(led_u))) ) { uL(fprintf(uH, "record (%s) failed to rewrite\n", ful_c)); u3_lo_bail(); } } // Hey, fscker! It worked. { u3_term_ef_boil(); } }