]> WPIA git - cassiopeia.git/blob - src/util.cpp
55d586405d738cf94343de66361b0f4490e6319b
[cassiopeia.git] / src / util.cpp
1 #include "util.h"
2
3 #include <sys/stat.h>
4
5 #include <fstream>
6 #include <iostream>
7 #include <sstream>
8 #include <time.h>
9 #include <stdexcept>
10
11 void writeFile( const std::string& name, const std::string& content ) {
12     std::ofstream file( name );
13     file << content;
14
15     //! \FIXME: Error checking
16 }
17
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>() );
21
22     return res;
23 }
24
25 std::string writeBackFile( const std::string& serial, const std::string& cert, const std::string& keydir ) {
26     errno = 0;
27
28     std::string filename = keydir;
29
30     if( 0 != mkdir( filename.c_str(), 0755 ) ) {
31         if( EEXIST != errno ) {
32             throw std::runtime_error( "Storage location could not be determined" );
33         }
34
35         //! \FIXME: Check this is a directory
36     }
37
38     filename += "/crt";
39
40     if( 0 != mkdir( filename.c_str(), 0755 ) ) {
41         if( EEXIST != errno ) {
42             return "";
43         }
44
45         //! \FIXME: Check this is a directory
46     }
47
48     std::string first;
49
50     if( serial.length() < 3 ) {
51         first = "0";
52     } else {
53         first = serial.substr( 0, serial.length() - 3 );
54     }
55
56     filename += "/" + first;
57
58     if( 0 != mkdir( filename.c_str(), 0755 ) ) {
59         if( EEXIST != errno ) {
60             return "";
61         }
62
63         //! \FIXME: Check this is a directory
64     }
65
66     filename += "/" + serial + ".crt";
67     writeFile( filename, cert );
68
69     return filename;
70 }
71
72 bool isDigit( char c ) {
73     return ( c >= '0' ) && ( c <= '9' );
74 }
75
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 );
79     }
80
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 );
90     }
91
92     std::tm t = {};
93     t.tm_sec = 0;
94     t.tm_min = 0;
95     t.tm_hour = 0;
96     t.tm_mday = std::stoi( date.substr( 8, 2 ) );
97     t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
98     t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
99
100     setenv( "TZ", "UTC", 1 );
101     tzset();
102     std::time_t res = mktime( &t );
103     char check[11];
104     std::size_t siz = strftime( check, 11, "%Y-%m-%d", &t );
105
106     if( siz != 10 ) {
107         return std::pair<bool, time_t>( false, 0 ); // NO-COVERAGE (by contract of strftime)
108     }
109
110     std::string checkS( check, siz );
111
112     if( checkS != date ) {
113         return { false, 0 };
114     }
115
116     return std::pair<bool, time_t>( true, res );
117 }
118
119 std::pair<bool, time_t> addMonths( std::time_t t, int32_t count ) {
120     std::tm* parsed = gmtime( &t );
121
122     if( !parsed || count <= 0 || count > 24 ) { // FIXED MAX-Validity-Length
123         return std::pair<bool, time_t>( false, 0 );
124     }
125
126     parsed->tm_mon += count;
127     int oldday = parsed->tm_mday;
128     setenv( "TZ", "UTC", 1 );
129     tzset();
130     std::time_t res = mktime( parsed );
131
132     if( parsed->tm_mday != oldday ) {
133         parsed->tm_mday = 0;
134         res = mktime( parsed );
135     }
136
137     return std::pair<bool, time_t>( true, res );
138 }
139
140 std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& date ) {
141     if( date[date.size() - 1] != 'm' ) {
142         return  std::pair<bool, time_t>( false, 0 );
143     }
144
145     try {
146         size_t end = 0;
147         int num = std::stoi( date.substr( 0, date.size() - 1 ), &end );
148
149         if( end != date.size() - 1 ) {
150             return  std::pair<bool, time_t>( false, 0 );
151         }
152
153         return addMonths( t, num );
154     } catch( const std::invalid_argument& a ) {
155         return std::pair<bool, time_t>( false, 0 );
156     } catch( const std::out_of_range& a ) {
157         return std::pair<bool, time_t>( false, 0 );
158     }
159 }
160
161 std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& date ) {
162     if( date[date.size() - 1] != 'y' ) {
163         return  std::pair<bool, time_t>( false, 0 );
164     }
165
166     try {
167         size_t end = 0;
168         int num = std::stoi( date.substr( 0, date.size() - 1 ), &end );
169
170         if( end != date.size() - 1 ) {
171             return  std::pair<bool, time_t>( false, 0 );
172         }
173
174         return addMonths( t, num * 12 );
175     } catch( std::invalid_argument& a ) {
176         return std::pair<bool, time_t>( false, 0 );
177     } catch( std::out_of_range& a ) {
178         return std::pair<bool, time_t>( false, 0 );
179     }
180 }
181
182 std::unique_ptr<std::ofstream> openLogfile( const std::string &name ) {
183     struct stat buffer;
184     std::string tname = name;
185     int ctr = 2;
186
187     while( stat( tname.c_str(), &buffer ) == 0 ) {
188         tname = name + "_" + std::to_string( ctr++ );
189     }
190
191     auto res = std::make_unique<std::ofstream>( tname );
192
193     if( ! res->good() ) {
194         throw std::runtime_error( std::string( "Failed to open file for logging: " ) + name );
195     }
196
197     return res;
198 }
199
200 std::string timestamp() {
201     time_t c_time;
202
203     if( time( &c_time ) == -1 ) {
204         throw std::runtime_error( "Error while fetching time?" );
205     }
206
207     return std::to_string( c_time );
208 }