+ config.close();
+
+ std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
+ std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
+
+ while( true ) {
+ std::shared_ptr<Job> job = jp->fetchJob();
+
+ if( !job ) {
+ std::cout << "Nothing to work on" << std::endl;
+ sleep( 5 );
+ continue;
+ }
+
+ if( job->task == "sign" ) {
+ try {
+ std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
+
+ if( !cert ) {
+ std::cout << "wasn't able to load CSR" << std::endl;
+ return 2;
+ }
+
+ std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
+ std::ifstream t( cert->csr );
+ cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
+
+ std::shared_ptr<SignedCertificate> res = sign->sign( cert );
+ std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
+ res->crt_name = fn;
+ jp->writeBack( job, res );
+ } catch( const char* c ) {
+ std::cerr << "ERROR: " << c << std::endl;
+ return 2;
+ } catch( std::string c ) {
+ std::cerr << "ERROR: " << c << std::endl;
+ return 2;
+ }
+ } else {
+ std::cout << "Unknown job type" << job->task << std::endl;
+ }
+
+ if( DAEMON && !jp->finishJob( job ) ) {
+ return 1;
+ }
+
+ if( !DAEMON || once ) {
+ return 0;
+ }
+ }