00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00026
00027
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <malloc.h>
00032 #include <string.h>
00033
00034 #include "_lcmaps_cred_data.h"
00035 #include "lcmaps_log.h"
00036 #include "lcmaps_vo_data.h"
00037
00038
00039
00040
00041
00042 static int compare_gids(const void *, const void *);
00043
00044
00045
00046
00047
00048 static cred_data_t credData =
00049 {
00050 (char *) NULL,
00051 (uid_t *) NULL,
00052 (gid_t *) NULL,
00053 (gid_t *) NULL,
00054 (lcmaps_vo_data_t *) NULL,
00055 (char **) NULL,
00056 (lcmaps_vo_mapping_t *) NULL,
00057 0,
00058 0,
00059 0,
00060 0,
00061 0,
00062 0,
00063 (char *) NULL,
00064 };
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00092 int addCredentialData(
00093 int datatype,
00094 void *data
00095 )
00096 {
00097 switch(datatype)
00098 {
00099 case DN :
00100 if (data)
00101 {
00102 credData.dn = strdup(*(char **)data);
00103 }
00104 break;
00105
00106 case UID :
00107 if (data)
00108 {
00109 credData.uid = (uid_t *) realloc(credData.uid, ((credData.cntUid+1) * sizeof(uid_t)));
00110 credData.uid[credData.cntUid] = *((uid_t *)data);
00111 credData.cntUid++;
00112 }
00113 break;
00114
00115 case PRI_GID :
00116 if (data)
00117 {
00118 credData.cntPriGid++;
00119 credData.priGid = (gid_t *) realloc(credData.priGid, (credData.cntPriGid * sizeof(gid_t)));
00120 credData.priGid[credData.cntPriGid - 1] = *((gid_t *)data);
00121 }
00122 break;
00123
00124 case SEC_GID :
00125 if (data)
00126 {
00127 int foundgid;
00128 int igid = 0;
00129 gid_t newgid;
00130
00131 newgid = *((gid_t *)data);
00132
00133
00134 foundgid = 0;
00135 for (igid = 0; igid < credData.cntSecGid; igid++)
00136 {
00137 if (newgid == credData.secGid[igid])
00138 {
00139 foundgid = 1;
00140 break;
00141 }
00142 }
00143 if (foundgid) break;
00144
00145
00146 credData.cntSecGid++;
00147 credData.secGid = (gid_t *) realloc(credData.secGid, (credData.cntSecGid * sizeof(gid_t)));
00148 credData.secGid[credData.cntSecGid - 1] = newgid;
00149
00150
00151 if (credData.cntSecGid > 1)
00152 {
00153 qsort(
00154 credData.secGid,
00155 credData.cntSecGid,
00156 sizeof(gid_t),
00157 (int (*)(const void *,const void *))compare_gids
00158 );
00159 }
00160 }
00161 break;
00162
00163 case LCMAPS_VO_CRED :
00164 if (data)
00165 {
00166 credData.VoCred = (lcmaps_vo_data_t *) realloc(credData.VoCred, ((credData.cntVoCred+1) * sizeof(lcmaps_vo_data_t)));
00167 lcmaps_copyVoData(&(credData.VoCred[credData.cntVoCred]), (lcmaps_vo_data_t *) data);
00168 credData.cntVoCred++;
00169 }
00170 break;
00171
00172 case LCMAPS_VO_CRED_STRING :
00173 if (data)
00174 {
00175 credData.VoCredString = (char **) realloc(credData.VoCredString, ((credData.cntVoCredString+1) * sizeof(char *)));
00176 credData.VoCredString[credData.cntVoCredString] = strdup(*((char **)data));
00177 credData.cntVoCredString++;
00178 }
00179 break;
00180
00181 case LCMAPS_VO_CRED_MAPPING:
00182 if (data)
00183 {
00184 credData.VoCredMapping = (lcmaps_vo_mapping_t *) realloc(credData.VoCredMapping, ((credData.cntVoCredMapping+1) * sizeof(lcmaps_vo_mapping_t)));
00185 lcmaps_copyVoMapping(&(credData.VoCredMapping[credData.cntVoCredMapping]), (lcmaps_vo_mapping_t *) data);
00186 credData.cntVoCredMapping++;
00187 }
00188 break;
00189
00190 case POOL_INDEX :
00191 if (data)
00192 {
00193 credData.pool_index = strdup(*(char **)data);
00194 }
00195 break;
00196
00197
00198
00199 default :
00200 return -1;
00201 }
00202 return 0;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00228 void *getCredentialData(
00229 int datatype,
00230 int *count
00231 )
00232 {
00233 switch(datatype)
00234 {
00235 case DN:
00236 *count = 1;
00237 return &(credData.dn);
00238
00239 case UID:
00240 *count = credData.cntUid;
00241 return (credData.uid);
00242
00243 case PRI_GID:
00244 *count = credData.cntPriGid;
00245 return (credData.priGid);
00246
00247 case SEC_GID:
00248 *count = credData.cntSecGid;
00249 return (credData.secGid);
00250
00251 case LCMAPS_VO_CRED:
00252 *count = credData.cntVoCred;
00253 return (credData.VoCred);
00254
00255 case LCMAPS_VO_CRED_STRING:
00256 *count = credData.cntVoCredString;
00257 return (credData.VoCredString);
00258
00259 case LCMAPS_VO_CRED_MAPPING :
00260 *count = credData.cntVoCredMapping;
00261 return (credData.VoCredMapping);
00262
00263 case POOL_INDEX:
00264 *count = 1;
00265 return &(credData.pool_index);
00266
00267
00268 default : return NULL;
00269 }
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00287 int cleanCredentialData()
00288 {
00289 int i = 0;
00290
00291 for (i = 0; i < credData.cntVoCred; i++)
00292 lcmaps_cleanVoData(&(credData.VoCred[i]));
00293 for (i = 0; i < credData.cntVoCredString; i++)
00294 if (credData.VoCredString[i]) free(credData.VoCredString[i]);
00295 for (i = 0; i < credData.cntVoCredMapping; i++)
00296 lcmaps_cleanVoMapping(&(credData.VoCredMapping[i]));
00297
00298 if (credData.dn) free(credData.dn);
00299 if (credData.uid) free(credData.uid);
00300 if (credData.priGid) free(credData.priGid);
00301 if (credData.secGid) free(credData.secGid);
00302 if (credData.VoCred) free(credData.VoCred);
00303 if (credData.VoCredString) free(credData.VoCredString);
00304 if (credData.VoCredMapping) free(credData.VoCredMapping);
00305 if (credData.pool_index) free(credData.pool_index);
00306
00307
00308 credData.dn = NULL;
00309 credData.uid = NULL;
00310 credData.priGid = NULL;
00311 credData.secGid = NULL;
00312 credData.VoCred = NULL;
00313 credData.VoCredString = NULL;
00314 credData.VoCredMapping = NULL;
00315 credData.pool_index = NULL;
00316
00317 credData.cntUid = 0;
00318 credData.cntPriGid = 0;
00319 credData.cntSecGid = 0;
00320 credData.cntVoCred = 0;
00321 credData.cntVoCredString = 0;
00322 credData.cntVoCredMapping = 0;
00323
00324 return 0;
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00345 void printCredData(int debug_level)
00346 {
00347 int i;
00348
00349 lcmaps_log_debug(debug_level, "\nCredential Print:\n");
00350
00351 if (credData.dn != NULL)
00352 lcmaps_log_debug(debug_level, "dn : %s\n", credData.dn);
00353 for (i = 0; i < credData.cntUid; i++)
00354 lcmaps_log_debug(debug_level, "uid : %d [%d/%d]\n", credData.uid[i], i+1, credData.cntUid);
00355 for (i = 0; i < credData.cntPriGid; i++)
00356 lcmaps_log_debug(debug_level, "pgid : %d [%d/%d]\n", credData.priGid[i], i+1, credData.cntPriGid);
00357 for (i = 0; i < credData.cntSecGid; i++)
00358 lcmaps_log_debug(debug_level, "sgid : %d [%d/%d]\n", credData.secGid[i], i+1, credData.cntSecGid);
00359 for (i = 0; i < credData.cntVoCred; i++)
00360 {
00361 lcmaps_log_debug(debug_level, "VO credential : [%d/%d]\n", i+1, credData.cntVoCred);
00362 lcmaps_printVoData(debug_level, &(credData.VoCred[i]));
00363 }
00364 for (i = 0; i < credData.cntVoCredString; i++)
00365 lcmaps_log_debug(debug_level, "VO credential string : %s [%d/%d]\n", credData.VoCredString[i], i+1, credData.cntVoCredString);
00366 for (i = 0; i < credData.cntVoCredMapping; i++)
00367 {
00368 lcmaps_log_debug(debug_level, "VO credential mapping : [%d/%d]\n", i+1, credData.cntVoCredMapping);
00369 lcmaps_printVoMapping(debug_level, &(credData.VoCredMapping[i]));
00370 }
00371 if (credData.pool_index != NULL)
00372 {
00373 lcmaps_log_a_string_debug(debug_level, "pool_index : %s\n", credData.pool_index);
00374 }
00375 }
00376
00377 static int compare_gids(const void * pgid1, const void * pgid2)
00378 {
00379 gid_t gid1, gid2;
00380
00381 if (pgid1 == NULL) return 0;
00382 if (pgid2 == NULL) return 0;
00383
00384 gid1 = *((gid_t *) (pgid1));
00385 gid2 = *((gid_t *) (pgid2));
00386 if (gid1 < gid2)
00387 return -1;
00388 else if (gid1 > gid2)
00389 return 1;
00390 return 0;
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400