Annotation of embedaddon/strongswan/src/libcharon/plugins/eap_sim_file/eap_sim_file_triplets.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2008 Martin Willi
! 3: * HSR Hochschule fuer Technik Rapperswil
! 4: *
! 5: * This program is free software; you can redistribute it and/or modify it
! 6: * under the terms of the GNU General Public License as published by the
! 7: * Free Software Foundation; either version 2 of the License, or (at your
! 8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
! 9: *
! 10: * This program is distributed in the hope that it will be useful, but
! 11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
! 13: * for more details.
! 14: */
! 15:
! 16: #include "eap_sim_file_triplets.h"
! 17:
! 18: #include <stdio.h>
! 19: #include <errno.h>
! 20:
! 21: #include <daemon.h>
! 22: #include <collections/linked_list.h>
! 23: #include <threading/mutex.h>
! 24: #include <simaka_manager.h>
! 25:
! 26: typedef struct private_eap_sim_file_triplets_t private_eap_sim_file_triplets_t;
! 27:
! 28: /**
! 29: * Private data of an eap_sim_file_triplets_t object.
! 30: */
! 31: struct private_eap_sim_file_triplets_t {
! 32:
! 33: /**
! 34: * Public eap_sim_file_triplets_t interface.
! 35: */
! 36: eap_sim_file_triplets_t public;
! 37:
! 38: /**
! 39: * List of triplets, as triplet_t
! 40: */
! 41: linked_list_t *triplets;
! 42:
! 43: /**
! 44: * mutex to lock triplets list
! 45: */
! 46: mutex_t *mutex;
! 47: };
! 48:
! 49: /**
! 50: * A single triplet
! 51: */
! 52: typedef struct {
! 53: identification_t *imsi;
! 54: char rand[SIM_RAND_LEN];
! 55: char sres[SIM_SRES_LEN];
! 56: char kc[SIM_KC_LEN];
! 57: } triplet_t;
! 58:
! 59: /**
! 60: * Destroy a triplet
! 61: */
! 62: static void triplet_destroy(triplet_t *this)
! 63: {
! 64: DESTROY_IF(this->imsi);
! 65: free(this);
! 66: }
! 67:
! 68: /**
! 69: * triplet enumerator
! 70: */
! 71: typedef struct {
! 72: /** implements enumerator */
! 73: enumerator_t public;
! 74: /** inner enumerator */
! 75: enumerator_t *inner;
! 76: /** current enumerating triplet */
! 77: triplet_t *current;
! 78: /** back ptr */
! 79: private_eap_sim_file_triplets_t *this;
! 80: } triplet_enumerator_t;
! 81:
! 82: METHOD(enumerator_t, enumerator_destroy, void,
! 83: triplet_enumerator_t *e)
! 84: {
! 85: if (e->current)
! 86: {
! 87: /* We assume that the current element is used on invocation if destroy.
! 88: * We move that triplet to the end to avoid handout of the same triplet
! 89: * next time. */
! 90: e->this->triplets->remove_at(e->this->triplets, e->inner);
! 91: e->this->triplets->insert_last(e->this->triplets, e->current);
! 92: }
! 93: e->inner->destroy(e->inner);
! 94: e->this->mutex->unlock(e->this->mutex);
! 95: free(e);
! 96: }
! 97:
! 98: METHOD(enumerator_t, enumerator_enumerate, bool,
! 99: triplet_enumerator_t *e, va_list args)
! 100: {
! 101: identification_t **imsi;
! 102: triplet_t *triplet;
! 103: char **rand, **sres, **kc;
! 104:
! 105: VA_ARGS_VGET(args, imsi, rand, sres, kc);
! 106:
! 107: if (e->inner->enumerate(e->inner, &triplet))
! 108: {
! 109: e->current = triplet;
! 110: *imsi = triplet->imsi;
! 111: *rand = triplet->rand;
! 112: *sres = triplet->sres;
! 113: *kc = triplet->kc;
! 114: return TRUE;
! 115: }
! 116: e->current = NULL;
! 117: return FALSE;
! 118: }
! 119:
! 120: METHOD(eap_sim_file_triplets_t, create_enumerator, enumerator_t*,
! 121: private_eap_sim_file_triplets_t *this)
! 122: {
! 123: triplet_enumerator_t *enumerator;
! 124:
! 125: this->mutex->lock(this->mutex);
! 126: INIT(enumerator,
! 127: .public = {
! 128: .enumerate = enumerator_enumerate_default,
! 129: .venumerate = _enumerator_enumerate,
! 130: .destroy = _enumerator_destroy,
! 131: },
! 132: .inner = this->triplets->create_enumerator(this->triplets),
! 133: .this = this,
! 134: );
! 135: return &enumerator->public;
! 136: }
! 137:
! 138: /**
! 139: * convert to token into the array
! 140: */
! 141: static void parse_token(char *to, char *from, size_t len)
! 142: {
! 143: chunk_t chunk;
! 144:
! 145: chunk = chunk_create(from, min(strlen(from), len * 2));
! 146: chunk = chunk_from_hex(chunk, NULL);
! 147: memset(to, 0, len);
! 148: memcpy(to + len - chunk.len, chunk.ptr, chunk.len);
! 149: free(chunk.ptr);
! 150: }
! 151:
! 152: /**
! 153: * Read the triplets from the file
! 154: */
! 155: static bool read_triplets(private_eap_sim_file_triplets_t *this, char *path)
! 156: {
! 157: char line[512];
! 158: FILE *file;
! 159: int i, nr = 0;
! 160:
! 161: file = fopen(path, "r");
! 162: if (file == NULL)
! 163: {
! 164: DBG1(DBG_CFG, "opening triplet file %s failed: %s",
! 165: path, strerror(errno));
! 166: return FALSE;
! 167: }
! 168:
! 169: /* read line by line */
! 170: while (fgets(line, sizeof(line), file))
! 171: {
! 172: triplet_t *triplet;
! 173: enumerator_t *enumerator;
! 174: char *token;
! 175:
! 176: nr++;
! 177: /* skip comments, empty lines */
! 178: switch (line[0])
! 179: {
! 180: case '\n':
! 181: case '\r':
! 182: case '#':
! 183: case '\0':
! 184: continue;
! 185: default:
! 186: break;
! 187: }
! 188: triplet = malloc_thing(triplet_t);
! 189: memset(triplet, 0, sizeof(triplet_t));
! 190:
! 191: i = 0;
! 192: enumerator = enumerator_create_token(line, ",", " \n\r#");
! 193: while (enumerator->enumerate(enumerator, &token))
! 194: {
! 195: switch (i++)
! 196: {
! 197: case 0: /* IMSI */
! 198: triplet->imsi = identification_create_from_string(token);
! 199: continue;
! 200: case 1: /* rand */
! 201: parse_token(triplet->rand, token, SIM_RAND_LEN);
! 202: continue;
! 203: case 2: /* sres */
! 204: parse_token(triplet->sres, token, SIM_SRES_LEN);
! 205: continue;
! 206: case 3: /* kc */
! 207: parse_token(triplet->kc, token, SIM_KC_LEN);
! 208: continue;
! 209: default:
! 210: break;;
! 211: }
! 212: break;
! 213: }
! 214: enumerator->destroy(enumerator);
! 215: if (i < 4)
! 216: {
! 217: DBG1(DBG_CFG, "error in triplet file, line %d", nr);
! 218: triplet_destroy(triplet);
! 219: continue;
! 220: }
! 221:
! 222: DBG2(DBG_CFG, "triplet: imsi %Y\nrand %b\nsres %b\nkc %b",
! 223: triplet->imsi, triplet->rand, SIM_RAND_LEN,
! 224: triplet->sres, SIM_SRES_LEN, triplet->kc, SIM_KC_LEN);
! 225:
! 226: this->triplets->insert_last(this->triplets, triplet);
! 227: }
! 228: fclose(file);
! 229:
! 230: DBG1(DBG_CFG, "read %d triplets from %s",
! 231: this->triplets->get_count(this->triplets), path);
! 232: return TRUE;
! 233: }
! 234:
! 235: METHOD(eap_sim_file_triplets_t, destroy, void,
! 236: private_eap_sim_file_triplets_t *this)
! 237: {
! 238: this->triplets->destroy_function(this->triplets, (void*)triplet_destroy);
! 239: this->mutex->destroy(this->mutex);
! 240: free(this);
! 241: }
! 242:
! 243: /**
! 244: * See header
! 245: */
! 246: eap_sim_file_triplets_t *eap_sim_file_triplets_create(char *file)
! 247: {
! 248: private_eap_sim_file_triplets_t *this;
! 249:
! 250: INIT(this,
! 251: .public = {
! 252: .create_enumerator = _create_enumerator,
! 253: .destroy = _destroy,
! 254: },
! 255: .triplets = linked_list_create(),
! 256: .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
! 257: );
! 258:
! 259: if (!read_triplets(this, file))
! 260: {
! 261: destroy(this);
! 262: return NULL;
! 263: }
! 264: return &this->public;
! 265: }
! 266:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>