Annotation of embedaddon/strongswan/src/libcharon/plugins/eap_simaka_sql/eap_simaka_sql_provider.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2010 Martin Willi
        !             3:  * Copyright (C) 2010 revosec AG
        !             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_simaka_sql_provider.h"
        !            17: 
        !            18: #include <time.h>
        !            19: 
        !            20: #include <daemon.h>
        !            21: 
        !            22: typedef struct private_eap_simaka_sql_provider_t private_eap_simaka_sql_provider_t;
        !            23: 
        !            24: /**
        !            25:  * Private data of an eap_simaka_sql_provider_t object.
        !            26:  */
        !            27: struct private_eap_simaka_sql_provider_t {
        !            28: 
        !            29:        /**
        !            30:         * Public eap_simaka_sql_provider_t interface.
        !            31:         */
        !            32:        eap_simaka_sql_provider_t public;
        !            33: 
        !            34:        /**
        !            35:         * Triplet/quintuplet database
        !            36:         */
        !            37:        database_t *db;
        !            38: 
        !            39:        /**
        !            40:         * Remove used triplets/quintuplets from database
        !            41:         */
        !            42:        bool remove_used;
        !            43: };
        !            44: 
        !            45: METHOD(simaka_provider_t, get_triplet, bool,
        !            46:        private_eap_simaka_sql_provider_t *this, identification_t *id,
        !            47:        char rand[SIM_RAND_LEN], char sres[SIM_SRES_LEN], char kc[SIM_KC_LEN])
        !            48: {
        !            49:        chunk_t rand_chunk, sres_chunk, kc_chunk;
        !            50:        enumerator_t *query;
        !            51:        bool found = FALSE;
        !            52:        char buf[128];
        !            53: 
        !            54:        snprintf(buf, sizeof(buf), "%Y", id);
        !            55:        query = this->db->query(this->db,
        !            56:                                "select rand, sres, kc from triplets where id = ? order by used",
        !            57:                                DB_TEXT, buf, DB_BLOB, DB_BLOB, DB_BLOB);
        !            58:        if (query)
        !            59:        {
        !            60:                if (query->enumerate(query, &rand_chunk, &sres_chunk, &kc_chunk))
        !            61:                {
        !            62:                        if (rand_chunk.len == SIM_RAND_LEN &&
        !            63:                                sres_chunk.len == SIM_SRES_LEN &&
        !            64:                                kc_chunk.len == SIM_KC_LEN)
        !            65:                        {
        !            66:                                memcpy(rand, rand_chunk.ptr, SIM_RAND_LEN);
        !            67:                                memcpy(sres, sres_chunk.ptr, SIM_SRES_LEN);
        !            68:                                memcpy(kc, kc_chunk.ptr, SIM_KC_LEN);
        !            69:                                found = TRUE;
        !            70:                        }
        !            71:                }
        !            72:                query->destroy(query);
        !            73:        }
        !            74:        if (found)
        !            75:        {
        !            76:                if (this->remove_used)
        !            77:                {
        !            78:                        this->db->execute(this->db, NULL,
        !            79:                                        "delete from triplets where id = ? and rand = ?",
        !            80:                                        DB_TEXT, buf, DB_BLOB, chunk_create(rand, SIM_RAND_LEN));
        !            81:                }
        !            82:                else
        !            83:                {
        !            84:                        this->db->execute(this->db, NULL,
        !            85:                                        "update triplets set used = ? where id = ? and rand = ?",
        !            86:                                        DB_UINT, time(NULL), DB_TEXT, buf,
        !            87:                                        DB_BLOB, chunk_create(rand, SIM_RAND_LEN));
        !            88:                }
        !            89:        }
        !            90:        return found;
        !            91: }
        !            92: 
        !            93: METHOD(simaka_provider_t, get_quintuplet, bool,
        !            94:        private_eap_simaka_sql_provider_t *this, identification_t *id,
        !            95:        char rand[AKA_RAND_LEN], char xres[AKA_RES_MAX], int *xres_len,
        !            96:        char ck[AKA_CK_LEN], char ik[AKA_IK_LEN], char autn[AKA_AUTN_LEN])
        !            97: {
        !            98:        chunk_t rand_chunk, xres_chunk, ck_chunk, ik_chunk, autn_chunk;
        !            99:        enumerator_t *query;
        !           100:        bool found = FALSE;
        !           101:        char buf[128];
        !           102: 
        !           103:        snprintf(buf, sizeof(buf), "%Y", id);
        !           104:        query = this->db->query(this->db, "select rand, res, ck, ik, autn "
        !           105:                                "from quintuplets where id = ? order by used", DB_TEXT, buf,
        !           106:                                DB_BLOB, DB_BLOB, DB_BLOB, DB_BLOB, DB_BLOB);
        !           107:        if (query)
        !           108:        {
        !           109:                if (query->enumerate(query, &rand_chunk, &xres_chunk,
        !           110:                                                         &ck_chunk, &ik_chunk, &autn_chunk))
        !           111:                {
        !           112:                        if (rand_chunk.len == AKA_RAND_LEN &&
        !           113:                                xres_chunk.len <= AKA_RES_MAX &&
        !           114:                                ck_chunk.len == AKA_CK_LEN &&
        !           115:                                ik_chunk.len == AKA_IK_LEN &&
        !           116:                                autn_chunk.len == AKA_AUTN_LEN)
        !           117:                        {
        !           118:                                memcpy(rand, rand_chunk.ptr, AKA_RAND_LEN);
        !           119:                                memcpy(xres, xres_chunk.ptr, xres_chunk.len);
        !           120:                                *xres_len = xres_chunk.len;
        !           121:                                memcpy(ck, ck_chunk.ptr, AKA_CK_LEN);
        !           122:                                memcpy(ik, ik_chunk.ptr, AKA_IK_LEN);
        !           123:                                memcpy(autn, autn_chunk.ptr, AKA_AUTN_LEN);
        !           124:                                found = TRUE;
        !           125:                        }
        !           126:                }
        !           127:                query->destroy(query);
        !           128:        }
        !           129:        if (found)
        !           130:        {
        !           131:                if (this->remove_used)
        !           132:                {
        !           133:                        this->db->execute(this->db, NULL,
        !           134:                                        "delete from quintuplets where id = ? and rand = ?",
        !           135:                                        DB_TEXT, buf, DB_BLOB, chunk_create(rand, SIM_RAND_LEN));
        !           136:                }
        !           137:                else
        !           138:                {
        !           139:                        this->db->execute(this->db, NULL,
        !           140:                                        "update quintuplets set used = ? where id = ? and rand = ?",
        !           141:                                        DB_UINT, time(NULL), DB_TEXT, buf,
        !           142:                                        DB_BLOB, chunk_create(rand, AKA_RAND_LEN));
        !           143:                }
        !           144:        }
        !           145:        return found;
        !           146: }
        !           147: 
        !           148: METHOD(eap_simaka_sql_provider_t, destroy, void,
        !           149:        private_eap_simaka_sql_provider_t *this)
        !           150: {
        !           151:        free(this);
        !           152: }
        !           153: 
        !           154: /**
        !           155:  * See header
        !           156:  */
        !           157: eap_simaka_sql_provider_t *eap_simaka_sql_provider_create(database_t *db,
        !           158:                                                                                                                  bool remove_used)
        !           159: {
        !           160:        private_eap_simaka_sql_provider_t *this;
        !           161: 
        !           162:        INIT(this,
        !           163:                .public = {
        !           164:                        .provider = {
        !           165:                                .get_triplet = _get_triplet,
        !           166:                                .get_quintuplet = _get_quintuplet,
        !           167:                                .resync = (void*)return_false,
        !           168:                                .is_pseudonym = (void*)return_null,
        !           169:                                .gen_pseudonym = (void*)return_null,
        !           170:                                .is_reauth = (void*)return_null,
        !           171:                                .gen_reauth = (void*)return_null,
        !           172:                        },
        !           173:                        .destroy = _destroy,
        !           174:                },
        !           175:                .db = db,
        !           176:                .remove_used = remove_used,
        !           177:        );
        !           178: 
        !           179:        return &this->public;
        !           180: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>