int install_sexe_userdata(sexe_t *S, char *tag) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; int is_new; k = shkey_str(tag); sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); memcpy(&S->pname, k, sizeof(S->pname)); shkey_free(&k); buff = shbuf_init(); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); is_new = shfs_read(fl, buff); udata = shjson_init(shbuf_size(buff) ? (char *)shbuf_data(buff) : NULL); shbuf_free(&buff); if (is_new) shjson_num_add(udata, "birth", shtimef(shtime())); sexe_table_set(S, udata); lua_setglobal(S, "userdata"); shjson_free(&udata); shfs_free(&fs); return (0); }
int sexe_exec_popen(shbuf_t *buff, shjson_t *arg, sexe_t **mod_p) { lua_State *L = luaL_newstate(); sexe_mod_t *mod; int narg = 1; int status; int err; if (!mod_p) return (SHERR_INVAL); *mod_p = NULL; if (shbuf_size(buff) < sizeof(sexe_mod_t)) return (SHERR_INVAL); mod = (sexe_mod_t *)shbuf_data(buff); if (0 != memcmp(mod->sig, SEXE_SIGNATURE, sizeof(mod->sig))) return (SHERR_ILSEQ); /* open standard libraries */ luaL_checkversion(L); lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ luaL_openlibs(L); /* open libraries */ lua_gc(L, LUA_GCRESTART, 0); //if (!args[has_E] && handle_luainit(L) != LUA_OK) err = _api_handle_luainit(L); if (err) { lua_close(L); return (err); } install_sexe_userdata(L, mod->name); /* sexe api lib */ install_sexe_functions(L); /* sexe api lib */ if (!arg) arg = shjson_init(NULL); lua_pushstring(L, "arg"); sexe_exec_pset(L, "arg", arg); status = sexe_loadmem(L, mod->name, buff); lua_insert(L, -(narg+1)); if (status != LUA_OK) { lua_pop(L, narg); status = _api_report(L, status); lua_close(L); return (status); } *mod_p = L; return (LUA_OK); }
/* api: grant_type = password */ int oauth_token_password(shd_t *cli, char *client_id, char *username, char *password) { shmap_t *sess; shjson_t *json; time_t expire_diff; uint64_t uid; char api_key[256]; char scope_str[256]; int err; if (!username || !password) return (SHERR_INVAL); sess = oauth_sess_load(cli, NULL); if (!sess) return (SHERR_ACCESS); err = oauth_sess_login_verify(cli, sess, username, password); if (err) return (err); #if 0 /* DEBUG: */ strcpy(api_key, oauth_api_token(cli, sess)); expire_diff = 300; strcpy(scope_str, "read"); uid = 1; #endif json = shjson_init(NULL); #if 0 shjson_str_add(json, "access_token", api_key); shjson_str_add(json, "token_type", "bearer"); shjson_num_add(json, "expires_in", expire_diff); shjson_str_add(json, "refresh_token", ""); /* optional */ shjson_str_add(json, "scope", scope_str); shjson_num_add(json, "uid", uid); /* "info":{"name", "email"} .. */ #endif shjson_str_add(json, "code", oauth_sess_token(sess)); oauth_html_json_template(cli->buff_out, json); shjson_free(&json); return (0); }
int sexe_exec_pevent(sexe_t *S, int e_type, shjson_t *arg) { shjson_t *json; int err; if (!arg) { json = shjson_init(NULL); err = sexe_event_handle(S, e_type, json); shjson_free(&json); } else { err = sexe_event_handle(S, e_type, arg); } if (err) return (err); return (0); }
/** @returns A API key (known as an 'oauth access token'). */ int oauth_token_authorization_code(shd_t *cli, char *client_id, char *client_secret, char *auth_code, char *redirect_uri) { shmap_t *sess; shjson_t *json; shbuf_t *buff = cli->buff_out; char text[1024]; char *sys_token; char api_key[256]; char scope_str[256]; uint64_t uid; int scope; int err; int idx; time_t expire_diff; int ok; if (!cli) return (SHERR_INVAL); fprintf(stderr, "DEBUG: oauth_token_autorization_code()\n"); sess = oauth_sess_find(auth_code); if (!sess) { fprintf(stderr, "DEBUG: oauth_sess_find('%s') = NULL\n", auth_code); return (SHERR_INVAL); } if (!oauth_sess_login(sess)) { fprintf(stderr, "DEBUG: oauth_token_authorization_code: unable to login\n"); return (SHERR_KEYEXPIRED); } fprintf(stderr, "DEBUG: oauth-token_auth_code: oauth_sess_token: %s\n", oauth_sess_token(sess)); sys_token = http_token_decode(oauth_sess_token(sess)); fprintf(stderr, "DEBUG: oauth_tok_auth_code: oauth_sess_token/deoce: %s\n", sys_token); if (!sys_token) { fprintf(stderr, "DEBUG: no session token ('oauth auth code') avail.\n"); return (SHERR_ACCESS); } ok = (0 == strcmp(sys_token, auth_code)); free(sys_token); if (!ok) { fprintf(stderr, "DEBUG: sys_token(%s) != auth_code(%s)\n", sys_token, auth_code); return (SHERR_ACCESS); } /* DEBUG: */ strcpy(api_key, oauth_api_token(cli, sess)); expire_diff = 300; strcpy(scope_str, "read"); uid = 1; json = shjson_init(NULL); shjson_str_add(json, "access_token", api_key); shjson_str_add(json, "token_type", "bearer"); shjson_num_add(json, "expires_in", expire_diff); shjson_str_add(json, "refresh_token", ""); /* optional */ shjson_str_add(json, "scope", scope_str); shjson_num_add(json, "uid", uid); /* "info":{"name", "email"} .. */ oauth_html_json_template(cli->buff_out, json); shjson_free(&json); return (0); }
int sharelog_list(shpeer_t *peer, time_t stime, time_t etime) { shbuf_t *buff; fd_set read_fd; shjson_t *json; char tbuf[256]; char *data; time_t now; char *str; int err; int fd; fd = shconnect_host("127.0.0.1", PROCESS_PORT, SHNET_ASYNC); if (fd < 0) return (fd); json = shjson_init(NULL); shjson_num_add(json, "id", 1); shjson_str_add(json, "method", "log.list"); shjson_str_add(json, "key", (char *)shkey_print(shpeer_kpub(peer))); shjson_null_add(json, "params"); str = shjson_print(json); shjson_free(&json); err = shnet_write(fd, str, strlen(str)); free(str); if (err < 0) { shclose(fd); return (err); } err = shnet_write(fd, "\n", 1); if (err < 0) { shclose(fd); return (err); } while (1) { FD_ZERO(&read_fd); FD_SET(fd, &read_fd); err = shnet_verify(&read_fd, NULL, NULL); if (err < 0) { continue; } buff = shnet_read_buf(fd); if (!buff) break; data = shbuf_data(buff); if (!strchr(data, '\n')) continue; json = shjson_init(data); if (json) { char *text = shjson_astr(json, "result", NULL); if (text) { printf("%s", text); } shjson_free(&json); } break; } shclose(fd); return (0); }
int sharelog_tail(shpeer_t *peer) { shbuf_t *buff; fd_set read_fd; shjson_t *json; char tbuf[256]; time_t stime, etime; time_t now; char *str; int err; int fd; fd = shconnect_host("127.0.0.1", PROCESS_PORT, SHNET_ASYNC); if (fd < 0) return (fd); json = shjson_init(NULL); shjson_num_add(json, "id", 1); shjson_str_add(json, "method", "log.subscribe"); shjson_str_add(json, "key", (char *)shkey_print(shpeer_kpub(peer))); shjson_null_add(json, "params"); str = shjson_print(json); shjson_free(&json); err = shnet_write(fd, str, strlen(str)); free(str); if (err < 0) { shclose(fd); return (err); } err = shnet_write(fd, "\n", 1); if (err < 0) { shclose(fd); return (err); } while (proc_mode == RUN_TAIL) { FD_ZERO(&read_fd); FD_SET(fd, &read_fd); err = shnet_verify(&read_fd, NULL, NULL); if (err < 0) { continue; } buff = shnet_read_buf(fd); if (!buff || shbuf_size(buff) == 0) continue; json = shjson_init(shbuf_data(buff)); if (json) { char *text = shjson_astr(json, "result", NULL); if (text) { now = time(NULL); strftime(tbuf, sizeof(tbuf) - 1, "%D %T", localtime(&now)); printf("[%s] %s", tbuf, text); } } shbuf_clear(buff); } shclose(fd); return (0); }
int oauth_admin_api_user(shd_t *cli, char *client_id, char *password, char *fullname, char *address, char *zipcode, char *phone, int b_2fa) { shmap_t *sess; char buf[1024]; char warning[256]; int err; if (!client_id) client_id = ""; sess = oauth_sess_load(cli, client_id); if (!sess) return (SHERR_INVAL); if (!oauth_sess_login(sess)) { oauth_admin_redir_login(cli, client_id); return (0); } /* apply new user-defined settings */ if (fullname && *fullname) { if (!oauth_admin_verify_fullname(fullname)) strcpy(warning, "Please specify a valid 'Real Name'."); else shmap_set_astr(sess, ashkey_str("fullname"), fullname); } if (address && *address) { if (!oauth_admin_verify_address(address)) strcpy(warning, "Please specify a valid 'Street Address'."); else shmap_set_astr(sess, ashkey_str("address"), address); } if (zipcode && *zipcode) { if (!oauth_admin_verify_zipcode(zipcode)) strcpy(warning, "Please specify a valid 'Zip Code'."); else shmap_set_astr(sess, ashkey_str("zipcode"), zipcode); } if (phone && *phone) { if (!oauth_admin_verify_phone(phone)) strcpy(warning, "Please specify a valid 'Phone Number'."); else shmap_set_astr(sess, ashkey_str("phone"), phone); } /* initialize variables */ if (!shmap_get_str(sess, ashkey_str("fullname"))) shmap_set_astr(sess, ashkey_str("fullname"), ""); if (!shmap_get_str(sess, ashkey_str("address"))) shmap_set_astr(sess, ashkey_str("address"), ""); if (!shmap_get_str(sess, ashkey_str("zipcode"))) shmap_set_astr(sess, ashkey_str("zipcode"), ""); if (!shmap_get_str(sess, ashkey_str("2fa"))) shmap_set_astr(sess, ashkey_str("2fa"), "0"); /* response with JSON context */ shjson_t *json = shjson_init(NULL); /* core attributes */ shjson_str_add(json, "fullname", shmap_get_str(sess, ashkey_str("fullname"))); shjson_str_add(json, "address", shmap_get_str(sess, ashkey_str("address"))); shjson_str_add(json, "zipcode", shmap_get_str(sess, ashkey_str("zipcode"))); shjson_str_add(json, "phone", shmap_get_str(sess, ashkey_str("phone"))); shjson_num_add(json, "2fa", atoi(shmap_get_str(sess, ashkey_str("2fa")))); oauth_html_json_template(cli->buff_out, json); shjson_free(&json); return (0); }