Esempio n. 1
0
int shfs_file_notify(shfs_ino_t *file)
{
  shfs_t *tree = file->tree;
  shbuf_t *buff;
  uint32_t mode; 
  int qid;
  int err;

  if (!file || !tree)
    return (0); /* done */

  qid = _shfs_file_qid();
  if (qid == -1)
    return (SHERR_IO);

  buff = shbuf_init();
  mode = TX_FILE;
  shbuf_cat(buff, &mode, sizeof(mode));
  shbuf_cat(buff, &tree->peer, sizeof(shpeer_t));
  shbuf_catstr(buff, shfs_inode_path(file));
  err = shmsg_write(qid, buff, NULL);
  shbuf_free(&buff);
  if (err)
    return (err);

  return (0);
}
Esempio n. 2
0
void sched_tx_payload(shkey_t *dest_key, void *data, size_t data_len, void *payload, size_t payload_len)
{
  tx_t *tx = (tx_t *)data;
  tx_t sig_tx;
  tx_net_t net;
  shsig_t sig;
  shsig_t new_sig;
  shbuf_t *buff;
  shpeer_t *self_peer;

  if (!data)
    return;
  if (data_len < sizeof(tx_t))
    return;

  prep_net_tx(tx, &net, dest_key, data_len + payload_len); 

  buff = shbuf_init();
  shbuf_cat(buff, &net, sizeof(tx_net_t));
  shbuf_cat(buff, data, data_len);
  if (payload && payload_len)
    shbuf_cat(buff, payload, payload_len);
tx_wrap(NULL, shbuf_data(buff) + sizeof(tx_net_t));
  broadcast_raw(shbuf_data(buff), shbuf_size(buff));
  shbuf_free(&buff);

fprintf(stderr, "DEBUG: SEND: sched_tx_payload: [OP %d] hash(%s) peer(%s) stamp(%llu) nonce(%d) method(%d) OP(%d)\n", (int)tx->tx_op, tx->hash, shkey_print(&tx->tx_peer), (unsigned long long)tx->tx_stamp, (int)tx->nonce, TXHASH_SCRYPT, (int)tx->tx_op);
}
Esempio n. 3
0
void shmap_print(shmap_t *h, shbuf_t *ret_buff)
{
  shmap_entry_t *ent;
  shmap_value_t mval;
  shmap_index_t *hi;
  shkey_t *key;
  char buf[256];
  char *val;
  char str[4096];
  ssize_t len;
  int flag;
  int idx;
  int i;

  if (!h || !ret_buff)
    return; /* all done */

  i = 0;

  for (hi = shmap_first(h); hi; hi = shmap_next(hi)) {
    shmap_self(hi, &key, &val, &len, &flag);
    if (!len || !val)
      continue;

    flag &= ~SHMAP_ALLOC;

    memset(&mval, 0, sizeof(mval));
    memcpy(&mval.name, key, sizeof(mval.name));
    mval.magic = SHMEM32_MAGIC;
    mval.stamp = shtime();
    mval.crc = shcrc(val, len); 
    mval.pf = flag;
    mval.sz = len;

    shbuf_cat(ret_buff, &mval, sizeof(shmap_value_t));
    shbuf_cat(ret_buff, val, len);

#if 0
    hdr = (shmap_value_t *)val;
    memcpy(&hdr->name, key, sizeof(shkey_t));
    shbuf_cat(ret_buff, hdr, sizeof(shmap_value_t));
    shbuf_cat(ret_buff, ((char *)val + sizeof(shmap_value_t)), hdr->sz);
#endif

    i++;
  }

}
Esempio n. 4
0
int shproc_schedule(shproc_t *proc, unsigned char *data, size_t data_len)
{
  shproc_req_t req;
  int err;

  if (!proc)
    return (SHERR_INVAL);

  shbuf_clear(proc->proc_buff);
  if (data && data_len)
    shbuf_cat(proc->proc_buff, data, data_len);

  memset(&req, 0, sizeof(req));
  req.state = SHPROC_RUN;
  req.user_fd = proc->user_fd;
  err = shproc_write(proc, &req);
  if (err) {
    return (err);
  }

  if (proc->user_fd)
    shproc_write_fd(proc, proc->user_fd);

  /* set process to pending state */
  shproc_state_set(proc, SHPROC_PEND);
  proc->stat.out_tot++;

  return (0);
}
Esempio n. 5
0
static int _test_shproc_req(int fd, shbuf_t *buff)
{
  int val;

  if (!fd) {
    if (shbuf_size(buff) != sizeof(val)) {
      return (-1);
    }
    val = *((int *)shbuf_data(buff));
  } else {
    if (shbuf_size(buff) != 0) {
      return (-1);
    }
    lseek(fd, 0L, SEEK_SET);
    read(fd, &val, sizeof(int));
    close(fd);
  }

  _test_shproc_value[val] = -1;

  shbuf_clear(buff);
  shbuf_cat(buff, &val, sizeof(int));

  return (0);
}
Esempio n. 6
0
int shpam_shadow_pass_remove(shfs_ino_t *file, uint64_t rem_uid)
{
  shbuf_t *rbuff;
  shbuf_t *buff;
  shseed_t *seeds;
  int total;
  int idx;
  int err;

  rbuff = shbuf_init();
  shfs_read(file, rbuff);

  buff = shbuf_init();
  seeds = (shseed_t *)shbuf_data(rbuff);
  total = shbuf_size(rbuff) / sizeof(shseed_t);
  for (idx = 0; idx < total; idx++) {
    if (seeds[idx].seed_uid == rem_uid)
      continue;

    shbuf_cat(buff, &seeds[idx], sizeof(shseed_t));
  }
  shbuf_free(&rbuff);

  err = shfs_write(file, buff);
  shbuf_free(&buff);
  if (err)
    return (err);
  
  return (0);
}
Esempio n. 7
0
int shpam_shadow_pass_append(shfs_ino_t *file, shseed_t *seed)
{
  shbuf_t *buff;
  shseed_t *seeds;
  int total;
  int idx;
  int err;

  buff = shbuf_init();
  shfs_read(file, buff);

  seeds = (shseed_t *)shbuf_data(buff);
  total = shbuf_size(buff) / sizeof(shseed_t);
  for (idx = 0; idx < total; idx++) {
    if (seeds[idx].seed_uid == seed->seed_uid) {
      memcpy(&seeds[idx], seed, sizeof(shseed_t));
      break;
    }
  }
  if (idx == total) {
    shbuf_cat(buff, seed, sizeof(shseed_t));
  }

  err = shfs_write(file, buff);
  shbuf_free(&buff);
  if (err)
    return (err);
  
  return (0);
}
Esempio n. 8
0
static int shfs_arch_buffer_write(shbuf_t *buff, void *data, size_t data_len)
{
  size_t len;

  len = 0;
  shbuf_cat(buff, data + len, data_len - len);
  shbuf_pos_set(buff, shbuf_size(buff));

  return (data_len);
}
Esempio n. 9
0
static int _xd3_file_write (_xd3_file *ofile, uint8_t *buf, usize_t size, const char *msg)
{
  size_t max_len;

  if (!ofile->buff)
    return (SHERR_IO);

  shbuf_cat(ofile->buff, buf, size);

  return (0);
}
Esempio n. 10
0
static int shproc_read(shproc_t *proc)
{
  struct shproc_req_t req;
  struct timeval to;
  fd_set in_set;
  char buf[32768];
  int r_len;
  int err;
  int of;

  err = shproc_read_wait(proc, child_proc ? 1000 : 1);

  memset(&req, 0, sizeof(req));
  r_len = read(proc->proc_fd, &req, sizeof(req));
  if (r_len == -1 && errno != EAGAIN) {
    return (errno2sherr());
  }
  if (r_len != sizeof(req))
    return (1); /* nothing to read */

  if (!child_proc) {
    /* parent process */
    if (req.state == SHPROC_RUN || 
        req.state == SHPROC_IDLE) {
      shproc_state_set(proc, req.state);
    }
    proc->stat.in_tot++;
    if (req.state == SHPROC_IDLE) {
      proc->user_fd = 0;
    }
  } else {
    proc->user_fd = req.user_fd;
  }


  of = 0;
  shbuf_clear(proc->proc_buff);
  for (of = 0; of < req.data_len; of += r_len) {
    r_len = read(proc->proc_fd, buf, MIN(req.data_len-of, sizeof(buf)));
    if (r_len == -1)
      return (errno2sherr());
    if (r_len == 0)
      return (SHERR_AGAIN);

    shbuf_cat(proc->proc_buff, buf, r_len);
  }

  proc->proc_error = req.error;

  return (0);
}
Esempio n. 11
0
/**
 * @param file The inode refeferencing another inode.
 * @param ref_file The inode being referenced.
 */
int shfs_ref_set(shfs_ino_t *file, shfs_ino_t *ref_file)
{
    shfs_ino_t *parent;
    shbuf_t *buff;
    int err;
    int i;

    if (!file || !file->tree)
        return (SHERR_INVAL);

    if (shfs_type(file) != shfs_type(ref_file)) {
        if (shfs_type(ref_file) != SHINODE_DIRECTORY)
            return (SHERR_ISDIR);
        return (SHERR_NOTDIR);
    }

    buff = shbuf_init();
    shbuf_cat(buff, &file->tree->peer, sizeof(shpeer_t));

    parent = ref_file;
    for (i = 0; i < SHFS_MAX_REFERENCE_HIERARCHY; i++) {
        if (parent) {
            shbuf_cat(buff, &parent->blk.hdr.name, sizeof(shkey_t));
            parent = shfs_inode_parent(parent);
        }
    }

    err = _shfs_ref_raw_write(file, buff);
    shbuf_free(&buff);
    if (err)
        return (err);

    err = shfs_inode_write_entity(file);
    if (err)
        return (err);

    return (0);
}
Esempio n. 12
0
int oauth_response_favicon(shbuf_t *buff)
{
  char text[1024];

  sprintf(text,
      "HTTP/1.1 200 OK\r\n"
      "Accept-Ranges: bytes\r\n"
      "Content-Length: %u\r\n"
      "Content-Type: image/vnd.microsoft.icon\r\n"
      "\r\n",
      (unsigned int)FAVICON_ICO_SIZE);

  shbuf_catstr(buff, text);
  shbuf_cat(buff, FAVICON_ICO, FAVICON_ICO_SIZE);

}
Esempio n. 13
0
int shfs_mem_read(char *path, shbuf_t *buff)
{
  FILE *fl;
  struct stat st;
  size_t data_len;
  ssize_t r_len;
  char inbuff[4096];
  char *data;
  int r_of;
  int err;

  if (!path || !buff)
    return (SHERR_INVAL);

  memset(&st, 0, sizeof(st));
  err = stat(path, &st);
  if (err)
    return (err);

  if (st.st_size == 0)
    return (0);

  if (S_ISDIR(st.st_mode))
    return (SHERR_ISDIR);

  fl = fopen(path, "rb");
  if (!fl) {
    free(data);
    return (-1);
  }

  r_of = 0;
  while (r_of < st.st_size) {
    r_len = fread(inbuff, sizeof(char), sizeof(inbuff), fl);
    if (r_len < 0)
      return (errno2sherr());

    shbuf_cat(buff, inbuff, r_len);
    r_of += r_len;
  }

  err = fclose(fl);
  if (err)
    return (err);

  return (0);
}
Esempio n. 14
0
int pstore_write(int tx_op, char *name, unsigned char *data, size_t data_len)
{
    SHFL *fl;
    char prefix[256];
    char path[PATH_MAX+1];
    shbuf_t *buff;
    int err;

    pstore_init();

    memset(prefix, 0, sizeof(prefix));
    switch (tx_op) {
    case TX_ACCOUNT:
        strcpy(prefix, "account");
        break;
    case TX_IDENT:
        strcpy(prefix, "id");
        break;
    case TX_SESSION:
        strcpy(prefix, "session");
        break;
    case TX_APP:
        strcpy(prefix, "app");
        break;
    case TX_LEDGER:
        strcpy(prefix, "ledger");
        break;
    default:
        strcpy(prefix, "default");
        break;
    }

    sprintf(path, "/sys/net/tx/%s/%s", prefix, name);
    fl = shfs_file_find(_pstore_fs, path);
    buff = shbuf_init();
    shbuf_cat(buff, data, data_len);
    err = shfs_write(fl, buff);
    shbuf_free(&buff);
    if (err)
        return (err);

    return (0);
}
Esempio n. 15
0
int _shfs_ref_raw_read(shfs_ino_t *file, shbuf_t *buff)
{
    shfs_ino_t *inode;
    int err;

    if (!file)
        return (SHERR_INVAL);

    if (shfs_format(file) != SHINODE_REFERENCE)
        return (SHERR_INVAL);

    inode = shfs_inode(file, NULL, SHINODE_REFERENCE);
    if (!inode)
        return (SHERR_IO);

    if (inode->blk.hdr.size < (sizeof(shpeer_t) + sizeof(shkey_t)) ||
            inode->blk.hdr.size > SHFS_BLOCK_DATA_SIZE)
        return (SHERR_IO);

    shbuf_cat(buff, (char *)inode->blk.raw, inode->blk.hdr.size);
    return (0);
}
Esempio n. 16
0
int shpam_pshadow_store(shfs_ino_t *file, shseed_t *seed)
{
  shbuf_t *buff;
  shseed_t *seeds;
  int total;
  int idx;
  int err;

#if 0
  /* ensure record exists. */
  err = shpam_pshadow_load(file, seed->seed_uid, NULL);
  if (err)
    return (err);
#endif

  buff = shbuf_init();
  shfs_read(file, buff);

  seeds = (shseed_t *)shbuf_data(buff);
  total = shbuf_size(buff) / sizeof(shseed_t);
  for (idx = 0; idx < total; idx++) {
    if (seeds[idx].seed_uid == seed->seed_uid) {
      memcpy(&seeds[idx], seed, sizeof(shseed_t));
      break;
    }
  }
  if (idx == total) {
    shbuf_cat(buff, seed, sizeof(shseed_t));
  }

  err = shfs_write(file, buff);
  shbuf_free(&buff);
  if (err)
    return (err);

  return (0);
}
Esempio n. 17
0
int confirm_tx_key(txop_t *op, tx_t *tx)
{
  shkey_t *c_key = &tx->tx_key;
  shkey_t *key;
  shbuf_t *buff;
  tx_t *k_tx;
  size_t len;
  int confirm;

  if (!op || !tx)
    return (SHERR_INVAL);

  if (op->op_size == 0)
    return (0);

  /* allocate working copy */
  len = MAX(sizeof(tx_t), op->op_keylen ? op->op_keylen : op->op_size);
  buff = shbuf_init();
  shbuf_cat(buff, (char *)tx, len);

  /* blank out tx key */
  k_tx = (tx_t *)shbuf_data(buff);
  memset(&k_tx->tx_key, '\000', sizeof(k_tx->tx_key));

  /* verify generated tx key matches. */
  key = shkey_bin(shbuf_data(buff), shbuf_size(buff));
  confirm = shkey_cmp(c_key, key);
  shkey_free(&key);
  shbuf_free(&buff);

  if (!confirm) {
    return (SHERR_INVAL);
  }

  return (0);
}
Esempio n. 18
0
ssize_t shnet_read(int fd, const void *buf, size_t count)
{
  unsigned int usk = (unsigned int)fd;
  struct timeval to;
  size_t max_r_len;
  ssize_t r_len;
  size_t len;
  fd_set read_set;
  fd_set exc_set;
  char tbuf[8];
  int err;

  if (_sk_table[usk].fd == 0)
    return (SHERR_BADF);

  if (count == 0) {
    buf = NULL;
    count = 65536;
  }
  count = MIN(MAX_READ_BUFFER_SIZE, count);

  if (!_sk_table[usk].recv_buff)
    _sk_table[usk].recv_buff = shbuf_init();

  if (!(_sk_table[usk].flags & SHNET_ASYNC)) {
    FD_ZERO(&read_set);
    FD_SET(fd, &read_set);
    FD_ZERO(&exc_set);
    FD_SET(fd, &exc_set);

    /* block for data */
    err = select(fd+1, &read_set, NULL, &exc_set, NULL);
    if (err == -1) 
      return (errno2sherr());
  } 

  /* data available for read. */
  memset(_read_buff, 0, sizeof(_read_buff));
  r_len = read(fd, _read_buff, count);
  if (r_len == 0) {
#if 0
    if (shbuf_size(_sk_table[usk].recv_buff) == 0)
#endif
      return (SHERR_CONNRESET);
  } else if (r_len < 1) {
    return (errno2sherr());
  } else {
    /* append to internal buffer */
    shbuf_cat(_sk_table[usk].recv_buff, _read_buff, r_len);
  }

  if (buf) {
    /* extract head */
    r_len = MIN(count, shbuf_size(_sk_table[usk].recv_buff));
    if (r_len != 0) {
      memcpy((char *)buf, (char *)shbuf_data(_sk_table[usk].recv_buff), r_len);
      shbuf_trim(_sk_table[usk].recv_buff, r_len);
    }
  }

  return (r_len);
}