00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <malloc.h>
00025 #include <string.h>
00026
00027 #include "_lcmaps_cred_data.h"
00028 #include "lcmaps_log.h"
00029 #include "lcmaps_vo_data.h"
00030
00031
00032
00033
00034
00035 static int compare_gids(const void *, const void *);
00036
00037
00038
00039
00040
00041 static cred_data_t credData =
00042 {
00043 (char *) NULL,
00044 (uid_t *) NULL, (gid_t *) NULL, (gid_t *) NULL,
00045 (lcmaps_vo_data_t *) NULL, (char **) NULL,
00046 0, 0, 0, 0, 0,
00047 };
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00075 int addCredentialData(
00076 int datatype,
00077 void *data
00078 )
00079 {
00080 switch(datatype)
00081 {
00082 case DN :
00083 if (data)
00084 {
00085 credData.dn = strdup(*(char **)data);
00086 }
00087 break;
00088
00089 case UID :
00090 if (data)
00091 {
00092 credData.uid = (uid_t *) realloc(credData.uid, ((credData.cntUid+1) * sizeof(uid_t)));
00093 credData.uid[credData.cntUid] = *((uid_t *)data);
00094 credData.cntUid++;
00095 }
00096 break;
00097
00098 case PRI_GID :
00099 if (data)
00100 {
00101 credData.cntPriGid++;
00102 credData.priGid = (gid_t *) realloc(credData.priGid, (credData.cntPriGid * sizeof(gid_t)));
00103 credData.priGid[credData.cntPriGid - 1] = *((gid_t *)data);
00104 }
00105 break;
00106
00107 case SEC_GID :
00108 if (data)
00109 {
00110 int foundgid;
00111 int igid = 0;
00112 gid_t newgid;
00113
00114 newgid = *((gid_t *)data);
00115
00116
00117 foundgid = 0;
00118 for (igid = 0; igid < credData.cntSecGid; igid++)
00119 {
00120 if (newgid == credData.secGid[igid])
00121 {
00122 foundgid = 1;
00123 break;
00124 }
00125 }
00126 if (foundgid) break;
00127
00128
00129 credData.cntSecGid++;
00130 credData.secGid = (gid_t *) realloc(credData.secGid, (credData.cntSecGid * sizeof(gid_t)));
00131 credData.secGid[credData.cntSecGid - 1] = newgid;
00132
00133
00134 if (credData.cntSecGid > 1)
00135 {
00136 qsort(
00137 credData.secGid,
00138 credData.cntSecGid,
00139 sizeof(gid_t),
00140 (int (*)(const void *,const void *))compare_gids
00141 );
00142 }
00143 }
00144 break;
00145
00146 case LCMAPS_VO_CRED :
00147 if (data)
00148 {
00149 credData.VoCred = (lcmaps_vo_data_t *) realloc(credData.VoCred, ((credData.cntVoCred+1) * sizeof(lcmaps_vo_data_t)));
00150 lcmaps_copyVoData(&(credData.VoCred[credData.cntVoCred]), (lcmaps_vo_data_t *) data);
00151 credData.cntVoCred++;
00152 }
00153 break;
00154
00155 case LCMAPS_VO_CRED_STRING :
00156 if (data)
00157 {
00158 credData.VoCredString = (char **) realloc(credData.VoCredString, ((credData.cntVoCredString+1) * sizeof(char *)));
00159 credData.VoCredString[credData.cntVoCredString] = strdup(*((char **)data));
00160 credData.cntVoCredString++;
00161 }
00162 break;
00163
00164 default :
00165 return -1;
00166 }
00167 return 0;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00193 void *getCredentialData(
00194 int datatype,
00195 int *count
00196 )
00197 {
00198 switch(datatype)
00199 {
00200 case DN : *count = 1;
00201 return &(credData.dn);
00202
00203 case UID : *count = credData.cntUid;
00204 return (credData.uid);
00205
00206 case PRI_GID : *count = credData.cntPriGid;
00207 return (credData.priGid);
00208
00209 case SEC_GID : *count = credData.cntSecGid;
00210 return (credData.secGid);
00211
00212 case LCMAPS_VO_CRED : *count = credData.cntVoCred;
00213 return (credData.VoCred);
00214
00215 case LCMAPS_VO_CRED_STRING : *count = credData.cntVoCredString;
00216 return (credData.VoCredString);
00217
00218 default : return NULL;
00219 }
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00237 int cleanCredentialData()
00238 {
00239 int i = 0;
00240
00241 for (i = 0; i < credData.cntVoCredString; i++)
00242 if (credData.VoCredString[i]) free(credData.VoCredString[i]);
00243 for (i = 0; i < credData.cntVoCred; i++)
00244 lcmaps_cleanVoData(&(credData.VoCred[i]));
00245
00246 free(credData.dn);
00247 free(credData.uid);
00248 free(credData.priGid);
00249 free(credData.secGid);
00250 free(credData.VoCredString);
00251 free(credData.VoCred);
00252
00253 credData.dn = NULL;
00254 credData.uid = NULL;
00255 credData.priGid = NULL;
00256 credData.secGid = NULL;
00257 credData.VoCredString = NULL;
00258 credData.VoCred = NULL;
00259
00260 credData.cntUid = 0;
00261 credData.cntPriGid = 0;
00262 credData.cntSecGid = 0;
00263 credData.cntVoCredString = 0;
00264 credData.cntVoCred = 0;
00265
00266 return 0;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00287 void printCredData(int debug_level)
00288 {
00289 int i;
00290
00291 lcmaps_log_debug(debug_level, "\nCredential Print:\n");
00292
00293 if (credData.dn != NULL)
00294 lcmaps_log_debug(debug_level, "dn : %s\n", credData.dn);
00295 for (i = 0; i < credData.cntUid; i++)
00296 lcmaps_log_debug(debug_level, "uid : %d [%d/%d]\n", credData.uid[i], i+1, credData.cntUid);
00297 for (i = 0; i < credData.cntPriGid; i++)
00298 lcmaps_log_debug(debug_level, "pgid : %d [%d/%d]\n", credData.priGid[i], i+1, credData.cntPriGid);
00299 for (i = 0; i < credData.cntSecGid; i++)
00300 lcmaps_log_debug(debug_level, "sgid : %d [%d/%d]\n", credData.secGid[i], i+1, credData.cntSecGid);
00301 for (i = 0; i < credData.cntVoCredString; i++)
00302 lcmaps_log_debug(debug_level, "VO credential string : %s [%d/%d]\n", credData.VoCredString[i], i+1, credData.cntVoCredString);
00303 for (i = 0; i < credData.cntVoCred; i++)
00304 {
00305 lcmaps_log_debug(debug_level, "VO credential : [%d/%d]\n", i+1, credData.cntVoCred);
00306 lcmaps_printVoData(debug_level, &(credData.VoCred[i]));
00307 }
00308 }
00309
00310 static int compare_gids(const void * pgid1, const void * pgid2)
00311 {
00312 gid_t gid1, gid2;
00313
00314 if (pgid1 == NULL) return 0;
00315 if (pgid2 == NULL) return 0;
00316
00317 gid1 = *((gid_t *) (pgid1));
00318 gid2 = *((gid_t *) (pgid2));
00319 if (gid1 < gid2)
00320 return -1;
00321 else if (gid1 > gid2)
00322 return 1;
00323 return 0;
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333