Example #1
0
void SshKeyController::refresh_key_files() {
  QMutexLocker locker(&m_mutex);  // Locks the mutex and unlocks when locker exits the scope
  std::vector<SshKey> tmp;

  QDir dir(CSettingsManager::Instance().ssh_keys_storage());
  if (!dir.exists()) {
    qCritical(
        "Wrong ssh keys storage : %s",
        dir.absolutePath().toStdString().c_str());
    return;
  }

  QStringList name_filters({"*.pub"});
  QStringList tmp_list =
      dir.entryList(name_filters, QDir::Files | QDir::NoSymLinks);

  for (auto i = tmp_list.begin(); i != tmp_list.end(); ++i) {
    QString file_path = dir.path() + QDir::separator() + *i;
    QFile key_file(file_path);
    if (!key_file.open(QFile::ReadOnly)) {
      qCritical(
          "Can't open ssh-key file : %s, reason : %s",
          file_path.toStdString().c_str(),
          key_file.errorString().toStdString().c_str());
      continue;
    }

    QFileInfo fi(key_file);
    if (!fi.baseName().contains(QRegularExpression("[/|\\\\$%~\"*?:<>^]"))
        && !(fi.baseName()[0] == '.')) {
      SshKey key;

      QByteArray arr_content = key_file.readAll();
      arr_content.truncate(arr_content.size() - 1);  // hack for hub

      key.file_name = *i;
      key.content = QString(arr_content).remove(QRegExp("[\\n\\t\\r\\v\\f]"));
      key.path = dir.absolutePath() + QDir::separator() + (*i);
      tmp.push_back(key);

      std::vector<SshKey>::iterator it;
      it = std::find_if(m_keys.begin(), m_keys.end(),
                        find_content(key.content));

      if (it == m_keys.end()) {
        m_keys.push_back(key);
      }

    }
    key_file.close();
  }

  emit finished_check_environment_keys();
}
Example #2
0
static sharefarm_content *
inject_share(sharefarm_content *tail, const char *fname)
{
  char *stem;
  int sharenum, i;
  sharefarm_content *c;
  struct stat sbuf;
  
  if (find_sharenum(fname, &stem, &sharenum) < 0) {
    return tail;
  }
  
  i = stat(fname, &sbuf);
  
  if ((i < 0) && errno != ENOENT) {
    free(stem);
    return tail;
  }
  
  printf("stat(%s) == %d (errno == %d)\n", fname, i, errno);
  
  c = find_content(tail, stem);
  
  if (c == NULL) {
    c = calloc(1, sizeof(*c));
    c->next = tail;
    c->stem = stem;
    c->statinfo.st_mode = S_IFREG;
    if (i == 0)
      memcpy(&c->statinfo, &sbuf, sizeof(sbuf));
    c->sharecount = 1;
    c->shares = calloc(1, sizeof(*c->shares));
    c->shares[0].fname = strdup(fname);
    c->shares[0].sharenum = sharenum;
    c->shares[0].present = (i == 0);
    update_share_mode(c);
    return c;
  }
  
  if (i == 0)
    memcpy(&c->statinfo, &sbuf, sizeof(sbuf));
  
  c->shares = realloc(c->shares, sizeof(*c->shares) * (c->sharecount + 1));
  c->shares[c->sharecount].fname = strdup(fname);
  c->shares[c->sharecount].sharenum = sharenum;
  c->shares[c->sharecount].present = (i == 0);
  c->sharecount++;
  free(stem);
  
  update_share_mode(c);
  
  return tail;
}
Example #3
0
void SshKeyController::clean_keys_list(QString content) {
  QMutexLocker locker(&m_mutex);

  std::vector<SshKey>::iterator it;
  it = std::find_if(m_keys.begin(), m_keys.end(),
                    find_content(content));

  if (it != m_keys.end()) {
    m_keys.erase(it);
  }
  emit finished_check_environment_keys();
}
Example #4
0
int
sharefarm_stat(const char *path, struct stat *sbuf)
{
  sharefarm_content *c = NULL, *cc = NULL;
  int ret = 0;
  
  if ((ret = find_all_shares(&c)) < 0)
    return ret;
  
  cc = find_content(c, path);
  
  if (cc == NULL) {
    free_content(c);
    return -ENOENT;
  }
  
  memcpy(sbuf, &cc->statinfo, sizeof(*sbuf));
  
  free_content(c);
  
  return 0;
}
Example #5
0
int
sharefarm_read(const char *path, size_t *len, char **buf)
{
  sharefarm_content *c = NULL, *cc = NULL;
  int ret, i;
  gfshare_ctx *gfc = NULL;
  unsigned char *sharenrs = NULL;
  unsigned char *sparebuf = NULL;
  
  if ((ret = find_all_shares(&c)) < 0)
    return ret;
  
  cc = find_content(c, path);
  
  if (cc == NULL) {
    free_content(c);
    return -ENOENT;
  }
  
  sharenrs = malloc(cc->sharecount);
  if (sharenrs == NULL) {
    free_content(c);
    return -ENOMEM;
  }
  
  for (i = 0; i < cc->sharecount; ++i)
    sharenrs[i] = (unsigned char)(cc->shares[i].sharenum);
  
  *len = cc->statinfo.st_size;
  *buf = calloc(1, *len);
  if (*buf == NULL) {
    ret = -ENOMEM;
    goto out;
  }
  
  mlock(*buf, *len);
  
  sparebuf = malloc(*len);
  if (sparebuf == NULL) {
    ret = -ENOMEM;
    goto out;
  }
  
  gfc = gfshare_ctx_init_dec(sharenrs, cc->sharecount, cc->statinfo.st_size);
  
  if (gfc == NULL) {
    ret = -ENOMEM;
    goto out;
  }
  
  for (i = 0; i < cc->sharecount; ++i) {
    int fd = open(cc->shares[i].fname, O_RDONLY);
    
    if (fd == -1) {
      ret = -errno;
      goto out;
    }
    
    if (read(fd, sparebuf, *len) != *len) {
      ret = -errno;
      close(fd);
      goto out;
    }
    
    close(fd);
    
    gfshare_ctx_dec_giveshare(gfc, i, sparebuf);
  }
  
  gfshare_ctx_dec_extract(gfc, (unsigned char *)*buf);

  out: 
  if (ret < 0 ) {
    if (*buf != NULL) {
      munlock(*buf, *len);
      free(*buf);
    }
  }
  if (gfc != NULL) {
    gfshare_ctx_free(gfc);
  }
  free(sharenrs);
  free_content(c);
  return ret;
}