author | Michael Krelin <hacker@klever.net> | 2012-01-20 20:46:30 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2012-01-20 20:46:30 (UTC) |
commit | 62d09cb2346588773a9e25ac9e9a942816b8581a (patch) (side-by-side diff) | |
tree | 791221bad465ac702a481490d5fdab0a96b6e185 | |
parent | a9f093cc83926b0e83324aa9e022903563d9d801 (diff) | |
download | iii-62d09cb2346588773a9e25ac9e9a942816b8581a.zip iii-62d09cb2346588773a9e25ac9e9a942816b8581a.tar.gz iii-62d09cb2346588773a9e25ac9e9a942816b8581a.tar.bz2 |
minor but numerous changes from compiler-happiness department
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/eyefiservice.cc | 2 | ||||
-rw-r--r-- | src/eyefiworker.cc | 4 | ||||
-rw-r--r-- | src/eyefiworker.h | 2 | ||||
-rw-r--r-- | src/eyekinfig.cc | 2 | ||||
-rw-r--r-- | src/eyetil.cc | 10 | ||||
-rw-r--r-- | src/iiid.cc | 2 |
6 files changed, 11 insertions, 11 deletions
diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index 2586ade..e89b2fc 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc @@ -1,229 +1,229 @@ #include <cassert> #include <iostream> #include <fstream> #include <stdexcept> #include <iterator> #include <algorithm> #include <syslog.h> #include <sys/wait.h> #include <autosprintf.h> #include "eyekinfig.h" #include "eyetil.h" #include "soapeyefiService.h" static binary_t session_nonce; static bool detached_child() { pid_t p = fork(); if(p<0) { syslog(LOG_ERR,"Failed to fork away for hook execution"); _exit(-1); } if(!p) { setsid(); for(int i=getdtablesize();i>=0;--i) close(i); int i=open("/dev/null",O_RDWR); assert(i==0); i = dup(i); assert(i==1); i = dup(i); assert(i==2); return true; } return false; } int eyefiService::StartSession( std::string macaddress,std::string cnonce, int transfermode,long transfermodetimestamp, struct rns__StartSessionResponse &r ) try { #ifndef NDEBUG syslog(LOG_DEBUG, "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); #endif eyekinfig_t eyekinfig(macaddress); r.credential = binary_t(macaddress+cnonce+eyekinfig.get_upload_key()).md5().hex(); r.snonce = session_nonce.make_nonce().hex(); r.transfermode=transfermode; r.transfermodetimestamp=transfermodetimestamp; r.upsyncallowed=false; std::string cmd = eyekinfig.get_on_start_session(); if(!cmd.empty()) { if(detached_child()) { putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; execv("/bin/sh",argv); syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); _exit(-1); } } return SOAP_OK; }catch(std::runtime_error& e) { syslog(LOG_ERR,"error while processing StartSession: %s",e.what()); return soap_receiverfault(e.what(),0); } int eyefiService::GetPhotoStatus( std::string credential, std::string macaddress, std::string filename, long filesize, std::string filesignature, int flags, struct rns__GetPhotoStatusResponse &r ) { #ifndef NDEBUG syslog(LOG_DEBUG, "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s, flags=%d; session nonce=%s", macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), flags, session_nonce.hex().c_str() ); #endif std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); #ifndef NDEBUG syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); #endif if (credential != computed_credential) throw std::runtime_error("card authentication failed"); r.fileid = 1; r.offset = 0; return SOAP_OK; } int eyefiService::MarkLastPhotoInRoll( std::string macaddress, int mergedelta, - struct rns__MarkLastPhotoInRollResponse &r ) { + struct rns__MarkLastPhotoInRollResponse&/* r */ ) { #ifndef NDEBUG syslog(LOG_DEBUG, "MarkLastPhotoInRoll request from %s with mergedelta=%d", macaddress.c_str(), mergedelta ); #endif std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); if(!cmd.empty()) { if(detached_child()) { putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); putenv( gnu::autosprintf("EYEFI_MERGEDELTA=%d",mergedelta) ); char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; execv("/bin/sh",argv); syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); _exit(-1); } } keep_alive = 0; return SOAP_OK; } int eyefiService::UploadPhoto( int fileid, std::string macaddress, std::string filename, long filesize, std::string filesignature, std::string encryption, int flags, struct rns__UploadPhotoResponse& r ) { #ifndef NDEBUG syslog(LOG_DEBUG, "UploadPhoto request from %s with fileid=%d, filename=%s, filesize=%ld," " filesignature=%s, encryption=%s, flags=%04X", macaddress.c_str(), fileid, filename.c_str(), filesize, filesignature.c_str(), encryption.c_str(), flags ); #endif eyekinfig_t eyekinfig(macaddress); umask(eyekinfig.get_umask()); std::string td = eyekinfig.get_targetdir(); tmpdir_t indir(td+"/.incoming.XXXXXX"); std::string tf,lf; binary_t digest, idigest; for(soap_multipart::iterator i=mime.begin(),ie=mime.end();i!=ie;++i) { #ifndef NDEBUG syslog(LOG_DEBUG, " MIME attachment with id=%s, type=%s, size=%ld", (*i).id, (*i).type, (long)(*i).size ); #endif if((*i).id && !strcmp((*i).id,"INTEGRITYDIGEST")) { std::string idigestr((*i).ptr,(*i).size); #ifndef NDEBUG syslog(LOG_DEBUG, " INTEGRITYDIGEST=%s", idigestr.c_str()); #endif idigest.from_hex(idigestr); } if( (*i).id && !strcmp((*i).id,"FILENAME") ) { assert( (*i).type && !strcmp((*i).type,"application/x-tar") ); #ifdef III_SAVE_TARS std::string tarfile = indir.get_file(filename); { std::ofstream(tarfile.c_str(),std::ios::out|std::ios::binary).write((*i).ptr,(*i).size); } #endif if(!tf.empty()) throw std::runtime_error("already seen tarball"); if(!digest.empty()) throw std::runtime_error("already have integrity digest"); digest = integrity_digest((*i).ptr,(*i).size,eyekinfig.get_upload_key()); #ifndef NDEBUG syslog(LOG_DEBUG," computed integrity digest=%s", digest.hex().c_str()); #endif tarchive_t a((*i).ptr,(*i).size); while(a.read_next_header()) { std::string f = indir.get_file(a.entry_pathname()); std::string::size_type fl = f.length(); if(fl<4) continue; const char *s = f.c_str()+fl-4; static const char *suffixes[] = { ".JPG",".AVI",".MP4",".NEF",".RAW",".TIF",".DNG",".CRW", ".RW2",".CR2" }; if(std::find_if(suffixes,suffixes+sizeof(suffixes)/sizeof(*suffixes), std::not1(std::bind1st(std::ptr_fun(strcasecmp),s))) != suffixes+sizeof(suffixes)/sizeof(*suffixes)) tf = f; else if(!strcasecmp(s,".log")) lf = f; else continue; int fd=open(f.c_str(),O_CREAT|O_WRONLY,0666); if(fd<0) throw std::runtime_error(gnu::autosprintf("failed to create output file '%s'",f.c_str())); if(!a.read_data_into_fd(fd)) throw std::runtime_error(gnu::autosprintf("failed to untar file into '%s'",f.c_str())); close(fd); } } } if(tf.empty()) throw std::runtime_error("haven't seen THE file"); if(digest!=idigest) throw std::runtime_error("integrity digest verification failed"); std::string::size_type ls = tf.rfind('/'); // XXX: actually, lack of '/' signifies error here std::string tbn = (ls==std::string::npos)?tf:tf.substr(ls+1); ls = lf.rfind('/'); std::string lbn = (ls==std::string::npos)?lf:lf.substr(ls+1); std::string ttf,tlf; bool success = false; for(int i=0;i<32767;++i) { const char *fmt = i ? "%1$s/(%3$05d)%2$s" : "%1$s/%2$s"; ttf = (const char*)gnu::autosprintf(fmt,td.c_str(),tbn.c_str(),i); if(!lf.empty()) tlf = (const char*)gnu::autosprintf(fmt,td.c_str(),lbn.c_str(),i); if( (!link(tf.c_str(),ttf.c_str())) && (lf.empty() || !link(lf.c_str(),tlf.c_str())) ) { unlink(tf.c_str()); if(!lf.empty()) unlink(lf.c_str()); success=true; break; } } std::string cmd = eyekinfig.get_on_upload_photo(); if(success && !cmd.empty()) { if(detached_child()) { putenv( gnu::autosprintf("EYEFI_UPLOADED_ORIG=%s",tbn.c_str()) ); putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); putenv( gnu::autosprintf("EYEFI_UPLOADED=%s",ttf.c_str()) ); if(!lf.empty()) putenv( gnu::autosprintf("EYEFI_LOG=%s",tlf.c_str()) ); char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; execv("/bin/sh",argv); syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); _exit(-1); } } r.success = true; return SOAP_OK; } diff --git a/src/eyefiworker.cc b/src/eyefiworker.cc index 2d7827c..c051482 100644 --- a/src/eyefiworker.cc +++ b/src/eyefiworker.cc @@ -1,36 +1,36 @@ #include <signal.h> #include <stdexcept> #include "eyefiworker.h" eyefiworker::eyefiworker() : eyefiService(SOAP_IO_STORE|SOAP_IO_KEEPALIVE) { bind_flags = SO_REUSEADDR; max_keep_alive = 0; socket_flags = #if defined(MSG_NOSIGNAL) MSG_NOSIGNAL #elif defined(SO_NOSIGPIPE) SO_NOSIGPIPE #else #error Something is wrong with sigpipe prevention on the platform #endif ; } -int eyefiworker::run(int port) { - if(!soap_valid_socket(bind(0,port,64))) +int eyefiworker::run(int bindport) { + if(!soap_valid_socket(bind(0,bindport,64))) throw std::runtime_error("failed to bind()"); signal(SIGCHLD,SIG_IGN); while(true) { if(!soap_valid_socket(accept())) throw std::runtime_error("failed to accept()"); pid_t p = fork(); if(p<0) throw std::runtime_error("failed to fork()"); if(!p) { recv_timeout = 600; send_timeout = 120; (void)serve(); soap_destroy(this); soap_end(this); soap_done(this); _exit(0); } close(socket); socket = SOAP_INVALID_SOCKET; } } diff --git a/src/eyefiworker.h b/src/eyefiworker.h index c08ec8b..afb97c7 100644 --- a/src/eyefiworker.h +++ b/src/eyefiworker.h @@ -1,15 +1,15 @@ #ifndef __EYEFIWORKER_H #define __EYEFIWORKER_H #include "soapeyefiService.h" class eyefiworker : public eyefiService { public: eyefiworker(); - int run(int port); + int run(int port) __attribute__ ((noreturn)); }; #endif /* __EYEFIWORKER_H */ diff --git a/src/eyekinfig.cc b/src/eyekinfig.cc index 27a5a56..56c74a7 100644 --- a/src/eyekinfig.cc +++ b/src/eyekinfig.cc @@ -1,67 +1,67 @@ #include <cassert> #include <stdexcept> #include <autosprintf.h> #include "eyekinfig.h" #include "config.h" eyekinfig_t::eyekinfig_t(const std::string& ma) : macaddress(ma) { static cfg_opt_t opts[] = { CFG_STR((char*)"targetdir",(char*)"/var/lib/" PACKAGE "/%s",CFGF_NONE), CFG_STR((char*)"uploadkey",(char*)"",CFGF_NONE), CFG_STR((char*)"on-start-session",(char*)"",CFGF_NONE), CFG_STR((char*)"on-upload-photo",(char*)"",CFGF_NONE), CFG_STR((char*)"on-mark-last-photo-in-roll",(char*)"",CFGF_NONE), CFG_INT((char*)"umask",022,CFGF_NONE), CFG_END() }; cfg = cfg_init(opts,CFGF_NONE); if(!cfg) throw std::runtime_error("failed to cfg_init()"); std::string::size_type ls = macaddress.rfind('/'); if(cfg_parse(cfg,gnu::autosprintf( EYEKIN_CONF_DIR "/%s.conf", (ls==std::string::npos) ? macaddress.c_str() : macaddress.substr(ls+1).c_str() )) ==CFG_PARSE_ERROR) { if(cfg) cfg_free(cfg); cfg=0; throw std::runtime_error("failed to cfg_parse()"); } } eyekinfig_t::~eyekinfig_t() { if(cfg) cfg_free(cfg); } std::string eyekinfig_t::get_targetdir() { assert(cfg); return gnu::autosprintf(cfg_getstr(cfg,"targetdir"),macaddress.c_str()); } std::string eyekinfig_t::get_upload_key() { assert(cfg); return cfg_getstr(cfg,"uploadkey"); } std::string eyekinfig_t::get_on_start_session() { assert(cfg); return cfg_getstr(cfg,"on-start-session"); } std::string eyekinfig_t::get_on_upload_photo() { assert(cfg); return cfg_getstr(cfg,"on-upload-photo"); } std::string eyekinfig_t::get_on_mark_last_photo_in_roll() { assert(cfg); return cfg_getstr(cfg,"on-mark-last-photo-in-roll"); } int eyekinfig_t::get_umask() { assert(cfg); - return cfg_getint(cfg,"umask"); + return 0777&cfg_getint(cfg,"umask"); } diff --git a/src/eyetil.cc b/src/eyetil.cc index 2e6ab7e..11e2fb7 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc @@ -1,173 +1,173 @@ #include <stdlib.h> #include <sys/stat.h> #include <syslog.h> #include <iostream> #include <cassert> #include <stdexcept> #include <algorithm> #include <numeric> #include <openssl/md5.h> #include "eyetil.h" #include "config.h" #ifdef HAVE_LIBUUID # include <uuid/uuid.h> #endif binary_t& binary_t::from_hex(const std::string& h) { std::string::size_type hs = h.length(); if(hs&1) throw std::runtime_error("odd number of characters in hexadecimal number"); - int rvs = hs>>1; + size_t rvs = hs>>1; resize(rvs); const unsigned char *hp = (const unsigned char*)h.data(); iterator oi=begin(); char t[3] = { 0,0,0 }; - for(int i=0;i<rvs;++i) { + for(size_t i=0;i<rvs;++i) { t[0]=*(hp++); t[1]=*(hp++); - *(oi++) = strtol(t,0,16); + *(oi++) = static_cast<binary_t::value_type>(0xff&strtol(t,0,16)); } return *this; } binary_t& binary_t::from_data(const void *d,size_t s) { resize(s); std::copy((const unsigned char*)d,(const unsigned char *)d+s, begin() ); return *this; } binary_t& binary_t::make_nonce() { #ifdef HAVE_LIBUUID uuid_t uuid; uuid_generate(uuid); from_data((unsigned char*)uuid,sizeof(uuid)); #else resize(16); std::generate_n(begin(),16,rand); #endif /* HAVE_LIBUUID */ return *this; } std::string binary_t::hex() const { std::string rv; rv.reserve((size()<<1)+1); char t[3] = {0,0,0}; for(const_iterator i=begin(),ie=end();i!=ie;++i) { - int rc = snprintf(t,sizeof(t),"%02x",*i); + size_t rc = snprintf(t,sizeof(t),"%02x",*i); assert(rc<sizeof(t)); rv += t; } return rv; } binary_t binary_t::md5() const { binary_t rv(MD5_DIGEST_LENGTH); if(!MD5( (const unsigned char*)&(front()),size(), (unsigned char*)&(rv.front()) )) throw std::runtime_error("failed to md5()"); return rv; } void md5_digester::init() { if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()"); } void md5_digester::update(const void *d,size_t l) { if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()"); } binary_t md5_digester::final() { binary_t rv(MD5_DIGEST_LENGTH); if(!MD5_Final((unsigned char*)&(rv.front()), &ctx)) throw std::runtime_error("failed to MD5_Final()"); return rv; } static void make_path_for_template(const std::string& p,mode_t m) { struct stat st; std::string pp; for(std::string::size_type sl=p.find('/',1); sl!=std::string::npos; sl=p.find('/',sl+1)) { if(stat( (pp=p.substr(0,sl)).c_str() ,&st) || !S_ISDIR(st.st_mode)) { if(mkdir(pp.c_str(),m)) throw std::runtime_error("failed to mkdir()"); } } } tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { make_path_for_template(dt,0777); if(!mkdtemp((char*)dir.data())) throw std::runtime_error("failed to mkdtmp()"); } tmpdir_t::~tmpdir_t() { assert(!dir.empty()); if(rmdir(dir.c_str())) { syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); } } std::string tmpdir_t::get_file(const std::string& f) { std::string::size_type ls = f.rfind('/'); return dir+'/'+( (ls==std::string::npos) ? f : f.substr(ls+1) ); } tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { if(!a) throw std::runtime_error("failed to archive_read_new()"); if(archive_read_support_format_tar(a)) { archive_read_finish(a); throw std::runtime_error("failed to archive_read_support_format_tar()"); } if(archive_read_open_memory(a,p,s)) { archive_read_finish(a); throw std::runtime_error("failed to archive_read_open_memory()"); } } tarchive_t::~tarchive_t() { assert(a); archive_read_finish(a); } bool tarchive_t::read_next_header() { assert(a); return archive_read_next_header(a,&e)==ARCHIVE_OK; } std::string tarchive_t::entry_pathname() { assert(a); assert(e); return archive_entry_pathname(e); } bool tarchive_t::read_data_into_fd(int fd) { assert(a); return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; } #pragma pack(1) struct block512_t { enum { words = 512 / sizeof(uint16_t) }; uint16_t data[words]; static uint16_t tcpcksum(block512_t& data) { uint32_t sum = std::accumulate(data.data,data.data+words,0); while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; - return ~sum; + return 0xffff&~sum; } }; #pragma pack() binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) { md5_digester rv; std::transform( (block512_t*)ptr, ((block512_t*)ptr)+size/sizeof(block512_t), rv.updater<uint16_t>(), block512_t::tcpcksum ); rv.update( binary_t(ukey) ); return rv.final(); } diff --git a/src/iiid.cc b/src/iiid.cc index 6c23790..c026050 100644 --- a/src/iiid.cc +++ b/src/iiid.cc @@ -1,86 +1,86 @@ #include <syslog.h> #include <getopt.h> #include <iostream> #include <cassert> #include <stdexcept> #include "eyetil.h" #include "eyefiworker.h" #include "config.h" #include "eyefi.nsmap" #define PHEADER \ PACKAGE " Version " VERSION "\n" \ "Copyright (c) 2009 Klever Group" int main(int argc,char **argv) try { int port = 59278; while(true) { static struct option opts[] = { { "help", no_argument, 0, 'h' }, { "usage", no_argument, 0, 'h' }, { "version", no_argument, 0, 'V' }, { "license", no_argument, 0, 'L' }, { "port", required_argument, 0, 'p' }, { NULL, 0, 0, 0 } }; int c = getopt_long(argc,argv,"hVLp:",opts,NULL); if(c==-1) break; switch(c) { case 'h': std::cerr << PHEADER << std::endl << std::endl << " " << argv[0] << " [options]" << std::endl << std::endl << " -h, --help,\n" " --usage display this text\n" " -V, --version display version information\n" " -L, --license show license\n" " -p <port>, --port=<port> port to listen to\n" " (you're not likely to ever need it)\n" << std::endl << std::endl; exit(0); break; case 'V': std::cerr << VERSION << std::endl; exit(0); break; case 'L': extern const char *COPYING; std::cerr << COPYING << std::endl; exit(0); break; case 'p': - port = strtol(optarg,0,0); + port = 0xffff&strtol(optarg,0,0); if(errno) { std::cerr << "Failed to parse port number" << std::endl; exit(1); } break; default: std::cerr << "Huh?" << std::endl; exit(1); break; } } const char *ident = rindex(*argv,'/'); if(ident) ++ident; else ident = *argv; openlog(ident,LOG_PERROR|LOG_PID,LOG_DAEMON); syslog(LOG_INFO,"Starting iii eye-fi manager"); eyefiworker().run(port); closelog(); return 0; } catch(std::exception& e) { syslog(LOG_CRIT,"Exiting iii daemon, because of error condition"); syslog(LOG_CRIT,"Exception: %s",e.what()); return 1; } |