void get_ba_member_email(uint64_t userid, char** email /*OUT*/, size_t *length /*OUT*/) {
  psync_sql_res *res;
  psync_variant_row row;
  const char *cstr;
  *length = 0;
  res=psync_sql_query("SELECT mail FROM baccountemail WHERE id=?");
  psync_sql_bind_uint(res, 1, userid);
  if ((row=psync_sql_fetch_row(res))){
      cstr=psync_get_lstring(row[0], length);
      *email=(char *)psync_malloc(*length);
      memcpy(*email, cstr, *length);
  } else {
    psync_sql_res *q;
    psync_userid_t userids[] = {userid};
    email_visitor_params params = {email, length};
    do_psync_account_users(userids, 1, &copy_email, &params);
   
    if (*length) {
      q=psync_sql_prep_statement("INSERT INTO baccountemail  (id, mail) VALUES (?, ?)");
      psync_sql_bind_uint(q, 1, userid);
      psync_sql_bind_lstring(q, 2, *email,  *length);
      psync_sql_run_free(q);
    }
  }
  psync_sql_free_result(res);
}
Exemple #2
0
static int psync_p2p_get_download_token(psync_fileid_t fileid, const unsigned char *filehashhex, uint64_t fsize, unsigned char **token, size_t *tlen){
  binparam params[]={P_STR("auth", psync_my_auth), P_NUM("fileid", fileid), P_NUM("filesize", fsize),
                     P_LSTR(PSYNC_CHECKSUM, filehashhex, PSYNC_HASH_DIGEST_HEXLEN),
                     P_LSTR("keydata", psync_rsa_public_bin->data, psync_rsa_public_bin->datalen)};
  psync_socket *api;
  binresult *res;
  const binresult *ctoken;
  *token=NULL; /* especially for gcc */
  *tlen=0;
  api=psync_apipool_get();
  if (unlikely_log(!api))
    return PSYNC_NET_TEMPFAIL;
  res=send_command(api, "getfileownershiptoken", params);
  if (unlikely_log(!res)){
    psync_apipool_release_bad(api);
    return PSYNC_NET_TEMPFAIL;
  }
  psync_apipool_release(api);
  if (unlikely_log(psync_find_result(res, "result", PARAM_NUM)->num!=0)){
    psync_free(res);
    return PSYNC_NET_PERMFAIL;
  }
  ctoken=psync_find_result(res, "token", PARAM_STR);
  *token=psync_malloc(ctoken->length+1);
  memcpy(*token, ctoken->str, ctoken->length+1);
  *tlen=ctoken->length;
  psync_free(res);
  return PSYNC_NET_OK;
}
static void psync_notifications_download_thumb(const binresult *thumb, const char *thumbpath){
  const char *path, *filename, *host;
  char *filepath, *tmpfilepath, *buff;
  char cookie[128];
  psync_http_socket *sock;
  psync_stat_t st;
  psync_file_t fd;
  int rd;
  rd=-1;
  path=psync_find_result(thumb, "path", PARAM_STR)->str;
  filename=strrchr(path, '/');
  if (unlikely_log(!filename++))
    return;
  filepath=psync_strcat(thumbpath, PSYNC_DIRECTORY_SEPARATOR, filename, NULL);
  if (!psync_stat(filepath, &st)){
    debug(D_NOTICE, "skipping download of %s as it already exists", filename);
    goto err0;
  }
  tmpfilepath=psync_strcat(filepath, ".part", NULL);
  debug(D_NOTICE, "downloading thumbnail %s", filename);
  if (unlikely_log((fd=psync_file_open(tmpfilepath, P_O_WRONLY, P_O_CREAT|P_O_TRUNC))==INVALID_HANDLE_VALUE))
    goto err1;
  sock=psync_http_connect_multihost(psync_find_result(thumb, "hosts", PARAM_ARRAY), &host);
  if (unlikely_log(!sock))
    goto err2;
  psync_slprintf(cookie, sizeof(cookie), "Cookie: dwltag=%s\015\012", psync_find_result(thumb, "dwltag", PARAM_STR)->str);
  if (unlikely_log(psync_http_request(sock, host, path, 0, 0, cookie)))
    goto err3;
  if (unlikely_log(psync_http_next_request(sock)))
    goto err3;
  buff=(char *)psync_malloc(PSYNC_COPY_BUFFER_SIZE);
  while (1){
    rd=psync_http_request_readall(sock, buff, PSYNC_COPY_BUFFER_SIZE);
    if (rd<=0)
      break;
    if (psync_file_write(fd, buff, rd)!=rd)
      break;
  }
  psync_free(buff);
err3:
  psync_http_close(sock);
err2:
  psync_file_close(fd);
err1:
  if (rd==0 && !psync_file_rename_overwrite(tmpfilepath, filepath))
    debug(D_NOTICE, "downloaded thumbnail %s", filename);
  else
    debug(D_WARNING, "downloading of thumbnail %s failed", filename);
  psync_free(tmpfilepath);
err0:
  psync_free(filepath);
}
void psync_send_event_by_path(psync_eventtype_t eventid, psync_syncid_t syncid, const char *localpath, psync_fileorfolderid_t remoteid, const char *remotepath){
  if (eventthreadrunning){
    event_list_t *event;
    size_t llen, rlen, slen;
    char *lcopy, *rcopy, *strct, *name;
    llen=strlen(localpath)+1;
    rlen=strlen(remotepath)+1;
    if (eventid&PEVENT_TYPE_FOLDER)
      slen=sizeof(psync_file_event_t);
    else
      slen=sizeof(psync_folder_event_t);
    event=(event_list_t *)psync_malloc(sizeof(event_list_t)+slen+llen+rlen);
    strct=(char *)(event+1);
    lcopy=strct+slen;
    rcopy=lcopy+llen;
    memcpy(lcopy, localpath, llen);
    memcpy(rcopy, remotepath, rlen);
    name=strrchr(rcopy, '/')+1;
    if (eventid&PEVENT_TYPE_FOLDER){
      psync_folder_event_t *f=(psync_folder_event_t *)strct;
      f->folderid=remoteid;
      f->name=name;
      f->localpath=lcopy;
      f->remotepath=rcopy;
      f->syncid=syncid;
      event->data.folder=f;
    }
    else{
      psync_file_event_t *f=(psync_file_event_t *)strct;
      f->fileid=remoteid;
      f->name=name;
      f->localpath=lcopy;
      f->remotepath=rcopy;
      f->syncid=syncid;
      event->data.file=f;
    }
    event->event=eventid;
    event->freedata=0;
    pthread_mutex_lock(&eventmutex);
    psync_list_add_tail(&eventlist, &event->list);
    pthread_cond_signal(&eventcond);
    pthread_mutex_unlock(&eventmutex);
  }
}
static void psync_notifications_thumb_dir_list(void *ptr, psync_pstat_fast *st){
  psync_tree **tree, **addto, *tr;
  psync_thumb_list_t *tl;
  size_t len;
  int cmp;
  if (st->isfolder)
    return;
  tree=(psync_tree **)ptr;
  tr=*tree;
  if (tr){
    while (1){
      cmp=psync_filename_cmp(st->name, psync_tree_element(tr, psync_thumb_list_t, tree)->name);
      if (cmp<0){
        if (tr->left)
          tr=tr->left;
        else{
          addto=&tr->left;
          break;
        }
      }
      else if (cmp>0){
        if (tr->right)
          tr=tr->right;
        else{
          addto=&tr->right;
          break;
        }
      }
      else{
        debug(D_WARNING, "duplicate name in file list %s, should not happen", st->name);
        return;
      }
    }
  }
  else
    addto=tree;
  len=strlen(st->name)+1;
  tl=(psync_thumb_list_t *)psync_malloc(offsetof(psync_thumb_list_t, name)+len);
  memcpy(tl->name, st->name, len);
  *addto=&tl->tree;
  psync_tree_added_at(tree, tr, &tl->tree);
}
void get_ba_team_name(uint64_t teamid, char** name /*OUT*/, size_t *length /*OUT*/) {
  psync_sql_res *res;
  psync_variant_row row;
  const char *cstr;
  
  res=psync_sql_query("SELECT name FROM baccountteam WHERE id=?");
  psync_sql_bind_uint(res, 1, teamid);
  if ((row=psync_sql_fetch_row(res))){
      cstr=psync_get_lstring(row[0], length);
      *name=(char *)psync_malloc(*length);
      memcpy(*name, cstr, *length);
  } else {
    psync_sql_res *q;
    psync_userid_t teamids[] = {teamid};
    team_visitor_params params = {name, length};
    do_psync_account_teams(teamids, 1, &copy_team, &params);
   
    q=psync_sql_prep_statement("INSERT INTO baccountteam  (id, name) VALUES (?, ?)");
    psync_sql_bind_uint(q, 1, teamid);
    psync_sql_bind_lstring(q, 2, *name,  *length);
    psync_sql_run_free(q);
  }
  psync_sql_free_result(res);
}
void instance_thread(LPVOID lpvParam)
{
  DWORD cbBytesRead = 0, cbWritten = 0;
  BOOL fSuccess = FALSE;
  HANDLE hPipe = NULL;
  char  chBuf[POVERLAY_BUFSIZE];

  message* request = NULL; //(message*)psync_malloc(POVERLAY_BUFSIZE);
  message* reply = (message*)psync_malloc(POVERLAY_BUFSIZE);
 // memset(request,0,sizeof(request));
  memset(reply, 0, POVERLAY_BUFSIZE);
  if (lpvParam == NULL)
  {
    debug(D_ERROR, "InstanceThread got an unexpected NULL value in lpvParam.\n");
    return;
  }

  // debug(D_NOTICE, "InstanceThread created, receiving and processing messages.\n");
  hPipe = (HANDLE)lpvParam;
  while (1)
  {
    do
    {
      fSuccess = ReadFile(
        hPipe,    // pipe handle 
        chBuf,    // buffer to receive reply 
        POVERLAY_BUFSIZE,  // size of buffer 
        &cbBytesRead,  // number of bytes read 
        NULL);    // not overlapped 

      if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
        break;
    } while (!fSuccess);  // repeat loop if ERROR_MORE_DATA 

    if (!fSuccess || cbBytesRead == 0)
    {
      if (GetLastError() == ERROR_BROKEN_PIPE){
        //debug(D_NOTICE, "InstanceThread: client disconnected.\n");
      }
      else{
        //debug(D_NOTICE, "InstanceThread ReadFile failed, GLE=%d.\n", GetLastError());
      }
      break;
    }
    message *request = (message *)chBuf;

    //debug(D_NOTICE, "bytes received  %d buffer[%s]\n", cbBytesRead, chBuf);
    get_answer_to_request(request, reply);
    fSuccess = WriteFile(
      hPipe,        // handle to pipe
      reply,     // buffer to write from
      reply->length, // number of bytes to write
      &cbWritten,   // number of bytes written
      NULL);        // not overlapped I/O

    if (!fSuccess || reply->length != cbWritten)
    {
      //debug(D_NOTICE, "InstanceThread WriteFile failed, GLE=%d.\n", GetLastError());
      break;
    }
  }
  FlushFileBuffers(hPipe);
  DisconnectNamedPipe(hPipe);
  CloseHandle(hPipe);
  psync_free(request);
  psync_free(reply);
  //debug(D_NOTICE, "InstanceThread exitting.\n");
  return;
}
int do_psync_account_stopshare(psync_shareid_t usershareids[], int nusershareid, psync_shareid_t teamshareids[], int nteamshareid, char **err) {
 psync_socket *api;
  binresult *bres;
  uint64_t result,userresult,teamresult;
  char *ids1 = NULL;
  char *ids2 = NULL;
  char *idsp = 0;
  int i,pind = 1, numparam = 1,k;
  binparam *t;
  const binresult *userres, *teamres, *statres;
  *err  = 0;
  
  numparam +=  !!nusershareid + !!nteamshareid;
  if (unlikely(numparam == 1))
    return -3;
    
  t = (binparam *) psync_malloc(numparam*sizeof(binparam));
  
  init_param_str(t, "auth", psync_my_auth);
  
  if (nusershareid) {
    ids1 = (char *) psync_malloc(nusershareid*FOLDERID_ENTRY_SIZE);
    idsp = ids1;
    for (i = 0; i < nusershareid; ++i) {
      k = sprintf(idsp, "%lld",(long long) usershareids[i]);
      if (unlikely(k <= 0 )) break;
      idsp[k] = ',';
      idsp = idsp + k + 1;
    }
    if (i > 0)
      *(idsp - 1) = '\0';
    //debug(D_NOTICE, "usershareids %s",ids1);
    init_param_str(t + pind++, "usershareids", ids1);
  }
  
  if (nteamshareid) {
    ids2 = (char *) psync_malloc(nteamshareid*FOLDERID_ENTRY_SIZE);
    idsp = ids2;
    for (i = 0; i < nteamshareid; ++i) {
      k = sprintf(idsp, "%lld", (long long) teamshareids[i]);
      if (unlikely(k <= 0 )) break;
      idsp[k] = ',';
      idsp = idsp + k + 1;
    }
    if (i > 0)
      *(idsp - 1) = '\0';
    //debug(D_NOTICE, "teamshareids %s",ids2);
    init_param_str(t + pind++, "teamshareids", ids2);
  }
  
  api=psync_apipool_get();
  if (unlikely(!api)) {
    debug(D_WARNING, "Can't gat api from the pool. No pool ?\n");
    return -2;
  }
  
  bres =  do_send_command(api, "account_stopshare", sizeof("account_stopshare") - 1, t, pind, -1, 1);
  
  if (likely(bres))
    psync_apipool_release(api);
  else {
    psync_apipool_release_bad(api);
    debug(D_WARNING, "Send command returned in valid result.\n");
    return -2;
  }
  
  result=psync_find_result(bres, "result", PARAM_NUM)->num;
  if (unlikely(result))
    return handle_result(bres, result, err);
 
  statres = psync_find_result(bres, "status", PARAM_HASH);
  teamres = psync_find_result(statres, "team", PARAM_ARRAY)->array[0];
  teamresult = psync_find_result(teamres, "result", PARAM_NUM)->num;
  userres = psync_find_result(statres, "user", PARAM_ARRAY)->array[0];
  userresult = psync_find_result(userres, "result", PARAM_NUM)->num;
  if (!userresult || !teamresult)
    result = 0;
  else {
    if(userresult == INVALID_SHAREDID_RESULT && teamresult == INVALID_SHAREDID_RESULT)
      result = handle_result(userres, userresult, err); 
    else if (userresult)
      result = handle_result(userres, userresult, err); 
    else 
      result = handle_result(teamres, teamresult, err); 
  }
  
  if (ids1)
    psync_free(ids1);
  if (ids2)
    psync_free(ids2);
  
  psync_free(bres);
  psync_free(t);
  
  return result;
  
}
int do_psync_account_teams(psync_userid_t teamids[], int nids, result_visitor vis, void *param) {
  psync_socket *sock;
  binresult *bres;
  char *ids = NULL;
  char *idsp = 0;
  int k,i;
  const binresult *users;
  
  if (nids) {
    ids = (char *) psync_malloc(nids*FOLDERID_ENTRY_SIZE);
    idsp = ids;
    for (i = 0; i < nids; ++i) {
      k = sprintf(idsp, "%lld", (long long) teamids[i]);
      if (unlikely(k <= 0 )) break;
      idsp[k] = ',';
      idsp = idsp + k + 1;
    }
    if (i > 0)
      *(idsp - 1) = '\0';
    
    //debug(D_NOTICE, "Account_teams numids %d\n", nids);
    
    binparam params[] = {P_STR("auth", psync_my_auth), P_STR("timeformat", "timestamp"),  P_STR("teamids", ids), P_STR("showeveryone", "1")};

    sock = psync_apipool_get();
    bres = send_command(sock, "account_teams", params);
  } else {
    if (psync_my_auth[0]) {
      binparam params[] = {P_STR("auth", psync_my_auth), P_STR("timeformat", "timestamp"), P_STR("showeveryone", "1")};
      sock = psync_apipool_get();
      bres = send_command(sock, "account_teams", params);
    } else if (psync_my_user && psync_my_pass) {
      binparam params[] =  {P_STR("username", psync_my_user), P_STR("password", psync_my_pass), P_STR("timeformat", "timestamp"), P_STR("showeveryone", "1")};
      sock = psync_apipool_get();
      bres = send_command(sock, "account_teams", params);
    } else return -1;
  } 
  if (likely(bres))
    psync_apipool_release(sock);
  else {
    psync_apipool_release_bad(sock);
    debug(D_WARNING, "Send command returned in valid result.\n");
    return -1;
  }
  
  
  users = psync_find_result(bres, "teams", PARAM_ARRAY);
  
  //debug(D_NOTICE, "Result contains %d teams\n", users->length);
  
  if (!users->length){
    psync_free(bres);
    psync_free(ids);
    debug(D_WARNING, "Account_teams returned empty result!\n");
    return -2;
  } else {
    for (i = 0; i < users->length; ++i)
      vis(i, users->array[i], param);
  } 
  psync_free(bres);
  psync_free(ids);
  return 0;
}