11 void writeFile( const std::string& name, const std::string& content ) {
12 std::ofstream file( name );
15 //! \FIXME: Error checking
18 std::string readFile( const std::string& name ) {
19 std::ifstream t( name );
20 std::string res = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
25 std::string writeBackFile( const std::string& serial, const std::string& cert, const std::string& keydir ) {
28 std::string filename = keydir;
30 if( 0 != mkdir( filename.c_str(), 0755 ) ) {
31 if( EEXIST != errno ) {
32 throw std::runtime_error("Storage location could not be determined");
35 //! \FIXME: Check this is a directory
40 if( 0 != mkdir( filename.c_str(), 0755 ) ) {
41 if( EEXIST != errno ) {
45 //! \FIXME: Check this is a directory
50 if( serial.length() < 3 ) {
53 first = serial.substr( 0, serial.length() - 3 );
56 filename += "/" + first;
58 if( 0 != mkdir( filename.c_str(), 0755 ) ) {
59 if( EEXIST != errno ) {
63 //! \FIXME: Check this is a directory
66 filename += "/" + serial + ".crt";
67 writeFile( filename, cert );
72 bool isDigit( char c ) {
73 return ( c >= '0' ) && ( c <= '9' );
76 std::pair<bool, time_t> parseDate( const std::string& date ) {
77 if( date.size() != 10 || date[4] != '-' || date[7] != '-' ) {
78 return std::pair<bool, time_t>( false, 0 );
81 if( !isDigit( date[0] )
82 || !isDigit( date[1] )
83 || !isDigit( date[2] )
84 || !isDigit( date[3] )
85 || !isDigit( date[5] )
86 || !isDigit( date[6] )
87 || !isDigit( date[8] )
88 || !isDigit( date[9] ) ) {
89 return std::pair<bool, time_t>( false, 0 );
96 t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
97 t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
98 t.tm_mday = std::stoi( date.substr( 8, 2 ) );
99 t.tm_wday = 0; /* Day of the week (0-6, Sunday = 0) */
100 t.tm_yday = 0; /* Day in the year (0-365, 1 Jan = 0) */
101 t.tm_isdst = 0; /* Daylight saving time */
103 setenv( "TZ", "UTC", 1 );
105 std::time_t res = mktime( &t );
107 std::size_t siz = strftime( check, 11, "%Y-%m-%d", &t );
110 return std::pair<bool, time_t>( false, 0 ); // NO-COVERAGE (by contract of strftime)
113 std::string checkS( check, siz );
115 if( checkS != date ) {
119 return std::pair<bool, time_t>( true, res );
122 std::pair<bool, time_t> addMonths( std::time_t t, int32_t count ) {
123 std::tm* parsed = gmtime( &t );
125 if( !parsed || count <= 0 || count > 24 ) { // FIXED MAX-Validity-Length
126 return std::pair<bool, time_t>( false, 0 );
129 parsed->tm_mon += count;
130 int oldday = parsed->tm_mday;
131 setenv( "TZ", "UTC", 1 );
133 std::time_t res = mktime( parsed );
135 if( parsed->tm_mday != oldday ) {
137 res = mktime( parsed );
140 return std::pair<bool, time_t>( true, res );
143 std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& date ) {
144 if( date[date.size() - 1] != 'm' ) {
145 return std::pair<bool, time_t>( false, 0 );
150 int num = std::stoi( date.substr( 0, date.size() - 1 ) , &end );
152 if( end != date.size() - 1 ) {
153 return std::pair<bool, time_t>( false, 0 );
156 return addMonths( t, num );
157 } catch( const std::invalid_argument& a ) {
158 return std::pair<bool, time_t>( false, 0 );
159 } catch( const std::out_of_range& a ) {
160 return std::pair<bool, time_t>( false, 0 );
164 std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& date ) {
165 if( date[date.size() - 1] != 'y' ) {
166 return std::pair<bool, time_t>( false, 0 );
171 int num = std::stoi( date.substr( 0, date.size() - 1 ), &end );
173 if( end != date.size() - 1 ) {
174 return std::pair<bool, time_t>( false, 0 );
177 return addMonths( t, num * 12 );
178 } catch( std::invalid_argument& a ) {
179 return std::pair<bool, time_t>( false, 0 );
180 } catch( std::out_of_range& a ) {
181 return std::pair<bool, time_t>( false, 0 );
185 std::unique_ptr<std::ofstream> openLogfile( const std::string &name ) {
187 std::string tname = name;
190 while( stat( tname.c_str(), &buffer ) == 0 ) {
191 tname = name + "_" + std::to_string( ctr++ );
194 auto res = std::make_unique<std::ofstream>( tname );
196 if( ! res->good() ) {
197 throw std::runtime_error( std::string("Failed to open file for logging: " ) + name );
203 std::string timestamp(){
205 if( time( &c_time ) == -1 ) {
206 throw std::runtime_error( "Error while fetching time?" );
208 return std::to_string( c_time );