Annotation of embedaddon/strongswan/src/libimcv/tcg/pts/tcg_pts_attr_unix_file_meta.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2011-2012 Sansar Choinyambuu
                      3:  * Copyright (C) 2011-2014 Andreas Steffen
                      4:  * HSR Hochschule fuer Technik Rapperswil
                      5:  *
                      6:  * This program is free software; you can redistribute it and/or modify it
                      7:  * under the terms of the GNU General Public License as published by the
                      8:  * Free Software Foundation; either version 2 of the License, or (at your
                      9:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                     10:  *
                     11:  * This program is distributed in the hope that it will be useful, but
                     12:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     13:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     14:  * for more details.
                     15:  */
                     16: 
                     17: #define _GNU_SOURCE /* for stdndup() */
                     18: #include <string.h>
                     19: 
                     20: #include "tcg_pts_attr_unix_file_meta.h"
                     21: 
                     22: #include <pa_tnc/pa_tnc_msg.h>
                     23: #include <bio/bio_writer.h>
                     24: #include <bio/bio_reader.h>
                     25: #include <collections/linked_list.h>
                     26: #include <utils/debug.h>
                     27: 
                     28: typedef struct private_tcg_pts_attr_file_meta_t private_tcg_pts_attr_file_meta_t;
                     29: 
                     30: /**
                     31:  * Unix-Style File Metadata
                     32:  * see section 3.17.3 of PTS Protocol: Binding to TNC IF-M Specification
                     33:  *
                     34:  *                                        1                               2                               3
                     35:  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
                     36:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     37:  *  |                             Number of Files included                                             |
                     38:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     39:  *  |                             Number of Files included                                             |
                     40:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     41:  *  |  File metadata Length        |    Type       |   Reserved        |
                     42:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     43:  *  |                                                  File Size                                                       |
                     44:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     45:  *  |                                                  File Size                                                       |
                     46:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     47:  *  |                                          File Create Time                                                |
                     48:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     49:  *  |                                          File Create Time                                                |
                     50:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     51:  *  |                                          Last Modify Time                                                |
                     52:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     53:  *  |                                          Last Modify Time                                                |
                     54:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     55:  *  |                                          Last Access Time                                                |
                     56:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     57:  *  |                                          Last Access Time                                                |
                     58:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     59:  *  |                                          File Owner ID                                                   |
                     60:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     61:  *  |                                          File Owner ID                                                   |
                     62:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     63:  *  |                                          File Group ID                                                   |
                     64:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     65:  *  |                                          File Group ID                                                   |
                     66:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     67:  *  ~                                  Filename (Variable Length)                                      ~
                     68:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     69:  *                                      ...........................
                     70:  */
                     71: 
                     72: #define PTS_FILE_META_SIZE                     8
                     73: #define PTS_FILE_MEAS_RESERVED         0x00
                     74: #define PTS_FILE_METADATA_SIZE         52
                     75: 
                     76: /**
                     77:  * Private data of an tcg_pts_attr_file_meta_t object.
                     78:  */
                     79: struct private_tcg_pts_attr_file_meta_t {
                     80: 
                     81:        /**
                     82:         * Public members of tcg_pts_attr_file_meta_t
                     83:         */
                     84:        tcg_pts_attr_file_meta_t public;
                     85: 
                     86:        /**
                     87:         * Vendor-specific attribute type
                     88:         */
                     89:        pen_type_t type;
                     90: 
                     91:        /**
                     92:         * Length of attribute value
                     93:         */
                     94:        size_t length;
                     95: 
                     96:        /**
                     97:         * Attribute value or segment
                     98:         */
                     99:        chunk_t value;
                    100: 
                    101:        /**
                    102:         * Noskip flag
                    103:         */
                    104:        bool noskip_flag;
                    105: 
                    106:        /**
                    107:         * PTS File Metadata
                    108:         */
                    109:        pts_file_meta_t *metadata;
                    110: 
                    111:        /**
                    112:         * Reference count
                    113:         */
                    114:        refcount_t ref;
                    115: };
                    116: 
                    117: METHOD(pa_tnc_attr_t, get_type, pen_type_t,
                    118:        private_tcg_pts_attr_file_meta_t *this)
                    119: {
                    120:        return this->type;
                    121: }
                    122: 
                    123: METHOD(pa_tnc_attr_t, get_value, chunk_t,
                    124:        private_tcg_pts_attr_file_meta_t *this)
                    125: {
                    126:        return this->value;
                    127: }
                    128: 
                    129: METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
                    130:        private_tcg_pts_attr_file_meta_t *this)
                    131: {
                    132:        return this->noskip_flag;
                    133: }
                    134: 
                    135: METHOD(pa_tnc_attr_t, set_noskip_flag,void,
                    136:        private_tcg_pts_attr_file_meta_t *this, bool noskip)
                    137: {
                    138:        this->noskip_flag = noskip;
                    139: }
                    140: 
                    141: METHOD(pa_tnc_attr_t, build, void,
                    142:        private_tcg_pts_attr_file_meta_t *this)
                    143: {
                    144:        bio_writer_t *writer;
                    145:        enumerator_t *enumerator;
                    146:        pts_file_metadata_t *entry;
                    147:        uint64_t number_of_files;
                    148: 
                    149:        if (this->value.ptr)
                    150:        {
                    151:                return;
                    152:        }
                    153:        number_of_files = this->metadata->get_file_count(this->metadata);
                    154:        writer = bio_writer_create(PTS_FILE_META_SIZE);
                    155: 
                    156:        writer->write_uint64(writer, number_of_files);
                    157: 
                    158:        enumerator = this->metadata->create_enumerator(this->metadata);
                    159:        while (enumerator->enumerate(enumerator, &entry))
                    160:        {
                    161:                writer->write_uint16(writer, PTS_FILE_METADATA_SIZE +
                    162:                                                                         strlen(entry->filename));
                    163:                writer->write_uint8 (writer, entry->type);
                    164:                writer->write_uint8 (writer, PTS_FILE_MEAS_RESERVED);
                    165:                writer->write_uint64(writer, entry->filesize);
                    166:                writer->write_uint64(writer, entry->created);
                    167:                writer->write_uint64(writer, entry->modified);
                    168:                writer->write_uint64(writer, entry->accessed);
                    169:                writer->write_uint64(writer, entry->owner);
                    170:                writer->write_uint64(writer, entry->group);
                    171:                writer->write_data  (writer, chunk_create(entry->filename,
                    172:                                                                                                  strlen(entry->filename)));
                    173:        }
                    174:        enumerator->destroy(enumerator);
                    175: 
                    176:        this->value = writer->extract_buf(writer);
                    177:        this->length = this->value.len;
                    178:        writer->destroy(writer);
                    179: }
                    180: 
                    181: METHOD(pa_tnc_attr_t, process, status_t,
                    182:        private_tcg_pts_attr_file_meta_t *this, uint32_t *offset)
                    183: {
                    184:        bio_reader_t *reader;
                    185:        pts_file_metadata_t *entry;
                    186:        uint8_t type, reserved;
                    187:        uint16_t len;
                    188:        uint64_t number_of_files, filesize, created, modified, accessed;
                    189:        uint64_t owner, group;
                    190:        chunk_t filename;
                    191:        status_t status = FAILED;
                    192: 
                    193:        *offset = 0;
                    194: 
                    195:        if (this->value.len < this->length)
                    196:        {
                    197:                return NEED_MORE;
                    198:        }
                    199:        if (this->value.len < PTS_FILE_META_SIZE)
                    200:        {
                    201:                DBG1(DBG_TNC, "insufficient data for PTS Unix-Style file metadata header");
                    202:                return FAILED;
                    203:        }
                    204:        reader = bio_reader_create(this->value);
                    205:        reader->read_uint64(reader, &number_of_files);
                    206: 
                    207:        this->metadata = pts_file_meta_create();
                    208: 
                    209:        while (number_of_files--)
                    210:        {
                    211:                if (!reader->read_uint16(reader, &len))
                    212:                {
                    213:                        DBG1(DBG_TNC, "insufficient data for PTS file metadata length");
                    214:                        goto end;
                    215:                }
                    216:                if (!reader->read_uint8(reader, &type))
                    217:                {
                    218:                        DBG1(DBG_TNC, "insufficient data for file type");
                    219:                        goto end;
                    220:                }
                    221:                if (!reader->read_uint8(reader, &reserved))
                    222:                {
                    223:                        DBG1(DBG_TNC, "insufficient data for reserved field");
                    224:                        goto end;
                    225:                }
                    226:                if (!reader->read_uint64(reader, &filesize))
                    227:                {
                    228:                        DBG1(DBG_TNC, "insufficient data for file size");
                    229:                        goto end;
                    230:                }
                    231:                if (!reader->read_uint64(reader, &created))
                    232:                {
                    233:                        DBG1(DBG_TNC, "insufficient data for file create time");
                    234:                        goto end;
                    235:                }
                    236:                if (!reader->read_uint64(reader, &modified))
                    237:                {
                    238:                        DBG1(DBG_TNC, "insufficient data for last modify time");
                    239:                        goto end;
                    240:                }
                    241:                if (!reader->read_uint64(reader, &accessed))
                    242:                {
                    243:                        DBG1(DBG_TNC, "insufficient data for last access time");
                    244:                        goto end;
                    245:                }
                    246:                if (!reader->read_uint64(reader, &owner))
                    247:                {
                    248:                        DBG1(DBG_TNC, "insufficient data for owner id");
                    249:                        goto end;
                    250:                }
                    251:                if (!reader->read_uint64(reader, &group))
                    252:                {
                    253:                        DBG1(DBG_TNC, "insufficient data for group id");
                    254:                        goto end;
                    255:                }
                    256:                if (!reader->read_data(reader, len - PTS_FILE_METADATA_SIZE, &filename))
                    257:                {
                    258:                        DBG1(DBG_TNC, "insufficient data for filename");
                    259:                        goto end;
                    260:                }
                    261: 
                    262:                entry = malloc_thing(pts_file_metadata_t);
                    263:                entry->type = type;
                    264:                entry->filesize = filesize;
                    265:                entry->created = created;
                    266:                entry->modified = modified;
                    267:                entry->accessed = accessed;
                    268:                entry->owner = owner;
                    269:                entry->group = group;
                    270:                entry->filename = strndup(filename.ptr, filename.len);
                    271: 
                    272:                this->metadata->add(this->metadata, entry);
                    273:        }
                    274:        status = SUCCESS;
                    275: 
                    276: end:
                    277:        reader->destroy(reader);
                    278:        return status;
                    279: }
                    280: 
                    281: METHOD(pa_tnc_attr_t, add_segment, void,
                    282:        private_tcg_pts_attr_file_meta_t *this, chunk_t segment)
                    283: {
                    284:        this->value = chunk_cat("mc", this->value, segment);
                    285: }
                    286: 
                    287: METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
                    288:        private_tcg_pts_attr_file_meta_t *this)
                    289: {
                    290:        ref_get(&this->ref);
                    291:        return &this->public.pa_tnc_attribute;
                    292: }
                    293: 
                    294: METHOD(pa_tnc_attr_t, destroy, void,
                    295:        private_tcg_pts_attr_file_meta_t *this)
                    296: {
                    297:        if (ref_put(&this->ref))
                    298:        {
                    299:                DESTROY_IF(this->metadata);
                    300:                free(this->value.ptr);
                    301:                free(this);
                    302:        }
                    303: }
                    304: 
                    305: METHOD(tcg_pts_attr_file_meta_t, get_metadata, pts_file_meta_t*,
                    306:        private_tcg_pts_attr_file_meta_t *this)
                    307: {
                    308:        return this->metadata;
                    309: }
                    310: 
                    311: /**
                    312:  * Described in header.
                    313:  */
                    314: pa_tnc_attr_t *tcg_pts_attr_unix_file_meta_create(pts_file_meta_t *metadata)
                    315: {
                    316:        private_tcg_pts_attr_file_meta_t *this;
                    317: 
                    318:        INIT(this,
                    319:                .public = {
                    320:                        .pa_tnc_attribute = {
                    321:                                .get_type = _get_type,
                    322:                                .get_value = _get_value,
                    323:                                .get_noskip_flag = _get_noskip_flag,
                    324:                                .set_noskip_flag = _set_noskip_flag,
                    325:                                .build = _build,
                    326:                                .process = _process,
                    327:                                .add_segment = _add_segment,
                    328:                                .get_ref = _get_ref,
                    329:                                .destroy = _destroy,
                    330:                        },
                    331:                        .get_metadata = _get_metadata,
                    332:                },
                    333:                .type = { PEN_TCG, TCG_PTS_UNIX_FILE_META },
                    334:                .metadata = metadata,
                    335:                .ref = 1,
                    336:        );
                    337: 
                    338:        return &this->public.pa_tnc_attribute;
                    339: }
                    340: 
                    341: 
                    342: /**
                    343:  * Described in header.
                    344:  */
                    345: pa_tnc_attr_t *tcg_pts_attr_unix_file_meta_create_from_data(size_t length,
                    346:                                                                                                                        chunk_t data)
                    347: {
                    348:        private_tcg_pts_attr_file_meta_t *this;
                    349: 
                    350:        INIT(this,
                    351:                .public = {
                    352:                        .pa_tnc_attribute = {
                    353:                                .get_type = _get_type,
                    354:                                .get_value = _get_value,
                    355:                                .get_noskip_flag = _get_noskip_flag,
                    356:                                .set_noskip_flag = _set_noskip_flag,
                    357:                                .build = _build,
                    358:                                .process = _process,
                    359:                                .add_segment = _add_segment,
                    360:                                .get_ref = _get_ref,
                    361:                                .destroy = _destroy,
                    362:                        },
                    363:                        .get_metadata = _get_metadata,
                    364:                },
                    365:                .type = { PEN_TCG, TCG_PTS_UNIX_FILE_META },
                    366:                .length = length,
                    367:                .value = chunk_clone(data),
                    368:                .ref = 1,
                    369:        );
                    370: 
                    371:        return &this->public.pa_tnc_attribute;
                    372: }

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