Пример #1
0
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);
}
Пример #2
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);
}
Пример #3
0
/* 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);
}
Пример #4
0
int update_sexe_userdata(sexe_t *S)
{
  SHFL *fl;
  shjson_t *udata;
  shfs_t *fs;
  shbuf_t *buff;
  shkey_t *k;
  char path[PATH_MAX+1];
  char *str;
  int err;

  k = &S->pname;
  if (shkey_cmp(k, ashkey_blank())) {
fprintf(stderr, "DEBUG: update_sexe_userdata: no app key\n");
    return (0); /* blank */
  }
  sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); 

  lua_getglobal(S, "userdata");
  udata = sexe_table_get(S);
  if (!udata) {
fprintf(stderr, "DEBUG: update_sexe_userdata: no global 'userdata' variable.\n");
    return (SHERR_INVAL);
  }

  str = shjson_print(udata);
  if (!str) {
fprintf(stderr, "DEBUG: update_sexe_userdata: error encoding JSON.\n");
    return (SHERR_INVAL);
}
  shjson_free(&udata);

  buff = shbuf_init();
  shbuf_catstr(buff, str);
  free(str);


  fs = shfs_init(NULL);
  fl = shfs_file_find(fs, path);
  err = shfs_write(fl, buff);
  shbuf_free(&buff);
  shfs_free(&fs);

  return (err);
}
Пример #5
0
static int _lfunc_trigger_event(lua_State *L)
{
  shjson_t *json;
  int e_type = (int)luaL_checknumber(L, 1);
  int t_reg = 0;

  json = NULL;
  if (lua_istable(L, 2)) {
    lua_pushvalue(L, 2);
    json = sexe_table_get(L);
  }

  /* second optional arg; table of data. */
  sexe_event_handle(L, e_type, json);

  if (json)
    shjson_free(&json);

}
Пример #6
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);
}
Пример #7
0
/**
 * The "os.trigger(<string>[,<table>])" function will trigger all callbacks for the specifid event name to be called with an optional object argument.
 */
int lfunc_trigger_event(sexe_t *L)
{
  const char *e_name;
  shjson_t *json;
  int t_reg = 0;
	int ret_bool;
	int err;

	/* first argument: event name */
	e_name = (int)luaL_checkstring(L, 1);

  /* second optional arg; table of data. */
  json = NULL;
  if (lua_istable(L, 2)) {
    lua_pushvalue(L, 2);
    json = sexe_table_get(L);
  }

	ret_bool = 1; 
	err = sexe_event_handle(L, e_name, json);
	if (err)
		ret_bool = 0;

#if 0

	/* iterate through registered functions for event */
	{
	  shkey_t *key = sexe_event_key(e_name);
		char *e_hex = shkey_hex(key);
		lua_getglobal(L, EVENT_ENV);
		if (lua_isnil(L, -1)) {
			return (0); /* error */
		}
		lua_getfield(L, -1, e_hex);
		if (lua_isnil(L, -1)) {
			return (0); /* error */
		}

		lua_pushnil(L);
		while (lua_next(L, -2)) {
			int t = lua_type(L, -1);
			if (t == LUA_TFUNCTION) {
				/* copy function call onto stack  lua_pushvalue(L, 1); */
				/* 1. user args */
				if (json)
					sexe_table_set(L, json);
				else
					lua_pushnil(L);
				/* 2. event name */
				lua_pushstring(L, e_name);
				/* exec */
				lua_pcall(L, 2, 0, 0); 
			} else {
				lua_pop(L, 1); /* value */
			}

			//lua_pop(L, 1); /* key */
		}
	}
#endif

  if (json)
    shjson_free(&json);

	/* return single boolean */
	lua_pushboolean(L, ret_bool);
	return (1);
}
Пример #8
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);
}
Пример #9
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);
}
Пример #10
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);
}