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)  

objMetaOpr.cpp
Go to the documentation of this file.
1 
3 /* objMetaOpr.c - metadata operation at the object level */
4 
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include "objMetaOpr.hpp"
8 #include "resource.hpp"
9 #include "collection.hpp"
10 #include "specColl.hpp"
11 #include "genQuery.h"
13 #include "miscUtil.h"
14 #include "modAVUMetadata.h"
15 #include "rsIcatOpr.hpp"
16 #include "rsGenQuery.hpp"
17 #include "rsModAVUMetadata.hpp"
18 
19 // =-=-=-=-=-=-
22 
23 #include "boost/lexical_cast.hpp"
24 
25 int
26 svrCloseQueryOut( rsComm_t *rsComm, genQueryOut_t *genQueryOut ) {
27  genQueryInp_t genQueryInp;
28  genQueryOut_t *junk = NULL;
29  int status;
30 
31  if ( genQueryOut->continueInx <= 0 ) {
32  return 0;
33  }
34 
35  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
36 
37  /* maxRows = 0 specifies that the genQueryOut should be closed */
38  genQueryInp.maxRows = 0;;
39  genQueryInp.continueInx = genQueryOut->continueInx;
40 
41  status = rsGenQuery( rsComm, &genQueryInp, &junk );
42 
43  return status;
44 }
45 
46 int
47 isData( rsComm_t *rsComm, char *objName, rodsLong_t *dataId ) {
48  genQueryInp_t genQueryInp;
49  genQueryOut_t *genQueryOut = NULL;
50  char tmpStr[MAX_NAME_LEN];
51  char logicalEndName[MAX_NAME_LEN];
52  char logicalParentDirName[MAX_NAME_LEN];
53  int status;
54 
55  status = splitPathByKey( objName,
56  logicalParentDirName, MAX_NAME_LEN, logicalEndName, MAX_NAME_LEN, '/' );
57  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
58  snprintf( tmpStr, MAX_NAME_LEN, "='%s'", logicalEndName );
59  addInxVal( &genQueryInp.sqlCondInp, COL_DATA_NAME, tmpStr );
60  addInxIval( &genQueryInp.selectInp, COL_D_DATA_ID, 1 );
61  snprintf( tmpStr, MAX_NAME_LEN, "='%s'", logicalParentDirName );
62  addInxVal( &genQueryInp.sqlCondInp, COL_COLL_NAME, tmpStr );
63  addInxIval( &genQueryInp.selectInp, COL_COLL_ID, 1 );
64  genQueryInp.maxRows = 2;
65  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
66  if ( status >= 0 ) {
67  sqlResult_t *dataIdRes;
68 
69  if ( ( dataIdRes = getSqlResultByInx( genQueryOut, COL_D_DATA_ID ) ) ==
70  NULL ) {
72  "isData: getSqlResultByInx for COL_D_DATA_ID failed" );
74  }
75  if ( dataId != NULL ) {
76  *dataId = strtoll( dataIdRes->value, 0, 0 );
77  }
78  }
79 
80  freeGenQueryOut( &genQueryOut );
81  clearGenQueryInp( &genQueryInp );
82  return status;
83 }
84 
85 int // JMC - backport 4680
87  rsComm_t* _comm,
88  char* _obj_name,
89  char* _resource,
90  char* _phy_path,
91  char* _resc_hier ) {
92  // =-=-=-=-=-=-=-
93  // if no resc hier is specified, call resolve on the object to
94  // ask the resource composition to pick a valid hier for open
95  std::string resc_hier;
96 
97  if ( 0 == strlen( _resc_hier ) ) {
98  dataObjInp_t data_inp;
99  memset( &data_inp, 0, sizeof( data_inp ) );
100  snprintf( data_inp.objPath, sizeof( data_inp.objPath ), "%s", _obj_name );
103  _comm,
104  &data_inp,
105  resc_hier );
106  if ( !ret.ok() ) {
107  irods::log( PASS( ret ) );
109  }
110  }
111  else {
112  resc_hier = _resc_hier;
113  }
114 
115  std::string root_resc;
117  parser.set_string( resc_hier );
118  parser.first_resc( root_resc );
119 
120  // =-=-=-=-=-=-=-
121  // perform a genquery to get the physical path of the data object
122  // as the hier reolve does not do that for us
123  genQueryOut_t* gen_out = NULL;
124  char tmp_str [ MAX_NAME_LEN ];
125  char logical_end_name [ MAX_NAME_LEN ];
126  char logical_parent_dir_name[ MAX_NAME_LEN ];
127 
128  // =-=-=-=-=-=-=-
129  // split the object path by the last delimiter /
130  int status = splitPathByKey(
131  _obj_name,
132  logical_parent_dir_name, MAX_NAME_LEN,
133  logical_end_name, MAX_NAME_LEN, '/' );
134 
135  genQueryInp_t gen_inp;
136  memset( &gen_inp, 0, sizeof( genQueryInp_t ) );
137 
138  // =-=-=-=-=-=-=-
139  // add query to the struct for the data object name
140  snprintf( tmp_str, MAX_NAME_LEN, "='%s'", logical_end_name );
141  addInxVal( &gen_inp.sqlCondInp, COL_DATA_NAME, tmp_str );
142 
143  // =-=-=-=-=-=-=-
144  // add query to the struct for the collection name
145  snprintf( tmp_str, MAX_NAME_LEN, "='%s'", logical_parent_dir_name );
146  addInxVal( &gen_inp.sqlCondInp, COL_COLL_NAME, tmp_str );
147 
148  // =-=-=-=-=-=-=-
149  // add query to the struct for the resource hierarchy
150  snprintf( tmp_str, MAX_NAME_LEN, "='%s'", resc_hier.c_str() );
151  addInxVal( &gen_inp.sqlCondInp, COL_D_RESC_HIER, tmp_str );
152 
153  // =-=-=-=-=-=-=-
154  // include request for data path and resource hierarchy
155  addInxIval( &gen_inp.selectInp, COL_D_DATA_PATH, 1 );
156 
157  // =-=-=-=-=-=-=-
158  // request only 2 results in the set
159  gen_inp.maxRows = 2;
160  status = rsGenQuery( _comm, &gen_inp, &gen_out );
161  if ( status >= 0 ) {
162  // =-=-=-=-=-=-=-
163  // extract the physical path from the query
164  sqlResult_t* phy_path_res = getSqlResultByInx( gen_out, COL_D_DATA_PATH );
165  if ( !phy_path_res ) {
167  "getPhyPath: getSqlResultByInx for COL_D_DATA_PATH failed" );
168  return UNMATCHED_KEY_OR_INDEX;
169  }
170 
171  // =-=-=-=-=-=-=-
172  // copy the results to the out variables
173  strncpy( _phy_path, phy_path_res->value, MAX_NAME_LEN );
174  strncpy( _resource, root_resc.c_str(), root_resc.size() );
175  strncpy( _resc_hier, resc_hier.c_str(), resc_hier.size() );
176  }
177 
178  freeGenQueryOut( &gen_out );
179  clearGenQueryInp( &gen_inp );
180 
181  return status;
182 
183 }
184 
185 int
186 isCollAllKinds( rsComm_t *rsComm, char *objName, rodsLong_t *collId ) {
187  dataObjInp_t dataObjInp;
188  int status;
189  rodsObjStat_t *rodsObjStatOut = NULL;
190 
191  bzero( &dataObjInp, sizeof( dataObjInp ) );
192  rstrcpy( dataObjInp.objPath, objName, MAX_NAME_LEN );
193  status = collStatAllKinds( rsComm, &dataObjInp, &rodsObjStatOut );
194  if ( status >= 0 && collId != NULL && NULL != rodsObjStatOut ) { // JMC cppcheck - nullptr
195  *collId = strtoll( rodsObjStatOut->dataId, 0, 0 );
196  }
197  if ( rodsObjStatOut != NULL ) {
198  freeRodsObjStat( rodsObjStatOut );
199  }
200  return status;
201 }
202 
203 int
204 isColl( rsComm_t *rsComm, char *objName, rodsLong_t *collId ) {
205  genQueryInp_t genQueryInp;
206  genQueryOut_t *genQueryOut = NULL;
207  char tmpStr[MAX_NAME_LEN];
208  int status;
209 
210  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
211  snprintf( tmpStr, MAX_NAME_LEN, "='%s'", objName );
212  addInxVal( &genQueryInp.sqlCondInp, COL_COLL_NAME, tmpStr );
213  addInxIval( &genQueryInp.selectInp, COL_COLL_ID, 1 );
214  genQueryInp.maxRows = 2;
215  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
216  if ( status >= 0 ) {
217  sqlResult_t *collIdRes;
218 
219  if ( ( collIdRes = getSqlResultByInx( genQueryOut, COL_COLL_ID ) ) ==
220  NULL ) {
222  "isColl: getSqlResultByInx for COL_D_DATA_ID failed" );
223  return UNMATCHED_KEY_OR_INDEX;
224  }
225 
226  if ( collId != NULL ) {
227  *collId = strtoll( collIdRes->value, 0, 0 );
228  }
229  }
230 
231  freeGenQueryOut( &genQueryOut );
232  clearGenQueryInp( &genQueryInp );
233  return status;
234 }
235 
236 int
237 isUser( rsComm_t *rsComm, char *objName ) {
238  genQueryInp_t genQueryInp;
239  genQueryOut_t *genQueryOut = NULL;
240  char tmpStr[NAME_LEN];
241  int status;
242 
243  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
244  snprintf( tmpStr, NAME_LEN, "='%s'", objName );
245  addInxVal( &genQueryInp.sqlCondInp, COL_USER_NAME, tmpStr );
246  addInxIval( &genQueryInp.selectInp, COL_USER_ID, 1 );
247  genQueryInp.maxRows = 2;
248  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
249  freeGenQueryOut( &genQueryOut );
250  clearGenQueryInp( &genQueryInp );
251  return status;
252 }
253 
254 int
255 isResc( rsComm_t *rsComm, char *objName ) {
256  genQueryInp_t genQueryInp;
257  genQueryOut_t *genQueryOut = NULL;
258  char tmpStr[NAME_LEN];
259  int status;
260 
261  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
262  snprintf( tmpStr, NAME_LEN, "='%s'", objName );
263  addInxVal( &genQueryInp.sqlCondInp, COL_R_RESC_NAME, tmpStr );
264  addInxIval( &genQueryInp.selectInp, COL_R_RESC_ID, 1 );
265  genQueryInp.maxRows = 2;
266  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
267  freeGenQueryOut( &genQueryOut );
268  clearGenQueryInp( &genQueryInp );
269  return status;
270 }
271 
272 int
273 isMeta( rsComm_t*, char* ) {
274  /* needs to be filled in later */
275  return INVALID_OBJECT_TYPE;
276 }
277 
278 int
279 isToken( rsComm_t*, char* ) {
280  /* needs to be filled in later */
281  return INVALID_OBJECT_TYPE;
282 }
283 
284 int
285 getObjType( rsComm_t *rsComm, char *objName, char * objType ) {
286  if ( isData( rsComm, objName, NULL ) >= 0 ) {
287  strcpy( objType, "-d" );
288  }
289  else if ( isColl( rsComm, objName, NULL ) >= 0 ) {
290  strcpy( objType, "-c" );
291  }
292  else if ( isResc( rsComm, objName ) == 0 ) {
293  strcpy( objType, "-r" );
294  }
295  else if ( isUser( rsComm, objName ) == 0 ) {
296  strcpy( objType, "-u" );
297  }
298  else if ( isMeta( rsComm, objName ) == 0 ) {
299  strcpy( objType, "-m" );
300  }
301  else if ( isToken( rsComm, objName ) == 0 ) {
302  strcpy( objType, "-t" );
303  }
304  else {
305  return INVALID_OBJECT_TYPE;
306  }
307  return 0;
308 }
309 
310 int
311 addAVUMetadataFromKVPairs( rsComm_t *rsComm, char *objName, char *inObjType,
312  keyValPair_t *kVP ) {
313  char objType[10];
314  if ( strcmp( inObjType, "-1" ) ) {
315  if ( strlen( inObjType ) >= sizeof( objType ) ) {
316  rodsLog( LOG_ERROR, "inObjType: [%s] must be fewer than %ju characters", inObjType, ( uintmax_t )sizeof( objType ) );
318  }
319  strcpy( objType, inObjType );
320  }
321  else {
322  int status = getObjType( rsComm, objName, objType );
323  if ( status < 0 ) {
324  return status;
325  }
326  }
327 
328  for ( int i = 0; i < kVP->len ; i++ ) {
329  /* Call rsModAVUMetadata to call chlAddAVUMetadata.
330  rsModAVUMetadata connects to the icat-enabled server if the
331  local host isn't.
332  */
333  modAVUMetadataInp_t modAVUMetadataInp;
334  memset( &modAVUMetadataInp, 0, sizeof( modAVUMetadataInp ) );
335  modAVUMetadataInp.arg0 = "add";
336  modAVUMetadataInp.arg1 = objType;
337  modAVUMetadataInp.arg2 = objName;
338  modAVUMetadataInp.arg3 = kVP->keyWord[i];
339  modAVUMetadataInp.arg4 = kVP->value[i];
340  modAVUMetadataInp.arg5 = "";
341  int status = rsModAVUMetadata( rsComm, &modAVUMetadataInp );
342  if ( status < 0 ) {
343  return status;
344  }
345  }
346  return 0;
347 }
348 // =-=-=-=-=-=-=-
349 // JMC - backport 4836
350 int
351 setAVUMetadataFromKVPairs( rsComm_t *rsComm, char *objName, char *inObjType,
352  keyValPair_t *kVP ) {
353 
354  char objType[10];
355  if ( strcmp( inObjType, "-1" ) ) {
356  snprintf( objType, sizeof( objType ), "%s", inObjType );
357  }
358  else {
359  int status = getObjType( rsComm, objName, objType );
360  if ( status < 0 ) {
361  return status;
362  }
363  }
364  for ( int i = 0; i < kVP->len ; i++ ) {
365  /* Call rsModAVUMetadata to call chlSetAVUMetadata.
366  rsModAVUMetadata connects to the icat-enabled server if the
367  local host isn't.
368  */
369  modAVUMetadataInp_t modAVUMetadataInp;
370  memset( &modAVUMetadataInp, 0, sizeof( modAVUMetadataInp ) );
371  modAVUMetadataInp.arg0 = "set";
372  modAVUMetadataInp.arg1 = objType;
373  modAVUMetadataInp.arg2 = objName;
374  modAVUMetadataInp.arg3 = kVP->keyWord[i];
375  modAVUMetadataInp.arg4 = kVP->value[i];
376  modAVUMetadataInp.arg5 = NULL;
377  int status = rsModAVUMetadata( rsComm, &modAVUMetadataInp );
378  if ( status < 0 ) {
379  return status;
380  }
381  }
382  return 0;
383 }
384 // =-=-=-=-=-=-=-
385 int
387  if ( specColl == NULL ) {
388  return -1;
389  }
390 
391  if ( specColl->collClass == STRUCT_FILE_COLL ) {
392  return ( int ) specColl->type;
393  }
394  else {
395  return -1;
396  }
397 }
398 
399 int
400 removeAVUMetadataFromKVPairs( rsComm_t *rsComm, char *objName, char *inObjType,
401  keyValPair_t *kVP ) {
402  char objType[10];
403  if ( strcmp( inObjType, "-1" ) ) {
404  snprintf( objType, sizeof( objType ), "%s", inObjType );
405  }
406  else {
407  int status = getObjType( rsComm, objName, objType );
408  if ( status < 0 ) {
409  return status;
410  }
411  }
412 
413  for ( int i = 0; i < kVP->len ; i++ ) {
414  /* Call rsModAVUMetadata to call chlAddAVUMetadata.
415  rsModAVUMetadata connects to the icat-enabled server if the
416  local host isn't.
417  */
418  modAVUMetadataInp_t modAVUMetadataInp;
419  memset( &modAVUMetadataInp, 0, sizeof( modAVUMetadataInp ) );
420  modAVUMetadataInp.arg0 = "rm";
421  modAVUMetadataInp.arg1 = objType;
422  modAVUMetadataInp.arg2 = objName;
423  modAVUMetadataInp.arg3 = kVP->keyWord[i];
424  modAVUMetadataInp.arg4 = kVP->value[i];
425  modAVUMetadataInp.arg5 = NULL;
426  int status = rsModAVUMetadata( rsComm, &modAVUMetadataInp );
427  if ( status < 0 ) {
428  return status;
429  }
430  }
431  return 0;
432 }
433 
435 getTokenId( rsComm_t *rsComm, char *tokenNameSpace, char *tokenName ) {
436 
437  genQueryInp_t genQueryInp;
438  genQueryOut_t *genQueryOut = NULL;
439  char tmpStr[MAX_NAME_LEN];
440  char tmpStr2[MAX_NAME_LEN];
442 
443  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
444  snprintf( tmpStr, NAME_LEN, "='%s'", tokenNameSpace );
445  snprintf( tmpStr2, NAME_LEN, "='%s'", tokenName );
446  addInxVal( &genQueryInp.sqlCondInp, COL_TOKEN_NAMESPACE, tmpStr );
447  addInxVal( &genQueryInp.sqlCondInp, COL_TOKEN_NAME, tmpStr2 );
448  addInxIval( &genQueryInp.selectInp, COL_TOKEN_ID, 1 );
449  genQueryInp.maxRows = 2;
450  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
451  clearGenQueryInp( &genQueryInp );
452  if ( status >= 0 ) {
453  sqlResult_t *tokenIdRes;
454 
455  if ( ( tokenIdRes = getSqlResultByInx( genQueryOut, COL_TOKEN_ID ) ) ==
456  NULL ) {
458  "getTokenId: getSqlResultByInx for COL_TOKEN_ID failed" );
459  freeGenQueryOut( &genQueryOut );
460  return UNMATCHED_KEY_OR_INDEX;
461  }
462  status = atoll( tokenIdRes->value );
463  }
464  freeGenQueryOut( &genQueryOut );
465  return status;
466 }
467 
469 getUserId( rsComm_t *rsComm, char *userName, char *zoneName ) {
470 
471  genQueryInp_t genQueryInp;
472  genQueryOut_t *genQueryOut = NULL;
473  char tmpStr[MAX_NAME_LEN];
474  char tmpStr2[MAX_NAME_LEN];
476 
477  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
478  snprintf( tmpStr, NAME_LEN, "='%s'", userName );
479  snprintf( tmpStr2, NAME_LEN, "='%s'", zoneName );
480  addInxVal( &genQueryInp.sqlCondInp, COL_USER_NAME, tmpStr );
481  addInxVal( &genQueryInp.sqlCondInp, COL_USER_ZONE, tmpStr2 );
482  addInxIval( &genQueryInp.selectInp, COL_USER_ID, 1 );
483  genQueryInp.maxRows = 2;
484  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
485  clearGenQueryInp( &genQueryInp );
486  if ( status >= 0 ) {
487  sqlResult_t *userIdRes;
488 
489  if ( ( userIdRes = getSqlResultByInx( genQueryOut, COL_USER_ID ) ) ==
490  NULL ) {
492  "getUserId: getSqlResultByInx for COL_USER_ID failed" );
493  freeGenQueryOut( &genQueryOut );
494  return UNMATCHED_KEY_OR_INDEX;
495  }
496  status = atoll( userIdRes->value );
497  }
498  freeGenQueryOut( &genQueryOut );
499  return status;
500 }
501 
502 
503 int
504 checkPermitForDataObject( rsComm_t *rsComm, char *objName, int userId, int operId ) {
505  genQueryInp_t genQueryInp;
506  genQueryOut_t *genQueryOut = NULL;
507  char t1[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
508  char t11[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
509  char t2[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
510  char t3[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
511  char logicalEndName[MAX_NAME_LEN];
512  char logicalParentDirName[MAX_NAME_LEN];
513  int status;
514 
515  status = splitPathByKey( objName,
516  logicalParentDirName, MAX_NAME_LEN, logicalEndName, MAX_NAME_LEN, '/' );
517  snprintf( t1, MAX_NAME_LEN, " = '%s'", logicalEndName );
518  snprintf( t11, MAX_NAME_LEN, " = '%s'", logicalParentDirName );
519  snprintf( t2, MAX_NAME_LEN, " = '%i'", userId );
520  snprintf( t3, MAX_NAME_LEN, " >= '%i' ", operId );
521 
522  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
523  addInxIval( &genQueryInp.selectInp, COL_D_DATA_ID, 1 );
524  addInxVal( &genQueryInp.sqlCondInp, COL_DATA_NAME, t1 );
525  addInxVal( &genQueryInp.sqlCondInp, COL_COLL_NAME, t11 );
526  addInxVal( &genQueryInp.sqlCondInp, COL_DATA_ACCESS_USER_ID, t2 );
527  addInxVal( &genQueryInp.sqlCondInp, COL_DATA_ACCESS_TYPE, t3 );
528  genQueryInp.maxRows = 2;
529  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
530  freeGenQueryOut( &genQueryOut );
531  clearGenQueryInp( &genQueryInp );
532  if ( status >= 0 ) {
533  return 1;
534  }
535  else {
536  return 0;
537  }
538 }
539 
540 int
541 checkPermitForCollection( rsComm_t *rsComm, char *objName, int userId, int operId ) {
542  genQueryInp_t genQueryInp;
543  genQueryOut_t *genQueryOut = NULL;
544  char t1[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
545  char t2[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
546  char t4[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
547  int status;
548 
549  snprintf( t1, MAX_NAME_LEN, " = '%s'", objName );
550  snprintf( t2, MAX_NAME_LEN, " = '%i'", userId );
551  snprintf( t4, MAX_NAME_LEN, " >= '%i' ", operId );
552 
553  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
554  addInxIval( &genQueryInp.selectInp, COL_COLL_ID, 1 );
555  addInxVal( &genQueryInp.sqlCondInp, COL_COLL_NAME, t1 );
556  addInxVal( &genQueryInp.sqlCondInp, COL_COLL_ACCESS_USER_ID, t2 );
557  addInxVal( &genQueryInp.sqlCondInp, COL_COLL_ACCESS_TYPE, t4 );
558  genQueryInp.maxRows = 2;
559  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
560  freeGenQueryOut( &genQueryOut );
561  clearGenQueryInp( &genQueryInp );
562  if ( status >= 0 ) {
563  return 1;
564  }
565  else {
566  return 0;
567  }
568 }
569 
570 int
571 checkPermitForResource( rsComm_t *rsComm, char *objName, int userId, int operId ) {
572  genQueryInp_t genQueryInp;
573  genQueryOut_t *genQueryOut = NULL;
574  char t1[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
575  char t2[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
576  char t4[MAX_NAME_LEN]; // JMC cppcheck - snprintf out of bounds
577  int status;
578 
579  snprintf( t1, MAX_NAME_LEN, " = '%s'", objName );
580  snprintf( t2, MAX_NAME_LEN, " = '%i'", userId );
581  snprintf( t4, MAX_NAME_LEN, " >= '%i' ", operId );
582 
583  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
584  addInxIval( &genQueryInp.selectInp, COL_R_RESC_ID, 1 );
585  addInxVal( &genQueryInp.sqlCondInp, COL_R_RESC_NAME, t1 );
586  addInxVal( &genQueryInp.sqlCondInp, COL_RESC_ACCESS_USER_ID, t2 );
587  addInxVal( &genQueryInp.sqlCondInp, COL_RESC_ACCESS_TYPE, t4 );
588  genQueryInp.maxRows = 2;
589  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
590  freeGenQueryOut( &genQueryOut );
591  clearGenQueryInp( &genQueryInp );
592  if ( status >= 0 ) {
593  return 1;
594  }
595  else {
596  return 0;
597  }
598 }
599 
600 
601 int
602 checkPermissionByObjType( rsComm_t *rsComm, char *objName, char *objType, char *user, char *zone, char *oper ) {
603  int i;
604  rodsLong_t operId;
605  rodsLong_t userId;
606  operId = getTokenId( rsComm, "access_type", oper );
607  if ( operId < 0 ) {
608  // jjames - if they provide an invalid oper, give a better return code
609  if ( operId == CAT_NO_ROWS_FOUND ) {
611  }
612  return operId;
613  }
614 
615  userId = getUserId( rsComm, user, zone );
616  if ( userId < 0 ) {
617  return userId;
618  }
619 
620  if ( !strcmp( objType, "-d" ) ) {
621  i = checkPermitForDataObject( rsComm, objName, userId, operId );
622  }
623  else if ( !strcmp( objType, "-c" ) ) {
624  i = checkPermitForCollection( rsComm, objName, userId, operId );
625  }
626  else if ( !strcmp( objType, "-r" ) ) {
627  i = checkPermitForResource( rsComm, objName, userId, operId );
628  }
629  /*
630  else if (!strcmp(objType,"-m"))
631  i = checkPermitForMetadata(rsComm, objName, userId, operId);
632  */
633  else {
634  return INVALID_OBJECT_TYPE;
635  }
636 
637  if ( i == 0 ) { // not found - check groups
638 
639  // Issue 3309 - iterate through user's groups and check for permission
640  genQueryInp_t genQueryInp;
641  genQueryOut_t *genQueryOut = NULL;
642  memset( &genQueryInp, 0, sizeof( genQueryInp_t ) );
643  genQueryInp.maxRows = MAX_SQL_ROWS;
644 
645  addInxIval( &genQueryInp.selectInp, COL_USER_GROUP_NAME, 1 );
646 
647  char condstr[MAX_NAME_LEN];
648  snprintf( condstr, MAX_NAME_LEN, "= '%s'", user);
649  addInxVal( &genQueryInp.sqlCondInp, COL_USER_NAME, condstr);
650 
651  sqlResult_t *group_sql_result;
652 
653  int status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
654 
655  // note: if rsGenQuery has an error, just continue to below
656  if ( genQueryOut && status >= 0 ) {
657 
658  group_sql_result = getSqlResultByInx(genQueryOut, COL_USER_GROUP_NAME);
659 
660  if (group_sql_result != nullptr) {
661 
662  char *group_str;
663 
664  // iterating over groups
665  for ( int j = 0; j < genQueryOut->rowCnt; j++ ) {
666 
667  group_str = &group_sql_result->value[group_sql_result->len * j];
668 
669  // if group_str is the same as user then we have already checked this
670  // and don't want to go into an infinite loop
671  if (strcmp(group_str, user) == 0) {
672  continue;
673  }
674 
675  int groupId = getUserId( rsComm, group_str, zone );
676  if ( groupId < 0 ) {
677  continue;
678  }
679 
680  if ( !strcmp( objType, "-d" ) ) {
681  i = checkPermitForDataObject( rsComm, objName, groupId, operId );
682  }
683  else if ( !strcmp( objType, "-c" ) ) {
684  i = checkPermitForCollection( rsComm, objName, groupId, operId );
685  }
686  else if ( !strcmp( objType, "-r" ) ) {
687  i = checkPermitForResource( rsComm, objName, groupId, operId );
688  }
689 
690  if (i == 1) {
691  // break out of loop if this group has permission
692  break;
693  }
694  }
695  }
696 
697  freeGenQueryOut( &genQueryOut );
698  clearGenQueryInp( &genQueryInp );
699 
700  }
701 
702  }
703 
704  return i;
705 }
706 
707 /* checkDupReplica - Check if a given object with a given rescName
708  * and physical path already exist. If it does, returns the replNum.
709  * JMC - backport 4497 */
710 int
711 checkDupReplica( rsComm_t *rsComm, rodsLong_t dataId, char *rescName,
712  char *filePath ) {
713  genQueryInp_t genQueryInp;
714  genQueryOut_t *genQueryOut = NULL;
715  char tmpStr[MAX_NAME_LEN];
716  int status;
717 
718  if ( rsComm == NULL || rescName == NULL || filePath == NULL ) {
719  return USER__NULL_INPUT_ERR;
720  }
721 
722  bzero( &genQueryInp, sizeof( genQueryInp_t ) );
723 
724  rodsLong_t resc_id;
725  irods::error ret = resc_mgr.hier_to_leaf_id(rescName,resc_id);
726  if(!ret.ok()) {
727  irods::log(PASS(ret));
728  return ret.code();
729  }
730 
731  std::string resc_id_str = boost::lexical_cast<std::string>(resc_id);
732 
733  snprintf( tmpStr, MAX_NAME_LEN, "='%s'", resc_id_str.c_str() );
734  addInxVal( &genQueryInp.sqlCondInp, COL_D_RESC_ID, tmpStr );
735  snprintf( tmpStr, MAX_NAME_LEN, "='%s'", filePath );
736  addInxVal( &genQueryInp.sqlCondInp, COL_D_DATA_PATH, tmpStr );
737  snprintf( tmpStr, MAX_NAME_LEN, "='%lld'", dataId );
738  addInxVal( &genQueryInp.sqlCondInp, COL_D_DATA_ID, tmpStr );
739 
740  addInxIval( &genQueryInp.selectInp, COL_DATA_REPL_NUM, 1 );
741  genQueryInp.maxRows = 2;
742  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
743  clearGenQueryInp( &genQueryInp );
744  if ( status >= 0 ) {
745  int intReplNum;
746  sqlResult_t *replNum;
747  if ( ( replNum = getSqlResultByInx( genQueryOut, COL_DATA_REPL_NUM ) ) ==
748  NULL ) {
749  freeGenQueryOut( &genQueryOut );
751  "checkDupReplica: getSqlResultByInx COL_DATA_REPL_NUM failed" );
752  return UNMATCHED_KEY_OR_INDEX;
753  }
754  intReplNum = atoi( replNum->value );
755  freeGenQueryOut( &genQueryOut );
756  return intReplNum;
757  }
758  else {
759  freeGenQueryOut( &genQueryOut );
760  return status;
761  }
762 }
763 // =-=-=-=-=-=-=-
764 // JMC - backport 4552
765 int
766 getNumSubfilesInBunfileObj( rsComm_t *rsComm, char *objPath ) {
767  int status;
768  genQueryOut_t *genQueryOut = NULL;
769  genQueryInp_t genQueryInp;
770  int totalRowCount;
771  char condStr[MAX_NAME_LEN];
772 
773  bzero( &genQueryInp, sizeof( genQueryInp ) );
774  genQueryInp.maxRows = 1;
775  genQueryInp.options = RETURN_TOTAL_ROW_COUNT;
776 
777  snprintf( condStr, MAX_NAME_LEN, "='%s'", objPath );
778  addInxVal( &genQueryInp.sqlCondInp, COL_D_DATA_PATH, condStr );
779  snprintf( condStr, MAX_NAME_LEN, "='%s'", BUNDLE_RESC_CLASS );
780  addInxVal( &genQueryInp.sqlCondInp, COL_R_CLASS_NAME, condStr );
781  addKeyVal( &genQueryInp.condInput, ZONE_KW, objPath );
782 
783  addInxIval( &genQueryInp.selectInp, COL_COLL_NAME, 1 );
784  addInxIval( &genQueryInp.selectInp, COL_DATA_NAME, 1 );
785  addInxIval( &genQueryInp.selectInp, COL_DATA_SIZE, 1 );
786 
787  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
788  if ( genQueryOut == NULL || status < 0 ) {
789  freeGenQueryOut( &genQueryOut );
790  clearGenQueryInp( &genQueryInp );
791  if ( status == CAT_NO_ROWS_FOUND ) {
792  return 0;
793  }
794  else {
795  return status;
796  }
797  }
798  totalRowCount = genQueryOut->totalRowCount;
799  freeGenQueryOut( &genQueryOut );
800  /* clear result */
801  genQueryInp.maxRows = 0;
802  status = rsGenQuery( rsComm, &genQueryInp, &genQueryOut );
803  clearGenQueryInp( &genQueryInp );
804 
805  return totalRowCount;
806 }
807 // =-=-=-=-=-=-=-
COL_USER_ZONE
#define COL_USER_ZONE
Definition: rodsGenQuery.h:134
rodsLog
void rodsLog(int level, const char *formatStr,...)
Definition: rodsLog.cpp:86
DataObjInp::objPath
char objPath[(1024+64)]
Definition: dataObjInpOut.h:66
MAX_SQL_ROWS
#define MAX_SQL_ROWS
Definition: rodsGenQuery.h:16
NULL
#define NULL
Definition: rodsDef.h:70
isToken
int isToken(rsComm_t *, char *)
Definition: objMetaOpr.cpp:279
COL_R_RESC_NAME
#define COL_R_RESC_NAME
Definition: rodsGenQuery.h:144
rsComm_t
Definition: rcConnect.h:145
COL_DATA_REPL_NUM
#define COL_DATA_REPL_NUM
Definition: rodsGenQuery.h:166
checkPermissionByObjType
int checkPermissionByObjType(rsComm_t *rsComm, char *objName, char *objType, char *user, char *zone, char *oper)
Definition: objMetaOpr.cpp:602
addKeyVal
int addKeyVal(keyValPair_t *condInput, const char *keyWord, const char *value)
Definition: rcMisc.cpp:789
GenQueryOut::totalRowCount
int totalRowCount
Definition: rodsGenQuery.h:71
GenQueryInp::continueInx
int continueInx
Definition: rodsGenQuery.h:28
modAVUMetadataInp_t::arg0
char * arg0
Definition: modAVUMetadata.h:7
COL_TOKEN_NAMESPACE
#define COL_TOKEN_NAMESPACE
Definition: rodsGenQuery.h:335
specColl.hpp
COL_COLL_ACCESS_USER_ID
#define COL_COLL_ACCESS_USER_ID
Definition: rodsGenQuery.h:281
COL_D_RESC_HIER
#define COL_D_RESC_HIER
Definition: rodsGenQuery.h:184
UNMATCHED_KEY_OR_INDEX
@ UNMATCHED_KEY_OR_INDEX
Definition: rodsErrorTable.h:244
rodsObjStat::dataId
char dataId[64]
Definition: objStat.h:12
PASS
#define PASS(prev_error_)
Definition: irods_error.hpp:118
resc_mgr
irods::resource_manager resc_mgr
Definition: irods_resource_manager.cpp:31
irods::resolve_resource_hierarchy
error resolve_resource_hierarchy(const std::string &, rsComm_t *, dataObjInp_t *, std::string &, dataObjInfo_t **_data_obj_info=0)
Definition: irods_resource_redirect.cpp:505
GenQueryInp
Definition: rodsGenQuery.h:24
modAVUMetadataInp_t::arg2
char * arg2
Definition: modAVUMetadata.h:9
irods::hierarchy_parser
Definition: irods_hierarchy_parser.hpp:14
SpecColl::type
structFileType_t type
Definition: objInfo.h:78
SpecColl
Definition: objInfo.h:76
COL_COLL_ID
#define COL_COLL_ID
Definition: rodsGenQuery.h:188
STRUCT_FILE_COLL
@ STRUCT_FILE_COLL
Definition: objInfo.h:47
deploy_schemas_locally.parser
parser
Definition: deploy_schemas_locally.py:59
modAVUMetadataInp_t::arg4
char * arg4
Definition: modAVUMetadata.h:11
addInxVal
int addInxVal(inxValPair_t *inxValPair, int inx, const char *value)
Definition: rcMisc.cpp:921
LOG_ERROR
#define LOG_ERROR
Definition: rodsLog.h:43
irods_resource_redirect.hpp
getStructFileType
int getStructFileType(specColl_t *specColl)
Definition: objMetaOpr.cpp:386
SYS_INVALID_INPUT_PARAM
@ SYS_INVALID_INPUT_PARAM
Definition: rodsErrorTable.h:195
GenQueryInp::selectInp
inxIvalPair_t selectInp
Definition: rodsGenQuery.h:53
GenQueryInp::maxRows
int maxRows
Definition: rodsGenQuery.h:25
rsModAVUMetadata.hpp
freeRodsObjStat
int freeRodsObjStat(rodsObjStat_t *rodsObjStat)
Definition: rcMisc.cpp:3537
KeyValPair::value
char ** value
Definition: objInfo.h:123
COL_DATA_NAME
#define COL_DATA_NAME
Definition: rodsGenQuery.h:165
irods::OPEN_OPERATION
const std::string OPEN_OPERATION("OPEN")
irods::error::code
long long code() const
Definition: irods_error.cpp:194
svrCloseQueryOut
int svrCloseQueryOut(rsComm_t *rsComm, genQueryOut_t *genQueryOut)
Definition: objMetaOpr.cpp:26
COL_R_RESC_ID
#define COL_R_RESC_ID
Definition: rodsGenQuery.h:143
COL_TOKEN_NAME
#define COL_TOKEN_NAME
Definition: rodsGenQuery.h:337
getUserId
rodsLong_t getUserId(rsComm_t *rsComm, char *userName, char *zoneName)
Definition: objMetaOpr.cpp:469
getSqlResultByInx
sqlResult_t * getSqlResultByInx(genQueryOut_t *genQueryOut, int attriInx)
Definition: rcMisc.cpp:1387
GenQueryOut::continueInx
int continueInx
Definition: rodsGenQuery.h:70
getTokenId
rodsLong_t getTokenId(rsComm_t *rsComm, char *tokenNameSpace, char *tokenName)
Definition: objMetaOpr.cpp:435
GenQueryInp::condInput
keyValPair_t condInput
Definition: rodsGenQuery.h:52
COL_USER_NAME
#define COL_USER_NAME
Definition: rodsGenQuery.h:132
KeyValPair::keyWord
char ** keyWord
Definition: objInfo.h:122
checkPermitForDataObject
int checkPermitForDataObject(rsComm_t *rsComm, char *objName, int userId, int operId)
Definition: objMetaOpr.cpp:504
GenQueryInp::sqlCondInp
inxValPair_t sqlCondInp
Definition: rodsGenQuery.h:56
DataObjInp
Definition: dataObjInpOut.h:65
genQuery.h
MAX_NAME_LEN
#define MAX_NAME_LEN
Definition: rodsDef.h:61
isUser
int isUser(rsComm_t *rsComm, char *objName)
Definition: objMetaOpr.cpp:237
addInxIval
int addInxIval(inxIvalPair_t *inxIvalPair, int inx, int value)
Definition: rcMisc.cpp:883
COL_TOKEN_ID
#define COL_TOKEN_ID
Definition: rodsGenQuery.h:336
isResc
int isResc(rsComm_t *rsComm, char *objName)
Definition: objMetaOpr.cpp:255
GenQueryOut
Definition: rodsGenQuery.h:67
modAVUMetadataInp_t::arg3
char * arg3
Definition: modAVUMetadata.h:10
irods.pypyodbc.status
status
Definition: pypyodbc.py:467
collection.hpp
splitPathByKey
int splitPathByKey(const char *srcPath, char *dir, size_t maxDirLen, char *file, size_t maxFileLen, char key)
Definition: stringOpr.cpp:222
GenQueryOut::rowCnt
int rowCnt
Definition: rodsGenQuery.h:68
irods::log
void log(const error &)
Definition: irods_log.cpp:13
rsModAVUMetadata
int rsModAVUMetadata(rsComm_t *rsComm, modAVUMetadataInp_t *modAVUMetadataInp)
Definition: rsModAVUMetadata.cpp:13
rsIcatOpr.hpp
INVALID_OBJECT_TYPE
@ INVALID_OBJECT_TYPE
Definition: rodsErrorTable.h:679
modAVUMetadataInp_t
Definition: modAVUMetadata.h:6
COL_DATA_ACCESS_USER_ID
#define COL_DATA_ACCESS_USER_ID
Definition: rodsGenQuery.h:275
isMeta
int isMeta(rsComm_t *, char *)
Definition: objMetaOpr.cpp:273
irods::error
Definition: irods_error.hpp:23
getObjType
int getObjType(rsComm_t *rsComm, char *objName, char *objType)
Definition: objMetaOpr.cpp:285
getNumSubfilesInBunfileObj
int getNumSubfilesInBunfileObj(rsComm_t *rsComm, char *objPath)
Definition: objMetaOpr.cpp:766
isData
int isData(rsComm_t *rsComm, char *objName, rodsLong_t *dataId)
Definition: objMetaOpr.cpp:47
removeAVUMetadataFromKVPairs
int removeAVUMetadataFromKVPairs(rsComm_t *rsComm, char *objName, char *inObjType, keyValPair_t *kVP)
Definition: objMetaOpr.cpp:400
addAVUMetadataFromKVPairs
int addAVUMetadataFromKVPairs(rsComm_t *rsComm, char *objName, char *inObjType, keyValPair_t *kVP)
Definition: objMetaOpr.cpp:311
SqlResult::value
char * value
Definition: rodsGenQuery.h:64
collStatAllKinds
int collStatAllKinds(rsComm_t *rsComm, dataObjInp_t *dataObjInp, rodsObjStat_t **rodsObjStatOut)
Definition: collection.cpp:405
irods_hierarchy_parser.hpp
irods::resource_manager::hier_to_leaf_id
error hier_to_leaf_id(const std::string &, rodsLong_t &)
Definition: irods_resource_manager.cpp:1082
RETURN_TOTAL_ROW_COUNT
#define RETURN_TOTAL_ROW_COUNT
Definition: rodsGenQuery.h:86
COL_RESC_ACCESS_USER_ID
#define COL_RESC_ACCESS_USER_ID
Definition: rodsGenQuery.h:288
CAT_NO_ROWS_FOUND
@ CAT_NO_ROWS_FOUND
Definition: rodsErrorTable.h:423
SqlResult
Definition: rodsGenQuery.h:61
isCollAllKinds
int isCollAllKinds(rsComm_t *rsComm, char *objName, rodsLong_t *collId)
Definition: objMetaOpr.cpp:186
checkDupReplica
int checkDupReplica(rsComm_t *rsComm, rodsLong_t dataId, char *rescName, char *filePath)
Definition: objMetaOpr.cpp:711
COL_COLL_NAME
#define COL_COLL_NAME
Definition: rodsGenQuery.h:189
modAVUMetadata.h
KeyValPair::len
int len
Definition: objInfo.h:121
setAVUMetadataFromKVPairs
int setAVUMetadataFromKVPairs(rsComm_t *rsComm, char *objName, char *inObjType, keyValPair_t *kVP)
Definition: objMetaOpr.cpp:351
COL_DATA_ACCESS_TYPE
#define COL_DATA_ACCESS_TYPE
Definition: rodsGenQuery.h:272
resource.hpp
SqlResult::len
int len
Definition: rodsGenQuery.h:63
BUNDLE_RESC_CLASS
#define BUNDLE_RESC_CLASS
Definition: phyBundleColl.h:14
icatHighLevelRoutines.hpp
getPhyPath
int getPhyPath(rsComm_t *_comm, char *_obj_name, char *_resource, char *_phy_path, char *_resc_hier)
Definition: objMetaOpr.cpp:86
rodsObjStat
Definition: objStat.h:8
COL_USER_GROUP_NAME
#define COL_USER_GROUP_NAME
Definition: rodsGenQuery.h:318
ZONE_KW
#define ZONE_KW
Definition: rodsKeyWdDef.h:80
rsGenQuery.hpp
checkPermitForCollection
int checkPermitForCollection(rsComm_t *rsComm, char *objName, int userId, int operId)
Definition: objMetaOpr.cpp:541
checkPermitForResource
int checkPermitForResource(rsComm_t *rsComm, char *objName, int userId, int operId)
Definition: objMetaOpr.cpp:571
COL_R_CLASS_NAME
#define COL_R_CLASS_NAME
Definition: rodsGenQuery.h:147
miscUtil.h
rstrcpy
char * rstrcpy(char *dest, const char *src, int maxLen)
Definition: stringOpr.cpp:51
COL_D_DATA_PATH
#define COL_D_DATA_PATH
Definition: rodsGenQuery.h:172
modAVUMetadataInp_t::arg5
char * arg5
Definition: modAVUMetadata.h:12
isColl
int isColl(rsComm_t *rsComm, char *objName, rodsLong_t *collId)
Definition: objMetaOpr.cpp:204
freeGenQueryOut
int freeGenQueryOut(genQueryOut_t **genQueryOut)
Definition: rcMisc.cpp:1133
NAME_LEN
#define NAME_LEN
Definition: rodsDef.h:55
KeyValPair
Definition: objInfo.h:120
irods::error::ok
bool ok()
Definition: irods_error.cpp:258
COL_RESC_ACCESS_TYPE
#define COL_RESC_ACCESS_TYPE
Definition: rodsGenQuery.h:285
COL_DATA_SIZE
#define COL_DATA_SIZE
Definition: rodsGenQuery.h:169
COL_USER_ID
#define COL_USER_ID
Definition: rodsGenQuery.h:131
clearGenQueryInp
void clearGenQueryInp(void *voidInp)
Definition: rcMisc.cpp:1118
rsGenQuery
int rsGenQuery(rsComm_t *rsComm, genQueryInp_t *genQueryInp, genQueryOut_t **genQueryOut)
Definition: rsGenQuery.cpp:604
GenQueryInp::options
int options
Definition: rodsGenQuery.h:34
rodsLong_t
long long rodsLong_t
Definition: rodsType.h:32
objMetaOpr.hpp
SpecColl::collClass
specCollClass_t collClass
Definition: objInfo.h:77
COL_COLL_ACCESS_TYPE
#define COL_COLL_ACCESS_TYPE
Definition: rodsGenQuery.h:278
COL_D_RESC_ID
#define COL_D_RESC_ID
Definition: rodsGenQuery.h:185
COL_D_DATA_ID
#define COL_D_DATA_ID
Definition: rodsGenQuery.h:163
USER__NULL_INPUT_ERR
@ USER__NULL_INPUT_ERR
Definition: rodsErrorTable.h:247
modAVUMetadataInp_t::arg1
char * arg1
Definition: modAVUMetadata.h:8