/* * Copyright (c) 2013-2015 pCloud Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of pCloud Ltd nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL pCloud Ltd BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "pclsync_lib.h" #include "psynclib.h" #include "pcompat.h" #include #include #include #include #include #include #include namespace cc = console_client; namespace clib = cc::clibrary; clib::pclsync_lib& clib::pclsync_lib::get_lib(){ static clib::pclsync_lib g_lib; return g_lib; } static std::string exec(const char* cmd) { FILE* pipe=popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[128]; std::string result = ""; while (!feof(pipe)) { if (fgets(buffer, 128, pipe) != NULL) result += buffer; } pclose(pipe); return result; } char * clib::pclsync_lib::get_token(){ return psync_get_token(); } void clib::pclsync_lib::get_pass_from_console() { do_get_pass_from_console(password_); } void clib::pclsync_lib::get_cryptopass_from_console() { do_get_pass_from_console(crypto_pass_); } void clib::pclsync_lib::do_get_pass_from_console(std::string& password) { if (daemon_) { std::cout << "Not able to read password when started as daemon." << std::endl; exit(1); } #ifdef P_OS_POSIX termios oldt; tcgetattr(STDIN_FILENO, &oldt); termios newt = oldt; newt.c_lflag &= ~ECHO; tcsetattr(STDIN_FILENO, TCSANOW, &newt); std::cout << "Please, enter password" << std::endl; getline(std::cin, password); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); #else HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD modeoff; DWORD modeon; DWORD mode; GetConsoleMode(hStdin, &mode); modeoff = mode & ~ENABLE_ECHO_INPUT; modeon = mode | ENABLE_ECHO_INPUT; SetConsoleMode(hStdin, modeoff); std::cout << "Please, enter password" << std::endl; getline(std::cin, password); SetConsoleMode(hStdin, modeon); //std::cout << "Password is " << password << std::endl; #endif } void event_handler(psync_eventtype_t event, psync_eventdata_t eventdata){ if (eventdownloadstr << "| Up: " << status->uploadstr <<", status is " << status2string(status->status) << std::endl; *clib::pclsync_lib::get_lib().status_ = *status; if (status->status==PSTATUS_LOGIN_REQUIRED){ if (clib::pclsync_lib::get_lib().get_password().empty()) clib::pclsync_lib::get_lib().get_pass_from_console(); //std::cout << "Username: " << clib::pclsync_lib::get_lib().get_username().c_str() << "| Password: " << clib::pclsync_lib::get_lib().get_password().c_str() << std::endl; psync_set_user_pass(clib::pclsync_lib::get_lib().get_username().c_str(), clib::pclsync_lib::get_lib().get_password().c_str(), (int) clib::pclsync_lib::get_lib().save_pass_); std::cout << "logging in" << std::endl; } else if (status->status==PSTATUS_BAD_LOGIN_DATA){ if (!clib::pclsync_lib::get_lib().newuser_) { clib::pclsync_lib::get_lib().get_pass_from_console(); psync_set_user_pass(clib::pclsync_lib::get_lib().get_username().c_str(), clib::pclsync_lib::get_lib().get_password().c_str(), (int) clib::pclsync_lib::get_lib().save_pass_); } else { std::cout << "registering" << std::endl; if (psync_register(clib::pclsync_lib::get_lib().get_username().c_str(), clib::pclsync_lib::get_lib().get_password().c_str(), 1, "bineapi.pcloud.com", 2, &err) ){ std::cout << "both login and registration failed" << std::endl; exit(1); } else{ std::cout << "registered, logging in" << std::endl; psync_set_user_pass(clib::pclsync_lib::get_lib().get_username().c_str(), clib::pclsync_lib::get_lib().get_password().c_str(), (int) clib::pclsync_lib::get_lib().save_pass_); } } } if (status->status==PSTATUS_READY || status->status==PSTATUS_UPLOADING || status->status==PSTATUS_DOWNLOADING || status->status==PSTATUS_DOWNLOADINGANDUPLOADING){ if (!cryptocheck){ cryptocheck=1; if (clib::pclsync_lib::get_lib().setup_crypto_) { lib_setup_cripto(); } } psync_fs_start(); } if (clib::pclsync_lib::get_lib().status_callback_) clib::pclsync_lib::get_lib().status_callback_((int)status->status, status2string(status->status)); } int clib::pclsync_lib::statrt_crypto (const char* pass, void * rep) { std::cout << "calling startcrypto pass: "<