irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

checksum.cpp
Go to the documentation of this file.
1 /* md5Checksum.c - checksumming routine on the client side
2  */
3 
4 #include "irods_stacktrace.hpp"
6 #include "getRodsEnv.h"
7 #include "irods_log.hpp"
8 #include "objInfo.h"
9 #include "SHA256Strategy.hpp"
10 #include "rodsKeyWdDef.h"
11 #include "rcMisc.h"
12 #include "checksum.hpp"
13 
14 #include <fstream>
15 
16 #define HASH_BUF_SZ (1024*1024)
17 
19  const char* _file_name,
20  char* _checksum,
21  const char* _hash_scheme ) {
22  if ( !_file_name ||
23  !_checksum ||
24  !_hash_scheme ) {
25  rodsLog(
26  LOG_ERROR,
27  "chksumLocFile :: null input param - %p %p %p",
28  _file_name,
29  _checksum,
30  _hash_scheme );
32  }
33 
34  // =-=-=-=-=-=-=-
35  // capture client side configuration
36  rodsEnv env;
37  int status = getRodsEnv( &env );
38  if ( status < 0 ) {
39  return status;
40  }
41 
42  // =-=-=-=-=-=-=-
43  // capture the configured scheme if it is valid
44  std::string env_scheme( irods::SHA256_NAME );
45  if ( strlen( env.rodsDefaultHashScheme ) > 0 ) {
46  env_scheme = env.rodsDefaultHashScheme;
47 
48  }
49 
50  // =-=-=-=-=-=-=-
51  // capture the configured hash match policy if it is valid
52  std::string env_policy;
53  if ( strlen( env.rodsMatchHashPolicy ) > 0 ) {
54  env_policy = env.rodsMatchHashPolicy;
55  // =-=-=-=-=-=-=-
56  // hash scheme keywords are all lowercase
58  env_scheme.begin(),
59  env_scheme.end(),
60  env_scheme.begin(),
61  ::tolower );
62  }
63 
64  // =-=-=-=-=-=-=-
65  // capture the incoming scheme if it is valid
66  std::string hash_scheme;
67  if ( _hash_scheme &&
68  strlen( _hash_scheme ) > 0 &&
69  strlen( _hash_scheme ) < NAME_LEN ) {
70  hash_scheme = _hash_scheme;
71  // =-=-=-=-=-=-=-
72  // hash scheme keywords are all lowercase
74  hash_scheme.begin(),
75  hash_scheme.end(),
76  hash_scheme.begin(),
77  ::tolower );
78  }
79  else {
80  hash_scheme = env_scheme;
81 
82  }
83 
84  // =-=-=-=-=-=-=-
85  // hash scheme keywords are all lowercase
87  hash_scheme.begin(),
88  hash_scheme.end(),
89  hash_scheme.begin(),
90  ::tolower );
92  env_scheme.begin(),
93  env_scheme.end(),
94  env_scheme.begin(),
95  ::tolower );
96 
97 
98  // =-=-=-=-=-=-=-
99  // verify checksum scheme against configuration
100  // if requested
101  std::string final_scheme( env_scheme );
102  if ( !hash_scheme.empty() ) {
103  if ( !env_policy.empty() ) {
104  if ( irods::STRICT_HASH_POLICY == env_policy ) {
105  if ( env_scheme != hash_scheme ) {
107  }
108  }
109  }
110 
111  final_scheme = hash_scheme;
112  }
113 
114  // =-=-=-=-=-=-=-
115  // init the hasher object
116  irods::Hasher hasher;
118  final_scheme,
119  hasher );
120  if ( !ret.ok() ) {
121  irods::log( PASS( ret ) );
122  return ret.code();
123 
124  }
125 
126  // =-=-=-=-=-=-=-
127  // open the local file
128  std::ifstream in_file(
129  _file_name,
130  std::ios::in | std::ios::binary );
131  if ( !in_file.is_open() ) {
132  status = UNIX_FILE_OPEN_ERR - errno;
133  rodsLogError(
134  LOG_ERROR,
135  status,
136  "chksumLocFile - fopen failed for %s, status = %d",
137  _file_name,
138  status );
139  return status;
140  }
141 
142  std::string buffer_read;
143  buffer_read.resize( HASH_BUF_SZ );
144 
145  while ( in_file.read( &buffer_read[0], HASH_BUF_SZ ) ) {
146  hasher.update( buffer_read );
147  }
148 
149  if ( in_file.eof() ) {
150  if ( in_file.gcount() > 0 ) {
151  buffer_read.resize( in_file.gcount() );
152  hasher.update( buffer_read );
153  }
154  } else {
155  status = UNIX_FILE_READ_ERR - errno;
156  rodsLogError(
157  LOG_ERROR,
158  status,
159  "chksumLocFile - read() failed for %s, status = %d",
160  _file_name,
161  status );
162  return status;
163  }
164 
165  // =-=-=-=-=-=-=-
166  // capture the digest
167  std::string digest;
168  hasher.digest( digest );
169  strncpy(
170  _checksum,
171  digest.c_str(),
172  digest.size() + 1 );
173 
174  return 0;
175 
176 } // chksumLocFile
177 
179  char *fileName,
180  const char *myChksum,
181  char *chksumStr ) {
182  // =-=-=-=-=-=-=-
183  // extract scheme from checksum string
184  std::string scheme;
185  irods::error ret = irods::get_hash_scheme_from_checksum( myChksum, scheme );
186  if ( !ret.ok() ) {
187  //irods::log( PASS( ret ) );
188  }
189 
190  char chksumBuf[CHKSUM_LEN];
191  if ( chksumStr == NULL ) {
192  chksumStr = chksumBuf;
193  }
194 
195  int status = chksumLocFile( fileName, chksumStr, scheme.c_str() );
196  if ( status < 0 ) {
197  return status;
198  }
199  if ( strcmp( myChksum, chksumStr ) != 0 ) {
200  return USER_CHKSUM_MISMATCH;
201  }
202  return 0;
203 }
204 
205 int
206 hashToStr( unsigned char *digest, char *digestStr ) {
207  int i;
208  char *outPtr = digestStr;
209 
210  for ( i = 0; i < 16; i++ ) {
211  sprintf( outPtr, "%02x", digest[i] );
212  outPtr += 2;
213  }
214 
215  return 0;
216 }
217 
218 /* rcChksumLocFile - chksum a local file and put the result in the
219  * condInput.
220  * Input -
221  * char *fileName - the local file name
222  * char *chksumFlag - the chksum flag. valid flags are
223  * VERIFY_CHKSUM_KW and REG_CHKSUM_KW.
224  * keyValPair_t *condInput - the result is put into this struct
225  */
226 
227 int
228 rcChksumLocFile( char *fileName, char *chksumFlag, keyValPair_t *condInput, const char* _scheme ) {
229  char chksumStr[NAME_LEN];
230  int status;
231 
232  if ( condInput == NULL || chksumFlag == NULL || fileName == NULL ) {
234  "rcChksumLocFile: NULL input" );
235  return USER__NULL_INPUT_ERR;
236  }
237 
238  if ( strcmp( chksumFlag, VERIFY_CHKSUM_KW ) != 0 &&
239  strcmp( chksumFlag, REG_CHKSUM_KW ) != 0 &&
240  strcmp( chksumFlag, RSYNC_CHKSUM_KW ) != 0 ) {
242  "rcChksumLocFile: bad input chksumFlag %s", chksumFlag );
243  return USER_BAD_KEYWORD_ERR;
244  }
245 
246  status = chksumLocFile( fileName, chksumStr, _scheme );
247  if ( status < 0 ) {
248  return status;
249  }
250 
251  addKeyVal( condInput, chksumFlag, chksumStr );
252 
253  return 0;
254 }
rodsLog
void rodsLog(int level, const char *formatStr,...)
Definition: rodsLog.cpp:86
rodsKeyWdDef.h
NULL
#define NULL
Definition: rodsDef.h:70
addKeyVal
int addKeyVal(keyValPair_t *condInput, const char *keyWord, const char *value)
Definition: rcMisc.cpp:789
getRodsEnv
int getRodsEnv(rodsEnv *myRodsEnv)
Definition: getRodsEnv.cpp:112
chksumLocFile
int chksumLocFile(const char *_file_name, char *_checksum, const char *_hash_scheme)
Definition: checksum.cpp:18
irods::transform
void transform(const InputC &ic, OutputC &oc, UnaryOperation func)
Definition: irods_re_plugin.hpp:822
irods_stacktrace.hpp
PASS
#define PASS(prev_error_)
Definition: irods_error.hpp:118
irods::SHA256_NAME
const std::string SHA256_NAME("sha256")
env
Definition: restructs.hpp:226
rcMisc.h
irods::getHasher
error getHasher(const std::string &name, Hasher &hasher)
Definition: irods_hasher_factory.cpp:27
checksum.hpp
irods::get_hash_scheme_from_checksum
error get_hash_scheme_from_checksum(const std::string &checksum, std::string &scheme)
Definition: irods_hasher_factory.cpp:39
LOG_ERROR
#define LOG_ERROR
Definition: rodsLog.h:43
verifyChksumLocFile
int verifyChksumLocFile(char *fileName, const char *myChksum, char *chksumStr)
Definition: checksum.cpp:178
SYS_INVALID_INPUT_PARAM
@ SYS_INVALID_INPUT_PARAM
Definition: rodsErrorTable.h:195
USER_HASH_TYPE_MISMATCH
@ USER_HASH_TYPE_MISMATCH
Definition: rodsErrorTable.h:284
REG_CHKSUM_KW
#define REG_CHKSUM_KW
Definition: rodsKeyWdDef.h:15
UNIX_FILE_OPEN_ERR
@ UNIX_FILE_OPEN_ERR
Definition: rodsErrorTable.h:300
irods::error::code
long long code() const
Definition: irods_error.cpp:194
USER_BAD_KEYWORD_ERR
@ USER_BAD_KEYWORD_ERR
Definition: rodsErrorTable.h:246
irods::Hasher
Definition: Hasher.hpp:15
irods.pypyodbc.status
status
Definition: pypyodbc.py:467
irods::log
void log(const error &)
Definition: irods_log.cpp:13
irods::Hasher::update
error update(const std::string &)
Definition: Hasher.cpp:22
LOG_NOTICE
#define LOG_NOTICE
Definition: rodsLog.h:33
irods::STRICT_HASH_POLICY
const std::string STRICT_HASH_POLICY("strict")
irods::error
Definition: irods_error.hpp:23
CHKSUM_LEN
#define CHKSUM_LEN
Definition: rodsDef.h:56
UNIX_FILE_READ_ERR
@ UNIX_FILE_READ_ERR
Definition: rodsErrorTable.h:302
objInfo.h
irods::Hasher::digest
error digest(std::string &messageDigest)
Definition: Hasher.cpp:35
RSYNC_CHKSUM_KW
#define RSYNC_CHKSUM_KW
Definition: rodsKeyWdDef.h:58
HASH_BUF_SZ
#define HASH_BUF_SZ
Definition: checksum.cpp:16
VERIFY_CHKSUM_KW
#define VERIFY_CHKSUM_KW
Definition: rodsKeyWdDef.h:16
irods_hasher_factory.hpp
rodsLogError
void rodsLogError(int level, int errCode, char *formatStr,...)
Definition: rodsLog.cpp:422
rcChksumLocFile
int rcChksumLocFile(char *fileName, char *chksumFlag, keyValPair_t *condInput, const char *_scheme)
Definition: checksum.cpp:228
hashToStr
int hashToStr(unsigned char *digest, char *digestStr)
Definition: checksum.cpp:206
rodsEnv
Definition: getRodsEnv.h:8
NAME_LEN
#define NAME_LEN
Definition: rodsDef.h:55
KeyValPair
Definition: objInfo.h:120
irods::error::ok
bool ok()
Definition: irods_error.cpp:258
SHA256Strategy.hpp
irods_log.hpp
USER_CHKSUM_MISMATCH
@ USER_CHKSUM_MISMATCH
Definition: rodsErrorTable.h:245
USER__NULL_INPUT_ERR
@ USER__NULL_INPUT_ERR
Definition: rodsErrorTable.h:247
getRodsEnv.h