00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATSecurityContext.c,v 1.9 2004/03/24 19:30:58 hartmutkaiser Exp $";
00027
00028
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033
00034
00035
00036 #include "GATType.h"
00037 #include "GATErrors.h"
00038 #include "GATLocation.h"
00039 #include "GATSecurityContext.h"
00040
00041
00042
00043
00044
00045
00046 GATOBJECT_DEFINE_VTABLE(GATSecurityContext);
00047
00048
00049 GATOBJECT_DEFINE_CONVERTERS(GATSecurityContext);
00050 GATOBJECT_DEFINE_CONVERTERS_QUALIFIED(extern, GATList_GATSecurityContext, GATType_GATList);
00051
00052
00053 GATLIST_IMPLEMENT(extern, GATSecurityContext, GATList_GATSecurityContext, GATType_GATSecurityContext);
00054
00055 typedef struct
00056 {
00057 char *name;
00058 char *passphrase;
00059 GATLocation location;
00060 } GATRemoteSecurityContext;
00061
00062 typedef struct
00063 {
00064 char *username;
00065 char *password;
00066 } GATPasswordSecurityContext;
00067
00068 typedef struct
00069 {
00070 char *keyfile;
00071 char *passphrase;
00072 char *certificate;
00073 } GATCertificateSecurityContext;
00074
00075 typedef union
00076 {
00077 GATRemoteSecurityContext remoteSecurityContext;
00078 GATPasswordSecurityContext passwordSecurityContext;
00079 GATCertificateSecurityContext certificateSecurityContext;
00080 } GATSecurityContextUnion;
00081
00082 struct GATSecurityContext_S
00083 {
00084 GATSecurityContext_vtable *GATObject__vtable;
00085
00086 GATSecurityContextType securityContextType;
00087 GATSecurityContextUnion securityContextUnion;
00088 };
00089
00090
00091 static char *GATSecurityContext_StringClone(const char *oldString);
00092 static void GATSecurityContext_PartialDestroy(GATSecurityContext *securityContext);
00093
00094
00095 static GATSecurityContext_vtable GATSecurityContext__vtable = {
00096 GATSecurityContext_GetType,
00097 GATSecurityContext_Destroy,
00098 GATSecurityContext_Equals,
00099 GATSecurityContext_Clone,
00100 GATSecurityContext_GetInterface,
00101 NULL
00102 };
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 GATSecurityContext GATSecurityContext_Create(GATSecurityContextType type)
00115 {
00116 GATSecurityContext this;
00117
00118 this = (GATSecurityContext) malloc( sizeof(struct GATSecurityContext_S) );
00119 if(NULL != this)
00120 {
00121 memset(this, 0, sizeof(struct GATSecurityContext_S));
00122 this->GATObject__vtable = &GATSecurityContext__vtable;
00123
00124 this->securityContextType = type;
00125 }
00126
00127 return this;
00128 }
00129
00130
00131
00132
00133
00134
00135 void GATSecurityContext_Destroy(GATSecurityContext *this)
00136 {
00137 if( NULL != (*this) )
00138 {
00139 if(GATSecurityContextType_Password == (*this)->securityContextType)
00140 {
00141 free( ((*this)->securityContextUnion).passwordSecurityContext.username );
00142 free( ((*this)->securityContextUnion).passwordSecurityContext.password );
00143
00144 ((*this)->securityContextUnion).passwordSecurityContext.username = NULL;
00145 ((*this)->securityContextUnion).passwordSecurityContext.password = NULL;
00146 }
00147
00148 if(GATSecurityContextType_Certificate == (*this)->securityContextType)
00149 {
00150 free( ((*this)->securityContextUnion).certificateSecurityContext.keyfile );
00151 free( ((*this)->securityContextUnion).certificateSecurityContext.passphrase );
00152 free( ((*this)->securityContextUnion).certificateSecurityContext.certificate );
00153
00154 ((*this)->securityContextUnion).certificateSecurityContext.keyfile = NULL;
00155 ((*this)->securityContextUnion).certificateSecurityContext.passphrase = NULL;
00156 ((*this)->securityContextUnion).certificateSecurityContext.certificate = NULL;
00157 }
00158
00159 if(GATSecurityContextType_Remote == (*this)->securityContextType)
00160 {
00161 free( ((*this)->securityContextUnion).remoteSecurityContext.name );
00162 free( ((*this)->securityContextUnion).remoteSecurityContext.passphrase );
00163 GATLocation_Destroy( &(((*this)->securityContextUnion).remoteSecurityContext.location) );
00164
00165 ((*this)->securityContextUnion).remoteSecurityContext.name = NULL;
00166 ((*this)->securityContextUnion).remoteSecurityContext.passphrase = NULL;
00167 }
00168
00169 free(*this);
00170 *this = NULL;
00171 }
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181 GATType GATSecurityContext_GetType(GATSecurityContext_const this)
00182 {
00183 GAT_UNUSED_PARAMETER(this);
00184 return GATType_GATSecurityContext;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 GATResult GATSecurityContext_Clone(GATSecurityContext_const this, GATSecurityContext *thisClone)
00198 {
00199 GATResult retval;
00200
00201 retval = GAT_INVALID_HANDLE;
00202 if( NULL != this )
00203 {
00204 retval = GAT_INVALID_PARAMETER;
00205 if( NULL != thisClone)
00206 {
00207 if( GATSecurityContextType_Password == this->securityContextType )
00208 {
00209 (*thisClone)->GATObject__vtable = &GATSecurityContext__vtable;
00210
00211 (*thisClone)->securityContextType = GATSecurityContextType_Password;
00212 ((*thisClone)->securityContextUnion).passwordSecurityContext.username =
00213 GATSecurityContext_StringClone( (this->securityContextUnion).passwordSecurityContext.username );
00214 ((*thisClone)->securityContextUnion).passwordSecurityContext.password =
00215 GATSecurityContext_StringClone( (this->securityContextUnion).passwordSecurityContext.password );
00216
00217 if(
00218 (NULL == ((*thisClone)->securityContextUnion).passwordSecurityContext.username) ||
00219 (NULL == ((*thisClone)->securityContextUnion).passwordSecurityContext.password)
00220 )
00221 {
00222 retval = GAT_MEMORYFAILURE;
00223 GATSecurityContext_Destroy( thisClone );
00224 }
00225 }
00226 if( GATSecurityContextType_Certificate == this->securityContextType )
00227 {
00228 (*thisClone)->GATObject__vtable = &GATSecurityContext__vtable;
00229
00230 (*thisClone)->securityContextType = GATSecurityContextType_Certificate;
00231 ((*thisClone)->securityContextUnion).certificateSecurityContext.keyfile =
00232 GATSecurityContext_StringClone( (this->securityContextUnion).certificateSecurityContext.keyfile );
00233 ((*thisClone)->securityContextUnion).certificateSecurityContext.passphrase =
00234 GATSecurityContext_StringClone( (this->securityContextUnion).certificateSecurityContext.passphrase );
00235 ((*thisClone)->securityContextUnion).certificateSecurityContext.certificate =
00236 GATSecurityContext_StringClone( (this->securityContextUnion).certificateSecurityContext.certificate );
00237
00238 if(
00239 (NULL == ((*thisClone)->securityContextUnion).certificateSecurityContext.keyfile) ||
00240 (NULL == ((*thisClone)->securityContextUnion).certificateSecurityContext.passphrase) ||
00241 (NULL == ((*thisClone)->securityContextUnion).certificateSecurityContext.certificate)
00242 )
00243 {
00244 retval = GAT_MEMORYFAILURE;
00245 GATSecurityContext_Destroy( thisClone );
00246 }
00247 }
00248 if( GATSecurityContextType_Remote == this->securityContextType )
00249 {
00250 (*thisClone)->GATObject__vtable = &GATSecurityContext__vtable;
00251
00252 (*thisClone)->securityContextType = GATSecurityContextType_Remote;
00253 ((*thisClone)->securityContextUnion).remoteSecurityContext.name =
00254 GATSecurityContext_StringClone( (this->securityContextUnion).remoteSecurityContext.name );
00255 ((*thisClone)->securityContextUnion).remoteSecurityContext.passphrase =
00256 GATSecurityContext_StringClone( (this->securityContextUnion).remoteSecurityContext.passphrase );
00257 GATLocation_Clone( (this->securityContextUnion).remoteSecurityContext.location, &(((*thisClone)->securityContextUnion).remoteSecurityContext.location) );
00258
00259 if(
00260 (NULL == ((*thisClone)->securityContextUnion).remoteSecurityContext.name) ||
00261 (NULL == ((*thisClone)->securityContextUnion).remoteSecurityContext.passphrase) ||
00262 (NULL == ((*thisClone)->securityContextUnion).remoteSecurityContext.location)
00263 )
00264 {
00265 retval = GAT_MEMORYFAILURE;
00266 GATSecurityContext_Destroy( thisClone );
00267 }
00268 }
00269 }
00270 }
00271
00272 return retval;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 GATResult GATSecurityContext_Equals(GATSecurityContext_const this, GATSecurityContext_const that, GATBool *isequal)
00286 {
00287 GATResult retval;
00288
00289 retval = GAT_INVALID_HANDLE;
00290 if( NULL != this )
00291 {
00292 retval = GAT_INVALID_PARAMETER;
00293 if( (NULL != that) && (NULL != isequal) )
00294 {
00295 retval = GAT_SUCCESS;
00296 (*isequal) = GATFalse;
00297 if( this->securityContextType == that->securityContextType )
00298 {
00299 if( GATSecurityContextType_Password == this->securityContextType )
00300 {
00301 if(
00302 ( 0 == strcmp((this->securityContextUnion).passwordSecurityContext.username, (that->securityContextUnion).passwordSecurityContext.username) ) &&
00303 ( 0 == strcmp((this->securityContextUnion).passwordSecurityContext.password, (that->securityContextUnion).passwordSecurityContext.password) )
00304 )
00305 {
00306 (*isequal) = GATTrue;
00307 }
00308 }
00309
00310 if( GATSecurityContextType_Certificate == this->securityContextType )
00311 {
00312 if(
00313 ( 0 == strcmp((this->securityContextUnion).certificateSecurityContext.keyfile, (that->securityContextUnion).certificateSecurityContext.keyfile) ) &&
00314 ( 0 == strcmp((this->securityContextUnion).certificateSecurityContext.passphrase, (that->securityContextUnion).certificateSecurityContext.passphrase) ) &&
00315 ( 0 == strcmp((this->securityContextUnion).certificateSecurityContext.certificate, (that->securityContextUnion).certificateSecurityContext.certificate) )
00316 )
00317 {
00318 (*isequal) = GATTrue;
00319 }
00320 }
00321
00322 if( GATSecurityContextType_Remote == this->securityContextType )
00323 {
00324 if(
00325 ( 0 == strcmp((this->securityContextUnion).remoteSecurityContext.name, (that->securityContextUnion).remoteSecurityContext.name) ) &&
00326 ( 0 == strcmp((this->securityContextUnion).remoteSecurityContext.passphrase, (that->securityContextUnion).remoteSecurityContext.passphrase) )
00327 )
00328 {
00329 retval = GATLocation_Equals( (this->securityContextUnion).remoteSecurityContext.location, (that->securityContextUnion).remoteSecurityContext.location, isequal );
00330 }
00331 }
00332 }
00333 }
00334 }
00335
00336 return retval;
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 GATResult
00351 GATSecurityContext_GetInterface(GATSecurityContext_const object,
00352 GATInterface iftype, void const **ifp)
00353 {
00354 GATResult retval;
00355
00356 retval = GAT_INVALID_PARAMETER;
00357 if( (NULL != object) && (NULL != ifp) )
00358 {
00359 (*ifp) = NULL;
00360 retval = GAT_NO_INTERFACE;
00361 GAT_UNUSED_PARAMETER(iftype);
00362 }
00363
00364 return retval;
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 GATResult GATSecurityContext_SetPasswordAuthenticate(GATSecurityContext this, const char *username, const char *password)
00377 {
00378 GATResult retval;
00379
00380 retval = GAT_INVALID_HANDLE;
00381 if( NULL != this )
00382 {
00383 retval = GAT_INVALID_PARAMETER;
00384 if( (NULL != username) && (NULL != password) )
00385 {
00386 char *tempUsername;
00387 char *tempPassword;
00388
00389 retval = GAT_MEMORYFAILURE;
00390 tempUsername = GATSecurityContext_StringClone(username);
00391 tempPassword = GATSecurityContext_StringClone(password);
00392 if( (NULL != tempUsername) && (NULL != tempPassword) )
00393 {
00394 retval = GAT_SUCCESS;
00395 GATSecurityContext_PartialDestroy( &this );
00396 this->securityContextType = GATSecurityContextType_Password;
00397 (this->securityContextUnion).passwordSecurityContext.username = tempUsername;
00398 (this->securityContextUnion).passwordSecurityContext.password = tempPassword;
00399 }
00400 else
00401 {
00402 free(tempUsername);
00403 free(tempPassword);
00404 }
00405 }
00406 }
00407
00408 return retval;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 GATResult GATSecurityContext_GetPasswordAuthenticate(GATSecurityContext this, char **username, char **password)
00421 {
00422 GATResult retval;
00423
00424 retval = GAT_INVALID_HANDLE;
00425 if( NULL != this )
00426 {
00427 retval = GAT_INVALID_PARAMETER;
00428 if( (NULL != username) && (NULL != password) )
00429 {
00430 retval = GAT_INVALID_STATE;
00431 if( GATSecurityContextType_Password == this->securityContextType )
00432 {
00433 retval = GAT_MEMORYFAILURE;
00434 (*username) = GATSecurityContext_StringClone( (this->securityContextUnion).passwordSecurityContext.username );
00435 (*password) = GATSecurityContext_StringClone( (this->securityContextUnion).passwordSecurityContext.password );
00436 if( (NULL != *username) && (NULL != *password) )
00437 {
00438 retval = GAT_SUCCESS;
00439 }
00440 else
00441 {
00442 free(*username);
00443 free(*password);
00444 }
00445 }
00446 }
00447 }
00448
00449 return retval;
00450 }
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 GATResult GATSecurityContext_SetCertificateAuthenticate(GATSecurityContext this, const char *keyfile, const char *certificate, const char *passphrase)
00468 {
00469 GATResult retval;
00470
00471 retval = GAT_INVALID_HANDLE;
00472 if( NULL != this )
00473 {
00474 retval = GAT_INVALID_PARAMETER;
00475 if( (NULL != keyfile) && (NULL != certificate) && (NULL != passphrase) )
00476 {
00477 char *tempKeyfile;
00478 char *tempPassphrase;
00479 char *tempCertificate;
00480
00481 retval = GAT_MEMORYFAILURE;
00482 tempKeyfile = GATSecurityContext_StringClone(keyfile);
00483 tempPassphrase = GATSecurityContext_StringClone(passphrase);
00484 tempCertificate = GATSecurityContext_StringClone(certificate);
00485 if( (NULL != tempKeyfile) && (NULL != tempPassphrase) && (NULL != tempCertificate) )
00486 {
00487 retval = GAT_SUCCESS;
00488 GATSecurityContext_PartialDestroy( &this );
00489 this->securityContextType = GATSecurityContextType_Certificate;
00490 (this->securityContextUnion).certificateSecurityContext.keyfile = tempKeyfile;
00491 (this->securityContextUnion).certificateSecurityContext.passphrase = tempPassphrase;
00492 (this->securityContextUnion).certificateSecurityContext.certificate = tempCertificate;
00493 }
00494 else
00495 {
00496 free(tempKeyfile);
00497 free(tempPassphrase);
00498 free(tempCertificate);
00499 }
00500 }
00501 }
00502
00503 return retval;
00504 }
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 GATResult GATSecurityContext_GetCertificateAuthenticate(GATSecurityContext this, char **keyfile, char **certificate, char **passphrase)
00518 {
00519 GATResult retval;
00520
00521 retval = GAT_INVALID_HANDLE;
00522 if( NULL != this )
00523 {
00524 retval = GAT_INVALID_PARAMETER;
00525 if( (NULL != keyfile) && (NULL != certificate) && (NULL != passphrase) )
00526 {
00527 retval = GAT_INVALID_STATE;
00528 if( GATSecurityContextType_Certificate == this->securityContextType )
00529 {
00530 retval = GAT_MEMORYFAILURE;
00531 (*keyfile) = GATSecurityContext_StringClone( (this->securityContextUnion).certificateSecurityContext.keyfile );
00532 (*passphrase) = GATSecurityContext_StringClone( (this->securityContextUnion).certificateSecurityContext.passphrase );
00533 (*certificate) = GATSecurityContext_StringClone( (this->securityContextUnion).certificateSecurityContext.certificate );
00534 if( (NULL != *keyfile) && (NULL != *passphrase) && (NULL != *certificate) )
00535 {
00536 retval = GAT_SUCCESS;
00537 }
00538 else
00539 {
00540 free(*keyfile);
00541 free(*passphrase);
00542 free(*certificate);
00543 }
00544 }
00545 }
00546 }
00547
00548 return retval;
00549 }
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 GATResult GATSecurityContext_SetRemoteAuthenticate(GATSecurityContext this, GATLocation location, const char *name, const char *passphrase)
00562 {
00563 GATResult retval;
00564
00565 retval = GAT_INVALID_HANDLE;
00566 if( NULL != this )
00567 {
00568 retval = GAT_INVALID_PARAMETER;
00569 if( (NULL != location) && (NULL != name) && (NULL != passphrase) )
00570 {
00571 char *tempName;
00572 char *tempPassphrase;
00573
00574 retval = GAT_MEMORYFAILURE;
00575 tempName = GATSecurityContext_StringClone(name);
00576 tempPassphrase = GATSecurityContext_StringClone(passphrase);
00577 if( (NULL != tempName) && (NULL != tempPassphrase) )
00578 {
00579 GATLocation tempLocation;
00580
00581 retval = GATLocation_Clone(location, &tempLocation);
00582 if(GAT_SUCCESS == retval)
00583 {
00584 retval = GAT_SUCCESS;
00585 GATSecurityContext_PartialDestroy( &this );
00586 this->securityContextType = GATSecurityContextType_Remote;
00587 (this->securityContextUnion).remoteSecurityContext.name = tempName;
00588 (this->securityContextUnion).remoteSecurityContext.location = tempLocation;
00589 (this->securityContextUnion).remoteSecurityContext.passphrase = tempPassphrase;
00590 }
00591 else
00592 {
00593 free(tempName);
00594 free(tempPassphrase);
00595 }
00596 }
00597 else
00598 {
00599 free(tempName);
00600 free(tempPassphrase);
00601 }
00602 }
00603 }
00604
00605 return retval;
00606 }
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 GATResult GATSecurityContext_GetRemoteAuthenticate(GATSecurityContext this, GATLocation *location, char **name, char **passphrase)
00619 {
00620 GATResult retval;
00621
00622 retval = GAT_INVALID_HANDLE;
00623 if( NULL != this )
00624 {
00625 retval = GAT_INVALID_PARAMETER;
00626 if( (NULL != location) && (NULL != name) && (NULL != passphrase) )
00627 {
00628 retval = GAT_INVALID_STATE;
00629 if( GATSecurityContextType_Remote == this->securityContextType )
00630 {
00631 retval = GAT_MEMORYFAILURE;
00632 (*name) = GATSecurityContext_StringClone( (this->securityContextUnion).remoteSecurityContext.name );
00633 (*passphrase) = GATSecurityContext_StringClone( (this->securityContextUnion).remoteSecurityContext.passphrase );
00634 if( (NULL != *name) && (NULL != *passphrase) )
00635 {
00636 retval = GATLocation_Clone( (this->securityContextUnion).remoteSecurityContext.location, location );
00637 if(GAT_SUCCESS != retval)
00638 {
00639 free(*name);
00640 free(*passphrase);
00641 }
00642 }
00643 else
00644 {
00645 free(*name);
00646 free(*passphrase);
00647 }
00648 }
00649 }
00650 }
00651
00652 return retval;
00653 }
00654
00655
00656
00657
00658
00659
00660
00661
00662 GATResult GATSecurityContext_GetSecurityContextType(GATSecurityContext this, GATSecurityContextType *type)
00663 {
00664 GATResult retval;
00665
00666 retval = GAT_INVALID_HANDLE;
00667 if( NULL != this )
00668 {
00669 retval = GAT_INVALID_PARAMETER;
00670 if( NULL != type )
00671 {
00672 retval = GAT_SUCCESS;
00673 (*type) = this->securityContextType;
00674 }
00675 }
00676
00677 return retval;
00678 }
00679
00680
00681 static char *GATSecurityContext_StringClone(const char *oldString)
00682 {
00683 char *newString;
00684 size_t memoryToAllocate;
00685
00686 memoryToAllocate = strlen(oldString) + 1;
00687 newString = (char *) malloc( memoryToAllocate );
00688 if( NULL != newString )
00689 {
00690 strcpy(newString, oldString);
00691 }
00692
00693 return newString;
00694 }
00695
00696 static void GATSecurityContext_PartialDestroy(GATSecurityContext *this)
00697 {
00698 if( NULL != (*this) )
00699 {
00700 if(GATSecurityContextType_Password == (*this)->securityContextType)
00701 {
00702 free( ((*this)->securityContextUnion).passwordSecurityContext.username );
00703 free( ((*this)->securityContextUnion).passwordSecurityContext.password );
00704
00705 ((*this)->securityContextUnion).passwordSecurityContext.username = NULL;
00706 ((*this)->securityContextUnion).passwordSecurityContext.password = NULL;
00707 }
00708
00709 if(GATSecurityContextType_Certificate == (*this)->securityContextType)
00710 {
00711 free( ((*this)->securityContextUnion).certificateSecurityContext.keyfile );
00712 free( ((*this)->securityContextUnion).certificateSecurityContext.passphrase );
00713 free( ((*this)->securityContextUnion).certificateSecurityContext.certificate );
00714
00715 ((*this)->securityContextUnion).certificateSecurityContext.keyfile = NULL;
00716 ((*this)->securityContextUnion).certificateSecurityContext.passphrase = NULL;
00717 ((*this)->securityContextUnion).certificateSecurityContext.certificate = NULL;
00718 }
00719
00720 if(GATSecurityContextType_Remote == (*this)->securityContextType)
00721 {
00722 free( ((*this)->securityContextUnion).remoteSecurityContext.name );
00723 free( ((*this)->securityContextUnion).remoteSecurityContext.passphrase );
00724 GATLocation_Destroy( &(((*this)->securityContextUnion).remoteSecurityContext.location) );
00725
00726 ((*this)->securityContextUnion).remoteSecurityContext.name = NULL;
00727 ((*this)->securityContextUnion).remoteSecurityContext.passphrase = NULL;
00728 }
00729 }
00730 }